4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/queue.h>
44 #include <rte_interrupts.h>
45 #include <rte_byteorder.h>
46 #include <rte_common.h>
48 #include <rte_debug.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_launch.h>
55 #include <rte_per_lcore.h>
56 #include <rte_lcore.h>
57 #include <rte_atomic.h>
58 #include <rte_branch_prediction.h>
60 #include <rte_mempool.h>
61 #include <rte_malloc.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_prefetch.h>
70 #include <rte_string_fns.h>
72 #include "e1000_logs.h"
73 #include "base/e1000_api.h"
74 #include "e1000_ethdev.h"
75 #include "base/e1000_osdep.h"
77 #define E1000_TXD_VLAN_SHIFT 16
79 #define E1000_RXDCTL_GRAN 0x01000000 /* RXDCTL Granularity */
81 static inline struct rte_mbuf *
82 rte_rxmbuf_alloc(struct rte_mempool *mp)
86 m = __rte_mbuf_raw_alloc(mp);
87 __rte_mbuf_sanity_check_raw(m, 0);
91 #define RTE_MBUF_DATA_DMA_ADDR(mb) \
92 (uint64_t) ((mb)->buf_physaddr + (mb)->data_off)
94 #define RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mb) \
95 (uint64_t) ((mb)->buf_physaddr + RTE_PKTMBUF_HEADROOM)
98 * Structure associated with each descriptor of the RX ring of a RX queue.
101 struct rte_mbuf *mbuf; /**< mbuf associated with RX descriptor. */
105 * Structure associated with each descriptor of the TX ring of a TX queue.
108 struct rte_mbuf *mbuf; /**< mbuf associated with TX desc, if any. */
109 uint16_t next_id; /**< Index of next descriptor in ring. */
110 uint16_t last_id; /**< Index of last scattered descriptor. */
114 * Structure associated with each RX queue.
117 struct rte_mempool *mb_pool; /**< mbuf pool to populate RX ring. */
118 volatile struct e1000_rx_desc *rx_ring; /**< RX ring virtual address. */
119 uint64_t rx_ring_phys_addr; /**< RX ring DMA address. */
120 volatile uint32_t *rdt_reg_addr; /**< RDT register address. */
121 volatile uint32_t *rdh_reg_addr; /**< RDH register address. */
122 struct em_rx_entry *sw_ring; /**< address of RX software ring. */
123 struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
124 struct rte_mbuf *pkt_last_seg; /**< Last segment of current packet. */
125 uint16_t nb_rx_desc; /**< number of RX descriptors. */
126 uint16_t rx_tail; /**< current value of RDT register. */
127 uint16_t nb_rx_hold; /**< number of held free RX desc. */
128 uint16_t rx_free_thresh; /**< max free RX desc to hold. */
129 uint16_t queue_id; /**< RX queue index. */
130 uint8_t port_id; /**< Device port identifier. */
131 uint8_t pthresh; /**< Prefetch threshold register. */
132 uint8_t hthresh; /**< Host threshold register. */
133 uint8_t wthresh; /**< Write-back threshold register. */
134 uint8_t crc_len; /**< 0 if CRC stripped, 4 otherwise. */
138 * Hardware context number
141 EM_CTX_0 = 0, /**< CTX0 */
142 EM_CTX_NUM = 1, /**< CTX NUM */
145 /** Offload features */
146 union em_vlan_macip {
149 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
150 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
152 /**< VLAN Tag Control Identifier (CPU order). */
157 * Compare mask for vlan_macip_len.data,
158 * should be in sync with em_vlan_macip.f layout.
160 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
161 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
162 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
163 /** MAC+IP length. */
164 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
167 * Structure to check if new context need be built
170 uint64_t flags; /**< ol_flags related to context build. */
171 uint32_t cmp_mask; /**< compare mask */
172 union em_vlan_macip hdrlen; /**< L2 and L3 header lenghts */
176 * Structure associated with each TX queue.
179 volatile struct e1000_data_desc *tx_ring; /**< TX ring address */
180 uint64_t tx_ring_phys_addr; /**< TX ring DMA address. */
181 struct em_tx_entry *sw_ring; /**< virtual address of SW ring. */
182 volatile uint32_t *tdt_reg_addr; /**< Address of TDT register. */
183 uint16_t nb_tx_desc; /**< number of TX descriptors. */
184 uint16_t tx_tail; /**< Current value of TDT register. */
185 uint16_t tx_free_thresh;/**< minimum TX before freeing. */
186 /**< Number of TX descriptors to use before RS bit is set. */
187 uint16_t tx_rs_thresh;
188 /** Number of TX descriptors used since RS bit was set. */
190 /** Index to last TX descriptor to have been cleaned. */
191 uint16_t last_desc_cleaned;
192 /** Total number of TX descriptors ready to be allocated. */
194 uint16_t queue_id; /**< TX queue index. */
195 uint8_t port_id; /**< Device port identifier. */
196 uint8_t pthresh; /**< Prefetch threshold register. */
197 uint8_t hthresh; /**< Host threshold register. */
198 uint8_t wthresh; /**< Write-back threshold register. */
199 struct em_ctx_info ctx_cache;
200 /**< Hardware context history.*/
204 #define RTE_PMD_USE_PREFETCH
207 #ifdef RTE_PMD_USE_PREFETCH
208 #define rte_em_prefetch(p) rte_prefetch0(p)
210 #define rte_em_prefetch(p) do {} while(0)
213 #ifdef RTE_PMD_PACKET_PREFETCH
214 #define rte_packet_prefetch(p) rte_prefetch1(p)
216 #define rte_packet_prefetch(p) do {} while(0)
219 #ifndef DEFAULT_TX_FREE_THRESH
220 #define DEFAULT_TX_FREE_THRESH 32
221 #endif /* DEFAULT_TX_FREE_THRESH */
223 #ifndef DEFAULT_TX_RS_THRESH
224 #define DEFAULT_TX_RS_THRESH 32
225 #endif /* DEFAULT_TX_RS_THRESH */
228 /*********************************************************************
232 **********************************************************************/
235 * Populates TX context descriptor.
238 em_set_xmit_ctx(struct em_tx_queue* txq,
239 volatile struct e1000_context_desc *ctx_txd,
241 union em_vlan_macip hdrlen)
243 uint32_t cmp_mask, cmd_len;
244 uint16_t ipcse, l2len;
245 struct e1000_context_desc ctx;
248 cmd_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_C;
250 l2len = hdrlen.f.l2_len;
251 ipcse = (uint16_t)(l2len + hdrlen.f.l3_len);
253 /* setup IPCS* fields */
254 ctx.lower_setup.ip_fields.ipcss = (uint8_t)l2len;
255 ctx.lower_setup.ip_fields.ipcso = (uint8_t)(l2len +
256 offsetof(struct ipv4_hdr, hdr_checksum));
259 * When doing checksum or TCP segmentation with IPv6 headers,
260 * IPCSE field should be set t0 0.
262 if (flags & PKT_TX_IP_CKSUM) {
263 ctx.lower_setup.ip_fields.ipcse =
264 (uint16_t)rte_cpu_to_le_16(ipcse - 1);
265 cmd_len |= E1000_TXD_CMD_IP;
266 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
268 ctx.lower_setup.ip_fields.ipcse = 0;
271 /* setup TUCS* fields */
272 ctx.upper_setup.tcp_fields.tucss = (uint8_t)ipcse;
273 ctx.upper_setup.tcp_fields.tucse = 0;
275 switch (flags & PKT_TX_L4_MASK) {
276 case PKT_TX_UDP_CKSUM:
277 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
278 offsetof(struct udp_hdr, dgram_cksum));
279 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
281 case PKT_TX_TCP_CKSUM:
282 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
283 offsetof(struct tcp_hdr, cksum));
284 cmd_len |= E1000_TXD_CMD_TCP;
285 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
288 ctx.upper_setup.tcp_fields.tucso = 0;
291 ctx.cmd_and_length = rte_cpu_to_le_32(cmd_len);
292 ctx.tcp_seg_setup.data = 0;
296 txq->ctx_cache.flags = flags;
297 txq->ctx_cache.cmp_mask = cmp_mask;
298 txq->ctx_cache.hdrlen = hdrlen;
302 * Check which hardware context can be used. Use the existing match
303 * or create a new context descriptor.
305 static inline uint32_t
306 what_ctx_update(struct em_tx_queue *txq, uint64_t flags,
307 union em_vlan_macip hdrlen)
309 /* If match with the current context */
310 if (likely (txq->ctx_cache.flags == flags &&
311 ((txq->ctx_cache.hdrlen.data ^ hdrlen.data) &
312 txq->ctx_cache.cmp_mask) == 0))
319 /* Reset transmit descriptors after they have been used */
321 em_xmit_cleanup(struct em_tx_queue *txq)
323 struct em_tx_entry *sw_ring = txq->sw_ring;
324 volatile struct e1000_data_desc *txr = txq->tx_ring;
325 uint16_t last_desc_cleaned = txq->last_desc_cleaned;
326 uint16_t nb_tx_desc = txq->nb_tx_desc;
327 uint16_t desc_to_clean_to;
328 uint16_t nb_tx_to_clean;
330 /* Determine the last descriptor needing to be cleaned */
331 desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_rs_thresh);
332 if (desc_to_clean_to >= nb_tx_desc)
333 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
335 /* Check to make sure the last descriptor to clean is done */
336 desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
337 if (! (txr[desc_to_clean_to].upper.fields.status & E1000_TXD_STAT_DD))
339 PMD_TX_FREE_LOG(DEBUG,
340 "TX descriptor %4u is not done"
341 "(port=%d queue=%d)", desc_to_clean_to,
342 txq->port_id, txq->queue_id);
343 /* Failed to clean any descriptors, better luck next time */
347 /* Figure out how many descriptors will be cleaned */
348 if (last_desc_cleaned > desc_to_clean_to)
349 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
352 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
355 PMD_TX_FREE_LOG(DEBUG,
356 "Cleaning %4u TX descriptors: %4u to %4u "
357 "(port=%d queue=%d)", nb_tx_to_clean,
358 last_desc_cleaned, desc_to_clean_to, txq->port_id,
362 * The last descriptor to clean is done, so that means all the
363 * descriptors from the last descriptor that was cleaned
364 * up to the last descriptor with the RS bit set
365 * are done. Only reset the threshold descriptor.
367 txr[desc_to_clean_to].upper.fields.status = 0;
369 /* Update the txq to reflect the last descriptor that was cleaned */
370 txq->last_desc_cleaned = desc_to_clean_to;
371 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
377 static inline uint32_t
378 tx_desc_cksum_flags_to_upper(uint64_t ol_flags)
380 static const uint32_t l4_olinfo[2] = {0, E1000_TXD_POPTS_TXSM << 8};
381 static const uint32_t l3_olinfo[2] = {0, E1000_TXD_POPTS_IXSM << 8};
384 tmp = l4_olinfo[(ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM];
385 tmp |= l3_olinfo[(ol_flags & PKT_TX_IP_CKSUM) != 0];
390 eth_em_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
393 struct em_tx_queue *txq;
394 struct em_tx_entry *sw_ring;
395 struct em_tx_entry *txe, *txn;
396 volatile struct e1000_data_desc *txr;
397 volatile struct e1000_data_desc *txd;
398 struct rte_mbuf *tx_pkt;
399 struct rte_mbuf *m_seg;
400 uint64_t buf_dma_addr;
402 uint32_t cmd_type_len;
412 union em_vlan_macip hdrlen;
415 sw_ring = txq->sw_ring;
417 tx_id = txq->tx_tail;
418 txe = &sw_ring[tx_id];
420 /* Determine if the descriptor ring needs to be cleaned. */
421 if ((txq->nb_tx_desc - txq->nb_tx_free) > txq->tx_free_thresh) {
422 em_xmit_cleanup(txq);
426 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
430 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
433 * Determine how many (if any) context descriptors
434 * are needed for offload functionality.
436 ol_flags = tx_pkt->ol_flags;
438 /* If hardware offload required */
439 tx_ol_req = (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK));
441 hdrlen.f.vlan_tci = tx_pkt->vlan_tci;
442 hdrlen.f.l2_len = tx_pkt->l2_len;
443 hdrlen.f.l3_len = tx_pkt->l3_len;
444 /* If new context to be built or reuse the exist ctx. */
445 ctx = what_ctx_update(txq, tx_ol_req, hdrlen);
447 /* Only allocate context descriptor if required*/
448 new_ctx = (ctx == EM_CTX_NUM);
452 * Keep track of how many descriptors are used this loop
453 * This will always be the number of segments + the number of
454 * Context descriptors required to transmit the packet
456 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
459 * The number of descriptors that must be allocated for a
460 * packet is the number of segments of that packet, plus 1
461 * Context Descriptor for the hardware offload, if any.
462 * Determine the last TX descriptor to allocate in the TX ring
463 * for the packet, starting from the current position (tx_id)
466 tx_last = (uint16_t) (tx_id + nb_used - 1);
469 if (tx_last >= txq->nb_tx_desc)
470 tx_last = (uint16_t) (tx_last - txq->nb_tx_desc);
472 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
473 " tx_first=%u tx_last=%u",
474 (unsigned) txq->port_id,
475 (unsigned) txq->queue_id,
476 (unsigned) tx_pkt->pkt_len,
481 * Make sure there are enough TX descriptors available to
482 * transmit the entire packet.
483 * nb_used better be less than or equal to txq->tx_rs_thresh
485 while (unlikely (nb_used > txq->nb_tx_free)) {
486 PMD_TX_FREE_LOG(DEBUG, "Not enough free TX descriptors "
487 "nb_used=%4u nb_free=%4u "
488 "(port=%d queue=%d)",
489 nb_used, txq->nb_tx_free,
490 txq->port_id, txq->queue_id);
492 if (em_xmit_cleanup(txq) != 0) {
493 /* Could not clean any descriptors */
501 * By now there are enough free TX descriptors to transmit
506 * Set common flags of all TX Data Descriptors.
508 * The following bits must be set in all Data Descriptors:
509 * - E1000_TXD_DTYP_DATA
510 * - E1000_TXD_DTYP_DEXT
512 * The following bits must be set in the first Data Descriptor
513 * and are ignored in the other ones:
514 * - E1000_TXD_POPTS_IXSM
515 * - E1000_TXD_POPTS_TXSM
517 * The following bits must be set in the last Data Descriptor
518 * and are ignored in the other ones:
519 * - E1000_TXD_CMD_VLE
520 * - E1000_TXD_CMD_IFCS
522 * The following bits must only be set in the last Data
524 * - E1000_TXD_CMD_EOP
526 * The following bits can be set in any Data Descriptor, but
527 * are only set in the last Data Descriptor:
530 cmd_type_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
534 /* Set VLAN Tag offload fields. */
535 if (ol_flags & PKT_TX_VLAN_PKT) {
536 cmd_type_len |= E1000_TXD_CMD_VLE;
537 popts_spec = tx_pkt->vlan_tci << E1000_TXD_VLAN_SHIFT;
542 * Setup the TX Context Descriptor if required
545 volatile struct e1000_context_desc *ctx_txd;
547 ctx_txd = (volatile struct e1000_context_desc *)
550 txn = &sw_ring[txe->next_id];
551 RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
553 if (txe->mbuf != NULL) {
554 rte_pktmbuf_free_seg(txe->mbuf);
558 em_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
561 txe->last_id = tx_last;
562 tx_id = txe->next_id;
567 * Setup the TX Data Descriptor,
568 * This path will go through
569 * whatever new/reuse the context descriptor
571 popts_spec |= tx_desc_cksum_flags_to_upper(ol_flags);
577 txn = &sw_ring[txe->next_id];
579 if (txe->mbuf != NULL)
580 rte_pktmbuf_free_seg(txe->mbuf);
584 * Set up Transmit Data Descriptor.
586 slen = m_seg->data_len;
587 buf_dma_addr = RTE_MBUF_DATA_DMA_ADDR(m_seg);
589 txd->buffer_addr = rte_cpu_to_le_64(buf_dma_addr);
590 txd->lower.data = rte_cpu_to_le_32(cmd_type_len | slen);
591 txd->upper.data = rte_cpu_to_le_32(popts_spec);
593 txe->last_id = tx_last;
594 tx_id = txe->next_id;
597 } while (m_seg != NULL);
600 * The last packet data descriptor needs End Of Packet (EOP)
602 cmd_type_len |= E1000_TXD_CMD_EOP;
603 txq->nb_tx_used = (uint16_t)(txq->nb_tx_used + nb_used);
604 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
606 /* Set RS bit only on threshold packets' last descriptor */
607 if (txq->nb_tx_used >= txq->tx_rs_thresh) {
608 PMD_TX_FREE_LOG(DEBUG,
609 "Setting RS bit on TXD id=%4u "
610 "(port=%d queue=%d)",
611 tx_last, txq->port_id, txq->queue_id);
613 cmd_type_len |= E1000_TXD_CMD_RS;
615 /* Update txq RS bit counters */
618 txd->lower.data |= rte_cpu_to_le_32(cmd_type_len);
624 * Set the Transmit Descriptor Tail (TDT)
626 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
627 (unsigned) txq->port_id, (unsigned) txq->queue_id,
628 (unsigned) tx_id, (unsigned) nb_tx);
629 E1000_PCI_REG_WRITE(txq->tdt_reg_addr, tx_id);
630 txq->tx_tail = tx_id;
635 /*********************************************************************
639 **********************************************************************/
641 static inline uint64_t
642 rx_desc_status_to_pkt_flags(uint32_t rx_status)
646 /* Check if VLAN present */
647 pkt_flags = ((rx_status & E1000_RXD_STAT_VP) ? PKT_RX_VLAN_PKT : 0);
652 static inline uint64_t
653 rx_desc_error_to_pkt_flags(uint32_t rx_error)
655 uint64_t pkt_flags = 0;
657 if (rx_error & E1000_RXD_ERR_IPE)
658 pkt_flags |= PKT_RX_IP_CKSUM_BAD;
659 if (rx_error & E1000_RXD_ERR_TCPE)
660 pkt_flags |= PKT_RX_L4_CKSUM_BAD;
665 eth_em_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
668 volatile struct e1000_rx_desc *rx_ring;
669 volatile struct e1000_rx_desc *rxdp;
670 struct em_rx_queue *rxq;
671 struct em_rx_entry *sw_ring;
672 struct em_rx_entry *rxe;
673 struct rte_mbuf *rxm;
674 struct rte_mbuf *nmb;
675 struct e1000_rx_desc rxd;
687 rx_id = rxq->rx_tail;
688 rx_ring = rxq->rx_ring;
689 sw_ring = rxq->sw_ring;
690 while (nb_rx < nb_pkts) {
692 * The order of operations here is important as the DD status
693 * bit must not be read after any other descriptor fields.
694 * rx_ring and rxdp are pointing to volatile data so the order
695 * of accesses cannot be reordered by the compiler. If they were
696 * not volatile, they could be reordered which could lead to
697 * using invalid descriptor fields when read from rxd.
699 rxdp = &rx_ring[rx_id];
700 status = rxdp->status;
701 if (! (status & E1000_RXD_STAT_DD))
708 * If the E1000_RXD_STAT_EOP flag is not set, the RX packet is
709 * likely to be invalid and to be dropped by the various
710 * validation checks performed by the network stack.
712 * Allocate a new mbuf to replenish the RX ring descriptor.
713 * If the allocation fails:
714 * - arrange for that RX descriptor to be the first one
715 * being parsed the next time the receive function is
716 * invoked [on the same queue].
718 * - Stop parsing the RX ring and return immediately.
720 * This policy do not drop the packet received in the RX
721 * descriptor for which the allocation of a new mbuf failed.
722 * Thus, it allows that packet to be later retrieved if
723 * mbuf have been freed in the mean time.
724 * As a side effect, holding RX descriptors instead of
725 * systematically giving them back to the NIC may lead to
726 * RX ring exhaustion situations.
727 * However, the NIC can gracefully prevent such situations
728 * to happen by sending specific "back-pressure" flow control
729 * frames to its peer(s).
731 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
732 "status=0x%x pkt_len=%u",
733 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
734 (unsigned) rx_id, (unsigned) status,
735 (unsigned) rte_le_to_cpu_16(rxd.length));
737 nmb = rte_rxmbuf_alloc(rxq->mb_pool);
739 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
741 (unsigned) rxq->port_id,
742 (unsigned) rxq->queue_id);
743 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
748 rxe = &sw_ring[rx_id];
750 if (rx_id == rxq->nb_rx_desc)
753 /* Prefetch next mbuf while processing current one. */
754 rte_em_prefetch(sw_ring[rx_id].mbuf);
757 * When next RX descriptor is on a cache-line boundary,
758 * prefetch the next 4 RX descriptors and the next 8 pointers
761 if ((rx_id & 0x3) == 0) {
762 rte_em_prefetch(&rx_ring[rx_id]);
763 rte_em_prefetch(&sw_ring[rx_id]);
766 /* Rearm RXD: attach new mbuf and reset status to zero. */
771 rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(nmb));
772 rxdp->buffer_addr = dma_addr;
776 * Initialize the returned mbuf.
777 * 1) setup generic mbuf fields:
778 * - number of segments,
781 * - RX port identifier.
782 * 2) integrate hardware offload data, if any:
784 * - IP checksum flag,
785 * - VLAN TCI, if any,
788 pkt_len = (uint16_t) (rte_le_to_cpu_16(rxd.length) -
790 rxm->data_off = RTE_PKTMBUF_HEADROOM;
791 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
794 rxm->pkt_len = pkt_len;
795 rxm->data_len = pkt_len;
796 rxm->port = rxq->port_id;
798 rxm->ol_flags = rx_desc_status_to_pkt_flags(status);
799 rxm->ol_flags = rxm->ol_flags |
800 rx_desc_error_to_pkt_flags(rxd.errors);
802 /* Only valid if PKT_RX_VLAN_PKT set in pkt_flags */
803 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
806 * Store the mbuf address into the next entry of the array
807 * of returned packets.
809 rx_pkts[nb_rx++] = rxm;
811 rxq->rx_tail = rx_id;
814 * If the number of free RX descriptors is greater than the RX free
815 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
817 * Update the RDT with the value of the last processed RX descriptor
818 * minus 1, to guarantee that the RDT register is never equal to the
819 * RDH register, which creates a "full" ring situtation from the
820 * hardware point of view...
822 nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
823 if (nb_hold > rxq->rx_free_thresh) {
824 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
825 "nb_hold=%u nb_rx=%u",
826 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
827 (unsigned) rx_id, (unsigned) nb_hold,
829 rx_id = (uint16_t) ((rx_id == 0) ?
830 (rxq->nb_rx_desc - 1) : (rx_id - 1));
831 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
834 rxq->nb_rx_hold = nb_hold;
839 eth_em_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
842 struct em_rx_queue *rxq;
843 volatile struct e1000_rx_desc *rx_ring;
844 volatile struct e1000_rx_desc *rxdp;
845 struct em_rx_entry *sw_ring;
846 struct em_rx_entry *rxe;
847 struct rte_mbuf *first_seg;
848 struct rte_mbuf *last_seg;
849 struct rte_mbuf *rxm;
850 struct rte_mbuf *nmb;
851 struct e1000_rx_desc rxd;
852 uint64_t dma; /* Physical address of mbuf data buffer */
863 rx_id = rxq->rx_tail;
864 rx_ring = rxq->rx_ring;
865 sw_ring = rxq->sw_ring;
868 * Retrieve RX context of current packet, if any.
870 first_seg = rxq->pkt_first_seg;
871 last_seg = rxq->pkt_last_seg;
873 while (nb_rx < nb_pkts) {
876 * The order of operations here is important as the DD status
877 * bit must not be read after any other descriptor fields.
878 * rx_ring and rxdp are pointing to volatile data so the order
879 * of accesses cannot be reordered by the compiler. If they were
880 * not volatile, they could be reordered which could lead to
881 * using invalid descriptor fields when read from rxd.
883 rxdp = &rx_ring[rx_id];
884 status = rxdp->status;
885 if (! (status & E1000_RXD_STAT_DD))
892 * Allocate a new mbuf to replenish the RX ring descriptor.
893 * If the allocation fails:
894 * - arrange for that RX descriptor to be the first one
895 * being parsed the next time the receive function is
896 * invoked [on the same queue].
898 * - Stop parsing the RX ring and return immediately.
900 * This policy does not drop the packet received in the RX
901 * descriptor for which the allocation of a new mbuf failed.
902 * Thus, it allows that packet to be later retrieved if
903 * mbuf have been freed in the mean time.
904 * As a side effect, holding RX descriptors instead of
905 * systematically giving them back to the NIC may lead to
906 * RX ring exhaustion situations.
907 * However, the NIC can gracefully prevent such situations
908 * to happen by sending specific "back-pressure" flow control
909 * frames to its peer(s).
911 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
912 "status=0x%x data_len=%u",
913 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
914 (unsigned) rx_id, (unsigned) status,
915 (unsigned) rte_le_to_cpu_16(rxd.length));
917 nmb = rte_rxmbuf_alloc(rxq->mb_pool);
919 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
920 "queue_id=%u", (unsigned) rxq->port_id,
921 (unsigned) rxq->queue_id);
922 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
927 rxe = &sw_ring[rx_id];
929 if (rx_id == rxq->nb_rx_desc)
932 /* Prefetch next mbuf while processing current one. */
933 rte_em_prefetch(sw_ring[rx_id].mbuf);
936 * When next RX descriptor is on a cache-line boundary,
937 * prefetch the next 4 RX descriptors and the next 8 pointers
940 if ((rx_id & 0x3) == 0) {
941 rte_em_prefetch(&rx_ring[rx_id]);
942 rte_em_prefetch(&sw_ring[rx_id]);
946 * Update RX descriptor with the physical address of the new
947 * data buffer of the new allocated mbuf.
951 dma = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(nmb));
952 rxdp->buffer_addr = dma;
956 * Set data length & data buffer address of mbuf.
958 data_len = rte_le_to_cpu_16(rxd.length);
959 rxm->data_len = data_len;
960 rxm->data_off = RTE_PKTMBUF_HEADROOM;
963 * If this is the first buffer of the received packet,
964 * set the pointer to the first mbuf of the packet and
965 * initialize its context.
966 * Otherwise, update the total length and the number of segments
967 * of the current scattered packet, and update the pointer to
968 * the last mbuf of the current packet.
970 if (first_seg == NULL) {
972 first_seg->pkt_len = data_len;
973 first_seg->nb_segs = 1;
975 first_seg->pkt_len += data_len;
976 first_seg->nb_segs++;
977 last_seg->next = rxm;
981 * If this is not the last buffer of the received packet,
982 * update the pointer to the last mbuf of the current scattered
983 * packet and continue to parse the RX ring.
985 if (! (status & E1000_RXD_STAT_EOP)) {
991 * This is the last buffer of the received packet.
992 * If the CRC is not stripped by the hardware:
993 * - Subtract the CRC length from the total packet length.
994 * - If the last buffer only contains the whole CRC or a part
995 * of it, free the mbuf associated to the last buffer.
996 * If part of the CRC is also contained in the previous
997 * mbuf, subtract the length of that CRC part from the
998 * data length of the previous mbuf.
1001 if (unlikely(rxq->crc_len > 0)) {
1002 first_seg->pkt_len -= ETHER_CRC_LEN;
1003 if (data_len <= ETHER_CRC_LEN) {
1004 rte_pktmbuf_free_seg(rxm);
1005 first_seg->nb_segs--;
1006 last_seg->data_len = (uint16_t)
1007 (last_seg->data_len -
1008 (ETHER_CRC_LEN - data_len));
1009 last_seg->next = NULL;
1012 (uint16_t) (data_len - ETHER_CRC_LEN);
1016 * Initialize the first mbuf of the returned packet:
1017 * - RX port identifier,
1018 * - hardware offload data, if any:
1019 * - IP checksum flag,
1022 first_seg->port = rxq->port_id;
1024 first_seg->ol_flags = rx_desc_status_to_pkt_flags(status);
1025 first_seg->ol_flags = first_seg->ol_flags |
1026 rx_desc_error_to_pkt_flags(rxd.errors);
1028 /* Only valid if PKT_RX_VLAN_PKT set in pkt_flags */
1029 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
1031 /* Prefetch data of first segment, if configured to do so. */
1032 rte_packet_prefetch((char *)first_seg->buf_addr +
1033 first_seg->data_off);
1036 * Store the mbuf address into the next entry of the array
1037 * of returned packets.
1039 rx_pkts[nb_rx++] = first_seg;
1042 * Setup receipt context for a new packet.
1048 * Record index of the next RX descriptor to probe.
1050 rxq->rx_tail = rx_id;
1053 * Save receive context.
1055 rxq->pkt_first_seg = first_seg;
1056 rxq->pkt_last_seg = last_seg;
1059 * If the number of free RX descriptors is greater than the RX free
1060 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1062 * Update the RDT with the value of the last processed RX descriptor
1063 * minus 1, to guarantee that the RDT register is never equal to the
1064 * RDH register, which creates a "full" ring situtation from the
1065 * hardware point of view...
1067 nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
1068 if (nb_hold > rxq->rx_free_thresh) {
1069 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1070 "nb_hold=%u nb_rx=%u",
1071 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1072 (unsigned) rx_id, (unsigned) nb_hold,
1074 rx_id = (uint16_t) ((rx_id == 0) ?
1075 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1076 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1079 rxq->nb_rx_hold = nb_hold;
1084 * Rings setup and release.
1086 * TDBA/RDBA should be aligned on 16 byte boundary. But TDLEN/RDLEN should be
1087 * multiple of 128 bytes. So we align TDBA/RDBA on 128 byte boundary.
1088 * This will also optimize cache line size effect.
1089 * H/W supports up to cache line size 128.
1091 #define EM_ALIGN 128
1094 * Maximum number of Ring Descriptors.
1096 * Since RDLEN/TDLEN should be multiple of 128 bytes, the number of ring
1097 * desscriptors should meet the following condition:
1098 * (num_ring_desc * sizeof(struct e1000_rx/tx_desc)) % 128 == 0
1100 #define EM_MIN_RING_DESC 32
1101 #define EM_MAX_RING_DESC 4096
1103 #define EM_MAX_BUF_SIZE 16384
1104 #define EM_RCTL_FLXBUF_STEP 1024
1106 static const struct rte_memzone *
1107 ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,
1108 uint16_t queue_id, uint32_t ring_size, int socket_id)
1110 const struct rte_memzone *mz;
1111 char z_name[RTE_MEMZONE_NAMESIZE];
1113 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
1114 dev->driver->pci_drv.name, ring_name, dev->data->port_id,
1117 if ((mz = rte_memzone_lookup(z_name)) != 0)
1120 #ifdef RTE_LIBRTE_XEN_DOM0
1121 return rte_memzone_reserve_bounded(z_name, ring_size,
1122 socket_id, 0, RTE_CACHE_LINE_SIZE, RTE_PGSIZE_2M);
1124 return rte_memzone_reserve(z_name, ring_size, socket_id, 0);
1129 em_tx_queue_release_mbufs(struct em_tx_queue *txq)
1133 if (txq->sw_ring != NULL) {
1134 for (i = 0; i != txq->nb_tx_desc; i++) {
1135 if (txq->sw_ring[i].mbuf != NULL) {
1136 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1137 txq->sw_ring[i].mbuf = NULL;
1144 em_tx_queue_release(struct em_tx_queue *txq)
1147 em_tx_queue_release_mbufs(txq);
1148 rte_free(txq->sw_ring);
1154 eth_em_tx_queue_release(void *txq)
1156 em_tx_queue_release(txq);
1159 /* (Re)set dynamic em_tx_queue fields to defaults */
1161 em_reset_tx_queue(struct em_tx_queue *txq)
1163 uint16_t i, nb_desc, prev;
1164 static const struct e1000_data_desc txd_init = {
1165 .upper.fields = {.status = E1000_TXD_STAT_DD},
1168 nb_desc = txq->nb_tx_desc;
1170 /* Initialize ring entries */
1172 prev = (uint16_t) (nb_desc - 1);
1174 for (i = 0; i < nb_desc; i++) {
1175 txq->tx_ring[i] = txd_init;
1176 txq->sw_ring[i].mbuf = NULL;
1177 txq->sw_ring[i].last_id = i;
1178 txq->sw_ring[prev].next_id = i;
1183 * Always allow 1 descriptor to be un-allocated to avoid
1184 * a H/W race condition
1186 txq->nb_tx_free = (uint16_t)(nb_desc - 1);
1187 txq->last_desc_cleaned = (uint16_t)(nb_desc - 1);
1188 txq->nb_tx_used = 0;
1191 memset((void*)&txq->ctx_cache, 0, sizeof (txq->ctx_cache));
1195 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1198 unsigned int socket_id,
1199 const struct rte_eth_txconf *tx_conf)
1201 const struct rte_memzone *tz;
1202 struct em_tx_queue *txq;
1203 struct e1000_hw *hw;
1205 uint16_t tx_rs_thresh, tx_free_thresh;
1207 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1210 * Validate number of transmit descriptors.
1211 * It must not exceed hardware maximum, and must be multiple
1214 if (((nb_desc * sizeof(*txq->tx_ring)) % EM_ALIGN) != 0 ||
1215 (nb_desc > EM_MAX_RING_DESC) ||
1216 (nb_desc < EM_MIN_RING_DESC)) {
1220 tx_free_thresh = tx_conf->tx_free_thresh;
1221 if (tx_free_thresh == 0)
1222 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1223 DEFAULT_TX_FREE_THRESH);
1225 tx_rs_thresh = tx_conf->tx_rs_thresh;
1226 if (tx_rs_thresh == 0)
1227 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1228 DEFAULT_TX_RS_THRESH);
1230 if (tx_free_thresh >= (nb_desc - 3)) {
1231 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the "
1232 "number of TX descriptors minus 3. "
1233 "(tx_free_thresh=%u port=%d queue=%d)",
1234 (unsigned int)tx_free_thresh,
1235 (int)dev->data->port_id, (int)queue_idx);
1238 if (tx_rs_thresh > tx_free_thresh) {
1239 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or equal to "
1240 "tx_free_thresh. (tx_free_thresh=%u "
1241 "tx_rs_thresh=%u port=%d queue=%d)",
1242 (unsigned int)tx_free_thresh,
1243 (unsigned int)tx_rs_thresh,
1244 (int)dev->data->port_id,
1250 * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1251 * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1252 * by the NIC and all descriptors are written back after the NIC
1253 * accumulates WTHRESH descriptors.
1255 if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1256 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
1257 "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1258 "port=%d queue=%d)", (unsigned int)tx_rs_thresh,
1259 (int)dev->data->port_id, (int)queue_idx);
1263 /* Free memory prior to re-allocation if needed... */
1264 if (dev->data->tx_queues[queue_idx] != NULL) {
1265 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1266 dev->data->tx_queues[queue_idx] = NULL;
1270 * Allocate TX ring hardware descriptors. A memzone large enough to
1271 * handle the maximum ring size is allocated in order to allow for
1272 * resizing in later calls to the queue setup function.
1274 tsize = sizeof (txq->tx_ring[0]) * EM_MAX_RING_DESC;
1275 if ((tz = ring_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1276 socket_id)) == NULL)
1279 /* Allocate the tx queue data structure. */
1280 if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1281 RTE_CACHE_LINE_SIZE)) == NULL)
1284 /* Allocate software ring */
1285 if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1286 sizeof(txq->sw_ring[0]) * nb_desc,
1287 RTE_CACHE_LINE_SIZE)) == NULL) {
1288 em_tx_queue_release(txq);
1292 txq->nb_tx_desc = nb_desc;
1293 txq->tx_free_thresh = tx_free_thresh;
1294 txq->tx_rs_thresh = tx_rs_thresh;
1295 txq->pthresh = tx_conf->tx_thresh.pthresh;
1296 txq->hthresh = tx_conf->tx_thresh.hthresh;
1297 txq->wthresh = tx_conf->tx_thresh.wthresh;
1298 txq->queue_id = queue_idx;
1299 txq->port_id = dev->data->port_id;
1301 txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1302 #ifndef RTE_LIBRTE_XEN_DOM0
1303 txq->tx_ring_phys_addr = (uint64_t) tz->phys_addr;
1305 txq->tx_ring_phys_addr = rte_mem_phy2mch(tz->memseg_id, tz->phys_addr);
1307 txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1309 PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1310 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1312 em_reset_tx_queue(txq);
1314 dev->data->tx_queues[queue_idx] = txq;
1319 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1323 if (rxq->sw_ring != NULL) {
1324 for (i = 0; i != rxq->nb_rx_desc; i++) {
1325 if (rxq->sw_ring[i].mbuf != NULL) {
1326 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1327 rxq->sw_ring[i].mbuf = NULL;
1334 em_rx_queue_release(struct em_rx_queue *rxq)
1337 em_rx_queue_release_mbufs(rxq);
1338 rte_free(rxq->sw_ring);
1344 eth_em_rx_queue_release(void *rxq)
1346 em_rx_queue_release(rxq);
1349 /* Reset dynamic em_rx_queue fields back to defaults */
1351 em_reset_rx_queue(struct em_rx_queue *rxq)
1354 rxq->nb_rx_hold = 0;
1355 rxq->pkt_first_seg = NULL;
1356 rxq->pkt_last_seg = NULL;
1360 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1363 unsigned int socket_id,
1364 const struct rte_eth_rxconf *rx_conf,
1365 struct rte_mempool *mp)
1367 const struct rte_memzone *rz;
1368 struct em_rx_queue *rxq;
1369 struct e1000_hw *hw;
1372 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1375 * Validate number of receive descriptors.
1376 * It must not exceed hardware maximum, and must be multiple
1379 if (((nb_desc * sizeof(rxq->rx_ring[0])) % EM_ALIGN) != 0 ||
1380 (nb_desc > EM_MAX_RING_DESC) ||
1381 (nb_desc < EM_MIN_RING_DESC)) {
1386 * EM devices don't support drop_en functionality
1388 if (rx_conf->rx_drop_en) {
1389 PMD_INIT_LOG(ERR, "drop_en functionality not supported by "
1394 /* Free memory prior to re-allocation if needed. */
1395 if (dev->data->rx_queues[queue_idx] != NULL) {
1396 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1397 dev->data->rx_queues[queue_idx] = NULL;
1400 /* Allocate RX ring for max possible mumber of hardware descriptors. */
1401 rsize = sizeof (rxq->rx_ring[0]) * EM_MAX_RING_DESC;
1402 if ((rz = ring_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1403 socket_id)) == NULL)
1406 /* Allocate the RX queue data structure. */
1407 if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1408 RTE_CACHE_LINE_SIZE)) == NULL)
1411 /* Allocate software ring. */
1412 if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1413 sizeof (rxq->sw_ring[0]) * nb_desc,
1414 RTE_CACHE_LINE_SIZE)) == NULL) {
1415 em_rx_queue_release(rxq);
1420 rxq->nb_rx_desc = nb_desc;
1421 rxq->pthresh = rx_conf->rx_thresh.pthresh;
1422 rxq->hthresh = rx_conf->rx_thresh.hthresh;
1423 rxq->wthresh = rx_conf->rx_thresh.wthresh;
1424 rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1425 rxq->queue_id = queue_idx;
1426 rxq->port_id = dev->data->port_id;
1427 rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ?
1430 rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1431 rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1432 #ifndef RTE_LIBRTE_XEN_DOM0
1433 rxq->rx_ring_phys_addr = (uint64_t) rz->phys_addr;
1435 rxq->rx_ring_phys_addr = rte_mem_phy2mch(rz->memseg_id, rz->phys_addr);
1437 rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1439 PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1440 rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1442 dev->data->rx_queues[queue_idx] = rxq;
1443 em_reset_rx_queue(rxq);
1449 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1451 #define EM_RXQ_SCAN_INTERVAL 4
1452 volatile struct e1000_rx_desc *rxdp;
1453 struct em_rx_queue *rxq;
1456 if (rx_queue_id >= dev->data->nb_rx_queues) {
1457 PMD_RX_LOG(DEBUG, "Invalid RX queue_id=%d", rx_queue_id);
1461 rxq = dev->data->rx_queues[rx_queue_id];
1462 rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1464 while ((desc < rxq->nb_rx_desc) &&
1465 (rxdp->status & E1000_RXD_STAT_DD)) {
1466 desc += EM_RXQ_SCAN_INTERVAL;
1467 rxdp += EM_RXQ_SCAN_INTERVAL;
1468 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1469 rxdp = &(rxq->rx_ring[rxq->rx_tail +
1470 desc - rxq->nb_rx_desc]);
1477 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1479 volatile struct e1000_rx_desc *rxdp;
1480 struct em_rx_queue *rxq = rx_queue;
1483 if (unlikely(offset >= rxq->nb_rx_desc))
1485 desc = rxq->rx_tail + offset;
1486 if (desc >= rxq->nb_rx_desc)
1487 desc -= rxq->nb_rx_desc;
1489 rxdp = &rxq->rx_ring[desc];
1490 return !!(rxdp->status & E1000_RXD_STAT_DD);
1494 em_dev_clear_queues(struct rte_eth_dev *dev)
1497 struct em_tx_queue *txq;
1498 struct em_rx_queue *rxq;
1500 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1501 txq = dev->data->tx_queues[i];
1503 em_tx_queue_release_mbufs(txq);
1504 em_reset_tx_queue(txq);
1508 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1509 rxq = dev->data->rx_queues[i];
1511 em_rx_queue_release_mbufs(rxq);
1512 em_reset_rx_queue(rxq);
1518 * Takes as input/output parameter RX buffer size.
1519 * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1522 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1525 * For BSIZE & BSEX all configurable sizes are:
1526 * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1527 * 8192: rctl |= (E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX);
1528 * 4096: rctl |= (E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX);
1529 * 2048: rctl |= E1000_RCTL_SZ_2048;
1530 * 1024: rctl |= E1000_RCTL_SZ_1024;
1531 * 512: rctl |= E1000_RCTL_SZ_512;
1532 * 256: rctl |= E1000_RCTL_SZ_256;
1534 static const struct {
1537 } bufsz_to_rctl[] = {
1538 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1539 {8192, (E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX)},
1540 {4096, (E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX)},
1541 {2048, E1000_RCTL_SZ_2048},
1542 {1024, E1000_RCTL_SZ_1024},
1543 {512, E1000_RCTL_SZ_512},
1544 {256, E1000_RCTL_SZ_256},
1548 uint32_t rctl_bsize;
1550 rctl_bsize = *bufsz;
1553 * Starting from 82571 it is possible to specify RX buffer size
1554 * by RCTL.FLXBUF. When this field is different from zero, the
1555 * RX buffer size = RCTL.FLXBUF * 1K
1556 * (e.g. t is possible to specify RX buffer size 1,2,...,15KB).
1557 * It is working ok on real HW, but by some reason doesn't work
1558 * on VMware emulated 82574L.
1559 * So for now, always use BSIZE/BSEX to setup RX buffer size.
1560 * If you don't plan to use it on VMware emulated 82574L and
1561 * would like to specify RX buffer size in 1K granularity,
1562 * uncomment the following lines:
1563 * ***************************************************************
1564 * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1565 * rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1566 * rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1567 * *bufsz = rctl_bsize;
1568 * return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1569 * E1000_RCTL_FLXBUF_MASK);
1571 * ***************************************************************
1574 for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1576 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1577 *bufsz = bufsz_to_rctl[i].bufsz;
1578 return (bufsz_to_rctl[i].rctl);
1582 /* Should never happen. */
1587 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1589 struct em_rx_entry *rxe = rxq->sw_ring;
1592 static const struct e1000_rx_desc rxd_init = {
1596 /* Initialize software ring entries */
1597 for (i = 0; i < rxq->nb_rx_desc; i++) {
1598 volatile struct e1000_rx_desc *rxd;
1599 struct rte_mbuf *mbuf = rte_rxmbuf_alloc(rxq->mb_pool);
1602 PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1603 "queue_id=%hu", rxq->queue_id);
1607 dma_addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR_DEFAULT(mbuf));
1609 /* Clear HW ring memory */
1610 rxq->rx_ring[i] = rxd_init;
1612 rxd = &rxq->rx_ring[i];
1613 rxd->buffer_addr = dma_addr;
1620 /*********************************************************************
1622 * Enable receive unit.
1624 **********************************************************************/
1626 eth_em_rx_init(struct rte_eth_dev *dev)
1628 struct e1000_hw *hw;
1629 struct em_rx_queue *rxq;
1633 uint32_t rctl_bsize;
1637 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1640 * Make sure receives are disabled while setting
1641 * up the descriptor ring.
1643 rctl = E1000_READ_REG(hw, E1000_RCTL);
1644 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1646 rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1648 /* Disable extended descriptor type. */
1649 rfctl &= ~E1000_RFCTL_EXTEN;
1650 /* Disable accelerated acknowledge */
1651 if (hw->mac.type == e1000_82574)
1652 rfctl |= E1000_RFCTL_ACK_DIS;
1654 E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1657 * XXX TEMPORARY WORKAROUND: on some systems with 82573
1658 * long latencies are observed, like Lenovo X60. This
1659 * change eliminates the problem, but since having positive
1660 * values in RDTR is a known source of problems on other
1661 * platforms another solution is being sought.
1663 if (hw->mac.type == e1000_82573)
1664 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1666 dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1668 /* Determine RX bufsize. */
1669 rctl_bsize = EM_MAX_BUF_SIZE;
1670 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1673 rxq = dev->data->rx_queues[i];
1674 buf_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
1675 RTE_PKTMBUF_HEADROOM;
1676 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1679 rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1681 /* Configure and enable each RX queue. */
1682 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1686 rxq = dev->data->rx_queues[i];
1688 /* Allocate buffers for descriptor rings and setup queue */
1689 ret = em_alloc_rx_queue_mbufs(rxq);
1694 * Reset crc_len in case it was changed after queue setup by a
1698 (uint8_t)(dev->data->dev_conf.rxmode.hw_strip_crc ?
1701 bus_addr = rxq->rx_ring_phys_addr;
1702 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1704 sizeof(*rxq->rx_ring));
1705 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1706 (uint32_t)(bus_addr >> 32));
1707 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1709 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1710 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1712 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1713 rxdctl &= 0xFE000000;
1714 rxdctl |= rxq->pthresh & 0x3F;
1715 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1716 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1717 rxdctl |= E1000_RXDCTL_GRAN;
1718 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1721 * Due to EM devices not having any sort of hardware
1722 * limit for packet length, jumbo frame of any size
1723 * can be accepted, thus we have to enable scattered
1724 * rx if jumbo frames are enabled (or if buffer size
1725 * is too small to accommodate non-jumbo packets)
1726 * to avoid splitting packets that don't fit into
1729 if (dev->data->dev_conf.rxmode.jumbo_frame ||
1730 rctl_bsize < ETHER_MAX_LEN) {
1731 if (!dev->data->scattered_rx)
1732 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1734 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1735 dev->data->scattered_rx = 1;
1739 if (dev->data->dev_conf.rxmode.enable_scatter) {
1740 if (!dev->data->scattered_rx)
1741 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1742 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1743 dev->data->scattered_rx = 1;
1747 * Setup the Checksum Register.
1748 * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1750 rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1752 if (dev->data->dev_conf.rxmode.hw_ip_checksum)
1753 rxcsum |= E1000_RXCSUM_IPOFL;
1755 rxcsum &= ~E1000_RXCSUM_IPOFL;
1756 E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1758 /* No MRQ or RSS support for now */
1760 /* Set early receive threshold on appropriate hw */
1761 if ((hw->mac.type == e1000_ich9lan ||
1762 hw->mac.type == e1000_pch2lan ||
1763 hw->mac.type == e1000_ich10lan) &&
1764 dev->data->dev_conf.rxmode.jumbo_frame == 1) {
1765 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1766 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1767 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1770 if (hw->mac.type == e1000_pch2lan) {
1771 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1772 e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1774 e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1777 /* Setup the Receive Control Register. */
1778 if (dev->data->dev_conf.rxmode.hw_strip_crc)
1779 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1781 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1783 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1784 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1785 E1000_RCTL_RDMTS_HALF |
1786 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1788 /* Make sure VLAN Filters are off. */
1789 rctl &= ~E1000_RCTL_VFE;
1790 /* Don't store bad packets. */
1791 rctl &= ~E1000_RCTL_SBP;
1792 /* Legacy descriptor type. */
1793 rctl &= ~E1000_RCTL_DTYP_MASK;
1796 * Configure support of jumbo frames, if any.
1798 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1799 rctl |= E1000_RCTL_LPE;
1801 rctl &= ~E1000_RCTL_LPE;
1803 /* Enable Receives. */
1804 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1809 /*********************************************************************
1811 * Enable transmit unit.
1813 **********************************************************************/
1815 eth_em_tx_init(struct rte_eth_dev *dev)
1817 struct e1000_hw *hw;
1818 struct em_tx_queue *txq;
1823 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1825 /* Setup the Base and Length of the Tx Descriptor Rings. */
1826 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1829 txq = dev->data->tx_queues[i];
1830 bus_addr = txq->tx_ring_phys_addr;
1831 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1833 sizeof(*txq->tx_ring));
1834 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1835 (uint32_t)(bus_addr >> 32));
1836 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1838 /* Setup the HW Tx Head and Tail descriptor pointers. */
1839 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1840 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1842 /* Setup Transmit threshold registers. */
1843 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1845 * bit 22 is reserved, on some models should always be 0,
1846 * on others - always 1.
1848 txdctl &= E1000_TXDCTL_COUNT_DESC;
1849 txdctl |= txq->pthresh & 0x3F;
1850 txdctl |= (txq->hthresh & 0x3F) << 8;
1851 txdctl |= (txq->wthresh & 0x3F) << 16;
1852 txdctl |= E1000_TXDCTL_GRAN;
1853 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1856 /* Program the Transmit Control Register. */
1857 tctl = E1000_READ_REG(hw, E1000_TCTL);
1858 tctl &= ~E1000_TCTL_CT;
1859 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1860 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1862 /* This write will effectively turn on the transmit unit. */
1863 E1000_WRITE_REG(hw, E1000_TCTL, tctl);