1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
15 #include <rte_interrupts.h>
16 #include <rte_byteorder.h>
17 #include <rte_common.h>
19 #include <rte_debug.h>
21 #include <rte_memory.h>
22 #include <rte_memcpy.h>
23 #include <rte_memzone.h>
24 #include <rte_launch.h>
26 #include <rte_per_lcore.h>
27 #include <rte_lcore.h>
28 #include <rte_atomic.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_mempool.h>
31 #include <rte_malloc.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev_driver.h>
35 #include <rte_prefetch.h>
41 #include <rte_string_fns.h>
43 #include "e1000_logs.h"
44 #include "base/e1000_api.h"
45 #include "e1000_ethdev.h"
46 #include "base/e1000_osdep.h"
48 #define E1000_TXD_VLAN_SHIFT 16
50 #define E1000_RXDCTL_GRAN 0x01000000 /* RXDCTL Granularity */
52 #define E1000_TX_OFFLOAD_MASK ( \
57 #define E1000_TX_OFFLOAD_NOTSUP_MASK \
58 (PKT_TX_OFFLOAD_MASK ^ E1000_TX_OFFLOAD_MASK)
61 * Structure associated with each descriptor of the RX ring of a RX queue.
64 struct rte_mbuf *mbuf; /**< mbuf associated with RX descriptor. */
68 * Structure associated with each descriptor of the TX ring of a TX queue.
71 struct rte_mbuf *mbuf; /**< mbuf associated with TX desc, if any. */
72 uint16_t next_id; /**< Index of next descriptor in ring. */
73 uint16_t last_id; /**< Index of last scattered descriptor. */
77 * Structure associated with each RX queue.
80 struct rte_mempool *mb_pool; /**< mbuf pool to populate RX ring. */
81 volatile struct e1000_rx_desc *rx_ring; /**< RX ring virtual address. */
82 uint64_t rx_ring_phys_addr; /**< RX ring DMA address. */
83 volatile uint32_t *rdt_reg_addr; /**< RDT register address. */
84 volatile uint32_t *rdh_reg_addr; /**< RDH register address. */
85 struct em_rx_entry *sw_ring; /**< address of RX software ring. */
86 struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
87 struct rte_mbuf *pkt_last_seg; /**< Last segment of current packet. */
88 uint64_t offloads; /**< Offloads of DEV_RX_OFFLOAD_* */
89 uint16_t nb_rx_desc; /**< number of RX descriptors. */
90 uint16_t rx_tail; /**< current value of RDT register. */
91 uint16_t nb_rx_hold; /**< number of held free RX desc. */
92 uint16_t rx_free_thresh; /**< max free RX desc to hold. */
93 uint16_t queue_id; /**< RX queue index. */
94 uint16_t port_id; /**< Device port identifier. */
95 uint8_t pthresh; /**< Prefetch threshold register. */
96 uint8_t hthresh; /**< Host threshold register. */
97 uint8_t wthresh; /**< Write-back threshold register. */
98 uint8_t crc_len; /**< 0 if CRC stripped, 4 otherwise. */
102 * Hardware context number
105 EM_CTX_0 = 0, /**< CTX0 */
106 EM_CTX_NUM = 1, /**< CTX NUM */
109 /** Offload features */
110 union em_vlan_macip {
113 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
114 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
116 /**< VLAN Tag Control Identifier (CPU order). */
121 * Compare mask for vlan_macip_len.data,
122 * should be in sync with em_vlan_macip.f layout.
124 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
125 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
126 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
127 /** MAC+IP length. */
128 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
131 * Structure to check if new context need be built
134 uint64_t flags; /**< ol_flags related to context build. */
135 uint32_t cmp_mask; /**< compare mask */
136 union em_vlan_macip hdrlen; /**< L2 and L3 header lenghts */
140 * Structure associated with each TX queue.
143 volatile struct e1000_data_desc *tx_ring; /**< TX ring address */
144 uint64_t tx_ring_phys_addr; /**< TX ring DMA address. */
145 struct em_tx_entry *sw_ring; /**< virtual address of SW ring. */
146 volatile uint32_t *tdt_reg_addr; /**< Address of TDT register. */
147 uint16_t nb_tx_desc; /**< number of TX descriptors. */
148 uint16_t tx_tail; /**< Current value of TDT register. */
149 /**< Start freeing TX buffers if there are less free descriptors than
151 uint16_t tx_free_thresh;
152 /**< Number of TX descriptors to use before RS bit is set. */
153 uint16_t tx_rs_thresh;
154 /** Number of TX descriptors used since RS bit was set. */
156 /** Index to last TX descriptor to have been cleaned. */
157 uint16_t last_desc_cleaned;
158 /** Total number of TX descriptors ready to be allocated. */
160 uint16_t queue_id; /**< TX queue index. */
161 uint16_t port_id; /**< Device port identifier. */
162 uint8_t pthresh; /**< Prefetch threshold register. */
163 uint8_t hthresh; /**< Host threshold register. */
164 uint8_t wthresh; /**< Write-back threshold register. */
165 struct em_ctx_info ctx_cache;
166 /**< Hardware context history.*/
167 uint64_t offloads; /**< offloads of DEV_TX_OFFLOAD_* */
171 #define RTE_PMD_USE_PREFETCH
174 #ifdef RTE_PMD_USE_PREFETCH
175 #define rte_em_prefetch(p) rte_prefetch0(p)
177 #define rte_em_prefetch(p) do {} while(0)
180 #ifdef RTE_PMD_PACKET_PREFETCH
181 #define rte_packet_prefetch(p) rte_prefetch1(p)
183 #define rte_packet_prefetch(p) do {} while(0)
186 #ifndef DEFAULT_TX_FREE_THRESH
187 #define DEFAULT_TX_FREE_THRESH 32
188 #endif /* DEFAULT_TX_FREE_THRESH */
190 #ifndef DEFAULT_TX_RS_THRESH
191 #define DEFAULT_TX_RS_THRESH 32
192 #endif /* DEFAULT_TX_RS_THRESH */
195 /*********************************************************************
199 **********************************************************************/
202 * Populates TX context descriptor.
205 em_set_xmit_ctx(struct em_tx_queue* txq,
206 volatile struct e1000_context_desc *ctx_txd,
208 union em_vlan_macip hdrlen)
210 uint32_t cmp_mask, cmd_len;
211 uint16_t ipcse, l2len;
212 struct e1000_context_desc ctx;
215 cmd_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_C;
217 l2len = hdrlen.f.l2_len;
218 ipcse = (uint16_t)(l2len + hdrlen.f.l3_len);
220 /* setup IPCS* fields */
221 ctx.lower_setup.ip_fields.ipcss = (uint8_t)l2len;
222 ctx.lower_setup.ip_fields.ipcso = (uint8_t)(l2len +
223 offsetof(struct ipv4_hdr, hdr_checksum));
226 * When doing checksum or TCP segmentation with IPv6 headers,
227 * IPCSE field should be set t0 0.
229 if (flags & PKT_TX_IP_CKSUM) {
230 ctx.lower_setup.ip_fields.ipcse =
231 (uint16_t)rte_cpu_to_le_16(ipcse - 1);
232 cmd_len |= E1000_TXD_CMD_IP;
233 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
235 ctx.lower_setup.ip_fields.ipcse = 0;
238 /* setup TUCS* fields */
239 ctx.upper_setup.tcp_fields.tucss = (uint8_t)ipcse;
240 ctx.upper_setup.tcp_fields.tucse = 0;
242 switch (flags & PKT_TX_L4_MASK) {
243 case PKT_TX_UDP_CKSUM:
244 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
245 offsetof(struct udp_hdr, dgram_cksum));
246 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
248 case PKT_TX_TCP_CKSUM:
249 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
250 offsetof(struct tcp_hdr, cksum));
251 cmd_len |= E1000_TXD_CMD_TCP;
252 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
255 ctx.upper_setup.tcp_fields.tucso = 0;
258 ctx.cmd_and_length = rte_cpu_to_le_32(cmd_len);
259 ctx.tcp_seg_setup.data = 0;
263 txq->ctx_cache.flags = flags;
264 txq->ctx_cache.cmp_mask = cmp_mask;
265 txq->ctx_cache.hdrlen = hdrlen;
269 * Check which hardware context can be used. Use the existing match
270 * or create a new context descriptor.
272 static inline uint32_t
273 what_ctx_update(struct em_tx_queue *txq, uint64_t flags,
274 union em_vlan_macip hdrlen)
276 /* If match with the current context */
277 if (likely (txq->ctx_cache.flags == flags &&
278 ((txq->ctx_cache.hdrlen.data ^ hdrlen.data) &
279 txq->ctx_cache.cmp_mask) == 0))
286 /* Reset transmit descriptors after they have been used */
288 em_xmit_cleanup(struct em_tx_queue *txq)
290 struct em_tx_entry *sw_ring = txq->sw_ring;
291 volatile struct e1000_data_desc *txr = txq->tx_ring;
292 uint16_t last_desc_cleaned = txq->last_desc_cleaned;
293 uint16_t nb_tx_desc = txq->nb_tx_desc;
294 uint16_t desc_to_clean_to;
295 uint16_t nb_tx_to_clean;
297 /* Determine the last descriptor needing to be cleaned */
298 desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_rs_thresh);
299 if (desc_to_clean_to >= nb_tx_desc)
300 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
302 /* Check to make sure the last descriptor to clean is done */
303 desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
304 if (! (txr[desc_to_clean_to].upper.fields.status & E1000_TXD_STAT_DD))
306 PMD_TX_FREE_LOG(DEBUG,
307 "TX descriptor %4u is not done"
308 "(port=%d queue=%d)", desc_to_clean_to,
309 txq->port_id, txq->queue_id);
310 /* Failed to clean any descriptors, better luck next time */
314 /* Figure out how many descriptors will be cleaned */
315 if (last_desc_cleaned > desc_to_clean_to)
316 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
319 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
322 PMD_TX_FREE_LOG(DEBUG,
323 "Cleaning %4u TX descriptors: %4u to %4u "
324 "(port=%d queue=%d)", nb_tx_to_clean,
325 last_desc_cleaned, desc_to_clean_to, txq->port_id,
329 * The last descriptor to clean is done, so that means all the
330 * descriptors from the last descriptor that was cleaned
331 * up to the last descriptor with the RS bit set
332 * are done. Only reset the threshold descriptor.
334 txr[desc_to_clean_to].upper.fields.status = 0;
336 /* Update the txq to reflect the last descriptor that was cleaned */
337 txq->last_desc_cleaned = desc_to_clean_to;
338 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
344 static inline uint32_t
345 tx_desc_cksum_flags_to_upper(uint64_t ol_flags)
347 static const uint32_t l4_olinfo[2] = {0, E1000_TXD_POPTS_TXSM << 8};
348 static const uint32_t l3_olinfo[2] = {0, E1000_TXD_POPTS_IXSM << 8};
351 tmp = l4_olinfo[(ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM];
352 tmp |= l3_olinfo[(ol_flags & PKT_TX_IP_CKSUM) != 0];
357 eth_em_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
360 struct em_tx_queue *txq;
361 struct em_tx_entry *sw_ring;
362 struct em_tx_entry *txe, *txn;
363 volatile struct e1000_data_desc *txr;
364 volatile struct e1000_data_desc *txd;
365 struct rte_mbuf *tx_pkt;
366 struct rte_mbuf *m_seg;
367 uint64_t buf_dma_addr;
369 uint32_t cmd_type_len;
379 union em_vlan_macip hdrlen;
382 sw_ring = txq->sw_ring;
384 tx_id = txq->tx_tail;
385 txe = &sw_ring[tx_id];
387 /* Determine if the descriptor ring needs to be cleaned. */
388 if (txq->nb_tx_free < txq->tx_free_thresh)
389 em_xmit_cleanup(txq);
392 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
396 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
399 * Determine how many (if any) context descriptors
400 * are needed for offload functionality.
402 ol_flags = tx_pkt->ol_flags;
404 /* If hardware offload required */
405 tx_ol_req = (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK));
407 hdrlen.f.vlan_tci = tx_pkt->vlan_tci;
408 hdrlen.f.l2_len = tx_pkt->l2_len;
409 hdrlen.f.l3_len = tx_pkt->l3_len;
410 /* If new context to be built or reuse the exist ctx. */
411 ctx = what_ctx_update(txq, tx_ol_req, hdrlen);
413 /* Only allocate context descriptor if required*/
414 new_ctx = (ctx == EM_CTX_NUM);
418 * Keep track of how many descriptors are used this loop
419 * This will always be the number of segments + the number of
420 * Context descriptors required to transmit the packet
422 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
425 * The number of descriptors that must be allocated for a
426 * packet is the number of segments of that packet, plus 1
427 * Context Descriptor for the hardware offload, if any.
428 * Determine the last TX descriptor to allocate in the TX ring
429 * for the packet, starting from the current position (tx_id)
432 tx_last = (uint16_t) (tx_id + nb_used - 1);
435 if (tx_last >= txq->nb_tx_desc)
436 tx_last = (uint16_t) (tx_last - txq->nb_tx_desc);
438 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
439 " tx_first=%u tx_last=%u",
440 (unsigned) txq->port_id,
441 (unsigned) txq->queue_id,
442 (unsigned) tx_pkt->pkt_len,
447 * Make sure there are enough TX descriptors available to
448 * transmit the entire packet.
449 * nb_used better be less than or equal to txq->tx_rs_thresh
451 while (unlikely (nb_used > txq->nb_tx_free)) {
452 PMD_TX_FREE_LOG(DEBUG, "Not enough free TX descriptors "
453 "nb_used=%4u nb_free=%4u "
454 "(port=%d queue=%d)",
455 nb_used, txq->nb_tx_free,
456 txq->port_id, txq->queue_id);
458 if (em_xmit_cleanup(txq) != 0) {
459 /* Could not clean any descriptors */
467 * By now there are enough free TX descriptors to transmit
472 * Set common flags of all TX Data Descriptors.
474 * The following bits must be set in all Data Descriptors:
475 * - E1000_TXD_DTYP_DATA
476 * - E1000_TXD_DTYP_DEXT
478 * The following bits must be set in the first Data Descriptor
479 * and are ignored in the other ones:
480 * - E1000_TXD_POPTS_IXSM
481 * - E1000_TXD_POPTS_TXSM
483 * The following bits must be set in the last Data Descriptor
484 * and are ignored in the other ones:
485 * - E1000_TXD_CMD_VLE
486 * - E1000_TXD_CMD_IFCS
488 * The following bits must only be set in the last Data
490 * - E1000_TXD_CMD_EOP
492 * The following bits can be set in any Data Descriptor, but
493 * are only set in the last Data Descriptor:
496 cmd_type_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
500 /* Set VLAN Tag offload fields. */
501 if (ol_flags & PKT_TX_VLAN_PKT) {
502 cmd_type_len |= E1000_TXD_CMD_VLE;
503 popts_spec = tx_pkt->vlan_tci << E1000_TXD_VLAN_SHIFT;
508 * Setup the TX Context Descriptor if required
511 volatile struct e1000_context_desc *ctx_txd;
513 ctx_txd = (volatile struct e1000_context_desc *)
516 txn = &sw_ring[txe->next_id];
517 RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
519 if (txe->mbuf != NULL) {
520 rte_pktmbuf_free_seg(txe->mbuf);
524 em_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
527 txe->last_id = tx_last;
528 tx_id = txe->next_id;
533 * Setup the TX Data Descriptor,
534 * This path will go through
535 * whatever new/reuse the context descriptor
537 popts_spec |= tx_desc_cksum_flags_to_upper(ol_flags);
543 txn = &sw_ring[txe->next_id];
545 if (txe->mbuf != NULL)
546 rte_pktmbuf_free_seg(txe->mbuf);
550 * Set up Transmit Data Descriptor.
552 slen = m_seg->data_len;
553 buf_dma_addr = rte_mbuf_data_iova(m_seg);
555 txd->buffer_addr = rte_cpu_to_le_64(buf_dma_addr);
556 txd->lower.data = rte_cpu_to_le_32(cmd_type_len | slen);
557 txd->upper.data = rte_cpu_to_le_32(popts_spec);
559 txe->last_id = tx_last;
560 tx_id = txe->next_id;
563 } while (m_seg != NULL);
566 * The last packet data descriptor needs End Of Packet (EOP)
568 cmd_type_len |= E1000_TXD_CMD_EOP;
569 txq->nb_tx_used = (uint16_t)(txq->nb_tx_used + nb_used);
570 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
572 /* Set RS bit only on threshold packets' last descriptor */
573 if (txq->nb_tx_used >= txq->tx_rs_thresh) {
574 PMD_TX_FREE_LOG(DEBUG,
575 "Setting RS bit on TXD id=%4u "
576 "(port=%d queue=%d)",
577 tx_last, txq->port_id, txq->queue_id);
579 cmd_type_len |= E1000_TXD_CMD_RS;
581 /* Update txq RS bit counters */
584 txd->lower.data |= rte_cpu_to_le_32(cmd_type_len);
590 * Set the Transmit Descriptor Tail (TDT)
592 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
593 (unsigned) txq->port_id, (unsigned) txq->queue_id,
594 (unsigned) tx_id, (unsigned) nb_tx);
595 E1000_PCI_REG_WRITE_RELAXED(txq->tdt_reg_addr, tx_id);
596 txq->tx_tail = tx_id;
601 /*********************************************************************
605 **********************************************************************/
607 eth_em_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
613 for (i = 0; i < nb_pkts; i++) {
616 if (m->ol_flags & E1000_TX_OFFLOAD_NOTSUP_MASK) {
617 rte_errno = -ENOTSUP;
621 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
622 ret = rte_validate_tx_offload(m);
628 ret = rte_net_intel_cksum_prepare(m);
638 /*********************************************************************
642 **********************************************************************/
644 static inline uint64_t
645 rx_desc_status_to_pkt_flags(uint32_t rx_status)
649 /* Check if VLAN present */
650 pkt_flags = ((rx_status & E1000_RXD_STAT_VP) ?
651 PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED : 0);
656 static inline uint64_t
657 rx_desc_error_to_pkt_flags(uint32_t rx_error)
659 uint64_t pkt_flags = 0;
661 if (rx_error & E1000_RXD_ERR_IPE)
662 pkt_flags |= PKT_RX_IP_CKSUM_BAD;
663 if (rx_error & E1000_RXD_ERR_TCPE)
664 pkt_flags |= PKT_RX_L4_CKSUM_BAD;
669 eth_em_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
672 volatile struct e1000_rx_desc *rx_ring;
673 volatile struct e1000_rx_desc *rxdp;
674 struct em_rx_queue *rxq;
675 struct em_rx_entry *sw_ring;
676 struct em_rx_entry *rxe;
677 struct rte_mbuf *rxm;
678 struct rte_mbuf *nmb;
679 struct e1000_rx_desc rxd;
691 rx_id = rxq->rx_tail;
692 rx_ring = rxq->rx_ring;
693 sw_ring = rxq->sw_ring;
694 while (nb_rx < nb_pkts) {
696 * The order of operations here is important as the DD status
697 * bit must not be read after any other descriptor fields.
698 * rx_ring and rxdp are pointing to volatile data so the order
699 * of accesses cannot be reordered by the compiler. If they were
700 * not volatile, they could be reordered which could lead to
701 * using invalid descriptor fields when read from rxd.
703 rxdp = &rx_ring[rx_id];
704 status = rxdp->status;
705 if (! (status & E1000_RXD_STAT_DD))
712 * If the E1000_RXD_STAT_EOP flag is not set, the RX packet is
713 * likely to be invalid and to be dropped by the various
714 * validation checks performed by the network stack.
716 * Allocate a new mbuf to replenish the RX ring descriptor.
717 * If the allocation fails:
718 * - arrange for that RX descriptor to be the first one
719 * being parsed the next time the receive function is
720 * invoked [on the same queue].
722 * - Stop parsing the RX ring and return immediately.
724 * This policy do not drop the packet received in the RX
725 * descriptor for which the allocation of a new mbuf failed.
726 * Thus, it allows that packet to be later retrieved if
727 * mbuf have been freed in the mean time.
728 * As a side effect, holding RX descriptors instead of
729 * systematically giving them back to the NIC may lead to
730 * RX ring exhaustion situations.
731 * However, the NIC can gracefully prevent such situations
732 * to happen by sending specific "back-pressure" flow control
733 * frames to its peer(s).
735 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
736 "status=0x%x pkt_len=%u",
737 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
738 (unsigned) rx_id, (unsigned) status,
739 (unsigned) rte_le_to_cpu_16(rxd.length));
741 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
743 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
745 (unsigned) rxq->port_id,
746 (unsigned) rxq->queue_id);
747 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
752 rxe = &sw_ring[rx_id];
754 if (rx_id == rxq->nb_rx_desc)
757 /* Prefetch next mbuf while processing current one. */
758 rte_em_prefetch(sw_ring[rx_id].mbuf);
761 * When next RX descriptor is on a cache-line boundary,
762 * prefetch the next 4 RX descriptors and the next 8 pointers
765 if ((rx_id & 0x3) == 0) {
766 rte_em_prefetch(&rx_ring[rx_id]);
767 rte_em_prefetch(&sw_ring[rx_id]);
770 /* Rearm RXD: attach new mbuf and reset status to zero. */
775 rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
776 rxdp->buffer_addr = dma_addr;
780 * Initialize the returned mbuf.
781 * 1) setup generic mbuf fields:
782 * - number of segments,
785 * - RX port identifier.
786 * 2) integrate hardware offload data, if any:
788 * - IP checksum flag,
789 * - VLAN TCI, if any,
792 pkt_len = (uint16_t) (rte_le_to_cpu_16(rxd.length) -
794 rxm->data_off = RTE_PKTMBUF_HEADROOM;
795 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
798 rxm->pkt_len = pkt_len;
799 rxm->data_len = pkt_len;
800 rxm->port = rxq->port_id;
802 rxm->ol_flags = rx_desc_status_to_pkt_flags(status);
803 rxm->ol_flags = rxm->ol_flags |
804 rx_desc_error_to_pkt_flags(rxd.errors);
806 /* Only valid if PKT_RX_VLAN set in pkt_flags */
807 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
810 * Store the mbuf address into the next entry of the array
811 * of returned packets.
813 rx_pkts[nb_rx++] = rxm;
815 rxq->rx_tail = rx_id;
818 * If the number of free RX descriptors is greater than the RX free
819 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
821 * Update the RDT with the value of the last processed RX descriptor
822 * minus 1, to guarantee that the RDT register is never equal to the
823 * RDH register, which creates a "full" ring situtation from the
824 * hardware point of view...
826 nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
827 if (nb_hold > rxq->rx_free_thresh) {
828 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
829 "nb_hold=%u nb_rx=%u",
830 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
831 (unsigned) rx_id, (unsigned) nb_hold,
833 rx_id = (uint16_t) ((rx_id == 0) ?
834 (rxq->nb_rx_desc - 1) : (rx_id - 1));
835 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
838 rxq->nb_rx_hold = nb_hold;
843 eth_em_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
846 struct em_rx_queue *rxq;
847 volatile struct e1000_rx_desc *rx_ring;
848 volatile struct e1000_rx_desc *rxdp;
849 struct em_rx_entry *sw_ring;
850 struct em_rx_entry *rxe;
851 struct rte_mbuf *first_seg;
852 struct rte_mbuf *last_seg;
853 struct rte_mbuf *rxm;
854 struct rte_mbuf *nmb;
855 struct e1000_rx_desc rxd;
856 uint64_t dma; /* Physical address of mbuf data buffer */
867 rx_id = rxq->rx_tail;
868 rx_ring = rxq->rx_ring;
869 sw_ring = rxq->sw_ring;
872 * Retrieve RX context of current packet, if any.
874 first_seg = rxq->pkt_first_seg;
875 last_seg = rxq->pkt_last_seg;
877 while (nb_rx < nb_pkts) {
880 * The order of operations here is important as the DD status
881 * bit must not be read after any other descriptor fields.
882 * rx_ring and rxdp are pointing to volatile data so the order
883 * of accesses cannot be reordered by the compiler. If they were
884 * not volatile, they could be reordered which could lead to
885 * using invalid descriptor fields when read from rxd.
887 rxdp = &rx_ring[rx_id];
888 status = rxdp->status;
889 if (! (status & E1000_RXD_STAT_DD))
896 * Allocate a new mbuf to replenish the RX ring descriptor.
897 * If the allocation fails:
898 * - arrange for that RX descriptor to be the first one
899 * being parsed the next time the receive function is
900 * invoked [on the same queue].
902 * - Stop parsing the RX ring and return immediately.
904 * This policy does not drop the packet received in the RX
905 * descriptor for which the allocation of a new mbuf failed.
906 * Thus, it allows that packet to be later retrieved if
907 * mbuf have been freed in the mean time.
908 * As a side effect, holding RX descriptors instead of
909 * systematically giving them back to the NIC may lead to
910 * RX ring exhaustion situations.
911 * However, the NIC can gracefully prevent such situations
912 * to happen by sending specific "back-pressure" flow control
913 * frames to its peer(s).
915 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
916 "status=0x%x data_len=%u",
917 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
918 (unsigned) rx_id, (unsigned) status,
919 (unsigned) rte_le_to_cpu_16(rxd.length));
921 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
923 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
924 "queue_id=%u", (unsigned) rxq->port_id,
925 (unsigned) rxq->queue_id);
926 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
931 rxe = &sw_ring[rx_id];
933 if (rx_id == rxq->nb_rx_desc)
936 /* Prefetch next mbuf while processing current one. */
937 rte_em_prefetch(sw_ring[rx_id].mbuf);
940 * When next RX descriptor is on a cache-line boundary,
941 * prefetch the next 4 RX descriptors and the next 8 pointers
944 if ((rx_id & 0x3) == 0) {
945 rte_em_prefetch(&rx_ring[rx_id]);
946 rte_em_prefetch(&sw_ring[rx_id]);
950 * Update RX descriptor with the physical address of the new
951 * data buffer of the new allocated mbuf.
955 dma = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
956 rxdp->buffer_addr = dma;
960 * Set data length & data buffer address of mbuf.
962 data_len = rte_le_to_cpu_16(rxd.length);
963 rxm->data_len = data_len;
964 rxm->data_off = RTE_PKTMBUF_HEADROOM;
967 * If this is the first buffer of the received packet,
968 * set the pointer to the first mbuf of the packet and
969 * initialize its context.
970 * Otherwise, update the total length and the number of segments
971 * of the current scattered packet, and update the pointer to
972 * the last mbuf of the current packet.
974 if (first_seg == NULL) {
976 first_seg->pkt_len = data_len;
977 first_seg->nb_segs = 1;
979 first_seg->pkt_len += data_len;
980 first_seg->nb_segs++;
981 last_seg->next = rxm;
985 * If this is not the last buffer of the received packet,
986 * update the pointer to the last mbuf of the current scattered
987 * packet and continue to parse the RX ring.
989 if (! (status & E1000_RXD_STAT_EOP)) {
995 * This is the last buffer of the received packet.
996 * If the CRC is not stripped by the hardware:
997 * - Subtract the CRC length from the total packet length.
998 * - If the last buffer only contains the whole CRC or a part
999 * of it, free the mbuf associated to the last buffer.
1000 * If part of the CRC is also contained in the previous
1001 * mbuf, subtract the length of that CRC part from the
1002 * data length of the previous mbuf.
1005 if (unlikely(rxq->crc_len > 0)) {
1006 first_seg->pkt_len -= ETHER_CRC_LEN;
1007 if (data_len <= ETHER_CRC_LEN) {
1008 rte_pktmbuf_free_seg(rxm);
1009 first_seg->nb_segs--;
1010 last_seg->data_len = (uint16_t)
1011 (last_seg->data_len -
1012 (ETHER_CRC_LEN - data_len));
1013 last_seg->next = NULL;
1016 (uint16_t) (data_len - ETHER_CRC_LEN);
1020 * Initialize the first mbuf of the returned packet:
1021 * - RX port identifier,
1022 * - hardware offload data, if any:
1023 * - IP checksum flag,
1026 first_seg->port = rxq->port_id;
1028 first_seg->ol_flags = rx_desc_status_to_pkt_flags(status);
1029 first_seg->ol_flags = first_seg->ol_flags |
1030 rx_desc_error_to_pkt_flags(rxd.errors);
1032 /* Only valid if PKT_RX_VLAN set in pkt_flags */
1033 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
1035 /* Prefetch data of first segment, if configured to do so. */
1036 rte_packet_prefetch((char *)first_seg->buf_addr +
1037 first_seg->data_off);
1040 * Store the mbuf address into the next entry of the array
1041 * of returned packets.
1043 rx_pkts[nb_rx++] = first_seg;
1046 * Setup receipt context for a new packet.
1052 * Record index of the next RX descriptor to probe.
1054 rxq->rx_tail = rx_id;
1057 * Save receive context.
1059 rxq->pkt_first_seg = first_seg;
1060 rxq->pkt_last_seg = last_seg;
1063 * If the number of free RX descriptors is greater than the RX free
1064 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1066 * Update the RDT with the value of the last processed RX descriptor
1067 * minus 1, to guarantee that the RDT register is never equal to the
1068 * RDH register, which creates a "full" ring situtation from the
1069 * 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 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1076 (unsigned) rx_id, (unsigned) nb_hold,
1078 rx_id = (uint16_t) ((rx_id == 0) ?
1079 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1080 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1083 rxq->nb_rx_hold = nb_hold;
1087 #define EM_MAX_BUF_SIZE 16384
1088 #define EM_RCTL_FLXBUF_STEP 1024
1091 em_tx_queue_release_mbufs(struct em_tx_queue *txq)
1095 if (txq->sw_ring != NULL) {
1096 for (i = 0; i != txq->nb_tx_desc; i++) {
1097 if (txq->sw_ring[i].mbuf != NULL) {
1098 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1099 txq->sw_ring[i].mbuf = NULL;
1106 em_tx_queue_release(struct em_tx_queue *txq)
1109 em_tx_queue_release_mbufs(txq);
1110 rte_free(txq->sw_ring);
1116 eth_em_tx_queue_release(void *txq)
1118 em_tx_queue_release(txq);
1121 /* (Re)set dynamic em_tx_queue fields to defaults */
1123 em_reset_tx_queue(struct em_tx_queue *txq)
1125 uint16_t i, nb_desc, prev;
1126 static const struct e1000_data_desc txd_init = {
1127 .upper.fields = {.status = E1000_TXD_STAT_DD},
1130 nb_desc = txq->nb_tx_desc;
1132 /* Initialize ring entries */
1134 prev = (uint16_t) (nb_desc - 1);
1136 for (i = 0; i < nb_desc; i++) {
1137 txq->tx_ring[i] = txd_init;
1138 txq->sw_ring[i].mbuf = NULL;
1139 txq->sw_ring[i].last_id = i;
1140 txq->sw_ring[prev].next_id = i;
1145 * Always allow 1 descriptor to be un-allocated to avoid
1146 * a H/W race condition
1148 txq->nb_tx_free = (uint16_t)(nb_desc - 1);
1149 txq->last_desc_cleaned = (uint16_t)(nb_desc - 1);
1150 txq->nb_tx_used = 0;
1153 memset((void*)&txq->ctx_cache, 0, sizeof (txq->ctx_cache));
1157 em_get_tx_port_offloads_capa(struct rte_eth_dev *dev)
1159 uint64_t tx_offload_capa;
1163 DEV_TX_OFFLOAD_VLAN_INSERT |
1164 DEV_TX_OFFLOAD_IPV4_CKSUM |
1165 DEV_TX_OFFLOAD_UDP_CKSUM |
1166 DEV_TX_OFFLOAD_TCP_CKSUM;
1168 return tx_offload_capa;
1172 em_get_tx_queue_offloads_capa(struct rte_eth_dev *dev)
1174 uint64_t tx_queue_offload_capa;
1177 * As only one Tx queue can be used, let per queue offloading
1178 * capability be same to per port queue offloading capability
1179 * for better convenience.
1181 tx_queue_offload_capa = em_get_tx_port_offloads_capa(dev);
1183 return tx_queue_offload_capa;
1187 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1190 unsigned int socket_id,
1191 const struct rte_eth_txconf *tx_conf)
1193 const struct rte_memzone *tz;
1194 struct em_tx_queue *txq;
1195 struct e1000_hw *hw;
1197 uint16_t tx_rs_thresh, tx_free_thresh;
1200 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1202 offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1205 * Validate number of transmit descriptors.
1206 * It must not exceed hardware maximum, and must be multiple
1209 if (nb_desc % EM_TXD_ALIGN != 0 ||
1210 (nb_desc > E1000_MAX_RING_DESC) ||
1211 (nb_desc < E1000_MIN_RING_DESC)) {
1215 tx_free_thresh = tx_conf->tx_free_thresh;
1216 if (tx_free_thresh == 0)
1217 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1218 DEFAULT_TX_FREE_THRESH);
1220 tx_rs_thresh = tx_conf->tx_rs_thresh;
1221 if (tx_rs_thresh == 0)
1222 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1223 DEFAULT_TX_RS_THRESH);
1225 if (tx_free_thresh >= (nb_desc - 3)) {
1226 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the "
1227 "number of TX descriptors minus 3. "
1228 "(tx_free_thresh=%u port=%d queue=%d)",
1229 (unsigned int)tx_free_thresh,
1230 (int)dev->data->port_id, (int)queue_idx);
1233 if (tx_rs_thresh > tx_free_thresh) {
1234 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or equal to "
1235 "tx_free_thresh. (tx_free_thresh=%u "
1236 "tx_rs_thresh=%u port=%d queue=%d)",
1237 (unsigned int)tx_free_thresh,
1238 (unsigned int)tx_rs_thresh,
1239 (int)dev->data->port_id,
1245 * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1246 * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1247 * by the NIC and all descriptors are written back after the NIC
1248 * accumulates WTHRESH descriptors.
1250 if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1251 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
1252 "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1253 "port=%d queue=%d)", (unsigned int)tx_rs_thresh,
1254 (int)dev->data->port_id, (int)queue_idx);
1258 /* Free memory prior to re-allocation if needed... */
1259 if (dev->data->tx_queues[queue_idx] != NULL) {
1260 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1261 dev->data->tx_queues[queue_idx] = NULL;
1265 * Allocate TX ring hardware descriptors. A memzone large enough to
1266 * handle the maximum ring size is allocated in order to allow for
1267 * resizing in later calls to the queue setup function.
1269 tsize = sizeof(txq->tx_ring[0]) * E1000_MAX_RING_DESC;
1270 tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1271 RTE_CACHE_LINE_SIZE, socket_id);
1275 /* Allocate the tx queue data structure. */
1276 if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1277 RTE_CACHE_LINE_SIZE)) == NULL)
1280 /* Allocate software ring */
1281 if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1282 sizeof(txq->sw_ring[0]) * nb_desc,
1283 RTE_CACHE_LINE_SIZE)) == NULL) {
1284 em_tx_queue_release(txq);
1288 txq->nb_tx_desc = nb_desc;
1289 txq->tx_free_thresh = tx_free_thresh;
1290 txq->tx_rs_thresh = tx_rs_thresh;
1291 txq->pthresh = tx_conf->tx_thresh.pthresh;
1292 txq->hthresh = tx_conf->tx_thresh.hthresh;
1293 txq->wthresh = tx_conf->tx_thresh.wthresh;
1294 txq->queue_id = queue_idx;
1295 txq->port_id = dev->data->port_id;
1297 txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1298 txq->tx_ring_phys_addr = tz->iova;
1299 txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1301 PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1302 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1304 em_reset_tx_queue(txq);
1306 dev->data->tx_queues[queue_idx] = txq;
1307 txq->offloads = offloads;
1312 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1316 if (rxq->sw_ring != NULL) {
1317 for (i = 0; i != rxq->nb_rx_desc; i++) {
1318 if (rxq->sw_ring[i].mbuf != NULL) {
1319 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1320 rxq->sw_ring[i].mbuf = NULL;
1327 em_rx_queue_release(struct em_rx_queue *rxq)
1330 em_rx_queue_release_mbufs(rxq);
1331 rte_free(rxq->sw_ring);
1337 eth_em_rx_queue_release(void *rxq)
1339 em_rx_queue_release(rxq);
1342 /* Reset dynamic em_rx_queue fields back to defaults */
1344 em_reset_rx_queue(struct em_rx_queue *rxq)
1347 rxq->nb_rx_hold = 0;
1348 rxq->pkt_first_seg = NULL;
1349 rxq->pkt_last_seg = NULL;
1353 em_get_rx_port_offloads_capa(struct rte_eth_dev *dev)
1355 uint64_t rx_offload_capa;
1356 uint32_t max_rx_pktlen;
1358 max_rx_pktlen = em_get_max_pktlen(dev);
1361 DEV_RX_OFFLOAD_VLAN_STRIP |
1362 DEV_RX_OFFLOAD_VLAN_FILTER |
1363 DEV_RX_OFFLOAD_IPV4_CKSUM |
1364 DEV_RX_OFFLOAD_UDP_CKSUM |
1365 DEV_RX_OFFLOAD_TCP_CKSUM |
1366 DEV_RX_OFFLOAD_CRC_STRIP |
1367 DEV_RX_OFFLOAD_SCATTER;
1368 if (max_rx_pktlen > ETHER_MAX_LEN)
1369 rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1371 return rx_offload_capa;
1375 em_get_rx_queue_offloads_capa(struct rte_eth_dev *dev)
1377 uint64_t rx_queue_offload_capa;
1380 * As only one Rx queue can be used, let per queue offloading
1381 * capability be same to per port queue offloading capability
1382 * for better convenience.
1384 rx_queue_offload_capa = em_get_rx_port_offloads_capa(dev);
1386 return rx_queue_offload_capa;
1390 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1393 unsigned int socket_id,
1394 const struct rte_eth_rxconf *rx_conf,
1395 struct rte_mempool *mp)
1397 const struct rte_memzone *rz;
1398 struct em_rx_queue *rxq;
1399 struct e1000_hw *hw;
1403 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1405 offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
1408 * Validate number of receive descriptors.
1409 * It must not exceed hardware maximum, and must be multiple
1412 if (nb_desc % EM_RXD_ALIGN != 0 ||
1413 (nb_desc > E1000_MAX_RING_DESC) ||
1414 (nb_desc < E1000_MIN_RING_DESC)) {
1419 * EM devices don't support drop_en functionality
1421 if (rx_conf->rx_drop_en) {
1422 PMD_INIT_LOG(ERR, "drop_en functionality not supported by "
1427 /* Free memory prior to re-allocation if needed. */
1428 if (dev->data->rx_queues[queue_idx] != NULL) {
1429 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1430 dev->data->rx_queues[queue_idx] = NULL;
1433 /* Allocate RX ring for max possible mumber of hardware descriptors. */
1434 rsize = sizeof(rxq->rx_ring[0]) * E1000_MAX_RING_DESC;
1435 rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1436 RTE_CACHE_LINE_SIZE, socket_id);
1440 /* Allocate the RX queue data structure. */
1441 if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1442 RTE_CACHE_LINE_SIZE)) == NULL)
1445 /* Allocate software ring. */
1446 if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1447 sizeof (rxq->sw_ring[0]) * nb_desc,
1448 RTE_CACHE_LINE_SIZE)) == NULL) {
1449 em_rx_queue_release(rxq);
1454 rxq->nb_rx_desc = nb_desc;
1455 rxq->pthresh = rx_conf->rx_thresh.pthresh;
1456 rxq->hthresh = rx_conf->rx_thresh.hthresh;
1457 rxq->wthresh = rx_conf->rx_thresh.wthresh;
1458 rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1459 rxq->queue_id = queue_idx;
1460 rxq->port_id = dev->data->port_id;
1461 rxq->crc_len = (uint8_t)((dev->data->dev_conf.rxmode.offloads &
1462 DEV_RX_OFFLOAD_CRC_STRIP) ? 0 : ETHER_CRC_LEN);
1464 rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1465 rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1466 rxq->rx_ring_phys_addr = rz->iova;
1467 rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1469 PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1470 rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1472 dev->data->rx_queues[queue_idx] = rxq;
1473 em_reset_rx_queue(rxq);
1474 rxq->offloads = offloads;
1480 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1482 #define EM_RXQ_SCAN_INTERVAL 4
1483 volatile struct e1000_rx_desc *rxdp;
1484 struct em_rx_queue *rxq;
1487 rxq = dev->data->rx_queues[rx_queue_id];
1488 rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1490 while ((desc < rxq->nb_rx_desc) &&
1491 (rxdp->status & E1000_RXD_STAT_DD)) {
1492 desc += EM_RXQ_SCAN_INTERVAL;
1493 rxdp += EM_RXQ_SCAN_INTERVAL;
1494 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1495 rxdp = &(rxq->rx_ring[rxq->rx_tail +
1496 desc - rxq->nb_rx_desc]);
1503 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1505 volatile struct e1000_rx_desc *rxdp;
1506 struct em_rx_queue *rxq = rx_queue;
1509 if (unlikely(offset >= rxq->nb_rx_desc))
1511 desc = rxq->rx_tail + offset;
1512 if (desc >= rxq->nb_rx_desc)
1513 desc -= rxq->nb_rx_desc;
1515 rxdp = &rxq->rx_ring[desc];
1516 return !!(rxdp->status & E1000_RXD_STAT_DD);
1520 eth_em_rx_descriptor_status(void *rx_queue, uint16_t offset)
1522 struct em_rx_queue *rxq = rx_queue;
1523 volatile uint8_t *status;
1526 if (unlikely(offset >= rxq->nb_rx_desc))
1529 if (offset >= rxq->nb_rx_desc - rxq->nb_rx_hold)
1530 return RTE_ETH_RX_DESC_UNAVAIL;
1532 desc = rxq->rx_tail + offset;
1533 if (desc >= rxq->nb_rx_desc)
1534 desc -= rxq->nb_rx_desc;
1536 status = &rxq->rx_ring[desc].status;
1537 if (*status & E1000_RXD_STAT_DD)
1538 return RTE_ETH_RX_DESC_DONE;
1540 return RTE_ETH_RX_DESC_AVAIL;
1544 eth_em_tx_descriptor_status(void *tx_queue, uint16_t offset)
1546 struct em_tx_queue *txq = tx_queue;
1547 volatile uint8_t *status;
1550 if (unlikely(offset >= txq->nb_tx_desc))
1553 desc = txq->tx_tail + offset;
1554 /* go to next desc that has the RS bit */
1555 desc = ((desc + txq->tx_rs_thresh - 1) / txq->tx_rs_thresh) *
1557 if (desc >= txq->nb_tx_desc) {
1558 desc -= txq->nb_tx_desc;
1559 if (desc >= txq->nb_tx_desc)
1560 desc -= txq->nb_tx_desc;
1563 status = &txq->tx_ring[desc].upper.fields.status;
1564 if (*status & E1000_TXD_STAT_DD)
1565 return RTE_ETH_TX_DESC_DONE;
1567 return RTE_ETH_TX_DESC_FULL;
1571 em_dev_clear_queues(struct rte_eth_dev *dev)
1574 struct em_tx_queue *txq;
1575 struct em_rx_queue *rxq;
1577 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1578 txq = dev->data->tx_queues[i];
1580 em_tx_queue_release_mbufs(txq);
1581 em_reset_tx_queue(txq);
1585 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1586 rxq = dev->data->rx_queues[i];
1588 em_rx_queue_release_mbufs(rxq);
1589 em_reset_rx_queue(rxq);
1595 em_dev_free_queues(struct rte_eth_dev *dev)
1599 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1600 eth_em_rx_queue_release(dev->data->rx_queues[i]);
1601 dev->data->rx_queues[i] = NULL;
1603 dev->data->nb_rx_queues = 0;
1605 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1606 eth_em_tx_queue_release(dev->data->tx_queues[i]);
1607 dev->data->tx_queues[i] = NULL;
1609 dev->data->nb_tx_queues = 0;
1613 * Takes as input/output parameter RX buffer size.
1614 * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1617 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1620 * For BSIZE & BSEX all configurable sizes are:
1621 * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1622 * 8192: rctl |= (E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX);
1623 * 4096: rctl |= (E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX);
1624 * 2048: rctl |= E1000_RCTL_SZ_2048;
1625 * 1024: rctl |= E1000_RCTL_SZ_1024;
1626 * 512: rctl |= E1000_RCTL_SZ_512;
1627 * 256: rctl |= E1000_RCTL_SZ_256;
1629 static const struct {
1632 } bufsz_to_rctl[] = {
1633 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1634 {8192, (E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX)},
1635 {4096, (E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX)},
1636 {2048, E1000_RCTL_SZ_2048},
1637 {1024, E1000_RCTL_SZ_1024},
1638 {512, E1000_RCTL_SZ_512},
1639 {256, E1000_RCTL_SZ_256},
1643 uint32_t rctl_bsize;
1645 rctl_bsize = *bufsz;
1648 * Starting from 82571 it is possible to specify RX buffer size
1649 * by RCTL.FLXBUF. When this field is different from zero, the
1650 * RX buffer size = RCTL.FLXBUF * 1K
1651 * (e.g. t is possible to specify RX buffer size 1,2,...,15KB).
1652 * It is working ok on real HW, but by some reason doesn't work
1653 * on VMware emulated 82574L.
1654 * So for now, always use BSIZE/BSEX to setup RX buffer size.
1655 * If you don't plan to use it on VMware emulated 82574L and
1656 * would like to specify RX buffer size in 1K granularity,
1657 * uncomment the following lines:
1658 * ***************************************************************
1659 * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1660 * rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1661 * rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1662 * *bufsz = rctl_bsize;
1663 * return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1664 * E1000_RCTL_FLXBUF_MASK);
1666 * ***************************************************************
1669 for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1671 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1672 *bufsz = bufsz_to_rctl[i].bufsz;
1673 return bufsz_to_rctl[i].rctl;
1677 /* Should never happen. */
1682 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1684 struct em_rx_entry *rxe = rxq->sw_ring;
1687 static const struct e1000_rx_desc rxd_init = {
1691 /* Initialize software ring entries */
1692 for (i = 0; i < rxq->nb_rx_desc; i++) {
1693 volatile struct e1000_rx_desc *rxd;
1694 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
1697 PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1698 "queue_id=%hu", rxq->queue_id);
1703 rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
1705 /* Clear HW ring memory */
1706 rxq->rx_ring[i] = rxd_init;
1708 rxd = &rxq->rx_ring[i];
1709 rxd->buffer_addr = dma_addr;
1716 /*********************************************************************
1718 * Enable receive unit.
1720 **********************************************************************/
1722 eth_em_rx_init(struct rte_eth_dev *dev)
1724 struct e1000_hw *hw;
1725 struct em_rx_queue *rxq;
1726 struct rte_eth_rxmode *rxmode;
1730 uint32_t rctl_bsize;
1734 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1735 rxmode = &dev->data->dev_conf.rxmode;
1738 * Make sure receives are disabled while setting
1739 * up the descriptor ring.
1741 rctl = E1000_READ_REG(hw, E1000_RCTL);
1742 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1744 rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1746 /* Disable extended descriptor type. */
1747 rfctl &= ~E1000_RFCTL_EXTEN;
1748 /* Disable accelerated acknowledge */
1749 if (hw->mac.type == e1000_82574)
1750 rfctl |= E1000_RFCTL_ACK_DIS;
1752 E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1755 * XXX TEMPORARY WORKAROUND: on some systems with 82573
1756 * long latencies are observed, like Lenovo X60. This
1757 * change eliminates the problem, but since having positive
1758 * values in RDTR is a known source of problems on other
1759 * platforms another solution is being sought.
1761 if (hw->mac.type == e1000_82573)
1762 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1764 dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1766 /* Determine RX bufsize. */
1767 rctl_bsize = EM_MAX_BUF_SIZE;
1768 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1771 rxq = dev->data->rx_queues[i];
1772 buf_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
1773 RTE_PKTMBUF_HEADROOM;
1774 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1777 rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1779 /* Configure and enable each RX queue. */
1780 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1784 rxq = dev->data->rx_queues[i];
1786 /* Allocate buffers for descriptor rings and setup queue */
1787 ret = em_alloc_rx_queue_mbufs(rxq);
1792 * Reset crc_len in case it was changed after queue setup by a
1796 (uint8_t)(dev->data->dev_conf.rxmode.offloads &
1797 DEV_RX_OFFLOAD_CRC_STRIP ? 0 : ETHER_CRC_LEN);
1799 bus_addr = rxq->rx_ring_phys_addr;
1800 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1802 sizeof(*rxq->rx_ring));
1803 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1804 (uint32_t)(bus_addr >> 32));
1805 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1807 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1808 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1810 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1811 rxdctl &= 0xFE000000;
1812 rxdctl |= rxq->pthresh & 0x3F;
1813 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1814 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1815 rxdctl |= E1000_RXDCTL_GRAN;
1816 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1819 * Due to EM devices not having any sort of hardware
1820 * limit for packet length, jumbo frame of any size
1821 * can be accepted, thus we have to enable scattered
1822 * rx if jumbo frames are enabled (or if buffer size
1823 * is too small to accommodate non-jumbo packets)
1824 * to avoid splitting packets that don't fit into
1827 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME ||
1828 rctl_bsize < ETHER_MAX_LEN) {
1829 if (!dev->data->scattered_rx)
1830 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1832 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1833 dev->data->scattered_rx = 1;
1837 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER) {
1838 if (!dev->data->scattered_rx)
1839 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1840 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1841 dev->data->scattered_rx = 1;
1845 * Setup the Checksum Register.
1846 * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1848 rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1850 if (rxmode->offloads & DEV_RX_OFFLOAD_CHECKSUM)
1851 rxcsum |= E1000_RXCSUM_IPOFL;
1853 rxcsum &= ~E1000_RXCSUM_IPOFL;
1854 E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1856 /* No MRQ or RSS support for now */
1858 /* Set early receive threshold on appropriate hw */
1859 if ((hw->mac.type == e1000_ich9lan ||
1860 hw->mac.type == e1000_pch2lan ||
1861 hw->mac.type == e1000_ich10lan) &&
1862 rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1863 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1864 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1865 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1868 if (hw->mac.type == e1000_pch2lan) {
1869 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
1870 e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1872 e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1875 /* Setup the Receive Control Register. */
1876 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_CRC_STRIP)
1877 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1879 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1881 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1882 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1883 E1000_RCTL_RDMTS_HALF |
1884 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1886 /* Make sure VLAN Filters are off. */
1887 rctl &= ~E1000_RCTL_VFE;
1888 /* Don't store bad packets. */
1889 rctl &= ~E1000_RCTL_SBP;
1890 /* Legacy descriptor type. */
1891 rctl &= ~E1000_RCTL_DTYP_MASK;
1894 * Configure support of jumbo frames, if any.
1896 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
1897 rctl |= E1000_RCTL_LPE;
1899 rctl &= ~E1000_RCTL_LPE;
1901 /* Enable Receives. */
1902 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1907 /*********************************************************************
1909 * Enable transmit unit.
1911 **********************************************************************/
1913 eth_em_tx_init(struct rte_eth_dev *dev)
1915 struct e1000_hw *hw;
1916 struct em_tx_queue *txq;
1921 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1923 /* Setup the Base and Length of the Tx Descriptor Rings. */
1924 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1927 txq = dev->data->tx_queues[i];
1928 bus_addr = txq->tx_ring_phys_addr;
1929 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1931 sizeof(*txq->tx_ring));
1932 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1933 (uint32_t)(bus_addr >> 32));
1934 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1936 /* Setup the HW Tx Head and Tail descriptor pointers. */
1937 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1938 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1940 /* Setup Transmit threshold registers. */
1941 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1943 * bit 22 is reserved, on some models should always be 0,
1944 * on others - always 1.
1946 txdctl &= E1000_TXDCTL_COUNT_DESC;
1947 txdctl |= txq->pthresh & 0x3F;
1948 txdctl |= (txq->hthresh & 0x3F) << 8;
1949 txdctl |= (txq->wthresh & 0x3F) << 16;
1950 txdctl |= E1000_TXDCTL_GRAN;
1951 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1954 /* Program the Transmit Control Register. */
1955 tctl = E1000_READ_REG(hw, E1000_TCTL);
1956 tctl &= ~E1000_TCTL_CT;
1957 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1958 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1960 /* This write will effectively turn on the transmit unit. */
1961 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1965 em_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1966 struct rte_eth_rxq_info *qinfo)
1968 struct em_rx_queue *rxq;
1970 rxq = dev->data->rx_queues[queue_id];
1972 qinfo->mp = rxq->mb_pool;
1973 qinfo->scattered_rx = dev->data->scattered_rx;
1974 qinfo->nb_desc = rxq->nb_rx_desc;
1975 qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
1976 qinfo->conf.offloads = rxq->offloads;
1980 em_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1981 struct rte_eth_txq_info *qinfo)
1983 struct em_tx_queue *txq;
1985 txq = dev->data->tx_queues[queue_id];
1987 qinfo->nb_desc = txq->nb_tx_desc;
1989 qinfo->conf.tx_thresh.pthresh = txq->pthresh;
1990 qinfo->conf.tx_thresh.hthresh = txq->hthresh;
1991 qinfo->conf.tx_thresh.wthresh = txq->wthresh;
1992 qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
1993 qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
1994 qinfo->conf.offloads = txq->offloads;