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 */
82 * Structure associated with each descriptor of the RX ring of a RX queue.
85 struct rte_mbuf *mbuf; /**< mbuf associated with RX descriptor. */
89 * Structure associated with each descriptor of the TX ring of a TX queue.
92 struct rte_mbuf *mbuf; /**< mbuf associated with TX desc, if any. */
93 uint16_t next_id; /**< Index of next descriptor in ring. */
94 uint16_t last_id; /**< Index of last scattered descriptor. */
98 * Structure associated with each RX queue.
101 struct rte_mempool *mb_pool; /**< mbuf pool to populate RX ring. */
102 volatile struct e1000_rx_desc *rx_ring; /**< RX ring virtual address. */
103 uint64_t rx_ring_phys_addr; /**< RX ring DMA address. */
104 volatile uint32_t *rdt_reg_addr; /**< RDT register address. */
105 volatile uint32_t *rdh_reg_addr; /**< RDH register address. */
106 struct em_rx_entry *sw_ring; /**< address of RX software ring. */
107 struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
108 struct rte_mbuf *pkt_last_seg; /**< Last segment of current packet. */
109 uint16_t nb_rx_desc; /**< number of RX descriptors. */
110 uint16_t rx_tail; /**< current value of RDT register. */
111 uint16_t nb_rx_hold; /**< number of held free RX desc. */
112 uint16_t rx_free_thresh; /**< max free RX desc to hold. */
113 uint16_t queue_id; /**< RX queue index. */
114 uint8_t port_id; /**< Device port identifier. */
115 uint8_t pthresh; /**< Prefetch threshold register. */
116 uint8_t hthresh; /**< Host threshold register. */
117 uint8_t wthresh; /**< Write-back threshold register. */
118 uint8_t crc_len; /**< 0 if CRC stripped, 4 otherwise. */
122 * Hardware context number
125 EM_CTX_0 = 0, /**< CTX0 */
126 EM_CTX_NUM = 1, /**< CTX NUM */
129 /** Offload features */
130 union em_vlan_macip {
133 uint16_t l3_len:9; /**< L3 (IP) Header Length. */
134 uint16_t l2_len:7; /**< L2 (MAC) Header Length. */
136 /**< VLAN Tag Control Identifier (CPU order). */
141 * Compare mask for vlan_macip_len.data,
142 * should be in sync with em_vlan_macip.f layout.
144 #define TX_VLAN_CMP_MASK 0xFFFF0000 /**< VLAN length - 16-bits. */
145 #define TX_MAC_LEN_CMP_MASK 0x0000FE00 /**< MAC length - 7-bits. */
146 #define TX_IP_LEN_CMP_MASK 0x000001FF /**< IP length - 9-bits. */
147 /** MAC+IP length. */
148 #define TX_MACIP_LEN_CMP_MASK (TX_MAC_LEN_CMP_MASK | TX_IP_LEN_CMP_MASK)
151 * Structure to check if new context need be built
154 uint64_t flags; /**< ol_flags related to context build. */
155 uint32_t cmp_mask; /**< compare mask */
156 union em_vlan_macip hdrlen; /**< L2 and L3 header lenghts */
160 * Structure associated with each TX queue.
163 volatile struct e1000_data_desc *tx_ring; /**< TX ring address */
164 uint64_t tx_ring_phys_addr; /**< TX ring DMA address. */
165 struct em_tx_entry *sw_ring; /**< virtual address of SW ring. */
166 volatile uint32_t *tdt_reg_addr; /**< Address of TDT register. */
167 uint16_t nb_tx_desc; /**< number of TX descriptors. */
168 uint16_t tx_tail; /**< Current value of TDT register. */
169 /**< Start freeing TX buffers if there are less free descriptors than
171 uint16_t tx_free_thresh;
172 /**< Number of TX descriptors to use before RS bit is set. */
173 uint16_t tx_rs_thresh;
174 /** Number of TX descriptors used since RS bit was set. */
176 /** Index to last TX descriptor to have been cleaned. */
177 uint16_t last_desc_cleaned;
178 /** Total number of TX descriptors ready to be allocated. */
180 uint16_t queue_id; /**< TX queue index. */
181 uint8_t port_id; /**< Device port identifier. */
182 uint8_t pthresh; /**< Prefetch threshold register. */
183 uint8_t hthresh; /**< Host threshold register. */
184 uint8_t wthresh; /**< Write-back threshold register. */
185 struct em_ctx_info ctx_cache;
186 /**< Hardware context history.*/
190 #define RTE_PMD_USE_PREFETCH
193 #ifdef RTE_PMD_USE_PREFETCH
194 #define rte_em_prefetch(p) rte_prefetch0(p)
196 #define rte_em_prefetch(p) do {} while(0)
199 #ifdef RTE_PMD_PACKET_PREFETCH
200 #define rte_packet_prefetch(p) rte_prefetch1(p)
202 #define rte_packet_prefetch(p) do {} while(0)
205 #ifndef DEFAULT_TX_FREE_THRESH
206 #define DEFAULT_TX_FREE_THRESH 32
207 #endif /* DEFAULT_TX_FREE_THRESH */
209 #ifndef DEFAULT_TX_RS_THRESH
210 #define DEFAULT_TX_RS_THRESH 32
211 #endif /* DEFAULT_TX_RS_THRESH */
214 /*********************************************************************
218 **********************************************************************/
221 * Populates TX context descriptor.
224 em_set_xmit_ctx(struct em_tx_queue* txq,
225 volatile struct e1000_context_desc *ctx_txd,
227 union em_vlan_macip hdrlen)
229 uint32_t cmp_mask, cmd_len;
230 uint16_t ipcse, l2len;
231 struct e1000_context_desc ctx;
234 cmd_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_C;
236 l2len = hdrlen.f.l2_len;
237 ipcse = (uint16_t)(l2len + hdrlen.f.l3_len);
239 /* setup IPCS* fields */
240 ctx.lower_setup.ip_fields.ipcss = (uint8_t)l2len;
241 ctx.lower_setup.ip_fields.ipcso = (uint8_t)(l2len +
242 offsetof(struct ipv4_hdr, hdr_checksum));
245 * When doing checksum or TCP segmentation with IPv6 headers,
246 * IPCSE field should be set t0 0.
248 if (flags & PKT_TX_IP_CKSUM) {
249 ctx.lower_setup.ip_fields.ipcse =
250 (uint16_t)rte_cpu_to_le_16(ipcse - 1);
251 cmd_len |= E1000_TXD_CMD_IP;
252 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
254 ctx.lower_setup.ip_fields.ipcse = 0;
257 /* setup TUCS* fields */
258 ctx.upper_setup.tcp_fields.tucss = (uint8_t)ipcse;
259 ctx.upper_setup.tcp_fields.tucse = 0;
261 switch (flags & PKT_TX_L4_MASK) {
262 case PKT_TX_UDP_CKSUM:
263 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
264 offsetof(struct udp_hdr, dgram_cksum));
265 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
267 case PKT_TX_TCP_CKSUM:
268 ctx.upper_setup.tcp_fields.tucso = (uint8_t)(ipcse +
269 offsetof(struct tcp_hdr, cksum));
270 cmd_len |= E1000_TXD_CMD_TCP;
271 cmp_mask |= TX_MACIP_LEN_CMP_MASK;
274 ctx.upper_setup.tcp_fields.tucso = 0;
277 ctx.cmd_and_length = rte_cpu_to_le_32(cmd_len);
278 ctx.tcp_seg_setup.data = 0;
282 txq->ctx_cache.flags = flags;
283 txq->ctx_cache.cmp_mask = cmp_mask;
284 txq->ctx_cache.hdrlen = hdrlen;
288 * Check which hardware context can be used. Use the existing match
289 * or create a new context descriptor.
291 static inline uint32_t
292 what_ctx_update(struct em_tx_queue *txq, uint64_t flags,
293 union em_vlan_macip hdrlen)
295 /* If match with the current context */
296 if (likely (txq->ctx_cache.flags == flags &&
297 ((txq->ctx_cache.hdrlen.data ^ hdrlen.data) &
298 txq->ctx_cache.cmp_mask) == 0))
305 /* Reset transmit descriptors after they have been used */
307 em_xmit_cleanup(struct em_tx_queue *txq)
309 struct em_tx_entry *sw_ring = txq->sw_ring;
310 volatile struct e1000_data_desc *txr = txq->tx_ring;
311 uint16_t last_desc_cleaned = txq->last_desc_cleaned;
312 uint16_t nb_tx_desc = txq->nb_tx_desc;
313 uint16_t desc_to_clean_to;
314 uint16_t nb_tx_to_clean;
316 /* Determine the last descriptor needing to be cleaned */
317 desc_to_clean_to = (uint16_t)(last_desc_cleaned + txq->tx_rs_thresh);
318 if (desc_to_clean_to >= nb_tx_desc)
319 desc_to_clean_to = (uint16_t)(desc_to_clean_to - nb_tx_desc);
321 /* Check to make sure the last descriptor to clean is done */
322 desc_to_clean_to = sw_ring[desc_to_clean_to].last_id;
323 if (! (txr[desc_to_clean_to].upper.fields.status & E1000_TXD_STAT_DD))
325 PMD_TX_FREE_LOG(DEBUG,
326 "TX descriptor %4u is not done"
327 "(port=%d queue=%d)", desc_to_clean_to,
328 txq->port_id, txq->queue_id);
329 /* Failed to clean any descriptors, better luck next time */
333 /* Figure out how many descriptors will be cleaned */
334 if (last_desc_cleaned > desc_to_clean_to)
335 nb_tx_to_clean = (uint16_t)((nb_tx_desc - last_desc_cleaned) +
338 nb_tx_to_clean = (uint16_t)(desc_to_clean_to -
341 PMD_TX_FREE_LOG(DEBUG,
342 "Cleaning %4u TX descriptors: %4u to %4u "
343 "(port=%d queue=%d)", nb_tx_to_clean,
344 last_desc_cleaned, desc_to_clean_to, txq->port_id,
348 * The last descriptor to clean is done, so that means all the
349 * descriptors from the last descriptor that was cleaned
350 * up to the last descriptor with the RS bit set
351 * are done. Only reset the threshold descriptor.
353 txr[desc_to_clean_to].upper.fields.status = 0;
355 /* Update the txq to reflect the last descriptor that was cleaned */
356 txq->last_desc_cleaned = desc_to_clean_to;
357 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free + nb_tx_to_clean);
363 static inline uint32_t
364 tx_desc_cksum_flags_to_upper(uint64_t ol_flags)
366 static const uint32_t l4_olinfo[2] = {0, E1000_TXD_POPTS_TXSM << 8};
367 static const uint32_t l3_olinfo[2] = {0, E1000_TXD_POPTS_IXSM << 8};
370 tmp = l4_olinfo[(ol_flags & PKT_TX_L4_MASK) != PKT_TX_L4_NO_CKSUM];
371 tmp |= l3_olinfo[(ol_flags & PKT_TX_IP_CKSUM) != 0];
376 eth_em_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
379 struct em_tx_queue *txq;
380 struct em_tx_entry *sw_ring;
381 struct em_tx_entry *txe, *txn;
382 volatile struct e1000_data_desc *txr;
383 volatile struct e1000_data_desc *txd;
384 struct rte_mbuf *tx_pkt;
385 struct rte_mbuf *m_seg;
386 uint64_t buf_dma_addr;
388 uint32_t cmd_type_len;
398 union em_vlan_macip hdrlen;
401 sw_ring = txq->sw_ring;
403 tx_id = txq->tx_tail;
404 txe = &sw_ring[tx_id];
406 /* Determine if the descriptor ring needs to be cleaned. */
407 if (txq->nb_tx_free < txq->tx_free_thresh)
408 em_xmit_cleanup(txq);
411 for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
415 RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
418 * Determine how many (if any) context descriptors
419 * are needed for offload functionality.
421 ol_flags = tx_pkt->ol_flags;
423 /* If hardware offload required */
424 tx_ol_req = (ol_flags & (PKT_TX_IP_CKSUM | PKT_TX_L4_MASK));
426 hdrlen.f.vlan_tci = tx_pkt->vlan_tci;
427 hdrlen.f.l2_len = tx_pkt->l2_len;
428 hdrlen.f.l3_len = tx_pkt->l3_len;
429 /* If new context to be built or reuse the exist ctx. */
430 ctx = what_ctx_update(txq, tx_ol_req, hdrlen);
432 /* Only allocate context descriptor if required*/
433 new_ctx = (ctx == EM_CTX_NUM);
437 * Keep track of how many descriptors are used this loop
438 * This will always be the number of segments + the number of
439 * Context descriptors required to transmit the packet
441 nb_used = (uint16_t)(tx_pkt->nb_segs + new_ctx);
444 * The number of descriptors that must be allocated for a
445 * packet is the number of segments of that packet, plus 1
446 * Context Descriptor for the hardware offload, if any.
447 * Determine the last TX descriptor to allocate in the TX ring
448 * for the packet, starting from the current position (tx_id)
451 tx_last = (uint16_t) (tx_id + nb_used - 1);
454 if (tx_last >= txq->nb_tx_desc)
455 tx_last = (uint16_t) (tx_last - txq->nb_tx_desc);
457 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
458 " tx_first=%u tx_last=%u",
459 (unsigned) txq->port_id,
460 (unsigned) txq->queue_id,
461 (unsigned) tx_pkt->pkt_len,
466 * Make sure there are enough TX descriptors available to
467 * transmit the entire packet.
468 * nb_used better be less than or equal to txq->tx_rs_thresh
470 while (unlikely (nb_used > txq->nb_tx_free)) {
471 PMD_TX_FREE_LOG(DEBUG, "Not enough free TX descriptors "
472 "nb_used=%4u nb_free=%4u "
473 "(port=%d queue=%d)",
474 nb_used, txq->nb_tx_free,
475 txq->port_id, txq->queue_id);
477 if (em_xmit_cleanup(txq) != 0) {
478 /* Could not clean any descriptors */
486 * By now there are enough free TX descriptors to transmit
491 * Set common flags of all TX Data Descriptors.
493 * The following bits must be set in all Data Descriptors:
494 * - E1000_TXD_DTYP_DATA
495 * - E1000_TXD_DTYP_DEXT
497 * The following bits must be set in the first Data Descriptor
498 * and are ignored in the other ones:
499 * - E1000_TXD_POPTS_IXSM
500 * - E1000_TXD_POPTS_TXSM
502 * The following bits must be set in the last Data Descriptor
503 * and are ignored in the other ones:
504 * - E1000_TXD_CMD_VLE
505 * - E1000_TXD_CMD_IFCS
507 * The following bits must only be set in the last Data
509 * - E1000_TXD_CMD_EOP
511 * The following bits can be set in any Data Descriptor, but
512 * are only set in the last Data Descriptor:
515 cmd_type_len = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
519 /* Set VLAN Tag offload fields. */
520 if (ol_flags & PKT_TX_VLAN_PKT) {
521 cmd_type_len |= E1000_TXD_CMD_VLE;
522 popts_spec = tx_pkt->vlan_tci << E1000_TXD_VLAN_SHIFT;
527 * Setup the TX Context Descriptor if required
530 volatile struct e1000_context_desc *ctx_txd;
532 ctx_txd = (volatile struct e1000_context_desc *)
535 txn = &sw_ring[txe->next_id];
536 RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
538 if (txe->mbuf != NULL) {
539 rte_pktmbuf_free_seg(txe->mbuf);
543 em_set_xmit_ctx(txq, ctx_txd, tx_ol_req,
546 txe->last_id = tx_last;
547 tx_id = txe->next_id;
552 * Setup the TX Data Descriptor,
553 * This path will go through
554 * whatever new/reuse the context descriptor
556 popts_spec |= tx_desc_cksum_flags_to_upper(ol_flags);
562 txn = &sw_ring[txe->next_id];
564 if (txe->mbuf != NULL)
565 rte_pktmbuf_free_seg(txe->mbuf);
569 * Set up Transmit Data Descriptor.
571 slen = m_seg->data_len;
572 buf_dma_addr = rte_mbuf_data_dma_addr(m_seg);
574 txd->buffer_addr = rte_cpu_to_le_64(buf_dma_addr);
575 txd->lower.data = rte_cpu_to_le_32(cmd_type_len | slen);
576 txd->upper.data = rte_cpu_to_le_32(popts_spec);
578 txe->last_id = tx_last;
579 tx_id = txe->next_id;
582 } while (m_seg != NULL);
585 * The last packet data descriptor needs End Of Packet (EOP)
587 cmd_type_len |= E1000_TXD_CMD_EOP;
588 txq->nb_tx_used = (uint16_t)(txq->nb_tx_used + nb_used);
589 txq->nb_tx_free = (uint16_t)(txq->nb_tx_free - nb_used);
591 /* Set RS bit only on threshold packets' last descriptor */
592 if (txq->nb_tx_used >= txq->tx_rs_thresh) {
593 PMD_TX_FREE_LOG(DEBUG,
594 "Setting RS bit on TXD id=%4u "
595 "(port=%d queue=%d)",
596 tx_last, txq->port_id, txq->queue_id);
598 cmd_type_len |= E1000_TXD_CMD_RS;
600 /* Update txq RS bit counters */
603 txd->lower.data |= rte_cpu_to_le_32(cmd_type_len);
609 * Set the Transmit Descriptor Tail (TDT)
611 PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
612 (unsigned) txq->port_id, (unsigned) txq->queue_id,
613 (unsigned) tx_id, (unsigned) nb_tx);
614 E1000_PCI_REG_WRITE(txq->tdt_reg_addr, tx_id);
615 txq->tx_tail = tx_id;
620 /*********************************************************************
624 **********************************************************************/
626 static inline uint64_t
627 rx_desc_status_to_pkt_flags(uint32_t rx_status)
631 /* Check if VLAN present */
632 pkt_flags = ((rx_status & E1000_RXD_STAT_VP) ?
633 PKT_RX_VLAN_PKT | PKT_RX_VLAN_STRIPPED : 0);
638 static inline uint64_t
639 rx_desc_error_to_pkt_flags(uint32_t rx_error)
641 uint64_t pkt_flags = 0;
643 if (rx_error & E1000_RXD_ERR_IPE)
644 pkt_flags |= PKT_RX_IP_CKSUM_BAD;
645 if (rx_error & E1000_RXD_ERR_TCPE)
646 pkt_flags |= PKT_RX_L4_CKSUM_BAD;
651 eth_em_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
654 volatile struct e1000_rx_desc *rx_ring;
655 volatile struct e1000_rx_desc *rxdp;
656 struct em_rx_queue *rxq;
657 struct em_rx_entry *sw_ring;
658 struct em_rx_entry *rxe;
659 struct rte_mbuf *rxm;
660 struct rte_mbuf *nmb;
661 struct e1000_rx_desc rxd;
673 rx_id = rxq->rx_tail;
674 rx_ring = rxq->rx_ring;
675 sw_ring = rxq->sw_ring;
676 while (nb_rx < nb_pkts) {
678 * The order of operations here is important as the DD status
679 * bit must not be read after any other descriptor fields.
680 * rx_ring and rxdp are pointing to volatile data so the order
681 * of accesses cannot be reordered by the compiler. If they were
682 * not volatile, they could be reordered which could lead to
683 * using invalid descriptor fields when read from rxd.
685 rxdp = &rx_ring[rx_id];
686 status = rxdp->status;
687 if (! (status & E1000_RXD_STAT_DD))
694 * If the E1000_RXD_STAT_EOP flag is not set, the RX packet is
695 * likely to be invalid and to be dropped by the various
696 * validation checks performed by the network stack.
698 * Allocate a new mbuf to replenish the RX ring descriptor.
699 * If the allocation fails:
700 * - arrange for that RX descriptor to be the first one
701 * being parsed the next time the receive function is
702 * invoked [on the same queue].
704 * - Stop parsing the RX ring and return immediately.
706 * This policy do not drop the packet received in the RX
707 * descriptor for which the allocation of a new mbuf failed.
708 * Thus, it allows that packet to be later retrieved if
709 * mbuf have been freed in the mean time.
710 * As a side effect, holding RX descriptors instead of
711 * systematically giving them back to the NIC may lead to
712 * RX ring exhaustion situations.
713 * However, the NIC can gracefully prevent such situations
714 * to happen by sending specific "back-pressure" flow control
715 * frames to its peer(s).
717 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
718 "status=0x%x pkt_len=%u",
719 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
720 (unsigned) rx_id, (unsigned) status,
721 (unsigned) rte_le_to_cpu_16(rxd.length));
723 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
725 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
727 (unsigned) rxq->port_id,
728 (unsigned) rxq->queue_id);
729 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
734 rxe = &sw_ring[rx_id];
736 if (rx_id == rxq->nb_rx_desc)
739 /* Prefetch next mbuf while processing current one. */
740 rte_em_prefetch(sw_ring[rx_id].mbuf);
743 * When next RX descriptor is on a cache-line boundary,
744 * prefetch the next 4 RX descriptors and the next 8 pointers
747 if ((rx_id & 0x3) == 0) {
748 rte_em_prefetch(&rx_ring[rx_id]);
749 rte_em_prefetch(&sw_ring[rx_id]);
752 /* Rearm RXD: attach new mbuf and reset status to zero. */
757 rte_cpu_to_le_64(rte_mbuf_data_dma_addr_default(nmb));
758 rxdp->buffer_addr = dma_addr;
762 * Initialize the returned mbuf.
763 * 1) setup generic mbuf fields:
764 * - number of segments,
767 * - RX port identifier.
768 * 2) integrate hardware offload data, if any:
770 * - IP checksum flag,
771 * - VLAN TCI, if any,
774 pkt_len = (uint16_t) (rte_le_to_cpu_16(rxd.length) -
776 rxm->data_off = RTE_PKTMBUF_HEADROOM;
777 rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
780 rxm->pkt_len = pkt_len;
781 rxm->data_len = pkt_len;
782 rxm->port = rxq->port_id;
784 rxm->ol_flags = rx_desc_status_to_pkt_flags(status);
785 rxm->ol_flags = rxm->ol_flags |
786 rx_desc_error_to_pkt_flags(rxd.errors);
788 /* Only valid if PKT_RX_VLAN_PKT set in pkt_flags */
789 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
792 * Store the mbuf address into the next entry of the array
793 * of returned packets.
795 rx_pkts[nb_rx++] = rxm;
797 rxq->rx_tail = rx_id;
800 * If the number of free RX descriptors is greater than the RX free
801 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
803 * Update the RDT with the value of the last processed RX descriptor
804 * minus 1, to guarantee that the RDT register is never equal to the
805 * RDH register, which creates a "full" ring situtation from the
806 * hardware point of view...
808 nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
809 if (nb_hold > rxq->rx_free_thresh) {
810 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
811 "nb_hold=%u nb_rx=%u",
812 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
813 (unsigned) rx_id, (unsigned) nb_hold,
815 rx_id = (uint16_t) ((rx_id == 0) ?
816 (rxq->nb_rx_desc - 1) : (rx_id - 1));
817 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
820 rxq->nb_rx_hold = nb_hold;
825 eth_em_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
828 struct em_rx_queue *rxq;
829 volatile struct e1000_rx_desc *rx_ring;
830 volatile struct e1000_rx_desc *rxdp;
831 struct em_rx_entry *sw_ring;
832 struct em_rx_entry *rxe;
833 struct rte_mbuf *first_seg;
834 struct rte_mbuf *last_seg;
835 struct rte_mbuf *rxm;
836 struct rte_mbuf *nmb;
837 struct e1000_rx_desc rxd;
838 uint64_t dma; /* Physical address of mbuf data buffer */
849 rx_id = rxq->rx_tail;
850 rx_ring = rxq->rx_ring;
851 sw_ring = rxq->sw_ring;
854 * Retrieve RX context of current packet, if any.
856 first_seg = rxq->pkt_first_seg;
857 last_seg = rxq->pkt_last_seg;
859 while (nb_rx < nb_pkts) {
862 * The order of operations here is important as the DD status
863 * bit must not be read after any other descriptor fields.
864 * rx_ring and rxdp are pointing to volatile data so the order
865 * of accesses cannot be reordered by the compiler. If they were
866 * not volatile, they could be reordered which could lead to
867 * using invalid descriptor fields when read from rxd.
869 rxdp = &rx_ring[rx_id];
870 status = rxdp->status;
871 if (! (status & E1000_RXD_STAT_DD))
878 * Allocate a new mbuf to replenish the RX ring descriptor.
879 * If the allocation fails:
880 * - arrange for that RX descriptor to be the first one
881 * being parsed the next time the receive function is
882 * invoked [on the same queue].
884 * - Stop parsing the RX ring and return immediately.
886 * This policy does not drop the packet received in the RX
887 * descriptor for which the allocation of a new mbuf failed.
888 * Thus, it allows that packet to be later retrieved if
889 * mbuf have been freed in the mean time.
890 * As a side effect, holding RX descriptors instead of
891 * systematically giving them back to the NIC may lead to
892 * RX ring exhaustion situations.
893 * However, the NIC can gracefully prevent such situations
894 * to happen by sending specific "back-pressure" flow control
895 * frames to its peer(s).
897 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
898 "status=0x%x data_len=%u",
899 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
900 (unsigned) rx_id, (unsigned) status,
901 (unsigned) rte_le_to_cpu_16(rxd.length));
903 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
905 PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
906 "queue_id=%u", (unsigned) rxq->port_id,
907 (unsigned) rxq->queue_id);
908 rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
913 rxe = &sw_ring[rx_id];
915 if (rx_id == rxq->nb_rx_desc)
918 /* Prefetch next mbuf while processing current one. */
919 rte_em_prefetch(sw_ring[rx_id].mbuf);
922 * When next RX descriptor is on a cache-line boundary,
923 * prefetch the next 4 RX descriptors and the next 8 pointers
926 if ((rx_id & 0x3) == 0) {
927 rte_em_prefetch(&rx_ring[rx_id]);
928 rte_em_prefetch(&sw_ring[rx_id]);
932 * Update RX descriptor with the physical address of the new
933 * data buffer of the new allocated mbuf.
937 dma = rte_cpu_to_le_64(rte_mbuf_data_dma_addr_default(nmb));
938 rxdp->buffer_addr = dma;
942 * Set data length & data buffer address of mbuf.
944 data_len = rte_le_to_cpu_16(rxd.length);
945 rxm->data_len = data_len;
946 rxm->data_off = RTE_PKTMBUF_HEADROOM;
949 * If this is the first buffer of the received packet,
950 * set the pointer to the first mbuf of the packet and
951 * initialize its context.
952 * Otherwise, update the total length and the number of segments
953 * of the current scattered packet, and update the pointer to
954 * the last mbuf of the current packet.
956 if (first_seg == NULL) {
958 first_seg->pkt_len = data_len;
959 first_seg->nb_segs = 1;
961 first_seg->pkt_len += data_len;
962 first_seg->nb_segs++;
963 last_seg->next = rxm;
967 * If this is not the last buffer of the received packet,
968 * update the pointer to the last mbuf of the current scattered
969 * packet and continue to parse the RX ring.
971 if (! (status & E1000_RXD_STAT_EOP)) {
977 * This is the last buffer of the received packet.
978 * If the CRC is not stripped by the hardware:
979 * - Subtract the CRC length from the total packet length.
980 * - If the last buffer only contains the whole CRC or a part
981 * of it, free the mbuf associated to the last buffer.
982 * If part of the CRC is also contained in the previous
983 * mbuf, subtract the length of that CRC part from the
984 * data length of the previous mbuf.
987 if (unlikely(rxq->crc_len > 0)) {
988 first_seg->pkt_len -= ETHER_CRC_LEN;
989 if (data_len <= ETHER_CRC_LEN) {
990 rte_pktmbuf_free_seg(rxm);
991 first_seg->nb_segs--;
992 last_seg->data_len = (uint16_t)
993 (last_seg->data_len -
994 (ETHER_CRC_LEN - data_len));
995 last_seg->next = NULL;
998 (uint16_t) (data_len - ETHER_CRC_LEN);
1002 * Initialize the first mbuf of the returned packet:
1003 * - RX port identifier,
1004 * - hardware offload data, if any:
1005 * - IP checksum flag,
1008 first_seg->port = rxq->port_id;
1010 first_seg->ol_flags = rx_desc_status_to_pkt_flags(status);
1011 first_seg->ol_flags = first_seg->ol_flags |
1012 rx_desc_error_to_pkt_flags(rxd.errors);
1014 /* Only valid if PKT_RX_VLAN_PKT set in pkt_flags */
1015 rxm->vlan_tci = rte_le_to_cpu_16(rxd.special);
1017 /* Prefetch data of first segment, if configured to do so. */
1018 rte_packet_prefetch((char *)first_seg->buf_addr +
1019 first_seg->data_off);
1022 * Store the mbuf address into the next entry of the array
1023 * of returned packets.
1025 rx_pkts[nb_rx++] = first_seg;
1028 * Setup receipt context for a new packet.
1034 * Record index of the next RX descriptor to probe.
1036 rxq->rx_tail = rx_id;
1039 * Save receive context.
1041 rxq->pkt_first_seg = first_seg;
1042 rxq->pkt_last_seg = last_seg;
1045 * If the number of free RX descriptors is greater than the RX free
1046 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1048 * Update the RDT with the value of the last processed RX descriptor
1049 * minus 1, to guarantee that the RDT register is never equal to the
1050 * RDH register, which creates a "full" ring situtation from the
1051 * hardware point of view...
1053 nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
1054 if (nb_hold > rxq->rx_free_thresh) {
1055 PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1056 "nb_hold=%u nb_rx=%u",
1057 (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1058 (unsigned) rx_id, (unsigned) nb_hold,
1060 rx_id = (uint16_t) ((rx_id == 0) ?
1061 (rxq->nb_rx_desc - 1) : (rx_id - 1));
1062 E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1065 rxq->nb_rx_hold = nb_hold;
1069 #define EM_MAX_BUF_SIZE 16384
1070 #define EM_RCTL_FLXBUF_STEP 1024
1073 em_tx_queue_release_mbufs(struct em_tx_queue *txq)
1077 if (txq->sw_ring != NULL) {
1078 for (i = 0; i != txq->nb_tx_desc; i++) {
1079 if (txq->sw_ring[i].mbuf != NULL) {
1080 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1081 txq->sw_ring[i].mbuf = NULL;
1088 em_tx_queue_release(struct em_tx_queue *txq)
1091 em_tx_queue_release_mbufs(txq);
1092 rte_free(txq->sw_ring);
1098 eth_em_tx_queue_release(void *txq)
1100 em_tx_queue_release(txq);
1103 /* (Re)set dynamic em_tx_queue fields to defaults */
1105 em_reset_tx_queue(struct em_tx_queue *txq)
1107 uint16_t i, nb_desc, prev;
1108 static const struct e1000_data_desc txd_init = {
1109 .upper.fields = {.status = E1000_TXD_STAT_DD},
1112 nb_desc = txq->nb_tx_desc;
1114 /* Initialize ring entries */
1116 prev = (uint16_t) (nb_desc - 1);
1118 for (i = 0; i < nb_desc; i++) {
1119 txq->tx_ring[i] = txd_init;
1120 txq->sw_ring[i].mbuf = NULL;
1121 txq->sw_ring[i].last_id = i;
1122 txq->sw_ring[prev].next_id = i;
1127 * Always allow 1 descriptor to be un-allocated to avoid
1128 * a H/W race condition
1130 txq->nb_tx_free = (uint16_t)(nb_desc - 1);
1131 txq->last_desc_cleaned = (uint16_t)(nb_desc - 1);
1132 txq->nb_tx_used = 0;
1135 memset((void*)&txq->ctx_cache, 0, sizeof (txq->ctx_cache));
1139 eth_em_tx_queue_setup(struct rte_eth_dev *dev,
1142 unsigned int socket_id,
1143 const struct rte_eth_txconf *tx_conf)
1145 const struct rte_memzone *tz;
1146 struct em_tx_queue *txq;
1147 struct e1000_hw *hw;
1149 uint16_t tx_rs_thresh, tx_free_thresh;
1151 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1154 * Validate number of transmit descriptors.
1155 * It must not exceed hardware maximum, and must be multiple
1158 if (nb_desc % EM_TXD_ALIGN != 0 ||
1159 (nb_desc > E1000_MAX_RING_DESC) ||
1160 (nb_desc < E1000_MIN_RING_DESC)) {
1164 tx_free_thresh = tx_conf->tx_free_thresh;
1165 if (tx_free_thresh == 0)
1166 tx_free_thresh = (uint16_t)RTE_MIN(nb_desc / 4,
1167 DEFAULT_TX_FREE_THRESH);
1169 tx_rs_thresh = tx_conf->tx_rs_thresh;
1170 if (tx_rs_thresh == 0)
1171 tx_rs_thresh = (uint16_t)RTE_MIN(tx_free_thresh,
1172 DEFAULT_TX_RS_THRESH);
1174 if (tx_free_thresh >= (nb_desc - 3)) {
1175 PMD_INIT_LOG(ERR, "tx_free_thresh must be less than the "
1176 "number of TX descriptors minus 3. "
1177 "(tx_free_thresh=%u port=%d queue=%d)",
1178 (unsigned int)tx_free_thresh,
1179 (int)dev->data->port_id, (int)queue_idx);
1182 if (tx_rs_thresh > tx_free_thresh) {
1183 PMD_INIT_LOG(ERR, "tx_rs_thresh must be less than or equal to "
1184 "tx_free_thresh. (tx_free_thresh=%u "
1185 "tx_rs_thresh=%u port=%d queue=%d)",
1186 (unsigned int)tx_free_thresh,
1187 (unsigned int)tx_rs_thresh,
1188 (int)dev->data->port_id,
1194 * If rs_bit_thresh is greater than 1, then TX WTHRESH should be
1195 * set to 0. If WTHRESH is greater than zero, the RS bit is ignored
1196 * by the NIC and all descriptors are written back after the NIC
1197 * accumulates WTHRESH descriptors.
1199 if (tx_conf->tx_thresh.wthresh != 0 && tx_rs_thresh != 1) {
1200 PMD_INIT_LOG(ERR, "TX WTHRESH must be set to 0 if "
1201 "tx_rs_thresh is greater than 1. (tx_rs_thresh=%u "
1202 "port=%d queue=%d)", (unsigned int)tx_rs_thresh,
1203 (int)dev->data->port_id, (int)queue_idx);
1207 /* Free memory prior to re-allocation if needed... */
1208 if (dev->data->tx_queues[queue_idx] != NULL) {
1209 em_tx_queue_release(dev->data->tx_queues[queue_idx]);
1210 dev->data->tx_queues[queue_idx] = NULL;
1214 * Allocate TX ring hardware descriptors. A memzone large enough to
1215 * handle the maximum ring size is allocated in order to allow for
1216 * resizing in later calls to the queue setup function.
1218 tsize = sizeof(txq->tx_ring[0]) * E1000_MAX_RING_DESC;
1219 tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx, tsize,
1220 RTE_CACHE_LINE_SIZE, socket_id);
1224 /* Allocate the tx queue data structure. */
1225 if ((txq = rte_zmalloc("ethdev TX queue", sizeof(*txq),
1226 RTE_CACHE_LINE_SIZE)) == NULL)
1229 /* Allocate software ring */
1230 if ((txq->sw_ring = rte_zmalloc("txq->sw_ring",
1231 sizeof(txq->sw_ring[0]) * nb_desc,
1232 RTE_CACHE_LINE_SIZE)) == NULL) {
1233 em_tx_queue_release(txq);
1237 txq->nb_tx_desc = nb_desc;
1238 txq->tx_free_thresh = tx_free_thresh;
1239 txq->tx_rs_thresh = tx_rs_thresh;
1240 txq->pthresh = tx_conf->tx_thresh.pthresh;
1241 txq->hthresh = tx_conf->tx_thresh.hthresh;
1242 txq->wthresh = tx_conf->tx_thresh.wthresh;
1243 txq->queue_id = queue_idx;
1244 txq->port_id = dev->data->port_id;
1246 txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(queue_idx));
1247 txq->tx_ring_phys_addr = rte_mem_phy2mch(tz->memseg_id, tz->phys_addr);
1248 txq->tx_ring = (struct e1000_data_desc *) tz->addr;
1250 PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1251 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1253 em_reset_tx_queue(txq);
1255 dev->data->tx_queues[queue_idx] = txq;
1260 em_rx_queue_release_mbufs(struct em_rx_queue *rxq)
1264 if (rxq->sw_ring != NULL) {
1265 for (i = 0; i != rxq->nb_rx_desc; i++) {
1266 if (rxq->sw_ring[i].mbuf != NULL) {
1267 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1268 rxq->sw_ring[i].mbuf = NULL;
1275 em_rx_queue_release(struct em_rx_queue *rxq)
1278 em_rx_queue_release_mbufs(rxq);
1279 rte_free(rxq->sw_ring);
1285 eth_em_rx_queue_release(void *rxq)
1287 em_rx_queue_release(rxq);
1290 /* Reset dynamic em_rx_queue fields back to defaults */
1292 em_reset_rx_queue(struct em_rx_queue *rxq)
1295 rxq->nb_rx_hold = 0;
1296 rxq->pkt_first_seg = NULL;
1297 rxq->pkt_last_seg = NULL;
1301 eth_em_rx_queue_setup(struct rte_eth_dev *dev,
1304 unsigned int socket_id,
1305 const struct rte_eth_rxconf *rx_conf,
1306 struct rte_mempool *mp)
1308 const struct rte_memzone *rz;
1309 struct em_rx_queue *rxq;
1310 struct e1000_hw *hw;
1313 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1316 * Validate number of receive descriptors.
1317 * It must not exceed hardware maximum, and must be multiple
1320 if (nb_desc % EM_RXD_ALIGN != 0 ||
1321 (nb_desc > E1000_MAX_RING_DESC) ||
1322 (nb_desc < E1000_MIN_RING_DESC)) {
1327 * EM devices don't support drop_en functionality
1329 if (rx_conf->rx_drop_en) {
1330 PMD_INIT_LOG(ERR, "drop_en functionality not supported by "
1335 /* Free memory prior to re-allocation if needed. */
1336 if (dev->data->rx_queues[queue_idx] != NULL) {
1337 em_rx_queue_release(dev->data->rx_queues[queue_idx]);
1338 dev->data->rx_queues[queue_idx] = NULL;
1341 /* Allocate RX ring for max possible mumber of hardware descriptors. */
1342 rsize = sizeof(rxq->rx_ring[0]) * E1000_MAX_RING_DESC;
1343 rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx, rsize,
1344 RTE_CACHE_LINE_SIZE, socket_id);
1348 /* Allocate the RX queue data structure. */
1349 if ((rxq = rte_zmalloc("ethdev RX queue", sizeof(*rxq),
1350 RTE_CACHE_LINE_SIZE)) == NULL)
1353 /* Allocate software ring. */
1354 if ((rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1355 sizeof (rxq->sw_ring[0]) * nb_desc,
1356 RTE_CACHE_LINE_SIZE)) == NULL) {
1357 em_rx_queue_release(rxq);
1362 rxq->nb_rx_desc = nb_desc;
1363 rxq->pthresh = rx_conf->rx_thresh.pthresh;
1364 rxq->hthresh = rx_conf->rx_thresh.hthresh;
1365 rxq->wthresh = rx_conf->rx_thresh.wthresh;
1366 rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1367 rxq->queue_id = queue_idx;
1368 rxq->port_id = dev->data->port_id;
1369 rxq->crc_len = (uint8_t) ((dev->data->dev_conf.rxmode.hw_strip_crc) ?
1372 rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(queue_idx));
1373 rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(queue_idx));
1374 rxq->rx_ring_phys_addr = rte_mem_phy2mch(rz->memseg_id, rz->phys_addr);
1375 rxq->rx_ring = (struct e1000_rx_desc *) rz->addr;
1377 PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1378 rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1380 dev->data->rx_queues[queue_idx] = rxq;
1381 em_reset_rx_queue(rxq);
1387 eth_em_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1389 #define EM_RXQ_SCAN_INTERVAL 4
1390 volatile struct e1000_rx_desc *rxdp;
1391 struct em_rx_queue *rxq;
1394 if (rx_queue_id >= dev->data->nb_rx_queues) {
1395 PMD_RX_LOG(DEBUG, "Invalid RX queue_id=%d", rx_queue_id);
1399 rxq = dev->data->rx_queues[rx_queue_id];
1400 rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1402 while ((desc < rxq->nb_rx_desc) &&
1403 (rxdp->status & E1000_RXD_STAT_DD)) {
1404 desc += EM_RXQ_SCAN_INTERVAL;
1405 rxdp += EM_RXQ_SCAN_INTERVAL;
1406 if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1407 rxdp = &(rxq->rx_ring[rxq->rx_tail +
1408 desc - rxq->nb_rx_desc]);
1415 eth_em_rx_descriptor_done(void *rx_queue, uint16_t offset)
1417 volatile struct e1000_rx_desc *rxdp;
1418 struct em_rx_queue *rxq = rx_queue;
1421 if (unlikely(offset >= rxq->nb_rx_desc))
1423 desc = rxq->rx_tail + offset;
1424 if (desc >= rxq->nb_rx_desc)
1425 desc -= rxq->nb_rx_desc;
1427 rxdp = &rxq->rx_ring[desc];
1428 return !!(rxdp->status & E1000_RXD_STAT_DD);
1432 em_dev_clear_queues(struct rte_eth_dev *dev)
1435 struct em_tx_queue *txq;
1436 struct em_rx_queue *rxq;
1438 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1439 txq = dev->data->tx_queues[i];
1441 em_tx_queue_release_mbufs(txq);
1442 em_reset_tx_queue(txq);
1446 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1447 rxq = dev->data->rx_queues[i];
1449 em_rx_queue_release_mbufs(rxq);
1450 em_reset_rx_queue(rxq);
1456 em_dev_free_queues(struct rte_eth_dev *dev)
1460 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1461 eth_em_rx_queue_release(dev->data->rx_queues[i]);
1462 dev->data->rx_queues[i] = NULL;
1464 dev->data->nb_rx_queues = 0;
1466 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1467 eth_em_tx_queue_release(dev->data->tx_queues[i]);
1468 dev->data->tx_queues[i] = NULL;
1470 dev->data->nb_tx_queues = 0;
1474 * Takes as input/output parameter RX buffer size.
1475 * Returns (BSIZE | BSEX | FLXBUF) fields of RCTL register.
1478 em_rctl_bsize(__rte_unused enum e1000_mac_type hwtyp, uint32_t *bufsz)
1481 * For BSIZE & BSEX all configurable sizes are:
1482 * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
1483 * 8192: rctl |= (E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX);
1484 * 4096: rctl |= (E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX);
1485 * 2048: rctl |= E1000_RCTL_SZ_2048;
1486 * 1024: rctl |= E1000_RCTL_SZ_1024;
1487 * 512: rctl |= E1000_RCTL_SZ_512;
1488 * 256: rctl |= E1000_RCTL_SZ_256;
1490 static const struct {
1493 } bufsz_to_rctl[] = {
1494 {16384, (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX)},
1495 {8192, (E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX)},
1496 {4096, (E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX)},
1497 {2048, E1000_RCTL_SZ_2048},
1498 {1024, E1000_RCTL_SZ_1024},
1499 {512, E1000_RCTL_SZ_512},
1500 {256, E1000_RCTL_SZ_256},
1504 uint32_t rctl_bsize;
1506 rctl_bsize = *bufsz;
1509 * Starting from 82571 it is possible to specify RX buffer size
1510 * by RCTL.FLXBUF. When this field is different from zero, the
1511 * RX buffer size = RCTL.FLXBUF * 1K
1512 * (e.g. t is possible to specify RX buffer size 1,2,...,15KB).
1513 * It is working ok on real HW, but by some reason doesn't work
1514 * on VMware emulated 82574L.
1515 * So for now, always use BSIZE/BSEX to setup RX buffer size.
1516 * If you don't plan to use it on VMware emulated 82574L and
1517 * would like to specify RX buffer size in 1K granularity,
1518 * uncomment the following lines:
1519 * ***************************************************************
1520 * if (hwtyp >= e1000_82571 && hwtyp <= e1000_82574 &&
1521 * rctl_bsize >= EM_RCTL_FLXBUF_STEP) {
1522 * rctl_bsize /= EM_RCTL_FLXBUF_STEP;
1523 * *bufsz = rctl_bsize;
1524 * return (rctl_bsize << E1000_RCTL_FLXBUF_SHIFT &
1525 * E1000_RCTL_FLXBUF_MASK);
1527 * ***************************************************************
1530 for (i = 0; i != sizeof(bufsz_to_rctl) / sizeof(bufsz_to_rctl[0]);
1532 if (rctl_bsize >= bufsz_to_rctl[i].bufsz) {
1533 *bufsz = bufsz_to_rctl[i].bufsz;
1534 return bufsz_to_rctl[i].rctl;
1538 /* Should never happen. */
1543 em_alloc_rx_queue_mbufs(struct em_rx_queue *rxq)
1545 struct em_rx_entry *rxe = rxq->sw_ring;
1548 static const struct e1000_rx_desc rxd_init = {
1552 /* Initialize software ring entries */
1553 for (i = 0; i < rxq->nb_rx_desc; i++) {
1554 volatile struct e1000_rx_desc *rxd;
1555 struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
1558 PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
1559 "queue_id=%hu", rxq->queue_id);
1564 rte_cpu_to_le_64(rte_mbuf_data_dma_addr_default(mbuf));
1566 /* Clear HW ring memory */
1567 rxq->rx_ring[i] = rxd_init;
1569 rxd = &rxq->rx_ring[i];
1570 rxd->buffer_addr = dma_addr;
1577 /*********************************************************************
1579 * Enable receive unit.
1581 **********************************************************************/
1583 eth_em_rx_init(struct rte_eth_dev *dev)
1585 struct e1000_hw *hw;
1586 struct em_rx_queue *rxq;
1590 uint32_t rctl_bsize;
1594 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1597 * Make sure receives are disabled while setting
1598 * up the descriptor ring.
1600 rctl = E1000_READ_REG(hw, E1000_RCTL);
1601 E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
1603 rfctl = E1000_READ_REG(hw, E1000_RFCTL);
1605 /* Disable extended descriptor type. */
1606 rfctl &= ~E1000_RFCTL_EXTEN;
1607 /* Disable accelerated acknowledge */
1608 if (hw->mac.type == e1000_82574)
1609 rfctl |= E1000_RFCTL_ACK_DIS;
1611 E1000_WRITE_REG(hw, E1000_RFCTL, rfctl);
1614 * XXX TEMPORARY WORKAROUND: on some systems with 82573
1615 * long latencies are observed, like Lenovo X60. This
1616 * change eliminates the problem, but since having positive
1617 * values in RDTR is a known source of problems on other
1618 * platforms another solution is being sought.
1620 if (hw->mac.type == e1000_82573)
1621 E1000_WRITE_REG(hw, E1000_RDTR, 0x20);
1623 dev->rx_pkt_burst = (eth_rx_burst_t)eth_em_recv_pkts;
1625 /* Determine RX bufsize. */
1626 rctl_bsize = EM_MAX_BUF_SIZE;
1627 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1630 rxq = dev->data->rx_queues[i];
1631 buf_size = rte_pktmbuf_data_room_size(rxq->mb_pool) -
1632 RTE_PKTMBUF_HEADROOM;
1633 rctl_bsize = RTE_MIN(rctl_bsize, buf_size);
1636 rctl |= em_rctl_bsize(hw->mac.type, &rctl_bsize);
1638 /* Configure and enable each RX queue. */
1639 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1643 rxq = dev->data->rx_queues[i];
1645 /* Allocate buffers for descriptor rings and setup queue */
1646 ret = em_alloc_rx_queue_mbufs(rxq);
1651 * Reset crc_len in case it was changed after queue setup by a
1655 (uint8_t)(dev->data->dev_conf.rxmode.hw_strip_crc ?
1658 bus_addr = rxq->rx_ring_phys_addr;
1659 E1000_WRITE_REG(hw, E1000_RDLEN(i),
1661 sizeof(*rxq->rx_ring));
1662 E1000_WRITE_REG(hw, E1000_RDBAH(i),
1663 (uint32_t)(bus_addr >> 32));
1664 E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
1666 E1000_WRITE_REG(hw, E1000_RDH(i), 0);
1667 E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
1669 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1670 rxdctl &= 0xFE000000;
1671 rxdctl |= rxq->pthresh & 0x3F;
1672 rxdctl |= (rxq->hthresh & 0x3F) << 8;
1673 rxdctl |= (rxq->wthresh & 0x3F) << 16;
1674 rxdctl |= E1000_RXDCTL_GRAN;
1675 E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
1678 * Due to EM devices not having any sort of hardware
1679 * limit for packet length, jumbo frame of any size
1680 * can be accepted, thus we have to enable scattered
1681 * rx if jumbo frames are enabled (or if buffer size
1682 * is too small to accommodate non-jumbo packets)
1683 * to avoid splitting packets that don't fit into
1686 if (dev->data->dev_conf.rxmode.jumbo_frame ||
1687 rctl_bsize < ETHER_MAX_LEN) {
1688 if (!dev->data->scattered_rx)
1689 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1691 (eth_rx_burst_t)eth_em_recv_scattered_pkts;
1692 dev->data->scattered_rx = 1;
1696 if (dev->data->dev_conf.rxmode.enable_scatter) {
1697 if (!dev->data->scattered_rx)
1698 PMD_INIT_LOG(DEBUG, "forcing scatter mode");
1699 dev->rx_pkt_burst = eth_em_recv_scattered_pkts;
1700 dev->data->scattered_rx = 1;
1704 * Setup the Checksum Register.
1705 * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
1707 rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
1709 if (dev->data->dev_conf.rxmode.hw_ip_checksum)
1710 rxcsum |= E1000_RXCSUM_IPOFL;
1712 rxcsum &= ~E1000_RXCSUM_IPOFL;
1713 E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
1715 /* No MRQ or RSS support for now */
1717 /* Set early receive threshold on appropriate hw */
1718 if ((hw->mac.type == e1000_ich9lan ||
1719 hw->mac.type == e1000_pch2lan ||
1720 hw->mac.type == e1000_ich10lan) &&
1721 dev->data->dev_conf.rxmode.jumbo_frame == 1) {
1722 u32 rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(0));
1723 E1000_WRITE_REG(hw, E1000_RXDCTL(0), rxdctl | 3);
1724 E1000_WRITE_REG(hw, E1000_ERT, 0x100 | (1 << 13));
1727 if (hw->mac.type == e1000_pch2lan) {
1728 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1729 e1000_lv_jumbo_workaround_ich8lan(hw, TRUE);
1731 e1000_lv_jumbo_workaround_ich8lan(hw, FALSE);
1734 /* Setup the Receive Control Register. */
1735 if (dev->data->dev_conf.rxmode.hw_strip_crc)
1736 rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
1738 rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
1740 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1741 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
1742 E1000_RCTL_RDMTS_HALF |
1743 (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
1745 /* Make sure VLAN Filters are off. */
1746 rctl &= ~E1000_RCTL_VFE;
1747 /* Don't store bad packets. */
1748 rctl &= ~E1000_RCTL_SBP;
1749 /* Legacy descriptor type. */
1750 rctl &= ~E1000_RCTL_DTYP_MASK;
1753 * Configure support of jumbo frames, if any.
1755 if (dev->data->dev_conf.rxmode.jumbo_frame == 1)
1756 rctl |= E1000_RCTL_LPE;
1758 rctl &= ~E1000_RCTL_LPE;
1760 /* Enable Receives. */
1761 E1000_WRITE_REG(hw, E1000_RCTL, rctl);
1766 /*********************************************************************
1768 * Enable transmit unit.
1770 **********************************************************************/
1772 eth_em_tx_init(struct rte_eth_dev *dev)
1774 struct e1000_hw *hw;
1775 struct em_tx_queue *txq;
1780 hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1782 /* Setup the Base and Length of the Tx Descriptor Rings. */
1783 for (i = 0; i < dev->data->nb_tx_queues; i++) {
1786 txq = dev->data->tx_queues[i];
1787 bus_addr = txq->tx_ring_phys_addr;
1788 E1000_WRITE_REG(hw, E1000_TDLEN(i),
1790 sizeof(*txq->tx_ring));
1791 E1000_WRITE_REG(hw, E1000_TDBAH(i),
1792 (uint32_t)(bus_addr >> 32));
1793 E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
1795 /* Setup the HW Tx Head and Tail descriptor pointers. */
1796 E1000_WRITE_REG(hw, E1000_TDT(i), 0);
1797 E1000_WRITE_REG(hw, E1000_TDH(i), 0);
1799 /* Setup Transmit threshold registers. */
1800 txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
1802 * bit 22 is reserved, on some models should always be 0,
1803 * on others - always 1.
1805 txdctl &= E1000_TXDCTL_COUNT_DESC;
1806 txdctl |= txq->pthresh & 0x3F;
1807 txdctl |= (txq->hthresh & 0x3F) << 8;
1808 txdctl |= (txq->wthresh & 0x3F) << 16;
1809 txdctl |= E1000_TXDCTL_GRAN;
1810 E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
1813 /* Program the Transmit Control Register. */
1814 tctl = E1000_READ_REG(hw, E1000_TCTL);
1815 tctl &= ~E1000_TCTL_CT;
1816 tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
1817 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
1819 /* This write will effectively turn on the transmit unit. */
1820 E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1824 em_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1825 struct rte_eth_rxq_info *qinfo)
1827 struct em_rx_queue *rxq;
1829 rxq = dev->data->rx_queues[queue_id];
1831 qinfo->mp = rxq->mb_pool;
1832 qinfo->scattered_rx = dev->data->scattered_rx;
1833 qinfo->nb_desc = rxq->nb_rx_desc;
1834 qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
1838 em_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
1839 struct rte_eth_txq_info *qinfo)
1841 struct em_tx_queue *txq;
1843 txq = dev->data->tx_queues[queue_id];
1845 qinfo->nb_desc = txq->nb_tx_desc;
1847 qinfo->conf.tx_thresh.pthresh = txq->pthresh;
1848 qinfo->conf.tx_thresh.hthresh = txq->hthresh;
1849 qinfo->conf.tx_thresh.wthresh = txq->wthresh;
1850 qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
1851 qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;