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