1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2016-2018 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
10 #include <rte_mempool.h>
15 #include "sfc_debug.h"
19 #include "sfc_kvargs.h"
20 #include "sfc_tweak.h"
23 * Maximum number of Rx queue flush attempt in the case of failure or
26 #define SFC_RX_QFLUSH_ATTEMPTS (3)
29 * Time to wait between event queue polling attempts when waiting for Rx
30 * queue flush done or failed events.
32 #define SFC_RX_QFLUSH_POLL_WAIT_MS (1)
35 * Maximum number of event queue polling attempts when waiting for Rx queue
36 * flush done or failed events. It defines Rx queue flush attempt timeout
37 * together with SFC_RX_QFLUSH_POLL_WAIT_MS.
39 #define SFC_RX_QFLUSH_POLL_ATTEMPTS (2000)
42 sfc_rx_qflush_done(struct sfc_rxq *rxq)
44 rxq->state |= SFC_RXQ_FLUSHED;
45 rxq->state &= ~SFC_RXQ_FLUSHING;
49 sfc_rx_qflush_failed(struct sfc_rxq *rxq)
51 rxq->state |= SFC_RXQ_FLUSH_FAILED;
52 rxq->state &= ~SFC_RXQ_FLUSHING;
56 sfc_efx_rx_qrefill(struct sfc_efx_rxq *rxq)
58 unsigned int free_space;
60 void *objs[SFC_RX_REFILL_BULK];
61 efsys_dma_addr_t addr[RTE_DIM(objs)];
62 unsigned int added = rxq->added;
65 struct sfc_efx_rx_sw_desc *rxd;
67 uint16_t port_id = rxq->dp.dpq.port_id;
69 free_space = rxq->max_fill_level - (added - rxq->completed);
71 if (free_space < rxq->refill_threshold)
74 bulks = free_space / RTE_DIM(objs);
75 /* refill_threshold guarantees that bulks is positive */
76 SFC_ASSERT(bulks > 0);
78 id = added & rxq->ptr_mask;
80 if (unlikely(rte_mempool_get_bulk(rxq->refill_mb_pool, objs,
81 RTE_DIM(objs)) < 0)) {
83 * It is hardly a safe way to increment counter
84 * from different contexts, but all PMDs do it.
86 rxq->evq->sa->eth_dev->data->rx_mbuf_alloc_failed +=
88 /* Return if we have posted nothing yet */
89 if (added == rxq->added)
95 for (i = 0; i < RTE_DIM(objs);
96 ++i, id = (id + 1) & rxq->ptr_mask) {
99 rxd = &rxq->sw_desc[id];
102 SFC_ASSERT(rte_mbuf_refcnt_read(m) == 1);
103 m->data_off = RTE_PKTMBUF_HEADROOM;
104 SFC_ASSERT(m->next == NULL);
105 SFC_ASSERT(m->nb_segs == 1);
108 addr[i] = rte_pktmbuf_iova(m);
111 efx_rx_qpost(rxq->common, addr, rxq->buf_size,
112 RTE_DIM(objs), rxq->completed, added);
113 added += RTE_DIM(objs);
114 } while (--bulks > 0);
116 SFC_ASSERT(added != rxq->added);
118 efx_rx_qpush(rxq->common, added, &rxq->pushed);
122 sfc_efx_rx_desc_flags_to_offload_flags(const unsigned int desc_flags)
124 uint64_t mbuf_flags = 0;
126 switch (desc_flags & (EFX_PKT_IPV4 | EFX_CKSUM_IPV4)) {
127 case (EFX_PKT_IPV4 | EFX_CKSUM_IPV4):
128 mbuf_flags |= PKT_RX_IP_CKSUM_GOOD;
131 mbuf_flags |= PKT_RX_IP_CKSUM_BAD;
134 RTE_BUILD_BUG_ON(PKT_RX_IP_CKSUM_UNKNOWN != 0);
135 SFC_ASSERT((mbuf_flags & PKT_RX_IP_CKSUM_MASK) ==
136 PKT_RX_IP_CKSUM_UNKNOWN);
140 switch ((desc_flags &
141 (EFX_PKT_TCP | EFX_PKT_UDP | EFX_CKSUM_TCPUDP))) {
142 case (EFX_PKT_TCP | EFX_CKSUM_TCPUDP):
143 case (EFX_PKT_UDP | EFX_CKSUM_TCPUDP):
144 mbuf_flags |= PKT_RX_L4_CKSUM_GOOD;
148 mbuf_flags |= PKT_RX_L4_CKSUM_BAD;
151 RTE_BUILD_BUG_ON(PKT_RX_L4_CKSUM_UNKNOWN != 0);
152 SFC_ASSERT((mbuf_flags & PKT_RX_L4_CKSUM_MASK) ==
153 PKT_RX_L4_CKSUM_UNKNOWN);
161 sfc_efx_rx_desc_flags_to_packet_type(const unsigned int desc_flags)
163 return RTE_PTYPE_L2_ETHER |
164 ((desc_flags & EFX_PKT_IPV4) ?
165 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN : 0) |
166 ((desc_flags & EFX_PKT_IPV6) ?
167 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN : 0) |
168 ((desc_flags & EFX_PKT_TCP) ? RTE_PTYPE_L4_TCP : 0) |
169 ((desc_flags & EFX_PKT_UDP) ? RTE_PTYPE_L4_UDP : 0);
172 static const uint32_t *
173 sfc_efx_supported_ptypes_get(__rte_unused uint32_t tunnel_encaps)
175 static const uint32_t ptypes[] = {
177 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
178 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
187 #if EFSYS_OPT_RX_SCALE
189 sfc_efx_rx_set_rss_hash(struct sfc_efx_rxq *rxq, unsigned int flags,
195 if ((rxq->flags & SFC_EFX_RXQ_FLAG_RSS_HASH) == 0)
198 mbuf_data = rte_pktmbuf_mtod(m, uint8_t *);
200 if (flags & (EFX_PKT_IPV4 | EFX_PKT_IPV6)) {
201 m->hash.rss = efx_pseudo_hdr_hash_get(rxq->common,
202 EFX_RX_HASHALG_TOEPLITZ,
205 m->ol_flags |= PKT_RX_RSS_HASH;
210 sfc_efx_rx_set_rss_hash(__rte_unused struct sfc_efx_rxq *rxq,
211 __rte_unused unsigned int flags,
212 __rte_unused struct rte_mbuf *m)
218 sfc_efx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
220 struct sfc_dp_rxq *dp_rxq = rx_queue;
221 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
222 unsigned int completed;
223 unsigned int prefix_size = rxq->prefix_size;
224 unsigned int done_pkts = 0;
225 boolean_t discard_next = B_FALSE;
226 struct rte_mbuf *scatter_pkt = NULL;
228 if (unlikely((rxq->flags & SFC_EFX_RXQ_FLAG_RUNNING) == 0))
231 sfc_ev_qpoll(rxq->evq);
233 completed = rxq->completed;
234 while (completed != rxq->pending && done_pkts < nb_pkts) {
236 struct sfc_efx_rx_sw_desc *rxd;
238 unsigned int seg_len;
239 unsigned int desc_flags;
241 id = completed++ & rxq->ptr_mask;
242 rxd = &rxq->sw_desc[id];
244 desc_flags = rxd->flags;
249 if (desc_flags & (EFX_ADDR_MISMATCH | EFX_DISCARD))
252 if (desc_flags & EFX_PKT_PREFIX_LEN) {
256 rc = efx_pseudo_hdr_pkt_length_get(rxq->common,
257 rte_pktmbuf_mtod(m, uint8_t *), &tmp_size);
261 seg_len = rxd->size - prefix_size;
264 rte_pktmbuf_data_len(m) = seg_len;
265 rte_pktmbuf_pkt_len(m) = seg_len;
267 if (scatter_pkt != NULL) {
268 if (rte_pktmbuf_chain(scatter_pkt, m) != 0) {
269 rte_pktmbuf_free(scatter_pkt);
272 /* The packet to deliver */
276 if (desc_flags & EFX_PKT_CONT) {
277 /* The packet is scattered, more fragments to come */
279 /* Further fragments have no prefix */
284 /* Scattered packet is done */
286 /* The first fragment of the packet has prefix */
287 prefix_size = rxq->prefix_size;
290 sfc_efx_rx_desc_flags_to_offload_flags(desc_flags);
292 sfc_efx_rx_desc_flags_to_packet_type(desc_flags);
295 * Extract RSS hash from the packet prefix and
296 * set the corresponding field (if needed and possible)
298 sfc_efx_rx_set_rss_hash(rxq, desc_flags, m);
300 m->data_off += prefix_size;
307 discard_next = ((desc_flags & EFX_PKT_CONT) != 0);
308 rte_mempool_put(rxq->refill_mb_pool, m);
312 /* pending is only moved when entire packet is received */
313 SFC_ASSERT(scatter_pkt == NULL);
315 rxq->completed = completed;
317 sfc_efx_rx_qrefill(rxq);
322 static sfc_dp_rx_qdesc_npending_t sfc_efx_rx_qdesc_npending;
324 sfc_efx_rx_qdesc_npending(struct sfc_dp_rxq *dp_rxq)
326 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
328 if ((rxq->flags & SFC_EFX_RXQ_FLAG_RUNNING) == 0)
331 sfc_ev_qpoll(rxq->evq);
333 return rxq->pending - rxq->completed;
336 static sfc_dp_rx_qdesc_status_t sfc_efx_rx_qdesc_status;
338 sfc_efx_rx_qdesc_status(struct sfc_dp_rxq *dp_rxq, uint16_t offset)
340 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
342 if (unlikely(offset > rxq->ptr_mask))
346 * Poll EvQ to derive up-to-date 'rxq->pending' figure;
347 * it is required for the queue to be running, but the
348 * check is omitted because API design assumes that it
349 * is the duty of the caller to satisfy all conditions
351 SFC_ASSERT((rxq->flags & SFC_EFX_RXQ_FLAG_RUNNING) ==
352 SFC_EFX_RXQ_FLAG_RUNNING);
353 sfc_ev_qpoll(rxq->evq);
356 * There is a handful of reserved entries in the ring,
357 * but an explicit check whether the offset points to
358 * a reserved entry is neglected since the two checks
359 * below rely on the figures which take the HW limits
360 * into account and thus if an entry is reserved, the
361 * checks will fail and UNAVAIL code will be returned
364 if (offset < (rxq->pending - rxq->completed))
365 return RTE_ETH_RX_DESC_DONE;
367 if (offset < (rxq->added - rxq->completed))
368 return RTE_ETH_RX_DESC_AVAIL;
370 return RTE_ETH_RX_DESC_UNAVAIL;
374 sfc_rxq_by_dp_rxq(const struct sfc_dp_rxq *dp_rxq)
376 const struct sfc_dp_queue *dpq = &dp_rxq->dpq;
377 struct rte_eth_dev *eth_dev;
378 struct sfc_adapter *sa;
381 SFC_ASSERT(rte_eth_dev_is_valid_port(dpq->port_id));
382 eth_dev = &rte_eth_devices[dpq->port_id];
384 sa = eth_dev->data->dev_private;
386 SFC_ASSERT(dpq->queue_id < sa->rxq_count);
387 rxq = sa->rxq_info[dpq->queue_id].rxq;
389 SFC_ASSERT(rxq != NULL);
393 static sfc_dp_rx_qsize_up_rings_t sfc_efx_rx_qsize_up_rings;
395 sfc_efx_rx_qsize_up_rings(uint16_t nb_rx_desc,
396 unsigned int *rxq_entries,
397 unsigned int *evq_entries,
398 unsigned int *rxq_max_fill_level)
400 *rxq_entries = nb_rx_desc;
401 *evq_entries = nb_rx_desc;
402 *rxq_max_fill_level = EFX_RXQ_LIMIT(*rxq_entries);
406 static sfc_dp_rx_qcreate_t sfc_efx_rx_qcreate;
408 sfc_efx_rx_qcreate(uint16_t port_id, uint16_t queue_id,
409 const struct rte_pci_addr *pci_addr, int socket_id,
410 const struct sfc_dp_rx_qcreate_info *info,
411 struct sfc_dp_rxq **dp_rxqp)
413 struct sfc_efx_rxq *rxq;
417 rxq = rte_zmalloc_socket("sfc-efx-rxq", sizeof(*rxq),
418 RTE_CACHE_LINE_SIZE, socket_id);
422 sfc_dp_queue_init(&rxq->dp.dpq, port_id, queue_id, pci_addr);
425 rxq->sw_desc = rte_calloc_socket("sfc-efx-rxq-sw_desc",
427 sizeof(*rxq->sw_desc),
428 RTE_CACHE_LINE_SIZE, socket_id);
429 if (rxq->sw_desc == NULL)
430 goto fail_desc_alloc;
432 /* efx datapath is bound to efx control path */
433 rxq->evq = sfc_rxq_by_dp_rxq(&rxq->dp)->evq;
434 if (info->flags & SFC_RXQ_FLAG_RSS_HASH)
435 rxq->flags |= SFC_EFX_RXQ_FLAG_RSS_HASH;
436 rxq->ptr_mask = info->rxq_entries - 1;
437 rxq->batch_max = info->batch_max;
438 rxq->prefix_size = info->prefix_size;
439 rxq->max_fill_level = info->max_fill_level;
440 rxq->refill_threshold = info->refill_threshold;
441 rxq->buf_size = info->buf_size;
442 rxq->refill_mb_pool = info->refill_mb_pool;
454 static sfc_dp_rx_qdestroy_t sfc_efx_rx_qdestroy;
456 sfc_efx_rx_qdestroy(struct sfc_dp_rxq *dp_rxq)
458 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
460 rte_free(rxq->sw_desc);
464 static sfc_dp_rx_qstart_t sfc_efx_rx_qstart;
466 sfc_efx_rx_qstart(struct sfc_dp_rxq *dp_rxq,
467 __rte_unused unsigned int evq_read_ptr)
469 /* libefx-based datapath is specific to libefx-based PMD */
470 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
471 struct sfc_rxq *crxq = sfc_rxq_by_dp_rxq(dp_rxq);
473 rxq->common = crxq->common;
475 rxq->pending = rxq->completed = rxq->added = rxq->pushed = 0;
477 sfc_efx_rx_qrefill(rxq);
479 rxq->flags |= (SFC_EFX_RXQ_FLAG_STARTED | SFC_EFX_RXQ_FLAG_RUNNING);
484 static sfc_dp_rx_qstop_t sfc_efx_rx_qstop;
486 sfc_efx_rx_qstop(struct sfc_dp_rxq *dp_rxq,
487 __rte_unused unsigned int *evq_read_ptr)
489 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
491 rxq->flags &= ~SFC_EFX_RXQ_FLAG_RUNNING;
493 /* libefx-based datapath is bound to libefx-based PMD and uses
494 * event queue structure directly. So, there is no necessity to
495 * return EvQ read pointer.
499 static sfc_dp_rx_qpurge_t sfc_efx_rx_qpurge;
501 sfc_efx_rx_qpurge(struct sfc_dp_rxq *dp_rxq)
503 struct sfc_efx_rxq *rxq = sfc_efx_rxq_by_dp_rxq(dp_rxq);
505 struct sfc_efx_rx_sw_desc *rxd;
507 for (i = rxq->completed; i != rxq->added; ++i) {
508 rxd = &rxq->sw_desc[i & rxq->ptr_mask];
509 rte_mempool_put(rxq->refill_mb_pool, rxd->mbuf);
511 /* Packed stream relies on 0 in inactive SW desc.
512 * Rx queue stop is not performance critical, so
513 * there is no harm to do it always.
519 rxq->flags &= ~SFC_EFX_RXQ_FLAG_STARTED;
522 struct sfc_dp_rx sfc_efx_rx = {
524 .name = SFC_KVARG_DATAPATH_EFX,
528 .features = SFC_DP_RX_FEAT_SCATTER,
529 .qsize_up_rings = sfc_efx_rx_qsize_up_rings,
530 .qcreate = sfc_efx_rx_qcreate,
531 .qdestroy = sfc_efx_rx_qdestroy,
532 .qstart = sfc_efx_rx_qstart,
533 .qstop = sfc_efx_rx_qstop,
534 .qpurge = sfc_efx_rx_qpurge,
535 .supported_ptypes_get = sfc_efx_supported_ptypes_get,
536 .qdesc_npending = sfc_efx_rx_qdesc_npending,
537 .qdesc_status = sfc_efx_rx_qdesc_status,
538 .pkt_burst = sfc_efx_recv_pkts,
542 sfc_rx_qdesc_npending(struct sfc_adapter *sa, unsigned int sw_index)
546 SFC_ASSERT(sw_index < sa->rxq_count);
547 rxq = sa->rxq_info[sw_index].rxq;
549 if (rxq == NULL || (rxq->state & SFC_RXQ_STARTED) == 0)
552 return sa->dp_rx->qdesc_npending(rxq->dp);
556 sfc_rx_qdesc_done(struct sfc_dp_rxq *dp_rxq, unsigned int offset)
558 struct sfc_rxq *rxq = sfc_rxq_by_dp_rxq(dp_rxq);
560 return offset < rxq->evq->sa->dp_rx->qdesc_npending(dp_rxq);
564 sfc_rx_qflush(struct sfc_adapter *sa, unsigned int sw_index)
567 unsigned int retry_count;
568 unsigned int wait_count;
571 rxq = sa->rxq_info[sw_index].rxq;
572 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
575 * Retry Rx queue flushing in the case of flush failed or
576 * timeout. In the worst case it can delay for 6 seconds.
578 for (retry_count = 0;
579 ((rxq->state & SFC_RXQ_FLUSHED) == 0) &&
580 (retry_count < SFC_RX_QFLUSH_ATTEMPTS);
582 rc = efx_rx_qflush(rxq->common);
584 rxq->state |= (rc == EALREADY) ?
585 SFC_RXQ_FLUSHED : SFC_RXQ_FLUSH_FAILED;
588 rxq->state &= ~SFC_RXQ_FLUSH_FAILED;
589 rxq->state |= SFC_RXQ_FLUSHING;
592 * Wait for Rx queue flush done or failed event at least
593 * SFC_RX_QFLUSH_POLL_WAIT_MS milliseconds and not more
594 * than 2 seconds (SFC_RX_QFLUSH_POLL_WAIT_MS multiplied
595 * by SFC_RX_QFLUSH_POLL_ATTEMPTS).
599 rte_delay_ms(SFC_RX_QFLUSH_POLL_WAIT_MS);
600 sfc_ev_qpoll(rxq->evq);
601 } while ((rxq->state & SFC_RXQ_FLUSHING) &&
602 (wait_count++ < SFC_RX_QFLUSH_POLL_ATTEMPTS));
604 if (rxq->state & SFC_RXQ_FLUSHING)
605 sfc_err(sa, "RxQ %u flush timed out", sw_index);
607 if (rxq->state & SFC_RXQ_FLUSH_FAILED)
608 sfc_err(sa, "RxQ %u flush failed", sw_index);
610 if (rxq->state & SFC_RXQ_FLUSHED)
611 sfc_info(sa, "RxQ %u flushed", sw_index);
614 sa->dp_rx->qpurge(rxq->dp);
618 sfc_rx_default_rxq_set_filter(struct sfc_adapter *sa, struct sfc_rxq *rxq)
620 boolean_t rss = (sa->rss_channels > 0) ? B_TRUE : B_FALSE;
621 struct sfc_port *port = &sa->port;
625 * If promiscuous or all-multicast mode has been requested, setting
626 * filter for the default Rx queue might fail, in particular, while
627 * running over PCI function which is not a member of corresponding
628 * privilege groups; if this occurs, few iterations will be made to
629 * repeat this step without promiscuous and all-multicast flags set
632 rc = efx_mac_filter_default_rxq_set(sa->nic, rxq->common, rss);
635 else if (rc != EOPNOTSUPP)
639 sfc_warn(sa, "promiscuous mode has been requested, "
640 "but the HW rejects it");
641 sfc_warn(sa, "promiscuous mode will be disabled");
643 port->promisc = B_FALSE;
644 rc = sfc_set_rx_mode(sa);
651 if (port->allmulti) {
652 sfc_warn(sa, "all-multicast mode has been requested, "
653 "but the HW rejects it");
654 sfc_warn(sa, "all-multicast mode will be disabled");
656 port->allmulti = B_FALSE;
657 rc = sfc_set_rx_mode(sa);
668 sfc_rx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
670 struct sfc_port *port = &sa->port;
671 struct sfc_rxq_info *rxq_info;
676 sfc_log_init(sa, "sw_index=%u", sw_index);
678 SFC_ASSERT(sw_index < sa->rxq_count);
680 rxq_info = &sa->rxq_info[sw_index];
682 SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
686 rc = sfc_ev_qstart(evq, sfc_evq_index_by_rxq_sw_index(sa, sw_index));
690 rc = efx_rx_qcreate(sa->nic, rxq->hw_index, 0, rxq_info->type,
691 &rxq->mem, rxq_info->entries,
692 0 /* not used on EF10 */, rxq_info->type_flags,
693 evq->common, &rxq->common);
695 goto fail_rx_qcreate;
697 efx_rx_qenable(rxq->common);
699 rc = sa->dp_rx->qstart(rxq->dp, evq->read_ptr);
703 rxq->state |= SFC_RXQ_STARTED;
705 if ((sw_index == 0) && !port->isolated) {
706 rc = sfc_rx_default_rxq_set_filter(sa, rxq);
708 goto fail_mac_filter_default_rxq_set;
711 /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
712 sa->eth_dev->data->rx_queue_state[sw_index] =
713 RTE_ETH_QUEUE_STATE_STARTED;
717 fail_mac_filter_default_rxq_set:
718 sa->dp_rx->qstop(rxq->dp, &rxq->evq->read_ptr);
721 sfc_rx_qflush(sa, sw_index);
731 sfc_rx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
733 struct sfc_rxq_info *rxq_info;
736 sfc_log_init(sa, "sw_index=%u", sw_index);
738 SFC_ASSERT(sw_index < sa->rxq_count);
740 rxq_info = &sa->rxq_info[sw_index];
743 if (rxq->state == SFC_RXQ_INITIALIZED)
745 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
747 /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
748 sa->eth_dev->data->rx_queue_state[sw_index] =
749 RTE_ETH_QUEUE_STATE_STOPPED;
751 sa->dp_rx->qstop(rxq->dp, &rxq->evq->read_ptr);
754 efx_mac_filter_default_rxq_clear(sa->nic);
756 sfc_rx_qflush(sa, sw_index);
758 rxq->state = SFC_RXQ_INITIALIZED;
760 efx_rx_qdestroy(rxq->common);
762 sfc_ev_qstop(rxq->evq);
766 sfc_rx_qcheck_conf(struct sfc_adapter *sa, unsigned int rxq_max_fill_level,
767 const struct rte_eth_rxconf *rx_conf)
771 if (rx_conf->rx_thresh.pthresh != 0 ||
772 rx_conf->rx_thresh.hthresh != 0 ||
773 rx_conf->rx_thresh.wthresh != 0) {
775 "RxQ prefetch/host/writeback thresholds are not supported");
778 if (rx_conf->rx_free_thresh > rxq_max_fill_level) {
780 "RxQ free threshold too large: %u vs maximum %u",
781 rx_conf->rx_free_thresh, rxq_max_fill_level);
785 if (rx_conf->rx_drop_en == 0) {
786 sfc_err(sa, "RxQ drop disable is not supported");
794 sfc_rx_mbuf_data_alignment(struct rte_mempool *mb_pool)
799 /* The mbuf object itself is always cache line aligned */
800 order = rte_bsf32(RTE_CACHE_LINE_SIZE);
802 /* Data offset from mbuf object start */
803 data_off = sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mb_pool) +
804 RTE_PKTMBUF_HEADROOM;
806 order = MIN(order, rte_bsf32(data_off));
808 return 1u << (order - 1);
812 sfc_rx_mb_pool_buf_size(struct sfc_adapter *sa, struct rte_mempool *mb_pool)
814 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
815 const uint32_t nic_align_start = MAX(1, encp->enc_rx_buf_align_start);
816 const uint32_t nic_align_end = MAX(1, encp->enc_rx_buf_align_end);
818 unsigned int buf_aligned;
819 unsigned int start_alignment;
820 unsigned int end_padding_alignment;
822 /* Below it is assumed that both alignments are power of 2 */
823 SFC_ASSERT(rte_is_power_of_2(nic_align_start));
824 SFC_ASSERT(rte_is_power_of_2(nic_align_end));
827 * mbuf is always cache line aligned, double-check
828 * that it meets rx buffer start alignment requirements.
831 /* Start from mbuf pool data room size */
832 buf_size = rte_pktmbuf_data_room_size(mb_pool);
834 /* Remove headroom */
835 if (buf_size <= RTE_PKTMBUF_HEADROOM) {
837 "RxQ mbuf pool %s object data room size %u is smaller than headroom %u",
838 mb_pool->name, buf_size, RTE_PKTMBUF_HEADROOM);
841 buf_size -= RTE_PKTMBUF_HEADROOM;
843 /* Calculate guaranteed data start alignment */
844 buf_aligned = sfc_rx_mbuf_data_alignment(mb_pool);
846 /* Reserve space for start alignment */
847 if (buf_aligned < nic_align_start) {
848 start_alignment = nic_align_start - buf_aligned;
849 if (buf_size <= start_alignment) {
851 "RxQ mbuf pool %s object data room size %u is insufficient for headroom %u and buffer start alignment %u required by NIC",
853 rte_pktmbuf_data_room_size(mb_pool),
854 RTE_PKTMBUF_HEADROOM, start_alignment);
857 buf_aligned = nic_align_start;
858 buf_size -= start_alignment;
863 /* Make sure that end padding does not write beyond the buffer */
864 if (buf_aligned < nic_align_end) {
866 * Estimate space which can be lost. If guarnteed buffer
867 * size is odd, lost space is (nic_align_end - 1). More
868 * accurate formula is below.
870 end_padding_alignment = nic_align_end -
871 MIN(buf_aligned, 1u << (rte_bsf32(buf_size) - 1));
872 if (buf_size <= end_padding_alignment) {
874 "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",
876 rte_pktmbuf_data_room_size(mb_pool),
877 RTE_PKTMBUF_HEADROOM, start_alignment,
878 end_padding_alignment);
881 buf_size -= end_padding_alignment;
884 * Start is aligned the same or better than end,
887 buf_size = P2ALIGN(buf_size, nic_align_end);
894 sfc_rx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
895 uint16_t nb_rx_desc, unsigned int socket_id,
896 const struct rte_eth_rxconf *rx_conf,
897 struct rte_mempool *mb_pool)
899 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
901 unsigned int rxq_entries;
902 unsigned int evq_entries;
903 unsigned int rxq_max_fill_level;
905 struct sfc_rxq_info *rxq_info;
908 struct sfc_dp_rx_qcreate_info info;
910 rc = sa->dp_rx->qsize_up_rings(nb_rx_desc, &rxq_entries, &evq_entries,
911 &rxq_max_fill_level);
913 goto fail_size_up_rings;
914 SFC_ASSERT(rxq_entries >= EFX_RXQ_MINNDESCS);
915 SFC_ASSERT(rxq_entries <= EFX_RXQ_MAXNDESCS);
916 SFC_ASSERT(rxq_entries >= nb_rx_desc);
917 SFC_ASSERT(rxq_max_fill_level <= nb_rx_desc);
919 rc = sfc_rx_qcheck_conf(sa, rxq_max_fill_level, rx_conf);
923 buf_size = sfc_rx_mb_pool_buf_size(sa, mb_pool);
925 sfc_err(sa, "RxQ %u mbuf pool object size is too small",
931 if ((buf_size < sa->port.pdu + encp->enc_rx_prefix_size) &&
932 !sa->eth_dev->data->dev_conf.rxmode.enable_scatter) {
933 sfc_err(sa, "Rx scatter is disabled and RxQ %u mbuf pool "
934 "object size is too small", sw_index);
935 sfc_err(sa, "RxQ %u calculated Rx buffer size is %u vs "
936 "PDU size %u plus Rx prefix %u bytes",
937 sw_index, buf_size, (unsigned int)sa->port.pdu,
938 encp->enc_rx_prefix_size);
943 SFC_ASSERT(sw_index < sa->rxq_count);
944 rxq_info = &sa->rxq_info[sw_index];
946 SFC_ASSERT(rxq_entries <= rxq_info->max_entries);
947 rxq_info->entries = rxq_entries;
948 rxq_info->type = EFX_RXQ_TYPE_DEFAULT;
949 rxq_info->type_flags =
950 sa->eth_dev->data->dev_conf.rxmode.enable_scatter ?
951 EFX_RXQ_FLAG_SCATTER : EFX_RXQ_FLAG_NONE;
953 if ((encp->enc_tunnel_encapsulations_supported != 0) &&
954 (sa->dp_rx->features & SFC_DP_RX_FEAT_TUNNELS))
955 rxq_info->type_flags |= EFX_RXQ_FLAG_INNER_CLASSES;
957 rc = sfc_ev_qinit(sa, SFC_EVQ_TYPE_RX, sw_index,
958 evq_entries, socket_id, &evq);
963 rxq = rte_zmalloc_socket("sfc-rxq", sizeof(*rxq), RTE_CACHE_LINE_SIZE,
971 rxq->hw_index = sw_index;
972 rxq->refill_threshold =
973 RTE_MAX(rx_conf->rx_free_thresh, SFC_RX_REFILL_BULK);
974 rxq->refill_mb_pool = mb_pool;
976 rc = sfc_dma_alloc(sa, "rxq", sw_index, EFX_RXQ_SIZE(rxq_info->entries),
977 socket_id, &rxq->mem);
981 memset(&info, 0, sizeof(info));
982 info.refill_mb_pool = rxq->refill_mb_pool;
983 info.max_fill_level = rxq_max_fill_level;
984 info.refill_threshold = rxq->refill_threshold;
985 info.buf_size = buf_size;
986 info.batch_max = encp->enc_rx_batch_max;
987 info.prefix_size = encp->enc_rx_prefix_size;
989 #if EFSYS_OPT_RX_SCALE
990 if (sa->hash_support == EFX_RX_HASH_AVAILABLE && sa->rss_channels > 0)
991 info.flags |= SFC_RXQ_FLAG_RSS_HASH;
994 info.rxq_entries = rxq_info->entries;
995 info.rxq_hw_ring = rxq->mem.esm_base;
996 info.evq_entries = evq_entries;
997 info.evq_hw_ring = evq->mem.esm_base;
998 info.hw_index = rxq->hw_index;
999 info.mem_bar = sa->mem_bar.esb_base;
1001 rc = sa->dp_rx->qcreate(sa->eth_dev->data->port_id, sw_index,
1002 &RTE_ETH_DEV_TO_PCI(sa->eth_dev)->addr,
1003 socket_id, &info, &rxq->dp);
1005 goto fail_dp_rx_qcreate;
1007 evq->dp_rxq = rxq->dp;
1009 rxq->state = SFC_RXQ_INITIALIZED;
1011 rxq_info->deferred_start = (rx_conf->rx_deferred_start != 0);
1016 sfc_dma_free(sa, &rxq->mem);
1019 rxq_info->rxq = NULL;
1026 rxq_info->entries = 0;
1030 sfc_log_init(sa, "failed %d", rc);
1035 sfc_rx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
1037 struct sfc_rxq_info *rxq_info;
1038 struct sfc_rxq *rxq;
1040 SFC_ASSERT(sw_index < sa->rxq_count);
1042 rxq_info = &sa->rxq_info[sw_index];
1044 rxq = rxq_info->rxq;
1045 SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
1047 sa->dp_rx->qdestroy(rxq->dp);
1050 rxq_info->rxq = NULL;
1051 rxq_info->entries = 0;
1053 sfc_dma_free(sa, &rxq->mem);
1055 sfc_ev_qfini(rxq->evq);
1061 #if EFSYS_OPT_RX_SCALE
1063 sfc_rte_to_efx_hash_type(uint64_t rss_hf)
1065 efx_rx_hash_type_t efx_hash_types = 0;
1067 if ((rss_hf & (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
1068 ETH_RSS_NONFRAG_IPV4_OTHER)) != 0)
1069 efx_hash_types |= EFX_RX_HASH_IPV4;
1071 if ((rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) != 0)
1072 efx_hash_types |= EFX_RX_HASH_TCPIPV4;
1074 if ((rss_hf & (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
1075 ETH_RSS_NONFRAG_IPV6_OTHER | ETH_RSS_IPV6_EX)) != 0)
1076 efx_hash_types |= EFX_RX_HASH_IPV6;
1078 if ((rss_hf & (ETH_RSS_NONFRAG_IPV6_TCP | ETH_RSS_IPV6_TCP_EX)) != 0)
1079 efx_hash_types |= EFX_RX_HASH_TCPIPV6;
1081 return efx_hash_types;
1085 sfc_efx_to_rte_hash_type(efx_rx_hash_type_t efx_hash_types)
1087 uint64_t rss_hf = 0;
1089 if ((efx_hash_types & EFX_RX_HASH_IPV4) != 0)
1090 rss_hf |= (ETH_RSS_IPV4 | ETH_RSS_FRAG_IPV4 |
1091 ETH_RSS_NONFRAG_IPV4_OTHER);
1093 if ((efx_hash_types & EFX_RX_HASH_TCPIPV4) != 0)
1094 rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
1096 if ((efx_hash_types & EFX_RX_HASH_IPV6) != 0)
1097 rss_hf |= (ETH_RSS_IPV6 | ETH_RSS_FRAG_IPV6 |
1098 ETH_RSS_NONFRAG_IPV6_OTHER | ETH_RSS_IPV6_EX);
1100 if ((efx_hash_types & EFX_RX_HASH_TCPIPV6) != 0)
1101 rss_hf |= (ETH_RSS_NONFRAG_IPV6_TCP | ETH_RSS_IPV6_TCP_EX);
1107 #if EFSYS_OPT_RX_SCALE
1109 sfc_rx_rss_config(struct sfc_adapter *sa)
1113 if (sa->rss_channels > 0) {
1114 rc = efx_rx_scale_mode_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1115 EFX_RX_HASHALG_TOEPLITZ,
1116 sa->rss_hash_types, B_TRUE);
1120 rc = efx_rx_scale_key_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1122 sizeof(sa->rss_key));
1126 rc = efx_rx_scale_tbl_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1127 sa->rss_tbl, RTE_DIM(sa->rss_tbl));
1135 sfc_rx_rss_config(__rte_unused struct sfc_adapter *sa)
1142 sfc_rx_start(struct sfc_adapter *sa)
1144 unsigned int sw_index;
1147 sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
1149 rc = efx_rx_init(sa->nic);
1153 rc = sfc_rx_rss_config(sa);
1155 goto fail_rss_config;
1157 for (sw_index = 0; sw_index < sa->rxq_count; ++sw_index) {
1158 if ((!sa->rxq_info[sw_index].deferred_start ||
1159 sa->rxq_info[sw_index].deferred_started)) {
1160 rc = sfc_rx_qstart(sa, sw_index);
1162 goto fail_rx_qstart;
1169 while (sw_index-- > 0)
1170 sfc_rx_qstop(sa, sw_index);
1173 efx_rx_fini(sa->nic);
1176 sfc_log_init(sa, "failed %d", rc);
1181 sfc_rx_stop(struct sfc_adapter *sa)
1183 unsigned int sw_index;
1185 sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
1187 sw_index = sa->rxq_count;
1188 while (sw_index-- > 0) {
1189 if (sa->rxq_info[sw_index].rxq != NULL)
1190 sfc_rx_qstop(sa, sw_index);
1193 efx_rx_fini(sa->nic);
1197 sfc_rx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
1199 struct sfc_rxq_info *rxq_info = &sa->rxq_info[sw_index];
1200 unsigned int max_entries;
1202 max_entries = EFX_RXQ_MAXNDESCS;
1203 SFC_ASSERT(rte_is_power_of_2(max_entries));
1205 rxq_info->max_entries = max_entries;
1211 sfc_rx_check_mode(struct sfc_adapter *sa, struct rte_eth_rxmode *rxmode)
1215 switch (rxmode->mq_mode) {
1216 case ETH_MQ_RX_NONE:
1217 /* No special checks are required */
1219 #if EFSYS_OPT_RX_SCALE
1221 if (sa->rss_support == EFX_RX_SCALE_UNAVAILABLE) {
1222 sfc_err(sa, "RSS is not available");
1228 sfc_err(sa, "Rx multi-queue mode %u not supported",
1233 if (rxmode->header_split) {
1234 sfc_err(sa, "Header split on Rx not supported");
1238 if (rxmode->hw_vlan_filter) {
1239 sfc_err(sa, "HW VLAN filtering not supported");
1243 if (rxmode->hw_vlan_strip) {
1244 sfc_err(sa, "HW VLAN stripping not supported");
1248 if (rxmode->hw_vlan_extend) {
1250 "Q-in-Q HW VLAN stripping not supported");
1254 if (!rxmode->hw_strip_crc) {
1256 "FCS stripping control not supported - always stripped");
1257 rxmode->hw_strip_crc = 1;
1260 if (rxmode->enable_scatter &&
1261 (~sa->dp_rx->features & SFC_DP_RX_FEAT_SCATTER)) {
1262 sfc_err(sa, "Rx scatter not supported by %s datapath",
1263 sa->dp_rx->dp.name);
1267 if (rxmode->enable_lro) {
1268 sfc_err(sa, "LRO not supported");
1276 * Destroy excess queues that are no longer needed after reconfiguration
1277 * or complete close.
1280 sfc_rx_fini_queues(struct sfc_adapter *sa, unsigned int nb_rx_queues)
1284 SFC_ASSERT(nb_rx_queues <= sa->rxq_count);
1286 sw_index = sa->rxq_count;
1287 while (--sw_index >= (int)nb_rx_queues) {
1288 if (sa->rxq_info[sw_index].rxq != NULL)
1289 sfc_rx_qfini(sa, sw_index);
1292 sa->rxq_count = nb_rx_queues;
1296 * Initialize Rx subsystem.
1298 * Called at device (re)configuration stage when number of receive queues is
1299 * specified together with other device level receive configuration.
1301 * It should be used to allocate NUMA-unaware resources.
1304 sfc_rx_configure(struct sfc_adapter *sa)
1306 struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
1307 const unsigned int nb_rx_queues = sa->eth_dev->data->nb_rx_queues;
1310 sfc_log_init(sa, "nb_rx_queues=%u (old %u)",
1311 nb_rx_queues, sa->rxq_count);
1313 rc = sfc_rx_check_mode(sa, &dev_conf->rxmode);
1315 goto fail_check_mode;
1317 if (nb_rx_queues == sa->rxq_count)
1320 if (sa->rxq_info == NULL) {
1322 sa->rxq_info = rte_calloc_socket("sfc-rxqs", nb_rx_queues,
1323 sizeof(sa->rxq_info[0]), 0,
1325 if (sa->rxq_info == NULL)
1326 goto fail_rxqs_alloc;
1328 struct sfc_rxq_info *new_rxq_info;
1330 if (nb_rx_queues < sa->rxq_count)
1331 sfc_rx_fini_queues(sa, nb_rx_queues);
1335 rte_realloc(sa->rxq_info,
1336 nb_rx_queues * sizeof(sa->rxq_info[0]), 0);
1337 if (new_rxq_info == NULL && nb_rx_queues > 0)
1338 goto fail_rxqs_realloc;
1340 sa->rxq_info = new_rxq_info;
1341 if (nb_rx_queues > sa->rxq_count)
1342 memset(&sa->rxq_info[sa->rxq_count], 0,
1343 (nb_rx_queues - sa->rxq_count) *
1344 sizeof(sa->rxq_info[0]));
1347 while (sa->rxq_count < nb_rx_queues) {
1348 rc = sfc_rx_qinit_info(sa, sa->rxq_count);
1350 goto fail_rx_qinit_info;
1355 #if EFSYS_OPT_RX_SCALE
1356 sa->rss_channels = (dev_conf->rxmode.mq_mode == ETH_MQ_RX_RSS) ?
1357 MIN(sa->rxq_count, EFX_MAXRSS) : 0;
1359 if (sa->rss_channels > 0) {
1360 unsigned int sw_index;
1362 for (sw_index = 0; sw_index < EFX_RSS_TBL_SIZE; ++sw_index)
1363 sa->rss_tbl[sw_index] = sw_index % sa->rss_channels;
1376 sfc_log_init(sa, "failed %d", rc);
1381 * Shutdown Rx subsystem.
1383 * Called at device close stage, for example, before device shutdown.
1386 sfc_rx_close(struct sfc_adapter *sa)
1388 sfc_rx_fini_queues(sa, 0);
1390 sa->rss_channels = 0;
1392 rte_free(sa->rxq_info);
1393 sa->rxq_info = NULL;