net/sfc: implement EF10 native Tx datapath
[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         info.txq_hw_ring = txq->mem.esm_base;
177         info.evq_entries = txq_info->entries;
178         info.evq_hw_ring = evq->mem.esm_base;
179         info.hw_index = txq->hw_index;
180         info.mem_bar = sa->mem_bar.esb_base;
181
182         rc = sa->dp_tx->qcreate(sa->eth_dev->data->port_id, sw_index,
183                                 &SFC_DEV_TO_PCI(sa->eth_dev)->addr,
184                                 socket_id, &info, &txq->dp);
185         if (rc != 0)
186                 goto fail_dp_tx_qinit;
187
188         evq->dp_txq = txq->dp;
189
190         txq->state = SFC_TXQ_INITIALIZED;
191
192         txq_info->deferred_start = (tx_conf->tx_deferred_start != 0);
193
194         return 0;
195
196 fail_dp_tx_qinit:
197         sfc_dma_free(sa, &txq->mem);
198
199 fail_dma_alloc:
200         txq_info->txq = NULL;
201         rte_free(txq);
202
203 fail_txq_alloc:
204         sfc_ev_qfini(sa, evq_index);
205
206 fail_ev_qinit:
207         txq_info->entries = 0;
208
209 fail_bad_conf:
210         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
211         return rc;
212 }
213
214 void
215 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
216 {
217         struct sfc_txq_info *txq_info;
218         struct sfc_txq *txq;
219
220         sfc_log_init(sa, "TxQ = %u", sw_index);
221
222         SFC_ASSERT(sw_index < sa->txq_count);
223         txq_info = &sa->txq_info[sw_index];
224
225         txq = txq_info->txq;
226         SFC_ASSERT(txq != NULL);
227         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
228
229         sa->dp_tx->qdestroy(txq->dp);
230         txq->dp = NULL;
231
232         txq_info->txq = NULL;
233         txq_info->entries = 0;
234
235         sfc_dma_free(sa, &txq->mem);
236         rte_free(txq);
237 }
238
239 static int
240 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
241 {
242         sfc_log_init(sa, "TxQ = %u", sw_index);
243
244         return 0;
245 }
246
247 static int
248 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
249 {
250         int rc = 0;
251
252         switch (txmode->mq_mode) {
253         case ETH_MQ_TX_NONE:
254                 break;
255         default:
256                 sfc_err(sa, "Tx multi-queue mode %u not supported",
257                         txmode->mq_mode);
258                 rc = EINVAL;
259         }
260
261         /*
262          * These features are claimed to be i40e-specific,
263          * but it does make sense to double-check their absence
264          */
265         if (txmode->hw_vlan_reject_tagged) {
266                 sfc_err(sa, "Rejecting tagged packets not supported");
267                 rc = EINVAL;
268         }
269
270         if (txmode->hw_vlan_reject_untagged) {
271                 sfc_err(sa, "Rejecting untagged packets not supported");
272                 rc = EINVAL;
273         }
274
275         if (txmode->hw_vlan_insert_pvid) {
276                 sfc_err(sa, "Port-based VLAN insertion not supported");
277                 rc = EINVAL;
278         }
279
280         return rc;
281 }
282
283 int
284 sfc_tx_init(struct sfc_adapter *sa)
285 {
286         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
287         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
288         unsigned int sw_index;
289         int rc = 0;
290
291         /*
292          * The datapath implementation assumes absence of boundary
293          * limits on Tx DMA descriptors. Addition of these checks on
294          * datapath would simply make the datapath slower.
295          */
296         if (encp->enc_tx_dma_desc_boundary != 0) {
297                 rc = ENOTSUP;
298                 goto fail_tx_dma_desc_boundary;
299         }
300
301         if (~sa->dp_tx->features & SFC_DP_TX_FEAT_TSO)
302                 sa->tso = B_FALSE;
303
304         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
305         if (rc != 0)
306                 goto fail_check_mode;
307
308         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
309
310         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
311                                          sizeof(sa->txq_info[0]), 0,
312                                          sa->socket_id);
313         if (sa->txq_info == NULL)
314                 goto fail_txqs_alloc;
315
316         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
317                 rc = sfc_tx_qinit_info(sa, sw_index);
318                 if (rc != 0)
319                         goto fail_tx_qinit_info;
320         }
321
322         return 0;
323
324 fail_tx_qinit_info:
325         rte_free(sa->txq_info);
326         sa->txq_info = NULL;
327
328 fail_txqs_alloc:
329         sa->txq_count = 0;
330
331 fail_check_mode:
332 fail_tx_dma_desc_boundary:
333         sfc_log_init(sa, "failed (rc = %d)", rc);
334         return rc;
335 }
336
337 void
338 sfc_tx_fini(struct sfc_adapter *sa)
339 {
340         int sw_index;
341
342         sw_index = sa->txq_count;
343         while (--sw_index >= 0) {
344                 if (sa->txq_info[sw_index].txq != NULL)
345                         sfc_tx_qfini(sa, sw_index);
346         }
347
348         rte_free(sa->txq_info);
349         sa->txq_info = NULL;
350         sa->txq_count = 0;
351 }
352
353 int
354 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
355 {
356         struct rte_eth_dev_data *dev_data;
357         struct sfc_txq_info *txq_info;
358         struct sfc_txq *txq;
359         struct sfc_evq *evq;
360         uint16_t flags;
361         unsigned int desc_index;
362         int rc = 0;
363
364         sfc_log_init(sa, "TxQ = %u", sw_index);
365
366         SFC_ASSERT(sw_index < sa->txq_count);
367         txq_info = &sa->txq_info[sw_index];
368
369         txq = txq_info->txq;
370
371         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
372
373         evq = txq->evq;
374
375         rc = sfc_ev_qstart(sa, evq->evq_index);
376         if (rc != 0)
377                 goto fail_ev_qstart;
378
379         /*
380          * It seems that DPDK has no controls regarding IPv4 offloads,
381          * hence, we always enable it here
382          */
383         if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
384             (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP)) {
385                 flags = EFX_TXQ_CKSUM_IPV4;
386         } else {
387                 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
388
389                 if (sa->tso)
390                         flags |= EFX_TXQ_FATSOV2;
391         }
392
393         rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
394                             txq_info->entries, 0 /* not used on EF10 */,
395                             flags, evq->common,
396                             &txq->common, &desc_index);
397         if (rc != 0) {
398                 if (sa->tso && (rc == ENOSPC))
399                         sfc_err(sa, "ran out of TSO contexts");
400
401                 goto fail_tx_qcreate;
402         }
403
404         efx_tx_qenable(txq->common);
405
406         txq->state |= SFC_TXQ_STARTED;
407
408         rc = sa->dp_tx->qstart(txq->dp, evq->read_ptr, desc_index);
409         if (rc != 0)
410                 goto fail_dp_qstart;
411
412         /*
413          * It seems to be used by DPDK for debug purposes only ('rte_ether')
414          */
415         dev_data = sa->eth_dev->data;
416         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
417
418         return 0;
419
420 fail_dp_qstart:
421         txq->state = SFC_TXQ_INITIALIZED;
422         efx_tx_qdestroy(txq->common);
423
424 fail_tx_qcreate:
425         sfc_ev_qstop(sa, evq->evq_index);
426
427 fail_ev_qstart:
428         return rc;
429 }
430
431 void
432 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
433 {
434         struct rte_eth_dev_data *dev_data;
435         struct sfc_txq_info *txq_info;
436         struct sfc_txq *txq;
437         unsigned int retry_count;
438         unsigned int wait_count;
439
440         sfc_log_init(sa, "TxQ = %u", sw_index);
441
442         SFC_ASSERT(sw_index < sa->txq_count);
443         txq_info = &sa->txq_info[sw_index];
444
445         txq = txq_info->txq;
446
447         if (txq->state == SFC_TXQ_INITIALIZED)
448                 return;
449
450         SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
451
452         sa->dp_tx->qstop(txq->dp, &txq->evq->read_ptr);
453
454         /*
455          * Retry TX queue flushing in case of flush failed or
456          * timeout; in the worst case it can delay for 6 seconds
457          */
458         for (retry_count = 0;
459              ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
460              (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
461              ++retry_count) {
462                 if (efx_tx_qflush(txq->common) != 0) {
463                         txq->state |= SFC_TXQ_FLUSHING;
464                         break;
465                 }
466
467                 /*
468                  * Wait for TX queue flush done or flush failed event at least
469                  * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
470                  * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
471                  * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
472                  */
473                 wait_count = 0;
474                 do {
475                         rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
476                         sfc_ev_qpoll(txq->evq);
477                 } while ((txq->state & SFC_TXQ_FLUSHING) &&
478                          wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
479
480                 if (txq->state & SFC_TXQ_FLUSHING)
481                         sfc_err(sa, "TxQ %u flush timed out", sw_index);
482
483                 if (txq->state & SFC_TXQ_FLUSHED)
484                         sfc_info(sa, "TxQ %u flushed", sw_index);
485         }
486
487         sa->dp_tx->qreap(txq->dp);
488
489         txq->state = SFC_TXQ_INITIALIZED;
490
491         efx_tx_qdestroy(txq->common);
492
493         sfc_ev_qstop(sa, txq->evq->evq_index);
494
495         /*
496          * It seems to be used by DPDK for debug purposes only ('rte_ether')
497          */
498         dev_data = sa->eth_dev->data;
499         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
500 }
501
502 int
503 sfc_tx_start(struct sfc_adapter *sa)
504 {
505         unsigned int sw_index;
506         int rc = 0;
507
508         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
509
510         if (sa->tso) {
511                 if (!efx_nic_cfg_get(sa->nic)->enc_fw_assisted_tso_v2_enabled) {
512                         sfc_warn(sa, "TSO support was unable to be restored");
513                         sa->tso = B_FALSE;
514                 }
515         }
516
517         rc = efx_tx_init(sa->nic);
518         if (rc != 0)
519                 goto fail_efx_tx_init;
520
521         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
522                 if (!(sa->txq_info[sw_index].deferred_start) ||
523                     sa->txq_info[sw_index].deferred_started) {
524                         rc = sfc_tx_qstart(sa, sw_index);
525                         if (rc != 0)
526                                 goto fail_tx_qstart;
527                 }
528         }
529
530         return 0;
531
532 fail_tx_qstart:
533         while (sw_index-- > 0)
534                 sfc_tx_qstop(sa, sw_index);
535
536         efx_tx_fini(sa->nic);
537
538 fail_efx_tx_init:
539         sfc_log_init(sa, "failed (rc = %d)", rc);
540         return rc;
541 }
542
543 void
544 sfc_tx_stop(struct sfc_adapter *sa)
545 {
546         unsigned int sw_index;
547
548         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
549
550         sw_index = sa->txq_count;
551         while (sw_index-- > 0) {
552                 if (sa->txq_info[sw_index].txq != NULL)
553                         sfc_tx_qstop(sa, sw_index);
554         }
555
556         efx_tx_fini(sa->nic);
557 }
558
559 static void
560 sfc_efx_tx_reap(struct sfc_efx_txq *txq)
561 {
562         unsigned int completed;
563
564         sfc_ev_qpoll(txq->evq);
565
566         for (completed = txq->completed;
567              completed != txq->pending; completed++) {
568                 struct sfc_efx_tx_sw_desc *txd;
569
570                 txd = &txq->sw_ring[completed & txq->ptr_mask];
571
572                 if (txd->mbuf != NULL) {
573                         rte_pktmbuf_free(txd->mbuf);
574                         txd->mbuf = NULL;
575                 }
576         }
577
578         txq->completed = completed;
579 }
580
581 /*
582  * The function is used to insert or update VLAN tag;
583  * the firmware has state of the firmware tag to insert per TxQ
584  * (controlled by option descriptors), hence, if the tag of the
585  * packet to be sent is different from one remembered by the firmware,
586  * the function will update it
587  */
588 static unsigned int
589 sfc_efx_tx_maybe_insert_tag(struct sfc_efx_txq *txq, struct rte_mbuf *m,
590                             efx_desc_t **pend)
591 {
592         uint16_t this_tag = ((m->ol_flags & PKT_TX_VLAN_PKT) ?
593                              m->vlan_tci : 0);
594
595         if (this_tag == txq->hw_vlan_tci)
596                 return 0;
597
598         /*
599          * The expression inside SFC_ASSERT() is not desired to be checked in
600          * a non-debug build because it might be too expensive on the data path
601          */
602         SFC_ASSERT(efx_nic_cfg_get(txq->evq->sa->nic)->enc_hw_tx_insert_vlan_enabled);
603
604         efx_tx_qdesc_vlantci_create(txq->common, rte_cpu_to_be_16(this_tag),
605                                     *pend);
606         (*pend)++;
607         txq->hw_vlan_tci = this_tag;
608
609         return 1;
610 }
611
612 static uint16_t
613 sfc_efx_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
614 {
615         struct sfc_dp_txq *dp_txq = (struct sfc_dp_txq *)tx_queue;
616         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
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->flags & SFC_EFX_TXQ_FLAG_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_efx_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_efx_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_efx_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_efx_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_efx_tx_reap(txq);
782 #endif
783
784 done:
785         return pkts_sent;
786 }
787
788 struct sfc_txq *
789 sfc_txq_by_dp_txq(const struct sfc_dp_txq *dp_txq)
790 {
791         const struct sfc_dp_queue *dpq = &dp_txq->dpq;
792         struct rte_eth_dev *eth_dev;
793         struct sfc_adapter *sa;
794         struct sfc_txq *txq;
795
796         SFC_ASSERT(rte_eth_dev_is_valid_port(dpq->port_id));
797         eth_dev = &rte_eth_devices[dpq->port_id];
798
799         sa = eth_dev->data->dev_private;
800
801         SFC_ASSERT(dpq->queue_id < sa->txq_count);
802         txq = sa->txq_info[dpq->queue_id].txq;
803
804         SFC_ASSERT(txq != NULL);
805         return txq;
806 }
807
808 static sfc_dp_tx_qcreate_t sfc_efx_tx_qcreate;
809 static int
810 sfc_efx_tx_qcreate(uint16_t port_id, uint16_t queue_id,
811                    const struct rte_pci_addr *pci_addr,
812                    int socket_id,
813                    const struct sfc_dp_tx_qcreate_info *info,
814                    struct sfc_dp_txq **dp_txqp)
815 {
816         struct sfc_efx_txq *txq;
817         struct sfc_txq *ctrl_txq;
818         int rc;
819
820         rc = ENOMEM;
821         txq = rte_zmalloc_socket("sfc-efx-txq", sizeof(*txq),
822                                  RTE_CACHE_LINE_SIZE, socket_id);
823         if (txq == NULL)
824                 goto fail_txq_alloc;
825
826         sfc_dp_queue_init(&txq->dp.dpq, port_id, queue_id, pci_addr);
827
828         rc = ENOMEM;
829         txq->pend_desc = rte_calloc_socket("sfc-efx-txq-pend-desc",
830                                            EFX_TXQ_LIMIT(info->txq_entries),
831                                            sizeof(*txq->pend_desc), 0,
832                                            socket_id);
833         if (txq->pend_desc == NULL)
834                 goto fail_pend_desc_alloc;
835
836         rc = ENOMEM;
837         txq->sw_ring = rte_calloc_socket("sfc-efx-txq-sw_ring",
838                                          info->txq_entries,
839                                          sizeof(*txq->sw_ring),
840                                          RTE_CACHE_LINE_SIZE, socket_id);
841         if (txq->sw_ring == NULL)
842                 goto fail_sw_ring_alloc;
843
844         ctrl_txq = sfc_txq_by_dp_txq(&txq->dp);
845         if (ctrl_txq->evq->sa->tso) {
846                 rc = sfc_efx_tso_alloc_tsoh_objs(txq->sw_ring,
847                                                  info->txq_entries, socket_id);
848                 if (rc != 0)
849                         goto fail_alloc_tsoh_objs;
850         }
851
852         txq->evq = ctrl_txq->evq;
853         txq->ptr_mask = info->txq_entries - 1;
854         txq->free_thresh = info->free_thresh;
855         txq->dma_desc_size_max = info->dma_desc_size_max;
856
857         *dp_txqp = &txq->dp;
858         return 0;
859
860 fail_alloc_tsoh_objs:
861         rte_free(txq->sw_ring);
862
863 fail_sw_ring_alloc:
864         rte_free(txq->pend_desc);
865
866 fail_pend_desc_alloc:
867         rte_free(txq);
868
869 fail_txq_alloc:
870         return rc;
871 }
872
873 static sfc_dp_tx_qdestroy_t sfc_efx_tx_qdestroy;
874 static void
875 sfc_efx_tx_qdestroy(struct sfc_dp_txq *dp_txq)
876 {
877         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
878
879         sfc_efx_tso_free_tsoh_objs(txq->sw_ring, txq->ptr_mask + 1);
880         rte_free(txq->sw_ring);
881         rte_free(txq->pend_desc);
882         rte_free(txq);
883 }
884
885 static sfc_dp_tx_qstart_t sfc_efx_tx_qstart;
886 static int
887 sfc_efx_tx_qstart(struct sfc_dp_txq *dp_txq,
888                   __rte_unused unsigned int evq_read_ptr,
889                   unsigned int txq_desc_index)
890 {
891         /* libefx-based datapath is specific to libefx-based PMD */
892         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
893         struct sfc_txq *ctrl_txq = sfc_txq_by_dp_txq(dp_txq);
894
895         txq->common = ctrl_txq->common;
896
897         txq->pending = txq->completed = txq->added = txq_desc_index;
898         txq->hw_vlan_tci = 0;
899
900         txq->flags |= (SFC_EFX_TXQ_FLAG_STARTED | SFC_EFX_TXQ_FLAG_RUNNING);
901
902         return 0;
903 }
904
905 static sfc_dp_tx_qstop_t sfc_efx_tx_qstop;
906 static void
907 sfc_efx_tx_qstop(struct sfc_dp_txq *dp_txq,
908                  __rte_unused unsigned int *evq_read_ptr)
909 {
910         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
911
912         txq->flags &= ~SFC_EFX_TXQ_FLAG_RUNNING;
913 }
914
915 static sfc_dp_tx_qreap_t sfc_efx_tx_qreap;
916 static void
917 sfc_efx_tx_qreap(struct sfc_dp_txq *dp_txq)
918 {
919         struct sfc_efx_txq *txq = sfc_efx_txq_by_dp_txq(dp_txq);
920         unsigned int txds;
921
922         sfc_efx_tx_reap(txq);
923
924         for (txds = 0; txds <= txq->ptr_mask; txds++) {
925                 if (txq->sw_ring[txds].mbuf != NULL) {
926                         rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
927                         txq->sw_ring[txds].mbuf = NULL;
928                 }
929         }
930
931         txq->flags &= ~SFC_EFX_TXQ_FLAG_STARTED;
932 }
933
934 struct sfc_dp_tx sfc_efx_tx = {
935         .dp = {
936                 .name           = SFC_KVARG_DATAPATH_EFX,
937                 .type           = SFC_DP_TX,
938                 .hw_fw_caps     = 0,
939         },
940         .features               = SFC_DP_TX_FEAT_VLAN_INSERT |
941                                   SFC_DP_TX_FEAT_TSO,
942         .qcreate                = sfc_efx_tx_qcreate,
943         .qdestroy               = sfc_efx_tx_qdestroy,
944         .qstart                 = sfc_efx_tx_qstart,
945         .qstop                  = sfc_efx_tx_qstop,
946         .qreap                  = sfc_efx_tx_qreap,
947         .pkt_burst              = sfc_efx_xmit_pkts,
948 };