1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
8 #include <rte_byteorder.h>
9 #include <rte_malloc.h>
12 #include "bnxt_hwrm.h"
13 #include "bnxt_ring.h"
16 #include "hsi_struct_def_dpdk.h"
23 void bnxt_free_tx_rings(struct bnxt *bp)
27 for (i = 0; i < (int)bp->tx_nr_rings; i++) {
28 struct bnxt_tx_queue *txq = bp->tx_queues[i];
33 bnxt_free_ring(txq->tx_ring->tx_ring_struct);
34 rte_free(txq->tx_ring->tx_ring_struct);
35 rte_free(txq->tx_ring);
37 bnxt_free_ring(txq->cp_ring->cp_ring_struct);
38 rte_free(txq->cp_ring->cp_ring_struct);
39 rte_free(txq->cp_ring);
42 bp->tx_queues[i] = NULL;
46 int bnxt_init_one_tx_ring(struct bnxt_tx_queue *txq)
48 struct bnxt_tx_ring_info *txr = txq->tx_ring;
49 struct bnxt_ring *ring = txr->tx_ring_struct;
51 txq->tx_wake_thresh = ring->ring_size / 2;
52 ring->fw_ring_id = INVALID_HW_RING_ID;
57 int bnxt_init_tx_ring_struct(struct bnxt_tx_queue *txq, unsigned int socket_id)
59 struct bnxt_cp_ring_info *cpr;
60 struct bnxt_tx_ring_info *txr;
61 struct bnxt_ring *ring;
63 txr = rte_zmalloc_socket("bnxt_tx_ring",
64 sizeof(struct bnxt_tx_ring_info),
65 RTE_CACHE_LINE_SIZE, socket_id);
70 ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
71 sizeof(struct bnxt_ring),
72 RTE_CACHE_LINE_SIZE, socket_id);
75 txr->tx_ring_struct = ring;
76 ring->ring_size = rte_align32pow2(txq->nb_tx_desc);
77 ring->ring_mask = ring->ring_size - 1;
78 ring->bd = (void *)txr->tx_desc_ring;
79 ring->bd_dma = txr->tx_desc_mapping;
80 ring->vmem_size = ring->ring_size * sizeof(struct rte_mbuf *);
81 ring->vmem = (void **)&txr->tx_buf_ring;
82 ring->fw_ring_id = INVALID_HW_RING_ID;
84 cpr = rte_zmalloc_socket("bnxt_tx_ring",
85 sizeof(struct bnxt_cp_ring_info),
86 RTE_CACHE_LINE_SIZE, socket_id);
91 ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
92 sizeof(struct bnxt_ring),
93 RTE_CACHE_LINE_SIZE, socket_id);
96 cpr->cp_ring_struct = ring;
97 ring->ring_size = txr->tx_ring_struct->ring_size;
98 ring->ring_mask = ring->ring_size - 1;
99 ring->bd = (void *)cpr->cp_desc_ring;
100 ring->bd_dma = cpr->cp_desc_mapping;
103 ring->fw_ring_id = INVALID_HW_RING_ID;
109 bnxt_xmit_need_long_bd(struct rte_mbuf *tx_pkt, struct bnxt_tx_queue *txq)
111 if (tx_pkt->ol_flags & (PKT_TX_TCP_SEG | PKT_TX_TCP_CKSUM |
112 PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM |
113 PKT_TX_VLAN_PKT | PKT_TX_OUTER_IP_CKSUM |
114 PKT_TX_TUNNEL_GRE | PKT_TX_TUNNEL_VXLAN |
115 PKT_TX_TUNNEL_GENEVE | PKT_TX_IEEE1588_TMST |
117 (BNXT_TRUFLOW_EN(txq->bp) &&
118 (txq->bp->tx_cfa_action || txq->vfr_tx_cfa_action)))
123 static uint16_t bnxt_start_xmit(struct rte_mbuf *tx_pkt,
124 struct bnxt_tx_queue *txq,
126 struct tx_bd_long **last_txbd)
128 struct bnxt_tx_ring_info *txr = txq->tx_ring;
129 struct bnxt_ring *ring = txr->tx_ring_struct;
130 uint32_t outer_tpid_bd = 0;
131 struct tx_bd_long *txbd;
132 struct tx_bd_long_hi *txbd1 = NULL;
133 uint32_t vlan_tag_flags;
134 bool long_bd = false;
135 unsigned short nr_bds;
137 struct rte_mbuf *m_seg;
138 struct rte_mbuf **tx_buf;
139 static const uint32_t lhint_arr[4] = {
140 TX_BD_LONG_FLAGS_LHINT_LT512,
141 TX_BD_LONG_FLAGS_LHINT_LT1K,
142 TX_BD_LONG_FLAGS_LHINT_LT2K,
143 TX_BD_LONG_FLAGS_LHINT_LT2K
146 if (unlikely(is_bnxt_in_error(txq->bp)))
149 long_bd = bnxt_xmit_need_long_bd(tx_pkt, txq);
150 nr_bds = long_bd + tx_pkt->nb_segs;
152 if (unlikely(bnxt_tx_avail(txq) < nr_bds))
155 /* Check if number of Tx descriptors is above HW limit */
156 if (unlikely(nr_bds > BNXT_MAX_TSO_SEGS)) {
158 "Num descriptors %d exceeds HW limit\n", nr_bds);
162 /* If packet length is less than minimum packet size, pad it */
163 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) < BNXT_MIN_PKT_SIZE)) {
164 uint8_t pad = BNXT_MIN_PKT_SIZE - rte_pktmbuf_pkt_len(tx_pkt);
165 char *seg = rte_pktmbuf_append(tx_pkt, pad);
169 "Failed to pad mbuf by %d bytes\n",
174 /* Note: data_len, pkt len are updated in rte_pktmbuf_append */
178 /* Check non zero data_len */
179 RTE_VERIFY(tx_pkt->data_len);
181 prod = RING_IDX(ring, txr->tx_raw_prod);
182 tx_buf = &txr->tx_buf_ring[prod];
185 txbd = &txr->tx_desc_ring[prod];
186 txbd->opaque = *coal_pkts;
187 txbd->flags_type = nr_bds << TX_BD_LONG_FLAGS_BD_CNT_SFT;
188 txbd->flags_type |= TX_BD_SHORT_FLAGS_COAL_NOW;
189 txbd->flags_type |= TX_BD_LONG_FLAGS_NO_CMPL;
190 txbd->len = tx_pkt->data_len;
191 if (tx_pkt->pkt_len >= 2048)
192 txbd->flags_type |= TX_BD_LONG_FLAGS_LHINT_GTE2K;
194 txbd->flags_type |= lhint_arr[tx_pkt->pkt_len >> 9];
195 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(tx_pkt));
199 txbd->flags_type |= TX_BD_LONG_TYPE_TX_BD_LONG;
202 /* HW can accelerate only outer vlan in QinQ mode */
203 if (tx_pkt->ol_flags & PKT_TX_QINQ_PKT) {
204 vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
205 tx_pkt->vlan_tci_outer;
206 outer_tpid_bd = txq->bp->outer_tpid_bd &
207 BNXT_OUTER_TPID_BD_MASK;
208 vlan_tag_flags |= outer_tpid_bd;
209 } else if (tx_pkt->ol_flags & PKT_TX_VLAN_PKT) {
210 /* shurd: Should this mask at
211 * TX_BD_LONG_CFA_META_VLAN_VID_MASK?
213 vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
215 /* Currently supports 8021Q, 8021AD vlan offloads
216 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
218 /* DPDK only supports 802.11q VLAN packets */
220 TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
223 txr->tx_raw_prod = RING_NEXT(txr->tx_raw_prod);
225 prod = RING_IDX(ring, txr->tx_raw_prod);
226 txbd1 = (struct tx_bd_long_hi *)&txr->tx_desc_ring[prod];
228 txbd1->cfa_meta = vlan_tag_flags;
229 /* Legacy tx_bd_long_hi->mss =
230 * tx_bd_long_hi->kid_or_ts_high_mss
232 txbd1->kid_or_ts_high_mss = 0;
234 if (txq->vfr_tx_cfa_action)
235 txbd1->cfa_action = txq->vfr_tx_cfa_action;
237 txbd1->cfa_action = txq->bp->tx_cfa_action;
239 if (tx_pkt->ol_flags & PKT_TX_TCP_SEG) {
243 txbd1->lflags |= TX_BD_LONG_LFLAGS_LSO |
244 TX_BD_LONG_LFLAGS_T_IPID;
245 hdr_size = tx_pkt->l2_len + tx_pkt->l3_len +
247 hdr_size += (tx_pkt->ol_flags & PKT_TX_TUNNEL_MASK) ?
248 tx_pkt->outer_l2_len +
249 tx_pkt->outer_l3_len : 0;
250 /* The hdr_size is multiple of 16bit units not 8bit.
252 * Also legacy hdr_size = kid_or_ts_low_hdr_size.
254 txbd1->kid_or_ts_low_hdr_size = hdr_size >> 1;
255 txbd1->kid_or_ts_high_mss = tx_pkt->tso_segsz;
256 RTE_VERIFY(txbd1->kid_or_ts_high_mss);
258 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_UDP_CKSUM) ==
259 PKT_TX_OIP_IIP_TCP_UDP_CKSUM) {
260 /* Outer IP, Inner IP, Inner TCP/UDP CSO */
261 txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
262 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_CKSUM) ==
263 PKT_TX_OIP_IIP_TCP_CKSUM) {
264 /* Outer IP, Inner IP, Inner TCP/UDP CSO */
265 txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
266 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_UDP_CKSUM) ==
267 PKT_TX_OIP_IIP_UDP_CKSUM) {
268 /* Outer IP, Inner IP, Inner TCP/UDP CSO */
269 txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
270 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_UDP_CKSUM) ==
271 PKT_TX_IIP_TCP_UDP_CKSUM) {
272 /* (Inner) IP, (Inner) TCP/UDP CSO */
273 txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
274 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_UDP_CKSUM) ==
275 PKT_TX_IIP_UDP_CKSUM) {
276 /* (Inner) IP, (Inner) TCP/UDP CSO */
277 txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
278 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_CKSUM) ==
279 PKT_TX_IIP_TCP_CKSUM) {
280 /* (Inner) IP, (Inner) TCP/UDP CSO */
281 txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
282 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_UDP_CKSUM) ==
283 PKT_TX_OIP_TCP_UDP_CKSUM) {
284 /* Outer IP, (Inner) TCP/UDP CSO */
285 txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
286 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_UDP_CKSUM) ==
287 PKT_TX_OIP_UDP_CKSUM) {
288 /* Outer IP, (Inner) TCP/UDP CSO */
289 txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
290 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_CKSUM) ==
291 PKT_TX_OIP_TCP_CKSUM) {
292 /* Outer IP, (Inner) TCP/UDP CSO */
293 txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
294 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_CKSUM) ==
295 PKT_TX_OIP_IIP_CKSUM) {
296 /* Outer IP, Inner IP CSO */
297 txbd1->lflags |= TX_BD_FLG_TIP_IP_CHKSUM;
298 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_UDP_CKSUM) ==
299 PKT_TX_TCP_UDP_CKSUM) {
301 txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
302 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_CKSUM) ==
305 txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
306 } else if ((tx_pkt->ol_flags & PKT_TX_UDP_CKSUM) ==
309 txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
310 } else if ((tx_pkt->ol_flags & PKT_TX_IP_CKSUM) ==
313 txbd1->lflags |= TX_BD_LONG_LFLAGS_IP_CHKSUM;
314 } else if ((tx_pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM) ==
315 PKT_TX_OUTER_IP_CKSUM) {
317 txbd1->lflags |= TX_BD_LONG_LFLAGS_T_IP_CHKSUM;
318 } else if ((tx_pkt->ol_flags & PKT_TX_IEEE1588_TMST) ==
319 PKT_TX_IEEE1588_TMST) {
321 txbd1->lflags |= TX_BD_LONG_LFLAGS_STAMP;
324 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
327 m_seg = tx_pkt->next;
329 /* Check non zero data_len */
330 RTE_VERIFY(m_seg->data_len);
331 txr->tx_raw_prod = RING_NEXT(txr->tx_raw_prod);
333 prod = RING_IDX(ring, txr->tx_raw_prod);
334 tx_buf = &txr->tx_buf_ring[prod];
337 txbd = &txr->tx_desc_ring[prod];
338 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(m_seg));
339 txbd->flags_type = TX_BD_SHORT_TYPE_TX_BD_SHORT;
340 txbd->len = m_seg->data_len;
345 txbd->flags_type |= TX_BD_LONG_FLAGS_PACKET_END;
347 txr->tx_raw_prod = RING_NEXT(txr->tx_raw_prod);
353 * Transmit completion function for use when DEV_TX_OFFLOAD_MBUF_FAST_FREE
356 static void bnxt_tx_cmp_fast(struct bnxt_tx_queue *txq, int nr_pkts)
358 struct bnxt_tx_ring_info *txr = txq->tx_ring;
359 struct bnxt_ring *ring = txr->tx_ring_struct;
360 struct rte_mbuf **free = txq->free;
361 uint16_t raw_cons = txr->tx_raw_cons;
362 unsigned int blk = 0;
365 for (i = 0; i < nr_pkts; i++) {
366 struct rte_mbuf **tx_buf;
367 unsigned short nr_bds;
369 tx_buf = &txr->tx_buf_ring[RING_IDX(ring, raw_cons)];
370 nr_bds = (*tx_buf)->nb_segs +
371 bnxt_xmit_need_long_bd(*tx_buf, txq);
372 for (j = 0; j < nr_bds; j++) {
374 /* Add mbuf to the bulk free array */
375 free[blk++] = *tx_buf;
378 raw_cons = RING_NEXT(raw_cons);
379 tx_buf = &txr->tx_buf_ring[RING_IDX(ring, raw_cons)];
383 rte_mempool_put_bulk(free[0]->pool, (void *)free, blk);
385 txr->tx_raw_cons = raw_cons;
388 static void bnxt_tx_cmp(struct bnxt_tx_queue *txq, int nr_pkts)
390 struct bnxt_tx_ring_info *txr = txq->tx_ring;
391 struct bnxt_ring *ring = txr->tx_ring_struct;
392 struct rte_mempool *pool = NULL;
393 struct rte_mbuf **free = txq->free;
394 uint16_t raw_cons = txr->tx_raw_cons;
395 unsigned int blk = 0;
398 for (i = 0; i < nr_pkts; i++) {
399 struct rte_mbuf *mbuf;
400 struct rte_mbuf **tx_buf;
401 unsigned short nr_bds;
403 tx_buf = &txr->tx_buf_ring[RING_IDX(ring, raw_cons)];
404 nr_bds = (*tx_buf)->nb_segs +
405 bnxt_xmit_need_long_bd(*tx_buf, txq);
406 for (j = 0; j < nr_bds; j++) {
409 raw_cons = RING_NEXT(raw_cons);
410 tx_buf = &txr->tx_buf_ring[RING_IDX(ring, raw_cons)];
411 if (!mbuf) /* long_bd's tx_buf ? */
414 mbuf = rte_pktmbuf_prefree_seg(mbuf);
418 /* EW - no need to unmap DMA memory? */
420 if (likely(mbuf->pool == pool)) {
421 /* Add mbuf to the bulk free array */
424 /* Found an mbuf from a different pool. Free
425 * mbufs accumulated so far to the previous
428 if (likely(pool != NULL))
429 rte_mempool_put_bulk(pool,
433 /* Start accumulating mbufs in a new pool */
441 rte_mempool_put_bulk(pool, (void *)free, blk);
443 txr->tx_raw_cons = raw_cons;
446 static int bnxt_handle_tx_cp(struct bnxt_tx_queue *txq)
448 uint32_t nb_tx_pkts = 0, cons, ring_mask, opaque;
449 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
450 uint32_t raw_cons = cpr->cp_raw_cons;
451 struct bnxt_ring *cp_ring_struct;
452 struct tx_cmpl *txcmp;
454 if (bnxt_tx_bds_in_hw(txq) < txq->tx_free_thresh)
457 cp_ring_struct = cpr->cp_ring_struct;
458 ring_mask = cp_ring_struct->ring_mask;
461 cons = RING_CMPL(ring_mask, raw_cons);
462 txcmp = (struct tx_cmpl *)&cpr->cp_desc_ring[cons];
464 if (!CMP_VALID(txcmp, raw_cons, cp_ring_struct))
467 opaque = rte_le_to_cpu_32(txcmp->opaque);
469 if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
470 nb_tx_pkts += opaque;
473 "Unhandled CMP type %02x\n",
475 raw_cons = NEXT_RAW_CMP(raw_cons);
476 } while (nb_tx_pkts < ring_mask);
479 if (txq->offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
480 bnxt_tx_cmp_fast(txq, nb_tx_pkts);
482 bnxt_tx_cmp(txq, nb_tx_pkts);
483 cpr->cp_raw_cons = raw_cons;
490 uint16_t bnxt_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
494 uint16_t nb_tx_pkts = 0;
495 uint16_t coal_pkts = 0;
496 struct bnxt_tx_queue *txq = tx_queue;
497 struct tx_bd_long *last_txbd = NULL;
499 /* Handle TX completions */
500 bnxt_handle_tx_cp(txq);
502 /* Tx queue was stopped; wait for it to be restarted */
503 if (unlikely(!txq->tx_started)) {
504 PMD_DRV_LOG(DEBUG, "Tx q stopped;return\n");
508 /* Handle TX burst request */
509 for (nb_tx_pkts = 0; nb_tx_pkts < nb_pkts; nb_tx_pkts++) {
511 rc = bnxt_start_xmit(tx_pkts[nb_tx_pkts], txq,
512 &coal_pkts, &last_txbd);
518 if (likely(nb_tx_pkts)) {
519 /* Request a completion on the last packet */
520 last_txbd->flags_type &= ~TX_BD_LONG_FLAGS_NO_CMPL;
521 bnxt_db_write(&txq->tx_ring->tx_db, txq->tx_ring->tx_raw_prod);
528 * Dummy DPDK callback for TX.
530 * This function is used to temporarily replace the real callback during
531 * unsafe control operations on the queue, or in case of error.
534 bnxt_dummy_xmit_pkts(void *tx_queue __rte_unused,
535 struct rte_mbuf **tx_pkts __rte_unused,
536 uint16_t nb_pkts __rte_unused)
541 int bnxt_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
543 struct bnxt *bp = dev->data->dev_private;
544 struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
547 rc = is_bnxt_in_error(bp);
551 bnxt_free_hwrm_tx_ring(bp, tx_queue_id);
552 rc = bnxt_alloc_hwrm_tx_ring(bp, tx_queue_id);
556 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
557 txq->tx_started = true;
558 PMD_DRV_LOG(DEBUG, "Tx queue started\n");
563 int bnxt_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
565 struct bnxt *bp = dev->data->dev_private;
566 struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
569 rc = is_bnxt_in_error(bp);
573 /* Handle TX completions */
574 bnxt_handle_tx_cp(txq);
576 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
577 txq->tx_started = false;
578 PMD_DRV_LOG(DEBUG, "Tx queue stopped\n");
583 /* Sweep the Tx completion queue till HWRM_DONE for ring flush is received.
584 * The mbufs will not be freed in this call.
585 * They will be freed during ring free as a part of mem cleanup.
587 int bnxt_flush_tx_cmp(struct bnxt_cp_ring_info *cpr)
589 uint32_t raw_cons = cpr->cp_raw_cons;
591 uint32_t nb_tx_pkts = 0;
592 struct tx_cmpl *txcmp;
593 struct cmpl_base *cp_desc_ring = cpr->cp_desc_ring;
594 struct bnxt_ring *cp_ring_struct = cpr->cp_ring_struct;
595 uint32_t ring_mask = cp_ring_struct->ring_mask;
599 cons = RING_CMPL(ring_mask, raw_cons);
600 txcmp = (struct tx_cmpl *)&cp_desc_ring[cons];
602 opaque = rte_cpu_to_le_32(txcmp->opaque);
603 raw_cons = NEXT_RAW_CMP(raw_cons);
605 if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
606 nb_tx_pkts += opaque;
607 else if (CMP_TYPE(txcmp) == HWRM_CMPL_TYPE_HWRM_DONE)
609 } while (nb_tx_pkts < ring_mask);
612 cpr->cp_raw_cons = raw_cons;