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