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