4 * Copyright(c) Broadcom Limited.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Broadcom Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_byteorder.h>
38 #include <rte_malloc.h>
39 #include <rte_memory.h>
43 #include "bnxt_ring.h"
46 #include "hsi_struct_def_dpdk.h"
52 static inline struct rte_mbuf *__bnxt_alloc_rx_data(struct rte_mempool *mb)
54 struct rte_mbuf *data;
56 data = rte_mbuf_raw_alloc(mb);
61 static inline int bnxt_alloc_rx_data(struct bnxt_rx_queue *rxq,
62 struct bnxt_rx_ring_info *rxr,
65 struct rx_prod_pkt_bd *rxbd = &rxr->rx_desc_ring[prod];
66 struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
67 struct rte_mbuf *data;
69 data = __bnxt_alloc_rx_data(rxq->mb_pool);
75 rxbd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(rx_buf->mbuf));
80 static inline int bnxt_alloc_ag_data(struct bnxt_rx_queue *rxq,
81 struct bnxt_rx_ring_info *rxr,
84 struct rx_prod_pkt_bd *rxbd = &rxr->ag_desc_ring[prod];
85 struct bnxt_sw_rx_bd *rx_buf = &rxr->ag_buf_ring[prod];
86 struct rte_mbuf *data;
88 data = __bnxt_alloc_rx_data(rxq->mb_pool);
93 RTE_LOG(ERR, PMD, "Jumbo Frame. rxbd is NULL\n");
95 RTE_LOG(ERR, PMD, "Jumbo Frame. rx_buf is NULL\n");
100 rxbd->addr = rte_cpu_to_le_64(RTE_MBUF_DATA_DMA_ADDR(rx_buf->mbuf));
106 static void bnxt_reuse_rx_mbuf(struct bnxt_rx_ring_info *rxr, uint16_t cons,
107 struct rte_mbuf *mbuf)
109 uint16_t prod = rxr->rx_prod;
110 struct bnxt_sw_rx_bd *prod_rx_buf;
111 struct rx_prod_pkt_bd *prod_bd, *cons_bd;
113 prod_rx_buf = &rxr->rx_buf_ring[prod];
115 prod_rx_buf->mbuf = mbuf;
117 prod_bd = &rxr->rx_desc_ring[prod];
118 cons_bd = &rxr->rx_desc_ring[cons];
120 prod_bd->addr = cons_bd->addr;
123 static void bnxt_reuse_ag_mbuf(struct bnxt_rx_ring_info *rxr, uint16_t cons,
124 struct rte_mbuf *mbuf)
126 uint16_t prod = rxr->ag_prod;
127 struct bnxt_sw_rx_bd *prod_rx_buf;
128 struct rx_prod_pkt_bd *prod_bd, *cons_bd;
130 prod_rx_buf = &rxr->ag_buf_ring[prod];
132 prod_rx_buf->mbuf = mbuf;
134 prod_bd = &rxr->ag_desc_ring[prod];
135 cons_bd = &rxr->ag_desc_ring[cons];
137 prod_bd->addr = cons_bd->addr;
141 static uint16_t bnxt_rx_pkt(struct rte_mbuf **rx_pkt,
142 struct bnxt_rx_queue *rxq, uint32_t *raw_cons)
144 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
145 struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
146 struct rx_pkt_cmpl *rxcmp;
147 struct rx_pkt_cmpl_hi *rxcmp1;
148 uint32_t tmp_raw_cons = *raw_cons;
149 uint16_t cons, prod, cp_cons =
150 RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
151 uint16_t ag_cons, ag_prod = rxr->ag_prod;
152 struct bnxt_sw_rx_bd *rx_buf;
153 struct rte_mbuf *mbuf;
158 rxcmp = (struct rx_pkt_cmpl *)
159 &cpr->cp_desc_ring[cp_cons];
161 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
162 cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
163 rxcmp1 = (struct rx_pkt_cmpl_hi *)&cpr->cp_desc_ring[cp_cons];
165 if (!CMP_VALID(rxcmp1, tmp_raw_cons, cpr->cp_ring_struct))
170 /* EW - GRO deferred to phase 3 */
171 cons = rxcmp->opaque;
172 rx_buf = &rxr->rx_buf_ring[cons];
181 mbuf->pkt_len = rxcmp->len;
182 mbuf->data_len = mbuf->pkt_len;
183 mbuf->port = rxq->port_id;
185 if (rxcmp->flags_type & RX_PKT_CMPL_FLAGS_RSS_VALID) {
186 mbuf->hash.rss = rxcmp->rss_hash;
187 mbuf->ol_flags |= PKT_RX_RSS_HASH;
189 mbuf->hash.fdir.id = rxcmp1->cfa_code;
190 mbuf->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
193 agg_buf = (rxcmp->agg_bufs_v1 & RX_PKT_CMPL_AGG_BUFS_MASK)
194 >> RX_PKT_CMPL_AGG_BUFS_SFT;
196 cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons + agg_buf);
197 rxcmp = (struct rx_pkt_cmpl *)
198 &cpr->cp_desc_ring[cp_cons];
199 if (!CMP_VALID(rxcmp, tmp_raw_cons + agg_buf,
200 cpr->cp_ring_struct))
202 RTE_LOG(DEBUG, PMD, "JUMBO Frame %d. %x, agg_buf %x,\n",
203 mbuf->pkt_len, rxcmp->agg_bufs_v1, agg_buf);
206 for (i = 0; i < agg_buf; i++) {
207 struct bnxt_sw_rx_bd *ag_buf;
208 struct rte_mbuf *ag_mbuf;
209 tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
210 cp_cons = RING_CMP(cpr->cp_ring_struct, tmp_raw_cons);
211 rxcmp = (struct rx_pkt_cmpl *)
212 &cpr->cp_desc_ring[cp_cons];
213 ag_cons = rxcmp->opaque;
214 ag_buf = &rxr->ag_buf_ring[ag_cons];
215 ag_mbuf = ag_buf->mbuf;
216 ag_mbuf->nb_segs = 1;
217 ag_mbuf->data_len = rxcmp->len;
220 mbuf->pkt_len += ag_mbuf->data_len;
221 if (mbuf->next == NULL) {
222 mbuf->next = ag_mbuf;
224 struct rte_mbuf *temp_mbuf = mbuf;
226 while (temp_mbuf->next != NULL)
227 temp_mbuf = temp_mbuf->next;
228 temp_mbuf->next = ag_mbuf;
232 ag_prod = RING_NEXT(rxr->ag_ring_struct, ag_prod);
233 if (bnxt_alloc_ag_data(rxq, rxr, ag_prod)) {
235 "agg mbuf alloc failed: prod=0x%x\n",
239 rxr->ag_prod = ag_prod;
242 if (!CMP_VALID((struct cmpl_base *)
243 &cpr->cp_desc_ring[cp_cons], tmp_raw_cons,
244 cpr->cp_ring_struct))
249 if (rxcmp1->flags2 & RX_PKT_CMPL_FLAGS2_META_FORMAT_VLAN) {
250 mbuf->vlan_tci = rxcmp1->metadata &
251 (RX_PKT_CMPL_METADATA_VID_MASK |
252 RX_PKT_CMPL_METADATA_DE |
253 RX_PKT_CMPL_METADATA_PRI_MASK);
254 mbuf->ol_flags |= PKT_RX_VLAN_PKT;
259 if (rxcmp1->errors_v2 & RX_CMP_L2_ERRORS) {
260 /* Re-install the mbuf back to the rx ring */
261 bnxt_reuse_rx_mbuf(rxr, cons, mbuf);
263 bnxt_reuse_ag_mbuf(rxr, ag_cons, mbuf);
270 * TODO: Redesign this....
271 * If the allocation fails, the packet does not get received.
272 * Simply returning this will result in slowly falling behind
273 * on the producer ring buffers.
274 * Instead, "filling up" the producer just before ringing the
275 * doorbell could be a better solution since it will let the
276 * producer ring starve until memory is available again pushing
277 * the drops into hardware and getting them out of the driver
278 * allowing recovery to a full producer ring.
280 * This could also help with cache usage by preventing per-packet
281 * calls in favour of a tight loop with the same function being called
284 prod = RING_NEXT(rxr->rx_ring_struct, prod);
285 if (bnxt_alloc_rx_data(rxq, rxr, prod)) {
286 RTE_LOG(ERR, PMD, "mbuf alloc failed with prod=0x%x\n", prod);
291 * All MBUFs are allocated with the same size under DPDK,
292 * no optimization for rx_copy_thresh
299 *raw_cons = tmp_raw_cons;
304 uint16_t bnxt_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
307 struct bnxt_rx_queue *rxq = rx_queue;
308 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
309 struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
310 uint32_t raw_cons = cpr->cp_raw_cons;
313 struct rx_pkt_cmpl *rxcmp;
314 uint16_t prod = rxr->rx_prod;
315 uint16_t ag_prod = rxr->ag_prod;
317 /* Handle RX burst request */
321 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
322 rte_prefetch0(&cpr->cp_desc_ring[cons]);
323 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
325 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
328 /* TODO: Avoid magic numbers... */
329 if ((CMP_TYPE(rxcmp) & 0x30) == 0x10) {
330 rc = bnxt_rx_pkt(&rx_pkts[nb_rx_pkts], rxq, &raw_cons);
333 else if (rc == -EBUSY) /* partial completion */
336 raw_cons = NEXT_RAW_CMP(raw_cons);
337 if (nb_rx_pkts == nb_pkts)
341 if (prod == rxr->rx_prod && ag_prod == rxr->ag_prod) {
343 * For PMD, there is no need to keep on pushing to REARM
344 * the doorbell if there are no new completions
348 cpr->cp_raw_cons = raw_cons;
350 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
351 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
352 /* Ring the AGG ring DB */
353 B_RX_DB(rxr->ag_doorbell, rxr->ag_prod);
357 void bnxt_free_rx_rings(struct bnxt *bp)
361 for (i = 0; i < (int)bp->rx_nr_rings; i++) {
362 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
367 bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
368 rte_free(rxq->rx_ring->rx_ring_struct);
370 /* Free the Aggregator ring */
371 bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
372 rte_free(rxq->rx_ring->ag_ring_struct);
373 rxq->rx_ring->ag_ring_struct = NULL;
375 rte_free(rxq->rx_ring);
377 bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
378 rte_free(rxq->cp_ring->cp_ring_struct);
379 rte_free(rxq->cp_ring);
382 bp->rx_queues[i] = NULL;
386 int bnxt_init_rx_ring_struct(struct bnxt_rx_queue *rxq, unsigned int socket_id)
388 struct bnxt_cp_ring_info *cpr;
389 struct bnxt_rx_ring_info *rxr;
390 struct bnxt_ring *ring;
392 rxq->rx_buf_use_size = BNXT_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN +
394 rxq->rx_buf_size = rxq->rx_buf_use_size + sizeof(struct rte_mbuf);
396 rxr = rte_zmalloc_socket("bnxt_rx_ring",
397 sizeof(struct bnxt_rx_ring_info),
398 RTE_CACHE_LINE_SIZE, socket_id);
403 ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
404 sizeof(struct bnxt_ring),
405 RTE_CACHE_LINE_SIZE, socket_id);
408 rxr->rx_ring_struct = ring;
409 ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
410 ring->ring_mask = ring->ring_size - 1;
411 ring->bd = (void *)rxr->rx_desc_ring;
412 ring->bd_dma = rxr->rx_desc_mapping;
413 ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
414 ring->vmem = (void **)&rxr->rx_buf_ring;
416 cpr = rte_zmalloc_socket("bnxt_rx_ring",
417 sizeof(struct bnxt_cp_ring_info),
418 RTE_CACHE_LINE_SIZE, socket_id);
423 ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
424 sizeof(struct bnxt_ring),
425 RTE_CACHE_LINE_SIZE, socket_id);
428 cpr->cp_ring_struct = ring;
429 ring->ring_size = rte_align32pow2(rxr->rx_ring_struct->ring_size *
430 (2 + AGG_RING_SIZE_FACTOR));
431 ring->ring_mask = ring->ring_size - 1;
432 ring->bd = (void *)cpr->cp_desc_ring;
433 ring->bd_dma = cpr->cp_desc_mapping;
437 /* Allocate Aggregator rings */
438 ring = rte_zmalloc_socket("bnxt_rx_ring_struct",
439 sizeof(struct bnxt_ring),
440 RTE_CACHE_LINE_SIZE, socket_id);
443 rxr->ag_ring_struct = ring;
444 ring->ring_size = rte_align32pow2(rxq->nb_rx_desc *
445 AGG_RING_SIZE_FACTOR);
446 ring->ring_mask = ring->ring_size - 1;
447 ring->bd = (void *)rxr->ag_desc_ring;
448 ring->bd_dma = rxr->ag_desc_mapping;
449 ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_rx_bd);
450 ring->vmem = (void **)&rxr->ag_buf_ring;
455 static void bnxt_init_rxbds(struct bnxt_ring *ring, uint32_t type,
459 struct rx_prod_pkt_bd *rx_bd_ring = (struct rx_prod_pkt_bd *)ring->bd;
463 for (j = 0; j < ring->ring_size; j++) {
464 rx_bd_ring[j].flags_type = rte_cpu_to_le_16(type);
465 rx_bd_ring[j].len = rte_cpu_to_le_16(len);
466 rx_bd_ring[j].opaque = j;
470 int bnxt_init_one_rx_ring(struct bnxt_rx_queue *rxq)
472 struct bnxt_rx_ring_info *rxr;
473 struct bnxt_ring *ring;
478 size = rte_pktmbuf_data_room_size(rxq->mb_pool) - RTE_PKTMBUF_HEADROOM;
479 if (rxq->rx_buf_use_size <= size)
480 size = rxq->rx_buf_use_size;
482 type = RX_PROD_PKT_BD_TYPE_RX_PROD_PKT;
485 ring = rxr->rx_ring_struct;
486 bnxt_init_rxbds(ring, type, size);
489 for (i = 0; i < ring->ring_size; i++) {
490 if (bnxt_alloc_rx_data(rxq, rxr, prod) != 0) {
491 RTE_LOG(WARNING, PMD,
492 "init'ed rx ring %d with %d/%d mbufs only\n",
493 rxq->queue_id, i, ring->ring_size);
497 prod = RING_NEXT(rxr->rx_ring_struct, prod);
499 RTE_LOG(DEBUG, PMD, "%s\n", __func__);
501 ring = rxr->ag_ring_struct;
502 type = RX_PROD_AGG_BD_TYPE_RX_PROD_AGG;
503 bnxt_init_rxbds(ring, type, size);
506 for (i = 0; i < ring->ring_size; i++) {
507 if (bnxt_alloc_ag_data(rxq, rxr, prod) != 0) {
508 RTE_LOG(WARNING, PMD,
509 "init'ed AG ring %d with %d/%d mbufs only\n",
510 rxq->queue_id, i, ring->ring_size);
514 prod = RING_NEXT(rxr->ag_ring_struct, prod);
516 RTE_LOG(DEBUG, PMD, "%s AGG Done!\n", __func__);