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