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;
72 max_vnics = bp->max_vnics;
73 STAILQ_INIT(&bp->free_vnic_list);
74 for (i = 0; i < max_vnics; i++) {
75 vnic = &bp->vnic_info[i];
76 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
77 vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
78 vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
79 vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
81 for (j = 0; j < MAX_QUEUES_PER_VNIC; j++)
82 vnic->fw_grp_ids[j] = (uint16_t)HWRM_NA_SIGNATURE;
84 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
85 STAILQ_INIT(&vnic->filter);
86 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
88 for (i = 0; i < MAX_FF_POOLS; i++)
89 STAILQ_INIT(&bp->ff_pool[i]);
92 int bnxt_free_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic,
95 struct bnxt_vnic_info *temp;
97 temp = STAILQ_FIRST(&bp->ff_pool[pool]);
100 STAILQ_REMOVE(&bp->ff_pool[pool], vnic,
101 bnxt_vnic_info, next);
102 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
103 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic,
107 temp = STAILQ_NEXT(temp, next);
109 RTE_LOG(ERR, PMD, "VNIC %p is not found in pool[%d]\n", vnic, pool);
113 struct bnxt_vnic_info *bnxt_alloc_vnic(struct bnxt *bp)
115 struct bnxt_vnic_info *vnic;
117 /* Find the 1st unused vnic from the free_vnic_list pool*/
118 vnic = STAILQ_FIRST(&bp->free_vnic_list);
120 RTE_LOG(ERR, PMD, "No more free VNIC resources\n");
123 STAILQ_REMOVE_HEAD(&bp->free_vnic_list, next);
127 void bnxt_free_all_vnics(struct bnxt *bp)
129 struct bnxt_vnic_info *temp, *next;
132 for (i = 0; i < MAX_FF_POOLS; i++) {
133 temp = STAILQ_FIRST(&bp->ff_pool[i]);
135 next = STAILQ_NEXT(temp, next);
136 STAILQ_REMOVE(&bp->ff_pool[i], temp, bnxt_vnic_info,
138 STAILQ_INSERT_TAIL(&bp->free_vnic_list, temp, next);
144 void bnxt_free_vnic_attributes(struct bnxt *bp)
146 struct bnxt_vnic_info *vnic;
148 STAILQ_FOREACH(vnic, &bp->free_vnic_list, next) {
149 if (vnic->rss_table) {
150 /* 'Unreserve' the rss_table */
153 vnic->rss_table = NULL;
156 if (vnic->rss_hash_key) {
157 /* 'Unreserve' the rss_hash_key */
160 vnic->rss_hash_key = NULL;
165 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
167 struct bnxt_vnic_info *vnic;
168 struct rte_pci_device *pdev = bp->pdev;
169 const struct rte_memzone *mz;
170 char mz_name[RTE_MEMZONE_NAMESIZE];
171 uint32_t entry_length = RTE_CACHE_LINE_ROUNDUP(
172 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table) +
174 BNXT_MAX_MC_ADDRS * ETHER_ADDR_LEN);
177 phys_addr_t mz_phys_addr;
179 max_vnics = bp->max_vnics;
180 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
181 "bnxt_%04x:%02x:%02x:%02x_vnicattr", pdev->addr.domain,
182 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
183 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
184 mz = rte_memzone_lookup(mz_name);
186 mz = rte_memzone_reserve(mz_name,
187 entry_length * max_vnics,
190 RTE_MEMZONE_SIZE_HINT_ONLY);
194 mz_phys_addr = mz->phys_addr;
195 if ((unsigned long)mz->addr == mz_phys_addr) {
196 RTE_LOG(WARNING, PMD,
197 "Memzone physical address same as virtual.\n");
198 RTE_LOG(WARNING, PMD,
199 "Using rte_mem_virt2phy()\n");
200 mz_phys_addr = rte_mem_virt2phy(mz->addr);
201 if (mz_phys_addr == 0) {
203 "unable to map vnic address to physical memory\n");
208 for (i = 0; i < max_vnics; i++) {
209 vnic = &bp->vnic_info[i];
211 /* Allocate rss table and hash key */
213 (void *)((char *)mz->addr + (entry_length * i));
214 memset(vnic->rss_table, -1, entry_length);
216 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
217 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
218 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table));
220 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
221 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
222 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
224 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
231 void bnxt_free_vnic_mem(struct bnxt *bp)
233 struct bnxt_vnic_info *vnic;
234 uint16_t max_vnics, i;
236 if (bp->vnic_info == NULL)
239 max_vnics = bp->max_vnics;
240 for (i = 0; i < max_vnics; i++) {
241 vnic = &bp->vnic_info[i];
242 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
243 RTE_LOG(ERR, PMD, "VNIC is not freed yet!\n");
244 /* TODO Call HWRM to free VNIC */
248 rte_free(bp->vnic_info);
249 bp->vnic_info = NULL;
252 int bnxt_alloc_vnic_mem(struct bnxt *bp)
254 struct bnxt_vnic_info *vnic_mem;
257 max_vnics = bp->max_vnics;
258 /* Allocate memory for VNIC pool and filter pool */
259 vnic_mem = rte_zmalloc("bnxt_vnic_info",
260 max_vnics * sizeof(struct bnxt_vnic_info), 0);
261 if (vnic_mem == NULL) {
262 RTE_LOG(ERR, PMD, "Failed to alloc memory for %d VNICs",
266 bp->vnic_info = vnic_mem;