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