1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2018 Broadcom
8 #include <rte_byteorder.h>
9 #include <rte_malloc.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_cp_ring_info *nqr;
61 struct bnxt_tx_ring_info *txr;
62 struct bnxt_ring *ring;
64 txr = rte_zmalloc_socket("bnxt_tx_ring",
65 sizeof(struct bnxt_tx_ring_info),
66 RTE_CACHE_LINE_SIZE, socket_id);
71 ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
72 sizeof(struct bnxt_ring),
73 RTE_CACHE_LINE_SIZE, socket_id);
76 txr->tx_ring_struct = ring;
77 ring->ring_size = rte_align32pow2(txq->nb_tx_desc);
78 ring->ring_mask = ring->ring_size - 1;
79 ring->bd = (void *)txr->tx_desc_ring;
80 ring->bd_dma = txr->tx_desc_mapping;
81 ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_tx_bd);
82 ring->vmem = (void **)&txr->tx_buf_ring;
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;
104 if (BNXT_HAS_NQ(txq->bp)) {
105 nqr = rte_zmalloc_socket("bnxt_tx_ring_nq",
106 sizeof(struct bnxt_cp_ring_info),
107 RTE_CACHE_LINE_SIZE, socket_id);
113 ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
114 sizeof(struct bnxt_ring),
115 RTE_CACHE_LINE_SIZE, socket_id);
119 nqr->cp_ring_struct = ring;
120 ring->ring_size = txr->tx_ring_struct->ring_size;
121 ring->ring_mask = ring->ring_size - 1;
122 ring->bd = (void *)nqr->cp_desc_ring;
123 ring->bd_dma = nqr->cp_desc_mapping;
131 static uint16_t bnxt_start_xmit(struct rte_mbuf *tx_pkt,
132 struct bnxt_tx_queue *txq,
134 struct tx_bd_long **last_txbd)
136 struct bnxt_tx_ring_info *txr = txq->tx_ring;
137 struct tx_bd_long *txbd;
138 struct tx_bd_long_hi *txbd1 = NULL;
139 uint32_t vlan_tag_flags, cfa_action;
140 bool long_bd = false;
141 unsigned short nr_bds = 0;
142 struct rte_mbuf *m_seg;
143 struct bnxt_sw_tx_bd *tx_buf;
144 static const uint32_t lhint_arr[4] = {
145 TX_BD_LONG_FLAGS_LHINT_LT512,
146 TX_BD_LONG_FLAGS_LHINT_LT1K,
147 TX_BD_LONG_FLAGS_LHINT_LT2K,
148 TX_BD_LONG_FLAGS_LHINT_LT2K
151 if (unlikely(is_bnxt_in_error(txq->bp)))
154 if (tx_pkt->ol_flags & (PKT_TX_TCP_SEG | PKT_TX_TCP_CKSUM |
155 PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM |
156 PKT_TX_VLAN_PKT | PKT_TX_OUTER_IP_CKSUM |
157 PKT_TX_TUNNEL_GRE | PKT_TX_TUNNEL_VXLAN |
158 PKT_TX_TUNNEL_GENEVE))
161 nr_bds = long_bd + tx_pkt->nb_segs;
162 if (unlikely(bnxt_tx_avail(txq) < nr_bds))
165 /* Check if number of Tx descriptors is above HW limit */
166 if (unlikely(nr_bds > BNXT_MAX_TSO_SEGS)) {
168 "Num descriptors %d exceeds HW limit\n", nr_bds);
172 /* If packet length is less than minimum packet size, pad it */
173 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) < BNXT_MIN_PKT_SIZE)) {
174 uint8_t pad = BNXT_MIN_PKT_SIZE - rte_pktmbuf_pkt_len(tx_pkt);
175 char *seg = rte_pktmbuf_append(tx_pkt, pad);
179 "Failed to pad mbuf by %d bytes\n",
184 /* Note: data_len, pkt len are updated in rte_pktmbuf_append */
188 /* Check non zero data_len */
189 RTE_VERIFY(tx_pkt->data_len);
191 tx_buf = &txr->tx_buf_ring[txr->tx_prod];
192 tx_buf->mbuf = tx_pkt;
193 tx_buf->nr_bds = nr_bds;
195 txbd = &txr->tx_desc_ring[txr->tx_prod];
196 txbd->opaque = *coal_pkts;
197 txbd->flags_type = nr_bds << TX_BD_LONG_FLAGS_BD_CNT_SFT;
198 txbd->flags_type |= TX_BD_SHORT_FLAGS_COAL_NOW;
199 txbd->flags_type |= TX_BD_LONG_FLAGS_NO_CMPL;
200 txbd->len = tx_pkt->data_len;
201 if (tx_pkt->pkt_len >= 2014)
202 txbd->flags_type |= TX_BD_LONG_FLAGS_LHINT_GTE2K;
204 txbd->flags_type |= lhint_arr[tx_pkt->pkt_len >> 9];
205 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(tx_buf->mbuf));
209 txbd->flags_type |= TX_BD_LONG_TYPE_TX_BD_LONG;
212 if (tx_buf->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
213 /* shurd: Should this mask at
214 * TX_BD_LONG_CFA_META_VLAN_VID_MASK?
216 vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
217 tx_buf->mbuf->vlan_tci;
218 /* Currently supports 8021Q, 8021AD vlan offloads
219 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
221 /* DPDK only supports 802.11q VLAN packets */
223 TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
226 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
228 txbd1 = (struct tx_bd_long_hi *)
229 &txr->tx_desc_ring[txr->tx_prod];
231 txbd1->cfa_meta = vlan_tag_flags;
232 txbd1->cfa_action = cfa_action;
234 if (tx_pkt->ol_flags & PKT_TX_TCP_SEG) {
238 txbd1->lflags |= TX_BD_LONG_LFLAGS_LSO |
239 TX_BD_LONG_LFLAGS_T_IPID;
240 hdr_size = tx_pkt->l2_len + tx_pkt->l3_len +
242 hdr_size += (tx_pkt->ol_flags & PKT_TX_TUNNEL_MASK) ?
243 tx_pkt->outer_l2_len +
244 tx_pkt->outer_l3_len : 0;
245 /* The hdr_size is multiple of 16bit units not 8bit.
248 txbd1->hdr_size = hdr_size >> 1;
249 txbd1->mss = tx_pkt->tso_segsz;
250 RTE_VERIFY(txbd1->mss);
252 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_UDP_CKSUM) ==
253 PKT_TX_OIP_IIP_TCP_UDP_CKSUM) {
254 /* Outer IP, Inner IP, Inner TCP/UDP CSO */
255 txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
257 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_CKSUM) ==
258 PKT_TX_OIP_IIP_TCP_CKSUM) {
259 /* Outer IP, Inner IP, Inner TCP/UDP CSO */
260 txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
262 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_UDP_CKSUM) ==
263 PKT_TX_OIP_IIP_UDP_CKSUM) {
264 /* Outer IP, Inner IP, Inner TCP/UDP CSO */
265 txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
267 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_UDP_CKSUM) ==
268 PKT_TX_IIP_TCP_UDP_CKSUM) {
269 /* (Inner) IP, (Inner) TCP/UDP CSO */
270 txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
272 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_UDP_CKSUM) ==
273 PKT_TX_IIP_UDP_CKSUM) {
274 /* (Inner) IP, (Inner) TCP/UDP CSO */
275 txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
277 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_CKSUM) ==
278 PKT_TX_IIP_TCP_CKSUM) {
279 /* (Inner) IP, (Inner) TCP/UDP CSO */
280 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;
287 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_UDP_CKSUM) ==
288 PKT_TX_OIP_UDP_CKSUM) {
289 /* Outer IP, (Inner) TCP/UDP CSO */
290 txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
292 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_CKSUM) ==
293 PKT_TX_OIP_TCP_CKSUM) {
294 /* Outer IP, (Inner) TCP/UDP CSO */
295 txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
297 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_CKSUM) ==
298 PKT_TX_OIP_IIP_CKSUM) {
299 /* Outer IP, Inner IP CSO */
300 txbd1->lflags |= TX_BD_FLG_TIP_IP_CHKSUM;
302 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_UDP_CKSUM) ==
303 PKT_TX_TCP_UDP_CKSUM) {
305 txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
307 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_CKSUM) ==
310 txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
312 } else if ((tx_pkt->ol_flags & PKT_TX_UDP_CKSUM) ==
315 txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
317 } else if ((tx_pkt->ol_flags & PKT_TX_IP_CKSUM) ==
320 txbd1->lflags |= TX_BD_LONG_LFLAGS_IP_CHKSUM;
322 } else if ((tx_pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM) ==
323 PKT_TX_OUTER_IP_CKSUM) {
325 txbd1->lflags |= TX_BD_LONG_LFLAGS_T_IP_CHKSUM;
329 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
332 m_seg = tx_pkt->next;
334 /* Check non zero data_len */
335 RTE_VERIFY(m_seg->data_len);
336 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
337 tx_buf = &txr->tx_buf_ring[txr->tx_prod];
338 tx_buf->mbuf = m_seg;
340 txbd = &txr->tx_desc_ring[txr->tx_prod];
341 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(m_seg));
342 txbd->flags_type = TX_BD_SHORT_TYPE_TX_BD_SHORT;
343 txbd->len = m_seg->data_len;
348 txbd->flags_type |= TX_BD_LONG_FLAGS_PACKET_END;
350 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
355 static void bnxt_tx_cmp(struct bnxt_tx_queue *txq, int nr_pkts)
357 struct bnxt_tx_ring_info *txr = txq->tx_ring;
358 struct rte_mempool *pool = NULL;
359 struct rte_mbuf **free = txq->free;
360 uint16_t cons = txr->tx_cons;
361 unsigned int blk = 0;
364 for (i = 0; i < nr_pkts; i++) {
365 struct rte_mbuf *mbuf;
366 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[cons];
367 unsigned short nr_bds = tx_buf->nr_bds;
369 for (j = 0; j < nr_bds; j++) {
372 cons = RING_NEXT(txr->tx_ring_struct, cons);
373 tx_buf = &txr->tx_buf_ring[cons];
374 if (!mbuf) /* long_bd's tx_buf ? */
377 mbuf = rte_pktmbuf_prefree_seg(mbuf);
381 /* EW - no need to unmap DMA memory? */
383 if (likely(mbuf->pool == pool)) {
384 /* Add mbuf to the bulk free array */
387 /* Found an mbuf from a different pool. Free
388 * mbufs accumulated so far to the previous
391 if (likely(pool != NULL))
392 rte_mempool_put_bulk(pool,
396 /* Start accumulating mbufs in a new pool */
404 rte_mempool_put_bulk(pool, (void *)free, blk);
409 static int bnxt_handle_tx_cp(struct bnxt_tx_queue *txq)
411 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
412 uint32_t raw_cons = cpr->cp_raw_cons;
414 uint32_t nb_tx_pkts = 0;
415 struct tx_cmpl *txcmp;
416 struct cmpl_base *cp_desc_ring = cpr->cp_desc_ring;
417 struct bnxt_ring *cp_ring_struct = cpr->cp_ring_struct;
418 uint32_t ring_mask = cp_ring_struct->ring_mask;
421 if (bnxt_tx_bds_in_hw(txq) < txq->tx_free_thresh)
425 cons = RING_CMPL(ring_mask, raw_cons);
426 txcmp = (struct tx_cmpl *)&cpr->cp_desc_ring[cons];
427 rte_prefetch_non_temporal(&cp_desc_ring[(cons + 2) &
430 if (!CMPL_VALID(txcmp, cpr->valid))
432 opaque = rte_cpu_to_le_32(txcmp->opaque);
433 NEXT_CMPL(cpr, cons, cpr->valid, 1);
434 rte_prefetch0(&cp_desc_ring[cons]);
436 if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
437 nb_tx_pkts += opaque;
440 "Unhandled CMP type %02x\n",
443 } while (nb_tx_pkts < ring_mask);
446 bnxt_tx_cmp(txq, nb_tx_pkts);
447 cpr->cp_raw_cons = raw_cons;
454 uint16_t bnxt_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
458 uint16_t nb_tx_pkts = 0;
459 uint16_t coal_pkts = 0;
460 struct bnxt_tx_queue *txq = tx_queue;
461 struct tx_bd_long *last_txbd = NULL;
463 /* Handle TX completions */
464 bnxt_handle_tx_cp(txq);
466 /* Tx queue was stopped; wait for it to be restarted */
467 if (txq->tx_deferred_start) {
468 PMD_DRV_LOG(DEBUG, "Tx q stopped;return\n");
472 /* Handle TX burst request */
473 for (nb_tx_pkts = 0; nb_tx_pkts < nb_pkts; nb_tx_pkts++) {
475 rc = bnxt_start_xmit(tx_pkts[nb_tx_pkts], txq,
476 &coal_pkts, &last_txbd);
482 if (likely(nb_tx_pkts)) {
483 /* Request a completion on the last packet */
484 last_txbd->flags_type &= ~TX_BD_LONG_FLAGS_NO_CMPL;
485 bnxt_db_write(&txq->tx_ring->tx_db, txq->tx_ring->tx_prod);
492 * Dummy DPDK callback for TX.
494 * This function is used to temporarily replace the real callback during
495 * unsafe control operations on the queue, or in case of error.
498 bnxt_dummy_xmit_pkts(void *tx_queue __rte_unused,
499 struct rte_mbuf **tx_pkts __rte_unused,
500 uint16_t nb_pkts __rte_unused)
505 int bnxt_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
507 struct bnxt *bp = dev->data->dev_private;
508 struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
511 rc = is_bnxt_in_error(bp);
515 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
516 txq->tx_deferred_start = false;
517 PMD_DRV_LOG(DEBUG, "Tx queue started\n");
522 int bnxt_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
524 struct bnxt *bp = dev->data->dev_private;
525 struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
528 rc = is_bnxt_in_error(bp);
532 /* Handle TX completions */
533 bnxt_handle_tx_cp(txq);
535 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
536 txq->tx_deferred_start = true;
537 PMD_DRV_LOG(DEBUG, "Tx queue stopped\n");