xref: /linux/drivers/net/ethernet/google/gve/gve_tx_dqo.c (revision 296c83b5ccc808c080865eb20fd7a477b0355bb7)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Google virtual Ethernet (gve) driver
3  *
4  * Copyright (C) 2015-2021 Google, Inc.
5  */
6 
7 #include "gve.h"
8 #include "gve_adminq.h"
9 #include "gve_utils.h"
10 #include "gve_dqo.h"
11 #include <net/ip.h>
12 #include <linux/bpf.h>
13 #include <linux/tcp.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <net/xdp_sock_drv.h>
17 
18 /* Returns true if tx_bufs are available. */
19 static bool gve_has_free_tx_qpl_bufs(struct gve_tx_ring *tx, int count)
20 {
21 	int num_avail;
22 
23 	if (!tx->dqo.qpl)
24 		return true;
25 
26 	num_avail = tx->dqo.num_tx_qpl_bufs -
27 		(tx->dqo_tx.alloc_tx_qpl_buf_cnt -
28 		 tx->dqo_tx.free_tx_qpl_buf_cnt);
29 
30 	if (count <= num_avail)
31 		return true;
32 
33 	/* Update cached value from dqo_compl. */
34 	tx->dqo_tx.free_tx_qpl_buf_cnt =
35 		atomic_read_acquire(&tx->dqo_compl.free_tx_qpl_buf_cnt);
36 
37 	num_avail = tx->dqo.num_tx_qpl_bufs -
38 		(tx->dqo_tx.alloc_tx_qpl_buf_cnt -
39 		 tx->dqo_tx.free_tx_qpl_buf_cnt);
40 
41 	return count <= num_avail;
42 }
43 
44 static s16
45 gve_alloc_tx_qpl_buf(struct gve_tx_ring *tx)
46 {
47 	s16 index;
48 
49 	index = tx->dqo_tx.free_tx_qpl_buf_head;
50 
51 	/* No TX buffers available, try to steal the list from the
52 	 * completion handler.
53 	 */
54 	if (unlikely(index == -1)) {
55 		tx->dqo_tx.free_tx_qpl_buf_head =
56 			atomic_xchg(&tx->dqo_compl.free_tx_qpl_buf_head, -1);
57 		index = tx->dqo_tx.free_tx_qpl_buf_head;
58 
59 		if (unlikely(index == -1))
60 			return index;
61 	}
62 
63 	/* Remove TX buf from free list */
64 	tx->dqo_tx.free_tx_qpl_buf_head = tx->dqo.tx_qpl_buf_next[index];
65 
66 	return index;
67 }
68 
69 static void
70 gve_free_tx_qpl_bufs(struct gve_tx_ring *tx,
71 		     struct gve_tx_pending_packet_dqo *pkt)
72 {
73 	s16 index;
74 	int i;
75 
76 	if (!pkt->num_bufs)
77 		return;
78 
79 	index = pkt->tx_qpl_buf_ids[0];
80 	/* Create a linked list of buffers to be added to the free list */
81 	for (i = 1; i < pkt->num_bufs; i++) {
82 		tx->dqo.tx_qpl_buf_next[index] = pkt->tx_qpl_buf_ids[i];
83 		index = pkt->tx_qpl_buf_ids[i];
84 	}
85 
86 	while (true) {
87 		s16 old_head = atomic_read_acquire(&tx->dqo_compl.free_tx_qpl_buf_head);
88 
89 		tx->dqo.tx_qpl_buf_next[index] = old_head;
90 		if (atomic_cmpxchg(&tx->dqo_compl.free_tx_qpl_buf_head,
91 				   old_head,
92 				   pkt->tx_qpl_buf_ids[0]) == old_head) {
93 			break;
94 		}
95 	}
96 
97 	atomic_add(pkt->num_bufs, &tx->dqo_compl.free_tx_qpl_buf_cnt);
98 	pkt->num_bufs = 0;
99 }
100 
101 /* Returns true if a gve_tx_pending_packet_dqo object is available. */
102 static bool gve_has_pending_packet(struct gve_tx_ring *tx)
103 {
104 	/* Check TX path's list. */
105 	if (tx->dqo_tx.free_pending_packets != -1)
106 		return true;
107 
108 	/* Check completion handler's list. */
109 	if (atomic_read_acquire(&tx->dqo_compl.free_pending_packets) != -1)
110 		return true;
111 
112 	return false;
113 }
114 
115 void gve_xdp_tx_flush_dqo(struct gve_priv *priv, u32 xdp_qid)
116 {
117 	u32 tx_qid = gve_xdp_tx_queue_id(priv, xdp_qid);
118 	struct gve_tx_ring *tx = &priv->tx[tx_qid];
119 
120 	gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
121 }
122 
123 static struct gve_tx_pending_packet_dqo *
124 gve_alloc_pending_packet(struct gve_tx_ring *tx)
125 {
126 	struct gve_tx_pending_packet_dqo *pending_packet;
127 	s16 index;
128 
129 	index = tx->dqo_tx.free_pending_packets;
130 
131 	/* No pending_packets available, try to steal the list from the
132 	 * completion handler.
133 	 */
134 	if (unlikely(index == -1)) {
135 		tx->dqo_tx.free_pending_packets =
136 			atomic_xchg(&tx->dqo_compl.free_pending_packets, -1);
137 		index = tx->dqo_tx.free_pending_packets;
138 
139 		if (unlikely(index == -1))
140 			return NULL;
141 	}
142 
143 	pending_packet = &tx->dqo.pending_packets[index];
144 
145 	/* Remove pending_packet from free list */
146 	tx->dqo_tx.free_pending_packets = pending_packet->next;
147 	pending_packet->state = GVE_PACKET_STATE_PENDING_DATA_COMPL;
148 
149 	return pending_packet;
150 }
151 
152 static void
153 gve_free_pending_packet(struct gve_tx_ring *tx,
154 			struct gve_tx_pending_packet_dqo *pending_packet)
155 {
156 	s16 index = pending_packet - tx->dqo.pending_packets;
157 
158 	pending_packet->state = GVE_PACKET_STATE_UNALLOCATED;
159 	while (true) {
160 		s16 old_head = atomic_read_acquire(&tx->dqo_compl.free_pending_packets);
161 
162 		pending_packet->next = old_head;
163 		if (atomic_cmpxchg(&tx->dqo_compl.free_pending_packets,
164 				   old_head, index) == old_head) {
165 			break;
166 		}
167 	}
168 }
169 
170 static void gve_unmap_packet(struct device *dev,
171 			     struct gve_tx_pending_packet_dqo *pkt)
172 {
173 	int i;
174 
175 	if (!pkt->num_bufs)
176 		return;
177 
178 	/* SKB linear portion is guaranteed to be mapped */
179 	dma_unmap_single(dev, dma_unmap_addr(pkt, dma[0]),
180 			 dma_unmap_len(pkt, len[0]), DMA_TO_DEVICE);
181 	for (i = 1; i < pkt->num_bufs; i++) {
182 		netmem_dma_unmap_page_attrs(dev, dma_unmap_addr(pkt, dma[i]),
183 					    dma_unmap_len(pkt, len[i]),
184 					    DMA_TO_DEVICE, 0);
185 	}
186 	pkt->num_bufs = 0;
187 }
188 
189 /* gve_tx_free_desc - Cleans up all pending tx requests and buffers.
190  */
191 static void gve_tx_clean_pending_packets(struct gve_tx_ring *tx)
192 {
193 	int i;
194 
195 	for (i = 0; i < tx->dqo.num_pending_packets; i++) {
196 		struct gve_tx_pending_packet_dqo *cur_state =
197 			&tx->dqo.pending_packets[i];
198 
199 		if (tx->dqo.qpl)
200 			gve_free_tx_qpl_bufs(tx, cur_state);
201 		else
202 			gve_unmap_packet(tx->dev, cur_state);
203 
204 		if (cur_state->skb) {
205 			dev_consume_skb_any(cur_state->skb);
206 			cur_state->skb = NULL;
207 		}
208 	}
209 }
210 
211 void gve_tx_stop_ring_dqo(struct gve_priv *priv, int idx)
212 {
213 	int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
214 	struct gve_tx_ring *tx = &priv->tx[idx];
215 
216 	if (!gve_tx_was_added_to_block(priv, idx))
217 		return;
218 
219 	gve_remove_napi(priv, ntfy_idx);
220 	gve_clean_tx_done_dqo(priv, tx, /*napi=*/NULL);
221 	if (tx->netdev_txq)
222 		netdev_tx_reset_queue(tx->netdev_txq);
223 	gve_tx_clean_pending_packets(tx);
224 	gve_tx_remove_from_block(priv, idx);
225 }
226 
227 static void gve_tx_free_ring_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
228 				 struct gve_tx_alloc_rings_cfg *cfg)
229 {
230 	struct device *hdev = &priv->pdev->dev;
231 	int idx = tx->q_num;
232 	size_t bytes;
233 	u32 qpl_id;
234 
235 	if (tx->q_resources) {
236 		dma_free_coherent(hdev, sizeof(*tx->q_resources),
237 				  tx->q_resources, tx->q_resources_bus);
238 		tx->q_resources = NULL;
239 	}
240 
241 	if (tx->dqo.compl_ring) {
242 		bytes = sizeof(tx->dqo.compl_ring[0]) *
243 			(tx->dqo.complq_mask + 1);
244 		dma_free_coherent(hdev, bytes, tx->dqo.compl_ring,
245 				  tx->complq_bus_dqo);
246 		tx->dqo.compl_ring = NULL;
247 	}
248 
249 	if (tx->dqo.tx_ring) {
250 		bytes = sizeof(tx->dqo.tx_ring[0]) * (tx->mask + 1);
251 		dma_free_coherent(hdev, bytes, tx->dqo.tx_ring, tx->bus);
252 		tx->dqo.tx_ring = NULL;
253 	}
254 
255 	kvfree(tx->dqo.xsk_reorder_queue);
256 	tx->dqo.xsk_reorder_queue = NULL;
257 
258 	kvfree(tx->dqo.pending_packets);
259 	tx->dqo.pending_packets = NULL;
260 
261 	kvfree(tx->dqo.tx_qpl_buf_next);
262 	tx->dqo.tx_qpl_buf_next = NULL;
263 
264 	if (tx->dqo.qpl) {
265 		qpl_id = gve_tx_qpl_id(priv, tx->q_num);
266 		gve_free_queue_page_list(priv, tx->dqo.qpl, qpl_id);
267 		tx->dqo.qpl = NULL;
268 	}
269 
270 	netif_dbg(priv, drv, priv->dev, "freed tx queue %d\n", idx);
271 }
272 
273 static int gve_tx_qpl_buf_init(struct gve_tx_ring *tx)
274 {
275 	int num_tx_qpl_bufs = GVE_TX_BUFS_PER_PAGE_DQO *
276 		tx->dqo.qpl->num_entries;
277 	int i;
278 
279 	tx->dqo.tx_qpl_buf_next = kvzalloc_objs(tx->dqo.tx_qpl_buf_next[0],
280 						num_tx_qpl_bufs);
281 	if (!tx->dqo.tx_qpl_buf_next)
282 		return -ENOMEM;
283 
284 	tx->dqo.num_tx_qpl_bufs = num_tx_qpl_bufs;
285 
286 	/* Generate free TX buf list */
287 	for (i = 0; i < num_tx_qpl_bufs - 1; i++)
288 		tx->dqo.tx_qpl_buf_next[i] = i + 1;
289 	tx->dqo.tx_qpl_buf_next[num_tx_qpl_bufs - 1] = -1;
290 
291 	atomic_set_release(&tx->dqo_compl.free_tx_qpl_buf_head, -1);
292 	return 0;
293 }
294 
295 void gve_tx_start_ring_dqo(struct gve_priv *priv, int idx)
296 {
297 	int ntfy_idx = gve_tx_idx_to_ntfy(priv, idx);
298 	struct gve_tx_ring *tx = &priv->tx[idx];
299 
300 	gve_tx_add_to_block(priv, idx);
301 
302 	if (idx < priv->tx_cfg.num_queues)
303 		tx->netdev_txq = netdev_get_tx_queue(priv->dev, idx);
304 	gve_add_napi(priv, ntfy_idx, gve_napi_poll_dqo);
305 }
306 
307 static int gve_tx_alloc_ring_dqo(struct gve_priv *priv,
308 				 struct gve_tx_alloc_rings_cfg *cfg,
309 				 struct gve_tx_ring *tx,
310 				 int idx)
311 {
312 	struct device *hdev = &priv->pdev->dev;
313 	int num_pending_packets;
314 	size_t bytes;
315 	u32 qpl_id;
316 	int i;
317 
318 	memset(tx, 0, sizeof(*tx));
319 	tx->q_num = idx;
320 	tx->dev = hdev;
321 	spin_lock_init(&tx->dqo_tx.xdp_lock);
322 	atomic_set_release(&tx->dqo_compl.hw_tx_head, 0);
323 
324 	/* Queue sizes must be a power of 2 */
325 	tx->mask = cfg->ring_size - 1;
326 	tx->dqo.complq_mask = tx->mask;
327 
328 	/* The max number of pending packets determines the maximum number of
329 	 * descriptors which maybe written to the completion queue.
330 	 *
331 	 * We must set the number small enough to make sure we never overrun the
332 	 * completion queue.
333 	 */
334 	num_pending_packets = tx->dqo.complq_mask + 1;
335 
336 	/* Reserve space for descriptor completions, which will be reported at
337 	 * most every GVE_TX_MIN_RE_INTERVAL packets.
338 	 */
339 	num_pending_packets -=
340 		(tx->dqo.complq_mask + 1) / GVE_TX_MIN_RE_INTERVAL;
341 
342 	/* Each packet may have at most 2 buffer completions if it receives both
343 	 * a miss and reinjection completion.
344 	 */
345 	num_pending_packets /= 2;
346 
347 	tx->dqo.num_pending_packets = min_t(int, num_pending_packets, S16_MAX);
348 	tx->dqo.pending_packets = kvzalloc_objs(tx->dqo.pending_packets[0],
349 						tx->dqo.num_pending_packets);
350 	if (!tx->dqo.pending_packets)
351 		goto err;
352 
353 	/* Set up linked list of pending packets */
354 	for (i = 0; i < tx->dqo.num_pending_packets - 1; i++)
355 		tx->dqo.pending_packets[i].next = i + 1;
356 
357 	tx->dqo.pending_packets[tx->dqo.num_pending_packets - 1].next = -1;
358 	atomic_set_release(&tx->dqo_compl.free_pending_packets, -1);
359 
360 	/* Only alloc xsk pool for XDP queues */
361 	if (idx >= cfg->qcfg->num_queues && cfg->num_xdp_rings) {
362 		tx->dqo.xsk_reorder_queue =
363 			kvcalloc(tx->dqo.complq_mask + 1,
364 				 sizeof(tx->dqo.xsk_reorder_queue[0]),
365 				 GFP_KERNEL);
366 		if (!tx->dqo.xsk_reorder_queue)
367 			goto err;
368 	}
369 
370 	tx->dqo_compl.miss_completions.head = -1;
371 	tx->dqo_compl.miss_completions.tail = -1;
372 	tx->dqo_compl.timed_out_completions.head = -1;
373 	tx->dqo_compl.timed_out_completions.tail = -1;
374 
375 	bytes = sizeof(tx->dqo.tx_ring[0]) * (tx->mask + 1);
376 	tx->dqo.tx_ring = dma_alloc_coherent(hdev, bytes, &tx->bus, GFP_KERNEL);
377 	if (!tx->dqo.tx_ring)
378 		goto err;
379 
380 	bytes = sizeof(tx->dqo.compl_ring[0]) * (tx->dqo.complq_mask + 1);
381 	tx->dqo.compl_ring = dma_alloc_coherent(hdev, bytes,
382 						&tx->complq_bus_dqo,
383 						GFP_KERNEL);
384 	if (!tx->dqo.compl_ring)
385 		goto err;
386 
387 	tx->q_resources = dma_alloc_coherent(hdev, sizeof(*tx->q_resources),
388 					     &tx->q_resources_bus, GFP_KERNEL);
389 	if (!tx->q_resources)
390 		goto err;
391 
392 	if (!cfg->raw_addressing) {
393 		qpl_id = gve_tx_qpl_id(priv, tx->q_num);
394 
395 		tx->dqo.qpl = gve_alloc_queue_page_list(priv, qpl_id,
396 							cfg->pages_per_qpl);
397 		if (!tx->dqo.qpl)
398 			goto err;
399 
400 		if (gve_tx_qpl_buf_init(tx))
401 			goto err;
402 	}
403 
404 	return 0;
405 
406 err:
407 	gve_tx_free_ring_dqo(priv, tx, cfg);
408 	return -ENOMEM;
409 }
410 
411 int gve_tx_alloc_rings_dqo(struct gve_priv *priv,
412 			   struct gve_tx_alloc_rings_cfg *cfg)
413 {
414 	struct gve_tx_ring *tx = cfg->tx;
415 	int total_queues;
416 	int err = 0;
417 	int i, j;
418 
419 	total_queues = cfg->qcfg->num_queues + cfg->num_xdp_rings;
420 	if (total_queues > cfg->qcfg->max_queues) {
421 		netif_err(priv, drv, priv->dev,
422 			  "Cannot alloc more than the max num of Tx rings\n");
423 		return -EINVAL;
424 	}
425 
426 	tx = kvzalloc_objs(struct gve_tx_ring, cfg->qcfg->max_queues);
427 	if (!tx)
428 		return -ENOMEM;
429 
430 	for (i = 0; i < total_queues; i++) {
431 		err = gve_tx_alloc_ring_dqo(priv, cfg, &tx[i], i);
432 		if (err) {
433 			netif_err(priv, drv, priv->dev,
434 				  "Failed to alloc tx ring=%d: err=%d\n",
435 				  i, err);
436 			goto err;
437 		}
438 	}
439 
440 	cfg->tx = tx;
441 	return 0;
442 
443 err:
444 	for (j = 0; j < i; j++)
445 		gve_tx_free_ring_dqo(priv, &tx[j], cfg);
446 	kvfree(tx);
447 	return err;
448 }
449 
450 void gve_tx_free_rings_dqo(struct gve_priv *priv,
451 			   struct gve_tx_alloc_rings_cfg *cfg)
452 {
453 	struct gve_tx_ring *tx = cfg->tx;
454 	int i;
455 
456 	if (!tx)
457 		return;
458 
459 	for (i = 0; i < cfg->qcfg->num_queues + cfg->qcfg->num_xdp_queues; i++)
460 		gve_tx_free_ring_dqo(priv, &tx[i], cfg);
461 
462 	kvfree(tx);
463 	cfg->tx = NULL;
464 }
465 
466 /* Returns the number of slots available in the ring */
467 static u32 num_avail_tx_slots(const struct gve_tx_ring *tx)
468 {
469 	u32 num_used = (tx->dqo_tx.tail - tx->dqo_tx.head) & tx->mask;
470 
471 	return tx->mask - num_used;
472 }
473 
474 /* Checks if the requested number of slots are available in the ring */
475 static bool gve_has_tx_slots_available(struct gve_tx_ring *tx, u32 slots_req)
476 {
477 	u32 num_avail = num_avail_tx_slots(tx);
478 
479 	slots_req += GVE_TX_MIN_DESC_PREVENT_CACHE_OVERLAP;
480 
481 	if (num_avail >= slots_req)
482 		return true;
483 
484 	/* Update cached TX head pointer */
485 	tx->dqo_tx.head = atomic_read_acquire(&tx->dqo_compl.hw_tx_head);
486 
487 	return num_avail_tx_slots(tx) >= slots_req;
488 }
489 
490 static bool gve_has_avail_slots_tx_dqo(struct gve_tx_ring *tx,
491 				       int desc_count, int buf_count)
492 {
493 	return gve_has_pending_packet(tx) &&
494 		gve_has_tx_slots_available(tx, desc_count) &&
495 		gve_has_free_tx_qpl_bufs(tx, buf_count);
496 }
497 
498 /* Stops the queue if available descriptors is less than 'count'.
499  * Return: 0 if stop is not required.
500  */
501 static int gve_maybe_stop_tx_dqo(struct gve_tx_ring *tx,
502 				 int desc_count, int buf_count)
503 {
504 	if (likely(gve_has_avail_slots_tx_dqo(tx, desc_count, buf_count)))
505 		return 0;
506 
507 	/* No space, so stop the queue */
508 	tx->stop_queue++;
509 	netif_tx_stop_queue(tx->netdev_txq);
510 
511 	/* Sync with restarting queue in `gve_tx_poll_dqo()` */
512 	mb();
513 
514 	/* After stopping queue, check if we can transmit again in order to
515 	 * avoid TOCTOU bug.
516 	 */
517 	if (likely(!gve_has_avail_slots_tx_dqo(tx, desc_count, buf_count)))
518 		return -EBUSY;
519 
520 	netif_tx_start_queue(tx->netdev_txq);
521 	tx->wake_queue++;
522 	return 0;
523 }
524 
525 static void gve_extract_tx_metadata_dqo(const struct sk_buff *skb,
526 					struct gve_tx_metadata_dqo *metadata)
527 {
528 	memset(metadata, 0, sizeof(*metadata));
529 	metadata->version = GVE_TX_METADATA_VERSION_DQO;
530 
531 	if (skb->l4_hash) {
532 		u16 path_hash = skb->hash ^ (skb->hash >> 16);
533 
534 		path_hash &= (1 << 15) - 1;
535 		if (unlikely(path_hash == 0))
536 			path_hash = ~path_hash;
537 
538 		metadata->path_hash = path_hash;
539 	}
540 }
541 
542 static void gve_tx_fill_pkt_desc_dqo(struct gve_tx_ring *tx, u32 *desc_idx,
543 				     bool enable_csum, u32 len, u64 addr,
544 				     s16 compl_tag, bool eop, bool is_gso)
545 {
546 	while (len > 0) {
547 		struct gve_tx_pkt_desc_dqo *desc =
548 			&tx->dqo.tx_ring[*desc_idx].pkt;
549 		u32 cur_len = min_t(u32, len, GVE_TX_MAX_BUF_SIZE_DQO);
550 		bool cur_eop = eop && cur_len == len;
551 
552 		*desc = (struct gve_tx_pkt_desc_dqo){
553 			.buf_addr = cpu_to_le64(addr),
554 			.dtype = GVE_TX_PKT_DESC_DTYPE_DQO,
555 			.end_of_packet = cur_eop,
556 			.checksum_offload_enable = enable_csum,
557 			.compl_tag = cpu_to_le16(compl_tag),
558 			.buf_size = cur_len,
559 		};
560 
561 		addr += cur_len;
562 		len -= cur_len;
563 		*desc_idx = (*desc_idx + 1) & tx->mask;
564 	}
565 }
566 
567 /* Validates and prepares `skb` for TSO.
568  *
569  * Returns header length, or < 0 if invalid.
570  */
571 static int gve_prep_tso(struct sk_buff *skb)
572 {
573 	struct skb_shared_info *shinfo = skb_shinfo(skb);
574 	u32 paylen, l4_start;
575 	struct tcphdr *tcp;
576 	struct udphdr *udp;
577 	int header_len;
578 	int err;
579 
580 	/* Note: HW requires the total length of the TSO to be <= 262143,
581 	 * this is enforced by netif_set_tso_max_size().
582 	 *
583 	 * MSS (gso_size) can not be trusted: packets forwarded from a tap or
584 	 * injected by a packet socket can carry an arbitrary value, while the
585 	 * mss field of the TSO context descriptor is only 14 bits wide.
586 	 *
587 	 * A too big MSS is dropped here instead of being rejected from
588 	 * gve_features_check_dqo(), because software segmentation would
589 	 * produce packets larger than the device can send.
590 	 */
591 	if (unlikely(shinfo->gso_size > GVE_TX_MAX_TSO_MSS_DQO))
592 		return -1;
593 
594 	/* Needed because we will modify header. */
595 	err = skb_cow_head(skb, 0);
596 	if (err < 0)
597 		return err;
598 
599 	l4_start = skb_transport_offset(skb);
600 	paylen = skb->len - l4_start;
601 
602 	switch (shinfo->gso_type) {
603 	case SKB_GSO_TCPV4:
604 	case SKB_GSO_TCPV6:
605 		tcp = tcp_hdr(skb);
606 		csum_replace_by_diff(&tcp->check,
607 				     (__force __wsum)htonl(paylen));
608 		header_len = skb_tcp_all_headers(skb);
609 		break;
610 	case SKB_GSO_UDP_L4:
611 		udp = udp_hdr(skb);
612 		csum_replace_by_diff(&udp->check,
613 				     (__force __wsum)htonl(paylen));
614 		header_len = sizeof(struct udphdr) + l4_start;
615 		break;
616 	default:
617 		return -EINVAL;
618 	}
619 
620 	if (unlikely(header_len > GVE_TX_MAX_HDR_SIZE_DQO))
621 		return -EINVAL;
622 
623 	return header_len;
624 }
625 
626 static void gve_tx_fill_tso_ctx_desc(struct gve_tx_tso_context_desc_dqo *desc,
627 				     const struct sk_buff *skb,
628 				     const struct gve_tx_metadata_dqo *metadata,
629 				     int header_len)
630 {
631 	*desc = (struct gve_tx_tso_context_desc_dqo){
632 		.header_len = header_len,
633 		.cmd_dtype = {
634 			.dtype = GVE_TX_TSO_CTX_DESC_DTYPE_DQO,
635 			.tso = 1,
636 		},
637 		.flex0 = metadata->bytes[0],
638 		.flex5 = metadata->bytes[5],
639 		.flex6 = metadata->bytes[6],
640 		.flex7 = metadata->bytes[7],
641 		.flex8 = metadata->bytes[8],
642 		.flex9 = metadata->bytes[9],
643 		.flex10 = metadata->bytes[10],
644 		.flex11 = metadata->bytes[11],
645 	};
646 	desc->tso_total_len = skb->len - header_len;
647 	desc->mss = skb_shinfo(skb)->gso_size;
648 }
649 
650 static void
651 gve_tx_fill_general_ctx_desc(struct gve_tx_general_context_desc_dqo *desc,
652 			     const struct gve_tx_metadata_dqo *metadata)
653 {
654 	*desc = (struct gve_tx_general_context_desc_dqo){
655 		.flex0 = metadata->bytes[0],
656 		.flex1 = metadata->bytes[1],
657 		.flex2 = metadata->bytes[2],
658 		.flex3 = metadata->bytes[3],
659 		.flex4 = metadata->bytes[4],
660 		.flex5 = metadata->bytes[5],
661 		.flex6 = metadata->bytes[6],
662 		.flex7 = metadata->bytes[7],
663 		.flex8 = metadata->bytes[8],
664 		.flex9 = metadata->bytes[9],
665 		.flex10 = metadata->bytes[10],
666 		.flex11 = metadata->bytes[11],
667 		.cmd_dtype = {.dtype = GVE_TX_GENERAL_CTX_DESC_DTYPE_DQO},
668 	};
669 }
670 
671 static void gve_tx_update_tail(struct gve_tx_ring *tx, u32 desc_idx)
672 {
673 	u32 last_desc_idx = (desc_idx - 1) & tx->mask;
674 	u32 last_report_event_interval =
675 			(last_desc_idx - tx->dqo_tx.last_re_idx) & tx->mask;
676 
677 	/* Commit the changes to our state */
678 	tx->dqo_tx.tail = desc_idx;
679 
680 	/* Request a descriptor completion on the last descriptor of the
681 	 * packet if we are allowed to by the HW enforced interval.
682 	 */
683 
684 	if (unlikely(last_report_event_interval >= GVE_TX_MIN_RE_INTERVAL)) {
685 		tx->dqo.tx_ring[last_desc_idx].pkt.report_event = true;
686 		tx->dqo_tx.last_re_idx = last_desc_idx;
687 	}
688 }
689 
690 static int gve_tx_add_skb_no_copy_dqo(struct gve_tx_ring *tx,
691 				      struct sk_buff *skb,
692 				      struct gve_tx_pending_packet_dqo *pkt,
693 				      s16 completion_tag,
694 				      u32 *desc_idx,
695 				      bool is_gso)
696 {
697 	bool enable_csum = skb->ip_summed == CHECKSUM_PARTIAL;
698 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
699 	int i;
700 
701 	/* Note: HW requires that the size of a non-TSO packet be within the
702 	 * range of [17, 9728].
703 	 *
704 	 * We don't double check because
705 	 * - We limited `netdev->min_mtu` to ETH_MIN_MTU.
706 	 * - Hypervisor won't allow MTU larger than 9216.
707 	 */
708 
709 	pkt->num_bufs = 0;
710 	/* Map the linear portion of skb */
711 	{
712 		u32 len = skb_headlen(skb);
713 		dma_addr_t addr;
714 
715 		addr = dma_map_single(tx->dev, skb->data, len, DMA_TO_DEVICE);
716 		if (unlikely(dma_mapping_error(tx->dev, addr)))
717 			goto err;
718 
719 		dma_unmap_len_set(pkt, len[pkt->num_bufs], len);
720 		dma_unmap_addr_set(pkt, dma[pkt->num_bufs], addr);
721 		++pkt->num_bufs;
722 
723 		gve_tx_fill_pkt_desc_dqo(tx, desc_idx, enable_csum, len, addr,
724 					 completion_tag,
725 					 /*eop=*/shinfo->nr_frags == 0, is_gso);
726 	}
727 
728 	for (i = 0; i < shinfo->nr_frags; i++) {
729 		const skb_frag_t *frag = &shinfo->frags[i];
730 		bool is_eop = i == (shinfo->nr_frags - 1);
731 		u32 len = skb_frag_size(frag);
732 		dma_addr_t addr;
733 
734 		addr = skb_frag_dma_map(tx->dev, frag, 0, len, DMA_TO_DEVICE);
735 		if (unlikely(dma_mapping_error(tx->dev, addr)))
736 			goto err;
737 
738 		dma_unmap_len_set(pkt, len[pkt->num_bufs], len);
739 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), pkt,
740 					  dma[pkt->num_bufs], addr);
741 		++pkt->num_bufs;
742 
743 		gve_tx_fill_pkt_desc_dqo(tx, desc_idx, enable_csum, len, addr,
744 					 completion_tag, is_eop, is_gso);
745 	}
746 
747 	return 0;
748 err:
749 	for (i = 0; i < pkt->num_bufs; i++) {
750 		if (i == 0) {
751 			dma_unmap_single(tx->dev,
752 					 dma_unmap_addr(pkt, dma[i]),
753 					 dma_unmap_len(pkt, len[i]),
754 					 DMA_TO_DEVICE);
755 		} else {
756 			dma_unmap_page(tx->dev,
757 				       dma_unmap_addr(pkt, dma[i]),
758 				       dma_unmap_len(pkt, len[i]),
759 				       DMA_TO_DEVICE);
760 		}
761 	}
762 	pkt->num_bufs = 0;
763 	return -1;
764 }
765 
766 /* Tx buffer i corresponds to
767  * qpl_page_id = i / GVE_TX_BUFS_PER_PAGE_DQO
768  * qpl_page_offset = (i % GVE_TX_BUFS_PER_PAGE_DQO) * GVE_TX_BUF_SIZE_DQO
769  */
770 static void gve_tx_buf_get_addr(struct gve_tx_ring *tx,
771 				s16 index,
772 				void **va, dma_addr_t *dma_addr)
773 {
774 	int page_id = index >> (PAGE_SHIFT - GVE_TX_BUF_SHIFT_DQO);
775 	int offset = (index & (GVE_TX_BUFS_PER_PAGE_DQO - 1)) << GVE_TX_BUF_SHIFT_DQO;
776 
777 	*va = page_address(tx->dqo.qpl->pages[page_id]) + offset;
778 	*dma_addr = tx->dqo.qpl->page_buses[page_id] + offset;
779 }
780 
781 static int gve_tx_add_skb_copy_dqo(struct gve_tx_ring *tx,
782 				   struct sk_buff *skb,
783 				   struct gve_tx_pending_packet_dqo *pkt,
784 				   s16 completion_tag,
785 				   u32 *desc_idx,
786 				   bool is_gso)
787 {
788 	bool enable_csum = skb->ip_summed == CHECKSUM_PARTIAL;
789 	u32 copy_offset = 0;
790 	dma_addr_t dma_addr;
791 	u32 copy_len;
792 	s16 index;
793 	void *va;
794 
795 	/* Break the packet into buffer size chunks */
796 	pkt->num_bufs = 0;
797 	while (copy_offset < skb->len) {
798 		index = gve_alloc_tx_qpl_buf(tx);
799 		if (unlikely(index == -1))
800 			goto err;
801 
802 		gve_tx_buf_get_addr(tx, index, &va, &dma_addr);
803 		copy_len = min_t(u32, GVE_TX_BUF_SIZE_DQO,
804 				 skb->len - copy_offset);
805 		skb_copy_bits(skb, copy_offset, va, copy_len);
806 
807 		copy_offset += copy_len;
808 		dma_sync_single_for_device(tx->dev, dma_addr,
809 					   copy_len, DMA_TO_DEVICE);
810 		gve_tx_fill_pkt_desc_dqo(tx, desc_idx, enable_csum,
811 					 copy_len,
812 					 dma_addr,
813 					 completion_tag,
814 					 copy_offset == skb->len,
815 					 is_gso);
816 
817 		pkt->tx_qpl_buf_ids[pkt->num_bufs] = index;
818 		++tx->dqo_tx.alloc_tx_qpl_buf_cnt;
819 		++pkt->num_bufs;
820 	}
821 
822 	return 0;
823 err:
824 	/* Should not be here if gve_has_free_tx_qpl_bufs() check is correct */
825 	gve_free_tx_qpl_bufs(tx, pkt);
826 	return -ENOMEM;
827 }
828 
829 /* Returns 0 on success, or < 0 on error.
830  *
831  * Before this function is called, the caller must ensure
832  * gve_has_pending_packet(tx) returns true.
833  */
834 static int gve_tx_add_skb_dqo(struct gve_tx_ring *tx,
835 			      struct sk_buff *skb)
836 {
837 	const bool is_gso = skb_is_gso(skb);
838 	u32 desc_idx = tx->dqo_tx.tail;
839 	struct gve_tx_pending_packet_dqo *pkt;
840 	struct gve_tx_metadata_dqo metadata;
841 	s16 completion_tag;
842 
843 	pkt = gve_alloc_pending_packet(tx);
844 	if (!pkt)
845 		return -ENOMEM;
846 
847 	pkt->skb = skb;
848 	pkt->type = GVE_TX_PENDING_PACKET_DQO_SKB;
849 	completion_tag = pkt - tx->dqo.pending_packets;
850 
851 	gve_extract_tx_metadata_dqo(skb, &metadata);
852 	if (is_gso) {
853 		int header_len = gve_prep_tso(skb);
854 
855 		if (unlikely(header_len < 0))
856 			goto err;
857 
858 		gve_tx_fill_tso_ctx_desc(&tx->dqo.tx_ring[desc_idx].tso_ctx,
859 					 skb, &metadata, header_len);
860 		desc_idx = (desc_idx + 1) & tx->mask;
861 	}
862 
863 	gve_tx_fill_general_ctx_desc(&tx->dqo.tx_ring[desc_idx].general_ctx,
864 				     &metadata);
865 	desc_idx = (desc_idx + 1) & tx->mask;
866 
867 	if (tx->dqo.qpl) {
868 		if (gve_tx_add_skb_copy_dqo(tx, skb, pkt,
869 					    completion_tag,
870 					    &desc_idx, is_gso))
871 			goto err;
872 	}  else {
873 		if (gve_tx_add_skb_no_copy_dqo(tx, skb, pkt,
874 					       completion_tag,
875 					       &desc_idx, is_gso))
876 			goto err;
877 	}
878 
879 	tx->dqo_tx.posted_packet_desc_cnt += pkt->num_bufs;
880 
881 	gve_tx_update_tail(tx, desc_idx);
882 	return 0;
883 
884 err:
885 	pkt->skb = NULL;
886 	gve_free_pending_packet(tx, pkt);
887 
888 	return -1;
889 }
890 
891 static int gve_num_descs_per_buf(size_t size)
892 {
893 	return DIV_ROUND_UP(size, GVE_TX_MAX_BUF_SIZE_DQO);
894 }
895 
896 static int gve_num_buffer_descs_needed(const struct sk_buff *skb)
897 {
898 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
899 	int num_descs;
900 	int i;
901 
902 	num_descs = gve_num_descs_per_buf(skb_headlen(skb));
903 
904 	for (i = 0; i < shinfo->nr_frags; i++) {
905 		unsigned int frag_size = skb_frag_size(&shinfo->frags[i]);
906 
907 		num_descs += gve_num_descs_per_buf(frag_size);
908 	}
909 
910 	return num_descs;
911 }
912 
913 /* Returns true if HW is capable of sending TSO represented by `skb`.
914  *
915  * Each segment must not span more than GVE_TX_MAX_DATA_DESCS buffers.
916  * - The header is counted as one buffer for every single segment.
917  * - A buffer which is split between two segments is counted for both.
918  * - If a buffer contains both header and payload, it is counted as two buffers.
919  */
920 static bool gve_can_send_tso(const struct sk_buff *skb)
921 {
922 	const int max_bufs_per_seg = GVE_TX_MAX_DATA_DESCS - 1;
923 	const struct skb_shared_info *shinfo = skb_shinfo(skb);
924 	const int gso_size = shinfo->gso_size;
925 	int cur_seg_num_bufs;
926 	int prev_frag_size;
927 	int cur_seg_size;
928 	int header_len;
929 	int i;
930 
931 	if (unlikely(gso_size < GVE_TX_MIN_TSO_MSS_DQO))
932 		return false;
933 
934 	/* Must match the header length programmed by gve_prep_tso(). */
935 	if (skb_is_gso_tcp(skb))
936 		header_len = skb_tcp_all_headers(skb);
937 	else
938 		header_len = skb_transport_offset(skb) + sizeof(struct udphdr);
939 
940 	cur_seg_size = skb_headlen(skb) - header_len;
941 	prev_frag_size = skb_headlen(skb);
942 	cur_seg_num_bufs = cur_seg_size > 0;
943 
944 	for (i = 0; i < shinfo->nr_frags; i++) {
945 		if (cur_seg_size >= gso_size) {
946 			cur_seg_size %= gso_size;
947 			cur_seg_num_bufs = cur_seg_size > 0;
948 
949 			if (prev_frag_size > GVE_TX_MAX_BUF_SIZE_DQO) {
950 				int prev_frag_remain = prev_frag_size %
951 					GVE_TX_MAX_BUF_SIZE_DQO;
952 
953 				/* If the last descriptor of the previous frag
954 				 * is less than cur_seg_size, the segment will
955 				 * span two descriptors in the previous frag.
956 				 * Since max gso size (9728) is less than
957 				 * GVE_TX_MAX_BUF_SIZE_DQO, it is impossible
958 				 * for the segment to span more than two
959 				 * descriptors.
960 				 */
961 				if (prev_frag_remain &&
962 				    cur_seg_size > prev_frag_remain)
963 					cur_seg_num_bufs++;
964 			}
965 		}
966 
967 		if (unlikely(++cur_seg_num_bufs > max_bufs_per_seg))
968 			return false;
969 
970 		prev_frag_size = skb_frag_size(&shinfo->frags[i]);
971 		cur_seg_size += prev_frag_size;
972 	}
973 
974 	return true;
975 }
976 
977 netdev_features_t gve_features_check_dqo(struct sk_buff *skb,
978 					 struct net_device *dev,
979 					 netdev_features_t features)
980 {
981 	if (!skb_is_gso(skb))
982 		return features;
983 
984 	/* Keep the GSO bits for a too big MSS, so that gve_prep_tso() drops
985 	 * the packet: software segmentation would give packets larger than
986 	 * the device can send.
987 	 */
988 	if (skb_shinfo(skb)->gso_size > GVE_TX_MAX_TSO_MSS_DQO)
989 		return features;
990 
991 	if (!gve_can_send_tso(skb))
992 		return features & ~NETIF_F_GSO_MASK;
993 
994 	return features;
995 }
996 
997 /* Attempt to transmit specified SKB.
998  *
999  * Returns 0 if the SKB was transmitted or dropped.
1000  * Returns -1 if there is not currently enough space to transmit the SKB.
1001  */
1002 static int gve_try_tx_skb(struct gve_priv *priv, struct gve_tx_ring *tx,
1003 			  struct sk_buff *skb)
1004 {
1005 	int num_buffer_descs;
1006 	int total_num_descs;
1007 
1008 	if (tx->dqo.qpl) {
1009 		/* We do not need to verify the number of buffers used per
1010 		 * packet or per segment in case of TSO as with 2K size buffers
1011 		 * none of the TX packet rules would be violated.
1012 		 *
1013 		 * gve_can_send_tso() checks that each TCP segment of gso_size is
1014 		 * not distributed over more than 9 SKB frags..
1015 		 */
1016 		num_buffer_descs = DIV_ROUND_UP(skb->len, GVE_TX_BUF_SIZE_DQO);
1017 	} else {
1018 		num_buffer_descs = gve_num_buffer_descs_needed(skb);
1019 		if (!skb_is_gso(skb)) {
1020 			if (unlikely(num_buffer_descs > GVE_TX_MAX_DATA_DESCS)) {
1021 				if (unlikely(skb_linearize(skb) < 0))
1022 					goto drop;
1023 
1024 				num_buffer_descs = 1;
1025 			}
1026 		}
1027 	}
1028 
1029 	/* Metadata + (optional TSO) + data descriptors. */
1030 	total_num_descs = 1 + skb_is_gso(skb) + num_buffer_descs;
1031 	if (unlikely(gve_maybe_stop_tx_dqo(tx, total_num_descs,
1032 					   num_buffer_descs))) {
1033 		return -1;
1034 	}
1035 
1036 	if (unlikely(gve_tx_add_skb_dqo(tx, skb) < 0))
1037 		goto drop;
1038 
1039 	netdev_tx_sent_queue(tx->netdev_txq, skb->len);
1040 	skb_tx_timestamp(skb);
1041 	return 0;
1042 
1043 drop:
1044 	u64_stats_update_begin(&tx->statss);
1045 	tx->dropped_pkt++;
1046 	u64_stats_update_end(&tx->statss);
1047 	dev_kfree_skb_any(skb);
1048 	return 0;
1049 }
1050 
1051 static void gve_xsk_reorder_queue_push_dqo(struct gve_tx_ring *tx,
1052 					   u16 completion_tag)
1053 {
1054 	u32 tail = atomic_read(&tx->dqo_tx.xsk_reorder_queue_tail);
1055 
1056 	tx->dqo.xsk_reorder_queue[tail] = completion_tag;
1057 	tail = (tail + 1) & tx->dqo.complq_mask;
1058 	atomic_set_release(&tx->dqo_tx.xsk_reorder_queue_tail, tail);
1059 }
1060 
1061 static struct gve_tx_pending_packet_dqo *
1062 gve_xsk_reorder_queue_head(struct gve_tx_ring *tx)
1063 {
1064 	u32 head = tx->dqo_compl.xsk_reorder_queue_head;
1065 
1066 	if (head == tx->dqo_compl.xsk_reorder_queue_tail) {
1067 		tx->dqo_compl.xsk_reorder_queue_tail =
1068 			atomic_read_acquire(&tx->dqo_tx.xsk_reorder_queue_tail);
1069 
1070 		if (head == tx->dqo_compl.xsk_reorder_queue_tail)
1071 			return NULL;
1072 	}
1073 
1074 	return &tx->dqo.pending_packets[tx->dqo.xsk_reorder_queue[head]];
1075 }
1076 
1077 static void gve_xsk_reorder_queue_pop_dqo(struct gve_tx_ring *tx)
1078 {
1079 	tx->dqo_compl.xsk_reorder_queue_head++;
1080 	tx->dqo_compl.xsk_reorder_queue_head &= tx->dqo.complq_mask;
1081 }
1082 
1083 /* Transmit a given skb and ring the doorbell. */
1084 netdev_tx_t gve_tx_dqo(struct sk_buff *skb, struct net_device *dev)
1085 {
1086 	struct gve_priv *priv = netdev_priv(dev);
1087 	struct gve_tx_ring *tx;
1088 
1089 	tx = &priv->tx[skb_get_queue_mapping(skb)];
1090 	if (unlikely(gve_try_tx_skb(priv, tx, skb) < 0)) {
1091 		/* We need to ring the txq doorbell -- we have stopped the Tx
1092 		 * queue for want of resources, but prior calls to gve_tx()
1093 		 * may have added descriptors without ringing the doorbell.
1094 		 */
1095 		gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1096 		return NETDEV_TX_BUSY;
1097 	}
1098 
1099 	if (!netif_xmit_stopped(tx->netdev_txq) && netdev_xmit_more())
1100 		return NETDEV_TX_OK;
1101 
1102 	gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1103 	return NETDEV_TX_OK;
1104 }
1105 
1106 static bool gve_xsk_tx_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1107 			   int budget)
1108 {
1109 	struct xsk_buff_pool *pool = tx->xsk_pool;
1110 	struct xdp_desc desc;
1111 	bool repoll = false;
1112 	int sent = 0;
1113 
1114 	spin_lock(&tx->dqo_tx.xdp_lock);
1115 	for (; sent < budget; sent++) {
1116 		struct gve_tx_pending_packet_dqo *pkt;
1117 		s16 completion_tag;
1118 		dma_addr_t addr;
1119 		u32 desc_idx;
1120 
1121 		if (unlikely(!gve_has_avail_slots_tx_dqo(tx, 1, 1))) {
1122 			repoll = true;
1123 			break;
1124 		}
1125 
1126 		if (!xsk_tx_peek_desc(pool, &desc))
1127 			break;
1128 
1129 		pkt = gve_alloc_pending_packet(tx);
1130 		pkt->type = GVE_TX_PENDING_PACKET_DQO_XSK;
1131 		pkt->num_bufs = 0;
1132 		completion_tag = pkt - tx->dqo.pending_packets;
1133 
1134 		addr = xsk_buff_raw_get_dma(pool, desc.addr);
1135 		xsk_buff_raw_dma_sync_for_device(pool, addr, desc.len);
1136 
1137 		desc_idx = tx->dqo_tx.tail;
1138 		gve_tx_fill_pkt_desc_dqo(tx, &desc_idx,
1139 					 true, desc.len,
1140 					 addr, completion_tag, true,
1141 					 false);
1142 		++pkt->num_bufs;
1143 		gve_tx_update_tail(tx, desc_idx);
1144 		tx->dqo_tx.posted_packet_desc_cnt += pkt->num_bufs;
1145 		gve_xsk_reorder_queue_push_dqo(tx, completion_tag);
1146 	}
1147 
1148 	if (sent) {
1149 		gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1150 		xsk_tx_release(pool);
1151 	}
1152 
1153 	spin_unlock(&tx->dqo_tx.xdp_lock);
1154 
1155 	u64_stats_update_begin(&tx->statss);
1156 	tx->xdp_xsk_sent += sent;
1157 	u64_stats_update_end(&tx->statss);
1158 
1159 	return (sent == budget) || repoll;
1160 }
1161 
1162 static void add_to_list(struct gve_tx_ring *tx, struct gve_index_list *list,
1163 			struct gve_tx_pending_packet_dqo *pending_packet)
1164 {
1165 	s16 old_tail, index;
1166 
1167 	index = pending_packet - tx->dqo.pending_packets;
1168 	old_tail = list->tail;
1169 	list->tail = index;
1170 	if (old_tail == -1)
1171 		list->head = index;
1172 	else
1173 		tx->dqo.pending_packets[old_tail].next = index;
1174 
1175 	pending_packet->next = -1;
1176 	pending_packet->prev = old_tail;
1177 }
1178 
1179 static void remove_from_list(struct gve_tx_ring *tx,
1180 			     struct gve_index_list *list,
1181 			     struct gve_tx_pending_packet_dqo *pkt)
1182 {
1183 	s16 prev_index, next_index;
1184 
1185 	prev_index = pkt->prev;
1186 	next_index = pkt->next;
1187 
1188 	if (prev_index == -1) {
1189 		/* Node is head */
1190 		list->head = next_index;
1191 	} else {
1192 		tx->dqo.pending_packets[prev_index].next = next_index;
1193 	}
1194 	if (next_index == -1) {
1195 		/* Node is tail */
1196 		list->tail = prev_index;
1197 	} else {
1198 		tx->dqo.pending_packets[next_index].prev = prev_index;
1199 	}
1200 }
1201 
1202 /* Completion types and expected behavior:
1203  * No Miss compl + Packet compl = Packet completed normally.
1204  * Miss compl + Re-inject compl = Packet completed normally.
1205  * No Miss compl + Re-inject compl = Skipped i.e. packet not completed.
1206  * Miss compl + Packet compl = Skipped i.e. packet not completed.
1207  */
1208 static void gve_handle_packet_completion(struct gve_priv *priv,
1209 					 struct gve_tx_ring *tx, bool is_napi,
1210 					 u16 compl_tag, u64 *bytes, u64 *pkts,
1211 					 bool is_reinjection)
1212 {
1213 	struct gve_tx_pending_packet_dqo *pending_packet;
1214 
1215 	if (unlikely(compl_tag >= tx->dqo.num_pending_packets)) {
1216 		net_err_ratelimited("%s: Invalid TX completion tag: %d\n",
1217 				    priv->dev->name, (int)compl_tag);
1218 		return;
1219 	}
1220 
1221 	pending_packet = &tx->dqo.pending_packets[compl_tag];
1222 
1223 	if (unlikely(is_reinjection)) {
1224 		if (unlikely(pending_packet->state ==
1225 			     GVE_PACKET_STATE_TIMED_OUT_COMPL)) {
1226 			net_err_ratelimited("%s: Re-injection completion: %d received after timeout.\n",
1227 					    priv->dev->name, (int)compl_tag);
1228 			/* Packet was already completed as a result of timeout,
1229 			 * so just remove from list and free pending packet.
1230 			 */
1231 			remove_from_list(tx,
1232 					 &tx->dqo_compl.timed_out_completions,
1233 					 pending_packet);
1234 			gve_free_pending_packet(tx, pending_packet);
1235 			return;
1236 		}
1237 		if (unlikely(pending_packet->state !=
1238 			     GVE_PACKET_STATE_PENDING_REINJECT_COMPL)) {
1239 			/* No outstanding miss completion but packet allocated
1240 			 * implies packet receives a re-injection completion
1241 			 * without a prior miss completion. Return without
1242 			 * completing the packet.
1243 			 */
1244 			net_err_ratelimited("%s: Re-injection completion received without corresponding miss completion: %d\n",
1245 					    priv->dev->name, (int)compl_tag);
1246 			return;
1247 		}
1248 		remove_from_list(tx, &tx->dqo_compl.miss_completions,
1249 				 pending_packet);
1250 	} else {
1251 		/* Packet is allocated but not a pending data completion. */
1252 		if (unlikely(pending_packet->state !=
1253 			     GVE_PACKET_STATE_PENDING_DATA_COMPL)) {
1254 			net_err_ratelimited("%s: No pending data completion: %d\n",
1255 					    priv->dev->name, (int)compl_tag);
1256 			return;
1257 		}
1258 	}
1259 	tx->dqo_tx.completed_packet_desc_cnt += pending_packet->num_bufs;
1260 
1261 	switch (pending_packet->type) {
1262 	case GVE_TX_PENDING_PACKET_DQO_SKB:
1263 		if (tx->dqo.qpl)
1264 			gve_free_tx_qpl_bufs(tx, pending_packet);
1265 		else
1266 			gve_unmap_packet(tx->dev, pending_packet);
1267 		(*pkts)++;
1268 		*bytes += pending_packet->skb->len;
1269 
1270 		napi_consume_skb(pending_packet->skb, is_napi);
1271 		pending_packet->skb = NULL;
1272 		gve_free_pending_packet(tx, pending_packet);
1273 		break;
1274 	case GVE_TX_PENDING_PACKET_DQO_XDP_FRAME:
1275 		gve_unmap_packet(tx->dev, pending_packet);
1276 		(*pkts)++;
1277 		*bytes += pending_packet->xdpf->len;
1278 
1279 		xdp_return_frame(pending_packet->xdpf);
1280 		pending_packet->xdpf = NULL;
1281 		gve_free_pending_packet(tx, pending_packet);
1282 		break;
1283 	case GVE_TX_PENDING_PACKET_DQO_XSK:
1284 		pending_packet->state = GVE_PACKET_STATE_XSK_COMPLETE;
1285 		break;
1286 	default:
1287 		WARN_ON_ONCE(1);
1288 	}
1289 }
1290 
1291 static void gve_handle_miss_completion(struct gve_priv *priv,
1292 				       struct gve_tx_ring *tx, u16 compl_tag,
1293 				       u64 *bytes, u64 *pkts)
1294 {
1295 	struct gve_tx_pending_packet_dqo *pending_packet;
1296 
1297 	if (unlikely(compl_tag >= tx->dqo.num_pending_packets)) {
1298 		net_err_ratelimited("%s: Invalid TX completion tag: %d\n",
1299 				    priv->dev->name, (int)compl_tag);
1300 		return;
1301 	}
1302 
1303 	pending_packet = &tx->dqo.pending_packets[compl_tag];
1304 	if (unlikely(pending_packet->state !=
1305 				GVE_PACKET_STATE_PENDING_DATA_COMPL)) {
1306 		net_err_ratelimited("%s: Unexpected packet state: %d for completion tag : %d\n",
1307 				    priv->dev->name, (int)pending_packet->state,
1308 				    (int)compl_tag);
1309 		return;
1310 	}
1311 
1312 	pending_packet->state = GVE_PACKET_STATE_PENDING_REINJECT_COMPL;
1313 	/* jiffies can wraparound but time comparisons can handle overflows. */
1314 	pending_packet->timeout_jiffies =
1315 			jiffies +
1316 			secs_to_jiffies(GVE_REINJECT_COMPL_TIMEOUT);
1317 	add_to_list(tx, &tx->dqo_compl.miss_completions, pending_packet);
1318 
1319 	*bytes += pending_packet->skb->len;
1320 	(*pkts)++;
1321 }
1322 
1323 static void remove_miss_completions(struct gve_priv *priv,
1324 				    struct gve_tx_ring *tx)
1325 {
1326 	struct gve_tx_pending_packet_dqo *pending_packet;
1327 	s16 next_index;
1328 
1329 	next_index = tx->dqo_compl.miss_completions.head;
1330 	while (next_index != -1) {
1331 		pending_packet = &tx->dqo.pending_packets[next_index];
1332 		next_index = pending_packet->next;
1333 		/* Break early because packets should timeout in order. */
1334 		if (time_is_after_jiffies(pending_packet->timeout_jiffies))
1335 			break;
1336 
1337 		remove_from_list(tx, &tx->dqo_compl.miss_completions,
1338 				 pending_packet);
1339 		/* Unmap/free TX buffers and free skb but do not unallocate packet i.e.
1340 		 * the completion tag is not freed to ensure that the driver
1341 		 * can take appropriate action if a corresponding valid
1342 		 * completion is received later.
1343 		 */
1344 		if (tx->dqo.qpl)
1345 			gve_free_tx_qpl_bufs(tx, pending_packet);
1346 		else
1347 			gve_unmap_packet(tx->dev, pending_packet);
1348 
1349 		/* This indicates the packet was dropped. */
1350 		dev_kfree_skb_any(pending_packet->skb);
1351 		pending_packet->skb = NULL;
1352 
1353 		u64_stats_update_begin(&tx->statss);
1354 		tx->dropped_pkt++;
1355 		u64_stats_update_end(&tx->statss);
1356 
1357 		net_err_ratelimited("%s: No reinjection completion was received for: %d.\n",
1358 				    priv->dev->name,
1359 				    (int)(pending_packet - tx->dqo.pending_packets));
1360 
1361 		pending_packet->state = GVE_PACKET_STATE_TIMED_OUT_COMPL;
1362 		pending_packet->timeout_jiffies =
1363 				jiffies +
1364 				secs_to_jiffies(GVE_DEALLOCATE_COMPL_TIMEOUT);
1365 		/* Maintain pending packet in another list so the packet can be
1366 		 * unallocated at a later time.
1367 		 */
1368 		add_to_list(tx, &tx->dqo_compl.timed_out_completions,
1369 			    pending_packet);
1370 	}
1371 }
1372 
1373 static void remove_timed_out_completions(struct gve_priv *priv,
1374 					 struct gve_tx_ring *tx)
1375 {
1376 	struct gve_tx_pending_packet_dqo *pending_packet;
1377 	s16 next_index;
1378 
1379 	next_index = tx->dqo_compl.timed_out_completions.head;
1380 	while (next_index != -1) {
1381 		pending_packet = &tx->dqo.pending_packets[next_index];
1382 		next_index = pending_packet->next;
1383 		/* Break early because packets should timeout in order. */
1384 		if (time_is_after_jiffies(pending_packet->timeout_jiffies))
1385 			break;
1386 
1387 		remove_from_list(tx, &tx->dqo_compl.timed_out_completions,
1388 				 pending_packet);
1389 
1390 		/* Need to count XSK packets in xsk_tx_completed. */
1391 		if (pending_packet->type == GVE_TX_PENDING_PACKET_DQO_XSK)
1392 			pending_packet->state = GVE_PACKET_STATE_XSK_COMPLETE;
1393 		else
1394 			gve_free_pending_packet(tx, pending_packet);
1395 	}
1396 }
1397 
1398 static void gve_tx_process_xsk_completions(struct gve_tx_ring *tx)
1399 {
1400 	u32 num_xsks = 0;
1401 
1402 	while (true) {
1403 		struct gve_tx_pending_packet_dqo *pending_packet =
1404 			gve_xsk_reorder_queue_head(tx);
1405 
1406 		if (!pending_packet ||
1407 		    pending_packet->state != GVE_PACKET_STATE_XSK_COMPLETE)
1408 			break;
1409 
1410 		num_xsks++;
1411 		gve_xsk_reorder_queue_pop_dqo(tx);
1412 		gve_free_pending_packet(tx, pending_packet);
1413 	}
1414 
1415 	if (num_xsks)
1416 		xsk_tx_completed(tx->xsk_pool, num_xsks);
1417 }
1418 
1419 int gve_clean_tx_done_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1420 			  struct napi_struct *napi)
1421 {
1422 	u64 reinject_compl_bytes = 0;
1423 	u64 reinject_compl_pkts = 0;
1424 	int num_descs_cleaned = 0;
1425 	u64 miss_compl_bytes = 0;
1426 	u64 miss_compl_pkts = 0;
1427 	u64 pkt_compl_bytes = 0;
1428 	u64 pkt_compl_pkts = 0;
1429 
1430 	/* Limit in order to avoid blocking for too long */
1431 	while (!napi || pkt_compl_pkts < napi->weight) {
1432 		struct gve_tx_compl_desc *compl_desc =
1433 			&tx->dqo.compl_ring[tx->dqo_compl.head];
1434 		u16 type;
1435 
1436 		if (compl_desc->generation == tx->dqo_compl.cur_gen_bit)
1437 			break;
1438 
1439 		/* Prefetch the next descriptor. */
1440 		prefetch(&tx->dqo.compl_ring[(tx->dqo_compl.head + 1) &
1441 				tx->dqo.complq_mask]);
1442 
1443 		/* Do not read data until we own the descriptor */
1444 		dma_rmb();
1445 		type = compl_desc->type;
1446 
1447 		if (type == GVE_COMPL_TYPE_DQO_DESC) {
1448 			/* This is the last descriptor fetched by HW plus one */
1449 			u16 tx_head = le16_to_cpu(compl_desc->tx_head);
1450 
1451 			atomic_set_release(&tx->dqo_compl.hw_tx_head, tx_head);
1452 		} else if (type == GVE_COMPL_TYPE_DQO_PKT) {
1453 			u16 compl_tag = le16_to_cpu(compl_desc->completion_tag);
1454 			if (compl_tag & GVE_ALT_MISS_COMPL_BIT) {
1455 				compl_tag &= ~GVE_ALT_MISS_COMPL_BIT;
1456 				gve_handle_miss_completion(priv, tx, compl_tag,
1457 							   &miss_compl_bytes,
1458 							   &miss_compl_pkts);
1459 			} else {
1460 				gve_handle_packet_completion(priv, tx, !!napi,
1461 							     compl_tag,
1462 							     &pkt_compl_bytes,
1463 							     &pkt_compl_pkts,
1464 							     false);
1465 			}
1466 		} else if (type == GVE_COMPL_TYPE_DQO_MISS) {
1467 			u16 compl_tag = le16_to_cpu(compl_desc->completion_tag);
1468 
1469 			gve_handle_miss_completion(priv, tx, compl_tag,
1470 						   &miss_compl_bytes,
1471 						   &miss_compl_pkts);
1472 		} else if (type == GVE_COMPL_TYPE_DQO_REINJECTION) {
1473 			u16 compl_tag = le16_to_cpu(compl_desc->completion_tag);
1474 
1475 			gve_handle_packet_completion(priv, tx, !!napi,
1476 						     compl_tag,
1477 						     &reinject_compl_bytes,
1478 						     &reinject_compl_pkts,
1479 						     true);
1480 		}
1481 
1482 		tx->dqo_compl.head =
1483 			(tx->dqo_compl.head + 1) & tx->dqo.complq_mask;
1484 		/* Flip the generation bit when we wrap around */
1485 		tx->dqo_compl.cur_gen_bit ^= tx->dqo_compl.head == 0;
1486 		num_descs_cleaned++;
1487 	}
1488 
1489 	if (tx->netdev_txq)
1490 		netdev_tx_completed_queue(tx->netdev_txq,
1491 					  pkt_compl_pkts + miss_compl_pkts,
1492 					  pkt_compl_bytes + miss_compl_bytes);
1493 
1494 	remove_miss_completions(priv, tx);
1495 	remove_timed_out_completions(priv, tx);
1496 
1497 	if (tx->xsk_pool)
1498 		gve_tx_process_xsk_completions(tx);
1499 
1500 	u64_stats_update_begin(&tx->statss);
1501 	tx->bytes_done += pkt_compl_bytes + reinject_compl_bytes;
1502 	tx->pkt_done += pkt_compl_pkts + reinject_compl_pkts;
1503 	u64_stats_update_end(&tx->statss);
1504 	return num_descs_cleaned;
1505 }
1506 
1507 bool gve_tx_poll_dqo(struct gve_notify_block *block, bool do_clean)
1508 {
1509 	struct gve_tx_compl_desc *compl_desc;
1510 	struct gve_tx_ring *tx = block->tx;
1511 	struct gve_priv *priv = block->priv;
1512 
1513 	if (do_clean) {
1514 		int num_descs_cleaned = gve_clean_tx_done_dqo(priv, tx,
1515 							      &block->napi);
1516 
1517 		/* Sync with queue being stopped in `gve_maybe_stop_tx_dqo()` */
1518 		mb();
1519 
1520 		if (netif_tx_queue_stopped(tx->netdev_txq) &&
1521 		    num_descs_cleaned > 0) {
1522 			tx->wake_queue++;
1523 			netif_tx_wake_queue(tx->netdev_txq);
1524 		}
1525 	}
1526 
1527 	/* Return true if we still have work. */
1528 	compl_desc = &tx->dqo.compl_ring[tx->dqo_compl.head];
1529 	return compl_desc->generation != tx->dqo_compl.cur_gen_bit;
1530 }
1531 
1532 bool gve_xsk_tx_poll_dqo(struct gve_notify_block *rx_block, int budget)
1533 {
1534 	struct gve_rx_ring *rx = rx_block->rx;
1535 	struct gve_priv *priv = rx->gve;
1536 	struct gve_tx_ring *tx;
1537 
1538 	tx = &priv->tx[gve_xdp_tx_queue_id(priv, rx->q_num)];
1539 	if (tx->xsk_pool)
1540 		return gve_xsk_tx_dqo(priv, tx, budget);
1541 
1542 	return 0;
1543 }
1544 
1545 bool gve_xdp_poll_dqo(struct gve_notify_block *block)
1546 {
1547 	struct gve_tx_compl_desc *compl_desc;
1548 	struct gve_tx_ring *tx = block->tx;
1549 	struct gve_priv *priv = block->priv;
1550 
1551 	gve_clean_tx_done_dqo(priv, tx, &block->napi);
1552 
1553 	/* Return true if we still have work. */
1554 	compl_desc = &tx->dqo.compl_ring[tx->dqo_compl.head];
1555 	return compl_desc->generation != tx->dqo_compl.cur_gen_bit;
1556 }
1557 
1558 int gve_xdp_xmit_one_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1559 			 struct xdp_frame *xdpf)
1560 {
1561 	struct gve_tx_pending_packet_dqo *pkt;
1562 	u32 desc_idx = tx->dqo_tx.tail;
1563 	s16 completion_tag;
1564 	int num_descs = 1;
1565 	dma_addr_t addr;
1566 	int err;
1567 
1568 	if (unlikely(!gve_has_tx_slots_available(tx, num_descs)))
1569 		return -EBUSY;
1570 
1571 	pkt = gve_alloc_pending_packet(tx);
1572 	if (unlikely(!pkt))
1573 		return -EBUSY;
1574 
1575 	pkt->type = GVE_TX_PENDING_PACKET_DQO_XDP_FRAME;
1576 	pkt->num_bufs = 0;
1577 	pkt->xdpf = xdpf;
1578 	completion_tag = pkt - tx->dqo.pending_packets;
1579 
1580 	/* Generate Packet Descriptor */
1581 	addr = dma_map_single(tx->dev, xdpf->data, xdpf->len, DMA_TO_DEVICE);
1582 	err = dma_mapping_error(tx->dev, addr);
1583 	if (unlikely(err))
1584 		goto err;
1585 
1586 	dma_unmap_len_set(pkt, len[pkt->num_bufs], xdpf->len);
1587 	dma_unmap_addr_set(pkt, dma[pkt->num_bufs], addr);
1588 	pkt->num_bufs++;
1589 
1590 	gve_tx_fill_pkt_desc_dqo(tx, &desc_idx,
1591 				 false, xdpf->len,
1592 				 addr, completion_tag, true,
1593 				 false);
1594 
1595 	gve_tx_update_tail(tx, desc_idx);
1596 	return 0;
1597 
1598 err:
1599 	pkt->xdpf = NULL;
1600 	pkt->num_bufs = 0;
1601 	gve_free_pending_packet(tx, pkt);
1602 	return err;
1603 }
1604 
1605 int gve_xdp_xmit_dqo(struct net_device *dev, int n, struct xdp_frame **frames,
1606 		     u32 flags)
1607 {
1608 	struct gve_priv *priv = netdev_priv(dev);
1609 	struct gve_tx_ring *tx;
1610 	int i, err = 0, qid;
1611 
1612 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1613 		return -EINVAL;
1614 
1615 	qid = gve_xdp_tx_queue_id(priv,
1616 				  smp_processor_id() % priv->tx_cfg.num_xdp_queues);
1617 
1618 	tx = &priv->tx[qid];
1619 
1620 	spin_lock(&tx->dqo_tx.xdp_lock);
1621 	for (i = 0; i < n; i++) {
1622 		err = gve_xdp_xmit_one_dqo(priv, tx, frames[i]);
1623 		if (err)
1624 			break;
1625 	}
1626 
1627 	if (flags & XDP_XMIT_FLUSH)
1628 		gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1629 
1630 	spin_unlock(&tx->dqo_tx.xdp_lock);
1631 
1632 	u64_stats_update_begin(&tx->statss);
1633 	tx->xdp_xmit += n;
1634 	tx->xdp_xmit_errors += n - i;
1635 	u64_stats_update_end(&tx->statss);
1636 
1637 	return i ? i : err;
1638 }
1639