2 * Copyright (c) 2016 Solarflare Communications Inc.
5 * This software was jointly developed between OKTET Labs (under contract
6 * for Solarflare) and Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #include "sfc_debug.h"
35 #include "sfc_tweak.h"
38 * Maximum number of TX queue flush attempts in case of
39 * failure or flush timeout
41 #define SFC_TX_QFLUSH_ATTEMPTS (3)
44 * Time to wait between event queue polling attempts when waiting for TX
45 * queue flush done or flush failed events
47 #define SFC_TX_QFLUSH_POLL_WAIT_MS (1)
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
54 #define SFC_TX_QFLUSH_POLL_ATTEMPTS (2000)
57 sfc_tx_qcheck_conf(struct sfc_adapter *sa, uint16_t nb_tx_desc,
58 const struct rte_eth_txconf *tx_conf)
60 unsigned int flags = tx_conf->txq_flags;
61 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
64 if (tx_conf->tx_rs_thresh != 0) {
65 sfc_err(sa, "RS bit in transmit descriptor is not supported");
69 if (tx_conf->tx_free_thresh > EFX_TXQ_LIMIT(nb_tx_desc)) {
71 "TxQ free threshold too large: %u vs maximum %u",
72 tx_conf->tx_free_thresh, EFX_TXQ_LIMIT(nb_tx_desc));
76 if (tx_conf->tx_thresh.pthresh != 0 ||
77 tx_conf->tx_thresh.hthresh != 0 ||
78 tx_conf->tx_thresh.wthresh != 0) {
80 "prefetch/host/writeback thresholds are not supported");
84 if (!encp->enc_hw_tx_insert_vlan_enabled &&
85 (flags & ETH_TXQ_FLAGS_NOVLANOFFL) == 0) {
86 sfc_err(sa, "VLAN offload is not supported");
90 if ((flags & ETH_TXQ_FLAGS_NOXSUMSCTP) == 0) {
91 sfc_err(sa, "SCTP offload is not supported");
95 /* We either perform both TCP and UDP offload, or no offload at all */
96 if (((flags & ETH_TXQ_FLAGS_NOXSUMTCP) == 0) !=
97 ((flags & ETH_TXQ_FLAGS_NOXSUMUDP) == 0)) {
98 sfc_err(sa, "TCP and UDP offloads can't be set independently");
106 sfc_tx_qflush_done(struct sfc_txq *txq)
108 txq->state |= SFC_TXQ_FLUSHED;
109 txq->state &= ~SFC_TXQ_FLUSHING;
113 sfc_tx_reap(struct sfc_txq *txq)
115 unsigned int completed;
118 sfc_ev_qpoll(txq->evq);
120 for (completed = txq->completed;
121 completed != txq->pending; completed++) {
122 struct sfc_tx_sw_desc *txd;
124 txd = &txq->sw_ring[completed & txq->ptr_mask];
126 if (txd->mbuf != NULL) {
127 rte_pktmbuf_free(txd->mbuf);
132 txq->completed = completed;
136 sfc_tx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
137 uint16_t nb_tx_desc, unsigned int socket_id,
138 const struct rte_eth_txconf *tx_conf)
140 struct sfc_txq_info *txq_info;
143 unsigned int evq_index = sfc_evq_index_by_txq_sw_index(sa, sw_index);
146 sfc_log_init(sa, "TxQ = %u", sw_index);
148 rc = sfc_tx_qcheck_conf(sa, nb_tx_desc, tx_conf);
152 SFC_ASSERT(sw_index < sa->txq_count);
153 txq_info = &sa->txq_info[sw_index];
155 SFC_ASSERT(nb_tx_desc <= sa->txq_max_entries);
156 txq_info->entries = nb_tx_desc;
158 rc = sfc_ev_qinit(sa, evq_index, txq_info->entries, socket_id);
162 evq = sa->evq_info[evq_index].evq;
165 txq = rte_zmalloc_socket("sfc-txq", sizeof(*txq), 0, socket_id);
169 rc = sfc_dma_alloc(sa, "txq", sw_index, EFX_TXQ_SIZE(txq_info->entries),
170 socket_id, &txq->mem);
175 txq->pend_desc = rte_calloc_socket("sfc-txq-pend-desc",
176 EFX_TXQ_LIMIT(txq_info->entries),
177 sizeof(efx_desc_t), 0, socket_id);
178 if (txq->pend_desc == NULL)
179 goto fail_pend_desc_alloc;
182 txq->sw_ring = rte_calloc_socket("sfc-txq-desc", txq_info->entries,
183 sizeof(*txq->sw_ring), 0, socket_id);
184 if (txq->sw_ring == NULL)
185 goto fail_desc_alloc;
188 rc = sfc_tso_alloc_tsoh_objs(txq->sw_ring, txq_info->entries,
191 goto fail_alloc_tsoh_objs;
194 txq->state = SFC_TXQ_INITIALIZED;
195 txq->ptr_mask = txq_info->entries - 1;
196 txq->free_thresh = (tx_conf->tx_free_thresh) ? tx_conf->tx_free_thresh :
197 SFC_TX_DEFAULT_FREE_THRESH;
198 txq->hw_index = sw_index;
199 txq->flags = tx_conf->txq_flags;
205 txq_info->deferred_start = (tx_conf->tx_deferred_start != 0);
209 fail_alloc_tsoh_objs:
210 rte_free(txq->sw_ring);
213 rte_free(txq->pend_desc);
215 fail_pend_desc_alloc:
216 sfc_dma_free(sa, &txq->mem);
222 sfc_ev_qfini(sa, evq_index);
225 txq_info->entries = 0;
228 sfc_log_init(sa, "failed (TxQ = %u, rc = %d)", sw_index, rc);
233 sfc_tx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
235 struct sfc_txq_info *txq_info;
238 sfc_log_init(sa, "TxQ = %u", sw_index);
240 SFC_ASSERT(sw_index < sa->txq_count);
241 txq_info = &sa->txq_info[sw_index];
244 SFC_ASSERT(txq != NULL);
245 SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
247 sfc_tso_free_tsoh_objs(txq->sw_ring, txq_info->entries);
249 txq_info->txq = NULL;
250 txq_info->entries = 0;
252 rte_free(txq->sw_ring);
253 rte_free(txq->pend_desc);
254 sfc_dma_free(sa, &txq->mem);
259 sfc_tx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
261 sfc_log_init(sa, "TxQ = %u", sw_index);
267 sfc_tx_check_mode(struct sfc_adapter *sa, const struct rte_eth_txmode *txmode)
271 switch (txmode->mq_mode) {
275 sfc_err(sa, "Tx multi-queue mode %u not supported",
281 * These features are claimed to be i40e-specific,
282 * but it does make sense to double-check their absence
284 if (txmode->hw_vlan_reject_tagged) {
285 sfc_err(sa, "Rejecting tagged packets not supported");
289 if (txmode->hw_vlan_reject_untagged) {
290 sfc_err(sa, "Rejecting untagged packets not supported");
294 if (txmode->hw_vlan_insert_pvid) {
295 sfc_err(sa, "Port-based VLAN insertion not supported");
303 sfc_tx_init(struct sfc_adapter *sa)
305 const struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
306 unsigned int sw_index;
309 rc = sfc_tx_check_mode(sa, &dev_conf->txmode);
311 goto fail_check_mode;
313 sa->txq_count = sa->eth_dev->data->nb_tx_queues;
315 sa->txq_info = rte_calloc_socket("sfc-txqs", sa->txq_count,
316 sizeof(sa->txq_info[0]), 0,
318 if (sa->txq_info == NULL)
319 goto fail_txqs_alloc;
321 for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
322 rc = sfc_tx_qinit_info(sa, sw_index);
324 goto fail_tx_qinit_info;
330 rte_free(sa->txq_info);
337 sfc_log_init(sa, "failed (rc = %d)", rc);
342 sfc_tx_fini(struct sfc_adapter *sa)
346 sw_index = sa->txq_count;
347 while (--sw_index >= 0) {
348 if (sa->txq_info[sw_index].txq != NULL)
349 sfc_tx_qfini(sa, sw_index);
352 rte_free(sa->txq_info);
358 sfc_tx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
360 struct rte_eth_dev_data *dev_data;
361 struct sfc_txq_info *txq_info;
365 unsigned int desc_index;
368 sfc_log_init(sa, "TxQ = %u", sw_index);
370 SFC_ASSERT(sw_index < sa->txq_count);
371 txq_info = &sa->txq_info[sw_index];
375 SFC_ASSERT(txq->state == SFC_TXQ_INITIALIZED);
379 rc = sfc_ev_qstart(sa, evq->evq_index);
384 * It seems that DPDK has no controls regarding IPv4 offloads,
385 * hence, we always enable it here
387 if ((txq->flags & ETH_TXQ_FLAGS_NOXSUMTCP) ||
388 (txq->flags & ETH_TXQ_FLAGS_NOXSUMUDP)) {
389 flags = EFX_TXQ_CKSUM_IPV4;
391 flags = EFX_TXQ_CKSUM_IPV4 | EFX_TXQ_CKSUM_TCPUDP;
394 flags |= EFX_TXQ_FATSOV2;
397 rc = efx_tx_qcreate(sa->nic, sw_index, 0, &txq->mem,
398 txq_info->entries, 0 /* not used on EF10 */,
400 &txq->common, &desc_index);
402 if (sa->tso && (rc == ENOSPC))
403 sfc_err(sa, "ran out of TSO contexts");
405 goto fail_tx_qcreate;
408 txq->added = txq->pending = txq->completed = desc_index;
409 txq->hw_vlan_tci = 0;
411 efx_tx_qenable(txq->common);
413 txq->state |= (SFC_TXQ_STARTED | SFC_TXQ_RUNNING);
416 * It seems to be used by DPDK for debug purposes only ('rte_ether')
418 dev_data = sa->eth_dev->data;
419 dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STARTED;
424 sfc_ev_qstop(sa, evq->evq_index);
431 sfc_tx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
433 struct rte_eth_dev_data *dev_data;
434 struct sfc_txq_info *txq_info;
436 unsigned int retry_count;
437 unsigned int wait_count;
440 sfc_log_init(sa, "TxQ = %u", sw_index);
442 SFC_ASSERT(sw_index < sa->txq_count);
443 txq_info = &sa->txq_info[sw_index];
447 if (txq->state == SFC_TXQ_INITIALIZED)
450 SFC_ASSERT(txq->state & SFC_TXQ_STARTED);
452 txq->state &= ~SFC_TXQ_RUNNING;
455 * Retry TX queue flushing in case of flush failed or
456 * timeout; in the worst case it can delay for 6 seconds
458 for (retry_count = 0;
459 ((txq->state & SFC_TXQ_FLUSHED) == 0) &&
460 (retry_count < SFC_TX_QFLUSH_ATTEMPTS);
462 if (efx_tx_qflush(txq->common) != 0) {
463 txq->state |= SFC_TXQ_FLUSHING;
468 * Wait for TX queue flush done or flush failed event at least
469 * SFC_TX_QFLUSH_POLL_WAIT_MS milliseconds and not more
470 * than 2 seconds (SFC_TX_QFLUSH_POLL_WAIT_MS multiplied
471 * by SFC_TX_QFLUSH_POLL_ATTEMPTS)
475 rte_delay_ms(SFC_TX_QFLUSH_POLL_WAIT_MS);
476 sfc_ev_qpoll(txq->evq);
477 } while ((txq->state & SFC_TXQ_FLUSHING) &&
478 wait_count++ < SFC_TX_QFLUSH_POLL_ATTEMPTS);
480 if (txq->state & SFC_TXQ_FLUSHING)
481 sfc_err(sa, "TxQ %u flush timed out", sw_index);
483 if (txq->state & SFC_TXQ_FLUSHED)
484 sfc_info(sa, "TxQ %u flushed", sw_index);
489 for (txds = 0; txds < txq_info->entries; txds++) {
490 if (txq->sw_ring[txds].mbuf != NULL) {
491 rte_pktmbuf_free(txq->sw_ring[txds].mbuf);
492 txq->sw_ring[txds].mbuf = NULL;
496 txq->state = SFC_TXQ_INITIALIZED;
498 efx_tx_qdestroy(txq->common);
500 sfc_ev_qstop(sa, txq->evq->evq_index);
503 * It seems to be used by DPDK for debug purposes only ('rte_ether')
505 dev_data = sa->eth_dev->data;
506 dev_data->tx_queue_state[sw_index] = RTE_ETH_QUEUE_STATE_STOPPED;
510 sfc_tx_start(struct sfc_adapter *sa)
512 unsigned int sw_index;
515 sfc_log_init(sa, "txq_count = %u", sa->txq_count);
518 if (!efx_nic_cfg_get(sa->nic)->enc_fw_assisted_tso_v2_enabled) {
519 sfc_warn(sa, "TSO support was unable to be restored");
524 rc = efx_tx_init(sa->nic);
526 goto fail_efx_tx_init;
528 for (sw_index = 0; sw_index < sa->txq_count; ++sw_index) {
529 if (!(sa->txq_info[sw_index].deferred_start) ||
530 sa->txq_info[sw_index].deferred_started) {
531 rc = sfc_tx_qstart(sa, sw_index);
540 while (sw_index-- > 0)
541 sfc_tx_qstop(sa, sw_index);
543 efx_tx_fini(sa->nic);
546 sfc_log_init(sa, "failed (rc = %d)", rc);
551 sfc_tx_stop(struct sfc_adapter *sa)
553 unsigned int sw_index;
555 sfc_log_init(sa, "txq_count = %u", sa->txq_count);
557 sw_index = sa->txq_count;
558 while (sw_index-- > 0) {
559 if (sa->txq_info[sw_index].txq != NULL)
560 sfc_tx_qstop(sa, sw_index);
563 efx_tx_fini(sa->nic);
567 * The function is used to insert or update VLAN tag;
568 * the firmware has state of the firmware tag to insert per TxQ
569 * (controlled by option descriptors), hence, if the tag of the
570 * packet to be sent is different from one remembered by the firmware,
571 * the function will update it
574 sfc_tx_maybe_insert_tag(struct sfc_txq *txq, struct rte_mbuf *m,
577 uint16_t this_tag = ((m->ol_flags & PKT_TX_VLAN_PKT) ?
580 if (this_tag == txq->hw_vlan_tci)
584 * The expression inside SFC_ASSERT() is not desired to be checked in
585 * a non-debug build because it might be too expensive on the data path
587 SFC_ASSERT(efx_nic_cfg_get(txq->evq->sa->nic)->enc_hw_tx_insert_vlan_enabled);
589 efx_tx_qdesc_vlantci_create(txq->common, rte_cpu_to_be_16(this_tag),
592 txq->hw_vlan_tci = this_tag;
598 sfc_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
600 struct sfc_txq *txq = (struct sfc_txq *)tx_queue;
601 unsigned int added = txq->added;
602 unsigned int pushed = added;
603 unsigned int pkts_sent = 0;
604 efx_desc_t *pend = &txq->pend_desc[0];
605 const unsigned int hard_max_fill = EFX_TXQ_LIMIT(txq->ptr_mask + 1);
606 const unsigned int soft_max_fill = hard_max_fill - txq->free_thresh;
607 unsigned int fill_level = added - txq->completed;
610 struct rte_mbuf **pktp;
612 if (unlikely((txq->state & SFC_TXQ_RUNNING) == 0))
616 * If insufficient space for a single packet is present,
617 * we should reap; otherwise, we shouldn't do that all the time
618 * to avoid latency increase
620 reap_done = (fill_level > soft_max_fill);
625 * Recalculate fill level since 'txq->completed'
626 * might have changed on reap
628 fill_level = added - txq->completed;
631 for (pkts_sent = 0, pktp = &tx_pkts[0];
632 (pkts_sent < nb_pkts) && (fill_level <= soft_max_fill);
633 pkts_sent++, pktp++) {
634 struct rte_mbuf *m_seg = *pktp;
635 size_t pkt_len = m_seg->pkt_len;
636 unsigned int pkt_descs = 0;
640 * Here VLAN TCI is expected to be zero in case if no
641 * DEV_TX_VLAN_OFFLOAD capability is advertised;
642 * if the calling app ignores the absence of
643 * DEV_TX_VLAN_OFFLOAD and pushes VLAN TCI, then
644 * TX_ERROR will occur
646 pkt_descs += sfc_tx_maybe_insert_tag(txq, m_seg, &pend);
648 if (m_seg->ol_flags & PKT_TX_TCP_SEG) {
650 * We expect correct 'pkt->l[2, 3, 4]_len' values
651 * to be set correctly by the caller
653 if (sfc_tso_do(txq, added, &m_seg, &in_off, &pend,
654 &pkt_descs, &pkt_len) != 0) {
655 /* We may have reached this place for
656 * one of the following reasons:
658 * 1) Packet header length is greater
659 * than SFC_TSOH_STD_LEN
660 * 2) TCP header starts at more then
661 * 208 bytes into the frame
663 * We will deceive RTE saying that we have sent
664 * the packet, but we will actually drop it.
665 * Hence, we should revert 'pend' to the
666 * previous state (in case we have added
667 * VLAN descriptor) and start processing
668 * another one packet. But the original
669 * mbuf shouldn't be orphaned
673 rte_pktmbuf_free(*pktp);
679 * We've only added 2 FATSOv2 option descriptors
680 * and 1 descriptor for the linearized packet header.
681 * The outstanding work will be done in the same manner
682 * as for the usual non-TSO path
686 for (; m_seg != NULL; m_seg = m_seg->next) {
687 efsys_dma_addr_t next_frag;
690 seg_len = m_seg->data_len;
691 next_frag = rte_mbuf_data_dma_addr(m_seg);
694 * If we've started TSO transaction few steps earlier,
695 * we'll skip packet header using an offset in the
696 * current segment (which has been set to the
697 * first one containing payload)
704 efsys_dma_addr_t frag_addr = next_frag;
707 next_frag = RTE_ALIGN(frag_addr + 1,
708 SFC_TX_SEG_BOUNDARY);
709 frag_len = MIN(next_frag - frag_addr, seg_len);
713 efx_tx_qdesc_dma_create(txq->common,
719 } while (seg_len != 0);
724 fill_level += pkt_descs;
725 if (unlikely(fill_level > hard_max_fill)) {
727 * Our estimation for maximum number of descriptors
728 * required to send a packet seems to be wrong.
729 * Try to reap (if we haven't yet).
734 fill_level = added - txq->completed;
735 if (fill_level > hard_max_fill) {
745 /* Assign mbuf to the last used desc */
746 txq->sw_ring[(added - 1) & txq->ptr_mask].mbuf = *pktp;
749 if (likely(pkts_sent > 0)) {
750 rc = efx_tx_qdesc_post(txq->common, txq->pend_desc,
751 pend - &txq->pend_desc[0],
752 txq->completed, &txq->added);
755 if (likely(pushed != txq->added))
756 efx_tx_qpush(txq->common, txq->added, pushed);
759 #if SFC_TX_XMIT_PKTS_REAP_AT_LEAST_ONCE