4 * Copyright (c) 2016-2017 Solarflare Communications Inc.
7 * This software was jointly developed between OKTET Labs (under contract
8 * for Solarflare) and Solarflare Communications, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * 1. Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <rte_mempool.h>
37 #include "sfc_debug.h"
41 #include "sfc_kvargs.h"
42 #include "sfc_tweak.h"
45 * Maximum number of Rx queue flush attempt in the case of failure or
48 #define SFC_RX_QFLUSH_ATTEMPTS (3)
51 * Time to wait between event queue polling attempts when waiting for Rx
52 * queue flush done or failed events.
54 #define SFC_RX_QFLUSH_POLL_WAIT_MS (1)
57 * Maximum number of event queue polling attempts when waiting for Rx queue
58 * flush done or failed events. It defines Rx queue flush attempt timeout
59 * together with SFC_RX_QFLUSH_POLL_WAIT_MS.
61 #define SFC_RX_QFLUSH_POLL_ATTEMPTS (2000)
64 sfc_rx_qflush_done(struct sfc_rxq *rxq)
66 rxq->state |= SFC_RXQ_FLUSHED;
67 rxq->state &= ~SFC_RXQ_FLUSHING;
71 sfc_rx_qflush_failed(struct sfc_rxq *rxq)
73 rxq->state |= SFC_RXQ_FLUSH_FAILED;
74 rxq->state &= ~SFC_RXQ_FLUSHING;
78 sfc_efx_rx_qrefill(struct sfc_efx_rxq *rxq)
80 unsigned int free_space;
82 void *objs[SFC_RX_REFILL_BULK];
83 efsys_dma_addr_t addr[RTE_DIM(objs)];
84 unsigned int added = rxq->added;
87 struct sfc_efx_rx_sw_desc *rxd;
89 uint16_t port_id = rxq->dp.dpq.port_id;
91 free_space = EFX_RXQ_LIMIT(rxq->ptr_mask + 1) -
92 (added - rxq->completed);
94 if (free_space < rxq->refill_threshold)
97 bulks = free_space / RTE_DIM(objs);
98 /* refill_threshold guarantees that bulks is positive */
99 SFC_ASSERT(bulks > 0);
101 id = added & rxq->ptr_mask;
103 if (unlikely(rte_mempool_get_bulk(rxq->refill_mb_pool, objs,
104 RTE_DIM(objs)) < 0)) {
106 * It is hardly a safe way to increment counter
107 * from different contexts, but all PMDs do it.
109 rxq->evq->sa->eth_dev->data->rx_mbuf_alloc_failed +=
111 /* Return if we have posted nothing yet */
112 if (added == rxq->added)
118 for (i = 0; i < RTE_DIM(objs);
119 ++i, id = (id + 1) & rxq->ptr_mask) {
122 rxd = &rxq->sw_desc[id];
125 SFC_ASSERT(rte_mbuf_refcnt_read(m) == 1);
126 m->data_off = RTE_PKTMBUF_HEADROOM;
127 SFC_ASSERT(m->next == NULL);
128 SFC_ASSERT(m->nb_segs == 1);
131 addr[i] = rte_pktmbuf_mtophys(m);
134 efx_rx_qpost(rxq->common, addr, rxq->buf_size,
135 RTE_DIM(objs), rxq->completed, added);
136 added += RTE_DIM(objs);
137 } while (--bulks > 0);
139 SFC_ASSERT(added != rxq->added);
141 efx_rx_qpush(rxq->common, added, &rxq->pushed);
145 sfc_efx_rx_desc_flags_to_offload_flags(const unsigned int desc_flags)
147 uint64_t mbuf_flags = 0;
149 switch (desc_flags & (EFX_PKT_IPV4 | EFX_CKSUM_IPV4)) {
150 case (EFX_PKT_IPV4 | EFX_CKSUM_IPV4):
151 mbuf_flags |= PKT_RX_IP_CKSUM_GOOD;
154 mbuf_flags |= PKT_RX_IP_CKSUM_BAD;
157 RTE_BUILD_BUG_ON(PKT_RX_IP_CKSUM_UNKNOWN != 0);
158 SFC_ASSERT((mbuf_flags & PKT_RX_IP_CKSUM_MASK) ==
159 PKT_RX_IP_CKSUM_UNKNOWN);
163 switch ((desc_flags &
164 (EFX_PKT_TCP | EFX_PKT_UDP | EFX_CKSUM_TCPUDP))) {
165 case (EFX_PKT_TCP | EFX_CKSUM_TCPUDP):
166 case (EFX_PKT_UDP | EFX_CKSUM_TCPUDP):
167 mbuf_flags |= PKT_RX_L4_CKSUM_GOOD;
171 mbuf_flags |= PKT_RX_L4_CKSUM_BAD;
174 RTE_BUILD_BUG_ON(PKT_RX_L4_CKSUM_UNKNOWN != 0);
175 SFC_ASSERT((mbuf_flags & PKT_RX_L4_CKSUM_MASK) ==
176 PKT_RX_L4_CKSUM_UNKNOWN);
184 sfc_efx_rx_desc_flags_to_packet_type(const unsigned int desc_flags)
186 return RTE_PTYPE_L2_ETHER |
187 ((desc_flags & EFX_PKT_IPV4) ?
188 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN : 0) |
189 ((desc_flags & EFX_PKT_IPV6) ?
190 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN : 0) |
191 ((desc_flags & EFX_PKT_TCP) ? RTE_PTYPE_L4_TCP : 0) |
192 ((desc_flags & EFX_PKT_UDP) ? RTE_PTYPE_L4_UDP : 0);
195 static const uint32_t *
196 sfc_efx_supported_ptypes_get(void)
198 static const uint32_t ptypes[] = {
200 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
201 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
210 #if EFSYS_OPT_RX_SCALE
212 sfc_efx_rx_set_rss_hash(struct sfc_efx_rxq *rxq, unsigned int flags,
218 if ((rxq->flags & SFC_EFX_RXQ_FLAG_RSS_HASH) == 0)
221 mbuf_data = rte_pktmbuf_mtod(m, uint8_t *);
223 if (flags & (EFX_PKT_IPV4 | EFX_PKT_IPV6)) {
224 m->hash.rss = efx_pseudo_hdr_hash_get(rxq->common,
225 EFX_RX_HASHALG_TOEPLITZ,
228 m->ol_flags |= PKT_RX_RSS_HASH;
233 sfc_efx_rx_set_rss_hash(__rte_unused struct sfc_efx_rxq *rxq,
234 __rte_unused unsigned int flags,
235 __rte_unused struct rte_mbuf *m)
241 sfc_efx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
243 struct sfc_dp_rxq *dp_rxq = rx_queue;
244 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
245 unsigned int completed;
246 unsigned int prefix_size = rxq->prefix_size;
247 unsigned int done_pkts = 0;
248 boolean_t discard_next = B_FALSE;
249 struct rte_mbuf *scatter_pkt = NULL;
251 if (unlikely((rxq->flags & SFC_EFX_RXQ_FLAG_RUNNING) == 0))
254 sfc_ev_qpoll(rxq->evq);
256 completed = rxq->completed;
257 while (completed != rxq->pending && done_pkts < nb_pkts) {
259 struct sfc_efx_rx_sw_desc *rxd;
261 unsigned int seg_len;
262 unsigned int desc_flags;
264 id = completed++ & rxq->ptr_mask;
265 rxd = &rxq->sw_desc[id];
267 desc_flags = rxd->flags;
272 if (desc_flags & (EFX_ADDR_MISMATCH | EFX_DISCARD))
275 if (desc_flags & EFX_PKT_PREFIX_LEN) {
279 rc = efx_pseudo_hdr_pkt_length_get(rxq->common,
280 rte_pktmbuf_mtod(m, uint8_t *), &tmp_size);
284 seg_len = rxd->size - prefix_size;
287 rte_pktmbuf_data_len(m) = seg_len;
288 rte_pktmbuf_pkt_len(m) = seg_len;
290 if (scatter_pkt != NULL) {
291 if (rte_pktmbuf_chain(scatter_pkt, m) != 0) {
292 rte_pktmbuf_free(scatter_pkt);
295 /* The packet to deliver */
299 if (desc_flags & EFX_PKT_CONT) {
300 /* The packet is scattered, more fragments to come */
302 /* Further fragments have no prefix */
307 /* Scattered packet is done */
309 /* The first fragment of the packet has prefix */
310 prefix_size = rxq->prefix_size;
313 sfc_efx_rx_desc_flags_to_offload_flags(desc_flags);
315 sfc_efx_rx_desc_flags_to_packet_type(desc_flags);
318 * Extract RSS hash from the packet prefix and
319 * set the corresponding field (if needed and possible)
321 sfc_efx_rx_set_rss_hash(rxq, desc_flags, m);
323 m->data_off += prefix_size;
330 discard_next = ((desc_flags & EFX_PKT_CONT) != 0);
331 rte_mempool_put(rxq->refill_mb_pool, m);
335 /* pending is only moved when entire packet is received */
336 SFC_ASSERT(scatter_pkt == NULL);
338 rxq->completed = completed;
340 sfc_efx_rx_qrefill(rxq);
345 static sfc_dp_rx_qdesc_npending_t sfc_efx_rx_qdesc_npending;
347 sfc_efx_rx_qdesc_npending(struct sfc_dp_rxq *dp_rxq)
349 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
351 if ((rxq->flags & SFC_EFX_RXQ_FLAG_RUNNING) == 0)
354 sfc_ev_qpoll(rxq->evq);
356 return rxq->pending - rxq->completed;
359 static sfc_dp_rx_qdesc_status_t sfc_efx_rx_qdesc_status;
361 sfc_efx_rx_qdesc_status(struct sfc_dp_rxq *dp_rxq, uint16_t offset)
363 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
365 if (unlikely(offset > rxq->ptr_mask))
369 * Poll EvQ to derive up-to-date 'rxq->pending' figure;
370 * it is required for the queue to be running, but the
371 * check is omitted because API design assumes that it
372 * is the duty of the caller to satisfy all conditions
374 SFC_ASSERT((rxq->flags & SFC_EFX_RXQ_FLAG_RUNNING) ==
375 SFC_EFX_RXQ_FLAG_RUNNING);
376 sfc_ev_qpoll(rxq->evq);
379 * There is a handful of reserved entries in the ring,
380 * but an explicit check whether the offset points to
381 * a reserved entry is neglected since the two checks
382 * below rely on the figures which take the HW limits
383 * into account and thus if an entry is reserved, the
384 * checks will fail and UNAVAIL code will be returned
387 if (offset < (rxq->pending - rxq->completed))
388 return RTE_ETH_RX_DESC_DONE;
390 if (offset < (rxq->added - rxq->completed))
391 return RTE_ETH_RX_DESC_AVAIL;
393 return RTE_ETH_RX_DESC_UNAVAIL;
397 sfc_rxq_by_dp_rxq(const struct sfc_dp_rxq *dp_rxq)
399 const struct sfc_dp_queue *dpq = &dp_rxq->dpq;
400 struct rte_eth_dev *eth_dev;
401 struct sfc_adapter *sa;
404 SFC_ASSERT(rte_eth_dev_is_valid_port(dpq->port_id));
405 eth_dev = &rte_eth_devices[dpq->port_id];
407 sa = eth_dev->data->dev_private;
409 SFC_ASSERT(dpq->queue_id < sa->rxq_count);
410 rxq = sa->rxq_info[dpq->queue_id].rxq;
412 SFC_ASSERT(rxq != NULL);
416 static sfc_dp_rx_qcreate_t sfc_efx_rx_qcreate;
418 sfc_efx_rx_qcreate(uint16_t port_id, uint16_t queue_id,
419 const struct rte_pci_addr *pci_addr, int socket_id,
420 const struct sfc_dp_rx_qcreate_info *info,
421 struct sfc_dp_rxq **dp_rxqp)
423 struct sfc_efx_rxq *rxq;
427 rxq = rte_zmalloc_socket("sfc-efx-rxq", sizeof(*rxq),
428 RTE_CACHE_LINE_SIZE, socket_id);
432 sfc_dp_queue_init(&rxq->dp.dpq, port_id, queue_id, pci_addr);
435 rxq->sw_desc = rte_calloc_socket("sfc-efx-rxq-sw_desc",
437 sizeof(*rxq->sw_desc),
438 RTE_CACHE_LINE_SIZE, socket_id);
439 if (rxq->sw_desc == NULL)
440 goto fail_desc_alloc;
442 /* efx datapath is bound to efx control path */
443 rxq->evq = sfc_rxq_by_dp_rxq(&rxq->dp)->evq;
444 if (info->flags & SFC_RXQ_FLAG_RSS_HASH)
445 rxq->flags |= SFC_EFX_RXQ_FLAG_RSS_HASH;
446 rxq->ptr_mask = info->rxq_entries - 1;
447 rxq->batch_max = info->batch_max;
448 rxq->prefix_size = info->prefix_size;
449 rxq->refill_threshold = info->refill_threshold;
450 rxq->buf_size = info->buf_size;
451 rxq->refill_mb_pool = info->refill_mb_pool;
463 static sfc_dp_rx_qdestroy_t sfc_efx_rx_qdestroy;
465 sfc_efx_rx_qdestroy(struct sfc_dp_rxq *dp_rxq)
467 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
469 rte_free(rxq->sw_desc);
473 static sfc_dp_rx_qstart_t sfc_efx_rx_qstart;
475 sfc_efx_rx_qstart(struct sfc_dp_rxq *dp_rxq,
476 __rte_unused unsigned int evq_read_ptr)
478 /* libefx-based datapath is specific to libefx-based PMD */
479 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
480 struct sfc_rxq *crxq = sfc_rxq_by_dp_rxq(dp_rxq);
482 rxq->common = crxq->common;
484 rxq->pending = rxq->completed = rxq->added = rxq->pushed = 0;
486 sfc_efx_rx_qrefill(rxq);
488 rxq->flags |= (SFC_EFX_RXQ_FLAG_STARTED | SFC_EFX_RXQ_FLAG_RUNNING);
493 static sfc_dp_rx_qstop_t sfc_efx_rx_qstop;
495 sfc_efx_rx_qstop(struct sfc_dp_rxq *dp_rxq,
496 __rte_unused unsigned int *evq_read_ptr)
498 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
500 rxq->flags &= ~SFC_EFX_RXQ_FLAG_RUNNING;
502 /* libefx-based datapath is bound to libefx-based PMD and uses
503 * event queue structure directly. So, there is no necessity to
504 * return EvQ read pointer.
508 static sfc_dp_rx_qpurge_t sfc_efx_rx_qpurge;
510 sfc_efx_rx_qpurge(struct sfc_dp_rxq *dp_rxq)
512 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
514 struct sfc_efx_rx_sw_desc *rxd;
516 for (i = rxq->completed; i != rxq->added; ++i) {
517 rxd = &rxq->sw_desc[i & rxq->ptr_mask];
518 rte_mempool_put(rxq->refill_mb_pool, rxd->mbuf);
520 /* Packed stream relies on 0 in inactive SW desc.
521 * Rx queue stop is not performance critical, so
522 * there is no harm to do it always.
528 rxq->flags &= ~SFC_EFX_RXQ_FLAG_STARTED;
531 struct sfc_dp_rx sfc_efx_rx = {
533 .name = SFC_KVARG_DATAPATH_EFX,
537 .features = SFC_DP_RX_FEAT_SCATTER,
538 .qcreate = sfc_efx_rx_qcreate,
539 .qdestroy = sfc_efx_rx_qdestroy,
540 .qstart = sfc_efx_rx_qstart,
541 .qstop = sfc_efx_rx_qstop,
542 .qpurge = sfc_efx_rx_qpurge,
543 .supported_ptypes_get = sfc_efx_supported_ptypes_get,
544 .qdesc_npending = sfc_efx_rx_qdesc_npending,
545 .qdesc_status = sfc_efx_rx_qdesc_status,
546 .pkt_burst = sfc_efx_recv_pkts,
550 sfc_rx_qdesc_npending(struct sfc_adapter *sa, unsigned int sw_index)
554 SFC_ASSERT(sw_index < sa->rxq_count);
555 rxq = sa->rxq_info[sw_index].rxq;
557 if (rxq == NULL || (rxq->state & SFC_RXQ_STARTED) == 0)
560 return sa->dp_rx->qdesc_npending(rxq->dp);
564 sfc_rx_qdesc_done(struct sfc_dp_rxq *dp_rxq, unsigned int offset)
566 struct sfc_rxq *rxq = sfc_rxq_by_dp_rxq(dp_rxq);
568 return offset < rxq->evq->sa->dp_rx->qdesc_npending(dp_rxq);
572 sfc_rx_qflush(struct sfc_adapter *sa, unsigned int sw_index)
575 unsigned int retry_count;
576 unsigned int wait_count;
579 rxq = sa->rxq_info[sw_index].rxq;
580 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
583 * Retry Rx queue flushing in the case of flush failed or
584 * timeout. In the worst case it can delay for 6 seconds.
586 for (retry_count = 0;
587 ((rxq->state & SFC_RXQ_FLUSHED) == 0) &&
588 (retry_count < SFC_RX_QFLUSH_ATTEMPTS);
590 rc = efx_rx_qflush(rxq->common);
592 rxq->state |= (rc == EALREADY) ?
593 SFC_RXQ_FLUSHED : SFC_RXQ_FLUSH_FAILED;
596 rxq->state &= ~SFC_RXQ_FLUSH_FAILED;
597 rxq->state |= SFC_RXQ_FLUSHING;
600 * Wait for Rx queue flush done or failed event at least
601 * SFC_RX_QFLUSH_POLL_WAIT_MS milliseconds and not more
602 * than 2 seconds (SFC_RX_QFLUSH_POLL_WAIT_MS multiplied
603 * by SFC_RX_QFLUSH_POLL_ATTEMPTS).
607 rte_delay_ms(SFC_RX_QFLUSH_POLL_WAIT_MS);
608 sfc_ev_qpoll(rxq->evq);
609 } while ((rxq->state & SFC_RXQ_FLUSHING) &&
610 (wait_count++ < SFC_RX_QFLUSH_POLL_ATTEMPTS));
612 if (rxq->state & SFC_RXQ_FLUSHING)
613 sfc_err(sa, "RxQ %u flush timed out", sw_index);
615 if (rxq->state & SFC_RXQ_FLUSH_FAILED)
616 sfc_err(sa, "RxQ %u flush failed", sw_index);
618 if (rxq->state & SFC_RXQ_FLUSHED)
619 sfc_info(sa, "RxQ %u flushed", sw_index);
622 sa->dp_rx->qpurge(rxq->dp);
626 sfc_rx_default_rxq_set_filter(struct sfc_adapter *sa, struct sfc_rxq *rxq)
628 boolean_t rss = (sa->rss_channels > 0) ? B_TRUE : B_FALSE;
629 struct sfc_port *port = &sa->port;
633 * If promiscuous or all-multicast mode has been requested, setting
634 * filter for the default Rx queue might fail, in particular, while
635 * running over PCI function which is not a member of corresponding
636 * privilege groups; if this occurs, few iterations will be made to
637 * repeat this step without promiscuous and all-multicast flags set
640 rc = efx_mac_filter_default_rxq_set(sa->nic, rxq->common, rss);
643 else if (rc != EOPNOTSUPP)
647 sfc_warn(sa, "promiscuous mode has been requested, "
648 "but the HW rejects it");
649 sfc_warn(sa, "promiscuous mode will be disabled");
651 port->promisc = B_FALSE;
652 rc = sfc_set_rx_mode(sa);
659 if (port->allmulti) {
660 sfc_warn(sa, "all-multicast mode has been requested, "
661 "but the HW rejects it");
662 sfc_warn(sa, "all-multicast mode will be disabled");
664 port->allmulti = B_FALSE;
665 rc = sfc_set_rx_mode(sa);
676 sfc_rx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
678 struct sfc_port *port = &sa->port;
679 struct sfc_rxq_info *rxq_info;
684 sfc_log_init(sa, "sw_index=%u", sw_index);
686 SFC_ASSERT(sw_index < sa->rxq_count);
688 rxq_info = &sa->rxq_info[sw_index];
690 SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
694 rc = sfc_ev_qstart(evq, sfc_evq_index_by_rxq_sw_index(sa, sw_index));
698 rc = efx_rx_qcreate(sa->nic, rxq->hw_index, 0, rxq_info->type,
699 &rxq->mem, rxq_info->entries,
700 0 /* not used on EF10 */, evq->common,
703 goto fail_rx_qcreate;
705 efx_rx_qenable(rxq->common);
707 rc = sa->dp_rx->qstart(rxq->dp, evq->read_ptr);
711 rxq->state |= SFC_RXQ_STARTED;
713 if ((sw_index == 0) && !port->isolated) {
714 rc = sfc_rx_default_rxq_set_filter(sa, rxq);
716 goto fail_mac_filter_default_rxq_set;
719 /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
720 sa->eth_dev->data->rx_queue_state[sw_index] =
721 RTE_ETH_QUEUE_STATE_STARTED;
725 fail_mac_filter_default_rxq_set:
726 sa->dp_rx->qstop(rxq->dp, &rxq->evq->read_ptr);
729 sfc_rx_qflush(sa, sw_index);
739 sfc_rx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
741 struct sfc_rxq_info *rxq_info;
744 sfc_log_init(sa, "sw_index=%u", sw_index);
746 SFC_ASSERT(sw_index < sa->rxq_count);
748 rxq_info = &sa->rxq_info[sw_index];
751 if (rxq->state == SFC_RXQ_INITIALIZED)
753 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
755 /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
756 sa->eth_dev->data->rx_queue_state[sw_index] =
757 RTE_ETH_QUEUE_STATE_STOPPED;
759 sa->dp_rx->qstop(rxq->dp, &rxq->evq->read_ptr);
762 efx_mac_filter_default_rxq_clear(sa->nic);
764 sfc_rx_qflush(sa, sw_index);
766 rxq->state = SFC_RXQ_INITIALIZED;
768 efx_rx_qdestroy(rxq->common);
770 sfc_ev_qstop(rxq->evq);
774 sfc_rx_qcheck_conf(struct sfc_adapter *sa, uint16_t nb_rx_desc,
775 const struct rte_eth_rxconf *rx_conf)
777 const uint16_t rx_free_thresh_max = EFX_RXQ_LIMIT(nb_rx_desc);
780 if (rx_conf->rx_thresh.pthresh != 0 ||
781 rx_conf->rx_thresh.hthresh != 0 ||
782 rx_conf->rx_thresh.wthresh != 0) {
784 "RxQ prefetch/host/writeback thresholds are not supported");
788 if (rx_conf->rx_free_thresh > rx_free_thresh_max) {
790 "RxQ free threshold too large: %u vs maximum %u",
791 rx_conf->rx_free_thresh, rx_free_thresh_max);
795 if (rx_conf->rx_drop_en == 0) {
796 sfc_err(sa, "RxQ drop disable is not supported");
804 sfc_rx_mbuf_data_alignment(struct rte_mempool *mb_pool)
809 /* The mbuf object itself is always cache line aligned */
810 order = rte_bsf32(RTE_CACHE_LINE_SIZE);
812 /* Data offset from mbuf object start */
813 data_off = sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mb_pool) +
814 RTE_PKTMBUF_HEADROOM;
816 order = MIN(order, rte_bsf32(data_off));
818 return 1u << (order - 1);
822 sfc_rx_mb_pool_buf_size(struct sfc_adapter *sa, struct rte_mempool *mb_pool)
824 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
825 const uint32_t nic_align_start = MAX(1, encp->enc_rx_buf_align_start);
826 const uint32_t nic_align_end = MAX(1, encp->enc_rx_buf_align_end);
828 unsigned int buf_aligned;
829 unsigned int start_alignment;
830 unsigned int end_padding_alignment;
832 /* Below it is assumed that both alignments are power of 2 */
833 SFC_ASSERT(rte_is_power_of_2(nic_align_start));
834 SFC_ASSERT(rte_is_power_of_2(nic_align_end));
837 * mbuf is always cache line aligned, double-check
838 * that it meets rx buffer start alignment requirements.
841 /* Start from mbuf pool data room size */
842 buf_size = rte_pktmbuf_data_room_size(mb_pool);
844 /* Remove headroom */
845 if (buf_size <= RTE_PKTMBUF_HEADROOM) {
847 "RxQ mbuf pool %s object data room size %u is smaller than headroom %u",
848 mb_pool->name, buf_size, RTE_PKTMBUF_HEADROOM);
851 buf_size -= RTE_PKTMBUF_HEADROOM;
853 /* Calculate guaranteed data start alignment */
854 buf_aligned = sfc_rx_mbuf_data_alignment(mb_pool);
856 /* Reserve space for start alignment */
857 if (buf_aligned < nic_align_start) {
858 start_alignment = nic_align_start - buf_aligned;
859 if (buf_size <= start_alignment) {
861 "RxQ mbuf pool %s object data room size %u is insufficient for headroom %u and buffer start alignment %u required by NIC",
863 rte_pktmbuf_data_room_size(mb_pool),
864 RTE_PKTMBUF_HEADROOM, start_alignment);
867 buf_aligned = nic_align_start;
868 buf_size -= start_alignment;
873 /* Make sure that end padding does not write beyond the buffer */
874 if (buf_aligned < nic_align_end) {
876 * Estimate space which can be lost. If guarnteed buffer
877 * size is odd, lost space is (nic_align_end - 1). More
878 * accurate formula is below.
880 end_padding_alignment = nic_align_end -
881 MIN(buf_aligned, 1u << (rte_bsf32(buf_size) - 1));
882 if (buf_size <= end_padding_alignment) {
884 "RxQ mbuf pool %s object data room size %u is insufficient for headroom %u, buffer start alignment %u and end padding alignment %u required by NIC",
886 rte_pktmbuf_data_room_size(mb_pool),
887 RTE_PKTMBUF_HEADROOM, start_alignment,
888 end_padding_alignment);
891 buf_size -= end_padding_alignment;
894 * Start is aligned the same or better than end,
897 buf_size = P2ALIGN(buf_size, nic_align_end);
904 sfc_rx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
905 uint16_t nb_rx_desc, unsigned int socket_id,
906 const struct rte_eth_rxconf *rx_conf,
907 struct rte_mempool *mb_pool)
909 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
912 struct sfc_rxq_info *rxq_info;
915 struct sfc_dp_rx_qcreate_info info;
917 rc = sfc_rx_qcheck_conf(sa, nb_rx_desc, rx_conf);
921 buf_size = sfc_rx_mb_pool_buf_size(sa, mb_pool);
923 sfc_err(sa, "RxQ %u mbuf pool object size is too small",
929 if ((buf_size < sa->port.pdu + encp->enc_rx_prefix_size) &&
930 !sa->eth_dev->data->dev_conf.rxmode.enable_scatter) {
931 sfc_err(sa, "Rx scatter is disabled and RxQ %u mbuf pool "
932 "object size is too small", sw_index);
933 sfc_err(sa, "RxQ %u calculated Rx buffer size is %u vs "
934 "PDU size %u plus Rx prefix %u bytes",
935 sw_index, buf_size, (unsigned int)sa->port.pdu,
936 encp->enc_rx_prefix_size);
941 SFC_ASSERT(sw_index < sa->rxq_count);
942 rxq_info = &sa->rxq_info[sw_index];
944 SFC_ASSERT(nb_rx_desc <= rxq_info->max_entries);
945 rxq_info->entries = nb_rx_desc;
947 sa->eth_dev->data->dev_conf.rxmode.enable_scatter ?
948 EFX_RXQ_TYPE_SCATTER : EFX_RXQ_TYPE_DEFAULT;
950 rc = sfc_ev_qinit(sa, SFC_EVQ_TYPE_RX, sw_index,
951 rxq_info->entries, socket_id, &evq);
956 rxq = rte_zmalloc_socket("sfc-rxq", sizeof(*rxq), RTE_CACHE_LINE_SIZE,
964 rxq->hw_index = sw_index;
965 rxq->refill_threshold =
966 RTE_MAX(rx_conf->rx_free_thresh, SFC_RX_REFILL_BULK);
967 rxq->refill_mb_pool = mb_pool;
969 rc = sfc_dma_alloc(sa, "rxq", sw_index, EFX_RXQ_SIZE(rxq_info->entries),
970 socket_id, &rxq->mem);
974 memset(&info, 0, sizeof(info));
975 info.refill_mb_pool = rxq->refill_mb_pool;
976 info.refill_threshold = rxq->refill_threshold;
977 info.buf_size = buf_size;
978 info.batch_max = encp->enc_rx_batch_max;
979 info.prefix_size = encp->enc_rx_prefix_size;
981 #if EFSYS_OPT_RX_SCALE
982 if (sa->hash_support == EFX_RX_HASH_AVAILABLE && sa->rss_channels > 0)
983 info.flags |= SFC_RXQ_FLAG_RSS_HASH;
986 info.rxq_entries = rxq_info->entries;
987 info.rxq_hw_ring = rxq->mem.esm_base;
988 info.evq_entries = rxq_info->entries;
989 info.evq_hw_ring = evq->mem.esm_base;
990 info.hw_index = rxq->hw_index;
991 info.mem_bar = sa->mem_bar.esb_base;
993 rc = sa->dp_rx->qcreate(sa->eth_dev->data->port_id, sw_index,
994 &RTE_ETH_DEV_TO_PCI(sa->eth_dev)->addr,
995 socket_id, &info, &rxq->dp);
997 goto fail_dp_rx_qcreate;
999 evq->dp_rxq = rxq->dp;
1001 rxq->state = SFC_RXQ_INITIALIZED;
1003 rxq_info->deferred_start = (rx_conf->rx_deferred_start != 0);
1008 sfc_dma_free(sa, &rxq->mem);
1011 rxq_info->rxq = NULL;
1018 rxq_info->entries = 0;
1021 sfc_log_init(sa, "failed %d", rc);
1026 sfc_rx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
1028 struct sfc_rxq_info *rxq_info;
1029 struct sfc_rxq *rxq;
1031 SFC_ASSERT(sw_index < sa->rxq_count);
1033 rxq_info = &sa->rxq_info[sw_index];
1035 rxq = rxq_info->rxq;
1036 SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
1038 sa->dp_rx->qdestroy(rxq->dp);
1041 rxq_info->rxq = NULL;
1042 rxq_info->entries = 0;
1044 sfc_dma_free(sa, &rxq->mem);
1046 sfc_ev_qfini(rxq->evq);
1052 #if EFSYS_OPT_RX_SCALE
1054 sfc_rte_to_efx_hash_type(uint64_t rss_hf)
1056 efx_rx_hash_type_t efx_hash_types = 0;
1058 if ((rss_hf & (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
1059 ETH_RSS_NONFRAG_IPV4_OTHER)) != 0)
1060 efx_hash_types |= EFX_RX_HASH_IPV4;
1062 if ((rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) != 0)
1063 efx_hash_types |= EFX_RX_HASH_TCPIPV4;
1065 if ((rss_hf & (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
1066 ETH_RSS_NONFRAG_IPV6_OTHER | ETH_RSS_IPV6_EX)) != 0)
1067 efx_hash_types |= EFX_RX_HASH_IPV6;
1069 if ((rss_hf & (ETH_RSS_NONFRAG_IPV6_TCP | ETH_RSS_IPV6_TCP_EX)) != 0)
1070 efx_hash_types |= EFX_RX_HASH_TCPIPV6;
1072 return efx_hash_types;
1076 sfc_efx_to_rte_hash_type(efx_rx_hash_type_t efx_hash_types)
1078 uint64_t rss_hf = 0;
1080 if ((efx_hash_types & EFX_RX_HASH_IPV4) != 0)
1081 rss_hf |= (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
1082 ETH_RSS_NONFRAG_IPV4_OTHER);
1084 if ((efx_hash_types & EFX_RX_HASH_TCPIPV4) != 0)
1085 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
1087 if ((efx_hash_types & EFX_RX_HASH_IPV6) != 0)
1088 rss_hf |= (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
1089 ETH_RSS_NONFRAG_IPV6_OTHER | ETH_RSS_IPV6_EX);
1091 if ((efx_hash_types & EFX_RX_HASH_TCPIPV6) != 0)
1092 rss_hf |= (ETH_RSS_NONFRAG_IPV6_TCP | ETH_RSS_IPV6_TCP_EX);
1098 #if EFSYS_OPT_RX_SCALE
1100 sfc_rx_rss_config(struct sfc_adapter *sa)
1104 if (sa->rss_channels > 0) {
1105 rc = efx_rx_scale_mode_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1106 EFX_RX_HASHALG_TOEPLITZ,
1107 sa->rss_hash_types, B_TRUE);
1111 rc = efx_rx_scale_key_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1113 sizeof(sa->rss_key));
1117 rc = efx_rx_scale_tbl_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1118 sa->rss_tbl, RTE_DIM(sa->rss_tbl));
1126 sfc_rx_rss_config(__rte_unused struct sfc_adapter *sa)
1133 sfc_rx_start(struct sfc_adapter *sa)
1135 unsigned int sw_index;
1138 sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
1140 rc = efx_rx_init(sa->nic);
1144 rc = sfc_rx_rss_config(sa);
1146 goto fail_rss_config;
1148 for (sw_index = 0; sw_index < sa->rxq_count; ++sw_index) {
1149 if ((!sa->rxq_info[sw_index].deferred_start ||
1150 sa->rxq_info[sw_index].deferred_started)) {
1151 rc = sfc_rx_qstart(sa, sw_index);
1153 goto fail_rx_qstart;
1160 while (sw_index-- > 0)
1161 sfc_rx_qstop(sa, sw_index);
1164 efx_rx_fini(sa->nic);
1167 sfc_log_init(sa, "failed %d", rc);
1172 sfc_rx_stop(struct sfc_adapter *sa)
1174 unsigned int sw_index;
1176 sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
1178 sw_index = sa->rxq_count;
1179 while (sw_index-- > 0) {
1180 if (sa->rxq_info[sw_index].rxq != NULL)
1181 sfc_rx_qstop(sa, sw_index);
1184 efx_rx_fini(sa->nic);
1188 sfc_rx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
1190 struct sfc_rxq_info *rxq_info = &sa->rxq_info[sw_index];
1191 unsigned int max_entries;
1193 max_entries = EFX_RXQ_MAXNDESCS;
1194 SFC_ASSERT(rte_is_power_of_2(max_entries));
1196 rxq_info->max_entries = max_entries;
1202 sfc_rx_check_mode(struct sfc_adapter *sa, struct rte_eth_rxmode *rxmode)
1206 switch (rxmode->mq_mode) {
1207 case ETH_MQ_RX_NONE:
1208 /* No special checks are required */
1210 #if EFSYS_OPT_RX_SCALE
1212 if (sa->rss_support == EFX_RX_SCALE_UNAVAILABLE) {
1213 sfc_err(sa, "RSS is not available");
1219 sfc_err(sa, "Rx multi-queue mode %u not supported",
1224 if (rxmode->header_split) {
1225 sfc_err(sa, "Header split on Rx not supported");
1229 if (rxmode->hw_vlan_filter) {
1230 sfc_err(sa, "HW VLAN filtering not supported");
1234 if (rxmode->hw_vlan_strip) {
1235 sfc_err(sa, "HW VLAN stripping not supported");
1239 if (rxmode->hw_vlan_extend) {
1241 "Q-in-Q HW VLAN stripping not supported");
1245 if (!rxmode->hw_strip_crc) {
1247 "FCS stripping control not supported - always stripped");
1248 rxmode->hw_strip_crc = 1;
1251 if (rxmode->enable_scatter &&
1252 (~sa->dp_rx->features & SFC_DP_RX_FEAT_SCATTER)) {
1253 sfc_err(sa, "Rx scatter not supported by %s datapath",
1254 sa->dp_rx->dp.name);
1258 if (rxmode->enable_lro) {
1259 sfc_err(sa, "LRO not supported");
1267 * Destroy excess queues that are no longer needed after reconfiguration
1268 * or complete close.
1271 sfc_rx_fini_queues(struct sfc_adapter *sa, unsigned int nb_rx_queues)
1275 SFC_ASSERT(nb_rx_queues <= sa->rxq_count);
1277 sw_index = sa->rxq_count;
1278 while (--sw_index >= (int)nb_rx_queues) {
1279 if (sa->rxq_info[sw_index].rxq != NULL)
1280 sfc_rx_qfini(sa, sw_index);
1283 sa->rxq_count = nb_rx_queues;
1287 * Initialize Rx subsystem.
1289 * Called at device (re)configuration stage when number of receive queues is
1290 * specified together with other device level receive configuration.
1292 * It should be used to allocate NUMA-unaware resources.
1295 sfc_rx_configure(struct sfc_adapter *sa)
1297 struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
1298 const unsigned int nb_rx_queues = sa->eth_dev->data->nb_rx_queues;
1301 sfc_log_init(sa, "nb_rx_queues=%u (old %u)",
1302 nb_rx_queues, sa->rxq_count);
1304 rc = sfc_rx_check_mode(sa, &dev_conf->rxmode);
1306 goto fail_check_mode;
1308 if (nb_rx_queues == sa->rxq_count)
1311 if (sa->rxq_info == NULL) {
1313 sa->rxq_info = rte_calloc_socket("sfc-rxqs", nb_rx_queues,
1314 sizeof(sa->rxq_info[0]), 0,
1316 if (sa->rxq_info == NULL)
1317 goto fail_rxqs_alloc;
1319 struct sfc_rxq_info *new_rxq_info;
1321 if (nb_rx_queues < sa->rxq_count)
1322 sfc_rx_fini_queues(sa, nb_rx_queues);
1326 rte_realloc(sa->rxq_info,
1327 nb_rx_queues * sizeof(sa->rxq_info[0]), 0);
1328 if (new_rxq_info == NULL && nb_rx_queues > 0)
1329 goto fail_rxqs_realloc;
1331 sa->rxq_info = new_rxq_info;
1332 if (nb_rx_queues > sa->rxq_count)
1333 memset(&sa->rxq_info[sa->rxq_count], 0,
1334 (nb_rx_queues - sa->rxq_count) *
1335 sizeof(sa->rxq_info[0]));
1338 while (sa->rxq_count < nb_rx_queues) {
1339 rc = sfc_rx_qinit_info(sa, sa->rxq_count);
1341 goto fail_rx_qinit_info;
1346 #if EFSYS_OPT_RX_SCALE
1347 sa->rss_channels = (dev_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) ?
1348 MIN(sa->rxq_count, EFX_MAXRSS) : 0;
1350 if (sa->rss_channels > 0) {
1351 unsigned int sw_index;
1353 for (sw_index = 0; sw_index < EFX_RSS_TBL_SIZE; ++sw_index)
1354 sa->rss_tbl[sw_index] = sw_index % sa->rss_channels;
1367 sfc_log_init(sa, "failed %d", rc);
1372 * Shutdown Rx subsystem.
1374 * Called at device close stage, for example, before device shutdown.
1377 sfc_rx_close(struct sfc_adapter *sa)
1379 sfc_rx_fini_queues(sa, 0);
1381 sa->rss_channels = 0;
1383 rte_free(sa->rxq_info);
1384 sa->rxq_info = NULL;