net/sfc: implement transmit path start / stop
[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
36 /*
37  * Maximum number of TX queue flush attempts in case of
38  * failure or flush timeout
39  */
40 #define SFC_TX_QFLUSH_ATTEMPTS          (3)
41
42 /*
43  * Time to wait between event queue polling attempts when waiting for TX
44  * queue flush done or flush failed events
45  */
46 #define SFC_TX_QFLUSH_POLL_WAIT_MS      (1)
47
48 /*
49  * Maximum number of event queue polling attempts when waiting for TX queue
50  * flush done or flush failed events; it defines TX queue flush attempt timeout
51  * together with SFC_TX_QFLUSH_POLL_WAIT_MS
52  */
53 #define SFC_TX_QFLUSH_POLL_ATTEMPTS     (2000)
54
55 static int
56 sfc_tx_qcheck_conf(struct sfc_adapter *sa,
57                    const struct rte_eth_txconf *tx_conf)
58 {
59         unsigned int flags = tx_conf->txq_flags;
60         int rc = 0;
61
62         if (tx_conf->tx_rs_thresh != 0) {
63                 sfc_err(sa, "RS bit in transmit descriptor is not supported");
64                 rc = EINVAL;
65         }
66
67         if (tx_conf->tx_free_thresh != 0) {
68                 sfc_err(sa,
69                         "setting explicit TX free threshold is not supported");
70                 rc = EINVAL;
71         }
72
73         if (tx_conf->tx_deferred_start != 0) {
74                 sfc_err(sa, "TX queue deferred start is not supported (yet)");
75                 rc = EINVAL;
76         }
77
78         if (tx_conf->tx_thresh.pthresh != 0 ||
79             tx_conf->tx_thresh.hthresh != 0 ||
80             tx_conf->tx_thresh.wthresh != 0) {
81                 sfc_err(sa,
82                         "prefetch/host/writeback thresholds are not supported");
83                 rc = EINVAL;
84         }
85
86         if ((flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
87                 sfc_err(sa, "VLAN offload is not supported");
88                 rc = EINVAL;
89         }
90
91         if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
92                 sfc_err(sa, "SCTP offload is not supported");
93                 rc = EINVAL;
94         }
95
96         /* We either perform both TCP and UDP offload, or no offload at all */
97         if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
98             ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
99                 sfc_err(sa, "TCP and UDP offloads can't be set independently");
100                 rc = EINVAL;
101         }
102
103         return rc;
104 }
105
106 void
107 sfc_tx_qflush_done(struct sfc_txq *txq)
108 {
109         txq->state |= SFC_TXQ_FLUSHED;
110         txq->state &= ~SFC_TXQ_FLUSHING;
111 }
112
113 static void
114 sfc_tx_reap(struct sfc_txq *txq)
115 {
116         unsigned int    completed;
117
118
119         sfc_ev_qpoll(txq->evq);
120
121         for (completed = txq->completed;
122              completed != txq->pending; completed++) {
123                 struct sfc_tx_sw_desc *txd;
124
125                 txd = &txq->sw_ring[completed & txq->ptr_mask];
126
127                 if (txd->mbuf != NULL) {
128                         rte_pktmbuf_free(txd->mbuf);
129                         txd->mbuf = NULL;
130                 }
131         }
132
133         txq->completed = completed;
134 }
135
136 int
137 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
138              uint16_t nb_tx_desc, unsigned int socket_id,
139              const struct rte_eth_txconf *tx_conf)
140 {
141         struct sfc_txq_info *txq_info;
142         struct sfc_evq *evq;
143         struct sfc_txq *txq;
144         unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
145         int rc = 0;
146
147         sfc_log_init(sa, "TxQ = %u", sw_index);
148
149         rc = sfc_tx_qcheck_conf(sa, tx_conf);
150         if (rc != 0)
151                 goto fail_bad_conf;
152
153         SFC_ASSERT(sw_index < sa->txq_count);
154         txq_info = &sa->txq_info[sw_index];
155
156         SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
157         txq_info->entries = nb_tx_desc;
158
159         rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
160         if (rc != 0)
161                 goto fail_ev_qinit;
162
163         evq = sa->evq_info[evq_index].evq;
164
165         rc = ENOMEM;
166         txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
167         if (txq == NULL)
168                 goto fail_txq_alloc;
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         rc = ENOMEM;
176         txq->pend_desc = rte_calloc_socket("sfc-txq-pend-desc",
177                                            EFX_TXQ_LIMIT(txq_info->entries),
178                                            sizeof(efx_desc_t), 0, socket_id);
179         if (txq->pend_desc == NULL)
180                 goto fail_pend_desc_alloc;
181
182         rc = ENOMEM;
183         txq->sw_ring = rte_calloc_socket("sfc-txq-desc", txq_info->entries,
184                                          sizeof(*txq->sw_ring), 0, socket_id);
185         if (txq->sw_ring == NULL)
186                 goto fail_desc_alloc;
187
188         txq->state = SFC_TXQ_INITIALIZED;
189         txq->ptr_mask = txq_info->entries - 1;
190         txq->hw_index = sw_index;
191         txq->flags = tx_conf->txq_flags;
192         txq->evq = evq;
193
194         evq->txq = txq;
195
196         txq_info->txq = txq;
197
198         return 0;
199
200 fail_desc_alloc:
201         rte_free(txq->pend_desc);
202
203 fail_pend_desc_alloc:
204         sfc_dma_free(sa, &txq->mem);
205
206 fail_dma_alloc:
207         rte_free(txq);
208
209 fail_txq_alloc:
210         sfc_ev_qfini(sa, evq_index);
211
212 fail_ev_qinit:
213         txq_info->entries = 0;
214
215 fail_bad_conf:
216         sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
217         return rc;
218 }
219
220 void
221 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
222 {
223         struct sfc_txq_info *txq_info;
224         struct sfc_txq *txq;
225
226         sfc_log_init(sa, "TxQ = %u", sw_index);
227
228         SFC_ASSERT(sw_index < sa->txq_count);
229         txq_info = &sa->txq_info[sw_index];
230
231         txq = txq_info->txq;
232         SFC_ASSERT(txq != NULL);
233         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
234
235         txq_info->txq = NULL;
236         txq_info->entries = 0;
237
238         rte_free(txq->sw_ring);
239         rte_free(txq->pend_desc);
240         sfc_dma_free(sa, &txq->mem);
241         rte_free(txq);
242 }
243
244 static int
245 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
246 {
247         sfc_log_init(sa, "TxQ = %u", sw_index);
248
249         return 0;
250 }
251
252 static int
253 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
254 {
255         int rc = 0;
256
257         switch (txmode->mq_mode) {
258         case ETH_MQ_TX_NONE:
259                 break;
260         default:
261                 sfc_err(sa, "Tx multi-queue mode %u not supported",
262                         txmode->mq_mode);
263                 rc = EINVAL;
264         }
265
266         /*
267          * These features are claimed to be i40e-specific,
268          * but it does make sense to double-check their absence
269          */
270         if (txmode->hw_vlan_reject_tagged) {
271                 sfc_err(sa, "Rejecting tagged packets not supported");
272                 rc = EINVAL;
273         }
274
275         if (txmode->hw_vlan_reject_untagged) {
276                 sfc_err(sa, "Rejecting untagged packets not supported");
277                 rc = EINVAL;
278         }
279
280         if (txmode->hw_vlan_insert_pvid) {
281                 sfc_err(sa, "Port-based VLAN insertion not supported");
282                 rc = EINVAL;
283         }
284
285         return rc;
286 }
287
288 int
289 sfc_tx_init(struct sfc_adapter *sa)
290 {
291         const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
292         unsigned int sw_index;
293         int rc = 0;
294
295         rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
296         if (rc != 0)
297                 goto fail_check_mode;
298
299         sa->txq_count = sa->eth_dev->data->nb_tx_queues;
300
301         sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
302                                          sizeof(sa->txq_info[0]), 0,
303                                          sa->socket_id);
304         if (sa->txq_info == NULL)
305                 goto fail_txqs_alloc;
306
307         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
308                 rc = sfc_tx_qinit_info(sa, sw_index);
309                 if (rc != 0)
310                         goto fail_tx_qinit_info;
311         }
312
313         return 0;
314
315 fail_tx_qinit_info:
316         rte_free(sa->txq_info);
317         sa->txq_info = NULL;
318
319 fail_txqs_alloc:
320         sa->txq_count = 0;
321
322 fail_check_mode:
323         sfc_log_init(sa, "failed (rc = %d)", rc);
324         return rc;
325 }
326
327 void
328 sfc_tx_fini(struct sfc_adapter *sa)
329 {
330         int sw_index;
331
332         sw_index = sa->txq_count;
333         while (--sw_index >= 0) {
334                 if (sa->txq_info[sw_index].txq != NULL)
335                         sfc_tx_qfini(sa, sw_index);
336         }
337
338         rte_free(sa->txq_info);
339         sa->txq_info = NULL;
340         sa->txq_count = 0;
341 }
342
343 int
344 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
345 {
346         struct rte_eth_dev_data *dev_data;
347         struct sfc_txq_info *txq_info;
348         struct sfc_txq *txq;
349         struct sfc_evq *evq;
350         uint16_t flags;
351         unsigned int desc_index;
352         int rc = 0;
353
354         sfc_log_init(sa, "TxQ = %u", sw_index);
355
356         SFC_ASSERT(sw_index < sa->txq_count);
357         txq_info = &sa->txq_info[sw_index];
358
359         txq = txq_info->txq;
360
361         SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
362
363         evq = txq->evq;
364
365         rc = sfc_ev_qstart(sa, evq->evq_index);
366         if (rc != 0)
367                 goto fail_ev_qstart;
368
369         /*
370          * It seems that DPDK has no controls regarding IPv4 offloads,
371          * hence, we always enable it here
372          */
373         if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
374             (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP))
375                 flags = EFX_TXQ_CKSUM_IPV4;
376         else
377                 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
378
379         rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
380                             txq_info->entries, 0 /* not used on EF10 */,
381                             flags, evq->common,
382                             &txq->common, &desc_index);
383         if (rc != 0)
384                 goto fail_tx_qcreate;
385
386         txq->added = txq->pending = txq->completed = desc_index;
387
388         efx_tx_qenable(txq->common);
389
390         txq->state |= (SFC_TXQ_STARTED | SFC_TXQ_RUNNING);
391
392         /*
393          * It seems to be used by DPDK for debug purposes only ('rte_ether')
394          */
395         dev_data = sa->eth_dev->data;
396         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
397
398         return 0;
399
400 fail_tx_qcreate:
401         sfc_ev_qstop(sa, evq->evq_index);
402
403 fail_ev_qstart:
404         return rc;
405 }
406
407 void
408 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
409 {
410         struct rte_eth_dev_data *dev_data;
411         struct sfc_txq_info *txq_info;
412         struct sfc_txq *txq;
413         unsigned int retry_count;
414         unsigned int wait_count;
415         unsigned int txds;
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->state & SFC_TXQ_STARTED);
425
426         txq->state &= ~SFC_TXQ_RUNNING;
427
428         /*
429          * Retry TX queue flushing in case of flush failed or
430          * timeout; in the worst case it can delay for 6 seconds
431          */
432         for (retry_count = 0;
433              ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
434              (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
435              ++retry_count) {
436                 if (efx_tx_qflush(txq->common) != 0) {
437                         txq->state |= SFC_TXQ_FLUSHING;
438                         break;
439                 }
440
441                 /*
442                  * Wait for TX queue flush done or flush failed event at least
443                  * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
444                  * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
445                  * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
446                  */
447                 wait_count = 0;
448                 do {
449                         rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
450                         sfc_ev_qpoll(txq->evq);
451                 } while ((txq->state & SFC_TXQ_FLUSHING) &&
452                          wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
453
454                 if (txq->state & SFC_TXQ_FLUSHING)
455                         sfc_err(sa, "TxQ %u flush timed out", sw_index);
456
457                 if (txq->state & SFC_TXQ_FLUSHED)
458                         sfc_info(sa, "TxQ %u flushed", sw_index);
459         }
460
461         sfc_tx_reap(txq);
462
463         for (txds = 0; txds < txq_info->entries; txds++) {
464                 if (txq->sw_ring[txds].mbuf != NULL) {
465                         rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
466                         txq->sw_ring[txds].mbuf = NULL;
467                 }
468         }
469
470         txq->state = SFC_TXQ_INITIALIZED;
471
472         efx_tx_qdestroy(txq->common);
473
474         sfc_ev_qstop(sa, txq->evq->evq_index);
475
476         /*
477          * It seems to be used by DPDK for debug purposes only ('rte_ether')
478          */
479         dev_data = sa->eth_dev->data;
480         dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
481 }
482
483 int
484 sfc_tx_start(struct sfc_adapter *sa)
485 {
486         unsigned int sw_index;
487         int rc = 0;
488
489         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
490
491         rc = efx_tx_init(sa->nic);
492         if (rc != 0)
493                 goto fail_efx_tx_init;
494
495         for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
496                 rc = sfc_tx_qstart(sa, sw_index);
497                 if (rc != 0)
498                         goto fail_tx_qstart;
499         }
500
501         return 0;
502
503 fail_tx_qstart:
504         while (sw_index-- > 0)
505                 sfc_tx_qstop(sa, sw_index);
506
507         efx_tx_fini(sa->nic);
508
509 fail_efx_tx_init:
510         sfc_log_init(sa, "failed (rc = %d)", rc);
511         return rc;
512 }
513
514 void
515 sfc_tx_stop(struct sfc_adapter *sa)
516 {
517         unsigned int sw_index;
518
519         sfc_log_init(sa, "txq_count = %u", sa->txq_count);
520
521         sw_index = sa->txq_count;
522         while (sw_index-- > 0) {
523                 if (sa->txq_info[sw_index].txq != NULL)
524                         sfc_tx_qstop(sa, sw_index);
525         }
526
527         efx_tx_fini(sa->nic);
528 }