New upstream version 18.02
[deb_dpdk.git] / drivers / net / bnxt / bnxt_txq.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) Broadcom Limited.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35
36 #include <rte_malloc.h>
37
38 #include "bnxt.h"
39 #include "bnxt_cpr.h"
40 #include "bnxt_ring.h"
41 #include "bnxt_txq.h"
42 #include "bnxt_txr.h"
43
44 /*
45  * TX Queues
46  */
47
48 void bnxt_free_txq_stats(struct bnxt_tx_queue *txq)
49 {
50         struct bnxt_cp_ring_info *cpr = txq->cp_ring;
51
52         if (cpr->hw_stats)
53                 cpr->hw_stats = NULL;
54 }
55
56 static void bnxt_tx_queue_release_mbufs(struct bnxt_tx_queue *txq)
57 {
58         struct bnxt_sw_tx_bd *sw_ring;
59         uint16_t i;
60
61         sw_ring = txq->tx_ring->tx_buf_ring;
62         if (sw_ring) {
63                 for (i = 0; i < txq->tx_ring->tx_ring_struct->ring_size; i++) {
64                         if (sw_ring[i].mbuf) {
65                                 rte_pktmbuf_free(sw_ring[i].mbuf);
66                                 sw_ring[i].mbuf = NULL;
67                         }
68                 }
69         }
70 }
71
72 void bnxt_free_tx_mbufs(struct bnxt *bp)
73 {
74         struct bnxt_tx_queue *txq;
75         int i;
76
77         for (i = 0; i < (int)bp->tx_nr_rings; i++) {
78                 txq = bp->tx_queues[i];
79                 bnxt_tx_queue_release_mbufs(txq);
80         }
81 }
82
83 void bnxt_tx_queue_release_op(void *tx_queue)
84 {
85         struct bnxt_tx_queue *txq = (struct bnxt_tx_queue *)tx_queue;
86
87         if (txq) {
88                 /* Free TX ring hardware descriptors */
89                 bnxt_tx_queue_release_mbufs(txq);
90                 bnxt_free_ring(txq->tx_ring->tx_ring_struct);
91
92                 /* Free TX completion ring hardware descriptors */
93                 bnxt_free_ring(txq->cp_ring->cp_ring_struct);
94
95                 bnxt_free_txq_stats(txq);
96
97                 rte_free(txq);
98         }
99 }
100
101 int bnxt_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
102                                uint16_t queue_idx,
103                                uint16_t nb_desc,
104                                unsigned int socket_id,
105                                const struct rte_eth_txconf *tx_conf)
106 {
107         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
108         struct bnxt_tx_queue *txq;
109         int rc = 0;
110
111         if (queue_idx >= bp->max_tx_rings) {
112                 PMD_DRV_LOG(ERR,
113                         "Cannot create Tx ring %d. Only %d rings available\n",
114                         queue_idx, bp->max_tx_rings);
115                 return -ENOSPC;
116         }
117
118         if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
119                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
120                 rc = -EINVAL;
121                 goto out;
122         }
123
124         if (eth_dev->data->tx_queues) {
125                 txq = eth_dev->data->tx_queues[queue_idx];
126                 if (txq) {
127                         bnxt_tx_queue_release_op(txq);
128                         txq = NULL;
129                 }
130         }
131         txq = rte_zmalloc_socket("bnxt_tx_queue", sizeof(struct bnxt_tx_queue),
132                                  RTE_CACHE_LINE_SIZE, socket_id);
133         if (!txq) {
134                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
135                 rc = -ENOMEM;
136                 goto out;
137         }
138         txq->bp = bp;
139         txq->nb_tx_desc = nb_desc;
140         txq->tx_free_thresh = tx_conf->tx_free_thresh;
141
142         rc = bnxt_init_tx_ring_struct(txq, socket_id);
143         if (rc)
144                 goto out;
145
146         txq->queue_id = queue_idx;
147         txq->port_id = eth_dev->data->port_id;
148
149         /* Allocate TX ring hardware descriptors */
150         if (bnxt_alloc_rings(bp, queue_idx, txq->tx_ring, NULL, txq->cp_ring,
151                         "txr")) {
152                 PMD_DRV_LOG(ERR, "ring_dma_zone_reserve for tx_ring failed!");
153                 bnxt_tx_queue_release_op(txq);
154                 rc = -ENOMEM;
155                 goto out;
156         }
157
158         if (bnxt_init_one_tx_ring(txq)) {
159                 PMD_DRV_LOG(ERR, "bnxt_init_one_tx_ring failed!");
160                 bnxt_tx_queue_release_op(txq);
161                 rc = -ENOMEM;
162                 goto out;
163         }
164
165         eth_dev->data->tx_queues[queue_idx] = txq;
166
167 out:
168         return rc;
169 }