New upstream version 16.11.7
[deb_dpdk.git] / drivers / net / bnxt / bnxt_ring.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 <rte_memzone.h>
35
36 #include "bnxt.h"
37 #include "bnxt_cpr.h"
38 #include "bnxt_hwrm.h"
39 #include "bnxt_ring.h"
40 #include "bnxt_rxq.h"
41 #include "bnxt_rxr.h"
42 #include "bnxt_txq.h"
43 #include "bnxt_txr.h"
44
45 #include "hsi_struct_def_dpdk.h"
46
47 /*
48  * Generic ring handling
49  */
50
51 void bnxt_free_ring(struct bnxt_ring *ring)
52 {
53         if (ring->vmem_size && *ring->vmem) {
54                 memset((char *)*ring->vmem, 0, ring->vmem_size);
55                 *ring->vmem = NULL;
56         }
57         ring->mem_zone = NULL;
58 }
59
60 /*
61  * Ring groups
62  */
63
64 int bnxt_init_ring_grps(struct bnxt *bp)
65 {
66         unsigned int i;
67
68         //One slot is still consumed by Default ring.
69         if (bp->max_ring_grps < 1 + bp->rx_cp_nr_rings)
70                 return -ENOMEM;
71
72         for (i = 0; i < bp->max_ring_grps; i++)
73                 memset(&bp->grp_info[i], (uint8_t)HWRM_NA_SIGNATURE,
74                        sizeof(struct bnxt_ring_grp_info));
75
76         return 0;
77 }
78
79 /*
80  * Allocates a completion ring with vmem and stats optionally also allocating
81  * a TX and/or RX ring.  Passing NULL as tx_ring_info and/or rx_ring_info
82  * to not allocate them.
83  *
84  * Order in the allocation is:
85  * stats - Always non-zero length
86  * cp vmem - Always zero-length, supported for the bnxt_ring abstraction
87  * tx vmem - Only non-zero length if tx_ring_info is not NULL
88  * rx vmem - Only non-zero length if rx_ring_info is not NULL
89  * cp bd ring - Always non-zero length
90  * tx bd ring - Only non-zero length if tx_ring_info is not NULL
91  * rx bd ring - Only non-zero length if rx_ring_info is not NULL
92  */
93 int bnxt_alloc_rings(struct bnxt *bp, uint16_t qidx,
94                             struct bnxt_tx_queue *txq,
95                             struct bnxt_rx_queue *rxq,
96                             struct bnxt_cp_ring_info *cp_ring_info,
97                             const char *suffix)
98 {
99         struct bnxt_ring *cp_ring = cp_ring_info->cp_ring_struct;
100         struct bnxt_rx_ring_info *rx_ring_info = rxq ? rxq->rx_ring : NULL;
101         struct bnxt_tx_ring_info *tx_ring_info = txq ? txq->tx_ring : NULL;
102         struct bnxt_ring *tx_ring;
103         struct bnxt_ring *rx_ring;
104         struct rte_pci_device *pdev = bp->pdev;
105         const struct rte_memzone *mz = NULL;
106         char mz_name[RTE_MEMZONE_NAMESIZE];
107
108         int stats_len = (tx_ring_info || rx_ring_info) ?
109             RTE_CACHE_LINE_ROUNDUP(sizeof(struct ctx_hw_stats64)) : 0;
110
111         int cp_vmem_start = stats_len;
112         int cp_vmem_len = RTE_CACHE_LINE_ROUNDUP(cp_ring->vmem_size);
113
114         int tx_vmem_start = cp_vmem_start + cp_vmem_len;
115         int tx_vmem_len =
116             tx_ring_info ? RTE_CACHE_LINE_ROUNDUP(tx_ring_info->
117                                                 tx_ring_struct->vmem_size) : 0;
118
119         int rx_vmem_start = tx_vmem_start + tx_vmem_len;
120         int rx_vmem_len = rx_ring_info ?
121                 RTE_CACHE_LINE_ROUNDUP(rx_ring_info->
122                                                 rx_ring_struct->vmem_size) : 0;
123
124         int cp_ring_start = rx_vmem_start + rx_vmem_len;
125         int cp_ring_len = RTE_CACHE_LINE_ROUNDUP(cp_ring->ring_size *
126                                                  sizeof(struct cmpl_base));
127
128         int tx_ring_start = cp_ring_start + cp_ring_len;
129         int tx_ring_len = tx_ring_info ?
130             RTE_CACHE_LINE_ROUNDUP(tx_ring_info->tx_ring_struct->ring_size *
131                                    sizeof(struct tx_bd_long)) : 0;
132
133         int rx_ring_start = tx_ring_start + tx_ring_len;
134         int rx_ring_len =  rx_ring_info ?
135                 RTE_CACHE_LINE_ROUNDUP(rx_ring_info->rx_ring_struct->ring_size *
136                 sizeof(struct rx_prod_pkt_bd)) : 0;
137
138         int total_alloc_len = rx_ring_start + rx_ring_len;
139
140         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
141                  "bnxt_%04x:%02x:%02x:%02x-%04x_%s", pdev->addr.domain,
142                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function, qidx,
143                  suffix);
144         mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
145         mz = rte_memzone_lookup(mz_name);
146         if (!mz) {
147                 mz = rte_memzone_reserve(mz_name, total_alloc_len,
148                                          SOCKET_ID_ANY,
149                                          RTE_MEMZONE_2MB |
150                                          RTE_MEMZONE_SIZE_HINT_ONLY);
151                 if (mz == NULL)
152                         return -ENOMEM;
153         }
154         memset(mz->addr, 0, mz->len);
155
156         if (tx_ring_info) {
157                 txq->mz = mz;
158                 tx_ring = tx_ring_info->tx_ring_struct;
159
160                 tx_ring->bd = ((char *)mz->addr + tx_ring_start);
161                 tx_ring_info->tx_desc_ring = (struct tx_bd_long *)tx_ring->bd;
162                 tx_ring->bd_dma = mz->phys_addr + tx_ring_start;
163                 tx_ring_info->tx_desc_mapping = tx_ring->bd_dma;
164                 tx_ring->mem_zone = (const void *)mz;
165
166                 if (!tx_ring->bd)
167                         return -ENOMEM;
168                 if (tx_ring->vmem_size) {
169                         tx_ring->vmem =
170                             (void **)((char *)mz->addr + tx_vmem_start);
171                         tx_ring_info->tx_buf_ring =
172                             (struct bnxt_sw_tx_bd *)tx_ring->vmem;
173                 }
174         }
175
176         if (rx_ring_info) {
177                 rxq->mz = mz;
178                 rx_ring = rx_ring_info->rx_ring_struct;
179
180                 rx_ring->bd = ((char *)mz->addr + rx_ring_start);
181                 rx_ring_info->rx_desc_ring =
182                     (struct rx_prod_pkt_bd *)rx_ring->bd;
183                 rx_ring->bd_dma = mz->phys_addr + rx_ring_start;
184                 rx_ring_info->rx_desc_mapping = rx_ring->bd_dma;
185                 rx_ring->mem_zone = (const void *)mz;
186
187                 if (!rx_ring->bd)
188                         return -ENOMEM;
189                 if (rx_ring->vmem_size) {
190                         rx_ring->vmem =
191                             (void **)((char *)mz->addr + rx_vmem_start);
192                         rx_ring_info->rx_buf_ring =
193                             (struct bnxt_sw_rx_bd *)rx_ring->vmem;
194                 }
195         }
196
197         cp_ring->bd = ((char *)mz->addr + cp_ring_start);
198         cp_ring->bd_dma = mz->phys_addr + cp_ring_start;
199         cp_ring_info->cp_desc_ring = cp_ring->bd;
200         cp_ring_info->cp_desc_mapping = cp_ring->bd_dma;
201         cp_ring->mem_zone = (const void *)mz;
202
203         if (!cp_ring->bd)
204                 return -ENOMEM;
205         if (cp_ring->vmem_size)
206                 *cp_ring->vmem = ((char *)mz->addr + stats_len);
207         if (stats_len) {
208                 cp_ring_info->hw_stats = mz->addr;
209                 cp_ring_info->hw_stats_map = mz->phys_addr;
210         }
211         cp_ring_info->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
212         return 0;
213 }
214
215 /* ring_grp usage:
216  * [0] = default completion ring
217  * [1 -> +rx_cp_nr_rings] = rx_cp, rx rings
218  * [1+rx_cp_nr_rings + 1 -> +tx_cp_nr_rings] = tx_cp, tx rings
219  */
220 int bnxt_alloc_hwrm_rings(struct bnxt *bp)
221 {
222         unsigned int i;
223         int rc = 0;
224
225         /* Default completion ring */
226         {
227                 struct bnxt_cp_ring_info *cpr = bp->def_cp_ring;
228                 struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
229
230                 rc = bnxt_hwrm_ring_alloc(bp, cp_ring,
231                                           HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
232                                           0, HWRM_NA_SIGNATURE,
233                                           HWRM_NA_SIGNATURE);
234                 if (rc)
235                         goto err_out;
236                 cpr->cp_doorbell =
237                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr;
238                 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
239                 bp->grp_info[0].cp_fw_ring_id = cp_ring->fw_ring_id;
240         }
241
242         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
243                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
244                 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
245                 struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
246                 struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
247                 struct bnxt_ring *ring = rxr->rx_ring_struct;
248                 unsigned int idx = i + 1;
249
250                 /* Rx cmpl */
251                 rc = bnxt_hwrm_ring_alloc(bp, cp_ring,
252                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
253                                         idx, HWRM_NA_SIGNATURE,
254                                         HWRM_NA_SIGNATURE);
255                 if (rc)
256                         goto err_out;
257                 cpr->cp_doorbell =
258                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
259                     idx * 0x80;
260                 bp->grp_info[idx].cp_fw_ring_id = cp_ring->fw_ring_id;
261                 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
262
263                 /* Rx ring */
264                 rc = bnxt_hwrm_ring_alloc(bp, ring,
265                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_RX,
266                                         idx, cpr->hw_stats_ctx_id,
267                                         cp_ring->fw_ring_id);
268                 if (rc)
269                         goto err_out;
270                 rxr->rx_prod = 0;
271                 rxr->rx_doorbell =
272                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
273                     idx * 0x80;
274                 bp->grp_info[idx].rx_fw_ring_id = ring->fw_ring_id;
275                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
276                 if (bnxt_init_one_rx_ring(rxq)) {
277                         RTE_LOG(ERR, PMD, "bnxt_init_one_rx_ring failed!");
278                         bnxt_rx_queue_release_op(rxq);
279                         return -ENOMEM;
280                 }
281                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
282         }
283
284         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
285                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
286                 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
287                 struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
288                 struct bnxt_tx_ring_info *txr = txq->tx_ring;
289                 struct bnxt_ring *ring = txr->tx_ring_struct;
290                 unsigned int idx = 1 + bp->rx_cp_nr_rings + i;
291
292                 /* Tx cmpl */
293                 rc = bnxt_hwrm_ring_alloc(bp, cp_ring,
294                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
295                                         idx, HWRM_NA_SIGNATURE,
296                                         HWRM_NA_SIGNATURE);
297                 if (rc)
298                         goto err_out;
299
300                 cpr->cp_doorbell =
301                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
302                     idx * 0x80;
303                 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
304
305                 /* Tx ring */
306                 rc = bnxt_hwrm_ring_alloc(bp, ring,
307                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_TX,
308                                         idx, cpr->hw_stats_ctx_id,
309                                         cp_ring->fw_ring_id);
310                 if (rc)
311                         goto err_out;
312
313                 txr->tx_doorbell =
314                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
315                     idx * 0x80;
316         }
317
318 err_out:
319         return rc;
320 }