xref: /f-stack/dpdk/drivers/net/bnxt/bnxt_txr.c (revision 031be553)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <inttypes.h>
35 
36 #include <rte_byteorder.h>
37 #include <rte_malloc.h>
38 
39 #include "bnxt.h"
40 #include "bnxt_cpr.h"
41 #include "bnxt_ring.h"
42 #include "bnxt_txq.h"
43 #include "bnxt_txr.h"
44 #include "hsi_struct_def_dpdk.h"
45 #include <stdbool.h>
46 
47 /*
48  * TX Ring handling
49  */
50 
51 void bnxt_free_tx_rings(struct bnxt *bp)
52 {
53 	int i;
54 
55 	for (i = 0; i < (int)bp->tx_nr_rings; i++) {
56 		struct bnxt_tx_queue *txq = bp->tx_queues[i];
57 
58 		if (!txq)
59 			continue;
60 
61 		bnxt_free_ring(txq->tx_ring->tx_ring_struct);
62 		rte_free(txq->tx_ring->tx_ring_struct);
63 		rte_free(txq->tx_ring);
64 
65 		bnxt_free_ring(txq->cp_ring->cp_ring_struct);
66 		rte_free(txq->cp_ring->cp_ring_struct);
67 		rte_free(txq->cp_ring);
68 
69 		rte_free(txq);
70 		bp->tx_queues[i] = NULL;
71 	}
72 }
73 
74 int bnxt_init_one_tx_ring(struct bnxt_tx_queue *txq)
75 {
76 	struct bnxt_tx_ring_info *txr = txq->tx_ring;
77 	struct bnxt_ring *ring = txr->tx_ring_struct;
78 
79 	txq->tx_wake_thresh = ring->ring_size / 2;
80 	ring->fw_ring_id = INVALID_HW_RING_ID;
81 
82 	return 0;
83 }
84 
85 int bnxt_init_tx_ring_struct(struct bnxt_tx_queue *txq, unsigned int socket_id)
86 {
87 	struct bnxt_cp_ring_info *cpr;
88 	struct bnxt_tx_ring_info *txr;
89 	struct bnxt_ring *ring;
90 
91 	txr = rte_zmalloc_socket("bnxt_tx_ring",
92 				 sizeof(struct bnxt_tx_ring_info),
93 				 RTE_CACHE_LINE_SIZE, socket_id);
94 	if (txr == NULL)
95 		return -ENOMEM;
96 	txq->tx_ring = txr;
97 
98 	ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
99 				  sizeof(struct bnxt_ring),
100 				  RTE_CACHE_LINE_SIZE, socket_id);
101 	if (ring == NULL)
102 		return -ENOMEM;
103 	txr->tx_ring_struct = ring;
104 	ring->ring_size = rte_align32pow2(txq->nb_tx_desc);
105 	ring->ring_mask = ring->ring_size - 1;
106 	ring->bd = (void *)txr->tx_desc_ring;
107 	ring->bd_dma = txr->tx_desc_mapping;
108 	ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_tx_bd);
109 	ring->vmem = (void **)&txr->tx_buf_ring;
110 
111 	cpr = rte_zmalloc_socket("bnxt_tx_ring",
112 				 sizeof(struct bnxt_cp_ring_info),
113 				 RTE_CACHE_LINE_SIZE, socket_id);
114 	if (cpr == NULL)
115 		return -ENOMEM;
116 	txq->cp_ring = cpr;
117 
118 	ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
119 				  sizeof(struct bnxt_ring),
120 				  RTE_CACHE_LINE_SIZE, socket_id);
121 	if (ring == NULL)
122 		return -ENOMEM;
123 	cpr->cp_ring_struct = ring;
124 	ring->ring_size = txr->tx_ring_struct->ring_size;
125 	ring->ring_mask = ring->ring_size - 1;
126 	ring->bd = (void *)cpr->cp_desc_ring;
127 	ring->bd_dma = cpr->cp_desc_mapping;
128 	ring->vmem_size = 0;
129 	ring->vmem = NULL;
130 
131 	return 0;
132 }
133 
134 static inline uint32_t bnxt_tx_avail(struct bnxt_tx_ring_info *txr)
135 {
136 	/* Tell compiler to fetch tx indices from memory. */
137 	rte_compiler_barrier();
138 
139 	return txr->tx_ring_struct->ring_size -
140 		((txr->tx_prod - txr->tx_cons) &
141 			txr->tx_ring_struct->ring_mask) - 1;
142 }
143 
144 static uint16_t bnxt_start_xmit(struct rte_mbuf *tx_pkt,
145 				struct bnxt_tx_queue *txq)
146 {
147 	struct bnxt_tx_ring_info *txr = txq->tx_ring;
148 	struct tx_bd_long *txbd;
149 	struct tx_bd_long_hi *txbd1;
150 	uint32_t vlan_tag_flags, cfa_action;
151 	bool long_bd = false;
152 	uint16_t last_prod = 0;
153 	struct rte_mbuf *m_seg;
154 	struct bnxt_sw_tx_bd *tx_buf;
155 	static const uint32_t lhint_arr[4] = {
156 		TX_BD_LONG_FLAGS_LHINT_LT512,
157 		TX_BD_LONG_FLAGS_LHINT_LT1K,
158 		TX_BD_LONG_FLAGS_LHINT_LT2K,
159 		TX_BD_LONG_FLAGS_LHINT_LT2K
160 	};
161 
162 	if (tx_pkt->ol_flags & (PKT_TX_TCP_SEG | PKT_TX_TCP_CKSUM |
163 				PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM |
164 				PKT_TX_VLAN_PKT | PKT_TX_OUTER_IP_CKSUM |
165 				PKT_TX_TUNNEL_GRE | PKT_TX_TUNNEL_VXLAN |
166 				PKT_TX_TUNNEL_GENEVE))
167 		long_bd = true;
168 
169 	tx_buf = &txr->tx_buf_ring[txr->tx_prod];
170 	tx_buf->mbuf = tx_pkt;
171 	tx_buf->nr_bds = long_bd + tx_pkt->nb_segs;
172 	last_prod = (txr->tx_prod + tx_buf->nr_bds - 1) &
173 				txr->tx_ring_struct->ring_mask;
174 
175 	if (unlikely(bnxt_tx_avail(txr) < tx_buf->nr_bds))
176 		return -ENOMEM;
177 
178 	txbd = &txr->tx_desc_ring[txr->tx_prod];
179 	txbd->opaque = txr->tx_prod;
180 	txbd->flags_type = tx_buf->nr_bds << TX_BD_LONG_FLAGS_BD_CNT_SFT;
181 	txbd->len = tx_pkt->data_len;
182 	if (txbd->len >= 2014)
183 		txbd->flags_type |= TX_BD_LONG_FLAGS_LHINT_GTE2K;
184 	else
185 		txbd->flags_type |= lhint_arr[txbd->len >> 9];
186 	txbd->addr = rte_cpu_to_le_32(RTE_MBUF_DATA_DMA_ADDR(tx_buf->mbuf));
187 
188 	if (long_bd) {
189 		txbd->flags_type |= TX_BD_LONG_TYPE_TX_BD_LONG;
190 		vlan_tag_flags = 0;
191 		cfa_action = 0;
192 		if (tx_buf->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
193 			/* shurd: Should this mask at
194 			 * TX_BD_LONG_CFA_META_VLAN_VID_MASK?
195 			 */
196 			vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
197 				tx_buf->mbuf->vlan_tci;
198 			/* Currently supports 8021Q, 8021AD vlan offloads
199 			 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
200 			 */
201 			/* DPDK only supports 802.11q VLAN packets */
202 			vlan_tag_flags |=
203 					TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
204 		}
205 
206 		txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
207 
208 		txbd1 = (struct tx_bd_long_hi *)
209 					&txr->tx_desc_ring[txr->tx_prod];
210 		txbd1->lflags = 0;
211 		txbd1->cfa_meta = vlan_tag_flags;
212 		txbd1->cfa_action = cfa_action;
213 
214 		if (tx_pkt->ol_flags & PKT_TX_TCP_SEG) {
215 			/* TSO */
216 			txbd1->lflags |= TX_BD_LONG_LFLAGS_LSO;
217 			txbd1->hdr_size = tx_pkt->l2_len + tx_pkt->l3_len +
218 					tx_pkt->l4_len + tx_pkt->outer_l2_len +
219 					tx_pkt->outer_l3_len;
220 			txbd1->mss = tx_pkt->tso_segsz;
221 
222 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_UDP_CKSUM) ==
223 			   PKT_TX_OIP_IIP_TCP_UDP_CKSUM) {
224 			/* Outer IP, Inner IP, Inner TCP/UDP CSO */
225 			txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
226 			txbd1->mss = 0;
227 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_CKSUM) ==
228 			   PKT_TX_OIP_IIP_TCP_CKSUM) {
229 			/* Outer IP, Inner IP, Inner TCP/UDP CSO */
230 			txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
231 			txbd1->mss = 0;
232 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_UDP_CKSUM) ==
233 			   PKT_TX_OIP_IIP_UDP_CKSUM) {
234 			/* Outer IP, Inner IP, Inner TCP/UDP CSO */
235 			txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
236 			txbd1->mss = 0;
237 		} else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_UDP_CKSUM) ==
238 			   PKT_TX_IIP_TCP_UDP_CKSUM) {
239 			/* (Inner) IP, (Inner) TCP/UDP CSO */
240 			txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
241 			txbd1->mss = 0;
242 		} else if ((tx_pkt->ol_flags & PKT_TX_IIP_UDP_CKSUM) ==
243 			   PKT_TX_IIP_UDP_CKSUM) {
244 			/* (Inner) IP, (Inner) TCP/UDP CSO */
245 			txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
246 			txbd1->mss = 0;
247 		} else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_CKSUM) ==
248 			   PKT_TX_IIP_TCP_CKSUM) {
249 			/* (Inner) IP, (Inner) TCP/UDP CSO */
250 			txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
251 			txbd1->mss = 0;
252 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_UDP_CKSUM) ==
253 			   PKT_TX_OIP_TCP_UDP_CKSUM) {
254 			/* Outer IP, (Inner) TCP/UDP CSO */
255 			txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
256 			txbd1->mss = 0;
257 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_UDP_CKSUM) ==
258 			   PKT_TX_OIP_UDP_CKSUM) {
259 			/* Outer IP, (Inner) TCP/UDP CSO */
260 			txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
261 			txbd1->mss = 0;
262 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_CKSUM) ==
263 			   PKT_TX_OIP_TCP_CKSUM) {
264 			/* Outer IP, (Inner) TCP/UDP CSO */
265 			txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
266 			txbd1->mss = 0;
267 		} else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_CKSUM) ==
268 			   PKT_TX_OIP_IIP_CKSUM) {
269 			/* Outer IP, Inner IP CSO */
270 			txbd1->lflags |= TX_BD_FLG_TIP_IP_CHKSUM;
271 			txbd1->mss = 0;
272 		} else if ((tx_pkt->ol_flags & PKT_TX_TCP_UDP_CKSUM) ==
273 			   PKT_TX_TCP_UDP_CKSUM) {
274 			/* TCP/UDP CSO */
275 			txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
276 			txbd1->mss = 0;
277 		} else if ((tx_pkt->ol_flags & PKT_TX_TCP_CKSUM) ==
278 			   PKT_TX_TCP_CKSUM) {
279 			/* TCP/UDP CSO */
280 			txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
281 			txbd1->mss = 0;
282 		} else if ((tx_pkt->ol_flags & PKT_TX_UDP_CKSUM) ==
283 			   PKT_TX_UDP_CKSUM) {
284 			/* TCP/UDP CSO */
285 			txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
286 			txbd1->mss = 0;
287 		} else if ((tx_pkt->ol_flags & PKT_TX_IP_CKSUM) ==
288 			   PKT_TX_IP_CKSUM) {
289 			/* IP CSO */
290 			txbd1->lflags |= TX_BD_LONG_LFLAGS_IP_CHKSUM;
291 			txbd1->mss = 0;
292 		} else if ((tx_pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM) ==
293 			   PKT_TX_OUTER_IP_CKSUM) {
294 			/* IP CSO */
295 			txbd1->lflags |= TX_BD_LONG_LFLAGS_T_IP_CHKSUM;
296 			txbd1->mss = 0;
297 		}
298 	} else {
299 		txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
300 	}
301 
302 	m_seg = tx_pkt->next;
303 	/* i is set at the end of the if(long_bd) block */
304 	while (txr->tx_prod != last_prod) {
305 		txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
306 		tx_buf = &txr->tx_buf_ring[txr->tx_prod];
307 
308 		txbd = &txr->tx_desc_ring[txr->tx_prod];
309 		txbd->addr = rte_cpu_to_le_32(RTE_MBUF_DATA_DMA_ADDR(m_seg));
310 		txbd->flags_type = TX_BD_SHORT_TYPE_TX_BD_SHORT;
311 		txbd->len = m_seg->data_len;
312 
313 		m_seg = m_seg->next;
314 	}
315 
316 	txbd->flags_type |= TX_BD_LONG_FLAGS_PACKET_END;
317 	txbd1->lflags = rte_cpu_to_le_32(txbd1->lflags);
318 
319 	txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
320 
321 	return 0;
322 }
323 
324 static void bnxt_tx_cmp(struct bnxt_tx_queue *txq, int nr_pkts)
325 {
326 	struct bnxt_tx_ring_info *txr = txq->tx_ring;
327 	uint16_t cons = txr->tx_cons;
328 	int i, j;
329 
330 	for (i = 0; i < nr_pkts; i++) {
331 		struct bnxt_sw_tx_bd *tx_buf;
332 		struct rte_mbuf *mbuf;
333 
334 		tx_buf = &txr->tx_buf_ring[cons];
335 		cons = RING_NEXT(txr->tx_ring_struct, cons);
336 		mbuf = tx_buf->mbuf;
337 		tx_buf->mbuf = NULL;
338 
339 		/* EW - no need to unmap DMA memory? */
340 
341 		for (j = 1; j < tx_buf->nr_bds; j++)
342 			cons = RING_NEXT(txr->tx_ring_struct, cons);
343 		rte_pktmbuf_free(mbuf);
344 	}
345 
346 	txr->tx_cons = cons;
347 }
348 
349 static int bnxt_handle_tx_cp(struct bnxt_tx_queue *txq)
350 {
351 	struct bnxt_cp_ring_info *cpr = txq->cp_ring;
352 	uint32_t raw_cons = cpr->cp_raw_cons;
353 	uint32_t cons;
354 	int nb_tx_pkts = 0;
355 	struct tx_cmpl *txcmp;
356 
357 	if ((txq->tx_ring->tx_ring_struct->ring_size -
358 			(bnxt_tx_avail(txq->tx_ring))) >
359 			txq->tx_free_thresh) {
360 		while (1) {
361 			cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
362 			txcmp = (struct tx_cmpl *)&cpr->cp_desc_ring[cons];
363 
364 			if (!CMP_VALID(txcmp, raw_cons, cpr->cp_ring_struct))
365 				break;
366 			cpr->valid = FLIP_VALID(cons,
367 						cpr->cp_ring_struct->ring_mask,
368 						cpr->valid);
369 
370 			if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
371 				nb_tx_pkts++;
372 			else
373 				RTE_LOG_DP(DEBUG, PMD,
374 						"Unhandled CMP type %02x\n",
375 						CMP_TYPE(txcmp));
376 			raw_cons = NEXT_RAW_CMP(raw_cons);
377 		}
378 		if (nb_tx_pkts)
379 			bnxt_tx_cmp(txq, nb_tx_pkts);
380 		cpr->cp_raw_cons = raw_cons;
381 		B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
382 	}
383 	return nb_tx_pkts;
384 }
385 
386 uint16_t bnxt_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
387 			       uint16_t nb_pkts)
388 {
389 	struct bnxt_tx_queue *txq = tx_queue;
390 	uint16_t nb_tx_pkts = 0;
391 	uint16_t db_mask = txq->tx_ring->tx_ring_struct->ring_size >> 2;
392 	uint16_t last_db_mask = 0;
393 
394 	/* Handle TX completions */
395 	bnxt_handle_tx_cp(txq);
396 
397 	/* Handle TX burst request */
398 	for (nb_tx_pkts = 0; nb_tx_pkts < nb_pkts; nb_tx_pkts++) {
399 		if (bnxt_start_xmit(tx_pkts[nb_tx_pkts], txq)) {
400 			break;
401 		} else if ((nb_tx_pkts & db_mask) != last_db_mask) {
402 			B_TX_DB(txq->tx_ring->tx_doorbell,
403 					txq->tx_ring->tx_prod);
404 			last_db_mask = nb_tx_pkts & db_mask;
405 		}
406 	}
407 	if (nb_tx_pkts)
408 		B_TX_DB(txq->tx_ring->tx_doorbell, txq->tx_ring->tx_prod);
409 
410 	return nb_tx_pkts;
411 }
412