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