4 * Copyright(c) 2014-2015 Broadcom Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <rte_memzone.h>
37 #include <rte_malloc.h>
40 #include "bnxt_vnic.h"
41 #include "hsi_struct_def_dpdk.h"
47 static void prandom_bytes(void *dest_ptr, size_t len)
49 char *dest = (char *)dest_ptr;
59 memcpy(dest, &rb, len);
66 void bnxt_init_vnics(struct bnxt *bp)
68 struct bnxt_vnic_info *vnic;
73 struct bnxt_pf_info *pf = &bp->pf;
75 max_vnics = pf->max_vnics;
77 struct bnxt_vf_info *vf = &bp->vf;
79 max_vnics = vf->max_vnics;
81 STAILQ_INIT(&bp->free_vnic_list);
82 for (i = 0; i < max_vnics; i++) {
83 vnic = &bp->vnic_info[i];
84 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
85 vnic->fw_rss_cos_lb_ctx = (uint16_t)HWRM_NA_SIGNATURE;
86 vnic->ctx_is_rss_cos_lb = HW_CONTEXT_NONE;
88 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
89 STAILQ_INIT(&vnic->filter);
90 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
92 for (i = 0; i < MAX_FF_POOLS; i++)
93 STAILQ_INIT(&bp->ff_pool[i]);
96 int bnxt_free_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic,
99 struct bnxt_vnic_info *temp;
101 temp = STAILQ_FIRST(&bp->ff_pool[pool]);
104 STAILQ_REMOVE(&bp->ff_pool[pool], vnic,
105 bnxt_vnic_info, next);
106 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
107 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic,
111 temp = STAILQ_NEXT(temp, next);
113 RTE_LOG(ERR, PMD, "VNIC %p is not found in pool[%d]\n", vnic, pool);
117 struct bnxt_vnic_info *bnxt_alloc_vnic(struct bnxt *bp)
119 struct bnxt_vnic_info *vnic;
121 /* Find the 1st unused vnic from the free_vnic_list pool*/
122 vnic = STAILQ_FIRST(&bp->free_vnic_list);
124 RTE_LOG(ERR, PMD, "No more free VNIC resources\n");
127 STAILQ_REMOVE_HEAD(&bp->free_vnic_list, next);
131 void bnxt_free_all_vnics(struct bnxt *bp)
133 struct bnxt_vnic_info *temp, *next;
136 for (i = 0; i < MAX_FF_POOLS; i++) {
137 temp = STAILQ_FIRST(&bp->ff_pool[i]);
139 next = STAILQ_NEXT(temp, next);
140 STAILQ_REMOVE(&bp->ff_pool[i], temp, bnxt_vnic_info,
142 STAILQ_INSERT_TAIL(&bp->free_vnic_list, temp, next);
148 void bnxt_free_vnic_attributes(struct bnxt *bp)
150 struct bnxt_vnic_info *vnic;
152 STAILQ_FOREACH(vnic, &bp->free_vnic_list, next) {
153 if (vnic->rss_table) {
154 /* 'Unreserve' the rss_table */
157 vnic->rss_table = NULL;
160 if (vnic->rss_hash_key) {
161 /* 'Unreserve' the rss_hash_key */
164 vnic->rss_hash_key = NULL;
169 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
171 struct bnxt_vnic_info *vnic;
172 struct rte_pci_device *pdev = bp->pdev;
173 const struct rte_memzone *mz;
174 char mz_name[RTE_MEMZONE_NAMESIZE];
175 uint32_t entry_length = RTE_CACHE_LINE_ROUNDUP(
176 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table) +
182 struct bnxt_pf_info *pf = &bp->pf;
184 max_vnics = pf->max_vnics;
186 struct bnxt_vf_info *vf = &bp->vf;
188 max_vnics = vf->max_vnics;
190 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
191 "bnxt_%04x:%02x:%02x:%02x_vnicattr", pdev->addr.domain,
192 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
193 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
194 mz = rte_memzone_lookup(mz_name);
196 mz = rte_memzone_reserve(mz_name,
197 entry_length * max_vnics,
200 RTE_MEMZONE_SIZE_HINT_ONLY);
205 for (i = 0; i < max_vnics; i++) {
206 vnic = &bp->vnic_info[i];
208 /* Allocate rss table and hash key */
210 (void *)((char *)mz->addr + (entry_length * i));
211 memset(vnic->rss_table, -1, entry_length);
213 vnic->rss_table_dma_addr = mz->phys_addr + (entry_length * i);
214 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
215 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table));
217 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
218 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
224 void bnxt_free_vnic_mem(struct bnxt *bp)
226 struct bnxt_vnic_info *vnic;
227 uint16_t max_vnics, i;
229 if (bp->vnic_info == NULL)
233 struct bnxt_pf_info *pf = &bp->pf;
235 max_vnics = pf->max_vnics;
237 struct bnxt_vf_info *vf = &bp->vf;
239 max_vnics = vf->max_vnics;
241 for (i = 0; i < max_vnics; i++) {
242 vnic = &bp->vnic_info[i];
243 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
244 RTE_LOG(ERR, PMD, "VNIC is not freed yet!\n");
245 /* TODO Call HWRM to free VNIC */
249 rte_free(bp->vnic_info);
250 bp->vnic_info = NULL;
253 int bnxt_alloc_vnic_mem(struct bnxt *bp)
255 struct bnxt_vnic_info *vnic_mem;
259 struct bnxt_pf_info *pf = &bp->pf;
261 max_vnics = pf->max_vnics;
263 struct bnxt_vf_info *vf = &bp->vf;
265 max_vnics = vf->max_vnics;
267 /* Allocate memory for VNIC pool and filter pool */
268 vnic_mem = rte_zmalloc("bnxt_vnic_info",
269 max_vnics * sizeof(struct bnxt_vnic_info), 0);
270 if (vnic_mem == NULL) {
271 RTE_LOG(ERR, PMD, "Failed to alloc memory for %d VNICs",
275 bp->vnic_info = vnic_mem;