net/sfc: send bursts of packets
[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,
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 != 0) {
69                 sfc_err(sa,
70                         "setting explicit TX free threshold is not supported");
71                 rc = EINVAL;
72         }
73
74         if (tx_conf->tx_deferred_start != 0) {
75                 sfc_err(sa, "TX queue deferred start is not supported (yet)");
76                 rc = EINVAL;
77         }
78
79         if (tx_conf->tx_thresh.pthresh != 0 ||
80             tx_conf->tx_thresh.hthresh != 0 ||
81             tx_conf->tx_thresh.wthresh != 0) {
82                 sfc_err(sa,
83                         "prefetch/host/writeback thresholds are not supported");
84                 rc = EINVAL;
85         }
86
87         if ((flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
88                 sfc_err(sa, "VLAN offload is not supported");
89                 rc = EINVAL;
90         }
91
92         if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
93                 sfc_err(sa, "SCTP offload is not supported");
94                 rc = EINVAL;
95         }
96
97         /* We either perform both TCP and UDP offload, or no offload at all */
98         if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
99             ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
100                 sfc_err(sa, "TCP and UDP offloads can't be set independently");
101                 rc = EINVAL;
102         }
103
104         return rc;
105 }
106
107 void
108 sfc_tx_qflush_done(struct sfc_txq *txq)
109 {
110         txq->state |= SFC_TXQ_FLUSHED;
111         txq->state &= ~SFC_TXQ_FLUSHING;
112 }
113
114 static void
115 sfc_tx_reap(struct sfc_txq *txq)
116 {
117         unsigned int    completed;
118
119
120         sfc_ev_qpoll(txq->evq);
121
122         for (completed = txq->completed;
123              completed != txq->pending; completed++) {
124                 struct sfc_tx_sw_desc *txd;
125
126                 txd = &txq->sw_ring[completed & txq->ptr_mask];
127
128                 if (txd->mbuf != NULL) {
129                         rte_pktmbuf_free(txd->mbuf);
130                         txd->mbuf = NULL;
131                 }
132         }
133
134         txq->completed = completed;
135 }
136
137 int
138 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
139              uint16_t nb_tx_desc, unsigned int socket_id,
140              const struct rte_eth_txconf *tx_conf)
141 {
142         struct sfc_txq_info *txq_info;
143         struct sfc_evq *evq;
144         struct sfc_txq *txq;
145         unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
146         int rc = 0;
147
148         sfc_log_init(sa, "TxQ = %u", sw_index);
149
150         rc = sfc_tx_qcheck_conf(sa, tx_conf);
151         if (rc != 0)
152                 goto fail_bad_conf;
153
154         SFC_ASSERT(sw_index < sa->txq_count);
155         txq_info = &sa->txq_info[sw_index];
156
157         SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
158         txq_info->entries = nb_tx_desc;
159
160         rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
161         if (rc != 0)
162                 goto fail_ev_qinit;
163
164         evq = sa->evq_info[evq_index].evq;
165
166         rc = ENOMEM;
167         txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
168         if (txq == NULL)
169                 goto fail_txq_alloc;
170
171         rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
172                            socket_id, &txq->mem);
173         if (rc != 0)
174                 goto fail_dma_alloc;
175
176         rc = ENOMEM;
177         txq->pend_desc = rte_calloc_socket("sfc-txq-pend-desc",
178                                            EFX_TXQ_LIMIT(txq_info->entries),
179                                            sizeof(efx_desc_t), 0, socket_id);
180         if (txq->pend_desc == NULL)
181                 goto fail_pend_desc_alloc;
182
183         rc = ENOMEM;
184         txq->sw_ring = rte_calloc_socket("sfc-txq-desc", txq_info->entries,
185                                          sizeof(*txq->sw_ring), 0, socket_id);
186         if (txq->sw_ring == NULL)
187                 goto fail_desc_alloc;
188
189         txq->state = SFC_TXQ_INITIALIZED;
190         txq->ptr_mask = txq_info->entries - 1;
191         txq->hw_index = sw_index;
192         txq->flags = tx_conf->txq_flags;
193         txq->evq = evq;
194
195         evq->txq = txq;
196
197         txq_info->txq = txq;
198
199         return 0;
200
201 fail_desc_alloc:
202         rte_free(txq->pend_desc);
203
204 fail_pend_desc_alloc:
205         sfc_dma_free(sa, &txq->mem);
206
207 fail_dma_alloc:
208         rte_free(txq);
209
210 fail_txq_alloc:
211         sfc_ev_qfini(sa, evq_index);
212
213 fail_ev_qinit:
214         txq_info->entries = 0;
215
216 fail_bad_conf:
217         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
218         return rc;
219 }
220
221 void
222 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
223 {
224         struct sfc_txq_info *txq_info;
225         struct sfc_txq *txq;
226
227         sfc_log_init(sa, "TxQ = %u", sw_index);
228
229         SFC_ASSERT(sw_index < sa->txq_count);
230         txq_info = &sa->txq_info[sw_index];
231
232         txq = txq_info->txq;
233         SFC_ASSERT(txq != NULL);
234         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
235
236         txq_info->txq = NULL;
237         txq_info->entries = 0;
238
239         rte_free(txq->sw_ring);
240         rte_free(txq->pend_desc);
241         sfc_dma_free(sa, &txq->mem);
242         rte_free(txq);
243 }
244
245 static int
246 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
247 {
248         sfc_log_init(sa, "TxQ = %u", sw_index);
249
250         return 0;
251 }
252
253 static int
254 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
255 {
256         int rc = 0;
257
258         switch (txmode->mq_mode) {
259         case ETH_MQ_TX_NONE:
260                 break;
261         default:
262                 sfc_err(sa, "Tx multi-queue mode %u not supported",
263                         txmode->mq_mode);
264                 rc = EINVAL;
265         }
266
267         /*
268          * These features are claimed to be i40e-specific,
269          * but it does make sense to double-check their absence
270          */
271         if (txmode->hw_vlan_reject_tagged) {
272                 sfc_err(sa, "Rejecting tagged packets not supported");
273                 rc = EINVAL;
274         }
275
276         if (txmode->hw_vlan_reject_untagged) {
277                 sfc_err(sa, "Rejecting untagged packets not supported");
278                 rc = EINVAL;
279         }
280
281         if (txmode->hw_vlan_insert_pvid) {
282                 sfc_err(sa, "Port-based VLAN insertion not supported");
283                 rc = EINVAL;
284         }
285
286         return rc;
287 }
288
289 int
290 sfc_tx_init(struct sfc_adapter *sa)
291 {
292         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
293         unsigned int sw_index;
294         int rc = 0;
295
296         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
297         if (rc != 0)
298                 goto fail_check_mode;
299
300         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
301
302         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
303                                          sizeof(sa->txq_info[0]), 0,
304                                          sa->socket_id);
305         if (sa->txq_info == NULL)
306                 goto fail_txqs_alloc;
307
308         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
309                 rc = sfc_tx_qinit_info(sa, sw_index);
310                 if (rc != 0)
311                         goto fail_tx_qinit_info;
312         }
313
314         return 0;
315
316 fail_tx_qinit_info:
317         rte_free(sa->txq_info);
318         sa->txq_info = NULL;
319
320 fail_txqs_alloc:
321         sa->txq_count = 0;
322
323 fail_check_mode:
324         sfc_log_init(sa, "failed (rc = %d)", rc);
325         return rc;
326 }
327
328 void
329 sfc_tx_fini(struct sfc_adapter *sa)
330 {
331         int sw_index;
332
333         sw_index = sa->txq_count;
334         while (--sw_index >= 0) {
335                 if (sa->txq_info[sw_index].txq != NULL)
336                         sfc_tx_qfini(sa, sw_index);
337         }
338
339         rte_free(sa->txq_info);
340         sa->txq_info = NULL;
341         sa->txq_count = 0;
342 }
343
344 int
345 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
346 {
347         struct rte_eth_dev_data *dev_data;
348         struct sfc_txq_info *txq_info;
349         struct sfc_txq *txq;
350         struct sfc_evq *evq;
351         uint16_t flags;
352         unsigned int desc_index;
353         int rc = 0;
354
355         sfc_log_init(sa, "TxQ = %u", sw_index);
356
357         SFC_ASSERT(sw_index < sa->txq_count);
358         txq_info = &sa->txq_info[sw_index];
359
360         txq = txq_info->txq;
361
362         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
363
364         evq = txq->evq;
365
366         rc = sfc_ev_qstart(sa, evq->evq_index);
367         if (rc != 0)
368                 goto fail_ev_qstart;
369
370         /*
371          * It seems that DPDK has no controls regarding IPv4 offloads,
372          * hence, we always enable it here
373          */
374         if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
375             (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP))
376                 flags = EFX_TXQ_CKSUM_IPV4;
377         else
378                 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
379
380         rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
381                             txq_info->entries, 0 /* not used on EF10 */,
382                             flags, evq->common,
383                             &txq->common, &desc_index);
384         if (rc != 0)
385                 goto fail_tx_qcreate;
386
387         txq->added = txq->pending = txq->completed = desc_index;
388
389         efx_tx_qenable(txq->common);
390
391         txq->state |= (SFC_TXQ_STARTED | SFC_TXQ_RUNNING);
392
393         /*
394          * It seems to be used by DPDK for debug purposes only ('rte_ether')
395          */
396         dev_data = sa->eth_dev->data;
397         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
398
399         return 0;
400
401 fail_tx_qcreate:
402         sfc_ev_qstop(sa, evq->evq_index);
403
404 fail_ev_qstart:
405         return rc;
406 }
407
408 void
409 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
410 {
411         struct rte_eth_dev_data *dev_data;
412         struct sfc_txq_info *txq_info;
413         struct sfc_txq *txq;
414         unsigned int retry_count;
415         unsigned int wait_count;
416         unsigned int txds;
417
418         sfc_log_init(sa, "TxQ = %u", sw_index);
419
420         SFC_ASSERT(sw_index < sa->txq_count);
421         txq_info = &sa->txq_info[sw_index];
422
423         txq = txq_info->txq;
424
425         SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
426
427         txq->state &= ~SFC_TXQ_RUNNING;
428
429         /*
430          * Retry TX queue flushing in case of flush failed or
431          * timeout; in the worst case it can delay for 6 seconds
432          */
433         for (retry_count = 0;
434              ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
435              (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
436              ++retry_count) {
437                 if (efx_tx_qflush(txq->common) != 0) {
438                         txq->state |= SFC_TXQ_FLUSHING;
439                         break;
440                 }
441
442                 /*
443                  * Wait for TX queue flush done or flush failed event at least
444                  * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
445                  * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
446                  * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
447                  */
448                 wait_count = 0;
449                 do {
450                         rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
451                         sfc_ev_qpoll(txq->evq);
452                 } while ((txq->state & SFC_TXQ_FLUSHING) &&
453                          wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
454
455                 if (txq->state & SFC_TXQ_FLUSHING)
456                         sfc_err(sa, "TxQ %u flush timed out", sw_index);
457
458                 if (txq->state & SFC_TXQ_FLUSHED)
459                         sfc_info(sa, "TxQ %u flushed", sw_index);
460         }
461
462         sfc_tx_reap(txq);
463
464         for (txds = 0; txds < txq_info->entries; txds++) {
465                 if (txq->sw_ring[txds].mbuf != NULL) {
466                         rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
467                         txq->sw_ring[txds].mbuf = NULL;
468                 }
469         }
470
471         txq->state = SFC_TXQ_INITIALIZED;
472
473         efx_tx_qdestroy(txq->common);
474
475         sfc_ev_qstop(sa, txq->evq->evq_index);
476
477         /*
478          * It seems to be used by DPDK for debug purposes only ('rte_ether')
479          */
480         dev_data = sa->eth_dev->data;
481         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
482 }
483
484 int
485 sfc_tx_start(struct sfc_adapter *sa)
486 {
487         unsigned int sw_index;
488         int rc = 0;
489
490         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
491
492         rc = efx_tx_init(sa->nic);
493         if (rc != 0)
494                 goto fail_efx_tx_init;
495
496         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
497                 rc = sfc_tx_qstart(sa, sw_index);
498                 if (rc != 0)
499                         goto fail_tx_qstart;
500         }
501
502         return 0;
503
504 fail_tx_qstart:
505         while (sw_index-- > 0)
506                 sfc_tx_qstop(sa, sw_index);
507
508         efx_tx_fini(sa->nic);
509
510 fail_efx_tx_init:
511         sfc_log_init(sa, "failed (rc = %d)", rc);
512         return rc;
513 }
514
515 void
516 sfc_tx_stop(struct sfc_adapter *sa)
517 {
518         unsigned int sw_index;
519
520         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
521
522         sw_index = sa->txq_count;
523         while (sw_index-- > 0) {
524                 if (sa->txq_info[sw_index].txq != NULL)
525                         sfc_tx_qstop(sa, sw_index);
526         }
527
528         efx_tx_fini(sa->nic);
529 }
530
531 uint16_t
532 sfc_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
533 {
534         struct sfc_txq *txq = (struct sfc_txq *)tx_queue;
535         unsigned int added = txq->added;
536         unsigned int pushed = added;
537         unsigned int pkts_sent = 0;
538         efx_desc_t *pend = &txq->pend_desc[0];
539         const unsigned int hard_max_fill = EFX_TXQ_LIMIT(txq->ptr_mask + 1);
540         const unsigned int soft_max_fill = hard_max_fill -
541                                            SFC_TX_MAX_PKT_DESC;
542         unsigned int fill_level = added - txq->completed;
543         boolean_t reap_done;
544         int rc __rte_unused;
545         struct rte_mbuf **pktp;
546
547         if (unlikely((txq->state & SFC_TXQ_RUNNING) == 0))
548                 goto done;
549
550         /*
551          * If insufficient space for a single packet is present,
552          * we should reap; otherwise, we shouldn't do that all the time
553          * to avoid latency increase
554          */
555         reap_done = (fill_level > soft_max_fill);
556
557         if (reap_done) {
558                 sfc_tx_reap(txq);
559                 /*
560                  * Recalculate fill level since 'txq->completed'
561                  * might have changed on reap
562                  */
563                 fill_level = added - txq->completed;
564         }
565
566         for (pkts_sent = 0, pktp = &tx_pkts[0];
567              (pkts_sent < nb_pkts) && (fill_level <= soft_max_fill);
568              pkts_sent++, pktp++) {
569                 struct rte_mbuf         *m_seg = *pktp;
570                 size_t                  pkt_len = m_seg->pkt_len;
571                 unsigned int            pkt_descs = 0;
572
573                 for (; m_seg != NULL; m_seg = m_seg->next) {
574                         efsys_dma_addr_t        next_frag;
575                         size_t                  seg_len;
576
577                         seg_len = m_seg->data_len;
578                         next_frag = rte_mbuf_data_dma_addr(m_seg);
579
580                         do {
581                                 efsys_dma_addr_t        frag_addr = next_frag;
582                                 size_t                  frag_len;
583
584                                 next_frag = RTE_ALIGN(frag_addr + 1,
585                                                       SFC_TX_SEG_BOUNDARY);
586                                 frag_len = MIN(next_frag - frag_addr, seg_len);
587                                 seg_len -= frag_len;
588                                 pkt_len -= frag_len;
589
590                                 efx_tx_qdesc_dma_create(txq->common,
591                                                         frag_addr, frag_len,
592                                                         (pkt_len == 0),
593                                                         pend++);
594
595                                 pkt_descs++;
596                         } while (seg_len != 0);
597                 }
598
599                 added += pkt_descs;
600
601                 fill_level += pkt_descs;
602                 if (unlikely(fill_level > hard_max_fill)) {
603                         /*
604                          * Our estimation for maximum number of descriptors
605                          * required to send a packet seems to be wrong.
606                          * Try to reap (if we haven't yet).
607                          */
608                         if (!reap_done) {
609                                 sfc_tx_reap(txq);
610                                 reap_done = B_TRUE;
611                                 fill_level = added - txq->completed;
612                                 if (fill_level > hard_max_fill) {
613                                         pend -= pkt_descs;
614                                         break;
615                                 }
616                         } else {
617                                 pend -= pkt_descs;
618                                 break;
619                         }
620                 }
621
622                 /* Assign mbuf to the last used desc */
623                 txq->sw_ring[(added - 1) & txq->ptr_mask].mbuf = *pktp;
624         }
625
626         if (likely(pkts_sent > 0)) {
627                 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc,
628                                        pend - &txq->pend_desc[0],
629                                        txq->completed, &txq->added);
630                 SFC_ASSERT(rc == 0);
631
632                 if (likely(pushed != txq->added))
633                         efx_tx_qpush(txq->common, txq->added, pushed);
634         }
635
636 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE
637         if (!reap_done)
638                 sfc_tx_reap(txq);
639 #endif
640
641 done:
642         return pkts_sent;
643 }