net/bnxt: allocate Rx/Tx and completion rings
[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_ring.h"
39 #include "bnxt_rxr.h"
40 #include "bnxt_txr.h"
41
42 #include "hsi_struct_def_dpdk.h"
43
44 /*
45  * Generic ring handling
46  */
47
48 void bnxt_free_ring(struct bnxt_ring *ring)
49 {
50         if (ring->vmem_size && *ring->vmem) {
51                 memset((char *)*ring->vmem, 0, ring->vmem_size);
52                 *ring->vmem = NULL;
53         }
54         rte_memzone_free((const struct rte_memzone *)ring->mem_zone);
55 }
56
57 /*
58  * Allocates a completion ring with vmem and stats optionally also allocating
59  * a TX and/or RX ring.  Passing NULL as tx_ring_info and/or rx_ring_info
60  * to not allocate them.
61  *
62  * Order in the allocation is:
63  * stats - Always non-zero length
64  * cp vmem - Always zero-length, supported for the bnxt_ring abstraction
65  * tx vmem - Only non-zero length if tx_ring_info is not NULL
66  * rx vmem - Only non-zero length if rx_ring_info is not NULL
67  * cp bd ring - Always non-zero length
68  * tx bd ring - Only non-zero length if tx_ring_info is not NULL
69  * rx bd ring - Only non-zero length if rx_ring_info is not NULL
70  */
71 int bnxt_alloc_rings(struct bnxt *bp, uint16_t qidx,
72                             struct bnxt_tx_ring_info *tx_ring_info,
73                             struct bnxt_rx_ring_info *rx_ring_info,
74                             struct bnxt_cp_ring_info *cp_ring_info,
75                             const char *suffix)
76 {
77         struct bnxt_ring *cp_ring = cp_ring_info->cp_ring_struct;
78         struct bnxt_ring *tx_ring;
79         struct bnxt_ring *rx_ring;
80         struct rte_pci_device *pdev = bp->pdev;
81         const struct rte_memzone *mz = NULL;
82         char mz_name[RTE_MEMZONE_NAMESIZE];
83
84         int stats_len = (tx_ring_info || rx_ring_info) ?
85             RTE_CACHE_LINE_ROUNDUP(sizeof(struct ctx_hw_stats64)) : 0;
86
87         int cp_vmem_start = stats_len;
88         int cp_vmem_len = RTE_CACHE_LINE_ROUNDUP(cp_ring->vmem_size);
89
90         int tx_vmem_start = cp_vmem_start + cp_vmem_len;
91         int tx_vmem_len =
92             tx_ring_info ? RTE_CACHE_LINE_ROUNDUP(tx_ring_info->
93                                                 tx_ring_struct->vmem_size) : 0;
94
95         int rx_vmem_start = tx_vmem_start + tx_vmem_len;
96         int rx_vmem_len = rx_ring_info ?
97                 RTE_CACHE_LINE_ROUNDUP(rx_ring_info->
98                                                 rx_ring_struct->vmem_size) : 0;
99
100         int cp_ring_start = rx_vmem_start + rx_vmem_len;
101         int cp_ring_len = RTE_CACHE_LINE_ROUNDUP(cp_ring->ring_size *
102                                                  sizeof(struct cmpl_base));
103
104         int tx_ring_start = cp_ring_start + cp_ring_len;
105         int tx_ring_len = tx_ring_info ?
106             RTE_CACHE_LINE_ROUNDUP(tx_ring_info->tx_ring_struct->ring_size *
107                                    sizeof(struct tx_bd_long)) : 0;
108
109         int rx_ring_start = tx_ring_start + tx_ring_len;
110         int rx_ring_len =  rx_ring_info ?
111                 RTE_CACHE_LINE_ROUNDUP(rx_ring_info->rx_ring_struct->ring_size *
112                 sizeof(struct rx_prod_pkt_bd)) : 0;
113
114         int total_alloc_len = rx_ring_start + rx_ring_len;
115
116         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
117                  "bnxt_%04x:%02x:%02x:%02x-%04x_%s", pdev->addr.domain,
118                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function, qidx,
119                  suffix);
120         mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
121         mz = rte_memzone_lookup(mz_name);
122         if (!mz) {
123                 mz = rte_memzone_reserve(mz_name, total_alloc_len,
124                                          SOCKET_ID_ANY,
125                                          RTE_MEMZONE_2MB |
126                                          RTE_MEMZONE_SIZE_HINT_ONLY);
127                 if (mz == NULL)
128                         return -ENOMEM;
129         }
130         memset(mz->addr, 0, mz->len);
131
132         if (tx_ring_info) {
133                 tx_ring = tx_ring_info->tx_ring_struct;
134
135                 tx_ring->bd = ((char *)mz->addr + tx_ring_start);
136                 tx_ring_info->tx_desc_ring = (struct tx_bd_long *)tx_ring->bd;
137                 tx_ring->bd_dma = mz->phys_addr + tx_ring_start;
138                 tx_ring_info->tx_desc_mapping = tx_ring->bd_dma;
139                 tx_ring->mem_zone = (const void *)mz;
140
141                 if (!tx_ring->bd)
142                         return -ENOMEM;
143                 if (tx_ring->vmem_size) {
144                         tx_ring->vmem =
145                             (void **)((char *)mz->addr + tx_vmem_start);
146                         tx_ring_info->tx_buf_ring =
147                             (struct bnxt_sw_tx_bd *)tx_ring->vmem;
148                 }
149         }
150
151         if (rx_ring_info) {
152                 rx_ring = rx_ring_info->rx_ring_struct;
153
154                 rx_ring->bd = ((char *)mz->addr + rx_ring_start);
155                 rx_ring_info->rx_desc_ring =
156                     (struct rx_prod_pkt_bd *)rx_ring->bd;
157                 rx_ring->bd_dma = mz->phys_addr + rx_ring_start;
158                 rx_ring_info->rx_desc_mapping = rx_ring->bd_dma;
159                 rx_ring->mem_zone = (const void *)mz;
160
161                 if (!rx_ring->bd)
162                         return -ENOMEM;
163                 if (rx_ring->vmem_size) {
164                         rx_ring->vmem =
165                             (void **)((char *)mz->addr + rx_vmem_start);
166                         rx_ring_info->rx_buf_ring =
167                             (struct bnxt_sw_rx_bd *)rx_ring->vmem;
168                 }
169         }
170
171         cp_ring->bd = ((char *)mz->addr + cp_ring_start);
172         cp_ring->bd_dma = mz->phys_addr + cp_ring_start;
173         cp_ring_info->cp_desc_ring = cp_ring->bd;
174         cp_ring_info->cp_desc_mapping = cp_ring->bd_dma;
175         cp_ring->mem_zone = (const void *)mz;
176
177         if (!cp_ring->bd)
178                 return -ENOMEM;
179         if (cp_ring->vmem_size)
180                 *cp_ring->vmem = ((char *)mz->addr + stats_len);
181         if (stats_len) {
182                 cp_ring_info->hw_stats = mz->addr;
183                 cp_ring_info->hw_stats_map = mz->phys_addr;
184         }
185         cp_ring_info->hw_stats_ctx_id = HWRM_NA_SIGNATURE;
186         return 0;
187 }