1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
13 #include <sys/queue.h>
15 #include <rte_string_fns.h>
16 #include <rte_memzone.h>
18 #include <rte_malloc.h>
19 #include <rte_ether.h>
20 #include <rte_ethdev_driver.h>
28 #include "base/iavf_prototype.h"
29 #include "base/iavf_type.h"
31 #include "iavf_rxtx.h"
34 check_rx_thresh(uint16_t nb_desc, uint16_t thresh)
36 /* The following constraints must be satisfied:
37 * thresh < rxq->nb_rx_desc
39 if (thresh >= nb_desc) {
40 PMD_INIT_LOG(ERR, "rx_free_thresh (%u) must be less than %u",
48 check_tx_thresh(uint16_t nb_desc, uint16_t tx_rs_thresh,
49 uint16_t tx_free_thresh)
51 /* TX descriptors will have their RS bit set after tx_rs_thresh
52 * descriptors have been used. The TX descriptor ring will be cleaned
53 * after tx_free_thresh descriptors are used or if the number of
54 * descriptors required to transmit a packet is greater than the
55 * number of free TX descriptors.
57 * The following constraints must be satisfied:
58 * - tx_rs_thresh must be less than the size of the ring minus 2.
59 * - tx_free_thresh must be less than the size of the ring minus 3.
60 * - tx_rs_thresh must be less than or equal to tx_free_thresh.
61 * - tx_rs_thresh must be a divisor of the ring size.
63 * One descriptor in the TX ring is used as a sentinel to avoid a H/W
64 * race condition, hence the maximum threshold constraints. When set
65 * to zero use default values.
67 if (tx_rs_thresh >= (nb_desc - 2)) {
68 PMD_INIT_LOG(ERR, "tx_rs_thresh (%u) must be less than the "
69 "number of TX descriptors (%u) minus 2",
70 tx_rs_thresh, nb_desc);
73 if (tx_free_thresh >= (nb_desc - 3)) {
74 PMD_INIT_LOG(ERR, "tx_free_thresh (%u) must be less than the "
75 "number of TX descriptors (%u) minus 3.",
76 tx_free_thresh, nb_desc);
79 if (tx_rs_thresh > tx_free_thresh) {
80 PMD_INIT_LOG(ERR, "tx_rs_thresh (%u) must be less than or "
81 "equal to tx_free_thresh (%u).",
82 tx_rs_thresh, tx_free_thresh);
85 if ((nb_desc % tx_rs_thresh) != 0) {
86 PMD_INIT_LOG(ERR, "tx_rs_thresh (%u) must be a divisor of the "
87 "number of TX descriptors (%u).",
88 tx_rs_thresh, nb_desc);
96 check_rx_vec_allow(struct iavf_rx_queue *rxq)
98 if (rxq->rx_free_thresh >= IAVF_VPMD_RX_MAX_BURST &&
99 rxq->nb_rx_desc % rxq->rx_free_thresh == 0) {
100 PMD_INIT_LOG(DEBUG, "Vector Rx can be enabled on this rxq.");
104 PMD_INIT_LOG(DEBUG, "Vector Rx cannot be enabled on this rxq.");
109 check_tx_vec_allow(struct iavf_tx_queue *txq)
111 if (!(txq->offloads & IAVF_NO_VECTOR_FLAGS) &&
112 txq->rs_thresh >= IAVF_VPMD_TX_MAX_BURST &&
113 txq->rs_thresh <= IAVF_VPMD_TX_MAX_FREE_BUF) {
114 PMD_INIT_LOG(DEBUG, "Vector tx can be enabled on this txq.");
117 PMD_INIT_LOG(DEBUG, "Vector Tx cannot be enabled on this txq.");
122 check_rx_bulk_allow(struct iavf_rx_queue *rxq)
126 if (!(rxq->rx_free_thresh >= IAVF_RX_MAX_BURST)) {
127 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
128 "rxq->rx_free_thresh=%d, "
129 "IAVF_RX_MAX_BURST=%d",
130 rxq->rx_free_thresh, IAVF_RX_MAX_BURST);
132 } else if (rxq->nb_rx_desc % rxq->rx_free_thresh != 0) {
133 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions: "
134 "rxq->nb_rx_desc=%d, "
135 "rxq->rx_free_thresh=%d",
136 rxq->nb_rx_desc, rxq->rx_free_thresh);
143 reset_rx_queue(struct iavf_rx_queue *rxq)
151 len = rxq->nb_rx_desc + IAVF_RX_MAX_BURST;
153 for (i = 0; i < len * sizeof(union iavf_rx_desc); i++)
154 ((volatile char *)rxq->rx_ring)[i] = 0;
156 memset(&rxq->fake_mbuf, 0x0, sizeof(rxq->fake_mbuf));
158 for (i = 0; i < IAVF_RX_MAX_BURST; i++)
159 rxq->sw_ring[rxq->nb_rx_desc + i] = &rxq->fake_mbuf;
162 rxq->rx_nb_avail = 0;
163 rxq->rx_next_avail = 0;
164 rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
168 rxq->pkt_first_seg = NULL;
169 rxq->pkt_last_seg = NULL;
173 reset_tx_queue(struct iavf_tx_queue *txq)
175 struct iavf_tx_entry *txe;
180 PMD_DRV_LOG(DEBUG, "Pointer to txq is NULL");
185 size = sizeof(struct iavf_tx_desc) * txq->nb_tx_desc;
186 for (i = 0; i < size; i++)
187 ((volatile char *)txq->tx_ring)[i] = 0;
189 prev = (uint16_t)(txq->nb_tx_desc - 1);
190 for (i = 0; i < txq->nb_tx_desc; i++) {
191 txq->tx_ring[i].cmd_type_offset_bsz =
192 rte_cpu_to_le_64(IAVF_TX_DESC_DTYPE_DESC_DONE);
195 txe[prev].next_id = i;
202 txq->last_desc_cleaned = txq->nb_tx_desc - 1;
203 txq->nb_free = txq->nb_tx_desc - 1;
205 txq->next_dd = txq->rs_thresh - 1;
206 txq->next_rs = txq->rs_thresh - 1;
210 alloc_rxq_mbufs(struct iavf_rx_queue *rxq)
212 volatile union iavf_rx_desc *rxd;
213 struct rte_mbuf *mbuf = NULL;
217 for (i = 0; i < rxq->nb_rx_desc; i++) {
218 mbuf = rte_mbuf_raw_alloc(rxq->mp);
219 if (unlikely(!mbuf)) {
220 PMD_DRV_LOG(ERR, "Failed to allocate mbuf for RX");
224 rte_mbuf_refcnt_set(mbuf, 1);
226 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
228 mbuf->port = rxq->port_id;
231 rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
233 rxd = &rxq->rx_ring[i];
234 rxd->read.pkt_addr = dma_addr;
235 rxd->read.hdr_addr = 0;
236 #ifndef RTE_LIBRTE_IAVF_16BYTE_RX_DESC
241 rxq->sw_ring[i] = mbuf;
248 release_rxq_mbufs(struct iavf_rx_queue *rxq)
255 for (i = 0; i < rxq->nb_rx_desc; i++) {
256 if (rxq->sw_ring[i]) {
257 rte_pktmbuf_free_seg(rxq->sw_ring[i]);
258 rxq->sw_ring[i] = NULL;
263 if (rxq->rx_nb_avail == 0)
265 for (i = 0; i < rxq->rx_nb_avail; i++) {
266 struct rte_mbuf *mbuf;
268 mbuf = rxq->rx_stage[rxq->rx_next_avail + i];
269 rte_pktmbuf_free_seg(mbuf);
271 rxq->rx_nb_avail = 0;
275 release_txq_mbufs(struct iavf_tx_queue *txq)
279 if (!txq || !txq->sw_ring) {
280 PMD_DRV_LOG(DEBUG, "Pointer to rxq or sw_ring is NULL");
284 for (i = 0; i < txq->nb_tx_desc; i++) {
285 if (txq->sw_ring[i].mbuf) {
286 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
287 txq->sw_ring[i].mbuf = NULL;
292 static const struct iavf_rxq_ops def_rxq_ops = {
293 .release_mbufs = release_rxq_mbufs,
296 static const struct iavf_txq_ops def_txq_ops = {
297 .release_mbufs = release_txq_mbufs,
301 iavf_dev_rx_queue_setup(struct rte_eth_dev *dev, uint16_t queue_idx,
302 uint16_t nb_desc, unsigned int socket_id,
303 const struct rte_eth_rxconf *rx_conf,
304 struct rte_mempool *mp)
306 struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
307 struct iavf_adapter *ad =
308 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
309 struct iavf_rx_queue *rxq;
310 const struct rte_memzone *mz;
313 uint16_t rx_free_thresh;
315 PMD_INIT_FUNC_TRACE();
317 if (nb_desc % IAVF_ALIGN_RING_DESC != 0 ||
318 nb_desc > IAVF_MAX_RING_DESC ||
319 nb_desc < IAVF_MIN_RING_DESC) {
320 PMD_INIT_LOG(ERR, "Number (%u) of receive descriptors is "
325 /* Check free threshold */
326 rx_free_thresh = (rx_conf->rx_free_thresh == 0) ?
327 IAVF_DEFAULT_RX_FREE_THRESH :
328 rx_conf->rx_free_thresh;
329 if (check_rx_thresh(nb_desc, rx_free_thresh) != 0)
332 /* Free memory if needed */
333 if (dev->data->rx_queues[queue_idx]) {
334 iavf_dev_rx_queue_release(dev->data->rx_queues[queue_idx]);
335 dev->data->rx_queues[queue_idx] = NULL;
338 /* Allocate the rx queue data structure */
339 rxq = rte_zmalloc_socket("iavf rxq",
340 sizeof(struct iavf_rx_queue),
344 PMD_INIT_LOG(ERR, "Failed to allocate memory for "
345 "rx queue data structure");
350 rxq->nb_rx_desc = nb_desc;
351 rxq->rx_free_thresh = rx_free_thresh;
352 rxq->queue_id = queue_idx;
353 rxq->port_id = dev->data->port_id;
354 rxq->crc_len = 0; /* crc stripping by default */
355 rxq->rx_deferred_start = rx_conf->rx_deferred_start;
358 len = rte_pktmbuf_data_room_size(rxq->mp) - RTE_PKTMBUF_HEADROOM;
359 rxq->rx_buf_len = RTE_ALIGN(len, (1 << IAVF_RXQ_CTX_DBUFF_SHIFT));
361 /* Allocate the software ring. */
362 len = nb_desc + IAVF_RX_MAX_BURST;
364 rte_zmalloc_socket("iavf rx sw ring",
365 sizeof(struct rte_mbuf *) * len,
369 PMD_INIT_LOG(ERR, "Failed to allocate memory for SW ring");
374 /* Allocate the maximun number of RX ring hardware descriptor with
375 * a liitle more to support bulk allocate.
377 len = IAVF_MAX_RING_DESC + IAVF_RX_MAX_BURST;
378 ring_size = RTE_ALIGN(len * sizeof(union iavf_rx_desc),
380 mz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx,
381 ring_size, IAVF_RING_BASE_ALIGN,
384 PMD_INIT_LOG(ERR, "Failed to reserve DMA memory for RX");
385 rte_free(rxq->sw_ring);
389 /* Zero all the descriptors in the ring. */
390 memset(mz->addr, 0, ring_size);
391 rxq->rx_ring_phys_addr = mz->iova;
392 rxq->rx_ring = (union iavf_rx_desc *)mz->addr;
397 dev->data->rx_queues[queue_idx] = rxq;
398 rxq->qrx_tail = hw->hw_addr + IAVF_QRX_TAIL1(rxq->queue_id);
399 rxq->ops = &def_rxq_ops;
401 if (check_rx_bulk_allow(rxq) == TRUE) {
402 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
403 "satisfied. Rx Burst Bulk Alloc function will be "
404 "used on port=%d, queue=%d.",
405 rxq->port_id, rxq->queue_id);
407 PMD_INIT_LOG(DEBUG, "Rx Burst Bulk Alloc Preconditions are "
408 "not satisfied, Scattered Rx is requested "
409 "on port=%d, queue=%d.",
410 rxq->port_id, rxq->queue_id);
411 ad->rx_bulk_alloc_allowed = false;
414 if (check_rx_vec_allow(rxq) == FALSE)
415 ad->rx_vec_allowed = false;
421 iavf_dev_tx_queue_setup(struct rte_eth_dev *dev,
424 unsigned int socket_id,
425 const struct rte_eth_txconf *tx_conf)
427 struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
428 struct iavf_tx_queue *txq;
429 const struct rte_memzone *mz;
431 uint16_t tx_rs_thresh, tx_free_thresh;
434 PMD_INIT_FUNC_TRACE();
436 offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
438 if (nb_desc % IAVF_ALIGN_RING_DESC != 0 ||
439 nb_desc > IAVF_MAX_RING_DESC ||
440 nb_desc < IAVF_MIN_RING_DESC) {
441 PMD_INIT_LOG(ERR, "Number (%u) of transmit descriptors is "
446 tx_rs_thresh = (uint16_t)((tx_conf->tx_rs_thresh) ?
447 tx_conf->tx_rs_thresh : DEFAULT_TX_RS_THRESH);
448 tx_free_thresh = (uint16_t)((tx_conf->tx_free_thresh) ?
449 tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH);
450 check_tx_thresh(nb_desc, tx_rs_thresh, tx_rs_thresh);
452 /* Free memory if needed. */
453 if (dev->data->tx_queues[queue_idx]) {
454 iavf_dev_tx_queue_release(dev->data->tx_queues[queue_idx]);
455 dev->data->tx_queues[queue_idx] = NULL;
458 /* Allocate the TX queue data structure. */
459 txq = rte_zmalloc_socket("iavf txq",
460 sizeof(struct iavf_tx_queue),
464 PMD_INIT_LOG(ERR, "Failed to allocate memory for "
465 "tx queue structure");
469 txq->nb_tx_desc = nb_desc;
470 txq->rs_thresh = tx_rs_thresh;
471 txq->free_thresh = tx_free_thresh;
472 txq->queue_id = queue_idx;
473 txq->port_id = dev->data->port_id;
474 txq->offloads = offloads;
475 txq->tx_deferred_start = tx_conf->tx_deferred_start;
477 /* Allocate software ring */
479 rte_zmalloc_socket("iavf tx sw ring",
480 sizeof(struct iavf_tx_entry) * nb_desc,
484 PMD_INIT_LOG(ERR, "Failed to allocate memory for SW TX ring");
489 /* Allocate TX hardware ring descriptors. */
490 ring_size = sizeof(struct iavf_tx_desc) * IAVF_MAX_RING_DESC;
491 ring_size = RTE_ALIGN(ring_size, IAVF_DMA_MEM_ALIGN);
492 mz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx,
493 ring_size, IAVF_RING_BASE_ALIGN,
496 PMD_INIT_LOG(ERR, "Failed to reserve DMA memory for TX");
497 rte_free(txq->sw_ring);
501 txq->tx_ring_phys_addr = mz->iova;
502 txq->tx_ring = (struct iavf_tx_desc *)mz->addr;
507 dev->data->tx_queues[queue_idx] = txq;
508 txq->qtx_tail = hw->hw_addr + IAVF_QTX_TAIL1(queue_idx);
509 txq->ops = &def_txq_ops;
511 if (check_tx_vec_allow(txq) == FALSE) {
512 struct iavf_adapter *ad =
513 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
514 ad->tx_vec_allowed = false;
521 iavf_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
523 struct iavf_adapter *adapter =
524 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
525 struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
526 struct iavf_rx_queue *rxq;
529 PMD_DRV_FUNC_TRACE();
531 if (rx_queue_id >= dev->data->nb_rx_queues)
534 rxq = dev->data->rx_queues[rx_queue_id];
536 err = alloc_rxq_mbufs(rxq);
538 PMD_DRV_LOG(ERR, "Failed to allocate RX queue mbuf");
544 /* Init the RX tail register. */
545 IAVF_PCI_REG_WRITE(rxq->qrx_tail, rxq->nb_rx_desc - 1);
546 IAVF_WRITE_FLUSH(hw);
548 /* Ready to switch the queue on */
549 err = iavf_switch_queue(adapter, rx_queue_id, TRUE, TRUE);
551 PMD_DRV_LOG(ERR, "Failed to switch RX queue %u on",
554 dev->data->rx_queue_state[rx_queue_id] =
555 RTE_ETH_QUEUE_STATE_STARTED;
561 iavf_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
563 struct iavf_adapter *adapter =
564 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
565 struct iavf_hw *hw = IAVF_DEV_PRIVATE_TO_HW(dev->data->dev_private);
566 struct iavf_tx_queue *txq;
569 PMD_DRV_FUNC_TRACE();
571 if (tx_queue_id >= dev->data->nb_tx_queues)
574 txq = dev->data->tx_queues[tx_queue_id];
576 /* Init the RX tail register. */
577 IAVF_PCI_REG_WRITE(txq->qtx_tail, 0);
578 IAVF_WRITE_FLUSH(hw);
580 /* Ready to switch the queue on */
581 err = iavf_switch_queue(adapter, tx_queue_id, FALSE, TRUE);
584 PMD_DRV_LOG(ERR, "Failed to switch TX queue %u on",
587 dev->data->tx_queue_state[tx_queue_id] =
588 RTE_ETH_QUEUE_STATE_STARTED;
594 iavf_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
596 struct iavf_adapter *adapter =
597 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
598 struct iavf_rx_queue *rxq;
601 PMD_DRV_FUNC_TRACE();
603 if (rx_queue_id >= dev->data->nb_rx_queues)
606 err = iavf_switch_queue(adapter, rx_queue_id, TRUE, FALSE);
608 PMD_DRV_LOG(ERR, "Failed to switch RX queue %u off",
613 rxq = dev->data->rx_queues[rx_queue_id];
614 rxq->ops->release_mbufs(rxq);
616 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
622 iavf_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
624 struct iavf_adapter *adapter =
625 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
626 struct iavf_tx_queue *txq;
629 PMD_DRV_FUNC_TRACE();
631 if (tx_queue_id >= dev->data->nb_tx_queues)
634 err = iavf_switch_queue(adapter, tx_queue_id, FALSE, FALSE);
636 PMD_DRV_LOG(ERR, "Failed to switch TX queue %u off",
641 txq = dev->data->tx_queues[tx_queue_id];
642 txq->ops->release_mbufs(txq);
644 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
650 iavf_dev_rx_queue_release(void *rxq)
652 struct iavf_rx_queue *q = (struct iavf_rx_queue *)rxq;
657 q->ops->release_mbufs(q);
658 rte_free(q->sw_ring);
659 rte_memzone_free(q->mz);
664 iavf_dev_tx_queue_release(void *txq)
666 struct iavf_tx_queue *q = (struct iavf_tx_queue *)txq;
671 q->ops->release_mbufs(q);
672 rte_free(q->sw_ring);
673 rte_memzone_free(q->mz);
678 iavf_stop_queues(struct rte_eth_dev *dev)
680 struct iavf_adapter *adapter =
681 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
682 struct iavf_rx_queue *rxq;
683 struct iavf_tx_queue *txq;
686 /* Stop All queues */
687 ret = iavf_disable_queues(adapter);
689 PMD_DRV_LOG(WARNING, "Fail to stop queues");
691 for (i = 0; i < dev->data->nb_tx_queues; i++) {
692 txq = dev->data->tx_queues[i];
695 txq->ops->release_mbufs(txq);
697 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
699 for (i = 0; i < dev->data->nb_rx_queues; i++) {
700 rxq = dev->data->rx_queues[i];
703 rxq->ops->release_mbufs(rxq);
705 dev->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
710 iavf_rxd_to_vlan_tci(struct rte_mbuf *mb, volatile union iavf_rx_desc *rxdp)
712 if (rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len) &
713 (1 << IAVF_RX_DESC_STATUS_L2TAG1P_SHIFT)) {
714 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
716 rte_le_to_cpu_16(rxdp->wb.qword0.lo_dword.l2tag1);
722 /* Translate the rx descriptor status and error fields to pkt flags */
723 static inline uint64_t
724 iavf_rxd_to_pkt_flags(uint64_t qword)
727 uint64_t error_bits = (qword >> IAVF_RXD_QW1_ERROR_SHIFT);
729 #define IAVF_RX_ERR_BITS 0x3f
731 /* Check if RSS_HASH */
732 flags = (((qword >> IAVF_RX_DESC_STATUS_FLTSTAT_SHIFT) &
733 IAVF_RX_DESC_FLTSTAT_RSS_HASH) ==
734 IAVF_RX_DESC_FLTSTAT_RSS_HASH) ? PKT_RX_RSS_HASH : 0;
736 if (likely((error_bits & IAVF_RX_ERR_BITS) == 0)) {
737 flags |= (PKT_RX_IP_CKSUM_GOOD | PKT_RX_L4_CKSUM_GOOD);
741 if (unlikely(error_bits & (1 << IAVF_RX_DESC_ERROR_IPE_SHIFT)))
742 flags |= PKT_RX_IP_CKSUM_BAD;
744 flags |= PKT_RX_IP_CKSUM_GOOD;
746 if (unlikely(error_bits & (1 << IAVF_RX_DESC_ERROR_L4E_SHIFT)))
747 flags |= PKT_RX_L4_CKSUM_BAD;
749 flags |= PKT_RX_L4_CKSUM_GOOD;
751 /* TODO: Oversize error bit is not processed here */
756 /* implement recv_pkts */
758 iavf_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
760 volatile union iavf_rx_desc *rx_ring;
761 volatile union iavf_rx_desc *rxdp;
762 struct iavf_rx_queue *rxq;
763 union iavf_rx_desc rxd;
764 struct rte_mbuf *rxe;
765 struct rte_eth_dev *dev;
766 struct rte_mbuf *rxm;
767 struct rte_mbuf *nmb;
771 uint16_t rx_packet_len;
772 uint16_t rx_id, nb_hold;
775 static const uint32_t ptype_tbl[UINT8_MAX + 1] __rte_cache_aligned = {
777 [1] = RTE_PTYPE_L2_ETHER,
778 /* [2] - [21] reserved */
779 [22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
781 [23] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
782 RTE_PTYPE_L4_NONFRAG,
783 [24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
786 [26] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
788 [27] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
790 [28] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
792 /* All others reserved */
798 rx_id = rxq->rx_tail;
799 rx_ring = rxq->rx_ring;
801 while (nb_rx < nb_pkts) {
802 rxdp = &rx_ring[rx_id];
803 qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
804 rx_status = (qword1 & IAVF_RXD_QW1_STATUS_MASK) >>
805 IAVF_RXD_QW1_STATUS_SHIFT;
807 /* Check the DD bit first */
808 if (!(rx_status & (1 << IAVF_RX_DESC_STATUS_DD_SHIFT)))
810 IAVF_DUMP_RX_DESC(rxq, rxdp, rx_id);
812 nmb = rte_mbuf_raw_alloc(rxq->mp);
813 if (unlikely(!nmb)) {
814 dev = &rte_eth_devices[rxq->port_id];
815 dev->data->rx_mbuf_alloc_failed++;
816 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
817 "queue_id=%u", rxq->port_id, rxq->queue_id);
823 rxe = rxq->sw_ring[rx_id];
825 if (unlikely(rx_id == rxq->nb_rx_desc))
828 /* Prefetch next mbuf */
829 rte_prefetch0(rxq->sw_ring[rx_id]);
831 /* When next RX descriptor is on a cache line boundary,
832 * prefetch the next 4 RX descriptors and next 8 pointers
835 if ((rx_id & 0x3) == 0) {
836 rte_prefetch0(&rx_ring[rx_id]);
837 rte_prefetch0(rxq->sw_ring[rx_id]);
842 rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
843 rxdp->read.hdr_addr = 0;
844 rxdp->read.pkt_addr = dma_addr;
846 rx_packet_len = ((qword1 & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >>
847 IAVF_RXD_QW1_LENGTH_PBUF_SHIFT) - rxq->crc_len;
849 rxm->data_off = RTE_PKTMBUF_HEADROOM;
850 rte_prefetch0(RTE_PTR_ADD(rxm->buf_addr, RTE_PKTMBUF_HEADROOM));
853 rxm->pkt_len = rx_packet_len;
854 rxm->data_len = rx_packet_len;
855 rxm->port = rxq->port_id;
857 iavf_rxd_to_vlan_tci(rxm, &rxd);
858 pkt_flags = iavf_rxd_to_pkt_flags(qword1);
860 ptype_tbl[(uint8_t)((qword1 &
861 IAVF_RXD_QW1_PTYPE_MASK) >> IAVF_RXD_QW1_PTYPE_SHIFT)];
863 if (pkt_flags & PKT_RX_RSS_HASH)
865 rte_le_to_cpu_32(rxd.wb.qword0.hi_dword.rss);
867 rxm->ol_flags |= pkt_flags;
869 rx_pkts[nb_rx++] = rxm;
871 rxq->rx_tail = rx_id;
873 /* If the number of free RX descriptors is greater than the RX free
874 * threshold of the queue, advance the receive tail register of queue.
875 * Update that register with the value of the last processed RX
876 * descriptor minus 1.
878 nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
879 if (nb_hold > rxq->rx_free_thresh) {
880 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
881 "nb_hold=%u nb_rx=%u",
882 rxq->port_id, rxq->queue_id,
883 rx_id, nb_hold, nb_rx);
884 rx_id = (uint16_t)((rx_id == 0) ?
885 (rxq->nb_rx_desc - 1) : (rx_id - 1));
886 IAVF_PCI_REG_WRITE(rxq->qrx_tail, rx_id);
889 rxq->nb_rx_hold = nb_hold;
894 /* implement recv_scattered_pkts */
896 iavf_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
899 struct iavf_rx_queue *rxq = rx_queue;
900 union iavf_rx_desc rxd;
901 struct rte_mbuf *rxe;
902 struct rte_mbuf *first_seg = rxq->pkt_first_seg;
903 struct rte_mbuf *last_seg = rxq->pkt_last_seg;
904 struct rte_mbuf *nmb, *rxm;
905 uint16_t rx_id = rxq->rx_tail;
906 uint16_t nb_rx = 0, nb_hold = 0, rx_packet_len;
907 struct rte_eth_dev *dev;
913 volatile union iavf_rx_desc *rx_ring = rxq->rx_ring;
914 volatile union iavf_rx_desc *rxdp;
915 static const uint32_t ptype_tbl[UINT8_MAX + 1] __rte_cache_aligned = {
917 [1] = RTE_PTYPE_L2_ETHER,
918 /* [2] - [21] reserved */
919 [22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
921 [23] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
922 RTE_PTYPE_L4_NONFRAG,
923 [24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
926 [26] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
928 [27] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
930 [28] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
932 /* All others reserved */
935 while (nb_rx < nb_pkts) {
936 rxdp = &rx_ring[rx_id];
937 qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
938 rx_status = (qword1 & IAVF_RXD_QW1_STATUS_MASK) >>
939 IAVF_RXD_QW1_STATUS_SHIFT;
941 /* Check the DD bit */
942 if (!(rx_status & (1 << IAVF_RX_DESC_STATUS_DD_SHIFT)))
944 IAVF_DUMP_RX_DESC(rxq, rxdp, rx_id);
946 nmb = rte_mbuf_raw_alloc(rxq->mp);
947 if (unlikely(!nmb)) {
948 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
949 "queue_id=%u", rxq->port_id, rxq->queue_id);
950 dev = &rte_eth_devices[rxq->port_id];
951 dev->data->rx_mbuf_alloc_failed++;
957 rxe = rxq->sw_ring[rx_id];
959 if (rx_id == rxq->nb_rx_desc)
962 /* Prefetch next mbuf */
963 rte_prefetch0(rxq->sw_ring[rx_id]);
965 /* When next RX descriptor is on a cache line boundary,
966 * prefetch the next 4 RX descriptors and next 8 pointers
969 if ((rx_id & 0x3) == 0) {
970 rte_prefetch0(&rx_ring[rx_id]);
971 rte_prefetch0(rxq->sw_ring[rx_id]);
977 rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
979 /* Set data buffer address and data length of the mbuf */
980 rxdp->read.hdr_addr = 0;
981 rxdp->read.pkt_addr = dma_addr;
982 rx_packet_len = (qword1 & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >>
983 IAVF_RXD_QW1_LENGTH_PBUF_SHIFT;
984 rxm->data_len = rx_packet_len;
985 rxm->data_off = RTE_PKTMBUF_HEADROOM;
987 /* If this is the first buffer of the received packet, set the
988 * pointer to the first mbuf of the packet and initialize its
989 * context. Otherwise, update the total length and the number
990 * of segments of the current scattered packet, and update the
991 * pointer to the last mbuf of the current packet.
995 first_seg->nb_segs = 1;
996 first_seg->pkt_len = rx_packet_len;
999 (uint16_t)(first_seg->pkt_len +
1001 first_seg->nb_segs++;
1002 last_seg->next = rxm;
1005 /* If this is not the last buffer of the received packet,
1006 * update the pointer to the last mbuf of the current scattered
1007 * packet and continue to parse the RX ring.
1009 if (!(rx_status & (1 << IAVF_RX_DESC_STATUS_EOF_SHIFT))) {
1014 /* This is the last buffer of the received packet. If the CRC
1015 * is not stripped by the hardware:
1016 * - Subtract the CRC length from the total packet length.
1017 * - If the last buffer only contains the whole CRC or a part
1018 * of it, free the mbuf associated to the last buffer. If part
1019 * of the CRC is also contained in the previous mbuf, subtract
1020 * the length of that CRC part from the data length of the
1024 if (unlikely(rxq->crc_len > 0)) {
1025 first_seg->pkt_len -= RTE_ETHER_CRC_LEN;
1026 if (rx_packet_len <= RTE_ETHER_CRC_LEN) {
1027 rte_pktmbuf_free_seg(rxm);
1028 first_seg->nb_segs--;
1029 last_seg->data_len =
1030 (uint16_t)(last_seg->data_len -
1031 (RTE_ETHER_CRC_LEN - rx_packet_len));
1032 last_seg->next = NULL;
1034 rxm->data_len = (uint16_t)(rx_packet_len -
1038 first_seg->port = rxq->port_id;
1039 first_seg->ol_flags = 0;
1040 iavf_rxd_to_vlan_tci(first_seg, &rxd);
1041 pkt_flags = iavf_rxd_to_pkt_flags(qword1);
1042 first_seg->packet_type =
1043 ptype_tbl[(uint8_t)((qword1 &
1044 IAVF_RXD_QW1_PTYPE_MASK) >> IAVF_RXD_QW1_PTYPE_SHIFT)];
1046 if (pkt_flags & PKT_RX_RSS_HASH)
1047 first_seg->hash.rss =
1048 rte_le_to_cpu_32(rxd.wb.qword0.hi_dword.rss);
1050 first_seg->ol_flags |= pkt_flags;
1052 /* Prefetch data of first segment, if configured to do so. */
1053 rte_prefetch0(RTE_PTR_ADD(first_seg->buf_addr,
1054 first_seg->data_off));
1055 rx_pkts[nb_rx++] = first_seg;
1059 /* Record index of the next RX descriptor to probe. */
1060 rxq->rx_tail = rx_id;
1061 rxq->pkt_first_seg = first_seg;
1062 rxq->pkt_last_seg = last_seg;
1064 /* If the number of free RX descriptors is greater than the RX free
1065 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1066 * register. Update the RDT with the value of the last processed RX
1067 * descriptor minus 1, to guarantee that the RDT register is never
1068 * equal to the RDH register, which creates a "full" ring situtation
1069 * from the hardware point of view.
1071 nb_hold = (uint16_t)(nb_hold + rxq->nb_rx_hold);
1072 if (nb_hold > rxq->rx_free_thresh) {
1073 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1074 "nb_hold=%u nb_rx=%u",
1075 rxq->port_id, rxq->queue_id,
1076 rx_id, nb_hold, nb_rx);
1077 rx_id = (uint16_t)(rx_id == 0 ?
1078 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1079 IAVF_PCI_REG_WRITE(rxq->qrx_tail, rx_id);
1082 rxq->nb_rx_hold = nb_hold;
1087 #define IAVF_LOOK_AHEAD 8
1089 iavf_rx_scan_hw_ring(struct iavf_rx_queue *rxq)
1091 volatile union iavf_rx_desc *rxdp;
1092 struct rte_mbuf **rxep;
1093 struct rte_mbuf *mb;
1097 int32_t s[IAVF_LOOK_AHEAD], nb_dd;
1098 int32_t i, j, nb_rx = 0;
1100 static const uint32_t ptype_tbl[UINT8_MAX + 1] __rte_cache_aligned = {
1102 [1] = RTE_PTYPE_L2_ETHER,
1103 /* [2] - [21] reserved */
1104 [22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
1106 [23] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
1107 RTE_PTYPE_L4_NONFRAG,
1108 [24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
1111 [26] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
1113 [27] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
1115 [28] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
1117 /* All others reserved */
1120 rxdp = &rxq->rx_ring[rxq->rx_tail];
1121 rxep = &rxq->sw_ring[rxq->rx_tail];
1123 qword1 = rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len);
1124 rx_status = (qword1 & IAVF_RXD_QW1_STATUS_MASK) >>
1125 IAVF_RXD_QW1_STATUS_SHIFT;
1127 /* Make sure there is at least 1 packet to receive */
1128 if (!(rx_status & (1 << IAVF_RX_DESC_STATUS_DD_SHIFT)))
1131 /* Scan LOOK_AHEAD descriptors at a time to determine which
1132 * descriptors reference packets that are ready to be received.
1134 for (i = 0; i < IAVF_RX_MAX_BURST; i += IAVF_LOOK_AHEAD,
1135 rxdp += IAVF_LOOK_AHEAD, rxep += IAVF_LOOK_AHEAD) {
1136 /* Read desc statuses backwards to avoid race condition */
1137 for (j = IAVF_LOOK_AHEAD - 1; j >= 0; j--) {
1138 qword1 = rte_le_to_cpu_64(
1139 rxdp[j].wb.qword1.status_error_len);
1140 s[j] = (qword1 & IAVF_RXD_QW1_STATUS_MASK) >>
1141 IAVF_RXD_QW1_STATUS_SHIFT;
1146 /* Compute how many status bits were set */
1147 for (j = 0, nb_dd = 0; j < IAVF_LOOK_AHEAD; j++)
1148 nb_dd += s[j] & (1 << IAVF_RX_DESC_STATUS_DD_SHIFT);
1152 /* Translate descriptor info to mbuf parameters */
1153 for (j = 0; j < nb_dd; j++) {
1154 IAVF_DUMP_RX_DESC(rxq, &rxdp[j],
1155 rxq->rx_tail + i * IAVF_LOOK_AHEAD + j);
1158 qword1 = rte_le_to_cpu_64
1159 (rxdp[j].wb.qword1.status_error_len);
1160 pkt_len = ((qword1 & IAVF_RXD_QW1_LENGTH_PBUF_MASK) >>
1161 IAVF_RXD_QW1_LENGTH_PBUF_SHIFT) - rxq->crc_len;
1162 mb->data_len = pkt_len;
1163 mb->pkt_len = pkt_len;
1165 iavf_rxd_to_vlan_tci(mb, &rxdp[j]);
1166 pkt_flags = iavf_rxd_to_pkt_flags(qword1);
1168 ptype_tbl[(uint8_t)((qword1 &
1169 IAVF_RXD_QW1_PTYPE_MASK) >>
1170 IAVF_RXD_QW1_PTYPE_SHIFT)];
1172 if (pkt_flags & PKT_RX_RSS_HASH)
1173 mb->hash.rss = rte_le_to_cpu_32(
1174 rxdp[j].wb.qword0.hi_dword.rss);
1176 mb->ol_flags |= pkt_flags;
1179 for (j = 0; j < IAVF_LOOK_AHEAD; j++)
1180 rxq->rx_stage[i + j] = rxep[j];
1182 if (nb_dd != IAVF_LOOK_AHEAD)
1186 /* Clear software ring entries */
1187 for (i = 0; i < nb_rx; i++)
1188 rxq->sw_ring[rxq->rx_tail + i] = NULL;
1193 static inline uint16_t
1194 iavf_rx_fill_from_stage(struct iavf_rx_queue *rxq,
1195 struct rte_mbuf **rx_pkts,
1199 struct rte_mbuf **stage = &rxq->rx_stage[rxq->rx_next_avail];
1201 nb_pkts = (uint16_t)RTE_MIN(nb_pkts, rxq->rx_nb_avail);
1203 for (i = 0; i < nb_pkts; i++)
1204 rx_pkts[i] = stage[i];
1206 rxq->rx_nb_avail = (uint16_t)(rxq->rx_nb_avail - nb_pkts);
1207 rxq->rx_next_avail = (uint16_t)(rxq->rx_next_avail + nb_pkts);
1213 iavf_rx_alloc_bufs(struct iavf_rx_queue *rxq)
1215 volatile union iavf_rx_desc *rxdp;
1216 struct rte_mbuf **rxep;
1217 struct rte_mbuf *mb;
1218 uint16_t alloc_idx, i;
1222 /* Allocate buffers in bulk */
1223 alloc_idx = (uint16_t)(rxq->rx_free_trigger -
1224 (rxq->rx_free_thresh - 1));
1225 rxep = &rxq->sw_ring[alloc_idx];
1226 diag = rte_mempool_get_bulk(rxq->mp, (void *)rxep,
1227 rxq->rx_free_thresh);
1228 if (unlikely(diag != 0)) {
1229 PMD_RX_LOG(ERR, "Failed to get mbufs in bulk");
1233 rxdp = &rxq->rx_ring[alloc_idx];
1234 for (i = 0; i < rxq->rx_free_thresh; i++) {
1235 if (likely(i < (rxq->rx_free_thresh - 1)))
1236 /* Prefetch next mbuf */
1237 rte_prefetch0(rxep[i + 1]);
1240 rte_mbuf_refcnt_set(mb, 1);
1242 mb->data_off = RTE_PKTMBUF_HEADROOM;
1244 mb->port = rxq->port_id;
1245 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mb));
1246 rxdp[i].read.hdr_addr = 0;
1247 rxdp[i].read.pkt_addr = dma_addr;
1250 /* Update rx tail register */
1252 IAVF_PCI_REG_WRITE_RELAXED(rxq->qrx_tail, rxq->rx_free_trigger);
1254 rxq->rx_free_trigger =
1255 (uint16_t)(rxq->rx_free_trigger + rxq->rx_free_thresh);
1256 if (rxq->rx_free_trigger >= rxq->nb_rx_desc)
1257 rxq->rx_free_trigger = (uint16_t)(rxq->rx_free_thresh - 1);
1262 static inline uint16_t
1263 rx_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1265 struct iavf_rx_queue *rxq = (struct iavf_rx_queue *)rx_queue;
1271 if (rxq->rx_nb_avail)
1272 return iavf_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1274 nb_rx = (uint16_t)iavf_rx_scan_hw_ring(rxq);
1275 rxq->rx_next_avail = 0;
1276 rxq->rx_nb_avail = nb_rx;
1277 rxq->rx_tail = (uint16_t)(rxq->rx_tail + nb_rx);
1279 if (rxq->rx_tail > rxq->rx_free_trigger) {
1280 if (iavf_rx_alloc_bufs(rxq) != 0) {
1283 /* TODO: count rx_mbuf_alloc_failed here */
1285 rxq->rx_nb_avail = 0;
1286 rxq->rx_tail = (uint16_t)(rxq->rx_tail - nb_rx);
1287 for (i = 0, j = rxq->rx_tail; i < nb_rx; i++, j++)
1288 rxq->sw_ring[j] = rxq->rx_stage[i];
1294 if (rxq->rx_tail >= rxq->nb_rx_desc)
1297 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u, nb_rx=%u",
1298 rxq->port_id, rxq->queue_id,
1299 rxq->rx_tail, nb_rx);
1301 if (rxq->rx_nb_avail)
1302 return iavf_rx_fill_from_stage(rxq, rx_pkts, nb_pkts);
1308 iavf_recv_pkts_bulk_alloc(void *rx_queue,
1309 struct rte_mbuf **rx_pkts,
1312 uint16_t nb_rx = 0, n, count;
1314 if (unlikely(nb_pkts == 0))
1317 if (likely(nb_pkts <= IAVF_RX_MAX_BURST))
1318 return rx_recv_pkts(rx_queue, rx_pkts, nb_pkts);
1321 n = RTE_MIN(nb_pkts, IAVF_RX_MAX_BURST);
1322 count = rx_recv_pkts(rx_queue, &rx_pkts[nb_rx], n);
1323 nb_rx = (uint16_t)(nb_rx + count);
1324 nb_pkts = (uint16_t)(nb_pkts - count);
1333 iavf_xmit_cleanup(struct iavf_tx_queue *txq)
1335 struct iavf_tx_entry *sw_ring = txq->sw_ring;
1336 uint16_t last_desc_cleaned = txq->last_desc_cleaned;
1337 uint16_t nb_tx_desc = txq->nb_tx_desc;
1338 uint16_t desc_to_clean_to;
1339 uint16_t nb_tx_to_clean;
1341 volatile struct iavf_tx_desc *txd = txq->tx_ring;
1343 desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->rs_thresh);
1344 if (desc_to_clean_to >= nb_tx_desc)
1345 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
1347 desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
1348 if ((txd[desc_to_clean_to].cmd_type_offset_bsz &
1349 rte_cpu_to_le_64(IAVF_TXD_QW1_DTYPE_MASK)) !=
1350 rte_cpu_to_le_64(IAVF_TX_DESC_DTYPE_DESC_DONE)) {
1351 PMD_TX_FREE_LOG(DEBUG, "TX descriptor %4u is not done "
1352 "(port=%d queue=%d)", desc_to_clean_to,
1353 txq->port_id, txq->queue_id);
1357 if (last_desc_cleaned > desc_to_clean_to)
1358 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
1361 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
1364 txd[desc_to_clean_to].cmd_type_offset_bsz = 0;
1366 txq->last_desc_cleaned = desc_to_clean_to;
1367 txq->nb_free = (uint16_t)(txq->nb_free + nb_tx_to_clean);
1372 /* Check if the context descriptor is needed for TX offloading */
1373 static inline uint16_t
1374 iavf_calc_context_desc(uint64_t flags)
1376 static uint64_t mask = PKT_TX_TCP_SEG;
1378 return (flags & mask) ? 1 : 0;
1382 iavf_txd_enable_checksum(uint64_t ol_flags,
1384 uint32_t *td_offset,
1385 union iavf_tx_offload tx_offload)
1388 *td_offset |= (tx_offload.l2_len >> 1) <<
1389 IAVF_TX_DESC_LENGTH_MACLEN_SHIFT;
1391 /* Enable L3 checksum offloads */
1392 if (ol_flags & PKT_TX_IP_CKSUM) {
1393 *td_cmd |= IAVF_TX_DESC_CMD_IIPT_IPV4_CSUM;
1394 *td_offset |= (tx_offload.l3_len >> 2) <<
1395 IAVF_TX_DESC_LENGTH_IPLEN_SHIFT;
1396 } else if (ol_flags & PKT_TX_IPV4) {
1397 *td_cmd |= IAVF_TX_DESC_CMD_IIPT_IPV4;
1398 *td_offset |= (tx_offload.l3_len >> 2) <<
1399 IAVF_TX_DESC_LENGTH_IPLEN_SHIFT;
1400 } else if (ol_flags & PKT_TX_IPV6) {
1401 *td_cmd |= IAVF_TX_DESC_CMD_IIPT_IPV6;
1402 *td_offset |= (tx_offload.l3_len >> 2) <<
1403 IAVF_TX_DESC_LENGTH_IPLEN_SHIFT;
1406 if (ol_flags & PKT_TX_TCP_SEG) {
1407 *td_cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_TCP;
1408 *td_offset |= (tx_offload.l4_len >> 2) <<
1409 IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1413 /* Enable L4 checksum offloads */
1414 switch (ol_flags & PKT_TX_L4_MASK) {
1415 case PKT_TX_TCP_CKSUM:
1416 *td_cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_TCP;
1417 *td_offset |= (sizeof(struct rte_tcp_hdr) >> 2) <<
1418 IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1420 case PKT_TX_SCTP_CKSUM:
1421 *td_cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_SCTP;
1422 *td_offset |= (sizeof(struct rte_sctp_hdr) >> 2) <<
1423 IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1425 case PKT_TX_UDP_CKSUM:
1426 *td_cmd |= IAVF_TX_DESC_CMD_L4T_EOFT_UDP;
1427 *td_offset |= (sizeof(struct rte_udp_hdr) >> 2) <<
1428 IAVF_TX_DESC_LENGTH_L4_FC_LEN_SHIFT;
1435 /* set TSO context descriptor
1436 * support IP -> L4 and IP -> IP -> L4
1438 static inline uint64_t
1439 iavf_set_tso_ctx(struct rte_mbuf *mbuf, union iavf_tx_offload tx_offload)
1441 uint64_t ctx_desc = 0;
1442 uint32_t cd_cmd, hdr_len, cd_tso_len;
1444 if (!tx_offload.l4_len) {
1445 PMD_TX_LOG(DEBUG, "L4 length set to 0");
1449 hdr_len = tx_offload.l2_len +
1453 cd_cmd = IAVF_TX_CTX_DESC_TSO;
1454 cd_tso_len = mbuf->pkt_len - hdr_len;
1455 ctx_desc |= ((uint64_t)cd_cmd << IAVF_TXD_CTX_QW1_CMD_SHIFT) |
1456 ((uint64_t)cd_tso_len << IAVF_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1457 ((uint64_t)mbuf->tso_segsz << IAVF_TXD_CTX_QW1_MSS_SHIFT);
1462 /* Construct the tx flags */
1463 static inline uint64_t
1464 iavf_build_ctob(uint32_t td_cmd, uint32_t td_offset, unsigned int size,
1467 return rte_cpu_to_le_64(IAVF_TX_DESC_DTYPE_DATA |
1468 ((uint64_t)td_cmd << IAVF_TXD_QW1_CMD_SHIFT) |
1469 ((uint64_t)td_offset <<
1470 IAVF_TXD_QW1_OFFSET_SHIFT) |
1472 IAVF_TXD_QW1_TX_BUF_SZ_SHIFT) |
1473 ((uint64_t)td_tag <<
1474 IAVF_TXD_QW1_L2TAG1_SHIFT));
1479 iavf_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1481 volatile struct iavf_tx_desc *txd;
1482 volatile struct iavf_tx_desc *txr;
1483 struct iavf_tx_queue *txq;
1484 struct iavf_tx_entry *sw_ring;
1485 struct iavf_tx_entry *txe, *txn;
1486 struct rte_mbuf *tx_pkt;
1487 struct rte_mbuf *m_seg;
1498 uint64_t buf_dma_addr;
1499 union iavf_tx_offload tx_offload = {0};
1502 sw_ring = txq->sw_ring;
1504 tx_id = txq->tx_tail;
1505 txe = &sw_ring[tx_id];
1507 /* Check if the descriptor ring needs to be cleaned. */
1508 if (txq->nb_free < txq->free_thresh)
1509 iavf_xmit_cleanup(txq);
1511 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
1516 tx_pkt = *tx_pkts++;
1517 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
1519 ol_flags = tx_pkt->ol_flags;
1520 tx_offload.l2_len = tx_pkt->l2_len;
1521 tx_offload.l3_len = tx_pkt->l3_len;
1522 tx_offload.l4_len = tx_pkt->l4_len;
1523 tx_offload.tso_segsz = tx_pkt->tso_segsz;
1525 /* Calculate the number of context descriptors needed. */
1526 nb_ctx = iavf_calc_context_desc(ol_flags);
1528 /* The number of descriptors that must be allocated for
1529 * a packet equals to the number of the segments of that
1530 * packet plus 1 context descriptor if needed.
1532 nb_used = (uint16_t)(tx_pkt->nb_segs + nb_ctx);
1533 tx_last = (uint16_t)(tx_id + nb_used - 1);
1536 if (tx_last >= txq->nb_tx_desc)
1537 tx_last = (uint16_t)(tx_last - txq->nb_tx_desc);
1539 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u"
1540 " tx_first=%u tx_last=%u",
1541 txq->port_id, txq->queue_id, tx_id, tx_last);
1543 if (nb_used > txq->nb_free) {
1544 if (iavf_xmit_cleanup(txq)) {
1549 if (unlikely(nb_used > txq->rs_thresh)) {
1550 while (nb_used > txq->nb_free) {
1551 if (iavf_xmit_cleanup(txq)) {
1560 /* Descriptor based VLAN insertion */
1561 if (ol_flags & PKT_TX_VLAN_PKT) {
1562 td_cmd |= IAVF_TX_DESC_CMD_IL2TAG1;
1563 td_tag = tx_pkt->vlan_tci;
1566 /* According to datasheet, the bit2 is reserved and must be
1571 /* Enable checksum offloading */
1572 if (ol_flags & IAVF_TX_CKSUM_OFFLOAD_MASK)
1573 iavf_txd_enable_checksum(ol_flags, &td_cmd,
1574 &td_offset, tx_offload);
1577 /* Setup TX context descriptor if required */
1578 uint64_t cd_type_cmd_tso_mss =
1579 IAVF_TX_DESC_DTYPE_CONTEXT;
1580 volatile struct iavf_tx_context_desc *ctx_txd =
1581 (volatile struct iavf_tx_context_desc *)
1584 txn = &sw_ring[txe->next_id];
1585 RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
1587 rte_pktmbuf_free_seg(txe->mbuf);
1592 if (ol_flags & PKT_TX_TCP_SEG)
1593 cd_type_cmd_tso_mss |=
1594 iavf_set_tso_ctx(tx_pkt, tx_offload);
1596 ctx_txd->type_cmd_tso_mss =
1597 rte_cpu_to_le_64(cd_type_cmd_tso_mss);
1599 IAVF_DUMP_TX_DESC(txq, &txr[tx_id], tx_id);
1600 txe->last_id = tx_last;
1601 tx_id = txe->next_id;
1608 txn = &sw_ring[txe->next_id];
1611 rte_pktmbuf_free_seg(txe->mbuf);
1614 /* Setup TX Descriptor */
1615 slen = m_seg->data_len;
1616 buf_dma_addr = rte_mbuf_data_iova(m_seg);
1617 txd->buffer_addr = rte_cpu_to_le_64(buf_dma_addr);
1618 txd->cmd_type_offset_bsz = iavf_build_ctob(td_cmd,
1623 IAVF_DUMP_TX_DESC(txq, txd, tx_id);
1624 txe->last_id = tx_last;
1625 tx_id = txe->next_id;
1627 m_seg = m_seg->next;
1630 /* The last packet data descriptor needs End Of Packet (EOP) */
1631 td_cmd |= IAVF_TX_DESC_CMD_EOP;
1632 txq->nb_used = (uint16_t)(txq->nb_used + nb_used);
1633 txq->nb_free = (uint16_t)(txq->nb_free - nb_used);
1635 if (txq->nb_used >= txq->rs_thresh) {
1636 PMD_TX_LOG(DEBUG, "Setting RS bit on TXD id="
1637 "%4u (port=%d queue=%d)",
1638 tx_last, txq->port_id, txq->queue_id);
1640 td_cmd |= IAVF_TX_DESC_CMD_RS;
1642 /* Update txq RS bit counters */
1646 txd->cmd_type_offset_bsz |=
1647 rte_cpu_to_le_64(((uint64_t)td_cmd) <<
1648 IAVF_TXD_QW1_CMD_SHIFT);
1649 IAVF_DUMP_TX_DESC(txq, txd, tx_id);
1655 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
1656 txq->port_id, txq->queue_id, tx_id, nb_tx);
1658 IAVF_PCI_REG_WRITE_RELAXED(txq->qtx_tail, tx_id);
1659 txq->tx_tail = tx_id;
1664 /* TX prep functions */
1666 iavf_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
1673 for (i = 0; i < nb_pkts; i++) {
1675 ol_flags = m->ol_flags;
1677 /* Check condition for nb_segs > IAVF_TX_MAX_MTU_SEG. */
1678 if (!(ol_flags & PKT_TX_TCP_SEG)) {
1679 if (m->nb_segs > IAVF_TX_MAX_MTU_SEG) {
1683 } else if ((m->tso_segsz < IAVF_MIN_TSO_MSS) ||
1684 (m->tso_segsz > IAVF_MAX_TSO_MSS)) {
1685 /* MSS outside the range are considered malicious */
1690 if (ol_flags & IAVF_TX_OFFLOAD_NOTSUP_MASK) {
1691 rte_errno = ENOTSUP;
1695 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1696 ret = rte_validate_tx_offload(m);
1702 ret = rte_net_intel_cksum_prepare(m);
1712 /* choose rx function*/
1714 iavf_set_rx_function(struct rte_eth_dev *dev)
1716 struct iavf_adapter *adapter =
1717 IAVF_DEV_PRIVATE_TO_ADAPTER(dev->data->dev_private);
1719 struct iavf_rx_queue *rxq;
1721 bool use_avx2 = false;
1723 if (!iavf_rx_vec_dev_check(dev)) {
1724 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1725 rxq = dev->data->rx_queues[i];
1726 (void)iavf_rxq_vec_setup(rxq);
1729 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2) == 1 ||
1730 rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) == 1)
1733 if (dev->data->scattered_rx) {
1735 "Using %sVector Scattered Rx (port %d).",
1736 use_avx2 ? "avx2 " : "",
1737 dev->data->port_id);
1738 dev->rx_pkt_burst = use_avx2 ?
1739 iavf_recv_scattered_pkts_vec_avx2 :
1740 iavf_recv_scattered_pkts_vec;
1742 PMD_DRV_LOG(DEBUG, "Using %sVector Rx (port %d).",
1743 use_avx2 ? "avx2 " : "",
1744 dev->data->port_id);
1745 dev->rx_pkt_burst = use_avx2 ?
1746 iavf_recv_pkts_vec_avx2 :
1754 if (dev->data->scattered_rx) {
1755 PMD_DRV_LOG(DEBUG, "Using a Scattered Rx callback (port=%d).",
1756 dev->data->port_id);
1757 dev->rx_pkt_burst = iavf_recv_scattered_pkts;
1758 } else if (adapter->rx_bulk_alloc_allowed) {
1759 PMD_DRV_LOG(DEBUG, "Using bulk Rx callback (port=%d).",
1760 dev->data->port_id);
1761 dev->rx_pkt_burst = iavf_recv_pkts_bulk_alloc;
1763 PMD_DRV_LOG(DEBUG, "Using Basic Rx callback (port=%d).",
1764 dev->data->port_id);
1765 dev->rx_pkt_burst = iavf_recv_pkts;
1769 /* choose tx function*/
1771 iavf_set_tx_function(struct rte_eth_dev *dev)
1774 struct iavf_tx_queue *txq;
1776 bool use_avx2 = false;
1778 if (!iavf_tx_vec_dev_check(dev)) {
1779 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1780 txq = dev->data->tx_queues[i];
1783 iavf_txq_vec_setup(txq);
1786 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2) == 1 ||
1787 rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) == 1)
1790 PMD_DRV_LOG(DEBUG, "Using %sVector Tx (port %d).",
1791 use_avx2 ? "avx2 " : "",
1792 dev->data->port_id);
1793 dev->tx_pkt_burst = use_avx2 ?
1794 iavf_xmit_pkts_vec_avx2 :
1796 dev->tx_pkt_prepare = NULL;
1802 PMD_DRV_LOG(DEBUG, "Using Basic Tx callback (port=%d).",
1803 dev->data->port_id);
1804 dev->tx_pkt_burst = iavf_xmit_pkts;
1805 dev->tx_pkt_prepare = iavf_prep_pkts;
1809 iavf_dev_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1810 struct rte_eth_rxq_info *qinfo)
1812 struct iavf_rx_queue *rxq;
1814 rxq = dev->data->rx_queues[queue_id];
1816 qinfo->mp = rxq->mp;
1817 qinfo->scattered_rx = dev->data->scattered_rx;
1818 qinfo->nb_desc = rxq->nb_rx_desc;
1820 qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
1821 qinfo->conf.rx_drop_en = TRUE;
1822 qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
1826 iavf_dev_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1827 struct rte_eth_txq_info *qinfo)
1829 struct iavf_tx_queue *txq;
1831 txq = dev->data->tx_queues[queue_id];
1833 qinfo->nb_desc = txq->nb_tx_desc;
1835 qinfo->conf.tx_free_thresh = txq->free_thresh;
1836 qinfo->conf.tx_rs_thresh = txq->rs_thresh;
1837 qinfo->conf.offloads = txq->offloads;
1838 qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
1841 /* Get the number of used descriptors of a rx queue */
1843 iavf_dev_rxq_count(struct rte_eth_dev *dev, uint16_t queue_id)
1845 #define IAVF_RXQ_SCAN_INTERVAL 4
1846 volatile union iavf_rx_desc *rxdp;
1847 struct iavf_rx_queue *rxq;
1850 rxq = dev->data->rx_queues[queue_id];
1851 rxdp = &rxq->rx_ring[rxq->rx_tail];
1852 while ((desc < rxq->nb_rx_desc) &&
1853 ((rte_le_to_cpu_64(rxdp->wb.qword1.status_error_len) &
1854 IAVF_RXD_QW1_STATUS_MASK) >> IAVF_RXD_QW1_STATUS_SHIFT) &
1855 (1 << IAVF_RX_DESC_STATUS_DD_SHIFT)) {
1856 /* Check the DD bit of a rx descriptor of each 4 in a group,
1857 * to avoid checking too frequently and downgrading performance
1860 desc += IAVF_RXQ_SCAN_INTERVAL;
1861 rxdp += IAVF_RXQ_SCAN_INTERVAL;
1862 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1863 rxdp = &(rxq->rx_ring[rxq->rx_tail +
1864 desc - rxq->nb_rx_desc]);
1871 iavf_dev_rx_desc_status(void *rx_queue, uint16_t offset)
1873 struct iavf_rx_queue *rxq = rx_queue;
1874 volatile uint64_t *status;
1878 if (unlikely(offset >= rxq->nb_rx_desc))
1881 if (offset >= rxq->nb_rx_desc - rxq->nb_rx_hold)
1882 return RTE_ETH_RX_DESC_UNAVAIL;
1884 desc = rxq->rx_tail + offset;
1885 if (desc >= rxq->nb_rx_desc)
1886 desc -= rxq->nb_rx_desc;
1888 status = &rxq->rx_ring[desc].wb.qword1.status_error_len;
1889 mask = rte_le_to_cpu_64((1ULL << IAVF_RX_DESC_STATUS_DD_SHIFT)
1890 << IAVF_RXD_QW1_STATUS_SHIFT);
1892 return RTE_ETH_RX_DESC_DONE;
1894 return RTE_ETH_RX_DESC_AVAIL;
1898 iavf_dev_tx_desc_status(void *tx_queue, uint16_t offset)
1900 struct iavf_tx_queue *txq = tx_queue;
1901 volatile uint64_t *status;
1902 uint64_t mask, expect;
1905 if (unlikely(offset >= txq->nb_tx_desc))
1908 desc = txq->tx_tail + offset;
1909 /* go to next desc that has the RS bit */
1910 desc = ((desc + txq->rs_thresh - 1) / txq->rs_thresh) *
1912 if (desc >= txq->nb_tx_desc) {
1913 desc -= txq->nb_tx_desc;
1914 if (desc >= txq->nb_tx_desc)
1915 desc -= txq->nb_tx_desc;
1918 status = &txq->tx_ring[desc].cmd_type_offset_bsz;
1919 mask = rte_le_to_cpu_64(IAVF_TXD_QW1_DTYPE_MASK);
1920 expect = rte_cpu_to_le_64(
1921 IAVF_TX_DESC_DTYPE_DESC_DONE << IAVF_TXD_QW1_DTYPE_SHIFT);
1922 if ((*status & mask) == expect)
1923 return RTE_ETH_TX_DESC_DONE;
1925 return RTE_ETH_TX_DESC_FULL;