xref: /openbmc/linux/drivers/net/virtio_net.c (revision ad7e615f646c9b5b2cf655cdfb9d91a28db4f25a)
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,
65418044e1SAndrew Melnychenko 	VIRTIO_NET_F_GUEST_USO6
667acd4329SColin Ian King };
673f93522fSJason Wang 
68dbcf24d1SJason Wang #define GUEST_OFFLOAD_GRO_HW_MASK ((1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
691a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
701a03b8a3STonghao Zhang 				(1ULL << VIRTIO_NET_F_GUEST_ECN)  | \
71418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_UFO)  | \
72418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_USO4) | \
73418044e1SAndrew Melnychenko 				(1ULL << VIRTIO_NET_F_GUEST_USO6))
741a03b8a3STonghao Zhang 
75d7dfc5cfSToshiaki Makita struct virtnet_stat_desc {
76d7dfc5cfSToshiaki Makita 	char desc[ETH_GSTRING_LEN];
77d7dfc5cfSToshiaki Makita 	size_t offset;
783fa2a1dfSstephen hemminger };
793fa2a1dfSstephen hemminger 
80d7dfc5cfSToshiaki Makita struct virtnet_sq_stats {
81d7dfc5cfSToshiaki Makita 	struct u64_stats_sync syncp;
82d7dfc5cfSToshiaki Makita 	u64 packets;
83d7dfc5cfSToshiaki Makita 	u64 bytes;
845b8f3c8dSToshiaki Makita 	u64 xdp_tx;
855b8f3c8dSToshiaki Makita 	u64 xdp_tx_drops;
86461f03dcSToshiaki Makita 	u64 kicks;
87a520794bSTony Lu 	u64 tx_timeouts;
88d7dfc5cfSToshiaki Makita };
89d7dfc5cfSToshiaki Makita 
90d46eeeafSJason Wang struct virtnet_rq_stats {
91d46eeeafSJason Wang 	struct u64_stats_sync syncp;
92d7dfc5cfSToshiaki Makita 	u64 packets;
93d7dfc5cfSToshiaki Makita 	u64 bytes;
942c4a2f7dSToshiaki Makita 	u64 drops;
955b8f3c8dSToshiaki Makita 	u64 xdp_packets;
965b8f3c8dSToshiaki Makita 	u64 xdp_tx;
975b8f3c8dSToshiaki Makita 	u64 xdp_redirects;
985b8f3c8dSToshiaki Makita 	u64 xdp_drops;
99461f03dcSToshiaki Makita 	u64 kicks;
100d7dfc5cfSToshiaki Makita };
101d7dfc5cfSToshiaki Makita 
102d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STAT(m)	offsetof(struct virtnet_sq_stats, m)
103d46eeeafSJason Wang #define VIRTNET_RQ_STAT(m)	offsetof(struct virtnet_rq_stats, m)
104d7dfc5cfSToshiaki Makita 
105d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_sq_stats_desc[] = {
106d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_SQ_STAT(packets) },
107d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_SQ_STAT(bytes) },
1085b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_SQ_STAT(xdp_tx) },
1095b8f3c8dSToshiaki Makita 	{ "xdp_tx_drops",	VIRTNET_SQ_STAT(xdp_tx_drops) },
110461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_SQ_STAT(kicks) },
111a520794bSTony Lu 	{ "tx_timeouts",	VIRTNET_SQ_STAT(tx_timeouts) },
112d7dfc5cfSToshiaki Makita };
113d7dfc5cfSToshiaki Makita 
114d7dfc5cfSToshiaki Makita static const struct virtnet_stat_desc virtnet_rq_stats_desc[] = {
115d7dfc5cfSToshiaki Makita 	{ "packets",		VIRTNET_RQ_STAT(packets) },
116d7dfc5cfSToshiaki Makita 	{ "bytes",		VIRTNET_RQ_STAT(bytes) },
1172c4a2f7dSToshiaki Makita 	{ "drops",		VIRTNET_RQ_STAT(drops) },
1185b8f3c8dSToshiaki Makita 	{ "xdp_packets",	VIRTNET_RQ_STAT(xdp_packets) },
1195b8f3c8dSToshiaki Makita 	{ "xdp_tx",		VIRTNET_RQ_STAT(xdp_tx) },
1205b8f3c8dSToshiaki Makita 	{ "xdp_redirects",	VIRTNET_RQ_STAT(xdp_redirects) },
1215b8f3c8dSToshiaki Makita 	{ "xdp_drops",		VIRTNET_RQ_STAT(xdp_drops) },
122461f03dcSToshiaki Makita 	{ "kicks",		VIRTNET_RQ_STAT(kicks) },
123d7dfc5cfSToshiaki Makita };
124d7dfc5cfSToshiaki Makita 
125d7dfc5cfSToshiaki Makita #define VIRTNET_SQ_STATS_LEN	ARRAY_SIZE(virtnet_sq_stats_desc)
126d7dfc5cfSToshiaki Makita #define VIRTNET_RQ_STATS_LEN	ARRAY_SIZE(virtnet_rq_stats_desc)
127d7dfc5cfSToshiaki Makita 
128e9d7417bSJason Wang /* Internal representation of a send virtqueue */
129e9d7417bSJason Wang struct send_queue {
130e9d7417bSJason Wang 	/* Virtqueue associated with this send _queue */
131e9d7417bSJason Wang 	struct virtqueue *vq;
132e9d7417bSJason Wang 
133e9d7417bSJason Wang 	/* TX: fragments + linear part + virtio header */
134e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
135986a4f4dSJason Wang 
136986a4f4dSJason Wang 	/* Name of the send queue: output.$index */
137986a4f4dSJason Wang 	char name[40];
138b92f1e67SWillem de Bruijn 
139d7dfc5cfSToshiaki Makita 	struct virtnet_sq_stats stats;
140d7dfc5cfSToshiaki Makita 
141b92f1e67SWillem de Bruijn 	struct napi_struct napi;
142ebcce492SXuan Zhuo 
143ebcce492SXuan Zhuo 	/* Record whether sq is in reset state. */
144ebcce492SXuan Zhuo 	bool reset;
145e9d7417bSJason Wang };
146e9d7417bSJason Wang 
147e9d7417bSJason Wang /* Internal representation of a receive virtqueue */
148e9d7417bSJason Wang struct receive_queue {
149e9d7417bSJason Wang 	/* Virtqueue associated with this receive_queue */
150e9d7417bSJason Wang 	struct virtqueue *vq;
151e9d7417bSJason Wang 
152296f96fcSRusty Russell 	struct napi_struct napi;
153296f96fcSRusty Russell 
154f600b690SJohn Fastabend 	struct bpf_prog __rcu *xdp_prog;
155f600b690SJohn Fastabend 
156d7dfc5cfSToshiaki Makita 	struct virtnet_rq_stats stats;
157d7dfc5cfSToshiaki Makita 
158e9d7417bSJason Wang 	/* Chain pages by the private ptr. */
159e9d7417bSJason Wang 	struct page *pages;
160e9d7417bSJason Wang 
161ab7db917SMichael Dalton 	/* Average packet length for mergeable receive buffers. */
1625377d758SJohannes Berg 	struct ewma_pkt_len mrg_avg_pkt_len;
163ab7db917SMichael Dalton 
164fb51879dSMichael Dalton 	/* Page frag for packet buffer allocation. */
165fb51879dSMichael Dalton 	struct page_frag alloc_frag;
166fb51879dSMichael Dalton 
167e9d7417bSJason Wang 	/* RX: fragments + linear part + virtio header */
168e9d7417bSJason Wang 	struct scatterlist sg[MAX_SKB_FRAGS + 2];
169986a4f4dSJason Wang 
170d85b758fSMichael S. Tsirkin 	/* Min single buffer size for mergeable buffers case. */
171d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len;
172d85b758fSMichael S. Tsirkin 
173986a4f4dSJason Wang 	/* Name of this receive queue: input.$index */
174986a4f4dSJason Wang 	char name[40];
175754b8a21SJesper Dangaard Brouer 
176754b8a21SJesper Dangaard Brouer 	struct xdp_rxq_info xdp_rxq;
177e9d7417bSJason Wang };
178e9d7417bSJason Wang 
179c7114b12SAndrew Melnychenko /* This structure can contain rss message with maximum settings for indirection table and keysize
180c7114b12SAndrew Melnychenko  * Note, that default structure that describes RSS configuration virtio_net_rss_config
181c7114b12SAndrew Melnychenko  * contains same info but can't handle table values.
182c7114b12SAndrew Melnychenko  * In any case, structure would be passed to virtio hw through sg_buf split by parts
183c7114b12SAndrew Melnychenko  * because table sizes may be differ according to the device configuration.
184c7114b12SAndrew Melnychenko  */
185c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_KEY_SIZE     40
186c7114b12SAndrew Melnychenko #define VIRTIO_NET_RSS_MAX_TABLE_LEN    128
187c7114b12SAndrew Melnychenko struct virtio_net_ctrl_rss {
188c7114b12SAndrew Melnychenko 	u32 hash_types;
189c7114b12SAndrew Melnychenko 	u16 indirection_table_mask;
190c7114b12SAndrew Melnychenko 	u16 unclassified_queue;
191c7114b12SAndrew Melnychenko 	u16 indirection_table[VIRTIO_NET_RSS_MAX_TABLE_LEN];
192c7114b12SAndrew Melnychenko 	u16 max_tx_vq;
193c7114b12SAndrew Melnychenko 	u8 hash_key_length;
194c7114b12SAndrew Melnychenko 	u8 key[VIRTIO_NET_RSS_MAX_KEY_SIZE];
195c7114b12SAndrew Melnychenko };
196c7114b12SAndrew Melnychenko 
19712e57169SMichael S. Tsirkin /* Control VQ buffers: protected by the rtnl lock */
19812e57169SMichael S. Tsirkin struct control_buf {
19912e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_hdr hdr;
20012e57169SMichael S. Tsirkin 	virtio_net_ctrl_ack status;
20112e57169SMichael S. Tsirkin 	struct virtio_net_ctrl_mq mq;
20212e57169SMichael S. Tsirkin 	u8 promisc;
20312e57169SMichael S. Tsirkin 	u8 allmulti;
204d7fad4c8SMichael S. Tsirkin 	__virtio16 vid;
205f4ee703aSMichael S. Tsirkin 	__virtio64 offloads;
206c7114b12SAndrew Melnychenko 	struct virtio_net_ctrl_rss rss;
20712e57169SMichael S. Tsirkin };
20812e57169SMichael S. Tsirkin 
209e9d7417bSJason Wang struct virtnet_info {
210e9d7417bSJason Wang 	struct virtio_device *vdev;
211e9d7417bSJason Wang 	struct virtqueue *cvq;
212e9d7417bSJason Wang 	struct net_device *dev;
213986a4f4dSJason Wang 	struct send_queue *sq;
214986a4f4dSJason Wang 	struct receive_queue *rq;
215e9d7417bSJason Wang 	unsigned int status;
216e9d7417bSJason Wang 
217986a4f4dSJason Wang 	/* Max # of queue pairs supported by the device */
218986a4f4dSJason Wang 	u16 max_queue_pairs;
219986a4f4dSJason Wang 
220986a4f4dSJason Wang 	/* # of queue pairs currently used by the driver */
221986a4f4dSJason Wang 	u16 curr_queue_pairs;
222986a4f4dSJason Wang 
223672aafd5SJohn Fastabend 	/* # of XDP queue pairs currently used by the driver */
224672aafd5SJohn Fastabend 	u16 xdp_queue_pairs;
225672aafd5SJohn Fastabend 
22697c2c69eSXuan Zhuo 	/* xdp_queue_pairs may be 0, when xdp is already loaded. So add this. */
22797c2c69eSXuan Zhuo 	bool xdp_enabled;
22897c2c69eSXuan Zhuo 
22997402b96SHerbert Xu 	/* I like... big packets and I cannot lie! */
23097402b96SHerbert Xu 	bool big_packets;
23197402b96SHerbert Xu 
2324959aebbSGavin Li 	/* number of sg entries allocated for big packets */
2334959aebbSGavin Li 	unsigned int big_packets_num_skbfrags;
2344959aebbSGavin Li 
2353f2c31d9SMark McLoughlin 	/* Host will merge rx buffers for big packets (shake it! shake it!) */
2363f2c31d9SMark McLoughlin 	bool mergeable_rx_bufs;
2373f2c31d9SMark McLoughlin 
238c7114b12SAndrew Melnychenko 	/* Host supports rss and/or hash report */
239c7114b12SAndrew Melnychenko 	bool has_rss;
24091f41f01SAndrew Melnychenko 	bool has_rss_hash_report;
241c7114b12SAndrew Melnychenko 	u8 rss_key_size;
242c7114b12SAndrew Melnychenko 	u16 rss_indir_table_size;
243c7114b12SAndrew Melnychenko 	u32 rss_hash_types_supported;
244c1170820SAndrew Melnychenko 	u32 rss_hash_types_saved;
245c7114b12SAndrew Melnychenko 
246986a4f4dSJason Wang 	/* Has control virtqueue */
247986a4f4dSJason Wang 	bool has_cvq;
248986a4f4dSJason Wang 
249e7428e95SMichael S. Tsirkin 	/* Host can handle any s/g split between our header and packet data */
250e7428e95SMichael S. Tsirkin 	bool any_header_sg;
251e7428e95SMichael S. Tsirkin 
252012873d0SMichael S. Tsirkin 	/* Packet virtio header size */
253012873d0SMichael S. Tsirkin 	u8 hdr_len;
254012873d0SMichael S. Tsirkin 
2555a159128SJason Wang 	/* Work struct for delayed refilling if we run low on memory. */
2563161e453SRusty Russell 	struct delayed_work refill;
2573161e453SRusty Russell 
2585a159128SJason Wang 	/* Is delayed refill enabled? */
2595a159128SJason Wang 	bool refill_enabled;
2605a159128SJason Wang 
2615a159128SJason Wang 	/* The lock to synchronize the access to refill_enabled */
2625a159128SJason Wang 	spinlock_t refill_lock;
2635a159128SJason Wang 
264586d17c5SJason Wang 	/* Work struct for config space updates */
265586d17c5SJason Wang 	struct work_struct config_work;
266586d17c5SJason Wang 
267986a4f4dSJason Wang 	/* Does the affinity hint is set for virtqueues? */
268986a4f4dSJason Wang 	bool affinity_hint_set;
26947be2479SWanlong Gao 
2708017c279SSebastian Andrzej Siewior 	/* CPU hotplug instances for online & dead */
2718017c279SSebastian Andrzej Siewior 	struct hlist_node node;
2728017c279SSebastian Andrzej Siewior 	struct hlist_node node_dead;
2732ac46030SMichael S. Tsirkin 
27412e57169SMichael S. Tsirkin 	struct control_buf *ctrl;
27516032be5SNikolay Aleksandrov 
27616032be5SNikolay Aleksandrov 	/* Ethtool settings */
27716032be5SNikolay Aleksandrov 	u8 duplex;
27816032be5SNikolay Aleksandrov 	u32 speed;
2793f93522fSJason Wang 
280699b045aSAlvaro Karsz 	/* Interrupt coalescing settings */
281699b045aSAlvaro Karsz 	u32 tx_usecs;
282699b045aSAlvaro Karsz 	u32 rx_usecs;
283699b045aSAlvaro Karsz 	u32 tx_max_packets;
284699b045aSAlvaro Karsz 	u32 rx_max_packets;
285699b045aSAlvaro Karsz 
2863f93522fSJason Wang 	unsigned long guest_offloads;
287a02e8964SWillem de Bruijn 	unsigned long guest_offloads_capable;
288ba5e4426SSridhar Samudrala 
289ba5e4426SSridhar Samudrala 	/* failover when STANDBY feature enabled */
290ba5e4426SSridhar Samudrala 	struct failover *failover;
291296f96fcSRusty Russell };
292296f96fcSRusty Russell 
2939ab86bbcSShirley Ma struct padded_vnet_hdr {
294c1ddc42dSAndrew Melnychenko 	struct virtio_net_hdr_v1_hash hdr;
2959ab86bbcSShirley Ma 	/*
296012873d0SMichael S. Tsirkin 	 * hdr is in a separate sg buffer, and data sg buffer shares same page
297012873d0SMichael S. Tsirkin 	 * with this header sg. This padding makes next sg 16 byte aligned
298012873d0SMichael S. Tsirkin 	 * after the header.
2999ab86bbcSShirley Ma 	 */
300c1ddc42dSAndrew Melnychenko 	char padding[12];
3019ab86bbcSShirley Ma };
3029ab86bbcSShirley Ma 
3036a4763e2SXuan Zhuo static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf);
304ebcce492SXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf);
3056a4763e2SXuan Zhuo 
3065050471dSToshiaki Makita static bool is_xdp_frame(void *ptr)
3075050471dSToshiaki Makita {
3085050471dSToshiaki Makita 	return (unsigned long)ptr & VIRTIO_XDP_FLAG;
3095050471dSToshiaki Makita }
3105050471dSToshiaki Makita 
3115050471dSToshiaki Makita static void *xdp_to_ptr(struct xdp_frame *ptr)
3125050471dSToshiaki Makita {
3135050471dSToshiaki Makita 	return (void *)((unsigned long)ptr | VIRTIO_XDP_FLAG);
3145050471dSToshiaki Makita }
3155050471dSToshiaki Makita 
3165050471dSToshiaki Makita static struct xdp_frame *ptr_to_xdp(void *ptr)
3175050471dSToshiaki Makita {
3185050471dSToshiaki Makita 	return (struct xdp_frame *)((unsigned long)ptr & ~VIRTIO_XDP_FLAG);
3195050471dSToshiaki Makita }
3205050471dSToshiaki Makita 
321986a4f4dSJason Wang /* Converting between virtqueue no. and kernel tx/rx queue no.
322986a4f4dSJason Wang  * 0:rx0 1:tx0 2:rx1 3:tx1 ... 2N:rxN 2N+1:txN 2N+2:cvq
323986a4f4dSJason Wang  */
324986a4f4dSJason Wang static int vq2txq(struct virtqueue *vq)
325986a4f4dSJason Wang {
3269d0ca6edSRusty Russell 	return (vq->index - 1) / 2;
327986a4f4dSJason Wang }
328986a4f4dSJason Wang 
329986a4f4dSJason Wang static int txq2vq(int txq)
330986a4f4dSJason Wang {
331986a4f4dSJason Wang 	return txq * 2 + 1;
332986a4f4dSJason Wang }
333986a4f4dSJason Wang 
334986a4f4dSJason Wang static int vq2rxq(struct virtqueue *vq)
335986a4f4dSJason Wang {
3369d0ca6edSRusty Russell 	return vq->index / 2;
337986a4f4dSJason Wang }
338986a4f4dSJason Wang 
339986a4f4dSJason Wang static int rxq2vq(int rxq)
340986a4f4dSJason Wang {
341986a4f4dSJason Wang 	return rxq * 2;
342986a4f4dSJason Wang }
343986a4f4dSJason Wang 
344012873d0SMichael S. Tsirkin static inline struct virtio_net_hdr_mrg_rxbuf *skb_vnet_hdr(struct sk_buff *skb)
345296f96fcSRusty Russell {
346012873d0SMichael S. Tsirkin 	return (struct virtio_net_hdr_mrg_rxbuf *)skb->cb;
347296f96fcSRusty Russell }
348296f96fcSRusty Russell 
3499ab86bbcSShirley Ma /*
3509ab86bbcSShirley Ma  * private is used to chain pages for big packets, put the whole
3519ab86bbcSShirley Ma  * most recent used list in the beginning for reuse
3529ab86bbcSShirley Ma  */
353e9d7417bSJason Wang static void give_pages(struct receive_queue *rq, struct page *page)
354fb6813f4SRusty Russell {
3559ab86bbcSShirley Ma 	struct page *end;
3569ab86bbcSShirley Ma 
357e9d7417bSJason Wang 	/* Find end of list, sew whole thing into vi->rq.pages. */
3589ab86bbcSShirley Ma 	for (end = page; end->private; end = (struct page *)end->private);
359e9d7417bSJason Wang 	end->private = (unsigned long)rq->pages;
360e9d7417bSJason Wang 	rq->pages = page;
361fb6813f4SRusty Russell }
362fb6813f4SRusty Russell 
363e9d7417bSJason Wang static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask)
364fb6813f4SRusty Russell {
365e9d7417bSJason Wang 	struct page *p = rq->pages;
366fb6813f4SRusty Russell 
3679ab86bbcSShirley Ma 	if (p) {
368e9d7417bSJason Wang 		rq->pages = (struct page *)p->private;
3699ab86bbcSShirley Ma 		/* clear private here, it is used to chain pages */
3709ab86bbcSShirley Ma 		p->private = 0;
3719ab86bbcSShirley Ma 	} else
372fb6813f4SRusty Russell 		p = alloc_page(gfp_mask);
373fb6813f4SRusty Russell 	return p;
374fb6813f4SRusty Russell }
375fb6813f4SRusty Russell 
3765a159128SJason Wang static void enable_delayed_refill(struct virtnet_info *vi)
3775a159128SJason Wang {
3785a159128SJason Wang 	spin_lock_bh(&vi->refill_lock);
3795a159128SJason Wang 	vi->refill_enabled = true;
3805a159128SJason Wang 	spin_unlock_bh(&vi->refill_lock);
3815a159128SJason Wang }
3825a159128SJason Wang 
3835a159128SJason Wang static void disable_delayed_refill(struct virtnet_info *vi)
3845a159128SJason Wang {
3855a159128SJason Wang 	spin_lock_bh(&vi->refill_lock);
3865a159128SJason Wang 	vi->refill_enabled = false;
3875a159128SJason Wang 	spin_unlock_bh(&vi->refill_lock);
3885a159128SJason Wang }
3895a159128SJason Wang 
390e4e8452aSWillem de Bruijn static void virtqueue_napi_schedule(struct napi_struct *napi,
391e4e8452aSWillem de Bruijn 				    struct virtqueue *vq)
392e4e8452aSWillem de Bruijn {
393e4e8452aSWillem de Bruijn 	if (napi_schedule_prep(napi)) {
394e4e8452aSWillem de Bruijn 		virtqueue_disable_cb(vq);
395e4e8452aSWillem de Bruijn 		__napi_schedule(napi);
396e4e8452aSWillem de Bruijn 	}
397e4e8452aSWillem de Bruijn }
398e4e8452aSWillem de Bruijn 
399e4e8452aSWillem de Bruijn static void virtqueue_napi_complete(struct napi_struct *napi,
400e4e8452aSWillem de Bruijn 				    struct virtqueue *vq, int processed)
401e4e8452aSWillem de Bruijn {
402e4e8452aSWillem de Bruijn 	int opaque;
403e4e8452aSWillem de Bruijn 
404e4e8452aSWillem de Bruijn 	opaque = virtqueue_enable_cb_prepare(vq);
405fdaa767aSToshiaki Makita 	if (napi_complete_done(napi, processed)) {
406fdaa767aSToshiaki Makita 		if (unlikely(virtqueue_poll(vq, opaque)))
407e4e8452aSWillem de Bruijn 			virtqueue_napi_schedule(napi, vq);
408fdaa767aSToshiaki Makita 	} else {
409fdaa767aSToshiaki Makita 		virtqueue_disable_cb(vq);
410fdaa767aSToshiaki Makita 	}
411e4e8452aSWillem de Bruijn }
412e4e8452aSWillem de Bruijn 
413e9d7417bSJason Wang static void skb_xmit_done(struct virtqueue *vq)
414296f96fcSRusty Russell {
415e9d7417bSJason Wang 	struct virtnet_info *vi = vq->vdev->priv;
416b92f1e67SWillem de Bruijn 	struct napi_struct *napi = &vi->sq[vq2txq(vq)].napi;
417296f96fcSRusty Russell 
4182cb9c6baSRusty Russell 	/* Suppress further interrupts. */
419e9d7417bSJason Wang 	virtqueue_disable_cb(vq);
42011a3a154SRusty Russell 
421b92f1e67SWillem de Bruijn 	if (napi->weight)
422b92f1e67SWillem de Bruijn 		virtqueue_napi_schedule(napi, vq);
423b92f1e67SWillem de Bruijn 	else
424363f1514SRusty Russell 		/* We were probably waiting for more output buffers. */
425986a4f4dSJason Wang 		netif_wake_subqueue(vi->dev, vq2txq(vq));
426296f96fcSRusty Russell }
427296f96fcSRusty Russell 
42828b39bc7SJason Wang #define MRG_CTX_HEADER_SHIFT 22
42928b39bc7SJason Wang static void *mergeable_len_to_ctx(unsigned int truesize,
43028b39bc7SJason Wang 				  unsigned int headroom)
43128b39bc7SJason Wang {
43228b39bc7SJason Wang 	return (void *)(unsigned long)((headroom << MRG_CTX_HEADER_SHIFT) | truesize);
43328b39bc7SJason Wang }
43428b39bc7SJason Wang 
43528b39bc7SJason Wang static unsigned int mergeable_ctx_to_headroom(void *mrg_ctx)
43628b39bc7SJason Wang {
43728b39bc7SJason Wang 	return (unsigned long)mrg_ctx >> MRG_CTX_HEADER_SHIFT;
43828b39bc7SJason Wang }
43928b39bc7SJason Wang 
44028b39bc7SJason Wang static unsigned int mergeable_ctx_to_truesize(void *mrg_ctx)
44128b39bc7SJason Wang {
44228b39bc7SJason Wang 	return (unsigned long)mrg_ctx & ((1 << MRG_CTX_HEADER_SHIFT) - 1);
44328b39bc7SJason Wang }
44428b39bc7SJason Wang 
4453464645aSMike Waychison /* Called from bottom half context */
446946fa564SMichael S. Tsirkin static struct sk_buff *page_to_skb(struct virtnet_info *vi,
447946fa564SMichael S. Tsirkin 				   struct receive_queue *rq,
4482613af0eSMichael Dalton 				   struct page *page, unsigned int offset,
449436c9453SJason Wang 				   unsigned int len, unsigned int truesize,
450fb32856bSXuan Zhuo 				   bool hdr_valid, unsigned int metasize,
451c32325b8SJakub Kicinski 				   unsigned int headroom)
4529ab86bbcSShirley Ma {
4539ab86bbcSShirley Ma 	struct sk_buff *skb;
454012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
4552613af0eSMichael Dalton 	unsigned int copy, hdr_len, hdr_padded_len;
456af39c8f7SEric Dumazet 	struct page *page_to_free = NULL;
457fb32856bSXuan Zhuo 	int tailroom, shinfo_size;
458f80bd740SXuan Zhuo 	char *p, *hdr_p, *buf;
4599ab86bbcSShirley Ma 
4602613af0eSMichael Dalton 	p = page_address(page) + offset;
461fb32856bSXuan Zhuo 	hdr_p = p;
4629ab86bbcSShirley Ma 
463012873d0SMichael S. Tsirkin 	hdr_len = vi->hdr_len;
464012873d0SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
465c1ddc42dSAndrew Melnychenko 		hdr_padded_len = hdr_len;
466012873d0SMichael S. Tsirkin 	else
4672613af0eSMichael Dalton 		hdr_padded_len = sizeof(struct padded_vnet_hdr);
4683f2c31d9SMark McLoughlin 
469c32325b8SJakub Kicinski 	/* If headroom is not 0, there is an offset between the beginning of the
470fb32856bSXuan Zhuo 	 * data and the allocated space, otherwise the data and the allocated
471fb32856bSXuan Zhuo 	 * space are aligned.
4728fb7da9eSXuan Zhuo 	 *
4738fb7da9eSXuan Zhuo 	 * Buffers with headroom use PAGE_SIZE as alloc size, see
4748fb7da9eSXuan Zhuo 	 * add_recvbuf_mergeable() + get_mergeable_buf_len()
475fb32856bSXuan Zhuo 	 */
476c32325b8SJakub Kicinski 	truesize = headroom ? PAGE_SIZE : truesize;
477fc02e8cbSMichael S. Tsirkin 	tailroom = truesize - headroom;
478c32325b8SJakub Kicinski 	buf = p - headroom;
4793f2c31d9SMark McLoughlin 
4809ab86bbcSShirley Ma 	len -= hdr_len;
4812613af0eSMichael Dalton 	offset += hdr_padded_len;
4822613af0eSMichael Dalton 	p += hdr_padded_len;
483fc02e8cbSMichael S. Tsirkin 	tailroom -= hdr_padded_len + len;
4843f2c31d9SMark McLoughlin 
485fb32856bSXuan Zhuo 	shinfo_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
486fb32856bSXuan Zhuo 
487f80bd740SXuan Zhuo 	/* copy small packet so we can reuse these pages */
488f5d7872aSEric Dumazet 	if (!NET_IP_ALIGN && len > GOOD_COPY_LEN && tailroom >= shinfo_size) {
489f80bd740SXuan Zhuo 		skb = build_skb(buf, truesize);
490fb32856bSXuan Zhuo 		if (unlikely(!skb))
491fb32856bSXuan Zhuo 			return NULL;
492fb32856bSXuan Zhuo 
493f80bd740SXuan Zhuo 		skb_reserve(skb, p - buf);
494fb32856bSXuan Zhuo 		skb_put(skb, len);
495afd92d82SJason Wang 
496afd92d82SJason Wang 		page = (struct page *)page->private;
497afd92d82SJason Wang 		if (page)
498afd92d82SJason Wang 			give_pages(rq, page);
499fb32856bSXuan Zhuo 		goto ok;
500fb32856bSXuan Zhuo 	}
501fb32856bSXuan Zhuo 
502fb32856bSXuan Zhuo 	/* copy small packet so we can reuse these pages for small data */
503fb32856bSXuan Zhuo 	skb = napi_alloc_skb(&rq->napi, GOOD_COPY_LEN);
504fb32856bSXuan Zhuo 	if (unlikely(!skb))
505fb32856bSXuan Zhuo 		return NULL;
506fb32856bSXuan Zhuo 
5070f6925b3SEric Dumazet 	/* Copy all frame if it fits skb->head, otherwise
5080f6925b3SEric Dumazet 	 * we let virtio_net_hdr_to_skb() and GRO pull headers as needed.
5090f6925b3SEric Dumazet 	 */
5100f6925b3SEric Dumazet 	if (len <= skb_tailroom(skb))
5113f2c31d9SMark McLoughlin 		copy = len;
5120f6925b3SEric Dumazet 	else
5130f6925b3SEric Dumazet 		copy = ETH_HLEN + metasize;
51459ae1d12SJohannes Berg 	skb_put_data(skb, p, copy);
5153f2c31d9SMark McLoughlin 
5163f2c31d9SMark McLoughlin 	len -= copy;
5179ab86bbcSShirley Ma 	offset += copy;
5183f2c31d9SMark McLoughlin 
5192613af0eSMichael Dalton 	if (vi->mergeable_rx_bufs) {
5202613af0eSMichael Dalton 		if (len)
5212613af0eSMichael Dalton 			skb_add_rx_frag(skb, 0, page, offset, len, truesize);
5222613af0eSMichael Dalton 		else
523af39c8f7SEric Dumazet 			page_to_free = page;
524fb32856bSXuan Zhuo 		goto ok;
5252613af0eSMichael Dalton 	}
5262613af0eSMichael Dalton 
527e878d78bSSasha Levin 	/*
528e878d78bSSasha Levin 	 * Verify that we can indeed put this data into a skb.
529e878d78bSSasha Levin 	 * This is here to handle cases when the device erroneously
530e878d78bSSasha Levin 	 * tries to receive more than is possible. This is usually
531e878d78bSSasha Levin 	 * the case of a broken device.
532e878d78bSSasha Levin 	 */
533e878d78bSSasha Levin 	if (unlikely(len > MAX_SKB_FRAGS * PAGE_SIZE)) {
534be443899SAmerigo Wang 		net_dbg_ratelimited("%s: too much data\n", skb->dev->name);
535e878d78bSSasha Levin 		dev_kfree_skb(skb);
536e878d78bSSasha Levin 		return NULL;
537e878d78bSSasha Levin 	}
5382613af0eSMichael Dalton 	BUG_ON(offset >= PAGE_SIZE);
5399ab86bbcSShirley Ma 	while (len) {
5402613af0eSMichael Dalton 		unsigned int frag_size = min((unsigned)PAGE_SIZE - offset, len);
5412613af0eSMichael Dalton 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, page, offset,
5422613af0eSMichael Dalton 				frag_size, truesize);
5432613af0eSMichael Dalton 		len -= frag_size;
5449ab86bbcSShirley Ma 		page = (struct page *)page->private;
5459ab86bbcSShirley Ma 		offset = 0;
5463f2c31d9SMark McLoughlin 	}
5473f2c31d9SMark McLoughlin 
5489ab86bbcSShirley Ma 	if (page)
549e9d7417bSJason Wang 		give_pages(rq, page);
5503f2c31d9SMark McLoughlin 
551fb32856bSXuan Zhuo ok:
552fb32856bSXuan Zhuo 	/* hdr_valid means no XDP, so we can copy the vnet header */
553fb32856bSXuan Zhuo 	if (hdr_valid) {
554fb32856bSXuan Zhuo 		hdr = skb_vnet_hdr(skb);
555fb32856bSXuan Zhuo 		memcpy(hdr, hdr_p, hdr_len);
556fb32856bSXuan Zhuo 	}
557af39c8f7SEric Dumazet 	if (page_to_free)
558af39c8f7SEric Dumazet 		put_page(page_to_free);
559fb32856bSXuan Zhuo 
560fb32856bSXuan Zhuo 	if (metasize) {
561fb32856bSXuan Zhuo 		__skb_pull(skb, metasize);
562fb32856bSXuan Zhuo 		skb_metadata_set(skb, metasize);
563fb32856bSXuan Zhuo 	}
564fb32856bSXuan Zhuo 
5659ab86bbcSShirley Ma 	return skb;
5669ab86bbcSShirley Ma }
5679ab86bbcSShirley Ma 
568735fc405SJesper Dangaard Brouer static int __virtnet_xdp_xmit_one(struct virtnet_info *vi,
569735fc405SJesper Dangaard Brouer 				   struct send_queue *sq,
57044fa2dbdSJesper Dangaard Brouer 				   struct xdp_frame *xdpf)
57156434a01SJohn Fastabend {
57256434a01SJohn Fastabend 	struct virtio_net_hdr_mrg_rxbuf *hdr;
57356434a01SJohn Fastabend 	int err;
57456434a01SJohn Fastabend 
575cac320c8SJesper Dangaard Brouer 	if (unlikely(xdpf->headroom < vi->hdr_len))
576cac320c8SJesper Dangaard Brouer 		return -EOVERFLOW;
577cac320c8SJesper Dangaard Brouer 
578cac320c8SJesper Dangaard Brouer 	/* Make room for virtqueue hdr (also change xdpf->headroom?) */
579cac320c8SJesper Dangaard Brouer 	xdpf->data -= vi->hdr_len;
58056434a01SJohn Fastabend 	/* Zero header and leave csum up to XDP layers */
581cac320c8SJesper Dangaard Brouer 	hdr = xdpf->data;
58256434a01SJohn Fastabend 	memset(hdr, 0, vi->hdr_len);
583cac320c8SJesper Dangaard Brouer 	xdpf->len   += vi->hdr_len;
58456434a01SJohn Fastabend 
585cac320c8SJesper Dangaard Brouer 	sg_init_one(sq->sg, xdpf->data, xdpf->len);
586bb91accfSJason Wang 
5875050471dSToshiaki Makita 	err = virtqueue_add_outbuf(sq->vq, sq->sg, 1, xdp_to_ptr(xdpf),
5885050471dSToshiaki Makita 				   GFP_ATOMIC);
58911b7d897SJesper Dangaard Brouer 	if (unlikely(err))
590cac320c8SJesper Dangaard Brouer 		return -ENOSPC; /* Caller handle free/refcnt */
59156434a01SJohn Fastabend 
592cac320c8SJesper Dangaard Brouer 	return 0;
59356434a01SJohn Fastabend }
59456434a01SJohn Fastabend 
59597c2c69eSXuan Zhuo /* when vi->curr_queue_pairs > nr_cpu_ids, the txq/sq is only used for xdp tx on
59697c2c69eSXuan Zhuo  * the current cpu, so it does not need to be locked.
59797c2c69eSXuan Zhuo  *
59897c2c69eSXuan Zhuo  * Here we use marco instead of inline functions because we have to deal with
59997c2c69eSXuan Zhuo  * three issues at the same time: 1. the choice of sq. 2. judge and execute the
60097c2c69eSXuan Zhuo  * lock/unlock of txq 3. make sparse happy. It is difficult for two inline
60197c2c69eSXuan Zhuo  * functions to perfectly solve these three problems at the same time.
60297c2c69eSXuan Zhuo  */
60397c2c69eSXuan Zhuo #define virtnet_xdp_get_sq(vi) ({                                       \
6043dcc1edcSLi RongQing 	int cpu = smp_processor_id();                                   \
60597c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
60697c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
60797c2c69eSXuan Zhuo 	unsigned int qp;                                                \
60897c2c69eSXuan Zhuo 									\
60997c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids) {                         \
61097c2c69eSXuan Zhuo 		qp = v->curr_queue_pairs - v->xdp_queue_pairs;          \
6113dcc1edcSLi RongQing 		qp += cpu;                                              \
61297c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
61397c2c69eSXuan Zhuo 		__netif_tx_acquire(txq);                                \
61497c2c69eSXuan Zhuo 	} else {                                                        \
6153dcc1edcSLi RongQing 		qp = cpu % v->curr_queue_pairs;                         \
61697c2c69eSXuan Zhuo 		txq = netdev_get_tx_queue(v->dev, qp);                  \
6173dcc1edcSLi RongQing 		__netif_tx_lock(txq, cpu);                              \
61897c2c69eSXuan Zhuo 	}                                                               \
61997c2c69eSXuan Zhuo 	v->sq + qp;                                                     \
62097c2c69eSXuan Zhuo })
6212a43565cSToshiaki Makita 
62297c2c69eSXuan Zhuo #define virtnet_xdp_put_sq(vi, q) {                                     \
62397c2c69eSXuan Zhuo 	struct netdev_queue *txq;                                       \
62497c2c69eSXuan Zhuo 	typeof(vi) v = (vi);                                            \
62597c2c69eSXuan Zhuo 									\
62697c2c69eSXuan Zhuo 	txq = netdev_get_tx_queue(v->dev, (q) - v->sq);                 \
62797c2c69eSXuan Zhuo 	if (v->curr_queue_pairs > nr_cpu_ids)                           \
62897c2c69eSXuan Zhuo 		__netif_tx_release(txq);                                \
62997c2c69eSXuan Zhuo 	else                                                            \
63097c2c69eSXuan Zhuo 		__netif_tx_unlock(txq);                                 \
6312a43565cSToshiaki Makita }
6322a43565cSToshiaki Makita 
633735fc405SJesper Dangaard Brouer static int virtnet_xdp_xmit(struct net_device *dev,
63442b33468SJesper Dangaard Brouer 			    int n, struct xdp_frame **frames, u32 flags)
635186b3c99SJason Wang {
636186b3c99SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
6378dcc5b0aSJesper Dangaard Brouer 	struct receive_queue *rq = vi->rq;
6388dcc5b0aSJesper Dangaard Brouer 	struct bpf_prog *xdp_prog;
639735fc405SJesper Dangaard Brouer 	struct send_queue *sq;
640735fc405SJesper Dangaard Brouer 	unsigned int len;
641546f2897SToshiaki Makita 	int packets = 0;
642546f2897SToshiaki Makita 	int bytes = 0;
643fdc13979SLorenzo Bianconi 	int nxmit = 0;
644461f03dcSToshiaki Makita 	int kicks = 0;
6455050471dSToshiaki Makita 	void *ptr;
646fdc13979SLorenzo Bianconi 	int ret;
647735fc405SJesper Dangaard Brouer 	int i;
648735fc405SJesper Dangaard Brouer 
6498dcc5b0aSJesper Dangaard Brouer 	/* Only allow ndo_xdp_xmit if XDP is loaded on dev, as this
6508dcc5b0aSJesper Dangaard Brouer 	 * indicate XDP resources have been successfully allocated.
6518dcc5b0aSJesper Dangaard Brouer 	 */
6529719c6b9SJohn Fastabend 	xdp_prog = rcu_access_pointer(rq->xdp_prog);
6531667c08aSToshiaki Makita 	if (!xdp_prog)
6541667c08aSToshiaki Makita 		return -ENXIO;
6551667c08aSToshiaki Makita 
65697c2c69eSXuan Zhuo 	sq = virtnet_xdp_get_sq(vi);
6579ab86bbcSShirley Ma 
6589ab86bbcSShirley Ma 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) {
659186b3c99SJason Wang 		ret = -EINVAL;
660186b3c99SJason Wang 		goto out;
661186b3c99SJason Wang 	}
662186b3c99SJason Wang 
663735fc405SJesper Dangaard Brouer 	/* Free up any pending old buffers before queueing new ones. */
6645050471dSToshiaki Makita 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
665546f2897SToshiaki Makita 		if (likely(is_xdp_frame(ptr))) {
666546f2897SToshiaki Makita 			struct xdp_frame *frame = ptr_to_xdp(ptr);
667546f2897SToshiaki Makita 
668546f2897SToshiaki Makita 			bytes += frame->len;
669546f2897SToshiaki Makita 			xdp_return_frame(frame);
670546f2897SToshiaki Makita 		} else {
671546f2897SToshiaki Makita 			struct sk_buff *skb = ptr;
672546f2897SToshiaki Makita 
673546f2897SToshiaki Makita 			bytes += skb->len;
674546f2897SToshiaki Makita 			napi_consume_skb(skb, false);
675546f2897SToshiaki Makita 		}
676546f2897SToshiaki Makita 		packets++;
6775050471dSToshiaki Makita 	}
678735fc405SJesper Dangaard Brouer 
679735fc405SJesper Dangaard Brouer 	for (i = 0; i < n; i++) {
680735fc405SJesper Dangaard Brouer 		struct xdp_frame *xdpf = frames[i];
681735fc405SJesper Dangaard Brouer 
682fdc13979SLorenzo Bianconi 		if (__virtnet_xdp_xmit_one(vi, sq, xdpf))
683fdc13979SLorenzo Bianconi 			break;
684fdc13979SLorenzo Bianconi 		nxmit++;
685735fc405SJesper Dangaard Brouer 	}
686fdc13979SLorenzo Bianconi 	ret = nxmit;
6875d274cb4SJesper Dangaard Brouer 
688461f03dcSToshiaki Makita 	if (flags & XDP_XMIT_FLUSH) {
689461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq))
690461f03dcSToshiaki Makita 			kicks = 1;
691461f03dcSToshiaki Makita 	}
6925b8f3c8dSToshiaki Makita out:
6935b8f3c8dSToshiaki Makita 	u64_stats_update_begin(&sq->stats.syncp);
694546f2897SToshiaki Makita 	sq->stats.bytes += bytes;
695546f2897SToshiaki Makita 	sq->stats.packets += packets;
6965b8f3c8dSToshiaki Makita 	sq->stats.xdp_tx += n;
697fdc13979SLorenzo Bianconi 	sq->stats.xdp_tx_drops += n - nxmit;
698461f03dcSToshiaki Makita 	sq->stats.kicks += kicks;
6995b8f3c8dSToshiaki Makita 	u64_stats_update_end(&sq->stats.syncp);
7005d274cb4SJesper Dangaard Brouer 
70197c2c69eSXuan Zhuo 	virtnet_xdp_put_sq(vi, sq);
7025b8f3c8dSToshiaki Makita 	return ret;
703186b3c99SJason Wang }
704186b3c99SJason Wang 
705f6b10209SJason Wang static unsigned int virtnet_get_headroom(struct virtnet_info *vi)
706f6b10209SJason Wang {
70797c2c69eSXuan Zhuo 	return vi->xdp_enabled ? VIRTIO_XDP_HEADROOM : 0;
708f6b10209SJason Wang }
709f6b10209SJason Wang 
7104941d472SJason Wang /* We copy the packet for XDP in the following cases:
7114941d472SJason Wang  *
7124941d472SJason Wang  * 1) Packet is scattered across multiple rx buffers.
7134941d472SJason Wang  * 2) Headroom space is insufficient.
7144941d472SJason Wang  *
7154941d472SJason Wang  * This is inefficient but it's a temporary condition that
7164941d472SJason Wang  * we hit right after XDP is enabled and until queue is refilled
7174941d472SJason Wang  * with large buffers with sufficient headroom - so it should affect
7184941d472SJason Wang  * at most queue size packets.
7194941d472SJason Wang  * Afterwards, the conditions to enable
7204941d472SJason Wang  * XDP should preclude the underlying device from sending packets
7214941d472SJason Wang  * across multiple buffers (num_buf > 1), and we make sure buffers
7224941d472SJason Wang  * have enough headroom.
72372979a6cSJohn Fastabend  */
72472979a6cSJohn Fastabend static struct page *xdp_linearize_page(struct receive_queue *rq,
72556a86f84SJason Wang 				       u16 *num_buf,
72672979a6cSJohn Fastabend 				       struct page *p,
72772979a6cSJohn Fastabend 				       int offset,
7284941d472SJason Wang 				       int page_off,
72972979a6cSJohn Fastabend 				       unsigned int *len)
73072979a6cSJohn Fastabend {
73172979a6cSJohn Fastabend 	struct page *page = alloc_page(GFP_ATOMIC);
73272979a6cSJohn Fastabend 
73372979a6cSJohn Fastabend 	if (!page)
73472979a6cSJohn Fastabend 		return NULL;
73572979a6cSJohn Fastabend 
73672979a6cSJohn Fastabend 	memcpy(page_address(page) + page_off, page_address(p) + offset, *len);
73772979a6cSJohn Fastabend 	page_off += *len;
73872979a6cSJohn Fastabend 
73956a86f84SJason Wang 	while (--*num_buf) {
7403cc81a9aSJason Wang 		int tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
74172979a6cSJohn Fastabend 		unsigned int buflen;
74272979a6cSJohn Fastabend 		void *buf;
74372979a6cSJohn Fastabend 		int off;
74472979a6cSJohn Fastabend 
745680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &buflen);
746680557cfSMichael S. Tsirkin 		if (unlikely(!buf))
74772979a6cSJohn Fastabend 			goto err_buf;
74872979a6cSJohn Fastabend 
74972979a6cSJohn Fastabend 		p = virt_to_head_page(buf);
75072979a6cSJohn Fastabend 		off = buf - page_address(p);
75172979a6cSJohn Fastabend 
75256a86f84SJason Wang 		/* guard against a misconfigured or uncooperative backend that
75356a86f84SJason Wang 		 * is sending packet larger than the MTU.
75456a86f84SJason Wang 		 */
7553cc81a9aSJason Wang 		if ((page_off + buflen + tailroom) > PAGE_SIZE) {
75656a86f84SJason Wang 			put_page(p);
75756a86f84SJason Wang 			goto err_buf;
75856a86f84SJason Wang 		}
75956a86f84SJason Wang 
76072979a6cSJohn Fastabend 		memcpy(page_address(page) + page_off,
76172979a6cSJohn Fastabend 		       page_address(p) + off, buflen);
76272979a6cSJohn Fastabend 		page_off += buflen;
76356a86f84SJason Wang 		put_page(p);
76472979a6cSJohn Fastabend 	}
76572979a6cSJohn Fastabend 
7662de2f7f4SJohn Fastabend 	/* Headroom does not contribute to packet length */
7672de2f7f4SJohn Fastabend 	*len = page_off - VIRTIO_XDP_HEADROOM;
76872979a6cSJohn Fastabend 	return page;
76972979a6cSJohn Fastabend err_buf:
77072979a6cSJohn Fastabend 	__free_pages(page, 0);
77172979a6cSJohn Fastabend 	return NULL;
77272979a6cSJohn Fastabend }
77372979a6cSJohn Fastabend 
7744941d472SJason Wang static struct sk_buff *receive_small(struct net_device *dev,
7754941d472SJason Wang 				     struct virtnet_info *vi,
7764941d472SJason Wang 				     struct receive_queue *rq,
7774941d472SJason Wang 				     void *buf, void *ctx,
778186b3c99SJason Wang 				     unsigned int len,
7797d9d60fdSToshiaki Makita 				     unsigned int *xdp_xmit,
780d46eeeafSJason Wang 				     struct virtnet_rq_stats *stats)
7814941d472SJason Wang {
7824941d472SJason Wang 	struct sk_buff *skb;
7834941d472SJason Wang 	struct bpf_prog *xdp_prog;
7844941d472SJason Wang 	unsigned int xdp_headroom = (unsigned long)ctx;
7854941d472SJason Wang 	unsigned int header_offset = VIRTNET_RX_PAD + xdp_headroom;
7864941d472SJason Wang 	unsigned int headroom = vi->hdr_len + header_offset;
7874941d472SJason Wang 	unsigned int buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
7884941d472SJason Wang 			      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
7894941d472SJason Wang 	struct page *page = virt_to_head_page(buf);
79011b7d897SJesper Dangaard Brouer 	unsigned int delta = 0;
7914941d472SJason Wang 	struct page *xdp_page;
79211b7d897SJesper Dangaard Brouer 	int err;
793503d539aSYuya Kusakabe 	unsigned int metasize = 0;
79411b7d897SJesper Dangaard Brouer 
7954941d472SJason Wang 	len -= vi->hdr_len;
796d46eeeafSJason Wang 	stats->bytes += len;
7974941d472SJason Wang 
798ad993a95SXie Yongji 	if (unlikely(len > GOOD_PACKET_LEN)) {
799ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds max size %d\n",
800ad993a95SXie Yongji 			 dev->name, len, GOOD_PACKET_LEN);
801ad993a95SXie Yongji 		dev->stats.rx_length_errors++;
802053c9e18SWenliang Wang 		goto err;
803ad993a95SXie Yongji 	}
8046213f07cSLi RongQing 
8056213f07cSLi RongQing 	if (likely(!vi->xdp_enabled)) {
8066213f07cSLi RongQing 		xdp_prog = NULL;
8076213f07cSLi RongQing 		goto skip_xdp;
8086213f07cSLi RongQing 	}
8096213f07cSLi RongQing 
8104941d472SJason Wang 	rcu_read_lock();
8114941d472SJason Wang 	xdp_prog = rcu_dereference(rq->xdp_prog);
8124941d472SJason Wang 	if (xdp_prog) {
8134941d472SJason Wang 		struct virtio_net_hdr_mrg_rxbuf *hdr = buf + header_offset;
81444fa2dbdSJesper Dangaard Brouer 		struct xdp_frame *xdpf;
8154941d472SJason Wang 		struct xdp_buff xdp;
8164941d472SJason Wang 		void *orig_data;
8174941d472SJason Wang 		u32 act;
8184941d472SJason Wang 
81995dbe9e7SJesper Dangaard Brouer 		if (unlikely(hdr->hdr.gso_type))
8204941d472SJason Wang 			goto err_xdp;
8214941d472SJason Wang 
8224941d472SJason Wang 		if (unlikely(xdp_headroom < virtnet_get_headroom(vi))) {
8234941d472SJason Wang 			int offset = buf - page_address(page) + header_offset;
8244941d472SJason Wang 			unsigned int tlen = len + vi->hdr_len;
8254941d472SJason Wang 			u16 num_buf = 1;
8264941d472SJason Wang 
8274941d472SJason Wang 			xdp_headroom = virtnet_get_headroom(vi);
8284941d472SJason Wang 			header_offset = VIRTNET_RX_PAD + xdp_headroom;
8294941d472SJason Wang 			headroom = vi->hdr_len + header_offset;
8304941d472SJason Wang 			buflen = SKB_DATA_ALIGN(GOOD_PACKET_LEN + headroom) +
8314941d472SJason Wang 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
8324941d472SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf, page,
8334941d472SJason Wang 						      offset, header_offset,
8344941d472SJason Wang 						      &tlen);
8354941d472SJason Wang 			if (!xdp_page)
8364941d472SJason Wang 				goto err_xdp;
8374941d472SJason Wang 
8384941d472SJason Wang 			buf = page_address(xdp_page);
8394941d472SJason Wang 			put_page(page);
8404941d472SJason Wang 			page = xdp_page;
8414941d472SJason Wang 		}
8424941d472SJason Wang 
84343b5169dSLorenzo Bianconi 		xdp_init_buff(&xdp, buflen, &rq->xdp_rxq);
844be9df4afSLorenzo Bianconi 		xdp_prepare_buff(&xdp, buf + VIRTNET_RX_PAD + vi->hdr_len,
845be9df4afSLorenzo Bianconi 				 xdp_headroom, len, true);
8464941d472SJason Wang 		orig_data = xdp.data;
8474941d472SJason Wang 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
848d46eeeafSJason Wang 		stats->xdp_packets++;
8494941d472SJason Wang 
8504941d472SJason Wang 		switch (act) {
8514941d472SJason Wang 		case XDP_PASS:
8524941d472SJason Wang 			/* Recalculate length in case bpf program changed it */
8534941d472SJason Wang 			delta = orig_data - xdp.data;
8546870de43SNikita V. Shirokov 			len = xdp.data_end - xdp.data;
855503d539aSYuya Kusakabe 			metasize = xdp.data - xdp.data_meta;
8564941d472SJason Wang 			break;
8574941d472SJason Wang 		case XDP_TX:
858d46eeeafSJason Wang 			stats->xdp_tx++;
8591b698fa5SLorenzo Bianconi 			xdpf = xdp_convert_buff_to_frame(&xdp);
86044fa2dbdSJesper Dangaard Brouer 			if (unlikely(!xdpf))
86144fa2dbdSJesper Dangaard Brouer 				goto err_xdp;
862ca9e83b4SJason Wang 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
863fdc13979SLorenzo Bianconi 			if (unlikely(!err)) {
864fdc13979SLorenzo Bianconi 				xdp_return_frame_rx_napi(xdpf);
865fdc13979SLorenzo Bianconi 			} else if (unlikely(err < 0)) {
8664941d472SJason Wang 				trace_xdp_exception(vi->dev, xdp_prog, act);
86711b7d897SJesper Dangaard Brouer 				goto err_xdp;
86811b7d897SJesper Dangaard Brouer 			}
8692471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_TX;
870186b3c99SJason Wang 			rcu_read_unlock();
871186b3c99SJason Wang 			goto xdp_xmit;
872186b3c99SJason Wang 		case XDP_REDIRECT:
873d46eeeafSJason Wang 			stats->xdp_redirects++;
874186b3c99SJason Wang 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
87511b7d897SJesper Dangaard Brouer 			if (err)
87611b7d897SJesper Dangaard Brouer 				goto err_xdp;
8772471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_REDIR;
8784941d472SJason Wang 			rcu_read_unlock();
8794941d472SJason Wang 			goto xdp_xmit;
8804941d472SJason Wang 		default:
881c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
882df561f66SGustavo A. R. Silva 			fallthrough;
8834941d472SJason Wang 		case XDP_ABORTED:
8844941d472SJason Wang 			trace_xdp_exception(vi->dev, xdp_prog, act);
88595efabf0SGustavo A. R. Silva 			goto err_xdp;
8864941d472SJason Wang 		case XDP_DROP:
8874941d472SJason Wang 			goto err_xdp;
8884941d472SJason Wang 		}
8894941d472SJason Wang 	}
8904941d472SJason Wang 	rcu_read_unlock();
8914941d472SJason Wang 
8926213f07cSLi RongQing skip_xdp:
8934941d472SJason Wang 	skb = build_skb(buf, buflen);
894053c9e18SWenliang Wang 	if (!skb)
8954941d472SJason Wang 		goto err;
8964941d472SJason Wang 	skb_reserve(skb, headroom - delta);
8976870de43SNikita V. Shirokov 	skb_put(skb, len);
898f1d4884dSYuya Kusakabe 	if (!xdp_prog) {
8994941d472SJason Wang 		buf += header_offset;
9004941d472SJason Wang 		memcpy(skb_vnet_hdr(skb), buf, vi->hdr_len);
901f1d4884dSYuya Kusakabe 	} /* keep zeroed vnet hdr since XDP is loaded */
9024941d472SJason Wang 
903503d539aSYuya Kusakabe 	if (metasize)
904503d539aSYuya Kusakabe 		skb_metadata_set(skb, metasize);
905503d539aSYuya Kusakabe 
9064941d472SJason Wang 	return skb;
9074941d472SJason Wang 
9084941d472SJason Wang err_xdp:
9094941d472SJason Wang 	rcu_read_unlock();
910d46eeeafSJason Wang 	stats->xdp_drops++;
911053c9e18SWenliang Wang err:
912d46eeeafSJason Wang 	stats->drops++;
9134941d472SJason Wang 	put_page(page);
9144941d472SJason Wang xdp_xmit:
9154941d472SJason Wang 	return NULL;
9164941d472SJason Wang }
9174941d472SJason Wang 
9184941d472SJason Wang static struct sk_buff *receive_big(struct net_device *dev,
9194941d472SJason Wang 				   struct virtnet_info *vi,
9204941d472SJason Wang 				   struct receive_queue *rq,
9214941d472SJason Wang 				   void *buf,
9227d9d60fdSToshiaki Makita 				   unsigned int len,
923d46eeeafSJason Wang 				   struct virtnet_rq_stats *stats)
9244941d472SJason Wang {
9254941d472SJason Wang 	struct page *page = buf;
926503d539aSYuya Kusakabe 	struct sk_buff *skb =
927fb32856bSXuan Zhuo 		page_to_skb(vi, rq, page, 0, len, PAGE_SIZE, true, 0, 0);
9284941d472SJason Wang 
929d46eeeafSJason Wang 	stats->bytes += len - vi->hdr_len;
9304941d472SJason Wang 	if (unlikely(!skb))
9314941d472SJason Wang 		goto err;
9324941d472SJason Wang 
9334941d472SJason Wang 	return skb;
9344941d472SJason Wang 
9354941d472SJason Wang err:
936d46eeeafSJason Wang 	stats->drops++;
9374941d472SJason Wang 	give_pages(rq, page);
9384941d472SJason Wang 	return NULL;
9394941d472SJason Wang }
9404941d472SJason Wang 
9418fc3b9e9SMichael S. Tsirkin static struct sk_buff *receive_mergeable(struct net_device *dev,
942fdd819b2SMichael S. Tsirkin 					 struct virtnet_info *vi,
9438fc3b9e9SMichael S. Tsirkin 					 struct receive_queue *rq,
944680557cfSMichael S. Tsirkin 					 void *buf,
945680557cfSMichael S. Tsirkin 					 void *ctx,
946186b3c99SJason Wang 					 unsigned int len,
9477d9d60fdSToshiaki Makita 					 unsigned int *xdp_xmit,
948d46eeeafSJason Wang 					 struct virtnet_rq_stats *stats)
9499ab86bbcSShirley Ma {
950012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr = buf;
951012873d0SMichael S. Tsirkin 	u16 num_buf = virtio16_to_cpu(vi->vdev, hdr->num_buffers);
9528fc3b9e9SMichael S. Tsirkin 	struct page *page = virt_to_head_page(buf);
9538fc3b9e9SMichael S. Tsirkin 	int offset = buf - page_address(page);
954f600b690SJohn Fastabend 	struct sk_buff *head_skb, *curr_skb;
955f600b690SJohn Fastabend 	struct bpf_prog *xdp_prog;
9569ce6146eSJesper Dangaard Brouer 	unsigned int truesize = mergeable_ctx_to_truesize(ctx);
9574941d472SJason Wang 	unsigned int headroom = mergeable_ctx_to_headroom(ctx);
958503d539aSYuya Kusakabe 	unsigned int metasize = 0;
9599ce6146eSJesper Dangaard Brouer 	unsigned int frame_sz;
9609ce6146eSJesper Dangaard Brouer 	int err;
961ab7db917SMichael Dalton 
96256434a01SJohn Fastabend 	head_skb = NULL;
963d46eeeafSJason Wang 	stats->bytes += len - vi->hdr_len;
96456434a01SJohn Fastabend 
965ad993a95SXie Yongji 	if (unlikely(len > truesize)) {
966ad993a95SXie Yongji 		pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
967ad993a95SXie Yongji 			 dev->name, len, (unsigned long)ctx);
968ad993a95SXie Yongji 		dev->stats.rx_length_errors++;
969ad993a95SXie Yongji 		goto err_skb;
970ad993a95SXie Yongji 	}
9716213f07cSLi RongQing 
9726213f07cSLi RongQing 	if (likely(!vi->xdp_enabled)) {
9736213f07cSLi RongQing 		xdp_prog = NULL;
9746213f07cSLi RongQing 		goto skip_xdp;
9756213f07cSLi RongQing 	}
9766213f07cSLi RongQing 
977f600b690SJohn Fastabend 	rcu_read_lock();
978f600b690SJohn Fastabend 	xdp_prog = rcu_dereference(rq->xdp_prog);
979f600b690SJohn Fastabend 	if (xdp_prog) {
98044fa2dbdSJesper Dangaard Brouer 		struct xdp_frame *xdpf;
98172979a6cSJohn Fastabend 		struct page *xdp_page;
9820354e4d1SJohn Fastabend 		struct xdp_buff xdp;
9830354e4d1SJohn Fastabend 		void *data;
984f600b690SJohn Fastabend 		u32 act;
985f600b690SJohn Fastabend 
9863d62b2a0SJason Wang 		/* Transient failure which in theory could occur if
9873d62b2a0SJason Wang 		 * in-flight packets from before XDP was enabled reach
9883d62b2a0SJason Wang 		 * the receive path after XDP is loaded.
9893d62b2a0SJason Wang 		 */
9903d62b2a0SJason Wang 		if (unlikely(hdr->hdr.gso_type))
9913d62b2a0SJason Wang 			goto err_xdp;
9923d62b2a0SJason Wang 
9939ce6146eSJesper Dangaard Brouer 		/* Buffers with headroom use PAGE_SIZE as alloc size,
9949ce6146eSJesper Dangaard Brouer 		 * see add_recvbuf_mergeable() + get_mergeable_buf_len()
9959ce6146eSJesper Dangaard Brouer 		 */
9969ce6146eSJesper Dangaard Brouer 		frame_sz = headroom ? PAGE_SIZE : truesize;
9979ce6146eSJesper Dangaard Brouer 
9983cc81a9aSJason Wang 		/* This happens when rx buffer size is underestimated
9993cc81a9aSJason Wang 		 * or headroom is not enough because of the buffer
10003cc81a9aSJason Wang 		 * was refilled before XDP is set. This should only
10013cc81a9aSJason Wang 		 * happen for the first several packets, so we don't
10023cc81a9aSJason Wang 		 * care much about its performance.
10033cc81a9aSJason Wang 		 */
10044941d472SJason Wang 		if (unlikely(num_buf > 1 ||
10054941d472SJason Wang 			     headroom < virtnet_get_headroom(vi))) {
100672979a6cSJohn Fastabend 			/* linearize data for XDP */
100756a86f84SJason Wang 			xdp_page = xdp_linearize_page(rq, &num_buf,
10084941d472SJason Wang 						      page, offset,
10094941d472SJason Wang 						      VIRTIO_XDP_HEADROOM,
10104941d472SJason Wang 						      &len);
10119ce6146eSJesper Dangaard Brouer 			frame_sz = PAGE_SIZE;
10129ce6146eSJesper Dangaard Brouer 
101372979a6cSJohn Fastabend 			if (!xdp_page)
1014f600b690SJohn Fastabend 				goto err_xdp;
10152de2f7f4SJohn Fastabend 			offset = VIRTIO_XDP_HEADROOM;
101672979a6cSJohn Fastabend 		} else {
101772979a6cSJohn Fastabend 			xdp_page = page;
1018f600b690SJohn Fastabend 		}
1019f600b690SJohn Fastabend 
10202de2f7f4SJohn Fastabend 		/* Allow consuming headroom but reserve enough space to push
10212de2f7f4SJohn Fastabend 		 * the descriptor on if we get an XDP_TX return code.
10222de2f7f4SJohn Fastabend 		 */
10230354e4d1SJohn Fastabend 		data = page_address(xdp_page) + offset;
102443b5169dSLorenzo Bianconi 		xdp_init_buff(&xdp, frame_sz - vi->hdr_len, &rq->xdp_rxq);
1025be9df4afSLorenzo Bianconi 		xdp_prepare_buff(&xdp, data - VIRTIO_XDP_HEADROOM + vi->hdr_len,
1026be9df4afSLorenzo Bianconi 				 VIRTIO_XDP_HEADROOM, len - vi->hdr_len, true);
1027754b8a21SJesper Dangaard Brouer 
10280354e4d1SJohn Fastabend 		act = bpf_prog_run_xdp(xdp_prog, &xdp);
1029d46eeeafSJason Wang 		stats->xdp_packets++;
10300354e4d1SJohn Fastabend 
103156434a01SJohn Fastabend 		switch (act) {
103256434a01SJohn Fastabend 		case XDP_PASS:
1033503d539aSYuya Kusakabe 			metasize = xdp.data - xdp.data_meta;
10342de2f7f4SJohn Fastabend 
1035503d539aSYuya Kusakabe 			/* recalculate offset to account for any header
1036503d539aSYuya Kusakabe 			 * adjustments and minus the metasize to copy the
1037503d539aSYuya Kusakabe 			 * metadata in page_to_skb(). Note other cases do not
1038503d539aSYuya Kusakabe 			 * build an skb and avoid using offset
10396870de43SNikita V. Shirokov 			 */
1040503d539aSYuya Kusakabe 			offset = xdp.data - page_address(xdp_page) -
1041503d539aSYuya Kusakabe 				 vi->hdr_len - metasize;
1042503d539aSYuya Kusakabe 
1043503d539aSYuya Kusakabe 			/* recalculate len if xdp.data, xdp.data_end or
1044503d539aSYuya Kusakabe 			 * xdp.data_meta were adjusted
1045503d539aSYuya Kusakabe 			 */
1046503d539aSYuya Kusakabe 			len = xdp.data_end - xdp.data + vi->hdr_len + metasize;
1047acb16b39SNikolay Aleksandrov 
1048acb16b39SNikolay Aleksandrov 			/* recalculate headroom if xdp.data or xdp_data_meta
1049acb16b39SNikolay Aleksandrov 			 * were adjusted, note that offset should always point
1050acb16b39SNikolay Aleksandrov 			 * to the start of the reserved bytes for virtio_net
1051acb16b39SNikolay Aleksandrov 			 * header which are followed by xdp.data, that means
1052acb16b39SNikolay Aleksandrov 			 * that offset is equal to the headroom (when buf is
1053acb16b39SNikolay Aleksandrov 			 * starting at the beginning of the page, otherwise
1054acb16b39SNikolay Aleksandrov 			 * there is a base offset inside the page) but it's used
1055acb16b39SNikolay Aleksandrov 			 * with a different starting point (buf start) than
1056acb16b39SNikolay Aleksandrov 			 * xdp.data (buf start + vnet hdr size). If xdp.data or
1057acb16b39SNikolay Aleksandrov 			 * data_meta were adjusted by the xdp prog then the
1058acb16b39SNikolay Aleksandrov 			 * headroom size has changed and so has the offset, we
1059acb16b39SNikolay Aleksandrov 			 * can use data_hard_start, which points at buf start +
1060acb16b39SNikolay Aleksandrov 			 * vnet hdr size, to calculate the new headroom and use
1061acb16b39SNikolay Aleksandrov 			 * it later to compute buf start in page_to_skb()
1062acb16b39SNikolay Aleksandrov 			 */
1063acb16b39SNikolay Aleksandrov 			headroom = xdp.data - xdp.data_hard_start - metasize;
1064acb16b39SNikolay Aleksandrov 
10651830f893SJason Wang 			/* We can only create skb based on xdp_page. */
10661830f893SJason Wang 			if (unlikely(xdp_page != page)) {
10671830f893SJason Wang 				rcu_read_unlock();
10681830f893SJason Wang 				put_page(page);
1069503d539aSYuya Kusakabe 				head_skb = page_to_skb(vi, rq, xdp_page, offset,
1070503d539aSYuya Kusakabe 						       len, PAGE_SIZE, false,
1071c32325b8SJakub Kicinski 						       metasize,
1072acb16b39SNikolay Aleksandrov 						       headroom);
10731830f893SJason Wang 				return head_skb;
10741830f893SJason Wang 			}
107556434a01SJohn Fastabend 			break;
107656434a01SJohn Fastabend 		case XDP_TX:
1077d46eeeafSJason Wang 			stats->xdp_tx++;
10781b698fa5SLorenzo Bianconi 			xdpf = xdp_convert_buff_to_frame(&xdp);
10797a542beeSXuan Zhuo 			if (unlikely(!xdpf)) {
10807a542beeSXuan Zhuo 				if (unlikely(xdp_page != page))
10817a542beeSXuan Zhuo 					put_page(xdp_page);
108244fa2dbdSJesper Dangaard Brouer 				goto err_xdp;
10837a542beeSXuan Zhuo 			}
1084ca9e83b4SJason Wang 			err = virtnet_xdp_xmit(dev, 1, &xdpf, 0);
1085fdc13979SLorenzo Bianconi 			if (unlikely(!err)) {
1086fdc13979SLorenzo Bianconi 				xdp_return_frame_rx_napi(xdpf);
1087fdc13979SLorenzo Bianconi 			} else if (unlikely(err < 0)) {
10880354e4d1SJohn Fastabend 				trace_xdp_exception(vi->dev, xdp_prog, act);
108911b7d897SJesper Dangaard Brouer 				if (unlikely(xdp_page != page))
109011b7d897SJesper Dangaard Brouer 					put_page(xdp_page);
109111b7d897SJesper Dangaard Brouer 				goto err_xdp;
109211b7d897SJesper Dangaard Brouer 			}
10932471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_TX;
109472979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
10955d458a13SJason Wang 				put_page(page);
109656434a01SJohn Fastabend 			rcu_read_unlock();
109756434a01SJohn Fastabend 			goto xdp_xmit;
10983cc81a9aSJason Wang 		case XDP_REDIRECT:
1099d46eeeafSJason Wang 			stats->xdp_redirects++;
11003cc81a9aSJason Wang 			err = xdp_do_redirect(dev, &xdp, xdp_prog);
11013cc81a9aSJason Wang 			if (err) {
11023cc81a9aSJason Wang 				if (unlikely(xdp_page != page))
11033cc81a9aSJason Wang 					put_page(xdp_page);
11043cc81a9aSJason Wang 				goto err_xdp;
11053cc81a9aSJason Wang 			}
11062471c75eSJesper Dangaard Brouer 			*xdp_xmit |= VIRTIO_XDP_REDIR;
11073cc81a9aSJason Wang 			if (unlikely(xdp_page != page))
11086890418bSJason Wang 				put_page(page);
11093cc81a9aSJason Wang 			rcu_read_unlock();
11103cc81a9aSJason Wang 			goto xdp_xmit;
111156434a01SJohn Fastabend 		default:
1112c8064e5bSPaolo Abeni 			bpf_warn_invalid_xdp_action(vi->dev, xdp_prog, act);
1113df561f66SGustavo A. R. Silva 			fallthrough;
11140354e4d1SJohn Fastabend 		case XDP_ABORTED:
11150354e4d1SJohn Fastabend 			trace_xdp_exception(vi->dev, xdp_prog, act);
1116df561f66SGustavo A. R. Silva 			fallthrough;
11170354e4d1SJohn Fastabend 		case XDP_DROP:
111872979a6cSJohn Fastabend 			if (unlikely(xdp_page != page))
111972979a6cSJohn Fastabend 				__free_pages(xdp_page, 0);
1120f600b690SJohn Fastabend 			goto err_xdp;
1121f600b690SJohn Fastabend 		}
112256434a01SJohn Fastabend 	}
1123f600b690SJohn Fastabend 	rcu_read_unlock();
1124f600b690SJohn Fastabend 
11256213f07cSLi RongQing skip_xdp:
1126503d539aSYuya Kusakabe 	head_skb = page_to_skb(vi, rq, page, offset, len, truesize, !xdp_prog,
1127c32325b8SJakub Kicinski 			       metasize, headroom);
1128f600b690SJohn Fastabend 	curr_skb = head_skb;
11299ab86bbcSShirley Ma 
11308fc3b9e9SMichael S. Tsirkin 	if (unlikely(!curr_skb))
11318fc3b9e9SMichael S. Tsirkin 		goto err_skb;
11329ab86bbcSShirley Ma 	while (--num_buf) {
11338fc3b9e9SMichael S. Tsirkin 		int num_skb_frags;
11348fc3b9e9SMichael S. Tsirkin 
1135680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx);
113603e9f8a0SYunjian Wang 		if (unlikely(!buf)) {
11378fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers out of %d missing\n",
1138fdd819b2SMichael S. Tsirkin 				 dev->name, num_buf,
1139012873d0SMichael S. Tsirkin 				 virtio16_to_cpu(vi->vdev,
1140012873d0SMichael S. Tsirkin 						 hdr->num_buffers));
11418fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
11428fc3b9e9SMichael S. Tsirkin 			goto err_buf;
11433f2c31d9SMark McLoughlin 		}
11448fc3b9e9SMichael S. Tsirkin 
1145d46eeeafSJason Wang 		stats->bytes += len;
11468fc3b9e9SMichael S. Tsirkin 		page = virt_to_head_page(buf);
114728b39bc7SJason Wang 
114828b39bc7SJason Wang 		truesize = mergeable_ctx_to_truesize(ctx);
114928b39bc7SJason Wang 		if (unlikely(len > truesize)) {
115056da5fd0SDan Carpenter 			pr_debug("%s: rx error: len %u exceeds truesize %lu\n",
1151680557cfSMichael S. Tsirkin 				 dev->name, len, (unsigned long)ctx);
1152680557cfSMichael S. Tsirkin 			dev->stats.rx_length_errors++;
1153680557cfSMichael S. Tsirkin 			goto err_skb;
1154680557cfSMichael S. Tsirkin 		}
11558fc3b9e9SMichael S. Tsirkin 
11568fc3b9e9SMichael S. Tsirkin 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
11572613af0eSMichael Dalton 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
11582613af0eSMichael Dalton 			struct sk_buff *nskb = alloc_skb(0, GFP_ATOMIC);
11598fc3b9e9SMichael S. Tsirkin 
11608fc3b9e9SMichael S. Tsirkin 			if (unlikely(!nskb))
11618fc3b9e9SMichael S. Tsirkin 				goto err_skb;
11622613af0eSMichael Dalton 			if (curr_skb == head_skb)
11632613af0eSMichael Dalton 				skb_shinfo(curr_skb)->frag_list = nskb;
11642613af0eSMichael Dalton 			else
11652613af0eSMichael Dalton 				curr_skb->next = nskb;
11662613af0eSMichael Dalton 			curr_skb = nskb;
11672613af0eSMichael Dalton 			head_skb->truesize += nskb->truesize;
11682613af0eSMichael Dalton 			num_skb_frags = 0;
11692613af0eSMichael Dalton 		}
11702613af0eSMichael Dalton 		if (curr_skb != head_skb) {
11712613af0eSMichael Dalton 			head_skb->data_len += len;
11722613af0eSMichael Dalton 			head_skb->len += len;
1173fb51879dSMichael Dalton 			head_skb->truesize += truesize;
11742613af0eSMichael Dalton 		}
11758fc3b9e9SMichael S. Tsirkin 		offset = buf - page_address(page);
1176ba275241SJason Wang 		if (skb_can_coalesce(curr_skb, num_skb_frags, page, offset)) {
1177ba275241SJason Wang 			put_page(page);
1178ba275241SJason Wang 			skb_coalesce_rx_frag(curr_skb, num_skb_frags - 1,
1179fb51879dSMichael Dalton 					     len, truesize);
1180ba275241SJason Wang 		} else {
11812613af0eSMichael Dalton 			skb_add_rx_frag(curr_skb, num_skb_frags, page,
1182fb51879dSMichael Dalton 					offset, len, truesize);
1183ba275241SJason Wang 		}
11848fc3b9e9SMichael S. Tsirkin 	}
11858fc3b9e9SMichael S. Tsirkin 
11865377d758SJohannes Berg 	ewma_pkt_len_add(&rq->mrg_avg_pkt_len, head_skb->len);
11878fc3b9e9SMichael S. Tsirkin 	return head_skb;
11888fc3b9e9SMichael S. Tsirkin 
1189f600b690SJohn Fastabend err_xdp:
1190f600b690SJohn Fastabend 	rcu_read_unlock();
1191d46eeeafSJason Wang 	stats->xdp_drops++;
11928fc3b9e9SMichael S. Tsirkin err_skb:
11938fc3b9e9SMichael S. Tsirkin 	put_page(page);
1194850e088dSJason Wang 	while (num_buf-- > 1) {
1195680557cfSMichael S. Tsirkin 		buf = virtqueue_get_buf(rq->vq, &len);
1196680557cfSMichael S. Tsirkin 		if (unlikely(!buf)) {
11978fc3b9e9SMichael S. Tsirkin 			pr_debug("%s: rx error: %d buffers missing\n",
11988fc3b9e9SMichael S. Tsirkin 				 dev->name, num_buf);
11998fc3b9e9SMichael S. Tsirkin 			dev->stats.rx_length_errors++;
12008fc3b9e9SMichael S. Tsirkin 			break;
12018fc3b9e9SMichael S. Tsirkin 		}
1202d46eeeafSJason Wang 		stats->bytes += len;
1203680557cfSMichael S. Tsirkin 		page = virt_to_head_page(buf);
12048fc3b9e9SMichael S. Tsirkin 		put_page(page);
12053f2c31d9SMark McLoughlin 	}
12068fc3b9e9SMichael S. Tsirkin err_buf:
1207d46eeeafSJason Wang 	stats->drops++;
12088fc3b9e9SMichael S. Tsirkin 	dev_kfree_skb(head_skb);
120956434a01SJohn Fastabend xdp_xmit:
12108fc3b9e9SMichael S. Tsirkin 	return NULL;
12119ab86bbcSShirley Ma }
12129ab86bbcSShirley Ma 
121391f41f01SAndrew Melnychenko static void virtio_skb_set_hash(const struct virtio_net_hdr_v1_hash *hdr_hash,
121491f41f01SAndrew Melnychenko 				struct sk_buff *skb)
121591f41f01SAndrew Melnychenko {
121691f41f01SAndrew Melnychenko 	enum pkt_hash_types rss_hash_type;
121791f41f01SAndrew Melnychenko 
121891f41f01SAndrew Melnychenko 	if (!hdr_hash || !skb)
121991f41f01SAndrew Melnychenko 		return;
122091f41f01SAndrew Melnychenko 
122195bb6330SMichael S. Tsirkin 	switch (__le16_to_cpu(hdr_hash->hash_report)) {
122291f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv4:
122391f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv4:
122491f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv6:
122591f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv6:
122691f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_TCPv6_EX:
122791f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_UDPv6_EX:
122891f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_L4;
122991f41f01SAndrew Melnychenko 		break;
123091f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv4:
123191f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv6:
123291f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_IPv6_EX:
123391f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_L3;
123491f41f01SAndrew Melnychenko 		break;
123591f41f01SAndrew Melnychenko 	case VIRTIO_NET_HASH_REPORT_NONE:
123691f41f01SAndrew Melnychenko 	default:
123791f41f01SAndrew Melnychenko 		rss_hash_type = PKT_HASH_TYPE_NONE;
123891f41f01SAndrew Melnychenko 	}
123995bb6330SMichael S. Tsirkin 	skb_set_hash(skb, __le32_to_cpu(hdr_hash->hash_value), rss_hash_type);
124091f41f01SAndrew Melnychenko }
124191f41f01SAndrew Melnychenko 
12427d9d60fdSToshiaki Makita static void receive_buf(struct virtnet_info *vi, struct receive_queue *rq,
12432471c75eSJesper Dangaard Brouer 			void *buf, unsigned int len, void **ctx,
1244a0929a44SToshiaki Makita 			unsigned int *xdp_xmit,
1245d46eeeafSJason Wang 			struct virtnet_rq_stats *stats)
12469ab86bbcSShirley Ma {
1247e9d7417bSJason Wang 	struct net_device *dev = vi->dev;
12489ab86bbcSShirley Ma 	struct sk_buff *skb;
1249012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
12509ab86bbcSShirley Ma 
1251bcff3162SMichael S. Tsirkin 	if (unlikely(len < vi->hdr_len + ETH_HLEN)) {
12529ab86bbcSShirley Ma 		pr_debug("%s: short packet %i\n", dev->name, len);
12539ab86bbcSShirley Ma 		dev->stats.rx_length_errors++;
1254ab7db917SMichael Dalton 		if (vi->mergeable_rx_bufs) {
1255680557cfSMichael S. Tsirkin 			put_page(virt_to_head_page(buf));
1256ab7db917SMichael Dalton 		} else if (vi->big_packets) {
125798bfd23cSMichael Dalton 			give_pages(rq, buf);
1258ab7db917SMichael Dalton 		} else {
1259f6b10209SJason Wang 			put_page(virt_to_head_page(buf));
1260ab7db917SMichael Dalton 		}
12617d9d60fdSToshiaki Makita 		return;
12629ab86bbcSShirley Ma 	}
12639ab86bbcSShirley Ma 
1264f121159dSMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
12657d9d60fdSToshiaki Makita 		skb = receive_mergeable(dev, vi, rq, buf, ctx, len, xdp_xmit,
1266a0929a44SToshiaki Makita 					stats);
1267f121159dSMichael S. Tsirkin 	else if (vi->big_packets)
1268a0929a44SToshiaki Makita 		skb = receive_big(dev, vi, rq, buf, len, stats);
1269f121159dSMichael S. Tsirkin 	else
1270a0929a44SToshiaki Makita 		skb = receive_small(dev, vi, rq, buf, ctx, len, xdp_xmit, stats);
1271f121159dSMichael S. Tsirkin 
12728fc3b9e9SMichael S. Tsirkin 	if (unlikely(!skb))
12737d9d60fdSToshiaki Makita 		return;
12743f2c31d9SMark McLoughlin 
12759ab86bbcSShirley Ma 	hdr = skb_vnet_hdr(skb);
127691f41f01SAndrew Melnychenko 	if (dev->features & NETIF_F_RXHASH && vi->has_rss_hash_report)
127791f41f01SAndrew Melnychenko 		virtio_skb_set_hash((const struct virtio_net_hdr_v1_hash *)hdr, skb);
12783fa2a1dfSstephen hemminger 
1279e858fae2SMike Rapoport 	if (hdr->hdr.flags & VIRTIO_NET_HDR_F_DATA_VALID)
128010a8d94aSJason Wang 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1281296f96fcSRusty Russell 
1282e858fae2SMike Rapoport 	if (virtio_net_hdr_to_skb(skb, &hdr->hdr,
1283e858fae2SMike Rapoport 				  virtio_is_little_endian(vi->vdev))) {
1284e858fae2SMike Rapoport 		net_warn_ratelimited("%s: bad gso: type: %u, size: %u\n",
1285e858fae2SMike Rapoport 				     dev->name, hdr->hdr.gso_type,
1286fdd819b2SMichael S. Tsirkin 				     hdr->hdr.gso_size);
1287296f96fcSRusty Russell 		goto frame_err;
1288296f96fcSRusty Russell 	}
1289296f96fcSRusty Russell 
1290133bbb18SWillem de Bruijn 	skb_record_rx_queue(skb, vq2rxq(rq->vq));
1291d1dc06dcSMike Rapoport 	skb->protocol = eth_type_trans(skb, dev);
1292d1dc06dcSMike Rapoport 	pr_debug("Receiving skb proto 0x%04x len %i type %i\n",
1293d1dc06dcSMike Rapoport 		 ntohs(skb->protocol), skb->len, skb->pkt_type);
1294d1dc06dcSMike Rapoport 
12950fbd050aSEric Dumazet 	napi_gro_receive(&rq->napi, skb);
12967d9d60fdSToshiaki Makita 	return;
1297296f96fcSRusty Russell 
1298296f96fcSRusty Russell frame_err:
1299296f96fcSRusty Russell 	dev->stats.rx_frame_errors++;
1300296f96fcSRusty Russell 	dev_kfree_skb(skb);
1301296f96fcSRusty Russell }
1302296f96fcSRusty Russell 
1303192f68cfSJason Wang /* Unlike mergeable buffers, all buffers are allocated to the
1304192f68cfSJason Wang  * same size, except for the headroom. For this reason we do
1305192f68cfSJason Wang  * not need to use  mergeable_len_to_ctx here - it is enough
1306192f68cfSJason Wang  * to store the headroom as the context ignoring the truesize.
1307192f68cfSJason Wang  */
1308946fa564SMichael S. Tsirkin static int add_recvbuf_small(struct virtnet_info *vi, struct receive_queue *rq,
1309946fa564SMichael S. Tsirkin 			     gfp_t gfp)
1310296f96fcSRusty Russell {
1311f6b10209SJason Wang 	struct page_frag *alloc_frag = &rq->alloc_frag;
1312f6b10209SJason Wang 	char *buf;
13132de2f7f4SJohn Fastabend 	unsigned int xdp_headroom = virtnet_get_headroom(vi);
1314192f68cfSJason Wang 	void *ctx = (void *)(unsigned long)xdp_headroom;
1315f6b10209SJason Wang 	int len = vi->hdr_len + VIRTNET_RX_PAD + GOOD_PACKET_LEN + xdp_headroom;
13169ab86bbcSShirley Ma 	int err;
13173f2c31d9SMark McLoughlin 
1318f6b10209SJason Wang 	len = SKB_DATA_ALIGN(len) +
1319f6b10209SJason Wang 	      SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1320f6b10209SJason Wang 	if (unlikely(!skb_page_frag_refill(len, alloc_frag, gfp)))
13219ab86bbcSShirley Ma 		return -ENOMEM;
1322296f96fcSRusty Russell 
1323f6b10209SJason Wang 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
1324f6b10209SJason Wang 	get_page(alloc_frag->page);
1325f6b10209SJason Wang 	alloc_frag->offset += len;
1326f6b10209SJason Wang 	sg_init_one(rq->sg, buf + VIRTNET_RX_PAD + xdp_headroom,
1327f6b10209SJason Wang 		    vi->hdr_len + GOOD_PACKET_LEN);
1328192f68cfSJason Wang 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
13299ab86bbcSShirley Ma 	if (err < 0)
1330f6b10209SJason Wang 		put_page(virt_to_head_page(buf));
13319ab86bbcSShirley Ma 	return err;
133297402b96SHerbert Xu }
133397402b96SHerbert Xu 
1334012873d0SMichael S. Tsirkin static int add_recvbuf_big(struct virtnet_info *vi, struct receive_queue *rq,
1335012873d0SMichael S. Tsirkin 			   gfp_t gfp)
13369ab86bbcSShirley Ma {
13379ab86bbcSShirley Ma 	struct page *first, *list = NULL;
13389ab86bbcSShirley Ma 	char *p;
13399ab86bbcSShirley Ma 	int i, err, offset;
1340296f96fcSRusty Russell 
13414959aebbSGavin Li 	sg_init_table(rq->sg, vi->big_packets_num_skbfrags + 2);
1342a5835440SRusty Russell 
13434959aebbSGavin Li 	/* page in rq->sg[vi->big_packets_num_skbfrags + 1] is list tail */
13444959aebbSGavin Li 	for (i = vi->big_packets_num_skbfrags + 1; i > 1; --i) {
1345e9d7417bSJason Wang 		first = get_a_page(rq, gfp);
13469ab86bbcSShirley Ma 		if (!first) {
13479ab86bbcSShirley Ma 			if (list)
1348e9d7417bSJason Wang 				give_pages(rq, list);
13499ab86bbcSShirley Ma 			return -ENOMEM;
1350296f96fcSRusty Russell 		}
1351e9d7417bSJason Wang 		sg_set_buf(&rq->sg[i], page_address(first), PAGE_SIZE);
13529ab86bbcSShirley Ma 
13539ab86bbcSShirley Ma 		/* chain new page in list head to match sg */
13549ab86bbcSShirley Ma 		first->private = (unsigned long)list;
13559ab86bbcSShirley Ma 		list = first;
13569ab86bbcSShirley Ma 	}
13579ab86bbcSShirley Ma 
1358e9d7417bSJason Wang 	first = get_a_page(rq, gfp);
13599ab86bbcSShirley Ma 	if (!first) {
1360e9d7417bSJason Wang 		give_pages(rq, list);
13619ab86bbcSShirley Ma 		return -ENOMEM;
13629ab86bbcSShirley Ma 	}
13639ab86bbcSShirley Ma 	p = page_address(first);
13649ab86bbcSShirley Ma 
1365e9d7417bSJason Wang 	/* rq->sg[0], rq->sg[1] share the same page */
1366012873d0SMichael S. Tsirkin 	/* a separated rq->sg[0] for header - required in case !any_header_sg */
1367012873d0SMichael S. Tsirkin 	sg_set_buf(&rq->sg[0], p, vi->hdr_len);
13689ab86bbcSShirley Ma 
1369e9d7417bSJason Wang 	/* rq->sg[1] for data packet, from offset */
13709ab86bbcSShirley Ma 	offset = sizeof(struct padded_vnet_hdr);
1371e9d7417bSJason Wang 	sg_set_buf(&rq->sg[1], p + offset, PAGE_SIZE - offset);
13729ab86bbcSShirley Ma 
13739ab86bbcSShirley Ma 	/* chain first in list head */
13749ab86bbcSShirley Ma 	first->private = (unsigned long)list;
13754959aebbSGavin Li 	err = virtqueue_add_inbuf(rq->vq, rq->sg, vi->big_packets_num_skbfrags + 2,
1376aa989f5eSMichael S. Tsirkin 				  first, gfp);
13779ab86bbcSShirley Ma 	if (err < 0)
1378e9d7417bSJason Wang 		give_pages(rq, first);
13799ab86bbcSShirley Ma 
13809ab86bbcSShirley Ma 	return err;
13819ab86bbcSShirley Ma }
13829ab86bbcSShirley Ma 
1383d85b758fSMichael S. Tsirkin static unsigned int get_mergeable_buf_len(struct receive_queue *rq,
13843cc81a9aSJason Wang 					  struct ewma_pkt_len *avg_pkt_len,
13853cc81a9aSJason Wang 					  unsigned int room)
13869ab86bbcSShirley Ma {
1387c1ddc42dSAndrew Melnychenko 	struct virtnet_info *vi = rq->vq->vdev->priv;
1388c1ddc42dSAndrew Melnychenko 	const size_t hdr_len = vi->hdr_len;
1389fbf28d78SMichael Dalton 	unsigned int len;
1390fbf28d78SMichael Dalton 
13913cc81a9aSJason Wang 	if (room)
13923cc81a9aSJason Wang 		return PAGE_SIZE - room;
13933cc81a9aSJason Wang 
13945377d758SJohannes Berg 	len = hdr_len +	clamp_t(unsigned int, ewma_pkt_len_read(avg_pkt_len),
1395f0c3192cSMichael S. Tsirkin 				rq->min_buf_len, PAGE_SIZE - hdr_len);
13963cc81a9aSJason Wang 
1397e377fcc8SMichael S. Tsirkin 	return ALIGN(len, L1_CACHE_BYTES);
1398fbf28d78SMichael Dalton }
1399fbf28d78SMichael Dalton 
14002de2f7f4SJohn Fastabend static int add_recvbuf_mergeable(struct virtnet_info *vi,
14012de2f7f4SJohn Fastabend 				 struct receive_queue *rq, gfp_t gfp)
1402fbf28d78SMichael Dalton {
1403fb51879dSMichael Dalton 	struct page_frag *alloc_frag = &rq->alloc_frag;
14042de2f7f4SJohn Fastabend 	unsigned int headroom = virtnet_get_headroom(vi);
14053cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
14063cc81a9aSJason Wang 	unsigned int room = SKB_DATA_ALIGN(headroom + tailroom);
1407fb51879dSMichael Dalton 	char *buf;
1408680557cfSMichael S. Tsirkin 	void *ctx;
14099ab86bbcSShirley Ma 	int err;
1410fb51879dSMichael Dalton 	unsigned int len, hole;
14119ab86bbcSShirley Ma 
14123cc81a9aSJason Wang 	/* Extra tailroom is needed to satisfy XDP's assumption. This
14133cc81a9aSJason Wang 	 * means rx frags coalescing won't work, but consider we've
14143cc81a9aSJason Wang 	 * disabled GSO for XDP, it won't be a big issue.
14153cc81a9aSJason Wang 	 */
14163cc81a9aSJason Wang 	len = get_mergeable_buf_len(rq, &rq->mrg_avg_pkt_len, room);
14173cc81a9aSJason Wang 	if (unlikely(!skb_page_frag_refill(len + room, alloc_frag, gfp)))
14189ab86bbcSShirley Ma 		return -ENOMEM;
1419ab7db917SMichael Dalton 
1420fb51879dSMichael Dalton 	buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
14212de2f7f4SJohn Fastabend 	buf += headroom; /* advance address leaving hole at front of pkt */
1422fb51879dSMichael Dalton 	get_page(alloc_frag->page);
14233cc81a9aSJason Wang 	alloc_frag->offset += len + room;
1424fb51879dSMichael Dalton 	hole = alloc_frag->size - alloc_frag->offset;
14253cc81a9aSJason Wang 	if (hole < len + room) {
1426ab7db917SMichael Dalton 		/* To avoid internal fragmentation, if there is very likely not
1427ab7db917SMichael Dalton 		 * enough space for another buffer, add the remaining space to
14281daa8790SMichael S. Tsirkin 		 * the current buffer.
1429ab7db917SMichael Dalton 		 */
1430fb51879dSMichael Dalton 		len += hole;
1431fb51879dSMichael Dalton 		alloc_frag->offset += hole;
1432fb51879dSMichael Dalton 	}
14339ab86bbcSShirley Ma 
1434fb51879dSMichael Dalton 	sg_init_one(rq->sg, buf, len);
143529fda25aSDavid S. Miller 	ctx = mergeable_len_to_ctx(len, headroom);
1436680557cfSMichael S. Tsirkin 	err = virtqueue_add_inbuf_ctx(rq->vq, rq->sg, 1, buf, ctx, gfp);
14379ab86bbcSShirley Ma 	if (err < 0)
14382613af0eSMichael Dalton 		put_page(virt_to_head_page(buf));
14399ab86bbcSShirley Ma 
14409ab86bbcSShirley Ma 	return err;
1441296f96fcSRusty Russell }
1442296f96fcSRusty Russell 
1443b2baed69SRusty Russell /*
1444b2baed69SRusty Russell  * Returns false if we couldn't fill entirely (OOM).
1445b2baed69SRusty Russell  *
1446b2baed69SRusty Russell  * Normally run in the receive path, but can also be run from ndo_open
1447b2baed69SRusty Russell  * before we're receiving packets, or from refill_work which is
1448b2baed69SRusty Russell  * careful to disable receiving (using napi_disable).
1449b2baed69SRusty Russell  */
1450946fa564SMichael S. Tsirkin static bool try_fill_recv(struct virtnet_info *vi, struct receive_queue *rq,
1451946fa564SMichael S. Tsirkin 			  gfp_t gfp)
14523f2c31d9SMark McLoughlin {
14533f2c31d9SMark McLoughlin 	int err;
14541788f495SMichael S. Tsirkin 	bool oom;
14553f2c31d9SMark McLoughlin 
14560aea51c3SAmit Shah 	do {
14579ab86bbcSShirley Ma 		if (vi->mergeable_rx_bufs)
14582de2f7f4SJohn Fastabend 			err = add_recvbuf_mergeable(vi, rq, gfp);
14599ab86bbcSShirley Ma 		else if (vi->big_packets)
1460012873d0SMichael S. Tsirkin 			err = add_recvbuf_big(vi, rq, gfp);
14619ab86bbcSShirley Ma 		else
1462946fa564SMichael S. Tsirkin 			err = add_recvbuf_small(vi, rq, gfp);
14633f2c31d9SMark McLoughlin 
14641788f495SMichael S. Tsirkin 		oom = err == -ENOMEM;
14659ed4cb07SRusty Russell 		if (err)
14663f2c31d9SMark McLoughlin 			break;
1467b7dfde95SLinus Torvalds 	} while (rq->vq->num_free);
1468461f03dcSToshiaki Makita 	if (virtqueue_kick_prepare(rq->vq) && virtqueue_notify(rq->vq)) {
146901c32598SMichael S. Tsirkin 		unsigned long flags;
147001c32598SMichael S. Tsirkin 
147101c32598SMichael S. Tsirkin 		flags = u64_stats_update_begin_irqsave(&rq->stats.syncp);
1472d46eeeafSJason Wang 		rq->stats.kicks++;
147301c32598SMichael S. Tsirkin 		u64_stats_update_end_irqrestore(&rq->stats.syncp, flags);
1474461f03dcSToshiaki Makita 	}
1475461f03dcSToshiaki Makita 
14763161e453SRusty Russell 	return !oom;
14773f2c31d9SMark McLoughlin }
14783f2c31d9SMark McLoughlin 
147918445c4dSRusty Russell static void skb_recv_done(struct virtqueue *rvq)
1480296f96fcSRusty Russell {
1481296f96fcSRusty Russell 	struct virtnet_info *vi = rvq->vdev->priv;
1482986a4f4dSJason Wang 	struct receive_queue *rq = &vi->rq[vq2rxq(rvq)];
1483e9d7417bSJason Wang 
1484e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(&rq->napi, rvq);
1485296f96fcSRusty Russell }
1486296f96fcSRusty Russell 
1487e4e8452aSWillem de Bruijn static void virtnet_napi_enable(struct virtqueue *vq, struct napi_struct *napi)
14883e9d08ecSBruce Rogers {
1489e4e8452aSWillem de Bruijn 	napi_enable(napi);
14903e9d08ecSBruce Rogers 
14913e9d08ecSBruce Rogers 	/* If all buffers were filled by other side before we napi_enabled, we
1492e4e8452aSWillem de Bruijn 	 * won't get another interrupt, so process any outstanding packets now.
1493e4e8452aSWillem de Bruijn 	 * Call local_bh_enable after to trigger softIRQ processing.
1494e4e8452aSWillem de Bruijn 	 */
1495ec13ee80SMichael S. Tsirkin 	local_bh_disable();
1496e4e8452aSWillem de Bruijn 	virtqueue_napi_schedule(napi, vq);
1497ec13ee80SMichael S. Tsirkin 	local_bh_enable();
14983e9d08ecSBruce Rogers }
14993e9d08ecSBruce Rogers 
1500b92f1e67SWillem de Bruijn static void virtnet_napi_tx_enable(struct virtnet_info *vi,
1501b92f1e67SWillem de Bruijn 				   struct virtqueue *vq,
1502b92f1e67SWillem de Bruijn 				   struct napi_struct *napi)
1503b92f1e67SWillem de Bruijn {
1504b92f1e67SWillem de Bruijn 	if (!napi->weight)
1505b92f1e67SWillem de Bruijn 		return;
1506b92f1e67SWillem de Bruijn 
1507b92f1e67SWillem de Bruijn 	/* Tx napi touches cachelines on the cpu handling tx interrupts. Only
1508b92f1e67SWillem de Bruijn 	 * enable the feature if this is likely affine with the transmit path.
1509b92f1e67SWillem de Bruijn 	 */
1510b92f1e67SWillem de Bruijn 	if (!vi->affinity_hint_set) {
1511b92f1e67SWillem de Bruijn 		napi->weight = 0;
1512b92f1e67SWillem de Bruijn 		return;
1513b92f1e67SWillem de Bruijn 	}
1514b92f1e67SWillem de Bruijn 
1515b92f1e67SWillem de Bruijn 	return virtnet_napi_enable(vq, napi);
1516b92f1e67SWillem de Bruijn }
1517b92f1e67SWillem de Bruijn 
151878a57b48SWillem de Bruijn static void virtnet_napi_tx_disable(struct napi_struct *napi)
151978a57b48SWillem de Bruijn {
152078a57b48SWillem de Bruijn 	if (napi->weight)
152178a57b48SWillem de Bruijn 		napi_disable(napi);
152278a57b48SWillem de Bruijn }
152378a57b48SWillem de Bruijn 
15243161e453SRusty Russell static void refill_work(struct work_struct *work)
15253161e453SRusty Russell {
1526e9d7417bSJason Wang 	struct virtnet_info *vi =
1527e9d7417bSJason Wang 		container_of(work, struct virtnet_info, refill.work);
15283161e453SRusty Russell 	bool still_empty;
1529986a4f4dSJason Wang 	int i;
15303161e453SRusty Russell 
153155257d72SSasha Levin 	for (i = 0; i < vi->curr_queue_pairs; i++) {
1532986a4f4dSJason Wang 		struct receive_queue *rq = &vi->rq[i];
1533986a4f4dSJason Wang 
1534986a4f4dSJason Wang 		napi_disable(&rq->napi);
1535946fa564SMichael S. Tsirkin 		still_empty = !try_fill_recv(vi, rq, GFP_KERNEL);
1536e4e8452aSWillem de Bruijn 		virtnet_napi_enable(rq->vq, &rq->napi);
15373161e453SRusty Russell 
15383161e453SRusty Russell 		/* In theory, this can happen: if we don't get any buffers in
1539986a4f4dSJason Wang 		 * we will *never* try to fill again.
1540986a4f4dSJason Wang 		 */
15413161e453SRusty Russell 		if (still_empty)
15423b07e9caSTejun Heo 			schedule_delayed_work(&vi->refill, HZ/2);
15433161e453SRusty Russell 	}
1544986a4f4dSJason Wang }
15453161e453SRusty Russell 
15462471c75eSJesper Dangaard Brouer static int virtnet_receive(struct receive_queue *rq, int budget,
15472471c75eSJesper Dangaard Brouer 			   unsigned int *xdp_xmit)
1548296f96fcSRusty Russell {
1549e9d7417bSJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
1550d46eeeafSJason Wang 	struct virtnet_rq_stats stats = {};
1551a0929a44SToshiaki Makita 	unsigned int len;
15529ab86bbcSShirley Ma 	void *buf;
1553a0929a44SToshiaki Makita 	int i;
1554296f96fcSRusty Russell 
1555192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
1556680557cfSMichael S. Tsirkin 		void *ctx;
1557680557cfSMichael S. Tsirkin 
1558d46eeeafSJason Wang 		while (stats.packets < budget &&
1559680557cfSMichael S. Tsirkin 		       (buf = virtqueue_get_buf_ctx(rq->vq, &len, &ctx))) {
1560a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, ctx, xdp_xmit, &stats);
1561d46eeeafSJason Wang 			stats.packets++;
1562680557cfSMichael S. Tsirkin 		}
1563680557cfSMichael S. Tsirkin 	} else {
1564d46eeeafSJason Wang 		while (stats.packets < budget &&
1565e9d7417bSJason Wang 		       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
1566a0929a44SToshiaki Makita 			receive_buf(vi, rq, buf, len, NULL, xdp_xmit, &stats);
1567d46eeeafSJason Wang 			stats.packets++;
1568296f96fcSRusty Russell 		}
1569680557cfSMichael S. Tsirkin 	}
1570296f96fcSRusty Russell 
1571718be6baS? jiang 	if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) {
15725a159128SJason Wang 		if (!try_fill_recv(vi, rq, GFP_ATOMIC)) {
15735a159128SJason Wang 			spin_lock(&vi->refill_lock);
15745a159128SJason Wang 			if (vi->refill_enabled)
15753b07e9caSTejun Heo 				schedule_delayed_work(&vi->refill, 0);
15765a159128SJason Wang 			spin_unlock(&vi->refill_lock);
15775a159128SJason Wang 		}
15783161e453SRusty Russell 	}
1579296f96fcSRusty Russell 
1580d7dfc5cfSToshiaki Makita 	u64_stats_update_begin(&rq->stats.syncp);
1581a0929a44SToshiaki Makita 	for (i = 0; i < VIRTNET_RQ_STATS_LEN; i++) {
1582a0929a44SToshiaki Makita 		size_t offset = virtnet_rq_stats_desc[i].offset;
1583a0929a44SToshiaki Makita 		u64 *item;
1584a0929a44SToshiaki Makita 
1585d46eeeafSJason Wang 		item = (u64 *)((u8 *)&rq->stats + offset);
1586d46eeeafSJason Wang 		*item += *(u64 *)((u8 *)&stats + offset);
1587a0929a44SToshiaki Makita 	}
1588d7dfc5cfSToshiaki Makita 	u64_stats_update_end(&rq->stats.syncp);
158961845d20SJason Wang 
1590d46eeeafSJason Wang 	return stats.packets;
15912ffa7598SJason Wang }
15922ffa7598SJason Wang 
1593df133f3fSMichael S. Tsirkin static void free_old_xmit_skbs(struct send_queue *sq, bool in_napi)
1594ea7735d9SWillem de Bruijn {
1595ea7735d9SWillem de Bruijn 	unsigned int len;
1596ea7735d9SWillem de Bruijn 	unsigned int packets = 0;
1597ea7735d9SWillem de Bruijn 	unsigned int bytes = 0;
15985050471dSToshiaki Makita 	void *ptr;
1599ea7735d9SWillem de Bruijn 
16005050471dSToshiaki Makita 	while ((ptr = virtqueue_get_buf(sq->vq, &len)) != NULL) {
16015050471dSToshiaki Makita 		if (likely(!is_xdp_frame(ptr))) {
16025050471dSToshiaki Makita 			struct sk_buff *skb = ptr;
16035050471dSToshiaki Makita 
1604ea7735d9SWillem de Bruijn 			pr_debug("Sent skb %p\n", skb);
1605ea7735d9SWillem de Bruijn 
1606ea7735d9SWillem de Bruijn 			bytes += skb->len;
1607df133f3fSMichael S. Tsirkin 			napi_consume_skb(skb, in_napi);
16085050471dSToshiaki Makita 		} else {
16095050471dSToshiaki Makita 			struct xdp_frame *frame = ptr_to_xdp(ptr);
16105050471dSToshiaki Makita 
16115050471dSToshiaki Makita 			bytes += frame->len;
16125050471dSToshiaki Makita 			xdp_return_frame(frame);
16135050471dSToshiaki Makita 		}
16145050471dSToshiaki Makita 		packets++;
1615ea7735d9SWillem de Bruijn 	}
1616ea7735d9SWillem de Bruijn 
1617ea7735d9SWillem de Bruijn 	/* Avoid overhead when no packets have been processed
1618ea7735d9SWillem de Bruijn 	 * happens when called speculatively from start_xmit.
1619ea7735d9SWillem de Bruijn 	 */
1620ea7735d9SWillem de Bruijn 	if (!packets)
1621ea7735d9SWillem de Bruijn 		return;
1622ea7735d9SWillem de Bruijn 
1623d7dfc5cfSToshiaki Makita 	u64_stats_update_begin(&sq->stats.syncp);
1624d7dfc5cfSToshiaki Makita 	sq->stats.bytes += bytes;
1625d7dfc5cfSToshiaki Makita 	sq->stats.packets += packets;
1626d7dfc5cfSToshiaki Makita 	u64_stats_update_end(&sq->stats.syncp);
1627ea7735d9SWillem de Bruijn }
1628ea7735d9SWillem de Bruijn 
1629534da5e8SToshiaki Makita static bool is_xdp_raw_buffer_queue(struct virtnet_info *vi, int q)
1630534da5e8SToshiaki Makita {
1631534da5e8SToshiaki Makita 	if (q < (vi->curr_queue_pairs - vi->xdp_queue_pairs))
1632534da5e8SToshiaki Makita 		return false;
1633534da5e8SToshiaki Makita 	else if (q < vi->curr_queue_pairs)
1634534da5e8SToshiaki Makita 		return true;
1635534da5e8SToshiaki Makita 	else
1636534da5e8SToshiaki Makita 		return false;
1637534da5e8SToshiaki Makita }
1638534da5e8SToshiaki Makita 
16397b0411efSWillem de Bruijn static void virtnet_poll_cleantx(struct receive_queue *rq)
16407b0411efSWillem de Bruijn {
16417b0411efSWillem de Bruijn 	struct virtnet_info *vi = rq->vq->vdev->priv;
16427b0411efSWillem de Bruijn 	unsigned int index = vq2rxq(rq->vq);
16437b0411efSWillem de Bruijn 	struct send_queue *sq = &vi->sq[index];
16447b0411efSWillem de Bruijn 	struct netdev_queue *txq = netdev_get_tx_queue(vi->dev, index);
16457b0411efSWillem de Bruijn 
1646534da5e8SToshiaki Makita 	if (!sq->napi.weight || is_xdp_raw_buffer_queue(vi, index))
16477b0411efSWillem de Bruijn 		return;
16487b0411efSWillem de Bruijn 
16497b0411efSWillem de Bruijn 	if (__netif_tx_trylock(txq)) {
1650ebcce492SXuan Zhuo 		if (sq->reset) {
1651ebcce492SXuan Zhuo 			__netif_tx_unlock(txq);
1652ebcce492SXuan Zhuo 			return;
1653ebcce492SXuan Zhuo 		}
1654ebcce492SXuan Zhuo 
1655a7766ef1SMichael S. Tsirkin 		do {
1656a7766ef1SMichael S. Tsirkin 			virtqueue_disable_cb(sq->vq);
1657df133f3fSMichael S. Tsirkin 			free_old_xmit_skbs(sq, true);
1658a7766ef1SMichael S. Tsirkin 		} while (unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
16597b0411efSWillem de Bruijn 
16607b0411efSWillem de Bruijn 		if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
16617b0411efSWillem de Bruijn 			netif_tx_wake_queue(txq);
166222bc63c5SMichael S. Tsirkin 
166322bc63c5SMichael S. Tsirkin 		__netif_tx_unlock(txq);
166422bc63c5SMichael S. Tsirkin 	}
16657b0411efSWillem de Bruijn }
16667b0411efSWillem de Bruijn 
16672ffa7598SJason Wang static int virtnet_poll(struct napi_struct *napi, int budget)
16682ffa7598SJason Wang {
16692ffa7598SJason Wang 	struct receive_queue *rq =
16702ffa7598SJason Wang 		container_of(napi, struct receive_queue, napi);
16719267c430SJason Wang 	struct virtnet_info *vi = rq->vq->vdev->priv;
16729267c430SJason Wang 	struct send_queue *sq;
16732a43565cSToshiaki Makita 	unsigned int received;
16742471c75eSJesper Dangaard Brouer 	unsigned int xdp_xmit = 0;
16752ffa7598SJason Wang 
16767b0411efSWillem de Bruijn 	virtnet_poll_cleantx(rq);
16777b0411efSWillem de Bruijn 
1678186b3c99SJason Wang 	received = virtnet_receive(rq, budget, &xdp_xmit);
16792ffa7598SJason Wang 
1680*ad7e615fSMagnus Karlsson 	if (xdp_xmit & VIRTIO_XDP_REDIR)
1681*ad7e615fSMagnus Karlsson 		xdp_do_flush();
1682*ad7e615fSMagnus Karlsson 
16838329d98eSRusty Russell 	/* Out of packets? */
1684e4e8452aSWillem de Bruijn 	if (received < budget)
1685e4e8452aSWillem de Bruijn 		virtqueue_napi_complete(napi, rq->vq, received);
1686296f96fcSRusty Russell 
16872471c75eSJesper Dangaard Brouer 	if (xdp_xmit & VIRTIO_XDP_TX) {
168897c2c69eSXuan Zhuo 		sq = virtnet_xdp_get_sq(vi);
1689461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1690461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
1691461f03dcSToshiaki Makita 			sq->stats.kicks++;
1692461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
1693461f03dcSToshiaki Makita 		}
169497c2c69eSXuan Zhuo 		virtnet_xdp_put_sq(vi, sq);
16959267c430SJason Wang 	}
1696186b3c99SJason Wang 
1697296f96fcSRusty Russell 	return received;
1698296f96fcSRusty Russell }
1699296f96fcSRusty Russell 
1700986a4f4dSJason Wang static int virtnet_open(struct net_device *dev)
1701986a4f4dSJason Wang {
1702986a4f4dSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
1703754b8a21SJesper Dangaard Brouer 	int i, err;
1704986a4f4dSJason Wang 
17055a159128SJason Wang 	enable_delayed_refill(vi);
17065a159128SJason Wang 
1707e4166625SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
1708e4166625SJason Wang 		if (i < vi->curr_queue_pairs)
1709986a4f4dSJason Wang 			/* Make sure we have some buffers: if oom use wq. */
1710946fa564SMichael S. Tsirkin 			if (!try_fill_recv(vi, &vi->rq[i], GFP_KERNEL))
1711986a4f4dSJason Wang 				schedule_delayed_work(&vi->refill, 0);
1712754b8a21SJesper Dangaard Brouer 
1713b02e5a0eSBjörn Töpel 		err = xdp_rxq_info_reg(&vi->rq[i].xdp_rxq, dev, i, vi->rq[i].napi.napi_id);
1714754b8a21SJesper Dangaard Brouer 		if (err < 0)
1715754b8a21SJesper Dangaard Brouer 			return err;
1716754b8a21SJesper Dangaard Brouer 
17178d5d8852SJesper Dangaard Brouer 		err = xdp_rxq_info_reg_mem_model(&vi->rq[i].xdp_rxq,
17188d5d8852SJesper Dangaard Brouer 						 MEM_TYPE_PAGE_SHARED, NULL);
17198d5d8852SJesper Dangaard Brouer 		if (err < 0) {
17208d5d8852SJesper Dangaard Brouer 			xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
17218d5d8852SJesper Dangaard Brouer 			return err;
17228d5d8852SJesper Dangaard Brouer 		}
17238d5d8852SJesper Dangaard Brouer 
1724e4e8452aSWillem de Bruijn 		virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
1725b92f1e67SWillem de Bruijn 		virtnet_napi_tx_enable(vi, vi->sq[i].vq, &vi->sq[i].napi);
1726986a4f4dSJason Wang 	}
1727986a4f4dSJason Wang 
1728986a4f4dSJason Wang 	return 0;
1729986a4f4dSJason Wang }
1730986a4f4dSJason Wang 
1731b92f1e67SWillem de Bruijn static int virtnet_poll_tx(struct napi_struct *napi, int budget)
1732b92f1e67SWillem de Bruijn {
1733b92f1e67SWillem de Bruijn 	struct send_queue *sq = container_of(napi, struct send_queue, napi);
1734b92f1e67SWillem de Bruijn 	struct virtnet_info *vi = sq->vq->vdev->priv;
1735534da5e8SToshiaki Makita 	unsigned int index = vq2txq(sq->vq);
1736534da5e8SToshiaki Makita 	struct netdev_queue *txq;
17375a2f966dSMichael S. Tsirkin 	int opaque;
17385a2f966dSMichael S. Tsirkin 	bool done;
1739b92f1e67SWillem de Bruijn 
1740534da5e8SToshiaki Makita 	if (unlikely(is_xdp_raw_buffer_queue(vi, index))) {
1741534da5e8SToshiaki Makita 		/* We don't need to enable cb for XDP */
1742534da5e8SToshiaki Makita 		napi_complete_done(napi, 0);
1743534da5e8SToshiaki Makita 		return 0;
1744534da5e8SToshiaki Makita 	}
1745534da5e8SToshiaki Makita 
1746534da5e8SToshiaki Makita 	txq = netdev_get_tx_queue(vi->dev, index);
1747b92f1e67SWillem de Bruijn 	__netif_tx_lock(txq, raw_smp_processor_id());
17485a2f966dSMichael S. Tsirkin 	virtqueue_disable_cb(sq->vq);
1749df133f3fSMichael S. Tsirkin 	free_old_xmit_skbs(sq, true);
17505a2f966dSMichael S. Tsirkin 
175122bc63c5SMichael S. Tsirkin 	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
175222bc63c5SMichael S. Tsirkin 		netif_tx_wake_queue(txq);
175322bc63c5SMichael S. Tsirkin 
17545a2f966dSMichael S. Tsirkin 	opaque = virtqueue_enable_cb_prepare(sq->vq);
17555a2f966dSMichael S. Tsirkin 
17565a2f966dSMichael S. Tsirkin 	done = napi_complete_done(napi, 0);
17575a2f966dSMichael S. Tsirkin 
17585a2f966dSMichael S. Tsirkin 	if (!done)
17595a2f966dSMichael S. Tsirkin 		virtqueue_disable_cb(sq->vq);
17605a2f966dSMichael S. Tsirkin 
1761b92f1e67SWillem de Bruijn 	__netif_tx_unlock(txq);
1762b92f1e67SWillem de Bruijn 
17635a2f966dSMichael S. Tsirkin 	if (done) {
17645a2f966dSMichael S. Tsirkin 		if (unlikely(virtqueue_poll(sq->vq, opaque))) {
17655a2f966dSMichael S. Tsirkin 			if (napi_schedule_prep(napi)) {
17665a2f966dSMichael S. Tsirkin 				__netif_tx_lock(txq, raw_smp_processor_id());
17675a2f966dSMichael S. Tsirkin 				virtqueue_disable_cb(sq->vq);
17685a2f966dSMichael S. Tsirkin 				__netif_tx_unlock(txq);
17695a2f966dSMichael S. Tsirkin 				__napi_schedule(napi);
17705a2f966dSMichael S. Tsirkin 			}
17715a2f966dSMichael S. Tsirkin 		}
17725a2f966dSMichael S. Tsirkin 	}
1773b92f1e67SWillem de Bruijn 
1774b92f1e67SWillem de Bruijn 	return 0;
1775b92f1e67SWillem de Bruijn }
1776b92f1e67SWillem de Bruijn 
1777e9d7417bSJason Wang static int xmit_skb(struct send_queue *sq, struct sk_buff *skb)
1778296f96fcSRusty Russell {
1779012873d0SMichael S. Tsirkin 	struct virtio_net_hdr_mrg_rxbuf *hdr;
1780296f96fcSRusty Russell 	const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
1781e9d7417bSJason Wang 	struct virtnet_info *vi = sq->vq->vdev->priv;
1782e2fcad58SJason A. Donenfeld 	int num_sg;
1783012873d0SMichael S. Tsirkin 	unsigned hdr_len = vi->hdr_len;
1784e7428e95SMichael S. Tsirkin 	bool can_push;
1785296f96fcSRusty Russell 
1786e174961cSJohannes Berg 	pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
1787e7428e95SMichael S. Tsirkin 
1788e7428e95SMichael S. Tsirkin 	can_push = vi->any_header_sg &&
1789e7428e95SMichael S. Tsirkin 		!((unsigned long)skb->data & (__alignof__(*hdr) - 1)) &&
1790e7428e95SMichael S. Tsirkin 		!skb_header_cloned(skb) && skb_headroom(skb) >= hdr_len;
1791e7428e95SMichael S. Tsirkin 	/* Even if we can, don't push here yet as this would skew
1792e7428e95SMichael S. Tsirkin 	 * csum_start offset below. */
1793e7428e95SMichael S. Tsirkin 	if (can_push)
1794012873d0SMichael S. Tsirkin 		hdr = (struct virtio_net_hdr_mrg_rxbuf *)(skb->data - hdr_len);
1795e7428e95SMichael S. Tsirkin 	else
1796e7428e95SMichael S. Tsirkin 		hdr = skb_vnet_hdr(skb);
1797296f96fcSRusty Russell 
1798e858fae2SMike Rapoport 	if (virtio_net_hdr_from_skb(skb, &hdr->hdr,
1799fd3a8862SWillem de Bruijn 				    virtio_is_little_endian(vi->vdev), false,
1800fd3a8862SWillem de Bruijn 				    0))
180185eb1389SXianting Tian 		return -EPROTO;
1802296f96fcSRusty Russell 
1803e7428e95SMichael S. Tsirkin 	if (vi->mergeable_rx_bufs)
1804012873d0SMichael S. Tsirkin 		hdr->num_buffers = 0;
18053f2c31d9SMark McLoughlin 
1806547c890cSJason Wang 	sg_init_table(sq->sg, skb_shinfo(skb)->nr_frags + (can_push ? 1 : 2));
1807e7428e95SMichael S. Tsirkin 	if (can_push) {
1808e7428e95SMichael S. Tsirkin 		__skb_push(skb, hdr_len);
1809e7428e95SMichael S. Tsirkin 		num_sg = skb_to_sgvec(skb, sq->sg, 0, skb->len);
1810e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
1811e2fcad58SJason A. Donenfeld 			return num_sg;
1812e7428e95SMichael S. Tsirkin 		/* Pull header back to avoid skew in tx bytes calculations. */
1813e7428e95SMichael S. Tsirkin 		__skb_pull(skb, hdr_len);
1814e7428e95SMichael S. Tsirkin 	} else {
1815e7428e95SMichael S. Tsirkin 		sg_set_buf(sq->sg, hdr, hdr_len);
1816e2fcad58SJason A. Donenfeld 		num_sg = skb_to_sgvec(skb, sq->sg + 1, 0, skb->len);
1817e2fcad58SJason A. Donenfeld 		if (unlikely(num_sg < 0))
1818e2fcad58SJason A. Donenfeld 			return num_sg;
1819e2fcad58SJason A. Donenfeld 		num_sg++;
1820e7428e95SMichael S. Tsirkin 	}
18219dc7b9e4SRusty Russell 	return virtqueue_add_outbuf(sq->vq, sq->sg, num_sg, skb, GFP_ATOMIC);
182211a3a154SRusty Russell }
182311a3a154SRusty Russell 
1824424efe9cSStephen Hemminger static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
182599ffc696SRusty Russell {
182699ffc696SRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
1827986a4f4dSJason Wang 	int qnum = skb_get_queue_mapping(skb);
1828986a4f4dSJason Wang 	struct send_queue *sq = &vi->sq[qnum];
18299ed4cb07SRusty Russell 	int err;
18304b7fd2e6SMichael S. Tsirkin 	struct netdev_queue *txq = netdev_get_tx_queue(dev, qnum);
18316b16f9eeSFlorian Westphal 	bool kick = !netdev_xmit_more();
1832b92f1e67SWillem de Bruijn 	bool use_napi = sq->napi.weight;
18332cb9c6baSRusty Russell 
18342cb9c6baSRusty Russell 	/* Free up any pending old buffers before queueing new ones. */
1835a7766ef1SMichael S. Tsirkin 	do {
1836a7766ef1SMichael S. Tsirkin 		if (use_napi)
1837a7766ef1SMichael S. Tsirkin 			virtqueue_disable_cb(sq->vq);
1838a7766ef1SMichael S. Tsirkin 
1839df133f3fSMichael S. Tsirkin 		free_old_xmit_skbs(sq, false);
184099ffc696SRusty Russell 
1841a7766ef1SMichael S. Tsirkin 	} while (use_napi && kick &&
1842a7766ef1SMichael S. Tsirkin 	       unlikely(!virtqueue_enable_cb_delayed(sq->vq)));
1843bdb12e0dSWillem de Bruijn 
1844074c3582SJacob Keller 	/* timestamp packet in software */
1845074c3582SJacob Keller 	skb_tx_timestamp(skb);
1846074c3582SJacob Keller 
184703f191baSMichael S. Tsirkin 	/* Try to transmit */
1848b7dfde95SLinus Torvalds 	err = xmit_skb(sq, skb);
184999ffc696SRusty Russell 
18509ed4cb07SRusty Russell 	/* This should not happen! */
1851681daee2SJason Wang 	if (unlikely(err)) {
185258eba97dSRusty Russell 		dev->stats.tx_fifo_errors++;
18532e57b79cSRick Jones 		if (net_ratelimit())
185458eba97dSRusty Russell 			dev_warn(&dev->dev,
18557934b481SYuval Shaia 				 "Unexpected TXQ (%d) queue failure: %d\n",
18567934b481SYuval Shaia 				 qnum, err);
185758eba97dSRusty Russell 		dev->stats.tx_dropped++;
185885e94525SEric W. Biederman 		dev_kfree_skb_any(skb);
185958eba97dSRusty Russell 		return NETDEV_TX_OK;
1860296f96fcSRusty Russell 	}
186103f191baSMichael S. Tsirkin 
186248925e37SRusty Russell 	/* Don't wait up for transmitted skbs to be freed. */
1863b92f1e67SWillem de Bruijn 	if (!use_napi) {
186448925e37SRusty Russell 		skb_orphan(skb);
1865895b5c9fSFlorian Westphal 		nf_reset_ct(skb);
1866b92f1e67SWillem de Bruijn 	}
186748925e37SRusty Russell 
186860302ff6SMichael S. Tsirkin 	/* If running out of space, stop queue to avoid getting packets that we
186960302ff6SMichael S. Tsirkin 	 * are then unable to transmit.
187060302ff6SMichael S. Tsirkin 	 * An alternative would be to force queuing layer to requeue the skb by
187160302ff6SMichael S. Tsirkin 	 * returning NETDEV_TX_BUSY. However, NETDEV_TX_BUSY should not be
187260302ff6SMichael S. Tsirkin 	 * returned in a normal path of operation: it means that driver is not
187360302ff6SMichael S. Tsirkin 	 * maintaining the TX queue stop/start state properly, and causes
187460302ff6SMichael S. Tsirkin 	 * the stack to do a non-trivial amount of useless work.
187560302ff6SMichael S. Tsirkin 	 * Since most packets only take 1 or 2 ring slots, stopping the queue
187660302ff6SMichael S. Tsirkin 	 * early means 16 slots are typically wasted.
1877d631b94eSstephen hemminger 	 */
1878b7dfde95SLinus Torvalds 	if (sq->vq->num_free < 2+MAX_SKB_FRAGS) {
1879986a4f4dSJason Wang 		netif_stop_subqueue(dev, qnum);
1880d71ebe81SJason Wang 		if (use_napi) {
1881d71ebe81SJason Wang 			if (unlikely(!virtqueue_enable_cb_delayed(sq->vq)))
1882d71ebe81SJason Wang 				virtqueue_napi_schedule(&sq->napi, sq->vq);
1883d71ebe81SJason Wang 		} else if (unlikely(!virtqueue_enable_cb_delayed(sq->vq))) {
188448925e37SRusty Russell 			/* More just got used, free them then recheck. */
1885df133f3fSMichael S. Tsirkin 			free_old_xmit_skbs(sq, false);
1886b7dfde95SLinus Torvalds 			if (sq->vq->num_free >= 2+MAX_SKB_FRAGS) {
1887986a4f4dSJason Wang 				netif_start_subqueue(dev, qnum);
1888e9d7417bSJason Wang 				virtqueue_disable_cb(sq->vq);
188948925e37SRusty Russell 			}
189048925e37SRusty Russell 		}
189148925e37SRusty Russell 	}
189248925e37SRusty Russell 
1893461f03dcSToshiaki Makita 	if (kick || netif_xmit_stopped(txq)) {
1894461f03dcSToshiaki Makita 		if (virtqueue_kick_prepare(sq->vq) && virtqueue_notify(sq->vq)) {
1895461f03dcSToshiaki Makita 			u64_stats_update_begin(&sq->stats.syncp);
1896461f03dcSToshiaki Makita 			sq->stats.kicks++;
1897461f03dcSToshiaki Makita 			u64_stats_update_end(&sq->stats.syncp);
1898461f03dcSToshiaki Makita 		}
1899461f03dcSToshiaki Makita 	}
19000b725a2cSDavid S. Miller 
19010b725a2cSDavid S. Miller 	return NETDEV_TX_OK;
1902c223a078SDavid S. Miller }
1903c223a078SDavid S. Miller 
19046a4763e2SXuan Zhuo static int virtnet_rx_resize(struct virtnet_info *vi,
19056a4763e2SXuan Zhuo 			     struct receive_queue *rq, u32 ring_num)
19066a4763e2SXuan Zhuo {
19076a4763e2SXuan Zhuo 	bool running = netif_running(vi->dev);
19086a4763e2SXuan Zhuo 	int err, qindex;
19096a4763e2SXuan Zhuo 
19106a4763e2SXuan Zhuo 	qindex = rq - vi->rq;
19116a4763e2SXuan Zhuo 
19126a4763e2SXuan Zhuo 	if (running)
19136a4763e2SXuan Zhuo 		napi_disable(&rq->napi);
19146a4763e2SXuan Zhuo 
19156a4763e2SXuan Zhuo 	err = virtqueue_resize(rq->vq, ring_num, virtnet_rq_free_unused_buf);
19166a4763e2SXuan Zhuo 	if (err)
19176a4763e2SXuan Zhuo 		netdev_err(vi->dev, "resize rx fail: rx queue index: %d err: %d\n", qindex, err);
19186a4763e2SXuan Zhuo 
19196a4763e2SXuan Zhuo 	if (!try_fill_recv(vi, rq, GFP_KERNEL))
19206a4763e2SXuan Zhuo 		schedule_delayed_work(&vi->refill, 0);
19216a4763e2SXuan Zhuo 
19226a4763e2SXuan Zhuo 	if (running)
19236a4763e2SXuan Zhuo 		virtnet_napi_enable(rq->vq, &rq->napi);
19246a4763e2SXuan Zhuo 	return err;
19256a4763e2SXuan Zhuo }
19266a4763e2SXuan Zhuo 
1927ebcce492SXuan Zhuo static int virtnet_tx_resize(struct virtnet_info *vi,
1928ebcce492SXuan Zhuo 			     struct send_queue *sq, u32 ring_num)
1929ebcce492SXuan Zhuo {
1930ebcce492SXuan Zhuo 	bool running = netif_running(vi->dev);
1931ebcce492SXuan Zhuo 	struct netdev_queue *txq;
1932ebcce492SXuan Zhuo 	int err, qindex;
1933ebcce492SXuan Zhuo 
1934ebcce492SXuan Zhuo 	qindex = sq - vi->sq;
1935ebcce492SXuan Zhuo 
1936ebcce492SXuan Zhuo 	if (running)
1937ebcce492SXuan Zhuo 		virtnet_napi_tx_disable(&sq->napi);
1938ebcce492SXuan Zhuo 
1939ebcce492SXuan Zhuo 	txq = netdev_get_tx_queue(vi->dev, qindex);
1940ebcce492SXuan Zhuo 
1941ebcce492SXuan Zhuo 	/* 1. wait all ximt complete
1942ebcce492SXuan Zhuo 	 * 2. fix the race of netif_stop_subqueue() vs netif_start_subqueue()
1943ebcce492SXuan Zhuo 	 */
1944ebcce492SXuan Zhuo 	__netif_tx_lock_bh(txq);
1945ebcce492SXuan Zhuo 
1946ebcce492SXuan Zhuo 	/* Prevent rx poll from accessing sq. */
1947ebcce492SXuan Zhuo 	sq->reset = true;
1948ebcce492SXuan Zhuo 
1949ebcce492SXuan Zhuo 	/* Prevent the upper layer from trying to send packets. */
1950ebcce492SXuan Zhuo 	netif_stop_subqueue(vi->dev, qindex);
1951ebcce492SXuan Zhuo 
1952ebcce492SXuan Zhuo 	__netif_tx_unlock_bh(txq);
1953ebcce492SXuan Zhuo 
1954ebcce492SXuan Zhuo 	err = virtqueue_resize(sq->vq, ring_num, virtnet_sq_free_unused_buf);
1955ebcce492SXuan Zhuo 	if (err)
1956ebcce492SXuan Zhuo 		netdev_err(vi->dev, "resize tx fail: tx queue index: %d err: %d\n", qindex, err);
1957ebcce492SXuan Zhuo 
1958ebcce492SXuan Zhuo 	__netif_tx_lock_bh(txq);
1959ebcce492SXuan Zhuo 	sq->reset = false;
1960ebcce492SXuan Zhuo 	netif_tx_wake_queue(txq);
1961ebcce492SXuan Zhuo 	__netif_tx_unlock_bh(txq);
1962ebcce492SXuan Zhuo 
1963ebcce492SXuan Zhuo 	if (running)
1964ebcce492SXuan Zhuo 		virtnet_napi_tx_enable(vi, sq->vq, &sq->napi);
1965ebcce492SXuan Zhuo 	return err;
1966ebcce492SXuan Zhuo }
1967ebcce492SXuan Zhuo 
196840cbfc37SAmos Kong /*
196940cbfc37SAmos Kong  * Send command via the control virtqueue and check status.  Commands
197040cbfc37SAmos Kong  * supported by the hypervisor, as indicated by feature bits, should
1971788a8b6dSstephen hemminger  * never fail unless improperly formatted.
197240cbfc37SAmos Kong  */
197340cbfc37SAmos Kong static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd,
1974d24bae32Sstephen hemminger 				 struct scatterlist *out)
197540cbfc37SAmos Kong {
1976f7bc9594SRusty Russell 	struct scatterlist *sgs[4], hdr, stat;
1977d24bae32Sstephen hemminger 	unsigned out_num = 0, tmp;
1978222722bcSYunjian Wang 	int ret;
197940cbfc37SAmos Kong 
198040cbfc37SAmos Kong 	/* Caller should know better */
1981f7bc9594SRusty Russell 	BUG_ON(!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ));
198240cbfc37SAmos Kong 
198312e57169SMichael S. Tsirkin 	vi->ctrl->status = ~0;
198412e57169SMichael S. Tsirkin 	vi->ctrl->hdr.class = class;
198512e57169SMichael S. Tsirkin 	vi->ctrl->hdr.cmd = cmd;
1986f7bc9594SRusty Russell 	/* Add header */
198712e57169SMichael S. Tsirkin 	sg_init_one(&hdr, &vi->ctrl->hdr, sizeof(vi->ctrl->hdr));
1988f7bc9594SRusty Russell 	sgs[out_num++] = &hdr;
198940cbfc37SAmos Kong 
1990f7bc9594SRusty Russell 	if (out)
1991f7bc9594SRusty Russell 		sgs[out_num++] = out;
199240cbfc37SAmos Kong 
1993f7bc9594SRusty Russell 	/* Add return status. */
199412e57169SMichael S. Tsirkin 	sg_init_one(&stat, &vi->ctrl->status, sizeof(vi->ctrl->status));
1995d24bae32Sstephen hemminger 	sgs[out_num] = &stat;
199640cbfc37SAmos Kong 
1997d24bae32Sstephen hemminger 	BUG_ON(out_num + 1 > ARRAY_SIZE(sgs));
1998222722bcSYunjian Wang 	ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC);
1999222722bcSYunjian Wang 	if (ret < 0) {
2000222722bcSYunjian Wang 		dev_warn(&vi->vdev->dev,
2001222722bcSYunjian Wang 			 "Failed to add sgs for command vq: %d\n.", ret);
2002222722bcSYunjian Wang 		return false;
2003222722bcSYunjian Wang 	}
200440cbfc37SAmos Kong 
200567975901SHeinz Graalfs 	if (unlikely(!virtqueue_kick(vi->cvq)))
200612e57169SMichael S. Tsirkin 		return vi->ctrl->status == VIRTIO_NET_OK;
200740cbfc37SAmos Kong 
200840cbfc37SAmos Kong 	/* Spin for a response, the kick causes an ioport write, trapping
200940cbfc37SAmos Kong 	 * into the hypervisor, so the request should be handled immediately.
201040cbfc37SAmos Kong 	 */
2011047b9b94SHeinz Graalfs 	while (!virtqueue_get_buf(vi->cvq, &tmp) &&
2012047b9b94SHeinz Graalfs 	       !virtqueue_is_broken(vi->cvq))
201340cbfc37SAmos Kong 		cpu_relax();
201440cbfc37SAmos Kong 
201512e57169SMichael S. Tsirkin 	return vi->ctrl->status == VIRTIO_NET_OK;
201640cbfc37SAmos Kong }
201740cbfc37SAmos Kong 
20189c46f6d4SAlex Williamson static int virtnet_set_mac_address(struct net_device *dev, void *p)
20199c46f6d4SAlex Williamson {
20209c46f6d4SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
20219c46f6d4SAlex Williamson 	struct virtio_device *vdev = vi->vdev;
2022f2f2c8b4SJiri Pirko 	int ret;
2023e37e2ff3SAndy Lutomirski 	struct sockaddr *addr;
20247e58d5aeSAmos Kong 	struct scatterlist sg;
20259c46f6d4SAlex Williamson 
2026ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
2027ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
2028ba5e4426SSridhar Samudrala 
2029801822d1SShyam Saini 	addr = kmemdup(p, sizeof(*addr), GFP_KERNEL);
2030e37e2ff3SAndy Lutomirski 	if (!addr)
2031e37e2ff3SAndy Lutomirski 		return -ENOMEM;
2032e37e2ff3SAndy Lutomirski 
2033e37e2ff3SAndy Lutomirski 	ret = eth_prepare_mac_addr_change(dev, addr);
2034f2f2c8b4SJiri Pirko 	if (ret)
2035e37e2ff3SAndy Lutomirski 		goto out;
20369c46f6d4SAlex Williamson 
20377e58d5aeSAmos Kong 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR)) {
20387e58d5aeSAmos Kong 		sg_init_one(&sg, addr->sa_data, dev->addr_len);
20397e58d5aeSAmos Kong 		if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2040d24bae32Sstephen hemminger 					  VIRTIO_NET_CTRL_MAC_ADDR_SET, &sg)) {
20417e58d5aeSAmos Kong 			dev_warn(&vdev->dev,
20427e58d5aeSAmos Kong 				 "Failed to set mac address by vq command.\n");
2043e37e2ff3SAndy Lutomirski 			ret = -EINVAL;
2044e37e2ff3SAndy Lutomirski 			goto out;
20457e58d5aeSAmos Kong 		}
20467e93a02fSMichael S. Tsirkin 	} else if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC) &&
20477e93a02fSMichael S. Tsirkin 		   !virtio_has_feature(vdev, VIRTIO_F_VERSION_1)) {
2048855e0c52SRusty Russell 		unsigned int i;
2049855e0c52SRusty Russell 
2050855e0c52SRusty Russell 		/* Naturally, this has an atomicity problem. */
2051855e0c52SRusty Russell 		for (i = 0; i < dev->addr_len; i++)
2052855e0c52SRusty Russell 			virtio_cwrite8(vdev,
2053855e0c52SRusty Russell 				       offsetof(struct virtio_net_config, mac) +
2054855e0c52SRusty Russell 				       i, addr->sa_data[i]);
20557e58d5aeSAmos Kong 	}
20567e58d5aeSAmos Kong 
20577e58d5aeSAmos Kong 	eth_commit_mac_addr_change(dev, p);
2058e37e2ff3SAndy Lutomirski 	ret = 0;
20599c46f6d4SAlex Williamson 
2060e37e2ff3SAndy Lutomirski out:
2061e37e2ff3SAndy Lutomirski 	kfree(addr);
2062e37e2ff3SAndy Lutomirski 	return ret;
20639c46f6d4SAlex Williamson }
20649c46f6d4SAlex Williamson 
2065bc1f4470Sstephen hemminger static void virtnet_stats(struct net_device *dev,
20663fa2a1dfSstephen hemminger 			  struct rtnl_link_stats64 *tot)
20673fa2a1dfSstephen hemminger {
20683fa2a1dfSstephen hemminger 	struct virtnet_info *vi = netdev_priv(dev);
20693fa2a1dfSstephen hemminger 	unsigned int start;
2070d7dfc5cfSToshiaki Makita 	int i;
20713fa2a1dfSstephen hemminger 
2072d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
2073a520794bSTony Lu 		u64 tpackets, tbytes, terrors, rpackets, rbytes, rdrops;
2074d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
2075d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
20763fa2a1dfSstephen hemminger 
20773fa2a1dfSstephen hemminger 		do {
2078068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&sq->stats.syncp);
2079d7dfc5cfSToshiaki Makita 			tpackets = sq->stats.packets;
2080d7dfc5cfSToshiaki Makita 			tbytes   = sq->stats.bytes;
2081a520794bSTony Lu 			terrors  = sq->stats.tx_timeouts;
2082068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
208383a27052SEric Dumazet 
208483a27052SEric Dumazet 		do {
2085068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq->stats.syncp);
2086d46eeeafSJason Wang 			rpackets = rq->stats.packets;
2087d46eeeafSJason Wang 			rbytes   = rq->stats.bytes;
2088d46eeeafSJason Wang 			rdrops   = rq->stats.drops;
2089068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq->stats.syncp, start));
20903fa2a1dfSstephen hemminger 
20913fa2a1dfSstephen hemminger 		tot->rx_packets += rpackets;
20923fa2a1dfSstephen hemminger 		tot->tx_packets += tpackets;
20933fa2a1dfSstephen hemminger 		tot->rx_bytes   += rbytes;
20943fa2a1dfSstephen hemminger 		tot->tx_bytes   += tbytes;
20952c4a2f7dSToshiaki Makita 		tot->rx_dropped += rdrops;
2096a520794bSTony Lu 		tot->tx_errors  += terrors;
20973fa2a1dfSstephen hemminger 	}
20983fa2a1dfSstephen hemminger 
20993fa2a1dfSstephen hemminger 	tot->tx_dropped = dev->stats.tx_dropped;
2100021ac8d3SRick Jones 	tot->tx_fifo_errors = dev->stats.tx_fifo_errors;
21013fa2a1dfSstephen hemminger 	tot->rx_length_errors = dev->stats.rx_length_errors;
21023fa2a1dfSstephen hemminger 	tot->rx_frame_errors = dev->stats.rx_frame_errors;
21033fa2a1dfSstephen hemminger }
21043fa2a1dfSstephen hemminger 
2105586d17c5SJason Wang static void virtnet_ack_link_announce(struct virtnet_info *vi)
2106586d17c5SJason Wang {
2107586d17c5SJason Wang 	rtnl_lock();
2108586d17c5SJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_ANNOUNCE,
2109d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_ANNOUNCE_ACK, NULL))
2110586d17c5SJason Wang 		dev_warn(&vi->dev->dev, "Failed to ack link announce.\n");
2111586d17c5SJason Wang 	rtnl_unlock();
2112586d17c5SJason Wang }
2113586d17c5SJason Wang 
211447315329SJohn Fastabend static int _virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
2115986a4f4dSJason Wang {
2116986a4f4dSJason Wang 	struct scatterlist sg;
2117986a4f4dSJason Wang 	struct net_device *dev = vi->dev;
2118986a4f4dSJason Wang 
2119986a4f4dSJason Wang 	if (!vi->has_cvq || !virtio_has_feature(vi->vdev, VIRTIO_NET_F_MQ))
2120986a4f4dSJason Wang 		return 0;
2121986a4f4dSJason Wang 
212212e57169SMichael S. Tsirkin 	vi->ctrl->mq.virtqueue_pairs = cpu_to_virtio16(vi->vdev, queue_pairs);
212312e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->mq, sizeof(vi->ctrl->mq));
2124986a4f4dSJason Wang 
2125986a4f4dSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
2126d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET, &sg)) {
2127986a4f4dSJason Wang 		dev_warn(&dev->dev, "Fail to set num of queue pairs to %d\n",
2128986a4f4dSJason Wang 			 queue_pairs);
2129986a4f4dSJason Wang 		return -EINVAL;
213055257d72SSasha Levin 	} else {
2131986a4f4dSJason Wang 		vi->curr_queue_pairs = queue_pairs;
213235ed159bSJason Wang 		/* virtnet_open() will refill when device is going to up. */
213335ed159bSJason Wang 		if (dev->flags & IFF_UP)
21349b9cd802SJason Wang 			schedule_delayed_work(&vi->refill, 0);
213555257d72SSasha Levin 	}
2136986a4f4dSJason Wang 
2137986a4f4dSJason Wang 	return 0;
2138986a4f4dSJason Wang }
2139986a4f4dSJason Wang 
214047315329SJohn Fastabend static int virtnet_set_queues(struct virtnet_info *vi, u16 queue_pairs)
214147315329SJohn Fastabend {
214247315329SJohn Fastabend 	int err;
214347315329SJohn Fastabend 
214447315329SJohn Fastabend 	rtnl_lock();
214547315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
214647315329SJohn Fastabend 	rtnl_unlock();
214747315329SJohn Fastabend 	return err;
214847315329SJohn Fastabend }
214947315329SJohn Fastabend 
2150296f96fcSRusty Russell static int virtnet_close(struct net_device *dev)
2151296f96fcSRusty Russell {
2152296f96fcSRusty Russell 	struct virtnet_info *vi = netdev_priv(dev);
2153986a4f4dSJason Wang 	int i;
2154296f96fcSRusty Russell 
21555a159128SJason Wang 	/* Make sure NAPI doesn't schedule refill work */
21565a159128SJason Wang 	disable_delayed_refill(vi);
2157b2baed69SRusty Russell 	/* Make sure refill_work doesn't re-enable napi! */
2158b2baed69SRusty Russell 	cancel_delayed_work_sync(&vi->refill);
2159986a4f4dSJason Wang 
2160b92f1e67SWillem de Bruijn 	for (i = 0; i < vi->max_queue_pairs; i++) {
2161754b8a21SJesper Dangaard Brouer 		xdp_rxq_info_unreg(&vi->rq[i].xdp_rxq);
2162986a4f4dSJason Wang 		napi_disable(&vi->rq[i].napi);
216378a57b48SWillem de Bruijn 		virtnet_napi_tx_disable(&vi->sq[i].napi);
2164b92f1e67SWillem de Bruijn 	}
2165296f96fcSRusty Russell 
2166296f96fcSRusty Russell 	return 0;
2167296f96fcSRusty Russell }
2168296f96fcSRusty Russell 
21692af7698eSAlex Williamson static void virtnet_set_rx_mode(struct net_device *dev)
21702af7698eSAlex Williamson {
21712af7698eSAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
2172f565a7c2SAlex Williamson 	struct scatterlist sg[2];
2173f565a7c2SAlex Williamson 	struct virtio_net_ctrl_mac *mac_data;
2174ccffad25SJiri Pirko 	struct netdev_hw_addr *ha;
217532e7bfc4SJiri Pirko 	int uc_count;
21764cd24eafSJiri Pirko 	int mc_count;
2177f565a7c2SAlex Williamson 	void *buf;
2178f565a7c2SAlex Williamson 	int i;
21792af7698eSAlex Williamson 
2180788a8b6dSstephen hemminger 	/* We can't dynamically set ndo_set_rx_mode, so return gracefully */
21812af7698eSAlex Williamson 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_RX))
21822af7698eSAlex Williamson 		return;
21832af7698eSAlex Williamson 
218412e57169SMichael S. Tsirkin 	vi->ctrl->promisc = ((dev->flags & IFF_PROMISC) != 0);
218512e57169SMichael S. Tsirkin 	vi->ctrl->allmulti = ((dev->flags & IFF_ALLMULTI) != 0);
21862af7698eSAlex Williamson 
218712e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->promisc, sizeof(vi->ctrl->promisc));
21882af7698eSAlex Williamson 
21892af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2190d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_PROMISC, sg))
21912af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable promisc mode.\n",
219212e57169SMichael S. Tsirkin 			 vi->ctrl->promisc ? "en" : "dis");
21932af7698eSAlex Williamson 
219412e57169SMichael S. Tsirkin 	sg_init_one(sg, &vi->ctrl->allmulti, sizeof(vi->ctrl->allmulti));
21952af7698eSAlex Williamson 
21962af7698eSAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_RX,
2197d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_RX_ALLMULTI, sg))
21982af7698eSAlex Williamson 		dev_warn(&dev->dev, "Failed to %sable allmulti mode.\n",
219912e57169SMichael S. Tsirkin 			 vi->ctrl->allmulti ? "en" : "dis");
2200f565a7c2SAlex Williamson 
220132e7bfc4SJiri Pirko 	uc_count = netdev_uc_count(dev);
22024cd24eafSJiri Pirko 	mc_count = netdev_mc_count(dev);
2203f565a7c2SAlex Williamson 	/* MAC filter - use one buffer for both lists */
22044cd24eafSJiri Pirko 	buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
2205f565a7c2SAlex Williamson 		      (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
22064cd24eafSJiri Pirko 	mac_data = buf;
2207e68ed8f0SJoe Perches 	if (!buf)
2208f565a7c2SAlex Williamson 		return;
2209f565a7c2SAlex Williamson 
221023e258e1SAlex Williamson 	sg_init_table(sg, 2);
221123e258e1SAlex Williamson 
2212f565a7c2SAlex Williamson 	/* Store the unicast list and count in the front of the buffer */
2213fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, uc_count);
2214ccffad25SJiri Pirko 	i = 0;
221532e7bfc4SJiri Pirko 	netdev_for_each_uc_addr(ha, dev)
2216ccffad25SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2217f565a7c2SAlex Williamson 
2218f565a7c2SAlex Williamson 	sg_set_buf(&sg[0], mac_data,
221932e7bfc4SJiri Pirko 		   sizeof(mac_data->entries) + (uc_count * ETH_ALEN));
2220f565a7c2SAlex Williamson 
2221f565a7c2SAlex Williamson 	/* multicast list and count fill the end */
222232e7bfc4SJiri Pirko 	mac_data = (void *)&mac_data->macs[uc_count][0];
2223f565a7c2SAlex Williamson 
2224fdd819b2SMichael S. Tsirkin 	mac_data->entries = cpu_to_virtio32(vi->vdev, mc_count);
2225567ec874SJiri Pirko 	i = 0;
222622bedad3SJiri Pirko 	netdev_for_each_mc_addr(ha, dev)
222722bedad3SJiri Pirko 		memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
2228f565a7c2SAlex Williamson 
2229f565a7c2SAlex Williamson 	sg_set_buf(&sg[1], mac_data,
22304cd24eafSJiri Pirko 		   sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
2231f565a7c2SAlex Williamson 
2232f565a7c2SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
2233d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_MAC_TABLE_SET, sg))
223499e872aeSThomas Huth 		dev_warn(&dev->dev, "Failed to set MAC filter table.\n");
2235f565a7c2SAlex Williamson 
2236f565a7c2SAlex Williamson 	kfree(buf);
22372af7698eSAlex Williamson }
22382af7698eSAlex Williamson 
223980d5c368SPatrick McHardy static int virtnet_vlan_rx_add_vid(struct net_device *dev,
224080d5c368SPatrick McHardy 				   __be16 proto, u16 vid)
22410bde9569SAlex Williamson {
22420bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
22430bde9569SAlex Williamson 	struct scatterlist sg;
22440bde9569SAlex Williamson 
2245d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
224612e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
22470bde9569SAlex Williamson 
22480bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2249d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_ADD, &sg))
22500bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to add VLAN ID %d.\n", vid);
22518e586137SJiri Pirko 	return 0;
22520bde9569SAlex Williamson }
22530bde9569SAlex Williamson 
225480d5c368SPatrick McHardy static int virtnet_vlan_rx_kill_vid(struct net_device *dev,
225580d5c368SPatrick McHardy 				    __be16 proto, u16 vid)
22560bde9569SAlex Williamson {
22570bde9569SAlex Williamson 	struct virtnet_info *vi = netdev_priv(dev);
22580bde9569SAlex Williamson 	struct scatterlist sg;
22590bde9569SAlex Williamson 
2260d7fad4c8SMichael S. Tsirkin 	vi->ctrl->vid = cpu_to_virtio16(vi->vdev, vid);
226112e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->vid, sizeof(vi->ctrl->vid));
22620bde9569SAlex Williamson 
22630bde9569SAlex Williamson 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_VLAN,
2264d24bae32Sstephen hemminger 				  VIRTIO_NET_CTRL_VLAN_DEL, &sg))
22650bde9569SAlex Williamson 		dev_warn(&dev->dev, "Failed to kill VLAN ID %d.\n", vid);
22668e586137SJiri Pirko 	return 0;
22670bde9569SAlex Williamson }
22680bde9569SAlex Williamson 
2269310974faSPeter Xu static void virtnet_clean_affinity(struct virtnet_info *vi)
2270986a4f4dSJason Wang {
2271986a4f4dSJason Wang 	int i;
22728898c21cSWanlong Gao 
22738898c21cSWanlong Gao 	if (vi->affinity_hint_set) {
22748898c21cSWanlong Gao 		for (i = 0; i < vi->max_queue_pairs; i++) {
227519e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->rq[i].vq, NULL);
227619e226e8SCaleb Raitto 			virtqueue_set_affinity(vi->sq[i].vq, NULL);
22778898c21cSWanlong Gao 		}
22788898c21cSWanlong Gao 
22798898c21cSWanlong Gao 		vi->affinity_hint_set = false;
22808898c21cSWanlong Gao 	}
22818898c21cSWanlong Gao }
22828898c21cSWanlong Gao 
22838898c21cSWanlong Gao static void virtnet_set_affinity(struct virtnet_info *vi)
2284986a4f4dSJason Wang {
22852ca653d6SCaleb Raitto 	cpumask_var_t mask;
22862ca653d6SCaleb Raitto 	int stragglers;
22872ca653d6SCaleb Raitto 	int group_size;
22882ca653d6SCaleb Raitto 	int i, j, cpu;
22892ca653d6SCaleb Raitto 	int num_cpu;
22902ca653d6SCaleb Raitto 	int stride;
2291986a4f4dSJason Wang 
22922ca653d6SCaleb Raitto 	if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) {
2293310974faSPeter Xu 		virtnet_clean_affinity(vi);
2294986a4f4dSJason Wang 		return;
2295986a4f4dSJason Wang 	}
2296986a4f4dSJason Wang 
22972ca653d6SCaleb Raitto 	num_cpu = num_online_cpus();
22982ca653d6SCaleb Raitto 	stride = max_t(int, num_cpu / vi->curr_queue_pairs, 1);
22992ca653d6SCaleb Raitto 	stragglers = num_cpu >= vi->curr_queue_pairs ?
23002ca653d6SCaleb Raitto 			num_cpu % vi->curr_queue_pairs :
23012ca653d6SCaleb Raitto 			0;
23029b51d9d8SYury Norov 	cpu = cpumask_first(cpu_online_mask);
23034d99f660SAndrei Vagin 
23042ca653d6SCaleb Raitto 	for (i = 0; i < vi->curr_queue_pairs; i++) {
23052ca653d6SCaleb Raitto 		group_size = stride + (i < stragglers ? 1 : 0);
23062ca653d6SCaleb Raitto 
23072ca653d6SCaleb Raitto 		for (j = 0; j < group_size; j++) {
23082ca653d6SCaleb Raitto 			cpumask_set_cpu(cpu, mask);
23092ca653d6SCaleb Raitto 			cpu = cpumask_next_wrap(cpu, cpu_online_mask,
23102ca653d6SCaleb Raitto 						nr_cpu_ids, false);
23112ca653d6SCaleb Raitto 		}
23122ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->rq[i].vq, mask);
23132ca653d6SCaleb Raitto 		virtqueue_set_affinity(vi->sq[i].vq, mask);
2314044ab86dSAntoine Tenart 		__netif_set_xps_queue(vi->dev, cpumask_bits(mask), i, XPS_CPUS);
23152ca653d6SCaleb Raitto 		cpumask_clear(mask);
2316986a4f4dSJason Wang 	}
2317986a4f4dSJason Wang 
2318986a4f4dSJason Wang 	vi->affinity_hint_set = true;
23192ca653d6SCaleb Raitto 	free_cpumask_var(mask);
232047be2479SWanlong Gao }
2321986a4f4dSJason Wang 
23228017c279SSebastian Andrzej Siewior static int virtnet_cpu_online(unsigned int cpu, struct hlist_node *node)
23238de4b2f3SWanlong Gao {
23248017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
23258017c279SSebastian Andrzej Siewior 						   node);
23268de4b2f3SWanlong Gao 	virtnet_set_affinity(vi);
23278017c279SSebastian Andrzej Siewior 	return 0;
23288de4b2f3SWanlong Gao }
23293ab098dfSJason Wang 
23308017c279SSebastian Andrzej Siewior static int virtnet_cpu_dead(unsigned int cpu, struct hlist_node *node)
23318017c279SSebastian Andrzej Siewior {
23328017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
23338017c279SSebastian Andrzej Siewior 						   node_dead);
23348017c279SSebastian Andrzej Siewior 	virtnet_set_affinity(vi);
23358017c279SSebastian Andrzej Siewior 	return 0;
23368017c279SSebastian Andrzej Siewior }
23378017c279SSebastian Andrzej Siewior 
23388017c279SSebastian Andrzej Siewior static int virtnet_cpu_down_prep(unsigned int cpu, struct hlist_node *node)
23398017c279SSebastian Andrzej Siewior {
23408017c279SSebastian Andrzej Siewior 	struct virtnet_info *vi = hlist_entry_safe(node, struct virtnet_info,
23418017c279SSebastian Andrzej Siewior 						   node);
23428017c279SSebastian Andrzej Siewior 
2343310974faSPeter Xu 	virtnet_clean_affinity(vi);
23448017c279SSebastian Andrzej Siewior 	return 0;
23458017c279SSebastian Andrzej Siewior }
23468017c279SSebastian Andrzej Siewior 
23478017c279SSebastian Andrzej Siewior static enum cpuhp_state virtionet_online;
23488017c279SSebastian Andrzej Siewior 
23498017c279SSebastian Andrzej Siewior static int virtnet_cpu_notif_add(struct virtnet_info *vi)
23508017c279SSebastian Andrzej Siewior {
23518017c279SSebastian Andrzej Siewior 	int ret;
23528017c279SSebastian Andrzej Siewior 
23538017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(virtionet_online, &vi->node);
23548017c279SSebastian Andrzej Siewior 	if (ret)
23558017c279SSebastian Andrzej Siewior 		return ret;
23568017c279SSebastian Andrzej Siewior 	ret = cpuhp_state_add_instance_nocalls(CPUHP_VIRT_NET_DEAD,
23578017c279SSebastian Andrzej Siewior 					       &vi->node_dead);
23588017c279SSebastian Andrzej Siewior 	if (!ret)
23598017c279SSebastian Andrzej Siewior 		return ret;
23608017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
23618017c279SSebastian Andrzej Siewior 	return ret;
23628017c279SSebastian Andrzej Siewior }
23638017c279SSebastian Andrzej Siewior 
23648017c279SSebastian Andrzej Siewior static void virtnet_cpu_notif_remove(struct virtnet_info *vi)
23658017c279SSebastian Andrzej Siewior {
23668017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(virtionet_online, &vi->node);
23678017c279SSebastian Andrzej Siewior 	cpuhp_state_remove_instance_nocalls(CPUHP_VIRT_NET_DEAD,
23688017c279SSebastian Andrzej Siewior 					    &vi->node_dead);
2369a9ea3fc6SHerbert Xu }
2370a9ea3fc6SHerbert Xu 
23718f9f4668SRick Jones static void virtnet_get_ringparam(struct net_device *dev,
237274624944SHao Chen 				  struct ethtool_ringparam *ring,
237374624944SHao Chen 				  struct kernel_ethtool_ringparam *kernel_ring,
237474624944SHao Chen 				  struct netlink_ext_ack *extack)
23758f9f4668SRick Jones {
23768f9f4668SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
23778f9f4668SRick Jones 
23788597b5ddSXuan Zhuo 	ring->rx_max_pending = vi->rq[0].vq->num_max;
23798597b5ddSXuan Zhuo 	ring->tx_max_pending = vi->sq[0].vq->num_max;
23808597b5ddSXuan Zhuo 	ring->rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
23818597b5ddSXuan Zhuo 	ring->tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
23828f9f4668SRick Jones }
23838f9f4668SRick Jones 
2384a335b33fSXuan Zhuo static int virtnet_set_ringparam(struct net_device *dev,
2385a335b33fSXuan Zhuo 				 struct ethtool_ringparam *ring,
2386a335b33fSXuan Zhuo 				 struct kernel_ethtool_ringparam *kernel_ring,
2387a335b33fSXuan Zhuo 				 struct netlink_ext_ack *extack)
2388a335b33fSXuan Zhuo {
2389a335b33fSXuan Zhuo 	struct virtnet_info *vi = netdev_priv(dev);
2390a335b33fSXuan Zhuo 	u32 rx_pending, tx_pending;
2391a335b33fSXuan Zhuo 	struct receive_queue *rq;
2392a335b33fSXuan Zhuo 	struct send_queue *sq;
2393a335b33fSXuan Zhuo 	int i, err;
2394a335b33fSXuan Zhuo 
2395a335b33fSXuan Zhuo 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
2396a335b33fSXuan Zhuo 		return -EINVAL;
2397a335b33fSXuan Zhuo 
2398a335b33fSXuan Zhuo 	rx_pending = virtqueue_get_vring_size(vi->rq[0].vq);
2399a335b33fSXuan Zhuo 	tx_pending = virtqueue_get_vring_size(vi->sq[0].vq);
2400a335b33fSXuan Zhuo 
2401a335b33fSXuan Zhuo 	if (ring->rx_pending == rx_pending &&
2402a335b33fSXuan Zhuo 	    ring->tx_pending == tx_pending)
2403a335b33fSXuan Zhuo 		return 0;
2404a335b33fSXuan Zhuo 
2405a335b33fSXuan Zhuo 	if (ring->rx_pending > vi->rq[0].vq->num_max)
2406a335b33fSXuan Zhuo 		return -EINVAL;
2407a335b33fSXuan Zhuo 
2408a335b33fSXuan Zhuo 	if (ring->tx_pending > vi->sq[0].vq->num_max)
2409a335b33fSXuan Zhuo 		return -EINVAL;
2410a335b33fSXuan Zhuo 
2411a335b33fSXuan Zhuo 	for (i = 0; i < vi->max_queue_pairs; i++) {
2412a335b33fSXuan Zhuo 		rq = vi->rq + i;
2413a335b33fSXuan Zhuo 		sq = vi->sq + i;
2414a335b33fSXuan Zhuo 
2415a335b33fSXuan Zhuo 		if (ring->tx_pending != tx_pending) {
2416a335b33fSXuan Zhuo 			err = virtnet_tx_resize(vi, sq, ring->tx_pending);
2417a335b33fSXuan Zhuo 			if (err)
2418a335b33fSXuan Zhuo 				return err;
2419a335b33fSXuan Zhuo 		}
2420a335b33fSXuan Zhuo 
2421a335b33fSXuan Zhuo 		if (ring->rx_pending != rx_pending) {
2422a335b33fSXuan Zhuo 			err = virtnet_rx_resize(vi, rq, ring->rx_pending);
2423a335b33fSXuan Zhuo 			if (err)
2424a335b33fSXuan Zhuo 				return err;
2425a335b33fSXuan Zhuo 		}
2426a335b33fSXuan Zhuo 	}
2427a335b33fSXuan Zhuo 
2428a335b33fSXuan Zhuo 	return 0;
2429a9ea3fc6SHerbert Xu }
2430a9ea3fc6SHerbert Xu 
2431c7114b12SAndrew Melnychenko static bool virtnet_commit_rss_command(struct virtnet_info *vi)
2432c7114b12SAndrew Melnychenko {
2433c7114b12SAndrew Melnychenko 	struct net_device *dev = vi->dev;
2434c7114b12SAndrew Melnychenko 	struct scatterlist sgs[4];
2435c7114b12SAndrew Melnychenko 	unsigned int sg_buf_size;
2436c7114b12SAndrew Melnychenko 
2437c7114b12SAndrew Melnychenko 	/* prepare sgs */
2438c7114b12SAndrew Melnychenko 	sg_init_table(sgs, 4);
2439c7114b12SAndrew Melnychenko 
2440c7114b12SAndrew Melnychenko 	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, indirection_table);
2441c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[0], &vi->ctrl->rss, sg_buf_size);
2442c7114b12SAndrew Melnychenko 
2443c7114b12SAndrew Melnychenko 	sg_buf_size = sizeof(uint16_t) * (vi->ctrl->rss.indirection_table_mask + 1);
2444c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[1], vi->ctrl->rss.indirection_table, sg_buf_size);
2445c7114b12SAndrew Melnychenko 
2446c7114b12SAndrew Melnychenko 	sg_buf_size = offsetof(struct virtio_net_ctrl_rss, key)
2447c7114b12SAndrew Melnychenko 			- offsetof(struct virtio_net_ctrl_rss, max_tx_vq);
2448c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[2], &vi->ctrl->rss.max_tx_vq, sg_buf_size);
2449c7114b12SAndrew Melnychenko 
2450c7114b12SAndrew Melnychenko 	sg_buf_size = vi->rss_key_size;
2451c7114b12SAndrew Melnychenko 	sg_set_buf(&sgs[3], vi->ctrl->rss.key, sg_buf_size);
2452c7114b12SAndrew Melnychenko 
2453c7114b12SAndrew Melnychenko 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MQ,
245491f41f01SAndrew Melnychenko 				  vi->has_rss ? VIRTIO_NET_CTRL_MQ_RSS_CONFIG
245591f41f01SAndrew Melnychenko 				  : VIRTIO_NET_CTRL_MQ_HASH_CONFIG, sgs)) {
2456c7114b12SAndrew Melnychenko 		dev_warn(&dev->dev, "VIRTIONET issue with committing RSS sgs\n");
2457c7114b12SAndrew Melnychenko 		return false;
2458c7114b12SAndrew Melnychenko 	}
2459c7114b12SAndrew Melnychenko 	return true;
2460c7114b12SAndrew Melnychenko }
2461c7114b12SAndrew Melnychenko 
2462c7114b12SAndrew Melnychenko static void virtnet_init_default_rss(struct virtnet_info *vi)
2463c7114b12SAndrew Melnychenko {
2464c7114b12SAndrew Melnychenko 	u32 indir_val = 0;
2465c7114b12SAndrew Melnychenko 	int i = 0;
2466c7114b12SAndrew Melnychenko 
2467c7114b12SAndrew Melnychenko 	vi->ctrl->rss.hash_types = vi->rss_hash_types_supported;
2468c1170820SAndrew Melnychenko 	vi->rss_hash_types_saved = vi->rss_hash_types_supported;
2469c7114b12SAndrew Melnychenko 	vi->ctrl->rss.indirection_table_mask = vi->rss_indir_table_size
2470c7114b12SAndrew Melnychenko 						? vi->rss_indir_table_size - 1 : 0;
2471c7114b12SAndrew Melnychenko 	vi->ctrl->rss.unclassified_queue = 0;
2472c7114b12SAndrew Melnychenko 
2473c7114b12SAndrew Melnychenko 	for (; i < vi->rss_indir_table_size; ++i) {
2474c7114b12SAndrew Melnychenko 		indir_val = ethtool_rxfh_indir_default(i, vi->curr_queue_pairs);
2475c7114b12SAndrew Melnychenko 		vi->ctrl->rss.indirection_table[i] = indir_val;
2476c7114b12SAndrew Melnychenko 	}
2477c7114b12SAndrew Melnychenko 
2478c7114b12SAndrew Melnychenko 	vi->ctrl->rss.max_tx_vq = vi->curr_queue_pairs;
2479c7114b12SAndrew Melnychenko 	vi->ctrl->rss.hash_key_length = vi->rss_key_size;
2480c7114b12SAndrew Melnychenko 
2481c7114b12SAndrew Melnychenko 	netdev_rss_key_fill(vi->ctrl->rss.key, vi->rss_key_size);
2482c7114b12SAndrew Melnychenko }
2483c7114b12SAndrew Melnychenko 
2484c1170820SAndrew Melnychenko static void virtnet_get_hashflow(const struct virtnet_info *vi, struct ethtool_rxnfc *info)
2485c1170820SAndrew Melnychenko {
2486c1170820SAndrew Melnychenko 	info->data = 0;
2487c1170820SAndrew Melnychenko 	switch (info->flow_type) {
2488c1170820SAndrew Melnychenko 	case TCP_V4_FLOW:
2489c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv4) {
2490c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2491c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2492c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2493c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2494c1170820SAndrew Melnychenko 		}
2495c1170820SAndrew Melnychenko 		break;
2496c1170820SAndrew Melnychenko 	case TCP_V6_FLOW:
2497c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_TCPv6) {
2498c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2499c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2500c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2501c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2502c1170820SAndrew Melnychenko 		}
2503c1170820SAndrew Melnychenko 		break;
2504c1170820SAndrew Melnychenko 	case UDP_V4_FLOW:
2505c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv4) {
2506c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2507c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2508c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4) {
2509c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2510c1170820SAndrew Melnychenko 		}
2511c1170820SAndrew Melnychenko 		break;
2512c1170820SAndrew Melnychenko 	case UDP_V6_FLOW:
2513c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_UDPv6) {
2514c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST |
2515c1170820SAndrew Melnychenko 						 RXH_L4_B_0_1 | RXH_L4_B_2_3;
2516c1170820SAndrew Melnychenko 		} else if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6) {
2517c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2518c1170820SAndrew Melnychenko 		}
2519c1170820SAndrew Melnychenko 		break;
2520c1170820SAndrew Melnychenko 	case IPV4_FLOW:
2521c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv4)
2522c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2523c1170820SAndrew Melnychenko 
2524c1170820SAndrew Melnychenko 		break;
2525c1170820SAndrew Melnychenko 	case IPV6_FLOW:
2526c1170820SAndrew Melnychenko 		if (vi->rss_hash_types_saved & VIRTIO_NET_RSS_HASH_TYPE_IPv6)
2527c1170820SAndrew Melnychenko 			info->data = RXH_IP_SRC | RXH_IP_DST;
2528c1170820SAndrew Melnychenko 
2529c1170820SAndrew Melnychenko 		break;
2530c1170820SAndrew Melnychenko 	default:
2531c1170820SAndrew Melnychenko 		info->data = 0;
2532c1170820SAndrew Melnychenko 		break;
2533c1170820SAndrew Melnychenko 	}
2534c1170820SAndrew Melnychenko }
2535c1170820SAndrew Melnychenko 
2536c1170820SAndrew Melnychenko static bool virtnet_set_hashflow(struct virtnet_info *vi, struct ethtool_rxnfc *info)
2537c1170820SAndrew Melnychenko {
2538c1170820SAndrew Melnychenko 	u32 new_hashtypes = vi->rss_hash_types_saved;
2539c1170820SAndrew Melnychenko 	bool is_disable = info->data & RXH_DISCARD;
2540c1170820SAndrew Melnychenko 	bool is_l4 = info->data == (RXH_IP_SRC | RXH_IP_DST | RXH_L4_B_0_1 | RXH_L4_B_2_3);
2541c1170820SAndrew Melnychenko 
2542c1170820SAndrew Melnychenko 	/* supports only 'sd', 'sdfn' and 'r' */
2543c1170820SAndrew Melnychenko 	if (!((info->data == (RXH_IP_SRC | RXH_IP_DST)) | is_l4 | is_disable))
2544c1170820SAndrew Melnychenko 		return false;
2545c1170820SAndrew Melnychenko 
2546c1170820SAndrew Melnychenko 	switch (info->flow_type) {
2547c1170820SAndrew Melnychenko 	case TCP_V4_FLOW:
2548c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_TCPv4);
2549c1170820SAndrew Melnychenko 		if (!is_disable)
2550c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2551c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv4 : 0);
2552c1170820SAndrew Melnychenko 		break;
2553c1170820SAndrew Melnychenko 	case UDP_V4_FLOW:
2554c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv4 | VIRTIO_NET_RSS_HASH_TYPE_UDPv4);
2555c1170820SAndrew Melnychenko 		if (!is_disable)
2556c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv4
2557c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv4 : 0);
2558c1170820SAndrew Melnychenko 		break;
2559c1170820SAndrew Melnychenko 	case IPV4_FLOW:
2560c1170820SAndrew Melnychenko 		new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2561c1170820SAndrew Melnychenko 		if (!is_disable)
2562c1170820SAndrew Melnychenko 			new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv4;
2563c1170820SAndrew Melnychenko 		break;
2564c1170820SAndrew Melnychenko 	case TCP_V6_FLOW:
2565c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_TCPv6);
2566c1170820SAndrew Melnychenko 		if (!is_disable)
2567c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2568c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_TCPv6 : 0);
2569c1170820SAndrew Melnychenko 		break;
2570c1170820SAndrew Melnychenko 	case UDP_V6_FLOW:
2571c1170820SAndrew Melnychenko 		new_hashtypes &= ~(VIRTIO_NET_RSS_HASH_TYPE_IPv6 | VIRTIO_NET_RSS_HASH_TYPE_UDPv6);
2572c1170820SAndrew Melnychenko 		if (!is_disable)
2573c1170820SAndrew Melnychenko 			new_hashtypes |= VIRTIO_NET_RSS_HASH_TYPE_IPv6
2574c1170820SAndrew Melnychenko 				| (is_l4 ? VIRTIO_NET_RSS_HASH_TYPE_UDPv6 : 0);
2575c1170820SAndrew Melnychenko 		break;
2576c1170820SAndrew Melnychenko 	case IPV6_FLOW:
2577c1170820SAndrew Melnychenko 		new_hashtypes &= ~VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2578c1170820SAndrew Melnychenko 		if (!is_disable)
2579c1170820SAndrew Melnychenko 			new_hashtypes = VIRTIO_NET_RSS_HASH_TYPE_IPv6;
2580c1170820SAndrew Melnychenko 		break;
2581c1170820SAndrew Melnychenko 	default:
2582c1170820SAndrew Melnychenko 		/* unsupported flow */
2583c1170820SAndrew Melnychenko 		return false;
2584c1170820SAndrew Melnychenko 	}
2585c1170820SAndrew Melnychenko 
2586c1170820SAndrew Melnychenko 	/* if unsupported hashtype was set */
2587c1170820SAndrew Melnychenko 	if (new_hashtypes != (new_hashtypes & vi->rss_hash_types_supported))
2588c1170820SAndrew Melnychenko 		return false;
2589c1170820SAndrew Melnychenko 
2590c1170820SAndrew Melnychenko 	if (new_hashtypes != vi->rss_hash_types_saved) {
2591c1170820SAndrew Melnychenko 		vi->rss_hash_types_saved = new_hashtypes;
2592c1170820SAndrew Melnychenko 		vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
2593c1170820SAndrew Melnychenko 		if (vi->dev->features & NETIF_F_RXHASH)
2594c1170820SAndrew Melnychenko 			return virtnet_commit_rss_command(vi);
2595c1170820SAndrew Melnychenko 	}
2596c1170820SAndrew Melnychenko 
2597c1170820SAndrew Melnychenko 	return true;
2598c1170820SAndrew Melnychenko }
259966846048SRick Jones 
260066846048SRick Jones static void virtnet_get_drvinfo(struct net_device *dev,
260166846048SRick Jones 				struct ethtool_drvinfo *info)
260266846048SRick Jones {
260366846048SRick Jones 	struct virtnet_info *vi = netdev_priv(dev);
260466846048SRick Jones 	struct virtio_device *vdev = vi->vdev;
260566846048SRick Jones 
2606fb3ceec1SWolfram Sang 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
2607fb3ceec1SWolfram Sang 	strscpy(info->version, VIRTNET_DRIVER_VERSION, sizeof(info->version));
2608fb3ceec1SWolfram Sang 	strscpy(info->bus_info, virtio_bus_name(vdev), sizeof(info->bus_info));
260966846048SRick Jones 
261066846048SRick Jones }
261166846048SRick Jones 
2612d73bcd2cSJason Wang /* TODO: Eliminate OOO packets during switching */
2613d73bcd2cSJason Wang static int virtnet_set_channels(struct net_device *dev,
2614d73bcd2cSJason Wang 				struct ethtool_channels *channels)
2615d73bcd2cSJason Wang {
2616d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2617d73bcd2cSJason Wang 	u16 queue_pairs = channels->combined_count;
2618d73bcd2cSJason Wang 	int err;
2619d73bcd2cSJason Wang 
2620d73bcd2cSJason Wang 	/* We don't support separate rx/tx channels.
2621d73bcd2cSJason Wang 	 * We don't allow setting 'other' channels.
2622d73bcd2cSJason Wang 	 */
2623d73bcd2cSJason Wang 	if (channels->rx_count || channels->tx_count || channels->other_count)
2624d73bcd2cSJason Wang 		return -EINVAL;
2625d73bcd2cSJason Wang 
2626c18e9cd6SAmos Kong 	if (queue_pairs > vi->max_queue_pairs || queue_pairs == 0)
2627d73bcd2cSJason Wang 		return -EINVAL;
2628d73bcd2cSJason Wang 
2629f600b690SJohn Fastabend 	/* For now we don't support modifying channels while XDP is loaded
2630f600b690SJohn Fastabend 	 * also when XDP is loaded all RX queues have XDP programs so we only
2631f600b690SJohn Fastabend 	 * need to check a single RX queue.
2632f600b690SJohn Fastabend 	 */
2633f600b690SJohn Fastabend 	if (vi->rq[0].xdp_prog)
2634f600b690SJohn Fastabend 		return -EINVAL;
2635f600b690SJohn Fastabend 
2636a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_lock();
263747315329SJohn Fastabend 	err = _virtnet_set_queues(vi, queue_pairs);
2638de33212fSJeff Dike 	if (err) {
2639a0d1d0f4SSebastian Andrzej Siewior 		cpus_read_unlock();
2640de33212fSJeff Dike 		goto err;
2641d73bcd2cSJason Wang 	}
2642de33212fSJeff Dike 	virtnet_set_affinity(vi);
2643a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_unlock();
2644d73bcd2cSJason Wang 
2645de33212fSJeff Dike 	netif_set_real_num_tx_queues(dev, queue_pairs);
2646de33212fSJeff Dike 	netif_set_real_num_rx_queues(dev, queue_pairs);
2647de33212fSJeff Dike  err:
2648d73bcd2cSJason Wang 	return err;
2649d73bcd2cSJason Wang }
2650d73bcd2cSJason Wang 
2651d7dfc5cfSToshiaki Makita static void virtnet_get_strings(struct net_device *dev, u32 stringset, u8 *data)
2652d7dfc5cfSToshiaki Makita {
2653d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2654d7dfc5cfSToshiaki Makita 	unsigned int i, j;
2655d7a9a01bSAlexander Duyck 	u8 *p = data;
2656d7dfc5cfSToshiaki Makita 
2657d7dfc5cfSToshiaki Makita 	switch (stringset) {
2658d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
2659d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2660d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++)
2661d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "rx_queue_%u_%s", i,
2662d7a9a01bSAlexander Duyck 						virtnet_rq_stats_desc[j].desc);
2663d7dfc5cfSToshiaki Makita 		}
2664d7dfc5cfSToshiaki Makita 
2665d7dfc5cfSToshiaki Makita 		for (i = 0; i < vi->curr_queue_pairs; i++) {
2666d7a9a01bSAlexander Duyck 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++)
2667d7a9a01bSAlexander Duyck 				ethtool_sprintf(&p, "tx_queue_%u_%s", i,
2668d7a9a01bSAlexander Duyck 						virtnet_sq_stats_desc[j].desc);
2669d7dfc5cfSToshiaki Makita 		}
2670d7dfc5cfSToshiaki Makita 		break;
2671d7dfc5cfSToshiaki Makita 	}
2672d7dfc5cfSToshiaki Makita }
2673d7dfc5cfSToshiaki Makita 
2674d7dfc5cfSToshiaki Makita static int virtnet_get_sset_count(struct net_device *dev, int sset)
2675d7dfc5cfSToshiaki Makita {
2676d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2677d7dfc5cfSToshiaki Makita 
2678d7dfc5cfSToshiaki Makita 	switch (sset) {
2679d7dfc5cfSToshiaki Makita 	case ETH_SS_STATS:
2680d7dfc5cfSToshiaki Makita 		return vi->curr_queue_pairs * (VIRTNET_RQ_STATS_LEN +
2681d7dfc5cfSToshiaki Makita 					       VIRTNET_SQ_STATS_LEN);
2682d7dfc5cfSToshiaki Makita 	default:
2683d7dfc5cfSToshiaki Makita 		return -EOPNOTSUPP;
2684d7dfc5cfSToshiaki Makita 	}
2685d7dfc5cfSToshiaki Makita }
2686d7dfc5cfSToshiaki Makita 
2687d7dfc5cfSToshiaki Makita static void virtnet_get_ethtool_stats(struct net_device *dev,
2688d7dfc5cfSToshiaki Makita 				      struct ethtool_stats *stats, u64 *data)
2689d7dfc5cfSToshiaki Makita {
2690d7dfc5cfSToshiaki Makita 	struct virtnet_info *vi = netdev_priv(dev);
2691d7dfc5cfSToshiaki Makita 	unsigned int idx = 0, start, i, j;
2692d7dfc5cfSToshiaki Makita 	const u8 *stats_base;
2693d7dfc5cfSToshiaki Makita 	size_t offset;
2694d7dfc5cfSToshiaki Makita 
2695d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2696d7dfc5cfSToshiaki Makita 		struct receive_queue *rq = &vi->rq[i];
2697d7dfc5cfSToshiaki Makita 
2698d46eeeafSJason Wang 		stats_base = (u8 *)&rq->stats;
2699d7dfc5cfSToshiaki Makita 		do {
2700068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&rq->stats.syncp);
2701d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_RQ_STATS_LEN; j++) {
2702d7dfc5cfSToshiaki Makita 				offset = virtnet_rq_stats_desc[j].offset;
2703d7dfc5cfSToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
2704d7dfc5cfSToshiaki Makita 			}
2705068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&rq->stats.syncp, start));
2706d7dfc5cfSToshiaki Makita 		idx += VIRTNET_RQ_STATS_LEN;
2707d7dfc5cfSToshiaki Makita 	}
2708d7dfc5cfSToshiaki Makita 
2709d7dfc5cfSToshiaki Makita 	for (i = 0; i < vi->curr_queue_pairs; i++) {
2710d7dfc5cfSToshiaki Makita 		struct send_queue *sq = &vi->sq[i];
2711d7dfc5cfSToshiaki Makita 
2712d7dfc5cfSToshiaki Makita 		stats_base = (u8 *)&sq->stats;
2713d7dfc5cfSToshiaki Makita 		do {
2714068c38adSThomas Gleixner 			start = u64_stats_fetch_begin(&sq->stats.syncp);
2715d7dfc5cfSToshiaki Makita 			for (j = 0; j < VIRTNET_SQ_STATS_LEN; j++) {
2716d7dfc5cfSToshiaki Makita 				offset = virtnet_sq_stats_desc[j].offset;
2717d7dfc5cfSToshiaki Makita 				data[idx + j] = *(u64 *)(stats_base + offset);
2718d7dfc5cfSToshiaki Makita 			}
2719068c38adSThomas Gleixner 		} while (u64_stats_fetch_retry(&sq->stats.syncp, start));
2720d7dfc5cfSToshiaki Makita 		idx += VIRTNET_SQ_STATS_LEN;
2721d7dfc5cfSToshiaki Makita 	}
2722d7dfc5cfSToshiaki Makita }
2723d7dfc5cfSToshiaki Makita 
2724d73bcd2cSJason Wang static void virtnet_get_channels(struct net_device *dev,
2725d73bcd2cSJason Wang 				 struct ethtool_channels *channels)
2726d73bcd2cSJason Wang {
2727d73bcd2cSJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2728d73bcd2cSJason Wang 
2729d73bcd2cSJason Wang 	channels->combined_count = vi->curr_queue_pairs;
2730d73bcd2cSJason Wang 	channels->max_combined = vi->max_queue_pairs;
2731d73bcd2cSJason Wang 	channels->max_other = 0;
2732d73bcd2cSJason Wang 	channels->rx_count = 0;
2733d73bcd2cSJason Wang 	channels->tx_count = 0;
2734d73bcd2cSJason Wang 	channels->other_count = 0;
2735d73bcd2cSJason Wang }
2736d73bcd2cSJason Wang 
2737ebb6b4b1SPhilippe Reynes static int virtnet_set_link_ksettings(struct net_device *dev,
2738ebb6b4b1SPhilippe Reynes 				      const struct ethtool_link_ksettings *cmd)
273916032be5SNikolay Aleksandrov {
274016032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
274116032be5SNikolay Aleksandrov 
27429aedc6e2SCris Forno 	return ethtool_virtdev_set_link_ksettings(dev, cmd,
27439aedc6e2SCris Forno 						  &vi->speed, &vi->duplex);
274416032be5SNikolay Aleksandrov }
274516032be5SNikolay Aleksandrov 
2746ebb6b4b1SPhilippe Reynes static int virtnet_get_link_ksettings(struct net_device *dev,
2747ebb6b4b1SPhilippe Reynes 				      struct ethtool_link_ksettings *cmd)
274816032be5SNikolay Aleksandrov {
274916032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
275016032be5SNikolay Aleksandrov 
2751ebb6b4b1SPhilippe Reynes 	cmd->base.speed = vi->speed;
2752ebb6b4b1SPhilippe Reynes 	cmd->base.duplex = vi->duplex;
2753ebb6b4b1SPhilippe Reynes 	cmd->base.port = PORT_OTHER;
275416032be5SNikolay Aleksandrov 
275516032be5SNikolay Aleksandrov 	return 0;
275616032be5SNikolay Aleksandrov }
275716032be5SNikolay Aleksandrov 
2758699b045aSAlvaro Karsz static int virtnet_send_notf_coal_cmds(struct virtnet_info *vi,
2759699b045aSAlvaro Karsz 				       struct ethtool_coalesce *ec)
2760699b045aSAlvaro Karsz {
2761699b045aSAlvaro Karsz 	struct scatterlist sgs_tx, sgs_rx;
2762699b045aSAlvaro Karsz 	struct virtio_net_ctrl_coal_tx coal_tx;
2763699b045aSAlvaro Karsz 	struct virtio_net_ctrl_coal_rx coal_rx;
2764699b045aSAlvaro Karsz 
2765699b045aSAlvaro Karsz 	coal_tx.tx_usecs = cpu_to_le32(ec->tx_coalesce_usecs);
2766699b045aSAlvaro Karsz 	coal_tx.tx_max_packets = cpu_to_le32(ec->tx_max_coalesced_frames);
2767699b045aSAlvaro Karsz 	sg_init_one(&sgs_tx, &coal_tx, sizeof(coal_tx));
2768699b045aSAlvaro Karsz 
2769699b045aSAlvaro Karsz 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
2770699b045aSAlvaro Karsz 				  VIRTIO_NET_CTRL_NOTF_COAL_TX_SET,
2771699b045aSAlvaro Karsz 				  &sgs_tx))
2772699b045aSAlvaro Karsz 		return -EINVAL;
2773699b045aSAlvaro Karsz 
2774699b045aSAlvaro Karsz 	/* Save parameters */
2775699b045aSAlvaro Karsz 	vi->tx_usecs = ec->tx_coalesce_usecs;
2776699b045aSAlvaro Karsz 	vi->tx_max_packets = ec->tx_max_coalesced_frames;
2777699b045aSAlvaro Karsz 
2778699b045aSAlvaro Karsz 	coal_rx.rx_usecs = cpu_to_le32(ec->rx_coalesce_usecs);
2779699b045aSAlvaro Karsz 	coal_rx.rx_max_packets = cpu_to_le32(ec->rx_max_coalesced_frames);
2780699b045aSAlvaro Karsz 	sg_init_one(&sgs_rx, &coal_rx, sizeof(coal_rx));
2781699b045aSAlvaro Karsz 
2782699b045aSAlvaro Karsz 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_NOTF_COAL,
2783699b045aSAlvaro Karsz 				  VIRTIO_NET_CTRL_NOTF_COAL_RX_SET,
2784699b045aSAlvaro Karsz 				  &sgs_rx))
2785699b045aSAlvaro Karsz 		return -EINVAL;
2786699b045aSAlvaro Karsz 
2787699b045aSAlvaro Karsz 	/* Save parameters */
2788699b045aSAlvaro Karsz 	vi->rx_usecs = ec->rx_coalesce_usecs;
2789699b045aSAlvaro Karsz 	vi->rx_max_packets = ec->rx_max_coalesced_frames;
2790699b045aSAlvaro Karsz 
2791699b045aSAlvaro Karsz 	return 0;
2792699b045aSAlvaro Karsz }
2793699b045aSAlvaro Karsz 
2794699b045aSAlvaro Karsz static int virtnet_coal_params_supported(struct ethtool_coalesce *ec)
2795699b045aSAlvaro Karsz {
2796699b045aSAlvaro Karsz 	/* usecs coalescing is supported only if VIRTIO_NET_F_NOTF_COAL
2797699b045aSAlvaro Karsz 	 * feature is negotiated.
2798699b045aSAlvaro Karsz 	 */
2799699b045aSAlvaro Karsz 	if (ec->rx_coalesce_usecs || ec->tx_coalesce_usecs)
2800699b045aSAlvaro Karsz 		return -EOPNOTSUPP;
2801699b045aSAlvaro Karsz 
2802699b045aSAlvaro Karsz 	if (ec->tx_max_coalesced_frames > 1 ||
2803699b045aSAlvaro Karsz 	    ec->rx_max_coalesced_frames != 1)
2804699b045aSAlvaro Karsz 		return -EINVAL;
2805699b045aSAlvaro Karsz 
2806699b045aSAlvaro Karsz 	return 0;
2807699b045aSAlvaro Karsz }
2808699b045aSAlvaro Karsz 
28090c465be1SJason Wang static int virtnet_set_coalesce(struct net_device *dev,
2810f3ccfda1SYufeng Mo 				struct ethtool_coalesce *ec,
2811f3ccfda1SYufeng Mo 				struct kernel_ethtool_coalesce *kernel_coal,
2812f3ccfda1SYufeng Mo 				struct netlink_ext_ack *extack)
28130c465be1SJason Wang {
28140c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
2815699b045aSAlvaro Karsz 	int ret, i, napi_weight;
2816699b045aSAlvaro Karsz 	bool update_napi = false;
28170c465be1SJason Wang 
2818699b045aSAlvaro Karsz 	/* Can't change NAPI weight if the link is up */
28190c465be1SJason Wang 	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
28200c465be1SJason Wang 	if (napi_weight ^ vi->sq[0].napi.weight) {
28210c465be1SJason Wang 		if (dev->flags & IFF_UP)
28220c465be1SJason Wang 			return -EBUSY;
2823699b045aSAlvaro Karsz 		else
2824699b045aSAlvaro Karsz 			update_napi = true;
2825699b045aSAlvaro Karsz 	}
2826699b045aSAlvaro Karsz 
2827699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL))
2828699b045aSAlvaro Karsz 		ret = virtnet_send_notf_coal_cmds(vi, ec);
2829699b045aSAlvaro Karsz 	else
2830699b045aSAlvaro Karsz 		ret = virtnet_coal_params_supported(ec);
2831699b045aSAlvaro Karsz 
2832699b045aSAlvaro Karsz 	if (ret)
2833699b045aSAlvaro Karsz 		return ret;
2834699b045aSAlvaro Karsz 
2835699b045aSAlvaro Karsz 	if (update_napi) {
28360c465be1SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
28370c465be1SJason Wang 			vi->sq[i].napi.weight = napi_weight;
28380c465be1SJason Wang 	}
28390c465be1SJason Wang 
2840699b045aSAlvaro Karsz 	return ret;
28410c465be1SJason Wang }
28420c465be1SJason Wang 
28430c465be1SJason Wang static int virtnet_get_coalesce(struct net_device *dev,
2844f3ccfda1SYufeng Mo 				struct ethtool_coalesce *ec,
2845f3ccfda1SYufeng Mo 				struct kernel_ethtool_coalesce *kernel_coal,
2846f3ccfda1SYufeng Mo 				struct netlink_ext_ack *extack)
28470c465be1SJason Wang {
28480c465be1SJason Wang 	struct virtnet_info *vi = netdev_priv(dev);
28490c465be1SJason Wang 
2850699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
2851699b045aSAlvaro Karsz 		ec->rx_coalesce_usecs = vi->rx_usecs;
2852699b045aSAlvaro Karsz 		ec->tx_coalesce_usecs = vi->tx_usecs;
2853699b045aSAlvaro Karsz 		ec->tx_max_coalesced_frames = vi->tx_max_packets;
2854699b045aSAlvaro Karsz 		ec->rx_max_coalesced_frames = vi->rx_max_packets;
2855699b045aSAlvaro Karsz 	} else {
2856699b045aSAlvaro Karsz 		ec->rx_max_coalesced_frames = 1;
28570c465be1SJason Wang 
28580c465be1SJason Wang 		if (vi->sq[0].napi.weight)
28590c465be1SJason Wang 			ec->tx_max_coalesced_frames = 1;
2860699b045aSAlvaro Karsz 	}
28610c465be1SJason Wang 
28620c465be1SJason Wang 	return 0;
28630c465be1SJason Wang }
28640c465be1SJason Wang 
286516032be5SNikolay Aleksandrov static void virtnet_init_settings(struct net_device *dev)
286616032be5SNikolay Aleksandrov {
286716032be5SNikolay Aleksandrov 	struct virtnet_info *vi = netdev_priv(dev);
286816032be5SNikolay Aleksandrov 
286916032be5SNikolay Aleksandrov 	vi->speed = SPEED_UNKNOWN;
287016032be5SNikolay Aleksandrov 	vi->duplex = DUPLEX_UNKNOWN;
287116032be5SNikolay Aleksandrov }
287216032be5SNikolay Aleksandrov 
2873faa9b39fSJason Baron static void virtnet_update_settings(struct virtnet_info *vi)
2874faa9b39fSJason Baron {
2875faa9b39fSJason Baron 	u32 speed;
2876faa9b39fSJason Baron 	u8 duplex;
2877faa9b39fSJason Baron 
2878faa9b39fSJason Baron 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_SPEED_DUPLEX))
2879faa9b39fSJason Baron 		return;
2880faa9b39fSJason Baron 
288164ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, speed, &speed);
288264ffa39dSMichael S. Tsirkin 
2883faa9b39fSJason Baron 	if (ethtool_validate_speed(speed))
2884faa9b39fSJason Baron 		vi->speed = speed;
288564ffa39dSMichael S. Tsirkin 
288664ffa39dSMichael S. Tsirkin 	virtio_cread_le(vi->vdev, struct virtio_net_config, duplex, &duplex);
288764ffa39dSMichael S. Tsirkin 
2888faa9b39fSJason Baron 	if (ethtool_validate_duplex(duplex))
2889faa9b39fSJason Baron 		vi->duplex = duplex;
2890faa9b39fSJason Baron }
2891faa9b39fSJason Baron 
2892c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_key_size(struct net_device *dev)
2893c7114b12SAndrew Melnychenko {
2894c7114b12SAndrew Melnychenko 	return ((struct virtnet_info *)netdev_priv(dev))->rss_key_size;
2895c7114b12SAndrew Melnychenko }
2896c7114b12SAndrew Melnychenko 
2897c7114b12SAndrew Melnychenko static u32 virtnet_get_rxfh_indir_size(struct net_device *dev)
2898c7114b12SAndrew Melnychenko {
2899c7114b12SAndrew Melnychenko 	return ((struct virtnet_info *)netdev_priv(dev))->rss_indir_table_size;
2900c7114b12SAndrew Melnychenko }
2901c7114b12SAndrew Melnychenko 
2902c7114b12SAndrew Melnychenko static int virtnet_get_rxfh(struct net_device *dev, u32 *indir, u8 *key, u8 *hfunc)
2903c7114b12SAndrew Melnychenko {
2904c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
2905c7114b12SAndrew Melnychenko 	int i;
2906c7114b12SAndrew Melnychenko 
2907c7114b12SAndrew Melnychenko 	if (indir) {
2908c7114b12SAndrew Melnychenko 		for (i = 0; i < vi->rss_indir_table_size; ++i)
2909c7114b12SAndrew Melnychenko 			indir[i] = vi->ctrl->rss.indirection_table[i];
2910c7114b12SAndrew Melnychenko 	}
2911c7114b12SAndrew Melnychenko 
2912c7114b12SAndrew Melnychenko 	if (key)
2913c7114b12SAndrew Melnychenko 		memcpy(key, vi->ctrl->rss.key, vi->rss_key_size);
2914c7114b12SAndrew Melnychenko 
2915c7114b12SAndrew Melnychenko 	if (hfunc)
2916c7114b12SAndrew Melnychenko 		*hfunc = ETH_RSS_HASH_TOP;
2917c7114b12SAndrew Melnychenko 
2918c7114b12SAndrew Melnychenko 	return 0;
2919c7114b12SAndrew Melnychenko }
2920c7114b12SAndrew Melnychenko 
2921c7114b12SAndrew Melnychenko static int virtnet_set_rxfh(struct net_device *dev, const u32 *indir, const u8 *key, const u8 hfunc)
2922c7114b12SAndrew Melnychenko {
2923c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
2924c7114b12SAndrew Melnychenko 	int i;
2925c7114b12SAndrew Melnychenko 
2926c7114b12SAndrew Melnychenko 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
2927c7114b12SAndrew Melnychenko 		return -EOPNOTSUPP;
2928c7114b12SAndrew Melnychenko 
2929c7114b12SAndrew Melnychenko 	if (indir) {
2930c7114b12SAndrew Melnychenko 		for (i = 0; i < vi->rss_indir_table_size; ++i)
2931c7114b12SAndrew Melnychenko 			vi->ctrl->rss.indirection_table[i] = indir[i];
2932c7114b12SAndrew Melnychenko 	}
2933c7114b12SAndrew Melnychenko 	if (key)
2934c7114b12SAndrew Melnychenko 		memcpy(vi->ctrl->rss.key, key, vi->rss_key_size);
2935c7114b12SAndrew Melnychenko 
2936c7114b12SAndrew Melnychenko 	virtnet_commit_rss_command(vi);
2937c7114b12SAndrew Melnychenko 
2938c7114b12SAndrew Melnychenko 	return 0;
2939c7114b12SAndrew Melnychenko }
2940c7114b12SAndrew Melnychenko 
2941c7114b12SAndrew Melnychenko static int virtnet_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info, u32 *rule_locs)
2942c7114b12SAndrew Melnychenko {
2943c7114b12SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
2944c7114b12SAndrew Melnychenko 	int rc = 0;
2945c7114b12SAndrew Melnychenko 
2946c7114b12SAndrew Melnychenko 	switch (info->cmd) {
2947c7114b12SAndrew Melnychenko 	case ETHTOOL_GRXRINGS:
2948c7114b12SAndrew Melnychenko 		info->data = vi->curr_queue_pairs;
2949c7114b12SAndrew Melnychenko 		break;
2950c1170820SAndrew Melnychenko 	case ETHTOOL_GRXFH:
2951c1170820SAndrew Melnychenko 		virtnet_get_hashflow(vi, info);
2952c1170820SAndrew Melnychenko 		break;
2953c1170820SAndrew Melnychenko 	default:
2954c1170820SAndrew Melnychenko 		rc = -EOPNOTSUPP;
2955c1170820SAndrew Melnychenko 	}
2956c1170820SAndrew Melnychenko 
2957c1170820SAndrew Melnychenko 	return rc;
2958c1170820SAndrew Melnychenko }
2959c1170820SAndrew Melnychenko 
2960c1170820SAndrew Melnychenko static int virtnet_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *info)
2961c1170820SAndrew Melnychenko {
2962c1170820SAndrew Melnychenko 	struct virtnet_info *vi = netdev_priv(dev);
2963c1170820SAndrew Melnychenko 	int rc = 0;
2964c1170820SAndrew Melnychenko 
2965c1170820SAndrew Melnychenko 	switch (info->cmd) {
2966c1170820SAndrew Melnychenko 	case ETHTOOL_SRXFH:
2967c1170820SAndrew Melnychenko 		if (!virtnet_set_hashflow(vi, info))
2968c1170820SAndrew Melnychenko 			rc = -EINVAL;
2969c1170820SAndrew Melnychenko 
2970c1170820SAndrew Melnychenko 		break;
2971c7114b12SAndrew Melnychenko 	default:
2972c7114b12SAndrew Melnychenko 		rc = -EOPNOTSUPP;
2973c7114b12SAndrew Melnychenko 	}
2974c7114b12SAndrew Melnychenko 
2975c7114b12SAndrew Melnychenko 	return rc;
2976c7114b12SAndrew Melnychenko }
2977c7114b12SAndrew Melnychenko 
29780fc0b732SStephen Hemminger static const struct ethtool_ops virtnet_ethtool_ops = {
2979699b045aSAlvaro Karsz 	.supported_coalesce_params = ETHTOOL_COALESCE_MAX_FRAMES |
2980699b045aSAlvaro Karsz 		ETHTOOL_COALESCE_USECS,
298166846048SRick Jones 	.get_drvinfo = virtnet_get_drvinfo,
29829f4d26d0SMark McLoughlin 	.get_link = ethtool_op_get_link,
29838f9f4668SRick Jones 	.get_ringparam = virtnet_get_ringparam,
2984a335b33fSXuan Zhuo 	.set_ringparam = virtnet_set_ringparam,
2985d7dfc5cfSToshiaki Makita 	.get_strings = virtnet_get_strings,
2986d7dfc5cfSToshiaki Makita 	.get_sset_count = virtnet_get_sset_count,
2987d7dfc5cfSToshiaki Makita 	.get_ethtool_stats = virtnet_get_ethtool_stats,
2988d73bcd2cSJason Wang 	.set_channels = virtnet_set_channels,
2989d73bcd2cSJason Wang 	.get_channels = virtnet_get_channels,
2990074c3582SJacob Keller 	.get_ts_info = ethtool_op_get_ts_info,
2991ebb6b4b1SPhilippe Reynes 	.get_link_ksettings = virtnet_get_link_ksettings,
2992ebb6b4b1SPhilippe Reynes 	.set_link_ksettings = virtnet_set_link_ksettings,
29930c465be1SJason Wang 	.set_coalesce = virtnet_set_coalesce,
29940c465be1SJason Wang 	.get_coalesce = virtnet_get_coalesce,
2995c7114b12SAndrew Melnychenko 	.get_rxfh_key_size = virtnet_get_rxfh_key_size,
2996c7114b12SAndrew Melnychenko 	.get_rxfh_indir_size = virtnet_get_rxfh_indir_size,
2997c7114b12SAndrew Melnychenko 	.get_rxfh = virtnet_get_rxfh,
2998c7114b12SAndrew Melnychenko 	.set_rxfh = virtnet_set_rxfh,
2999c7114b12SAndrew Melnychenko 	.get_rxnfc = virtnet_get_rxnfc,
3000c1170820SAndrew Melnychenko 	.set_rxnfc = virtnet_set_rxnfc,
3001a9ea3fc6SHerbert Xu };
3002a9ea3fc6SHerbert Xu 
30039fe7bfceSJohn Fastabend static void virtnet_freeze_down(struct virtio_device *vdev)
30049fe7bfceSJohn Fastabend {
30059fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
30069fe7bfceSJohn Fastabend 
30079fe7bfceSJohn Fastabend 	/* Make sure no work handler is accessing the device */
30089fe7bfceSJohn Fastabend 	flush_work(&vi->config_work);
30099fe7bfceSJohn Fastabend 
301005c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
30119fe7bfceSJohn Fastabend 	netif_device_detach(vi->dev);
301205c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
30138af52fe9SStephan Gerhold 	if (netif_running(vi->dev))
30148af52fe9SStephan Gerhold 		virtnet_close(vi->dev);
30159fe7bfceSJohn Fastabend }
30169fe7bfceSJohn Fastabend 
30179fe7bfceSJohn Fastabend static int init_vqs(struct virtnet_info *vi);
30189fe7bfceSJohn Fastabend 
30199fe7bfceSJohn Fastabend static int virtnet_restore_up(struct virtio_device *vdev)
30209fe7bfceSJohn Fastabend {
30219fe7bfceSJohn Fastabend 	struct virtnet_info *vi = vdev->priv;
30228af52fe9SStephan Gerhold 	int err;
30239fe7bfceSJohn Fastabend 
30249fe7bfceSJohn Fastabend 	err = init_vqs(vi);
30259fe7bfceSJohn Fastabend 	if (err)
30269fe7bfceSJohn Fastabend 		return err;
30279fe7bfceSJohn Fastabend 
30289fe7bfceSJohn Fastabend 	virtio_device_ready(vdev);
30299fe7bfceSJohn Fastabend 
30305a159128SJason Wang 	enable_delayed_refill(vi);
30315a159128SJason Wang 
30329fe7bfceSJohn Fastabend 	if (netif_running(vi->dev)) {
30338af52fe9SStephan Gerhold 		err = virtnet_open(vi->dev);
30348af52fe9SStephan Gerhold 		if (err)
30358af52fe9SStephan Gerhold 			return err;
30369fe7bfceSJohn Fastabend 	}
30379fe7bfceSJohn Fastabend 
303805c998b7SAke Koomsin 	netif_tx_lock_bh(vi->dev);
30399fe7bfceSJohn Fastabend 	netif_device_attach(vi->dev);
304005c998b7SAke Koomsin 	netif_tx_unlock_bh(vi->dev);
30419fe7bfceSJohn Fastabend 	return err;
30429fe7bfceSJohn Fastabend }
30439fe7bfceSJohn Fastabend 
30443f93522fSJason Wang static int virtnet_set_guest_offloads(struct virtnet_info *vi, u64 offloads)
30453f93522fSJason Wang {
30463f93522fSJason Wang 	struct scatterlist sg;
304712e57169SMichael S. Tsirkin 	vi->ctrl->offloads = cpu_to_virtio64(vi->vdev, offloads);
30483f93522fSJason Wang 
304912e57169SMichael S. Tsirkin 	sg_init_one(&sg, &vi->ctrl->offloads, sizeof(vi->ctrl->offloads));
30503f93522fSJason Wang 
30513f93522fSJason Wang 	if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_GUEST_OFFLOADS,
30523f93522fSJason Wang 				  VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET, &sg)) {
30533f93522fSJason Wang 		dev_warn(&vi->dev->dev, "Fail to set guest offload.\n");
30543f93522fSJason Wang 		return -EINVAL;
30553f93522fSJason Wang 	}
30563f93522fSJason Wang 
30573f93522fSJason Wang 	return 0;
30583f93522fSJason Wang }
30593f93522fSJason Wang 
30603f93522fSJason Wang static int virtnet_clear_guest_offloads(struct virtnet_info *vi)
30613f93522fSJason Wang {
30623f93522fSJason Wang 	u64 offloads = 0;
30633f93522fSJason Wang 
30643f93522fSJason Wang 	if (!vi->guest_offloads)
30653f93522fSJason Wang 		return 0;
30663f93522fSJason Wang 
30673f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
30683f93522fSJason Wang }
30693f93522fSJason Wang 
30703f93522fSJason Wang static int virtnet_restore_guest_offloads(struct virtnet_info *vi)
30713f93522fSJason Wang {
30723f93522fSJason Wang 	u64 offloads = vi->guest_offloads;
30733f93522fSJason Wang 
30743f93522fSJason Wang 	if (!vi->guest_offloads)
30753f93522fSJason Wang 		return 0;
30763f93522fSJason Wang 
30773f93522fSJason Wang 	return virtnet_set_guest_offloads(vi, offloads);
30783f93522fSJason Wang }
30793f93522fSJason Wang 
30809861ce03SJakub Kicinski static int virtnet_xdp_set(struct net_device *dev, struct bpf_prog *prog,
30819861ce03SJakub Kicinski 			   struct netlink_ext_ack *extack)
3082f600b690SJohn Fastabend {
3083f600b690SJohn Fastabend 	unsigned long int max_sz = PAGE_SIZE - sizeof(struct padded_vnet_hdr);
3084f600b690SJohn Fastabend 	struct virtnet_info *vi = netdev_priv(dev);
3085f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
3086017b29c3SJason Wang 	u16 xdp_qp = 0, curr_qp;
3087672aafd5SJohn Fastabend 	int i, err;
3088f600b690SJohn Fastabend 
30893f93522fSJason Wang 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)
30903f93522fSJason Wang 	    && (virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
309192502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
309292502fe8SJason Wang 	        virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
309318ba58e1SJason Wang 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3094418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_CSUM) ||
3095418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) ||
3096418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6))) {
3097dbcf24d1SJason Wang 		NL_SET_ERR_MSG_MOD(extack, "Can't set XDP while host is implementing GRO_HW/CSUM, disable GRO_HW/CSUM first");
3098f600b690SJohn Fastabend 		return -EOPNOTSUPP;
3099f600b690SJohn Fastabend 	}
3100f600b690SJohn Fastabend 
3101f600b690SJohn Fastabend 	if (vi->mergeable_rx_bufs && !vi->any_header_sg) {
31024d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "XDP expects header/data in single page, any_header_sg required");
3103f600b690SJohn Fastabend 		return -EINVAL;
3104f600b690SJohn Fastabend 	}
3105f600b690SJohn Fastabend 
3106f600b690SJohn Fastabend 	if (dev->mtu > max_sz) {
31074d463c4dSDaniel Borkmann 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
3108f600b690SJohn Fastabend 		netdev_warn(dev, "XDP requires MTU less than %lu\n", max_sz);
3109f600b690SJohn Fastabend 		return -EINVAL;
3110f600b690SJohn Fastabend 	}
3111f600b690SJohn Fastabend 
3112672aafd5SJohn Fastabend 	curr_qp = vi->curr_queue_pairs - vi->xdp_queue_pairs;
3113672aafd5SJohn Fastabend 	if (prog)
3114672aafd5SJohn Fastabend 		xdp_qp = nr_cpu_ids;
3115672aafd5SJohn Fastabend 
3116672aafd5SJohn Fastabend 	/* XDP requires extra queues for XDP_TX */
3117672aafd5SJohn Fastabend 	if (curr_qp + xdp_qp > vi->max_queue_pairs) {
31189ce4e3d6SXuan 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",
3119672aafd5SJohn Fastabend 				 curr_qp + xdp_qp, vi->max_queue_pairs);
312097c2c69eSXuan Zhuo 		xdp_qp = 0;
3121672aafd5SJohn Fastabend 	}
3122672aafd5SJohn Fastabend 
312303aa6d34SToshiaki Makita 	old_prog = rtnl_dereference(vi->rq[0].xdp_prog);
312403aa6d34SToshiaki Makita 	if (!prog && !old_prog)
312503aa6d34SToshiaki Makita 		return 0;
312603aa6d34SToshiaki Makita 
312785192dbfSAndrii Nakryiko 	if (prog)
312885192dbfSAndrii Nakryiko 		bpf_prog_add(prog, vi->max_queue_pairs - 1);
31292de2f7f4SJohn Fastabend 
31304941d472SJason Wang 	/* Make sure NAPI is not using any XDP TX queues for RX. */
3131534da5e8SToshiaki Makita 	if (netif_running(dev)) {
3132534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
31334941d472SJason Wang 			napi_disable(&vi->rq[i].napi);
3134534da5e8SToshiaki Makita 			virtnet_napi_tx_disable(&vi->sq[i].napi);
3135534da5e8SToshiaki Makita 		}
3136534da5e8SToshiaki Makita 	}
31372de2f7f4SJohn Fastabend 
313803aa6d34SToshiaki Makita 	if (!prog) {
313903aa6d34SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
314003aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
314103aa6d34SToshiaki Makita 			if (i == 0)
314203aa6d34SToshiaki Makita 				virtnet_restore_guest_offloads(vi);
314303aa6d34SToshiaki Makita 		}
314403aa6d34SToshiaki Makita 		synchronize_net();
314503aa6d34SToshiaki Makita 	}
314603aa6d34SToshiaki Makita 
31474941d472SJason Wang 	err = _virtnet_set_queues(vi, curr_qp + xdp_qp);
31484941d472SJason Wang 	if (err)
31494941d472SJason Wang 		goto err;
3150188313c1SToshiaki Makita 	netif_set_real_num_rx_queues(dev, curr_qp + xdp_qp);
31514941d472SJason Wang 	vi->xdp_queue_pairs = xdp_qp;
3152f600b690SJohn Fastabend 
315303aa6d34SToshiaki Makita 	if (prog) {
315497c2c69eSXuan Zhuo 		vi->xdp_enabled = true;
3155f600b690SJohn Fastabend 		for (i = 0; i < vi->max_queue_pairs; i++) {
3156f600b690SJohn Fastabend 			rcu_assign_pointer(vi->rq[i].xdp_prog, prog);
315703aa6d34SToshiaki Makita 			if (i == 0 && !old_prog)
31583f93522fSJason Wang 				virtnet_clear_guest_offloads(vi);
31593f93522fSJason Wang 		}
316097c2c69eSXuan Zhuo 	} else {
316197c2c69eSXuan Zhuo 		vi->xdp_enabled = false;
316203aa6d34SToshiaki Makita 	}
316303aa6d34SToshiaki Makita 
316403aa6d34SToshiaki Makita 	for (i = 0; i < vi->max_queue_pairs; i++) {
3165f600b690SJohn Fastabend 		if (old_prog)
3166f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
3167534da5e8SToshiaki Makita 		if (netif_running(dev)) {
31684941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3169534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3170534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
3171534da5e8SToshiaki Makita 		}
3172f600b690SJohn Fastabend 	}
3173f600b690SJohn Fastabend 
3174f600b690SJohn Fastabend 	return 0;
31752de2f7f4SJohn Fastabend 
31764941d472SJason Wang err:
317703aa6d34SToshiaki Makita 	if (!prog) {
317803aa6d34SToshiaki Makita 		virtnet_clear_guest_offloads(vi);
31794941d472SJason Wang 		for (i = 0; i < vi->max_queue_pairs; i++)
318003aa6d34SToshiaki Makita 			rcu_assign_pointer(vi->rq[i].xdp_prog, old_prog);
318103aa6d34SToshiaki Makita 	}
318203aa6d34SToshiaki Makita 
31838be4d9a4SToshiaki Makita 	if (netif_running(dev)) {
3184534da5e8SToshiaki Makita 		for (i = 0; i < vi->max_queue_pairs; i++) {
31854941d472SJason Wang 			virtnet_napi_enable(vi->rq[i].vq, &vi->rq[i].napi);
3186534da5e8SToshiaki Makita 			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
3187534da5e8SToshiaki Makita 					       &vi->sq[i].napi);
3188534da5e8SToshiaki Makita 		}
31898be4d9a4SToshiaki Makita 	}
31902de2f7f4SJohn Fastabend 	if (prog)
31912de2f7f4SJohn Fastabend 		bpf_prog_sub(prog, vi->max_queue_pairs - 1);
31922de2f7f4SJohn Fastabend 	return err;
3193f600b690SJohn Fastabend }
3194f600b690SJohn Fastabend 
3195f4e63525SJakub Kicinski static int virtnet_xdp(struct net_device *dev, struct netdev_bpf *xdp)
3196f600b690SJohn Fastabend {
3197f600b690SJohn Fastabend 	switch (xdp->command) {
3198f600b690SJohn Fastabend 	case XDP_SETUP_PROG:
31999861ce03SJakub Kicinski 		return virtnet_xdp_set(dev, xdp->prog, xdp->extack);
3200f600b690SJohn Fastabend 	default:
3201f600b690SJohn Fastabend 		return -EINVAL;
3202f600b690SJohn Fastabend 	}
3203f600b690SJohn Fastabend }
3204f600b690SJohn Fastabend 
3205ba5e4426SSridhar Samudrala static int virtnet_get_phys_port_name(struct net_device *dev, char *buf,
3206ba5e4426SSridhar Samudrala 				      size_t len)
3207ba5e4426SSridhar Samudrala {
3208ba5e4426SSridhar Samudrala 	struct virtnet_info *vi = netdev_priv(dev);
3209ba5e4426SSridhar Samudrala 	int ret;
3210ba5e4426SSridhar Samudrala 
3211ba5e4426SSridhar Samudrala 	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STANDBY))
3212ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
3213ba5e4426SSridhar Samudrala 
3214ba5e4426SSridhar Samudrala 	ret = snprintf(buf, len, "sby");
3215ba5e4426SSridhar Samudrala 	if (ret >= len)
3216ba5e4426SSridhar Samudrala 		return -EOPNOTSUPP;
3217ba5e4426SSridhar Samudrala 
3218ba5e4426SSridhar Samudrala 	return 0;
3219ba5e4426SSridhar Samudrala }
3220ba5e4426SSridhar Samudrala 
3221a02e8964SWillem de Bruijn static int virtnet_set_features(struct net_device *dev,
3222a02e8964SWillem de Bruijn 				netdev_features_t features)
3223a02e8964SWillem de Bruijn {
3224a02e8964SWillem de Bruijn 	struct virtnet_info *vi = netdev_priv(dev);
3225cf8691cbSMichael S. Tsirkin 	u64 offloads;
3226a02e8964SWillem de Bruijn 	int err;
3227a02e8964SWillem de Bruijn 
3228dbcf24d1SJason Wang 	if ((dev->features ^ features) & NETIF_F_GRO_HW) {
322997c2c69eSXuan Zhuo 		if (vi->xdp_enabled)
3230a02e8964SWillem de Bruijn 			return -EBUSY;
3231a02e8964SWillem de Bruijn 
3232dbcf24d1SJason Wang 		if (features & NETIF_F_GRO_HW)
3233cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable;
3234a02e8964SWillem de Bruijn 		else
3235cf8691cbSMichael S. Tsirkin 			offloads = vi->guest_offloads_capable &
3236dbcf24d1SJason Wang 				   ~GUEST_OFFLOAD_GRO_HW_MASK;
3237a02e8964SWillem de Bruijn 
3238a02e8964SWillem de Bruijn 		err = virtnet_set_guest_offloads(vi, offloads);
3239a02e8964SWillem de Bruijn 		if (err)
3240a02e8964SWillem de Bruijn 			return err;
32413618ad2aSTonghao Zhang 		vi->guest_offloads = offloads;
3242cf8691cbSMichael S. Tsirkin 	}
3243cf8691cbSMichael S. Tsirkin 
3244c7114b12SAndrew Melnychenko 	if ((dev->features ^ features) & NETIF_F_RXHASH) {
3245c7114b12SAndrew Melnychenko 		if (features & NETIF_F_RXHASH)
3246c1170820SAndrew Melnychenko 			vi->ctrl->rss.hash_types = vi->rss_hash_types_saved;
3247c7114b12SAndrew Melnychenko 		else
3248c7114b12SAndrew Melnychenko 			vi->ctrl->rss.hash_types = VIRTIO_NET_HASH_REPORT_NONE;
3249c7114b12SAndrew Melnychenko 
3250c7114b12SAndrew Melnychenko 		if (!virtnet_commit_rss_command(vi))
3251c7114b12SAndrew Melnychenko 			return -EINVAL;
3252c7114b12SAndrew Melnychenko 	}
3253c7114b12SAndrew Melnychenko 
3254a02e8964SWillem de Bruijn 	return 0;
3255a02e8964SWillem de Bruijn }
3256a02e8964SWillem de Bruijn 
3257a520794bSTony Lu static void virtnet_tx_timeout(struct net_device *dev, unsigned int txqueue)
3258a520794bSTony Lu {
3259a520794bSTony Lu 	struct virtnet_info *priv = netdev_priv(dev);
3260a520794bSTony Lu 	struct send_queue *sq = &priv->sq[txqueue];
3261a520794bSTony Lu 	struct netdev_queue *txq = netdev_get_tx_queue(dev, txqueue);
3262a520794bSTony Lu 
3263a520794bSTony Lu 	u64_stats_update_begin(&sq->stats.syncp);
3264a520794bSTony Lu 	sq->stats.tx_timeouts++;
3265a520794bSTony Lu 	u64_stats_update_end(&sq->stats.syncp);
3266a520794bSTony Lu 
3267a520794bSTony Lu 	netdev_err(dev, "TX timeout on queue: %u, sq: %s, vq: 0x%x, name: %s, %u usecs ago\n",
3268a520794bSTony Lu 		   txqueue, sq->name, sq->vq->index, sq->vq->name,
32695337824fSEric Dumazet 		   jiffies_to_usecs(jiffies - READ_ONCE(txq->trans_start)));
3270a520794bSTony Lu }
3271a520794bSTony Lu 
327276288b4eSStephen Hemminger static const struct net_device_ops virtnet_netdev = {
327376288b4eSStephen Hemminger 	.ndo_open            = virtnet_open,
327476288b4eSStephen Hemminger 	.ndo_stop   	     = virtnet_close,
327576288b4eSStephen Hemminger 	.ndo_start_xmit      = start_xmit,
327676288b4eSStephen Hemminger 	.ndo_validate_addr   = eth_validate_addr,
32779c46f6d4SAlex Williamson 	.ndo_set_mac_address = virtnet_set_mac_address,
32782af7698eSAlex Williamson 	.ndo_set_rx_mode     = virtnet_set_rx_mode,
32793fa2a1dfSstephen hemminger 	.ndo_get_stats64     = virtnet_stats,
32801824a989SAlex Williamson 	.ndo_vlan_rx_add_vid = virtnet_vlan_rx_add_vid,
32811824a989SAlex Williamson 	.ndo_vlan_rx_kill_vid = virtnet_vlan_rx_kill_vid,
3282f4e63525SJakub Kicinski 	.ndo_bpf		= virtnet_xdp,
3283186b3c99SJason Wang 	.ndo_xdp_xmit		= virtnet_xdp_xmit,
32842836b4f2SVlad Yasevich 	.ndo_features_check	= passthru_features_check,
3285ba5e4426SSridhar Samudrala 	.ndo_get_phys_port_name	= virtnet_get_phys_port_name,
3286a02e8964SWillem de Bruijn 	.ndo_set_features	= virtnet_set_features,
3287a520794bSTony Lu 	.ndo_tx_timeout		= virtnet_tx_timeout,
328876288b4eSStephen Hemminger };
328976288b4eSStephen Hemminger 
3290586d17c5SJason Wang static void virtnet_config_changed_work(struct work_struct *work)
32919f4d26d0SMark McLoughlin {
3292586d17c5SJason Wang 	struct virtnet_info *vi =
3293586d17c5SJason Wang 		container_of(work, struct virtnet_info, config_work);
32949f4d26d0SMark McLoughlin 	u16 v;
32959f4d26d0SMark McLoughlin 
3296855e0c52SRusty Russell 	if (virtio_cread_feature(vi->vdev, VIRTIO_NET_F_STATUS,
3297855e0c52SRusty Russell 				 struct virtio_net_config, status, &v) < 0)
3298507613bfSMichael S. Tsirkin 		return;
3299586d17c5SJason Wang 
3300586d17c5SJason Wang 	if (v & VIRTIO_NET_S_ANNOUNCE) {
3301ee89bab1SAmerigo Wang 		netdev_notify_peers(vi->dev);
3302586d17c5SJason Wang 		virtnet_ack_link_announce(vi);
3303586d17c5SJason Wang 	}
33049f4d26d0SMark McLoughlin 
33059f4d26d0SMark McLoughlin 	/* Ignore unknown (future) status bits */
33069f4d26d0SMark McLoughlin 	v &= VIRTIO_NET_S_LINK_UP;
33079f4d26d0SMark McLoughlin 
33089f4d26d0SMark McLoughlin 	if (vi->status == v)
3309507613bfSMichael S. Tsirkin 		return;
33109f4d26d0SMark McLoughlin 
33119f4d26d0SMark McLoughlin 	vi->status = v;
33129f4d26d0SMark McLoughlin 
33139f4d26d0SMark McLoughlin 	if (vi->status & VIRTIO_NET_S_LINK_UP) {
3314faa9b39fSJason Baron 		virtnet_update_settings(vi);
33159f4d26d0SMark McLoughlin 		netif_carrier_on(vi->dev);
3316986a4f4dSJason Wang 		netif_tx_wake_all_queues(vi->dev);
33179f4d26d0SMark McLoughlin 	} else {
33189f4d26d0SMark McLoughlin 		netif_carrier_off(vi->dev);
3319986a4f4dSJason Wang 		netif_tx_stop_all_queues(vi->dev);
33209f4d26d0SMark McLoughlin 	}
33219f4d26d0SMark McLoughlin }
33229f4d26d0SMark McLoughlin 
33239f4d26d0SMark McLoughlin static void virtnet_config_changed(struct virtio_device *vdev)
33249f4d26d0SMark McLoughlin {
33259f4d26d0SMark McLoughlin 	struct virtnet_info *vi = vdev->priv;
33269f4d26d0SMark McLoughlin 
33273b07e9caSTejun Heo 	schedule_work(&vi->config_work);
33289f4d26d0SMark McLoughlin }
33299f4d26d0SMark McLoughlin 
3330986a4f4dSJason Wang static void virtnet_free_queues(struct virtnet_info *vi)
3331986a4f4dSJason Wang {
3332d4fb84eeSAndrey Vagin 	int i;
3333d4fb84eeSAndrey Vagin 
3334ab3971b1SJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
33355198d545SJakub Kicinski 		__netif_napi_del(&vi->rq[i].napi);
33365198d545SJakub Kicinski 		__netif_napi_del(&vi->sq[i].napi);
3337ab3971b1SJason Wang 	}
3338d4fb84eeSAndrey Vagin 
33395198d545SJakub Kicinski 	/* We called __netif_napi_del(),
3340963abe5cSEric Dumazet 	 * we need to respect an RCU grace period before freeing vi->rq
3341963abe5cSEric Dumazet 	 */
3342963abe5cSEric Dumazet 	synchronize_net();
3343963abe5cSEric Dumazet 
3344986a4f4dSJason Wang 	kfree(vi->rq);
3345986a4f4dSJason Wang 	kfree(vi->sq);
334612e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
3347986a4f4dSJason Wang }
3348986a4f4dSJason Wang 
334947315329SJohn Fastabend static void _free_receive_bufs(struct virtnet_info *vi)
3350986a4f4dSJason Wang {
3351f600b690SJohn Fastabend 	struct bpf_prog *old_prog;
3352986a4f4dSJason Wang 	int i;
3353986a4f4dSJason Wang 
3354986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3355986a4f4dSJason Wang 		while (vi->rq[i].pages)
3356986a4f4dSJason Wang 			__free_pages(get_a_page(&vi->rq[i], GFP_KERNEL), 0);
3357f600b690SJohn Fastabend 
3358f600b690SJohn Fastabend 		old_prog = rtnl_dereference(vi->rq[i].xdp_prog);
3359f600b690SJohn Fastabend 		RCU_INIT_POINTER(vi->rq[i].xdp_prog, NULL);
3360f600b690SJohn Fastabend 		if (old_prog)
3361f600b690SJohn Fastabend 			bpf_prog_put(old_prog);
3362986a4f4dSJason Wang 	}
336347315329SJohn Fastabend }
336447315329SJohn Fastabend 
336547315329SJohn Fastabend static void free_receive_bufs(struct virtnet_info *vi)
336647315329SJohn Fastabend {
336747315329SJohn Fastabend 	rtnl_lock();
336847315329SJohn Fastabend 	_free_receive_bufs(vi);
3369f600b690SJohn Fastabend 	rtnl_unlock();
3370986a4f4dSJason Wang }
3371986a4f4dSJason Wang 
3372fb51879dSMichael Dalton static void free_receive_page_frags(struct virtnet_info *vi)
3373fb51879dSMichael Dalton {
3374fb51879dSMichael Dalton 	int i;
3375fb51879dSMichael Dalton 	for (i = 0; i < vi->max_queue_pairs; i++)
3376fb51879dSMichael Dalton 		if (vi->rq[i].alloc_frag.page)
3377fb51879dSMichael Dalton 			put_page(vi->rq[i].alloc_frag.page);
3378fb51879dSMichael Dalton }
3379fb51879dSMichael Dalton 
33806e345f8cSXuan Zhuo static void virtnet_sq_free_unused_buf(struct virtqueue *vq, void *buf)
33816e345f8cSXuan Zhuo {
33826e345f8cSXuan Zhuo 	if (!is_xdp_frame(buf))
33836e345f8cSXuan Zhuo 		dev_kfree_skb(buf);
33846e345f8cSXuan Zhuo 	else
33856e345f8cSXuan Zhuo 		xdp_return_frame(ptr_to_xdp(buf));
33866e345f8cSXuan Zhuo }
33876e345f8cSXuan Zhuo 
33886e345f8cSXuan Zhuo static void virtnet_rq_free_unused_buf(struct virtqueue *vq, void *buf)
33896e345f8cSXuan Zhuo {
33906e345f8cSXuan Zhuo 	struct virtnet_info *vi = vq->vdev->priv;
33916e345f8cSXuan Zhuo 	int i = vq2rxq(vq);
33926e345f8cSXuan Zhuo 
33936e345f8cSXuan Zhuo 	if (vi->mergeable_rx_bufs)
33946e345f8cSXuan Zhuo 		put_page(virt_to_head_page(buf));
33956e345f8cSXuan Zhuo 	else if (vi->big_packets)
33966e345f8cSXuan Zhuo 		give_pages(&vi->rq[i], buf);
33976e345f8cSXuan Zhuo 	else
33986e345f8cSXuan Zhuo 		put_page(virt_to_head_page(buf));
33996e345f8cSXuan Zhuo }
34006e345f8cSXuan Zhuo 
3401986a4f4dSJason Wang static void free_unused_bufs(struct virtnet_info *vi)
3402986a4f4dSJason Wang {
3403986a4f4dSJason Wang 	void *buf;
3404986a4f4dSJason Wang 	int i;
3405986a4f4dSJason Wang 
3406986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3407986a4f4dSJason Wang 		struct virtqueue *vq = vi->sq[i].vq;
34086e345f8cSXuan Zhuo 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
34096e345f8cSXuan Zhuo 			virtnet_sq_free_unused_buf(vq, buf);
3410986a4f4dSJason Wang 	}
3411986a4f4dSJason Wang 
3412986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3413986a4f4dSJason Wang 		struct virtqueue *vq = vi->rq[i].vq;
34146e345f8cSXuan Zhuo 		while ((buf = virtqueue_detach_unused_buf(vq)) != NULL)
34156e345f8cSXuan Zhuo 			virtnet_rq_free_unused_buf(vq, buf);
3416986a4f4dSJason Wang 	}
3417ab7db917SMichael Dalton }
3418986a4f4dSJason Wang 
3419e9d7417bSJason Wang static void virtnet_del_vqs(struct virtnet_info *vi)
3420e9d7417bSJason Wang {
3421e9d7417bSJason Wang 	struct virtio_device *vdev = vi->vdev;
3422e9d7417bSJason Wang 
3423310974faSPeter Xu 	virtnet_clean_affinity(vi);
3424986a4f4dSJason Wang 
3425e9d7417bSJason Wang 	vdev->config->del_vqs(vdev);
3426986a4f4dSJason Wang 
3427986a4f4dSJason Wang 	virtnet_free_queues(vi);
3428986a4f4dSJason Wang }
3429986a4f4dSJason Wang 
3430d85b758fSMichael S. Tsirkin /* How large should a single buffer be so a queue full of these can fit at
3431d85b758fSMichael S. Tsirkin  * least one full packet?
3432d85b758fSMichael S. Tsirkin  * Logic below assumes the mergeable buffer header is used.
3433d85b758fSMichael S. Tsirkin  */
3434d85b758fSMichael S. Tsirkin static unsigned int mergeable_min_buf_len(struct virtnet_info *vi, struct virtqueue *vq)
3435d85b758fSMichael S. Tsirkin {
3436c1ddc42dSAndrew Melnychenko 	const unsigned int hdr_len = vi->hdr_len;
3437d85b758fSMichael S. Tsirkin 	unsigned int rq_size = virtqueue_get_vring_size(vq);
3438d85b758fSMichael S. Tsirkin 	unsigned int packet_len = vi->big_packets ? IP_MAX_MTU : vi->dev->max_mtu;
3439d85b758fSMichael S. Tsirkin 	unsigned int buf_len = hdr_len + ETH_HLEN + VLAN_HLEN + packet_len;
3440d85b758fSMichael S. Tsirkin 	unsigned int min_buf_len = DIV_ROUND_UP(buf_len, rq_size);
3441d85b758fSMichael S. Tsirkin 
3442f0c3192cSMichael S. Tsirkin 	return max(max(min_buf_len, hdr_len) - hdr_len,
3443f0c3192cSMichael S. Tsirkin 		   (unsigned int)GOOD_PACKET_LEN);
3444d85b758fSMichael S. Tsirkin }
3445d85b758fSMichael S. Tsirkin 
3446986a4f4dSJason Wang static int virtnet_find_vqs(struct virtnet_info *vi)
3447986a4f4dSJason Wang {
3448986a4f4dSJason Wang 	vq_callback_t **callbacks;
3449986a4f4dSJason Wang 	struct virtqueue **vqs;
3450986a4f4dSJason Wang 	int ret = -ENOMEM;
3451986a4f4dSJason Wang 	int i, total_vqs;
3452986a4f4dSJason Wang 	const char **names;
3453d45b897bSMichael S. Tsirkin 	bool *ctx;
3454986a4f4dSJason Wang 
3455986a4f4dSJason Wang 	/* We expect 1 RX virtqueue followed by 1 TX virtqueue, followed by
3456986a4f4dSJason Wang 	 * possible N-1 RX/TX queue pairs used in multiqueue mode, followed by
3457986a4f4dSJason Wang 	 * possible control vq.
3458986a4f4dSJason Wang 	 */
3459986a4f4dSJason Wang 	total_vqs = vi->max_queue_pairs * 2 +
3460986a4f4dSJason Wang 		    virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VQ);
3461986a4f4dSJason Wang 
3462986a4f4dSJason Wang 	/* Allocate space for find_vqs parameters */
34636396bb22SKees Cook 	vqs = kcalloc(total_vqs, sizeof(*vqs), GFP_KERNEL);
3464986a4f4dSJason Wang 	if (!vqs)
3465986a4f4dSJason Wang 		goto err_vq;
34666da2ec56SKees Cook 	callbacks = kmalloc_array(total_vqs, sizeof(*callbacks), GFP_KERNEL);
3467986a4f4dSJason Wang 	if (!callbacks)
3468986a4f4dSJason Wang 		goto err_callback;
34696da2ec56SKees Cook 	names = kmalloc_array(total_vqs, sizeof(*names), GFP_KERNEL);
3470986a4f4dSJason Wang 	if (!names)
3471986a4f4dSJason Wang 		goto err_names;
3472192f68cfSJason Wang 	if (!vi->big_packets || vi->mergeable_rx_bufs) {
34736396bb22SKees Cook 		ctx = kcalloc(total_vqs, sizeof(*ctx), GFP_KERNEL);
3474d45b897bSMichael S. Tsirkin 		if (!ctx)
3475d45b897bSMichael S. Tsirkin 			goto err_ctx;
3476d45b897bSMichael S. Tsirkin 	} else {
3477d45b897bSMichael S. Tsirkin 		ctx = NULL;
3478d45b897bSMichael S. Tsirkin 	}
3479986a4f4dSJason Wang 
3480986a4f4dSJason Wang 	/* Parameters for control virtqueue, if any */
3481986a4f4dSJason Wang 	if (vi->has_cvq) {
3482986a4f4dSJason Wang 		callbacks[total_vqs - 1] = NULL;
3483986a4f4dSJason Wang 		names[total_vqs - 1] = "control";
3484986a4f4dSJason Wang 	}
3485986a4f4dSJason Wang 
3486986a4f4dSJason Wang 	/* Allocate/initialize parameters for send/receive virtqueues */
3487986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3488986a4f4dSJason Wang 		callbacks[rxq2vq(i)] = skb_recv_done;
3489986a4f4dSJason Wang 		callbacks[txq2vq(i)] = skb_xmit_done;
3490986a4f4dSJason Wang 		sprintf(vi->rq[i].name, "input.%d", i);
3491986a4f4dSJason Wang 		sprintf(vi->sq[i].name, "output.%d", i);
3492986a4f4dSJason Wang 		names[rxq2vq(i)] = vi->rq[i].name;
3493986a4f4dSJason Wang 		names[txq2vq(i)] = vi->sq[i].name;
3494d45b897bSMichael S. Tsirkin 		if (ctx)
3495d45b897bSMichael S. Tsirkin 			ctx[rxq2vq(i)] = true;
3496986a4f4dSJason Wang 	}
3497986a4f4dSJason Wang 
34982e9ca760SMichael S. Tsirkin 	ret = virtio_find_vqs_ctx(vi->vdev, total_vqs, vqs, callbacks,
34992e9ca760SMichael S. Tsirkin 				  names, ctx, NULL);
3500986a4f4dSJason Wang 	if (ret)
3501986a4f4dSJason Wang 		goto err_find;
3502986a4f4dSJason Wang 
3503986a4f4dSJason Wang 	if (vi->has_cvq) {
3504986a4f4dSJason Wang 		vi->cvq = vqs[total_vqs - 1];
3505986a4f4dSJason Wang 		if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_CTRL_VLAN))
3506f646968fSPatrick McHardy 			vi->dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3507986a4f4dSJason Wang 	}
3508986a4f4dSJason Wang 
3509986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3510986a4f4dSJason Wang 		vi->rq[i].vq = vqs[rxq2vq(i)];
3511d85b758fSMichael S. Tsirkin 		vi->rq[i].min_buf_len = mergeable_min_buf_len(vi, vi->rq[i].vq);
3512986a4f4dSJason Wang 		vi->sq[i].vq = vqs[txq2vq(i)];
3513986a4f4dSJason Wang 	}
3514986a4f4dSJason Wang 
35152fa3c8a8STonghao Zhang 	/* run here: ret == 0. */
3516986a4f4dSJason Wang 
3517986a4f4dSJason Wang 
3518986a4f4dSJason Wang err_find:
3519d45b897bSMichael S. Tsirkin 	kfree(ctx);
3520d45b897bSMichael S. Tsirkin err_ctx:
3521986a4f4dSJason Wang 	kfree(names);
3522986a4f4dSJason Wang err_names:
3523986a4f4dSJason Wang 	kfree(callbacks);
3524986a4f4dSJason Wang err_callback:
3525986a4f4dSJason Wang 	kfree(vqs);
3526986a4f4dSJason Wang err_vq:
3527986a4f4dSJason Wang 	return ret;
3528986a4f4dSJason Wang }
3529986a4f4dSJason Wang 
3530986a4f4dSJason Wang static int virtnet_alloc_queues(struct virtnet_info *vi)
3531986a4f4dSJason Wang {
3532986a4f4dSJason Wang 	int i;
3533986a4f4dSJason Wang 
3534122b84a1SMax Gurtovoy 	if (vi->has_cvq) {
353512e57169SMichael S. Tsirkin 		vi->ctrl = kzalloc(sizeof(*vi->ctrl), GFP_KERNEL);
353612e57169SMichael S. Tsirkin 		if (!vi->ctrl)
353712e57169SMichael S. Tsirkin 			goto err_ctrl;
3538122b84a1SMax Gurtovoy 	} else {
3539122b84a1SMax Gurtovoy 		vi->ctrl = NULL;
3540122b84a1SMax Gurtovoy 	}
35416396bb22SKees Cook 	vi->sq = kcalloc(vi->max_queue_pairs, sizeof(*vi->sq), GFP_KERNEL);
3542986a4f4dSJason Wang 	if (!vi->sq)
3543986a4f4dSJason Wang 		goto err_sq;
35446396bb22SKees Cook 	vi->rq = kcalloc(vi->max_queue_pairs, sizeof(*vi->rq), GFP_KERNEL);
3545008d4278SAmerigo Wang 	if (!vi->rq)
3546986a4f4dSJason Wang 		goto err_rq;
3547986a4f4dSJason Wang 
3548986a4f4dSJason Wang 	INIT_DELAYED_WORK(&vi->refill, refill_work);
3549986a4f4dSJason Wang 	for (i = 0; i < vi->max_queue_pairs; i++) {
3550986a4f4dSJason Wang 		vi->rq[i].pages = NULL;
3551d484735dSJakub Kicinski 		netif_napi_add_weight(vi->dev, &vi->rq[i].napi, virtnet_poll,
3552986a4f4dSJason Wang 				      napi_weight);
35538d602e1aSJakub Kicinski 		netif_napi_add_tx_weight(vi->dev, &vi->sq[i].napi,
35548d602e1aSJakub Kicinski 					 virtnet_poll_tx,
3555b92f1e67SWillem de Bruijn 					 napi_tx ? napi_weight : 0);
3556986a4f4dSJason Wang 
3557986a4f4dSJason Wang 		sg_init_table(vi->rq[i].sg, ARRAY_SIZE(vi->rq[i].sg));
35585377d758SJohannes Berg 		ewma_pkt_len_init(&vi->rq[i].mrg_avg_pkt_len);
3559986a4f4dSJason Wang 		sg_init_table(vi->sq[i].sg, ARRAY_SIZE(vi->sq[i].sg));
3560d7dfc5cfSToshiaki Makita 
3561d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->rq[i].stats.syncp);
3562d7dfc5cfSToshiaki Makita 		u64_stats_init(&vi->sq[i].stats.syncp);
3563986a4f4dSJason Wang 	}
3564986a4f4dSJason Wang 
3565986a4f4dSJason Wang 	return 0;
3566986a4f4dSJason Wang 
3567986a4f4dSJason Wang err_rq:
3568986a4f4dSJason Wang 	kfree(vi->sq);
3569986a4f4dSJason Wang err_sq:
357012e57169SMichael S. Tsirkin 	kfree(vi->ctrl);
357112e57169SMichael S. Tsirkin err_ctrl:
3572986a4f4dSJason Wang 	return -ENOMEM;
3573e9d7417bSJason Wang }
3574e9d7417bSJason Wang 
35753f9c10b0SAmit Shah static int init_vqs(struct virtnet_info *vi)
35763f9c10b0SAmit Shah {
3577986a4f4dSJason Wang 	int ret;
35783f9c10b0SAmit Shah 
3579986a4f4dSJason Wang 	/* Allocate send & receive queues */
3580986a4f4dSJason Wang 	ret = virtnet_alloc_queues(vi);
3581986a4f4dSJason Wang 	if (ret)
3582986a4f4dSJason Wang 		goto err;
35833f9c10b0SAmit Shah 
3584986a4f4dSJason Wang 	ret = virtnet_find_vqs(vi);
3585986a4f4dSJason Wang 	if (ret)
3586986a4f4dSJason Wang 		goto err_free;
35873f9c10b0SAmit Shah 
3588a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_lock();
35898898c21cSWanlong Gao 	virtnet_set_affinity(vi);
3590a0d1d0f4SSebastian Andrzej Siewior 	cpus_read_unlock();
359147be2479SWanlong Gao 
35923f9c10b0SAmit Shah 	return 0;
3593986a4f4dSJason Wang 
3594986a4f4dSJason Wang err_free:
3595986a4f4dSJason Wang 	virtnet_free_queues(vi);
3596986a4f4dSJason Wang err:
3597986a4f4dSJason Wang 	return ret;
35983f9c10b0SAmit Shah }
35993f9c10b0SAmit Shah 
3600fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
3601fbf28d78SMichael Dalton static ssize_t mergeable_rx_buffer_size_show(struct netdev_rx_queue *queue,
3602718ad681Sstephen hemminger 		char *buf)
3603fbf28d78SMichael Dalton {
3604fbf28d78SMichael Dalton 	struct virtnet_info *vi = netdev_priv(queue->dev);
3605fbf28d78SMichael Dalton 	unsigned int queue_index = get_netdev_rx_queue_index(queue);
36063cc81a9aSJason Wang 	unsigned int headroom = virtnet_get_headroom(vi);
36073cc81a9aSJason Wang 	unsigned int tailroom = headroom ? sizeof(struct skb_shared_info) : 0;
36085377d758SJohannes Berg 	struct ewma_pkt_len *avg;
3609fbf28d78SMichael Dalton 
3610fbf28d78SMichael Dalton 	BUG_ON(queue_index >= vi->max_queue_pairs);
3611fbf28d78SMichael Dalton 	avg = &vi->rq[queue_index].mrg_avg_pkt_len;
3612d85b758fSMichael S. Tsirkin 	return sprintf(buf, "%u\n",
36133cc81a9aSJason Wang 		       get_mergeable_buf_len(&vi->rq[queue_index], avg,
36143cc81a9aSJason Wang 				       SKB_DATA_ALIGN(headroom + tailroom)));
3615fbf28d78SMichael Dalton }
3616fbf28d78SMichael Dalton 
3617fbf28d78SMichael Dalton static struct rx_queue_attribute mergeable_rx_buffer_size_attribute =
3618fbf28d78SMichael Dalton 	__ATTR_RO(mergeable_rx_buffer_size);
3619fbf28d78SMichael Dalton 
3620fbf28d78SMichael Dalton static struct attribute *virtio_net_mrg_rx_attrs[] = {
3621fbf28d78SMichael Dalton 	&mergeable_rx_buffer_size_attribute.attr,
3622fbf28d78SMichael Dalton 	NULL
3623fbf28d78SMichael Dalton };
3624fbf28d78SMichael Dalton 
3625fbf28d78SMichael Dalton static const struct attribute_group virtio_net_mrg_rx_group = {
3626fbf28d78SMichael Dalton 	.name = "virtio_net",
3627fbf28d78SMichael Dalton 	.attrs = virtio_net_mrg_rx_attrs
3628fbf28d78SMichael Dalton };
3629fbf28d78SMichael Dalton #endif
3630fbf28d78SMichael Dalton 
3631892d6eb1SJason Wang static bool virtnet_fail_on_feature(struct virtio_device *vdev,
3632892d6eb1SJason Wang 				    unsigned int fbit,
3633892d6eb1SJason Wang 				    const char *fname, const char *dname)
3634892d6eb1SJason Wang {
3635892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, fbit))
3636892d6eb1SJason Wang 		return false;
3637892d6eb1SJason Wang 
3638892d6eb1SJason Wang 	dev_err(&vdev->dev, "device advertises feature %s but not %s",
3639892d6eb1SJason Wang 		fname, dname);
3640892d6eb1SJason Wang 
3641892d6eb1SJason Wang 	return true;
3642892d6eb1SJason Wang }
3643892d6eb1SJason Wang 
3644892d6eb1SJason Wang #define VIRTNET_FAIL_ON(vdev, fbit, dbit)			\
3645892d6eb1SJason Wang 	virtnet_fail_on_feature(vdev, fbit, #fbit, dbit)
3646892d6eb1SJason Wang 
3647892d6eb1SJason Wang static bool virtnet_validate_features(struct virtio_device *vdev)
3648892d6eb1SJason Wang {
3649892d6eb1SJason Wang 	if (!virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ) &&
3650892d6eb1SJason Wang 	    (VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_RX,
3651892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3652892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_VLAN,
3653892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3654892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE,
3655892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ") ||
3656892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_MQ, "VIRTIO_NET_F_CTRL_VQ") ||
3657892d6eb1SJason Wang 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR,
3658c7114b12SAndrew Melnychenko 			     "VIRTIO_NET_F_CTRL_VQ") ||
3659c7114b12SAndrew Melnychenko 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_RSS,
366091f41f01SAndrew Melnychenko 			     "VIRTIO_NET_F_CTRL_VQ") ||
366191f41f01SAndrew Melnychenko 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_HASH_REPORT,
3662699b045aSAlvaro Karsz 			     "VIRTIO_NET_F_CTRL_VQ") ||
3663699b045aSAlvaro Karsz 	     VIRTNET_FAIL_ON(vdev, VIRTIO_NET_F_NOTF_COAL,
3664892d6eb1SJason Wang 			     "VIRTIO_NET_F_CTRL_VQ"))) {
3665892d6eb1SJason Wang 		return false;
3666892d6eb1SJason Wang 	}
3667892d6eb1SJason Wang 
3668892d6eb1SJason Wang 	return true;
3669892d6eb1SJason Wang }
3670892d6eb1SJason Wang 
3671d0c2c997SJarod Wilson #define MIN_MTU ETH_MIN_MTU
3672d0c2c997SJarod Wilson #define MAX_MTU ETH_MAX_MTU
3673d0c2c997SJarod Wilson 
3674fe36cbe0SMichael S. Tsirkin static int virtnet_validate(struct virtio_device *vdev)
3675296f96fcSRusty Russell {
36766ba42248SMichael S. Tsirkin 	if (!vdev->config->get) {
36776ba42248SMichael S. Tsirkin 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
36786ba42248SMichael S. Tsirkin 			__func__);
36796ba42248SMichael S. Tsirkin 		return -EINVAL;
36806ba42248SMichael S. Tsirkin 	}
36816ba42248SMichael S. Tsirkin 
3682892d6eb1SJason Wang 	if (!virtnet_validate_features(vdev))
3683892d6eb1SJason Wang 		return -EINVAL;
3684892d6eb1SJason Wang 
3685fe36cbe0SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
3686fe36cbe0SMichael S. Tsirkin 		int mtu = virtio_cread16(vdev,
3687fe36cbe0SMichael S. Tsirkin 					 offsetof(struct virtio_net_config,
3688fe36cbe0SMichael S. Tsirkin 						  mtu));
3689fe36cbe0SMichael S. Tsirkin 		if (mtu < MIN_MTU)
3690fe36cbe0SMichael S. Tsirkin 			__virtio_clear_bit(vdev, VIRTIO_NET_F_MTU);
3691fe36cbe0SMichael S. Tsirkin 	}
3692fe36cbe0SMichael S. Tsirkin 
3693fe36cbe0SMichael S. Tsirkin 	return 0;
3694fe36cbe0SMichael S. Tsirkin }
3695fe36cbe0SMichael S. Tsirkin 
369646cd26f4SGavin Li static bool virtnet_check_guest_gso(const struct virtnet_info *vi)
369746cd26f4SGavin Li {
369846cd26f4SGavin Li 	return virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO4) ||
369946cd26f4SGavin Li 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_TSO6) ||
370046cd26f4SGavin Li 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_ECN) ||
3701418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_UFO) ||
3702418044e1SAndrew Melnychenko 		(virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO4) &&
3703418044e1SAndrew Melnychenko 		virtio_has_feature(vi->vdev, VIRTIO_NET_F_GUEST_USO6));
370446cd26f4SGavin Li }
370546cd26f4SGavin Li 
37064959aebbSGavin Li static void virtnet_set_big_packets(struct virtnet_info *vi, const int mtu)
37074959aebbSGavin Li {
37084959aebbSGavin Li 	bool guest_gso = virtnet_check_guest_gso(vi);
37094959aebbSGavin Li 
37104959aebbSGavin Li 	/* If device can receive ANY guest GSO packets, regardless of mtu,
37114959aebbSGavin Li 	 * allocate packets of maximum size, otherwise limit it to only
37124959aebbSGavin Li 	 * mtu size worth only.
37134959aebbSGavin Li 	 */
37144959aebbSGavin Li 	if (mtu > ETH_DATA_LEN || guest_gso) {
37154959aebbSGavin Li 		vi->big_packets = true;
37164959aebbSGavin Li 		vi->big_packets_num_skbfrags = guest_gso ? MAX_SKB_FRAGS : DIV_ROUND_UP(mtu, PAGE_SIZE);
37174959aebbSGavin Li 	}
37184959aebbSGavin Li }
37194959aebbSGavin Li 
3720fe36cbe0SMichael S. Tsirkin static int virtnet_probe(struct virtio_device *vdev)
3721fe36cbe0SMichael S. Tsirkin {
3722d7dfc5cfSToshiaki Makita 	int i, err = -ENOMEM;
3723fe36cbe0SMichael S. Tsirkin 	struct net_device *dev;
3724fe36cbe0SMichael S. Tsirkin 	struct virtnet_info *vi;
3725fe36cbe0SMichael S. Tsirkin 	u16 max_queue_pairs;
37264959aebbSGavin Li 	int mtu = 0;
3727fe36cbe0SMichael S. Tsirkin 
3728c7114b12SAndrew Melnychenko 	/* Find if host supports multiqueue/rss virtio_net device */
3729c7114b12SAndrew Melnychenko 	max_queue_pairs = 1;
3730c7114b12SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MQ) || virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3731c7114b12SAndrew Melnychenko 		max_queue_pairs =
3732c7114b12SAndrew Melnychenko 		     virtio_cread16(vdev, offsetof(struct virtio_net_config, max_virtqueue_pairs));
3733986a4f4dSJason Wang 
3734986a4f4dSJason Wang 	/* We need at least 2 queue's */
3735c7114b12SAndrew Melnychenko 	if (max_queue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
3736986a4f4dSJason Wang 	    max_queue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
3737986a4f4dSJason Wang 	    !virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3738986a4f4dSJason Wang 		max_queue_pairs = 1;
3739296f96fcSRusty Russell 
3740296f96fcSRusty Russell 	/* Allocate ourselves a network device with room for our info */
3741986a4f4dSJason Wang 	dev = alloc_etherdev_mq(sizeof(struct virtnet_info), max_queue_pairs);
3742296f96fcSRusty Russell 	if (!dev)
3743296f96fcSRusty Russell 		return -ENOMEM;
3744296f96fcSRusty Russell 
3745296f96fcSRusty Russell 	/* Set up network device as normal. */
3746ab5bd583SXuan Zhuo 	dev->priv_flags |= IFF_UNICAST_FLT | IFF_LIVE_ADDR_CHANGE |
3747ab5bd583SXuan Zhuo 			   IFF_TX_SKB_NO_LINEAR;
374876288b4eSStephen Hemminger 	dev->netdev_ops = &virtnet_netdev;
3749296f96fcSRusty Russell 	dev->features = NETIF_F_HIGHDMA;
37503fa2a1dfSstephen hemminger 
37517ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &virtnet_ethtool_ops;
3752296f96fcSRusty Russell 	SET_NETDEV_DEV(dev, &vdev->dev);
3753296f96fcSRusty Russell 
3754296f96fcSRusty Russell 	/* Do we support "hardware" checksums? */
375598e778c9SMichał Mirosław 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CSUM)) {
3756296f96fcSRusty Russell 		/* This opens up the world of extra features. */
375748900cb6SJason Wang 		dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_SG;
375898e778c9SMichał Mirosław 		if (csum)
375948900cb6SJason Wang 			dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
376098e778c9SMichał Mirosław 
376198e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_GSO)) {
3762e078de03SDavid S. Miller 			dev->hw_features |= NETIF_F_TSO
376334a48579SRusty Russell 				| NETIF_F_TSO_ECN | NETIF_F_TSO6;
376434a48579SRusty Russell 		}
37655539ae96SRusty Russell 		/* Individual feature bits: what can host handle? */
376698e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO4))
376798e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO;
376898e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_TSO6))
376998e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO6;
377098e778c9SMichał Mirosław 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_ECN))
377198e778c9SMichał Mirosław 			dev->hw_features |= NETIF_F_TSO_ECN;
3772418044e1SAndrew Melnychenko 		if (virtio_has_feature(vdev, VIRTIO_NET_F_HOST_USO))
3773418044e1SAndrew Melnychenko 			dev->hw_features |= NETIF_F_GSO_UDP_L4;
377498e778c9SMichał Mirosław 
377541f2f127SJason Wang 		dev->features |= NETIF_F_GSO_ROBUST;
377641f2f127SJason Wang 
377798e778c9SMichał Mirosław 		if (gso)
3778e078de03SDavid S. Miller 			dev->features |= dev->hw_features & NETIF_F_ALL_TSO;
377998e778c9SMichał Mirosław 		/* (!csum && gso) case will be fixed by register_netdev() */
3780296f96fcSRusty Russell 	}
37814f49129bSThomas Huth 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_CSUM))
37824f49129bSThomas Huth 		dev->features |= NETIF_F_RXCSUM;
3783a02e8964SWillem de Bruijn 	if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
3784a02e8964SWillem de Bruijn 	    virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO6))
3785dbcf24d1SJason Wang 		dev->features |= NETIF_F_GRO_HW;
3786cf8691cbSMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS))
3787dbcf24d1SJason Wang 		dev->hw_features |= NETIF_F_GRO_HW;
3788296f96fcSRusty Russell 
37894fda8302SJason Wang 	dev->vlan_features = dev->features;
37904fda8302SJason Wang 
3791d0c2c997SJarod Wilson 	/* MTU range: 68 - 65535 */
3792d0c2c997SJarod Wilson 	dev->min_mtu = MIN_MTU;
3793d0c2c997SJarod Wilson 	dev->max_mtu = MAX_MTU;
3794d0c2c997SJarod Wilson 
3795296f96fcSRusty Russell 	/* Configuration may specify what MAC to use.  Otherwise random. */
3796f2edaa4aSJakub Kicinski 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MAC)) {
3797f2edaa4aSJakub Kicinski 		u8 addr[ETH_ALEN];
3798f2edaa4aSJakub Kicinski 
3799855e0c52SRusty Russell 		virtio_cread_bytes(vdev,
3800a586d4f6SRusty Russell 				   offsetof(struct virtio_net_config, mac),
3801f2edaa4aSJakub Kicinski 				   addr, ETH_ALEN);
3802f2edaa4aSJakub Kicinski 		eth_hw_addr_set(dev, addr);
3803f2edaa4aSJakub Kicinski 	} else {
3804f2cedb63SDanny Kukawka 		eth_hw_addr_random(dev);
3805f2edaa4aSJakub Kicinski 	}
3806296f96fcSRusty Russell 
3807296f96fcSRusty Russell 	/* Set up our device-specific information */
3808296f96fcSRusty Russell 	vi = netdev_priv(dev);
3809296f96fcSRusty Russell 	vi->dev = dev;
3810296f96fcSRusty Russell 	vi->vdev = vdev;
3811d9d5dcc8SChristian Borntraeger 	vdev->priv = vi;
3812827da44cSJohn Stultz 
3813586d17c5SJason Wang 	INIT_WORK(&vi->config_work, virtnet_config_changed_work);
38145a159128SJason Wang 	spin_lock_init(&vi->refill_lock);
3815296f96fcSRusty Russell 
38163f2c31d9SMark McLoughlin 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF))
38173f2c31d9SMark McLoughlin 		vi->mergeable_rx_bufs = true;
38183f2c31d9SMark McLoughlin 
3819699b045aSAlvaro Karsz 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_NOTF_COAL)) {
3820699b045aSAlvaro Karsz 		vi->rx_usecs = 0;
3821699b045aSAlvaro Karsz 		vi->tx_usecs = 0;
3822699b045aSAlvaro Karsz 		vi->tx_max_packets = 0;
3823699b045aSAlvaro Karsz 		vi->rx_max_packets = 0;
3824699b045aSAlvaro Karsz 	}
3825699b045aSAlvaro Karsz 
382691f41f01SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_HASH_REPORT))
382791f41f01SAndrew Melnychenko 		vi->has_rss_hash_report = true;
382891f41f01SAndrew Melnychenko 
382991f41f01SAndrew Melnychenko 	if (virtio_has_feature(vdev, VIRTIO_NET_F_RSS))
3830c7114b12SAndrew Melnychenko 		vi->has_rss = true;
383191f41f01SAndrew Melnychenko 
383291f41f01SAndrew Melnychenko 	if (vi->has_rss || vi->has_rss_hash_report) {
3833c7114b12SAndrew Melnychenko 		vi->rss_indir_table_size =
3834c7114b12SAndrew Melnychenko 			virtio_cread16(vdev, offsetof(struct virtio_net_config,
3835c7114b12SAndrew Melnychenko 				rss_max_indirection_table_length));
3836c7114b12SAndrew Melnychenko 		vi->rss_key_size =
3837c7114b12SAndrew Melnychenko 			virtio_cread8(vdev, offsetof(struct virtio_net_config, rss_max_key_size));
3838c7114b12SAndrew Melnychenko 
3839c7114b12SAndrew Melnychenko 		vi->rss_hash_types_supported =
3840c7114b12SAndrew Melnychenko 		    virtio_cread32(vdev, offsetof(struct virtio_net_config, supported_hash_types));
3841c7114b12SAndrew Melnychenko 		vi->rss_hash_types_supported &=
3842c7114b12SAndrew Melnychenko 				~(VIRTIO_NET_RSS_HASH_TYPE_IP_EX |
3843c7114b12SAndrew Melnychenko 				  VIRTIO_NET_RSS_HASH_TYPE_TCP_EX |
3844c7114b12SAndrew Melnychenko 				  VIRTIO_NET_RSS_HASH_TYPE_UDP_EX);
3845c7114b12SAndrew Melnychenko 
3846c7114b12SAndrew Melnychenko 		dev->hw_features |= NETIF_F_RXHASH;
3847c7114b12SAndrew Melnychenko 	}
384891f41f01SAndrew Melnychenko 
384991f41f01SAndrew Melnychenko 	if (vi->has_rss_hash_report)
385091f41f01SAndrew Melnychenko 		vi->hdr_len = sizeof(struct virtio_net_hdr_v1_hash);
385191f41f01SAndrew Melnychenko 	else if (virtio_has_feature(vdev, VIRTIO_NET_F_MRG_RXBUF) ||
3852d04302b3SMichael S. Tsirkin 		 virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3853012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
3854012873d0SMichael S. Tsirkin 	else
3855012873d0SMichael S. Tsirkin 		vi->hdr_len = sizeof(struct virtio_net_hdr);
3856012873d0SMichael S. Tsirkin 
385775993300SMichael S. Tsirkin 	if (virtio_has_feature(vdev, VIRTIO_F_ANY_LAYOUT) ||
385875993300SMichael S. Tsirkin 	    virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
3859e7428e95SMichael S. Tsirkin 		vi->any_header_sg = true;
3860e7428e95SMichael S. Tsirkin 
3861986a4f4dSJason Wang 	if (virtio_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ))
3862986a4f4dSJason Wang 		vi->has_cvq = true;
3863986a4f4dSJason Wang 
386414de9d11SAaron Conole 	if (virtio_has_feature(vdev, VIRTIO_NET_F_MTU)) {
386514de9d11SAaron Conole 		mtu = virtio_cread16(vdev,
386614de9d11SAaron Conole 				     offsetof(struct virtio_net_config,
386714de9d11SAaron Conole 					      mtu));
386893a205eeSAaron Conole 		if (mtu < dev->min_mtu) {
3869fe36cbe0SMichael S. Tsirkin 			/* Should never trigger: MTU was previously validated
3870fe36cbe0SMichael S. Tsirkin 			 * in virtnet_validate.
3871fe36cbe0SMichael S. Tsirkin 			 */
38727934b481SYuval Shaia 			dev_err(&vdev->dev,
38737934b481SYuval Shaia 				"device MTU appears to have changed it is now %d < %d",
38747934b481SYuval Shaia 				mtu, dev->min_mtu);
3875411ea23aSDan Carpenter 			err = -EINVAL;
3876d7dfc5cfSToshiaki Makita 			goto free;
3877fe36cbe0SMichael S. Tsirkin 		}
3878fe36cbe0SMichael S. Tsirkin 
3879d0c2c997SJarod Wilson 		dev->mtu = mtu;
388093a205eeSAaron Conole 		dev->max_mtu = mtu;
388114de9d11SAaron Conole 	}
388214de9d11SAaron Conole 
38834959aebbSGavin Li 	virtnet_set_big_packets(vi, mtu);
38844959aebbSGavin Li 
3885012873d0SMichael S. Tsirkin 	if (vi->any_header_sg)
3886012873d0SMichael S. Tsirkin 		dev->needed_headroom = vi->hdr_len;
38876ebbc1a6SZhangjie \(HZ\) 
388844900010SJason Wang 	/* Enable multiqueue by default */
388944900010SJason Wang 	if (num_online_cpus() >= max_queue_pairs)
389044900010SJason Wang 		vi->curr_queue_pairs = max_queue_pairs;
389144900010SJason Wang 	else
389244900010SJason Wang 		vi->curr_queue_pairs = num_online_cpus();
3893986a4f4dSJason Wang 	vi->max_queue_pairs = max_queue_pairs;
3894986a4f4dSJason Wang 
3895986a4f4dSJason Wang 	/* Allocate/initialize the rx/tx queues, and invoke find_vqs */
38963f9c10b0SAmit Shah 	err = init_vqs(vi);
3897d2a7dddaSMichael S. Tsirkin 	if (err)
3898d7dfc5cfSToshiaki Makita 		goto free;
3899d2a7dddaSMichael S. Tsirkin 
3900fbf28d78SMichael Dalton #ifdef CONFIG_SYSFS
3901fbf28d78SMichael Dalton 	if (vi->mergeable_rx_bufs)
3902fbf28d78SMichael Dalton 		dev->sysfs_rx_queue_group = &virtio_net_mrg_rx_group;
3903fbf28d78SMichael Dalton #endif
39040f13b66bSZhi Yong Wu 	netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs);
39050f13b66bSZhi Yong Wu 	netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs);
3906986a4f4dSJason Wang 
39072e9ca760SMichael S. Tsirkin 	virtnet_init_settings(dev);
39082e9ca760SMichael S. Tsirkin 
3909ba5e4426SSridhar Samudrala 	if (virtio_has_feature(vdev, VIRTIO_NET_F_STANDBY)) {
3910ba5e4426SSridhar Samudrala 		vi->failover = net_failover_create(vi->dev);
39114b8e6ac4SWei Yongjun 		if (IS_ERR(vi->failover)) {
39124b8e6ac4SWei Yongjun 			err = PTR_ERR(vi->failover);
3913ba5e4426SSridhar Samudrala 			goto free_vqs;
3914ba5e4426SSridhar Samudrala 		}
39154b8e6ac4SWei Yongjun 	}
3916ba5e4426SSridhar Samudrala 
391791f41f01SAndrew Melnychenko 	if (vi->has_rss || vi->has_rss_hash_report)
3918c7114b12SAndrew Melnychenko 		virtnet_init_default_rss(vi);
3919c7114b12SAndrew Melnychenko 
392050c0ada6SJason Wang 	/* serialize netdev register + virtio_device_ready() with ndo_open() */
392150c0ada6SJason Wang 	rtnl_lock();
392250c0ada6SJason Wang 
392350c0ada6SJason Wang 	err = register_netdevice(dev);
3924296f96fcSRusty Russell 	if (err) {
3925296f96fcSRusty Russell 		pr_debug("virtio_net: registering device failed\n");
392650c0ada6SJason Wang 		rtnl_unlock();
3927ba5e4426SSridhar Samudrala 		goto free_failover;
3928296f96fcSRusty Russell 	}
3929b3369c1fSRusty Russell 
39304baf1e33SMichael S. Tsirkin 	virtio_device_ready(vdev);
39314baf1e33SMichael S. Tsirkin 
393250c0ada6SJason Wang 	rtnl_unlock();
393350c0ada6SJason Wang 
39348017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
39358de4b2f3SWanlong Gao 	if (err) {
39368de4b2f3SWanlong Gao 		pr_debug("virtio_net: registering cpu notifier failed\n");
3937f00e35e2Swangyunjian 		goto free_unregister_netdev;
39388de4b2f3SWanlong Gao 	}
39398de4b2f3SWanlong Gao 
3940a220871bSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
394144900010SJason Wang 
3942167c25e4SJason Wang 	/* Assume link up if device can't report link status,
3943167c25e4SJason Wang 	   otherwise get link status from config. */
3944167c25e4SJason Wang 	netif_carrier_off(dev);
3945bda7fab5SJay Vosburgh 	if (virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS)) {
39463b07e9caSTejun Heo 		schedule_work(&vi->config_work);
3947167c25e4SJason Wang 	} else {
3948167c25e4SJason Wang 		vi->status = VIRTIO_NET_S_LINK_UP;
3949faa9b39fSJason Baron 		virtnet_update_settings(vi);
39504783256eSPantelis Koukousoulas 		netif_carrier_on(dev);
3951167c25e4SJason Wang 	}
39529f4d26d0SMark McLoughlin 
39533f93522fSJason Wang 	for (i = 0; i < ARRAY_SIZE(guest_offloads); i++)
39543f93522fSJason Wang 		if (virtio_has_feature(vi->vdev, guest_offloads[i]))
39553f93522fSJason Wang 			set_bit(guest_offloads[i], &vi->guest_offloads);
3956a02e8964SWillem de Bruijn 	vi->guest_offloads_capable = vi->guest_offloads;
39573f93522fSJason Wang 
3958986a4f4dSJason Wang 	pr_debug("virtnet: registered device %s with %d RX and TX vq's\n",
3959986a4f4dSJason Wang 		 dev->name, max_queue_pairs);
3960986a4f4dSJason Wang 
3961296f96fcSRusty Russell 	return 0;
3962296f96fcSRusty Russell 
3963f00e35e2Swangyunjian free_unregister_netdev:
3964b3369c1fSRusty Russell 	unregister_netdev(dev);
3965ba5e4426SSridhar Samudrala free_failover:
3966ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
3967d2a7dddaSMichael S. Tsirkin free_vqs:
3968b0686565SLi Zetao 	virtio_reset_device(vdev);
3969986a4f4dSJason Wang 	cancel_delayed_work_sync(&vi->refill);
3970fb51879dSMichael Dalton 	free_receive_page_frags(vi);
3971e9d7417bSJason Wang 	virtnet_del_vqs(vi);
3972296f96fcSRusty Russell free:
3973296f96fcSRusty Russell 	free_netdev(dev);
3974296f96fcSRusty Russell 	return err;
3975296f96fcSRusty Russell }
3976296f96fcSRusty Russell 
397704486ed0SAmit Shah static void remove_vq_common(struct virtnet_info *vi)
3978296f96fcSRusty Russell {
3979d9679d00SMichael S. Tsirkin 	virtio_reset_device(vi->vdev);
3980830a8a97SShirley Ma 
3981830a8a97SShirley Ma 	/* Free unused buffers in both send and recv, if any. */
39829ab86bbcSShirley Ma 	free_unused_bufs(vi);
3983fb6813f4SRusty Russell 
3984986a4f4dSJason Wang 	free_receive_bufs(vi);
3985d2a7dddaSMichael S. Tsirkin 
3986fb51879dSMichael Dalton 	free_receive_page_frags(vi);
3987fb51879dSMichael Dalton 
3988986a4f4dSJason Wang 	virtnet_del_vqs(vi);
398904486ed0SAmit Shah }
399004486ed0SAmit Shah 
39918cc085d6SBill Pemberton static void virtnet_remove(struct virtio_device *vdev)
399204486ed0SAmit Shah {
399304486ed0SAmit Shah 	struct virtnet_info *vi = vdev->priv;
399404486ed0SAmit Shah 
39958017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
39968de4b2f3SWanlong Gao 
3997102a2786SMichael S. Tsirkin 	/* Make sure no work handler is accessing the device. */
3998102a2786SMichael S. Tsirkin 	flush_work(&vi->config_work);
3999586d17c5SJason Wang 
400004486ed0SAmit Shah 	unregister_netdev(vi->dev);
400104486ed0SAmit Shah 
4002ba5e4426SSridhar Samudrala 	net_failover_destroy(vi->failover);
4003ba5e4426SSridhar Samudrala 
400404486ed0SAmit Shah 	remove_vq_common(vi);
4005fb6813f4SRusty Russell 
400674b2553fSRusty Russell 	free_netdev(vi->dev);
4007296f96fcSRusty Russell }
4008296f96fcSRusty Russell 
400967a75194SArnd Bergmann static __maybe_unused int virtnet_freeze(struct virtio_device *vdev)
40100741bcb5SAmit Shah {
40110741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
40120741bcb5SAmit Shah 
40138017c279SSebastian Andrzej Siewior 	virtnet_cpu_notif_remove(vi);
40149fe7bfceSJohn Fastabend 	virtnet_freeze_down(vdev);
40150741bcb5SAmit Shah 	remove_vq_common(vi);
40160741bcb5SAmit Shah 
40170741bcb5SAmit Shah 	return 0;
40180741bcb5SAmit Shah }
40190741bcb5SAmit Shah 
402067a75194SArnd Bergmann static __maybe_unused int virtnet_restore(struct virtio_device *vdev)
40210741bcb5SAmit Shah {
40220741bcb5SAmit Shah 	struct virtnet_info *vi = vdev->priv;
40239fe7bfceSJohn Fastabend 	int err;
40240741bcb5SAmit Shah 
40259fe7bfceSJohn Fastabend 	err = virtnet_restore_up(vdev);
40260741bcb5SAmit Shah 	if (err)
40270741bcb5SAmit Shah 		return err;
4028986a4f4dSJason Wang 	virtnet_set_queues(vi, vi->curr_queue_pairs);
4029986a4f4dSJason Wang 
40308017c279SSebastian Andrzej Siewior 	err = virtnet_cpu_notif_add(vi);
40313f2869caSXie Yongji 	if (err) {
40323f2869caSXie Yongji 		virtnet_freeze_down(vdev);
40333f2869caSXie Yongji 		remove_vq_common(vi);
4034ec9debbdSJason Wang 		return err;
40353f2869caSXie Yongji 	}
4036ec9debbdSJason Wang 
40370741bcb5SAmit Shah 	return 0;
40380741bcb5SAmit Shah }
40390741bcb5SAmit Shah 
4040296f96fcSRusty Russell static struct virtio_device_id id_table[] = {
4041296f96fcSRusty Russell 	{ VIRTIO_ID_NET, VIRTIO_DEV_ANY_ID },
4042296f96fcSRusty Russell 	{ 0 },
4043296f96fcSRusty Russell };
4044296f96fcSRusty Russell 
4045f3358507SMichael S. Tsirkin #define VIRTNET_FEATURES \
4046f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CSUM, VIRTIO_NET_F_GUEST_CSUM, \
4047f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MAC, \
4048f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6, \
4049f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6, \
4050f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ECN, VIRTIO_NET_F_GUEST_UFO, \
4051418044e1SAndrew Melnychenko 	VIRTIO_NET_F_HOST_USO, VIRTIO_NET_F_GUEST_USO4, VIRTIO_NET_F_GUEST_USO6, \
4052f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS, VIRTIO_NET_F_CTRL_VQ, \
4053f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_RX, VIRTIO_NET_F_CTRL_VLAN, \
4054f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GUEST_ANNOUNCE, VIRTIO_NET_F_MQ, \
4055f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_CTRL_MAC_ADDR, \
4056faa9b39fSJason Baron 	VIRTIO_NET_F_MTU, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, \
4057c7114b12SAndrew Melnychenko 	VIRTIO_NET_F_SPEED_DUPLEX, VIRTIO_NET_F_STANDBY, \
4058699b045aSAlvaro Karsz 	VIRTIO_NET_F_RSS, VIRTIO_NET_F_HASH_REPORT, VIRTIO_NET_F_NOTF_COAL
4059f3358507SMichael S. Tsirkin 
4060c45a6816SRusty Russell static unsigned int features[] = {
4061f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
4062f3358507SMichael S. Tsirkin };
4063f3358507SMichael S. Tsirkin 
4064f3358507SMichael S. Tsirkin static unsigned int features_legacy[] = {
4065f3358507SMichael S. Tsirkin 	VIRTNET_FEATURES,
4066f3358507SMichael S. Tsirkin 	VIRTIO_NET_F_GSO,
4067e7428e95SMichael S. Tsirkin 	VIRTIO_F_ANY_LAYOUT,
4068c45a6816SRusty Russell };
4069c45a6816SRusty Russell 
407022402529SUwe Kleine-König static struct virtio_driver virtio_net_driver = {
4071c45a6816SRusty Russell 	.feature_table = features,
4072c45a6816SRusty Russell 	.feature_table_size = ARRAY_SIZE(features),
4073f3358507SMichael S. Tsirkin 	.feature_table_legacy = features_legacy,
4074f3358507SMichael S. Tsirkin 	.feature_table_size_legacy = ARRAY_SIZE(features_legacy),
4075296f96fcSRusty Russell 	.driver.name =	KBUILD_MODNAME,
4076296f96fcSRusty Russell 	.driver.owner =	THIS_MODULE,
4077296f96fcSRusty Russell 	.id_table =	id_table,
4078fe36cbe0SMichael S. Tsirkin 	.validate =	virtnet_validate,
4079296f96fcSRusty Russell 	.probe =	virtnet_probe,
40808cc085d6SBill Pemberton 	.remove =	virtnet_remove,
40819f4d26d0SMark McLoughlin 	.config_changed = virtnet_config_changed,
408289107000SAaron Lu #ifdef CONFIG_PM_SLEEP
40830741bcb5SAmit Shah 	.freeze =	virtnet_freeze,
40840741bcb5SAmit Shah 	.restore =	virtnet_restore,
40850741bcb5SAmit Shah #endif
4086296f96fcSRusty Russell };
4087296f96fcSRusty Russell 
40888017c279SSebastian Andrzej Siewior static __init int virtio_net_driver_init(void)
40898017c279SSebastian Andrzej Siewior {
40908017c279SSebastian Andrzej Siewior 	int ret;
40918017c279SSebastian Andrzej Siewior 
409273c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "virtio/net:online",
40938017c279SSebastian Andrzej Siewior 				      virtnet_cpu_online,
40948017c279SSebastian Andrzej Siewior 				      virtnet_cpu_down_prep);
40958017c279SSebastian Andrzej Siewior 	if (ret < 0)
40968017c279SSebastian Andrzej Siewior 		goto out;
40978017c279SSebastian Andrzej Siewior 	virtionet_online = ret;
409873c1b41eSThomas Gleixner 	ret = cpuhp_setup_state_multi(CPUHP_VIRT_NET_DEAD, "virtio/net:dead",
40998017c279SSebastian Andrzej Siewior 				      NULL, virtnet_cpu_dead);
41008017c279SSebastian Andrzej Siewior 	if (ret)
41018017c279SSebastian Andrzej Siewior 		goto err_dead;
41028017c279SSebastian Andrzej Siewior 	ret = register_virtio_driver(&virtio_net_driver);
41038017c279SSebastian Andrzej Siewior 	if (ret)
41048017c279SSebastian Andrzej Siewior 		goto err_virtio;
41058017c279SSebastian Andrzej Siewior 	return 0;
41068017c279SSebastian Andrzej Siewior err_virtio:
41078017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
41088017c279SSebastian Andrzej Siewior err_dead:
41098017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
41108017c279SSebastian Andrzej Siewior out:
41118017c279SSebastian Andrzej Siewior 	return ret;
41128017c279SSebastian Andrzej Siewior }
41138017c279SSebastian Andrzej Siewior module_init(virtio_net_driver_init);
41148017c279SSebastian Andrzej Siewior 
41158017c279SSebastian Andrzej Siewior static __exit void virtio_net_driver_exit(void)
41168017c279SSebastian Andrzej Siewior {
4117cfa0ebc9SAndrew Jones 	unregister_virtio_driver(&virtio_net_driver);
41188017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_VIRT_NET_DEAD);
41198017c279SSebastian Andrzej Siewior 	cpuhp_remove_multi_state(virtionet_online);
41208017c279SSebastian Andrzej Siewior }
41218017c279SSebastian Andrzej Siewior module_exit(virtio_net_driver_exit);
4122296f96fcSRusty Russell 
4123296f96fcSRusty Russell MODULE_DEVICE_TABLE(virtio, id_table);
4124296f96fcSRusty Russell MODULE_DESCRIPTION("Virtio network driver");
4125296f96fcSRusty Russell MODULE_LICENSE("GPL");
4126