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