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