net/sfc: add missing BSD license line and update year
[dpdk.git] / drivers / net / sfc / sfc_tx.c
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2016-2017 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was jointly developed between OKTET Labs (under contract
8  * for Solarflare) and Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "sfc.h"
33 #include "sfc_debug.h"
34 #include "sfc_log.h"
35 #include "sfc_ev.h"
36 #include "sfc_tx.h"
37 #include "sfc_tweak.h"
38
39 /*
40  * Maximum number of TX queue flush attempts in case of
41  * failure or flush timeout
42  */
43 #define SFC_TX_QFLUSH_ATTEMPTS          (3)
44
45 /*
46  * Time to wait between event queue polling attempts when waiting for TX
47  * queue flush done or flush failed events
48  */
49 #define SFC_TX_QFLUSH_POLL_WAIT_MS      (1)
50
51 /*
52  * Maximum number of event queue polling attempts when waiting for TX queue
53  * flush done or flush failed events; it defines TX queue flush attempt timeout
54  * together with SFC_TX_QFLUSH_POLL_WAIT_MS
55  */
56 #define SFC_TX_QFLUSH_POLL_ATTEMPTS     (2000)
57
58 static int
59 sfc_tx_qcheck_conf(struct sfc_adapter *sa, uint16_t nb_tx_desc,
60                    const struct rte_eth_txconf *tx_conf)
61 {
62         unsigned int flags = tx_conf->txq_flags;
63         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
64         int rc = 0;
65
66         if (tx_conf->tx_rs_thresh != 0) {
67                 sfc_err(sa, "RS bit in transmit descriptor is not supported");
68                 rc = EINVAL;
69         }
70
71         if (tx_conf->tx_free_thresh > EFX_TXQ_LIMIT(nb_tx_desc)) {
72                 sfc_err(sa,
73                         "TxQ free threshold too large: %u vs maximum %u",
74                         tx_conf->tx_free_thresh, EFX_TXQ_LIMIT(nb_tx_desc));
75                 rc = EINVAL;
76         }
77
78         if (tx_conf->tx_thresh.pthresh != 0 ||
79             tx_conf->tx_thresh.hthresh != 0 ||
80             tx_conf->tx_thresh.wthresh != 0) {
81                 sfc_err(sa,
82                         "prefetch/host/writeback thresholds are not supported");
83                 rc = EINVAL;
84         }
85
86         if (!encp->enc_hw_tx_insert_vlan_enabled &&
87             (flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
88                 sfc_err(sa, "VLAN offload is not supported");
89                 rc = EINVAL;
90         }
91
92         if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
93                 sfc_err(sa, "SCTP offload is not supported");
94                 rc = EINVAL;
95         }
96
97         /* We either perform both TCP and UDP offload, or no offload at all */
98         if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
99             ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
100                 sfc_err(sa, "TCP and UDP offloads can't be set independently");
101                 rc = EINVAL;
102         }
103
104         return rc;
105 }
106
107 void
108 sfc_tx_qflush_done(struct sfc_txq *txq)
109 {
110         txq->state |= SFC_TXQ_FLUSHED;
111         txq->state &= ~SFC_TXQ_FLUSHING;
112 }
113
114 static void
115 sfc_tx_reap(struct sfc_txq *txq)
116 {
117         unsigned int    completed;
118
119
120         sfc_ev_qpoll(txq->evq);
121
122         for (completed = txq->completed;
123              completed != txq->pending; completed++) {
124                 struct sfc_tx_sw_desc *txd;
125
126                 txd = &txq->sw_ring[completed & txq->ptr_mask];
127
128                 if (txd->mbuf != NULL) {
129                         rte_pktmbuf_free(txd->mbuf);
130                         txd->mbuf = NULL;
131                 }
132         }
133
134         txq->completed = completed;
135 }
136
137 int
138 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
139              uint16_t nb_tx_desc, unsigned int socket_id,
140              const struct rte_eth_txconf *tx_conf)
141 {
142         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
143         struct sfc_txq_info *txq_info;
144         struct sfc_evq *evq;
145         struct sfc_txq *txq;
146         unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
147         int rc = 0;
148
149         sfc_log_init(sa, "TxQ = %u", sw_index);
150
151         rc = sfc_tx_qcheck_conf(sa, nb_tx_desc, tx_conf);
152         if (rc != 0)
153                 goto fail_bad_conf;
154
155         SFC_ASSERT(sw_index < sa->txq_count);
156         txq_info = &sa->txq_info[sw_index];
157
158         SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
159         txq_info->entries = nb_tx_desc;
160
161         rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
162         if (rc != 0)
163                 goto fail_ev_qinit;
164
165         evq = sa->evq_info[evq_index].evq;
166
167         rc = ENOMEM;
168         txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
169         if (txq == NULL)
170                 goto fail_txq_alloc;
171
172         rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
173                            socket_id, &txq->mem);
174         if (rc != 0)
175                 goto fail_dma_alloc;
176
177         rc = ENOMEM;
178         txq->pend_desc = rte_calloc_socket("sfc-txq-pend-desc",
179                                            EFX_TXQ_LIMIT(txq_info->entries),
180                                            sizeof(efx_desc_t), 0, socket_id);
181         if (txq->pend_desc == NULL)
182                 goto fail_pend_desc_alloc;
183
184         rc = ENOMEM;
185         txq->sw_ring = rte_calloc_socket("sfc-txq-desc", txq_info->entries,
186                                          sizeof(*txq->sw_ring), 0, socket_id);
187         if (txq->sw_ring == NULL)
188                 goto fail_desc_alloc;
189
190         if (sa->tso) {
191                 rc = sfc_tso_alloc_tsoh_objs(txq->sw_ring, txq_info->entries,
192                                              socket_id);
193                 if (rc != 0)
194                         goto fail_alloc_tsoh_objs;
195         }
196
197         txq->state = SFC_TXQ_INITIALIZED;
198         txq->ptr_mask = txq_info->entries - 1;
199         txq->free_thresh = (tx_conf->tx_free_thresh) ? tx_conf->tx_free_thresh :
200                                                      SFC_TX_DEFAULT_FREE_THRESH;
201         txq->dma_desc_size_max = encp->enc_tx_dma_desc_size_max;
202         txq->hw_index = sw_index;
203         txq->flags = tx_conf->txq_flags;
204         txq->evq = evq;
205
206         evq->txq = txq;
207
208         txq_info->txq = txq;
209         txq_info->deferred_start = (tx_conf->tx_deferred_start != 0);
210
211         return 0;
212
213 fail_alloc_tsoh_objs:
214         rte_free(txq->sw_ring);
215
216 fail_desc_alloc:
217         rte_free(txq->pend_desc);
218
219 fail_pend_desc_alloc:
220         sfc_dma_free(sa, &txq->mem);
221
222 fail_dma_alloc:
223         rte_free(txq);
224
225 fail_txq_alloc:
226         sfc_ev_qfini(sa, evq_index);
227
228 fail_ev_qinit:
229         txq_info->entries = 0;
230
231 fail_bad_conf:
232         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
233         return rc;
234 }
235
236 void
237 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
238 {
239         struct sfc_txq_info *txq_info;
240         struct sfc_txq *txq;
241
242         sfc_log_init(sa, "TxQ = %u", sw_index);
243
244         SFC_ASSERT(sw_index < sa->txq_count);
245         txq_info = &sa->txq_info[sw_index];
246
247         txq = txq_info->txq;
248         SFC_ASSERT(txq != NULL);
249         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
250
251         sfc_tso_free_tsoh_objs(txq->sw_ring, txq_info->entries);
252
253         txq_info->txq = NULL;
254         txq_info->entries = 0;
255
256         rte_free(txq->sw_ring);
257         rte_free(txq->pend_desc);
258         sfc_dma_free(sa, &txq->mem);
259         rte_free(txq);
260 }
261
262 static int
263 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
264 {
265         sfc_log_init(sa, "TxQ = %u", sw_index);
266
267         return 0;
268 }
269
270 static int
271 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
272 {
273         int rc = 0;
274
275         switch (txmode->mq_mode) {
276         case ETH_MQ_TX_NONE:
277                 break;
278         default:
279                 sfc_err(sa, "Tx multi-queue mode %u not supported",
280                         txmode->mq_mode);
281                 rc = EINVAL;
282         }
283
284         /*
285          * These features are claimed to be i40e-specific,
286          * but it does make sense to double-check their absence
287          */
288         if (txmode->hw_vlan_reject_tagged) {
289                 sfc_err(sa, "Rejecting tagged packets not supported");
290                 rc = EINVAL;
291         }
292
293         if (txmode->hw_vlan_reject_untagged) {
294                 sfc_err(sa, "Rejecting untagged packets not supported");
295                 rc = EINVAL;
296         }
297
298         if (txmode->hw_vlan_insert_pvid) {
299                 sfc_err(sa, "Port-based VLAN insertion not supported");
300                 rc = EINVAL;
301         }
302
303         return rc;
304 }
305
306 int
307 sfc_tx_init(struct sfc_adapter *sa)
308 {
309         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
310         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
311         unsigned int sw_index;
312         int rc = 0;
313
314         /*
315          * The datapath implementation assumes absence of boundary
316          * limits on Tx DMA descriptors. Addition of these checks on
317          * datapath would simply make the datapath slower.
318          */
319         if (encp->enc_tx_dma_desc_boundary != 0) {
320                 rc = ENOTSUP;
321                 goto fail_tx_dma_desc_boundary;
322         }
323
324         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
325         if (rc != 0)
326                 goto fail_check_mode;
327
328         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
329
330         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
331                                          sizeof(sa->txq_info[0]), 0,
332                                          sa->socket_id);
333         if (sa->txq_info == NULL)
334                 goto fail_txqs_alloc;
335
336         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
337                 rc = sfc_tx_qinit_info(sa, sw_index);
338                 if (rc != 0)
339                         goto fail_tx_qinit_info;
340         }
341
342         return 0;
343
344 fail_tx_qinit_info:
345         rte_free(sa->txq_info);
346         sa->txq_info = NULL;
347
348 fail_txqs_alloc:
349         sa->txq_count = 0;
350
351 fail_check_mode:
352 fail_tx_dma_desc_boundary:
353         sfc_log_init(sa, "failed (rc = %d)", rc);
354         return rc;
355 }
356
357 void
358 sfc_tx_fini(struct sfc_adapter *sa)
359 {
360         int sw_index;
361
362         sw_index = sa->txq_count;
363         while (--sw_index >= 0) {
364                 if (sa->txq_info[sw_index].txq != NULL)
365                         sfc_tx_qfini(sa, sw_index);
366         }
367
368         rte_free(sa->txq_info);
369         sa->txq_info = NULL;
370         sa->txq_count = 0;
371 }
372
373 int
374 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
375 {
376         struct rte_eth_dev_data *dev_data;
377         struct sfc_txq_info *txq_info;
378         struct sfc_txq *txq;
379         struct sfc_evq *evq;
380         uint16_t flags;
381         unsigned int desc_index;
382         int rc = 0;
383
384         sfc_log_init(sa, "TxQ = %u", sw_index);
385
386         SFC_ASSERT(sw_index < sa->txq_count);
387         txq_info = &sa->txq_info[sw_index];
388
389         txq = txq_info->txq;
390
391         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
392
393         evq = txq->evq;
394
395         rc = sfc_ev_qstart(sa, evq->evq_index);
396         if (rc != 0)
397                 goto fail_ev_qstart;
398
399         /*
400          * It seems that DPDK has no controls regarding IPv4 offloads,
401          * hence, we always enable it here
402          */
403         if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
404             (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP)) {
405                 flags = EFX_TXQ_CKSUM_IPV4;
406         } else {
407                 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
408
409                 if (sa->tso)
410                         flags |= EFX_TXQ_FATSOV2;
411         }
412
413         rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
414                             txq_info->entries, 0 /* not used on EF10 */,
415                             flags, evq->common,
416                             &txq->common, &desc_index);
417         if (rc != 0) {
418                 if (sa->tso && (rc == ENOSPC))
419                         sfc_err(sa, "ran out of TSO contexts");
420
421                 goto fail_tx_qcreate;
422         }
423
424         txq->added = txq->pending = txq->completed = desc_index;
425         txq->hw_vlan_tci = 0;
426
427         efx_tx_qenable(txq->common);
428
429         txq->state |= (SFC_TXQ_STARTED | SFC_TXQ_RUNNING);
430
431         /*
432          * It seems to be used by DPDK for debug purposes only ('rte_ether')
433          */
434         dev_data = sa->eth_dev->data;
435         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
436
437         return 0;
438
439 fail_tx_qcreate:
440         sfc_ev_qstop(sa, evq->evq_index);
441
442 fail_ev_qstart:
443         return rc;
444 }
445
446 void
447 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
448 {
449         struct rte_eth_dev_data *dev_data;
450         struct sfc_txq_info *txq_info;
451         struct sfc_txq *txq;
452         unsigned int retry_count;
453         unsigned int wait_count;
454         unsigned int txds;
455
456         sfc_log_init(sa, "TxQ = %u", sw_index);
457
458         SFC_ASSERT(sw_index < sa->txq_count);
459         txq_info = &sa->txq_info[sw_index];
460
461         txq = txq_info->txq;
462
463         if (txq->state == SFC_TXQ_INITIALIZED)
464                 return;
465
466         SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
467
468         txq->state &= ~SFC_TXQ_RUNNING;
469
470         /*
471          * Retry TX queue flushing in case of flush failed or
472          * timeout; in the worst case it can delay for 6 seconds
473          */
474         for (retry_count = 0;
475              ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
476              (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
477              ++retry_count) {
478                 if (efx_tx_qflush(txq->common) != 0) {
479                         txq->state |= SFC_TXQ_FLUSHING;
480                         break;
481                 }
482
483                 /*
484                  * Wait for TX queue flush done or flush failed event at least
485                  * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
486                  * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
487                  * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
488                  */
489                 wait_count = 0;
490                 do {
491                         rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
492                         sfc_ev_qpoll(txq->evq);
493                 } while ((txq->state & SFC_TXQ_FLUSHING) &&
494                          wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
495
496                 if (txq->state & SFC_TXQ_FLUSHING)
497                         sfc_err(sa, "TxQ %u flush timed out", sw_index);
498
499                 if (txq->state & SFC_TXQ_FLUSHED)
500                         sfc_info(sa, "TxQ %u flushed", sw_index);
501         }
502
503         sfc_tx_reap(txq);
504
505         for (txds = 0; txds < txq_info->entries; txds++) {
506                 if (txq->sw_ring[txds].mbuf != NULL) {
507                         rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
508                         txq->sw_ring[txds].mbuf = NULL;
509                 }
510         }
511
512         txq->state = SFC_TXQ_INITIALIZED;
513
514         efx_tx_qdestroy(txq->common);
515
516         sfc_ev_qstop(sa, txq->evq->evq_index);
517
518         /*
519          * It seems to be used by DPDK for debug purposes only ('rte_ether')
520          */
521         dev_data = sa->eth_dev->data;
522         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
523 }
524
525 int
526 sfc_tx_start(struct sfc_adapter *sa)
527 {
528         unsigned int sw_index;
529         int rc = 0;
530
531         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
532
533         if (sa->tso) {
534                 if (!efx_nic_cfg_get(sa->nic)->enc_fw_assisted_tso_v2_enabled) {
535                         sfc_warn(sa, "TSO support was unable to be restored");
536                         sa->tso = B_FALSE;
537                 }
538         }
539
540         rc = efx_tx_init(sa->nic);
541         if (rc != 0)
542                 goto fail_efx_tx_init;
543
544         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
545                 if (!(sa->txq_info[sw_index].deferred_start) ||
546                     sa->txq_info[sw_index].deferred_started) {
547                         rc = sfc_tx_qstart(sa, sw_index);
548                         if (rc != 0)
549                                 goto fail_tx_qstart;
550                 }
551         }
552
553         return 0;
554
555 fail_tx_qstart:
556         while (sw_index-- > 0)
557                 sfc_tx_qstop(sa, sw_index);
558
559         efx_tx_fini(sa->nic);
560
561 fail_efx_tx_init:
562         sfc_log_init(sa, "failed (rc = %d)", rc);
563         return rc;
564 }
565
566 void
567 sfc_tx_stop(struct sfc_adapter *sa)
568 {
569         unsigned int sw_index;
570
571         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
572
573         sw_index = sa->txq_count;
574         while (sw_index-- > 0) {
575                 if (sa->txq_info[sw_index].txq != NULL)
576                         sfc_tx_qstop(sa, sw_index);
577         }
578
579         efx_tx_fini(sa->nic);
580 }
581
582 /*
583  * The function is used to insert or update VLAN tag;
584  * the firmware has state of the firmware tag to insert per TxQ
585  * (controlled by option descriptors), hence, if the tag of the
586  * packet to be sent is different from one remembered by the firmware,
587  * the function will update it
588  */
589 static unsigned int
590 sfc_tx_maybe_insert_tag(struct sfc_txq *txq, struct rte_mbuf *m,
591                         efx_desc_t **pend)
592 {
593         uint16_t this_tag = ((m->ol_flags & PKT_TX_VLAN_PKT) ?
594                              m->vlan_tci : 0);
595
596         if (this_tag == txq->hw_vlan_tci)
597                 return 0;
598
599         /*
600          * The expression inside SFC_ASSERT() is not desired to be checked in
601          * a non-debug build because it might be too expensive on the data path
602          */
603         SFC_ASSERT(efx_nic_cfg_get(txq->evq->sa->nic)->enc_hw_tx_insert_vlan_enabled);
604
605         efx_tx_qdesc_vlantci_create(txq->common, rte_cpu_to_be_16(this_tag),
606                                     *pend);
607         (*pend)++;
608         txq->hw_vlan_tci = this_tag;
609
610         return 1;
611 }
612
613 uint16_t
614 sfc_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
615 {
616         struct sfc_txq *txq = (struct sfc_txq *)tx_queue;
617         unsigned int added = txq->added;
618         unsigned int pushed = added;
619         unsigned int pkts_sent = 0;
620         efx_desc_t *pend = &txq->pend_desc[0];
621         const unsigned int hard_max_fill = EFX_TXQ_LIMIT(txq->ptr_mask + 1);
622         const unsigned int soft_max_fill = hard_max_fill - txq->free_thresh;
623         unsigned int fill_level = added - txq->completed;
624         boolean_t reap_done;
625         int rc __rte_unused;
626         struct rte_mbuf **pktp;
627
628         if (unlikely((txq->state & SFC_TXQ_RUNNING) == 0))
629                 goto done;
630
631         /*
632          * If insufficient space for a single packet is present,
633          * we should reap; otherwise, we shouldn't do that all the time
634          * to avoid latency increase
635          */
636         reap_done = (fill_level > soft_max_fill);
637
638         if (reap_done) {
639                 sfc_tx_reap(txq);
640                 /*
641                  * Recalculate fill level since 'txq->completed'
642                  * might have changed on reap
643                  */
644                 fill_level = added - txq->completed;
645         }
646
647         for (pkts_sent = 0, pktp = &tx_pkts[0];
648              (pkts_sent < nb_pkts) && (fill_level <= soft_max_fill);
649              pkts_sent++, pktp++) {
650                 struct rte_mbuf         *m_seg = *pktp;
651                 size_t                  pkt_len = m_seg->pkt_len;
652                 unsigned int            pkt_descs = 0;
653                 size_t                  in_off = 0;
654
655                 /*
656                  * Here VLAN TCI is expected to be zero in case if no
657                  * DEV_TX_VLAN_OFFLOAD capability is advertised;
658                  * if the calling app ignores the absence of
659                  * DEV_TX_VLAN_OFFLOAD and pushes VLAN TCI, then
660                  * TX_ERROR will occur
661                  */
662                 pkt_descs += sfc_tx_maybe_insert_tag(txq, m_seg, &pend);
663
664                 if (m_seg->ol_flags & PKT_TX_TCP_SEG) {
665                         /*
666                          * We expect correct 'pkt->l[2, 3, 4]_len' values
667                          * to be set correctly by the caller
668                          */
669                         if (sfc_tso_do(txq, added, &m_seg, &in_off, &pend,
670                                        &pkt_descs, &pkt_len) != 0) {
671                                 /* We may have reached this place for
672                                  * one of the following reasons:
673                                  *
674                                  * 1) Packet header length is greater
675                                  *    than SFC_TSOH_STD_LEN
676                                  * 2) TCP header starts at more then
677                                  *    208 bytes into the frame
678                                  *
679                                  * We will deceive RTE saying that we have sent
680                                  * the packet, but we will actually drop it.
681                                  * Hence, we should revert 'pend' to the
682                                  * previous state (in case we have added
683                                  * VLAN descriptor) and start processing
684                                  * another one packet. But the original
685                                  * mbuf shouldn't be orphaned
686                                  */
687                                 pend -= pkt_descs;
688
689                                 rte_pktmbuf_free(*pktp);
690
691                                 continue;
692                         }
693
694                         /*
695                          * We've only added 2 FATSOv2 option descriptors
696                          * and 1 descriptor for the linearized packet header.
697                          * The outstanding work will be done in the same manner
698                          * as for the usual non-TSO path
699                          */
700                 }
701
702                 for (; m_seg != NULL; m_seg = m_seg->next) {
703                         efsys_dma_addr_t        next_frag;
704                         size_t                  seg_len;
705
706                         seg_len = m_seg->data_len;
707                         next_frag = rte_mbuf_data_dma_addr(m_seg);
708
709                         /*
710                          * If we've started TSO transaction few steps earlier,
711                          * we'll skip packet header using an offset in the
712                          * current segment (which has been set to the
713                          * first one containing payload)
714                          */
715                         seg_len -= in_off;
716                         next_frag += in_off;
717                         in_off = 0;
718
719                         do {
720                                 efsys_dma_addr_t        frag_addr = next_frag;
721                                 size_t                  frag_len;
722
723                                 /*
724                                  * It is assumed here that there is no
725                                  * limitation on address boundary
726                                  * crossing by DMA descriptor.
727                                  */
728                                 frag_len = MIN(seg_len, txq->dma_desc_size_max);
729                                 next_frag += frag_len;
730                                 seg_len -= frag_len;
731                                 pkt_len -= frag_len;
732
733                                 efx_tx_qdesc_dma_create(txq->common,
734                                                         frag_addr, frag_len,
735                                                         (pkt_len == 0),
736                                                         pend++);
737
738                                 pkt_descs++;
739                         } while (seg_len != 0);
740                 }
741
742                 added += pkt_descs;
743
744                 fill_level += pkt_descs;
745                 if (unlikely(fill_level > hard_max_fill)) {
746                         /*
747                          * Our estimation for maximum number of descriptors
748                          * required to send a packet seems to be wrong.
749                          * Try to reap (if we haven't yet).
750                          */
751                         if (!reap_done) {
752                                 sfc_tx_reap(txq);
753                                 reap_done = B_TRUE;
754                                 fill_level = added - txq->completed;
755                                 if (fill_level > hard_max_fill) {
756                                         pend -= pkt_descs;
757                                         break;
758                                 }
759                         } else {
760                                 pend -= pkt_descs;
761                                 break;
762                         }
763                 }
764
765                 /* Assign mbuf to the last used desc */
766                 txq->sw_ring[(added - 1) & txq->ptr_mask].mbuf = *pktp;
767         }
768
769         if (likely(pkts_sent > 0)) {
770                 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc,
771                                        pend - &txq->pend_desc[0],
772                                        txq->completed, &txq->added);
773                 SFC_ASSERT(rc == 0);
774
775                 if (likely(pushed != txq->added))
776                         efx_tx_qpush(txq->common, txq->added, pushed);
777         }
778
779 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
780         if (!reap_done)
781                 sfc_tx_reap(txq);
782 #endif
783
784 done:
785         return pkts_sent;
786 }