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.
30 #include <rte_mempool.h>
35 #include "sfc_debug.h"
39 #include "sfc_tweak.h"
42 * Maximum number of Rx queue flush attempt in the case of failure or
45 #define SFC_RX_QFLUSH_ATTEMPTS (3)
48 * Time to wait between event queue polling attempts when waiting for Rx
49 * queue flush done or failed events.
51 #define SFC_RX_QFLUSH_POLL_WAIT_MS (1)
54 * Maximum number of event queue polling attempts when waiting for Rx queue
55 * flush done or failed events. It defines Rx queue flush attempt timeout
56 * together with SFC_RX_QFLUSH_POLL_WAIT_MS.
58 #define SFC_RX_QFLUSH_POLL_ATTEMPTS (2000)
61 sfc_rx_qflush_done(struct sfc_rxq *rxq)
63 rxq->state |= SFC_RXQ_FLUSHED;
64 rxq->state &= ~SFC_RXQ_FLUSHING;
68 sfc_rx_qflush_failed(struct sfc_rxq *rxq)
70 rxq->state |= SFC_RXQ_FLUSH_FAILED;
71 rxq->state &= ~SFC_RXQ_FLUSHING;
75 sfc_rx_qrefill(struct sfc_rxq *rxq)
77 unsigned int free_space;
79 void *objs[SFC_RX_REFILL_BULK];
80 efsys_dma_addr_t addr[RTE_DIM(objs)];
81 unsigned int added = rxq->added;
84 struct sfc_rx_sw_desc *rxd;
86 uint8_t port_id = rxq->port_id;
88 free_space = EFX_RXQ_LIMIT(rxq->ptr_mask + 1) -
89 (added - rxq->completed);
90 bulks = free_space / RTE_DIM(objs);
92 id = added & rxq->ptr_mask;
94 if (rte_mempool_get_bulk(rxq->refill_mb_pool, objs,
97 * It is hardly a safe way to increment counter
98 * from different contexts, but all PMDs do it.
100 rxq->evq->sa->eth_dev->data->rx_mbuf_alloc_failed +=
105 for (i = 0; i < RTE_DIM(objs);
106 ++i, id = (id + 1) & rxq->ptr_mask) {
109 rxd = &rxq->sw_desc[id];
112 rte_mbuf_refcnt_set(m, 1);
113 m->data_off = RTE_PKTMBUF_HEADROOM;
118 addr[i] = rte_pktmbuf_mtophys(m);
121 efx_rx_qpost(rxq->common, addr, rxq->buf_size,
122 RTE_DIM(objs), rxq->completed, added);
123 added += RTE_DIM(objs);
126 /* Push doorbell if something is posted */
127 if (rxq->added != added) {
129 efx_rx_qpush(rxq->common, added, &rxq->pushed);
134 sfc_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
136 struct sfc_rxq *rxq = rx_queue;
137 unsigned int completed;
138 unsigned int prefix_size = rxq->prefix_size;
139 unsigned int done_pkts = 0;
140 boolean_t discard_next = B_FALSE;
142 if (unlikely((rxq->state & SFC_RXQ_RUNNING) == 0))
145 sfc_ev_qpoll(rxq->evq);
147 completed = rxq->completed;
148 while (completed != rxq->pending && done_pkts < nb_pkts) {
150 struct sfc_rx_sw_desc *rxd;
152 unsigned int seg_len;
153 unsigned int desc_flags;
155 id = completed++ & rxq->ptr_mask;
156 rxd = &rxq->sw_desc[id];
158 desc_flags = rxd->flags;
163 if (desc_flags & (EFX_ADDR_MISMATCH | EFX_DISCARD))
166 if (desc_flags & EFX_PKT_CONT)
169 if (desc_flags & EFX_PKT_PREFIX_LEN) {
173 rc = efx_pseudo_hdr_pkt_length_get(rxq->common,
174 rte_pktmbuf_mtod(m, uint8_t *), &tmp_size);
178 seg_len = rxd->size - prefix_size;
181 m->data_off += prefix_size;
182 rte_pktmbuf_data_len(m) = seg_len;
183 rte_pktmbuf_pkt_len(m) = seg_len;
185 m->packet_type = RTE_PTYPE_L2_ETHER;
192 discard_next = ((desc_flags & EFX_PKT_CONT) != 0);
193 rte_mempool_put(rxq->refill_mb_pool, m);
197 rxq->completed = completed;
205 sfc_rx_qpurge(struct sfc_rxq *rxq)
208 struct sfc_rx_sw_desc *rxd;
210 for (i = rxq->completed; i != rxq->added; ++i) {
211 rxd = &rxq->sw_desc[i & rxq->ptr_mask];
212 rte_mempool_put(rxq->refill_mb_pool, rxd->mbuf);
218 sfc_rx_qflush(struct sfc_adapter *sa, unsigned int sw_index)
221 unsigned int retry_count;
222 unsigned int wait_count;
224 rxq = sa->rxq_info[sw_index].rxq;
225 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
228 * Retry Rx queue flushing in the case of flush failed or
229 * timeout. In the worst case it can delay for 6 seconds.
231 for (retry_count = 0;
232 ((rxq->state & SFC_RXQ_FLUSHED) == 0) &&
233 (retry_count < SFC_RX_QFLUSH_ATTEMPTS);
235 if (efx_rx_qflush(rxq->common) != 0) {
236 rxq->state |= SFC_RXQ_FLUSH_FAILED;
239 rxq->state &= ~SFC_RXQ_FLUSH_FAILED;
240 rxq->state |= SFC_RXQ_FLUSHING;
243 * Wait for Rx queue flush done or failed event at least
244 * SFC_RX_QFLUSH_POLL_WAIT_MS milliseconds and not more
245 * than 2 seconds (SFC_RX_QFLUSH_POLL_WAIT_MS multiplied
246 * by SFC_RX_QFLUSH_POLL_ATTEMPTS).
250 rte_delay_ms(SFC_RX_QFLUSH_POLL_WAIT_MS);
251 sfc_ev_qpoll(rxq->evq);
252 } while ((rxq->state & SFC_RXQ_FLUSHING) &&
253 (wait_count++ < SFC_RX_QFLUSH_POLL_ATTEMPTS));
255 if (rxq->state & SFC_RXQ_FLUSHING)
256 sfc_err(sa, "RxQ %u flush timed out", sw_index);
258 if (rxq->state & SFC_RXQ_FLUSH_FAILED)
259 sfc_err(sa, "RxQ %u flush failed", sw_index);
261 if (rxq->state & SFC_RXQ_FLUSHED)
262 sfc_info(sa, "RxQ %u flushed", sw_index);
269 sfc_rx_qstart(struct sfc_adapter *sa, unsigned int sw_index)
271 struct sfc_rxq_info *rxq_info;
276 sfc_log_init(sa, "sw_index=%u", sw_index);
278 SFC_ASSERT(sw_index < sa->rxq_count);
280 rxq_info = &sa->rxq_info[sw_index];
282 SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
286 rc = sfc_ev_qstart(sa, evq->evq_index);
290 rc = efx_rx_qcreate(sa->nic, rxq->hw_index, 0, rxq_info->type,
291 &rxq->mem, rxq_info->entries,
292 0 /* not used on EF10 */, evq->common,
295 goto fail_rx_qcreate;
297 efx_rx_qenable(rxq->common);
299 rxq->pending = rxq->completed = rxq->added = rxq->pushed = 0;
301 rxq->state |= (SFC_RXQ_STARTED | SFC_RXQ_RUNNING);
306 rc = efx_mac_filter_default_rxq_set(sa->nic, rxq->common,
309 goto fail_mac_filter_default_rxq_set;
312 /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
313 sa->eth_dev->data->rx_queue_state[sw_index] =
314 RTE_ETH_QUEUE_STATE_STARTED;
318 fail_mac_filter_default_rxq_set:
319 sfc_rx_qflush(sa, sw_index);
322 sfc_ev_qstop(sa, evq->evq_index);
329 sfc_rx_qstop(struct sfc_adapter *sa, unsigned int sw_index)
331 struct sfc_rxq_info *rxq_info;
334 sfc_log_init(sa, "sw_index=%u", sw_index);
336 SFC_ASSERT(sw_index < sa->rxq_count);
338 rxq_info = &sa->rxq_info[sw_index];
340 SFC_ASSERT(rxq->state & SFC_RXQ_STARTED);
342 /* It seems to be used by DPDK for debug purposes only ('rte_ether') */
343 sa->eth_dev->data->rx_queue_state[sw_index] =
344 RTE_ETH_QUEUE_STATE_STOPPED;
346 rxq->state &= ~SFC_RXQ_RUNNING;
349 efx_mac_filter_default_rxq_clear(sa->nic);
351 sfc_rx_qflush(sa, sw_index);
353 rxq->state = SFC_RXQ_INITIALIZED;
355 efx_rx_qdestroy(rxq->common);
357 sfc_ev_qstop(sa, rxq->evq->evq_index);
361 sfc_rx_qcheck_conf(struct sfc_adapter *sa,
362 const struct rte_eth_rxconf *rx_conf)
366 if (rx_conf->rx_thresh.pthresh != 0 ||
367 rx_conf->rx_thresh.hthresh != 0 ||
368 rx_conf->rx_thresh.wthresh != 0) {
370 "RxQ prefetch/host/writeback thresholds are not supported");
374 if (rx_conf->rx_free_thresh != 0) {
375 sfc_err(sa, "RxQ free threshold is not supported");
379 if (rx_conf->rx_drop_en == 0) {
380 sfc_err(sa, "RxQ drop disable is not supported");
384 if (rx_conf->rx_deferred_start != 0) {
385 sfc_err(sa, "RxQ deferred start is not supported");
393 sfc_rx_mbuf_data_alignment(struct rte_mempool *mb_pool)
398 /* The mbuf object itself is always cache line aligned */
399 order = rte_bsf32(RTE_CACHE_LINE_SIZE);
401 /* Data offset from mbuf object start */
402 data_off = sizeof(struct rte_mbuf) + rte_pktmbuf_priv_size(mb_pool) +
403 RTE_PKTMBUF_HEADROOM;
405 order = MIN(order, rte_bsf32(data_off));
407 return 1u << (order - 1);
411 sfc_rx_mb_pool_buf_size(struct sfc_adapter *sa, struct rte_mempool *mb_pool)
413 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
414 const uint32_t nic_align_start = MAX(1, encp->enc_rx_buf_align_start);
415 const uint32_t nic_align_end = MAX(1, encp->enc_rx_buf_align_end);
417 unsigned int buf_aligned;
418 unsigned int start_alignment;
419 unsigned int end_padding_alignment;
421 /* Below it is assumed that both alignments are power of 2 */
422 SFC_ASSERT(rte_is_power_of_2(nic_align_start));
423 SFC_ASSERT(rte_is_power_of_2(nic_align_end));
426 * mbuf is always cache line aligned, double-check
427 * that it meets rx buffer start alignment requirements.
430 /* Start from mbuf pool data room size */
431 buf_size = rte_pktmbuf_data_room_size(mb_pool);
433 /* Remove headroom */
434 if (buf_size <= RTE_PKTMBUF_HEADROOM) {
436 "RxQ mbuf pool %s object data room size %u is smaller than headroom %u",
437 mb_pool->name, buf_size, RTE_PKTMBUF_HEADROOM);
440 buf_size -= RTE_PKTMBUF_HEADROOM;
442 /* Calculate guaranteed data start alignment */
443 buf_aligned = sfc_rx_mbuf_data_alignment(mb_pool);
445 /* Reserve space for start alignment */
446 if (buf_aligned < nic_align_start) {
447 start_alignment = nic_align_start - buf_aligned;
448 if (buf_size <= start_alignment) {
450 "RxQ mbuf pool %s object data room size %u is insufficient for headroom %u and buffer start alignment %u required by NIC",
452 rte_pktmbuf_data_room_size(mb_pool),
453 RTE_PKTMBUF_HEADROOM, start_alignment);
456 buf_aligned = nic_align_start;
457 buf_size -= start_alignment;
462 /* Make sure that end padding does not write beyond the buffer */
463 if (buf_aligned < nic_align_end) {
465 * Estimate space which can be lost. If guarnteed buffer
466 * size is odd, lost space is (nic_align_end - 1). More
467 * accurate formula is below.
469 end_padding_alignment = nic_align_end -
470 MIN(buf_aligned, 1u << (rte_bsf32(buf_size) - 1));
471 if (buf_size <= end_padding_alignment) {
473 "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",
475 rte_pktmbuf_data_room_size(mb_pool),
476 RTE_PKTMBUF_HEADROOM, start_alignment,
477 end_padding_alignment);
480 buf_size -= end_padding_alignment;
483 * Start is aligned the same or better than end,
486 buf_size = P2ALIGN(buf_size, nic_align_end);
493 sfc_rx_qinit(struct sfc_adapter *sa, unsigned int sw_index,
494 uint16_t nb_rx_desc, unsigned int socket_id,
495 const struct rte_eth_rxconf *rx_conf,
496 struct rte_mempool *mb_pool)
498 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
501 struct sfc_rxq_info *rxq_info;
502 unsigned int evq_index;
506 rc = sfc_rx_qcheck_conf(sa, rx_conf);
510 buf_size = sfc_rx_mb_pool_buf_size(sa, mb_pool);
512 sfc_err(sa, "RxQ %u mbuf pool object size is too small",
518 if ((buf_size < sa->port.pdu + encp->enc_rx_prefix_size) &&
519 !sa->eth_dev->data->dev_conf.rxmode.enable_scatter) {
520 sfc_err(sa, "Rx scatter is disabled and RxQ %u mbuf pool "
521 "object size is too small", sw_index);
522 sfc_err(sa, "RxQ %u calculated Rx buffer size is %u vs "
523 "PDU size %u plus Rx prefix %u bytes",
524 sw_index, buf_size, (unsigned int)sa->port.pdu,
525 encp->enc_rx_prefix_size);
530 SFC_ASSERT(sw_index < sa->rxq_count);
531 rxq_info = &sa->rxq_info[sw_index];
533 SFC_ASSERT(nb_rx_desc <= rxq_info->max_entries);
534 rxq_info->entries = nb_rx_desc;
535 rxq_info->type = EFX_RXQ_TYPE_DEFAULT;
537 evq_index = sfc_evq_index_by_rxq_sw_index(sa, sw_index);
539 rc = sfc_ev_qinit(sa, evq_index, rxq_info->entries, socket_id);
543 evq = sa->evq_info[evq_index].evq;
546 rxq = rte_zmalloc_socket("sfc-rxq", sizeof(*rxq), RTE_CACHE_LINE_SIZE,
551 rc = sfc_dma_alloc(sa, "rxq", sw_index, EFX_RXQ_SIZE(rxq_info->entries),
552 socket_id, &rxq->mem);
557 rxq->sw_desc = rte_calloc_socket("sfc-rxq-sw_desc", rxq_info->entries,
558 sizeof(*rxq->sw_desc),
559 RTE_CACHE_LINE_SIZE, socket_id);
560 if (rxq->sw_desc == NULL)
561 goto fail_desc_alloc;
565 rxq->ptr_mask = rxq_info->entries - 1;
566 rxq->refill_mb_pool = mb_pool;
567 rxq->buf_size = buf_size;
568 rxq->hw_index = sw_index;
569 rxq->port_id = sa->eth_dev->data->port_id;
571 /* Cache limits required on datapath in RxQ structure */
572 rxq->batch_max = encp->enc_rx_batch_max;
573 rxq->prefix_size = encp->enc_rx_prefix_size;
575 rxq->state = SFC_RXQ_INITIALIZED;
582 sfc_dma_free(sa, &rxq->mem);
588 sfc_ev_qfini(sa, evq_index);
591 rxq_info->entries = 0;
594 sfc_log_init(sa, "failed %d", rc);
599 sfc_rx_qfini(struct sfc_adapter *sa, unsigned int sw_index)
601 struct sfc_rxq_info *rxq_info;
604 SFC_ASSERT(sw_index < sa->rxq_count);
606 rxq_info = &sa->rxq_info[sw_index];
609 SFC_ASSERT(rxq->state == SFC_RXQ_INITIALIZED);
611 rxq_info->rxq = NULL;
612 rxq_info->entries = 0;
614 rte_free(rxq->sw_desc);
615 sfc_dma_free(sa, &rxq->mem);
620 sfc_rx_start(struct sfc_adapter *sa)
622 unsigned int sw_index;
625 sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
627 rc = efx_rx_init(sa->nic);
631 for (sw_index = 0; sw_index < sa->rxq_count; ++sw_index) {
632 rc = sfc_rx_qstart(sa, sw_index);
640 while (sw_index-- > 0)
641 sfc_rx_qstop(sa, sw_index);
643 efx_rx_fini(sa->nic);
646 sfc_log_init(sa, "failed %d", rc);
651 sfc_rx_stop(struct sfc_adapter *sa)
653 unsigned int sw_index;
655 sfc_log_init(sa, "rxq_count=%u", sa->rxq_count);
657 sw_index = sa->rxq_count;
658 while (sw_index-- > 0) {
659 if (sa->rxq_info[sw_index].rxq != NULL)
660 sfc_rx_qstop(sa, sw_index);
663 efx_rx_fini(sa->nic);
667 sfc_rx_qinit_info(struct sfc_adapter *sa, unsigned int sw_index)
669 struct sfc_rxq_info *rxq_info = &sa->rxq_info[sw_index];
670 unsigned int max_entries;
672 max_entries = EFX_RXQ_MAXNDESCS;
673 SFC_ASSERT(rte_is_power_of_2(max_entries));
675 rxq_info->max_entries = max_entries;
681 sfc_rx_check_mode(struct sfc_adapter *sa, struct rte_eth_rxmode *rxmode)
685 switch (rxmode->mq_mode) {
687 /* No special checks are required */
690 sfc_err(sa, "Rx multi-queue mode %u not supported",
695 if (rxmode->header_split) {
696 sfc_err(sa, "Header split on Rx not supported");
700 if (rxmode->hw_vlan_filter) {
701 sfc_err(sa, "HW VLAN filtering not supported");
705 if (rxmode->hw_vlan_strip) {
706 sfc_err(sa, "HW VLAN stripping not supported");
710 if (rxmode->hw_vlan_extend) {
712 "Q-in-Q HW VLAN stripping not supported");
716 if (!rxmode->hw_strip_crc) {
718 "FCS stripping control not supported - always stripped");
719 rxmode->hw_strip_crc = 1;
722 if (rxmode->enable_scatter) {
723 sfc_err(sa, "Scatter on Rx not supported");
727 if (rxmode->enable_lro) {
728 sfc_err(sa, "LRO not supported");
736 * Initialize Rx subsystem.
738 * Called at device configuration stage when number of receive queues is
739 * specified together with other device level receive configuration.
741 * It should be used to allocate NUMA-unaware resources.
744 sfc_rx_init(struct sfc_adapter *sa)
746 struct rte_eth_conf *dev_conf = &sa->eth_dev->data->dev_conf;
747 unsigned int sw_index;
750 rc = sfc_rx_check_mode(sa, &dev_conf->rxmode);
752 goto fail_check_mode;
754 sa->rxq_count = sa->eth_dev->data->nb_rx_queues;
757 sa->rxq_info = rte_calloc_socket("sfc-rxqs", sa->rxq_count,
758 sizeof(struct sfc_rxq_info), 0,
760 if (sa->rxq_info == NULL)
761 goto fail_rxqs_alloc;
763 for (sw_index = 0; sw_index < sa->rxq_count; ++sw_index) {
764 rc = sfc_rx_qinit_info(sa, sw_index);
766 goto fail_rx_qinit_info;
772 rte_free(sa->rxq_info);
778 sfc_log_init(sa, "failed %d", rc);
783 * Shutdown Rx subsystem.
785 * Called at device close stage, for example, before device
786 * reconfiguration or shutdown.
789 sfc_rx_fini(struct sfc_adapter *sa)
791 unsigned int sw_index;
793 sw_index = sa->rxq_count;
794 while (sw_index-- > 0) {
795 if (sa->rxq_info[sw_index].rxq != NULL)
796 sfc_rx_qfini(sa, sw_index);
799 rte_free(sa->rxq_info);