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