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