net/bnx2x: change copyright info to Cavium
[dpdk.git] / drivers / net / bnx2x / bnx2x_rxtx.c
1 /*
2  * Copyright (c) 2013-2015 Brocade Communications Systems, Inc.
3  *
4  * Copyright (c) 2015-2018 Cavium Inc.
5  * All rights reserved.
6  * www.cavium.com
7  *
8  * See LICENSE.bnx2x_pmd for copyright and licensing details.
9  */
10
11 #include "bnx2x.h"
12 #include "bnx2x_rxtx.h"
13
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)
17 {
18         char z_name[RTE_MEMZONE_NAMESIZE];
19         const struct rte_memzone *mz;
20
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);
24
25         mz = rte_memzone_lookup(z_name);
26         if (mz)
27                 return mz;
28
29         return rte_memzone_reserve_aligned(z_name, ring_size, socket_id,
30                         RTE_MEMZONE_IOVA_CONTIG, BNX2X_PAGE_SIZE);
31 }
32
33 static void
34 bnx2x_rx_queue_release(struct bnx2x_rx_queue *rx_queue)
35 {
36         uint16_t i;
37         struct rte_mbuf **sw_ring;
38
39         if (NULL != rx_queue) {
40
41                 sw_ring = rx_queue->sw_ring;
42                 if (NULL != sw_ring) {
43                         for (i = 0; i < rx_queue->nb_rx_desc; i++) {
44                                 if (NULL != sw_ring[i])
45                                         rte_pktmbuf_free(sw_ring[i]);
46                         }
47                         rte_free(sw_ring);
48                 }
49                 rte_free(rx_queue);
50         }
51 }
52
53 void
54 bnx2x_dev_rx_queue_release(void *rxq)
55 {
56         bnx2x_rx_queue_release(rxq);
57 }
58
59 int
60 bnx2x_dev_rx_queue_setup(struct rte_eth_dev *dev,
61                        uint16_t queue_idx,
62                        uint16_t nb_desc,
63                        unsigned int socket_id,
64                        __rte_unused const struct rte_eth_rxconf *rx_conf,
65                        struct rte_mempool *mp)
66 {
67         uint16_t j, idx;
68         const struct rte_memzone *dma;
69         struct bnx2x_rx_queue *rxq;
70         uint32_t dma_size;
71         struct rte_mbuf *mbuf;
72         struct bnx2x_softc *sc = dev->data->dev_private;
73         struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
74         struct eth_rx_cqe_next_page *nextpg;
75         rte_iova_t *rx_bd;
76         rte_iova_t busaddr;
77
78         /* First allocate the rx queue data structure */
79         rxq = rte_zmalloc_socket("ethdev RX queue", sizeof(struct bnx2x_rx_queue),
80                                  RTE_CACHE_LINE_SIZE, socket_id);
81         if (NULL == rxq) {
82                 PMD_INIT_LOG(ERR, "rte_zmalloc for rxq failed!");
83                 return -ENOMEM;
84         }
85         rxq->sc = sc;
86         rxq->mb_pool = mp;
87         rxq->queue_id = queue_idx;
88         rxq->port_id = dev->data->port_id;
89
90         rxq->nb_rx_pages = 1;
91         while (USABLE_RX_BD(rxq) < nb_desc)
92                 rxq->nb_rx_pages <<= 1;
93
94         rxq->nb_rx_desc  = TOTAL_RX_BD(rxq);
95         sc->rx_ring_size = USABLE_RX_BD(rxq);
96         rxq->nb_cq_pages = RCQ_BD_PAGES(rxq);
97
98         PMD_INIT_LOG(DEBUG, "fp[%02d] req_bd=%u, usable_bd=%lu, "
99                        "total_bd=%lu, rx_pages=%u, cq_pages=%u",
100                        queue_idx, nb_desc, (unsigned long)USABLE_RX_BD(rxq),
101                        (unsigned long)TOTAL_RX_BD(rxq), rxq->nb_rx_pages,
102                        rxq->nb_cq_pages);
103
104         /* Allocate RX ring hardware descriptors */
105         dma_size = rxq->nb_rx_desc * sizeof(struct eth_rx_bd);
106         dma = ring_dma_zone_reserve(dev, "hw_ring", queue_idx, dma_size, socket_id);
107         if (NULL == dma) {
108                 PMD_RX_LOG(ERR, "ring_dma_zone_reserve for rx_ring failed!");
109                 bnx2x_rx_queue_release(rxq);
110                 return -ENOMEM;
111         }
112         fp->rx_desc_mapping = rxq->rx_ring_phys_addr = (uint64_t)dma->iova;
113         rxq->rx_ring = (uint64_t*)dma->addr;
114         memset((void *)rxq->rx_ring, 0, dma_size);
115
116         /* Link the RX chain pages. */
117         for (j = 1; j <= rxq->nb_rx_pages; j++) {
118                 rx_bd = &rxq->rx_ring[TOTAL_RX_BD_PER_PAGE * j - 2];
119                 busaddr = rxq->rx_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_rx_pages);
120                 *rx_bd = busaddr;
121         }
122
123         /* Allocate software ring */
124         dma_size = rxq->nb_rx_desc * sizeof(struct bnx2x_rx_entry);
125         rxq->sw_ring = rte_zmalloc_socket("sw_ring", dma_size,
126                                           RTE_CACHE_LINE_SIZE,
127                                           socket_id);
128         if (NULL == rxq->sw_ring) {
129                 PMD_RX_LOG(ERR, "rte_zmalloc for sw_ring failed!");
130                 bnx2x_rx_queue_release(rxq);
131                 return -ENOMEM;
132         }
133
134         /* Initialize software ring entries */
135         for (idx = 0; idx < rxq->nb_rx_desc; idx = NEXT_RX_BD(idx)) {
136                 mbuf = rte_mbuf_raw_alloc(mp);
137                 if (NULL == mbuf) {
138                         PMD_RX_LOG(ERR, "RX mbuf alloc failed queue_id=%u, idx=%d",
139                                    (unsigned)rxq->queue_id, idx);
140                         bnx2x_rx_queue_release(rxq);
141                         return -ENOMEM;
142                 }
143                 rxq->sw_ring[idx] = mbuf;
144                 rxq->rx_ring[idx] =
145                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
146         }
147         rxq->pkt_first_seg = NULL;
148         rxq->pkt_last_seg = NULL;
149         rxq->rx_bd_head = 0;
150         rxq->rx_bd_tail = rxq->nb_rx_desc;
151
152         /* Allocate CQ chain. */
153         dma_size = BNX2X_RX_CHAIN_PAGE_SZ * rxq->nb_cq_pages;
154         dma = ring_dma_zone_reserve(dev, "bnx2x_rcq", queue_idx, dma_size, socket_id);
155         if (NULL == dma) {
156                 PMD_RX_LOG(ERR, "RCQ  alloc failed");
157                 return -ENOMEM;
158         }
159         fp->rx_comp_mapping = rxq->cq_ring_phys_addr = (uint64_t)dma->iova;
160         rxq->cq_ring = (union eth_rx_cqe*)dma->addr;
161
162         /* Link the CQ chain pages. */
163         for (j = 1; j <= rxq->nb_cq_pages; j++) {
164                 nextpg = &rxq->cq_ring[TOTAL_RCQ_ENTRIES_PER_PAGE * j - 1].next_page_cqe;
165                 busaddr = rxq->cq_ring_phys_addr + BNX2X_PAGE_SIZE * (j % rxq->nb_cq_pages);
166                 nextpg->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
167                 nextpg->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
168         }
169         rxq->rx_cq_head = 0;
170         rxq->rx_cq_tail = TOTAL_RCQ_ENTRIES(rxq);
171
172         dev->data->rx_queues[queue_idx] = rxq;
173         if (!sc->rx_queues) sc->rx_queues = dev->data->rx_queues;
174
175         return 0;
176 }
177
178 static void
179 bnx2x_tx_queue_release(struct bnx2x_tx_queue *tx_queue)
180 {
181         uint16_t i;
182         struct rte_mbuf **sw_ring;
183
184         if (NULL != tx_queue) {
185
186                 sw_ring = tx_queue->sw_ring;
187                 if (NULL != sw_ring) {
188                         for (i = 0; i < tx_queue->nb_tx_desc; i++) {
189                                 if (NULL != sw_ring[i])
190                                         rte_pktmbuf_free(sw_ring[i]);
191                         }
192                         rte_free(sw_ring);
193                 }
194                 rte_free(tx_queue);
195         }
196 }
197
198 void
199 bnx2x_dev_tx_queue_release(void *txq)
200 {
201         bnx2x_tx_queue_release(txq);
202 }
203
204 static uint16_t
205 bnx2x_xmit_pkts(void *p_txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
206 {
207         struct bnx2x_tx_queue *txq;
208         struct bnx2x_softc *sc;
209         struct bnx2x_fastpath *fp;
210         uint16_t nb_tx_pkts;
211         uint16_t nb_pkt_sent = 0;
212         uint32_t ret;
213
214         txq = p_txq;
215         sc = txq->sc;
216         fp = &sc->fp[txq->queue_id];
217
218         if ((unlikely((txq->nb_tx_desc - txq->nb_tx_avail) >
219                                 txq->tx_free_thresh)))
220                 bnx2x_txeof(sc, fp);
221
222         nb_tx_pkts = RTE_MIN(nb_pkts, txq->nb_tx_avail / BDS_PER_TX_PKT);
223         if (unlikely(nb_tx_pkts == 0))
224                 return 0;
225
226         while (nb_tx_pkts--) {
227                 struct rte_mbuf *m = *tx_pkts++;
228                 assert(m != NULL);
229                 ret = bnx2x_tx_encap(txq, m);
230                 fp->tx_db.data.prod += ret;
231                 nb_pkt_sent++;
232         }
233
234         bnx2x_update_fp_sb_idx(fp);
235         mb();
236         DOORBELL(sc, txq->queue_id, fp->tx_db.raw);
237         mb();
238
239         if ((txq->nb_tx_desc - txq->nb_tx_avail) >
240                                 txq->tx_free_thresh)
241                 bnx2x_txeof(sc, fp);
242
243         return nb_pkt_sent;
244 }
245
246 int
247 bnx2x_dev_tx_queue_setup(struct rte_eth_dev *dev,
248                        uint16_t queue_idx,
249                        uint16_t nb_desc,
250                        unsigned int socket_id,
251                        const struct rte_eth_txconf *tx_conf)
252 {
253         uint16_t i;
254         unsigned int tsize;
255         const struct rte_memzone *tz;
256         struct bnx2x_tx_queue *txq;
257         struct eth_tx_next_bd *tx_n_bd;
258         uint64_t busaddr;
259         struct bnx2x_softc *sc = dev->data->dev_private;
260         struct bnx2x_fastpath *fp = &sc->fp[queue_idx];
261
262         /* First allocate the tx queue data structure */
263         txq = rte_zmalloc("ethdev TX queue", sizeof(struct bnx2x_tx_queue),
264                           RTE_CACHE_LINE_SIZE);
265         if (txq == NULL)
266                 return -ENOMEM;
267         txq->sc = sc;
268
269         txq->nb_tx_pages = 1;
270         while (USABLE_TX_BD(txq) < nb_desc)
271                 txq->nb_tx_pages <<= 1;
272
273         txq->nb_tx_desc  = TOTAL_TX_BD(txq);
274         sc->tx_ring_size = TOTAL_TX_BD(txq);
275
276         txq->tx_free_thresh = tx_conf->tx_free_thresh ?
277                 tx_conf->tx_free_thresh : DEFAULT_TX_FREE_THRESH;
278         txq->tx_free_thresh = min(txq->tx_free_thresh,
279                                   txq->nb_tx_desc - BDS_PER_TX_PKT);
280
281         PMD_INIT_LOG(DEBUG, "fp[%02d] req_bd=%u, thresh=%u, usable_bd=%lu, "
282                      "total_bd=%lu, tx_pages=%u",
283                      queue_idx, nb_desc, txq->tx_free_thresh,
284                      (unsigned long)USABLE_TX_BD(txq),
285                      (unsigned long)TOTAL_TX_BD(txq), txq->nb_tx_pages);
286
287         /* Allocate TX ring hardware descriptors */
288         tsize = txq->nb_tx_desc * sizeof(union eth_tx_bd_types);
289         tz = ring_dma_zone_reserve(dev, "tx_hw_ring", queue_idx, tsize, socket_id);
290         if (tz == NULL) {
291                 bnx2x_tx_queue_release(txq);
292                 return -ENOMEM;
293         }
294         fp->tx_desc_mapping = txq->tx_ring_phys_addr = (uint64_t)tz->iova;
295         txq->tx_ring = (union eth_tx_bd_types *) tz->addr;
296         memset(txq->tx_ring, 0, tsize);
297
298         /* Allocate software ring */
299         tsize = txq->nb_tx_desc * sizeof(struct rte_mbuf *);
300         txq->sw_ring = rte_zmalloc("tx_sw_ring", tsize,
301                                    RTE_CACHE_LINE_SIZE);
302         if (txq->sw_ring == NULL) {
303                 bnx2x_tx_queue_release(txq);
304                 return -ENOMEM;
305         }
306
307         /* PMD_DRV_LOG(DEBUG, "sw_ring=%p hw_ring=%p dma_addr=0x%"PRIx64,
308            txq->sw_ring, txq->tx_ring, txq->tx_ring_phys_addr); */
309
310         /* Link TX pages */
311         for (i = 1; i <= txq->nb_tx_pages; i++) {
312                 tx_n_bd = &txq->tx_ring[TOTAL_TX_BD_PER_PAGE * i - 1].next_bd;
313                 busaddr = txq->tx_ring_phys_addr + BNX2X_PAGE_SIZE * (i % txq->nb_tx_pages);
314                 tx_n_bd->addr_hi = rte_cpu_to_le_32(U64_HI(busaddr));
315                 tx_n_bd->addr_lo = rte_cpu_to_le_32(U64_LO(busaddr));
316                 /* PMD_DRV_LOG(DEBUG, "link tx page %lu", (TOTAL_TX_BD_PER_PAGE * i - 1)); */
317         }
318
319         txq->queue_id = queue_idx;
320         txq->port_id = dev->data->port_id;
321         txq->tx_pkt_tail = 0;
322         txq->tx_pkt_head = 0;
323         txq->tx_bd_tail = 0;
324         txq->tx_bd_head = 0;
325         txq->nb_tx_avail = txq->nb_tx_desc;
326         dev->tx_pkt_burst = bnx2x_xmit_pkts;
327         dev->data->tx_queues[queue_idx] = txq;
328         if (!sc->tx_queues) sc->tx_queues = dev->data->tx_queues;
329
330         return 0;
331 }
332
333 static inline void
334 bnx2x_upd_rx_prod_fast(struct bnx2x_softc *sc, struct bnx2x_fastpath *fp,
335                 uint16_t rx_bd_prod, uint16_t rx_cq_prod)
336 {
337         union ustorm_eth_rx_producers rx_prods;
338
339         rx_prods.prod.bd_prod  = rx_bd_prod;
340         rx_prods.prod.cqe_prod = rx_cq_prod;
341
342         REG_WR(sc, fp->ustorm_rx_prods_offset, rx_prods.raw_data[0]);
343 }
344
345 static uint16_t
346 bnx2x_recv_pkts(void *p_rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
347 {
348         struct bnx2x_rx_queue *rxq = p_rxq;
349         struct bnx2x_softc *sc = rxq->sc;
350         struct bnx2x_fastpath *fp = &sc->fp[rxq->queue_id];
351         uint32_t nb_rx = 0;
352         uint16_t hw_cq_cons, sw_cq_cons, sw_cq_prod;
353         uint16_t bd_cons, bd_prod;
354         struct rte_mbuf *new_mb;
355         uint16_t rx_pref;
356         struct eth_fast_path_rx_cqe *cqe_fp;
357         uint16_t len, pad;
358         struct rte_mbuf *rx_mb = NULL;
359
360         hw_cq_cons = le16toh(*fp->rx_cq_cons_sb);
361         if ((hw_cq_cons & USABLE_RCQ_ENTRIES_PER_PAGE) ==
362                         USABLE_RCQ_ENTRIES_PER_PAGE) {
363                 ++hw_cq_cons;
364         }
365
366         bd_cons = rxq->rx_bd_head;
367         bd_prod = rxq->rx_bd_tail;
368         sw_cq_cons = rxq->rx_cq_head;
369         sw_cq_prod = rxq->rx_cq_tail;
370
371         if (sw_cq_cons == hw_cq_cons)
372                 return 0;
373
374         while (nb_rx < nb_pkts && sw_cq_cons != hw_cq_cons) {
375
376                 bd_prod &= MAX_RX_BD(rxq);
377                 bd_cons &= MAX_RX_BD(rxq);
378
379                 cqe_fp = &rxq->cq_ring[sw_cq_cons & MAX_RX_BD(rxq)].fast_path_cqe;
380
381                 if (unlikely(CQE_TYPE_SLOW(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_TYPE))) {
382                         PMD_RX_LOG(ERR, "slowpath event during traffic processing");
383                         break;
384                 }
385
386                 if (unlikely(cqe_fp->type_error_flags & ETH_FAST_PATH_RX_CQE_PHY_DECODE_ERR_FLG)) {
387                         PMD_RX_LOG(ERR, "flags 0x%x rx packet %u",
388                                         cqe_fp->type_error_flags, sw_cq_cons);
389                         goto next_rx;
390                 }
391
392                 len = cqe_fp->pkt_len_or_gro_seg_len;
393                 pad = cqe_fp->placement_offset;
394
395                 new_mb = rte_mbuf_raw_alloc(rxq->mb_pool);
396                 if (unlikely(!new_mb)) {
397                         PMD_RX_LOG(ERR, "mbuf alloc fail fp[%02d]", fp->index);
398                         rte_eth_devices[rxq->port_id].data->
399                                         rx_mbuf_alloc_failed++;
400                         goto next_rx;
401                 }
402
403                 rx_mb = rxq->sw_ring[bd_cons];
404                 rxq->sw_ring[bd_cons] = new_mb;
405                 rxq->rx_ring[bd_prod] =
406                         rte_cpu_to_le_64(rte_mbuf_data_iova_default(new_mb));
407
408                 rx_pref = NEXT_RX_BD(bd_cons) & MAX_RX_BD(rxq);
409                 rte_prefetch0(rxq->sw_ring[rx_pref]);
410                 if ((rx_pref & 0x3) == 0) {
411                         rte_prefetch0(&rxq->rx_ring[rx_pref]);
412                         rte_prefetch0(&rxq->sw_ring[rx_pref]);
413                 }
414
415                 rx_mb->data_off = pad + RTE_PKTMBUF_HEADROOM;
416                 rx_mb->nb_segs = 1;
417                 rx_mb->next = NULL;
418                 rx_mb->pkt_len = rx_mb->data_len = len;
419                 rx_mb->port = rxq->port_id;
420                 rte_prefetch1(rte_pktmbuf_mtod(rx_mb, void *));
421
422                 /*
423                  * If we received a packet with a vlan tag,
424                  * attach that information to the packet.
425                  */
426                 if (cqe_fp->pars_flags.flags & PARSING_FLAGS_VLAN) {
427                         rx_mb->vlan_tci = cqe_fp->vlan_tag;
428                         rx_mb->ol_flags |= PKT_RX_VLAN;
429                 }
430
431                 rx_pkts[nb_rx] = rx_mb;
432                 nb_rx++;
433
434                 /* limit spinning on the queue */
435                 if (unlikely(nb_rx == sc->rx_budget)) {
436                         PMD_RX_LOG(ERR, "Limit spinning on the queue");
437                         break;
438                 }
439
440 next_rx:
441                 bd_cons    = NEXT_RX_BD(bd_cons);
442                 bd_prod    = NEXT_RX_BD(bd_prod);
443                 sw_cq_prod = NEXT_RCQ_IDX(sw_cq_prod);
444                 sw_cq_cons = NEXT_RCQ_IDX(sw_cq_cons);
445         }
446         rxq->rx_bd_head = bd_cons;
447         rxq->rx_bd_tail = bd_prod;
448         rxq->rx_cq_head = sw_cq_cons;
449         rxq->rx_cq_tail = sw_cq_prod;
450
451         bnx2x_upd_rx_prod_fast(sc, fp, bd_prod, sw_cq_prod);
452
453         return nb_rx;
454 }
455
456 int
457 bnx2x_dev_rx_init(struct rte_eth_dev *dev)
458 {
459         dev->rx_pkt_burst = bnx2x_recv_pkts;
460
461         return 0;
462 }
463
464 void
465 bnx2x_dev_clear_queues(struct rte_eth_dev *dev)
466 {
467         uint8_t i;
468
469         PMD_INIT_FUNC_TRACE();
470
471         for (i = 0; i < dev->data->nb_tx_queues; i++) {
472                 struct bnx2x_tx_queue *txq = dev->data->tx_queues[i];
473                 if (txq != NULL) {
474                         bnx2x_tx_queue_release(txq);
475                         dev->data->tx_queues[i] = NULL;
476                 }
477         }
478
479         for (i = 0; i < dev->data->nb_rx_queues; i++) {
480                 struct bnx2x_rx_queue *rxq = dev->data->rx_queues[i];
481                 if (rxq != NULL) {
482                         bnx2x_rx_queue_release(rxq);
483                         dev->data->rx_queues[i] = NULL;
484                 }
485         }
486 }