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. */
gve_has_free_tx_qpl_bufs(struct gve_tx_ring * tx,int count)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
gve_alloc_tx_qpl_buf(struct gve_tx_ring * tx)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
gve_free_tx_qpl_bufs(struct gve_tx_ring * tx,struct gve_tx_pending_packet_dqo * pkt)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. */
gve_has_pending_packet(struct gve_tx_ring * tx)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
gve_xdp_tx_flush_dqo(struct gve_priv * priv,u32 xdp_qid)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 *
gve_alloc_pending_packet(struct gve_tx_ring * tx)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
gve_free_pending_packet(struct gve_tx_ring * tx,struct gve_tx_pending_packet_dqo * pending_packet)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
gve_unmap_packet(struct device * dev,struct gve_tx_pending_packet_dqo * pkt)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 */
gve_tx_clean_pending_packets(struct gve_tx_ring * tx)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
gve_tx_stop_ring_dqo(struct gve_priv * priv,int idx)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
gve_tx_free_ring_dqo(struct gve_priv * priv,struct gve_tx_ring * tx,struct gve_tx_alloc_rings_cfg * cfg)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
gve_tx_qpl_buf_init(struct gve_tx_ring * tx)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
gve_tx_start_ring_dqo(struct gve_priv * priv,int idx)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
gve_tx_alloc_ring_dqo(struct gve_priv * priv,struct gve_tx_alloc_rings_cfg * cfg,struct gve_tx_ring * tx,int idx)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
gve_tx_alloc_rings_dqo(struct gve_priv * priv,struct gve_tx_alloc_rings_cfg * cfg)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
gve_tx_free_rings_dqo(struct gve_priv * priv,struct gve_tx_alloc_rings_cfg * cfg)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 */
num_avail_tx_slots(const struct gve_tx_ring * tx)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 */
gve_has_tx_slots_available(struct gve_tx_ring * tx,u32 slots_req)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
gve_has_avail_slots_tx_dqo(struct gve_tx_ring * tx,int desc_count,int buf_count)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 */
gve_maybe_stop_tx_dqo(struct gve_tx_ring * tx,int desc_count,int buf_count)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
gve_extract_tx_metadata_dqo(const struct sk_buff * skb,struct gve_tx_metadata_dqo * metadata)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
gve_tx_fill_pkt_desc_dqo(struct gve_tx_ring * tx,u32 * desc_idx,bool enable_csum,u32 len,u64 addr,s16 compl_tag,bool eop,bool is_gso)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 */
gve_prep_tso(struct sk_buff * skb)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 MSS (gso_size) to be <= 9728 and the total length
581 * of the TSO to be <= 262143.
582 *
583 * However, we don't validate these because:
584 * - Hypervisor enforces a limit of 9K MTU
585 * - Kernel will not produce a TSO larger than 64k
586 */
587
588 if (unlikely(shinfo->gso_size < GVE_TX_MIN_TSO_MSS_DQO))
589 return -1;
590
591 /* Needed because we will modify header. */
592 err = skb_cow_head(skb, 0);
593 if (err < 0)
594 return err;
595
596 l4_start = skb_transport_offset(skb);
597 paylen = skb->len - l4_start;
598
599 switch (shinfo->gso_type) {
600 case SKB_GSO_TCPV4:
601 case SKB_GSO_TCPV6:
602 tcp = tcp_hdr(skb);
603 csum_replace_by_diff(&tcp->check,
604 (__force __wsum)htonl(paylen));
605 header_len = skb_tcp_all_headers(skb);
606 break;
607 case SKB_GSO_UDP_L4:
608 udp = udp_hdr(skb);
609 csum_replace_by_diff(&udp->check,
610 (__force __wsum)htonl(paylen));
611 header_len = sizeof(struct udphdr) + l4_start;
612 break;
613 default:
614 return -EINVAL;
615 }
616
617 if (unlikely(header_len > GVE_TX_MAX_HDR_SIZE_DQO))
618 return -EINVAL;
619
620 return header_len;
621 }
622
gve_tx_fill_tso_ctx_desc(struct gve_tx_tso_context_desc_dqo * desc,const struct sk_buff * skb,const struct gve_tx_metadata_dqo * metadata,int header_len)623 static void gve_tx_fill_tso_ctx_desc(struct gve_tx_tso_context_desc_dqo *desc,
624 const struct sk_buff *skb,
625 const struct gve_tx_metadata_dqo *metadata,
626 int header_len)
627 {
628 *desc = (struct gve_tx_tso_context_desc_dqo){
629 .header_len = header_len,
630 .cmd_dtype = {
631 .dtype = GVE_TX_TSO_CTX_DESC_DTYPE_DQO,
632 .tso = 1,
633 },
634 .flex0 = metadata->bytes[0],
635 .flex5 = metadata->bytes[5],
636 .flex6 = metadata->bytes[6],
637 .flex7 = metadata->bytes[7],
638 .flex8 = metadata->bytes[8],
639 .flex9 = metadata->bytes[9],
640 .flex10 = metadata->bytes[10],
641 .flex11 = metadata->bytes[11],
642 };
643 desc->tso_total_len = skb->len - header_len;
644 desc->mss = skb_shinfo(skb)->gso_size;
645 }
646
647 static void
gve_tx_fill_general_ctx_desc(struct gve_tx_general_context_desc_dqo * desc,const struct gve_tx_metadata_dqo * metadata)648 gve_tx_fill_general_ctx_desc(struct gve_tx_general_context_desc_dqo *desc,
649 const struct gve_tx_metadata_dqo *metadata)
650 {
651 *desc = (struct gve_tx_general_context_desc_dqo){
652 .flex0 = metadata->bytes[0],
653 .flex1 = metadata->bytes[1],
654 .flex2 = metadata->bytes[2],
655 .flex3 = metadata->bytes[3],
656 .flex4 = metadata->bytes[4],
657 .flex5 = metadata->bytes[5],
658 .flex6 = metadata->bytes[6],
659 .flex7 = metadata->bytes[7],
660 .flex8 = metadata->bytes[8],
661 .flex9 = metadata->bytes[9],
662 .flex10 = metadata->bytes[10],
663 .flex11 = metadata->bytes[11],
664 .cmd_dtype = {.dtype = GVE_TX_GENERAL_CTX_DESC_DTYPE_DQO},
665 };
666 }
667
gve_tx_update_tail(struct gve_tx_ring * tx,u32 desc_idx)668 static void gve_tx_update_tail(struct gve_tx_ring *tx, u32 desc_idx)
669 {
670 u32 last_desc_idx = (desc_idx - 1) & tx->mask;
671 u32 last_report_event_interval =
672 (last_desc_idx - tx->dqo_tx.last_re_idx) & tx->mask;
673
674 /* Commit the changes to our state */
675 tx->dqo_tx.tail = desc_idx;
676
677 /* Request a descriptor completion on the last descriptor of the
678 * packet if we are allowed to by the HW enforced interval.
679 */
680
681 if (unlikely(last_report_event_interval >= GVE_TX_MIN_RE_INTERVAL)) {
682 tx->dqo.tx_ring[last_desc_idx].pkt.report_event = true;
683 tx->dqo_tx.last_re_idx = last_desc_idx;
684 }
685 }
686
gve_tx_add_skb_no_copy_dqo(struct gve_tx_ring * tx,struct sk_buff * skb,struct gve_tx_pending_packet_dqo * pkt,s16 completion_tag,u32 * desc_idx,bool is_gso)687 static int gve_tx_add_skb_no_copy_dqo(struct gve_tx_ring *tx,
688 struct sk_buff *skb,
689 struct gve_tx_pending_packet_dqo *pkt,
690 s16 completion_tag,
691 u32 *desc_idx,
692 bool is_gso)
693 {
694 bool enable_csum = skb->ip_summed == CHECKSUM_PARTIAL;
695 const struct skb_shared_info *shinfo = skb_shinfo(skb);
696 int i;
697
698 /* Note: HW requires that the size of a non-TSO packet be within the
699 * range of [17, 9728].
700 *
701 * We don't double check because
702 * - We limited `netdev->min_mtu` to ETH_MIN_MTU.
703 * - Hypervisor won't allow MTU larger than 9216.
704 */
705
706 pkt->num_bufs = 0;
707 /* Map the linear portion of skb */
708 {
709 u32 len = skb_headlen(skb);
710 dma_addr_t addr;
711
712 addr = dma_map_single(tx->dev, skb->data, len, DMA_TO_DEVICE);
713 if (unlikely(dma_mapping_error(tx->dev, addr)))
714 goto err;
715
716 dma_unmap_len_set(pkt, len[pkt->num_bufs], len);
717 dma_unmap_addr_set(pkt, dma[pkt->num_bufs], addr);
718 ++pkt->num_bufs;
719
720 gve_tx_fill_pkt_desc_dqo(tx, desc_idx, enable_csum, len, addr,
721 completion_tag,
722 /*eop=*/shinfo->nr_frags == 0, is_gso);
723 }
724
725 for (i = 0; i < shinfo->nr_frags; i++) {
726 const skb_frag_t *frag = &shinfo->frags[i];
727 bool is_eop = i == (shinfo->nr_frags - 1);
728 u32 len = skb_frag_size(frag);
729 dma_addr_t addr;
730
731 addr = skb_frag_dma_map(tx->dev, frag, 0, len, DMA_TO_DEVICE);
732 if (unlikely(dma_mapping_error(tx->dev, addr)))
733 goto err;
734
735 dma_unmap_len_set(pkt, len[pkt->num_bufs], len);
736 netmem_dma_unmap_addr_set(skb_frag_netmem(frag), pkt,
737 dma[pkt->num_bufs], addr);
738 ++pkt->num_bufs;
739
740 gve_tx_fill_pkt_desc_dqo(tx, desc_idx, enable_csum, len, addr,
741 completion_tag, is_eop, is_gso);
742 }
743
744 return 0;
745 err:
746 for (i = 0; i < pkt->num_bufs; i++) {
747 if (i == 0) {
748 dma_unmap_single(tx->dev,
749 dma_unmap_addr(pkt, dma[i]),
750 dma_unmap_len(pkt, len[i]),
751 DMA_TO_DEVICE);
752 } else {
753 dma_unmap_page(tx->dev,
754 dma_unmap_addr(pkt, dma[i]),
755 dma_unmap_len(pkt, len[i]),
756 DMA_TO_DEVICE);
757 }
758 }
759 pkt->num_bufs = 0;
760 return -1;
761 }
762
763 /* Tx buffer i corresponds to
764 * qpl_page_id = i / GVE_TX_BUFS_PER_PAGE_DQO
765 * qpl_page_offset = (i % GVE_TX_BUFS_PER_PAGE_DQO) * GVE_TX_BUF_SIZE_DQO
766 */
gve_tx_buf_get_addr(struct gve_tx_ring * tx,s16 index,void ** va,dma_addr_t * dma_addr)767 static void gve_tx_buf_get_addr(struct gve_tx_ring *tx,
768 s16 index,
769 void **va, dma_addr_t *dma_addr)
770 {
771 int page_id = index >> (PAGE_SHIFT - GVE_TX_BUF_SHIFT_DQO);
772 int offset = (index & (GVE_TX_BUFS_PER_PAGE_DQO - 1)) << GVE_TX_BUF_SHIFT_DQO;
773
774 *va = page_address(tx->dqo.qpl->pages[page_id]) + offset;
775 *dma_addr = tx->dqo.qpl->page_buses[page_id] + offset;
776 }
777
gve_tx_add_skb_copy_dqo(struct gve_tx_ring * tx,struct sk_buff * skb,struct gve_tx_pending_packet_dqo * pkt,s16 completion_tag,u32 * desc_idx,bool is_gso)778 static int gve_tx_add_skb_copy_dqo(struct gve_tx_ring *tx,
779 struct sk_buff *skb,
780 struct gve_tx_pending_packet_dqo *pkt,
781 s16 completion_tag,
782 u32 *desc_idx,
783 bool is_gso)
784 {
785 bool enable_csum = skb->ip_summed == CHECKSUM_PARTIAL;
786 u32 copy_offset = 0;
787 dma_addr_t dma_addr;
788 u32 copy_len;
789 s16 index;
790 void *va;
791
792 /* Break the packet into buffer size chunks */
793 pkt->num_bufs = 0;
794 while (copy_offset < skb->len) {
795 index = gve_alloc_tx_qpl_buf(tx);
796 if (unlikely(index == -1))
797 goto err;
798
799 gve_tx_buf_get_addr(tx, index, &va, &dma_addr);
800 copy_len = min_t(u32, GVE_TX_BUF_SIZE_DQO,
801 skb->len - copy_offset);
802 skb_copy_bits(skb, copy_offset, va, copy_len);
803
804 copy_offset += copy_len;
805 dma_sync_single_for_device(tx->dev, dma_addr,
806 copy_len, DMA_TO_DEVICE);
807 gve_tx_fill_pkt_desc_dqo(tx, desc_idx, enable_csum,
808 copy_len,
809 dma_addr,
810 completion_tag,
811 copy_offset == skb->len,
812 is_gso);
813
814 pkt->tx_qpl_buf_ids[pkt->num_bufs] = index;
815 ++tx->dqo_tx.alloc_tx_qpl_buf_cnt;
816 ++pkt->num_bufs;
817 }
818
819 return 0;
820 err:
821 /* Should not be here if gve_has_free_tx_qpl_bufs() check is correct */
822 gve_free_tx_qpl_bufs(tx, pkt);
823 return -ENOMEM;
824 }
825
826 /* Returns 0 on success, or < 0 on error.
827 *
828 * Before this function is called, the caller must ensure
829 * gve_has_pending_packet(tx) returns true.
830 */
gve_tx_add_skb_dqo(struct gve_tx_ring * tx,struct sk_buff * skb)831 static int gve_tx_add_skb_dqo(struct gve_tx_ring *tx,
832 struct sk_buff *skb)
833 {
834 const bool is_gso = skb_is_gso(skb);
835 u32 desc_idx = tx->dqo_tx.tail;
836 struct gve_tx_pending_packet_dqo *pkt;
837 struct gve_tx_metadata_dqo metadata;
838 s16 completion_tag;
839
840 pkt = gve_alloc_pending_packet(tx);
841 if (!pkt)
842 return -ENOMEM;
843
844 pkt->skb = skb;
845 pkt->type = GVE_TX_PENDING_PACKET_DQO_SKB;
846 completion_tag = pkt - tx->dqo.pending_packets;
847
848 gve_extract_tx_metadata_dqo(skb, &metadata);
849 if (is_gso) {
850 int header_len = gve_prep_tso(skb);
851
852 if (unlikely(header_len < 0))
853 goto err;
854
855 gve_tx_fill_tso_ctx_desc(&tx->dqo.tx_ring[desc_idx].tso_ctx,
856 skb, &metadata, header_len);
857 desc_idx = (desc_idx + 1) & tx->mask;
858 }
859
860 gve_tx_fill_general_ctx_desc(&tx->dqo.tx_ring[desc_idx].general_ctx,
861 &metadata);
862 desc_idx = (desc_idx + 1) & tx->mask;
863
864 if (tx->dqo.qpl) {
865 if (gve_tx_add_skb_copy_dqo(tx, skb, pkt,
866 completion_tag,
867 &desc_idx, is_gso))
868 goto err;
869 } else {
870 if (gve_tx_add_skb_no_copy_dqo(tx, skb, pkt,
871 completion_tag,
872 &desc_idx, is_gso))
873 goto err;
874 }
875
876 tx->dqo_tx.posted_packet_desc_cnt += pkt->num_bufs;
877
878 gve_tx_update_tail(tx, desc_idx);
879 return 0;
880
881 err:
882 pkt->skb = NULL;
883 gve_free_pending_packet(tx, pkt);
884
885 return -1;
886 }
887
gve_num_descs_per_buf(size_t size)888 static int gve_num_descs_per_buf(size_t size)
889 {
890 return DIV_ROUND_UP(size, GVE_TX_MAX_BUF_SIZE_DQO);
891 }
892
gve_num_buffer_descs_needed(const struct sk_buff * skb)893 static int gve_num_buffer_descs_needed(const struct sk_buff *skb)
894 {
895 const struct skb_shared_info *shinfo = skb_shinfo(skb);
896 int num_descs;
897 int i;
898
899 num_descs = gve_num_descs_per_buf(skb_headlen(skb));
900
901 for (i = 0; i < shinfo->nr_frags; i++) {
902 unsigned int frag_size = skb_frag_size(&shinfo->frags[i]);
903
904 num_descs += gve_num_descs_per_buf(frag_size);
905 }
906
907 return num_descs;
908 }
909
910 /* Returns true if HW is capable of sending TSO represented by `skb`.
911 *
912 * Each segment must not span more than GVE_TX_MAX_DATA_DESCS buffers.
913 * - The header is counted as one buffer for every single segment.
914 * - A buffer which is split between two segments is counted for both.
915 * - If a buffer contains both header and payload, it is counted as two buffers.
916 */
gve_can_send_tso(const struct sk_buff * skb)917 static bool gve_can_send_tso(const struct sk_buff *skb)
918 {
919 const int max_bufs_per_seg = GVE_TX_MAX_DATA_DESCS - 1;
920 const struct skb_shared_info *shinfo = skb_shinfo(skb);
921 const int header_len = skb_tcp_all_headers(skb);
922 const int gso_size = shinfo->gso_size;
923 int cur_seg_num_bufs;
924 int prev_frag_size;
925 int cur_seg_size;
926 int i;
927
928 cur_seg_size = skb_headlen(skb) - header_len;
929 prev_frag_size = skb_headlen(skb);
930 cur_seg_num_bufs = cur_seg_size > 0;
931
932 for (i = 0; i < shinfo->nr_frags; i++) {
933 if (cur_seg_size >= gso_size) {
934 cur_seg_size %= gso_size;
935 cur_seg_num_bufs = cur_seg_size > 0;
936
937 if (prev_frag_size > GVE_TX_MAX_BUF_SIZE_DQO) {
938 int prev_frag_remain = prev_frag_size %
939 GVE_TX_MAX_BUF_SIZE_DQO;
940
941 /* If the last descriptor of the previous frag
942 * is less than cur_seg_size, the segment will
943 * span two descriptors in the previous frag.
944 * Since max gso size (9728) is less than
945 * GVE_TX_MAX_BUF_SIZE_DQO, it is impossible
946 * for the segment to span more than two
947 * descriptors.
948 */
949 if (prev_frag_remain &&
950 cur_seg_size > prev_frag_remain)
951 cur_seg_num_bufs++;
952 }
953 }
954
955 if (unlikely(++cur_seg_num_bufs > max_bufs_per_seg))
956 return false;
957
958 prev_frag_size = skb_frag_size(&shinfo->frags[i]);
959 cur_seg_size += prev_frag_size;
960 }
961
962 return true;
963 }
964
gve_features_check_dqo(struct sk_buff * skb,struct net_device * dev,netdev_features_t features)965 netdev_features_t gve_features_check_dqo(struct sk_buff *skb,
966 struct net_device *dev,
967 netdev_features_t features)
968 {
969 if (skb_is_gso(skb) && !gve_can_send_tso(skb))
970 return features & ~NETIF_F_GSO_MASK;
971
972 return features;
973 }
974
975 /* Attempt to transmit specified SKB.
976 *
977 * Returns 0 if the SKB was transmitted or dropped.
978 * Returns -1 if there is not currently enough space to transmit the SKB.
979 */
gve_try_tx_skb(struct gve_priv * priv,struct gve_tx_ring * tx,struct sk_buff * skb)980 static int gve_try_tx_skb(struct gve_priv *priv, struct gve_tx_ring *tx,
981 struct sk_buff *skb)
982 {
983 int num_buffer_descs;
984 int total_num_descs;
985
986 if (tx->dqo.qpl) {
987 /* We do not need to verify the number of buffers used per
988 * packet or per segment in case of TSO as with 2K size buffers
989 * none of the TX packet rules would be violated.
990 *
991 * gve_can_send_tso() checks that each TCP segment of gso_size is
992 * not distributed over more than 9 SKB frags..
993 */
994 num_buffer_descs = DIV_ROUND_UP(skb->len, GVE_TX_BUF_SIZE_DQO);
995 } else {
996 num_buffer_descs = gve_num_buffer_descs_needed(skb);
997 if (!skb_is_gso(skb)) {
998 if (unlikely(num_buffer_descs > GVE_TX_MAX_DATA_DESCS)) {
999 if (unlikely(skb_linearize(skb) < 0))
1000 goto drop;
1001
1002 num_buffer_descs = 1;
1003 }
1004 }
1005 }
1006
1007 /* Metadata + (optional TSO) + data descriptors. */
1008 total_num_descs = 1 + skb_is_gso(skb) + num_buffer_descs;
1009 if (unlikely(gve_maybe_stop_tx_dqo(tx, total_num_descs,
1010 num_buffer_descs))) {
1011 return -1;
1012 }
1013
1014 if (unlikely(gve_tx_add_skb_dqo(tx, skb) < 0))
1015 goto drop;
1016
1017 netdev_tx_sent_queue(tx->netdev_txq, skb->len);
1018 skb_tx_timestamp(skb);
1019 return 0;
1020
1021 drop:
1022 u64_stats_update_begin(&tx->statss);
1023 tx->dropped_pkt++;
1024 u64_stats_update_end(&tx->statss);
1025 dev_kfree_skb_any(skb);
1026 return 0;
1027 }
1028
gve_xsk_reorder_queue_push_dqo(struct gve_tx_ring * tx,u16 completion_tag)1029 static void gve_xsk_reorder_queue_push_dqo(struct gve_tx_ring *tx,
1030 u16 completion_tag)
1031 {
1032 u32 tail = atomic_read(&tx->dqo_tx.xsk_reorder_queue_tail);
1033
1034 tx->dqo.xsk_reorder_queue[tail] = completion_tag;
1035 tail = (tail + 1) & tx->dqo.complq_mask;
1036 atomic_set_release(&tx->dqo_tx.xsk_reorder_queue_tail, tail);
1037 }
1038
1039 static struct gve_tx_pending_packet_dqo *
gve_xsk_reorder_queue_head(struct gve_tx_ring * tx)1040 gve_xsk_reorder_queue_head(struct gve_tx_ring *tx)
1041 {
1042 u32 head = tx->dqo_compl.xsk_reorder_queue_head;
1043
1044 if (head == tx->dqo_compl.xsk_reorder_queue_tail) {
1045 tx->dqo_compl.xsk_reorder_queue_tail =
1046 atomic_read_acquire(&tx->dqo_tx.xsk_reorder_queue_tail);
1047
1048 if (head == tx->dqo_compl.xsk_reorder_queue_tail)
1049 return NULL;
1050 }
1051
1052 return &tx->dqo.pending_packets[tx->dqo.xsk_reorder_queue[head]];
1053 }
1054
gve_xsk_reorder_queue_pop_dqo(struct gve_tx_ring * tx)1055 static void gve_xsk_reorder_queue_pop_dqo(struct gve_tx_ring *tx)
1056 {
1057 tx->dqo_compl.xsk_reorder_queue_head++;
1058 tx->dqo_compl.xsk_reorder_queue_head &= tx->dqo.complq_mask;
1059 }
1060
1061 /* Transmit a given skb and ring the doorbell. */
gve_tx_dqo(struct sk_buff * skb,struct net_device * dev)1062 netdev_tx_t gve_tx_dqo(struct sk_buff *skb, struct net_device *dev)
1063 {
1064 struct gve_priv *priv = netdev_priv(dev);
1065 struct gve_tx_ring *tx;
1066
1067 tx = &priv->tx[skb_get_queue_mapping(skb)];
1068 if (unlikely(gve_try_tx_skb(priv, tx, skb) < 0)) {
1069 /* We need to ring the txq doorbell -- we have stopped the Tx
1070 * queue for want of resources, but prior calls to gve_tx()
1071 * may have added descriptors without ringing the doorbell.
1072 */
1073 gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1074 return NETDEV_TX_BUSY;
1075 }
1076
1077 if (!netif_xmit_stopped(tx->netdev_txq) && netdev_xmit_more())
1078 return NETDEV_TX_OK;
1079
1080 gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1081 return NETDEV_TX_OK;
1082 }
1083
gve_xsk_tx_dqo(struct gve_priv * priv,struct gve_tx_ring * tx,int budget)1084 static bool gve_xsk_tx_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1085 int budget)
1086 {
1087 struct xsk_buff_pool *pool = tx->xsk_pool;
1088 struct xdp_desc desc;
1089 bool repoll = false;
1090 int sent = 0;
1091
1092 spin_lock(&tx->dqo_tx.xdp_lock);
1093 for (; sent < budget; sent++) {
1094 struct gve_tx_pending_packet_dqo *pkt;
1095 s16 completion_tag;
1096 dma_addr_t addr;
1097 u32 desc_idx;
1098
1099 if (unlikely(!gve_has_avail_slots_tx_dqo(tx, 1, 1))) {
1100 repoll = true;
1101 break;
1102 }
1103
1104 if (!xsk_tx_peek_desc(pool, &desc))
1105 break;
1106
1107 pkt = gve_alloc_pending_packet(tx);
1108 pkt->type = GVE_TX_PENDING_PACKET_DQO_XSK;
1109 pkt->num_bufs = 0;
1110 completion_tag = pkt - tx->dqo.pending_packets;
1111
1112 addr = xsk_buff_raw_get_dma(pool, desc.addr);
1113 xsk_buff_raw_dma_sync_for_device(pool, addr, desc.len);
1114
1115 desc_idx = tx->dqo_tx.tail;
1116 gve_tx_fill_pkt_desc_dqo(tx, &desc_idx,
1117 true, desc.len,
1118 addr, completion_tag, true,
1119 false);
1120 ++pkt->num_bufs;
1121 gve_tx_update_tail(tx, desc_idx);
1122 tx->dqo_tx.posted_packet_desc_cnt += pkt->num_bufs;
1123 gve_xsk_reorder_queue_push_dqo(tx, completion_tag);
1124 }
1125
1126 if (sent) {
1127 gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1128 xsk_tx_release(pool);
1129 }
1130
1131 spin_unlock(&tx->dqo_tx.xdp_lock);
1132
1133 u64_stats_update_begin(&tx->statss);
1134 tx->xdp_xsk_sent += sent;
1135 u64_stats_update_end(&tx->statss);
1136
1137 return (sent == budget) || repoll;
1138 }
1139
add_to_list(struct gve_tx_ring * tx,struct gve_index_list * list,struct gve_tx_pending_packet_dqo * pending_packet)1140 static void add_to_list(struct gve_tx_ring *tx, struct gve_index_list *list,
1141 struct gve_tx_pending_packet_dqo *pending_packet)
1142 {
1143 s16 old_tail, index;
1144
1145 index = pending_packet - tx->dqo.pending_packets;
1146 old_tail = list->tail;
1147 list->tail = index;
1148 if (old_tail == -1)
1149 list->head = index;
1150 else
1151 tx->dqo.pending_packets[old_tail].next = index;
1152
1153 pending_packet->next = -1;
1154 pending_packet->prev = old_tail;
1155 }
1156
remove_from_list(struct gve_tx_ring * tx,struct gve_index_list * list,struct gve_tx_pending_packet_dqo * pkt)1157 static void remove_from_list(struct gve_tx_ring *tx,
1158 struct gve_index_list *list,
1159 struct gve_tx_pending_packet_dqo *pkt)
1160 {
1161 s16 prev_index, next_index;
1162
1163 prev_index = pkt->prev;
1164 next_index = pkt->next;
1165
1166 if (prev_index == -1) {
1167 /* Node is head */
1168 list->head = next_index;
1169 } else {
1170 tx->dqo.pending_packets[prev_index].next = next_index;
1171 }
1172 if (next_index == -1) {
1173 /* Node is tail */
1174 list->tail = prev_index;
1175 } else {
1176 tx->dqo.pending_packets[next_index].prev = prev_index;
1177 }
1178 }
1179
1180 /* Completion types and expected behavior:
1181 * No Miss compl + Packet compl = Packet completed normally.
1182 * Miss compl + Re-inject compl = Packet completed normally.
1183 * No Miss compl + Re-inject compl = Skipped i.e. packet not completed.
1184 * Miss compl + Packet compl = Skipped i.e. packet not completed.
1185 */
gve_handle_packet_completion(struct gve_priv * priv,struct gve_tx_ring * tx,bool is_napi,u16 compl_tag,u64 * bytes,u64 * pkts,bool is_reinjection)1186 static void gve_handle_packet_completion(struct gve_priv *priv,
1187 struct gve_tx_ring *tx, bool is_napi,
1188 u16 compl_tag, u64 *bytes, u64 *pkts,
1189 bool is_reinjection)
1190 {
1191 struct gve_tx_pending_packet_dqo *pending_packet;
1192
1193 if (unlikely(compl_tag >= tx->dqo.num_pending_packets)) {
1194 net_err_ratelimited("%s: Invalid TX completion tag: %d\n",
1195 priv->dev->name, (int)compl_tag);
1196 return;
1197 }
1198
1199 pending_packet = &tx->dqo.pending_packets[compl_tag];
1200
1201 if (unlikely(is_reinjection)) {
1202 if (unlikely(pending_packet->state ==
1203 GVE_PACKET_STATE_TIMED_OUT_COMPL)) {
1204 net_err_ratelimited("%s: Re-injection completion: %d received after timeout.\n",
1205 priv->dev->name, (int)compl_tag);
1206 /* Packet was already completed as a result of timeout,
1207 * so just remove from list and free pending packet.
1208 */
1209 remove_from_list(tx,
1210 &tx->dqo_compl.timed_out_completions,
1211 pending_packet);
1212 gve_free_pending_packet(tx, pending_packet);
1213 return;
1214 }
1215 if (unlikely(pending_packet->state !=
1216 GVE_PACKET_STATE_PENDING_REINJECT_COMPL)) {
1217 /* No outstanding miss completion but packet allocated
1218 * implies packet receives a re-injection completion
1219 * without a prior miss completion. Return without
1220 * completing the packet.
1221 */
1222 net_err_ratelimited("%s: Re-injection completion received without corresponding miss completion: %d\n",
1223 priv->dev->name, (int)compl_tag);
1224 return;
1225 }
1226 remove_from_list(tx, &tx->dqo_compl.miss_completions,
1227 pending_packet);
1228 } else {
1229 /* Packet is allocated but not a pending data completion. */
1230 if (unlikely(pending_packet->state !=
1231 GVE_PACKET_STATE_PENDING_DATA_COMPL)) {
1232 net_err_ratelimited("%s: No pending data completion: %d\n",
1233 priv->dev->name, (int)compl_tag);
1234 return;
1235 }
1236 }
1237 tx->dqo_tx.completed_packet_desc_cnt += pending_packet->num_bufs;
1238
1239 switch (pending_packet->type) {
1240 case GVE_TX_PENDING_PACKET_DQO_SKB:
1241 if (tx->dqo.qpl)
1242 gve_free_tx_qpl_bufs(tx, pending_packet);
1243 else
1244 gve_unmap_packet(tx->dev, pending_packet);
1245 (*pkts)++;
1246 *bytes += pending_packet->skb->len;
1247
1248 napi_consume_skb(pending_packet->skb, is_napi);
1249 pending_packet->skb = NULL;
1250 gve_free_pending_packet(tx, pending_packet);
1251 break;
1252 case GVE_TX_PENDING_PACKET_DQO_XDP_FRAME:
1253 gve_unmap_packet(tx->dev, pending_packet);
1254 (*pkts)++;
1255 *bytes += pending_packet->xdpf->len;
1256
1257 xdp_return_frame(pending_packet->xdpf);
1258 pending_packet->xdpf = NULL;
1259 gve_free_pending_packet(tx, pending_packet);
1260 break;
1261 case GVE_TX_PENDING_PACKET_DQO_XSK:
1262 pending_packet->state = GVE_PACKET_STATE_XSK_COMPLETE;
1263 break;
1264 default:
1265 WARN_ON_ONCE(1);
1266 }
1267 }
1268
gve_handle_miss_completion(struct gve_priv * priv,struct gve_tx_ring * tx,u16 compl_tag,u64 * bytes,u64 * pkts)1269 static void gve_handle_miss_completion(struct gve_priv *priv,
1270 struct gve_tx_ring *tx, u16 compl_tag,
1271 u64 *bytes, u64 *pkts)
1272 {
1273 struct gve_tx_pending_packet_dqo *pending_packet;
1274
1275 if (unlikely(compl_tag >= tx->dqo.num_pending_packets)) {
1276 net_err_ratelimited("%s: Invalid TX completion tag: %d\n",
1277 priv->dev->name, (int)compl_tag);
1278 return;
1279 }
1280
1281 pending_packet = &tx->dqo.pending_packets[compl_tag];
1282 if (unlikely(pending_packet->state !=
1283 GVE_PACKET_STATE_PENDING_DATA_COMPL)) {
1284 net_err_ratelimited("%s: Unexpected packet state: %d for completion tag : %d\n",
1285 priv->dev->name, (int)pending_packet->state,
1286 (int)compl_tag);
1287 return;
1288 }
1289
1290 pending_packet->state = GVE_PACKET_STATE_PENDING_REINJECT_COMPL;
1291 /* jiffies can wraparound but time comparisons can handle overflows. */
1292 pending_packet->timeout_jiffies =
1293 jiffies +
1294 secs_to_jiffies(GVE_REINJECT_COMPL_TIMEOUT);
1295 add_to_list(tx, &tx->dqo_compl.miss_completions, pending_packet);
1296
1297 *bytes += pending_packet->skb->len;
1298 (*pkts)++;
1299 }
1300
remove_miss_completions(struct gve_priv * priv,struct gve_tx_ring * tx)1301 static void remove_miss_completions(struct gve_priv *priv,
1302 struct gve_tx_ring *tx)
1303 {
1304 struct gve_tx_pending_packet_dqo *pending_packet;
1305 s16 next_index;
1306
1307 next_index = tx->dqo_compl.miss_completions.head;
1308 while (next_index != -1) {
1309 pending_packet = &tx->dqo.pending_packets[next_index];
1310 next_index = pending_packet->next;
1311 /* Break early because packets should timeout in order. */
1312 if (time_is_after_jiffies(pending_packet->timeout_jiffies))
1313 break;
1314
1315 remove_from_list(tx, &tx->dqo_compl.miss_completions,
1316 pending_packet);
1317 /* Unmap/free TX buffers and free skb but do not unallocate packet i.e.
1318 * the completion tag is not freed to ensure that the driver
1319 * can take appropriate action if a corresponding valid
1320 * completion is received later.
1321 */
1322 if (tx->dqo.qpl)
1323 gve_free_tx_qpl_bufs(tx, pending_packet);
1324 else
1325 gve_unmap_packet(tx->dev, pending_packet);
1326
1327 /* This indicates the packet was dropped. */
1328 dev_kfree_skb_any(pending_packet->skb);
1329 pending_packet->skb = NULL;
1330
1331 u64_stats_update_begin(&tx->statss);
1332 tx->dropped_pkt++;
1333 u64_stats_update_end(&tx->statss);
1334
1335 net_err_ratelimited("%s: No reinjection completion was received for: %d.\n",
1336 priv->dev->name,
1337 (int)(pending_packet - tx->dqo.pending_packets));
1338
1339 pending_packet->state = GVE_PACKET_STATE_TIMED_OUT_COMPL;
1340 pending_packet->timeout_jiffies =
1341 jiffies +
1342 secs_to_jiffies(GVE_DEALLOCATE_COMPL_TIMEOUT);
1343 /* Maintain pending packet in another list so the packet can be
1344 * unallocated at a later time.
1345 */
1346 add_to_list(tx, &tx->dqo_compl.timed_out_completions,
1347 pending_packet);
1348 }
1349 }
1350
remove_timed_out_completions(struct gve_priv * priv,struct gve_tx_ring * tx)1351 static void remove_timed_out_completions(struct gve_priv *priv,
1352 struct gve_tx_ring *tx)
1353 {
1354 struct gve_tx_pending_packet_dqo *pending_packet;
1355 s16 next_index;
1356
1357 next_index = tx->dqo_compl.timed_out_completions.head;
1358 while (next_index != -1) {
1359 pending_packet = &tx->dqo.pending_packets[next_index];
1360 next_index = pending_packet->next;
1361 /* Break early because packets should timeout in order. */
1362 if (time_is_after_jiffies(pending_packet->timeout_jiffies))
1363 break;
1364
1365 remove_from_list(tx, &tx->dqo_compl.timed_out_completions,
1366 pending_packet);
1367
1368 /* Need to count XSK packets in xsk_tx_completed. */
1369 if (pending_packet->type == GVE_TX_PENDING_PACKET_DQO_XSK)
1370 pending_packet->state = GVE_PACKET_STATE_XSK_COMPLETE;
1371 else
1372 gve_free_pending_packet(tx, pending_packet);
1373 }
1374 }
1375
gve_tx_process_xsk_completions(struct gve_tx_ring * tx)1376 static void gve_tx_process_xsk_completions(struct gve_tx_ring *tx)
1377 {
1378 u32 num_xsks = 0;
1379
1380 while (true) {
1381 struct gve_tx_pending_packet_dqo *pending_packet =
1382 gve_xsk_reorder_queue_head(tx);
1383
1384 if (!pending_packet ||
1385 pending_packet->state != GVE_PACKET_STATE_XSK_COMPLETE)
1386 break;
1387
1388 num_xsks++;
1389 gve_xsk_reorder_queue_pop_dqo(tx);
1390 gve_free_pending_packet(tx, pending_packet);
1391 }
1392
1393 if (num_xsks)
1394 xsk_tx_completed(tx->xsk_pool, num_xsks);
1395 }
1396
gve_clean_tx_done_dqo(struct gve_priv * priv,struct gve_tx_ring * tx,struct napi_struct * napi)1397 int gve_clean_tx_done_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1398 struct napi_struct *napi)
1399 {
1400 u64 reinject_compl_bytes = 0;
1401 u64 reinject_compl_pkts = 0;
1402 int num_descs_cleaned = 0;
1403 u64 miss_compl_bytes = 0;
1404 u64 miss_compl_pkts = 0;
1405 u64 pkt_compl_bytes = 0;
1406 u64 pkt_compl_pkts = 0;
1407
1408 /* Limit in order to avoid blocking for too long */
1409 while (!napi || pkt_compl_pkts < napi->weight) {
1410 struct gve_tx_compl_desc *compl_desc =
1411 &tx->dqo.compl_ring[tx->dqo_compl.head];
1412 u16 type;
1413
1414 if (compl_desc->generation == tx->dqo_compl.cur_gen_bit)
1415 break;
1416
1417 /* Prefetch the next descriptor. */
1418 prefetch(&tx->dqo.compl_ring[(tx->dqo_compl.head + 1) &
1419 tx->dqo.complq_mask]);
1420
1421 /* Do not read data until we own the descriptor */
1422 dma_rmb();
1423 type = compl_desc->type;
1424
1425 if (type == GVE_COMPL_TYPE_DQO_DESC) {
1426 /* This is the last descriptor fetched by HW plus one */
1427 u16 tx_head = le16_to_cpu(compl_desc->tx_head);
1428
1429 atomic_set_release(&tx->dqo_compl.hw_tx_head, tx_head);
1430 } else if (type == GVE_COMPL_TYPE_DQO_PKT) {
1431 u16 compl_tag = le16_to_cpu(compl_desc->completion_tag);
1432 if (compl_tag & GVE_ALT_MISS_COMPL_BIT) {
1433 compl_tag &= ~GVE_ALT_MISS_COMPL_BIT;
1434 gve_handle_miss_completion(priv, tx, compl_tag,
1435 &miss_compl_bytes,
1436 &miss_compl_pkts);
1437 } else {
1438 gve_handle_packet_completion(priv, tx, !!napi,
1439 compl_tag,
1440 &pkt_compl_bytes,
1441 &pkt_compl_pkts,
1442 false);
1443 }
1444 } else if (type == GVE_COMPL_TYPE_DQO_MISS) {
1445 u16 compl_tag = le16_to_cpu(compl_desc->completion_tag);
1446
1447 gve_handle_miss_completion(priv, tx, compl_tag,
1448 &miss_compl_bytes,
1449 &miss_compl_pkts);
1450 } else if (type == GVE_COMPL_TYPE_DQO_REINJECTION) {
1451 u16 compl_tag = le16_to_cpu(compl_desc->completion_tag);
1452
1453 gve_handle_packet_completion(priv, tx, !!napi,
1454 compl_tag,
1455 &reinject_compl_bytes,
1456 &reinject_compl_pkts,
1457 true);
1458 }
1459
1460 tx->dqo_compl.head =
1461 (tx->dqo_compl.head + 1) & tx->dqo.complq_mask;
1462 /* Flip the generation bit when we wrap around */
1463 tx->dqo_compl.cur_gen_bit ^= tx->dqo_compl.head == 0;
1464 num_descs_cleaned++;
1465 }
1466
1467 if (tx->netdev_txq)
1468 netdev_tx_completed_queue(tx->netdev_txq,
1469 pkt_compl_pkts + miss_compl_pkts,
1470 pkt_compl_bytes + miss_compl_bytes);
1471
1472 remove_miss_completions(priv, tx);
1473 remove_timed_out_completions(priv, tx);
1474
1475 if (tx->xsk_pool)
1476 gve_tx_process_xsk_completions(tx);
1477
1478 u64_stats_update_begin(&tx->statss);
1479 tx->bytes_done += pkt_compl_bytes + reinject_compl_bytes;
1480 tx->pkt_done += pkt_compl_pkts + reinject_compl_pkts;
1481 u64_stats_update_end(&tx->statss);
1482 return num_descs_cleaned;
1483 }
1484
gve_tx_poll_dqo(struct gve_notify_block * block,bool do_clean)1485 bool gve_tx_poll_dqo(struct gve_notify_block *block, bool do_clean)
1486 {
1487 struct gve_tx_compl_desc *compl_desc;
1488 struct gve_tx_ring *tx = block->tx;
1489 struct gve_priv *priv = block->priv;
1490
1491 if (do_clean) {
1492 int num_descs_cleaned = gve_clean_tx_done_dqo(priv, tx,
1493 &block->napi);
1494
1495 /* Sync with queue being stopped in `gve_maybe_stop_tx_dqo()` */
1496 mb();
1497
1498 if (netif_tx_queue_stopped(tx->netdev_txq) &&
1499 num_descs_cleaned > 0) {
1500 tx->wake_queue++;
1501 netif_tx_wake_queue(tx->netdev_txq);
1502 }
1503 }
1504
1505 /* Return true if we still have work. */
1506 compl_desc = &tx->dqo.compl_ring[tx->dqo_compl.head];
1507 return compl_desc->generation != tx->dqo_compl.cur_gen_bit;
1508 }
1509
gve_xsk_tx_poll_dqo(struct gve_notify_block * rx_block,int budget)1510 bool gve_xsk_tx_poll_dqo(struct gve_notify_block *rx_block, int budget)
1511 {
1512 struct gve_rx_ring *rx = rx_block->rx;
1513 struct gve_priv *priv = rx->gve;
1514 struct gve_tx_ring *tx;
1515
1516 tx = &priv->tx[gve_xdp_tx_queue_id(priv, rx->q_num)];
1517 if (tx->xsk_pool)
1518 return gve_xsk_tx_dqo(priv, tx, budget);
1519
1520 return 0;
1521 }
1522
gve_xdp_poll_dqo(struct gve_notify_block * block)1523 bool gve_xdp_poll_dqo(struct gve_notify_block *block)
1524 {
1525 struct gve_tx_compl_desc *compl_desc;
1526 struct gve_tx_ring *tx = block->tx;
1527 struct gve_priv *priv = block->priv;
1528
1529 gve_clean_tx_done_dqo(priv, tx, &block->napi);
1530
1531 /* Return true if we still have work. */
1532 compl_desc = &tx->dqo.compl_ring[tx->dqo_compl.head];
1533 return compl_desc->generation != tx->dqo_compl.cur_gen_bit;
1534 }
1535
gve_xdp_xmit_one_dqo(struct gve_priv * priv,struct gve_tx_ring * tx,struct xdp_frame * xdpf)1536 int gve_xdp_xmit_one_dqo(struct gve_priv *priv, struct gve_tx_ring *tx,
1537 struct xdp_frame *xdpf)
1538 {
1539 struct gve_tx_pending_packet_dqo *pkt;
1540 u32 desc_idx = tx->dqo_tx.tail;
1541 s16 completion_tag;
1542 int num_descs = 1;
1543 dma_addr_t addr;
1544 int err;
1545
1546 if (unlikely(!gve_has_tx_slots_available(tx, num_descs)))
1547 return -EBUSY;
1548
1549 pkt = gve_alloc_pending_packet(tx);
1550 if (unlikely(!pkt))
1551 return -EBUSY;
1552
1553 pkt->type = GVE_TX_PENDING_PACKET_DQO_XDP_FRAME;
1554 pkt->num_bufs = 0;
1555 pkt->xdpf = xdpf;
1556 completion_tag = pkt - tx->dqo.pending_packets;
1557
1558 /* Generate Packet Descriptor */
1559 addr = dma_map_single(tx->dev, xdpf->data, xdpf->len, DMA_TO_DEVICE);
1560 err = dma_mapping_error(tx->dev, addr);
1561 if (unlikely(err))
1562 goto err;
1563
1564 dma_unmap_len_set(pkt, len[pkt->num_bufs], xdpf->len);
1565 dma_unmap_addr_set(pkt, dma[pkt->num_bufs], addr);
1566 pkt->num_bufs++;
1567
1568 gve_tx_fill_pkt_desc_dqo(tx, &desc_idx,
1569 false, xdpf->len,
1570 addr, completion_tag, true,
1571 false);
1572
1573 gve_tx_update_tail(tx, desc_idx);
1574 return 0;
1575
1576 err:
1577 pkt->xdpf = NULL;
1578 pkt->num_bufs = 0;
1579 gve_free_pending_packet(tx, pkt);
1580 return err;
1581 }
1582
gve_xdp_xmit_dqo(struct net_device * dev,int n,struct xdp_frame ** frames,u32 flags)1583 int gve_xdp_xmit_dqo(struct net_device *dev, int n, struct xdp_frame **frames,
1584 u32 flags)
1585 {
1586 struct gve_priv *priv = netdev_priv(dev);
1587 struct gve_tx_ring *tx;
1588 int i, err = 0, qid;
1589
1590 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
1591 return -EINVAL;
1592
1593 qid = gve_xdp_tx_queue_id(priv,
1594 smp_processor_id() % priv->tx_cfg.num_xdp_queues);
1595
1596 tx = &priv->tx[qid];
1597
1598 spin_lock(&tx->dqo_tx.xdp_lock);
1599 for (i = 0; i < n; i++) {
1600 err = gve_xdp_xmit_one_dqo(priv, tx, frames[i]);
1601 if (err)
1602 break;
1603 }
1604
1605 if (flags & XDP_XMIT_FLUSH)
1606 gve_tx_put_doorbell_dqo(priv, tx->q_resources, tx->dqo_tx.tail);
1607
1608 spin_unlock(&tx->dqo_tx.xdp_lock);
1609
1610 u64_stats_update_begin(&tx->statss);
1611 tx->xdp_xmit += n;
1612 tx->xdp_xmit_errors += n - i;
1613 u64_stats_update_end(&tx->statss);
1614
1615 return i ? i : err;
1616 }
1617