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 rte_pktmbuf_free(sw_ring[i]);
39 bnx2x_dev_rx_queue_release(struct rte_eth_dev *dev, uint16_t queue_idx)
41 bnx2x_rx_queue_release(dev->data->rx_queues[queue_idx]);
45 bnx2x_dev_rx_queue_setup(struct rte_eth_dev *dev,
48 unsigned int socket_id,
49 __rte_unused const struct rte_eth_rxconf *rx_conf,
50 struct rte_mempool *mp)
53 const struct rte_memzone *dma;
54 struct bnx2x_rx_queue *rxq;
56 struct rte_mbuf *mbuf;
57 struct bnx2x_softc *sc = dev->data->dev_private;
58 struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
59 struct eth_rx_cqe_next_page *nextpg;
63 /* First allocate the rx queue data structure */
64 rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct bnx2x_rx_queue),
65 RTE_CACHE_LINE_SIZE, socket_id);
67 PMD_DRV_LOG(ERR, sc, "rte_zmalloc for rxq failed!");
72 rxq->queue_id = queue_idx;
73 rxq->port_id = dev->data->port_id;
76 while (USABLE_RX_BD(rxq) < nb_desc)
77 rxq->nb_rx_pages <<= 1;
79 rxq->nb_rx_desc = TOTAL_RX_BD(rxq);
80 sc->rx_ring_size = USABLE_RX_BD(rxq);
81 rxq->nb_cq_pages = RCQ_BD_PAGES(rxq);
83 PMD_DRV_LOG(DEBUG, sc, "fp[%02d] req_bd=%u, usable_bd=%lu, "
84 "total_bd=%lu, rx_pages=%u, cq_pages=%u",
85 queue_idx, nb_desc, (unsigned long)USABLE_RX_BD(rxq),
86 (unsigned long)TOTAL_RX_BD(rxq), rxq->nb_rx_pages,
89 /* Allocate RX ring hardware descriptors */
90 dma_size = rxq->nb_rx_desc * sizeof(struct eth_rx_bd);
91 dma = ring_dma_zone_reserve(dev, "hw_ring", queue_idx, dma_size, socket_id);
93 PMD_RX_LOG(ERR, "ring_dma_zone_reserve for rx_ring failed!");
94 bnx2x_rx_queue_release(rxq);
97 fp->rx_desc_mapping = rxq->rx_ring_phys_addr = (uint64_t)dma->iova;
98 rxq->rx_ring = (uint64_t*)dma->addr;
99 memset((void *)rxq->rx_ring, 0, dma_size);
101 /* Link the RX chain pages. */
102 for (j = 1; j <= rxq->nb_rx_pages; j++) {
103 rx_bd = &rxq->rx_ring[TOTAL_RX_BD_PER_PAGE * j - 2];
104 busaddr = rxq->rx_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_rx_pages);
108 /* Allocate software ring */
109 dma_size = rxq->nb_rx_desc * sizeof(struct bnx2x_rx_entry);
110 rxq->sw_ring = rte_zmalloc_socket("sw_ring", dma_size,
113 if (NULL == rxq->sw_ring) {
114 PMD_RX_LOG(ERR, "rte_zmalloc for sw_ring failed!");
115 bnx2x_rx_queue_release(rxq);
119 /* Initialize software ring entries */
120 for (idx = 0; idx < rxq->nb_rx_desc; idx = NEXT_RX_BD(idx)) {
121 mbuf = rte_mbuf_raw_alloc(mp);
123 PMD_RX_LOG(ERR, "RX mbuf alloc failed queue_id=%u, idx=%d",
124 (unsigned)rxq->queue_id, idx);
125 bnx2x_rx_queue_release(rxq);
128 rxq->sw_ring[idx] = mbuf;
130 rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
132 rxq->pkt_first_seg = NULL;
133 rxq->pkt_last_seg = NULL;
135 rxq->rx_bd_tail = rxq->nb_rx_desc;
137 /* Allocate CQ chain. */
138 dma_size = BNX2X_RX_CHAIN_PAGE_SZ * rxq->nb_cq_pages;
139 dma = ring_dma_zone_reserve(dev, "bnx2x_rcq", queue_idx, dma_size, socket_id);
141 PMD_RX_LOG(ERR, "RCQ alloc failed");
144 fp->rx_comp_mapping = rxq->cq_ring_phys_addr = (uint64_t)dma->iova;
145 rxq->cq_ring = (union eth_rx_cqe*)dma->addr;
147 /* Link the CQ chain pages. */
148 for (j = 1; j <= rxq->nb_cq_pages; j++) {
149 nextpg = &rxq->cq_ring[TOTAL_RCQ_ENTRIES_PER_PAGE * j - 1].next_page_cqe;
150 busaddr = rxq->cq_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_cq_pages);
151 nextpg->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
152 nextpg->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
155 rxq->rx_cq_tail = TOTAL_RCQ_ENTRIES(rxq);
157 dev->data->rx_queues[queue_idx] = rxq;
158 if (!sc->rx_queues) sc->rx_queues = dev->data->rx_queues;
164 bnx2x_tx_queue_release(struct bnx2x_tx_queue *tx_queue)
167 struct rte_mbuf **sw_ring;
169 if (NULL != tx_queue) {
171 sw_ring = tx_queue->sw_ring;
172 if (NULL != sw_ring) {
173 for (i = 0; i < tx_queue->nb_tx_desc; i++) {
174 rte_pktmbuf_free(sw_ring[i]);
183 bnx2x_dev_tx_queue_release(struct rte_eth_dev *dev, uint16_t queue_idx)
185 bnx2x_tx_queue_release(dev->data->tx_queues[queue_idx]);
189 bnx2x_xmit_pkts(void *p_txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
191 struct bnx2x_tx_queue *txq;
192 struct bnx2x_softc *sc;
193 struct bnx2x_fastpath *fp;
195 uint16_t nb_pkt_sent = 0;
200 fp = &sc->fp[txq->queue_id];
202 if ((unlikely((txq->nb_tx_desc - txq->nb_tx_avail) >
203 txq->tx_free_thresh)))
206 nb_tx_pkts = RTE_MIN(nb_pkts, txq->nb_tx_avail / BDS_PER_TX_PKT);
207 if (unlikely(nb_tx_pkts == 0))
210 while (nb_tx_pkts--) {
211 struct rte_mbuf *m = *tx_pkts++;
213 ret = bnx2x_tx_encap(txq, m);
214 fp->tx_db.data.prod += ret;
218 bnx2x_update_fp_sb_idx(fp);
220 DOORBELL(sc, txq->queue_id, fp->tx_db.raw);
223 if ((txq->nb_tx_desc - txq->nb_tx_avail) >
231 bnx2x_dev_tx_queue_setup(struct rte_eth_dev *dev,
234 unsigned int socket_id,
235 const struct rte_eth_txconf *tx_conf)
239 const struct rte_memzone *tz;
240 struct bnx2x_tx_queue *txq;
241 struct eth_tx_next_bd *tx_n_bd;
243 struct bnx2x_softc *sc = dev->data->dev_private;
244 struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
246 /* First allocate the tx queue data structure */
247 txq = rte_zmalloc("ethdev TX queue", sizeof(struct bnx2x_tx_queue),
248 RTE_CACHE_LINE_SIZE);
253 txq->nb_tx_pages = 1;
254 while (USABLE_TX_BD(txq) < nb_desc)
255 txq->nb_tx_pages <<= 1;
257 txq->nb_tx_desc = TOTAL_TX_BD(txq);
258 sc->tx_ring_size = TOTAL_TX_BD(txq);
260 txq->tx_free_thresh = tx_conf->tx_free_thresh ?
261 tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH;
262 txq->tx_free_thresh = min(txq->tx_free_thresh,
263 txq->nb_tx_desc - BDS_PER_TX_PKT);
265 PMD_DRV_LOG(DEBUG, sc, "fp[%02d] req_bd=%u, thresh=%u, usable_bd=%lu, "
266 "total_bd=%lu, tx_pages=%u",
267 queue_idx, nb_desc, txq->tx_free_thresh,
268 (unsigned long)USABLE_TX_BD(txq),
269 (unsigned long)TOTAL_TX_BD(txq), txq->nb_tx_pages);
271 /* Allocate TX ring hardware descriptors */
272 tsize = txq->nb_tx_desc * sizeof(union eth_tx_bd_types);
273 tz = ring_dma_zone_reserve(dev, "tx_hw_ring", queue_idx, tsize, socket_id);
275 bnx2x_tx_queue_release(txq);
278 fp->tx_desc_mapping = txq->tx_ring_phys_addr = (uint64_t)tz->iova;
279 txq->tx_ring = (union eth_tx_bd_types *) tz->addr;
280 memset(txq->tx_ring, 0, tsize);
282 /* Allocate software ring */
283 tsize = txq->nb_tx_desc * sizeof(struct rte_mbuf *);
284 txq->sw_ring = rte_zmalloc("tx_sw_ring", tsize,
285 RTE_CACHE_LINE_SIZE);
286 if (txq->sw_ring == NULL) {
287 bnx2x_tx_queue_release(txq);
291 /* PMD_DRV_LOG(DEBUG, sc, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
292 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr); */
295 for (i = 1; i <= txq->nb_tx_pages; i++) {
296 tx_n_bd = &txq->tx_ring[TOTAL_TX_BD_PER_PAGE * i - 1].next_bd;
297 busaddr = txq->tx_ring_phys_addr + BNX2X_PAGE_SIZE * (i % txq->nb_tx_pages);
298 tx_n_bd->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
299 tx_n_bd->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
300 /* PMD_DRV_LOG(DEBUG, sc, "link tx page %lu",
301 * (TOTAL_TX_BD_PER_PAGE * i - 1));
305 txq->queue_id = queue_idx;
306 txq->port_id = dev->data->port_id;
307 txq->tx_pkt_tail = 0;
308 txq->tx_pkt_head = 0;
311 txq->nb_tx_avail = txq->nb_tx_desc;
312 dev->data->tx_queues[queue_idx] = txq;
313 if (!sc->tx_queues) sc->tx_queues = dev->data->tx_queues;
319 bnx2x_upd_rx_prod_fast(struct bnx2x_softc *sc, struct bnx2x_fastpath *fp,
320 uint16_t rx_bd_prod, uint16_t rx_cq_prod)
323 struct ustorm_eth_rx_producers rx_prods;
327 val.rx_prods.bd_prod = rx_bd_prod;
328 val.rx_prods.cqe_prod = rx_cq_prod;
330 REG_WR(sc, fp->ustorm_rx_prods_offset, val.val);
334 bnx2x_recv_pkts(void *p_rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
336 struct bnx2x_rx_queue *rxq = p_rxq;
337 struct bnx2x_softc *sc = rxq->sc;
338 struct bnx2x_fastpath *fp = &sc->fp[rxq->queue_id];
340 uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
341 uint16_t bd_cons, bd_prod;
342 struct rte_mbuf *new_mb;
344 struct eth_fast_path_rx_cqe *cqe_fp;
345 uint16_t len, pad, bd_len, buf_len;
346 struct rte_mbuf *rx_mb = NULL;
347 static bool log_once = true;
349 rte_spinlock_lock(&(fp)->rx_mtx);
351 hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
352 if ((hw_cq_cons & USABLE_RCQ_ENTRIES_PER_PAGE) ==
353 USABLE_RCQ_ENTRIES_PER_PAGE) {
357 bd_cons = rxq->rx_bd_head;
358 bd_prod = rxq->rx_bd_tail;
359 sw_cq_cons = rxq->rx_cq_head;
360 sw_cq_prod = rxq->rx_cq_tail;
362 if (sw_cq_cons == hw_cq_cons) {
363 rte_spinlock_unlock(&(fp)->rx_mtx);
367 while (nb_rx < nb_pkts && sw_cq_cons != hw_cq_cons) {
369 bd_prod &= MAX_RX_BD(rxq);
370 bd_cons &= MAX_RX_BD(rxq);
372 cqe_fp = &rxq->cq_ring[sw_cq_cons & MAX_RX_BD(rxq)].fast_path_cqe;
374 if (unlikely(CQE_TYPE_SLOW(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_TYPE))) {
375 PMD_RX_LOG(ERR, "slowpath event during traffic processing");
379 if (unlikely(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
380 PMD_RX_LOG(ERR, "flags 0x%x rx packet %u",
381 cqe_fp->type_error_flags, sw_cq_cons);
385 len = cqe_fp->pkt_len_or_gro_seg_len;
386 pad = cqe_fp->placement_offset;
387 bd_len = cqe_fp->len_on_bd;
388 buf_len = rxq->sw_ring[bd_cons]->buf_len;
390 /* Check for sufficient buffer length */
391 if (unlikely(buf_len < len + (pad + RTE_PKTMBUF_HEADROOM))) {
392 if (unlikely(log_once)) {
393 PMD_DRV_LOG(ERR, sc, "mbuf size %d is not enough to hold Rx packet length more than %d",
394 buf_len - RTE_PKTMBUF_HEADROOM,
396 (pad + RTE_PKTMBUF_HEADROOM));
402 new_mb = rte_mbuf_raw_alloc(rxq->mb_pool);
403 if (unlikely(!new_mb)) {
404 PMD_RX_LOG(ERR, "mbuf alloc fail fp[%02d]", fp->index);
405 rte_eth_devices[rxq->port_id].data->
406 rx_mbuf_alloc_failed++;
410 rx_mb = rxq->sw_ring[bd_cons];
411 rxq->sw_ring[bd_cons] = new_mb;
412 rxq->rx_ring[bd_prod] =
413 rte_cpu_to_le_64(rte_mbuf_data_iova_default(new_mb));
415 rx_pref = NEXT_RX_BD(bd_cons) & MAX_RX_BD(rxq);
416 rte_prefetch0(rxq->sw_ring[rx_pref]);
417 if ((rx_pref & 0x3) == 0) {
418 rte_prefetch0(&rxq->rx_ring[rx_pref]);
419 rte_prefetch0(&rxq->sw_ring[rx_pref]);
422 rx_mb->data_off = pad + RTE_PKTMBUF_HEADROOM;
425 rx_mb->pkt_len = len;
426 rx_mb->data_len = bd_len;
427 rx_mb->port = rxq->port_id;
428 rte_prefetch1(rte_pktmbuf_mtod(rx_mb, void *));
431 * If we received a packet with a vlan tag,
432 * attach that information to the packet.
434 if (cqe_fp->pars_flags.flags & PARSING_FLAGS_VLAN) {
435 rx_mb->vlan_tci = cqe_fp->vlan_tag;
436 rx_mb->ol_flags |= RTE_MBUF_F_RX_VLAN | RTE_MBUF_F_RX_VLAN_STRIPPED;
439 rx_pkts[nb_rx] = rx_mb;
442 /* limit spinning on the queue */
443 if (unlikely(nb_rx == sc->rx_budget)) {
444 PMD_RX_LOG(ERR, "Limit spinning on the queue");
449 bd_cons = NEXT_RX_BD(bd_cons);
450 bd_prod = NEXT_RX_BD(bd_prod);
451 sw_cq_prod = NEXT_RCQ_IDX(sw_cq_prod);
452 sw_cq_cons = NEXT_RCQ_IDX(sw_cq_cons);
454 rxq->rx_bd_head = bd_cons;
455 rxq->rx_bd_tail = bd_prod;
456 rxq->rx_cq_head = sw_cq_cons;
457 rxq->rx_cq_tail = sw_cq_prod;
459 bnx2x_upd_rx_prod_fast(sc, fp, bd_prod, sw_cq_prod);
461 rte_spinlock_unlock(&(fp)->rx_mtx);
467 bnx2x_rxtx_pkts_dummy(__rte_unused void *p_rxq,
468 __rte_unused struct rte_mbuf **rx_pkts,
469 __rte_unused uint16_t nb_pkts)
474 void bnx2x_dev_rxtx_init_dummy(struct rte_eth_dev *dev)
476 dev->rx_pkt_burst = bnx2x_rxtx_pkts_dummy;
477 dev->tx_pkt_burst = bnx2x_rxtx_pkts_dummy;
480 void bnx2x_dev_rxtx_init(struct rte_eth_dev *dev)
482 dev->rx_pkt_burst = bnx2x_recv_pkts;
483 dev->tx_pkt_burst = bnx2x_xmit_pkts;
487 bnx2x_dev_clear_queues(struct rte_eth_dev *dev)
489 struct bnx2x_softc *sc = dev->data->dev_private;
492 PMD_INIT_FUNC_TRACE(sc);
494 for (i = 0; i < dev->data->nb_tx_queues; i++) {
495 struct bnx2x_tx_queue *txq = dev->data->tx_queues[i];
497 bnx2x_tx_queue_release(txq);
498 dev->data->tx_queues[i] = NULL;
502 for (i = 0; i < dev->data->nb_rx_queues; i++) {
503 struct bnx2x_rx_queue *rxq = dev->data->rx_queues[i];
505 bnx2x_rx_queue_release(rxq);
506 dev->data->rx_queues[i] = NULL;