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