2 * Copyright (c) 2013-2015 Brocade Communications Systems, Inc.
4 * Copyright (c) 2015 QLogic Corporation.
8 * See LICENSE.bnx2x_pmd for copyright and licensing details.
12 #include "bnx2x_rxtx.h"
14 static const struct rte_memzone *
15 ring_dma_zone_reserve(struct rte_eth_dev *dev, const char *ring_name,
16 uint16_t queue_id, uint32_t ring_size, int socket_id)
18 char z_name[RTE_MEMZONE_NAMESIZE];
19 const struct rte_memzone *mz;
21 snprintf(z_name, sizeof(z_name), "%s_%s_%d_%d",
22 dev->device->driver->name, ring_name,
23 dev->data->port_id, queue_id);
25 mz = rte_memzone_lookup(z_name);
29 return rte_memzone_reserve_aligned(z_name, ring_size, socket_id, 0, BNX2X_PAGE_SIZE);
33 bnx2x_rx_queue_release(struct bnx2x_rx_queue *rx_queue)
36 struct rte_mbuf **sw_ring;
38 if (NULL != rx_queue) {
40 sw_ring = rx_queue->sw_ring;
41 if (NULL != sw_ring) {
42 for (i = 0; i < rx_queue->nb_rx_desc; i++) {
43 if (NULL != sw_ring[i])
44 rte_pktmbuf_free(sw_ring[i]);
53 bnx2x_dev_rx_queue_release(void *rxq)
55 bnx2x_rx_queue_release(rxq);
59 bnx2x_dev_rx_queue_setup(struct rte_eth_dev *dev,
62 unsigned int socket_id,
63 __rte_unused const struct rte_eth_rxconf *rx_conf,
64 struct rte_mempool *mp)
67 const struct rte_memzone *dma;
68 struct bnx2x_rx_queue *rxq;
70 struct rte_mbuf *mbuf;
71 struct bnx2x_softc *sc = dev->data->dev_private;
72 struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
73 struct eth_rx_cqe_next_page *nextpg;
77 /* First allocate the rx queue data structure */
78 rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct bnx2x_rx_queue),
79 RTE_CACHE_LINE_SIZE, socket_id);
81 PMD_INIT_LOG(ERR, "rte_zmalloc for rxq failed!");
86 rxq->queue_id = queue_idx;
87 rxq->port_id = dev->data->port_id;
90 while (USABLE_RX_BD(rxq) < nb_desc)
91 rxq->nb_rx_pages <<= 1;
93 rxq->nb_rx_desc = TOTAL_RX_BD(rxq);
94 sc->rx_ring_size = USABLE_RX_BD(rxq);
95 rxq->nb_cq_pages = RCQ_BD_PAGES(rxq);
97 PMD_INIT_LOG(DEBUG, "fp[%02d] req_bd=%u, usable_bd=%lu, "
98 "total_bd=%lu, rx_pages=%u, cq_pages=%u",
99 queue_idx, nb_desc, (unsigned long)USABLE_RX_BD(rxq),
100 (unsigned long)TOTAL_RX_BD(rxq), rxq->nb_rx_pages,
103 /* Allocate RX ring hardware descriptors */
104 dma_size = rxq->nb_rx_desc * sizeof(struct eth_rx_bd);
105 dma = ring_dma_zone_reserve(dev, "hw_ring", queue_idx, dma_size, socket_id);
107 PMD_RX_LOG(ERR, "ring_dma_zone_reserve for rx_ring failed!");
108 bnx2x_rx_queue_release(rxq);
111 fp->rx_desc_mapping = rxq->rx_ring_phys_addr = (uint64_t)dma->phys_addr;
112 rxq->rx_ring = (uint64_t*)dma->addr;
113 memset((void *)rxq->rx_ring, 0, dma_size);
115 /* Link the RX chain pages. */
116 for (j = 1; j <= rxq->nb_rx_pages; j++) {
117 rx_bd = &rxq->rx_ring[TOTAL_RX_BD_PER_PAGE * j - 2];
118 busaddr = rxq->rx_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_rx_pages);
122 /* Allocate software ring */
123 dma_size = rxq->nb_rx_desc * sizeof(struct bnx2x_rx_entry);
124 rxq->sw_ring = rte_zmalloc_socket("sw_ring", dma_size,
127 if (NULL == rxq->sw_ring) {
128 PMD_RX_LOG(ERR, "rte_zmalloc for sw_ring failed!");
129 bnx2x_rx_queue_release(rxq);
133 /* Initialize software ring entries */
134 for (idx = 0; idx < rxq->nb_rx_desc; idx = NEXT_RX_BD(idx)) {
135 mbuf = rte_mbuf_raw_alloc(mp);
137 PMD_RX_LOG(ERR, "RX mbuf alloc failed queue_id=%u, idx=%d",
138 (unsigned)rxq->queue_id, idx);
139 bnx2x_rx_queue_release(rxq);
142 rxq->sw_ring[idx] = mbuf;
143 rxq->rx_ring[idx] = mbuf->buf_physaddr;
145 rxq->pkt_first_seg = NULL;
146 rxq->pkt_last_seg = NULL;
148 rxq->rx_bd_tail = rxq->nb_rx_desc;
150 /* Allocate CQ chain. */
151 dma_size = BNX2X_RX_CHAIN_PAGE_SZ * rxq->nb_cq_pages;
152 dma = ring_dma_zone_reserve(dev, "bnx2x_rcq", queue_idx, dma_size, socket_id);
154 PMD_RX_LOG(ERR, "RCQ alloc failed");
157 fp->rx_comp_mapping = rxq->cq_ring_phys_addr = (uint64_t)dma->phys_addr;
158 rxq->cq_ring = (union eth_rx_cqe*)dma->addr;
160 /* Link the CQ chain pages. */
161 for (j = 1; j <= rxq->nb_cq_pages; j++) {
162 nextpg = &rxq->cq_ring[TOTAL_RCQ_ENTRIES_PER_PAGE * j - 1].next_page_cqe;
163 busaddr = rxq->cq_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_cq_pages);
164 nextpg->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
165 nextpg->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
168 rxq->rx_cq_tail = TOTAL_RCQ_ENTRIES(rxq);
170 dev->data->rx_queues[queue_idx] = rxq;
171 if (!sc->rx_queues) sc->rx_queues = dev->data->rx_queues;
177 bnx2x_tx_queue_release(struct bnx2x_tx_queue *tx_queue)
180 struct rte_mbuf **sw_ring;
182 if (NULL != tx_queue) {
184 sw_ring = tx_queue->sw_ring;
185 if (NULL != sw_ring) {
186 for (i = 0; i < tx_queue->nb_tx_desc; i++) {
187 if (NULL != sw_ring[i])
188 rte_pktmbuf_free(sw_ring[i]);
197 bnx2x_dev_tx_queue_release(void *txq)
199 bnx2x_tx_queue_release(txq);
203 bnx2x_xmit_pkts(void *p_txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
205 struct bnx2x_tx_queue *txq;
206 struct bnx2x_softc *sc;
207 struct bnx2x_fastpath *fp;
209 uint16_t nb_pkt_sent = 0;
214 fp = &sc->fp[txq->queue_id];
216 if ((unlikely((txq->nb_tx_desc - txq->nb_tx_avail) >
217 txq->tx_free_thresh)))
220 nb_tx_pkts = RTE_MIN(nb_pkts, txq->nb_tx_avail / BDS_PER_TX_PKT);
221 if (unlikely(nb_tx_pkts == 0))
224 while (nb_tx_pkts--) {
225 struct rte_mbuf *m = *tx_pkts++;
227 ret = bnx2x_tx_encap(txq, m);
228 fp->tx_db.data.prod += ret;
232 bnx2x_update_fp_sb_idx(fp);
234 DOORBELL(sc, txq->queue_id, fp->tx_db.raw);
237 if ((txq->nb_tx_desc - txq->nb_tx_avail) >
245 bnx2x_dev_tx_queue_setup(struct rte_eth_dev *dev,
248 unsigned int socket_id,
249 const struct rte_eth_txconf *tx_conf)
253 const struct rte_memzone *tz;
254 struct bnx2x_tx_queue *txq;
255 struct eth_tx_next_bd *tx_n_bd;
257 struct bnx2x_softc *sc = dev->data->dev_private;
258 struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
260 /* First allocate the tx queue data structure */
261 txq = rte_zmalloc("ethdev TX queue", sizeof(struct bnx2x_tx_queue),
262 RTE_CACHE_LINE_SIZE);
267 txq->nb_tx_pages = 1;
268 while (USABLE_TX_BD(txq) < nb_desc)
269 txq->nb_tx_pages <<= 1;
271 txq->nb_tx_desc = TOTAL_TX_BD(txq);
272 sc->tx_ring_size = TOTAL_TX_BD(txq);
274 txq->tx_free_thresh = tx_conf->tx_free_thresh ?
275 tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH;
276 txq->tx_free_thresh = min(txq->tx_free_thresh,
277 txq->nb_tx_desc - BDS_PER_TX_PKT);
279 PMD_INIT_LOG(DEBUG, "fp[%02d] req_bd=%u, thresh=%u, usable_bd=%lu, "
280 "total_bd=%lu, tx_pages=%u",
281 queue_idx, nb_desc, txq->tx_free_thresh,
282 (unsigned long)USABLE_TX_BD(txq),
283 (unsigned long)TOTAL_TX_BD(txq), txq->nb_tx_pages);
285 /* Allocate TX ring hardware descriptors */
286 tsize = txq->nb_tx_desc * sizeof(union eth_tx_bd_types);
287 tz = ring_dma_zone_reserve(dev, "tx_hw_ring", queue_idx, tsize, socket_id);
289 bnx2x_tx_queue_release(txq);
292 fp->tx_desc_mapping = txq->tx_ring_phys_addr = (uint64_t)tz->phys_addr;
293 txq->tx_ring = (union eth_tx_bd_types *) tz->addr;
294 memset(txq->tx_ring, 0, tsize);
296 /* Allocate software ring */
297 tsize = txq->nb_tx_desc * sizeof(struct rte_mbuf *);
298 txq->sw_ring = rte_zmalloc("tx_sw_ring", tsize,
299 RTE_CACHE_LINE_SIZE);
300 if (txq->sw_ring == NULL) {
301 bnx2x_tx_queue_release(txq);
305 /* PMD_DRV_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
306 txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr); */
309 for (i = 1; i <= txq->nb_tx_pages; i++) {
310 tx_n_bd = &txq->tx_ring[TOTAL_TX_BD_PER_PAGE * i - 1].next_bd;
311 busaddr = txq->tx_ring_phys_addr + BNX2X_PAGE_SIZE * (i % txq->nb_tx_pages);
312 tx_n_bd->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
313 tx_n_bd->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
314 /* PMD_DRV_LOG(DEBUG, "link tx page %lu", (TOTAL_TX_BD_PER_PAGE * i - 1)); */
317 txq->queue_id = queue_idx;
318 txq->port_id = dev->data->port_id;
319 txq->tx_pkt_tail = 0;
320 txq->tx_pkt_head = 0;
323 txq->nb_tx_avail = txq->nb_tx_desc;
324 dev->tx_pkt_burst = bnx2x_xmit_pkts;
325 dev->data->tx_queues[queue_idx] = txq;
326 if (!sc->tx_queues) sc->tx_queues = dev->data->tx_queues;
332 bnx2x_upd_rx_prod_fast(struct bnx2x_softc *sc, struct bnx2x_fastpath *fp,
333 uint16_t rx_bd_prod, uint16_t rx_cq_prod)
335 union ustorm_eth_rx_producers rx_prods;
337 rx_prods.prod.bd_prod = rx_bd_prod;
338 rx_prods.prod.cqe_prod = rx_cq_prod;
340 REG_WR(sc, fp->ustorm_rx_prods_offset, rx_prods.raw_data[0]);
344 bnx2x_recv_pkts(void *p_rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
346 struct bnx2x_rx_queue *rxq = p_rxq;
347 struct bnx2x_softc *sc = rxq->sc;
348 struct bnx2x_fastpath *fp = &sc->fp[rxq->queue_id];
350 uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
351 uint16_t bd_cons, bd_prod;
352 struct rte_mbuf *new_mb;
354 struct eth_fast_path_rx_cqe *cqe_fp;
356 struct rte_mbuf *rx_mb = NULL;
358 hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
359 if ((hw_cq_cons & USABLE_RCQ_ENTRIES_PER_PAGE) ==
360 USABLE_RCQ_ENTRIES_PER_PAGE) {
364 bd_cons = rxq->rx_bd_head;
365 bd_prod = rxq->rx_bd_tail;
366 sw_cq_cons = rxq->rx_cq_head;
367 sw_cq_prod = rxq->rx_cq_tail;
369 if (sw_cq_cons == hw_cq_cons)
372 while (nb_rx < nb_pkts && sw_cq_cons != hw_cq_cons) {
374 bd_prod &= MAX_RX_BD(rxq);
375 bd_cons &= MAX_RX_BD(rxq);
377 cqe_fp = &rxq->cq_ring[sw_cq_cons & MAX_RX_BD(rxq)].fast_path_cqe;
379 if (unlikely(CQE_TYPE_SLOW(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_TYPE))) {
380 PMD_RX_LOG(ERR, "slowpath event during traffic processing");
384 if (unlikely(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
385 PMD_RX_LOG(ERR, "flags 0x%x rx packet %u",
386 cqe_fp->type_error_flags, sw_cq_cons);
390 len = cqe_fp->pkt_len_or_gro_seg_len;
391 pad = cqe_fp->placement_offset;
393 new_mb = rte_mbuf_raw_alloc(rxq->mb_pool);
394 if (unlikely(!new_mb)) {
395 PMD_RX_LOG(ERR, "mbuf alloc fail fp[%02d]", fp->index);
396 rte_eth_devices[rxq->port_id].data->
397 rx_mbuf_alloc_failed++;
401 rx_mb = rxq->sw_ring[bd_cons];
402 rxq->sw_ring[bd_cons] = new_mb;
403 rxq->rx_ring[bd_prod] = new_mb->buf_physaddr;
405 rx_pref = NEXT_RX_BD(bd_cons) & MAX_RX_BD(rxq);
406 rte_prefetch0(rxq->sw_ring[rx_pref]);
407 if ((rx_pref & 0x3) == 0) {
408 rte_prefetch0(&rxq->rx_ring[rx_pref]);
409 rte_prefetch0(&rxq->sw_ring[rx_pref]);
412 rx_mb->data_off = pad;
415 rx_mb->pkt_len = rx_mb->data_len = len;
416 rx_mb->port = rxq->port_id;
417 rte_prefetch1(rte_pktmbuf_mtod(rx_mb, void *));
420 * If we received a packet with a vlan tag,
421 * attach that information to the packet.
423 if (cqe_fp->pars_flags.flags & PARSING_FLAGS_VLAN) {
424 rx_mb->vlan_tci = cqe_fp->vlan_tag;
425 rx_mb->ol_flags |= PKT_RX_VLAN_PKT;
428 rx_pkts[nb_rx] = rx_mb;
431 /* limit spinning on the queue */
432 if (unlikely(nb_rx == sc->rx_budget)) {
433 PMD_RX_LOG(ERR, "Limit spinning on the queue");
438 bd_cons = NEXT_RX_BD(bd_cons);
439 bd_prod = NEXT_RX_BD(bd_prod);
440 sw_cq_prod = NEXT_RCQ_IDX(sw_cq_prod);
441 sw_cq_cons = NEXT_RCQ_IDX(sw_cq_cons);
443 rxq->rx_bd_head = bd_cons;
444 rxq->rx_bd_tail = bd_prod;
445 rxq->rx_cq_head = sw_cq_cons;
446 rxq->rx_cq_tail = sw_cq_prod;
448 bnx2x_upd_rx_prod_fast(sc, fp, bd_prod, sw_cq_prod);
454 bnx2x_dev_rx_init(struct rte_eth_dev *dev)
456 dev->rx_pkt_burst = bnx2x_recv_pkts;
462 bnx2x_dev_clear_queues(struct rte_eth_dev *dev)
466 PMD_INIT_FUNC_TRACE();
468 for (i = 0; i < dev->data->nb_tx_queues; i++) {
469 struct bnx2x_tx_queue *txq = dev->data->tx_queues[i];
471 bnx2x_tx_queue_release(txq);
472 dev->data->tx_queues[i] = NULL;
476 for (i = 0; i < dev->data->nb_rx_queues; i++) {
477 struct bnx2x_rx_queue *rxq = dev->data->rx_queues[i];
479 bnx2x_rx_queue_release(rxq);
480 dev->data->rx_queues[i] = NULL;