net/sfc: make TSO a datapath-dependent feature
[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 #include "sfc_kvargs.h"
39
40 /*
41  * Maximum number of TX queue flush attempts in case of
42  * failure or flush timeout
43  */
44 #define SFC_TX_QFLUSH_ATTEMPTS          (3)
45
46 /*
47  * Time to wait between event queue polling attempts when waiting for TX
48  * queue flush done or flush failed events
49  */
50 #define SFC_TX_QFLUSH_POLL_WAIT_MS      (1)
51
52 /*
53  * Maximum number of event queue polling attempts when waiting for TX queue
54  * flush done or flush failed events; it defines TX queue flush attempt timeout
55  * together with SFC_TX_QFLUSH_POLL_WAIT_MS
56  */
57 #define SFC_TX_QFLUSH_POLL_ATTEMPTS     (2000)
58
59 static int
60 sfc_tx_qcheck_conf(struct sfc_adapter *sa, uint16_t nb_tx_desc,
61                    const struct rte_eth_txconf *tx_conf)
62 {
63         unsigned int flags = tx_conf->txq_flags;
64         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
65         int rc = 0;
66
67         if (tx_conf->tx_rs_thresh != 0) {
68                 sfc_err(sa, "RS bit in transmit descriptor is not supported");
69                 rc = EINVAL;
70         }
71
72         if (tx_conf->tx_free_thresh > EFX_TXQ_LIMIT(nb_tx_desc)) {
73                 sfc_err(sa,
74                         "TxQ free threshold too large: %u vs maximum %u",
75                         tx_conf->tx_free_thresh, EFX_TXQ_LIMIT(nb_tx_desc));
76                 rc = EINVAL;
77         }
78
79         if (tx_conf->tx_thresh.pthresh != 0 ||
80             tx_conf->tx_thresh.hthresh != 0 ||
81             tx_conf->tx_thresh.wthresh != 0) {
82                 sfc_err(sa,
83                         "prefetch/host/writeback thresholds are not supported");
84                 rc = EINVAL;
85         }
86
87         if ((flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
88                 if (!encp->enc_hw_tx_insert_vlan_enabled) {
89                         sfc_err(sa, "VLAN offload is not supported");
90                         rc = EINVAL;
91                 } else if (~sa->dp_tx->features & SFC_DP_TX_FEAT_VLAN_INSERT) {
92                         sfc_err(sa,
93                                 "VLAN offload is not supported by %s datapath",
94                                 sa->dp_tx->dp.name);
95                         rc = EINVAL;
96                 }
97         }
98
99         if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
100                 sfc_err(sa, "SCTP offload is not supported");
101                 rc = EINVAL;
102         }
103
104         /* We either perform both TCP and UDP offload, or no offload at all */
105         if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
106             ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
107                 sfc_err(sa, "TCP and UDP offloads can't be set independently");
108                 rc = EINVAL;
109         }
110
111         return rc;
112 }
113
114 void
115 sfc_tx_qflush_done(struct sfc_txq *txq)
116 {
117         txq->state |= SFC_TXQ_FLUSHED;
118         txq->state &= ~SFC_TXQ_FLUSHING;
119 }
120
121 int
122 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
123              uint16_t nb_tx_desc, unsigned int socket_id,
124              const struct rte_eth_txconf *tx_conf)
125 {
126         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
127         struct sfc_txq_info *txq_info;
128         struct sfc_evq *evq;
129         struct sfc_txq *txq;
130         unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
131         int rc = 0;
132         struct sfc_dp_tx_qcreate_info info;
133
134         sfc_log_init(sa, "TxQ = %u", sw_index);
135
136         rc = sfc_tx_qcheck_conf(sa, nb_tx_desc, tx_conf);
137         if (rc != 0)
138                 goto fail_bad_conf;
139
140         SFC_ASSERT(sw_index < sa->txq_count);
141         txq_info = &sa->txq_info[sw_index];
142
143         SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
144         txq_info->entries = nb_tx_desc;
145
146         rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
147         if (rc != 0)
148                 goto fail_ev_qinit;
149
150         evq = sa->evq_info[evq_index].evq;
151
152         rc = ENOMEM;
153         txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
154         if (txq == NULL)
155                 goto fail_txq_alloc;
156
157         txq_info->txq = txq;
158
159         txq->hw_index = sw_index;
160         txq->evq = evq;
161         txq->free_thresh =
162                 (tx_conf->tx_free_thresh) ? tx_conf->tx_free_thresh :
163                 SFC_TX_DEFAULT_FREE_THRESH;
164         txq->flags = tx_conf->txq_flags;
165
166         rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
167                            socket_id, &txq->mem);
168         if (rc != 0)
169                 goto fail_dma_alloc;
170
171         memset(&info, 0, sizeof(info));
172         info.free_thresh = txq->free_thresh;
173         info.flags = tx_conf->txq_flags;
174         info.txq_entries = txq_info->entries;
175         info.dma_desc_size_max = encp->enc_tx_dma_desc_size_max;
176
177         rc = sa->dp_tx->qcreate(sa->eth_dev->data->port_id, sw_index,
178                                 &SFC_DEV_TO_PCI(sa->eth_dev)->addr,
179                                 socket_id, &info, &txq->dp);
180         if (rc != 0)
181                 goto fail_dp_tx_qinit;
182
183         evq->dp_txq = txq->dp;
184
185         txq->state = SFC_TXQ_INITIALIZED;
186
187         txq_info->deferred_start = (tx_conf->tx_deferred_start != 0);
188
189         return 0;
190
191 fail_dp_tx_qinit:
192         sfc_dma_free(sa, &txq->mem);
193
194 fail_dma_alloc:
195         txq_info->txq = NULL;
196         rte_free(txq);
197
198 fail_txq_alloc:
199         sfc_ev_qfini(sa, evq_index);
200
201 fail_ev_qinit:
202         txq_info->entries = 0;
203
204 fail_bad_conf:
205         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
206         return rc;
207 }
208
209 void
210 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
211 {
212         struct sfc_txq_info *txq_info;
213         struct sfc_txq *txq;
214
215         sfc_log_init(sa, "TxQ = %u", sw_index);
216
217         SFC_ASSERT(sw_index < sa->txq_count);
218         txq_info = &sa->txq_info[sw_index];
219
220         txq = txq_info->txq;
221         SFC_ASSERT(txq != NULL);
222         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
223
224         sa->dp_tx->qdestroy(txq->dp);
225         txq->dp = NULL;
226
227         txq_info->txq = NULL;
228         txq_info->entries = 0;
229
230         sfc_dma_free(sa, &txq->mem);
231         rte_free(txq);
232 }
233
234 static int
235 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
236 {
237         sfc_log_init(sa, "TxQ = %u", sw_index);
238
239         return 0;
240 }
241
242 static int
243 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
244 {
245         int rc = 0;
246
247         switch (txmode->mq_mode) {
248         case ETH_MQ_TX_NONE:
249                 break;
250         default:
251                 sfc_err(sa, "Tx multi-queue mode %u not supported",
252                         txmode->mq_mode);
253                 rc = EINVAL;
254         }
255
256         /*
257          * These features are claimed to be i40e-specific,
258          * but it does make sense to double-check their absence
259          */
260         if (txmode->hw_vlan_reject_tagged) {
261                 sfc_err(sa, "Rejecting tagged packets not supported");
262                 rc = EINVAL;
263         }
264
265         if (txmode->hw_vlan_reject_untagged) {
266                 sfc_err(sa, "Rejecting untagged packets not supported");
267                 rc = EINVAL;
268         }
269
270         if (txmode->hw_vlan_insert_pvid) {
271                 sfc_err(sa, "Port-based VLAN insertion not supported");
272                 rc = EINVAL;
273         }
274
275         return rc;
276 }
277
278 int
279 sfc_tx_init(struct sfc_adapter *sa)
280 {
281         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
282         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
283         unsigned int sw_index;
284         int rc = 0;
285
286         /*
287          * The datapath implementation assumes absence of boundary
288          * limits on Tx DMA descriptors. Addition of these checks on
289          * datapath would simply make the datapath slower.
290          */
291         if (encp->enc_tx_dma_desc_boundary != 0) {
292                 rc = ENOTSUP;
293                 goto fail_tx_dma_desc_boundary;
294         }
295
296         if (~sa->dp_tx->features & SFC_DP_TX_FEAT_TSO)
297                 sa->tso = B_FALSE;
298
299         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
300         if (rc != 0)
301                 goto fail_check_mode;
302
303         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
304
305         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
306                                          sizeof(sa->txq_info[0]), 0,
307                                          sa->socket_id);
308         if (sa->txq_info == NULL)
309                 goto fail_txqs_alloc;
310
311         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
312                 rc = sfc_tx_qinit_info(sa, sw_index);
313                 if (rc != 0)
314                         goto fail_tx_qinit_info;
315         }
316
317         return 0;
318
319 fail_tx_qinit_info:
320         rte_free(sa->txq_info);
321         sa->txq_info = NULL;
322
323 fail_txqs_alloc:
324         sa->txq_count = 0;
325
326 fail_check_mode:
327 fail_tx_dma_desc_boundary:
328         sfc_log_init(sa, "failed (rc = %d)", rc);
329         return rc;
330 }
331
332 void
333 sfc_tx_fini(struct sfc_adapter *sa)
334 {
335         int sw_index;
336
337         sw_index = sa->txq_count;
338         while (--sw_index >= 0) {
339                 if (sa->txq_info[sw_index].txq != NULL)
340                         sfc_tx_qfini(sa, sw_index);
341         }
342
343         rte_free(sa->txq_info);
344         sa->txq_info = NULL;
345         sa->txq_count = 0;
346 }
347
348 int
349 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
350 {
351         struct rte_eth_dev_data *dev_data;
352         struct sfc_txq_info *txq_info;
353         struct sfc_txq *txq;
354         struct sfc_evq *evq;
355         uint16_t flags;
356         unsigned int desc_index;
357         int rc = 0;
358
359         sfc_log_init(sa, "TxQ = %u", sw_index);
360
361         SFC_ASSERT(sw_index < sa->txq_count);
362         txq_info = &sa->txq_info[sw_index];
363
364         txq = txq_info->txq;
365
366         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
367
368         evq = txq->evq;
369
370         rc = sfc_ev_qstart(sa, evq->evq_index);
371         if (rc != 0)
372                 goto fail_ev_qstart;
373
374         /*
375          * It seems that DPDK has no controls regarding IPv4 offloads,
376          * hence, we always enable it here
377          */
378         if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
379             (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP)) {
380                 flags = EFX_TXQ_CKSUM_IPV4;
381         } else {
382                 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
383
384                 if (sa->tso)
385                         flags |= EFX_TXQ_FATSOV2;
386         }
387
388         rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
389                             txq_info->entries, 0 /* not used on EF10 */,
390                             flags, evq->common,
391                             &txq->common, &desc_index);
392         if (rc != 0) {
393                 if (sa->tso && (rc == ENOSPC))
394                         sfc_err(sa, "ran out of TSO contexts");
395
396                 goto fail_tx_qcreate;
397         }
398
399         efx_tx_qenable(txq->common);
400
401         txq->state |= SFC_TXQ_STARTED;
402
403         rc = sa->dp_tx->qstart(txq->dp, evq->read_ptr, desc_index);
404         if (rc != 0)
405                 goto fail_dp_qstart;
406
407         /*
408          * It seems to be used by DPDK for debug purposes only ('rte_ether')
409          */
410         dev_data = sa->eth_dev->data;
411         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
412
413         return 0;
414
415 fail_dp_qstart:
416         txq->state = SFC_TXQ_INITIALIZED;
417         efx_tx_qdestroy(txq->common);
418
419 fail_tx_qcreate:
420         sfc_ev_qstop(sa, evq->evq_index);
421
422 fail_ev_qstart:
423         return rc;
424 }
425
426 void
427 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
428 {
429         struct rte_eth_dev_data *dev_data;
430         struct sfc_txq_info *txq_info;
431         struct sfc_txq *txq;
432         unsigned int retry_count;
433         unsigned int wait_count;
434
435         sfc_log_init(sa, "TxQ = %u", sw_index);
436
437         SFC_ASSERT(sw_index < sa->txq_count);
438         txq_info = &sa->txq_info[sw_index];
439
440         txq = txq_info->txq;
441
442         if (txq->state == SFC_TXQ_INITIALIZED)
443                 return;
444
445         SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
446
447         sa->dp_tx->qstop(txq->dp, &txq->evq->read_ptr);
448
449         /*
450          * Retry TX queue flushing in case of flush failed or
451          * timeout; in the worst case it can delay for 6 seconds
452          */
453         for (retry_count = 0;
454              ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
455              (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
456              ++retry_count) {
457                 if (efx_tx_qflush(txq->common) != 0) {
458                         txq->state |= SFC_TXQ_FLUSHING;
459                         break;
460                 }
461
462                 /*
463                  * Wait for TX queue flush done or flush failed event at least
464                  * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
465                  * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
466                  * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
467                  */
468                 wait_count = 0;
469                 do {
470                         rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
471                         sfc_ev_qpoll(txq->evq);
472                 } while ((txq->state & SFC_TXQ_FLUSHING) &&
473                          wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
474
475                 if (txq->state & SFC_TXQ_FLUSHING)
476                         sfc_err(sa, "TxQ %u flush timed out", sw_index);
477
478                 if (txq->state & SFC_TXQ_FLUSHED)
479                         sfc_info(sa, "TxQ %u flushed", sw_index);
480         }
481
482         sa->dp_tx->qreap(txq->dp);
483
484         txq->state = SFC_TXQ_INITIALIZED;
485
486         efx_tx_qdestroy(txq->common);
487
488         sfc_ev_qstop(sa, txq->evq->evq_index);
489
490         /*
491          * It seems to be used by DPDK for debug purposes only ('rte_ether')
492          */
493         dev_data = sa->eth_dev->data;
494         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
495 }
496
497 int
498 sfc_tx_start(struct sfc_adapter *sa)
499 {
500         unsigned int sw_index;
501         int rc = 0;
502
503         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
504
505         if (sa->tso) {
506                 if (!efx_nic_cfg_get(sa->nic)->enc_fw_assisted_tso_v2_enabled) {
507                         sfc_warn(sa, "TSO support was unable to be restored");
508                         sa->tso = B_FALSE;
509                 }
510         }
511
512         rc = efx_tx_init(sa->nic);
513         if (rc != 0)
514                 goto fail_efx_tx_init;
515
516         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
517                 if (!(sa->txq_info[sw_index].deferred_start) ||
518                     sa->txq_info[sw_index].deferred_started) {
519                         rc = sfc_tx_qstart(sa, sw_index);
520                         if (rc != 0)
521                                 goto fail_tx_qstart;
522                 }
523         }
524
525         return 0;
526
527 fail_tx_qstart:
528         while (sw_index-- > 0)
529                 sfc_tx_qstop(sa, sw_index);
530
531         efx_tx_fini(sa->nic);
532
533 fail_efx_tx_init:
534         sfc_log_init(sa, "failed (rc = %d)", rc);
535         return rc;
536 }
537
538 void
539 sfc_tx_stop(struct sfc_adapter *sa)
540 {
541         unsigned int sw_index;
542
543         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
544
545         sw_index = sa->txq_count;
546         while (sw_index-- > 0) {
547                 if (sa->txq_info[sw_index].txq != NULL)
548                         sfc_tx_qstop(sa, sw_index);
549         }
550
551         efx_tx_fini(sa->nic);
552 }
553
554 static void
555 sfc_efx_tx_reap(struct sfc_efx_txq *txq)
556 {
557         unsigned int completed;
558
559         sfc_ev_qpoll(txq->evq);
560
561         for (completed = txq->completed;
562              completed != txq->pending; completed++) {
563                 struct sfc_efx_tx_sw_desc *txd;
564
565                 txd = &txq->sw_ring[completed & txq->ptr_mask];
566
567                 if (txd->mbuf != NULL) {
568                         rte_pktmbuf_free(txd->mbuf);
569                         txd->mbuf = NULL;
570                 }
571         }
572
573         txq->completed = completed;
574 }
575
576 /*
577  * The function is used to insert or update VLAN tag;
578  * the firmware has state of the firmware tag to insert per TxQ
579  * (controlled by option descriptors), hence, if the tag of the
580  * packet to be sent is different from one remembered by the firmware,
581  * the function will update it
582  */
583 static unsigned int
584 sfc_efx_tx_maybe_insert_tag(struct sfc_efx_txq *txq, struct rte_mbuf *m,
585                             efx_desc_t **pend)
586 {
587         uint16_t this_tag = ((m->ol_flags & PKT_TX_VLAN_PKT) ?
588                              m->vlan_tci : 0);
589
590         if (this_tag == txq->hw_vlan_tci)
591                 return 0;
592
593         /*
594          * The expression inside SFC_ASSERT() is not desired to be checked in
595          * a non-debug build because it might be too expensive on the data path
596          */
597         SFC_ASSERT(efx_nic_cfg_get(txq->evq->sa->nic)->enc_hw_tx_insert_vlan_enabled);
598
599         efx_tx_qdesc_vlantci_create(txq->common, rte_cpu_to_be_16(this_tag),
600                                     *pend);
601         (*pend)++;
602         txq->hw_vlan_tci = this_tag;
603
604         return 1;
605 }
606
607 static uint16_t
608 sfc_efx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
609 {
610         struct sfc_dp_txq *dp_txq = (struct sfc_dp_txq *)tx_queue;
611         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
612         unsigned int added = txq->added;
613         unsigned int pushed = added;
614         unsigned int pkts_sent = 0;
615         efx_desc_t *pend = &txq->pend_desc[0];
616         const unsigned int hard_max_fill = EFX_TXQ_LIMIT(txq->ptr_mask + 1);
617         const unsigned int soft_max_fill = hard_max_fill - txq->free_thresh;
618         unsigned int fill_level = added - txq->completed;
619         boolean_t reap_done;
620         int rc __rte_unused;
621         struct rte_mbuf **pktp;
622
623         if (unlikely((txq->flags & SFC_EFX_TXQ_FLAG_RUNNING) == 0))
624                 goto done;
625
626         /*
627          * If insufficient space for a single packet is present,
628          * we should reap; otherwise, we shouldn't do that all the time
629          * to avoid latency increase
630          */
631         reap_done = (fill_level > soft_max_fill);
632
633         if (reap_done) {
634                 sfc_efx_tx_reap(txq);
635                 /*
636                  * Recalculate fill level since 'txq->completed'
637                  * might have changed on reap
638                  */
639                 fill_level = added - txq->completed;
640         }
641
642         for (pkts_sent = 0, pktp = &tx_pkts[0];
643              (pkts_sent < nb_pkts) && (fill_level <= soft_max_fill);
644              pkts_sent++, pktp++) {
645                 struct rte_mbuf         *m_seg = *pktp;
646                 size_t                  pkt_len = m_seg->pkt_len;
647                 unsigned int            pkt_descs = 0;
648                 size_t                  in_off = 0;
649
650                 /*
651                  * Here VLAN TCI is expected to be zero in case if no
652                  * DEV_TX_VLAN_OFFLOAD capability is advertised;
653                  * if the calling app ignores the absence of
654                  * DEV_TX_VLAN_OFFLOAD and pushes VLAN TCI, then
655                  * TX_ERROR will occur
656                  */
657                 pkt_descs += sfc_efx_tx_maybe_insert_tag(txq, m_seg, &pend);
658
659                 if (m_seg->ol_flags & PKT_TX_TCP_SEG) {
660                         /*
661                          * We expect correct 'pkt->l[2, 3, 4]_len' values
662                          * to be set correctly by the caller
663                          */
664                         if (sfc_efx_tso_do(txq, added, &m_seg, &in_off, &pend,
665                                            &pkt_descs, &pkt_len) != 0) {
666                                 /* We may have reached this place for
667                                  * one of the following reasons:
668                                  *
669                                  * 1) Packet header length is greater
670                                  *    than SFC_TSOH_STD_LEN
671                                  * 2) TCP header starts at more then
672                                  *    208 bytes into the frame
673                                  *
674                                  * We will deceive RTE saying that we have sent
675                                  * the packet, but we will actually drop it.
676                                  * Hence, we should revert 'pend' to the
677                                  * previous state (in case we have added
678                                  * VLAN descriptor) and start processing
679                                  * another one packet. But the original
680                                  * mbuf shouldn't be orphaned
681                                  */
682                                 pend -= pkt_descs;
683
684                                 rte_pktmbuf_free(*pktp);
685
686                                 continue;
687                         }
688
689                         /*
690                          * We've only added 2 FATSOv2 option descriptors
691                          * and 1 descriptor for the linearized packet header.
692                          * The outstanding work will be done in the same manner
693                          * as for the usual non-TSO path
694                          */
695                 }
696
697                 for (; m_seg != NULL; m_seg = m_seg->next) {
698                         efsys_dma_addr_t        next_frag;
699                         size_t                  seg_len;
700
701                         seg_len = m_seg->data_len;
702                         next_frag = rte_mbuf_data_dma_addr(m_seg);
703
704                         /*
705                          * If we've started TSO transaction few steps earlier,
706                          * we'll skip packet header using an offset in the
707                          * current segment (which has been set to the
708                          * first one containing payload)
709                          */
710                         seg_len -= in_off;
711                         next_frag += in_off;
712                         in_off = 0;
713
714                         do {
715                                 efsys_dma_addr_t        frag_addr = next_frag;
716                                 size_t                  frag_len;
717
718                                 /*
719                                  * It is assumed here that there is no
720                                  * limitation on address boundary
721                                  * crossing by DMA descriptor.
722                                  */
723                                 frag_len = MIN(seg_len, txq->dma_desc_size_max);
724                                 next_frag += frag_len;
725                                 seg_len -= frag_len;
726                                 pkt_len -= frag_len;
727
728                                 efx_tx_qdesc_dma_create(txq->common,
729                                                         frag_addr, frag_len,
730                                                         (pkt_len == 0),
731                                                         pend++);
732
733                                 pkt_descs++;
734                         } while (seg_len != 0);
735                 }
736
737                 added += pkt_descs;
738
739                 fill_level += pkt_descs;
740                 if (unlikely(fill_level > hard_max_fill)) {
741                         /*
742                          * Our estimation for maximum number of descriptors
743                          * required to send a packet seems to be wrong.
744                          * Try to reap (if we haven't yet).
745                          */
746                         if (!reap_done) {
747                                 sfc_efx_tx_reap(txq);
748                                 reap_done = B_TRUE;
749                                 fill_level = added - txq->completed;
750                                 if (fill_level > hard_max_fill) {
751                                         pend -= pkt_descs;
752                                         break;
753                                 }
754                         } else {
755                                 pend -= pkt_descs;
756                                 break;
757                         }
758                 }
759
760                 /* Assign mbuf to the last used desc */
761                 txq->sw_ring[(added - 1) & txq->ptr_mask].mbuf = *pktp;
762         }
763
764         if (likely(pkts_sent > 0)) {
765                 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc,
766                                        pend - &txq->pend_desc[0],
767                                        txq->completed, &txq->added);
768                 SFC_ASSERT(rc == 0);
769
770                 if (likely(pushed != txq->added))
771                         efx_tx_qpush(txq->common, txq->added, pushed);
772         }
773
774 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
775         if (!reap_done)
776                 sfc_efx_tx_reap(txq);
777 #endif
778
779 done:
780         return pkts_sent;
781 }
782
783 struct sfc_txq *
784 sfc_txq_by_dp_txq(const struct sfc_dp_txq *dp_txq)
785 {
786         const struct sfc_dp_queue *dpq = &dp_txq->dpq;
787         struct rte_eth_dev *eth_dev;
788         struct sfc_adapter *sa;
789         struct sfc_txq *txq;
790
791         SFC_ASSERT(rte_eth_dev_is_valid_port(dpq->port_id));
792         eth_dev = &rte_eth_devices[dpq->port_id];
793
794         sa = eth_dev->data->dev_private;
795
796         SFC_ASSERT(dpq->queue_id < sa->txq_count);
797         txq = sa->txq_info[dpq->queue_id].txq;
798
799         SFC_ASSERT(txq != NULL);
800         return txq;
801 }
802
803 static sfc_dp_tx_qcreate_t sfc_efx_tx_qcreate;
804 static int
805 sfc_efx_tx_qcreate(uint16_t port_id, uint16_t queue_id,
806                    const struct rte_pci_addr *pci_addr,
807                    int socket_id,
808                    const struct sfc_dp_tx_qcreate_info *info,
809                    struct sfc_dp_txq **dp_txqp)
810 {
811         struct sfc_efx_txq *txq;
812         struct sfc_txq *ctrl_txq;
813         int rc;
814
815         rc = ENOMEM;
816         txq = rte_zmalloc_socket("sfc-efx-txq", sizeof(*txq),
817                                  RTE_CACHE_LINE_SIZE, socket_id);
818         if (txq == NULL)
819                 goto fail_txq_alloc;
820
821         sfc_dp_queue_init(&txq->dp.dpq, port_id, queue_id, pci_addr);
822
823         rc = ENOMEM;
824         txq->pend_desc = rte_calloc_socket("sfc-efx-txq-pend-desc",
825                                            EFX_TXQ_LIMIT(info->txq_entries),
826                                            sizeof(*txq->pend_desc), 0,
827                                            socket_id);
828         if (txq->pend_desc == NULL)
829                 goto fail_pend_desc_alloc;
830
831         rc = ENOMEM;
832         txq->sw_ring = rte_calloc_socket("sfc-efx-txq-sw_ring",
833                                          info->txq_entries,
834                                          sizeof(*txq->sw_ring),
835                                          RTE_CACHE_LINE_SIZE, socket_id);
836         if (txq->sw_ring == NULL)
837                 goto fail_sw_ring_alloc;
838
839         ctrl_txq = sfc_txq_by_dp_txq(&txq->dp);
840         if (ctrl_txq->evq->sa->tso) {
841                 rc = sfc_efx_tso_alloc_tsoh_objs(txq->sw_ring,
842                                                  info->txq_entries, socket_id);
843                 if (rc != 0)
844                         goto fail_alloc_tsoh_objs;
845         }
846
847         txq->evq = ctrl_txq->evq;
848         txq->ptr_mask = info->txq_entries - 1;
849         txq->free_thresh = info->free_thresh;
850         txq->dma_desc_size_max = info->dma_desc_size_max;
851
852         *dp_txqp = &txq->dp;
853         return 0;
854
855 fail_alloc_tsoh_objs:
856         rte_free(txq->sw_ring);
857
858 fail_sw_ring_alloc:
859         rte_free(txq->pend_desc);
860
861 fail_pend_desc_alloc:
862         rte_free(txq);
863
864 fail_txq_alloc:
865         return rc;
866 }
867
868 static sfc_dp_tx_qdestroy_t sfc_efx_tx_qdestroy;
869 static void
870 sfc_efx_tx_qdestroy(struct sfc_dp_txq *dp_txq)
871 {
872         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
873
874         sfc_efx_tso_free_tsoh_objs(txq->sw_ring, txq->ptr_mask + 1);
875         rte_free(txq->sw_ring);
876         rte_free(txq->pend_desc);
877         rte_free(txq);
878 }
879
880 static sfc_dp_tx_qstart_t sfc_efx_tx_qstart;
881 static int
882 sfc_efx_tx_qstart(struct sfc_dp_txq *dp_txq,
883                   __rte_unused unsigned int evq_read_ptr,
884                   unsigned int txq_desc_index)
885 {
886         /* libefx-based datapath is specific to libefx-based PMD */
887         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
888         struct sfc_txq *ctrl_txq = sfc_txq_by_dp_txq(dp_txq);
889
890         txq->common = ctrl_txq->common;
891
892         txq->pending = txq->completed = txq->added = txq_desc_index;
893         txq->hw_vlan_tci = 0;
894
895         txq->flags |= (SFC_EFX_TXQ_FLAG_STARTED | SFC_EFX_TXQ_FLAG_RUNNING);
896
897         return 0;
898 }
899
900 static sfc_dp_tx_qstop_t sfc_efx_tx_qstop;
901 static void
902 sfc_efx_tx_qstop(struct sfc_dp_txq *dp_txq,
903                  __rte_unused unsigned int *evq_read_ptr)
904 {
905         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
906
907         txq->flags &= ~SFC_EFX_TXQ_FLAG_RUNNING;
908 }
909
910 static sfc_dp_tx_qreap_t sfc_efx_tx_qreap;
911 static void
912 sfc_efx_tx_qreap(struct sfc_dp_txq *dp_txq)
913 {
914         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
915         unsigned int txds;
916
917         sfc_efx_tx_reap(txq);
918
919         for (txds = 0; txds <= txq->ptr_mask; txds++) {
920                 if (txq->sw_ring[txds].mbuf != NULL) {
921                         rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
922                         txq->sw_ring[txds].mbuf = NULL;
923                 }
924         }
925
926         txq->flags &= ~SFC_EFX_TXQ_FLAG_STARTED;
927 }
928
929 struct sfc_dp_tx sfc_efx_tx = {
930         .dp = {
931                 .name           = SFC_KVARG_DATAPATH_EFX,
932                 .type           = SFC_DP_TX,
933                 .hw_fw_caps     = 0,
934         },
935         .features               = SFC_DP_TX_FEAT_VLAN_INSERT |
936                                   SFC_DP_TX_FEAT_TSO,
937         .qcreate                = sfc_efx_tx_qcreate,
938         .qdestroy               = sfc_efx_tx_qdestroy,
939         .qstart                 = sfc_efx_tx_qstart,
940         .qstop                  = sfc_efx_tx_qstop,
941         .qreap                  = sfc_efx_tx_qreap,
942         .pkt_burst              = sfc_efx_xmit_pkts,
943 };