net/bnxt: optimize Tx completion handling
[dpdk.git] / drivers / net / bnxt / bnxt_txq.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_malloc.h>
9
10 #include "bnxt.h"
11 #include "bnxt_ring.h"
12 #include "bnxt_txq.h"
13 #include "bnxt_txr.h"
14
15 /*
16  * TX Queues
17  */
18
19 void bnxt_free_txq_stats(struct bnxt_tx_queue *txq)
20 {
21         if (txq && txq->cp_ring && txq->cp_ring->hw_stats)
22                 txq->cp_ring->hw_stats = NULL;
23 }
24
25 static void bnxt_tx_queue_release_mbufs(struct bnxt_tx_queue *txq)
26 {
27         struct rte_mbuf **sw_ring;
28         uint16_t i;
29
30         if (!txq || !txq->tx_ring)
31                 return;
32
33         sw_ring = txq->tx_ring->tx_buf_ring;
34         if (sw_ring) {
35                 for (i = 0; i < txq->tx_ring->tx_ring_struct->ring_size; i++) {
36                         if (sw_ring[i]) {
37                                 rte_pktmbuf_free_seg(sw_ring[i]);
38                                 sw_ring[i] = NULL;
39                         }
40                 }
41         }
42 }
43
44 void bnxt_free_tx_mbufs(struct bnxt *bp)
45 {
46         struct bnxt_tx_queue *txq;
47         int i;
48
49         for (i = 0; i < (int)bp->tx_nr_rings; i++) {
50                 txq = bp->tx_queues[i];
51                 bnxt_tx_queue_release_mbufs(txq);
52         }
53 }
54
55 void bnxt_tx_queue_release_op(void *tx_queue)
56 {
57         struct bnxt_tx_queue *txq = (struct bnxt_tx_queue *)tx_queue;
58
59         if (txq) {
60                 if (is_bnxt_in_error(txq->bp))
61                         return;
62
63                 /* Free TX ring hardware descriptors */
64                 bnxt_tx_queue_release_mbufs(txq);
65                 if (txq->tx_ring) {
66                         bnxt_free_ring(txq->tx_ring->tx_ring_struct);
67                         rte_free(txq->tx_ring->tx_ring_struct);
68                         rte_free(txq->tx_ring);
69                 }
70
71                 /* Free TX completion ring hardware descriptors */
72                 if (txq->cp_ring) {
73                         bnxt_free_ring(txq->cp_ring->cp_ring_struct);
74                         rte_free(txq->cp_ring->cp_ring_struct);
75                         rte_free(txq->cp_ring);
76                 }
77
78                 bnxt_free_txq_stats(txq);
79                 rte_memzone_free(txq->mz);
80                 txq->mz = NULL;
81
82                 rte_free(txq->free);
83                 rte_free(txq);
84         }
85 }
86
87 int bnxt_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
88                                uint16_t queue_idx,
89                                uint16_t nb_desc,
90                                unsigned int socket_id,
91                                const struct rte_eth_txconf *tx_conf)
92 {
93         struct bnxt *bp = eth_dev->data->dev_private;
94         struct bnxt_tx_queue *txq;
95         int rc = 0;
96
97         rc = is_bnxt_in_error(bp);
98         if (rc)
99                 return rc;
100
101         if (queue_idx >= bnxt_max_rings(bp)) {
102                 PMD_DRV_LOG(ERR,
103                         "Cannot create Tx ring %d. Only %d rings available\n",
104                         queue_idx, bp->max_tx_rings);
105                 return -EINVAL;
106         }
107
108         if (nb_desc < BNXT_MIN_RING_DESC || nb_desc > MAX_TX_DESC_CNT) {
109                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
110                 return -EINVAL;
111         }
112
113         if (eth_dev->data->tx_queues) {
114                 txq = eth_dev->data->tx_queues[queue_idx];
115                 if (txq) {
116                         bnxt_tx_queue_release_op(txq);
117                         txq = NULL;
118                 }
119         }
120         txq = rte_zmalloc_socket("bnxt_tx_queue", sizeof(struct bnxt_tx_queue),
121                                  RTE_CACHE_LINE_SIZE, socket_id);
122         if (!txq) {
123                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
124                 return -ENOMEM;
125         }
126
127         txq->free = rte_zmalloc_socket(NULL,
128                                        sizeof(struct rte_mbuf *) * nb_desc,
129                                        RTE_CACHE_LINE_SIZE, socket_id);
130         if (!txq->free) {
131                 PMD_DRV_LOG(ERR, "allocation of tx mbuf free array failed!");
132                 rc = -ENOMEM;
133                 goto err;
134         }
135         txq->bp = bp;
136         txq->nb_tx_desc = nb_desc;
137         txq->tx_free_thresh =
138                 RTE_MIN(rte_align32pow2(nb_desc) / 4, RTE_BNXT_MAX_TX_BURST);
139         txq->offloads = eth_dev->data->dev_conf.txmode.offloads |
140                         tx_conf->offloads;
141
142         txq->tx_deferred_start = tx_conf->tx_deferred_start;
143
144         rc = bnxt_init_tx_ring_struct(txq, socket_id);
145         if (rc)
146                 goto err;
147
148         txq->queue_id = queue_idx;
149         txq->port_id = eth_dev->data->port_id;
150
151         /* Allocate TX ring hardware descriptors */
152         if (bnxt_alloc_rings(bp, queue_idx, txq, NULL, txq->cp_ring, NULL,
153                              "txr")) {
154                 PMD_DRV_LOG(ERR, "ring_dma_zone_reserve for tx_ring failed!");
155                 rc = -ENOMEM;
156                 goto err;
157         }
158
159         if (bnxt_init_one_tx_ring(txq)) {
160                 PMD_DRV_LOG(ERR, "bnxt_init_one_tx_ring failed!");
161                 rc = -ENOMEM;
162                 goto err;
163         }
164
165         eth_dev->data->tx_queues[queue_idx] = txq;
166
167         if (txq->tx_deferred_start)
168                 txq->tx_started = false;
169         else
170                 txq->tx_started = true;
171
172         return 0;
173 err:
174         bnxt_tx_queue_release_op(txq);
175         return rc;
176 }