xref: /openbmc/linux/drivers/net/virtio_net.c (revision accc1bf23068c1cdc4c2b015320ba856e210dd98)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
248925e37SRusty Russell /* A network driver using virtio.
3296f96fcSRusty Russell  *
4296f96fcSRusty Russell  * Copyright 2007 Rusty Russell <rusty@rustcorp.com.au> IBM Corporation
5296f96fcSRusty Russell  */
6296f96fcSRusty Russell //#define DEBUG
7296f96fcSRusty Russell #include <linux/netdevice.h>
8296f96fcSRusty Russell #include <linux/etherdevice.h>
9a9ea3fc6SHerbert Xu #include <linux/ethtool.h>
10296f96fcSRusty Russell #include <linux/module.h>
11296f96fcSRusty Russell #include <linux/virtio.h>
12296f96fcSRusty Russell #include <linux/virtio_net.h>
13f600b690SJohn Fastabend #include <linux/bpf.h>
14a67edbf4SDaniel Borkmann #include <linux/bpf_trace.h>
15296f96fcSRusty Russell #include <linux/scatterlist.h>
16e918085aSAlex Williamson #include <linux/if_vlan.h>
175a0e3ad6STejun Heo #include <linux/slab.h>
188de4b2f3SWanlong Gao #include <linux/cpu.h>
19ab7db917SMichael Dalton #include <linux/average.h>
20186b3c99SJason Wang #include <linux/filter.h>
212ca653d6SCaleb Raitto #include <linux/kernel.h>
22d85b758fSMichael S. Tsirkin #include <net/route.h>
23754b8a21SJesper Dangaard Brouer #include <net/xdp.h>
24ba5e4426SSridhar Samudrala #include <net/net_failover.h>
25296f96fcSRusty Russell 
26d34710e3SAmerigo Wang static int napi_weight = NAPI_POLL_WEIGHT;
276c0cd7c0SDor Laor module_param(napi_weight, int, 0444);
286c0cd7c0SDor Laor 
2931c03aefSWillem de Bruijn static bool csum = true, gso = true, napi_tx = true;
3034a48579SRusty Russell module_param(csum, bool, 0444);
3134a48579SRusty Russell module_param(gso, bool, 0444);
32b92f1e67SWillem de Bruijn module_param(napi_tx, bool, 0644);
3334a48579SRusty Russell 
34296f96fcSRusty Russell /* FIXME: MTU in config. */
355061de36SMichael Dalton #define GOOD_PACKET_LEN (ETH_HLEN + VLAN_HLEN + ETH_DATA_LEN)
363f2c31d9SMark McLoughlin #define GOOD_COPY_LEN	128
37296f96fcSRusty Russell 
38f6b10209SJason Wang #define VIRTNET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
39f6b10209SJason Wang 
402de2f7f4SJohn Fastabend /* Amount of XDP headroom to prepend to packets for use by xdp_adjust_head */
412de2f7f4SJohn Fastabend #define VIRTIO_XDP_HEADROOM 256
422de2f7f4SJohn Fastabend 
432471c75eSJesper Dangaard Brouer /* Separating two types of XDP xmit */
442471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_TX		BIT(0)
452471c75eSJesper Dangaard Brouer #define VIRTIO_XDP_REDIR	BIT(1)
462471c75eSJesper Dangaard Brouer 
475050471dSToshiaki Makita #define VIRTIO_XDP_FLAG	BIT(0)
485050471dSToshiaki Makita 
495377d758SJohannes Berg /* RX packet size EWMA. The average packet size is used to determine the packet
505377d758SJohannes Berg  * buffer size when refilling RX rings. As the entire RX ring may be refilled
515377d758SJohannes Berg  * at once, the weight is chosen so that the EWMA will be insensitive to short-
525377d758SJohannes Berg  * term, transient changes in packet size.
53ab7db917SMichael Dalton  */
54eb1e011aSJohannes Berg DECLARE_EWMA(pkt_len, 0, 64)
55ab7db917SMichael Dalton 
5666846048SRick Jones #define VIRTNET_DRIVER_VERSION "1.0.0"
572a41f71dSAlex Williamson 
587acd4329SColin Ian King static const unsigned long guest_offloads[] = {
597acd4329SColin Ian King 	VIRTIO_NET_F_GUEST_TSO4,
603f93522fSJason Wang 	VIRTIO_NET_F_GUEST_TSO6,
613f93522fSJason Wang 	VIRTIO_NET_F_GUEST_ECN,
62e59ff2c4SJason Wang 	VIRTIO_NET_F_GUEST_UFO,
63418044e1SAndrew Melnychenko 	VIRTIO_NET_F_GUEST_CSUM,
64418044e1SAndrew Melnychenko 	VIRTIO_NET_F_GUEST_USO4,
65be50da3eSJiri Pirko 	VIRTIO_NET_F_GUEST_USO6,
66be50da3eSJiri Pirko 	VIRTIO_NET_F_GUEST_HDRLEN
677acd4329SColin Ian King };
683f93522fSJason Wang 
69dbcf24d1SJason Wang #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
701a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
711a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_ECN)  | \
72418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_UFO)  | \
73418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_USO4) | \
74418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_USO6))
751a03b8a3STonghao Zhang 
76d7dfc5cfSToshiaki Makita struct virtnet_stat_desc {
77d7dfc5cfSToshiaki Makita 	char desc[ETH_GSTRING_LEN];
78d7dfc5cfSToshiaki Makita 	size_t offset;
793fa2a1dfSstephen hemminger };
803fa2a1dfSstephen hemminger 
81d7dfc5cfSToshiaki Makita struct virtnet_sq_stats {
82d7dfc5cfSToshiaki Makita 	struct u64_stats_sync syncp;
83d7dfc5cfSToshiaki Makita 	u64 packets;
84d7dfc5cfSToshiaki Makita 	u64 bytes;
855b8f3c8dSToshiaki Makita 	u64 xdp_tx;
865b8f3c8dSToshiaki Makita 	u64 xdp_tx_drops;
87461f03dcSToshiaki Makita 	u64 kicks;
88a520794bSTony Lu 	u64 tx_timeouts;
89d7dfc5cfSToshiaki Makita };
90d7dfc5cfSToshiaki Makita 
91d46eeeafSJason Wang struct virtnet_rq_stats {
92d46eeeafSJason Wang 	struct u64_stats_sync syncp;
93d7dfc5cfSToshiaki Makita 	u64 packets;
94d7dfc5cfSToshiaki Makita 	u64 bytes;
952c4a2f7dSToshiaki Makita 	u64 drops;
965b8f3c8dSToshiaki Makita 	u64 xdp_packets;
975b8f3c8dSToshiaki Makita 	u64 xdp_tx;
985b8f3c8dSToshiaki Makita 	u64 xdp_redirects;
995b8f3c8dSToshiaki Makita 	u64 xdp_drops;
100461f03dcSToshiaki Makita 	u64 kicks;
101d7dfc5cfSToshiaki Makita };
102d7dfc5cfSToshiaki Makita 
103d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STAT(m)	offsetof(struct virtnet_sq_stats, m)
104d46eeeafSJason Wang #define VIRTNET_RQ_STAT(m)	offsetof(struct virtnet_rq_stats, m)
105d7dfc5cfSToshiaki Makita 
106d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
107d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_SQ_STAT(packets) },
108d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_SQ_STAT(bytes) },
1095b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_SQ_STAT(xdp_tx) },
1105b8f3c8dSToshiaki Makita 	{ "xdp_tx_drops",	VIRTNET_SQ_STAT(xdp_tx_drops) },
111461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_SQ_STAT(kicks) },
112a520794bSTony Lu 	{ "tx_timeouts",	VIRTNET_SQ_STAT(tx_timeouts) },
113d7dfc5cfSToshiaki Makita };
114d7dfc5cfSToshiaki Makita 
115d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
116d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_RQ_STAT(packets) },
117d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_RQ_STAT(bytes) },
1182c4a2f7dSToshiaki Makita 	{ "drops",		VIRTNET_RQ_STAT(drops) },
1195b8f3c8dSToshiaki Makita 	{ "xdp_packets",	VIRTNET_RQ_STAT(xdp_packets) },
1205b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_RQ_STAT(xdp_tx) },
1215b8f3c8dSToshiaki Makita 	{ "xdp_redirects",	VIRTNET_RQ_STAT(xdp_redirects) },
1225b8f3c8dSToshiaki Makita 	{ "xdp_drops",		VIRTNET_RQ_STAT(xdp_drops) },
123461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_RQ_STAT(kicks) },
124d7dfc5cfSToshiaki Makita };
125d7dfc5cfSToshiaki Makita 
126d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN	ARRAY_SIZE(virtnet_sq_stats_desc)
127d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN	ARRAY_SIZE(virtnet_rq_stats_desc)
128d7dfc5cfSToshiaki Makita 
129e9d7417bSJason Wang /* Internal representation of a send virtqueue */
130e9d7417bSJason Wang struct send_queue {
131e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
132e9d7417bSJason Wang 	struct virtqueue *vq;
133e9d7417bSJason Wang 
134e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
135e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
136986a4f4dSJason Wang 
137986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
138d0671115SParav Pandit 	char name[16];
139b92f1e67SWillem de Bruijn 
140d7dfc5cfSToshiaki Makita 	struct virtnet_sq_stats stats;
141d7dfc5cfSToshiaki Makita 
142b92f1e67SWillem de Bruijn 	struct napi_struct napi;
143ebcce492SXuan Zhuo 
144ebcce492SXuan Zhuo 	/* Record whether sq is in reset state. */
145ebcce492SXuan Zhuo 	bool reset;
146e9d7417bSJason Wang };
147e9d7417bSJason Wang 
148e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
149e9d7417bSJason Wang struct receive_queue {
150e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
151e9d7417bSJason Wang 	struct virtqueue *vq;
152e9d7417bSJason Wang 
153296f96fcSRusty Russell 	struct napi_struct napi;
154296f96fcSRusty Russell 
155f600b690SJohn Fastabend 	struct bpf_prog __rcu *xdp_prog;
156f600b690SJohn Fastabend 
157d7dfc5cfSToshiaki Makita 	struct virtnet_rq_stats stats;
158d7dfc5cfSToshiaki Makita 
159e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
160e9d7417bSJason Wang 	struct page *pages;
161e9d7417bSJason Wang 
162ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
1635377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
164ab7db917SMichael Dalton 
165fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
166fb51879dSMichael Dalton 	struct page_frag alloc_frag;
167fb51879dSMichael Dalton 
168e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
169e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
170986a4f4dSJason Wang 
171d85b758fSMichael S. Tsirkin 	/* Min single buffer size for mergeable buffers case. */
172d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len;
173d85b758fSMichael S. Tsirkin 
174986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
175d0671115SParav Pandit 	char name[16];
176754b8a21SJesper Dangaard Brouer 
177754b8a21SJesper Dangaard Brouer 	struct xdp_rxq_info xdp_rxq;
178e9d7417bSJason Wang };
179e9d7417bSJason Wang 
180c7114b12SAndrew Melnychenko /* This structure can contain rss message with maximum settings for indirection table and keysize
181c7114b12SAndrew Melnychenko  * Note, that default structure that describes RSS configuration virtio_net_rss_config
182c7114b12SAndrew Melnychenko  * contains same info but can't handle table values.
183c7114b12SAndrew Melnychenko  * In any case, structure would be passed to virtio hw through sg_buf split by parts
184c7114b12SAndrew Melnychenko  * because table sizes may be differ according to the device configuration.
185c7114b12SAndrew Melnychenko  */
186c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
187c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_TABLE_LEN    128
188c7114b12SAndrew Melnychenko struct virtio_net_ctrl_rss {
189c7114b12SAndrew Melnychenko 	u32 hash_types;
190c7114b12SAndrew Melnychenko 	u16 indirection_table_mask;
191c7114b12SAndrew Melnychenko 	u16 unclassified_queue;
192c7114b12SAndrew Melnychenko 	u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
193c7114b12SAndrew Melnychenko 	u16 max_tx_vq;
194c7114b12SAndrew Melnychenko 	u8 hash_key_length;
195c7114b12SAndrew Melnychenko 	u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
196c7114b12SAndrew Melnychenko };
197c7114b12SAndrew Melnychenko 
19812e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */
19912e57169SMichael S. Tsirkin struct control_buf {
20012e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr hdr;
20112e57169SMichael S. Tsirkin 	virtio_net_ctrl_ack status;
20212e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_mq mq;
20312e57169SMichael S. Tsirkin 	u8 promisc;
20412e57169SMichael S. Tsirkin 	u8 allmulti;
205d7fad4c8SMichael S. Tsirkin 	__virtio16 vid;
206f4ee703aSMichael S. Tsirkin 	__virtio64 offloads;
207c7114b12SAndrew Melnychenko 	struct virtio_net_ctrl_rss rss;
208*accc1bf2SBrett Creeley 	struct virtio_net_ctrl_coal_tx coal_tx;
209*accc1bf2SBrett Creeley 	struct virtio_net_ctrl_coal_rx coal_rx;
21012e57169SMichael S. Tsirkin };
21112e57169SMichael S. Tsirkin 
212e9d7417bSJason Wang struct virtnet_info {
213e9d7417bSJason Wang 	struct virtio_device *vdev;
214e9d7417bSJason Wang 	struct virtqueue *cvq;
215e9d7417bSJason Wang 	struct net_device *dev;
216986a4f4dSJason Wang 	struct send_queue *sq;
217986a4f4dSJason Wang 	struct receive_queue *rq;
218e9d7417bSJason Wang 	unsigned int status;
219e9d7417bSJason Wang 
220986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
221986a4f4dSJason Wang 	u16 max_queue_pairs;
222986a4f4dSJason Wang 
223986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
224986a4f4dSJason Wang 	u16 curr_queue_pairs;
225986a4f4dSJason Wang 
226672aafd5SJohn Fastabend 	/* # of XDP queue pairs currently used by the driver */
227672aafd5SJohn Fastabend 	u16 xdp_queue_pairs;
228672aafd5SJohn Fastabend 
22997c2c69eSXuan Zhuo 	/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
23097c2c69eSXuan Zhuo 	bool xdp_enabled;
23197c2c69eSXuan Zhuo 
23297402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
23397402b96SHerbert Xu 	bool big_packets;
23497402b96SHerbert Xu 
2354959aebbSGavin Li 	/* number of sg entries allocated for big packets */
2364959aebbSGavin Li 	unsigned int big_packets_num_skbfrags;
2374959aebbSGavin Li 
2383f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
2393f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
2403f2c31d9SMark McLoughlin 
241c7114b12SAndrew Melnychenko 	/* Host supports rss and/or hash report */
242c7114b12SAndrew Melnychenko 	bool has_rss;
24391f41f01SAndrew Melnychenko 	bool has_rss_hash_report;
244c7114b12SAndrew Melnychenko 	u8 rss_key_size;
245c7114b12SAndrew Melnychenko 	u16 rss_indir_table_size;
246c7114b12SAndrew Melnychenko 	u32 rss_hash_types_supported;
247c1170820SAndrew Melnychenko 	u32 rss_hash_types_saved;
248c7114b12SAndrew Melnychenko 
249986a4f4dSJason Wang 	/* Has control virtqueue */
250986a4f4dSJason Wang 	bool has_cvq;
251986a4f4dSJason Wang 
252e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
253e7428e95SMichael S. Tsirkin 	bool any_header_sg;
254e7428e95SMichael S. Tsirkin 
255012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
256012873d0SMichael S. Tsirkin 	u8 hdr_len;
257012873d0SMichael S. Tsirkin 
2585a159128SJason Wang 	/* Work struct for delayed refilling if we run low on memory. */
2593161e453SRusty Russell 	struct delayed_work refill;
2603161e453SRusty Russell 
2615a159128SJason Wang 	/* Is delayed refill enabled? */
2625a159128SJason Wang 	bool refill_enabled;
2635a159128SJason Wang 
2645a159128SJason Wang 	/* The lock to synchronize the access to refill_enabled */
2655a159128SJason Wang 	spinlock_t refill_lock;
2665a159128SJason Wang 
267586d17c5SJason Wang 	/* Work struct for config space updates */
268586d17c5SJason Wang 	struct work_struct config_work;
269586d17c5SJason Wang 
270986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
271986a4f4dSJason Wang 	bool affinity_hint_set;
27247be2479SWanlong Gao 
2738017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
2748017c279SSebastian Andrzej Siewior 	struct hlist_node node;
2758017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
2762ac46030SMichael S. Tsirkin 
27712e57169SMichael S. Tsirkin 	struct control_buf *ctrl;
27816032be5SNikolay Aleksandrov 
27916032be5SNikolay Aleksandrov 	/* Ethtool settings */
28016032be5SNikolay Aleksandrov 	u8 duplex;
28116032be5SNikolay Aleksandrov 	u32 speed;
2823f93522fSJason Wang 
283699b045aSAlvaro Karsz 	/* Interrupt coalescing settings */
284699b045aSAlvaro Karsz 	u32 tx_usecs;
285699b045aSAlvaro Karsz 	u32 rx_usecs;
286699b045aSAlvaro Karsz 	u32 tx_max_packets;
287699b045aSAlvaro Karsz 	u32 rx_max_packets;
288699b045aSAlvaro Karsz 
2893f93522fSJason Wang 	unsigned long guest_offloads;
290a02e8964SWillem de Bruijn 	unsigned long guest_offloads_capable;
291ba5e4426SSridhar Samudrala 
292ba5e4426SSridhar Samudrala 	/* failover when STANDBY feature enabled */
293ba5e4426SSridhar Samudrala 	struct failover *failover;
294296f96fcSRusty Russell };
295296f96fcSRusty Russell 
2969ab86bbcSShirley Ma struct padded_vnet_hdr {
297c1ddc42dSAndrew Melnychenko 	struct virtio_net_hdr_v1_hash hdr;
2989ab86bbcSShirley Ma 	/*
299012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
300012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
301012873d0SMichael S. Tsirkin 	 * after the header.
3029ab86bbcSShirley Ma 	 */
303c1ddc42dSAndrew Melnychenko 	char padding[12];
3049ab86bbcSShirley Ma };
3059ab86bbcSShirley Ma 
3066a4763e2SXuan Zhuo static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
307ebcce492SXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
3086a4763e2SXuan Zhuo 
3095050471dSToshiaki Makita static bool is_xdp_frame(void *ptr)
3105050471dSToshiaki Makita {
3115050471dSToshiaki Makita 	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
3125050471dSToshiaki Makita }
3135050471dSToshiaki Makita 
3145050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr)
3155050471dSToshiaki Makita {
3165050471dSToshiaki Makita 	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
3175050471dSToshiaki Makita }
3185050471dSToshiaki Makita 
3195050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr)
3205050471dSToshiaki Makita {
3215050471dSToshiaki Makita 	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
3225050471dSToshiaki Makita }
3235050471dSToshiaki Makita 
324986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
325986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
326986a4f4dSJason Wang  */
327986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
328986a4f4dSJason Wang {
3299d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
330986a4f4dSJason Wang }
331986a4f4dSJason Wang 
332986a4f4dSJason Wang static int txq2vq(int txq)
333986a4f4dSJason Wang {
334986a4f4dSJason Wang 	return txq * 2 + 1;
335986a4f4dSJason Wang }
336986a4f4dSJason Wang 
337986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
338986a4f4dSJason Wang {
3399d0ca6edSRusty Russell 	return vq->index / 2;
340986a4f4dSJason Wang }
341986a4f4dSJason Wang 
342986a4f4dSJason Wang static int rxq2vq(int rxq)
343986a4f4dSJason Wang {
344986a4f4dSJason Wang 	return rxq * 2;
345986a4f4dSJason Wang }
346986a4f4dSJason Wang 
347012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
348296f96fcSRusty Russell {
349012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
350296f96fcSRusty Russell }
351296f96fcSRusty Russell 
3529ab86bbcSShirley Ma /*
3539ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
3549ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
3559ab86bbcSShirley Ma  */
356e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
357fb6813f4SRusty Russell {
3589ab86bbcSShirley Ma 	struct page *end;
3599ab86bbcSShirley Ma 
360e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
3619ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
362e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
363e9d7417bSJason Wang 	rq->pages = page;
364fb6813f4SRusty Russell }
365fb6813f4SRusty Russell 
366e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
367fb6813f4SRusty Russell {
368e9d7417bSJason Wang 	struct page *p = rq->pages;
369fb6813f4SRusty Russell 
3709ab86bbcSShirley Ma 	if (p) {
371e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
3729ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
3739ab86bbcSShirley Ma 		p->private = 0;
3749ab86bbcSShirley Ma 	} else
375fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
376fb6813f4SRusty Russell 	return p;
377fb6813f4SRusty Russell }
378fb6813f4SRusty Russell 
3795a159128SJason Wang static void enable_delayed_refill(struct virtnet_info *vi)
3805a159128SJason Wang {
3815a159128SJason Wang 	spin_lock_bh(&vi->refill_lock);
3825a159128SJason Wang 	vi->refill_enabled = true;
3835a159128SJason Wang 	spin_unlock_bh(&vi->refill_lock);
3845a159128SJason Wang }
3855a159128SJason Wang 
3865a159128SJason Wang static void disable_delayed_refill(struct virtnet_info *vi)
3875a159128SJason Wang {
3885a159128SJason Wang 	spin_lock_bh(&vi->refill_lock);
3895a159128SJason Wang 	vi->refill_enabled = false;
3905a159128SJason Wang 	spin_unlock_bh(&vi->refill_lock);
3915a159128SJason Wang }
3925a159128SJason Wang 
393e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi,
394e4e8452aSWillem de Bruijn 				    struct virtqueue *vq)
395e4e8452aSWillem de Bruijn {
396e4e8452aSWillem de Bruijn 	if (napi_schedule_prep(napi)) {
397e4e8452aSWillem de Bruijn 		virtqueue_disable_cb(vq);
398e4e8452aSWillem de Bruijn 		__napi_schedule(napi);
399e4e8452aSWillem de Bruijn 	}
400e4e8452aSWillem de Bruijn }
401e4e8452aSWillem de Bruijn 
402e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi,
403e4e8452aSWillem de Bruijn 				    struct virtqueue *vq, int processed)
404e4e8452aSWillem de Bruijn {
405e4e8452aSWillem de Bruijn 	int opaque;
406e4e8452aSWillem de Bruijn 
407e4e8452aSWillem de Bruijn 	opaque = virtqueue_enable_cb_prepare(vq);
408fdaa767aSToshiaki Makita 	if (napi_complete_done(napi, processed)) {
409fdaa767aSToshiaki Makita 		if (unlikely(virtqueue_poll(vq, opaque)))
410e4e8452aSWillem de Bruijn 			virtqueue_napi_schedule(napi, vq);
411fdaa767aSToshiaki Makita 	} else {
412fdaa767aSToshiaki Makita 		virtqueue_disable_cb(vq);
413fdaa767aSToshiaki Makita 	}
414e4e8452aSWillem de Bruijn }
415e4e8452aSWillem de Bruijn 
416e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
417296f96fcSRusty Russell {
418e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
419b92f1e67SWillem de Bruijn 	struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
420296f96fcSRusty Russell 
4212cb9c6baSRusty Russell 	/* Suppress further interrupts. */
422e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
42311a3a154SRusty Russell 
424b92f1e67SWillem de Bruijn 	if (napi->weight)
425b92f1e67SWillem de Bruijn 		virtqueue_napi_schedule(napi, vq);
426b92f1e67SWillem de Bruijn 	else
427363f1514SRusty Russell 		/* We were probably waiting for more output buffers. */
428986a4f4dSJason Wang 		netif_wake_subqueue(vi->dev, vq2txq(vq));
429296f96fcSRusty Russell }
430296f96fcSRusty Russell 
43128b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22
43228b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize,
43328b39bc7SJason Wang 				  unsigned int headroom)
43428b39bc7SJason Wang {
43528b39bc7SJason Wang 	return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
43628b39bc7SJason Wang }
43728b39bc7SJason Wang 
43828b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
43928b39bc7SJason Wang {
44028b39bc7SJason Wang 	return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
44128b39bc7SJason Wang }
44228b39bc7SJason Wang 
44328b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
44428b39bc7SJason Wang {
44528b39bc7SJason Wang 	return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
44628b39bc7SJason Wang }
44728b39bc7SJason Wang 
4483464645aSMike Waychison /* Called from bottom half context */
449946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
450946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
4512613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
452fa0f1ba7SXuan Zhuo 				   unsigned int len, unsigned int truesize,
453fa0f1ba7SXuan Zhuo 				   unsigned int headroom)
4549ab86bbcSShirley Ma {
4559ab86bbcSShirley Ma 	struct sk_buff *skb;
456012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
4572613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
458af39c8f7SEric Dumazet 	struct page *page_to_free = NULL;
459fb32856bSXuan Zhuo 	int tailroom, shinfo_size;
460f80bd740SXuan Zhuo 	char *p, *hdr_p, *buf;
4619ab86bbcSShirley Ma 
4622613af0eSMichael Dalton 	p = page_address(page) + offset;
463fb32856bSXuan Zhuo 	hdr_p = p;
4649ab86bbcSShirley Ma 
465012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
466012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
467c1ddc42dSAndrew Melnychenko 		hdr_padded_len = hdr_len;
468012873d0SMichael S. Tsirkin 	else
4692613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
4703f2c31d9SMark McLoughlin 
471fa0f1ba7SXuan Zhuo 	buf = p - headroom;
4729ab86bbcSShirley Ma 	len -= hdr_len;
4732613af0eSMichael Dalton 	offset += hdr_padded_len;
4742613af0eSMichael Dalton 	p += hdr_padded_len;
475fa0f1ba7SXuan Zhuo 	tailroom = truesize - headroom  - hdr_padded_len - len;
4763f2c31d9SMark McLoughlin 
477fb32856bSXuan Zhuo 	shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
478fb32856bSXuan Zhuo 
479f80bd740SXuan Zhuo 	/* copy small packet so we can reuse these pages */
480f5d7872aSEric Dumazet 	if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
481f80bd740SXuan Zhuo 		skb = build_skb(buf, truesize);
482fb32856bSXuan Zhuo 		if (unlikely(!skb))
483fb32856bSXuan Zhuo 			return NULL;
484fb32856bSXuan Zhuo 
485f80bd740SXuan Zhuo 		skb_reserve(skb, p - buf);
486fb32856bSXuan Zhuo 		skb_put(skb, len);
487afd92d82SJason Wang 
488afd92d82SJason Wang 		page = (struct page *)page->private;
489afd92d82SJason Wang 		if (page)
490afd92d82SJason Wang 			give_pages(rq, page);
491fb32856bSXuan Zhuo 		goto ok;
492fb32856bSXuan Zhuo 	}
493fb32856bSXuan Zhuo 
494fb32856bSXuan Zhuo 	/* copy small packet so we can reuse these pages for small data */
495fb32856bSXuan Zhuo 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
496fb32856bSXuan Zhuo 	if (unlikely(!skb))
497fb32856bSXuan Zhuo 		return NULL;
498fb32856bSXuan Zhuo 
4990f6925b3SEric Dumazet 	/* Copy all frame if it fits skb->head, otherwise
5000f6925b3SEric Dumazet 	 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
5010f6925b3SEric Dumazet 	 */
5020f6925b3SEric Dumazet 	if (len <= skb_tailroom(skb))
5033f2c31d9SMark McLoughlin 		copy = len;
5040f6925b3SEric Dumazet 	else
50518117a84SHeng Qi 		copy = ETH_HLEN;
50659ae1d12SJohannes Berg 	skb_put_data(skb, p, copy);
5073f2c31d9SMark McLoughlin 
5083f2c31d9SMark McLoughlin 	len -= copy;
5099ab86bbcSShirley Ma 	offset += copy;
5103f2c31d9SMark McLoughlin 
5112613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
5122613af0eSMichael Dalton 		if (len)
5132613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
5142613af0eSMichael Dalton 		else
515af39c8f7SEric Dumazet 			page_to_free = page;
516fb32856bSXuan Zhuo 		goto ok;
5172613af0eSMichael Dalton 	}
5182613af0eSMichael Dalton 
519e878d78bSSasha Levin 	/*
520e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
521e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
522e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
523e878d78bSSasha Levin 	 * the case of a broken device.
524e878d78bSSasha Levin 	 */
525e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
526be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
527e878d78bSSasha Levin 		dev_kfree_skb(skb);
528e878d78bSSasha Levin 		return NULL;
529e878d78bSSasha Levin 	}
5302613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
5319ab86bbcSShirley Ma 	while (len) {
5322613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
5332613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
5342613af0eSMichael Dalton 				frag_size, truesize);
5352613af0eSMichael Dalton 		len -= frag_size;
5369ab86bbcSShirley Ma 		page = (struct page *)page->private;
5379ab86bbcSShirley Ma 		offset = 0;
5383f2c31d9SMark McLoughlin 	}
5393f2c31d9SMark McLoughlin 
5409ab86bbcSShirley Ma 	if (page)
541e9d7417bSJason Wang 		give_pages(rq, page);
5423f2c31d9SMark McLoughlin 
543fb32856bSXuan Zhuo ok:
544fb32856bSXuan Zhuo 	hdr = skb_vnet_hdr(skb);
545fb32856bSXuan Zhuo 	memcpy(hdr, hdr_p, hdr_len);
546af39c8f7SEric Dumazet 	if (page_to_free)
547af39c8f7SEric Dumazet 		put_page(page_to_free);
548fb32856bSXuan Zhuo 
5499ab86bbcSShirley Ma 	return skb;
5509ab86bbcSShirley Ma }
5519ab86bbcSShirley Ma 
55225074a44SXuan Zhuo static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
55325074a44SXuan Zhuo {
55425074a44SXuan Zhuo 	unsigned int len;
55525074a44SXuan Zhuo 	unsigned int packets = 0;
55625074a44SXuan Zhuo 	unsigned int bytes = 0;
55725074a44SXuan Zhuo 	void *ptr;
55825074a44SXuan Zhuo 
55925074a44SXuan Zhuo 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
56025074a44SXuan Zhuo 		if (likely(!is_xdp_frame(ptr))) {
56125074a44SXuan Zhuo 			struct sk_buff *skb = ptr;
56225074a44SXuan Zhuo 
56325074a44SXuan Zhuo 			pr_debug("Sent skb %p\n", skb);
56425074a44SXuan Zhuo 
56525074a44SXuan Zhuo 			bytes += skb->len;
56625074a44SXuan Zhuo 			napi_consume_skb(skb, in_napi);
56725074a44SXuan Zhuo 		} else {
56825074a44SXuan Zhuo 			struct xdp_frame *frame = ptr_to_xdp(ptr);
56925074a44SXuan Zhuo 
57025074a44SXuan Zhuo 			bytes += xdp_get_frame_len(frame);
57125074a44SXuan Zhuo 			xdp_return_frame(frame);
57225074a44SXuan Zhuo 		}
57325074a44SXuan Zhuo 		packets++;
57425074a44SXuan Zhuo 	}
57525074a44SXuan Zhuo 
57625074a44SXuan Zhuo 	/* Avoid overhead when no packets have been processed
57725074a44SXuan Zhuo 	 * happens when called speculatively from start_xmit.
57825074a44SXuan Zhuo 	 */
57925074a44SXuan Zhuo 	if (!packets)
58025074a44SXuan Zhuo 		return;
58125074a44SXuan Zhuo 
58225074a44SXuan Zhuo 	u64_stats_update_begin(&sq->stats.syncp);
58325074a44SXuan Zhuo 	sq->stats.bytes += bytes;
58425074a44SXuan Zhuo 	sq->stats.packets += packets;
58525074a44SXuan Zhuo 	u64_stats_update_end(&sq->stats.syncp);
58625074a44SXuan Zhuo }
58725074a44SXuan Zhuo 
58825074a44SXuan Zhuo static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
58925074a44SXuan Zhuo {
59025074a44SXuan Zhuo 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
59125074a44SXuan Zhuo 		return false;
59225074a44SXuan Zhuo 	else if (q < vi->curr_queue_pairs)
59325074a44SXuan Zhuo 		return true;
59425074a44SXuan Zhuo 	else
59525074a44SXuan Zhuo 		return false;
59625074a44SXuan Zhuo }
59725074a44SXuan Zhuo 
598b8ef4809SXuan Zhuo static void check_sq_full_and_disable(struct virtnet_info *vi,
599b8ef4809SXuan Zhuo 				      struct net_device *dev,
600b8ef4809SXuan Zhuo 				      struct send_queue *sq)
601b8ef4809SXuan Zhuo {
602b8ef4809SXuan Zhuo 	bool use_napi = sq->napi.weight;
603b8ef4809SXuan Zhuo 	int qnum;
604b8ef4809SXuan Zhuo 
605b8ef4809SXuan Zhuo 	qnum = sq - vi->sq;
606b8ef4809SXuan Zhuo 
607b8ef4809SXuan Zhuo 	/* If running out of space, stop queue to avoid getting packets that we
608b8ef4809SXuan Zhuo 	 * are then unable to transmit.
609b8ef4809SXuan Zhuo 	 * An alternative would be to force queuing layer to requeue the skb by
610b8ef4809SXuan Zhuo 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
611b8ef4809SXuan Zhuo 	 * returned in a normal path of operation: it means that driver is not
612b8ef4809SXuan Zhuo 	 * maintaining the TX queue stop/start state properly, and causes
613b8ef4809SXuan Zhuo 	 * the stack to do a non-trivial amount of useless work.
614b8ef4809SXuan Zhuo 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
615b8ef4809SXuan Zhuo 	 * early means 16 slots are typically wasted.
616b8ef4809SXuan Zhuo 	 */
617b8ef4809SXuan Zhuo 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
618b8ef4809SXuan Zhuo 		netif_stop_subqueue(dev, qnum);
619b8ef4809SXuan Zhuo 		if (use_napi) {
620b8ef4809SXuan Zhuo 			if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
621b8ef4809SXuan Zhuo 				virtqueue_napi_schedule(&sq->napi, sq->vq);
622b8ef4809SXuan Zhuo 		} else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
623b8ef4809SXuan Zhuo 			/* More just got used, free them then recheck. */
624b8ef4809SXuan Zhuo 			free_old_xmit_skbs(sq, false);
625b8ef4809SXuan Zhuo 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
626b8ef4809SXuan Zhuo 				netif_start_subqueue(dev, qnum);
627b8ef4809SXuan Zhuo 				virtqueue_disable_cb(sq->vq);
628b8ef4809SXuan Zhuo 			}
629b8ef4809SXuan Zhuo 		}
630b8ef4809SXuan Zhuo 	}
631b8ef4809SXuan Zhuo }
632b8ef4809SXuan Zhuo 
633735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
634735fc405SJesper Dangaard Brouer 				   struct send_queue *sq,
63544fa2dbdSJesper Dangaard Brouer 				   struct xdp_frame *xdpf)
63656434a01SJohn Fastabend {
63756434a01SJohn Fastabend 	struct virtio_net_hdr_mrg_rxbuf *hdr;
63897717e8dSHeng Qi 	struct skb_shared_info *shinfo;
63997717e8dSHeng Qi 	u8 nr_frags = 0;
64097717e8dSHeng Qi 	int err, i;
64156434a01SJohn Fastabend 
642cac320c8SJesper Dangaard Brouer 	if (unlikely(xdpf->headroom < vi->hdr_len))
643cac320c8SJesper Dangaard Brouer 		return -EOVERFLOW;
644cac320c8SJesper Dangaard Brouer 
64597717e8dSHeng Qi 	if (unlikely(xdp_frame_has_frags(xdpf))) {
64697717e8dSHeng Qi 		shinfo = xdp_get_shared_info_from_frame(xdpf);
64797717e8dSHeng Qi 		nr_frags = shinfo->nr_frags;
64897717e8dSHeng Qi 	}
64997717e8dSHeng Qi 
65097717e8dSHeng Qi 	/* In wrapping function virtnet_xdp_xmit(), we need to free
65197717e8dSHeng Qi 	 * up the pending old buffers, where we need to calculate the
65297717e8dSHeng Qi 	 * position of skb_shared_info in xdp_get_frame_len() and
65397717e8dSHeng Qi 	 * xdp_return_frame(), which will involve to xdpf->data and
65497717e8dSHeng Qi 	 * xdpf->headroom. Therefore, we need to update the value of
65597717e8dSHeng Qi 	 * headroom synchronously here.
65697717e8dSHeng Qi 	 */
65797717e8dSHeng Qi 	xdpf->headroom -= vi->hdr_len;
658cac320c8SJesper Dangaard Brouer 	xdpf->data -= vi->hdr_len;
65956434a01SJohn Fastabend 	/* Zero header and leave csum up to XDP layers */
660cac320c8SJesper Dangaard Brouer 	hdr = xdpf->data;
66156434a01SJohn Fastabend 	memset(hdr, 0, vi->hdr_len);
662cac320c8SJesper Dangaard Brouer 	xdpf->len   += vi->hdr_len;
66356434a01SJohn Fastabend 
66497717e8dSHeng Qi 	sg_init_table(sq->sg, nr_frags + 1);
66597717e8dSHeng Qi 	sg_set_buf(sq->sg, xdpf->data, xdpf->len);
66697717e8dSHeng Qi 	for (i = 0; i < nr_frags; i++) {
66797717e8dSHeng Qi 		skb_frag_t *frag = &shinfo->frags[i];
668bb91accfSJason Wang 
66997717e8dSHeng Qi 		sg_set_page(&sq->sg[i + 1], skb_frag_page(frag),
67097717e8dSHeng Qi 			    skb_frag_size(frag), skb_frag_off(frag));
67197717e8dSHeng Qi 	}
67297717e8dSHeng Qi 
67397717e8dSHeng Qi 	err = virtqueue_add_outbuf(sq->vq, sq->sg, nr_frags + 1,
67497717e8dSHeng Qi 				   xdp_to_ptr(xdpf), GFP_ATOMIC);
67511b7d897SJesper Dangaard Brouer 	if (unlikely(err))
676cac320c8SJesper Dangaard Brouer 		return -ENOSPC; /* Caller handle free/refcnt */
67756434a01SJohn Fastabend 
678cac320c8SJesper Dangaard Brouer 	return 0;
67956434a01SJohn Fastabend }
68056434a01SJohn Fastabend 
68197c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
68297c2c69eSXuan Zhuo  * the current cpu, so it does not need to be locked.
68397c2c69eSXuan Zhuo  *
68497c2c69eSXuan Zhuo  * Here we use marco instead of inline functions because we have to deal with
68597c2c69eSXuan Zhuo  * three issues at the same time: 1. the choice of sq. 2. judge and execute the
68697c2c69eSXuan Zhuo  * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
68797c2c69eSXuan Zhuo  * functions to perfectly solve these three problems at the same time.
68897c2c69eSXuan Zhuo  */
68997c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({                                       \
6903dcc1edcSLi RongQing 	int cpu = smp_processor_id();                                   \
69197c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
69297c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
69397c2c69eSXuan Zhuo 	unsigned int qp;                                                \
69497c2c69eSXuan Zhuo 									\
69597c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
69697c2c69eSXuan Zhuo 		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
6973dcc1edcSLi RongQing 		qp += cpu;                                              \
69897c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
69997c2c69eSXuan Zhuo 		__netif_tx_acquire(txq);                                \
70097c2c69eSXuan Zhuo 	} else {                                                        \
7013dcc1edcSLi RongQing 		qp = cpu % v->curr_queue_pairs;                         \
70297c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
7033dcc1edcSLi RongQing 		__netif_tx_lock(txq, cpu);                              \
70497c2c69eSXuan Zhuo 	}                                                               \
70597c2c69eSXuan Zhuo 	v->sq + qp;                                                     \
70697c2c69eSXuan Zhuo })
7072a43565cSToshiaki Makita 
70897c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) {                                     \
70997c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
71097c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
71197c2c69eSXuan Zhuo 									\
71297c2c69eSXuan Zhuo 	txq = netdev_get_tx_queue(v->dev, (q) - v->sq);                 \
71397c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids)                           \
71497c2c69eSXuan Zhuo 		__netif_tx_release(txq);                                \
71597c2c69eSXuan Zhuo 	else                                                            \
71697c2c69eSXuan Zhuo 		__netif_tx_unlock(txq);                                 \
7172a43565cSToshiaki Makita }
7182a43565cSToshiaki Makita 
719735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev,
72042b33468SJesper Dangaard Brouer 			    int n, struct xdp_frame **frames, u32 flags)
721186b3c99SJason Wang {
722186b3c99SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
7238dcc5b0aSJesper Dangaard Brouer 	struct receive_queue *rq = vi->rq;
7248dcc5b0aSJesper Dangaard Brouer 	struct bpf_prog *xdp_prog;
725735fc405SJesper Dangaard Brouer 	struct send_queue *sq;
726735fc405SJesper Dangaard Brouer 	unsigned int len;
727546f2897SToshiaki Makita 	int packets = 0;
728546f2897SToshiaki Makita 	int bytes = 0;
729fdc13979SLorenzo Bianconi 	int nxmit = 0;
730461f03dcSToshiaki Makita 	int kicks = 0;
7315050471dSToshiaki Makita 	void *ptr;
732fdc13979SLorenzo Bianconi 	int ret;
733735fc405SJesper Dangaard Brouer 	int i;
734735fc405SJesper Dangaard Brouer 
7358dcc5b0aSJesper Dangaard Brouer 	/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
7368dcc5b0aSJesper Dangaard Brouer 	 * indicate XDP resources have been successfully allocated.
7378dcc5b0aSJesper Dangaard Brouer 	 */
7389719c6b9SJohn Fastabend 	xdp_prog = rcu_access_pointer(rq->xdp_prog);
7391667c08aSToshiaki Makita 	if (!xdp_prog)
7401667c08aSToshiaki Makita 		return -ENXIO;
7411667c08aSToshiaki Makita 
74297c2c69eSXuan Zhuo 	sq = virtnet_xdp_get_sq(vi);
7439ab86bbcSShirley Ma 
7449ab86bbcSShirley Ma 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
745186b3c99SJason Wang 		ret = -EINVAL;
746186b3c99SJason Wang 		goto out;
747186b3c99SJason Wang 	}
748186b3c99SJason Wang 
749735fc405SJesper Dangaard Brouer 	/* Free up any pending old buffers before queueing new ones. */
7505050471dSToshiaki Makita 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
751546f2897SToshiaki Makita 		if (likely(is_xdp_frame(ptr))) {
752546f2897SToshiaki Makita 			struct xdp_frame *frame = ptr_to_xdp(ptr);
753546f2897SToshiaki Makita 
75450bd14bcSHeng Qi 			bytes += xdp_get_frame_len(frame);
755546f2897SToshiaki Makita 			xdp_return_frame(frame);
756546f2897SToshiaki Makita 		} else {
757546f2897SToshiaki Makita 			struct sk_buff *skb = ptr;
758546f2897SToshiaki Makita 
759546f2897SToshiaki Makita 			bytes += skb->len;
760546f2897SToshiaki Makita 			napi_consume_skb(skb, false);
761546f2897SToshiaki Makita 		}
762546f2897SToshiaki Makita 		packets++;
7635050471dSToshiaki Makita 	}
764735fc405SJesper Dangaard Brouer 
765735fc405SJesper Dangaard Brouer 	for (i = 0; i < n; i++) {
766735fc405SJesper Dangaard Brouer 		struct xdp_frame *xdpf = frames[i];
767735fc405SJesper Dangaard Brouer 
768fdc13979SLorenzo Bianconi 		if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
769fdc13979SLorenzo Bianconi 			break;
770fdc13979SLorenzo Bianconi 		nxmit++;
771735fc405SJesper Dangaard Brouer 	}
772fdc13979SLorenzo Bianconi 	ret = nxmit;
7735d274cb4SJesper Dangaard Brouer 
774cd1c604aSXuan Zhuo 	if (!is_xdp_raw_buffer_queue(vi, sq - vi->sq))
775cd1c604aSXuan Zhuo 		check_sq_full_and_disable(vi, dev, sq);
776cd1c604aSXuan Zhuo 
777461f03dcSToshiaki Makita 	if (flags & XDP_XMIT_FLUSH) {
778461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
779461f03dcSToshiaki Makita 			kicks = 1;
780461f03dcSToshiaki Makita 	}
7815b8f3c8dSToshiaki Makita out:
7825b8f3c8dSToshiaki Makita 	u64_stats_update_begin(&sq->stats.syncp);
783546f2897SToshiaki Makita 	sq->stats.bytes += bytes;
784546f2897SToshiaki Makita 	sq->stats.packets += packets;
7855b8f3c8dSToshiaki Makita 	sq->stats.xdp_tx += n;
786fdc13979SLorenzo Bianconi 	sq->stats.xdp_tx_drops += n - nxmit;
787461f03dcSToshiaki Makita 	sq->stats.kicks += kicks;
7885b8f3c8dSToshiaki Makita 	u64_stats_update_end(&sq->stats.syncp);
7895d274cb4SJesper Dangaard Brouer 
79097c2c69eSXuan Zhuo 	virtnet_xdp_put_sq(vi, sq);
7915b8f3c8dSToshiaki Makita 	return ret;
792186b3c99SJason Wang }
793186b3c99SJason Wang 
794f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
795f6b10209SJason Wang {
79697c2c69eSXuan Zhuo 	return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
797f6b10209SJason Wang }
798f6b10209SJason Wang 
7994941d472SJason Wang /* We copy the packet for XDP in the following cases:
8004941d472SJason Wang  *
8014941d472SJason Wang  * 1) Packet is scattered across multiple rx buffers.
8024941d472SJason Wang  * 2) Headroom space is insufficient.
8034941d472SJason Wang  *
8044941d472SJason Wang  * This is inefficient but it's a temporary condition that
8054941d472SJason Wang  * we hit right after XDP is enabled and until queue is refilled
8064941d472SJason Wang  * with large buffers with sufficient headroom - so it should affect
8074941d472SJason Wang  * at most queue size packets.
8084941d472SJason Wang  * Afterwards, the conditions to enable
8094941d472SJason Wang  * XDP should preclude the underlying device from sending packets
8104941d472SJason Wang  * across multiple buffers (num_buf > 1), and we make sure buffers
8114941d472SJason Wang  * have enough headroom.
81272979a6cSJohn Fastabend  */
81372979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq,
814981f14d4SHeng Qi 				       int *num_buf,
81572979a6cSJohn Fastabend 				       struct page *p,
81672979a6cSJohn Fastabend 				       int offset,
8174941d472SJason Wang 				       int page_off,
81872979a6cSJohn Fastabend 				       unsigned int *len)
81972979a6cSJohn Fastabend {
820853618d5SXuan Zhuo 	int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
821853618d5SXuan Zhuo 	struct page *page;
82272979a6cSJohn Fastabend 
823853618d5SXuan Zhuo 	if (page_off + *len + tailroom > PAGE_SIZE)
824853618d5SXuan Zhuo 		return NULL;
825853618d5SXuan Zhuo 
826853618d5SXuan Zhuo 	page = alloc_page(GFP_ATOMIC);
82772979a6cSJohn Fastabend 	if (!page)
82872979a6cSJohn Fastabend 		return NULL;
82972979a6cSJohn Fastabend 
83072979a6cSJohn Fastabend 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
83172979a6cSJohn Fastabend 	page_off += *len;
83272979a6cSJohn Fastabend 
83356a86f84SJason Wang 	while (--*num_buf) {
83472979a6cSJohn Fastabend 		unsigned int buflen;
83572979a6cSJohn Fastabend 		void *buf;
83672979a6cSJohn Fastabend 		int off;
83772979a6cSJohn Fastabend 
838680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &buflen);
839680557cfSMichael S. Tsirkin 		if (unlikely(!buf))
84072979a6cSJohn Fastabend 			goto err_buf;
84172979a6cSJohn Fastabend 
84272979a6cSJohn Fastabend 		p = virt_to_head_page(buf);
84372979a6cSJohn Fastabend 		off = buf - page_address(p);
84472979a6cSJohn Fastabend 
84556a86f84SJason Wang 		/* guard against a misconfigured or uncooperative backend that
84656a86f84SJason Wang 		 * is sending packet larger than the MTU.
84756a86f84SJason Wang 		 */
8483cc81a9aSJason Wang 		if ((page_off + buflen + tailroom) > PAGE_SIZE) {
84956a86f84SJason Wang 			put_page(p);
85056a86f84SJason Wang 			goto err_buf;
85156a86f84SJason Wang 		}
85256a86f84SJason Wang 
85372979a6cSJohn Fastabend 		memcpy(page_address(page) + page_off,
85472979a6cSJohn Fastabend 		       page_address(p) + off, buflen);
85572979a6cSJohn Fastabend 		page_off += buflen;
85656a86f84SJason Wang 		put_page(p);
85772979a6cSJohn Fastabend 	}
85872979a6cSJohn Fastabend 
8592de2f7f4SJohn Fastabend 	/* Headroom does not contribute to packet length */
8602de2f7f4SJohn Fastabend 	*len = page_off - VIRTIO_XDP_HEADROOM;
86172979a6cSJohn Fastabend 	return page;
86272979a6cSJohn Fastabend err_buf:
86372979a6cSJohn Fastabend 	__free_pages(page, 0);
86472979a6cSJohn Fastabend 	return NULL;
86572979a6cSJohn Fastabend }
86672979a6cSJohn Fastabend 
8674941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev,
8684941d472SJason Wang 				     struct virtnet_info *vi,
8694941d472SJason Wang 				     struct receive_queue *rq,
8704941d472SJason Wang 				     void *buf, void *ctx,
871186b3c99SJason Wang 				     unsigned int len,
8727d9d60fdSToshiaki Makita 				     unsigned int *xdp_xmit,
873d46eeeafSJason Wang 				     struct virtnet_rq_stats *stats)
8744941d472SJason Wang {
8754941d472SJason Wang 	struct sk_buff *skb;
8764941d472SJason Wang 	struct bpf_prog *xdp_prog;
8774941d472SJason Wang 	unsigned int xdp_headroom = (unsigned long)ctx;
8784941d472SJason Wang 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
8794941d472SJason Wang 	unsigned int headroom = vi->hdr_len + header_offset;
8804941d472SJason Wang 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
8814941d472SJason Wang 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
8824941d472SJason Wang 	struct page *page = virt_to_head_page(buf);
88311b7d897SJesper Dangaard Brouer 	unsigned int delta = 0;
8844941d472SJason Wang 	struct page *xdp_page;
88511b7d897SJesper Dangaard Brouer 	int err;
886503d539aSYuya Kusakabe 	unsigned int metasize = 0;
88711b7d897SJesper Dangaard Brouer 
8884941d472SJason Wang 	len -= vi->hdr_len;
889d46eeeafSJason Wang 	stats->bytes += len;
8904941d472SJason Wang 
891ad993a95SXie Yongji 	if (unlikely(len > GOOD_PACKET_LEN)) {
892ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds max size %d\n",
893ad993a95SXie Yongji 			 dev->name, len, GOOD_PACKET_LEN);
894ad993a95SXie Yongji 		dev->stats.rx_length_errors++;
895053c9e18SWenliang Wang 		goto err;
896ad993a95SXie Yongji 	}
8976213f07cSLi RongQing 
8986213f07cSLi RongQing 	if (likely(!vi->xdp_enabled)) {
8996213f07cSLi RongQing 		xdp_prog = NULL;
9006213f07cSLi RongQing 		goto skip_xdp;
9016213f07cSLi RongQing 	}
9026213f07cSLi RongQing 
9034941d472SJason Wang 	rcu_read_lock();
9044941d472SJason Wang 	xdp_prog = rcu_dereference(rq->xdp_prog);
9054941d472SJason Wang 	if (xdp_prog) {
9064941d472SJason Wang 		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
90744fa2dbdSJesper Dangaard Brouer 		struct xdp_frame *xdpf;
9084941d472SJason Wang 		struct xdp_buff xdp;
9094941d472SJason Wang 		void *orig_data;
9104941d472SJason Wang 		u32 act;
9114941d472SJason Wang 
91295dbe9e7SJesper Dangaard Brouer 		if (unlikely(hdr->hdr.gso_type))
9134941d472SJason Wang 			goto err_xdp;
9144941d472SJason Wang 
9154941d472SJason Wang 		if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
9164941d472SJason Wang 			int offset = buf - page_address(page) + header_offset;
9174941d472SJason Wang 			unsigned int tlen = len + vi->hdr_len;
918981f14d4SHeng Qi 			int num_buf = 1;
9194941d472SJason Wang 
9204941d472SJason Wang 			xdp_headroom = virtnet_get_headroom(vi);
9214941d472SJason Wang 			header_offset = VIRTNET_RX_PAD + xdp_headroom;
9224941d472SJason Wang 			headroom = vi->hdr_len + header_offset;
9234941d472SJason Wang 			buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
9244941d472SJason Wang 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
9254941d472SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf, page,
9264941d472SJason Wang 						      offset, header_offset,
9274941d472SJason Wang 						      &tlen);
9284941d472SJason Wang 			if (!xdp_page)
9294941d472SJason Wang 				goto err_xdp;
9304941d472SJason Wang 
9314941d472SJason Wang 			buf = page_address(xdp_page);
9324941d472SJason Wang 			put_page(page);
9334941d472SJason Wang 			page = xdp_page;
9344941d472SJason Wang 		}
9354941d472SJason Wang 
93643b5169dSLorenzo Bianconi 		xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
937be9df4afSLorenzo Bianconi 		xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
938be9df4afSLorenzo Bianconi 				 xdp_headroom, len, true);
9394941d472SJason Wang 		orig_data = xdp.data;
9404941d472SJason Wang 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
941d46eeeafSJason Wang 		stats->xdp_packets++;
9424941d472SJason Wang 
9434941d472SJason Wang 		switch (act) {
9444941d472SJason Wang 		case XDP_PASS:
9454941d472SJason Wang 			/* Recalculate length in case bpf program changed it */
9464941d472SJason Wang 			delta = orig_data - xdp.data;
9476870de43SNikita V. Shirokov 			len = xdp.data_end - xdp.data;
948503d539aSYuya Kusakabe 			metasize = xdp.data - xdp.data_meta;
9494941d472SJason Wang 			break;
9504941d472SJason Wang 		case XDP_TX:
951d46eeeafSJason Wang 			stats->xdp_tx++;
9521b698fa5SLorenzo Bianconi 			xdpf = xdp_convert_buff_to_frame(&xdp);
95344fa2dbdSJesper Dangaard Brouer 			if (unlikely(!xdpf))
95444fa2dbdSJesper Dangaard Brouer 				goto err_xdp;
955ca9e83b4SJason Wang 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
956fdc13979SLorenzo Bianconi 			if (unlikely(!err)) {
957fdc13979SLorenzo Bianconi 				xdp_return_frame_rx_napi(xdpf);
958fdc13979SLorenzo Bianconi 			} else if (unlikely(err < 0)) {
9594941d472SJason Wang 				trace_xdp_exception(vi->dev, xdp_prog, act);
96011b7d897SJesper Dangaard Brouer 				goto err_xdp;
96111b7d897SJesper Dangaard Brouer 			}
9622471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_TX;
963186b3c99SJason Wang 			rcu_read_unlock();
964186b3c99SJason Wang 			goto xdp_xmit;
965186b3c99SJason Wang 		case XDP_REDIRECT:
966d46eeeafSJason Wang 			stats->xdp_redirects++;
967186b3c99SJason Wang 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
96811b7d897SJesper Dangaard Brouer 			if (err)
96911b7d897SJesper Dangaard Brouer 				goto err_xdp;
9702471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_REDIR;
9714941d472SJason Wang 			rcu_read_unlock();
9724941d472SJason Wang 			goto xdp_xmit;
9734941d472SJason Wang 		default:
974c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
975df561f66SGustavo A. R. Silva 			fallthrough;
9764941d472SJason Wang 		case XDP_ABORTED:
9774941d472SJason Wang 			trace_xdp_exception(vi->dev, xdp_prog, act);
97895efabf0SGustavo A. R. Silva 			goto err_xdp;
9794941d472SJason Wang 		case XDP_DROP:
9804941d472SJason Wang 			goto err_xdp;
9814941d472SJason Wang 		}
9824941d472SJason Wang 	}
9834941d472SJason Wang 	rcu_read_unlock();
9844941d472SJason Wang 
9856213f07cSLi RongQing skip_xdp:
9864941d472SJason Wang 	skb = build_skb(buf, buflen);
987053c9e18SWenliang Wang 	if (!skb)
9884941d472SJason Wang 		goto err;
9894941d472SJason Wang 	skb_reserve(skb, headroom - delta);
9906870de43SNikita V. Shirokov 	skb_put(skb, len);
991f1d4884dSYuya Kusakabe 	if (!xdp_prog) {
9924941d472SJason Wang 		buf += header_offset;
9934941d472SJason Wang 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
994f1d4884dSYuya Kusakabe 	} /* keep zeroed vnet hdr since XDP is loaded */
9954941d472SJason Wang 
996503d539aSYuya Kusakabe 	if (metasize)
997503d539aSYuya Kusakabe 		skb_metadata_set(skb, metasize);
998503d539aSYuya Kusakabe 
9994941d472SJason Wang 	return skb;
10004941d472SJason Wang 
10014941d472SJason Wang err_xdp:
10024941d472SJason Wang 	rcu_read_unlock();
1003d46eeeafSJason Wang 	stats->xdp_drops++;
1004053c9e18SWenliang Wang err:
1005d46eeeafSJason Wang 	stats->drops++;
10064941d472SJason Wang 	put_page(page);
10074941d472SJason Wang xdp_xmit:
10084941d472SJason Wang 	return NULL;
10094941d472SJason Wang }
10104941d472SJason Wang 
10114941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev,
10124941d472SJason Wang 				   struct virtnet_info *vi,
10134941d472SJason Wang 				   struct receive_queue *rq,
10144941d472SJason Wang 				   void *buf,
10157d9d60fdSToshiaki Makita 				   unsigned int len,
1016d46eeeafSJason Wang 				   struct virtnet_rq_stats *stats)
10174941d472SJason Wang {
10184941d472SJason Wang 	struct page *page = buf;
1019503d539aSYuya Kusakabe 	struct sk_buff *skb =
1020fa0f1ba7SXuan Zhuo 		page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, 0);
10214941d472SJason Wang 
1022d46eeeafSJason Wang 	stats->bytes += len - vi->hdr_len;
10234941d472SJason Wang 	if (unlikely(!skb))
10244941d472SJason Wang 		goto err;
10254941d472SJason Wang 
10264941d472SJason Wang 	return skb;
10274941d472SJason Wang 
10284941d472SJason Wang err:
1029d46eeeafSJason Wang 	stats->drops++;
10304941d472SJason Wang 	give_pages(rq, page);
10314941d472SJason Wang 	return NULL;
10324941d472SJason Wang }
10334941d472SJason Wang 
1034b26aa481SHeng Qi /* Why not use xdp_build_skb_from_frame() ?
1035b26aa481SHeng Qi  * XDP core assumes that xdp frags are PAGE_SIZE in length, while in
1036b26aa481SHeng Qi  * virtio-net there are 2 points that do not match its requirements:
1037b26aa481SHeng Qi  *  1. The size of the prefilled buffer is not fixed before xdp is set.
1038b26aa481SHeng Qi  *  2. xdp_build_skb_from_frame() does more checks that we don't need,
1039b26aa481SHeng Qi  *     like eth_type_trans() (which virtio-net does in receive_buf()).
1040b26aa481SHeng Qi  */
1041b26aa481SHeng Qi static struct sk_buff *build_skb_from_xdp_buff(struct net_device *dev,
1042b26aa481SHeng Qi 					       struct virtnet_info *vi,
1043b26aa481SHeng Qi 					       struct xdp_buff *xdp,
1044b26aa481SHeng Qi 					       unsigned int xdp_frags_truesz)
1045b26aa481SHeng Qi {
1046b26aa481SHeng Qi 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
1047b26aa481SHeng Qi 	unsigned int headroom, data_len;
1048b26aa481SHeng Qi 	struct sk_buff *skb;
1049b26aa481SHeng Qi 	int metasize;
1050b26aa481SHeng Qi 	u8 nr_frags;
1051b26aa481SHeng Qi 
1052b26aa481SHeng Qi 	if (unlikely(xdp->data_end > xdp_data_hard_end(xdp))) {
1053b26aa481SHeng Qi 		pr_debug("Error building skb as missing reserved tailroom for xdp");
1054b26aa481SHeng Qi 		return NULL;
1055b26aa481SHeng Qi 	}
1056b26aa481SHeng Qi 
1057b26aa481SHeng Qi 	if (unlikely(xdp_buff_has_frags(xdp)))
1058b26aa481SHeng Qi 		nr_frags = sinfo->nr_frags;
1059b26aa481SHeng Qi 
1060b26aa481SHeng Qi 	skb = build_skb(xdp->data_hard_start, xdp->frame_sz);
1061b26aa481SHeng Qi 	if (unlikely(!skb))
1062b26aa481SHeng Qi 		return NULL;
1063b26aa481SHeng Qi 
1064b26aa481SHeng Qi 	headroom = xdp->data - xdp->data_hard_start;
1065b26aa481SHeng Qi 	data_len = xdp->data_end - xdp->data;
1066b26aa481SHeng Qi 	skb_reserve(skb, headroom);
1067b26aa481SHeng Qi 	__skb_put(skb, data_len);
1068b26aa481SHeng Qi 
1069b26aa481SHeng Qi 	metasize = xdp->data - xdp->data_meta;
1070b26aa481SHeng Qi 	metasize = metasize > 0 ? metasize : 0;
1071b26aa481SHeng Qi 	if (metasize)
1072b26aa481SHeng Qi 		skb_metadata_set(skb, metasize);
1073b26aa481SHeng Qi 
1074b26aa481SHeng Qi 	if (unlikely(xdp_buff_has_frags(xdp)))
1075b26aa481SHeng Qi 		xdp_update_skb_shared_info(skb, nr_frags,
1076b26aa481SHeng Qi 					   sinfo->xdp_frags_size,
1077b26aa481SHeng Qi 					   xdp_frags_truesz,
1078b26aa481SHeng Qi 					   xdp_buff_is_frag_pfmemalloc(xdp));
1079b26aa481SHeng Qi 
1080b26aa481SHeng Qi 	return skb;
1081b26aa481SHeng Qi }
1082b26aa481SHeng Qi 
1083ef75cb51SHeng Qi /* TODO: build xdp in big mode */
1084ef75cb51SHeng Qi static int virtnet_build_xdp_buff_mrg(struct net_device *dev,
1085ef75cb51SHeng Qi 				      struct virtnet_info *vi,
1086ef75cb51SHeng Qi 				      struct receive_queue *rq,
1087ef75cb51SHeng Qi 				      struct xdp_buff *xdp,
1088ef75cb51SHeng Qi 				      void *buf,
1089ef75cb51SHeng Qi 				      unsigned int len,
1090ef75cb51SHeng Qi 				      unsigned int frame_sz,
1091981f14d4SHeng Qi 				      int *num_buf,
1092ef75cb51SHeng Qi 				      unsigned int *xdp_frags_truesize,
1093ef75cb51SHeng Qi 				      struct virtnet_rq_stats *stats)
1094ef75cb51SHeng Qi {
1095ef75cb51SHeng Qi 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1096ef75cb51SHeng Qi 	unsigned int headroom, tailroom, room;
1097ef75cb51SHeng Qi 	unsigned int truesize, cur_frag_size;
1098ef75cb51SHeng Qi 	struct skb_shared_info *shinfo;
1099ef75cb51SHeng Qi 	unsigned int xdp_frags_truesz = 0;
1100ef75cb51SHeng Qi 	struct page *page;
1101ef75cb51SHeng Qi 	skb_frag_t *frag;
1102ef75cb51SHeng Qi 	int offset;
1103ef75cb51SHeng Qi 	void *ctx;
1104ef75cb51SHeng Qi 
1105ef75cb51SHeng Qi 	xdp_init_buff(xdp, frame_sz, &rq->xdp_rxq);
1106ef75cb51SHeng Qi 	xdp_prepare_buff(xdp, buf - VIRTIO_XDP_HEADROOM,
1107ef75cb51SHeng Qi 			 VIRTIO_XDP_HEADROOM + vi->hdr_len, len - vi->hdr_len, true);
1108ef75cb51SHeng Qi 
1109981f14d4SHeng Qi 	if (!*num_buf)
1110981f14d4SHeng Qi 		return 0;
1111981f14d4SHeng Qi 
1112ef75cb51SHeng Qi 	if (*num_buf > 1) {
1113ef75cb51SHeng Qi 		/* If we want to build multi-buffer xdp, we need
1114ef75cb51SHeng Qi 		 * to specify that the flags of xdp_buff have the
1115ef75cb51SHeng Qi 		 * XDP_FLAGS_HAS_FRAG bit.
1116ef75cb51SHeng Qi 		 */
1117ef75cb51SHeng Qi 		if (!xdp_buff_has_frags(xdp))
1118ef75cb51SHeng Qi 			xdp_buff_set_frags_flag(xdp);
1119ef75cb51SHeng Qi 
1120ef75cb51SHeng Qi 		shinfo = xdp_get_shared_info_from_buff(xdp);
1121ef75cb51SHeng Qi 		shinfo->nr_frags = 0;
1122ef75cb51SHeng Qi 		shinfo->xdp_frags_size = 0;
1123ef75cb51SHeng Qi 	}
1124ef75cb51SHeng Qi 
1125981f14d4SHeng Qi 	if (*num_buf > MAX_SKB_FRAGS + 1)
1126ef75cb51SHeng Qi 		return -EINVAL;
1127ef75cb51SHeng Qi 
1128981f14d4SHeng Qi 	while (--*num_buf > 0) {
1129ef75cb51SHeng Qi 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
1130ef75cb51SHeng Qi 		if (unlikely(!buf)) {
1131ef75cb51SHeng Qi 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1132ef75cb51SHeng Qi 				 dev->name, *num_buf,
1133ef75cb51SHeng Qi 				 virtio16_to_cpu(vi->vdev, hdr->num_buffers));
1134ef75cb51SHeng Qi 			dev->stats.rx_length_errors++;
1135ef75cb51SHeng Qi 			return -EINVAL;
1136ef75cb51SHeng Qi 		}
1137ef75cb51SHeng Qi 
1138ef75cb51SHeng Qi 		stats->bytes += len;
1139ef75cb51SHeng Qi 		page = virt_to_head_page(buf);
1140ef75cb51SHeng Qi 		offset = buf - page_address(page);
1141ef75cb51SHeng Qi 
1142ef75cb51SHeng Qi 		truesize = mergeable_ctx_to_truesize(ctx);
1143ef75cb51SHeng Qi 		headroom = mergeable_ctx_to_headroom(ctx);
1144ef75cb51SHeng Qi 		tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1145ef75cb51SHeng Qi 		room = SKB_DATA_ALIGN(headroom + tailroom);
1146ef75cb51SHeng Qi 
1147ef75cb51SHeng Qi 		cur_frag_size = truesize;
1148ef75cb51SHeng Qi 		xdp_frags_truesz += cur_frag_size;
1149ef75cb51SHeng Qi 		if (unlikely(len > truesize - room || cur_frag_size > PAGE_SIZE)) {
1150ef75cb51SHeng Qi 			put_page(page);
1151ef75cb51SHeng Qi 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1152ef75cb51SHeng Qi 				 dev->name, len, (unsigned long)(truesize - room));
1153ef75cb51SHeng Qi 			dev->stats.rx_length_errors++;
1154ef75cb51SHeng Qi 			return -EINVAL;
1155ef75cb51SHeng Qi 		}
1156ef75cb51SHeng Qi 
1157ef75cb51SHeng Qi 		frag = &shinfo->frags[shinfo->nr_frags++];
1158ef75cb51SHeng Qi 		__skb_frag_set_page(frag, page);
1159ef75cb51SHeng Qi 		skb_frag_off_set(frag, offset);
1160ef75cb51SHeng Qi 		skb_frag_size_set(frag, len);
1161ef75cb51SHeng Qi 		if (page_is_pfmemalloc(page))
1162ef75cb51SHeng Qi 			xdp_buff_set_frag_pfmemalloc(xdp);
1163ef75cb51SHeng Qi 
1164ef75cb51SHeng Qi 		shinfo->xdp_frags_size += len;
1165ef75cb51SHeng Qi 	}
1166ef75cb51SHeng Qi 
1167ef75cb51SHeng Qi 	*xdp_frags_truesize = xdp_frags_truesz;
1168ef75cb51SHeng Qi 	return 0;
1169ef75cb51SHeng Qi }
1170ef75cb51SHeng Qi 
11718fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
1172fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
11738fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
1174680557cfSMichael S. Tsirkin 					 void *buf,
1175680557cfSMichael S. Tsirkin 					 void *ctx,
1176186b3c99SJason Wang 					 unsigned int len,
11777d9d60fdSToshiaki Makita 					 unsigned int *xdp_xmit,
1178d46eeeafSJason Wang 					 struct virtnet_rq_stats *stats)
11799ab86bbcSShirley Ma {
1180012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
1181981f14d4SHeng Qi 	int num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
11828fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
11838fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
1184f600b690SJohn Fastabend 	struct sk_buff *head_skb, *curr_skb;
1185f600b690SJohn Fastabend 	struct bpf_prog *xdp_prog;
11869ce6146eSJesper Dangaard Brouer 	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
11874941d472SJason Wang 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
1188ef75cb51SHeng Qi 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1189ef75cb51SHeng Qi 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
119022174f79SHeng Qi 	unsigned int frame_sz, xdp_room;
11919ce6146eSJesper Dangaard Brouer 	int err;
1192ab7db917SMichael Dalton 
119356434a01SJohn Fastabend 	head_skb = NULL;
1194d46eeeafSJason Wang 	stats->bytes += len - vi->hdr_len;
119556434a01SJohn Fastabend 
1196ef75cb51SHeng Qi 	if (unlikely(len > truesize - room)) {
1197ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1198ef75cb51SHeng Qi 			 dev->name, len, (unsigned long)(truesize - room));
1199ad993a95SXie Yongji 		dev->stats.rx_length_errors++;
1200ad993a95SXie Yongji 		goto err_skb;
1201ad993a95SXie Yongji 	}
12026213f07cSLi RongQing 
12036213f07cSLi RongQing 	if (likely(!vi->xdp_enabled)) {
12046213f07cSLi RongQing 		xdp_prog = NULL;
12056213f07cSLi RongQing 		goto skip_xdp;
12066213f07cSLi RongQing 	}
12076213f07cSLi RongQing 
1208f600b690SJohn Fastabend 	rcu_read_lock();
1209f600b690SJohn Fastabend 	xdp_prog = rcu_dereference(rq->xdp_prog);
1210f600b690SJohn Fastabend 	if (xdp_prog) {
121122174f79SHeng Qi 		unsigned int xdp_frags_truesz = 0;
121222174f79SHeng Qi 		struct skb_shared_info *shinfo;
121344fa2dbdSJesper Dangaard Brouer 		struct xdp_frame *xdpf;
121472979a6cSJohn Fastabend 		struct page *xdp_page;
12150354e4d1SJohn Fastabend 		struct xdp_buff xdp;
12160354e4d1SJohn Fastabend 		void *data;
1217f600b690SJohn Fastabend 		u32 act;
121822174f79SHeng Qi 		int i;
1219f600b690SJohn Fastabend 
12203d62b2a0SJason Wang 		/* Transient failure which in theory could occur if
12213d62b2a0SJason Wang 		 * in-flight packets from before XDP was enabled reach
12223d62b2a0SJason Wang 		 * the receive path after XDP is loaded.
12233d62b2a0SJason Wang 		 */
12243d62b2a0SJason Wang 		if (unlikely(hdr->hdr.gso_type))
12253d62b2a0SJason Wang 			goto err_xdp;
12263d62b2a0SJason Wang 
1227ef75cb51SHeng Qi 		/* Now XDP core assumes frag size is PAGE_SIZE, but buffers
1228ef75cb51SHeng Qi 		 * with headroom may add hole in truesize, which
1229ef75cb51SHeng Qi 		 * make their length exceed PAGE_SIZE. So we disabled the
1230ef75cb51SHeng Qi 		 * hole mechanism for xdp. See add_recvbuf_mergeable().
12319ce6146eSJesper Dangaard Brouer 		 */
1232ef75cb51SHeng Qi 		frame_sz = truesize;
12339ce6146eSJesper Dangaard Brouer 
123422174f79SHeng Qi 		/* This happens when headroom is not enough because
123522174f79SHeng Qi 		 * of the buffer was prefilled before XDP is set.
123622174f79SHeng Qi 		 * This should only happen for the first several packets.
123722174f79SHeng Qi 		 * In fact, vq reset can be used here to help us clean up
123822174f79SHeng Qi 		 * the prefilled buffers, but many existing devices do not
123922174f79SHeng Qi 		 * support it, and we don't want to bother users who are
124022174f79SHeng Qi 		 * using xdp normally.
12413cc81a9aSJason Wang 		 */
124222174f79SHeng Qi 		if (!xdp_prog->aux->xdp_has_frags &&
124322174f79SHeng Qi 		    (num_buf > 1 || headroom < virtnet_get_headroom(vi))) {
124472979a6cSJohn Fastabend 			/* linearize data for XDP */
124556a86f84SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf,
12464941d472SJason Wang 						      page, offset,
12474941d472SJason Wang 						      VIRTIO_XDP_HEADROOM,
12484941d472SJason Wang 						      &len);
12499ce6146eSJesper Dangaard Brouer 			frame_sz = PAGE_SIZE;
12509ce6146eSJesper Dangaard Brouer 
125172979a6cSJohn Fastabend 			if (!xdp_page)
1252f600b690SJohn Fastabend 				goto err_xdp;
12532de2f7f4SJohn Fastabend 			offset = VIRTIO_XDP_HEADROOM;
125422174f79SHeng Qi 		} else if (unlikely(headroom < virtnet_get_headroom(vi))) {
125522174f79SHeng Qi 			xdp_room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
125622174f79SHeng Qi 						  sizeof(struct skb_shared_info));
125722174f79SHeng Qi 			if (len + xdp_room > PAGE_SIZE)
125822174f79SHeng Qi 				goto err_xdp;
125922174f79SHeng Qi 
126022174f79SHeng Qi 			xdp_page = alloc_page(GFP_ATOMIC);
126122174f79SHeng Qi 			if (!xdp_page)
126222174f79SHeng Qi 				goto err_xdp;
126322174f79SHeng Qi 
126422174f79SHeng Qi 			memcpy(page_address(xdp_page) + VIRTIO_XDP_HEADROOM,
126522174f79SHeng Qi 			       page_address(page) + offset, len);
126622174f79SHeng Qi 			frame_sz = PAGE_SIZE;
126722174f79SHeng Qi 			offset = VIRTIO_XDP_HEADROOM;
126872979a6cSJohn Fastabend 		} else {
126972979a6cSJohn Fastabend 			xdp_page = page;
1270f600b690SJohn Fastabend 		}
1271f600b690SJohn Fastabend 
12720354e4d1SJohn Fastabend 		data = page_address(xdp_page) + offset;
127322174f79SHeng Qi 		err = virtnet_build_xdp_buff_mrg(dev, vi, rq, &xdp, data, len, frame_sz,
127422174f79SHeng Qi 						 &num_buf, &xdp_frags_truesz, stats);
127522174f79SHeng Qi 		if (unlikely(err))
127622174f79SHeng Qi 			goto err_xdp_frags;
1277754b8a21SJesper Dangaard Brouer 
12780354e4d1SJohn Fastabend 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
1279d46eeeafSJason Wang 		stats->xdp_packets++;
12800354e4d1SJohn Fastabend 
128156434a01SJohn Fastabend 		switch (act) {
128256434a01SJohn Fastabend 		case XDP_PASS:
12831a3bd6eaSXuan Zhuo 			head_skb = build_skb_from_xdp_buff(dev, vi, &xdp, xdp_frags_truesz);
12841a3bd6eaSXuan Zhuo 			if (unlikely(!head_skb))
12851a3bd6eaSXuan Zhuo 				goto err_xdp_frags;
12861a3bd6eaSXuan Zhuo 
1287fab89bafSHeng Qi 			if (unlikely(xdp_page != page))
12881830f893SJason Wang 				put_page(page);
1289fab89bafSHeng Qi 			rcu_read_unlock();
12901830f893SJason Wang 			return head_skb;
129156434a01SJohn Fastabend 		case XDP_TX:
1292d46eeeafSJason Wang 			stats->xdp_tx++;
12931b698fa5SLorenzo Bianconi 			xdpf = xdp_convert_buff_to_frame(&xdp);
12947a542beeSXuan Zhuo 			if (unlikely(!xdpf)) {
1295fab89bafSHeng Qi 				netdev_dbg(dev, "convert buff to frame failed for xdp\n");
1296fab89bafSHeng Qi 				goto err_xdp_frags;
12977a542beeSXuan Zhuo 			}
1298ca9e83b4SJason Wang 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1299fdc13979SLorenzo Bianconi 			if (unlikely(!err)) {
1300fdc13979SLorenzo Bianconi 				xdp_return_frame_rx_napi(xdpf);
1301fdc13979SLorenzo Bianconi 			} else if (unlikely(err < 0)) {
13020354e4d1SJohn Fastabend 				trace_xdp_exception(vi->dev, xdp_prog, act);
1303fab89bafSHeng Qi 				goto err_xdp_frags;
130411b7d897SJesper Dangaard Brouer 			}
13052471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_TX;
130672979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
13075d458a13SJason Wang 				put_page(page);
130856434a01SJohn Fastabend 			rcu_read_unlock();
130956434a01SJohn Fastabend 			goto xdp_xmit;
13103cc81a9aSJason Wang 		case XDP_REDIRECT:
1311d46eeeafSJason Wang 			stats->xdp_redirects++;
13123cc81a9aSJason Wang 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
1313fab89bafSHeng Qi 			if (err)
1314fab89bafSHeng Qi 				goto err_xdp_frags;
13152471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_REDIR;
13163cc81a9aSJason Wang 			if (unlikely(xdp_page != page))
13176890418bSJason Wang 				put_page(page);
13183cc81a9aSJason Wang 			rcu_read_unlock();
13193cc81a9aSJason Wang 			goto xdp_xmit;
132056434a01SJohn Fastabend 		default:
1321c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1322df561f66SGustavo A. R. Silva 			fallthrough;
13230354e4d1SJohn Fastabend 		case XDP_ABORTED:
13240354e4d1SJohn Fastabend 			trace_xdp_exception(vi->dev, xdp_prog, act);
1325df561f66SGustavo A. R. Silva 			fallthrough;
13260354e4d1SJohn Fastabend 		case XDP_DROP:
1327fab89bafSHeng Qi 			goto err_xdp_frags;
1328f600b690SJohn Fastabend 		}
132922174f79SHeng Qi err_xdp_frags:
133022174f79SHeng Qi 		if (unlikely(xdp_page != page))
133122174f79SHeng Qi 			__free_pages(xdp_page, 0);
133222174f79SHeng Qi 
133322174f79SHeng Qi 		if (xdp_buff_has_frags(&xdp)) {
133422174f79SHeng Qi 			shinfo = xdp_get_shared_info_from_buff(&xdp);
133522174f79SHeng Qi 			for (i = 0; i < shinfo->nr_frags; i++) {
133622174f79SHeng Qi 				xdp_page = skb_frag_page(&shinfo->frags[i]);
133722174f79SHeng Qi 				put_page(xdp_page);
133822174f79SHeng Qi 			}
133922174f79SHeng Qi 		}
134022174f79SHeng Qi 
134122174f79SHeng Qi 		goto err_xdp;
134256434a01SJohn Fastabend 	}
1343f600b690SJohn Fastabend 	rcu_read_unlock();
1344f600b690SJohn Fastabend 
13456213f07cSLi RongQing skip_xdp:
1346fa0f1ba7SXuan Zhuo 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize, headroom);
1347f600b690SJohn Fastabend 	curr_skb = head_skb;
13489ab86bbcSShirley Ma 
13498fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
13508fc3b9e9SMichael S. Tsirkin 		goto err_skb;
13519ab86bbcSShirley Ma 	while (--num_buf) {
13528fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
13538fc3b9e9SMichael S. Tsirkin 
1354680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
135503e9f8a0SYunjian Wang 		if (unlikely(!buf)) {
13568fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1357fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
1358012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
1359012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
13608fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
13618fc3b9e9SMichael S. Tsirkin 			goto err_buf;
13623f2c31d9SMark McLoughlin 		}
13638fc3b9e9SMichael S. Tsirkin 
1364d46eeeafSJason Wang 		stats->bytes += len;
13658fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
136628b39bc7SJason Wang 
136728b39bc7SJason Wang 		truesize = mergeable_ctx_to_truesize(ctx);
1368ef75cb51SHeng Qi 		headroom = mergeable_ctx_to_headroom(ctx);
1369ef75cb51SHeng Qi 		tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
1370ef75cb51SHeng Qi 		room = SKB_DATA_ALIGN(headroom + tailroom);
1371ef75cb51SHeng Qi 		if (unlikely(len > truesize - room)) {
137256da5fd0SDan Carpenter 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1373ef75cb51SHeng Qi 				 dev->name, len, (unsigned long)(truesize - room));
1374680557cfSMichael S. Tsirkin 			dev->stats.rx_length_errors++;
1375680557cfSMichael S. Tsirkin 			goto err_skb;
1376680557cfSMichael S. Tsirkin 		}
13778fc3b9e9SMichael S. Tsirkin 
13788fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
13792613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
13802613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
13818fc3b9e9SMichael S. Tsirkin 
13828fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
13838fc3b9e9SMichael S. Tsirkin 				goto err_skb;
13842613af0eSMichael Dalton 			if (curr_skb == head_skb)
13852613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
13862613af0eSMichael Dalton 			else
13872613af0eSMichael Dalton 				curr_skb->next = nskb;
13882613af0eSMichael Dalton 			curr_skb = nskb;
13892613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
13902613af0eSMichael Dalton 			num_skb_frags = 0;
13912613af0eSMichael Dalton 		}
13922613af0eSMichael Dalton 		if (curr_skb != head_skb) {
13932613af0eSMichael Dalton 			head_skb->data_len += len;
13942613af0eSMichael Dalton 			head_skb->len += len;
1395fb51879dSMichael Dalton 			head_skb->truesize += truesize;
13962613af0eSMichael Dalton 		}
13978fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
1398ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1399ba275241SJason Wang 			put_page(page);
1400ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1401fb51879dSMichael Dalton 					     len, truesize);
1402ba275241SJason Wang 		} else {
14032613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
1404fb51879dSMichael Dalton 					offset, len, truesize);
1405ba275241SJason Wang 		}
14068fc3b9e9SMichael S. Tsirkin 	}
14078fc3b9e9SMichael S. Tsirkin 
14085377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
14098fc3b9e9SMichael S. Tsirkin 	return head_skb;
14108fc3b9e9SMichael S. Tsirkin 
1411f600b690SJohn Fastabend err_xdp:
1412f600b690SJohn Fastabend 	rcu_read_unlock();
1413d46eeeafSJason Wang 	stats->xdp_drops++;
14148fc3b9e9SMichael S. Tsirkin err_skb:
14158fc3b9e9SMichael S. Tsirkin 	put_page(page);
1416850e088dSJason Wang 	while (num_buf-- > 1) {
1417680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &len);
1418680557cfSMichael S. Tsirkin 		if (unlikely(!buf)) {
14198fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
14208fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
14218fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
14228fc3b9e9SMichael S. Tsirkin 			break;
14238fc3b9e9SMichael S. Tsirkin 		}
1424d46eeeafSJason Wang 		stats->bytes += len;
1425680557cfSMichael S. Tsirkin 		page = virt_to_head_page(buf);
14268fc3b9e9SMichael S. Tsirkin 		put_page(page);
14273f2c31d9SMark McLoughlin 	}
14288fc3b9e9SMichael S. Tsirkin err_buf:
1429d46eeeafSJason Wang 	stats->drops++;
14308fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
143156434a01SJohn Fastabend xdp_xmit:
14328fc3b9e9SMichael S. Tsirkin 	return NULL;
14339ab86bbcSShirley Ma }
14349ab86bbcSShirley Ma 
143591f41f01SAndrew Melnychenko static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
143691f41f01SAndrew Melnychenko 				struct sk_buff *skb)
143791f41f01SAndrew Melnychenko {
143891f41f01SAndrew Melnychenko 	enum pkt_hash_types rss_hash_type;
143991f41f01SAndrew Melnychenko 
144091f41f01SAndrew Melnychenko 	if (!hdr_hash || !skb)
144191f41f01SAndrew Melnychenko 		return;
144291f41f01SAndrew Melnychenko 
144395bb6330SMichael S. Tsirkin 	switch (__le16_to_cpu(hdr_hash->hash_report)) {
144491f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv4:
144591f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv4:
144691f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv6:
144791f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv6:
144891f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
144991f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
145091f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_L4;
145191f41f01SAndrew Melnychenko 		break;
145291f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv4:
145391f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv6:
145491f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv6_EX:
145591f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_L3;
145691f41f01SAndrew Melnychenko 		break;
145791f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_NONE:
145891f41f01SAndrew Melnychenko 	default:
145991f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_NONE;
146091f41f01SAndrew Melnychenko 	}
146195bb6330SMichael S. Tsirkin 	skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
146291f41f01SAndrew Melnychenko }
146391f41f01SAndrew Melnychenko 
14647d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
14652471c75eSJesper Dangaard Brouer 			void *buf, unsigned int len, void **ctx,
1466a0929a44SToshiaki Makita 			unsigned int *xdp_xmit,
1467d46eeeafSJason Wang 			struct virtnet_rq_stats *stats)
14689ab86bbcSShirley Ma {
1469e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
14709ab86bbcSShirley Ma 	struct sk_buff *skb;
1471012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
14729ab86bbcSShirley Ma 
1473bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
14749ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
14759ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
1476eb1d929fSParav Pandit 		virtnet_rq_free_unused_buf(rq->vq, buf);
14777d9d60fdSToshiaki Makita 		return;
14789ab86bbcSShirley Ma 	}
14799ab86bbcSShirley Ma 
1480f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
14817d9d60fdSToshiaki Makita 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1482a0929a44SToshiaki Makita 					stats);
1483f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
1484a0929a44SToshiaki Makita 		skb = receive_big(dev, vi, rq, buf, len, stats);
1485f121159dSMichael S. Tsirkin 	else
1486a0929a44SToshiaki Makita 		skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1487f121159dSMichael S. Tsirkin 
14888fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
14897d9d60fdSToshiaki Makita 		return;
14903f2c31d9SMark McLoughlin 
14919ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
149291f41f01SAndrew Melnychenko 	if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
149391f41f01SAndrew Melnychenko 		virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
14943fa2a1dfSstephen hemminger 
1495e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
149610a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1497296f96fcSRusty Russell 
1498e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1499e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
1500e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1501e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
1502fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
1503296f96fcSRusty Russell 		goto frame_err;
1504296f96fcSRusty Russell 	}
1505296f96fcSRusty Russell 
1506133bbb18SWillem de Bruijn 	skb_record_rx_queue(skb, vq2rxq(rq->vq));
1507d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
1508d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1509d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
1510d1dc06dcSMike Rapoport 
15110fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
15127d9d60fdSToshiaki Makita 	return;
1513296f96fcSRusty Russell 
1514296f96fcSRusty Russell frame_err:
1515296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
1516296f96fcSRusty Russell 	dev_kfree_skb(skb);
1517296f96fcSRusty Russell }
1518296f96fcSRusty Russell 
1519192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the
1520192f68cfSJason Wang  * same size, except for the headroom. For this reason we do
1521192f68cfSJason Wang  * not need to use  mergeable_len_to_ctx here - it is enough
1522192f68cfSJason Wang  * to store the headroom as the context ignoring the truesize.
1523192f68cfSJason Wang  */
1524946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1525946fa564SMichael S. Tsirkin 			     gfp_t gfp)
1526296f96fcSRusty Russell {
1527f6b10209SJason Wang 	struct page_frag *alloc_frag = &rq->alloc_frag;
1528f6b10209SJason Wang 	char *buf;
15292de2f7f4SJohn Fastabend 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
1530192f68cfSJason Wang 	void *ctx = (void *)(unsigned long)xdp_headroom;
1531f6b10209SJason Wang 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
15329ab86bbcSShirley Ma 	int err;
15333f2c31d9SMark McLoughlin 
1534f6b10209SJason Wang 	len = SKB_DATA_ALIGN(len) +
1535f6b10209SJason Wang 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1536f6b10209SJason Wang 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
15379ab86bbcSShirley Ma 		return -ENOMEM;
1538296f96fcSRusty Russell 
1539f6b10209SJason Wang 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1540f6b10209SJason Wang 	get_page(alloc_frag->page);
1541f6b10209SJason Wang 	alloc_frag->offset += len;
1542f6b10209SJason Wang 	sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1543f6b10209SJason Wang 		    vi->hdr_len + GOOD_PACKET_LEN);
1544192f68cfSJason Wang 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
15459ab86bbcSShirley Ma 	if (err < 0)
1546f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
15479ab86bbcSShirley Ma 	return err;
154897402b96SHerbert Xu }
154997402b96SHerbert Xu 
1550012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1551012873d0SMichael S. Tsirkin 			   gfp_t gfp)
15529ab86bbcSShirley Ma {
15539ab86bbcSShirley Ma 	struct page *first, *list = NULL;
15549ab86bbcSShirley Ma 	char *p;
15559ab86bbcSShirley Ma 	int i, err, offset;
1556296f96fcSRusty Russell 
15574959aebbSGavin Li 	sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1558a5835440SRusty Russell 
15594959aebbSGavin Li 	/* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
15604959aebbSGavin Li 	for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1561e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
15629ab86bbcSShirley Ma 		if (!first) {
15639ab86bbcSShirley Ma 			if (list)
1564e9d7417bSJason Wang 				give_pages(rq, list);
15659ab86bbcSShirley Ma 			return -ENOMEM;
1566296f96fcSRusty Russell 		}
1567e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
15689ab86bbcSShirley Ma 
15699ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
15709ab86bbcSShirley Ma 		first->private = (unsigned long)list;
15719ab86bbcSShirley Ma 		list = first;
15729ab86bbcSShirley Ma 	}
15739ab86bbcSShirley Ma 
1574e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
15759ab86bbcSShirley Ma 	if (!first) {
1576e9d7417bSJason Wang 		give_pages(rq, list);
15779ab86bbcSShirley Ma 		return -ENOMEM;
15789ab86bbcSShirley Ma 	}
15799ab86bbcSShirley Ma 	p = page_address(first);
15809ab86bbcSShirley Ma 
1581e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
1582012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
1583012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
15849ab86bbcSShirley Ma 
1585e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
15869ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
1587e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
15889ab86bbcSShirley Ma 
15899ab86bbcSShirley Ma 	/* chain first in list head */
15909ab86bbcSShirley Ma 	first->private = (unsigned long)list;
15914959aebbSGavin Li 	err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1592aa989f5eSMichael S. Tsirkin 				  first, gfp);
15939ab86bbcSShirley Ma 	if (err < 0)
1594e9d7417bSJason Wang 		give_pages(rq, first);
15959ab86bbcSShirley Ma 
15969ab86bbcSShirley Ma 	return err;
15979ab86bbcSShirley Ma }
15989ab86bbcSShirley Ma 
1599d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
16003cc81a9aSJason Wang 					  struct ewma_pkt_len *avg_pkt_len,
16013cc81a9aSJason Wang 					  unsigned int room)
16029ab86bbcSShirley Ma {
1603c1ddc42dSAndrew Melnychenko 	struct virtnet_info *vi = rq->vq->vdev->priv;
1604c1ddc42dSAndrew Melnychenko 	const size_t hdr_len = vi->hdr_len;
1605fbf28d78SMichael Dalton 	unsigned int len;
1606fbf28d78SMichael Dalton 
16073cc81a9aSJason Wang 	if (room)
16083cc81a9aSJason Wang 		return PAGE_SIZE - room;
16093cc81a9aSJason Wang 
16105377d758SJohannes Berg 	len = hdr_len +	clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1611f0c3192cSMichael S. Tsirkin 				rq->min_buf_len, PAGE_SIZE - hdr_len);
16123cc81a9aSJason Wang 
1613e377fcc8SMichael S. Tsirkin 	return ALIGN(len, L1_CACHE_BYTES);
1614fbf28d78SMichael Dalton }
1615fbf28d78SMichael Dalton 
16162de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi,
16172de2f7f4SJohn Fastabend 				 struct receive_queue *rq, gfp_t gfp)
1618fbf28d78SMichael Dalton {
1619fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
16202de2f7f4SJohn Fastabend 	unsigned int headroom = virtnet_get_headroom(vi);
16213cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
16223cc81a9aSJason Wang 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1623fb51879dSMichael Dalton 	char *buf;
1624680557cfSMichael S. Tsirkin 	void *ctx;
16259ab86bbcSShirley Ma 	int err;
1626fb51879dSMichael Dalton 	unsigned int len, hole;
16279ab86bbcSShirley Ma 
16283cc81a9aSJason Wang 	/* Extra tailroom is needed to satisfy XDP's assumption. This
16293cc81a9aSJason Wang 	 * means rx frags coalescing won't work, but consider we've
16303cc81a9aSJason Wang 	 * disabled GSO for XDP, it won't be a big issue.
16313cc81a9aSJason Wang 	 */
16323cc81a9aSJason Wang 	len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
16333cc81a9aSJason Wang 	if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
16349ab86bbcSShirley Ma 		return -ENOMEM;
1635ab7db917SMichael Dalton 
1636fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
16372de2f7f4SJohn Fastabend 	buf += headroom; /* advance address leaving hole at front of pkt */
1638fb51879dSMichael Dalton 	get_page(alloc_frag->page);
16393cc81a9aSJason Wang 	alloc_frag->offset += len + room;
1640fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
16413cc81a9aSJason Wang 	if (hole < len + room) {
1642ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
1643ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
16441daa8790SMichael S. Tsirkin 		 * the current buffer.
1645484beac2SHeng Qi 		 * XDP core assumes that frame_size of xdp_buff and the length
1646484beac2SHeng Qi 		 * of the frag are PAGE_SIZE, so we disable the hole mechanism.
1647ab7db917SMichael Dalton 		 */
1648484beac2SHeng Qi 		if (!headroom)
1649fb51879dSMichael Dalton 			len += hole;
1650fb51879dSMichael Dalton 		alloc_frag->offset += hole;
1651fb51879dSMichael Dalton 	}
16529ab86bbcSShirley Ma 
1653fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
1654ef75cb51SHeng Qi 	ctx = mergeable_len_to_ctx(len + room, headroom);
1655680557cfSMichael S. Tsirkin 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
16569ab86bbcSShirley Ma 	if (err < 0)
16572613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
16589ab86bbcSShirley Ma 
16599ab86bbcSShirley Ma 	return err;
1660296f96fcSRusty Russell }
1661296f96fcSRusty Russell 
1662b2baed69SRusty Russell /*
1663b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
1664b2baed69SRusty Russell  *
1665b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
1666b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
1667b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
1668b2baed69SRusty Russell  */
1669946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1670946fa564SMichael S. Tsirkin 			  gfp_t gfp)
16713f2c31d9SMark McLoughlin {
16723f2c31d9SMark McLoughlin 	int err;
16731788f495SMichael S. Tsirkin 	bool oom;
16743f2c31d9SMark McLoughlin 
16750aea51c3SAmit Shah 	do {
16769ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
16772de2f7f4SJohn Fastabend 			err = add_recvbuf_mergeable(vi, rq, gfp);
16789ab86bbcSShirley Ma 		else if (vi->big_packets)
1679012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
16809ab86bbcSShirley Ma 		else
1681946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
16823f2c31d9SMark McLoughlin 
16831788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
16849ed4cb07SRusty Russell 		if (err)
16853f2c31d9SMark McLoughlin 			break;
1686b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
1687461f03dcSToshiaki Makita 	if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
168801c32598SMichael S. Tsirkin 		unsigned long flags;
168901c32598SMichael S. Tsirkin 
169001c32598SMichael S. Tsirkin 		flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1691d46eeeafSJason Wang 		rq->stats.kicks++;
169201c32598SMichael S. Tsirkin 		u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1693461f03dcSToshiaki Makita 	}
1694461f03dcSToshiaki Makita 
16953161e453SRusty Russell 	return !oom;
16963f2c31d9SMark McLoughlin }
16973f2c31d9SMark McLoughlin 
169818445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
1699296f96fcSRusty Russell {
1700296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
1701986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1702e9d7417bSJason Wang 
1703e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(&rq->napi, rvq);
1704296f96fcSRusty Russell }
1705296f96fcSRusty Russell 
1706e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
17073e9d08ecSBruce Rogers {
1708e4e8452aSWillem de Bruijn 	napi_enable(napi);
17093e9d08ecSBruce Rogers 
17103e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
1711e4e8452aSWillem de Bruijn 	 * won't get another interrupt, so process any outstanding packets now.
1712e4e8452aSWillem de Bruijn 	 * Call local_bh_enable after to trigger softIRQ processing.
1713e4e8452aSWillem de Bruijn 	 */
1714ec13ee80SMichael S. Tsirkin 	local_bh_disable();
1715e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(napi, vq);
1716ec13ee80SMichael S. Tsirkin 	local_bh_enable();
17173e9d08ecSBruce Rogers }
17183e9d08ecSBruce Rogers 
1719b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1720b92f1e67SWillem de Bruijn 				   struct virtqueue *vq,
1721b92f1e67SWillem de Bruijn 				   struct napi_struct *napi)
1722b92f1e67SWillem de Bruijn {
1723b92f1e67SWillem de Bruijn 	if (!napi->weight)
1724b92f1e67SWillem de Bruijn 		return;
1725b92f1e67SWillem de Bruijn 
1726b92f1e67SWillem de Bruijn 	/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1727b92f1e67SWillem de Bruijn 	 * enable the feature if this is likely affine with the transmit path.
1728b92f1e67SWillem de Bruijn 	 */
1729b92f1e67SWillem de Bruijn 	if (!vi->affinity_hint_set) {
1730b92f1e67SWillem de Bruijn 		napi->weight = 0;
1731b92f1e67SWillem de Bruijn 		return;
1732b92f1e67SWillem de Bruijn 	}
1733b92f1e67SWillem de Bruijn 
1734b92f1e67SWillem de Bruijn 	return virtnet_napi_enable(vq, napi);
1735b92f1e67SWillem de Bruijn }
1736b92f1e67SWillem de Bruijn 
173778a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi)
173878a57b48SWillem de Bruijn {
173978a57b48SWillem de Bruijn 	if (napi->weight)
174078a57b48SWillem de Bruijn 		napi_disable(napi);
174178a57b48SWillem de Bruijn }
174278a57b48SWillem de Bruijn 
17433161e453SRusty Russell static void refill_work(struct work_struct *work)
17443161e453SRusty Russell {
1745e9d7417bSJason Wang 	struct virtnet_info *vi =
1746e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
17473161e453SRusty Russell 	bool still_empty;
1748986a4f4dSJason Wang 	int i;
17493161e453SRusty Russell 
175055257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1751986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
1752986a4f4dSJason Wang 
1753986a4f4dSJason Wang 		napi_disable(&rq->napi);
1754946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1755e4e8452aSWillem de Bruijn 		virtnet_napi_enable(rq->vq, &rq->napi);
17563161e453SRusty Russell 
17573161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
1758986a4f4dSJason Wang 		 * we will *never* try to fill again.
1759986a4f4dSJason Wang 		 */
17603161e453SRusty Russell 		if (still_empty)
17613b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
17623161e453SRusty Russell 	}
1763986a4f4dSJason Wang }
17643161e453SRusty Russell 
17652471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget,
17662471c75eSJesper Dangaard Brouer 			   unsigned int *xdp_xmit)
1767296f96fcSRusty Russell {
1768e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
1769d46eeeafSJason Wang 	struct virtnet_rq_stats stats = {};
1770a0929a44SToshiaki Makita 	unsigned int len;
17719ab86bbcSShirley Ma 	void *buf;
1772a0929a44SToshiaki Makita 	int i;
1773296f96fcSRusty Russell 
1774192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
1775680557cfSMichael S. Tsirkin 		void *ctx;
1776680557cfSMichael S. Tsirkin 
1777d46eeeafSJason Wang 		while (stats.packets < budget &&
1778680557cfSMichael S. Tsirkin 		       (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1779a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1780d46eeeafSJason Wang 			stats.packets++;
1781680557cfSMichael S. Tsirkin 		}
1782680557cfSMichael S. Tsirkin 	} else {
1783d46eeeafSJason Wang 		while (stats.packets < budget &&
1784e9d7417bSJason Wang 		       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1785a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1786d46eeeafSJason Wang 			stats.packets++;
1787296f96fcSRusty Russell 		}
1788680557cfSMichael S. Tsirkin 	}
1789296f96fcSRusty Russell 
1790718be6baS? jiang 	if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
17915a159128SJason Wang 		if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
17925a159128SJason Wang 			spin_lock(&vi->refill_lock);
17935a159128SJason Wang 			if (vi->refill_enabled)
17943b07e9caSTejun Heo 				schedule_delayed_work(&vi->refill, 0);
17955a159128SJason Wang 			spin_unlock(&vi->refill_lock);
17965a159128SJason Wang 		}
17973161e453SRusty Russell 	}
1798296f96fcSRusty Russell 
1799d7dfc5cfSToshiaki Makita 	u64_stats_update_begin(&rq->stats.syncp);
1800a0929a44SToshiaki Makita 	for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1801a0929a44SToshiaki Makita 		size_t offset = virtnet_rq_stats_desc[i].offset;
1802a0929a44SToshiaki Makita 		u64 *item;
1803a0929a44SToshiaki Makita 
1804d46eeeafSJason Wang 		item = (u64 *)((u8 *)&rq->stats + offset);
1805d46eeeafSJason Wang 		*item += *(u64 *)((u8 *)&stats + offset);
1806a0929a44SToshiaki Makita 	}
1807d7dfc5cfSToshiaki Makita 	u64_stats_update_end(&rq->stats.syncp);
180861845d20SJason Wang 
1809d46eeeafSJason Wang 	return stats.packets;
18102ffa7598SJason Wang }
18112ffa7598SJason Wang 
18127b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq)
18137b0411efSWillem de Bruijn {
18147b0411efSWillem de Bruijn 	struct virtnet_info *vi = rq->vq->vdev->priv;
18157b0411efSWillem de Bruijn 	unsigned int index = vq2rxq(rq->vq);
18167b0411efSWillem de Bruijn 	struct send_queue *sq = &vi->sq[index];
18177b0411efSWillem de Bruijn 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
18187b0411efSWillem de Bruijn 
1819534da5e8SToshiaki Makita 	if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
18207b0411efSWillem de Bruijn 		return;
18217b0411efSWillem de Bruijn 
18227b0411efSWillem de Bruijn 	if (__netif_tx_trylock(txq)) {
1823ebcce492SXuan Zhuo 		if (sq->reset) {
1824ebcce492SXuan Zhuo 			__netif_tx_unlock(txq);
1825ebcce492SXuan Zhuo 			return;
1826ebcce492SXuan Zhuo 		}
1827ebcce492SXuan Zhuo 
1828a7766ef1SMichael S. Tsirkin 		do {
1829a7766ef1SMichael S. Tsirkin 			virtqueue_disable_cb(sq->vq);
1830df133f3fSMichael S. Tsirkin 			free_old_xmit_skbs(sq, true);
1831a7766ef1SMichael S. Tsirkin 		} while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
18327b0411efSWillem de Bruijn 
18337b0411efSWillem de Bruijn 		if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
18347b0411efSWillem de Bruijn 			netif_tx_wake_queue(txq);
183522bc63c5SMichael S. Tsirkin 
183622bc63c5SMichael S. Tsirkin 		__netif_tx_unlock(txq);
183722bc63c5SMichael S. Tsirkin 	}
18387b0411efSWillem de Bruijn }
18397b0411efSWillem de Bruijn 
18402ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
18412ffa7598SJason Wang {
18422ffa7598SJason Wang 	struct receive_queue *rq =
18432ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
18449267c430SJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
18459267c430SJason Wang 	struct send_queue *sq;
18462a43565cSToshiaki Makita 	unsigned int received;
18472471c75eSJesper Dangaard Brouer 	unsigned int xdp_xmit = 0;
18482ffa7598SJason Wang 
18497b0411efSWillem de Bruijn 	virtnet_poll_cleantx(rq);
18507b0411efSWillem de Bruijn 
1851186b3c99SJason Wang 	received = virtnet_receive(rq, budget, &xdp_xmit);
18522ffa7598SJason Wang 
1853ad7e615fSMagnus Karlsson 	if (xdp_xmit & VIRTIO_XDP_REDIR)
1854ad7e615fSMagnus Karlsson 		xdp_do_flush();
1855ad7e615fSMagnus Karlsson 
18568329d98eSRusty Russell 	/* Out of packets? */
1857e4e8452aSWillem de Bruijn 	if (received < budget)
1858e4e8452aSWillem de Bruijn 		virtqueue_napi_complete(napi, rq->vq, received);
1859296f96fcSRusty Russell 
18602471c75eSJesper Dangaard Brouer 	if (xdp_xmit & VIRTIO_XDP_TX) {
186197c2c69eSXuan Zhuo 		sq = virtnet_xdp_get_sq(vi);
1862461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1863461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
1864461f03dcSToshiaki Makita 			sq->stats.kicks++;
1865461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
1866461f03dcSToshiaki Makita 		}
186797c2c69eSXuan Zhuo 		virtnet_xdp_put_sq(vi, sq);
18689267c430SJason Wang 	}
1869186b3c99SJason Wang 
1870296f96fcSRusty Russell 	return received;
1871296f96fcSRusty Russell }
1872296f96fcSRusty Russell 
18735306623aSFeng Liu static void virtnet_disable_queue_pair(struct virtnet_info *vi, int qp_index)
18745306623aSFeng Liu {
18755306623aSFeng Liu 	virtnet_napi_tx_disable(&vi->sq[qp_index].napi);
18765306623aSFeng Liu 	napi_disable(&vi->rq[qp_index].napi);
18775306623aSFeng Liu 	xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
18785306623aSFeng Liu }
18795306623aSFeng Liu 
18805306623aSFeng Liu static int virtnet_enable_queue_pair(struct virtnet_info *vi, int qp_index)
18815306623aSFeng Liu {
18825306623aSFeng Liu 	struct net_device *dev = vi->dev;
18835306623aSFeng Liu 	int err;
18845306623aSFeng Liu 
18855306623aSFeng Liu 	err = xdp_rxq_info_reg(&vi->rq[qp_index].xdp_rxq, dev, qp_index,
18865306623aSFeng Liu 			       vi->rq[qp_index].napi.napi_id);
18875306623aSFeng Liu 	if (err < 0)
18885306623aSFeng Liu 		return err;
18895306623aSFeng Liu 
18905306623aSFeng Liu 	err = xdp_rxq_info_reg_mem_model(&vi->rq[qp_index].xdp_rxq,
18915306623aSFeng Liu 					 MEM_TYPE_PAGE_SHARED, NULL);
18925306623aSFeng Liu 	if (err < 0)
18935306623aSFeng Liu 		goto err_xdp_reg_mem_model;
18945306623aSFeng Liu 
18955306623aSFeng Liu 	virtnet_napi_enable(vi->rq[qp_index].vq, &vi->rq[qp_index].napi);
18965306623aSFeng Liu 	virtnet_napi_tx_enable(vi, vi->sq[qp_index].vq, &vi->sq[qp_index].napi);
18975306623aSFeng Liu 
18985306623aSFeng Liu 	return 0;
18995306623aSFeng Liu 
19005306623aSFeng Liu err_xdp_reg_mem_model:
19015306623aSFeng Liu 	xdp_rxq_info_unreg(&vi->rq[qp_index].xdp_rxq);
19025306623aSFeng Liu 	return err;
19035306623aSFeng Liu }
19045306623aSFeng Liu 
1905986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
1906986a4f4dSJason Wang {
1907986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1908754b8a21SJesper Dangaard Brouer 	int i, err;
1909986a4f4dSJason Wang 
19105a159128SJason Wang 	enable_delayed_refill(vi);
19115a159128SJason Wang 
1912e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1913e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
1914986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
1915946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1916986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
1917754b8a21SJesper Dangaard Brouer 
19185306623aSFeng Liu 		err = virtnet_enable_queue_pair(vi, i);
1919754b8a21SJesper Dangaard Brouer 		if (err < 0)
19205306623aSFeng Liu 			goto err_enable_qp;
1921986a4f4dSJason Wang 	}
1922986a4f4dSJason Wang 
1923986a4f4dSJason Wang 	return 0;
19245306623aSFeng Liu 
19255306623aSFeng Liu err_enable_qp:
19265306623aSFeng Liu 	disable_delayed_refill(vi);
19275306623aSFeng Liu 	cancel_delayed_work_sync(&vi->refill);
19285306623aSFeng Liu 
19295306623aSFeng Liu 	for (i--; i >= 0; i--)
19305306623aSFeng Liu 		virtnet_disable_queue_pair(vi, i);
19315306623aSFeng Liu 	return err;
1932986a4f4dSJason Wang }
1933986a4f4dSJason Wang 
1934b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1935b92f1e67SWillem de Bruijn {
1936b92f1e67SWillem de Bruijn 	struct send_queue *sq = container_of(napi, struct send_queue, napi);
1937b92f1e67SWillem de Bruijn 	struct virtnet_info *vi = sq->vq->vdev->priv;
1938534da5e8SToshiaki Makita 	unsigned int index = vq2txq(sq->vq);
1939534da5e8SToshiaki Makita 	struct netdev_queue *txq;
19405a2f966dSMichael S. Tsirkin 	int opaque;
19415a2f966dSMichael S. Tsirkin 	bool done;
1942b92f1e67SWillem de Bruijn 
1943534da5e8SToshiaki Makita 	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1944534da5e8SToshiaki Makita 		/* We don't need to enable cb for XDP */
1945534da5e8SToshiaki Makita 		napi_complete_done(napi, 0);
1946534da5e8SToshiaki Makita 		return 0;
1947534da5e8SToshiaki Makita 	}
1948534da5e8SToshiaki Makita 
1949534da5e8SToshiaki Makita 	txq = netdev_get_tx_queue(vi->dev, index);
1950b92f1e67SWillem de Bruijn 	__netif_tx_lock(txq, raw_smp_processor_id());
19515a2f966dSMichael S. Tsirkin 	virtqueue_disable_cb(sq->vq);
1952df133f3fSMichael S. Tsirkin 	free_old_xmit_skbs(sq, true);
19535a2f966dSMichael S. Tsirkin 
195422bc63c5SMichael S. Tsirkin 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
195522bc63c5SMichael S. Tsirkin 		netif_tx_wake_queue(txq);
195622bc63c5SMichael S. Tsirkin 
19575a2f966dSMichael S. Tsirkin 	opaque = virtqueue_enable_cb_prepare(sq->vq);
19585a2f966dSMichael S. Tsirkin 
19595a2f966dSMichael S. Tsirkin 	done = napi_complete_done(napi, 0);
19605a2f966dSMichael S. Tsirkin 
19615a2f966dSMichael S. Tsirkin 	if (!done)
19625a2f966dSMichael S. Tsirkin 		virtqueue_disable_cb(sq->vq);
19635a2f966dSMichael S. Tsirkin 
1964b92f1e67SWillem de Bruijn 	__netif_tx_unlock(txq);
1965b92f1e67SWillem de Bruijn 
19665a2f966dSMichael S. Tsirkin 	if (done) {
19675a2f966dSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(sq->vq, opaque))) {
19685a2f966dSMichael S. Tsirkin 			if (napi_schedule_prep(napi)) {
19695a2f966dSMichael S. Tsirkin 				__netif_tx_lock(txq, raw_smp_processor_id());
19705a2f966dSMichael S. Tsirkin 				virtqueue_disable_cb(sq->vq);
19715a2f966dSMichael S. Tsirkin 				__netif_tx_unlock(txq);
19725a2f966dSMichael S. Tsirkin 				__napi_schedule(napi);
19735a2f966dSMichael S. Tsirkin 			}
19745a2f966dSMichael S. Tsirkin 		}
19755a2f966dSMichael S. Tsirkin 	}
1976b92f1e67SWillem de Bruijn 
1977b92f1e67SWillem de Bruijn 	return 0;
1978b92f1e67SWillem de Bruijn }
1979b92f1e67SWillem de Bruijn 
1980e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1981296f96fcSRusty Russell {
1982012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1983296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1984e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
1985e2fcad58SJason A. Donenfeld 	int num_sg;
1986012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
1987e7428e95SMichael S. Tsirkin 	bool can_push;
1988296f96fcSRusty Russell 
1989e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1990e7428e95SMichael S. Tsirkin 
1991e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
1992e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1993e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1994e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
1995e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
1996e7428e95SMichael S. Tsirkin 	if (can_push)
1997012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1998e7428e95SMichael S. Tsirkin 	else
1999e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
2000296f96fcSRusty Russell 
2001e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
2002fd3a8862SWillem de Bruijn 				    virtio_is_little_endian(vi->vdev), false,
2003fd3a8862SWillem de Bruijn 				    0))
200485eb1389SXianting Tian 		return -EPROTO;
2005296f96fcSRusty Russell 
2006e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
2007012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
20083f2c31d9SMark McLoughlin 
2009547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
2010e7428e95SMichael S. Tsirkin 	if (can_push) {
2011e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
2012e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
2013e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
2014e2fcad58SJason A. Donenfeld 			return num_sg;
2015e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
2016e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
2017e7428e95SMichael S. Tsirkin 	} else {
2018e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
2019e2fcad58SJason A. Donenfeld 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
2020e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
2021e2fcad58SJason A. Donenfeld 			return num_sg;
2022e2fcad58SJason A. Donenfeld 		num_sg++;
2023e7428e95SMichael S. Tsirkin 	}
20249dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
202511a3a154SRusty Russell }
202611a3a154SRusty Russell 
2027424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
202899ffc696SRusty Russell {
202999ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
2030986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
2031986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
20329ed4cb07SRusty Russell 	int err;
20334b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
20346b16f9eeSFlorian Westphal 	bool kick = !netdev_xmit_more();
2035b92f1e67SWillem de Bruijn 	bool use_napi = sq->napi.weight;
20362cb9c6baSRusty Russell 
20372cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
2038a7766ef1SMichael S. Tsirkin 	do {
2039a7766ef1SMichael S. Tsirkin 		if (use_napi)
2040a7766ef1SMichael S. Tsirkin 			virtqueue_disable_cb(sq->vq);
2041a7766ef1SMichael S. Tsirkin 
2042df133f3fSMichael S. Tsirkin 		free_old_xmit_skbs(sq, false);
204399ffc696SRusty Russell 
2044a7766ef1SMichael S. Tsirkin 	} while (use_napi && kick &&
2045a7766ef1SMichael S. Tsirkin 	       unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
2046bdb12e0dSWillem de Bruijn 
2047074c3582SJacob Keller 	/* timestamp packet in software */
2048074c3582SJacob Keller 	skb_tx_timestamp(skb);
2049074c3582SJacob Keller 
205003f191baSMichael S. Tsirkin 	/* Try to transmit */
2051b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
205299ffc696SRusty Russell 
20539ed4cb07SRusty Russell 	/* This should not happen! */
2054681daee2SJason Wang 	if (unlikely(err)) {
205558eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
20562e57b79cSRick Jones 		if (net_ratelimit())
205758eba97dSRusty Russell 			dev_warn(&dev->dev,
20587934b481SYuval Shaia 				 "Unexpected TXQ (%d) queue failure: %d\n",
20597934b481SYuval Shaia 				 qnum, err);
206058eba97dSRusty Russell 		dev->stats.tx_dropped++;
206185e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
206258eba97dSRusty Russell 		return NETDEV_TX_OK;
2063296f96fcSRusty Russell 	}
206403f191baSMichael S. Tsirkin 
206548925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
2066b92f1e67SWillem de Bruijn 	if (!use_napi) {
206748925e37SRusty Russell 		skb_orphan(skb);
2068895b5c9fSFlorian Westphal 		nf_reset_ct(skb);
2069b92f1e67SWillem de Bruijn 	}
207048925e37SRusty Russell 
2071b8ef4809SXuan Zhuo 	check_sq_full_and_disable(vi, dev, sq);
207248925e37SRusty Russell 
2073461f03dcSToshiaki Makita 	if (kick || netif_xmit_stopped(txq)) {
2074461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
2075461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
2076461f03dcSToshiaki Makita 			sq->stats.kicks++;
2077461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
2078461f03dcSToshiaki Makita 		}
2079461f03dcSToshiaki Makita 	}
20800b725a2cSDavid S. Miller 
20810b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
2082c223a078SDavid S. Miller }
2083c223a078SDavid S. Miller 
20846a4763e2SXuan Zhuo static int virtnet_rx_resize(struct virtnet_info *vi,
20856a4763e2SXuan Zhuo 			     struct receive_queue *rq, u32 ring_num)
20866a4763e2SXuan Zhuo {
20876a4763e2SXuan Zhuo 	bool running = netif_running(vi->dev);
20886a4763e2SXuan Zhuo 	int err, qindex;
20896a4763e2SXuan Zhuo 
20906a4763e2SXuan Zhuo 	qindex = rq - vi->rq;
20916a4763e2SXuan Zhuo 
20926a4763e2SXuan Zhuo 	if (running)
20936a4763e2SXuan Zhuo 		napi_disable(&rq->napi);
20946a4763e2SXuan Zhuo 
20956a4763e2SXuan Zhuo 	err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_free_unused_buf);
20966a4763e2SXuan Zhuo 	if (err)
20976a4763e2SXuan Zhuo 		netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
20986a4763e2SXuan Zhuo 
20996a4763e2SXuan Zhuo 	if (!try_fill_recv(vi, rq, GFP_KERNEL))
21006a4763e2SXuan Zhuo 		schedule_delayed_work(&vi->refill, 0);
21016a4763e2SXuan Zhuo 
21026a4763e2SXuan Zhuo 	if (running)
21036a4763e2SXuan Zhuo 		virtnet_napi_enable(rq->vq, &rq->napi);
21046a4763e2SXuan Zhuo 	return err;
21056a4763e2SXuan Zhuo }
21066a4763e2SXuan Zhuo 
2107ebcce492SXuan Zhuo static int virtnet_tx_resize(struct virtnet_info *vi,
2108ebcce492SXuan Zhuo 			     struct send_queue *sq, u32 ring_num)
2109ebcce492SXuan Zhuo {
2110ebcce492SXuan Zhuo 	bool running = netif_running(vi->dev);
2111ebcce492SXuan Zhuo 	struct netdev_queue *txq;
2112ebcce492SXuan Zhuo 	int err, qindex;
2113ebcce492SXuan Zhuo 
2114ebcce492SXuan Zhuo 	qindex = sq - vi->sq;
2115ebcce492SXuan Zhuo 
2116ebcce492SXuan Zhuo 	if (running)
2117ebcce492SXuan Zhuo 		virtnet_napi_tx_disable(&sq->napi);
2118ebcce492SXuan Zhuo 
2119ebcce492SXuan Zhuo 	txq = netdev_get_tx_queue(vi->dev, qindex);
2120ebcce492SXuan Zhuo 
2121ebcce492SXuan Zhuo 	/* 1. wait all ximt complete
2122ebcce492SXuan Zhuo 	 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
2123ebcce492SXuan Zhuo 	 */
2124ebcce492SXuan Zhuo 	__netif_tx_lock_bh(txq);
2125ebcce492SXuan Zhuo 
2126ebcce492SXuan Zhuo 	/* Prevent rx poll from accessing sq. */
2127ebcce492SXuan Zhuo 	sq->reset = true;
2128ebcce492SXuan Zhuo 
2129ebcce492SXuan Zhuo 	/* Prevent the upper layer from trying to send packets. */
2130ebcce492SXuan Zhuo 	netif_stop_subqueue(vi->dev, qindex);
2131ebcce492SXuan Zhuo 
2132ebcce492SXuan Zhuo 	__netif_tx_unlock_bh(txq);
2133ebcce492SXuan Zhuo 
2134ebcce492SXuan Zhuo 	err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
2135ebcce492SXuan Zhuo 	if (err)
2136ebcce492SXuan Zhuo 		netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
2137ebcce492SXuan Zhuo 
2138ebcce492SXuan Zhuo 	__netif_tx_lock_bh(txq);
2139ebcce492SXuan Zhuo 	sq->reset = false;
2140ebcce492SXuan Zhuo 	netif_tx_wake_queue(txq);
2141ebcce492SXuan Zhuo 	__netif_tx_unlock_bh(txq);
2142ebcce492SXuan Zhuo 
2143ebcce492SXuan Zhuo 	if (running)
2144ebcce492SXuan Zhuo 		virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
2145ebcce492SXuan Zhuo 	return err;
2146ebcce492SXuan Zhuo }
2147ebcce492SXuan Zhuo 
214840cbfc37SAmos Kong /*
214940cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
215040cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
2151788a8b6dSstephen hemminger  * never fail unless improperly formatted.
215240cbfc37SAmos Kong  */
215340cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
2154d24bae32Sstephen hemminger 				 struct scatterlist *out)
215540cbfc37SAmos Kong {
2156f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
2157d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
2158222722bcSYunjian Wang 	int ret;
215940cbfc37SAmos Kong 
216040cbfc37SAmos Kong 	/* Caller should know better */
2161f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
216240cbfc37SAmos Kong 
216312e57169SMichael S. Tsirkin 	vi->ctrl->status = ~0;
216412e57169SMichael S. Tsirkin 	vi->ctrl->hdr.class = class;
216512e57169SMichael S. Tsirkin 	vi->ctrl->hdr.cmd = cmd;
2166f7bc9594SRusty Russell 	/* Add header */
216712e57169SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
2168f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
216940cbfc37SAmos Kong 
2170f7bc9594SRusty Russell 	if (out)
2171f7bc9594SRusty Russell 		sgs[out_num++] = out;
217240cbfc37SAmos Kong 
2173f7bc9594SRusty Russell 	/* Add return status. */
217412e57169SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
2175d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
217640cbfc37SAmos Kong 
2177d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
2178222722bcSYunjian Wang 	ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
2179222722bcSYunjian Wang 	if (ret < 0) {
2180222722bcSYunjian Wang 		dev_warn(&vi->vdev->dev,
2181222722bcSYunjian Wang 			 "Failed to add sgs for command vq: %d\n.", ret);
2182222722bcSYunjian Wang 		return false;
2183222722bcSYunjian Wang 	}
218440cbfc37SAmos Kong 
218567975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
218612e57169SMichael S. Tsirkin 		return vi->ctrl->status == VIRTIO_NET_OK;
218740cbfc37SAmos Kong 
218840cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
218940cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
219040cbfc37SAmos Kong 	 */
2191047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2192047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
219340cbfc37SAmos Kong 		cpu_relax();
219440cbfc37SAmos Kong 
219512e57169SMichael S. Tsirkin 	return vi->ctrl->status == VIRTIO_NET_OK;
219640cbfc37SAmos Kong }
219740cbfc37SAmos Kong 
21989c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
21999c46f6d4SAlex Williamson {
22009c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
22019c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
2202f2f2c8b4SJiri Pirko 	int ret;
2203e37e2ff3SAndy Lutomirski 	struct sockaddr *addr;
22047e58d5aeSAmos Kong 	struct scatterlist sg;
22059c46f6d4SAlex Williamson 
2206ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2207ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
2208ba5e4426SSridhar Samudrala 
2209801822d1SShyam Saini 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2210e37e2ff3SAndy Lutomirski 	if (!addr)
2211e37e2ff3SAndy Lutomirski 		return -ENOMEM;
2212e37e2ff3SAndy Lutomirski 
2213e37e2ff3SAndy Lutomirski 	ret = eth_prepare_mac_addr_change(dev, addr);
2214f2f2c8b4SJiri Pirko 	if (ret)
2215e37e2ff3SAndy Lutomirski 		goto out;
22169c46f6d4SAlex Williamson 
22177e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
22187e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
22197e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2220d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
22217e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
22227e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
2223e37e2ff3SAndy Lutomirski 			ret = -EINVAL;
2224e37e2ff3SAndy Lutomirski 			goto out;
22257e58d5aeSAmos Kong 		}
22267e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
22277e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2228855e0c52SRusty Russell 		unsigned int i;
2229855e0c52SRusty Russell 
2230855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
2231855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
2232855e0c52SRusty Russell 			virtio_cwrite8(vdev,
2233855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
2234855e0c52SRusty Russell 				       i, addr->sa_data[i]);
22357e58d5aeSAmos Kong 	}
22367e58d5aeSAmos Kong 
22377e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
2238e37e2ff3SAndy Lutomirski 	ret = 0;
22399c46f6d4SAlex Williamson 
2240e37e2ff3SAndy Lutomirski out:
2241e37e2ff3SAndy Lutomirski 	kfree(addr);
2242e37e2ff3SAndy Lutomirski 	return ret;
22439c46f6d4SAlex Williamson }
22449c46f6d4SAlex Williamson 
2245bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev,
22463fa2a1dfSstephen hemminger 			  struct rtnl_link_stats64 *tot)
22473fa2a1dfSstephen hemminger {
22483fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
22493fa2a1dfSstephen hemminger 	unsigned int start;
2250d7dfc5cfSToshiaki Makita 	int i;
22513fa2a1dfSstephen hemminger 
2252d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
2253a520794bSTony Lu 		u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2254d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
2255d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
22563fa2a1dfSstephen hemminger 
22573fa2a1dfSstephen hemminger 		do {
2258068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&sq->stats.syncp);
2259d7dfc5cfSToshiaki Makita 			tpackets = sq->stats.packets;
2260d7dfc5cfSToshiaki Makita 			tbytes   = sq->stats.bytes;
2261a520794bSTony Lu 			terrors  = sq->stats.tx_timeouts;
2262068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
226383a27052SEric Dumazet 
226483a27052SEric Dumazet 		do {
2265068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq->stats.syncp);
2266d46eeeafSJason Wang 			rpackets = rq->stats.packets;
2267d46eeeafSJason Wang 			rbytes   = rq->stats.bytes;
2268d46eeeafSJason Wang 			rdrops   = rq->stats.drops;
2269068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq->stats.syncp, start));
22703fa2a1dfSstephen hemminger 
22713fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
22723fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
22733fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
22743fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
22752c4a2f7dSToshiaki Makita 		tot->rx_dropped += rdrops;
2276a520794bSTony Lu 		tot->tx_errors  += terrors;
22773fa2a1dfSstephen hemminger 	}
22783fa2a1dfSstephen hemminger 
22793fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
2280021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
22813fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
22823fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
22833fa2a1dfSstephen hemminger }
22843fa2a1dfSstephen hemminger 
2285586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
2286586d17c5SJason Wang {
2287586d17c5SJason Wang 	rtnl_lock();
2288586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2289d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2290586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2291586d17c5SJason Wang 	rtnl_unlock();
2292586d17c5SJason Wang }
2293586d17c5SJason Wang 
229447315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2295986a4f4dSJason Wang {
2296986a4f4dSJason Wang 	struct scatterlist sg;
2297986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
2298986a4f4dSJason Wang 
2299986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2300986a4f4dSJason Wang 		return 0;
2301986a4f4dSJason Wang 
230212e57169SMichael S. Tsirkin 	vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
230312e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2304986a4f4dSJason Wang 
2305986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2306d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2307986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2308986a4f4dSJason Wang 			 queue_pairs);
2309986a4f4dSJason Wang 		return -EINVAL;
231055257d72SSasha Levin 	} else {
2311986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
231235ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
231335ed159bSJason Wang 		if (dev->flags & IFF_UP)
23149b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
231555257d72SSasha Levin 	}
2316986a4f4dSJason Wang 
2317986a4f4dSJason Wang 	return 0;
2318986a4f4dSJason Wang }
2319986a4f4dSJason Wang 
232047315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
232147315329SJohn Fastabend {
232247315329SJohn Fastabend 	int err;
232347315329SJohn Fastabend 
232447315329SJohn Fastabend 	rtnl_lock();
232547315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
232647315329SJohn Fastabend 	rtnl_unlock();
232747315329SJohn Fastabend 	return err;
232847315329SJohn Fastabend }
232947315329SJohn Fastabend 
2330296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
2331296f96fcSRusty Russell {
2332296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
2333986a4f4dSJason Wang 	int i;
2334296f96fcSRusty Russell 
23355a159128SJason Wang 	/* Make sure NAPI doesn't schedule refill work */
23365a159128SJason Wang 	disable_delayed_refill(vi);
2337b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
2338b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
2339986a4f4dSJason Wang 
23405306623aSFeng Liu 	for (i = 0; i < vi->max_queue_pairs; i++)
23415306623aSFeng Liu 		virtnet_disable_queue_pair(vi, i);
2342296f96fcSRusty Russell 
2343296f96fcSRusty Russell 	return 0;
2344296f96fcSRusty Russell }
2345296f96fcSRusty Russell 
23462af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
23472af7698eSAlex Williamson {
23482af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
2349f565a7c2SAlex Williamson 	struct scatterlist sg[2];
2350f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
2351ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
235232e7bfc4SJiri Pirko 	int uc_count;
23534cd24eafSJiri Pirko 	int mc_count;
2354f565a7c2SAlex Williamson 	void *buf;
2355f565a7c2SAlex Williamson 	int i;
23562af7698eSAlex Williamson 
2357788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
23582af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
23592af7698eSAlex Williamson 		return;
23602af7698eSAlex Williamson 
236112e57169SMichael S. Tsirkin 	vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
236212e57169SMichael S. Tsirkin 	vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
23632af7698eSAlex Williamson 
236412e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
23652af7698eSAlex Williamson 
23662af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2367d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
23682af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
236912e57169SMichael S. Tsirkin 			 vi->ctrl->promisc ? "en" : "dis");
23702af7698eSAlex Williamson 
237112e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
23722af7698eSAlex Williamson 
23732af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2374d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
23752af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
237612e57169SMichael S. Tsirkin 			 vi->ctrl->allmulti ? "en" : "dis");
2377f565a7c2SAlex Williamson 
237832e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
23794cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
2380f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
23814cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2382f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
23834cd24eafSJiri Pirko 	mac_data = buf;
2384e68ed8f0SJoe Perches 	if (!buf)
2385f565a7c2SAlex Williamson 		return;
2386f565a7c2SAlex Williamson 
238723e258e1SAlex Williamson 	sg_init_table(sg, 2);
238823e258e1SAlex Williamson 
2389f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
2390fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2391ccffad25SJiri Pirko 	i = 0;
239232e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
2393ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2394f565a7c2SAlex Williamson 
2395f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
239632e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2397f565a7c2SAlex Williamson 
2398f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
239932e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
2400f565a7c2SAlex Williamson 
2401fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2402567ec874SJiri Pirko 	i = 0;
240322bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
240422bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2405f565a7c2SAlex Williamson 
2406f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
24074cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2408f565a7c2SAlex Williamson 
2409f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2410d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
241199e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2412f565a7c2SAlex Williamson 
2413f565a7c2SAlex Williamson 	kfree(buf);
24142af7698eSAlex Williamson }
24152af7698eSAlex Williamson 
241680d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
241780d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
24180bde9569SAlex Williamson {
24190bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
24200bde9569SAlex Williamson 	struct scatterlist sg;
24210bde9569SAlex Williamson 
2422d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
242312e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
24240bde9569SAlex Williamson 
24250bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2426d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
24270bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
24288e586137SJiri Pirko 	return 0;
24290bde9569SAlex Williamson }
24300bde9569SAlex Williamson 
243180d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
243280d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
24330bde9569SAlex Williamson {
24340bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
24350bde9569SAlex Williamson 	struct scatterlist sg;
24360bde9569SAlex Williamson 
2437d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
243812e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
24390bde9569SAlex Williamson 
24400bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2441d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
24420bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
24438e586137SJiri Pirko 	return 0;
24440bde9569SAlex Williamson }
24450bde9569SAlex Williamson 
2446310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi)
2447986a4f4dSJason Wang {
2448986a4f4dSJason Wang 	int i;
24498898c21cSWanlong Gao 
24508898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
24518898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
245219e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->rq[i].vq, NULL);
245319e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->sq[i].vq, NULL);
24548898c21cSWanlong Gao 		}
24558898c21cSWanlong Gao 
24568898c21cSWanlong Gao 		vi->affinity_hint_set = false;
24578898c21cSWanlong Gao 	}
24588898c21cSWanlong Gao }
24598898c21cSWanlong Gao 
24608898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
2461986a4f4dSJason Wang {
24622ca653d6SCaleb Raitto 	cpumask_var_t mask;
24632ca653d6SCaleb Raitto 	int stragglers;
24642ca653d6SCaleb Raitto 	int group_size;
24652ca653d6SCaleb Raitto 	int i, j, cpu;
24662ca653d6SCaleb Raitto 	int num_cpu;
24672ca653d6SCaleb Raitto 	int stride;
2468986a4f4dSJason Wang 
24692ca653d6SCaleb Raitto 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2470310974faSPeter Xu 		virtnet_clean_affinity(vi);
2471986a4f4dSJason Wang 		return;
2472986a4f4dSJason Wang 	}
2473986a4f4dSJason Wang 
24742ca653d6SCaleb Raitto 	num_cpu = num_online_cpus();
24752ca653d6SCaleb Raitto 	stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
24762ca653d6SCaleb Raitto 	stragglers = num_cpu >= vi->curr_queue_pairs ?
24772ca653d6SCaleb Raitto 			num_cpu % vi->curr_queue_pairs :
24782ca653d6SCaleb Raitto 			0;
24799b51d9d8SYury Norov 	cpu = cpumask_first(cpu_online_mask);
24804d99f660SAndrei Vagin 
24812ca653d6SCaleb Raitto 	for (i = 0; i < vi->curr_queue_pairs; i++) {
24822ca653d6SCaleb Raitto 		group_size = stride + (i < stragglers ? 1 : 0);
24832ca653d6SCaleb Raitto 
24842ca653d6SCaleb Raitto 		for (j = 0; j < group_size; j++) {
24852ca653d6SCaleb Raitto 			cpumask_set_cpu(cpu, mask);
24862ca653d6SCaleb Raitto 			cpu = cpumask_next_wrap(cpu, cpu_online_mask,
24872ca653d6SCaleb Raitto 						nr_cpu_ids, false);
24882ca653d6SCaleb Raitto 		}
24892ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->rq[i].vq, mask);
24902ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->sq[i].vq, mask);
2491044ab86dSAntoine Tenart 		__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
24922ca653d6SCaleb Raitto 		cpumask_clear(mask);
2493986a4f4dSJason Wang 	}
2494986a4f4dSJason Wang 
2495986a4f4dSJason Wang 	vi->affinity_hint_set = true;
24962ca653d6SCaleb Raitto 	free_cpumask_var(mask);
249747be2479SWanlong Gao }
2498986a4f4dSJason Wang 
24998017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
25008de4b2f3SWanlong Gao {
25018017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
25028017c279SSebastian Andrzej Siewior 						   node);
25038de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
25048017c279SSebastian Andrzej Siewior 	return 0;
25058de4b2f3SWanlong Gao }
25063ab098dfSJason Wang 
25078017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
25088017c279SSebastian Andrzej Siewior {
25098017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
25108017c279SSebastian Andrzej Siewior 						   node_dead);
25118017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
25128017c279SSebastian Andrzej Siewior 	return 0;
25138017c279SSebastian Andrzej Siewior }
25148017c279SSebastian Andrzej Siewior 
25158017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
25168017c279SSebastian Andrzej Siewior {
25178017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
25188017c279SSebastian Andrzej Siewior 						   node);
25198017c279SSebastian Andrzej Siewior 
2520310974faSPeter Xu 	virtnet_clean_affinity(vi);
25218017c279SSebastian Andrzej Siewior 	return 0;
25228017c279SSebastian Andrzej Siewior }
25238017c279SSebastian Andrzej Siewior 
25248017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
25258017c279SSebastian Andrzej Siewior 
25268017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
25278017c279SSebastian Andrzej Siewior {
25288017c279SSebastian Andrzej Siewior 	int ret;
25298017c279SSebastian Andrzej Siewior 
25308017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
25318017c279SSebastian Andrzej Siewior 	if (ret)
25328017c279SSebastian Andrzej Siewior 		return ret;
25338017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
25348017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
25358017c279SSebastian Andrzej Siewior 	if (!ret)
25368017c279SSebastian Andrzej Siewior 		return ret;
25378017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
25388017c279SSebastian Andrzej Siewior 	return ret;
25398017c279SSebastian Andrzej Siewior }
25408017c279SSebastian Andrzej Siewior 
25418017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
25428017c279SSebastian Andrzej Siewior {
25438017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
25448017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
25458017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
2546a9ea3fc6SHerbert Xu }
2547a9ea3fc6SHerbert Xu 
25488f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
254974624944SHao Chen 				  struct ethtool_ringparam *ring,
255074624944SHao Chen 				  struct kernel_ethtool_ringparam *kernel_ring,
255174624944SHao Chen 				  struct netlink_ext_ack *extack)
25528f9f4668SRick Jones {
25538f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
25548f9f4668SRick Jones 
25558597b5ddSXuan Zhuo 	ring->rx_max_pending = vi->rq[0].vq->num_max;
25568597b5ddSXuan Zhuo 	ring->tx_max_pending = vi->sq[0].vq->num_max;
25578597b5ddSXuan Zhuo 	ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
25588597b5ddSXuan Zhuo 	ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
25598f9f4668SRick Jones }
25608f9f4668SRick Jones 
2561a335b33fSXuan Zhuo static int virtnet_set_ringparam(struct net_device *dev,
2562a335b33fSXuan Zhuo 				 struct ethtool_ringparam *ring,
2563a335b33fSXuan Zhuo 				 struct kernel_ethtool_ringparam *kernel_ring,
2564a335b33fSXuan Zhuo 				 struct netlink_ext_ack *extack)
2565a335b33fSXuan Zhuo {
2566a335b33fSXuan Zhuo 	struct virtnet_info *vi = netdev_priv(dev);
2567a335b33fSXuan Zhuo 	u32 rx_pending, tx_pending;
2568a335b33fSXuan Zhuo 	struct receive_queue *rq;
2569a335b33fSXuan Zhuo 	struct send_queue *sq;
2570a335b33fSXuan Zhuo 	int i, err;
2571a335b33fSXuan Zhuo 
2572a335b33fSXuan Zhuo 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2573a335b33fSXuan Zhuo 		return -EINVAL;
2574a335b33fSXuan Zhuo 
2575a335b33fSXuan Zhuo 	rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2576a335b33fSXuan Zhuo 	tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2577a335b33fSXuan Zhuo 
2578a335b33fSXuan Zhuo 	if (ring->rx_pending == rx_pending &&
2579a335b33fSXuan Zhuo 	    ring->tx_pending == tx_pending)
2580a335b33fSXuan Zhuo 		return 0;
2581a335b33fSXuan Zhuo 
2582a335b33fSXuan Zhuo 	if (ring->rx_pending > vi->rq[0].vq->num_max)
2583a335b33fSXuan Zhuo 		return -EINVAL;
2584a335b33fSXuan Zhuo 
2585a335b33fSXuan Zhuo 	if (ring->tx_pending > vi->sq[0].vq->num_max)
2586a335b33fSXuan Zhuo 		return -EINVAL;
2587a335b33fSXuan Zhuo 
2588a335b33fSXuan Zhuo 	for (i = 0; i < vi->max_queue_pairs; i++) {
2589a335b33fSXuan Zhuo 		rq = vi->rq + i;
2590a335b33fSXuan Zhuo 		sq = vi->sq + i;
2591a335b33fSXuan Zhuo 
2592a335b33fSXuan Zhuo 		if (ring->tx_pending != tx_pending) {
2593a335b33fSXuan Zhuo 			err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2594a335b33fSXuan Zhuo 			if (err)
2595a335b33fSXuan Zhuo 				return err;
2596a335b33fSXuan Zhuo 		}
2597a335b33fSXuan Zhuo 
2598a335b33fSXuan Zhuo 		if (ring->rx_pending != rx_pending) {
2599a335b33fSXuan Zhuo 			err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2600a335b33fSXuan Zhuo 			if (err)
2601a335b33fSXuan Zhuo 				return err;
2602a335b33fSXuan Zhuo 		}
2603a335b33fSXuan Zhuo 	}
2604a335b33fSXuan Zhuo 
2605a335b33fSXuan Zhuo 	return 0;
2606a9ea3fc6SHerbert Xu }
2607a9ea3fc6SHerbert Xu 
2608c7114b12SAndrew Melnychenko static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2609c7114b12SAndrew Melnychenko {
2610c7114b12SAndrew Melnychenko 	struct net_device *dev = vi->dev;
2611c7114b12SAndrew Melnychenko 	struct scatterlist sgs[4];
2612c7114b12SAndrew Melnychenko 	unsigned int sg_buf_size;
2613c7114b12SAndrew Melnychenko 
2614c7114b12SAndrew Melnychenko 	/* prepare sgs */
2615c7114b12SAndrew Melnychenko 	sg_init_table(sgs, 4);
2616c7114b12SAndrew Melnychenko 
2617c7114b12SAndrew Melnychenko 	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2618c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2619c7114b12SAndrew Melnychenko 
2620c7114b12SAndrew Melnychenko 	sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2621c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2622c7114b12SAndrew Melnychenko 
2623c7114b12SAndrew Melnychenko 	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2624c7114b12SAndrew Melnychenko 			- offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2625c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2626c7114b12SAndrew Melnychenko 
2627c7114b12SAndrew Melnychenko 	sg_buf_size = vi->rss_key_size;
2628c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2629c7114b12SAndrew Melnychenko 
2630c7114b12SAndrew Melnychenko 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
263191f41f01SAndrew Melnychenko 				  vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
263291f41f01SAndrew Melnychenko 				  : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2633c7114b12SAndrew Melnychenko 		dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2634c7114b12SAndrew Melnychenko 		return false;
2635c7114b12SAndrew Melnychenko 	}
2636c7114b12SAndrew Melnychenko 	return true;
2637c7114b12SAndrew Melnychenko }
2638c7114b12SAndrew Melnychenko 
2639c7114b12SAndrew Melnychenko static void virtnet_init_default_rss(struct virtnet_info *vi)
2640c7114b12SAndrew Melnychenko {
2641c7114b12SAndrew Melnychenko 	u32 indir_val = 0;
2642c7114b12SAndrew Melnychenko 	int i = 0;
2643c7114b12SAndrew Melnychenko 
2644c7114b12SAndrew Melnychenko 	vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2645c1170820SAndrew Melnychenko 	vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2646c7114b12SAndrew Melnychenko 	vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2647c7114b12SAndrew Melnychenko 						? vi->rss_indir_table_size - 1 : 0;
2648c7114b12SAndrew Melnychenko 	vi->ctrl->rss.unclassified_queue = 0;
2649c7114b12SAndrew Melnychenko 
2650c7114b12SAndrew Melnychenko 	for (; i < vi->rss_indir_table_size; ++i) {
2651c7114b12SAndrew Melnychenko 		indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2652c7114b12SAndrew Melnychenko 		vi->ctrl->rss.indirection_table[i] = indir_val;
2653c7114b12SAndrew Melnychenko 	}
2654c7114b12SAndrew Melnychenko 
2655c7114b12SAndrew Melnychenko 	vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs;
2656c7114b12SAndrew Melnychenko 	vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2657c7114b12SAndrew Melnychenko 
2658c7114b12SAndrew Melnychenko 	netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
2659c7114b12SAndrew Melnychenko }
2660c7114b12SAndrew Melnychenko 
2661c1170820SAndrew Melnychenko static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
2662c1170820SAndrew Melnychenko {
2663c1170820SAndrew Melnychenko 	info->data = 0;
2664c1170820SAndrew Melnychenko 	switch (info->flow_type) {
2665c1170820SAndrew Melnychenko 	case TCP_V4_FLOW:
2666c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
2667c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2668c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2669c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2670c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2671c1170820SAndrew Melnychenko 		}
2672c1170820SAndrew Melnychenko 		break;
2673c1170820SAndrew Melnychenko 	case TCP_V6_FLOW:
2674c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
2675c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2676c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2677c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2678c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2679c1170820SAndrew Melnychenko 		}
2680c1170820SAndrew Melnychenko 		break;
2681c1170820SAndrew Melnychenko 	case UDP_V4_FLOW:
2682c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
2683c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2684c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2685c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2686c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2687c1170820SAndrew Melnychenko 		}
2688c1170820SAndrew Melnychenko 		break;
2689c1170820SAndrew Melnychenko 	case UDP_V6_FLOW:
2690c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
2691c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2692c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2693c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2694c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2695c1170820SAndrew Melnychenko 		}
2696c1170820SAndrew Melnychenko 		break;
2697c1170820SAndrew Melnychenko 	case IPV4_FLOW:
2698c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
2699c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2700c1170820SAndrew Melnychenko 
2701c1170820SAndrew Melnychenko 		break;
2702c1170820SAndrew Melnychenko 	case IPV6_FLOW:
2703c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
2704c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2705c1170820SAndrew Melnychenko 
2706c1170820SAndrew Melnychenko 		break;
2707c1170820SAndrew Melnychenko 	default:
2708c1170820SAndrew Melnychenko 		info->data = 0;
2709c1170820SAndrew Melnychenko 		break;
2710c1170820SAndrew Melnychenko 	}
2711c1170820SAndrew Melnychenko }
2712c1170820SAndrew Melnychenko 
2713c1170820SAndrew Melnychenko static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
2714c1170820SAndrew Melnychenko {
2715c1170820SAndrew Melnychenko 	u32 new_hashtypes = vi->rss_hash_types_saved;
2716c1170820SAndrew Melnychenko 	bool is_disable = info->data & RXH_DISCARD;
2717c1170820SAndrew Melnychenko 	bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
2718c1170820SAndrew Melnychenko 
2719c1170820SAndrew Melnychenko 	/* supports only 'sd', 'sdfn' and 'r' */
2720c1170820SAndrew Melnychenko 	if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
2721c1170820SAndrew Melnychenko 		return false;
2722c1170820SAndrew Melnychenko 
2723c1170820SAndrew Melnychenko 	switch (info->flow_type) {
2724c1170820SAndrew Melnychenko 	case TCP_V4_FLOW:
2725c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
2726c1170820SAndrew Melnychenko 		if (!is_disable)
2727c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2728c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
2729c1170820SAndrew Melnychenko 		break;
2730c1170820SAndrew Melnychenko 	case UDP_V4_FLOW:
2731c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
2732c1170820SAndrew Melnychenko 		if (!is_disable)
2733c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2734c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
2735c1170820SAndrew Melnychenko 		break;
2736c1170820SAndrew Melnychenko 	case IPV4_FLOW:
2737c1170820SAndrew Melnychenko 		new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2738c1170820SAndrew Melnychenko 		if (!is_disable)
2739c1170820SAndrew Melnychenko 			new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2740c1170820SAndrew Melnychenko 		break;
2741c1170820SAndrew Melnychenko 	case TCP_V6_FLOW:
2742c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
2743c1170820SAndrew Melnychenko 		if (!is_disable)
2744c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2745c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
2746c1170820SAndrew Melnychenko 		break;
2747c1170820SAndrew Melnychenko 	case UDP_V6_FLOW:
2748c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
2749c1170820SAndrew Melnychenko 		if (!is_disable)
2750c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2751c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
2752c1170820SAndrew Melnychenko 		break;
2753c1170820SAndrew Melnychenko 	case IPV6_FLOW:
2754c1170820SAndrew Melnychenko 		new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2755c1170820SAndrew Melnychenko 		if (!is_disable)
2756c1170820SAndrew Melnychenko 			new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2757c1170820SAndrew Melnychenko 		break;
2758c1170820SAndrew Melnychenko 	default:
2759c1170820SAndrew Melnychenko 		/* unsupported flow */
2760c1170820SAndrew Melnychenko 		return false;
2761c1170820SAndrew Melnychenko 	}
2762c1170820SAndrew Melnychenko 
2763c1170820SAndrew Melnychenko 	/* if unsupported hashtype was set */
2764c1170820SAndrew Melnychenko 	if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
2765c1170820SAndrew Melnychenko 		return false;
2766c1170820SAndrew Melnychenko 
2767c1170820SAndrew Melnychenko 	if (new_hashtypes != vi->rss_hash_types_saved) {
2768c1170820SAndrew Melnychenko 		vi->rss_hash_types_saved = new_hashtypes;
2769c1170820SAndrew Melnychenko 		vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
2770c1170820SAndrew Melnychenko 		if (vi->dev->features & NETIF_F_RXHASH)
2771c1170820SAndrew Melnychenko 			return virtnet_commit_rss_command(vi);
2772c1170820SAndrew Melnychenko 	}
2773c1170820SAndrew Melnychenko 
2774c1170820SAndrew Melnychenko 	return true;
2775c1170820SAndrew Melnychenko }
277666846048SRick Jones 
277766846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
277866846048SRick Jones 				struct ethtool_drvinfo *info)
277966846048SRick Jones {
278066846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
278166846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
278266846048SRick Jones 
2783fb3ceec1SWolfram Sang 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2784fb3ceec1SWolfram Sang 	strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2785fb3ceec1SWolfram Sang 	strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
278666846048SRick Jones 
278766846048SRick Jones }
278866846048SRick Jones 
2789d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
2790d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
2791d73bcd2cSJason Wang 				struct ethtool_channels *channels)
2792d73bcd2cSJason Wang {
2793d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2794d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
2795d73bcd2cSJason Wang 	int err;
2796d73bcd2cSJason Wang 
2797d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
2798d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
2799d73bcd2cSJason Wang 	 */
2800d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
2801d73bcd2cSJason Wang 		return -EINVAL;
2802d73bcd2cSJason Wang 
2803c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2804d73bcd2cSJason Wang 		return -EINVAL;
2805d73bcd2cSJason Wang 
2806f600b690SJohn Fastabend 	/* For now we don't support modifying channels while XDP is loaded
2807f600b690SJohn Fastabend 	 * also when XDP is loaded all RX queues have XDP programs so we only
2808f600b690SJohn Fastabend 	 * need to check a single RX queue.
2809f600b690SJohn Fastabend 	 */
2810f600b690SJohn Fastabend 	if (vi->rq[0].xdp_prog)
2811f600b690SJohn Fastabend 		return -EINVAL;
2812f600b690SJohn Fastabend 
2813a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_lock();
281447315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
2815de33212fSJeff Dike 	if (err) {
2816a0d1d0f4SSebastian Andrzej Siewior 		cpus_read_unlock();
2817de33212fSJeff Dike 		goto err;
2818d73bcd2cSJason Wang 	}
2819de33212fSJeff Dike 	virtnet_set_affinity(vi);
2820a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_unlock();
2821d73bcd2cSJason Wang 
2822de33212fSJeff Dike 	netif_set_real_num_tx_queues(dev, queue_pairs);
2823de33212fSJeff Dike 	netif_set_real_num_rx_queues(dev, queue_pairs);
2824de33212fSJeff Dike  err:
2825d73bcd2cSJason Wang 	return err;
2826d73bcd2cSJason Wang }
2827d73bcd2cSJason Wang 
2828d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2829d7dfc5cfSToshiaki Makita {
2830d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2831d7dfc5cfSToshiaki Makita 	unsigned int i, j;
2832d7a9a01bSAlexander Duyck 	u8 *p = data;
2833d7dfc5cfSToshiaki Makita 
2834d7dfc5cfSToshiaki Makita 	switch (stringset) {
2835d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
2836d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2837d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2838d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2839d7a9a01bSAlexander Duyck 						virtnet_rq_stats_desc[j].desc);
2840d7dfc5cfSToshiaki Makita 		}
2841d7dfc5cfSToshiaki Makita 
2842d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2843d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2844d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2845d7a9a01bSAlexander Duyck 						virtnet_sq_stats_desc[j].desc);
2846d7dfc5cfSToshiaki Makita 		}
2847d7dfc5cfSToshiaki Makita 		break;
2848d7dfc5cfSToshiaki Makita 	}
2849d7dfc5cfSToshiaki Makita }
2850d7dfc5cfSToshiaki Makita 
2851d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset)
2852d7dfc5cfSToshiaki Makita {
2853d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2854d7dfc5cfSToshiaki Makita 
2855d7dfc5cfSToshiaki Makita 	switch (sset) {
2856d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
2857d7dfc5cfSToshiaki Makita 		return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2858d7dfc5cfSToshiaki Makita 					       VIRTNET_SQ_STATS_LEN);
2859d7dfc5cfSToshiaki Makita 	default:
2860d7dfc5cfSToshiaki Makita 		return -EOPNOTSUPP;
2861d7dfc5cfSToshiaki Makita 	}
2862d7dfc5cfSToshiaki Makita }
2863d7dfc5cfSToshiaki Makita 
2864d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev,
2865d7dfc5cfSToshiaki Makita 				      struct ethtool_stats *stats, u64 *data)
2866d7dfc5cfSToshiaki Makita {
2867d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2868d7dfc5cfSToshiaki Makita 	unsigned int idx = 0, start, i, j;
2869d7dfc5cfSToshiaki Makita 	const u8 *stats_base;
2870d7dfc5cfSToshiaki Makita 	size_t offset;
2871d7dfc5cfSToshiaki Makita 
2872d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2873d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
2874d7dfc5cfSToshiaki Makita 
2875d46eeeafSJason Wang 		stats_base = (u8 *)&rq->stats;
2876d7dfc5cfSToshiaki Makita 		do {
2877068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq->stats.syncp);
2878d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2879d7dfc5cfSToshiaki Makita 				offset = virtnet_rq_stats_desc[j].offset;
2880d7dfc5cfSToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
2881d7dfc5cfSToshiaki Makita 			}
2882068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2883d7dfc5cfSToshiaki Makita 		idx += VIRTNET_RQ_STATS_LEN;
2884d7dfc5cfSToshiaki Makita 	}
2885d7dfc5cfSToshiaki Makita 
2886d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2887d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
2888d7dfc5cfSToshiaki Makita 
2889d7dfc5cfSToshiaki Makita 		stats_base = (u8 *)&sq->stats;
2890d7dfc5cfSToshiaki Makita 		do {
2891068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&sq->stats.syncp);
2892d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2893d7dfc5cfSToshiaki Makita 				offset = virtnet_sq_stats_desc[j].offset;
2894d7dfc5cfSToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
2895d7dfc5cfSToshiaki Makita 			}
2896068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2897d7dfc5cfSToshiaki Makita 		idx += VIRTNET_SQ_STATS_LEN;
2898d7dfc5cfSToshiaki Makita 	}
2899d7dfc5cfSToshiaki Makita }
2900d7dfc5cfSToshiaki Makita 
2901d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
2902d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
2903d73bcd2cSJason Wang {
2904d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2905d73bcd2cSJason Wang 
2906d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
2907d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
2908d73bcd2cSJason Wang 	channels->max_other = 0;
2909d73bcd2cSJason Wang 	channels->rx_count = 0;
2910d73bcd2cSJason Wang 	channels->tx_count = 0;
2911d73bcd2cSJason Wang 	channels->other_count = 0;
2912d73bcd2cSJason Wang }
2913d73bcd2cSJason Wang 
2914ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev,
2915ebb6b4b1SPhilippe Reynes 				      const struct ethtool_link_ksettings *cmd)
291616032be5SNikolay Aleksandrov {
291716032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
291816032be5SNikolay Aleksandrov 
29199aedc6e2SCris Forno 	return ethtool_virtdev_set_link_ksettings(dev, cmd,
29209aedc6e2SCris Forno 						  &vi->speed, &vi->duplex);
292116032be5SNikolay Aleksandrov }
292216032be5SNikolay Aleksandrov 
2923ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev,
2924ebb6b4b1SPhilippe Reynes 				      struct ethtool_link_ksettings *cmd)
292516032be5SNikolay Aleksandrov {
292616032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
292716032be5SNikolay Aleksandrov 
2928ebb6b4b1SPhilippe Reynes 	cmd->base.speed = vi->speed;
2929ebb6b4b1SPhilippe Reynes 	cmd->base.duplex = vi->duplex;
2930ebb6b4b1SPhilippe Reynes 	cmd->base.port = PORT_OTHER;
293116032be5SNikolay Aleksandrov 
293216032be5SNikolay Aleksandrov 	return 0;
293316032be5SNikolay Aleksandrov }
293416032be5SNikolay Aleksandrov 
2935699b045aSAlvaro Karsz static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
2936699b045aSAlvaro Karsz 				       struct ethtool_coalesce *ec)
2937699b045aSAlvaro Karsz {
2938699b045aSAlvaro Karsz 	struct scatterlist sgs_tx, sgs_rx;
2939699b045aSAlvaro Karsz 
2940*accc1bf2SBrett Creeley 	vi->ctrl->coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
2941*accc1bf2SBrett Creeley 	vi->ctrl->coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
2942*accc1bf2SBrett Creeley 	sg_init_one(&sgs_tx, &vi->ctrl->coal_tx, sizeof(vi->ctrl->coal_tx));
2943699b045aSAlvaro Karsz 
2944699b045aSAlvaro Karsz 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
2945699b045aSAlvaro Karsz 				  VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
2946699b045aSAlvaro Karsz 				  &sgs_tx))
2947699b045aSAlvaro Karsz 		return -EINVAL;
2948699b045aSAlvaro Karsz 
2949699b045aSAlvaro Karsz 	/* Save parameters */
2950699b045aSAlvaro Karsz 	vi->tx_usecs = ec->tx_coalesce_usecs;
2951699b045aSAlvaro Karsz 	vi->tx_max_packets = ec->tx_max_coalesced_frames;
2952699b045aSAlvaro Karsz 
2953*accc1bf2SBrett Creeley 	vi->ctrl->coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
2954*accc1bf2SBrett Creeley 	vi->ctrl->coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
2955*accc1bf2SBrett Creeley 	sg_init_one(&sgs_rx, &vi->ctrl->coal_rx, sizeof(vi->ctrl->coal_rx));
2956699b045aSAlvaro Karsz 
2957699b045aSAlvaro Karsz 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
2958699b045aSAlvaro Karsz 				  VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
2959699b045aSAlvaro Karsz 				  &sgs_rx))
2960699b045aSAlvaro Karsz 		return -EINVAL;
2961699b045aSAlvaro Karsz 
2962699b045aSAlvaro Karsz 	/* Save parameters */
2963699b045aSAlvaro Karsz 	vi->rx_usecs = ec->rx_coalesce_usecs;
2964699b045aSAlvaro Karsz 	vi->rx_max_packets = ec->rx_max_coalesced_frames;
2965699b045aSAlvaro Karsz 
2966699b045aSAlvaro Karsz 	return 0;
2967699b045aSAlvaro Karsz }
2968699b045aSAlvaro Karsz 
2969699b045aSAlvaro Karsz static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
2970699b045aSAlvaro Karsz {
2971699b045aSAlvaro Karsz 	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
2972699b045aSAlvaro Karsz 	 * feature is negotiated.
2973699b045aSAlvaro Karsz 	 */
2974699b045aSAlvaro Karsz 	if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
2975699b045aSAlvaro Karsz 		return -EOPNOTSUPP;
2976699b045aSAlvaro Karsz 
2977699b045aSAlvaro Karsz 	if (ec->tx_max_coalesced_frames > 1 ||
2978699b045aSAlvaro Karsz 	    ec->rx_max_coalesced_frames != 1)
2979699b045aSAlvaro Karsz 		return -EINVAL;
2980699b045aSAlvaro Karsz 
2981699b045aSAlvaro Karsz 	return 0;
2982699b045aSAlvaro Karsz }
2983699b045aSAlvaro Karsz 
29840c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev,
2985f3ccfda1SYufeng Mo 				struct ethtool_coalesce *ec,
2986f3ccfda1SYufeng Mo 				struct kernel_ethtool_coalesce *kernel_coal,
2987f3ccfda1SYufeng Mo 				struct netlink_ext_ack *extack)
29880c465be1SJason Wang {
29890c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2990699b045aSAlvaro Karsz 	int ret, i, napi_weight;
2991699b045aSAlvaro Karsz 	bool update_napi = false;
29920c465be1SJason Wang 
2993699b045aSAlvaro Karsz 	/* Can't change NAPI weight if the link is up */
29940c465be1SJason Wang 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
29950c465be1SJason Wang 	if (napi_weight ^ vi->sq[0].napi.weight) {
29960c465be1SJason Wang 		if (dev->flags & IFF_UP)
29970c465be1SJason Wang 			return -EBUSY;
2998699b045aSAlvaro Karsz 		else
2999699b045aSAlvaro Karsz 			update_napi = true;
3000699b045aSAlvaro Karsz 	}
3001699b045aSAlvaro Karsz 
3002699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
3003699b045aSAlvaro Karsz 		ret = virtnet_send_notf_coal_cmds(vi, ec);
3004699b045aSAlvaro Karsz 	else
3005699b045aSAlvaro Karsz 		ret = virtnet_coal_params_supported(ec);
3006699b045aSAlvaro Karsz 
3007699b045aSAlvaro Karsz 	if (ret)
3008699b045aSAlvaro Karsz 		return ret;
3009699b045aSAlvaro Karsz 
3010699b045aSAlvaro Karsz 	if (update_napi) {
30110c465be1SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
30120c465be1SJason Wang 			vi->sq[i].napi.weight = napi_weight;
30130c465be1SJason Wang 	}
30140c465be1SJason Wang 
3015699b045aSAlvaro Karsz 	return ret;
30160c465be1SJason Wang }
30170c465be1SJason Wang 
30180c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev,
3019f3ccfda1SYufeng Mo 				struct ethtool_coalesce *ec,
3020f3ccfda1SYufeng Mo 				struct kernel_ethtool_coalesce *kernel_coal,
3021f3ccfda1SYufeng Mo 				struct netlink_ext_ack *extack)
30220c465be1SJason Wang {
30230c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
30240c465be1SJason Wang 
3025699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3026699b045aSAlvaro Karsz 		ec->rx_coalesce_usecs = vi->rx_usecs;
3027699b045aSAlvaro Karsz 		ec->tx_coalesce_usecs = vi->tx_usecs;
3028699b045aSAlvaro Karsz 		ec->tx_max_coalesced_frames = vi->tx_max_packets;
3029699b045aSAlvaro Karsz 		ec->rx_max_coalesced_frames = vi->rx_max_packets;
3030699b045aSAlvaro Karsz 	} else {
3031699b045aSAlvaro Karsz 		ec->rx_max_coalesced_frames = 1;
30320c465be1SJason Wang 
30330c465be1SJason Wang 		if (vi->sq[0].napi.weight)
30340c465be1SJason Wang 			ec->tx_max_coalesced_frames = 1;
3035699b045aSAlvaro Karsz 	}
30360c465be1SJason Wang 
30370c465be1SJason Wang 	return 0;
30380c465be1SJason Wang }
30390c465be1SJason Wang 
304016032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
304116032be5SNikolay Aleksandrov {
304216032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
304316032be5SNikolay Aleksandrov 
304416032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
304516032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
304616032be5SNikolay Aleksandrov }
304716032be5SNikolay Aleksandrov 
3048faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi)
3049faa9b39fSJason Baron {
3050faa9b39fSJason Baron 	u32 speed;
3051faa9b39fSJason Baron 	u8 duplex;
3052faa9b39fSJason Baron 
3053faa9b39fSJason Baron 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
3054faa9b39fSJason Baron 		return;
3055faa9b39fSJason Baron 
305664ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
305764ffa39dSMichael S. Tsirkin 
3058faa9b39fSJason Baron 	if (ethtool_validate_speed(speed))
3059faa9b39fSJason Baron 		vi->speed = speed;
306064ffa39dSMichael S. Tsirkin 
306164ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
306264ffa39dSMichael S. Tsirkin 
3063faa9b39fSJason Baron 	if (ethtool_validate_duplex(duplex))
3064faa9b39fSJason Baron 		vi->duplex = duplex;
3065faa9b39fSJason Baron }
3066faa9b39fSJason Baron 
3067c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
3068c7114b12SAndrew Melnychenko {
3069c7114b12SAndrew Melnychenko 	return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
3070c7114b12SAndrew Melnychenko }
3071c7114b12SAndrew Melnychenko 
3072c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
3073c7114b12SAndrew Melnychenko {
3074c7114b12SAndrew Melnychenko 	return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
3075c7114b12SAndrew Melnychenko }
3076c7114b12SAndrew Melnychenko 
3077c7114b12SAndrew Melnychenko static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
3078c7114b12SAndrew Melnychenko {
3079c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3080c7114b12SAndrew Melnychenko 	int i;
3081c7114b12SAndrew Melnychenko 
3082c7114b12SAndrew Melnychenko 	if (indir) {
3083c7114b12SAndrew Melnychenko 		for (i = 0; i < vi->rss_indir_table_size; ++i)
3084c7114b12SAndrew Melnychenko 			indir[i] = vi->ctrl->rss.indirection_table[i];
3085c7114b12SAndrew Melnychenko 	}
3086c7114b12SAndrew Melnychenko 
3087c7114b12SAndrew Melnychenko 	if (key)
3088c7114b12SAndrew Melnychenko 		memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
3089c7114b12SAndrew Melnychenko 
3090c7114b12SAndrew Melnychenko 	if (hfunc)
3091c7114b12SAndrew Melnychenko 		*hfunc = ETH_RSS_HASH_TOP;
3092c7114b12SAndrew Melnychenko 
3093c7114b12SAndrew Melnychenko 	return 0;
3094c7114b12SAndrew Melnychenko }
3095c7114b12SAndrew Melnychenko 
3096c7114b12SAndrew Melnychenko static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
3097c7114b12SAndrew Melnychenko {
3098c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3099c7114b12SAndrew Melnychenko 	int i;
3100c7114b12SAndrew Melnychenko 
3101c7114b12SAndrew Melnychenko 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
3102c7114b12SAndrew Melnychenko 		return -EOPNOTSUPP;
3103c7114b12SAndrew Melnychenko 
3104c7114b12SAndrew Melnychenko 	if (indir) {
3105c7114b12SAndrew Melnychenko 		for (i = 0; i < vi->rss_indir_table_size; ++i)
3106c7114b12SAndrew Melnychenko 			vi->ctrl->rss.indirection_table[i] = indir[i];
3107c7114b12SAndrew Melnychenko 	}
3108c7114b12SAndrew Melnychenko 	if (key)
3109c7114b12SAndrew Melnychenko 		memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
3110c7114b12SAndrew Melnychenko 
3111c7114b12SAndrew Melnychenko 	virtnet_commit_rss_command(vi);
3112c7114b12SAndrew Melnychenko 
3113c7114b12SAndrew Melnychenko 	return 0;
3114c7114b12SAndrew Melnychenko }
3115c7114b12SAndrew Melnychenko 
3116c7114b12SAndrew Melnychenko static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
3117c7114b12SAndrew Melnychenko {
3118c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3119c7114b12SAndrew Melnychenko 	int rc = 0;
3120c7114b12SAndrew Melnychenko 
3121c7114b12SAndrew Melnychenko 	switch (info->cmd) {
3122c7114b12SAndrew Melnychenko 	case ETHTOOL_GRXRINGS:
3123c7114b12SAndrew Melnychenko 		info->data = vi->curr_queue_pairs;
3124c7114b12SAndrew Melnychenko 		break;
3125c1170820SAndrew Melnychenko 	case ETHTOOL_GRXFH:
3126c1170820SAndrew Melnychenko 		virtnet_get_hashflow(vi, info);
3127c1170820SAndrew Melnychenko 		break;
3128c1170820SAndrew Melnychenko 	default:
3129c1170820SAndrew Melnychenko 		rc = -EOPNOTSUPP;
3130c1170820SAndrew Melnychenko 	}
3131c1170820SAndrew Melnychenko 
3132c1170820SAndrew Melnychenko 	return rc;
3133c1170820SAndrew Melnychenko }
3134c1170820SAndrew Melnychenko 
3135c1170820SAndrew Melnychenko static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
3136c1170820SAndrew Melnychenko {
3137c1170820SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
3138c1170820SAndrew Melnychenko 	int rc = 0;
3139c1170820SAndrew Melnychenko 
3140c1170820SAndrew Melnychenko 	switch (info->cmd) {
3141c1170820SAndrew Melnychenko 	case ETHTOOL_SRXFH:
3142c1170820SAndrew Melnychenko 		if (!virtnet_set_hashflow(vi, info))
3143c1170820SAndrew Melnychenko 			rc = -EINVAL;
3144c1170820SAndrew Melnychenko 
3145c1170820SAndrew Melnychenko 		break;
3146c7114b12SAndrew Melnychenko 	default:
3147c7114b12SAndrew Melnychenko 		rc = -EOPNOTSUPP;
3148c7114b12SAndrew Melnychenko 	}
3149c7114b12SAndrew Melnychenko 
3150c7114b12SAndrew Melnychenko 	return rc;
3151c7114b12SAndrew Melnychenko }
3152c7114b12SAndrew Melnychenko 
31530fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
3154699b045aSAlvaro Karsz 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
3155699b045aSAlvaro Karsz 		ETHTOOL_COALESCE_USECS,
315666846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
31579f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
31588f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
3159a335b33fSXuan Zhuo 	.set_ringparam = virtnet_set_ringparam,
3160d7dfc5cfSToshiaki Makita 	.get_strings = virtnet_get_strings,
3161d7dfc5cfSToshiaki Makita 	.get_sset_count = virtnet_get_sset_count,
3162d7dfc5cfSToshiaki Makita 	.get_ethtool_stats = virtnet_get_ethtool_stats,
3163d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
3164d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
3165074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
3166ebb6b4b1SPhilippe Reynes 	.get_link_ksettings = virtnet_get_link_ksettings,
3167ebb6b4b1SPhilippe Reynes 	.set_link_ksettings = virtnet_set_link_ksettings,
31680c465be1SJason Wang 	.set_coalesce = virtnet_set_coalesce,
31690c465be1SJason Wang 	.get_coalesce = virtnet_get_coalesce,
3170c7114b12SAndrew Melnychenko 	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
3171c7114b12SAndrew Melnychenko 	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
3172c7114b12SAndrew Melnychenko 	.get_rxfh = virtnet_get_rxfh,
3173c7114b12SAndrew Melnychenko 	.set_rxfh = virtnet_set_rxfh,
3174c7114b12SAndrew Melnychenko 	.get_rxnfc = virtnet_get_rxnfc,
3175c1170820SAndrew Melnychenko 	.set_rxnfc = virtnet_set_rxnfc,
3176a9ea3fc6SHerbert Xu };
3177a9ea3fc6SHerbert Xu 
31789fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev)
31799fe7bfceSJohn Fastabend {
31809fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
31819fe7bfceSJohn Fastabend 
31829fe7bfceSJohn Fastabend 	/* Make sure no work handler is accessing the device */
31839fe7bfceSJohn Fastabend 	flush_work(&vi->config_work);
31849fe7bfceSJohn Fastabend 
318505c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
31869fe7bfceSJohn Fastabend 	netif_device_detach(vi->dev);
318705c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
31888af52fe9SStephan Gerhold 	if (netif_running(vi->dev))
31898af52fe9SStephan Gerhold 		virtnet_close(vi->dev);
31909fe7bfceSJohn Fastabend }
31919fe7bfceSJohn Fastabend 
31929fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi);
31939fe7bfceSJohn Fastabend 
31949fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev)
31959fe7bfceSJohn Fastabend {
31969fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
31978af52fe9SStephan Gerhold 	int err;
31989fe7bfceSJohn Fastabend 
31999fe7bfceSJohn Fastabend 	err = init_vqs(vi);
32009fe7bfceSJohn Fastabend 	if (err)
32019fe7bfceSJohn Fastabend 		return err;
32029fe7bfceSJohn Fastabend 
32039fe7bfceSJohn Fastabend 	virtio_device_ready(vdev);
32049fe7bfceSJohn Fastabend 
32055a159128SJason Wang 	enable_delayed_refill(vi);
32065a159128SJason Wang 
32079fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
32088af52fe9SStephan Gerhold 		err = virtnet_open(vi->dev);
32098af52fe9SStephan Gerhold 		if (err)
32108af52fe9SStephan Gerhold 			return err;
32119fe7bfceSJohn Fastabend 	}
32129fe7bfceSJohn Fastabend 
321305c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
32149fe7bfceSJohn Fastabend 	netif_device_attach(vi->dev);
321505c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
32169fe7bfceSJohn Fastabend 	return err;
32179fe7bfceSJohn Fastabend }
32189fe7bfceSJohn Fastabend 
32193f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
32203f93522fSJason Wang {
32213f93522fSJason Wang 	struct scatterlist sg;
322212e57169SMichael S. Tsirkin 	vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
32233f93522fSJason Wang 
322412e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
32253f93522fSJason Wang 
32263f93522fSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
32273f93522fSJason Wang 				  VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
32283f93522fSJason Wang 		dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
32293f93522fSJason Wang 		return -EINVAL;
32303f93522fSJason Wang 	}
32313f93522fSJason Wang 
32323f93522fSJason Wang 	return 0;
32333f93522fSJason Wang }
32343f93522fSJason Wang 
32353f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
32363f93522fSJason Wang {
32373f93522fSJason Wang 	u64 offloads = 0;
32383f93522fSJason Wang 
32393f93522fSJason Wang 	if (!vi->guest_offloads)
32403f93522fSJason Wang 		return 0;
32413f93522fSJason Wang 
32423f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
32433f93522fSJason Wang }
32443f93522fSJason Wang 
32453f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
32463f93522fSJason Wang {
32473f93522fSJason Wang 	u64 offloads = vi->guest_offloads;
32483f93522fSJason Wang 
32493f93522fSJason Wang 	if (!vi->guest_offloads)
32503f93522fSJason Wang 		return 0;
32513f93522fSJason Wang 
32523f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
32533f93522fSJason Wang }
32543f93522fSJason Wang 
32559861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
32569861ce03SJakub Kicinski 			   struct netlink_ext_ack *extack)
3257f600b690SJohn Fastabend {
3258e814b958SHeng Qi 	unsigned int room = SKB_DATA_ALIGN(VIRTIO_XDP_HEADROOM +
3259e814b958SHeng Qi 					   sizeof(struct skb_shared_info));
3260e814b958SHeng Qi 	unsigned int max_sz = PAGE_SIZE - room - ETH_HLEN;
3261f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
3262f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
3263017b29c3SJason Wang 	u16 xdp_qp = 0, curr_qp;
3264672aafd5SJohn Fastabend 	int i, err;
3265f600b690SJohn Fastabend 
32663f93522fSJason Wang 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
32673f93522fSJason Wang 	    && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
326892502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
326992502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
327018ba58e1SJason Wang 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3271418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3272418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3273418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3274dbcf24d1SJason Wang 		NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3275f600b690SJohn Fastabend 		return -EOPNOTSUPP;
3276f600b690SJohn Fastabend 	}
3277f600b690SJohn Fastabend 
3278f600b690SJohn Fastabend 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
32794d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3280f600b690SJohn Fastabend 		return -EINVAL;
3281f600b690SJohn Fastabend 	}
3282f600b690SJohn Fastabend 
32838d9bc36dSHeng Qi 	if (prog && !prog->aux->xdp_has_frags && dev->mtu > max_sz) {
32848d9bc36dSHeng Qi 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP without frags");
32858d9bc36dSHeng Qi 		netdev_warn(dev, "single-buffer XDP requires MTU less than %u\n", max_sz);
3286f600b690SJohn Fastabend 		return -EINVAL;
3287f600b690SJohn Fastabend 	}
3288f600b690SJohn Fastabend 
3289672aafd5SJohn Fastabend 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3290672aafd5SJohn Fastabend 	if (prog)
3291672aafd5SJohn Fastabend 		xdp_qp = nr_cpu_ids;
3292672aafd5SJohn Fastabend 
3293672aafd5SJohn Fastabend 	/* XDP requires extra queues for XDP_TX */
3294672aafd5SJohn Fastabend 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
32959ce4e3d6SXuan 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",
3296672aafd5SJohn Fastabend 				 curr_qp + xdp_qp, vi->max_queue_pairs);
329797c2c69eSXuan Zhuo 		xdp_qp = 0;
3298672aafd5SJohn Fastabend 	}
3299672aafd5SJohn Fastabend 
330003aa6d34SToshiaki Makita 	old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
330103aa6d34SToshiaki Makita 	if (!prog && !old_prog)
330203aa6d34SToshiaki Makita 		return 0;
330303aa6d34SToshiaki Makita 
330485192dbfSAndrii Nakryiko 	if (prog)
330585192dbfSAndrii Nakryiko 		bpf_prog_add(prog, vi->max_queue_pairs - 1);
33062de2f7f4SJohn Fastabend 
33074941d472SJason Wang 	/* Make sure NAPI is not using any XDP TX queues for RX. */
3308534da5e8SToshiaki Makita 	if (netif_running(dev)) {
3309534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
33104941d472SJason Wang 			napi_disable(&vi->rq[i].napi);
3311534da5e8SToshiaki Makita 			virtnet_napi_tx_disable(&vi->sq[i].napi);
3312534da5e8SToshiaki Makita 		}
3313534da5e8SToshiaki Makita 	}
33142de2f7f4SJohn Fastabend 
331503aa6d34SToshiaki Makita 	if (!prog) {
331603aa6d34SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
331703aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
331803aa6d34SToshiaki Makita 			if (i == 0)
331903aa6d34SToshiaki Makita 				virtnet_restore_guest_offloads(vi);
332003aa6d34SToshiaki Makita 		}
332103aa6d34SToshiaki Makita 		synchronize_net();
332203aa6d34SToshiaki Makita 	}
332303aa6d34SToshiaki Makita 
33244941d472SJason Wang 	err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
33254941d472SJason Wang 	if (err)
33264941d472SJason Wang 		goto err;
3327188313c1SToshiaki Makita 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
33284941d472SJason Wang 	vi->xdp_queue_pairs = xdp_qp;
3329f600b690SJohn Fastabend 
333003aa6d34SToshiaki Makita 	if (prog) {
333197c2c69eSXuan Zhuo 		vi->xdp_enabled = true;
3332f600b690SJohn Fastabend 		for (i = 0; i < vi->max_queue_pairs; i++) {
3333f600b690SJohn Fastabend 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
333403aa6d34SToshiaki Makita 			if (i == 0 && !old_prog)
33353f93522fSJason Wang 				virtnet_clear_guest_offloads(vi);
33363f93522fSJason Wang 		}
333766c0e13aSMarek Majtyka 		if (!old_prog)
333830bbf891SLorenzo Bianconi 			xdp_features_set_redirect_target(dev, true);
333997c2c69eSXuan Zhuo 	} else {
334066c0e13aSMarek Majtyka 		xdp_features_clear_redirect_target(dev);
334197c2c69eSXuan Zhuo 		vi->xdp_enabled = false;
334203aa6d34SToshiaki Makita 	}
334303aa6d34SToshiaki Makita 
334403aa6d34SToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
3345f600b690SJohn Fastabend 		if (old_prog)
3346f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
3347534da5e8SToshiaki Makita 		if (netif_running(dev)) {
33484941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3349534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3350534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
3351534da5e8SToshiaki Makita 		}
3352f600b690SJohn Fastabend 	}
3353f600b690SJohn Fastabend 
3354f600b690SJohn Fastabend 	return 0;
33552de2f7f4SJohn Fastabend 
33564941d472SJason Wang err:
335703aa6d34SToshiaki Makita 	if (!prog) {
335803aa6d34SToshiaki Makita 		virtnet_clear_guest_offloads(vi);
33594941d472SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
336003aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
336103aa6d34SToshiaki Makita 	}
336203aa6d34SToshiaki Makita 
33638be4d9a4SToshiaki Makita 	if (netif_running(dev)) {
3364534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
33654941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3366534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3367534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
3368534da5e8SToshiaki Makita 		}
33698be4d9a4SToshiaki Makita 	}
33702de2f7f4SJohn Fastabend 	if (prog)
33712de2f7f4SJohn Fastabend 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
33722de2f7f4SJohn Fastabend 	return err;
3373f600b690SJohn Fastabend }
3374f600b690SJohn Fastabend 
3375f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3376f600b690SJohn Fastabend {
3377f600b690SJohn Fastabend 	switch (xdp->command) {
3378f600b690SJohn Fastabend 	case XDP_SETUP_PROG:
33799861ce03SJakub Kicinski 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3380f600b690SJohn Fastabend 	default:
3381f600b690SJohn Fastabend 		return -EINVAL;
3382f600b690SJohn Fastabend 	}
3383f600b690SJohn Fastabend }
3384f600b690SJohn Fastabend 
3385ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3386ba5e4426SSridhar Samudrala 				      size_t len)
3387ba5e4426SSridhar Samudrala {
3388ba5e4426SSridhar Samudrala 	struct virtnet_info *vi = netdev_priv(dev);
3389ba5e4426SSridhar Samudrala 	int ret;
3390ba5e4426SSridhar Samudrala 
3391ba5e4426SSridhar Samudrala 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3392ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
3393ba5e4426SSridhar Samudrala 
3394ba5e4426SSridhar Samudrala 	ret = snprintf(buf, len, "sby");
3395ba5e4426SSridhar Samudrala 	if (ret >= len)
3396ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
3397ba5e4426SSridhar Samudrala 
3398ba5e4426SSridhar Samudrala 	return 0;
3399ba5e4426SSridhar Samudrala }
3400ba5e4426SSridhar Samudrala 
3401a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev,
3402a02e8964SWillem de Bruijn 				netdev_features_t features)
3403a02e8964SWillem de Bruijn {
3404a02e8964SWillem de Bruijn 	struct virtnet_info *vi = netdev_priv(dev);
3405cf8691cbSMichael S. Tsirkin 	u64 offloads;
3406a02e8964SWillem de Bruijn 	int err;
3407a02e8964SWillem de Bruijn 
3408dbcf24d1SJason Wang 	if ((dev->features ^ features) & NETIF_F_GRO_HW) {
340997c2c69eSXuan Zhuo 		if (vi->xdp_enabled)
3410a02e8964SWillem de Bruijn 			return -EBUSY;
3411a02e8964SWillem de Bruijn 
3412dbcf24d1SJason Wang 		if (features & NETIF_F_GRO_HW)
3413cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable;
3414a02e8964SWillem de Bruijn 		else
3415cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable &
3416dbcf24d1SJason Wang 				   ~GUEST_OFFLOAD_GRO_HW_MASK;
3417a02e8964SWillem de Bruijn 
3418a02e8964SWillem de Bruijn 		err = virtnet_set_guest_offloads(vi, offloads);
3419a02e8964SWillem de Bruijn 		if (err)
3420a02e8964SWillem de Bruijn 			return err;
34213618ad2aSTonghao Zhang 		vi->guest_offloads = offloads;
3422cf8691cbSMichael S. Tsirkin 	}
3423cf8691cbSMichael S. Tsirkin 
3424c7114b12SAndrew Melnychenko 	if ((dev->features ^ features) & NETIF_F_RXHASH) {
3425c7114b12SAndrew Melnychenko 		if (features & NETIF_F_RXHASH)
3426c1170820SAndrew Melnychenko 			vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3427c7114b12SAndrew Melnychenko 		else
3428c7114b12SAndrew Melnychenko 			vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3429c7114b12SAndrew Melnychenko 
3430c7114b12SAndrew Melnychenko 		if (!virtnet_commit_rss_command(vi))
3431c7114b12SAndrew Melnychenko 			return -EINVAL;
3432c7114b12SAndrew Melnychenko 	}
3433c7114b12SAndrew Melnychenko 
3434a02e8964SWillem de Bruijn 	return 0;
3435a02e8964SWillem de Bruijn }
3436a02e8964SWillem de Bruijn 
3437a520794bSTony Lu static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3438a520794bSTony Lu {
3439a520794bSTony Lu 	struct virtnet_info *priv = netdev_priv(dev);
3440a520794bSTony Lu 	struct send_queue *sq = &priv->sq[txqueue];
3441a520794bSTony Lu 	struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3442a520794bSTony Lu 
3443a520794bSTony Lu 	u64_stats_update_begin(&sq->stats.syncp);
3444a520794bSTony Lu 	sq->stats.tx_timeouts++;
3445a520794bSTony Lu 	u64_stats_update_end(&sq->stats.syncp);
3446a520794bSTony Lu 
3447a520794bSTony Lu 	netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3448a520794bSTony Lu 		   txqueue, sq->name, sq->vq->index, sq->vq->name,
34495337824fSEric Dumazet 		   jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3450a520794bSTony Lu }
3451a520794bSTony Lu 
345276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
345376288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
345476288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
345576288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
345676288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
34579c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
34582af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
34593fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
34601824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
34611824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3462f4e63525SJakub Kicinski 	.ndo_bpf		= virtnet_xdp,
3463186b3c99SJason Wang 	.ndo_xdp_xmit		= virtnet_xdp_xmit,
34642836b4f2SVlad Yasevich 	.ndo_features_check	= passthru_features_check,
3465ba5e4426SSridhar Samudrala 	.ndo_get_phys_port_name	= virtnet_get_phys_port_name,
3466a02e8964SWillem de Bruijn 	.ndo_set_features	= virtnet_set_features,
3467a520794bSTony Lu 	.ndo_tx_timeout		= virtnet_tx_timeout,
346876288b4eSStephen Hemminger };
346976288b4eSStephen Hemminger 
3470586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
34719f4d26d0SMark McLoughlin {
3472586d17c5SJason Wang 	struct virtnet_info *vi =
3473586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
34749f4d26d0SMark McLoughlin 	u16 v;
34759f4d26d0SMark McLoughlin 
3476855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3477855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
3478507613bfSMichael S. Tsirkin 		return;
3479586d17c5SJason Wang 
3480586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
3481ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
3482586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
3483586d17c5SJason Wang 	}
34849f4d26d0SMark McLoughlin 
34859f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
34869f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
34879f4d26d0SMark McLoughlin 
34889f4d26d0SMark McLoughlin 	if (vi->status == v)
3489507613bfSMichael S. Tsirkin 		return;
34909f4d26d0SMark McLoughlin 
34919f4d26d0SMark McLoughlin 	vi->status = v;
34929f4d26d0SMark McLoughlin 
34939f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
3494faa9b39fSJason Baron 		virtnet_update_settings(vi);
34959f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
3496986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
34979f4d26d0SMark McLoughlin 	} else {
34989f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
3499986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
35009f4d26d0SMark McLoughlin 	}
35019f4d26d0SMark McLoughlin }
35029f4d26d0SMark McLoughlin 
35039f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
35049f4d26d0SMark McLoughlin {
35059f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
35069f4d26d0SMark McLoughlin 
35073b07e9caSTejun Heo 	schedule_work(&vi->config_work);
35089f4d26d0SMark McLoughlin }
35099f4d26d0SMark McLoughlin 
3510986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
3511986a4f4dSJason Wang {
3512d4fb84eeSAndrey Vagin 	int i;
3513d4fb84eeSAndrey Vagin 
3514ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
35155198d545SJakub Kicinski 		__netif_napi_del(&vi->rq[i].napi);
35165198d545SJakub Kicinski 		__netif_napi_del(&vi->sq[i].napi);
3517ab3971b1SJason Wang 	}
3518d4fb84eeSAndrey Vagin 
35195198d545SJakub Kicinski 	/* We called __netif_napi_del(),
3520963abe5cSEric Dumazet 	 * we need to respect an RCU grace period before freeing vi->rq
3521963abe5cSEric Dumazet 	 */
3522963abe5cSEric Dumazet 	synchronize_net();
3523963abe5cSEric Dumazet 
3524986a4f4dSJason Wang 	kfree(vi->rq);
3525986a4f4dSJason Wang 	kfree(vi->sq);
352612e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
3527986a4f4dSJason Wang }
3528986a4f4dSJason Wang 
352947315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi)
3530986a4f4dSJason Wang {
3531f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
3532986a4f4dSJason Wang 	int i;
3533986a4f4dSJason Wang 
3534986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3535986a4f4dSJason Wang 		while (vi->rq[i].pages)
3536986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
3537f600b690SJohn Fastabend 
3538f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
3539f600b690SJohn Fastabend 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
3540f600b690SJohn Fastabend 		if (old_prog)
3541f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
3542986a4f4dSJason Wang 	}
354347315329SJohn Fastabend }
354447315329SJohn Fastabend 
354547315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi)
354647315329SJohn Fastabend {
354747315329SJohn Fastabend 	rtnl_lock();
354847315329SJohn Fastabend 	_free_receive_bufs(vi);
3549f600b690SJohn Fastabend 	rtnl_unlock();
3550986a4f4dSJason Wang }
3551986a4f4dSJason Wang 
3552fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
3553fb51879dSMichael Dalton {
3554fb51879dSMichael Dalton 	int i;
3555fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
3556fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
3557fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
3558fb51879dSMichael Dalton }
3559fb51879dSMichael Dalton 
35606e345f8cSXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
35616e345f8cSXuan Zhuo {
35626e345f8cSXuan Zhuo 	if (!is_xdp_frame(buf))
35636e345f8cSXuan Zhuo 		dev_kfree_skb(buf);
35646e345f8cSXuan Zhuo 	else
35656e345f8cSXuan Zhuo 		xdp_return_frame(ptr_to_xdp(buf));
35666e345f8cSXuan Zhuo }
35676e345f8cSXuan Zhuo 
35686e345f8cSXuan Zhuo static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf)
35696e345f8cSXuan Zhuo {
35706e345f8cSXuan Zhuo 	struct virtnet_info *vi = vq->vdev->priv;
35716e345f8cSXuan Zhuo 	int i = vq2rxq(vq);
35726e345f8cSXuan Zhuo 
35736e345f8cSXuan Zhuo 	if (vi->mergeable_rx_bufs)
35746e345f8cSXuan Zhuo 		put_page(virt_to_head_page(buf));
35756e345f8cSXuan Zhuo 	else if (vi->big_packets)
35766e345f8cSXuan Zhuo 		give_pages(&vi->rq[i], buf);
35776e345f8cSXuan Zhuo 	else
35786e345f8cSXuan Zhuo 		put_page(virt_to_head_page(buf));
35796e345f8cSXuan Zhuo }
35806e345f8cSXuan Zhuo 
3581986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
3582986a4f4dSJason Wang {
3583986a4f4dSJason Wang 	void *buf;
3584986a4f4dSJason Wang 	int i;
3585986a4f4dSJason Wang 
3586986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3587986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
35886e345f8cSXuan Zhuo 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
35896e345f8cSXuan Zhuo 			virtnet_sq_free_unused_buf(vq, buf);
3590f8bb5104SWenliang Wang 		cond_resched();
3591986a4f4dSJason Wang 	}
3592986a4f4dSJason Wang 
3593986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3594986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
35956e345f8cSXuan Zhuo 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
35966e345f8cSXuan Zhuo 			virtnet_rq_free_unused_buf(vq, buf);
3597f8bb5104SWenliang Wang 		cond_resched();
3598986a4f4dSJason Wang 	}
3599ab7db917SMichael Dalton }
3600986a4f4dSJason Wang 
3601e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
3602e9d7417bSJason Wang {
3603e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
3604e9d7417bSJason Wang 
3605310974faSPeter Xu 	virtnet_clean_affinity(vi);
3606986a4f4dSJason Wang 
3607e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
3608986a4f4dSJason Wang 
3609986a4f4dSJason Wang 	virtnet_free_queues(vi);
3610986a4f4dSJason Wang }
3611986a4f4dSJason Wang 
3612d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at
3613d85b758fSMichael S. Tsirkin  * least one full packet?
3614d85b758fSMichael S. Tsirkin  * Logic below assumes the mergeable buffer header is used.
3615d85b758fSMichael S. Tsirkin  */
3616d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
3617d85b758fSMichael S. Tsirkin {
3618c1ddc42dSAndrew Melnychenko 	const unsigned int hdr_len = vi->hdr_len;
3619d85b758fSMichael S. Tsirkin 	unsigned int rq_size = virtqueue_get_vring_size(vq);
3620d85b758fSMichael S. Tsirkin 	unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
3621d85b758fSMichael S. Tsirkin 	unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
3622d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
3623d85b758fSMichael S. Tsirkin 
3624f0c3192cSMichael S. Tsirkin 	return max(max(min_buf_len, hdr_len) - hdr_len,
3625f0c3192cSMichael S. Tsirkin 		   (unsigned int)GOOD_PACKET_LEN);
3626d85b758fSMichael S. Tsirkin }
3627d85b758fSMichael S. Tsirkin 
3628986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
3629986a4f4dSJason Wang {
3630986a4f4dSJason Wang 	vq_callback_t **callbacks;
3631986a4f4dSJason Wang 	struct virtqueue **vqs;
3632986a4f4dSJason Wang 	int ret = -ENOMEM;
3633986a4f4dSJason Wang 	int i, total_vqs;
3634986a4f4dSJason Wang 	const char **names;
3635d45b897bSMichael S. Tsirkin 	bool *ctx;
3636986a4f4dSJason Wang 
3637986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
3638986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
3639986a4f4dSJason Wang 	 * possible control vq.
3640986a4f4dSJason Wang 	 */
3641986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
3642986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
3643986a4f4dSJason Wang 
3644986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
36456396bb22SKees Cook 	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
3646986a4f4dSJason Wang 	if (!vqs)
3647986a4f4dSJason Wang 		goto err_vq;
36486da2ec56SKees Cook 	callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
3649986a4f4dSJason Wang 	if (!callbacks)
3650986a4f4dSJason Wang 		goto err_callback;
36516da2ec56SKees Cook 	names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
3652986a4f4dSJason Wang 	if (!names)
3653986a4f4dSJason Wang 		goto err_names;
3654192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
36556396bb22SKees Cook 		ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
3656d45b897bSMichael S. Tsirkin 		if (!ctx)
3657d45b897bSMichael S. Tsirkin 			goto err_ctx;
3658d45b897bSMichael S. Tsirkin 	} else {
3659d45b897bSMichael S. Tsirkin 		ctx = NULL;
3660d45b897bSMichael S. Tsirkin 	}
3661986a4f4dSJason Wang 
3662986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
3663986a4f4dSJason Wang 	if (vi->has_cvq) {
3664986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
3665986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
3666986a4f4dSJason Wang 	}
3667986a4f4dSJason Wang 
3668986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
3669986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3670986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
3671986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
3672986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
3673986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
3674986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
3675986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
3676d45b897bSMichael S. Tsirkin 		if (ctx)
3677d45b897bSMichael S. Tsirkin 			ctx[rxq2vq(i)] = true;
3678986a4f4dSJason Wang 	}
3679986a4f4dSJason Wang 
36802e9ca760SMichael S. Tsirkin 	ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
36812e9ca760SMichael S. Tsirkin 				  names, ctx, NULL);
3682986a4f4dSJason Wang 	if (ret)
3683986a4f4dSJason Wang 		goto err_find;
3684986a4f4dSJason Wang 
3685986a4f4dSJason Wang 	if (vi->has_cvq) {
3686986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
3687986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
3688f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3689986a4f4dSJason Wang 	}
3690986a4f4dSJason Wang 
3691986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3692986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
3693d85b758fSMichael S. Tsirkin 		vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
3694986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
3695986a4f4dSJason Wang 	}
3696986a4f4dSJason Wang 
36972fa3c8a8STonghao Zhang 	/* run here: ret == 0. */
3698986a4f4dSJason Wang 
3699986a4f4dSJason Wang 
3700986a4f4dSJason Wang err_find:
3701d45b897bSMichael S. Tsirkin 	kfree(ctx);
3702d45b897bSMichael S. Tsirkin err_ctx:
3703986a4f4dSJason Wang 	kfree(names);
3704986a4f4dSJason Wang err_names:
3705986a4f4dSJason Wang 	kfree(callbacks);
3706986a4f4dSJason Wang err_callback:
3707986a4f4dSJason Wang 	kfree(vqs);
3708986a4f4dSJason Wang err_vq:
3709986a4f4dSJason Wang 	return ret;
3710986a4f4dSJason Wang }
3711986a4f4dSJason Wang 
3712986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
3713986a4f4dSJason Wang {
3714986a4f4dSJason Wang 	int i;
3715986a4f4dSJason Wang 
3716122b84a1SMax Gurtovoy 	if (vi->has_cvq) {
371712e57169SMichael S. Tsirkin 		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
371812e57169SMichael S. Tsirkin 		if (!vi->ctrl)
371912e57169SMichael S. Tsirkin 			goto err_ctrl;
3720122b84a1SMax Gurtovoy 	} else {
3721122b84a1SMax Gurtovoy 		vi->ctrl = NULL;
3722122b84a1SMax Gurtovoy 	}
37236396bb22SKees Cook 	vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
3724986a4f4dSJason Wang 	if (!vi->sq)
3725986a4f4dSJason Wang 		goto err_sq;
37266396bb22SKees Cook 	vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
3727008d4278SAmerigo Wang 	if (!vi->rq)
3728986a4f4dSJason Wang 		goto err_rq;
3729986a4f4dSJason Wang 
3730986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
3731986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3732986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
3733d484735dSJakub Kicinski 		netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
3734986a4f4dSJason Wang 				      napi_weight);
37358d602e1aSJakub Kicinski 		netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
37368d602e1aSJakub Kicinski 					 virtnet_poll_tx,
3737b92f1e67SWillem de Bruijn 					 napi_tx ? napi_weight : 0);
3738986a4f4dSJason Wang 
3739986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
37405377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
3741986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
3742d7dfc5cfSToshiaki Makita 
3743d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->rq[i].stats.syncp);
3744d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->sq[i].stats.syncp);
3745986a4f4dSJason Wang 	}
3746986a4f4dSJason Wang 
3747986a4f4dSJason Wang 	return 0;
3748986a4f4dSJason Wang 
3749986a4f4dSJason Wang err_rq:
3750986a4f4dSJason Wang 	kfree(vi->sq);
3751986a4f4dSJason Wang err_sq:
375212e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
375312e57169SMichael S. Tsirkin err_ctrl:
3754986a4f4dSJason Wang 	return -ENOMEM;
3755e9d7417bSJason Wang }
3756e9d7417bSJason Wang 
37573f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
37583f9c10b0SAmit Shah {
3759986a4f4dSJason Wang 	int ret;
37603f9c10b0SAmit Shah 
3761986a4f4dSJason Wang 	/* Allocate send & receive queues */
3762986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
3763986a4f4dSJason Wang 	if (ret)
3764986a4f4dSJason Wang 		goto err;
37653f9c10b0SAmit Shah 
3766986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
3767986a4f4dSJason Wang 	if (ret)
3768986a4f4dSJason Wang 		goto err_free;
37693f9c10b0SAmit Shah 
3770a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_lock();
37718898c21cSWanlong Gao 	virtnet_set_affinity(vi);
3772a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_unlock();
377347be2479SWanlong Gao 
37743f9c10b0SAmit Shah 	return 0;
3775986a4f4dSJason Wang 
3776986a4f4dSJason Wang err_free:
3777986a4f4dSJason Wang 	virtnet_free_queues(vi);
3778986a4f4dSJason Wang err:
3779986a4f4dSJason Wang 	return ret;
37803f9c10b0SAmit Shah }
37813f9c10b0SAmit Shah 
3782fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
3783fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3784718ad681Sstephen hemminger 		char *buf)
3785fbf28d78SMichael Dalton {
3786fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
3787fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
37883cc81a9aSJason Wang 	unsigned int headroom = virtnet_get_headroom(vi);
37893cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
37905377d758SJohannes Berg 	struct ewma_pkt_len *avg;
3791fbf28d78SMichael Dalton 
3792fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
3793fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3794d85b758fSMichael S. Tsirkin 	return sprintf(buf, "%u\n",
37953cc81a9aSJason Wang 		       get_mergeable_buf_len(&vi->rq[queue_index], avg,
37963cc81a9aSJason Wang 				       SKB_DATA_ALIGN(headroom + tailroom)));
3797fbf28d78SMichael Dalton }
3798fbf28d78SMichael Dalton 
3799fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3800fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
3801fbf28d78SMichael Dalton 
3802fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
3803fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
3804fbf28d78SMichael Dalton 	NULL
3805fbf28d78SMichael Dalton };
3806fbf28d78SMichael Dalton 
3807fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
3808fbf28d78SMichael Dalton 	.name = "virtio_net",
3809fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
3810fbf28d78SMichael Dalton };
3811fbf28d78SMichael Dalton #endif
3812fbf28d78SMichael Dalton 
3813892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3814892d6eb1SJason Wang 				    unsigned int fbit,
3815892d6eb1SJason Wang 				    const char *fname, const char *dname)
3816892d6eb1SJason Wang {
3817892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
3818892d6eb1SJason Wang 		return false;
3819892d6eb1SJason Wang 
3820892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
3821892d6eb1SJason Wang 		fname, dname);
3822892d6eb1SJason Wang 
3823892d6eb1SJason Wang 	return true;
3824892d6eb1SJason Wang }
3825892d6eb1SJason Wang 
3826892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
3827892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3828892d6eb1SJason Wang 
3829892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
3830892d6eb1SJason Wang {
3831892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3832892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3833892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3834892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3835892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3836892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3837892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3838892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3839892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3840c7114b12SAndrew Melnychenko 			     "VIRTIO_NET_F_CTRL_VQ") ||
3841c7114b12SAndrew Melnychenko 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
384291f41f01SAndrew Melnychenko 			     "VIRTIO_NET_F_CTRL_VQ") ||
384391f41f01SAndrew Melnychenko 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
3844699b045aSAlvaro Karsz 			     "VIRTIO_NET_F_CTRL_VQ") ||
3845699b045aSAlvaro Karsz 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
3846892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
3847892d6eb1SJason Wang 		return false;
3848892d6eb1SJason Wang 	}
3849892d6eb1SJason Wang 
3850892d6eb1SJason Wang 	return true;
3851892d6eb1SJason Wang }
3852892d6eb1SJason Wang 
3853d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
3854d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
3855d0c2c997SJarod Wilson 
3856fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev)
3857296f96fcSRusty Russell {
38586ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
38596ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
38606ba42248SMichael S. Tsirkin 			__func__);
38616ba42248SMichael S. Tsirkin 		return -EINVAL;
38626ba42248SMichael S. Tsirkin 	}
38636ba42248SMichael S. Tsirkin 
3864892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
3865892d6eb1SJason Wang 		return -EINVAL;
3866892d6eb1SJason Wang 
3867fe36cbe0SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3868fe36cbe0SMichael S. Tsirkin 		int mtu = virtio_cread16(vdev,
3869fe36cbe0SMichael S. Tsirkin 					 offsetof(struct virtio_net_config,
3870fe36cbe0SMichael S. Tsirkin 						  mtu));
3871fe36cbe0SMichael S. Tsirkin 		if (mtu < MIN_MTU)
3872fe36cbe0SMichael S. Tsirkin 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3873fe36cbe0SMichael S. Tsirkin 	}
3874fe36cbe0SMichael S. Tsirkin 
38757c06458cSLaurent Vivier 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY) &&
38767c06458cSLaurent Vivier 	    !virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
38777c06458cSLaurent Vivier 		dev_warn(&vdev->dev, "device advertises feature VIRTIO_NET_F_STANDBY but not VIRTIO_NET_F_MAC, disabling standby");
38787c06458cSLaurent Vivier 		__virtio_clear_bit(vdev, VIRTIO_NET_F_STANDBY);
38797c06458cSLaurent Vivier 	}
38807c06458cSLaurent Vivier 
3881fe36cbe0SMichael S. Tsirkin 	return 0;
3882fe36cbe0SMichael S. Tsirkin }
3883fe36cbe0SMichael S. Tsirkin 
388446cd26f4SGavin Li static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
388546cd26f4SGavin Li {
388646cd26f4SGavin Li 	return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
388746cd26f4SGavin Li 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
388846cd26f4SGavin Li 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3889418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3890418044e1SAndrew Melnychenko 		(virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
3891418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
389246cd26f4SGavin Li }
389346cd26f4SGavin Li 
38944959aebbSGavin Li static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
38954959aebbSGavin Li {
38964959aebbSGavin Li 	bool guest_gso = virtnet_check_guest_gso(vi);
38974959aebbSGavin Li 
38984959aebbSGavin Li 	/* If device can receive ANY guest GSO packets, regardless of mtu,
38994959aebbSGavin Li 	 * allocate packets of maximum size, otherwise limit it to only
39004959aebbSGavin Li 	 * mtu size worth only.
39014959aebbSGavin Li 	 */
39024959aebbSGavin Li 	if (mtu > ETH_DATA_LEN || guest_gso) {
39034959aebbSGavin Li 		vi->big_packets = true;
39044959aebbSGavin Li 		vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
39054959aebbSGavin Li 	}
39064959aebbSGavin Li }
39074959aebbSGavin Li 
3908fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev)
3909fe36cbe0SMichael S. Tsirkin {
3910d7dfc5cfSToshiaki Makita 	int i, err = -ENOMEM;
3911fe36cbe0SMichael S. Tsirkin 	struct net_device *dev;
3912fe36cbe0SMichael S. Tsirkin 	struct virtnet_info *vi;
3913fe36cbe0SMichael S. Tsirkin 	u16 max_queue_pairs;
39144959aebbSGavin Li 	int mtu = 0;
3915fe36cbe0SMichael S. Tsirkin 
3916c7114b12SAndrew Melnychenko 	/* Find if host supports multiqueue/rss virtio_net device */
3917c7114b12SAndrew Melnychenko 	max_queue_pairs = 1;
3918c7114b12SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3919c7114b12SAndrew Melnychenko 		max_queue_pairs =
3920c7114b12SAndrew Melnychenko 		     virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
3921986a4f4dSJason Wang 
3922986a4f4dSJason Wang 	/* We need at least 2 queue's */
3923c7114b12SAndrew Melnychenko 	if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3924986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3925986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3926986a4f4dSJason Wang 		max_queue_pairs = 1;
3927296f96fcSRusty Russell 
3928296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
3929986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3930296f96fcSRusty Russell 	if (!dev)
3931296f96fcSRusty Russell 		return -ENOMEM;
3932296f96fcSRusty Russell 
3933296f96fcSRusty Russell 	/* Set up network device as normal. */
3934ab5bd583SXuan Zhuo 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3935ab5bd583SXuan Zhuo 			   IFF_TX_SKB_NO_LINEAR;
393676288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
3937296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
39383fa2a1dfSstephen hemminger 
39397ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
3940296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
3941296f96fcSRusty Russell 
3942296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
394398e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3944296f96fcSRusty Russell 		/* This opens up the world of extra features. */
394548900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
394698e778c9SMichał Mirosław 		if (csum)
394748900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
394898e778c9SMichał Mirosław 
394998e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3950e078de03SDavid S. Miller 			dev->hw_features |= NETIF_F_TSO
395134a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
395234a48579SRusty Russell 		}
39535539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
395498e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
395598e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
395698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
395798e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
395898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
395998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
3960418044e1SAndrew Melnychenko 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
3961418044e1SAndrew Melnychenko 			dev->hw_features |= NETIF_F_GSO_UDP_L4;
396298e778c9SMichał Mirosław 
396341f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
396441f2f127SJason Wang 
396598e778c9SMichał Mirosław 		if (gso)
3966e078de03SDavid S. Miller 			dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
396798e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
3968296f96fcSRusty Russell 	}
39694f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
39704f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
3971a02e8964SWillem de Bruijn 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3972a02e8964SWillem de Bruijn 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3973dbcf24d1SJason Wang 		dev->features |= NETIF_F_GRO_HW;
3974cf8691cbSMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3975dbcf24d1SJason Wang 		dev->hw_features |= NETIF_F_GRO_HW;
3976296f96fcSRusty Russell 
39774fda8302SJason Wang 	dev->vlan_features = dev->features;
397866c0e13aSMarek Majtyka 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT;
39794fda8302SJason Wang 
3980d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
3981d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
3982d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
3983d0c2c997SJarod Wilson 
3984296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
3985f2edaa4aSJakub Kicinski 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3986f2edaa4aSJakub Kicinski 		u8 addr[ETH_ALEN];
3987f2edaa4aSJakub Kicinski 
3988855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
3989a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
3990f2edaa4aSJakub Kicinski 				   addr, ETH_ALEN);
3991f2edaa4aSJakub Kicinski 		eth_hw_addr_set(dev, addr);
3992f2edaa4aSJakub Kicinski 	} else {
3993f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
39949f62d221SLaurent Vivier 		dev_info(&vdev->dev, "Assigned random MAC address %pM\n",
39959f62d221SLaurent Vivier 			 dev->dev_addr);
3996f2edaa4aSJakub Kicinski 	}
3997296f96fcSRusty Russell 
3998296f96fcSRusty Russell 	/* Set up our device-specific information */
3999296f96fcSRusty Russell 	vi = netdev_priv(dev);
4000296f96fcSRusty Russell 	vi->dev = dev;
4001296f96fcSRusty Russell 	vi->vdev = vdev;
4002d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
4003827da44cSJohn Stultz 
4004586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
40055a159128SJason Wang 	spin_lock_init(&vi->refill_lock);
4006296f96fcSRusty Russell 
400730bbf891SLorenzo Bianconi 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF)) {
40083f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
400930bbf891SLorenzo Bianconi 		dev->xdp_features |= NETDEV_XDP_ACT_RX_SG;
401030bbf891SLorenzo Bianconi 	}
40113f2c31d9SMark McLoughlin 
4012699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
4013699b045aSAlvaro Karsz 		vi->rx_usecs = 0;
4014699b045aSAlvaro Karsz 		vi->tx_usecs = 0;
4015699b045aSAlvaro Karsz 		vi->tx_max_packets = 0;
4016699b045aSAlvaro Karsz 		vi->rx_max_packets = 0;
4017699b045aSAlvaro Karsz 	}
4018699b045aSAlvaro Karsz 
401991f41f01SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
402091f41f01SAndrew Melnychenko 		vi->has_rss_hash_report = true;
402191f41f01SAndrew Melnychenko 
402291f41f01SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
4023c7114b12SAndrew Melnychenko 		vi->has_rss = true;
402491f41f01SAndrew Melnychenko 
402591f41f01SAndrew Melnychenko 	if (vi->has_rss || vi->has_rss_hash_report) {
4026c7114b12SAndrew Melnychenko 		vi->rss_indir_table_size =
4027c7114b12SAndrew Melnychenko 			virtio_cread16(vdev, offsetof(struct virtio_net_config,
4028c7114b12SAndrew Melnychenko 				rss_max_indirection_table_length));
4029c7114b12SAndrew Melnychenko 		vi->rss_key_size =
4030c7114b12SAndrew Melnychenko 			virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
4031c7114b12SAndrew Melnychenko 
4032c7114b12SAndrew Melnychenko 		vi->rss_hash_types_supported =
4033c7114b12SAndrew Melnychenko 		    virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
4034c7114b12SAndrew Melnychenko 		vi->rss_hash_types_supported &=
4035c7114b12SAndrew Melnychenko 				~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
4036c7114b12SAndrew Melnychenko 				  VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
4037c7114b12SAndrew Melnychenko 				  VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
4038c7114b12SAndrew Melnychenko 
4039c7114b12SAndrew Melnychenko 		dev->hw_features |= NETIF_F_RXHASH;
4040c7114b12SAndrew Melnychenko 	}
404191f41f01SAndrew Melnychenko 
404291f41f01SAndrew Melnychenko 	if (vi->has_rss_hash_report)
404391f41f01SAndrew Melnychenko 		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
404491f41f01SAndrew Melnychenko 	else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
4045d04302b3SMichael S. Tsirkin 		 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4046012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
4047012873d0SMichael S. Tsirkin 	else
4048012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
4049012873d0SMichael S. Tsirkin 
405075993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
405175993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
4052e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
4053e7428e95SMichael S. Tsirkin 
4054986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
4055986a4f4dSJason Wang 		vi->has_cvq = true;
4056986a4f4dSJason Wang 
405714de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
405814de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
405914de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
406014de9d11SAaron Conole 					      mtu));
406193a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
4062fe36cbe0SMichael S. Tsirkin 			/* Should never trigger: MTU was previously validated
4063fe36cbe0SMichael S. Tsirkin 			 * in virtnet_validate.
4064fe36cbe0SMichael S. Tsirkin 			 */
40657934b481SYuval Shaia 			dev_err(&vdev->dev,
40667934b481SYuval Shaia 				"device MTU appears to have changed it is now %d < %d",
40677934b481SYuval Shaia 				mtu, dev->min_mtu);
4068411ea23aSDan Carpenter 			err = -EINVAL;
4069d7dfc5cfSToshiaki Makita 			goto free;
4070fe36cbe0SMichael S. Tsirkin 		}
4071fe36cbe0SMichael S. Tsirkin 
4072d0c2c997SJarod Wilson 		dev->mtu = mtu;
407393a205eeSAaron Conole 		dev->max_mtu = mtu;
407414de9d11SAaron Conole 	}
407514de9d11SAaron Conole 
40764959aebbSGavin Li 	virtnet_set_big_packets(vi, mtu);
40774959aebbSGavin Li 
4078012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
4079012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
40806ebbc1a6SZhangjie \(HZ\) 
408144900010SJason Wang 	/* Enable multiqueue by default */
408244900010SJason Wang 	if (num_online_cpus() >= max_queue_pairs)
408344900010SJason Wang 		vi->curr_queue_pairs = max_queue_pairs;
408444900010SJason Wang 	else
408544900010SJason Wang 		vi->curr_queue_pairs = num_online_cpus();
4086986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
4087986a4f4dSJason Wang 
4088986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
40893f9c10b0SAmit Shah 	err = init_vqs(vi);
4090d2a7dddaSMichael S. Tsirkin 	if (err)
4091d7dfc5cfSToshiaki Makita 		goto free;
4092d2a7dddaSMichael S. Tsirkin 
4093fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
4094fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
4095fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
4096fbf28d78SMichael Dalton #endif
40970f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
40980f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
4099986a4f4dSJason Wang 
41002e9ca760SMichael S. Tsirkin 	virtnet_init_settings(dev);
41012e9ca760SMichael S. Tsirkin 
4102ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
4103ba5e4426SSridhar Samudrala 		vi->failover = net_failover_create(vi->dev);
41044b8e6ac4SWei Yongjun 		if (IS_ERR(vi->failover)) {
41054b8e6ac4SWei Yongjun 			err = PTR_ERR(vi->failover);
4106ba5e4426SSridhar Samudrala 			goto free_vqs;
4107ba5e4426SSridhar Samudrala 		}
41084b8e6ac4SWei Yongjun 	}
4109ba5e4426SSridhar Samudrala 
411091f41f01SAndrew Melnychenko 	if (vi->has_rss || vi->has_rss_hash_report)
4111c7114b12SAndrew Melnychenko 		virtnet_init_default_rss(vi);
4112c7114b12SAndrew Melnychenko 
411350c0ada6SJason Wang 	/* serialize netdev register + virtio_device_ready() with ndo_open() */
411450c0ada6SJason Wang 	rtnl_lock();
411550c0ada6SJason Wang 
411650c0ada6SJason Wang 	err = register_netdevice(dev);
4117296f96fcSRusty Russell 	if (err) {
4118296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
411950c0ada6SJason Wang 		rtnl_unlock();
4120ba5e4426SSridhar Samudrala 		goto free_failover;
4121296f96fcSRusty Russell 	}
4122b3369c1fSRusty Russell 
41234baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
41244baf1e33SMichael S. Tsirkin 
41259f62d221SLaurent Vivier 	/* a random MAC address has been assigned, notify the device.
41269f62d221SLaurent Vivier 	 * We don't fail probe if VIRTIO_NET_F_CTRL_MAC_ADDR is not there
41279f62d221SLaurent Vivier 	 * because many devices work fine without getting MAC explicitly
41289f62d221SLaurent Vivier 	 */
41299f62d221SLaurent Vivier 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
41309f62d221SLaurent Vivier 	    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
41319f62d221SLaurent Vivier 		struct scatterlist sg;
41329f62d221SLaurent Vivier 
41339f62d221SLaurent Vivier 		sg_init_one(&sg, dev->dev_addr, dev->addr_len);
41349f62d221SLaurent Vivier 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
41359f62d221SLaurent Vivier 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
41369f62d221SLaurent Vivier 			pr_debug("virtio_net: setting MAC address failed\n");
41379f62d221SLaurent Vivier 			rtnl_unlock();
41389f62d221SLaurent Vivier 			err = -EINVAL;
41399f62d221SLaurent Vivier 			goto free_unregister_netdev;
41409f62d221SLaurent Vivier 		}
41419f62d221SLaurent Vivier 	}
41429f62d221SLaurent Vivier 
414350c0ada6SJason Wang 	rtnl_unlock();
414450c0ada6SJason Wang 
41458017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
41468de4b2f3SWanlong Gao 	if (err) {
41478de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
4148f00e35e2Swangyunjian 		goto free_unregister_netdev;
41498de4b2f3SWanlong Gao 	}
41508de4b2f3SWanlong Gao 
4151a220871bSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
415244900010SJason Wang 
4153167c25e4SJason Wang 	/* Assume link up if device can't report link status,
4154167c25e4SJason Wang 	   otherwise get link status from config. */
4155167c25e4SJason Wang 	netif_carrier_off(dev);
4156bda7fab5SJay Vosburgh 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
41573b07e9caSTejun Heo 		schedule_work(&vi->config_work);
4158167c25e4SJason Wang 	} else {
4159167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
4160faa9b39fSJason Baron 		virtnet_update_settings(vi);
41614783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
4162167c25e4SJason Wang 	}
41639f4d26d0SMark McLoughlin 
41643f93522fSJason Wang 	for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
41653f93522fSJason Wang 		if (virtio_has_feature(vi->vdev, guest_offloads[i]))
41663f93522fSJason Wang 			set_bit(guest_offloads[i], &vi->guest_offloads);
4167a02e8964SWillem de Bruijn 	vi->guest_offloads_capable = vi->guest_offloads;
41683f93522fSJason Wang 
4169986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
4170986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
4171986a4f4dSJason Wang 
4172296f96fcSRusty Russell 	return 0;
4173296f96fcSRusty Russell 
4174f00e35e2Swangyunjian free_unregister_netdev:
4175b3369c1fSRusty Russell 	unregister_netdev(dev);
4176ba5e4426SSridhar Samudrala free_failover:
4177ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
4178d2a7dddaSMichael S. Tsirkin free_vqs:
4179b0686565SLi Zetao 	virtio_reset_device(vdev);
4180986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
4181fb51879dSMichael Dalton 	free_receive_page_frags(vi);
4182e9d7417bSJason Wang 	virtnet_del_vqs(vi);
4183296f96fcSRusty Russell free:
4184296f96fcSRusty Russell 	free_netdev(dev);
4185296f96fcSRusty Russell 	return err;
4186296f96fcSRusty Russell }
4187296f96fcSRusty Russell 
418804486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
4189296f96fcSRusty Russell {
4190d9679d00SMichael S. Tsirkin 	virtio_reset_device(vi->vdev);
4191830a8a97SShirley Ma 
4192830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
41939ab86bbcSShirley Ma 	free_unused_bufs(vi);
4194fb6813f4SRusty Russell 
4195986a4f4dSJason Wang 	free_receive_bufs(vi);
4196d2a7dddaSMichael S. Tsirkin 
4197fb51879dSMichael Dalton 	free_receive_page_frags(vi);
4198fb51879dSMichael Dalton 
4199986a4f4dSJason Wang 	virtnet_del_vqs(vi);
420004486ed0SAmit Shah }
420104486ed0SAmit Shah 
42028cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
420304486ed0SAmit Shah {
420404486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
420504486ed0SAmit Shah 
42068017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
42078de4b2f3SWanlong Gao 
4208102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
4209102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
4210586d17c5SJason Wang 
421104486ed0SAmit Shah 	unregister_netdev(vi->dev);
421204486ed0SAmit Shah 
4213ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
4214ba5e4426SSridhar Samudrala 
421504486ed0SAmit Shah 	remove_vq_common(vi);
4216fb6813f4SRusty Russell 
421774b2553fSRusty Russell 	free_netdev(vi->dev);
4218296f96fcSRusty Russell }
4219296f96fcSRusty Russell 
422067a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
42210741bcb5SAmit Shah {
42220741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
42230741bcb5SAmit Shah 
42248017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
42259fe7bfceSJohn Fastabend 	virtnet_freeze_down(vdev);
42260741bcb5SAmit Shah 	remove_vq_common(vi);
42270741bcb5SAmit Shah 
42280741bcb5SAmit Shah 	return 0;
42290741bcb5SAmit Shah }
42300741bcb5SAmit Shah 
423167a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
42320741bcb5SAmit Shah {
42330741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
42349fe7bfceSJohn Fastabend 	int err;
42350741bcb5SAmit Shah 
42369fe7bfceSJohn Fastabend 	err = virtnet_restore_up(vdev);
42370741bcb5SAmit Shah 	if (err)
42380741bcb5SAmit Shah 		return err;
4239986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
4240986a4f4dSJason Wang 
42418017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
42423f2869caSXie Yongji 	if (err) {
42433f2869caSXie Yongji 		virtnet_freeze_down(vdev);
42443f2869caSXie Yongji 		remove_vq_common(vi);
4245ec9debbdSJason Wang 		return err;
42463f2869caSXie Yongji 	}
4247ec9debbdSJason Wang 
42480741bcb5SAmit Shah 	return 0;
42490741bcb5SAmit Shah }
42500741bcb5SAmit Shah 
4251296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
4252296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4253296f96fcSRusty Russell 	{ 0 },
4254296f96fcSRusty Russell };
4255296f96fcSRusty Russell 
4256f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \
4257f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4258f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MAC, \
4259f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4260f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4261f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4262418044e1SAndrew Melnychenko 	VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4263f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4264f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4265f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4266f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
4267faa9b39fSJason Baron 	VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4268c7114b12SAndrew Melnychenko 	VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4269be50da3eSJiri Pirko 	VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL, \
4270be50da3eSJiri Pirko 	VIRTIO_NET_F_GUEST_HDRLEN
4271f3358507SMichael S. Tsirkin 
4272c45a6816SRusty Russell static unsigned int features[] = {
4273f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
4274f3358507SMichael S. Tsirkin };
4275f3358507SMichael S. Tsirkin 
4276f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = {
4277f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
4278f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GSO,
4279e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
4280c45a6816SRusty Russell };
4281c45a6816SRusty Russell 
428222402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
4283c45a6816SRusty Russell 	.feature_table = features,
4284c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
4285f3358507SMichael S. Tsirkin 	.feature_table_legacy = features_legacy,
4286f3358507SMichael S. Tsirkin 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4287296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
4288296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
4289296f96fcSRusty Russell 	.id_table =	id_table,
4290fe36cbe0SMichael S. Tsirkin 	.validate =	virtnet_validate,
4291296f96fcSRusty Russell 	.probe =	virtnet_probe,
42928cc085d6SBill Pemberton 	.remove =	virtnet_remove,
42939f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
429489107000SAaron Lu #ifdef CONFIG_PM_SLEEP
42950741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
42960741bcb5SAmit Shah 	.restore =	virtnet_restore,
42970741bcb5SAmit Shah #endif
4298296f96fcSRusty Russell };
4299296f96fcSRusty Russell 
43008017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
43018017c279SSebastian Andrzej Siewior {
43028017c279SSebastian Andrzej Siewior 	int ret;
43038017c279SSebastian Andrzej Siewior 
430473c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
43058017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
43068017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
43078017c279SSebastian Andrzej Siewior 	if (ret < 0)
43088017c279SSebastian Andrzej Siewior 		goto out;
43098017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
431073c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
43118017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
43128017c279SSebastian Andrzej Siewior 	if (ret)
43138017c279SSebastian Andrzej Siewior 		goto err_dead;
43148017c279SSebastian Andrzej Siewior 	ret = register_virtio_driver(&virtio_net_driver);
43158017c279SSebastian Andrzej Siewior 	if (ret)
43168017c279SSebastian Andrzej Siewior 		goto err_virtio;
43178017c279SSebastian Andrzej Siewior 	return 0;
43188017c279SSebastian Andrzej Siewior err_virtio:
43198017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
43208017c279SSebastian Andrzej Siewior err_dead:
43218017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
43228017c279SSebastian Andrzej Siewior out:
43238017c279SSebastian Andrzej Siewior 	return ret;
43248017c279SSebastian Andrzej Siewior }
43258017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
43268017c279SSebastian Andrzej Siewior 
43278017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
43288017c279SSebastian Andrzej Siewior {
4329cfa0ebc9SAndrew Jones 	unregister_virtio_driver(&virtio_net_driver);
43308017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
43318017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
43328017c279SSebastian Andrzej Siewior }
43338017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
4334296f96fcSRusty Russell 
4335296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
4336296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
4337296f96fcSRusty Russell MODULE_LICENSE("GPL");
4338