1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright (c) 2013-2015 Brocade Communications Systems, Inc.
3 * Copyright (c) 2015-2018 Cavium Inc.
9 #include "bnx2x_rxtx.h"
11 static const struct rte_memzone *
12 ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,
13 uint16_t queue_id, uint32_t ring_size, int socket_id)
15 return rte_eth_dma_zone_reserve(dev, ring_name, queue_id,
16 ring_size, BNX2X_PAGE_SIZE, socket_id);
20 bnx2x_rx_queue_release(struct bnx2x_rx_queue *rx_queue)
23 struct rte_mbuf **sw_ring;
25 if (NULL != rx_queue) {
27 sw_ring = rx_queue->sw_ring;
28 if (NULL != sw_ring) {
29 for (i = 0; i < rx_queue->nb_rx_desc; i++) {
30 if (NULL != sw_ring[i])
31 rte_pktmbuf_free(sw_ring[i]);
40 bnx2x_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t queue_idx)
42 bnx2x_rx_queue_release(dev->data->rx_queues[queue_idx]);
46 bnx2x_dev_rx_queue_setup(struct rte_eth_dev *dev,
49 unsigned int socket_id,
50 __rte_unused const struct rte_eth_rxconf *rx_conf,
51 struct rte_mempool *mp)
54 const struct rte_memzone *dma;
55 struct bnx2x_rx_queue *rxq;
57 struct rte_mbuf *mbuf;
58 struct bnx2x_softc *sc = dev->data->dev_private;
59 struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
60 struct eth_rx_cqe_next_page *nextpg;
64 /* First allocate the rx queue data structure */
65 rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct bnx2x_rx_queue),
66 RTE_CACHE_LINE_SIZE, socket_id);
68 PMD_DRV_LOG(ERR, sc, "rte_zmalloc for rxq failed!");
73 rxq->queue_id = queue_idx;
74 rxq->port_id = dev->data->port_id;
77 while (USABLE_RX_BD(rxq) < nb_desc)
78 rxq->nb_rx_pages <<= 1;
80 rxq->nb_rx_desc = TOTAL_RX_BD(rxq);
81 sc->rx_ring_size = USABLE_RX_BD(rxq);
82 rxq->nb_cq_pages = RCQ_BD_PAGES(rxq);
84 PMD_DRV_LOG(DEBUG, sc, "fp[%02d] req_bd=%u, usable_bd=%lu, "
85 "total_bd=%lu, rx_pages=%u, cq_pages=%u",
86 queue_idx, nb_desc, (unsigned long)USABLE_RX_BD(rxq),
87 (unsigned long)TOTAL_RX_BD(rxq), rxq->nb_rx_pages,
90 /* Allocate RX ring hardware descriptors */
91 dma_size = rxq->nb_rx_desc * sizeof(struct eth_rx_bd);
92 dma = ring_dma_zone_reserve(dev, "hw_ring", queue_idx, dma_size, socket_id);
94 PMD_RX_LOG(ERR, "ring_dma_zone_reserve for rx_ring failed!");
95 bnx2x_rx_queue_release(rxq);
98 fp->rx_desc_mapping = rxq->rx_ring_phys_addr = (uint64_t)dma->iova;
99 rxq->rx_ring = (uint64_t*)dma->addr;
100 memset((void *)rxq->rx_ring, 0, dma_size);
102 /* Link the RX chain pages. */
103 for (j = 1; j <= rxq->nb_rx_pages; j++) {
104 rx_bd = &rxq->rx_ring[TOTAL_RX_BD_PER_PAGE * j - 2];
105 busaddr = rxq->rx_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_rx_pages);
109 /* Allocate software ring */
110 dma_size = rxq->nb_rx_desc * sizeof(struct bnx2x_rx_entry);
111 rxq->sw_ring = rte_zmalloc_socket("sw_ring", dma_size,
114 if (NULL == rxq->sw_ring) {
115 PMD_RX_LOG(ERR, "rte_zmalloc for sw_ring failed!");
116 bnx2x_rx_queue_release(rxq);
120 /* Initialize software ring entries */
121 for (idx = 0; idx < rxq->nb_rx_desc; idx = NEXT_RX_BD(idx)) {
122 mbuf = rte_mbuf_raw_alloc(mp);
124 PMD_RX_LOG(ERR, "RX mbuf alloc failed queue_id=%u, idx=%d",
125 (unsigned)rxq->queue_id, idx);
126 bnx2x_rx_queue_release(rxq);
129 rxq->sw_ring[idx] = mbuf;
131 rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
133 rxq->pkt_first_seg = NULL;
134 rxq->pkt_last_seg = NULL;
136 rxq->rx_bd_tail = rxq->nb_rx_desc;
138 /* Allocate CQ chain. */
139 dma_size = BNX2X_RX_CHAIN_PAGE_SZ * rxq->nb_cq_pages;
140 dma = ring_dma_zone_reserve(dev, "bnx2x_rcq", queue_idx, dma_size, socket_id);
142 PMD_RX_LOG(ERR, "RCQ alloc failed");
145 fp->rx_comp_mapping = rxq->cq_ring_phys_addr = (uint64_t)dma->iova;
146 rxq->cq_ring = (union eth_rx_cqe*)dma->addr;
148 /* Link the CQ chain pages. */
149 for (j = 1; j <= rxq->nb_cq_pages; j++) {
150 nextpg = &rxq->cq_ring[TOTAL_RCQ_ENTRIES_PER_PAGE * j - 1].next_page_cqe;
151 busaddr = rxq->cq_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_cq_pages);
152 nextpg->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
153 nextpg->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
156 rxq->rx_cq_tail = TOTAL_RCQ_ENTRIES(rxq);
158 dev->data->rx_queues[queue_idx] = rxq;
159 if (!sc->rx_queues) sc->rx_queues = dev->data->rx_queues;
165 bnx2x_tx_queue_release(struct bnx2x_tx_queue *tx_queue)
168 struct rte_mbuf **sw_ring;
170 if (NULL != tx_queue) {
172 sw_ring = tx_queue->sw_ring;
173 if (NULL != sw_ring) {
174 for (i = 0; i < tx_queue->nb_tx_desc; i++) {
175 if (NULL != sw_ring[i])
176 rte_pktmbuf_free(sw_ring[i]);
185 bnx2x_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t queue_idx)
187 bnx2x_tx_queue_release(dev->data->tx_queues[queue_idx]);
191 bnx2x_xmit_pkts(void *p_txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
193 struct bnx2x_tx_queue *txq;
194 struct bnx2x_softc *sc;
195 struct bnx2x_fastpath *fp;
197 uint16_t nb_pkt_sent = 0;
202 fp = &sc->fp[txq->queue_id];
204 if ((unlikely((txq->nb_tx_desc - txq->nb_tx_avail) >
205 txq->tx_free_thresh)))
208 nb_tx_pkts = RTE_MIN(nb_pkts, txq->nb_tx_avail / BDS_PER_TX_PKT);
209 if (unlikely(nb_tx_pkts == 0))
212 while (nb_tx_pkts--) {
213 struct rte_mbuf *m = *tx_pkts++;
215 ret = bnx2x_tx_encap(txq, m);
216 fp->tx_db.data.prod += ret;
220 bnx2x_update_fp_sb_idx(fp);
222 DOORBELL(sc, txq->queue_id, fp->tx_db.raw);
225 if ((txq->nb_tx_desc - txq->nb_tx_avail) >
233 bnx2x_dev_tx_queue_setup(struct rte_eth_dev *dev,
236 unsigned int socket_id,
237 const struct rte_eth_txconf *tx_conf)
241 const struct rte_memzone *tz;
242 struct bnx2x_tx_queue *txq;
243 struct eth_tx_next_bd *tx_n_bd;
245 struct bnx2x_softc *sc = dev->data->dev_private;
246 struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
248 /* First allocate the tx queue data structure */
249 txq = rte_zmalloc("ethdev TX queue", sizeof(struct bnx2x_tx_queue),
250 RTE_CACHE_LINE_SIZE);
255 txq->nb_tx_pages = 1;
256 while (USABLE_TX_BD(txq) < nb_desc)
257 txq->nb_tx_pages <<= 1;
259 txq->nb_tx_desc = TOTAL_TX_BD(txq);
260 sc->tx_ring_size = TOTAL_TX_BD(txq);
262 txq->tx_free_thresh = tx_conf->tx_free_thresh ?
263 tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH;
264 txq->tx_free_thresh = min(txq->tx_free_thresh,
265 txq->nb_tx_desc - BDS_PER_TX_PKT);
267 PMD_DRV_LOG(DEBUG, sc, "fp[%02d] req_bd=%u, thresh=%u, usable_bd=%lu, "
268 "total_bd=%lu, tx_pages=%u",
269 queue_idx, nb_desc, txq->tx_free_thresh,
270 (unsigned long)USABLE_TX_BD(txq),
271 (unsigned long)TOTAL_TX_BD(txq), txq->nb_tx_pages);
273 /* Allocate TX ring hardware descriptors */
274 tsize = txq->nb_tx_desc * sizeof(union eth_tx_bd_types);
275 tz = ring_dma_zone_reserve(dev, "tx_hw_ring", queue_idx, tsize, socket_id);
277 bnx2x_tx_queue_release(txq);
280 fp->tx_desc_mapping = txq->tx_ring_phys_addr = (uint64_t)tz->iova;
281 txq->tx_ring = (union eth_tx_bd_types *) tz->addr;
282 memset(txq->tx_ring, 0, tsize);
284 /* Allocate software ring */
285 tsize = txq->nb_tx_desc * sizeof(struct rte_mbuf *);
286 txq->sw_ring = rte_zmalloc("tx_sw_ring", tsize,
287 RTE_CACHE_LINE_SIZE);
288 if (txq->sw_ring == NULL) {
289 bnx2x_tx_queue_release(txq);
293 /* PMD_DRV_LOG(DEBUG, sc, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
294 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr); */
297 for (i = 1; i <= txq->nb_tx_pages; i++) {
298 tx_n_bd = &txq->tx_ring[TOTAL_TX_BD_PER_PAGE * i - 1].next_bd;
299 busaddr = txq->tx_ring_phys_addr + BNX2X_PAGE_SIZE * (i % txq->nb_tx_pages);
300 tx_n_bd->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
301 tx_n_bd->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
302 /* PMD_DRV_LOG(DEBUG, sc, "link tx page %lu",
303 * (TOTAL_TX_BD_PER_PAGE * i - 1));
307 txq->queue_id = queue_idx;
308 txq->port_id = dev->data->port_id;
309 txq->tx_pkt_tail = 0;
310 txq->tx_pkt_head = 0;
313 txq->nb_tx_avail = txq->nb_tx_desc;
314 dev->data->tx_queues[queue_idx] = txq;
315 if (!sc->tx_queues) sc->tx_queues = dev->data->tx_queues;
321 bnx2x_upd_rx_prod_fast(struct bnx2x_softc *sc, struct bnx2x_fastpath *fp,
322 uint16_t rx_bd_prod, uint16_t rx_cq_prod)
325 struct ustorm_eth_rx_producers rx_prods;
329 val.rx_prods.bd_prod = rx_bd_prod;
330 val.rx_prods.cqe_prod = rx_cq_prod;
332 REG_WR(sc, fp->ustorm_rx_prods_offset, val.val);
336 bnx2x_recv_pkts(void *p_rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
338 struct bnx2x_rx_queue *rxq = p_rxq;
339 struct bnx2x_softc *sc = rxq->sc;
340 struct bnx2x_fastpath *fp = &sc->fp[rxq->queue_id];
342 uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
343 uint16_t bd_cons, bd_prod;
344 struct rte_mbuf *new_mb;
346 struct eth_fast_path_rx_cqe *cqe_fp;
347 uint16_t len, pad, bd_len, buf_len;
348 struct rte_mbuf *rx_mb = NULL;
349 static bool log_once = true;
351 rte_spinlock_lock(&(fp)->rx_mtx);
353 hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
354 if ((hw_cq_cons & USABLE_RCQ_ENTRIES_PER_PAGE) ==
355 USABLE_RCQ_ENTRIES_PER_PAGE) {
359 bd_cons = rxq->rx_bd_head;
360 bd_prod = rxq->rx_bd_tail;
361 sw_cq_cons = rxq->rx_cq_head;
362 sw_cq_prod = rxq->rx_cq_tail;
364 if (sw_cq_cons == hw_cq_cons) {
365 rte_spinlock_unlock(&(fp)->rx_mtx);
369 while (nb_rx < nb_pkts && sw_cq_cons != hw_cq_cons) {
371 bd_prod &= MAX_RX_BD(rxq);
372 bd_cons &= MAX_RX_BD(rxq);
374 cqe_fp = &rxq->cq_ring[sw_cq_cons & MAX_RX_BD(rxq)].fast_path_cqe;
376 if (unlikely(CQE_TYPE_SLOW(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_TYPE))) {
377 PMD_RX_LOG(ERR, "slowpath event during traffic processing");
381 if (unlikely(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
382 PMD_RX_LOG(ERR, "flags 0x%x rx packet %u",
383 cqe_fp->type_error_flags, sw_cq_cons);
387 len = cqe_fp->pkt_len_or_gro_seg_len;
388 pad = cqe_fp->placement_offset;
389 bd_len = cqe_fp->len_on_bd;
390 buf_len = rxq->sw_ring[bd_cons]->buf_len;
392 /* Check for sufficient buffer length */
393 if (unlikely(buf_len < len + (pad + RTE_PKTMBUF_HEADROOM))) {
394 if (unlikely(log_once)) {
395 PMD_DRV_LOG(ERR, sc, "mbuf size %d is not enough to hold Rx packet length more than %d",
396 buf_len - RTE_PKTMBUF_HEADROOM,
398 (pad + RTE_PKTMBUF_HEADROOM));
404 new_mb = rte_mbuf_raw_alloc(rxq->mb_pool);
405 if (unlikely(!new_mb)) {
406 PMD_RX_LOG(ERR, "mbuf alloc fail fp[%02d]", fp->index);
407 rte_eth_devices[rxq->port_id].data->
408 rx_mbuf_alloc_failed++;
412 rx_mb = rxq->sw_ring[bd_cons];
413 rxq->sw_ring[bd_cons] = new_mb;
414 rxq->rx_ring[bd_prod] =
415 rte_cpu_to_le_64(rte_mbuf_data_iova_default(new_mb));
417 rx_pref = NEXT_RX_BD(bd_cons) & MAX_RX_BD(rxq);
418 rte_prefetch0(rxq->sw_ring[rx_pref]);
419 if ((rx_pref & 0x3) == 0) {
420 rte_prefetch0(&rxq->rx_ring[rx_pref]);
421 rte_prefetch0(&rxq->sw_ring[rx_pref]);
424 rx_mb->data_off = pad + RTE_PKTMBUF_HEADROOM;
427 rx_mb->pkt_len = len;
428 rx_mb->data_len = bd_len;
429 rx_mb->port = rxq->port_id;
430 rte_prefetch1(rte_pktmbuf_mtod(rx_mb, void *));
433 * If we received a packet with a vlan tag,
434 * attach that information to the packet.
436 if (cqe_fp->pars_flags.flags & PARSING_FLAGS_VLAN) {
437 rx_mb->vlan_tci = cqe_fp->vlan_tag;
438 rx_mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
441 rx_pkts[nb_rx] = rx_mb;
444 /* limit spinning on the queue */
445 if (unlikely(nb_rx == sc->rx_budget)) {
446 PMD_RX_LOG(ERR, "Limit spinning on the queue");
451 bd_cons = NEXT_RX_BD(bd_cons);
452 bd_prod = NEXT_RX_BD(bd_prod);
453 sw_cq_prod = NEXT_RCQ_IDX(sw_cq_prod);
454 sw_cq_cons = NEXT_RCQ_IDX(sw_cq_cons);
456 rxq->rx_bd_head = bd_cons;
457 rxq->rx_bd_tail = bd_prod;
458 rxq->rx_cq_head = sw_cq_cons;
459 rxq->rx_cq_tail = sw_cq_prod;
461 bnx2x_upd_rx_prod_fast(sc, fp, bd_prod, sw_cq_prod);
463 rte_spinlock_unlock(&(fp)->rx_mtx);
469 bnx2x_rxtx_pkts_dummy(__rte_unused void *p_rxq,
470 __rte_unused struct rte_mbuf **rx_pkts,
471 __rte_unused uint16_t nb_pkts)
476 void bnx2x_dev_rxtx_init_dummy(struct rte_eth_dev *dev)
478 dev->rx_pkt_burst = bnx2x_rxtx_pkts_dummy;
479 dev->tx_pkt_burst = bnx2x_rxtx_pkts_dummy;
482 void bnx2x_dev_rxtx_init(struct rte_eth_dev *dev)
484 dev->rx_pkt_burst = bnx2x_recv_pkts;
485 dev->tx_pkt_burst = bnx2x_xmit_pkts;
489 bnx2x_dev_clear_queues(struct rte_eth_dev *dev)
491 struct bnx2x_softc *sc = dev->data->dev_private;
494 PMD_INIT_FUNC_TRACE(sc);
496 for (i = 0; i < dev->data->nb_tx_queues; i++) {
497 struct bnx2x_tx_queue *txq = dev->data->tx_queues[i];
499 bnx2x_tx_queue_release(txq);
500 dev->data->tx_queues[i] = NULL;
504 for (i = 0; i < dev->data->nb_rx_queues; i++) {
505 struct bnx2x_rx_queue *rxq = dev->data->rx_queues[i];
507 bnx2x_rx_queue_release(rxq);
508 dev->data->rx_queues[i] = NULL;