net/sfc: support Tx free threshold
[dpdk.git] / drivers / net / sfc / sfc_tx.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "sfc.h"
31 #include "sfc_debug.h"
32 #include "sfc_log.h"
33 #include "sfc_ev.h"
34 #include "sfc_tx.h"
35 #include "sfc_tweak.h"
36
37 /*
38  * Maximum number of TX queue flush attempts in case of
39  * failure or flush timeout
40  */
41 #define SFC_TX_QFLUSH_ATTEMPTS          (3)
42
43 /*
44  * Time to wait between event queue polling attempts when waiting for TX
45  * queue flush done or flush failed events
46  */
47 #define SFC_TX_QFLUSH_POLL_WAIT_MS      (1)
48
49 /*
50  * Maximum number of event queue polling attempts when waiting for TX queue
51  * flush done or flush failed events; it defines TX queue flush attempt timeout
52  * together with SFC_TX_QFLUSH_POLL_WAIT_MS
53  */
54 #define SFC_TX_QFLUSH_POLL_ATTEMPTS     (2000)
55
56 static int
57 sfc_tx_qcheck_conf(struct sfc_adapter *sa, uint16_t nb_tx_desc,
58                    const struct rte_eth_txconf *tx_conf)
59 {
60         unsigned int flags = tx_conf->txq_flags;
61         int rc = 0;
62
63         if (tx_conf->tx_rs_thresh != 0) {
64                 sfc_err(sa, "RS bit in transmit descriptor is not supported");
65                 rc = EINVAL;
66         }
67
68         if (tx_conf->tx_free_thresh > EFX_TXQ_LIMIT(nb_tx_desc)) {
69                 sfc_err(sa,
70                         "TxQ free threshold too large: %u vs maximum %u",
71                         tx_conf->tx_free_thresh, EFX_TXQ_LIMIT(nb_tx_desc));
72                 rc = EINVAL;
73         }
74
75         if (tx_conf->tx_deferred_start != 0) {
76                 sfc_err(sa, "TX queue deferred start is not supported (yet)");
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_err(sa,
84                         "prefetch/host/writeback thresholds are not supported");
85                 rc = EINVAL;
86         }
87
88         if ((flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
89                 sfc_err(sa, "VLAN offload is not supported");
90                 rc = EINVAL;
91         }
92
93         if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
94                 sfc_err(sa, "SCTP offload is not supported");
95                 rc = EINVAL;
96         }
97
98         /* We either perform both TCP and UDP offload, or no offload at all */
99         if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
100             ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
101                 sfc_err(sa, "TCP and UDP offloads can't be set independently");
102                 rc = EINVAL;
103         }
104
105         return rc;
106 }
107
108 void
109 sfc_tx_qflush_done(struct sfc_txq *txq)
110 {
111         txq->state |= SFC_TXQ_FLUSHED;
112         txq->state &= ~SFC_TXQ_FLUSHING;
113 }
114
115 static void
116 sfc_tx_reap(struct sfc_txq *txq)
117 {
118         unsigned int    completed;
119
120
121         sfc_ev_qpoll(txq->evq);
122
123         for (completed = txq->completed;
124              completed != txq->pending; completed++) {
125                 struct sfc_tx_sw_desc *txd;
126
127                 txd = &txq->sw_ring[completed & txq->ptr_mask];
128
129                 if (txd->mbuf != NULL) {
130                         rte_pktmbuf_free(txd->mbuf);
131                         txd->mbuf = NULL;
132                 }
133         }
134
135         txq->completed = completed;
136 }
137
138 int
139 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
140              uint16_t nb_tx_desc, unsigned int socket_id,
141              const struct rte_eth_txconf *tx_conf)
142 {
143         struct sfc_txq_info *txq_info;
144         struct sfc_evq *evq;
145         struct sfc_txq *txq;
146         unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
147         int rc = 0;
148
149         sfc_log_init(sa, "TxQ = %u", sw_index);
150
151         rc = sfc_tx_qcheck_conf(sa, nb_tx_desc, tx_conf);
152         if (rc != 0)
153                 goto fail_bad_conf;
154
155         SFC_ASSERT(sw_index < sa->txq_count);
156         txq_info = &sa->txq_info[sw_index];
157
158         SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
159         txq_info->entries = nb_tx_desc;
160
161         rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
162         if (rc != 0)
163                 goto fail_ev_qinit;
164
165         evq = sa->evq_info[evq_index].evq;
166
167         rc = ENOMEM;
168         txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
169         if (txq == NULL)
170                 goto fail_txq_alloc;
171
172         rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
173                            socket_id, &txq->mem);
174         if (rc != 0)
175                 goto fail_dma_alloc;
176
177         rc = ENOMEM;
178         txq->pend_desc = rte_calloc_socket("sfc-txq-pend-desc",
179                                            EFX_TXQ_LIMIT(txq_info->entries),
180                                            sizeof(efx_desc_t), 0, socket_id);
181         if (txq->pend_desc == NULL)
182                 goto fail_pend_desc_alloc;
183
184         rc = ENOMEM;
185         txq->sw_ring = rte_calloc_socket("sfc-txq-desc", txq_info->entries,
186                                          sizeof(*txq->sw_ring), 0, socket_id);
187         if (txq->sw_ring == NULL)
188                 goto fail_desc_alloc;
189
190         txq->state = SFC_TXQ_INITIALIZED;
191         txq->ptr_mask = txq_info->entries - 1;
192         txq->free_thresh = (tx_conf->tx_free_thresh) ? tx_conf->tx_free_thresh :
193                                                      SFC_TX_DEFAULT_FREE_THRESH;
194         txq->hw_index = sw_index;
195         txq->flags = tx_conf->txq_flags;
196         txq->evq = evq;
197
198         evq->txq = txq;
199
200         txq_info->txq = txq;
201
202         return 0;
203
204 fail_desc_alloc:
205         rte_free(txq->pend_desc);
206
207 fail_pend_desc_alloc:
208         sfc_dma_free(sa, &txq->mem);
209
210 fail_dma_alloc:
211         rte_free(txq);
212
213 fail_txq_alloc:
214         sfc_ev_qfini(sa, evq_index);
215
216 fail_ev_qinit:
217         txq_info->entries = 0;
218
219 fail_bad_conf:
220         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
221         return rc;
222 }
223
224 void
225 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
226 {
227         struct sfc_txq_info *txq_info;
228         struct sfc_txq *txq;
229
230         sfc_log_init(sa, "TxQ = %u", sw_index);
231
232         SFC_ASSERT(sw_index < sa->txq_count);
233         txq_info = &sa->txq_info[sw_index];
234
235         txq = txq_info->txq;
236         SFC_ASSERT(txq != NULL);
237         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
238
239         txq_info->txq = NULL;
240         txq_info->entries = 0;
241
242         rte_free(txq->sw_ring);
243         rte_free(txq->pend_desc);
244         sfc_dma_free(sa, &txq->mem);
245         rte_free(txq);
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 int
293 sfc_tx_init(struct sfc_adapter *sa)
294 {
295         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
296         unsigned int sw_index;
297         int rc = 0;
298
299         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
300         if (rc != 0)
301                 goto fail_check_mode;
302
303         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
304
305         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
306                                          sizeof(sa->txq_info[0]), 0,
307                                          sa->socket_id);
308         if (sa->txq_info == NULL)
309                 goto fail_txqs_alloc;
310
311         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
312                 rc = sfc_tx_qinit_info(sa, sw_index);
313                 if (rc != 0)
314                         goto fail_tx_qinit_info;
315         }
316
317         return 0;
318
319 fail_tx_qinit_info:
320         rte_free(sa->txq_info);
321         sa->txq_info = NULL;
322
323 fail_txqs_alloc:
324         sa->txq_count = 0;
325
326 fail_check_mode:
327         sfc_log_init(sa, "failed (rc = %d)", rc);
328         return rc;
329 }
330
331 void
332 sfc_tx_fini(struct sfc_adapter *sa)
333 {
334         int sw_index;
335
336         sw_index = sa->txq_count;
337         while (--sw_index >= 0) {
338                 if (sa->txq_info[sw_index].txq != NULL)
339                         sfc_tx_qfini(sa, sw_index);
340         }
341
342         rte_free(sa->txq_info);
343         sa->txq_info = NULL;
344         sa->txq_count = 0;
345 }
346
347 int
348 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
349 {
350         struct rte_eth_dev_data *dev_data;
351         struct sfc_txq_info *txq_info;
352         struct sfc_txq *txq;
353         struct sfc_evq *evq;
354         uint16_t flags;
355         unsigned int desc_index;
356         int rc = 0;
357
358         sfc_log_init(sa, "TxQ = %u", sw_index);
359
360         SFC_ASSERT(sw_index < sa->txq_count);
361         txq_info = &sa->txq_info[sw_index];
362
363         txq = txq_info->txq;
364
365         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
366
367         evq = txq->evq;
368
369         rc = sfc_ev_qstart(sa, evq->evq_index);
370         if (rc != 0)
371                 goto fail_ev_qstart;
372
373         /*
374          * It seems that DPDK has no controls regarding IPv4 offloads,
375          * hence, we always enable it here
376          */
377         if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
378             (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP))
379                 flags = EFX_TXQ_CKSUM_IPV4;
380         else
381                 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
382
383         rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
384                             txq_info->entries, 0 /* not used on EF10 */,
385                             flags, evq->common,
386                             &txq->common, &desc_index);
387         if (rc != 0)
388                 goto fail_tx_qcreate;
389
390         txq->added = txq->pending = txq->completed = desc_index;
391
392         efx_tx_qenable(txq->common);
393
394         txq->state |= (SFC_TXQ_STARTED | SFC_TXQ_RUNNING);
395
396         /*
397          * It seems to be used by DPDK for debug purposes only ('rte_ether')
398          */
399         dev_data = sa->eth_dev->data;
400         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
401
402         return 0;
403
404 fail_tx_qcreate:
405         sfc_ev_qstop(sa, evq->evq_index);
406
407 fail_ev_qstart:
408         return rc;
409 }
410
411 void
412 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
413 {
414         struct rte_eth_dev_data *dev_data;
415         struct sfc_txq_info *txq_info;
416         struct sfc_txq *txq;
417         unsigned int retry_count;
418         unsigned int wait_count;
419         unsigned int txds;
420
421         sfc_log_init(sa, "TxQ = %u", sw_index);
422
423         SFC_ASSERT(sw_index < sa->txq_count);
424         txq_info = &sa->txq_info[sw_index];
425
426         txq = txq_info->txq;
427
428         SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
429
430         txq->state &= ~SFC_TXQ_RUNNING;
431
432         /*
433          * Retry TX queue flushing in case of flush failed or
434          * timeout; in the worst case it can delay for 6 seconds
435          */
436         for (retry_count = 0;
437              ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
438              (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
439              ++retry_count) {
440                 if (efx_tx_qflush(txq->common) != 0) {
441                         txq->state |= SFC_TXQ_FLUSHING;
442                         break;
443                 }
444
445                 /*
446                  * Wait for TX queue flush done or flush failed event at least
447                  * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
448                  * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
449                  * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
450                  */
451                 wait_count = 0;
452                 do {
453                         rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
454                         sfc_ev_qpoll(txq->evq);
455                 } while ((txq->state & SFC_TXQ_FLUSHING) &&
456                          wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
457
458                 if (txq->state & SFC_TXQ_FLUSHING)
459                         sfc_err(sa, "TxQ %u flush timed out", sw_index);
460
461                 if (txq->state & SFC_TXQ_FLUSHED)
462                         sfc_info(sa, "TxQ %u flushed", sw_index);
463         }
464
465         sfc_tx_reap(txq);
466
467         for (txds = 0; txds < txq_info->entries; txds++) {
468                 if (txq->sw_ring[txds].mbuf != NULL) {
469                         rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
470                         txq->sw_ring[txds].mbuf = NULL;
471                 }
472         }
473
474         txq->state = SFC_TXQ_INITIALIZED;
475
476         efx_tx_qdestroy(txq->common);
477
478         sfc_ev_qstop(sa, txq->evq->evq_index);
479
480         /*
481          * It seems to be used by DPDK for debug purposes only ('rte_ether')
482          */
483         dev_data = sa->eth_dev->data;
484         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
485 }
486
487 int
488 sfc_tx_start(struct sfc_adapter *sa)
489 {
490         unsigned int sw_index;
491         int rc = 0;
492
493         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
494
495         rc = efx_tx_init(sa->nic);
496         if (rc != 0)
497                 goto fail_efx_tx_init;
498
499         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
500                 rc = sfc_tx_qstart(sa, sw_index);
501                 if (rc != 0)
502                         goto fail_tx_qstart;
503         }
504
505         return 0;
506
507 fail_tx_qstart:
508         while (sw_index-- > 0)
509                 sfc_tx_qstop(sa, sw_index);
510
511         efx_tx_fini(sa->nic);
512
513 fail_efx_tx_init:
514         sfc_log_init(sa, "failed (rc = %d)", rc);
515         return rc;
516 }
517
518 void
519 sfc_tx_stop(struct sfc_adapter *sa)
520 {
521         unsigned int sw_index;
522
523         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
524
525         sw_index = sa->txq_count;
526         while (sw_index-- > 0) {
527                 if (sa->txq_info[sw_index].txq != NULL)
528                         sfc_tx_qstop(sa, sw_index);
529         }
530
531         efx_tx_fini(sa->nic);
532 }
533
534 uint16_t
535 sfc_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
536 {
537         struct sfc_txq *txq = (struct sfc_txq *)tx_queue;
538         unsigned int added = txq->added;
539         unsigned int pushed = added;
540         unsigned int pkts_sent = 0;
541         efx_desc_t *pend = &txq->pend_desc[0];
542         const unsigned int hard_max_fill = EFX_TXQ_LIMIT(txq->ptr_mask + 1);
543         const unsigned int soft_max_fill = hard_max_fill - txq->free_thresh;
544         unsigned int fill_level = added - txq->completed;
545         boolean_t reap_done;
546         int rc __rte_unused;
547         struct rte_mbuf **pktp;
548
549         if (unlikely((txq->state & SFC_TXQ_RUNNING) == 0))
550                 goto done;
551
552         /*
553          * If insufficient space for a single packet is present,
554          * we should reap; otherwise, we shouldn't do that all the time
555          * to avoid latency increase
556          */
557         reap_done = (fill_level > soft_max_fill);
558
559         if (reap_done) {
560                 sfc_tx_reap(txq);
561                 /*
562                  * Recalculate fill level since 'txq->completed'
563                  * might have changed on reap
564                  */
565                 fill_level = added - txq->completed;
566         }
567
568         for (pkts_sent = 0, pktp = &tx_pkts[0];
569              (pkts_sent < nb_pkts) && (fill_level <= soft_max_fill);
570              pkts_sent++, pktp++) {
571                 struct rte_mbuf         *m_seg = *pktp;
572                 size_t                  pkt_len = m_seg->pkt_len;
573                 unsigned int            pkt_descs = 0;
574
575                 for (; m_seg != NULL; m_seg = m_seg->next) {
576                         efsys_dma_addr_t        next_frag;
577                         size_t                  seg_len;
578
579                         seg_len = m_seg->data_len;
580                         next_frag = rte_mbuf_data_dma_addr(m_seg);
581
582                         do {
583                                 efsys_dma_addr_t        frag_addr = next_frag;
584                                 size_t                  frag_len;
585
586                                 next_frag = RTE_ALIGN(frag_addr + 1,
587                                                       SFC_TX_SEG_BOUNDARY);
588                                 frag_len = MIN(next_frag - frag_addr, seg_len);
589                                 seg_len -= frag_len;
590                                 pkt_len -= frag_len;
591
592                                 efx_tx_qdesc_dma_create(txq->common,
593                                                         frag_addr, frag_len,
594                                                         (pkt_len == 0),
595                                                         pend++);
596
597                                 pkt_descs++;
598                         } while (seg_len != 0);
599                 }
600
601                 added += pkt_descs;
602
603                 fill_level += pkt_descs;
604                 if (unlikely(fill_level > hard_max_fill)) {
605                         /*
606                          * Our estimation for maximum number of descriptors
607                          * required to send a packet seems to be wrong.
608                          * Try to reap (if we haven't yet).
609                          */
610                         if (!reap_done) {
611                                 sfc_tx_reap(txq);
612                                 reap_done = B_TRUE;
613                                 fill_level = added - txq->completed;
614                                 if (fill_level > hard_max_fill) {
615                                         pend -= pkt_descs;
616                                         break;
617                                 }
618                         } else {
619                                 pend -= pkt_descs;
620                                 break;
621                         }
622                 }
623
624                 /* Assign mbuf to the last used desc */
625                 txq->sw_ring[(added - 1) & txq->ptr_mask].mbuf = *pktp;
626         }
627
628         if (likely(pkts_sent > 0)) {
629                 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc,
630                                        pend - &txq->pend_desc[0],
631                                        txq->completed, &txq->added);
632                 SFC_ASSERT(rc == 0);
633
634                 if (likely(pushed != txq->added))
635                         efx_tx_qpush(txq->common, txq->added, pushed);
636         }
637
638 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
639         if (!reap_done)
640                 sfc_tx_reap(txq);
641 #endif
642
643 done:
644         return pkts_sent;
645 }