2bceb4dbaa25b8c4909ba81e66f456ab82eadf9d
[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         rte_memzone_free((const struct rte_memzone *)ring->mem_zone);
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_ring_info *tx_ring_info,
95                             struct bnxt_rx_ring_info *rx_ring_info,
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_ring *tx_ring;
101         struct bnxt_ring *rx_ring;
102         struct rte_pci_device *pdev = bp->pdev;
103         const struct rte_memzone *mz = NULL;
104         char mz_name[RTE_MEMZONE_NAMESIZE];
105
106         int stats_len = (tx_ring_info || rx_ring_info) ?
107             RTE_CACHE_LINE_ROUNDUP(sizeof(struct ctx_hw_stats64)) : 0;
108
109         int cp_vmem_start = stats_len;
110         int cp_vmem_len = RTE_CACHE_LINE_ROUNDUP(cp_ring->vmem_size);
111
112         int tx_vmem_start = cp_vmem_start + cp_vmem_len;
113         int tx_vmem_len =
114             tx_ring_info ? RTE_CACHE_LINE_ROUNDUP(tx_ring_info->
115                                                 tx_ring_struct->vmem_size) : 0;
116
117         int rx_vmem_start = tx_vmem_start + tx_vmem_len;
118         int rx_vmem_len = rx_ring_info ?
119                 RTE_CACHE_LINE_ROUNDUP(rx_ring_info->
120                                                 rx_ring_struct->vmem_size) : 0;
121
122         int cp_ring_start = rx_vmem_start + rx_vmem_len;
123         int cp_ring_len = RTE_CACHE_LINE_ROUNDUP(cp_ring->ring_size *
124                                                  sizeof(struct cmpl_base));
125
126         int tx_ring_start = cp_ring_start + cp_ring_len;
127         int tx_ring_len = tx_ring_info ?
128             RTE_CACHE_LINE_ROUNDUP(tx_ring_info->tx_ring_struct->ring_size *
129                                    sizeof(struct tx_bd_long)) : 0;
130
131         int rx_ring_start = tx_ring_start + tx_ring_len;
132         int rx_ring_len =  rx_ring_info ?
133                 RTE_CACHE_LINE_ROUNDUP(rx_ring_info->rx_ring_struct->ring_size *
134                 sizeof(struct rx_prod_pkt_bd)) : 0;
135
136         int total_alloc_len = rx_ring_start + rx_ring_len;
137
138         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
139                  "bnxt_%04x:%02x:%02x:%02x-%04x_%s", pdev->addr.domain,
140                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function, qidx,
141                  suffix);
142         mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
143         mz = rte_memzone_lookup(mz_name);
144         if (!mz) {
145                 mz = rte_memzone_reserve(mz_name, total_alloc_len,
146                                          SOCKET_ID_ANY,
147                                          RTE_MEMZONE_2MB |
148                                          RTE_MEMZONE_SIZE_HINT_ONLY);
149                 if (mz == NULL)
150                         return -ENOMEM;
151         }
152         memset(mz->addr, 0, mz->len);
153
154         if (tx_ring_info) {
155                 tx_ring = tx_ring_info->tx_ring_struct;
156
157                 tx_ring->bd = ((char *)mz->addr + tx_ring_start);
158                 tx_ring_info->tx_desc_ring = (struct tx_bd_long *)tx_ring->bd;
159                 tx_ring->bd_dma = mz->phys_addr + tx_ring_start;
160                 tx_ring_info->tx_desc_mapping = tx_ring->bd_dma;
161                 tx_ring->mem_zone = (const void *)mz;
162
163                 if (!tx_ring->bd)
164                         return -ENOMEM;
165                 if (tx_ring->vmem_size) {
166                         tx_ring->vmem =
167                             (void **)((char *)mz->addr + tx_vmem_start);
168                         tx_ring_info->tx_buf_ring =
169                             (struct bnxt_sw_tx_bd *)tx_ring->vmem;
170                 }
171         }
172
173         if (rx_ring_info) {
174                 rx_ring = rx_ring_info->rx_ring_struct;
175
176                 rx_ring->bd = ((char *)mz->addr + rx_ring_start);
177                 rx_ring_info->rx_desc_ring =
178                     (struct rx_prod_pkt_bd *)rx_ring->bd;
179                 rx_ring->bd_dma = mz->phys_addr + rx_ring_start;
180                 rx_ring_info->rx_desc_mapping = rx_ring->bd_dma;
181                 rx_ring->mem_zone = (const void *)mz;
182
183                 if (!rx_ring->bd)
184                         return -ENOMEM;
185                 if (rx_ring->vmem_size) {
186                         rx_ring->vmem =
187                             (void **)((char *)mz->addr + rx_vmem_start);
188                         rx_ring_info->rx_buf_ring =
189                             (struct bnxt_sw_rx_bd *)rx_ring->vmem;
190                 }
191         }
192
193         cp_ring->bd = ((char *)mz->addr + cp_ring_start);
194         cp_ring->bd_dma = mz->phys_addr + cp_ring_start;
195         cp_ring_info->cp_desc_ring = cp_ring->bd;
196         cp_ring_info->cp_desc_mapping = cp_ring->bd_dma;
197         cp_ring->mem_zone = (const void *)mz;
198
199         if (!cp_ring->bd)
200                 return -ENOMEM;
201         if (cp_ring->vmem_size)
202                 *cp_ring->vmem = ((char *)mz->addr + stats_len);
203         if (stats_len) {
204                 cp_ring_info->hw_stats = mz->addr;
205                 cp_ring_info->hw_stats_map = mz->phys_addr;
206         }
207         cp_ring_info->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
208         return 0;
209 }
210
211 /* ring_grp usage:
212  * [0] = default completion ring
213  * [1 -> +rx_cp_nr_rings] = rx_cp, rx rings
214  * [1+rx_cp_nr_rings + 1 -> +tx_cp_nr_rings] = tx_cp, tx rings
215  */
216 int bnxt_alloc_hwrm_rings(struct bnxt *bp)
217 {
218         unsigned int i;
219         int rc = 0;
220
221         /* Default completion ring */
222         {
223                 struct bnxt_cp_ring_info *cpr = bp->def_cp_ring;
224                 struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
225
226                 rc = bnxt_hwrm_ring_alloc(bp, cp_ring,
227                                           HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
228                                           0, HWRM_NA_SIGNATURE,
229                                           HWRM_NA_SIGNATURE);
230                 if (rc)
231                         goto err_out;
232                 cpr->cp_doorbell =
233                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr;
234                 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
235                 bp->grp_info[0].cp_fw_ring_id = cp_ring->fw_ring_id;
236         }
237
238         for (i = 0; i < bp->rx_cp_nr_rings; i++) {
239                 struct bnxt_rx_queue *rxq = bp->rx_queues[i];
240                 struct bnxt_cp_ring_info *cpr = rxq->cp_ring;
241                 struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
242                 struct bnxt_rx_ring_info *rxr = rxq->rx_ring;
243                 struct bnxt_ring *ring = rxr->rx_ring_struct;
244                 unsigned int idx = i + 1;
245
246                 /* Rx cmpl */
247                 rc = bnxt_hwrm_ring_alloc(bp, cp_ring,
248                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
249                                         idx, HWRM_NA_SIGNATURE,
250                                         HWRM_NA_SIGNATURE);
251                 if (rc)
252                         goto err_out;
253                 cpr->cp_doorbell =
254                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
255                     idx * 0x80;
256                 bp->grp_info[idx].cp_fw_ring_id = cp_ring->fw_ring_id;
257                 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
258
259                 /* Rx ring */
260                 rc = bnxt_hwrm_ring_alloc(bp, ring,
261                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_RX,
262                                         idx, cpr->hw_stats_ctx_id,
263                                         cp_ring->fw_ring_id);
264                 if (rc)
265                         goto err_out;
266                 rxr->rx_prod = 0;
267                 rxr->rx_doorbell =
268                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
269                     idx * 0x80;
270                 bp->grp_info[idx].rx_fw_ring_id = ring->fw_ring_id;
271                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
272                 if (bnxt_init_one_rx_ring(rxq)) {
273                         RTE_LOG(ERR, PMD, "bnxt_init_one_rx_ring failed!");
274                         bnxt_rx_queue_release_op(rxq);
275                         return -ENOMEM;
276                 }
277                 B_RX_DB(rxr->rx_doorbell, rxr->rx_prod);
278         }
279
280         for (i = 0; i < bp->tx_cp_nr_rings; i++) {
281                 struct bnxt_tx_queue *txq = bp->tx_queues[i];
282                 struct bnxt_cp_ring_info *cpr = txq->cp_ring;
283                 struct bnxt_ring *cp_ring = cpr->cp_ring_struct;
284                 struct bnxt_tx_ring_info *txr = txq->tx_ring;
285                 struct bnxt_ring *ring = txr->tx_ring_struct;
286                 unsigned int idx = 1 + bp->rx_cp_nr_rings + i;
287
288                 /* Tx cmpl */
289                 rc = bnxt_hwrm_ring_alloc(bp, cp_ring,
290                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
291                                         idx, HWRM_NA_SIGNATURE,
292                                         HWRM_NA_SIGNATURE);
293                 if (rc)
294                         goto err_out;
295
296                 cpr->cp_doorbell =
297                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
298                     idx * 0x80;
299                 B_CP_DIS_DB(cpr, cpr->cp_raw_cons);
300
301                 /* Tx ring */
302                 rc = bnxt_hwrm_ring_alloc(bp, ring,
303                                         HWRM_RING_ALLOC_INPUT_RING_TYPE_TX,
304                                         idx, cpr->hw_stats_ctx_id,
305                                         cp_ring->fw_ring_id);
306                 if (rc)
307                         goto err_out;
308
309                 txr->tx_doorbell =
310                     (char *)bp->eth_dev->pci_dev->mem_resource[2].addr +
311                     idx * 0x80;
312         }
313
314 err_out:
315         return rc;
316 }