net/bnxt: support thor controller
[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         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 bnxt_sw_tx_bd *sw_ring;
29         uint16_t i;
30
31         if (!txq)
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].mbuf) {
38                                 rte_pktmbuf_free(sw_ring[i].mbuf);
39                                 sw_ring[i].mbuf = 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(void *tx_queue)
57 {
58         struct bnxt_tx_queue *txq = (struct bnxt_tx_queue *)tx_queue;
59
60         if (txq) {
61                 /* Free TX ring hardware descriptors */
62                 bnxt_tx_queue_release_mbufs(txq);
63                 bnxt_free_ring(txq->tx_ring->tx_ring_struct);
64
65                 /* Free TX completion ring hardware descriptors */
66                 bnxt_free_ring(txq->cp_ring->cp_ring_struct);
67
68                 bnxt_free_txq_stats(txq);
69                 rte_memzone_free(txq->mz);
70                 txq->mz = NULL;
71
72                 rte_free(txq->free);
73                 rte_free(txq);
74         }
75 }
76
77 int bnxt_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
78                                uint16_t queue_idx,
79                                uint16_t nb_desc,
80                                unsigned int socket_id,
81                                const struct rte_eth_txconf *tx_conf)
82 {
83         struct bnxt *bp = eth_dev->data->dev_private;
84         struct bnxt_tx_queue *txq;
85         int rc = 0;
86
87         if (queue_idx >= bp->max_tx_rings) {
88                 PMD_DRV_LOG(ERR,
89                         "Cannot create Tx ring %d. Only %d rings available\n",
90                         queue_idx, bp->max_tx_rings);
91                 return -EINVAL;
92         }
93
94         if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
95                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
96                 rc = -EINVAL;
97                 goto out;
98         }
99
100         if (eth_dev->data->tx_queues) {
101                 txq = eth_dev->data->tx_queues[queue_idx];
102                 if (txq) {
103                         bnxt_tx_queue_release_op(txq);
104                         txq = NULL;
105                 }
106         }
107         txq = rte_zmalloc_socket("bnxt_tx_queue", sizeof(struct bnxt_tx_queue),
108                                  RTE_CACHE_LINE_SIZE, socket_id);
109         if (!txq) {
110                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
111                 rc = -ENOMEM;
112                 goto out;
113         }
114
115         txq->free = rte_zmalloc_socket(NULL,
116                                        sizeof(struct rte_mbuf *) * nb_desc,
117                                        RTE_CACHE_LINE_SIZE, socket_id);
118         if (!txq->free) {
119                 PMD_DRV_LOG(ERR, "allocation of tx mbuf free array failed!");
120                 rte_free(txq);
121                 rc = -ENOMEM;
122                 goto out;
123         }
124         txq->bp = bp;
125         txq->nb_tx_desc = nb_desc;
126         txq->tx_free_thresh = tx_conf->tx_free_thresh;
127
128         rc = bnxt_init_tx_ring_struct(txq, socket_id);
129         if (rc)
130                 goto out;
131
132         txq->queue_id = queue_idx;
133         txq->port_id = eth_dev->data->port_id;
134
135         /* Allocate TX ring hardware descriptors */
136         if (bnxt_alloc_rings(bp, queue_idx, txq, NULL, txq->cp_ring,
137                         txq->nq_ring, "txr")) {
138                 PMD_DRV_LOG(ERR, "ring_dma_zone_reserve for tx_ring failed!");
139                 bnxt_tx_queue_release_op(txq);
140                 rc = -ENOMEM;
141                 goto out;
142         }
143
144         if (bnxt_init_one_tx_ring(txq)) {
145                 PMD_DRV_LOG(ERR, "bnxt_init_one_tx_ring failed!");
146                 bnxt_tx_queue_release_op(txq);
147                 rc = -ENOMEM;
148                 goto out;
149         }
150
151         eth_dev->data->tx_queues[queue_idx] = txq;
152
153 out:
154         return rc;
155 }