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