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