xref: /OK3568_Linux_fs/external/dpdk/pcie/e1000/igb_rxtx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: BSD-3-Clause
2*4882a593Smuzhiyun  * Copyright(c) 2010-2016 Intel Corporation
3*4882a593Smuzhiyun  */
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <sys/queue.h>
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <stdio.h>
8*4882a593Smuzhiyun #include <stdlib.h>
9*4882a593Smuzhiyun #include <string.h>
10*4882a593Smuzhiyun #include <errno.h>
11*4882a593Smuzhiyun #include <stdint.h>
12*4882a593Smuzhiyun #include <stdarg.h>
13*4882a593Smuzhiyun #include <inttypes.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <rte_interrupts.h>
16*4882a593Smuzhiyun #include <rte_byteorder.h>
17*4882a593Smuzhiyun #include <rte_common.h>
18*4882a593Smuzhiyun #include <rte_log.h>
19*4882a593Smuzhiyun #include <rte_debug.h>
20*4882a593Smuzhiyun #include <rte_pci.h>
21*4882a593Smuzhiyun #include <rte_memory.h>
22*4882a593Smuzhiyun #include <rte_memcpy.h>
23*4882a593Smuzhiyun #include <rte_memzone.h>
24*4882a593Smuzhiyun #include <rte_launch.h>
25*4882a593Smuzhiyun #include <rte_eal.h>
26*4882a593Smuzhiyun #include <rte_per_lcore.h>
27*4882a593Smuzhiyun #include <rte_lcore.h>
28*4882a593Smuzhiyun #include <rte_atomic.h>
29*4882a593Smuzhiyun #include <rte_branch_prediction.h>
30*4882a593Smuzhiyun #include <rte_mempool.h>
31*4882a593Smuzhiyun #include <rte_malloc.h>
32*4882a593Smuzhiyun #include <rte_mbuf.h>
33*4882a593Smuzhiyun #include <rte_ether.h>
34*4882a593Smuzhiyun #include <ethdev_driver.h>
35*4882a593Smuzhiyun #include <rte_prefetch.h>
36*4882a593Smuzhiyun #include <rte_udp.h>
37*4882a593Smuzhiyun #include <rte_tcp.h>
38*4882a593Smuzhiyun #include <rte_sctp.h>
39*4882a593Smuzhiyun #include <rte_net.h>
40*4882a593Smuzhiyun #include <rte_string_fns.h>
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #include "e1000_logs.h"
43*4882a593Smuzhiyun #include "base/e1000_api.h"
44*4882a593Smuzhiyun #include "e1000_ethdev.h"
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define dcbf(p) { asm volatile("dc cvac, %0" : : "r"(p) : "memory"); }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun extern uint32_t		igb_gbd_addr_b_p[4];
49*4882a593Smuzhiyun extern uint32_t		igb_gbd_addr_r_p[4];
50*4882a593Smuzhiyun extern uint32_t		igb_gbd_addr_t_p[4];
51*4882a593Smuzhiyun extern uint32_t		igb_gbd_addr_x_p[4];
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun extern void			*igb_gbd_addr_b_v[4];
54*4882a593Smuzhiyun extern void			*igb_gbd_addr_t_v[4];
55*4882a593Smuzhiyun extern void			*igb_gbd_addr_r_v[4];
56*4882a593Smuzhiyun extern void			*igb_gbd_addr_x_v[4];
57*4882a593Smuzhiyun extern uint64_t base_hw_addr;
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun #ifdef RTE_LIBRTE_IEEE1588
60*4882a593Smuzhiyun #define IGB_TX_IEEE1588_TMST RTE_MBUF_F_TX_IEEE1588_TMST
61*4882a593Smuzhiyun #else
62*4882a593Smuzhiyun #define IGB_TX_IEEE1588_TMST 0
63*4882a593Smuzhiyun #endif
64*4882a593Smuzhiyun /* Bit Mask to indicate what bits required for building TX context */
65*4882a593Smuzhiyun #define IGB_TX_OFFLOAD_MASK (RTE_MBUF_F_TX_OUTER_IPV6 |	 \
66*4882a593Smuzhiyun 		RTE_MBUF_F_TX_OUTER_IPV4 |	 \
67*4882a593Smuzhiyun 		RTE_MBUF_F_TX_IPV6 |		 \
68*4882a593Smuzhiyun 		RTE_MBUF_F_TX_IPV4 |		 \
69*4882a593Smuzhiyun 		RTE_MBUF_F_TX_VLAN |		 \
70*4882a593Smuzhiyun 		RTE_MBUF_F_TX_IP_CKSUM |		 \
71*4882a593Smuzhiyun 		RTE_MBUF_F_TX_L4_MASK |		 \
72*4882a593Smuzhiyun 		RTE_MBUF_F_TX_TCP_SEG |		 \
73*4882a593Smuzhiyun 		IGB_TX_IEEE1588_TMST)
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define IGB_TX_OFFLOAD_NOTSUP_MASK \
76*4882a593Smuzhiyun 		(RTE_MBUF_F_TX_OFFLOAD_MASK ^ IGB_TX_OFFLOAD_MASK)
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun /**
79*4882a593Smuzhiyun  * Structure associated with each descriptor of the RX ring of a RX queue.
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun struct igb_rx_entry {
82*4882a593Smuzhiyun 	struct rte_mbuf *mbuf; /**< mbuf associated with RX descriptor. */
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun  * Structure associated with each descriptor of the TX ring of a TX queue.
87*4882a593Smuzhiyun  */
88*4882a593Smuzhiyun struct igb_tx_entry {
89*4882a593Smuzhiyun 	struct rte_mbuf *mbuf; /**< mbuf associated with TX desc, if any. */
90*4882a593Smuzhiyun 	uint16_t next_id; /**< Index of next descriptor in ring. */
91*4882a593Smuzhiyun 	uint16_t last_id; /**< Index of last scattered descriptor. */
92*4882a593Smuzhiyun };
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun  * rx queue flags
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun enum igb_rxq_flags {
98*4882a593Smuzhiyun 	IGB_RXQ_FLAG_LB_BSWAP_VLAN = 0x01,
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /**
102*4882a593Smuzhiyun  * Structure associated with each RX queue.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun struct igb_rx_queue {
105*4882a593Smuzhiyun 	struct rte_mempool  *mb_pool;   /**< mbuf pool to populate RX ring. */
106*4882a593Smuzhiyun 	volatile union e1000_adv_rx_desc *rx_ring; /**< RX ring virtual address. */
107*4882a593Smuzhiyun 	uint64_t            rx_ring_phys_addr; /**< RX ring DMA address. */
108*4882a593Smuzhiyun 	volatile uint32_t   *rdt_reg_addr; /**< RDT register address. */
109*4882a593Smuzhiyun 	volatile uint32_t   *rdh_reg_addr; /**< RDH register address. */
110*4882a593Smuzhiyun 	struct igb_rx_entry *sw_ring;   /**< address of RX software ring. */
111*4882a593Smuzhiyun 	struct rte_mbuf *pkt_first_seg; /**< First segment of current packet. */
112*4882a593Smuzhiyun 	struct rte_mbuf *pkt_last_seg;  /**< Last segment of current packet. */
113*4882a593Smuzhiyun 	uint16_t            nb_rx_desc; /**< number of RX descriptors. */
114*4882a593Smuzhiyun 	uint16_t            rx_tail;    /**< current value of RDT register. */
115*4882a593Smuzhiyun 	uint16_t            nb_rx_hold; /**< number of held free RX desc. */
116*4882a593Smuzhiyun 	uint16_t            rx_free_thresh; /**< max free RX desc to hold. */
117*4882a593Smuzhiyun 	uint16_t            queue_id;   /**< RX queue index. */
118*4882a593Smuzhiyun 	uint16_t            reg_idx;    /**< RX queue register index. */
119*4882a593Smuzhiyun 	uint16_t            port_id;    /**< Device port identifier. */
120*4882a593Smuzhiyun 	uint8_t             pthresh;    /**< Prefetch threshold register. */
121*4882a593Smuzhiyun 	uint8_t             hthresh;    /**< Host threshold register. */
122*4882a593Smuzhiyun 	uint8_t             wthresh;    /**< Write-back threshold register. */
123*4882a593Smuzhiyun 	uint8_t             crc_len;    /**< 0 if CRC stripped, 4 otherwise. */
124*4882a593Smuzhiyun 	uint8_t             drop_en;  /**< If not 0, set SRRCTL.Drop_En. */
125*4882a593Smuzhiyun 	uint32_t            flags;      /**< RX flags. */
126*4882a593Smuzhiyun 	uint64_t	    offloads;   /**< offloads of RTE_ETH_RX_OFFLOAD_* */
127*4882a593Smuzhiyun 	const struct rte_memzone *mz;
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun  * Hardware context number
132*4882a593Smuzhiyun  */
133*4882a593Smuzhiyun enum igb_advctx_num {
134*4882a593Smuzhiyun 	IGB_CTX_0    = 0, /**< CTX0    */
135*4882a593Smuzhiyun 	IGB_CTX_1    = 1, /**< CTX1    */
136*4882a593Smuzhiyun 	IGB_CTX_NUM  = 2, /**< CTX_NUM */
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /** Offload features */
140*4882a593Smuzhiyun union igb_tx_offload {
141*4882a593Smuzhiyun 	uint64_t data;
142*4882a593Smuzhiyun 	struct {
143*4882a593Smuzhiyun 		uint64_t l3_len:9; /**< L3 (IP) Header Length. */
144*4882a593Smuzhiyun 		uint64_t l2_len:7; /**< L2 (MAC) Header Length. */
145*4882a593Smuzhiyun 		uint64_t vlan_tci:16;  /**< VLAN Tag Control Identifier(CPU order). */
146*4882a593Smuzhiyun 		uint64_t l4_len:8; /**< L4 (TCP/UDP) Header Length. */
147*4882a593Smuzhiyun 		uint64_t tso_segsz:16; /**< TCP TSO segment size. */
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 		/* uint64_t unused:8; */
150*4882a593Smuzhiyun 	};
151*4882a593Smuzhiyun };
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun  * Compare mask for igb_tx_offload.data,
155*4882a593Smuzhiyun  * should be in sync with igb_tx_offload layout.
156*4882a593Smuzhiyun  * */
157*4882a593Smuzhiyun #define TX_MACIP_LEN_CMP_MASK	0x000000000000FFFFULL /**< L2L3 header mask. */
158*4882a593Smuzhiyun #define TX_VLAN_CMP_MASK		0x00000000FFFF0000ULL /**< Vlan mask. */
159*4882a593Smuzhiyun #define TX_TCP_LEN_CMP_MASK		0x000000FF00000000ULL /**< TCP header mask. */
160*4882a593Smuzhiyun #define TX_TSO_MSS_CMP_MASK		0x00FFFF0000000000ULL /**< TSO segsz mask. */
161*4882a593Smuzhiyun /** Mac + IP + TCP + Mss mask. */
162*4882a593Smuzhiyun #define TX_TSO_CMP_MASK	\
163*4882a593Smuzhiyun 	(TX_MACIP_LEN_CMP_MASK | TX_TCP_LEN_CMP_MASK | TX_TSO_MSS_CMP_MASK)
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /**
166*4882a593Smuzhiyun  * Strucutre to check if new context need be built
167*4882a593Smuzhiyun  */
168*4882a593Smuzhiyun struct igb_advctx_info {
169*4882a593Smuzhiyun 	uint64_t flags;           /**< ol_flags related to context build. */
170*4882a593Smuzhiyun 	/** tx offload: vlan, tso, l2-l3-l4 lengths. */
171*4882a593Smuzhiyun 	union igb_tx_offload tx_offload;
172*4882a593Smuzhiyun 	/** compare mask for tx offload. */
173*4882a593Smuzhiyun 	union igb_tx_offload tx_offload_mask;
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun  * Structure associated with each TX queue.
178*4882a593Smuzhiyun  */
179*4882a593Smuzhiyun struct igb_tx_queue {
180*4882a593Smuzhiyun 	volatile union e1000_adv_tx_desc *tx_ring; /**< TX ring address */
181*4882a593Smuzhiyun 	uint64_t               tx_ring_phys_addr; /**< TX ring DMA address. */
182*4882a593Smuzhiyun 	struct igb_tx_entry    *sw_ring; /**< virtual address of SW ring. */
183*4882a593Smuzhiyun 	volatile uint32_t      *tdt_reg_addr; /**< Address of TDT register. */
184*4882a593Smuzhiyun 	uint32_t               txd_type;      /**< Device-specific TXD type */
185*4882a593Smuzhiyun 	uint16_t               nb_tx_desc;    /**< number of TX descriptors. */
186*4882a593Smuzhiyun 	uint16_t               tx_tail; /**< Current value of TDT register. */
187*4882a593Smuzhiyun 	uint16_t               tx_head;
188*4882a593Smuzhiyun 	/**< Index of first used TX descriptor. */
189*4882a593Smuzhiyun 	uint16_t               queue_id; /**< TX queue index. */
190*4882a593Smuzhiyun 	uint16_t               reg_idx;  /**< TX queue register index. */
191*4882a593Smuzhiyun 	uint16_t               port_id;  /**< Device port identifier. */
192*4882a593Smuzhiyun 	uint8_t                pthresh;  /**< Prefetch threshold register. */
193*4882a593Smuzhiyun 	uint8_t                hthresh;  /**< Host threshold register. */
194*4882a593Smuzhiyun 	uint8_t                wthresh;  /**< Write-back threshold register. */
195*4882a593Smuzhiyun 	uint32_t               ctx_curr;
196*4882a593Smuzhiyun 	/**< Current used hardware descriptor. */
197*4882a593Smuzhiyun 	uint32_t               ctx_start;
198*4882a593Smuzhiyun 	/**< Start context position for transmit queue. */
199*4882a593Smuzhiyun 	struct igb_advctx_info ctx_cache[IGB_CTX_NUM];
200*4882a593Smuzhiyun 	/**< Hardware context history.*/
201*4882a593Smuzhiyun 	uint64_t	       offloads; /**< offloads of RTE_ETH_TX_OFFLOAD_* */
202*4882a593Smuzhiyun 	const struct rte_memzone *mz;
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun #if 1
206*4882a593Smuzhiyun #define RTE_PMD_USE_PREFETCH
207*4882a593Smuzhiyun #endif
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun #ifdef RTE_PMD_USE_PREFETCH
210*4882a593Smuzhiyun #define rte_igb_prefetch(p)	rte_prefetch0(p)
211*4882a593Smuzhiyun #else
212*4882a593Smuzhiyun #define rte_igb_prefetch(p)	do {} while(0)
213*4882a593Smuzhiyun #endif
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun #ifdef RTE_PMD_PACKET_PREFETCH
216*4882a593Smuzhiyun #define rte_packet_prefetch(p) rte_prefetch1(p)
217*4882a593Smuzhiyun #else
218*4882a593Smuzhiyun #define rte_packet_prefetch(p)	do {} while(0)
219*4882a593Smuzhiyun #endif
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun  * Macro for VMDq feature for 1 GbE NIC.
223*4882a593Smuzhiyun  */
224*4882a593Smuzhiyun #define E1000_VMOLR_SIZE			(8)
225*4882a593Smuzhiyun #define IGB_TSO_MAX_HDRLEN			(512)
226*4882a593Smuzhiyun #define IGB_TSO_MAX_MSS				(9216)
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun /*********************************************************************
229*4882a593Smuzhiyun  *
230*4882a593Smuzhiyun  *  TX function
231*4882a593Smuzhiyun  *
232*4882a593Smuzhiyun  **********************************************************************/
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /*
235*4882a593Smuzhiyun  *There're some limitations in hardware for TCP segmentation offload. We
236*4882a593Smuzhiyun  *should check whether the parameters are valid.
237*4882a593Smuzhiyun  */
238*4882a593Smuzhiyun static inline uint64_t
check_tso_para(uint64_t ol_req,union igb_tx_offload ol_para)239*4882a593Smuzhiyun check_tso_para(uint64_t ol_req, union igb_tx_offload ol_para)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	if (!(ol_req & RTE_MBUF_F_TX_TCP_SEG))
242*4882a593Smuzhiyun 		return ol_req;
243*4882a593Smuzhiyun 	if ((ol_para.tso_segsz > IGB_TSO_MAX_MSS) || (ol_para.l2_len +
244*4882a593Smuzhiyun 			ol_para.l3_len + ol_para.l4_len > IGB_TSO_MAX_HDRLEN)) {
245*4882a593Smuzhiyun 		ol_req &= ~RTE_MBUF_F_TX_TCP_SEG;
246*4882a593Smuzhiyun 		ol_req |= RTE_MBUF_F_TX_TCP_CKSUM;
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 	return ol_req;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun /*
252*4882a593Smuzhiyun  * Advanced context descriptor are almost same between igb/ixgbe
253*4882a593Smuzhiyun  * This is a separate function, looking for optimization opportunity here
254*4882a593Smuzhiyun  * Rework required to go with the pre-defined values.
255*4882a593Smuzhiyun  */
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun static inline void
igbe_set_xmit_ctx(struct igb_tx_queue * txq,volatile struct e1000_adv_tx_context_desc * ctx_txd,uint64_t ol_flags,union igb_tx_offload tx_offload)258*4882a593Smuzhiyun igbe_set_xmit_ctx(struct igb_tx_queue* txq,
259*4882a593Smuzhiyun 		volatile struct e1000_adv_tx_context_desc *ctx_txd,
260*4882a593Smuzhiyun 		uint64_t ol_flags, union igb_tx_offload tx_offload)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	uint32_t type_tucmd_mlhl;
263*4882a593Smuzhiyun 	uint32_t mss_l4len_idx;
264*4882a593Smuzhiyun 	uint32_t ctx_idx, ctx_curr;
265*4882a593Smuzhiyun 	uint32_t vlan_macip_lens;
266*4882a593Smuzhiyun 	union igb_tx_offload tx_offload_mask;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	ctx_curr = txq->ctx_curr;
269*4882a593Smuzhiyun 	ctx_idx = ctx_curr + txq->ctx_start;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	tx_offload_mask.data = 0;
272*4882a593Smuzhiyun 	type_tucmd_mlhl = 0;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Specify which HW CTX to upload. */
275*4882a593Smuzhiyun 	mss_l4len_idx = (ctx_idx << E1000_ADVTXD_IDX_SHIFT);
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	if (ol_flags & RTE_MBUF_F_TX_VLAN)
278*4882a593Smuzhiyun 		tx_offload_mask.data |= TX_VLAN_CMP_MASK;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/* check if TCP segmentation required for this packet */
281*4882a593Smuzhiyun 	if (ol_flags & RTE_MBUF_F_TX_TCP_SEG) {
282*4882a593Smuzhiyun 		/* implies IP cksum in IPv4 */
283*4882a593Smuzhiyun 		if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
284*4882a593Smuzhiyun 			type_tucmd_mlhl = E1000_ADVTXD_TUCMD_IPV4 |
285*4882a593Smuzhiyun 				E1000_ADVTXD_TUCMD_L4T_TCP |
286*4882a593Smuzhiyun 				E1000_ADVTXD_DTYP_CTXT | E1000_ADVTXD_DCMD_DEXT;
287*4882a593Smuzhiyun 		else
288*4882a593Smuzhiyun 			type_tucmd_mlhl = E1000_ADVTXD_TUCMD_IPV6 |
289*4882a593Smuzhiyun 				E1000_ADVTXD_TUCMD_L4T_TCP |
290*4882a593Smuzhiyun 				E1000_ADVTXD_DTYP_CTXT | E1000_ADVTXD_DCMD_DEXT;
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 		tx_offload_mask.data |= TX_TSO_CMP_MASK;
293*4882a593Smuzhiyun 		mss_l4len_idx |= tx_offload.tso_segsz << E1000_ADVTXD_MSS_SHIFT;
294*4882a593Smuzhiyun 		mss_l4len_idx |= tx_offload.l4_len << E1000_ADVTXD_L4LEN_SHIFT;
295*4882a593Smuzhiyun 	} else { /* no TSO, check if hardware checksum is needed */
296*4882a593Smuzhiyun 		if (ol_flags & (RTE_MBUF_F_TX_IP_CKSUM | RTE_MBUF_F_TX_L4_MASK))
297*4882a593Smuzhiyun 			tx_offload_mask.data |= TX_MACIP_LEN_CMP_MASK;
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 		if (ol_flags & RTE_MBUF_F_TX_IP_CKSUM)
300*4882a593Smuzhiyun 			type_tucmd_mlhl = E1000_ADVTXD_TUCMD_IPV4;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 		switch (ol_flags & RTE_MBUF_F_TX_L4_MASK) {
303*4882a593Smuzhiyun 		case RTE_MBUF_F_TX_UDP_CKSUM:
304*4882a593Smuzhiyun 			type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_UDP |
305*4882a593Smuzhiyun 				E1000_ADVTXD_DTYP_CTXT | E1000_ADVTXD_DCMD_DEXT;
306*4882a593Smuzhiyun 			mss_l4len_idx |= sizeof(struct rte_udp_hdr)
307*4882a593Smuzhiyun 				<< E1000_ADVTXD_L4LEN_SHIFT;
308*4882a593Smuzhiyun 			break;
309*4882a593Smuzhiyun 		case RTE_MBUF_F_TX_TCP_CKSUM:
310*4882a593Smuzhiyun 			type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_TCP |
311*4882a593Smuzhiyun 				E1000_ADVTXD_DTYP_CTXT | E1000_ADVTXD_DCMD_DEXT;
312*4882a593Smuzhiyun 			mss_l4len_idx |= sizeof(struct rte_tcp_hdr)
313*4882a593Smuzhiyun 				<< E1000_ADVTXD_L4LEN_SHIFT;
314*4882a593Smuzhiyun 			break;
315*4882a593Smuzhiyun 		case RTE_MBUF_F_TX_SCTP_CKSUM:
316*4882a593Smuzhiyun 			type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_SCTP |
317*4882a593Smuzhiyun 				E1000_ADVTXD_DTYP_CTXT | E1000_ADVTXD_DCMD_DEXT;
318*4882a593Smuzhiyun 			mss_l4len_idx |= sizeof(struct rte_sctp_hdr)
319*4882a593Smuzhiyun 				<< E1000_ADVTXD_L4LEN_SHIFT;
320*4882a593Smuzhiyun 			break;
321*4882a593Smuzhiyun 		default:
322*4882a593Smuzhiyun 			type_tucmd_mlhl |= E1000_ADVTXD_TUCMD_L4T_RSV |
323*4882a593Smuzhiyun 				E1000_ADVTXD_DTYP_CTXT | E1000_ADVTXD_DCMD_DEXT;
324*4882a593Smuzhiyun 			break;
325*4882a593Smuzhiyun 		}
326*4882a593Smuzhiyun 	}
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	txq->ctx_cache[ctx_curr].flags = ol_flags;
329*4882a593Smuzhiyun 	txq->ctx_cache[ctx_curr].tx_offload.data =
330*4882a593Smuzhiyun 		tx_offload_mask.data & tx_offload.data;
331*4882a593Smuzhiyun 	txq->ctx_cache[ctx_curr].tx_offload_mask = tx_offload_mask;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	ctx_txd->type_tucmd_mlhl = rte_cpu_to_le_32(type_tucmd_mlhl);
334*4882a593Smuzhiyun 	vlan_macip_lens = (uint32_t)tx_offload.data;
335*4882a593Smuzhiyun 	ctx_txd->vlan_macip_lens = rte_cpu_to_le_32(vlan_macip_lens);
336*4882a593Smuzhiyun 	ctx_txd->mss_l4len_idx = rte_cpu_to_le_32(mss_l4len_idx);
337*4882a593Smuzhiyun 	ctx_txd->u.seqnum_seed = 0;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun /*
341*4882a593Smuzhiyun  * Check which hardware context can be used. Use the existing match
342*4882a593Smuzhiyun  * or create a new context descriptor.
343*4882a593Smuzhiyun  */
344*4882a593Smuzhiyun static inline uint32_t
what_advctx_update(struct igb_tx_queue * txq,uint64_t flags,union igb_tx_offload tx_offload)345*4882a593Smuzhiyun what_advctx_update(struct igb_tx_queue *txq, uint64_t flags,
346*4882a593Smuzhiyun 		union igb_tx_offload tx_offload)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	/* If match with the current context */
349*4882a593Smuzhiyun 	if (likely((txq->ctx_cache[txq->ctx_curr].flags == flags) &&
350*4882a593Smuzhiyun 		(txq->ctx_cache[txq->ctx_curr].tx_offload.data ==
351*4882a593Smuzhiyun 		(txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data & tx_offload.data)))) {
352*4882a593Smuzhiyun 			return txq->ctx_curr;
353*4882a593Smuzhiyun 	}
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	/* If match with the second context */
356*4882a593Smuzhiyun 	txq->ctx_curr ^= 1;
357*4882a593Smuzhiyun 	if (likely((txq->ctx_cache[txq->ctx_curr].flags == flags) &&
358*4882a593Smuzhiyun 		(txq->ctx_cache[txq->ctx_curr].tx_offload.data ==
359*4882a593Smuzhiyun 		(txq->ctx_cache[txq->ctx_curr].tx_offload_mask.data & tx_offload.data)))) {
360*4882a593Smuzhiyun 			return txq->ctx_curr;
361*4882a593Smuzhiyun 	}
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	/* Mismatch, use the previous context */
364*4882a593Smuzhiyun 	return IGB_CTX_NUM;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun static inline uint32_t
tx_desc_cksum_flags_to_olinfo(uint64_t ol_flags)368*4882a593Smuzhiyun tx_desc_cksum_flags_to_olinfo(uint64_t ol_flags)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	static const uint32_t l4_olinfo[2] = {0, E1000_ADVTXD_POPTS_TXSM};
371*4882a593Smuzhiyun 	static const uint32_t l3_olinfo[2] = {0, E1000_ADVTXD_POPTS_IXSM};
372*4882a593Smuzhiyun 	uint32_t tmp;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	tmp  = l4_olinfo[(ol_flags & RTE_MBUF_F_TX_L4_MASK)  != RTE_MBUF_F_TX_L4_NO_CKSUM];
375*4882a593Smuzhiyun 	tmp |= l3_olinfo[(ol_flags & RTE_MBUF_F_TX_IP_CKSUM) != 0];
376*4882a593Smuzhiyun 	tmp |= l4_olinfo[(ol_flags & RTE_MBUF_F_TX_TCP_SEG) != 0];
377*4882a593Smuzhiyun 	return tmp;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun static inline uint32_t
tx_desc_vlan_flags_to_cmdtype(uint64_t ol_flags)381*4882a593Smuzhiyun tx_desc_vlan_flags_to_cmdtype(uint64_t ol_flags)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun 	uint32_t cmdtype;
384*4882a593Smuzhiyun 	static uint32_t vlan_cmd[2] = {0, E1000_ADVTXD_DCMD_VLE};
385*4882a593Smuzhiyun 	static uint32_t tso_cmd[2] = {0, E1000_ADVTXD_DCMD_TSE};
386*4882a593Smuzhiyun 	cmdtype = vlan_cmd[(ol_flags & RTE_MBUF_F_TX_VLAN) != 0];
387*4882a593Smuzhiyun 	cmdtype |= tso_cmd[(ol_flags & RTE_MBUF_F_TX_TCP_SEG) != 0];
388*4882a593Smuzhiyun 	return cmdtype;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun uint16_t
eth_igb_xmit_pkts(void * tx_queue,struct rte_mbuf ** tx_pkts,uint16_t nb_pkts)392*4882a593Smuzhiyun eth_igb_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
393*4882a593Smuzhiyun 	       uint16_t nb_pkts)
394*4882a593Smuzhiyun {
395*4882a593Smuzhiyun 	struct igb_tx_queue *txq;
396*4882a593Smuzhiyun 	struct igb_tx_entry *sw_ring;
397*4882a593Smuzhiyun 	struct igb_tx_entry *txe, *txn;
398*4882a593Smuzhiyun 	volatile union e1000_adv_tx_desc *txr;
399*4882a593Smuzhiyun 	volatile union e1000_adv_tx_desc *txd;
400*4882a593Smuzhiyun 	struct rte_mbuf     *tx_pkt;
401*4882a593Smuzhiyun 	struct rte_mbuf     *m_seg;
402*4882a593Smuzhiyun 	uint64_t buf_dma_addr;
403*4882a593Smuzhiyun 	uint32_t olinfo_status;
404*4882a593Smuzhiyun 	uint32_t cmd_type_len;
405*4882a593Smuzhiyun 	uint32_t pkt_len;
406*4882a593Smuzhiyun 	uint16_t slen;
407*4882a593Smuzhiyun 	uint64_t ol_flags;
408*4882a593Smuzhiyun 	uint16_t tx_end;
409*4882a593Smuzhiyun 	uint16_t tx_id;
410*4882a593Smuzhiyun 	uint16_t tx_last;
411*4882a593Smuzhiyun 	uint16_t nb_tx;
412*4882a593Smuzhiyun 	uint64_t tx_ol_req;
413*4882a593Smuzhiyun 	uint32_t new_ctx = 0;
414*4882a593Smuzhiyun 	uint32_t ctx = 0;
415*4882a593Smuzhiyun 	union igb_tx_offload tx_offload = {0};
416*4882a593Smuzhiyun 	uint8_t *data;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	txq = tx_queue;
419*4882a593Smuzhiyun 	sw_ring = txq->sw_ring;
420*4882a593Smuzhiyun 	txr     = txq->tx_ring;
421*4882a593Smuzhiyun 	tx_id   = txq->tx_tail;
422*4882a593Smuzhiyun 	txe = &sw_ring[tx_id];
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	for (nb_tx = 0; nb_tx < nb_pkts; nb_tx++) {
425*4882a593Smuzhiyun 		tx_pkt = *tx_pkts++;
426*4882a593Smuzhiyun 		pkt_len = tx_pkt->pkt_len;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 		RTE_MBUF_PREFETCH_TO_FREE(txe->mbuf);
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 		/*
431*4882a593Smuzhiyun 		 * The number of descriptors that must be allocated for a
432*4882a593Smuzhiyun 		 * packet is the number of segments of that packet, plus 1
433*4882a593Smuzhiyun 		 * Context Descriptor for the VLAN Tag Identifier, if any.
434*4882a593Smuzhiyun 		 * Determine the last TX descriptor to allocate in the TX ring
435*4882a593Smuzhiyun 		 * for the packet, starting from the current position (tx_id)
436*4882a593Smuzhiyun 		 * in the ring.
437*4882a593Smuzhiyun 		 */
438*4882a593Smuzhiyun 		tx_last = (uint16_t) (tx_id + tx_pkt->nb_segs - 1);
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 		ol_flags = tx_pkt->ol_flags;
441*4882a593Smuzhiyun 		tx_ol_req = ol_flags & IGB_TX_OFFLOAD_MASK;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 		/* If a Context Descriptor need be built . */
444*4882a593Smuzhiyun 		if (tx_ol_req) {
445*4882a593Smuzhiyun 			tx_offload.l2_len = tx_pkt->l2_len;
446*4882a593Smuzhiyun 			tx_offload.l3_len = tx_pkt->l3_len;
447*4882a593Smuzhiyun 			tx_offload.l4_len = tx_pkt->l4_len;
448*4882a593Smuzhiyun 			tx_offload.vlan_tci = tx_pkt->vlan_tci;
449*4882a593Smuzhiyun 			tx_offload.tso_segsz = tx_pkt->tso_segsz;
450*4882a593Smuzhiyun 			tx_ol_req = check_tso_para(tx_ol_req, tx_offload);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 			ctx = what_advctx_update(txq, tx_ol_req, tx_offload);
453*4882a593Smuzhiyun 			/* Only allocate context descriptor if required*/
454*4882a593Smuzhiyun 			new_ctx = (ctx == IGB_CTX_NUM);
455*4882a593Smuzhiyun 			ctx = txq->ctx_curr + txq->ctx_start;
456*4882a593Smuzhiyun 			tx_last = (uint16_t) (tx_last + new_ctx);
457*4882a593Smuzhiyun 		}
458*4882a593Smuzhiyun 		if (tx_last >= txq->nb_tx_desc)
459*4882a593Smuzhiyun 			tx_last = (uint16_t) (tx_last - txq->nb_tx_desc);
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 		PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u pktlen=%u"
462*4882a593Smuzhiyun 			   " tx_first=%u tx_last=%u",
463*4882a593Smuzhiyun 			   (unsigned) txq->port_id,
464*4882a593Smuzhiyun 			   (unsigned) txq->queue_id,
465*4882a593Smuzhiyun 			   (unsigned) pkt_len,
466*4882a593Smuzhiyun 			   (unsigned) tx_id,
467*4882a593Smuzhiyun 			   (unsigned) tx_last);
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 		/*
470*4882a593Smuzhiyun 		 * Check if there are enough free descriptors in the TX ring
471*4882a593Smuzhiyun 		 * to transmit the next packet.
472*4882a593Smuzhiyun 		 * This operation is based on the two following rules:
473*4882a593Smuzhiyun 		 *
474*4882a593Smuzhiyun 		 *   1- Only check that the last needed TX descriptor can be
475*4882a593Smuzhiyun 		 *      allocated (by construction, if that descriptor is free,
476*4882a593Smuzhiyun 		 *      all intermediate ones are also free).
477*4882a593Smuzhiyun 		 *
478*4882a593Smuzhiyun 		 *      For this purpose, the index of the last TX descriptor
479*4882a593Smuzhiyun 		 *      used for a packet (the "last descriptor" of a packet)
480*4882a593Smuzhiyun 		 *      is recorded in the TX entries (the last one included)
481*4882a593Smuzhiyun 		 *      that are associated with all TX descriptors allocated
482*4882a593Smuzhiyun 		 *      for that packet.
483*4882a593Smuzhiyun 		 *
484*4882a593Smuzhiyun 		 *   2- Avoid to allocate the last free TX descriptor of the
485*4882a593Smuzhiyun 		 *      ring, in order to never set the TDT register with the
486*4882a593Smuzhiyun 		 *      same value stored in parallel by the NIC in the TDH
487*4882a593Smuzhiyun 		 *      register, which makes the TX engine of the NIC enter
488*4882a593Smuzhiyun 		 *      in a deadlock situation.
489*4882a593Smuzhiyun 		 *
490*4882a593Smuzhiyun 		 *      By extension, avoid to allocate a free descriptor that
491*4882a593Smuzhiyun 		 *      belongs to the last set of free descriptors allocated
492*4882a593Smuzhiyun 		 *      to the same packet previously transmitted.
493*4882a593Smuzhiyun 		 */
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 		/*
496*4882a593Smuzhiyun 		 * The "last descriptor" of the previously sent packet, if any,
497*4882a593Smuzhiyun 		 * which used the last descriptor to allocate.
498*4882a593Smuzhiyun 		 */
499*4882a593Smuzhiyun 		tx_end = sw_ring[tx_last].last_id;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 		/*
502*4882a593Smuzhiyun 		 * The next descriptor following that "last descriptor" in the
503*4882a593Smuzhiyun 		 * ring.
504*4882a593Smuzhiyun 		 */
505*4882a593Smuzhiyun 		tx_end = sw_ring[tx_end].next_id;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		/*
508*4882a593Smuzhiyun 		 * The "last descriptor" associated with that next descriptor.
509*4882a593Smuzhiyun 		 */
510*4882a593Smuzhiyun 		tx_end = sw_ring[tx_end].last_id;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 		/*
513*4882a593Smuzhiyun 		 * Check that this descriptor is free.
514*4882a593Smuzhiyun 		 */
515*4882a593Smuzhiyun 		if (! (txr[tx_end].wb.status & E1000_TXD_STAT_DD)) {
516*4882a593Smuzhiyun 			if (nb_tx == 0)
517*4882a593Smuzhiyun 				return 0;
518*4882a593Smuzhiyun 			goto end_of_tx;
519*4882a593Smuzhiyun 		}
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 		/*
522*4882a593Smuzhiyun 		 * Set common flags of all TX Data Descriptors.
523*4882a593Smuzhiyun 		 *
524*4882a593Smuzhiyun 		 * The following bits must be set in all Data Descriptors:
525*4882a593Smuzhiyun 		 *   - E1000_ADVTXD_DTYP_DATA
526*4882a593Smuzhiyun 		 *   - E1000_ADVTXD_DCMD_DEXT
527*4882a593Smuzhiyun 		 *
528*4882a593Smuzhiyun 		 * The following bits must be set in the first Data Descriptor
529*4882a593Smuzhiyun 		 * and are ignored in the other ones:
530*4882a593Smuzhiyun 		 *   - E1000_ADVTXD_DCMD_IFCS
531*4882a593Smuzhiyun 		 *   - E1000_ADVTXD_MAC_1588
532*4882a593Smuzhiyun 		 *   - E1000_ADVTXD_DCMD_VLE
533*4882a593Smuzhiyun 		 *
534*4882a593Smuzhiyun 		 * The following bits must only be set in the last Data
535*4882a593Smuzhiyun 		 * Descriptor:
536*4882a593Smuzhiyun 		 *   - E1000_TXD_CMD_EOP
537*4882a593Smuzhiyun 		 *
538*4882a593Smuzhiyun 		 * The following bits can be set in any Data Descriptor, but
539*4882a593Smuzhiyun 		 * are only set in the last Data Descriptor:
540*4882a593Smuzhiyun 		 *   - E1000_TXD_CMD_RS
541*4882a593Smuzhiyun 		 */
542*4882a593Smuzhiyun 		cmd_type_len = txq->txd_type |
543*4882a593Smuzhiyun 			E1000_ADVTXD_DCMD_IFCS | E1000_ADVTXD_DCMD_DEXT;
544*4882a593Smuzhiyun 		if (tx_ol_req & RTE_MBUF_F_TX_TCP_SEG)
545*4882a593Smuzhiyun 			pkt_len -= (tx_pkt->l2_len + tx_pkt->l3_len + tx_pkt->l4_len);
546*4882a593Smuzhiyun 		olinfo_status = (pkt_len << E1000_ADVTXD_PAYLEN_SHIFT);
547*4882a593Smuzhiyun #if defined(RTE_LIBRTE_IEEE1588)
548*4882a593Smuzhiyun 		if (ol_flags & RTE_MBUF_F_TX_IEEE1588_TMST)
549*4882a593Smuzhiyun 			cmd_type_len |= E1000_ADVTXD_MAC_TSTAMP;
550*4882a593Smuzhiyun #endif
551*4882a593Smuzhiyun 		if (tx_ol_req) {
552*4882a593Smuzhiyun 			/* Setup TX Advanced context descriptor if required */
553*4882a593Smuzhiyun 			if (new_ctx) {
554*4882a593Smuzhiyun 				volatile struct e1000_adv_tx_context_desc *
555*4882a593Smuzhiyun 				    ctx_txd;
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 				ctx_txd = (volatile struct
558*4882a593Smuzhiyun 				    e1000_adv_tx_context_desc *)
559*4882a593Smuzhiyun 				    &txr[tx_id];
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 				txn = &sw_ring[txe->next_id];
562*4882a593Smuzhiyun 				RTE_MBUF_PREFETCH_TO_FREE(txn->mbuf);
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 				if (txe->mbuf != NULL) {
565*4882a593Smuzhiyun 					rte_pktmbuf_free_seg(txe->mbuf);
566*4882a593Smuzhiyun 					txe->mbuf = NULL;
567*4882a593Smuzhiyun 				}
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 				igbe_set_xmit_ctx(txq, ctx_txd, tx_ol_req, tx_offload);
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 				txe->last_id = tx_last;
572*4882a593Smuzhiyun 				tx_id = txe->next_id;
573*4882a593Smuzhiyun 				txe = txn;
574*4882a593Smuzhiyun 			}
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 			/* Setup the TX Advanced Data Descriptor */
577*4882a593Smuzhiyun 			cmd_type_len  |= tx_desc_vlan_flags_to_cmdtype(tx_ol_req);
578*4882a593Smuzhiyun 			olinfo_status |= tx_desc_cksum_flags_to_olinfo(tx_ol_req);
579*4882a593Smuzhiyun 			olinfo_status |= (ctx << E1000_ADVTXD_IDX_SHIFT);
580*4882a593Smuzhiyun 		}
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 		m_seg = tx_pkt;
583*4882a593Smuzhiyun 		do {
584*4882a593Smuzhiyun 			txn = &sw_ring[txe->next_id];
585*4882a593Smuzhiyun 			txd = &txr[tx_id];
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 			if (txe->mbuf != NULL)
588*4882a593Smuzhiyun 				rte_pktmbuf_free_seg(txe->mbuf);
589*4882a593Smuzhiyun 			txe->mbuf = m_seg;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 			data = rte_pktmbuf_mtod(m_seg, uint8_t *);
592*4882a593Smuzhiyun 			for (int i = 0; i < m_seg->data_len; i += 64) {
593*4882a593Smuzhiyun 				dcbf(data + i);
594*4882a593Smuzhiyun 			}
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 			/*
597*4882a593Smuzhiyun 			 * Set up transmit descriptor.
598*4882a593Smuzhiyun 			 */
599*4882a593Smuzhiyun 			slen = (uint16_t) m_seg->data_len;
600*4882a593Smuzhiyun 			buf_dma_addr = rte_mbuf_data_iova(m_seg);
601*4882a593Smuzhiyun 			txd->read.buffer_addr =
602*4882a593Smuzhiyun 				rte_cpu_to_le_64(buf_dma_addr);
603*4882a593Smuzhiyun 			txd->read.cmd_type_len =
604*4882a593Smuzhiyun 				rte_cpu_to_le_32(cmd_type_len | slen);
605*4882a593Smuzhiyun 			txd->read.olinfo_status =
606*4882a593Smuzhiyun 				rte_cpu_to_le_32(olinfo_status);
607*4882a593Smuzhiyun 			txe->last_id = tx_last;
608*4882a593Smuzhiyun 			tx_id = txe->next_id;
609*4882a593Smuzhiyun 			txe = txn;
610*4882a593Smuzhiyun 			m_seg = m_seg->next;
611*4882a593Smuzhiyun 		} while (m_seg != NULL);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 		/*
614*4882a593Smuzhiyun 		 * The last packet data descriptor needs End Of Packet (EOP)
615*4882a593Smuzhiyun 		 * and Report Status (RS).
616*4882a593Smuzhiyun 		 */
617*4882a593Smuzhiyun 		txd->read.cmd_type_len |=
618*4882a593Smuzhiyun 			rte_cpu_to_le_32(E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS);
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun  end_of_tx:
621*4882a593Smuzhiyun 	rte_wmb();
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun 	/*
624*4882a593Smuzhiyun 	 * Set the Transmit Descriptor Tail (TDT).
625*4882a593Smuzhiyun 	 */
626*4882a593Smuzhiyun 	E1000_PCI_REG_WRITE_RELAXED(txq->tdt_reg_addr, tx_id);
627*4882a593Smuzhiyun 	PMD_TX_LOG(DEBUG, "port_id=%u queue_id=%u tx_tail=%u nb_tx=%u",
628*4882a593Smuzhiyun 		   (unsigned) txq->port_id, (unsigned) txq->queue_id,
629*4882a593Smuzhiyun 		   (unsigned) tx_id, (unsigned) nb_tx);
630*4882a593Smuzhiyun 	txq->tx_tail = tx_id;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	return nb_tx;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun /*********************************************************************
636*4882a593Smuzhiyun  *
637*4882a593Smuzhiyun  *  TX prep functions
638*4882a593Smuzhiyun  *
639*4882a593Smuzhiyun  **********************************************************************/
640*4882a593Smuzhiyun uint16_t
eth_igb_prep_pkts(__rte_unused void * tx_queue,struct rte_mbuf ** tx_pkts,uint16_t nb_pkts)641*4882a593Smuzhiyun eth_igb_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
642*4882a593Smuzhiyun 		uint16_t nb_pkts)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	int i, ret;
645*4882a593Smuzhiyun 	struct rte_mbuf *m;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	for (i = 0; i < nb_pkts; i++) {
648*4882a593Smuzhiyun 		m = tx_pkts[i];
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 		/* Check some limitations for TSO in hardware */
651*4882a593Smuzhiyun 		if (m->ol_flags & RTE_MBUF_F_TX_TCP_SEG)
652*4882a593Smuzhiyun 			if ((m->tso_segsz > IGB_TSO_MAX_MSS) ||
653*4882a593Smuzhiyun 					(m->l2_len + m->l3_len + m->l4_len >
654*4882a593Smuzhiyun 					IGB_TSO_MAX_HDRLEN)) {
655*4882a593Smuzhiyun 				rte_errno = EINVAL;
656*4882a593Smuzhiyun 				return i;
657*4882a593Smuzhiyun 			}
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 		if (m->ol_flags & IGB_TX_OFFLOAD_NOTSUP_MASK) {
660*4882a593Smuzhiyun 			rte_errno = ENOTSUP;
661*4882a593Smuzhiyun 			return i;
662*4882a593Smuzhiyun 		}
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun #ifdef RTE_ETHDEV_DEBUG_TX
665*4882a593Smuzhiyun 		ret = rte_validate_tx_offload(m);
666*4882a593Smuzhiyun 		if (ret != 0) {
667*4882a593Smuzhiyun 			rte_errno = -ret;
668*4882a593Smuzhiyun 			return i;
669*4882a593Smuzhiyun 		}
670*4882a593Smuzhiyun #endif
671*4882a593Smuzhiyun 		ret = rte_net_intel_cksum_prepare(m);
672*4882a593Smuzhiyun 		if (ret != 0) {
673*4882a593Smuzhiyun 			rte_errno = -ret;
674*4882a593Smuzhiyun 			return i;
675*4882a593Smuzhiyun 		}
676*4882a593Smuzhiyun 	}
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun 	return i;
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun /*********************************************************************
682*4882a593Smuzhiyun  *
683*4882a593Smuzhiyun  *  RX functions
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  **********************************************************************/
686*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4              0X01
687*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_TCP          0X11
688*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_UDP          0X21
689*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_SCTP         0X41
690*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_EXT          0X03
691*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_EXT_SCTP     0X43
692*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV6              0X04
693*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV6_TCP          0X14
694*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV6_UDP          0X24
695*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV6_EXT          0X0C
696*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV6_EXT_TCP      0X1C
697*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV6_EXT_UDP      0X2C
698*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_IPV6         0X05
699*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_IPV6_TCP     0X15
700*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_IPV6_UDP     0X25
701*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_IPV6_EXT     0X0D
702*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_IPV6_EXT_TCP 0X1D
703*4882a593Smuzhiyun #define IGB_PACKET_TYPE_IPV4_IPV6_EXT_UDP 0X2D
704*4882a593Smuzhiyun #define IGB_PACKET_TYPE_MAX               0X80
705*4882a593Smuzhiyun #define IGB_PACKET_TYPE_MASK              0X7F
706*4882a593Smuzhiyun #define IGB_PACKET_TYPE_SHIFT             0X04
707*4882a593Smuzhiyun static inline uint32_t
igb_rxd_pkt_info_to_pkt_type(uint16_t pkt_info)708*4882a593Smuzhiyun igb_rxd_pkt_info_to_pkt_type(uint16_t pkt_info)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun 	static const uint32_t
711*4882a593Smuzhiyun 		ptype_table[IGB_PACKET_TYPE_MAX] __rte_cache_aligned = {
712*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4] = RTE_PTYPE_L2_ETHER |
713*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4,
714*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_EXT] = RTE_PTYPE_L2_ETHER |
715*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4_EXT,
716*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV6] = RTE_PTYPE_L2_ETHER |
717*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV6,
718*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_IPV6] = RTE_PTYPE_L2_ETHER |
719*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_TUNNEL_IP |
720*4882a593Smuzhiyun 			RTE_PTYPE_INNER_L3_IPV6,
721*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV6_EXT] = RTE_PTYPE_L2_ETHER |
722*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV6_EXT,
723*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_IPV6_EXT] = RTE_PTYPE_L2_ETHER |
724*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_TUNNEL_IP |
725*4882a593Smuzhiyun 			RTE_PTYPE_INNER_L3_IPV6_EXT,
726*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_TCP] = RTE_PTYPE_L2_ETHER |
727*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_TCP,
728*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV6_TCP] = RTE_PTYPE_L2_ETHER |
729*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_TCP,
730*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_IPV6_TCP] = RTE_PTYPE_L2_ETHER |
731*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_TUNNEL_IP |
732*4882a593Smuzhiyun 			RTE_PTYPE_INNER_L3_IPV6 | RTE_PTYPE_INNER_L4_TCP,
733*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV6_EXT_TCP] = RTE_PTYPE_L2_ETHER |
734*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV6_EXT | RTE_PTYPE_L4_TCP,
735*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_IPV6_EXT_TCP] = RTE_PTYPE_L2_ETHER |
736*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_TUNNEL_IP |
737*4882a593Smuzhiyun 			RTE_PTYPE_INNER_L3_IPV6_EXT | RTE_PTYPE_INNER_L4_TCP,
738*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_UDP] = RTE_PTYPE_L2_ETHER |
739*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_UDP,
740*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV6_UDP] = RTE_PTYPE_L2_ETHER |
741*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV6 | RTE_PTYPE_L4_UDP,
742*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_IPV6_UDP] =  RTE_PTYPE_L2_ETHER |
743*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_TUNNEL_IP |
744*4882a593Smuzhiyun 			RTE_PTYPE_INNER_L3_IPV6 | RTE_PTYPE_INNER_L4_UDP,
745*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV6_EXT_UDP] = RTE_PTYPE_L2_ETHER |
746*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV6_EXT | RTE_PTYPE_L4_UDP,
747*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_IPV6_EXT_UDP] = RTE_PTYPE_L2_ETHER |
748*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_TUNNEL_IP |
749*4882a593Smuzhiyun 			RTE_PTYPE_INNER_L3_IPV6_EXT | RTE_PTYPE_INNER_L4_UDP,
750*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_SCTP] = RTE_PTYPE_L2_ETHER |
751*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4 | RTE_PTYPE_L4_SCTP,
752*4882a593Smuzhiyun 		[IGB_PACKET_TYPE_IPV4_EXT_SCTP] = RTE_PTYPE_L2_ETHER |
753*4882a593Smuzhiyun 			RTE_PTYPE_L3_IPV4_EXT | RTE_PTYPE_L4_SCTP,
754*4882a593Smuzhiyun 	};
755*4882a593Smuzhiyun 	if (unlikely(pkt_info & E1000_RXDADV_PKTTYPE_ETQF))
756*4882a593Smuzhiyun 		return RTE_PTYPE_UNKNOWN;
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 	pkt_info = (pkt_info >> IGB_PACKET_TYPE_SHIFT) & IGB_PACKET_TYPE_MASK;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	return ptype_table[pkt_info];
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun static inline uint64_t
rx_desc_hlen_type_rss_to_pkt_flags(struct igb_rx_queue * rxq,uint32_t hl_tp_rs)764*4882a593Smuzhiyun rx_desc_hlen_type_rss_to_pkt_flags(struct igb_rx_queue *rxq, uint32_t hl_tp_rs)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	uint64_t pkt_flags = ((hl_tp_rs & 0x0F) == 0) ?  0 : RTE_MBUF_F_RX_RSS_HASH;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun #if defined(RTE_LIBRTE_IEEE1588)
769*4882a593Smuzhiyun 	static uint32_t ip_pkt_etqf_map[8] = {
770*4882a593Smuzhiyun 		0, 0, 0, RTE_MBUF_F_RX_IEEE1588_PTP,
771*4882a593Smuzhiyun 		0, 0, 0, 0,
772*4882a593Smuzhiyun 	};
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	struct rte_eth_dev dev = rte_eth_devices[rxq->port_id];
775*4882a593Smuzhiyun 	struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev.data->dev_private);
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	/* EtherType is in bits 8:10 in Packet Type, and not in the default 0:2 */
778*4882a593Smuzhiyun 	if (hw->mac.type == e1000_i210)
779*4882a593Smuzhiyun 		pkt_flags |= ip_pkt_etqf_map[(hl_tp_rs >> 12) & 0x07];
780*4882a593Smuzhiyun 	else
781*4882a593Smuzhiyun 		pkt_flags |= ip_pkt_etqf_map[(hl_tp_rs >> 4) & 0x07];
782*4882a593Smuzhiyun #else
783*4882a593Smuzhiyun 	RTE_SET_USED(rxq);
784*4882a593Smuzhiyun #endif
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	return pkt_flags;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun static inline uint64_t
rx_desc_status_to_pkt_flags(uint32_t rx_status)790*4882a593Smuzhiyun rx_desc_status_to_pkt_flags(uint32_t rx_status)
791*4882a593Smuzhiyun {
792*4882a593Smuzhiyun 	uint64_t pkt_flags;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 	/* Check if VLAN present */
795*4882a593Smuzhiyun 	pkt_flags = ((rx_status & E1000_RXD_STAT_VP) ?
796*4882a593Smuzhiyun 		RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED : 0);
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun #if defined(RTE_LIBRTE_IEEE1588)
799*4882a593Smuzhiyun 	if (rx_status & E1000_RXD_STAT_TMST)
800*4882a593Smuzhiyun 		pkt_flags = pkt_flags | RTE_MBUF_F_RX_IEEE1588_TMST;
801*4882a593Smuzhiyun #endif
802*4882a593Smuzhiyun 	return pkt_flags;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun static inline uint64_t
rx_desc_error_to_pkt_flags(uint32_t rx_status)806*4882a593Smuzhiyun rx_desc_error_to_pkt_flags(uint32_t rx_status)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	/*
809*4882a593Smuzhiyun 	 * Bit 30: IPE, IPv4 checksum error
810*4882a593Smuzhiyun 	 * Bit 29: L4I, L4I integrity error
811*4882a593Smuzhiyun 	 */
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	static uint64_t error_to_pkt_flags_map[4] = {
814*4882a593Smuzhiyun 		RTE_MBUF_F_RX_IP_CKSUM_GOOD | RTE_MBUF_F_RX_L4_CKSUM_GOOD,
815*4882a593Smuzhiyun 		RTE_MBUF_F_RX_IP_CKSUM_GOOD | RTE_MBUF_F_RX_L4_CKSUM_BAD,
816*4882a593Smuzhiyun 		RTE_MBUF_F_RX_IP_CKSUM_BAD | RTE_MBUF_F_RX_L4_CKSUM_GOOD,
817*4882a593Smuzhiyun 		RTE_MBUF_F_RX_IP_CKSUM_BAD | RTE_MBUF_F_RX_L4_CKSUM_BAD
818*4882a593Smuzhiyun 	};
819*4882a593Smuzhiyun 	return error_to_pkt_flags_map[(rx_status >>
820*4882a593Smuzhiyun 		E1000_RXD_ERR_CKSUM_BIT) & E1000_RXD_ERR_CKSUM_MSK];
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun uint16_t
eth_igb_recv_pkts(void * rx_queue,struct rte_mbuf ** rx_pkts,uint16_t nb_pkts)824*4882a593Smuzhiyun eth_igb_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
825*4882a593Smuzhiyun 	       uint16_t nb_pkts)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
828*4882a593Smuzhiyun 	volatile union e1000_adv_rx_desc *rx_ring;
829*4882a593Smuzhiyun 	volatile union e1000_adv_rx_desc *rxdp;
830*4882a593Smuzhiyun 	struct igb_rx_entry *sw_ring;
831*4882a593Smuzhiyun 	struct igb_rx_entry *rxe;
832*4882a593Smuzhiyun 	struct rte_mbuf *rxm;
833*4882a593Smuzhiyun 	struct rte_mbuf *nmb;
834*4882a593Smuzhiyun 	union e1000_adv_rx_desc rxd;
835*4882a593Smuzhiyun 	uint64_t dma_addr;
836*4882a593Smuzhiyun 	uint32_t staterr;
837*4882a593Smuzhiyun 	uint32_t hlen_type_rss;
838*4882a593Smuzhiyun 	uint16_t pkt_len;
839*4882a593Smuzhiyun 	uint16_t rx_id;
840*4882a593Smuzhiyun 	uint16_t nb_rx;
841*4882a593Smuzhiyun 	uint16_t nb_hold;
842*4882a593Smuzhiyun 	uint64_t pkt_flags;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	nb_rx = 0;
845*4882a593Smuzhiyun 	nb_hold = 0;
846*4882a593Smuzhiyun 	rxq = rx_queue;
847*4882a593Smuzhiyun 	rx_id = rxq->rx_tail;
848*4882a593Smuzhiyun 	rx_ring = rxq->rx_ring;
849*4882a593Smuzhiyun 	sw_ring = rxq->sw_ring;
850*4882a593Smuzhiyun 	while (nb_rx < nb_pkts) {
851*4882a593Smuzhiyun 		/*
852*4882a593Smuzhiyun 		 * The order of operations here is important as the DD status
853*4882a593Smuzhiyun 		 * bit must not be read after any other descriptor fields.
854*4882a593Smuzhiyun 		 * rx_ring and rxdp are pointing to volatile data so the order
855*4882a593Smuzhiyun 		 * of accesses cannot be reordered by the compiler. If they were
856*4882a593Smuzhiyun 		 * not volatile, they could be reordered which could lead to
857*4882a593Smuzhiyun 		 * using invalid descriptor fields when read from rxd.
858*4882a593Smuzhiyun 		 */
859*4882a593Smuzhiyun 		rxdp = &rx_ring[rx_id];
860*4882a593Smuzhiyun 		staterr = rxdp->wb.upper.status_error;
861*4882a593Smuzhiyun 		if (! (staterr & rte_cpu_to_le_32(E1000_RXD_STAT_DD)))
862*4882a593Smuzhiyun 			break;
863*4882a593Smuzhiyun 		rxd = *rxdp;
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun 		/*
866*4882a593Smuzhiyun 		 * End of packet.
867*4882a593Smuzhiyun 		 *
868*4882a593Smuzhiyun 		 * If the E1000_RXD_STAT_EOP flag is not set, the RX packet is
869*4882a593Smuzhiyun 		 * likely to be invalid and to be dropped by the various
870*4882a593Smuzhiyun 		 * validation checks performed by the network stack.
871*4882a593Smuzhiyun 		 *
872*4882a593Smuzhiyun 		 * Allocate a new mbuf to replenish the RX ring descriptor.
873*4882a593Smuzhiyun 		 * If the allocation fails:
874*4882a593Smuzhiyun 		 *    - arrange for that RX descriptor to be the first one
875*4882a593Smuzhiyun 		 *      being parsed the next time the receive function is
876*4882a593Smuzhiyun 		 *      invoked [on the same queue].
877*4882a593Smuzhiyun 		 *
878*4882a593Smuzhiyun 		 *    - Stop parsing the RX ring and return immediately.
879*4882a593Smuzhiyun 		 *
880*4882a593Smuzhiyun 		 * This policy do not drop the packet received in the RX
881*4882a593Smuzhiyun 		 * descriptor for which the allocation of a new mbuf failed.
882*4882a593Smuzhiyun 		 * Thus, it allows that packet to be later retrieved if
883*4882a593Smuzhiyun 		 * mbuf have been freed in the mean time.
884*4882a593Smuzhiyun 		 * As a side effect, holding RX descriptors instead of
885*4882a593Smuzhiyun 		 * systematically giving them back to the NIC may lead to
886*4882a593Smuzhiyun 		 * RX ring exhaustion situations.
887*4882a593Smuzhiyun 		 * However, the NIC can gracefully prevent such situations
888*4882a593Smuzhiyun 		 * to happen by sending specific "back-pressure" flow control
889*4882a593Smuzhiyun 		 * frames to its peer(s).
890*4882a593Smuzhiyun 		 */
891*4882a593Smuzhiyun 		PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
892*4882a593Smuzhiyun 			   "staterr=0x%x pkt_len=%u",
893*4882a593Smuzhiyun 			   (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
894*4882a593Smuzhiyun 			   (unsigned) rx_id, (unsigned) staterr,
895*4882a593Smuzhiyun 			   (unsigned) rte_le_to_cpu_16(rxd.wb.upper.length));
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 		nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
898*4882a593Smuzhiyun 		if (nmb == NULL) {
899*4882a593Smuzhiyun 			PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
900*4882a593Smuzhiyun 				   "queue_id=%u", (unsigned) rxq->port_id,
901*4882a593Smuzhiyun 				   (unsigned) rxq->queue_id);
902*4882a593Smuzhiyun 			rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
903*4882a593Smuzhiyun 			break;
904*4882a593Smuzhiyun 		}
905*4882a593Smuzhiyun 
906*4882a593Smuzhiyun 		nb_hold++;
907*4882a593Smuzhiyun 		rxe = &sw_ring[rx_id];
908*4882a593Smuzhiyun 		rx_id++;
909*4882a593Smuzhiyun 		if (rx_id == rxq->nb_rx_desc)
910*4882a593Smuzhiyun 			rx_id = 0;
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 		/* Prefetch next mbuf while processing current one. */
913*4882a593Smuzhiyun 		rte_igb_prefetch(sw_ring[rx_id].mbuf);
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 		/*
916*4882a593Smuzhiyun 		 * When next RX descriptor is on a cache-line boundary,
917*4882a593Smuzhiyun 		 * prefetch the next 4 RX descriptors and the next 8 pointers
918*4882a593Smuzhiyun 		 * to mbufs.
919*4882a593Smuzhiyun 		 */
920*4882a593Smuzhiyun 		if ((rx_id & 0x3) == 0) {
921*4882a593Smuzhiyun 			rte_igb_prefetch(&rx_ring[rx_id]);
922*4882a593Smuzhiyun 			rte_igb_prefetch(&sw_ring[rx_id]);
923*4882a593Smuzhiyun 		}
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 		rxm = rxe->mbuf;
926*4882a593Smuzhiyun 		rxe->mbuf = nmb;
927*4882a593Smuzhiyun 		dma_addr =
928*4882a593Smuzhiyun 			rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
929*4882a593Smuzhiyun 		rxdp->read.hdr_addr = 0;
930*4882a593Smuzhiyun 		rxdp->read.pkt_addr = dma_addr;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 		/*
933*4882a593Smuzhiyun 		 * Initialize the returned mbuf.
934*4882a593Smuzhiyun 		 * 1) setup generic mbuf fields:
935*4882a593Smuzhiyun 		 *    - number of segments,
936*4882a593Smuzhiyun 		 *    - next segment,
937*4882a593Smuzhiyun 		 *    - packet length,
938*4882a593Smuzhiyun 		 *    - RX port identifier.
939*4882a593Smuzhiyun 		 * 2) integrate hardware offload data, if any:
940*4882a593Smuzhiyun 		 *    - RSS flag & hash,
941*4882a593Smuzhiyun 		 *    - IP checksum flag,
942*4882a593Smuzhiyun 		 *    - VLAN TCI, if any,
943*4882a593Smuzhiyun 		 *    - error flags.
944*4882a593Smuzhiyun 		 */
945*4882a593Smuzhiyun 		pkt_len = (uint16_t) (rte_le_to_cpu_16(rxd.wb.upper.length) -
946*4882a593Smuzhiyun 				      rxq->crc_len);
947*4882a593Smuzhiyun 		rxm->data_off = RTE_PKTMBUF_HEADROOM;
948*4882a593Smuzhiyun 		rte_packet_prefetch((char *)rxm->buf_addr + rxm->data_off);
949*4882a593Smuzhiyun 		rxm->nb_segs = 1;
950*4882a593Smuzhiyun 		rxm->next = NULL;
951*4882a593Smuzhiyun 		rxm->pkt_len = pkt_len;
952*4882a593Smuzhiyun 		rxm->data_len = pkt_len;
953*4882a593Smuzhiyun 		rxm->port = rxq->port_id;
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 		rxm->hash.rss = rxd.wb.lower.hi_dword.rss;
956*4882a593Smuzhiyun 		hlen_type_rss = rte_le_to_cpu_32(rxd.wb.lower.lo_dword.data);
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 		/*
959*4882a593Smuzhiyun 		 * The vlan_tci field is only valid when RTE_MBUF_F_RX_VLAN is
960*4882a593Smuzhiyun 		 * set in the pkt_flags field and must be in CPU byte order.
961*4882a593Smuzhiyun 		 */
962*4882a593Smuzhiyun 		if ((staterr & rte_cpu_to_le_32(E1000_RXDEXT_STATERR_LB)) &&
963*4882a593Smuzhiyun 				(rxq->flags & IGB_RXQ_FLAG_LB_BSWAP_VLAN)) {
964*4882a593Smuzhiyun 			rxm->vlan_tci = rte_be_to_cpu_16(rxd.wb.upper.vlan);
965*4882a593Smuzhiyun 		} else {
966*4882a593Smuzhiyun 			rxm->vlan_tci = rte_le_to_cpu_16(rxd.wb.upper.vlan);
967*4882a593Smuzhiyun 		}
968*4882a593Smuzhiyun 		pkt_flags = rx_desc_hlen_type_rss_to_pkt_flags(rxq, hlen_type_rss);
969*4882a593Smuzhiyun 		pkt_flags = pkt_flags | rx_desc_status_to_pkt_flags(staterr);
970*4882a593Smuzhiyun 		pkt_flags = pkt_flags | rx_desc_error_to_pkt_flags(staterr);
971*4882a593Smuzhiyun 		rxm->ol_flags = pkt_flags;
972*4882a593Smuzhiyun 		rxm->packet_type = igb_rxd_pkt_info_to_pkt_type(rxd.wb.lower.
973*4882a593Smuzhiyun 						lo_dword.hs_rss.pkt_info);
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 		/*
976*4882a593Smuzhiyun 		 * Store the mbuf address into the next entry of the array
977*4882a593Smuzhiyun 		 * of returned packets.
978*4882a593Smuzhiyun 		 */
979*4882a593Smuzhiyun 		rx_pkts[nb_rx++] = rxm;
980*4882a593Smuzhiyun 	}
981*4882a593Smuzhiyun 	rxq->rx_tail = rx_id;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	/*
984*4882a593Smuzhiyun 	 * If the number of free RX descriptors is greater than the RX free
985*4882a593Smuzhiyun 	 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
986*4882a593Smuzhiyun 	 * register.
987*4882a593Smuzhiyun 	 * Update the RDT with the value of the last processed RX descriptor
988*4882a593Smuzhiyun 	 * minus 1, to guarantee that the RDT register is never equal to the
989*4882a593Smuzhiyun 	 * RDH register, which creates a "full" ring situtation from the
990*4882a593Smuzhiyun 	 * hardware point of view...
991*4882a593Smuzhiyun 	 */
992*4882a593Smuzhiyun 	nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
993*4882a593Smuzhiyun 	if (nb_hold > rxq->rx_free_thresh) {
994*4882a593Smuzhiyun 		PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
995*4882a593Smuzhiyun 			   "nb_hold=%u nb_rx=%u",
996*4882a593Smuzhiyun 			   (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
997*4882a593Smuzhiyun 			   (unsigned) rx_id, (unsigned) nb_hold,
998*4882a593Smuzhiyun 			   (unsigned) nb_rx);
999*4882a593Smuzhiyun 		rx_id = (uint16_t) ((rx_id == 0) ?
1000*4882a593Smuzhiyun 				     (rxq->nb_rx_desc - 1) : (rx_id - 1));
1001*4882a593Smuzhiyun 		E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1002*4882a593Smuzhiyun 		nb_hold = 0;
1003*4882a593Smuzhiyun 	}
1004*4882a593Smuzhiyun 	rxq->nb_rx_hold = nb_hold;
1005*4882a593Smuzhiyun 	return nb_rx;
1006*4882a593Smuzhiyun }
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun uint16_t
eth_igb_recv_scattered_pkts(void * rx_queue,struct rte_mbuf ** rx_pkts,uint16_t nb_pkts)1009*4882a593Smuzhiyun eth_igb_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
1010*4882a593Smuzhiyun 			 uint16_t nb_pkts)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
1013*4882a593Smuzhiyun 	volatile union e1000_adv_rx_desc *rx_ring;
1014*4882a593Smuzhiyun 	volatile union e1000_adv_rx_desc *rxdp;
1015*4882a593Smuzhiyun 	struct igb_rx_entry *sw_ring;
1016*4882a593Smuzhiyun 	struct igb_rx_entry *rxe;
1017*4882a593Smuzhiyun 	struct rte_mbuf *first_seg;
1018*4882a593Smuzhiyun 	struct rte_mbuf *last_seg;
1019*4882a593Smuzhiyun 	struct rte_mbuf *rxm;
1020*4882a593Smuzhiyun 	struct rte_mbuf *nmb;
1021*4882a593Smuzhiyun 	union e1000_adv_rx_desc rxd;
1022*4882a593Smuzhiyun 	uint64_t dma; /* Physical address of mbuf data buffer */
1023*4882a593Smuzhiyun 	uint32_t staterr;
1024*4882a593Smuzhiyun 	uint32_t hlen_type_rss;
1025*4882a593Smuzhiyun 	uint16_t rx_id;
1026*4882a593Smuzhiyun 	uint16_t nb_rx;
1027*4882a593Smuzhiyun 	uint16_t nb_hold;
1028*4882a593Smuzhiyun 	uint16_t data_len;
1029*4882a593Smuzhiyun 	uint64_t pkt_flags;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	nb_rx = 0;
1032*4882a593Smuzhiyun 	nb_hold = 0;
1033*4882a593Smuzhiyun 	rxq = rx_queue;
1034*4882a593Smuzhiyun 	rx_id = rxq->rx_tail;
1035*4882a593Smuzhiyun 	rx_ring = rxq->rx_ring;
1036*4882a593Smuzhiyun 	sw_ring = rxq->sw_ring;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	/*
1039*4882a593Smuzhiyun 	 * Retrieve RX context of current packet, if any.
1040*4882a593Smuzhiyun 	 */
1041*4882a593Smuzhiyun 	first_seg = rxq->pkt_first_seg;
1042*4882a593Smuzhiyun 	last_seg = rxq->pkt_last_seg;
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	while (nb_rx < nb_pkts) {
1045*4882a593Smuzhiyun 	next_desc:
1046*4882a593Smuzhiyun 		/*
1047*4882a593Smuzhiyun 		 * The order of operations here is important as the DD status
1048*4882a593Smuzhiyun 		 * bit must not be read after any other descriptor fields.
1049*4882a593Smuzhiyun 		 * rx_ring and rxdp are pointing to volatile data so the order
1050*4882a593Smuzhiyun 		 * of accesses cannot be reordered by the compiler. If they were
1051*4882a593Smuzhiyun 		 * not volatile, they could be reordered which could lead to
1052*4882a593Smuzhiyun 		 * using invalid descriptor fields when read from rxd.
1053*4882a593Smuzhiyun 		 */
1054*4882a593Smuzhiyun 		rxdp = &rx_ring[rx_id];
1055*4882a593Smuzhiyun 		staterr = rxdp->wb.upper.status_error;
1056*4882a593Smuzhiyun 		if (! (staterr & rte_cpu_to_le_32(E1000_RXD_STAT_DD)))
1057*4882a593Smuzhiyun 			break;
1058*4882a593Smuzhiyun 		rxd = *rxdp;
1059*4882a593Smuzhiyun 
1060*4882a593Smuzhiyun 		/*
1061*4882a593Smuzhiyun 		 * Descriptor done.
1062*4882a593Smuzhiyun 		 *
1063*4882a593Smuzhiyun 		 * Allocate a new mbuf to replenish the RX ring descriptor.
1064*4882a593Smuzhiyun 		 * If the allocation fails:
1065*4882a593Smuzhiyun 		 *    - arrange for that RX descriptor to be the first one
1066*4882a593Smuzhiyun 		 *      being parsed the next time the receive function is
1067*4882a593Smuzhiyun 		 *      invoked [on the same queue].
1068*4882a593Smuzhiyun 		 *
1069*4882a593Smuzhiyun 		 *    - Stop parsing the RX ring and return immediately.
1070*4882a593Smuzhiyun 		 *
1071*4882a593Smuzhiyun 		 * This policy does not drop the packet received in the RX
1072*4882a593Smuzhiyun 		 * descriptor for which the allocation of a new mbuf failed.
1073*4882a593Smuzhiyun 		 * Thus, it allows that packet to be later retrieved if
1074*4882a593Smuzhiyun 		 * mbuf have been freed in the mean time.
1075*4882a593Smuzhiyun 		 * As a side effect, holding RX descriptors instead of
1076*4882a593Smuzhiyun 		 * systematically giving them back to the NIC may lead to
1077*4882a593Smuzhiyun 		 * RX ring exhaustion situations.
1078*4882a593Smuzhiyun 		 * However, the NIC can gracefully prevent such situations
1079*4882a593Smuzhiyun 		 * to happen by sending specific "back-pressure" flow control
1080*4882a593Smuzhiyun 		 * frames to its peer(s).
1081*4882a593Smuzhiyun 		 */
1082*4882a593Smuzhiyun 		PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_id=%u "
1083*4882a593Smuzhiyun 			   "staterr=0x%x data_len=%u",
1084*4882a593Smuzhiyun 			   (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1085*4882a593Smuzhiyun 			   (unsigned) rx_id, (unsigned) staterr,
1086*4882a593Smuzhiyun 			   (unsigned) rte_le_to_cpu_16(rxd.wb.upper.length));
1087*4882a593Smuzhiyun 
1088*4882a593Smuzhiyun 		nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1089*4882a593Smuzhiyun 		if (nmb == NULL) {
1090*4882a593Smuzhiyun 			PMD_RX_LOG(DEBUG, "RX mbuf alloc failed port_id=%u "
1091*4882a593Smuzhiyun 				   "queue_id=%u", (unsigned) rxq->port_id,
1092*4882a593Smuzhiyun 				   (unsigned) rxq->queue_id);
1093*4882a593Smuzhiyun 			rte_eth_devices[rxq->port_id].data->rx_mbuf_alloc_failed++;
1094*4882a593Smuzhiyun 			break;
1095*4882a593Smuzhiyun 		}
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun 		nb_hold++;
1098*4882a593Smuzhiyun 		rxe = &sw_ring[rx_id];
1099*4882a593Smuzhiyun 		rx_id++;
1100*4882a593Smuzhiyun 		if (rx_id == rxq->nb_rx_desc)
1101*4882a593Smuzhiyun 			rx_id = 0;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 		/* Prefetch next mbuf while processing current one. */
1104*4882a593Smuzhiyun 		rte_igb_prefetch(sw_ring[rx_id].mbuf);
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 		/*
1107*4882a593Smuzhiyun 		 * When next RX descriptor is on a cache-line boundary,
1108*4882a593Smuzhiyun 		 * prefetch the next 4 RX descriptors and the next 8 pointers
1109*4882a593Smuzhiyun 		 * to mbufs.
1110*4882a593Smuzhiyun 		 */
1111*4882a593Smuzhiyun 		if ((rx_id & 0x3) == 0) {
1112*4882a593Smuzhiyun 			rte_igb_prefetch(&rx_ring[rx_id]);
1113*4882a593Smuzhiyun 			rte_igb_prefetch(&sw_ring[rx_id]);
1114*4882a593Smuzhiyun 		}
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 		/*
1117*4882a593Smuzhiyun 		 * Update RX descriptor with the physical address of the new
1118*4882a593Smuzhiyun 		 * data buffer of the new allocated mbuf.
1119*4882a593Smuzhiyun 		 */
1120*4882a593Smuzhiyun 		rxm = rxe->mbuf;
1121*4882a593Smuzhiyun 		rxe->mbuf = nmb;
1122*4882a593Smuzhiyun 		dma = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1123*4882a593Smuzhiyun 		rxdp->read.pkt_addr = dma;
1124*4882a593Smuzhiyun 		rxdp->read.hdr_addr = 0;
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 		/*
1127*4882a593Smuzhiyun 		 * Set data length & data buffer address of mbuf.
1128*4882a593Smuzhiyun 		 */
1129*4882a593Smuzhiyun 		data_len = rte_le_to_cpu_16(rxd.wb.upper.length);
1130*4882a593Smuzhiyun 		rxm->data_len = data_len;
1131*4882a593Smuzhiyun 		rxm->data_off = RTE_PKTMBUF_HEADROOM;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 		/*
1134*4882a593Smuzhiyun 		 * If this is the first buffer of the received packet,
1135*4882a593Smuzhiyun 		 * set the pointer to the first mbuf of the packet and
1136*4882a593Smuzhiyun 		 * initialize its context.
1137*4882a593Smuzhiyun 		 * Otherwise, update the total length and the number of segments
1138*4882a593Smuzhiyun 		 * of the current scattered packet, and update the pointer to
1139*4882a593Smuzhiyun 		 * the last mbuf of the current packet.
1140*4882a593Smuzhiyun 		 */
1141*4882a593Smuzhiyun 		if (first_seg == NULL) {
1142*4882a593Smuzhiyun 			first_seg = rxm;
1143*4882a593Smuzhiyun 			first_seg->pkt_len = data_len;
1144*4882a593Smuzhiyun 			first_seg->nb_segs = 1;
1145*4882a593Smuzhiyun 		} else {
1146*4882a593Smuzhiyun 			first_seg->pkt_len += data_len;
1147*4882a593Smuzhiyun 			first_seg->nb_segs++;
1148*4882a593Smuzhiyun 			last_seg->next = rxm;
1149*4882a593Smuzhiyun 		}
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 		/*
1152*4882a593Smuzhiyun 		 * If this is not the last buffer of the received packet,
1153*4882a593Smuzhiyun 		 * update the pointer to the last mbuf of the current scattered
1154*4882a593Smuzhiyun 		 * packet and continue to parse the RX ring.
1155*4882a593Smuzhiyun 		 */
1156*4882a593Smuzhiyun 		if (! (staterr & E1000_RXD_STAT_EOP)) {
1157*4882a593Smuzhiyun 			last_seg = rxm;
1158*4882a593Smuzhiyun 			goto next_desc;
1159*4882a593Smuzhiyun 		}
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 		/*
1162*4882a593Smuzhiyun 		 * This is the last buffer of the received packet.
1163*4882a593Smuzhiyun 		 * If the CRC is not stripped by the hardware:
1164*4882a593Smuzhiyun 		 *   - Subtract the CRC	length from the total packet length.
1165*4882a593Smuzhiyun 		 *   - If the last buffer only contains the whole CRC or a part
1166*4882a593Smuzhiyun 		 *     of it, free the mbuf associated to the last buffer.
1167*4882a593Smuzhiyun 		 *     If part of the CRC is also contained in the previous
1168*4882a593Smuzhiyun 		 *     mbuf, subtract the length of that CRC part from the
1169*4882a593Smuzhiyun 		 *     data length of the previous mbuf.
1170*4882a593Smuzhiyun 		 */
1171*4882a593Smuzhiyun 		rxm->next = NULL;
1172*4882a593Smuzhiyun 		if (unlikely(rxq->crc_len > 0)) {
1173*4882a593Smuzhiyun 			first_seg->pkt_len -= RTE_ETHER_CRC_LEN;
1174*4882a593Smuzhiyun 			if (data_len <= RTE_ETHER_CRC_LEN) {
1175*4882a593Smuzhiyun 				rte_pktmbuf_free_seg(rxm);
1176*4882a593Smuzhiyun 				first_seg->nb_segs--;
1177*4882a593Smuzhiyun 				last_seg->data_len = (uint16_t)
1178*4882a593Smuzhiyun 					(last_seg->data_len -
1179*4882a593Smuzhiyun 					 (RTE_ETHER_CRC_LEN - data_len));
1180*4882a593Smuzhiyun 				last_seg->next = NULL;
1181*4882a593Smuzhiyun 			} else
1182*4882a593Smuzhiyun 				rxm->data_len = (uint16_t)
1183*4882a593Smuzhiyun 					(data_len - RTE_ETHER_CRC_LEN);
1184*4882a593Smuzhiyun 		}
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 		/*
1187*4882a593Smuzhiyun 		 * Initialize the first mbuf of the returned packet:
1188*4882a593Smuzhiyun 		 *    - RX port identifier,
1189*4882a593Smuzhiyun 		 *    - hardware offload data, if any:
1190*4882a593Smuzhiyun 		 *      - RSS flag & hash,
1191*4882a593Smuzhiyun 		 *      - IP checksum flag,
1192*4882a593Smuzhiyun 		 *      - VLAN TCI, if any,
1193*4882a593Smuzhiyun 		 *      - error flags.
1194*4882a593Smuzhiyun 		 */
1195*4882a593Smuzhiyun 		first_seg->port = rxq->port_id;
1196*4882a593Smuzhiyun 		first_seg->hash.rss = rxd.wb.lower.hi_dword.rss;
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 		/*
1199*4882a593Smuzhiyun 		 * The vlan_tci field is only valid when RTE_MBUF_F_RX_VLAN is
1200*4882a593Smuzhiyun 		 * set in the pkt_flags field and must be in CPU byte order.
1201*4882a593Smuzhiyun 		 */
1202*4882a593Smuzhiyun 		if ((staterr & rte_cpu_to_le_32(E1000_RXDEXT_STATERR_LB)) &&
1203*4882a593Smuzhiyun 				(rxq->flags & IGB_RXQ_FLAG_LB_BSWAP_VLAN)) {
1204*4882a593Smuzhiyun 			first_seg->vlan_tci =
1205*4882a593Smuzhiyun 				rte_be_to_cpu_16(rxd.wb.upper.vlan);
1206*4882a593Smuzhiyun 		} else {
1207*4882a593Smuzhiyun 			first_seg->vlan_tci =
1208*4882a593Smuzhiyun 				rte_le_to_cpu_16(rxd.wb.upper.vlan);
1209*4882a593Smuzhiyun 		}
1210*4882a593Smuzhiyun 		hlen_type_rss = rte_le_to_cpu_32(rxd.wb.lower.lo_dword.data);
1211*4882a593Smuzhiyun 		pkt_flags = rx_desc_hlen_type_rss_to_pkt_flags(rxq, hlen_type_rss);
1212*4882a593Smuzhiyun 		pkt_flags = pkt_flags | rx_desc_status_to_pkt_flags(staterr);
1213*4882a593Smuzhiyun 		pkt_flags = pkt_flags | rx_desc_error_to_pkt_flags(staterr);
1214*4882a593Smuzhiyun 		first_seg->ol_flags = pkt_flags;
1215*4882a593Smuzhiyun 		first_seg->packet_type = igb_rxd_pkt_info_to_pkt_type(rxd.wb.
1216*4882a593Smuzhiyun 					lower.lo_dword.hs_rss.pkt_info);
1217*4882a593Smuzhiyun 
1218*4882a593Smuzhiyun 		/* Prefetch data of first segment, if configured to do so. */
1219*4882a593Smuzhiyun 		rte_packet_prefetch((char *)first_seg->buf_addr +
1220*4882a593Smuzhiyun 			first_seg->data_off);
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun 		/*
1223*4882a593Smuzhiyun 		 * Store the mbuf address into the next entry of the array
1224*4882a593Smuzhiyun 		 * of returned packets.
1225*4882a593Smuzhiyun 		 */
1226*4882a593Smuzhiyun 		rx_pkts[nb_rx++] = first_seg;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 		/*
1229*4882a593Smuzhiyun 		 * Setup receipt context for a new packet.
1230*4882a593Smuzhiyun 		 */
1231*4882a593Smuzhiyun 		first_seg = NULL;
1232*4882a593Smuzhiyun 	}
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 	/*
1235*4882a593Smuzhiyun 	 * Record index of the next RX descriptor to probe.
1236*4882a593Smuzhiyun 	 */
1237*4882a593Smuzhiyun 	rxq->rx_tail = rx_id;
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun 	/*
1240*4882a593Smuzhiyun 	 * Save receive context.
1241*4882a593Smuzhiyun 	 */
1242*4882a593Smuzhiyun 	rxq->pkt_first_seg = first_seg;
1243*4882a593Smuzhiyun 	rxq->pkt_last_seg = last_seg;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	/*
1246*4882a593Smuzhiyun 	 * If the number of free RX descriptors is greater than the RX free
1247*4882a593Smuzhiyun 	 * threshold of the queue, advance the Receive Descriptor Tail (RDT)
1248*4882a593Smuzhiyun 	 * register.
1249*4882a593Smuzhiyun 	 * Update the RDT with the value of the last processed RX descriptor
1250*4882a593Smuzhiyun 	 * minus 1, to guarantee that the RDT register is never equal to the
1251*4882a593Smuzhiyun 	 * RDH register, which creates a "full" ring situtation from the
1252*4882a593Smuzhiyun 	 * hardware point of view...
1253*4882a593Smuzhiyun 	 */
1254*4882a593Smuzhiyun 	nb_hold = (uint16_t) (nb_hold + rxq->nb_rx_hold);
1255*4882a593Smuzhiyun 	if (nb_hold > rxq->rx_free_thresh) {
1256*4882a593Smuzhiyun 		PMD_RX_LOG(DEBUG, "port_id=%u queue_id=%u rx_tail=%u "
1257*4882a593Smuzhiyun 			   "nb_hold=%u nb_rx=%u",
1258*4882a593Smuzhiyun 			   (unsigned) rxq->port_id, (unsigned) rxq->queue_id,
1259*4882a593Smuzhiyun 			   (unsigned) rx_id, (unsigned) nb_hold,
1260*4882a593Smuzhiyun 			   (unsigned) nb_rx);
1261*4882a593Smuzhiyun 		rx_id = (uint16_t) ((rx_id == 0) ?
1262*4882a593Smuzhiyun 				     (rxq->nb_rx_desc - 1) : (rx_id - 1));
1263*4882a593Smuzhiyun 		E1000_PCI_REG_WRITE(rxq->rdt_reg_addr, rx_id);
1264*4882a593Smuzhiyun 		nb_hold = 0;
1265*4882a593Smuzhiyun 	}
1266*4882a593Smuzhiyun 	rxq->nb_rx_hold = nb_hold;
1267*4882a593Smuzhiyun 	return nb_rx;
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun /*
1271*4882a593Smuzhiyun  * Maximum number of Ring Descriptors.
1272*4882a593Smuzhiyun  *
1273*4882a593Smuzhiyun  * Since RDLEN/TDLEN should be multiple of 128bytes, the number of ring
1274*4882a593Smuzhiyun  * desscriptors should meet the following condition:
1275*4882a593Smuzhiyun  *      (num_ring_desc * sizeof(struct e1000_rx/tx_desc)) % 128 == 0
1276*4882a593Smuzhiyun  */
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun static void
igb_tx_queue_release_mbufs(struct igb_tx_queue * txq)1279*4882a593Smuzhiyun igb_tx_queue_release_mbufs(struct igb_tx_queue *txq)
1280*4882a593Smuzhiyun {
1281*4882a593Smuzhiyun 	unsigned i;
1282*4882a593Smuzhiyun 
1283*4882a593Smuzhiyun 	if (txq->sw_ring != NULL) {
1284*4882a593Smuzhiyun 		for (i = 0; i < txq->nb_tx_desc; i++) {
1285*4882a593Smuzhiyun 			if (txq->sw_ring[i].mbuf != NULL) {
1286*4882a593Smuzhiyun 				rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
1287*4882a593Smuzhiyun 				txq->sw_ring[i].mbuf = NULL;
1288*4882a593Smuzhiyun 			}
1289*4882a593Smuzhiyun 		}
1290*4882a593Smuzhiyun 	}
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun static void
igb_tx_queue_release(struct igb_tx_queue * txq)1294*4882a593Smuzhiyun igb_tx_queue_release(struct igb_tx_queue *txq)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	if (txq != NULL) {
1297*4882a593Smuzhiyun 		igb_tx_queue_release_mbufs(txq);
1298*4882a593Smuzhiyun 		rte_free(txq->sw_ring);
1299*4882a593Smuzhiyun 		rte_memzone_free(txq->mz);
1300*4882a593Smuzhiyun 		rte_free(txq);
1301*4882a593Smuzhiyun 	}
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun void
eth_igb_tx_queue_release(struct rte_eth_dev * dev,uint16_t qid)1305*4882a593Smuzhiyun eth_igb_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1306*4882a593Smuzhiyun {
1307*4882a593Smuzhiyun 	igb_tx_queue_release(dev->data->tx_queues[qid]);
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun static int
igb_tx_done_cleanup(struct igb_tx_queue * txq,uint32_t free_cnt)1311*4882a593Smuzhiyun igb_tx_done_cleanup(struct igb_tx_queue *txq, uint32_t free_cnt)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	struct igb_tx_entry *sw_ring;
1314*4882a593Smuzhiyun 	volatile union e1000_adv_tx_desc *txr;
1315*4882a593Smuzhiyun 	uint16_t tx_first; /* First segment analyzed. */
1316*4882a593Smuzhiyun 	uint16_t tx_id;    /* Current segment being processed. */
1317*4882a593Smuzhiyun 	uint16_t tx_last;  /* Last segment in the current packet. */
1318*4882a593Smuzhiyun 	uint16_t tx_next;  /* First segment of the next packet. */
1319*4882a593Smuzhiyun 	int count = 0;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	if (!txq)
1322*4882a593Smuzhiyun 		return -ENODEV;
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	sw_ring = txq->sw_ring;
1325*4882a593Smuzhiyun 	txr = txq->tx_ring;
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	/* tx_tail is the last sent packet on the sw_ring. Goto the end
1328*4882a593Smuzhiyun 	 * of that packet (the last segment in the packet chain) and
1329*4882a593Smuzhiyun 	 * then the next segment will be the start of the oldest segment
1330*4882a593Smuzhiyun 	 * in the sw_ring. This is the first packet that will be
1331*4882a593Smuzhiyun 	 * attempted to be freed.
1332*4882a593Smuzhiyun 	 */
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	/* Get last segment in most recently added packet. */
1335*4882a593Smuzhiyun 	tx_first = sw_ring[txq->tx_tail].last_id;
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	/* Get the next segment, which is the oldest segment in ring. */
1338*4882a593Smuzhiyun 	tx_first = sw_ring[tx_first].next_id;
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	/* Set the current index to the first. */
1341*4882a593Smuzhiyun 	tx_id = tx_first;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	/* Loop through each packet. For each packet, verify that an
1344*4882a593Smuzhiyun 	 * mbuf exists and that the last segment is free. If so, free
1345*4882a593Smuzhiyun 	 * it and move on.
1346*4882a593Smuzhiyun 	 */
1347*4882a593Smuzhiyun 	while (1) {
1348*4882a593Smuzhiyun 		tx_last = sw_ring[tx_id].last_id;
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 		if (sw_ring[tx_last].mbuf) {
1351*4882a593Smuzhiyun 			if (txr[tx_last].wb.status &
1352*4882a593Smuzhiyun 			    E1000_TXD_STAT_DD) {
1353*4882a593Smuzhiyun 				/* Increment the number of packets
1354*4882a593Smuzhiyun 				 * freed.
1355*4882a593Smuzhiyun 				 */
1356*4882a593Smuzhiyun 				count++;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 				/* Get the start of the next packet. */
1359*4882a593Smuzhiyun 				tx_next = sw_ring[tx_last].next_id;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 				/* Loop through all segments in a
1362*4882a593Smuzhiyun 				 * packet.
1363*4882a593Smuzhiyun 				 */
1364*4882a593Smuzhiyun 				do {
1365*4882a593Smuzhiyun 					if (sw_ring[tx_id].mbuf) {
1366*4882a593Smuzhiyun 						rte_pktmbuf_free_seg(
1367*4882a593Smuzhiyun 							sw_ring[tx_id].mbuf);
1368*4882a593Smuzhiyun 						sw_ring[tx_id].mbuf = NULL;
1369*4882a593Smuzhiyun 						sw_ring[tx_id].last_id = tx_id;
1370*4882a593Smuzhiyun 					}
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 					/* Move to next segemnt. */
1373*4882a593Smuzhiyun 					tx_id = sw_ring[tx_id].next_id;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 				} while (tx_id != tx_next);
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 				if (unlikely(count == (int)free_cnt))
1378*4882a593Smuzhiyun 					break;
1379*4882a593Smuzhiyun 			} else {
1380*4882a593Smuzhiyun 				/* mbuf still in use, nothing left to
1381*4882a593Smuzhiyun 				 * free.
1382*4882a593Smuzhiyun 				 */
1383*4882a593Smuzhiyun 				break;
1384*4882a593Smuzhiyun 			}
1385*4882a593Smuzhiyun 		} else {
1386*4882a593Smuzhiyun 			/* There are multiple reasons to be here:
1387*4882a593Smuzhiyun 			 * 1) All the packets on the ring have been
1388*4882a593Smuzhiyun 			 *    freed - tx_id is equal to tx_first
1389*4882a593Smuzhiyun 			 *    and some packets have been freed.
1390*4882a593Smuzhiyun 			 *    - Done, exit
1391*4882a593Smuzhiyun 			 * 2) Interfaces has not sent a rings worth of
1392*4882a593Smuzhiyun 			 *    packets yet, so the segment after tail is
1393*4882a593Smuzhiyun 			 *    still empty. Or a previous call to this
1394*4882a593Smuzhiyun 			 *    function freed some of the segments but
1395*4882a593Smuzhiyun 			 *    not all so there is a hole in the list.
1396*4882a593Smuzhiyun 			 *    Hopefully this is a rare case.
1397*4882a593Smuzhiyun 			 *    - Walk the list and find the next mbuf. If
1398*4882a593Smuzhiyun 			 *      there isn't one, then done.
1399*4882a593Smuzhiyun 			 */
1400*4882a593Smuzhiyun 			if (likely(tx_id == tx_first && count != 0))
1401*4882a593Smuzhiyun 				break;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 			/* Walk the list and find the next mbuf, if any. */
1404*4882a593Smuzhiyun 			do {
1405*4882a593Smuzhiyun 				/* Move to next segemnt. */
1406*4882a593Smuzhiyun 				tx_id = sw_ring[tx_id].next_id;
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 				if (sw_ring[tx_id].mbuf)
1409*4882a593Smuzhiyun 					break;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 			} while (tx_id != tx_first);
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 			/* Determine why previous loop bailed. If there
1414*4882a593Smuzhiyun 			 * is not an mbuf, done.
1415*4882a593Smuzhiyun 			 */
1416*4882a593Smuzhiyun 			if (!sw_ring[tx_id].mbuf)
1417*4882a593Smuzhiyun 				break;
1418*4882a593Smuzhiyun 		}
1419*4882a593Smuzhiyun 	}
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	return count;
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun int
eth_igb_tx_done_cleanup(void * txq,uint32_t free_cnt)1425*4882a593Smuzhiyun eth_igb_tx_done_cleanup(void *txq, uint32_t free_cnt)
1426*4882a593Smuzhiyun {
1427*4882a593Smuzhiyun 	return igb_tx_done_cleanup(txq, free_cnt);
1428*4882a593Smuzhiyun }
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun static void
igb_reset_tx_queue_stat(struct igb_tx_queue * txq)1431*4882a593Smuzhiyun igb_reset_tx_queue_stat(struct igb_tx_queue *txq)
1432*4882a593Smuzhiyun {
1433*4882a593Smuzhiyun 	txq->tx_head = 0;
1434*4882a593Smuzhiyun 	txq->tx_tail = 0;
1435*4882a593Smuzhiyun 	txq->ctx_curr = 0;
1436*4882a593Smuzhiyun 	memset((void*)&txq->ctx_cache, 0,
1437*4882a593Smuzhiyun 		IGB_CTX_NUM * sizeof(struct igb_advctx_info));
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun static void
igb_reset_tx_queue(struct igb_tx_queue * txq,struct rte_eth_dev * dev)1441*4882a593Smuzhiyun igb_reset_tx_queue(struct igb_tx_queue *txq, struct rte_eth_dev *dev)
1442*4882a593Smuzhiyun {
1443*4882a593Smuzhiyun 	static const union e1000_adv_tx_desc zeroed_desc = {{0}};
1444*4882a593Smuzhiyun 	struct igb_tx_entry *txe = txq->sw_ring;
1445*4882a593Smuzhiyun 	uint16_t i, prev;
1446*4882a593Smuzhiyun 	struct e1000_hw *hw;
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1449*4882a593Smuzhiyun 	/* Zero out HW ring memory */
1450*4882a593Smuzhiyun 	for (i = 0; i < txq->nb_tx_desc; i++) {
1451*4882a593Smuzhiyun 		txq->tx_ring[i] = zeroed_desc;
1452*4882a593Smuzhiyun 	}
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 	/* Initialize ring entries */
1455*4882a593Smuzhiyun 	prev = (uint16_t)(txq->nb_tx_desc - 1);
1456*4882a593Smuzhiyun 	for (i = 0; i < txq->nb_tx_desc; i++) {
1457*4882a593Smuzhiyun 		volatile union e1000_adv_tx_desc *txd = &(txq->tx_ring[i]);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 		txd->wb.status = E1000_TXD_STAT_DD;
1460*4882a593Smuzhiyun 		txe[i].mbuf = NULL;
1461*4882a593Smuzhiyun 		txe[i].last_id = i;
1462*4882a593Smuzhiyun 		txe[prev].next_id = i;
1463*4882a593Smuzhiyun 		prev = i;
1464*4882a593Smuzhiyun 	}
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun 	txq->txd_type = E1000_ADVTXD_DTYP_DATA;
1467*4882a593Smuzhiyun 	/* 82575 specific, each tx queue will use 2 hw contexts */
1468*4882a593Smuzhiyun 	if (hw->mac.type == e1000_82575)
1469*4882a593Smuzhiyun 		txq->ctx_start = txq->queue_id * IGB_CTX_NUM;
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	igb_reset_tx_queue_stat(txq);
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun uint64_t
igb_get_tx_port_offloads_capa(struct rte_eth_dev * dev)1475*4882a593Smuzhiyun igb_get_tx_port_offloads_capa(struct rte_eth_dev *dev)
1476*4882a593Smuzhiyun {
1477*4882a593Smuzhiyun 	uint64_t tx_offload_capa;
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 	RTE_SET_USED(dev);
1480*4882a593Smuzhiyun 	tx_offload_capa = RTE_ETH_TX_OFFLOAD_VLAN_INSERT |
1481*4882a593Smuzhiyun 			  RTE_ETH_TX_OFFLOAD_IPV4_CKSUM  |
1482*4882a593Smuzhiyun 			  RTE_ETH_TX_OFFLOAD_UDP_CKSUM   |
1483*4882a593Smuzhiyun 			  RTE_ETH_TX_OFFLOAD_TCP_CKSUM   |
1484*4882a593Smuzhiyun 			  RTE_ETH_TX_OFFLOAD_SCTP_CKSUM  |
1485*4882a593Smuzhiyun 			  RTE_ETH_TX_OFFLOAD_TCP_TSO     |
1486*4882a593Smuzhiyun 			  RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
1487*4882a593Smuzhiyun 
1488*4882a593Smuzhiyun 	return tx_offload_capa;
1489*4882a593Smuzhiyun }
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun uint64_t
igb_get_tx_queue_offloads_capa(struct rte_eth_dev * dev)1492*4882a593Smuzhiyun igb_get_tx_queue_offloads_capa(struct rte_eth_dev *dev)
1493*4882a593Smuzhiyun {
1494*4882a593Smuzhiyun 	uint64_t tx_queue_offload_capa;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	tx_queue_offload_capa = igb_get_tx_port_offloads_capa(dev);
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	return tx_queue_offload_capa;
1499*4882a593Smuzhiyun }
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun int
eth_igb_tx_queue_setup(struct rte_eth_dev * dev,uint16_t queue_idx,uint16_t nb_desc,unsigned int socket_id,const struct rte_eth_txconf * tx_conf)1502*4882a593Smuzhiyun eth_igb_tx_queue_setup(struct rte_eth_dev *dev,
1503*4882a593Smuzhiyun 			 uint16_t queue_idx,
1504*4882a593Smuzhiyun 			 uint16_t nb_desc,
1505*4882a593Smuzhiyun 			 unsigned int socket_id,
1506*4882a593Smuzhiyun 			 const struct rte_eth_txconf *tx_conf)
1507*4882a593Smuzhiyun {
1508*4882a593Smuzhiyun 	const struct rte_memzone *tz;
1509*4882a593Smuzhiyun 	struct igb_tx_queue *txq;
1510*4882a593Smuzhiyun 	struct e1000_hw     *hw;
1511*4882a593Smuzhiyun 	uint32_t size;
1512*4882a593Smuzhiyun 	uint64_t offloads;
1513*4882a593Smuzhiyun 	uint64_t index;
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun 	offloads = tx_conf->offloads | dev->data->dev_conf.txmode.offloads;
1516*4882a593Smuzhiyun 
1517*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 	/*
1520*4882a593Smuzhiyun 	 * Validate number of transmit descriptors.
1521*4882a593Smuzhiyun 	 * It must not exceed hardware maximum, and must be multiple
1522*4882a593Smuzhiyun 	 * of E1000_ALIGN.
1523*4882a593Smuzhiyun 	 */
1524*4882a593Smuzhiyun 	if (nb_desc % IGB_TXD_ALIGN != 0 ||
1525*4882a593Smuzhiyun 			(nb_desc > E1000_MAX_RING_DESC) ||
1526*4882a593Smuzhiyun 			(nb_desc < E1000_MIN_RING_DESC)) {
1527*4882a593Smuzhiyun 		return -EINVAL;
1528*4882a593Smuzhiyun 	}
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	/*
1531*4882a593Smuzhiyun 	 * The tx_free_thresh and tx_rs_thresh values are not used in the 1G
1532*4882a593Smuzhiyun 	 * driver.
1533*4882a593Smuzhiyun 	 */
1534*4882a593Smuzhiyun 	if (tx_conf->tx_free_thresh != 0)
1535*4882a593Smuzhiyun 		PMD_INIT_LOG(INFO, "The tx_free_thresh parameter is not "
1536*4882a593Smuzhiyun 			     "used for the 1G driver.");
1537*4882a593Smuzhiyun 	if (tx_conf->tx_rs_thresh != 0)
1538*4882a593Smuzhiyun 		PMD_INIT_LOG(INFO, "The tx_rs_thresh parameter is not "
1539*4882a593Smuzhiyun 			     "used for the 1G driver.");
1540*4882a593Smuzhiyun 	if (tx_conf->tx_thresh.wthresh == 0 && hw->mac.type != e1000_82576)
1541*4882a593Smuzhiyun 		PMD_INIT_LOG(INFO, "To improve 1G driver performance, "
1542*4882a593Smuzhiyun 			     "consider setting the TX WTHRESH value to 4, 8, "
1543*4882a593Smuzhiyun 			     "or 16.");
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	/* Free memory prior to re-allocation if needed */
1546*4882a593Smuzhiyun 	if (dev->data->tx_queues[queue_idx] != NULL) {
1547*4882a593Smuzhiyun 		igb_tx_queue_release(dev->data->tx_queues[queue_idx]);
1548*4882a593Smuzhiyun 		dev->data->tx_queues[queue_idx] = NULL;
1549*4882a593Smuzhiyun 	}
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	/* First allocate the tx queue data structure */
1552*4882a593Smuzhiyun 	txq = rte_zmalloc("ethdev TX queue", sizeof(struct igb_tx_queue),
1553*4882a593Smuzhiyun 							RTE_CACHE_LINE_SIZE);
1554*4882a593Smuzhiyun 	if (txq == NULL)
1555*4882a593Smuzhiyun 		return -ENOMEM;
1556*4882a593Smuzhiyun 
1557*4882a593Smuzhiyun 	/*
1558*4882a593Smuzhiyun 	 * Allocate TX ring hardware descriptors. A memzone large enough to
1559*4882a593Smuzhiyun 	 * handle the maximum ring size is allocated in order to allow for
1560*4882a593Smuzhiyun 	 * resizing in later calls to the queue setup function.
1561*4882a593Smuzhiyun 	 */
1562*4882a593Smuzhiyun 	size = sizeof(union e1000_adv_tx_desc) * E1000_MAX_RING_DESC;
1563*4882a593Smuzhiyun 	tz = rte_eth_dma_zone_reserve(dev, "tx_ring", queue_idx, size,
1564*4882a593Smuzhiyun 				      E1000_ALIGN, socket_id);
1565*4882a593Smuzhiyun 	if (tz == NULL) {
1566*4882a593Smuzhiyun 		igb_tx_queue_release(txq);
1567*4882a593Smuzhiyun 		return -ENOMEM;
1568*4882a593Smuzhiyun 	}
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	txq->mz = tz;
1571*4882a593Smuzhiyun 	txq->nb_tx_desc = nb_desc;
1572*4882a593Smuzhiyun 	txq->pthresh = tx_conf->tx_thresh.pthresh;
1573*4882a593Smuzhiyun 	txq->hthresh = tx_conf->tx_thresh.hthresh;
1574*4882a593Smuzhiyun 	txq->wthresh = tx_conf->tx_thresh.wthresh;
1575*4882a593Smuzhiyun 	if (txq->wthresh > 0 && hw->mac.type == e1000_82576)
1576*4882a593Smuzhiyun 		txq->wthresh = 1;
1577*4882a593Smuzhiyun 	txq->queue_id = queue_idx;
1578*4882a593Smuzhiyun 	txq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
1579*4882a593Smuzhiyun 		queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
1580*4882a593Smuzhiyun 	txq->port_id = dev->data->port_id;
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	txq->tdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_TDT(txq->reg_idx));
1583*4882a593Smuzhiyun 	txq->tx_ring_phys_addr = tz->iova;
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 	txq->tx_ring = (union e1000_adv_tx_desc *) tz->addr;
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	index = ((uint64_t)hw->hw_addr - base_hw_addr) / 0x104000;
1588*4882a593Smuzhiyun 	txq->tx_ring_phys_addr = igb_gbd_addr_t_p[index];
1589*4882a593Smuzhiyun 	txq->tx_ring = (union e1000_adv_tx_desc *)igb_gbd_addr_t_v[index];
1590*4882a593Smuzhiyun 	printf("hw tx ring size: %d:%ld[0x%lx:%p]\n",
1591*4882a593Smuzhiyun 							size,
1592*4882a593Smuzhiyun 							sizeof(union e1000_adv_tx_desc),
1593*4882a593Smuzhiyun 							txq->tx_ring_phys_addr,
1594*4882a593Smuzhiyun 							txq->tx_ring);
1595*4882a593Smuzhiyun 
1596*4882a593Smuzhiyun 	/* Allocate software ring */
1597*4882a593Smuzhiyun 	txq->sw_ring = rte_zmalloc("txq->sw_ring",
1598*4882a593Smuzhiyun 				   sizeof(struct igb_tx_entry) * nb_desc,
1599*4882a593Smuzhiyun 				   RTE_CACHE_LINE_SIZE);
1600*4882a593Smuzhiyun 	if (txq->sw_ring == NULL) {
1601*4882a593Smuzhiyun 		igb_tx_queue_release(txq);
1602*4882a593Smuzhiyun 		return -ENOMEM;
1603*4882a593Smuzhiyun 	}
1604*4882a593Smuzhiyun 	PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1605*4882a593Smuzhiyun 		     txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr);
1606*4882a593Smuzhiyun 
1607*4882a593Smuzhiyun 	igb_reset_tx_queue(txq, dev);
1608*4882a593Smuzhiyun 	dev->tx_pkt_burst = eth_igb_xmit_pkts;
1609*4882a593Smuzhiyun 	dev->tx_pkt_prepare = &eth_igb_prep_pkts;
1610*4882a593Smuzhiyun 	dev->data->tx_queues[queue_idx] = txq;
1611*4882a593Smuzhiyun 	txq->offloads = offloads;
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun 	return 0;
1614*4882a593Smuzhiyun }
1615*4882a593Smuzhiyun 
1616*4882a593Smuzhiyun static void
igb_rx_queue_release_mbufs(struct igb_rx_queue * rxq)1617*4882a593Smuzhiyun igb_rx_queue_release_mbufs(struct igb_rx_queue *rxq)
1618*4882a593Smuzhiyun {
1619*4882a593Smuzhiyun 	unsigned i;
1620*4882a593Smuzhiyun 
1621*4882a593Smuzhiyun 	if (rxq->sw_ring != NULL) {
1622*4882a593Smuzhiyun 		for (i = 0; i < rxq->nb_rx_desc; i++) {
1623*4882a593Smuzhiyun 			if (rxq->sw_ring[i].mbuf != NULL) {
1624*4882a593Smuzhiyun 				rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
1625*4882a593Smuzhiyun 				rxq->sw_ring[i].mbuf = NULL;
1626*4882a593Smuzhiyun 			}
1627*4882a593Smuzhiyun 		}
1628*4882a593Smuzhiyun 	}
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun static void
igb_rx_queue_release(struct igb_rx_queue * rxq)1632*4882a593Smuzhiyun igb_rx_queue_release(struct igb_rx_queue *rxq)
1633*4882a593Smuzhiyun {
1634*4882a593Smuzhiyun 	if (rxq != NULL) {
1635*4882a593Smuzhiyun 		igb_rx_queue_release_mbufs(rxq);
1636*4882a593Smuzhiyun 		rte_free(rxq->sw_ring);
1637*4882a593Smuzhiyun 		rte_memzone_free(rxq->mz);
1638*4882a593Smuzhiyun 		rte_free(rxq);
1639*4882a593Smuzhiyun 	}
1640*4882a593Smuzhiyun }
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun void
eth_igb_rx_queue_release(struct rte_eth_dev * dev,uint16_t qid)1643*4882a593Smuzhiyun eth_igb_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
1644*4882a593Smuzhiyun {
1645*4882a593Smuzhiyun 	igb_rx_queue_release(dev->data->rx_queues[qid]);
1646*4882a593Smuzhiyun }
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun static void
igb_reset_rx_queue(struct igb_rx_queue * rxq)1649*4882a593Smuzhiyun igb_reset_rx_queue(struct igb_rx_queue *rxq)
1650*4882a593Smuzhiyun {
1651*4882a593Smuzhiyun 	static const union e1000_adv_rx_desc zeroed_desc = {{0}};
1652*4882a593Smuzhiyun 	unsigned i;
1653*4882a593Smuzhiyun 
1654*4882a593Smuzhiyun 	/* Zero out HW ring memory */
1655*4882a593Smuzhiyun 	for (i = 0; i < rxq->nb_rx_desc; i++) {
1656*4882a593Smuzhiyun 		rxq->rx_ring[i] = zeroed_desc;
1657*4882a593Smuzhiyun 	}
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun 	rxq->rx_tail = 0;
1660*4882a593Smuzhiyun 	rxq->pkt_first_seg = NULL;
1661*4882a593Smuzhiyun 	rxq->pkt_last_seg = NULL;
1662*4882a593Smuzhiyun }
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun uint64_t
igb_get_rx_port_offloads_capa(struct rte_eth_dev * dev)1665*4882a593Smuzhiyun igb_get_rx_port_offloads_capa(struct rte_eth_dev *dev)
1666*4882a593Smuzhiyun {
1667*4882a593Smuzhiyun 	uint64_t rx_offload_capa;
1668*4882a593Smuzhiyun 	struct e1000_hw *hw;
1669*4882a593Smuzhiyun 
1670*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1671*4882a593Smuzhiyun 
1672*4882a593Smuzhiyun 	rx_offload_capa = RTE_ETH_RX_OFFLOAD_VLAN_STRIP  |
1673*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_VLAN_FILTER |
1674*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_IPV4_CKSUM  |
1675*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_UDP_CKSUM   |
1676*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_TCP_CKSUM   |
1677*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_KEEP_CRC    |
1678*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_SCATTER     |
1679*4882a593Smuzhiyun 			  RTE_ETH_RX_OFFLOAD_RSS_HASH;
1680*4882a593Smuzhiyun 
1681*4882a593Smuzhiyun 	if (hw->mac.type == e1000_i350 ||
1682*4882a593Smuzhiyun 	    hw->mac.type == e1000_i210 ||
1683*4882a593Smuzhiyun 	    hw->mac.type == e1000_i211)
1684*4882a593Smuzhiyun 		rx_offload_capa |= RTE_ETH_RX_OFFLOAD_VLAN_EXTEND;
1685*4882a593Smuzhiyun 
1686*4882a593Smuzhiyun 	return rx_offload_capa;
1687*4882a593Smuzhiyun }
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun uint64_t
igb_get_rx_queue_offloads_capa(struct rte_eth_dev * dev)1690*4882a593Smuzhiyun igb_get_rx_queue_offloads_capa(struct rte_eth_dev *dev)
1691*4882a593Smuzhiyun {
1692*4882a593Smuzhiyun 	struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1693*4882a593Smuzhiyun 	uint64_t rx_queue_offload_capa;
1694*4882a593Smuzhiyun 
1695*4882a593Smuzhiyun 	switch (hw->mac.type) {
1696*4882a593Smuzhiyun 	case e1000_vfadapt_i350:
1697*4882a593Smuzhiyun 		/*
1698*4882a593Smuzhiyun 		 * As only one Rx queue can be used, let per queue offloading
1699*4882a593Smuzhiyun 		 * capability be same to per port queue offloading capability
1700*4882a593Smuzhiyun 		 * for better convenience.
1701*4882a593Smuzhiyun 		 */
1702*4882a593Smuzhiyun 		rx_queue_offload_capa = igb_get_rx_port_offloads_capa(dev);
1703*4882a593Smuzhiyun 		break;
1704*4882a593Smuzhiyun 	default:
1705*4882a593Smuzhiyun 		rx_queue_offload_capa = 0;
1706*4882a593Smuzhiyun 	}
1707*4882a593Smuzhiyun 	return rx_queue_offload_capa;
1708*4882a593Smuzhiyun }
1709*4882a593Smuzhiyun 
1710*4882a593Smuzhiyun int
eth_igb_rx_queue_setup(struct rte_eth_dev * dev,uint16_t queue_idx,uint16_t nb_desc,unsigned int socket_id,const struct rte_eth_rxconf * rx_conf,struct rte_mempool * mp)1711*4882a593Smuzhiyun eth_igb_rx_queue_setup(struct rte_eth_dev *dev,
1712*4882a593Smuzhiyun 			 uint16_t queue_idx,
1713*4882a593Smuzhiyun 			 uint16_t nb_desc,
1714*4882a593Smuzhiyun 			 unsigned int socket_id,
1715*4882a593Smuzhiyun 			 const struct rte_eth_rxconf *rx_conf,
1716*4882a593Smuzhiyun 			 struct rte_mempool *mp)
1717*4882a593Smuzhiyun {
1718*4882a593Smuzhiyun 	const struct rte_memzone *rz;
1719*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
1720*4882a593Smuzhiyun 	struct e1000_hw     *hw;
1721*4882a593Smuzhiyun 	unsigned int size;
1722*4882a593Smuzhiyun 	uint64_t offloads;
1723*4882a593Smuzhiyun 	uint64_t index;
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	offloads = rx_conf->offloads | dev->data->dev_conf.rxmode.offloads;
1726*4882a593Smuzhiyun 
1727*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1728*4882a593Smuzhiyun 
1729*4882a593Smuzhiyun 	/*
1730*4882a593Smuzhiyun 	 * Validate number of receive descriptors.
1731*4882a593Smuzhiyun 	 * It must not exceed hardware maximum, and must be multiple
1732*4882a593Smuzhiyun 	 * of E1000_ALIGN.
1733*4882a593Smuzhiyun 	 */
1734*4882a593Smuzhiyun 	if (nb_desc % IGB_RXD_ALIGN != 0 ||
1735*4882a593Smuzhiyun 			(nb_desc > E1000_MAX_RING_DESC) ||
1736*4882a593Smuzhiyun 			(nb_desc < E1000_MIN_RING_DESC)) {
1737*4882a593Smuzhiyun 		return -EINVAL;
1738*4882a593Smuzhiyun 	}
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 	/* Free memory prior to re-allocation if needed */
1741*4882a593Smuzhiyun 	if (dev->data->rx_queues[queue_idx] != NULL) {
1742*4882a593Smuzhiyun 		igb_rx_queue_release(dev->data->rx_queues[queue_idx]);
1743*4882a593Smuzhiyun 		dev->data->rx_queues[queue_idx] = NULL;
1744*4882a593Smuzhiyun 	}
1745*4882a593Smuzhiyun 
1746*4882a593Smuzhiyun 	/* First allocate the RX queue data structure. */
1747*4882a593Smuzhiyun 	rxq = rte_zmalloc("ethdev RX queue", sizeof(struct igb_rx_queue),
1748*4882a593Smuzhiyun 			  RTE_CACHE_LINE_SIZE);
1749*4882a593Smuzhiyun 	if (rxq == NULL)
1750*4882a593Smuzhiyun 		return -ENOMEM;
1751*4882a593Smuzhiyun 	rxq->offloads = offloads;
1752*4882a593Smuzhiyun 	rxq->mb_pool = mp;
1753*4882a593Smuzhiyun 	rxq->nb_rx_desc = nb_desc;
1754*4882a593Smuzhiyun 	rxq->pthresh = rx_conf->rx_thresh.pthresh;
1755*4882a593Smuzhiyun 	rxq->hthresh = rx_conf->rx_thresh.hthresh;
1756*4882a593Smuzhiyun 	rxq->wthresh = rx_conf->rx_thresh.wthresh;
1757*4882a593Smuzhiyun 	if (rxq->wthresh > 0 &&
1758*4882a593Smuzhiyun 	    (hw->mac.type == e1000_82576 || hw->mac.type == e1000_vfadapt_i350))
1759*4882a593Smuzhiyun 		rxq->wthresh = 1;
1760*4882a593Smuzhiyun 	rxq->drop_en = rx_conf->rx_drop_en;
1761*4882a593Smuzhiyun 	rxq->rx_free_thresh = rx_conf->rx_free_thresh;
1762*4882a593Smuzhiyun 	rxq->queue_id = queue_idx;
1763*4882a593Smuzhiyun 	rxq->reg_idx = (uint16_t)((RTE_ETH_DEV_SRIOV(dev).active == 0) ?
1764*4882a593Smuzhiyun 		queue_idx : RTE_ETH_DEV_SRIOV(dev).def_pool_q_idx + queue_idx);
1765*4882a593Smuzhiyun 	rxq->port_id = dev->data->port_id;
1766*4882a593Smuzhiyun 	if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
1767*4882a593Smuzhiyun 		rxq->crc_len = RTE_ETHER_CRC_LEN;
1768*4882a593Smuzhiyun 	else
1769*4882a593Smuzhiyun 		rxq->crc_len = 0;
1770*4882a593Smuzhiyun 
1771*4882a593Smuzhiyun 	/*
1772*4882a593Smuzhiyun 	 *  Allocate RX ring hardware descriptors. A memzone large enough to
1773*4882a593Smuzhiyun 	 *  handle the maximum ring size is allocated in order to allow for
1774*4882a593Smuzhiyun 	 *  resizing in later calls to the queue setup function.
1775*4882a593Smuzhiyun 	 */
1776*4882a593Smuzhiyun 	size = sizeof(union e1000_adv_rx_desc) * E1000_MAX_RING_DESC;
1777*4882a593Smuzhiyun 	rz = rte_eth_dma_zone_reserve(dev, "rx_ring", queue_idx, size,
1778*4882a593Smuzhiyun 				      E1000_ALIGN, socket_id);
1779*4882a593Smuzhiyun 	if (rz == NULL) {
1780*4882a593Smuzhiyun 		igb_rx_queue_release(rxq);
1781*4882a593Smuzhiyun 		return -ENOMEM;
1782*4882a593Smuzhiyun 	}
1783*4882a593Smuzhiyun 
1784*4882a593Smuzhiyun 	rxq->mz = rz;
1785*4882a593Smuzhiyun 	rxq->rdt_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDT(rxq->reg_idx));
1786*4882a593Smuzhiyun 	rxq->rdh_reg_addr = E1000_PCI_REG_ADDR(hw, E1000_RDH(rxq->reg_idx));
1787*4882a593Smuzhiyun 	rxq->rx_ring_phys_addr = rz->iova;
1788*4882a593Smuzhiyun 	rxq->rx_ring = (union e1000_adv_rx_desc *) rz->addr;
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun 	index = ((uint64_t)hw->hw_addr - base_hw_addr) / 0x104000;
1791*4882a593Smuzhiyun 	rxq->rx_ring_phys_addr = igb_gbd_addr_r_p[index];
1792*4882a593Smuzhiyun 	rxq->rx_ring = (union e1000_adv_rx_desc *)igb_gbd_addr_r_v[index];
1793*4882a593Smuzhiyun 	printf("hw rx ring size: %d:%ld[0x%lx:%p]\n",
1794*4882a593Smuzhiyun 							size,
1795*4882a593Smuzhiyun 							sizeof(union e1000_adv_rx_desc),
1796*4882a593Smuzhiyun 							rxq->rx_ring_phys_addr,
1797*4882a593Smuzhiyun 							rxq->rx_ring);
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	/* Allocate software ring. */
1800*4882a593Smuzhiyun 	rxq->sw_ring = rte_zmalloc("rxq->sw_ring",
1801*4882a593Smuzhiyun 				   sizeof(struct igb_rx_entry) * nb_desc,
1802*4882a593Smuzhiyun 				   RTE_CACHE_LINE_SIZE);
1803*4882a593Smuzhiyun 	if (rxq->sw_ring == NULL) {
1804*4882a593Smuzhiyun 		igb_rx_queue_release(rxq);
1805*4882a593Smuzhiyun 		return -ENOMEM;
1806*4882a593Smuzhiyun 	}
1807*4882a593Smuzhiyun 	PMD_INIT_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
1808*4882a593Smuzhiyun 		     rxq->sw_ring, rxq->rx_ring, rxq->rx_ring_phys_addr);
1809*4882a593Smuzhiyun 
1810*4882a593Smuzhiyun 	dev->data->rx_queues[queue_idx] = rxq;
1811*4882a593Smuzhiyun 	igb_reset_rx_queue(rxq);
1812*4882a593Smuzhiyun 
1813*4882a593Smuzhiyun 	return 0;
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun 
1816*4882a593Smuzhiyun uint32_t
eth_igb_rx_queue_count(void * rx_queue)1817*4882a593Smuzhiyun eth_igb_rx_queue_count(void *rx_queue)
1818*4882a593Smuzhiyun {
1819*4882a593Smuzhiyun #define IGB_RXQ_SCAN_INTERVAL 4
1820*4882a593Smuzhiyun 	volatile union e1000_adv_rx_desc *rxdp;
1821*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
1822*4882a593Smuzhiyun 	uint32_t desc = 0;
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun 	rxq = rx_queue;
1825*4882a593Smuzhiyun 	rxdp = &(rxq->rx_ring[rxq->rx_tail]);
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 	while ((desc < rxq->nb_rx_desc) &&
1828*4882a593Smuzhiyun 		(rxdp->wb.upper.status_error & E1000_RXD_STAT_DD)) {
1829*4882a593Smuzhiyun 		desc += IGB_RXQ_SCAN_INTERVAL;
1830*4882a593Smuzhiyun 		rxdp += IGB_RXQ_SCAN_INTERVAL;
1831*4882a593Smuzhiyun 		if (rxq->rx_tail + desc >= rxq->nb_rx_desc)
1832*4882a593Smuzhiyun 			rxdp = &(rxq->rx_ring[rxq->rx_tail +
1833*4882a593Smuzhiyun 				desc - rxq->nb_rx_desc]);
1834*4882a593Smuzhiyun 	}
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun 	return desc;
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun 
1839*4882a593Smuzhiyun int
eth_igb_rx_descriptor_status(void * rx_queue,uint16_t offset)1840*4882a593Smuzhiyun eth_igb_rx_descriptor_status(void *rx_queue, uint16_t offset)
1841*4882a593Smuzhiyun {
1842*4882a593Smuzhiyun 	struct igb_rx_queue *rxq = rx_queue;
1843*4882a593Smuzhiyun 	volatile uint32_t *status;
1844*4882a593Smuzhiyun 	uint32_t desc;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	if (unlikely(offset >= rxq->nb_rx_desc))
1847*4882a593Smuzhiyun 		return -EINVAL;
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	if (offset >= rxq->nb_rx_desc - rxq->nb_rx_hold)
1850*4882a593Smuzhiyun 		return RTE_ETH_RX_DESC_UNAVAIL;
1851*4882a593Smuzhiyun 
1852*4882a593Smuzhiyun 	desc = rxq->rx_tail + offset;
1853*4882a593Smuzhiyun 	if (desc >= rxq->nb_rx_desc)
1854*4882a593Smuzhiyun 		desc -= rxq->nb_rx_desc;
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	status = &rxq->rx_ring[desc].wb.upper.status_error;
1857*4882a593Smuzhiyun 	if (*status & rte_cpu_to_le_32(E1000_RXD_STAT_DD))
1858*4882a593Smuzhiyun 		return RTE_ETH_RX_DESC_DONE;
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	return RTE_ETH_RX_DESC_AVAIL;
1861*4882a593Smuzhiyun }
1862*4882a593Smuzhiyun 
1863*4882a593Smuzhiyun int
eth_igb_tx_descriptor_status(void * tx_queue,uint16_t offset)1864*4882a593Smuzhiyun eth_igb_tx_descriptor_status(void *tx_queue, uint16_t offset)
1865*4882a593Smuzhiyun {
1866*4882a593Smuzhiyun 	struct igb_tx_queue *txq = tx_queue;
1867*4882a593Smuzhiyun 	volatile uint32_t *status;
1868*4882a593Smuzhiyun 	uint32_t desc;
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun 	if (unlikely(offset >= txq->nb_tx_desc))
1871*4882a593Smuzhiyun 		return -EINVAL;
1872*4882a593Smuzhiyun 
1873*4882a593Smuzhiyun 	desc = txq->tx_tail + offset;
1874*4882a593Smuzhiyun 	if (desc >= txq->nb_tx_desc)
1875*4882a593Smuzhiyun 		desc -= txq->nb_tx_desc;
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun 	status = &txq->tx_ring[desc].wb.status;
1878*4882a593Smuzhiyun 	if (*status & rte_cpu_to_le_32(E1000_TXD_STAT_DD))
1879*4882a593Smuzhiyun 		return RTE_ETH_TX_DESC_DONE;
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 	return RTE_ETH_TX_DESC_FULL;
1882*4882a593Smuzhiyun }
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun void
igb_dev_clear_queues(struct rte_eth_dev * dev)1885*4882a593Smuzhiyun igb_dev_clear_queues(struct rte_eth_dev *dev)
1886*4882a593Smuzhiyun {
1887*4882a593Smuzhiyun 	uint16_t i;
1888*4882a593Smuzhiyun 	struct igb_tx_queue *txq;
1889*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
1890*4882a593Smuzhiyun 
1891*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1892*4882a593Smuzhiyun 		txq = dev->data->tx_queues[i];
1893*4882a593Smuzhiyun 		if (txq != NULL) {
1894*4882a593Smuzhiyun 			igb_tx_queue_release_mbufs(txq);
1895*4882a593Smuzhiyun 			igb_reset_tx_queue(txq, dev);
1896*4882a593Smuzhiyun 		}
1897*4882a593Smuzhiyun 	}
1898*4882a593Smuzhiyun 
1899*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1900*4882a593Smuzhiyun 		rxq = dev->data->rx_queues[i];
1901*4882a593Smuzhiyun 		if (rxq != NULL) {
1902*4882a593Smuzhiyun 			igb_rx_queue_release_mbufs(rxq);
1903*4882a593Smuzhiyun 			igb_reset_rx_queue(rxq);
1904*4882a593Smuzhiyun 		}
1905*4882a593Smuzhiyun 	}
1906*4882a593Smuzhiyun }
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun void
igb_dev_free_queues(struct rte_eth_dev * dev)1909*4882a593Smuzhiyun igb_dev_free_queues(struct rte_eth_dev *dev)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun 	uint16_t i;
1912*4882a593Smuzhiyun 
1913*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
1914*4882a593Smuzhiyun 		eth_igb_rx_queue_release(dev, i);
1915*4882a593Smuzhiyun 		dev->data->rx_queues[i] = NULL;
1916*4882a593Smuzhiyun 	}
1917*4882a593Smuzhiyun 	dev->data->nb_rx_queues = 0;
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
1920*4882a593Smuzhiyun 		eth_igb_tx_queue_release(dev, i);
1921*4882a593Smuzhiyun 		dev->data->tx_queues[i] = NULL;
1922*4882a593Smuzhiyun 	}
1923*4882a593Smuzhiyun 	dev->data->nb_tx_queues = 0;
1924*4882a593Smuzhiyun }
1925*4882a593Smuzhiyun 
1926*4882a593Smuzhiyun /**
1927*4882a593Smuzhiyun  * Receive Side Scaling (RSS).
1928*4882a593Smuzhiyun  * See section 7.1.1.7 in the following document:
1929*4882a593Smuzhiyun  *     "Intel 82576 GbE Controller Datasheet" - Revision 2.45 October 2009
1930*4882a593Smuzhiyun  *
1931*4882a593Smuzhiyun  * Principles:
1932*4882a593Smuzhiyun  * The source and destination IP addresses of the IP header and the source and
1933*4882a593Smuzhiyun  * destination ports of TCP/UDP headers, if any, of received packets are hashed
1934*4882a593Smuzhiyun  * against a configurable random key to compute a 32-bit RSS hash result.
1935*4882a593Smuzhiyun  * The seven (7) LSBs of the 32-bit hash result are used as an index into a
1936*4882a593Smuzhiyun  * 128-entry redirection table (RETA).  Each entry of the RETA provides a 3-bit
1937*4882a593Smuzhiyun  * RSS output index which is used as the RX queue index where to store the
1938*4882a593Smuzhiyun  * received packets.
1939*4882a593Smuzhiyun  * The following output is supplied in the RX write-back descriptor:
1940*4882a593Smuzhiyun  *     - 32-bit result of the Microsoft RSS hash function,
1941*4882a593Smuzhiyun  *     - 4-bit RSS type field.
1942*4882a593Smuzhiyun  */
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun /*
1945*4882a593Smuzhiyun  * RSS random key supplied in section 7.1.1.7.3 of the Intel 82576 datasheet.
1946*4882a593Smuzhiyun  * Used as the default key.
1947*4882a593Smuzhiyun  */
1948*4882a593Smuzhiyun static uint8_t rss_intel_key[40] = {
1949*4882a593Smuzhiyun 	0x6D, 0x5A, 0x56, 0xDA, 0x25, 0x5B, 0x0E, 0xC2,
1950*4882a593Smuzhiyun 	0x41, 0x67, 0x25, 0x3D, 0x43, 0xA3, 0x8F, 0xB0,
1951*4882a593Smuzhiyun 	0xD0, 0xCA, 0x2B, 0xCB, 0xAE, 0x7B, 0x30, 0xB4,
1952*4882a593Smuzhiyun 	0x77, 0xCB, 0x2D, 0xA3, 0x80, 0x30, 0xF2, 0x0C,
1953*4882a593Smuzhiyun 	0x6A, 0x42, 0xB7, 0x3B, 0xBE, 0xAC, 0x01, 0xFA,
1954*4882a593Smuzhiyun };
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun static void
igb_rss_disable(struct rte_eth_dev * dev)1957*4882a593Smuzhiyun igb_rss_disable(struct rte_eth_dev *dev)
1958*4882a593Smuzhiyun {
1959*4882a593Smuzhiyun 	struct e1000_hw *hw;
1960*4882a593Smuzhiyun 	uint32_t mrqc;
1961*4882a593Smuzhiyun 
1962*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1963*4882a593Smuzhiyun 	mrqc = E1000_READ_REG(hw, E1000_MRQC);
1964*4882a593Smuzhiyun 	mrqc &= ~E1000_MRQC_ENABLE_MASK;
1965*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
1966*4882a593Smuzhiyun }
1967*4882a593Smuzhiyun 
1968*4882a593Smuzhiyun static void
igb_hw_rss_hash_set(struct e1000_hw * hw,struct rte_eth_rss_conf * rss_conf)1969*4882a593Smuzhiyun igb_hw_rss_hash_set(struct e1000_hw *hw, struct rte_eth_rss_conf *rss_conf)
1970*4882a593Smuzhiyun {
1971*4882a593Smuzhiyun 	uint8_t  *hash_key;
1972*4882a593Smuzhiyun 	uint32_t rss_key;
1973*4882a593Smuzhiyun 	uint32_t mrqc;
1974*4882a593Smuzhiyun 	uint64_t rss_hf;
1975*4882a593Smuzhiyun 	uint16_t i;
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun 	hash_key = rss_conf->rss_key;
1978*4882a593Smuzhiyun 	if (hash_key != NULL) {
1979*4882a593Smuzhiyun 		/* Fill in RSS hash key */
1980*4882a593Smuzhiyun 		for (i = 0; i < 10; i++) {
1981*4882a593Smuzhiyun 			rss_key  = hash_key[(i * 4)];
1982*4882a593Smuzhiyun 			rss_key |= hash_key[(i * 4) + 1] << 8;
1983*4882a593Smuzhiyun 			rss_key |= hash_key[(i * 4) + 2] << 16;
1984*4882a593Smuzhiyun 			rss_key |= hash_key[(i * 4) + 3] << 24;
1985*4882a593Smuzhiyun 			E1000_WRITE_REG_ARRAY(hw, E1000_RSSRK(0), i, rss_key);
1986*4882a593Smuzhiyun 		}
1987*4882a593Smuzhiyun 	}
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	/* Set configured hashing protocols in MRQC register */
1990*4882a593Smuzhiyun 	rss_hf = rss_conf->rss_hf;
1991*4882a593Smuzhiyun 	mrqc = E1000_MRQC_ENABLE_RSS_4Q; /* RSS enabled. */
1992*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_IPV4)
1993*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV4;
1994*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_TCP)
1995*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV4_TCP;
1996*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_IPV6)
1997*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV6;
1998*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_IPV6_EX)
1999*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV6_EX;
2000*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_TCP)
2001*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV6_TCP;
2002*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_IPV6_TCP_EX)
2003*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
2004*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV4_UDP)
2005*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
2006*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_NONFRAG_IPV6_UDP)
2007*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
2008*4882a593Smuzhiyun 	if (rss_hf & RTE_ETH_RSS_IPV6_UDP_EX)
2009*4882a593Smuzhiyun 		mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP_EX;
2010*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
2011*4882a593Smuzhiyun }
2012*4882a593Smuzhiyun 
2013*4882a593Smuzhiyun int
eth_igb_rss_hash_update(struct rte_eth_dev * dev,struct rte_eth_rss_conf * rss_conf)2014*4882a593Smuzhiyun eth_igb_rss_hash_update(struct rte_eth_dev *dev,
2015*4882a593Smuzhiyun 			struct rte_eth_rss_conf *rss_conf)
2016*4882a593Smuzhiyun {
2017*4882a593Smuzhiyun 	struct e1000_hw *hw;
2018*4882a593Smuzhiyun 	uint32_t mrqc;
2019*4882a593Smuzhiyun 	uint64_t rss_hf;
2020*4882a593Smuzhiyun 
2021*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2022*4882a593Smuzhiyun 
2023*4882a593Smuzhiyun 	/*
2024*4882a593Smuzhiyun 	 * Before changing anything, first check that the update RSS operation
2025*4882a593Smuzhiyun 	 * does not attempt to disable RSS, if RSS was enabled at
2026*4882a593Smuzhiyun 	 * initialization time, or does not attempt to enable RSS, if RSS was
2027*4882a593Smuzhiyun 	 * disabled at initialization time.
2028*4882a593Smuzhiyun 	 */
2029*4882a593Smuzhiyun 	rss_hf = rss_conf->rss_hf & IGB_RSS_OFFLOAD_ALL;
2030*4882a593Smuzhiyun 	mrqc = E1000_READ_REG(hw, E1000_MRQC);
2031*4882a593Smuzhiyun 	if (!(mrqc & E1000_MRQC_ENABLE_MASK)) { /* RSS disabled */
2032*4882a593Smuzhiyun 		if (rss_hf != 0) /* Enable RSS */
2033*4882a593Smuzhiyun 			return -(EINVAL);
2034*4882a593Smuzhiyun 		return 0; /* Nothing to do */
2035*4882a593Smuzhiyun 	}
2036*4882a593Smuzhiyun 	/* RSS enabled */
2037*4882a593Smuzhiyun 	if (rss_hf == 0) /* Disable RSS */
2038*4882a593Smuzhiyun 		return -(EINVAL);
2039*4882a593Smuzhiyun 	igb_hw_rss_hash_set(hw, rss_conf);
2040*4882a593Smuzhiyun 	return 0;
2041*4882a593Smuzhiyun }
2042*4882a593Smuzhiyun 
eth_igb_rss_hash_conf_get(struct rte_eth_dev * dev,struct rte_eth_rss_conf * rss_conf)2043*4882a593Smuzhiyun int eth_igb_rss_hash_conf_get(struct rte_eth_dev *dev,
2044*4882a593Smuzhiyun 			      struct rte_eth_rss_conf *rss_conf)
2045*4882a593Smuzhiyun {
2046*4882a593Smuzhiyun 	struct e1000_hw *hw;
2047*4882a593Smuzhiyun 	uint8_t *hash_key;
2048*4882a593Smuzhiyun 	uint32_t rss_key;
2049*4882a593Smuzhiyun 	uint32_t mrqc;
2050*4882a593Smuzhiyun 	uint64_t rss_hf;
2051*4882a593Smuzhiyun 	uint16_t i;
2052*4882a593Smuzhiyun 
2053*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2054*4882a593Smuzhiyun 	hash_key = rss_conf->rss_key;
2055*4882a593Smuzhiyun 	if (hash_key != NULL) {
2056*4882a593Smuzhiyun 		/* Return RSS hash key */
2057*4882a593Smuzhiyun 		for (i = 0; i < 10; i++) {
2058*4882a593Smuzhiyun 			rss_key = E1000_READ_REG_ARRAY(hw, E1000_RSSRK(0), i);
2059*4882a593Smuzhiyun 			hash_key[(i * 4)] = rss_key & 0x000000FF;
2060*4882a593Smuzhiyun 			hash_key[(i * 4) + 1] = (rss_key >> 8) & 0x000000FF;
2061*4882a593Smuzhiyun 			hash_key[(i * 4) + 2] = (rss_key >> 16) & 0x000000FF;
2062*4882a593Smuzhiyun 			hash_key[(i * 4) + 3] = (rss_key >> 24) & 0x000000FF;
2063*4882a593Smuzhiyun 		}
2064*4882a593Smuzhiyun 	}
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun 	/* Get RSS functions configured in MRQC register */
2067*4882a593Smuzhiyun 	mrqc = E1000_READ_REG(hw, E1000_MRQC);
2068*4882a593Smuzhiyun 	if ((mrqc & E1000_MRQC_ENABLE_RSS_4Q) == 0) { /* RSS is disabled */
2069*4882a593Smuzhiyun 		rss_conf->rss_hf = 0;
2070*4882a593Smuzhiyun 		return 0;
2071*4882a593Smuzhiyun 	}
2072*4882a593Smuzhiyun 	rss_hf = 0;
2073*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV4)
2074*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_IPV4;
2075*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV4_TCP)
2076*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_TCP;
2077*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV6)
2078*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_IPV6;
2079*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_EX)
2080*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_IPV6_EX;
2081*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_TCP)
2082*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_TCP;
2083*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_TCP_EX)
2084*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_IPV6_TCP_EX;
2085*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV4_UDP)
2086*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV4_UDP;
2087*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_UDP)
2088*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_NONFRAG_IPV6_UDP;
2089*4882a593Smuzhiyun 	if (mrqc & E1000_MRQC_RSS_FIELD_IPV6_UDP_EX)
2090*4882a593Smuzhiyun 		rss_hf |= RTE_ETH_RSS_IPV6_UDP_EX;
2091*4882a593Smuzhiyun 	rss_conf->rss_hf = rss_hf;
2092*4882a593Smuzhiyun 	return 0;
2093*4882a593Smuzhiyun }
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun static void
igb_rss_configure(struct rte_eth_dev * dev)2096*4882a593Smuzhiyun igb_rss_configure(struct rte_eth_dev *dev)
2097*4882a593Smuzhiyun {
2098*4882a593Smuzhiyun 	struct rte_eth_rss_conf rss_conf;
2099*4882a593Smuzhiyun 	struct e1000_hw *hw;
2100*4882a593Smuzhiyun 	uint32_t shift;
2101*4882a593Smuzhiyun 	uint16_t i;
2102*4882a593Smuzhiyun 
2103*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2104*4882a593Smuzhiyun 
2105*4882a593Smuzhiyun 	/* Fill in redirection table. */
2106*4882a593Smuzhiyun 	shift = (hw->mac.type == e1000_82575) ? 6 : 0;
2107*4882a593Smuzhiyun 	for (i = 0; i < 128; i++) {
2108*4882a593Smuzhiyun 		union e1000_reta {
2109*4882a593Smuzhiyun 			uint32_t dword;
2110*4882a593Smuzhiyun 			uint8_t  bytes[4];
2111*4882a593Smuzhiyun 		} reta;
2112*4882a593Smuzhiyun 		uint8_t q_idx;
2113*4882a593Smuzhiyun 
2114*4882a593Smuzhiyun 		q_idx = (uint8_t) ((dev->data->nb_rx_queues > 1) ?
2115*4882a593Smuzhiyun 				   i % dev->data->nb_rx_queues : 0);
2116*4882a593Smuzhiyun 		reta.bytes[i & 3] = (uint8_t) (q_idx << shift);
2117*4882a593Smuzhiyun 		if ((i & 3) == 3)
2118*4882a593Smuzhiyun 			E1000_WRITE_REG(hw, E1000_RETA(i >> 2), reta.dword);
2119*4882a593Smuzhiyun 	}
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	/*
2122*4882a593Smuzhiyun 	 * Configure the RSS key and the RSS protocols used to compute
2123*4882a593Smuzhiyun 	 * the RSS hash of input packets.
2124*4882a593Smuzhiyun 	 */
2125*4882a593Smuzhiyun 	rss_conf = dev->data->dev_conf.rx_adv_conf.rss_conf;
2126*4882a593Smuzhiyun 	if ((rss_conf.rss_hf & IGB_RSS_OFFLOAD_ALL) == 0) {
2127*4882a593Smuzhiyun 		igb_rss_disable(dev);
2128*4882a593Smuzhiyun 		return;
2129*4882a593Smuzhiyun 	}
2130*4882a593Smuzhiyun 	if (rss_conf.rss_key == NULL)
2131*4882a593Smuzhiyun 		rss_conf.rss_key = rss_intel_key; /* Default hash key */
2132*4882a593Smuzhiyun 	igb_hw_rss_hash_set(hw, &rss_conf);
2133*4882a593Smuzhiyun }
2134*4882a593Smuzhiyun 
2135*4882a593Smuzhiyun /*
2136*4882a593Smuzhiyun  * Check if the mac type support VMDq or not.
2137*4882a593Smuzhiyun  * Return 1 if it supports, otherwise, return 0.
2138*4882a593Smuzhiyun  */
2139*4882a593Smuzhiyun static int
igb_is_vmdq_supported(const struct rte_eth_dev * dev)2140*4882a593Smuzhiyun igb_is_vmdq_supported(const struct rte_eth_dev *dev)
2141*4882a593Smuzhiyun {
2142*4882a593Smuzhiyun 	const struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun 	switch (hw->mac.type) {
2145*4882a593Smuzhiyun 	case e1000_82576:
2146*4882a593Smuzhiyun 	case e1000_82580:
2147*4882a593Smuzhiyun 	case e1000_i350:
2148*4882a593Smuzhiyun 		return 1;
2149*4882a593Smuzhiyun 	case e1000_82540:
2150*4882a593Smuzhiyun 	case e1000_82541:
2151*4882a593Smuzhiyun 	case e1000_82542:
2152*4882a593Smuzhiyun 	case e1000_82543:
2153*4882a593Smuzhiyun 	case e1000_82544:
2154*4882a593Smuzhiyun 	case e1000_82545:
2155*4882a593Smuzhiyun 	case e1000_82546:
2156*4882a593Smuzhiyun 	case e1000_82547:
2157*4882a593Smuzhiyun 	case e1000_82571:
2158*4882a593Smuzhiyun 	case e1000_82572:
2159*4882a593Smuzhiyun 	case e1000_82573:
2160*4882a593Smuzhiyun 	case e1000_82574:
2161*4882a593Smuzhiyun 	case e1000_82583:
2162*4882a593Smuzhiyun 	case e1000_i210:
2163*4882a593Smuzhiyun 	case e1000_i211:
2164*4882a593Smuzhiyun 	default:
2165*4882a593Smuzhiyun 		PMD_INIT_LOG(ERR, "Cannot support VMDq feature");
2166*4882a593Smuzhiyun 		return 0;
2167*4882a593Smuzhiyun 	}
2168*4882a593Smuzhiyun }
2169*4882a593Smuzhiyun 
2170*4882a593Smuzhiyun static int
igb_vmdq_rx_hw_configure(struct rte_eth_dev * dev)2171*4882a593Smuzhiyun igb_vmdq_rx_hw_configure(struct rte_eth_dev *dev)
2172*4882a593Smuzhiyun {
2173*4882a593Smuzhiyun 	struct rte_eth_vmdq_rx_conf *cfg;
2174*4882a593Smuzhiyun 	struct e1000_hw *hw;
2175*4882a593Smuzhiyun 	uint32_t mrqc, vt_ctl, vmolr, rctl;
2176*4882a593Smuzhiyun 	int i;
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun 	PMD_INIT_FUNC_TRACE();
2179*4882a593Smuzhiyun 
2180*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2181*4882a593Smuzhiyun 	cfg = &dev->data->dev_conf.rx_adv_conf.vmdq_rx_conf;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 	/* Check if mac type can support VMDq, return value of 0 means NOT support */
2184*4882a593Smuzhiyun 	if (igb_is_vmdq_supported(dev) == 0)
2185*4882a593Smuzhiyun 		return -1;
2186*4882a593Smuzhiyun 
2187*4882a593Smuzhiyun 	igb_rss_disable(dev);
2188*4882a593Smuzhiyun 
2189*4882a593Smuzhiyun 	/* RCTL: eanble VLAN filter */
2190*4882a593Smuzhiyun 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2191*4882a593Smuzhiyun 	rctl |= E1000_RCTL_VFE;
2192*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2193*4882a593Smuzhiyun 
2194*4882a593Smuzhiyun 	/* MRQC: enable vmdq */
2195*4882a593Smuzhiyun 	mrqc = E1000_READ_REG(hw, E1000_MRQC);
2196*4882a593Smuzhiyun 	mrqc |= E1000_MRQC_ENABLE_VMDQ;
2197*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun 	/* VTCTL:  pool selection according to VLAN tag */
2200*4882a593Smuzhiyun 	vt_ctl = E1000_READ_REG(hw, E1000_VT_CTL);
2201*4882a593Smuzhiyun 	if (cfg->enable_default_pool)
2202*4882a593Smuzhiyun 		vt_ctl |= (cfg->default_pool << E1000_VT_CTL_DEFAULT_POOL_SHIFT);
2203*4882a593Smuzhiyun 	vt_ctl |= E1000_VT_CTL_IGNORE_MAC;
2204*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_VT_CTL, vt_ctl);
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun 	for (i = 0; i < E1000_VMOLR_SIZE; i++) {
2207*4882a593Smuzhiyun 		vmolr = E1000_READ_REG(hw, E1000_VMOLR(i));
2208*4882a593Smuzhiyun 		vmolr &= ~(E1000_VMOLR_AUPE | E1000_VMOLR_ROMPE |
2209*4882a593Smuzhiyun 			E1000_VMOLR_ROPE | E1000_VMOLR_BAM |
2210*4882a593Smuzhiyun 			E1000_VMOLR_MPME);
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 		if (cfg->rx_mode & RTE_ETH_VMDQ_ACCEPT_UNTAG)
2213*4882a593Smuzhiyun 			vmolr |= E1000_VMOLR_AUPE;
2214*4882a593Smuzhiyun 		if (cfg->rx_mode & RTE_ETH_VMDQ_ACCEPT_HASH_MC)
2215*4882a593Smuzhiyun 			vmolr |= E1000_VMOLR_ROMPE;
2216*4882a593Smuzhiyun 		if (cfg->rx_mode & RTE_ETH_VMDQ_ACCEPT_HASH_UC)
2217*4882a593Smuzhiyun 			vmolr |= E1000_VMOLR_ROPE;
2218*4882a593Smuzhiyun 		if (cfg->rx_mode & RTE_ETH_VMDQ_ACCEPT_BROADCAST)
2219*4882a593Smuzhiyun 			vmolr |= E1000_VMOLR_BAM;
2220*4882a593Smuzhiyun 		if (cfg->rx_mode & RTE_ETH_VMDQ_ACCEPT_MULTICAST)
2221*4882a593Smuzhiyun 			vmolr |= E1000_VMOLR_MPME;
2222*4882a593Smuzhiyun 
2223*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_VMOLR(i), vmolr);
2224*4882a593Smuzhiyun 	}
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun 	/*
2227*4882a593Smuzhiyun 	 * VMOLR: set STRVLAN as 1 if IGMAC in VTCTL is set as 1
2228*4882a593Smuzhiyun 	 * Both 82576 and 82580 support it
2229*4882a593Smuzhiyun 	 */
2230*4882a593Smuzhiyun 	if (hw->mac.type != e1000_i350) {
2231*4882a593Smuzhiyun 		for (i = 0; i < E1000_VMOLR_SIZE; i++) {
2232*4882a593Smuzhiyun 			vmolr = E1000_READ_REG(hw, E1000_VMOLR(i));
2233*4882a593Smuzhiyun 			vmolr |= E1000_VMOLR_STRVLAN;
2234*4882a593Smuzhiyun 			E1000_WRITE_REG(hw, E1000_VMOLR(i), vmolr);
2235*4882a593Smuzhiyun 		}
2236*4882a593Smuzhiyun 	}
2237*4882a593Smuzhiyun 
2238*4882a593Smuzhiyun 	/* VFTA - enable all vlan filters */
2239*4882a593Smuzhiyun 	for (i = 0; i < IGB_VFTA_SIZE; i++)
2240*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, (E1000_VFTA+(i*4)), UINT32_MAX);
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun 	/* VFRE: 8 pools enabling for rx, both 82576 and i350 support it */
2243*4882a593Smuzhiyun 	if (hw->mac.type != e1000_82580)
2244*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_VFRE, E1000_MBVFICR_VFREQ_MASK);
2245*4882a593Smuzhiyun 
2246*4882a593Smuzhiyun 	/*
2247*4882a593Smuzhiyun 	 * RAH/RAL - allow pools to read specific mac addresses
2248*4882a593Smuzhiyun 	 * In this case, all pools should be able to read from mac addr 0
2249*4882a593Smuzhiyun 	 */
2250*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_RAH(0), (E1000_RAH_AV | UINT16_MAX));
2251*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_RAL(0), UINT32_MAX);
2252*4882a593Smuzhiyun 
2253*4882a593Smuzhiyun 	/* VLVF: set up filters for vlan tags as configured */
2254*4882a593Smuzhiyun 	for (i = 0; i < cfg->nb_pool_maps; i++) {
2255*4882a593Smuzhiyun 		/* set vlan id in VF register and set the valid bit */
2256*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_VLVF(i), (E1000_VLVF_VLANID_ENABLE |
2257*4882a593Smuzhiyun 			(cfg->pool_map[i].vlan_id & RTE_ETH_VLAN_ID_MAX) |
2258*4882a593Smuzhiyun 			((cfg->pool_map[i].pools << E1000_VLVF_POOLSEL_SHIFT) &
2259*4882a593Smuzhiyun 			E1000_VLVF_POOLSEL_MASK)));
2260*4882a593Smuzhiyun 	}
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun 	E1000_WRITE_FLUSH(hw);
2263*4882a593Smuzhiyun 
2264*4882a593Smuzhiyun 	return 0;
2265*4882a593Smuzhiyun }
2266*4882a593Smuzhiyun 
2267*4882a593Smuzhiyun 
2268*4882a593Smuzhiyun /*********************************************************************
2269*4882a593Smuzhiyun  *
2270*4882a593Smuzhiyun  *  Enable receive unit.
2271*4882a593Smuzhiyun  *
2272*4882a593Smuzhiyun  **********************************************************************/
2273*4882a593Smuzhiyun 
2274*4882a593Smuzhiyun static int
igb_alloc_rx_queue_mbufs(struct igb_rx_queue * rxq)2275*4882a593Smuzhiyun igb_alloc_rx_queue_mbufs(struct igb_rx_queue *rxq)
2276*4882a593Smuzhiyun {
2277*4882a593Smuzhiyun 	struct igb_rx_entry *rxe = rxq->sw_ring;
2278*4882a593Smuzhiyun 	uint64_t dma_addr;
2279*4882a593Smuzhiyun 	unsigned i;
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun 	/* Initialize software ring entries. */
2282*4882a593Smuzhiyun 	for (i = 0; i < rxq->nb_rx_desc; i++) {
2283*4882a593Smuzhiyun 		volatile union e1000_adv_rx_desc *rxd;
2284*4882a593Smuzhiyun 		struct rte_mbuf *mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun 		if (mbuf == NULL) {
2287*4882a593Smuzhiyun 			PMD_INIT_LOG(ERR, "RX mbuf alloc failed "
2288*4882a593Smuzhiyun 				     "queue_id=%hu", rxq->queue_id);
2289*4882a593Smuzhiyun 			return -ENOMEM;
2290*4882a593Smuzhiyun 		}
2291*4882a593Smuzhiyun 		dma_addr =
2292*4882a593Smuzhiyun 			rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
2293*4882a593Smuzhiyun 		rxd = &rxq->rx_ring[i];
2294*4882a593Smuzhiyun 		rxd->read.hdr_addr = 0;
2295*4882a593Smuzhiyun 		rxd->read.pkt_addr = dma_addr;
2296*4882a593Smuzhiyun 		rxe[i].mbuf = mbuf;
2297*4882a593Smuzhiyun 	}
2298*4882a593Smuzhiyun 
2299*4882a593Smuzhiyun 	return 0;
2300*4882a593Smuzhiyun }
2301*4882a593Smuzhiyun 
2302*4882a593Smuzhiyun #define E1000_MRQC_DEF_Q_SHIFT               (3)
2303*4882a593Smuzhiyun static int
igb_dev_mq_rx_configure(struct rte_eth_dev * dev)2304*4882a593Smuzhiyun igb_dev_mq_rx_configure(struct rte_eth_dev *dev)
2305*4882a593Smuzhiyun {
2306*4882a593Smuzhiyun 	struct e1000_hw *hw =
2307*4882a593Smuzhiyun 		E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2308*4882a593Smuzhiyun 	uint32_t mrqc;
2309*4882a593Smuzhiyun 
2310*4882a593Smuzhiyun 	if (RTE_ETH_DEV_SRIOV(dev).active == RTE_ETH_8_POOLS) {
2311*4882a593Smuzhiyun 		/*
2312*4882a593Smuzhiyun 		 * SRIOV active scheme
2313*4882a593Smuzhiyun 		 * FIXME if support RSS together with VMDq & SRIOV
2314*4882a593Smuzhiyun 		 */
2315*4882a593Smuzhiyun 		mrqc = E1000_MRQC_ENABLE_VMDQ;
2316*4882a593Smuzhiyun 		/* 011b Def_Q ignore, according to VT_CTL.DEF_PL */
2317*4882a593Smuzhiyun 		mrqc |= 0x3 << E1000_MRQC_DEF_Q_SHIFT;
2318*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_MRQC, mrqc);
2319*4882a593Smuzhiyun 	} else if(RTE_ETH_DEV_SRIOV(dev).active == 0) {
2320*4882a593Smuzhiyun 		/*
2321*4882a593Smuzhiyun 		 * SRIOV inactive scheme
2322*4882a593Smuzhiyun 		 */
2323*4882a593Smuzhiyun 		switch (dev->data->dev_conf.rxmode.mq_mode) {
2324*4882a593Smuzhiyun 			case RTE_ETH_MQ_RX_RSS:
2325*4882a593Smuzhiyun 				igb_rss_configure(dev);
2326*4882a593Smuzhiyun 				break;
2327*4882a593Smuzhiyun 			case RTE_ETH_MQ_RX_VMDQ_ONLY:
2328*4882a593Smuzhiyun 				/*Configure general VMDQ only RX parameters*/
2329*4882a593Smuzhiyun 				igb_vmdq_rx_hw_configure(dev);
2330*4882a593Smuzhiyun 				break;
2331*4882a593Smuzhiyun 			case RTE_ETH_MQ_RX_NONE:
2332*4882a593Smuzhiyun 				/* if mq_mode is none, disable rss mode.*/
2333*4882a593Smuzhiyun 			default:
2334*4882a593Smuzhiyun 				igb_rss_disable(dev);
2335*4882a593Smuzhiyun 				break;
2336*4882a593Smuzhiyun 		}
2337*4882a593Smuzhiyun 	}
2338*4882a593Smuzhiyun 
2339*4882a593Smuzhiyun 	return 0;
2340*4882a593Smuzhiyun }
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun int
eth_igb_rx_init(struct rte_eth_dev * dev)2343*4882a593Smuzhiyun eth_igb_rx_init(struct rte_eth_dev *dev)
2344*4882a593Smuzhiyun {
2345*4882a593Smuzhiyun 	struct rte_eth_rxmode *rxmode;
2346*4882a593Smuzhiyun 	struct e1000_hw     *hw;
2347*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
2348*4882a593Smuzhiyun 	uint32_t rctl;
2349*4882a593Smuzhiyun 	uint32_t rxcsum;
2350*4882a593Smuzhiyun 	uint32_t srrctl;
2351*4882a593Smuzhiyun 	uint16_t buf_size;
2352*4882a593Smuzhiyun 	uint16_t rctl_bsize;
2353*4882a593Smuzhiyun 	uint32_t max_len;
2354*4882a593Smuzhiyun 	uint16_t i;
2355*4882a593Smuzhiyun 	int ret;
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2358*4882a593Smuzhiyun 	srrctl = 0;
2359*4882a593Smuzhiyun 
2360*4882a593Smuzhiyun 	/*
2361*4882a593Smuzhiyun 	 * Make sure receives are disabled while setting
2362*4882a593Smuzhiyun 	 * up the descriptor ring.
2363*4882a593Smuzhiyun 	 */
2364*4882a593Smuzhiyun 	rctl = E1000_READ_REG(hw, E1000_RCTL);
2365*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_RCTL, rctl & ~E1000_RCTL_EN);
2366*4882a593Smuzhiyun 
2367*4882a593Smuzhiyun 	rxmode = &dev->data->dev_conf.rxmode;
2368*4882a593Smuzhiyun 
2369*4882a593Smuzhiyun 	/*
2370*4882a593Smuzhiyun 	 * Configure support of jumbo frames, if any.
2371*4882a593Smuzhiyun 	 */
2372*4882a593Smuzhiyun 	max_len = dev->data->mtu + E1000_ETH_OVERHEAD;
2373*4882a593Smuzhiyun 	if (dev->data->mtu > RTE_ETHER_MTU) {
2374*4882a593Smuzhiyun 		rctl |= E1000_RCTL_LPE;
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 		/*
2377*4882a593Smuzhiyun 		 * Set maximum packet length by default, and might be updated
2378*4882a593Smuzhiyun 		 * together with enabling/disabling dual VLAN.
2379*4882a593Smuzhiyun 		 */
2380*4882a593Smuzhiyun 		if (rxmode->offloads & RTE_ETH_RX_OFFLOAD_VLAN_EXTEND)
2381*4882a593Smuzhiyun 			max_len += VLAN_TAG_SIZE;
2382*4882a593Smuzhiyun 
2383*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RLPML, max_len);
2384*4882a593Smuzhiyun 	} else
2385*4882a593Smuzhiyun 		rctl &= ~E1000_RCTL_LPE;
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 	/* Configure and enable each RX queue. */
2388*4882a593Smuzhiyun 	rctl_bsize = 0;
2389*4882a593Smuzhiyun 	dev->rx_pkt_burst = eth_igb_recv_pkts;
2390*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2391*4882a593Smuzhiyun 		uint64_t bus_addr;
2392*4882a593Smuzhiyun 		uint32_t rxdctl;
2393*4882a593Smuzhiyun 
2394*4882a593Smuzhiyun 		rxq = dev->data->rx_queues[i];
2395*4882a593Smuzhiyun 
2396*4882a593Smuzhiyun 		rxq->flags = 0;
2397*4882a593Smuzhiyun 		/*
2398*4882a593Smuzhiyun 		 * i350 and i354 vlan packets have vlan tags byte swapped.
2399*4882a593Smuzhiyun 		 */
2400*4882a593Smuzhiyun 		if (hw->mac.type == e1000_i350 || hw->mac.type == e1000_i354) {
2401*4882a593Smuzhiyun 			rxq->flags |= IGB_RXQ_FLAG_LB_BSWAP_VLAN;
2402*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "IGB rx vlan bswap required");
2403*4882a593Smuzhiyun 		} else {
2404*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "IGB rx vlan bswap not required");
2405*4882a593Smuzhiyun 		}
2406*4882a593Smuzhiyun 
2407*4882a593Smuzhiyun 		/* Allocate buffers for descriptor rings and set up queue */
2408*4882a593Smuzhiyun 		ret = igb_alloc_rx_queue_mbufs(rxq);
2409*4882a593Smuzhiyun 		if (ret)
2410*4882a593Smuzhiyun 			return ret;
2411*4882a593Smuzhiyun 
2412*4882a593Smuzhiyun 		/*
2413*4882a593Smuzhiyun 		 * Reset crc_len in case it was changed after queue setup by a
2414*4882a593Smuzhiyun 		 *  call to configure
2415*4882a593Smuzhiyun 		 */
2416*4882a593Smuzhiyun 		if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC)
2417*4882a593Smuzhiyun 			rxq->crc_len = RTE_ETHER_CRC_LEN;
2418*4882a593Smuzhiyun 		else
2419*4882a593Smuzhiyun 			rxq->crc_len = 0;
2420*4882a593Smuzhiyun 
2421*4882a593Smuzhiyun 		bus_addr = rxq->rx_ring_phys_addr;
2422*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDLEN(rxq->reg_idx),
2423*4882a593Smuzhiyun 				rxq->nb_rx_desc *
2424*4882a593Smuzhiyun 				sizeof(union e1000_adv_rx_desc));
2425*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDBAH(rxq->reg_idx),
2426*4882a593Smuzhiyun 				(uint32_t)(bus_addr >> 32));
2427*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDBAL(rxq->reg_idx), (uint32_t)bus_addr);
2428*4882a593Smuzhiyun 
2429*4882a593Smuzhiyun 		srrctl = E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
2430*4882a593Smuzhiyun 
2431*4882a593Smuzhiyun 		/*
2432*4882a593Smuzhiyun 		 * Configure RX buffer size.
2433*4882a593Smuzhiyun 		 */
2434*4882a593Smuzhiyun 		buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
2435*4882a593Smuzhiyun 			RTE_PKTMBUF_HEADROOM);
2436*4882a593Smuzhiyun 		if (buf_size >= 1024) {
2437*4882a593Smuzhiyun 			/*
2438*4882a593Smuzhiyun 			 * Configure the BSIZEPACKET field of the SRRCTL
2439*4882a593Smuzhiyun 			 * register of the queue.
2440*4882a593Smuzhiyun 			 * Value is in 1 KB resolution, from 1 KB to 127 KB.
2441*4882a593Smuzhiyun 			 * If this field is equal to 0b, then RCTL.BSIZE
2442*4882a593Smuzhiyun 			 * determines the RX packet buffer size.
2443*4882a593Smuzhiyun 			 */
2444*4882a593Smuzhiyun 			srrctl |= ((buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) &
2445*4882a593Smuzhiyun 				   E1000_SRRCTL_BSIZEPKT_MASK);
2446*4882a593Smuzhiyun 			buf_size = (uint16_t) ((srrctl &
2447*4882a593Smuzhiyun 						E1000_SRRCTL_BSIZEPKT_MASK) <<
2448*4882a593Smuzhiyun 					       E1000_SRRCTL_BSIZEPKT_SHIFT);
2449*4882a593Smuzhiyun 
2450*4882a593Smuzhiyun 			/* It adds dual VLAN length for supporting dual VLAN */
2451*4882a593Smuzhiyun 			if ((max_len + 2 * VLAN_TAG_SIZE) > buf_size) {
2452*4882a593Smuzhiyun 				if (!dev->data->scattered_rx)
2453*4882a593Smuzhiyun 					PMD_INIT_LOG(DEBUG,
2454*4882a593Smuzhiyun 						     "forcing scatter mode");
2455*4882a593Smuzhiyun 				dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
2456*4882a593Smuzhiyun 				dev->data->scattered_rx = 1;
2457*4882a593Smuzhiyun 			}
2458*4882a593Smuzhiyun 		} else {
2459*4882a593Smuzhiyun 			/*
2460*4882a593Smuzhiyun 			 * Use BSIZE field of the device RCTL register.
2461*4882a593Smuzhiyun 			 */
2462*4882a593Smuzhiyun 			if ((rctl_bsize == 0) || (rctl_bsize > buf_size))
2463*4882a593Smuzhiyun 				rctl_bsize = buf_size;
2464*4882a593Smuzhiyun 			if (!dev->data->scattered_rx)
2465*4882a593Smuzhiyun 				PMD_INIT_LOG(DEBUG, "forcing scatter mode");
2466*4882a593Smuzhiyun 			dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
2467*4882a593Smuzhiyun 			dev->data->scattered_rx = 1;
2468*4882a593Smuzhiyun 		}
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 		/* Set if packets are dropped when no descriptors available */
2471*4882a593Smuzhiyun 		if (rxq->drop_en)
2472*4882a593Smuzhiyun 			srrctl |= E1000_SRRCTL_DROP_EN;
2473*4882a593Smuzhiyun 
2474*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_SRRCTL(rxq->reg_idx), srrctl);
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 		/* Enable this RX queue. */
2477*4882a593Smuzhiyun 		rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(rxq->reg_idx));
2478*4882a593Smuzhiyun 		rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
2479*4882a593Smuzhiyun 		rxdctl &= 0xFFF00000;
2480*4882a593Smuzhiyun 		rxdctl |= (rxq->pthresh & 0x1F);
2481*4882a593Smuzhiyun 		rxdctl |= ((rxq->hthresh & 0x1F) << 8);
2482*4882a593Smuzhiyun 		rxdctl |= ((rxq->wthresh & 0x1F) << 16);
2483*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RXDCTL(rxq->reg_idx), rxdctl);
2484*4882a593Smuzhiyun 	}
2485*4882a593Smuzhiyun 
2486*4882a593Smuzhiyun 	if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_SCATTER) {
2487*4882a593Smuzhiyun 		if (!dev->data->scattered_rx)
2488*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "forcing scatter mode");
2489*4882a593Smuzhiyun 		dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
2490*4882a593Smuzhiyun 		dev->data->scattered_rx = 1;
2491*4882a593Smuzhiyun 	}
2492*4882a593Smuzhiyun 
2493*4882a593Smuzhiyun 	/*
2494*4882a593Smuzhiyun 	 * Setup BSIZE field of RCTL register, if needed.
2495*4882a593Smuzhiyun 	 * Buffer sizes >= 1024 are not [supposed to be] setup in the RCTL
2496*4882a593Smuzhiyun 	 * register, since the code above configures the SRRCTL register of
2497*4882a593Smuzhiyun 	 * the RX queue in such a case.
2498*4882a593Smuzhiyun 	 * All configurable sizes are:
2499*4882a593Smuzhiyun 	 * 16384: rctl |= (E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX);
2500*4882a593Smuzhiyun 	 *  8192: rctl |= (E1000_RCTL_SZ_8192  | E1000_RCTL_BSEX);
2501*4882a593Smuzhiyun 	 *  4096: rctl |= (E1000_RCTL_SZ_4096  | E1000_RCTL_BSEX);
2502*4882a593Smuzhiyun 	 *  2048: rctl |= E1000_RCTL_SZ_2048;
2503*4882a593Smuzhiyun 	 *  1024: rctl |= E1000_RCTL_SZ_1024;
2504*4882a593Smuzhiyun 	 *   512: rctl |= E1000_RCTL_SZ_512;
2505*4882a593Smuzhiyun 	 *   256: rctl |= E1000_RCTL_SZ_256;
2506*4882a593Smuzhiyun 	 */
2507*4882a593Smuzhiyun 	if (rctl_bsize > 0) {
2508*4882a593Smuzhiyun 		if (rctl_bsize >= 512) /* 512 <= buf_size < 1024 - use 512 */
2509*4882a593Smuzhiyun 			rctl |= E1000_RCTL_SZ_512;
2510*4882a593Smuzhiyun 		else /* 256 <= buf_size < 512 - use 256 */
2511*4882a593Smuzhiyun 			rctl |= E1000_RCTL_SZ_256;
2512*4882a593Smuzhiyun 	}
2513*4882a593Smuzhiyun 
2514*4882a593Smuzhiyun 	/*
2515*4882a593Smuzhiyun 	 * Configure RSS if device configured with multiple RX queues.
2516*4882a593Smuzhiyun 	 */
2517*4882a593Smuzhiyun 	igb_dev_mq_rx_configure(dev);
2518*4882a593Smuzhiyun 
2519*4882a593Smuzhiyun 	/* Update the rctl since igb_dev_mq_rx_configure may change its value */
2520*4882a593Smuzhiyun 	rctl |= E1000_READ_REG(hw, E1000_RCTL);
2521*4882a593Smuzhiyun 
2522*4882a593Smuzhiyun 	/*
2523*4882a593Smuzhiyun 	 * Setup the Checksum Register.
2524*4882a593Smuzhiyun 	 * Receive Full-Packet Checksum Offload is mutually exclusive with RSS.
2525*4882a593Smuzhiyun 	 */
2526*4882a593Smuzhiyun 	rxcsum = E1000_READ_REG(hw, E1000_RXCSUM);
2527*4882a593Smuzhiyun 	rxcsum |= E1000_RXCSUM_PCSD;
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	/* Enable both L3/L4 rx checksum offload */
2530*4882a593Smuzhiyun 	if (rxmode->offloads & RTE_ETH_RX_OFFLOAD_IPV4_CKSUM)
2531*4882a593Smuzhiyun 		rxcsum |= E1000_RXCSUM_IPOFL;
2532*4882a593Smuzhiyun 	else
2533*4882a593Smuzhiyun 		rxcsum &= ~E1000_RXCSUM_IPOFL;
2534*4882a593Smuzhiyun 	if (rxmode->offloads &
2535*4882a593Smuzhiyun 		(RTE_ETH_RX_OFFLOAD_TCP_CKSUM | RTE_ETH_RX_OFFLOAD_UDP_CKSUM))
2536*4882a593Smuzhiyun 		rxcsum |= E1000_RXCSUM_TUOFL;
2537*4882a593Smuzhiyun 	else
2538*4882a593Smuzhiyun 		rxcsum &= ~E1000_RXCSUM_TUOFL;
2539*4882a593Smuzhiyun 	if (rxmode->offloads & RTE_ETH_RX_OFFLOAD_CHECKSUM)
2540*4882a593Smuzhiyun 		rxcsum |= E1000_RXCSUM_CRCOFL;
2541*4882a593Smuzhiyun 	else
2542*4882a593Smuzhiyun 		rxcsum &= ~E1000_RXCSUM_CRCOFL;
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_RXCSUM, rxcsum);
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	/* Setup the Receive Control Register. */
2547*4882a593Smuzhiyun 	if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_KEEP_CRC) {
2548*4882a593Smuzhiyun 		rctl &= ~E1000_RCTL_SECRC; /* Do not Strip Ethernet CRC. */
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 		/* clear STRCRC bit in all queues */
2551*4882a593Smuzhiyun 		if (hw->mac.type == e1000_i350 ||
2552*4882a593Smuzhiyun 		    hw->mac.type == e1000_i210 ||
2553*4882a593Smuzhiyun 		    hw->mac.type == e1000_i211 ||
2554*4882a593Smuzhiyun 		    hw->mac.type == e1000_i354) {
2555*4882a593Smuzhiyun 			for (i = 0; i < dev->data->nb_rx_queues; i++) {
2556*4882a593Smuzhiyun 				rxq = dev->data->rx_queues[i];
2557*4882a593Smuzhiyun 				uint32_t dvmolr = E1000_READ_REG(hw,
2558*4882a593Smuzhiyun 					E1000_DVMOLR(rxq->reg_idx));
2559*4882a593Smuzhiyun 				dvmolr &= ~E1000_DVMOLR_STRCRC;
2560*4882a593Smuzhiyun 				E1000_WRITE_REG(hw, E1000_DVMOLR(rxq->reg_idx), dvmolr);
2561*4882a593Smuzhiyun 			}
2562*4882a593Smuzhiyun 		}
2563*4882a593Smuzhiyun 	} else {
2564*4882a593Smuzhiyun 		rctl |= E1000_RCTL_SECRC; /* Strip Ethernet CRC. */
2565*4882a593Smuzhiyun 
2566*4882a593Smuzhiyun 		/* set STRCRC bit in all queues */
2567*4882a593Smuzhiyun 		if (hw->mac.type == e1000_i350 ||
2568*4882a593Smuzhiyun 		    hw->mac.type == e1000_i210 ||
2569*4882a593Smuzhiyun 		    hw->mac.type == e1000_i211 ||
2570*4882a593Smuzhiyun 		    hw->mac.type == e1000_i354) {
2571*4882a593Smuzhiyun 			for (i = 0; i < dev->data->nb_rx_queues; i++) {
2572*4882a593Smuzhiyun 				rxq = dev->data->rx_queues[i];
2573*4882a593Smuzhiyun 				uint32_t dvmolr = E1000_READ_REG(hw,
2574*4882a593Smuzhiyun 					E1000_DVMOLR(rxq->reg_idx));
2575*4882a593Smuzhiyun 				dvmolr |= E1000_DVMOLR_STRCRC;
2576*4882a593Smuzhiyun 				E1000_WRITE_REG(hw, E1000_DVMOLR(rxq->reg_idx), dvmolr);
2577*4882a593Smuzhiyun 			}
2578*4882a593Smuzhiyun 		}
2579*4882a593Smuzhiyun 	}
2580*4882a593Smuzhiyun 
2581*4882a593Smuzhiyun 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
2582*4882a593Smuzhiyun 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
2583*4882a593Smuzhiyun 		E1000_RCTL_RDMTS_HALF |
2584*4882a593Smuzhiyun 		(hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
2585*4882a593Smuzhiyun 
2586*4882a593Smuzhiyun 	/* Make sure VLAN Filters are off. */
2587*4882a593Smuzhiyun 	if (dev->data->dev_conf.rxmode.mq_mode != RTE_ETH_MQ_RX_VMDQ_ONLY)
2588*4882a593Smuzhiyun 		rctl &= ~E1000_RCTL_VFE;
2589*4882a593Smuzhiyun 	/* Don't store bad packets. */
2590*4882a593Smuzhiyun 	rctl &= ~E1000_RCTL_SBP;
2591*4882a593Smuzhiyun 
2592*4882a593Smuzhiyun 	/* Enable Receives. */
2593*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_RCTL, rctl);
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	/*
2596*4882a593Smuzhiyun 	 * Setup the HW Rx Head and Tail Descriptor Pointers.
2597*4882a593Smuzhiyun 	 * This needs to be done after enable.
2598*4882a593Smuzhiyun 	 */
2599*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2600*4882a593Smuzhiyun 		rxq = dev->data->rx_queues[i];
2601*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDH(rxq->reg_idx), 0);
2602*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDT(rxq->reg_idx), rxq->nb_rx_desc - 1);
2603*4882a593Smuzhiyun 	}
2604*4882a593Smuzhiyun 
2605*4882a593Smuzhiyun 	return 0;
2606*4882a593Smuzhiyun }
2607*4882a593Smuzhiyun 
2608*4882a593Smuzhiyun /*********************************************************************
2609*4882a593Smuzhiyun  *
2610*4882a593Smuzhiyun  *  Enable transmit unit.
2611*4882a593Smuzhiyun  *
2612*4882a593Smuzhiyun  **********************************************************************/
2613*4882a593Smuzhiyun void
eth_igb_tx_init(struct rte_eth_dev * dev)2614*4882a593Smuzhiyun eth_igb_tx_init(struct rte_eth_dev *dev)
2615*4882a593Smuzhiyun {
2616*4882a593Smuzhiyun 	struct e1000_hw     *hw;
2617*4882a593Smuzhiyun 	struct igb_tx_queue *txq;
2618*4882a593Smuzhiyun 	uint32_t tctl;
2619*4882a593Smuzhiyun 	uint32_t txdctl;
2620*4882a593Smuzhiyun 	uint16_t i;
2621*4882a593Smuzhiyun 
2622*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2623*4882a593Smuzhiyun 
2624*4882a593Smuzhiyun 	/* Setup the Base and Length of the Tx Descriptor Rings. */
2625*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2626*4882a593Smuzhiyun 		uint64_t bus_addr;
2627*4882a593Smuzhiyun 		txq = dev->data->tx_queues[i];
2628*4882a593Smuzhiyun 		bus_addr = txq->tx_ring_phys_addr;
2629*4882a593Smuzhiyun 
2630*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDLEN(txq->reg_idx),
2631*4882a593Smuzhiyun 				txq->nb_tx_desc *
2632*4882a593Smuzhiyun 				sizeof(union e1000_adv_tx_desc));
2633*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDBAH(txq->reg_idx),
2634*4882a593Smuzhiyun 				(uint32_t)(bus_addr >> 32));
2635*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDBAL(txq->reg_idx), (uint32_t)bus_addr);
2636*4882a593Smuzhiyun 
2637*4882a593Smuzhiyun 		/* Setup the HW Tx Head and Tail descriptor pointers. */
2638*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDT(txq->reg_idx), 0);
2639*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDH(txq->reg_idx), 0);
2640*4882a593Smuzhiyun 
2641*4882a593Smuzhiyun 		/* Setup Transmit threshold registers. */
2642*4882a593Smuzhiyun 		txdctl = E1000_READ_REG(hw, E1000_TXDCTL(txq->reg_idx));
2643*4882a593Smuzhiyun 		txdctl |= txq->pthresh & 0x1F;
2644*4882a593Smuzhiyun 		txdctl |= ((txq->hthresh & 0x1F) << 8);
2645*4882a593Smuzhiyun 		txdctl |= ((txq->wthresh & 0x1F) << 16);
2646*4882a593Smuzhiyun 		txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2647*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TXDCTL(txq->reg_idx), txdctl);
2648*4882a593Smuzhiyun 	}
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun 	/* Program the Transmit Control Register. */
2651*4882a593Smuzhiyun 	tctl = E1000_READ_REG(hw, E1000_TCTL);
2652*4882a593Smuzhiyun 	tctl &= ~E1000_TCTL_CT;
2653*4882a593Smuzhiyun 	tctl |= (E1000_TCTL_PSP | E1000_TCTL_RTLC | E1000_TCTL_EN |
2654*4882a593Smuzhiyun 		 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT));
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	e1000_config_collision_dist(hw);
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun 	/* This write will effectively turn on the transmit unit. */
2659*4882a593Smuzhiyun 	E1000_WRITE_REG(hw, E1000_TCTL, tctl);
2660*4882a593Smuzhiyun }
2661*4882a593Smuzhiyun 
2662*4882a593Smuzhiyun /*********************************************************************
2663*4882a593Smuzhiyun  *
2664*4882a593Smuzhiyun  *  Enable VF receive unit.
2665*4882a593Smuzhiyun  *
2666*4882a593Smuzhiyun  **********************************************************************/
2667*4882a593Smuzhiyun int
eth_igbvf_rx_init(struct rte_eth_dev * dev)2668*4882a593Smuzhiyun eth_igbvf_rx_init(struct rte_eth_dev *dev)
2669*4882a593Smuzhiyun {
2670*4882a593Smuzhiyun 	struct e1000_hw     *hw;
2671*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
2672*4882a593Smuzhiyun 	uint32_t srrctl;
2673*4882a593Smuzhiyun 	uint16_t buf_size;
2674*4882a593Smuzhiyun 	uint16_t rctl_bsize;
2675*4882a593Smuzhiyun 	uint32_t max_len;
2676*4882a593Smuzhiyun 	uint16_t i;
2677*4882a593Smuzhiyun 	int ret;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2680*4882a593Smuzhiyun 
2681*4882a593Smuzhiyun 	/* setup MTU */
2682*4882a593Smuzhiyun 	max_len = dev->data->mtu + E1000_ETH_OVERHEAD;
2683*4882a593Smuzhiyun 	e1000_rlpml_set_vf(hw, (uint16_t)(max_len + VLAN_TAG_SIZE));
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun 	/* Configure and enable each RX queue. */
2686*4882a593Smuzhiyun 	rctl_bsize = 0;
2687*4882a593Smuzhiyun 	dev->rx_pkt_burst = eth_igb_recv_pkts;
2688*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2689*4882a593Smuzhiyun 		uint64_t bus_addr;
2690*4882a593Smuzhiyun 		uint32_t rxdctl;
2691*4882a593Smuzhiyun 
2692*4882a593Smuzhiyun 		rxq = dev->data->rx_queues[i];
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun 		rxq->flags = 0;
2695*4882a593Smuzhiyun 		/*
2696*4882a593Smuzhiyun 		 * i350VF LB vlan packets have vlan tags byte swapped.
2697*4882a593Smuzhiyun 		 */
2698*4882a593Smuzhiyun 		if (hw->mac.type == e1000_vfadapt_i350) {
2699*4882a593Smuzhiyun 			rxq->flags |= IGB_RXQ_FLAG_LB_BSWAP_VLAN;
2700*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "IGB rx vlan bswap required");
2701*4882a593Smuzhiyun 		} else {
2702*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "IGB rx vlan bswap not required");
2703*4882a593Smuzhiyun 		}
2704*4882a593Smuzhiyun 
2705*4882a593Smuzhiyun 		/* Allocate buffers for descriptor rings and set up queue */
2706*4882a593Smuzhiyun 		ret = igb_alloc_rx_queue_mbufs(rxq);
2707*4882a593Smuzhiyun 		if (ret)
2708*4882a593Smuzhiyun 			return ret;
2709*4882a593Smuzhiyun 
2710*4882a593Smuzhiyun 		bus_addr = rxq->rx_ring_phys_addr;
2711*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDLEN(i),
2712*4882a593Smuzhiyun 				rxq->nb_rx_desc *
2713*4882a593Smuzhiyun 				sizeof(union e1000_adv_rx_desc));
2714*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDBAH(i),
2715*4882a593Smuzhiyun 				(uint32_t)(bus_addr >> 32));
2716*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDBAL(i), (uint32_t)bus_addr);
2717*4882a593Smuzhiyun 
2718*4882a593Smuzhiyun 		srrctl = E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
2719*4882a593Smuzhiyun 
2720*4882a593Smuzhiyun 		/*
2721*4882a593Smuzhiyun 		 * Configure RX buffer size.
2722*4882a593Smuzhiyun 		 */
2723*4882a593Smuzhiyun 		buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
2724*4882a593Smuzhiyun 			RTE_PKTMBUF_HEADROOM);
2725*4882a593Smuzhiyun 		if (buf_size >= 1024) {
2726*4882a593Smuzhiyun 			/*
2727*4882a593Smuzhiyun 			 * Configure the BSIZEPACKET field of the SRRCTL
2728*4882a593Smuzhiyun 			 * register of the queue.
2729*4882a593Smuzhiyun 			 * Value is in 1 KB resolution, from 1 KB to 127 KB.
2730*4882a593Smuzhiyun 			 * If this field is equal to 0b, then RCTL.BSIZE
2731*4882a593Smuzhiyun 			 * determines the RX packet buffer size.
2732*4882a593Smuzhiyun 			 */
2733*4882a593Smuzhiyun 			srrctl |= ((buf_size >> E1000_SRRCTL_BSIZEPKT_SHIFT) &
2734*4882a593Smuzhiyun 				   E1000_SRRCTL_BSIZEPKT_MASK);
2735*4882a593Smuzhiyun 			buf_size = (uint16_t) ((srrctl &
2736*4882a593Smuzhiyun 						E1000_SRRCTL_BSIZEPKT_MASK) <<
2737*4882a593Smuzhiyun 					       E1000_SRRCTL_BSIZEPKT_SHIFT);
2738*4882a593Smuzhiyun 
2739*4882a593Smuzhiyun 			/* It adds dual VLAN length for supporting dual VLAN */
2740*4882a593Smuzhiyun 			if ((max_len + 2 * VLAN_TAG_SIZE) > buf_size) {
2741*4882a593Smuzhiyun 				if (!dev->data->scattered_rx)
2742*4882a593Smuzhiyun 					PMD_INIT_LOG(DEBUG,
2743*4882a593Smuzhiyun 						     "forcing scatter mode");
2744*4882a593Smuzhiyun 				dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
2745*4882a593Smuzhiyun 				dev->data->scattered_rx = 1;
2746*4882a593Smuzhiyun 			}
2747*4882a593Smuzhiyun 		} else {
2748*4882a593Smuzhiyun 			/*
2749*4882a593Smuzhiyun 			 * Use BSIZE field of the device RCTL register.
2750*4882a593Smuzhiyun 			 */
2751*4882a593Smuzhiyun 			if ((rctl_bsize == 0) || (rctl_bsize > buf_size))
2752*4882a593Smuzhiyun 				rctl_bsize = buf_size;
2753*4882a593Smuzhiyun 			if (!dev->data->scattered_rx)
2754*4882a593Smuzhiyun 				PMD_INIT_LOG(DEBUG, "forcing scatter mode");
2755*4882a593Smuzhiyun 			dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
2756*4882a593Smuzhiyun 			dev->data->scattered_rx = 1;
2757*4882a593Smuzhiyun 		}
2758*4882a593Smuzhiyun 
2759*4882a593Smuzhiyun 		/* Set if packets are dropped when no descriptors available */
2760*4882a593Smuzhiyun 		if (rxq->drop_en)
2761*4882a593Smuzhiyun 			srrctl |= E1000_SRRCTL_DROP_EN;
2762*4882a593Smuzhiyun 
2763*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_SRRCTL(i), srrctl);
2764*4882a593Smuzhiyun 
2765*4882a593Smuzhiyun 		/* Enable this RX queue. */
2766*4882a593Smuzhiyun 		rxdctl = E1000_READ_REG(hw, E1000_RXDCTL(i));
2767*4882a593Smuzhiyun 		rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
2768*4882a593Smuzhiyun 		rxdctl &= 0xFFF00000;
2769*4882a593Smuzhiyun 		rxdctl |= (rxq->pthresh & 0x1F);
2770*4882a593Smuzhiyun 		rxdctl |= ((rxq->hthresh & 0x1F) << 8);
2771*4882a593Smuzhiyun 		if (hw->mac.type == e1000_vfadapt) {
2772*4882a593Smuzhiyun 			/*
2773*4882a593Smuzhiyun 			 * Workaround of 82576 VF Erratum
2774*4882a593Smuzhiyun 			 * force set WTHRESH to 1
2775*4882a593Smuzhiyun 			 * to avoid Write-Back not triggered sometimes
2776*4882a593Smuzhiyun 			 */
2777*4882a593Smuzhiyun 			rxdctl |= 0x10000;
2778*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "Force set RX WTHRESH to 1 !");
2779*4882a593Smuzhiyun 		}
2780*4882a593Smuzhiyun 		else
2781*4882a593Smuzhiyun 			rxdctl |= ((rxq->wthresh & 0x1F) << 16);
2782*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RXDCTL(i), rxdctl);
2783*4882a593Smuzhiyun 	}
2784*4882a593Smuzhiyun 
2785*4882a593Smuzhiyun 	if (dev->data->dev_conf.rxmode.offloads & RTE_ETH_RX_OFFLOAD_SCATTER) {
2786*4882a593Smuzhiyun 		if (!dev->data->scattered_rx)
2787*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "forcing scatter mode");
2788*4882a593Smuzhiyun 		dev->rx_pkt_burst = eth_igb_recv_scattered_pkts;
2789*4882a593Smuzhiyun 		dev->data->scattered_rx = 1;
2790*4882a593Smuzhiyun 	}
2791*4882a593Smuzhiyun 
2792*4882a593Smuzhiyun 	/*
2793*4882a593Smuzhiyun 	 * Setup the HW Rx Head and Tail Descriptor Pointers.
2794*4882a593Smuzhiyun 	 * This needs to be done after enable.
2795*4882a593Smuzhiyun 	 */
2796*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_rx_queues; i++) {
2797*4882a593Smuzhiyun 		rxq = dev->data->rx_queues[i];
2798*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDH(i), 0);
2799*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_RDT(i), rxq->nb_rx_desc - 1);
2800*4882a593Smuzhiyun 	}
2801*4882a593Smuzhiyun 
2802*4882a593Smuzhiyun 	return 0;
2803*4882a593Smuzhiyun }
2804*4882a593Smuzhiyun 
2805*4882a593Smuzhiyun /*********************************************************************
2806*4882a593Smuzhiyun  *
2807*4882a593Smuzhiyun  *  Enable VF transmit unit.
2808*4882a593Smuzhiyun  *
2809*4882a593Smuzhiyun  **********************************************************************/
2810*4882a593Smuzhiyun void
eth_igbvf_tx_init(struct rte_eth_dev * dev)2811*4882a593Smuzhiyun eth_igbvf_tx_init(struct rte_eth_dev *dev)
2812*4882a593Smuzhiyun {
2813*4882a593Smuzhiyun 	struct e1000_hw     *hw;
2814*4882a593Smuzhiyun 	struct igb_tx_queue *txq;
2815*4882a593Smuzhiyun 	uint32_t txdctl;
2816*4882a593Smuzhiyun 	uint16_t i;
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2819*4882a593Smuzhiyun 
2820*4882a593Smuzhiyun 	/* Setup the Base and Length of the Tx Descriptor Rings. */
2821*4882a593Smuzhiyun 	for (i = 0; i < dev->data->nb_tx_queues; i++) {
2822*4882a593Smuzhiyun 		uint64_t bus_addr;
2823*4882a593Smuzhiyun 
2824*4882a593Smuzhiyun 		txq = dev->data->tx_queues[i];
2825*4882a593Smuzhiyun 		bus_addr = txq->tx_ring_phys_addr;
2826*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDLEN(i),
2827*4882a593Smuzhiyun 				txq->nb_tx_desc *
2828*4882a593Smuzhiyun 				sizeof(union e1000_adv_tx_desc));
2829*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDBAH(i),
2830*4882a593Smuzhiyun 				(uint32_t)(bus_addr >> 32));
2831*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDBAL(i), (uint32_t)bus_addr);
2832*4882a593Smuzhiyun 
2833*4882a593Smuzhiyun 		/* Setup the HW Tx Head and Tail descriptor pointers. */
2834*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDT(i), 0);
2835*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TDH(i), 0);
2836*4882a593Smuzhiyun 
2837*4882a593Smuzhiyun 		/* Setup Transmit threshold registers. */
2838*4882a593Smuzhiyun 		txdctl = E1000_READ_REG(hw, E1000_TXDCTL(i));
2839*4882a593Smuzhiyun 		txdctl |= txq->pthresh & 0x1F;
2840*4882a593Smuzhiyun 		txdctl |= ((txq->hthresh & 0x1F) << 8);
2841*4882a593Smuzhiyun 		if (hw->mac.type == e1000_82576) {
2842*4882a593Smuzhiyun 			/*
2843*4882a593Smuzhiyun 			 * Workaround of 82576 VF Erratum
2844*4882a593Smuzhiyun 			 * force set WTHRESH to 1
2845*4882a593Smuzhiyun 			 * to avoid Write-Back not triggered sometimes
2846*4882a593Smuzhiyun 			 */
2847*4882a593Smuzhiyun 			txdctl |= 0x10000;
2848*4882a593Smuzhiyun 			PMD_INIT_LOG(DEBUG, "Force set TX WTHRESH to 1 !");
2849*4882a593Smuzhiyun 		}
2850*4882a593Smuzhiyun 		else
2851*4882a593Smuzhiyun 			txdctl |= ((txq->wthresh & 0x1F) << 16);
2852*4882a593Smuzhiyun 		txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
2853*4882a593Smuzhiyun 		E1000_WRITE_REG(hw, E1000_TXDCTL(i), txdctl);
2854*4882a593Smuzhiyun 	}
2855*4882a593Smuzhiyun 
2856*4882a593Smuzhiyun }
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun void
igb_rxq_info_get(struct rte_eth_dev * dev,uint16_t queue_id,struct rte_eth_rxq_info * qinfo)2859*4882a593Smuzhiyun igb_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
2860*4882a593Smuzhiyun 	struct rte_eth_rxq_info *qinfo)
2861*4882a593Smuzhiyun {
2862*4882a593Smuzhiyun 	struct igb_rx_queue *rxq;
2863*4882a593Smuzhiyun 
2864*4882a593Smuzhiyun 	rxq = dev->data->rx_queues[queue_id];
2865*4882a593Smuzhiyun 
2866*4882a593Smuzhiyun 	qinfo->mp = rxq->mb_pool;
2867*4882a593Smuzhiyun 	qinfo->scattered_rx = dev->data->scattered_rx;
2868*4882a593Smuzhiyun 	qinfo->nb_desc = rxq->nb_rx_desc;
2869*4882a593Smuzhiyun 
2870*4882a593Smuzhiyun 	qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
2871*4882a593Smuzhiyun 	qinfo->conf.rx_drop_en = rxq->drop_en;
2872*4882a593Smuzhiyun 	qinfo->conf.offloads = rxq->offloads;
2873*4882a593Smuzhiyun }
2874*4882a593Smuzhiyun 
2875*4882a593Smuzhiyun void
igb_txq_info_get(struct rte_eth_dev * dev,uint16_t queue_id,struct rte_eth_txq_info * qinfo)2876*4882a593Smuzhiyun igb_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
2877*4882a593Smuzhiyun 	struct rte_eth_txq_info *qinfo)
2878*4882a593Smuzhiyun {
2879*4882a593Smuzhiyun 	struct igb_tx_queue *txq;
2880*4882a593Smuzhiyun 
2881*4882a593Smuzhiyun 	txq = dev->data->tx_queues[queue_id];
2882*4882a593Smuzhiyun 
2883*4882a593Smuzhiyun 	qinfo->nb_desc = txq->nb_tx_desc;
2884*4882a593Smuzhiyun 
2885*4882a593Smuzhiyun 	qinfo->conf.tx_thresh.pthresh = txq->pthresh;
2886*4882a593Smuzhiyun 	qinfo->conf.tx_thresh.hthresh = txq->hthresh;
2887*4882a593Smuzhiyun 	qinfo->conf.tx_thresh.wthresh = txq->wthresh;
2888*4882a593Smuzhiyun 	qinfo->conf.offloads = txq->offloads;
2889*4882a593Smuzhiyun }
2890*4882a593Smuzhiyun 
2891*4882a593Smuzhiyun int
igb_rss_conf_init(struct rte_eth_dev * dev,struct igb_rte_flow_rss_conf * out,const struct rte_flow_action_rss * in)2892*4882a593Smuzhiyun igb_rss_conf_init(struct rte_eth_dev *dev,
2893*4882a593Smuzhiyun 		  struct igb_rte_flow_rss_conf *out,
2894*4882a593Smuzhiyun 		  const struct rte_flow_action_rss *in)
2895*4882a593Smuzhiyun {
2896*4882a593Smuzhiyun 	struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2897*4882a593Smuzhiyun 
2898*4882a593Smuzhiyun 	if (in->key_len > RTE_DIM(out->key) ||
2899*4882a593Smuzhiyun 	    ((hw->mac.type == e1000_82576) &&
2900*4882a593Smuzhiyun 	     (in->queue_num > IGB_MAX_RX_QUEUE_NUM_82576)) ||
2901*4882a593Smuzhiyun 	    ((hw->mac.type != e1000_82576) &&
2902*4882a593Smuzhiyun 	     (in->queue_num > IGB_MAX_RX_QUEUE_NUM)))
2903*4882a593Smuzhiyun 		return -EINVAL;
2904*4882a593Smuzhiyun 	out->conf = (struct rte_flow_action_rss){
2905*4882a593Smuzhiyun 		.func = in->func,
2906*4882a593Smuzhiyun 		.level = in->level,
2907*4882a593Smuzhiyun 		.types = in->types,
2908*4882a593Smuzhiyun 		.key_len = in->key_len,
2909*4882a593Smuzhiyun 		.queue_num = in->queue_num,
2910*4882a593Smuzhiyun 		.key = memcpy(out->key, in->key, in->key_len),
2911*4882a593Smuzhiyun 		.queue = memcpy(out->queue, in->queue,
2912*4882a593Smuzhiyun 				sizeof(*in->queue) * in->queue_num),
2913*4882a593Smuzhiyun 	};
2914*4882a593Smuzhiyun 	return 0;
2915*4882a593Smuzhiyun }
2916*4882a593Smuzhiyun 
2917*4882a593Smuzhiyun int
igb_action_rss_same(const struct rte_flow_action_rss * comp,const struct rte_flow_action_rss * with)2918*4882a593Smuzhiyun igb_action_rss_same(const struct rte_flow_action_rss *comp,
2919*4882a593Smuzhiyun 		    const struct rte_flow_action_rss *with)
2920*4882a593Smuzhiyun {
2921*4882a593Smuzhiyun 	return (comp->func == with->func &&
2922*4882a593Smuzhiyun 		comp->level == with->level &&
2923*4882a593Smuzhiyun 		comp->types == with->types &&
2924*4882a593Smuzhiyun 		comp->key_len == with->key_len &&
2925*4882a593Smuzhiyun 		comp->queue_num == with->queue_num &&
2926*4882a593Smuzhiyun 		!memcmp(comp->key, with->key, with->key_len) &&
2927*4882a593Smuzhiyun 		!memcmp(comp->queue, with->queue,
2928*4882a593Smuzhiyun 			sizeof(*with->queue) * with->queue_num));
2929*4882a593Smuzhiyun }
2930*4882a593Smuzhiyun 
2931*4882a593Smuzhiyun int
igb_config_rss_filter(struct rte_eth_dev * dev,struct igb_rte_flow_rss_conf * conf,bool add)2932*4882a593Smuzhiyun igb_config_rss_filter(struct rte_eth_dev *dev,
2933*4882a593Smuzhiyun 		struct igb_rte_flow_rss_conf *conf, bool add)
2934*4882a593Smuzhiyun {
2935*4882a593Smuzhiyun 	uint32_t shift;
2936*4882a593Smuzhiyun 	uint16_t i, j;
2937*4882a593Smuzhiyun 	struct rte_eth_rss_conf rss_conf = {
2938*4882a593Smuzhiyun 		.rss_key = conf->conf.key_len ?
2939*4882a593Smuzhiyun 			(void *)(uintptr_t)conf->conf.key : NULL,
2940*4882a593Smuzhiyun 		.rss_key_len = conf->conf.key_len,
2941*4882a593Smuzhiyun 		.rss_hf = conf->conf.types,
2942*4882a593Smuzhiyun 	};
2943*4882a593Smuzhiyun 	struct e1000_filter_info *filter_info =
2944*4882a593Smuzhiyun 		E1000_DEV_PRIVATE_TO_FILTER_INFO(dev->data->dev_private);
2945*4882a593Smuzhiyun 	struct e1000_hw *hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2946*4882a593Smuzhiyun 
2947*4882a593Smuzhiyun 	hw = E1000_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun 	if (!add) {
2950*4882a593Smuzhiyun 		if (igb_action_rss_same(&filter_info->rss_info.conf,
2951*4882a593Smuzhiyun 					&conf->conf)) {
2952*4882a593Smuzhiyun 			igb_rss_disable(dev);
2953*4882a593Smuzhiyun 			memset(&filter_info->rss_info, 0,
2954*4882a593Smuzhiyun 				sizeof(struct igb_rte_flow_rss_conf));
2955*4882a593Smuzhiyun 			return 0;
2956*4882a593Smuzhiyun 		}
2957*4882a593Smuzhiyun 		return -EINVAL;
2958*4882a593Smuzhiyun 	}
2959*4882a593Smuzhiyun 
2960*4882a593Smuzhiyun 	if (filter_info->rss_info.conf.queue_num)
2961*4882a593Smuzhiyun 		return -EINVAL;
2962*4882a593Smuzhiyun 
2963*4882a593Smuzhiyun 	/* Fill in redirection table. */
2964*4882a593Smuzhiyun 	shift = (hw->mac.type == e1000_82575) ? 6 : 0;
2965*4882a593Smuzhiyun 	for (i = 0, j = 0; i < 128; i++, j++) {
2966*4882a593Smuzhiyun 		union e1000_reta {
2967*4882a593Smuzhiyun 			uint32_t dword;
2968*4882a593Smuzhiyun 			uint8_t  bytes[4];
2969*4882a593Smuzhiyun 		} reta;
2970*4882a593Smuzhiyun 		uint8_t q_idx;
2971*4882a593Smuzhiyun 
2972*4882a593Smuzhiyun 		if (j == conf->conf.queue_num)
2973*4882a593Smuzhiyun 			j = 0;
2974*4882a593Smuzhiyun 		q_idx = conf->conf.queue[j];
2975*4882a593Smuzhiyun 		reta.bytes[i & 3] = (uint8_t)(q_idx << shift);
2976*4882a593Smuzhiyun 		if ((i & 3) == 3)
2977*4882a593Smuzhiyun 			E1000_WRITE_REG(hw, E1000_RETA(i >> 2), reta.dword);
2978*4882a593Smuzhiyun 	}
2979*4882a593Smuzhiyun 
2980*4882a593Smuzhiyun 	/* Configure the RSS key and the RSS protocols used to compute
2981*4882a593Smuzhiyun 	 * the RSS hash of input packets.
2982*4882a593Smuzhiyun 	 */
2983*4882a593Smuzhiyun 	if ((rss_conf.rss_hf & IGB_RSS_OFFLOAD_ALL) == 0) {
2984*4882a593Smuzhiyun 		igb_rss_disable(dev);
2985*4882a593Smuzhiyun 		return 0;
2986*4882a593Smuzhiyun 	}
2987*4882a593Smuzhiyun 	if (rss_conf.rss_key == NULL)
2988*4882a593Smuzhiyun 		rss_conf.rss_key = rss_intel_key; /* Default hash key */
2989*4882a593Smuzhiyun 	igb_hw_rss_hash_set(hw, &rss_conf);
2990*4882a593Smuzhiyun 
2991*4882a593Smuzhiyun 	if (igb_rss_conf_init(dev, &filter_info->rss_info, &conf->conf))
2992*4882a593Smuzhiyun 		return -EINVAL;
2993*4882a593Smuzhiyun 
2994*4882a593Smuzhiyun 	return 0;
2995*4882a593Smuzhiyun }
2996