net/bnxt: support bulk free of Tx mbufs
[dpdk.git] / drivers / net / bnxt / bnxt_txr.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_byteorder.h>
9 #include <rte_malloc.h>
10
11 #include "bnxt.h"
12 #include "bnxt_cpr.h"
13 #include "bnxt_ring.h"
14 #include "bnxt_txq.h"
15 #include "bnxt_txr.h"
16 #include "hsi_struct_def_dpdk.h"
17 #include <stdbool.h>
18
19 /*
20  * TX Ring handling
21  */
22
23 void bnxt_free_tx_rings(struct bnxt *bp)
24 {
25         int i;
26
27         for (i = 0; i < (int)bp->tx_nr_rings; i++) {
28                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
29
30                 if (!txq)
31                         continue;
32
33                 bnxt_free_ring(txq->tx_ring->tx_ring_struct);
34                 rte_free(txq->tx_ring->tx_ring_struct);
35                 rte_free(txq->tx_ring);
36
37                 bnxt_free_ring(txq->cp_ring->cp_ring_struct);
38                 rte_free(txq->cp_ring->cp_ring_struct);
39                 rte_free(txq->cp_ring);
40
41                 rte_free(txq);
42                 bp->tx_queues[i] = NULL;
43         }
44 }
45
46 int bnxt_init_one_tx_ring(struct bnxt_tx_queue *txq)
47 {
48         struct bnxt_tx_ring_info *txr = txq->tx_ring;
49         struct bnxt_ring *ring = txr->tx_ring_struct;
50
51         txq->tx_wake_thresh = ring->ring_size / 2;
52         ring->fw_ring_id = INVALID_HW_RING_ID;
53
54         return 0;
55 }
56
57 int bnxt_init_tx_ring_struct(struct bnxt_tx_queue *txq, unsigned int socket_id)
58 {
59         struct bnxt_cp_ring_info *cpr;
60         struct bnxt_tx_ring_info *txr;
61         struct bnxt_ring *ring;
62
63         txr = rte_zmalloc_socket("bnxt_tx_ring",
64                                  sizeof(struct bnxt_tx_ring_info),
65                                  RTE_CACHE_LINE_SIZE, socket_id);
66         if (txr == NULL)
67                 return -ENOMEM;
68         txq->tx_ring = txr;
69
70         ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
71                                   sizeof(struct bnxt_ring),
72                                   RTE_CACHE_LINE_SIZE, socket_id);
73         if (ring == NULL)
74                 return -ENOMEM;
75         txr->tx_ring_struct = ring;
76         ring->ring_size = rte_align32pow2(txq->nb_tx_desc);
77         ring->ring_mask = ring->ring_size - 1;
78         ring->bd = (void *)txr->tx_desc_ring;
79         ring->bd_dma = txr->tx_desc_mapping;
80         ring->vmem_size = ring->ring_size * sizeof(struct bnxt_sw_tx_bd);
81         ring->vmem = (void **)&txr->tx_buf_ring;
82
83         cpr = rte_zmalloc_socket("bnxt_tx_ring",
84                                  sizeof(struct bnxt_cp_ring_info),
85                                  RTE_CACHE_LINE_SIZE, socket_id);
86         if (cpr == NULL)
87                 return -ENOMEM;
88         txq->cp_ring = cpr;
89
90         ring = rte_zmalloc_socket("bnxt_tx_ring_struct",
91                                   sizeof(struct bnxt_ring),
92                                   RTE_CACHE_LINE_SIZE, socket_id);
93         if (ring == NULL)
94                 return -ENOMEM;
95         cpr->cp_ring_struct = ring;
96         ring->ring_size = txr->tx_ring_struct->ring_size;
97         ring->ring_mask = ring->ring_size - 1;
98         ring->bd = (void *)cpr->cp_desc_ring;
99         ring->bd_dma = cpr->cp_desc_mapping;
100         ring->vmem_size = 0;
101         ring->vmem = NULL;
102
103         return 0;
104 }
105
106 static inline uint32_t bnxt_tx_bds_in_hw(struct bnxt_tx_queue *txq)
107 {
108         return ((txq->tx_ring->tx_prod - txq->tx_ring->tx_cons) &
109                 txq->tx_ring->tx_ring_struct->ring_mask);
110 }
111
112 static inline uint32_t bnxt_tx_avail(struct bnxt_tx_queue *txq)
113 {
114         /* Tell compiler to fetch tx indices from memory. */
115         rte_compiler_barrier();
116
117         return ((txq->tx_ring->tx_ring_struct->ring_size -
118                  bnxt_tx_bds_in_hw(txq)) - 1);
119 }
120
121 static uint16_t bnxt_start_xmit(struct rte_mbuf *tx_pkt,
122                                 struct bnxt_tx_queue *txq,
123                                 uint16_t *coal_pkts,
124                                 struct tx_bd_long **last_txbd)
125 {
126         struct bnxt_tx_ring_info *txr = txq->tx_ring;
127         struct tx_bd_long *txbd;
128         struct tx_bd_long_hi *txbd1 = NULL;
129         uint32_t vlan_tag_flags, cfa_action;
130         bool long_bd = false;
131         unsigned short nr_bds = 0;
132         struct rte_mbuf *m_seg;
133         struct bnxt_sw_tx_bd *tx_buf;
134         static const uint32_t lhint_arr[4] = {
135                 TX_BD_LONG_FLAGS_LHINT_LT512,
136                 TX_BD_LONG_FLAGS_LHINT_LT1K,
137                 TX_BD_LONG_FLAGS_LHINT_LT2K,
138                 TX_BD_LONG_FLAGS_LHINT_LT2K
139         };
140
141         if (tx_pkt->ol_flags & (PKT_TX_TCP_SEG | PKT_TX_TCP_CKSUM |
142                                 PKT_TX_UDP_CKSUM | PKT_TX_IP_CKSUM |
143                                 PKT_TX_VLAN_PKT | PKT_TX_OUTER_IP_CKSUM |
144                                 PKT_TX_TUNNEL_GRE | PKT_TX_TUNNEL_VXLAN |
145                                 PKT_TX_TUNNEL_GENEVE))
146                 long_bd = true;
147
148         nr_bds = long_bd + tx_pkt->nb_segs;
149         if (unlikely(bnxt_tx_avail(txq) < nr_bds))
150                 return -ENOMEM;
151
152         /* Check if number of Tx descriptors is above HW limit */
153         if (unlikely(nr_bds > BNXT_MAX_TSO_SEGS)) {
154                 PMD_DRV_LOG(ERR,
155                             "Num descriptors %d exceeds HW limit\n", nr_bds);
156                 return -ENOSPC;
157         }
158
159         /* If packet length is less than minimum packet size, pad it */
160         if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) < BNXT_MIN_PKT_SIZE)) {
161                 uint8_t pad = BNXT_MIN_PKT_SIZE - rte_pktmbuf_pkt_len(tx_pkt);
162                 char *seg = rte_pktmbuf_append(tx_pkt, pad);
163
164                 if (!seg) {
165                         PMD_DRV_LOG(ERR,
166                                     "Failed to pad mbuf by %d bytes\n",
167                                     pad);
168                         return -ENOMEM;
169                 }
170
171                 /* Note: data_len, pkt len are updated in rte_pktmbuf_append */
172                 memset(seg, 0, pad);
173         }
174
175         /* Check non zero data_len */
176         RTE_VERIFY(tx_pkt->data_len);
177
178         tx_buf = &txr->tx_buf_ring[txr->tx_prod];
179         tx_buf->mbuf = tx_pkt;
180         tx_buf->nr_bds = nr_bds;
181
182         txbd = &txr->tx_desc_ring[txr->tx_prod];
183         txbd->opaque = *coal_pkts;
184         txbd->flags_type = nr_bds << TX_BD_LONG_FLAGS_BD_CNT_SFT;
185         txbd->flags_type |= TX_BD_SHORT_FLAGS_COAL_NOW;
186         txbd->flags_type |= TX_BD_LONG_FLAGS_NO_CMPL;
187         txbd->len = tx_pkt->data_len;
188         if (tx_pkt->pkt_len >= 2014)
189                 txbd->flags_type |= TX_BD_LONG_FLAGS_LHINT_GTE2K;
190         else
191                 txbd->flags_type |= lhint_arr[tx_pkt->pkt_len >> 9];
192         txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(tx_buf->mbuf));
193         *last_txbd = txbd;
194
195         if (long_bd) {
196                 txbd->flags_type |= TX_BD_LONG_TYPE_TX_BD_LONG;
197                 vlan_tag_flags = 0;
198                 cfa_action = 0;
199                 if (tx_buf->mbuf->ol_flags & PKT_TX_VLAN_PKT) {
200                         /* shurd: Should this mask at
201                          * TX_BD_LONG_CFA_META_VLAN_VID_MASK?
202                          */
203                         vlan_tag_flags = TX_BD_LONG_CFA_META_KEY_VLAN_TAG |
204                                 tx_buf->mbuf->vlan_tci;
205                         /* Currently supports 8021Q, 8021AD vlan offloads
206                          * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
207                          */
208                         /* DPDK only supports 802.11q VLAN packets */
209                         vlan_tag_flags |=
210                                         TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
211                 }
212
213                 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
214
215                 txbd1 = (struct tx_bd_long_hi *)
216                                         &txr->tx_desc_ring[txr->tx_prod];
217                 txbd1->lflags = 0;
218                 txbd1->cfa_meta = vlan_tag_flags;
219                 txbd1->cfa_action = cfa_action;
220
221                 if (tx_pkt->ol_flags & PKT_TX_TCP_SEG) {
222                         uint16_t hdr_size;
223
224                         /* TSO */
225                         txbd1->lflags |= TX_BD_LONG_LFLAGS_LSO |
226                                          TX_BD_LONG_LFLAGS_T_IPID;
227                         hdr_size = tx_pkt->l2_len + tx_pkt->l3_len +
228                                         tx_pkt->l4_len + tx_pkt->outer_l2_len +
229                                         tx_pkt->outer_l3_len;
230                         /* The hdr_size is multiple of 16bit units not 8bit.
231                          * Hence divide by 2.
232                          */
233                         txbd1->hdr_size = hdr_size >> 1;
234                         txbd1->mss = tx_pkt->tso_segsz;
235                         RTE_VERIFY(txbd1->mss);
236
237                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_UDP_CKSUM) ==
238                            PKT_TX_OIP_IIP_TCP_UDP_CKSUM) {
239                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
240                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
241                         txbd1->mss = 0;
242                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_TCP_CKSUM) ==
243                            PKT_TX_OIP_IIP_TCP_CKSUM) {
244                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
245                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
246                         txbd1->mss = 0;
247                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_UDP_CKSUM) ==
248                            PKT_TX_OIP_IIP_UDP_CKSUM) {
249                         /* Outer IP, Inner IP, Inner TCP/UDP CSO */
250                         txbd1->lflags |= TX_BD_FLG_TIP_IP_TCP_UDP_CHKSUM;
251                         txbd1->mss = 0;
252                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_UDP_CKSUM) ==
253                            PKT_TX_IIP_TCP_UDP_CKSUM) {
254                         /* (Inner) IP, (Inner) TCP/UDP CSO */
255                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
256                         txbd1->mss = 0;
257                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_UDP_CKSUM) ==
258                            PKT_TX_IIP_UDP_CKSUM) {
259                         /* (Inner) IP, (Inner) TCP/UDP CSO */
260                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
261                         txbd1->mss = 0;
262                 } else if ((tx_pkt->ol_flags & PKT_TX_IIP_TCP_CKSUM) ==
263                            PKT_TX_IIP_TCP_CKSUM) {
264                         /* (Inner) IP, (Inner) TCP/UDP CSO */
265                         txbd1->lflags |= TX_BD_FLG_IP_TCP_UDP_CHKSUM;
266                         txbd1->mss = 0;
267                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_UDP_CKSUM) ==
268                            PKT_TX_OIP_TCP_UDP_CKSUM) {
269                         /* Outer IP, (Inner) TCP/UDP CSO */
270                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
271                         txbd1->mss = 0;
272                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_UDP_CKSUM) ==
273                            PKT_TX_OIP_UDP_CKSUM) {
274                         /* Outer IP, (Inner) TCP/UDP CSO */
275                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
276                         txbd1->mss = 0;
277                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_TCP_CKSUM) ==
278                            PKT_TX_OIP_TCP_CKSUM) {
279                         /* Outer IP, (Inner) TCP/UDP CSO */
280                         txbd1->lflags |= TX_BD_FLG_TIP_TCP_UDP_CHKSUM;
281                         txbd1->mss = 0;
282                 } else if ((tx_pkt->ol_flags & PKT_TX_OIP_IIP_CKSUM) ==
283                            PKT_TX_OIP_IIP_CKSUM) {
284                         /* Outer IP, Inner IP CSO */
285                         txbd1->lflags |= TX_BD_FLG_TIP_IP_CHKSUM;
286                         txbd1->mss = 0;
287                 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_UDP_CKSUM) ==
288                            PKT_TX_TCP_UDP_CKSUM) {
289                         /* TCP/UDP CSO */
290                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
291                         txbd1->mss = 0;
292                 } else if ((tx_pkt->ol_flags & PKT_TX_TCP_CKSUM) ==
293                            PKT_TX_TCP_CKSUM) {
294                         /* TCP/UDP CSO */
295                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
296                         txbd1->mss = 0;
297                 } else if ((tx_pkt->ol_flags & PKT_TX_UDP_CKSUM) ==
298                            PKT_TX_UDP_CKSUM) {
299                         /* TCP/UDP CSO */
300                         txbd1->lflags |= TX_BD_LONG_LFLAGS_TCP_UDP_CHKSUM;
301                         txbd1->mss = 0;
302                 } else if ((tx_pkt->ol_flags & PKT_TX_IP_CKSUM) ==
303                            PKT_TX_IP_CKSUM) {
304                         /* IP CSO */
305                         txbd1->lflags |= TX_BD_LONG_LFLAGS_IP_CHKSUM;
306                         txbd1->mss = 0;
307                 } else if ((tx_pkt->ol_flags & PKT_TX_OUTER_IP_CKSUM) ==
308                            PKT_TX_OUTER_IP_CKSUM) {
309                         /* IP CSO */
310                         txbd1->lflags |= TX_BD_LONG_LFLAGS_T_IP_CHKSUM;
311                         txbd1->mss = 0;
312                 }
313         } else {
314                 txbd->flags_type |= TX_BD_SHORT_TYPE_TX_BD_SHORT;
315         }
316
317         m_seg = tx_pkt->next;
318         while (m_seg) {
319                 /* Check non zero data_len */
320                 RTE_VERIFY(m_seg->data_len);
321                 txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
322                 tx_buf = &txr->tx_buf_ring[txr->tx_prod];
323                 tx_buf->mbuf = m_seg;
324
325                 txbd = &txr->tx_desc_ring[txr->tx_prod];
326                 txbd->address = rte_cpu_to_le_64(rte_mbuf_data_iova(m_seg));
327                 txbd->flags_type = TX_BD_SHORT_TYPE_TX_BD_SHORT;
328                 txbd->len = m_seg->data_len;
329
330                 m_seg = m_seg->next;
331         }
332
333         txbd->flags_type |= TX_BD_LONG_FLAGS_PACKET_END;
334
335         txr->tx_prod = RING_NEXT(txr->tx_ring_struct, txr->tx_prod);
336
337         return 0;
338 }
339
340 static void bnxt_tx_cmp(struct bnxt_tx_queue *txq, int nr_pkts)
341 {
342         struct bnxt_tx_ring_info *txr = txq->tx_ring;
343         struct rte_mempool *pool = NULL;
344         struct rte_mbuf **free = txq->free;
345         uint16_t cons = txr->tx_cons;
346         unsigned int blk = 0;
347         int i, j;
348
349         for (i = 0; i < nr_pkts; i++) {
350                 struct rte_mbuf *mbuf;
351                 struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[cons];
352                 unsigned short nr_bds = tx_buf->nr_bds;
353
354                 for (j = 0; j < nr_bds; j++) {
355                         mbuf = tx_buf->mbuf;
356                         tx_buf->mbuf = NULL;
357                         cons = RING_NEXT(txr->tx_ring_struct, cons);
358                         tx_buf = &txr->tx_buf_ring[cons];
359                         if (!mbuf)      /* long_bd's tx_buf ? */
360                                 continue;
361
362                         mbuf = rte_pktmbuf_prefree_seg(mbuf);
363                         if (unlikely(!mbuf))
364                                 continue;
365
366                         /* EW - no need to unmap DMA memory? */
367
368                         if (likely(mbuf->pool == pool)) {
369                                 /* Add mbuf to the bulk free array */
370                                 free[blk++] = mbuf;
371                         } else {
372                                 /* Found an mbuf from a different pool. Free
373                                  * mbufs accumulated so far to the previous
374                                  * pool
375                                  */
376                                 if (likely(pool != NULL))
377                                         rte_mempool_put_bulk(pool,
378                                                              (void *)free,
379                                                              blk);
380
381                                 /* Start accumulating mbufs in a new pool */
382                                 free[0] = mbuf;
383                                 pool = mbuf->pool;
384                                 blk = 1;
385                         }
386                 }
387         }
388         if (blk)
389                 rte_mempool_put_bulk(pool, (void *)free, blk);
390
391         txr->tx_cons = cons;
392 }
393
394 static int bnxt_handle_tx_cp(struct bnxt_tx_queue *txq)
395 {
396         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
397         uint32_t raw_cons = cpr->cp_raw_cons;
398         uint32_t cons;
399         uint32_t nb_tx_pkts = 0;
400         struct tx_cmpl *txcmp;
401         struct cmpl_base *cp_desc_ring = cpr->cp_desc_ring;
402         struct bnxt_ring *cp_ring_struct = cpr->cp_ring_struct;
403         uint32_t ring_mask = cp_ring_struct->ring_mask;
404         uint32_t opaque = 0;
405
406         if (bnxt_tx_bds_in_hw(txq) < txq->tx_free_thresh)
407                 return 0;
408
409         do {
410                 cons = RING_CMPL(ring_mask, raw_cons);
411                 txcmp = (struct tx_cmpl *)&cpr->cp_desc_ring[cons];
412                 rte_prefetch_non_temporal(&cp_desc_ring[(cons + 2) &
413                                                         ring_mask]);
414
415                 if (!CMPL_VALID(txcmp, cpr->valid))
416                         break;
417                 opaque = rte_cpu_to_le_32(txcmp->opaque);
418                 NEXT_CMPL(cpr, cons, cpr->valid, 1);
419                 rte_prefetch0(&cp_desc_ring[cons]);
420
421                 if (CMP_TYPE(txcmp) == TX_CMPL_TYPE_TX_L2)
422                         nb_tx_pkts += opaque;
423                 else
424                         RTE_LOG_DP(ERR, PMD,
425                                         "Unhandled CMP type %02x\n",
426                                         CMP_TYPE(txcmp));
427                 raw_cons = cons;
428         } while (nb_tx_pkts < ring_mask);
429
430         if (nb_tx_pkts) {
431                 bnxt_tx_cmp(txq, nb_tx_pkts);
432                 cpr->cp_raw_cons = raw_cons;
433                 B_CP_DB(cpr, cpr->cp_raw_cons, ring_mask);
434         }
435
436         return nb_tx_pkts;
437 }
438
439 uint16_t bnxt_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
440                                uint16_t nb_pkts)
441 {
442         int rc;
443         uint16_t nb_tx_pkts = 0;
444         uint16_t coal_pkts = 0;
445         struct bnxt_tx_queue *txq = tx_queue;
446         struct tx_bd_long *last_txbd = NULL;
447
448         /* Handle TX completions */
449         bnxt_handle_tx_cp(txq);
450
451         /* Tx queue was stopped; wait for it to be restarted */
452         if (txq->tx_deferred_start) {
453                 PMD_DRV_LOG(DEBUG, "Tx q stopped;return\n");
454                 return 0;
455         }
456
457         /* Handle TX burst request */
458         for (nb_tx_pkts = 0; nb_tx_pkts < nb_pkts; nb_tx_pkts++) {
459                 coal_pkts++;
460                 rc = bnxt_start_xmit(tx_pkts[nb_tx_pkts], txq,
461                                      &coal_pkts, &last_txbd);
462
463                 if (unlikely(rc))
464                         break;
465         }
466
467         if (likely(nb_tx_pkts)) {
468                 /* Request a completion on the last packet */
469                 last_txbd->flags_type &= ~TX_BD_LONG_FLAGS_NO_CMPL;
470                 B_TX_DB(txq->tx_ring->tx_doorbell, txq->tx_ring->tx_prod);
471         }
472
473         return nb_tx_pkts;
474 }
475
476 int bnxt_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
477 {
478         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
479         struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
480
481         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
482         txq->tx_deferred_start = false;
483         PMD_DRV_LOG(DEBUG, "Tx queue started\n");
484
485         return 0;
486 }
487
488 int bnxt_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
489 {
490         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
491         struct bnxt_tx_queue *txq = bp->tx_queues[tx_queue_id];
492
493         /* Handle TX completions */
494         bnxt_handle_tx_cp(txq);
495
496         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
497         txq->tx_deferred_start = true;
498         PMD_DRV_LOG(DEBUG, "Tx queue stopped\n");
499
500         return 0;
501 }