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