net/bnxt: avoid freeing memzone multiple times
[dpdk.git] / drivers / net / bnxt / bnxt_txq.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_malloc.h>
9
10 #include "bnxt.h"
11 #include "bnxt_cpr.h"
12 #include "bnxt_ring.h"
13 #include "bnxt_txq.h"
14 #include "bnxt_txr.h"
15
16 /*
17  * TX Queues
18  */
19
20 void bnxt_free_txq_stats(struct bnxt_tx_queue *txq)
21 {
22         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
23
24         if (cpr->hw_stats)
25                 cpr->hw_stats = NULL;
26 }
27
28 static void bnxt_tx_queue_release_mbufs(struct bnxt_tx_queue *txq)
29 {
30         struct bnxt_sw_tx_bd *sw_ring;
31         uint16_t i;
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].mbuf) {
37                                 rte_pktmbuf_free(sw_ring[i].mbuf);
38                                 sw_ring[i].mbuf = 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                 /* Free TX ring hardware descriptors */
61                 bnxt_tx_queue_release_mbufs(txq);
62                 bnxt_free_ring(txq->tx_ring->tx_ring_struct);
63
64                 /* Free TX completion ring hardware descriptors */
65                 bnxt_free_ring(txq->cp_ring->cp_ring_struct);
66
67                 bnxt_free_txq_stats(txq);
68                 rte_memzone_free(txq->mz);
69                 txq->mz = NULL;
70
71                 rte_free(txq);
72         }
73 }
74
75 int bnxt_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
76                                uint16_t queue_idx,
77                                uint16_t nb_desc,
78                                unsigned int socket_id,
79                                const struct rte_eth_txconf *tx_conf)
80 {
81         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
82         struct bnxt_tx_queue *txq;
83         int rc = 0;
84
85         if (queue_idx >= bp->max_tx_rings) {
86                 PMD_DRV_LOG(ERR,
87                         "Cannot create Tx ring %d. Only %d rings available\n",
88                         queue_idx, bp->max_tx_rings);
89                 return -ENOSPC;
90         }
91
92         if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
93                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
94                 rc = -EINVAL;
95                 goto out;
96         }
97
98         if (eth_dev->data->tx_queues) {
99                 txq = eth_dev->data->tx_queues[queue_idx];
100                 if (txq) {
101                         bnxt_tx_queue_release_op(txq);
102                         txq = NULL;
103                 }
104         }
105         txq = rte_zmalloc_socket("bnxt_tx_queue", sizeof(struct bnxt_tx_queue),
106                                  RTE_CACHE_LINE_SIZE, socket_id);
107         if (!txq) {
108                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
109                 rc = -ENOMEM;
110                 goto out;
111         }
112         txq->bp = bp;
113         txq->nb_tx_desc = nb_desc;
114         txq->tx_free_thresh = tx_conf->tx_free_thresh;
115
116         rc = bnxt_init_tx_ring_struct(txq, socket_id);
117         if (rc)
118                 goto out;
119
120         txq->queue_id = queue_idx;
121         txq->port_id = eth_dev->data->port_id;
122
123         /* Allocate TX ring hardware descriptors */
124         if (bnxt_alloc_rings(bp, queue_idx, txq, NULL, txq->cp_ring,
125                         "txr")) {
126                 PMD_DRV_LOG(ERR, "ring_dma_zone_reserve for tx_ring failed!");
127                 bnxt_tx_queue_release_op(txq);
128                 rc = -ENOMEM;
129                 goto out;
130         }
131
132         if (bnxt_init_one_tx_ring(txq)) {
133                 PMD_DRV_LOG(ERR, "bnxt_init_one_tx_ring failed!");
134                 bnxt_tx_queue_release_op(txq);
135                 rc = -ENOMEM;
136                 goto out;
137         }
138
139         eth_dev->data->tx_queues[queue_idx] = txq;
140
141 out:
142         return rc;
143 }