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