]> Git Repo - J-linux.git/blob - drivers/net/ethernet/google/gve/gve_tx.c
Merge tag 'trace-v5.13-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[J-linux.git] / drivers / net / ethernet / google / gve / gve_tx.c
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Google virtual Ethernet (gve) driver
3  *
4  * Copyright (C) 2015-2019 Google, Inc.
5  */
6
7 #include "gve.h"
8 #include "gve_adminq.h"
9 #include <linux/ip.h>
10 #include <linux/tcp.h>
11 #include <linux/vmalloc.h>
12 #include <linux/skbuff.h>
13
14 static inline void gve_tx_put_doorbell(struct gve_priv *priv,
15                                        struct gve_queue_resources *q_resources,
16                                        u32 val)
17 {
18         iowrite32be(val, &priv->db_bar2[be32_to_cpu(q_resources->db_index)]);
19 }
20
21 /* gvnic can only transmit from a Registered Segment.
22  * We copy skb payloads into the registered segment before writing Tx
23  * descriptors and ringing the Tx doorbell.
24  *
25  * gve_tx_fifo_* manages the Registered Segment as a FIFO - clients must
26  * free allocations in the order they were allocated.
27  */
28
29 static int gve_tx_fifo_init(struct gve_priv *priv, struct gve_tx_fifo *fifo)
30 {
31         fifo->base = vmap(fifo->qpl->pages, fifo->qpl->num_entries, VM_MAP,
32                           PAGE_KERNEL);
33         if (unlikely(!fifo->base)) {
34                 netif_err(priv, drv, priv->dev, "Failed to vmap fifo, qpl_id = %d\n",
35                           fifo->qpl->id);
36                 return -ENOMEM;
37         }
38
39         fifo->size = fifo->qpl->num_entries * PAGE_SIZE;
40         atomic_set(&fifo->available, fifo->size);
41         fifo->head = 0;
42         return 0;
43 }
44
45 static void gve_tx_fifo_release(struct gve_priv *priv, struct gve_tx_fifo *fifo)
46 {
47         WARN(atomic_read(&fifo->available) != fifo->size,
48              "Releasing non-empty fifo");
49
50         vunmap(fifo->base);
51 }
52
53 static int gve_tx_fifo_pad_alloc_one_frag(struct gve_tx_fifo *fifo,
54                                           size_t bytes)
55 {
56         return (fifo->head + bytes < fifo->size) ? 0 : fifo->size - fifo->head;
57 }
58
59 static bool gve_tx_fifo_can_alloc(struct gve_tx_fifo *fifo, size_t bytes)
60 {
61         return (atomic_read(&fifo->available) <= bytes) ? false : true;
62 }
63
64 /* gve_tx_alloc_fifo - Allocate fragment(s) from Tx FIFO
65  * @fifo: FIFO to allocate from
66  * @bytes: Allocation size
67  * @iov: Scatter-gather elements to fill with allocation fragment base/len
68  *
69  * Returns number of valid elements in iov[] or negative on error.
70  *
71  * Allocations from a given FIFO must be externally synchronized but concurrent
72  * allocation and frees are allowed.
73  */
74 static int gve_tx_alloc_fifo(struct gve_tx_fifo *fifo, size_t bytes,
75                              struct gve_tx_iovec iov[2])
76 {
77         size_t overflow, padding;
78         u32 aligned_head;
79         int nfrags = 0;
80
81         if (!bytes)
82                 return 0;
83
84         /* This check happens before we know how much padding is needed to
85          * align to a cacheline boundary for the payload, but that is fine,
86          * because the FIFO head always start aligned, and the FIFO's boundaries
87          * are aligned, so if there is space for the data, there is space for
88          * the padding to the next alignment.
89          */
90         WARN(!gve_tx_fifo_can_alloc(fifo, bytes),
91              "Reached %s when there's not enough space in the fifo", __func__);
92
93         nfrags++;
94
95         iov[0].iov_offset = fifo->head;
96         iov[0].iov_len = bytes;
97         fifo->head += bytes;
98
99         if (fifo->head > fifo->size) {
100                 /* If the allocation did not fit in the tail fragment of the
101                  * FIFO, also use the head fragment.
102                  */
103                 nfrags++;
104                 overflow = fifo->head - fifo->size;
105                 iov[0].iov_len -= overflow;
106                 iov[1].iov_offset = 0;  /* Start of fifo*/
107                 iov[1].iov_len = overflow;
108
109                 fifo->head = overflow;
110         }
111
112         /* Re-align to a cacheline boundary */
113         aligned_head = L1_CACHE_ALIGN(fifo->head);
114         padding = aligned_head - fifo->head;
115         iov[nfrags - 1].iov_padding = padding;
116         atomic_sub(bytes + padding, &fifo->available);
117         fifo->head = aligned_head;
118
119         if (fifo->head == fifo->size)
120                 fifo->head = 0;
121
122         return nfrags;
123 }
124
125 /* gve_tx_free_fifo - Return space to Tx FIFO
126  * @fifo: FIFO to return fragments to
127  * @bytes: Bytes to free
128  */
129 static void gve_tx_free_fifo(struct gve_tx_fifo *fifo, size_t bytes)
130 {
131         atomic_add(bytes, &fifo->available);
132 }
133
134 static void gve_tx_remove_from_block(struct gve_priv *priv, int queue_idx)
135 {
136         struct gve_notify_block *block =
137                         &priv->ntfy_blocks[gve_tx_idx_to_ntfy(priv, queue_idx)];
138
139         block->tx = NULL;
140 }
141
142 static int gve_clean_tx_done(struct gve_priv *priv, struct gve_tx_ring *tx,
143                              u32 to_do, bool try_to_wake);
144
145 static void gve_tx_free_ring(struct gve_priv *priv, int idx)
146 {
147         struct gve_tx_ring *tx = &priv->tx[idx];
148         struct device *hdev = &priv->pdev->dev;
149         size_t bytes;
150         u32 slots;
151
152         gve_tx_remove_from_block(priv, idx);
153         slots = tx->mask + 1;
154         gve_clean_tx_done(priv, tx, tx->req, false);
155         netdev_tx_reset_queue(tx->netdev_txq);
156
157         dma_free_coherent(hdev, sizeof(*tx->q_resources),
158                           tx->q_resources, tx->q_resources_bus);
159         tx->q_resources = NULL;
160
161         if (!tx->raw_addressing) {
162                 gve_tx_fifo_release(priv, &tx->tx_fifo);
163                 gve_unassign_qpl(priv, tx->tx_fifo.qpl->id);
164                 tx->tx_fifo.qpl = NULL;
165         }
166
167         bytes = sizeof(*tx->desc) * slots;
168         dma_free_coherent(hdev, bytes, tx->desc, tx->bus);
169         tx->desc = NULL;
170
171         vfree(tx->info);
172         tx->info = NULL;
173
174         netif_dbg(priv, drv, priv->dev, "freed tx queue %d\n", idx);
175 }
176
177 static void gve_tx_add_to_block(struct gve_priv *priv, int queue_idx)
178 {
179         int ntfy_idx = gve_tx_idx_to_ntfy(priv, queue_idx);
180         struct gve_notify_block *block = &priv->ntfy_blocks[ntfy_idx];
181         struct gve_tx_ring *tx = &priv->tx[queue_idx];
182
183         block->tx = tx;
184         tx->ntfy_id = ntfy_idx;
185 }
186
187 static int gve_tx_alloc_ring(struct gve_priv *priv, int idx)
188 {
189         struct gve_tx_ring *tx = &priv->tx[idx];
190         struct device *hdev = &priv->pdev->dev;
191         u32 slots = priv->tx_desc_cnt;
192         size_t bytes;
193
194         /* Make sure everything is zeroed to start */
195         memset(tx, 0, sizeof(*tx));
196         tx->q_num = idx;
197
198         tx->mask = slots - 1;
199
200         /* alloc metadata */
201         tx->info = vzalloc(sizeof(*tx->info) * slots);
202         if (!tx->info)
203                 return -ENOMEM;
204
205         /* alloc tx queue */
206         bytes = sizeof(*tx->desc) * slots;
207         tx->desc = dma_alloc_coherent(hdev, bytes, &tx->bus, GFP_KERNEL);
208         if (!tx->desc)
209                 goto abort_with_info;
210
211         tx->raw_addressing = priv->raw_addressing;
212         tx->dev = &priv->pdev->dev;
213         if (!tx->raw_addressing) {
214                 tx->tx_fifo.qpl = gve_assign_tx_qpl(priv);
215
216                 /* map Tx FIFO */
217                 if (gve_tx_fifo_init(priv, &tx->tx_fifo))
218                         goto abort_with_desc;
219         }
220
221         tx->q_resources =
222                 dma_alloc_coherent(hdev,
223                                    sizeof(*tx->q_resources),
224                                    &tx->q_resources_bus,
225                                    GFP_KERNEL);
226         if (!tx->q_resources)
227                 goto abort_with_fifo;
228
229         netif_dbg(priv, drv, priv->dev, "tx[%d]->bus=%lx\n", idx,
230                   (unsigned long)tx->bus);
231         tx->netdev_txq = netdev_get_tx_queue(priv->dev, idx);
232         gve_tx_add_to_block(priv, idx);
233
234         return 0;
235
236 abort_with_fifo:
237         if (!tx->raw_addressing)
238                 gve_tx_fifo_release(priv, &tx->tx_fifo);
239 abort_with_desc:
240         dma_free_coherent(hdev, bytes, tx->desc, tx->bus);
241         tx->desc = NULL;
242 abort_with_info:
243         vfree(tx->info);
244         tx->info = NULL;
245         return -ENOMEM;
246 }
247
248 int gve_tx_alloc_rings(struct gve_priv *priv)
249 {
250         int err = 0;
251         int i;
252
253         for (i = 0; i < priv->tx_cfg.num_queues; i++) {
254                 err = gve_tx_alloc_ring(priv, i);
255                 if (err) {
256                         netif_err(priv, drv, priv->dev,
257                                   "Failed to alloc tx ring=%d: err=%d\n",
258                                   i, err);
259                         break;
260                 }
261         }
262         /* Unallocate if there was an error */
263         if (err) {
264                 int j;
265
266                 for (j = 0; j < i; j++)
267                         gve_tx_free_ring(priv, j);
268         }
269         return err;
270 }
271
272 void gve_tx_free_rings(struct gve_priv *priv)
273 {
274         int i;
275
276         for (i = 0; i < priv->tx_cfg.num_queues; i++)
277                 gve_tx_free_ring(priv, i);
278 }
279
280 /* gve_tx_avail - Calculates the number of slots available in the ring
281  * @tx: tx ring to check
282  *
283  * Returns the number of slots available
284  *
285  * The capacity of the queue is mask + 1. We don't need to reserve an entry.
286  **/
287 static inline u32 gve_tx_avail(struct gve_tx_ring *tx)
288 {
289         return tx->mask + 1 - (tx->req - tx->done);
290 }
291
292 static inline int gve_skb_fifo_bytes_required(struct gve_tx_ring *tx,
293                                               struct sk_buff *skb)
294 {
295         int pad_bytes, align_hdr_pad;
296         int bytes;
297         int hlen;
298
299         hlen = skb_is_gso(skb) ? skb_checksum_start_offset(skb) +
300                                  tcp_hdrlen(skb) : skb_headlen(skb);
301
302         pad_bytes = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo,
303                                                    hlen);
304         /* We need to take into account the header alignment padding. */
305         align_hdr_pad = L1_CACHE_ALIGN(hlen) - hlen;
306         bytes = align_hdr_pad + pad_bytes + skb->len;
307
308         return bytes;
309 }
310
311 /* The most descriptors we could need is MAX_SKB_FRAGS + 3 : 1 for each skb frag,
312  * +1 for the skb linear portion, +1 for when tcp hdr needs to be in separate descriptor,
313  * and +1 if the payload wraps to the beginning of the FIFO.
314  */
315 #define MAX_TX_DESC_NEEDED      (MAX_SKB_FRAGS + 3)
316 static void gve_tx_unmap_buf(struct device *dev, struct gve_tx_buffer_state *info)
317 {
318         if (info->skb) {
319                 dma_unmap_single(dev, dma_unmap_addr(&info->buf, dma),
320                                  dma_unmap_len(&info->buf, len),
321                                  DMA_TO_DEVICE);
322                 dma_unmap_len_set(&info->buf, len, 0);
323         } else {
324                 dma_unmap_page(dev, dma_unmap_addr(&info->buf, dma),
325                                dma_unmap_len(&info->buf, len),
326                                DMA_TO_DEVICE);
327                 dma_unmap_len_set(&info->buf, len, 0);
328         }
329 }
330
331 /* Check if sufficient resources (descriptor ring space, FIFO space) are
332  * available to transmit the given number of bytes.
333  */
334 static inline bool gve_can_tx(struct gve_tx_ring *tx, int bytes_required)
335 {
336         bool can_alloc = true;
337
338         if (!tx->raw_addressing)
339                 can_alloc = gve_tx_fifo_can_alloc(&tx->tx_fifo, bytes_required);
340
341         return (gve_tx_avail(tx) >= MAX_TX_DESC_NEEDED && can_alloc);
342 }
343
344 /* Stops the queue if the skb cannot be transmitted. */
345 static int gve_maybe_stop_tx(struct gve_tx_ring *tx, struct sk_buff *skb)
346 {
347         int bytes_required = 0;
348
349         if (!tx->raw_addressing)
350                 bytes_required = gve_skb_fifo_bytes_required(tx, skb);
351
352         if (likely(gve_can_tx(tx, bytes_required)))
353                 return 0;
354
355         /* No space, so stop the queue */
356         tx->stop_queue++;
357         netif_tx_stop_queue(tx->netdev_txq);
358         smp_mb();       /* sync with restarting queue in gve_clean_tx_done() */
359
360         /* Now check for resources again, in case gve_clean_tx_done() freed
361          * resources after we checked and we stopped the queue after
362          * gve_clean_tx_done() checked.
363          *
364          * gve_maybe_stop_tx()                  gve_clean_tx_done()
365          *   nsegs/can_alloc test failed
366          *                                        gve_tx_free_fifo()
367          *                                        if (tx queue stopped)
368          *                                          netif_tx_queue_wake()
369          *   netif_tx_stop_queue()
370          *   Need to check again for space here!
371          */
372         if (likely(!gve_can_tx(tx, bytes_required)))
373                 return -EBUSY;
374
375         netif_tx_start_queue(tx->netdev_txq);
376         tx->wake_queue++;
377         return 0;
378 }
379
380 static void gve_tx_fill_pkt_desc(union gve_tx_desc *pkt_desc,
381                                  struct sk_buff *skb, bool is_gso,
382                                  int l4_hdr_offset, u32 desc_cnt,
383                                  u16 hlen, u64 addr)
384 {
385         /* l4_hdr_offset and csum_offset are in units of 16-bit words */
386         if (is_gso) {
387                 pkt_desc->pkt.type_flags = GVE_TXD_TSO | GVE_TXF_L4CSUM;
388                 pkt_desc->pkt.l4_csum_offset = skb->csum_offset >> 1;
389                 pkt_desc->pkt.l4_hdr_offset = l4_hdr_offset >> 1;
390         } else if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
391                 pkt_desc->pkt.type_flags = GVE_TXD_STD | GVE_TXF_L4CSUM;
392                 pkt_desc->pkt.l4_csum_offset = skb->csum_offset >> 1;
393                 pkt_desc->pkt.l4_hdr_offset = l4_hdr_offset >> 1;
394         } else {
395                 pkt_desc->pkt.type_flags = GVE_TXD_STD;
396                 pkt_desc->pkt.l4_csum_offset = 0;
397                 pkt_desc->pkt.l4_hdr_offset = 0;
398         }
399         pkt_desc->pkt.desc_cnt = desc_cnt;
400         pkt_desc->pkt.len = cpu_to_be16(skb->len);
401         pkt_desc->pkt.seg_len = cpu_to_be16(hlen);
402         pkt_desc->pkt.seg_addr = cpu_to_be64(addr);
403 }
404
405 static void gve_tx_fill_seg_desc(union gve_tx_desc *seg_desc,
406                                  struct sk_buff *skb, bool is_gso,
407                                  u16 len, u64 addr)
408 {
409         seg_desc->seg.type_flags = GVE_TXD_SEG;
410         if (is_gso) {
411                 if (skb_is_gso_v6(skb))
412                         seg_desc->seg.type_flags |= GVE_TXSF_IPV6;
413                 seg_desc->seg.l3_offset = skb_network_offset(skb) >> 1;
414                 seg_desc->seg.mss = cpu_to_be16(skb_shinfo(skb)->gso_size);
415         }
416         seg_desc->seg.seg_len = cpu_to_be16(len);
417         seg_desc->seg.seg_addr = cpu_to_be64(addr);
418 }
419
420 static void gve_dma_sync_for_device(struct device *dev, dma_addr_t *page_buses,
421                                     u64 iov_offset, u64 iov_len)
422 {
423         u64 last_page = (iov_offset + iov_len - 1) / PAGE_SIZE;
424         u64 first_page = iov_offset / PAGE_SIZE;
425         u64 page;
426
427         for (page = first_page; page <= last_page; page++)
428                 dma_sync_single_for_device(dev, page_buses[page], PAGE_SIZE, DMA_TO_DEVICE);
429 }
430
431 static int gve_tx_add_skb_copy(struct gve_priv *priv, struct gve_tx_ring *tx, struct sk_buff *skb)
432 {
433         int pad_bytes, hlen, hdr_nfrags, payload_nfrags, l4_hdr_offset;
434         union gve_tx_desc *pkt_desc, *seg_desc;
435         struct gve_tx_buffer_state *info;
436         bool is_gso = skb_is_gso(skb);
437         u32 idx = tx->req & tx->mask;
438         int payload_iov = 2;
439         int copy_offset;
440         u32 next_idx;
441         int i;
442
443         info = &tx->info[idx];
444         pkt_desc = &tx->desc[idx];
445
446         l4_hdr_offset = skb_checksum_start_offset(skb);
447         /* If the skb is gso, then we want the tcp header in the first segment
448          * otherwise we want the linear portion of the skb (which will contain
449          * the checksum because skb->csum_start and skb->csum_offset are given
450          * relative to skb->head) in the first segment.
451          */
452         hlen = is_gso ? l4_hdr_offset + tcp_hdrlen(skb) :
453                         skb_headlen(skb);
454
455         info->skb =  skb;
456         /* We don't want to split the header, so if necessary, pad to the end
457          * of the fifo and then put the header at the beginning of the fifo.
458          */
459         pad_bytes = gve_tx_fifo_pad_alloc_one_frag(&tx->tx_fifo, hlen);
460         hdr_nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, hlen + pad_bytes,
461                                        &info->iov[0]);
462         WARN(!hdr_nfrags, "hdr_nfrags should never be 0!");
463         payload_nfrags = gve_tx_alloc_fifo(&tx->tx_fifo, skb->len - hlen,
464                                            &info->iov[payload_iov]);
465
466         gve_tx_fill_pkt_desc(pkt_desc, skb, is_gso, l4_hdr_offset,
467                              1 + payload_nfrags, hlen,
468                              info->iov[hdr_nfrags - 1].iov_offset);
469
470         skb_copy_bits(skb, 0,
471                       tx->tx_fifo.base + info->iov[hdr_nfrags - 1].iov_offset,
472                       hlen);
473         gve_dma_sync_for_device(&priv->pdev->dev, tx->tx_fifo.qpl->page_buses,
474                                 info->iov[hdr_nfrags - 1].iov_offset,
475                                 info->iov[hdr_nfrags - 1].iov_len);
476         copy_offset = hlen;
477
478         for (i = payload_iov; i < payload_nfrags + payload_iov; i++) {
479                 next_idx = (tx->req + 1 + i - payload_iov) & tx->mask;
480                 seg_desc = &tx->desc[next_idx];
481
482                 gve_tx_fill_seg_desc(seg_desc, skb, is_gso,
483                                      info->iov[i].iov_len,
484                                      info->iov[i].iov_offset);
485
486                 skb_copy_bits(skb, copy_offset,
487                               tx->tx_fifo.base + info->iov[i].iov_offset,
488                               info->iov[i].iov_len);
489                 gve_dma_sync_for_device(&priv->pdev->dev, tx->tx_fifo.qpl->page_buses,
490                                         info->iov[i].iov_offset,
491                                         info->iov[i].iov_len);
492                 copy_offset += info->iov[i].iov_len;
493         }
494
495         return 1 + payload_nfrags;
496 }
497
498 static int gve_tx_add_skb_no_copy(struct gve_priv *priv, struct gve_tx_ring *tx,
499                                   struct sk_buff *skb)
500 {
501         const struct skb_shared_info *shinfo = skb_shinfo(skb);
502         int hlen, payload_nfrags, l4_hdr_offset;
503         union gve_tx_desc *pkt_desc, *seg_desc;
504         struct gve_tx_buffer_state *info;
505         bool is_gso = skb_is_gso(skb);
506         u32 idx = tx->req & tx->mask;
507         struct gve_tx_dma_buf *buf;
508         u64 addr;
509         u32 len;
510         int i;
511
512         info = &tx->info[idx];
513         pkt_desc = &tx->desc[idx];
514
515         l4_hdr_offset = skb_checksum_start_offset(skb);
516         /* If the skb is gso, then we want only up to the tcp header in the first segment
517          * to efficiently replicate on each segment otherwise we want the linear portion
518          * of the skb (which will contain the checksum because skb->csum_start and
519          * skb->csum_offset are given relative to skb->head) in the first segment.
520          */
521         hlen = is_gso ? l4_hdr_offset + tcp_hdrlen(skb) : skb_headlen(skb);
522         len = skb_headlen(skb);
523
524         info->skb =  skb;
525
526         addr = dma_map_single(tx->dev, skb->data, len, DMA_TO_DEVICE);
527         if (unlikely(dma_mapping_error(tx->dev, addr))) {
528                 tx->dma_mapping_error++;
529                 goto drop;
530         }
531         buf = &info->buf;
532         dma_unmap_len_set(buf, len, len);
533         dma_unmap_addr_set(buf, dma, addr);
534
535         payload_nfrags = shinfo->nr_frags;
536         if (hlen < len) {
537                 /* For gso the rest of the linear portion of the skb needs to
538                  * be in its own descriptor.
539                  */
540                 payload_nfrags++;
541                 gve_tx_fill_pkt_desc(pkt_desc, skb, is_gso, l4_hdr_offset,
542                                      1 + payload_nfrags, hlen, addr);
543
544                 len -= hlen;
545                 addr += hlen;
546                 idx = (tx->req + 1) & tx->mask;
547                 seg_desc = &tx->desc[idx];
548                 gve_tx_fill_seg_desc(seg_desc, skb, is_gso, len, addr);
549         } else {
550                 gve_tx_fill_pkt_desc(pkt_desc, skb, is_gso, l4_hdr_offset,
551                                      1 + payload_nfrags, hlen, addr);
552         }
553
554         for (i = 0; i < shinfo->nr_frags; i++) {
555                 const skb_frag_t *frag = &shinfo->frags[i];
556
557                 idx = (idx + 1) & tx->mask;
558                 seg_desc = &tx->desc[idx];
559                 len = skb_frag_size(frag);
560                 addr = skb_frag_dma_map(tx->dev, frag, 0, len, DMA_TO_DEVICE);
561                 if (unlikely(dma_mapping_error(tx->dev, addr))) {
562                         tx->dma_mapping_error++;
563                         goto unmap_drop;
564                 }
565                 buf = &tx->info[idx].buf;
566                 tx->info[idx].skb = NULL;
567                 dma_unmap_len_set(buf, len, len);
568                 dma_unmap_addr_set(buf, dma, addr);
569
570                 gve_tx_fill_seg_desc(seg_desc, skb, is_gso, len, addr);
571         }
572
573         return 1 + payload_nfrags;
574
575 unmap_drop:
576         i += (payload_nfrags == shinfo->nr_frags ? 1 : 2);
577         while (i--) {
578                 idx--;
579                 gve_tx_unmap_buf(tx->dev, &tx->info[idx & tx->mask]);
580         }
581 drop:
582         tx->dropped_pkt++;
583         return 0;
584 }
585
586 netdev_tx_t gve_tx(struct sk_buff *skb, struct net_device *dev)
587 {
588         struct gve_priv *priv = netdev_priv(dev);
589         struct gve_tx_ring *tx;
590         int nsegs;
591
592         WARN(skb_get_queue_mapping(skb) > priv->tx_cfg.num_queues,
593              "skb queue index out of range");
594         tx = &priv->tx[skb_get_queue_mapping(skb)];
595         if (unlikely(gve_maybe_stop_tx(tx, skb))) {
596                 /* We need to ring the txq doorbell -- we have stopped the Tx
597                  * queue for want of resources, but prior calls to gve_tx()
598                  * may have added descriptors without ringing the doorbell.
599                  */
600
601                 gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
602                 return NETDEV_TX_BUSY;
603         }
604         if (tx->raw_addressing)
605                 nsegs = gve_tx_add_skb_no_copy(priv, tx, skb);
606         else
607                 nsegs = gve_tx_add_skb_copy(priv, tx, skb);
608
609         /* If the packet is getting sent, we need to update the skb */
610         if (nsegs) {
611                 netdev_tx_sent_queue(tx->netdev_txq, skb->len);
612                 skb_tx_timestamp(skb);
613                 tx->req += nsegs;
614         } else {
615                 dev_kfree_skb_any(skb);
616         }
617
618         if (!netif_xmit_stopped(tx->netdev_txq) && netdev_xmit_more())
619                 return NETDEV_TX_OK;
620
621         /* Give packets to NIC. Even if this packet failed to send the doorbell
622          * might need to be rung because of xmit_more.
623          */
624         gve_tx_put_doorbell(priv, tx->q_resources, tx->req);
625         return NETDEV_TX_OK;
626 }
627
628 #define GVE_TX_START_THRESH     PAGE_SIZE
629
630 static int gve_clean_tx_done(struct gve_priv *priv, struct gve_tx_ring *tx,
631                              u32 to_do, bool try_to_wake)
632 {
633         struct gve_tx_buffer_state *info;
634         u64 pkts = 0, bytes = 0;
635         size_t space_freed = 0;
636         struct sk_buff *skb;
637         int i, j;
638         u32 idx;
639
640         for (j = 0; j < to_do; j++) {
641                 idx = tx->done & tx->mask;
642                 netif_info(priv, tx_done, priv->dev,
643                            "[%d] %s: idx=%d (req=%u done=%u)\n",
644                            tx->q_num, __func__, idx, tx->req, tx->done);
645                 info = &tx->info[idx];
646                 skb = info->skb;
647
648                 /* Unmap the buffer */
649                 if (tx->raw_addressing)
650                         gve_tx_unmap_buf(tx->dev, info);
651                 tx->done++;
652                 /* Mark as free */
653                 if (skb) {
654                         info->skb = NULL;
655                         bytes += skb->len;
656                         pkts++;
657                         dev_consume_skb_any(skb);
658                         if (tx->raw_addressing)
659                                 continue;
660                         /* FIFO free */
661                         for (i = 0; i < ARRAY_SIZE(info->iov); i++) {
662                                 space_freed += info->iov[i].iov_len + info->iov[i].iov_padding;
663                                 info->iov[i].iov_len = 0;
664                                 info->iov[i].iov_padding = 0;
665                         }
666                 }
667         }
668
669         if (!tx->raw_addressing)
670                 gve_tx_free_fifo(&tx->tx_fifo, space_freed);
671         u64_stats_update_begin(&tx->statss);
672         tx->bytes_done += bytes;
673         tx->pkt_done += pkts;
674         u64_stats_update_end(&tx->statss);
675         netdev_tx_completed_queue(tx->netdev_txq, pkts, bytes);
676
677         /* start the queue if we've stopped it */
678 #ifndef CONFIG_BQL
679         /* Make sure that the doorbells are synced */
680         smp_mb();
681 #endif
682         if (try_to_wake && netif_tx_queue_stopped(tx->netdev_txq) &&
683             likely(gve_can_tx(tx, GVE_TX_START_THRESH))) {
684                 tx->wake_queue++;
685                 netif_tx_wake_queue(tx->netdev_txq);
686         }
687
688         return pkts;
689 }
690
691 __be32 gve_tx_load_event_counter(struct gve_priv *priv,
692                                  struct gve_tx_ring *tx)
693 {
694         u32 counter_index = be32_to_cpu((tx->q_resources->counter_index));
695
696         return READ_ONCE(priv->counter_array[counter_index]);
697 }
698
699 bool gve_tx_poll(struct gve_notify_block *block, int budget)
700 {
701         struct gve_priv *priv = block->priv;
702         struct gve_tx_ring *tx = block->tx;
703         bool repoll = false;
704         u32 nic_done;
705         u32 to_do;
706
707         /* If budget is 0, do all the work */
708         if (budget == 0)
709                 budget = INT_MAX;
710
711         /* Find out how much work there is to be done */
712         tx->last_nic_done = gve_tx_load_event_counter(priv, tx);
713         nic_done = be32_to_cpu(tx->last_nic_done);
714         if (budget > 0) {
715                 /* Do as much work as we have that the budget will
716                  * allow
717                  */
718                 to_do = min_t(u32, (nic_done - tx->done), budget);
719                 gve_clean_tx_done(priv, tx, to_do, true);
720         }
721         /* If we still have work we want to repoll */
722         repoll |= (nic_done != tx->done);
723         return repoll;
724 }
This page took 0.074404 seconds and 4 git commands to generate.