1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2018 Broadcom
8 #include <rte_memzone.h>
9 #include <rte_malloc.h>
12 #include "bnxt_vnic.h"
13 #include "hsi_struct_def_dpdk.h"
19 static void prandom_bytes(void *dest_ptr, size_t len)
21 char *dest = (char *)dest_ptr;
31 memcpy(dest, &rb, len);
38 void bnxt_init_vnics(struct bnxt *bp)
40 struct bnxt_vnic_info *vnic;
44 max_vnics = bp->max_vnics;
45 STAILQ_INIT(&bp->free_vnic_list);
46 for (i = 0; i < max_vnics; i++) {
47 vnic = &bp->vnic_info[i];
48 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
49 vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
50 vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
51 vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
53 for (j = 0; j < MAX_QUEUES_PER_VNIC; j++)
54 vnic->fw_grp_ids[j] = (uint16_t)HWRM_NA_SIGNATURE;
56 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
57 STAILQ_INIT(&vnic->filter);
58 STAILQ_INIT(&vnic->flow_list);
59 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
61 for (i = 0; i < MAX_FF_POOLS; i++)
62 STAILQ_INIT(&bp->ff_pool[i]);
65 int bnxt_free_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic,
68 struct bnxt_vnic_info *temp;
70 temp = STAILQ_FIRST(&bp->ff_pool[pool]);
73 STAILQ_REMOVE(&bp->ff_pool[pool], vnic,
74 bnxt_vnic_info, next);
75 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
76 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic,
80 temp = STAILQ_NEXT(temp, next);
82 PMD_DRV_LOG(ERR, "VNIC %p is not found in pool[%d]\n", vnic, pool);
86 struct bnxt_vnic_info *bnxt_alloc_vnic(struct bnxt *bp)
88 struct bnxt_vnic_info *vnic;
90 /* Find the 1st unused vnic from the free_vnic_list pool*/
91 vnic = STAILQ_FIRST(&bp->free_vnic_list);
93 PMD_DRV_LOG(ERR, "No more free VNIC resources\n");
96 STAILQ_REMOVE_HEAD(&bp->free_vnic_list, next);
100 void bnxt_free_all_vnics(struct bnxt *bp)
102 struct bnxt_vnic_info *temp, *next;
105 for (i = 0; i < MAX_FF_POOLS; i++) {
106 temp = STAILQ_FIRST(&bp->ff_pool[i]);
108 next = STAILQ_NEXT(temp, next);
109 STAILQ_REMOVE(&bp->ff_pool[i], temp, bnxt_vnic_info,
111 STAILQ_INSERT_TAIL(&bp->free_vnic_list, temp, next);
117 void bnxt_free_vnic_attributes(struct bnxt *bp)
119 struct bnxt_vnic_info *vnic;
121 STAILQ_FOREACH(vnic, &bp->free_vnic_list, next) {
122 if (vnic->rss_table) {
123 /* 'Unreserve' the rss_table */
126 vnic->rss_table = NULL;
129 if (vnic->rss_hash_key) {
130 /* 'Unreserve' the rss_hash_key */
133 vnic->rss_hash_key = NULL;
138 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
140 struct bnxt_vnic_info *vnic;
141 struct rte_pci_device *pdev = bp->pdev;
142 const struct rte_memzone *mz;
143 char mz_name[RTE_MEMZONE_NAMESIZE];
144 uint32_t entry_length = RTE_CACHE_LINE_ROUNDUP(
145 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table) +
147 BNXT_MAX_MC_ADDRS * ETHER_ADDR_LEN);
150 rte_iova_t mz_phys_addr;
152 max_vnics = bp->max_vnics;
153 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
154 "bnxt_%04x:%02x:%02x:%02x_vnicattr", pdev->addr.domain,
155 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
156 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
157 mz = rte_memzone_lookup(mz_name);
159 mz = rte_memzone_reserve(mz_name,
160 entry_length * max_vnics, SOCKET_ID_ANY,
162 RTE_MEMZONE_SIZE_HINT_ONLY |
163 RTE_MEMZONE_IOVA_CONTIG);
167 mz_phys_addr = mz->iova;
168 if ((unsigned long)mz->addr == mz_phys_addr) {
170 "Memzone physical address same as virtual.\n");
172 "Using rte_mem_virt2iova()\n");
173 mz_phys_addr = rte_mem_virt2iova(mz->addr);
174 if (mz_phys_addr == 0) {
176 "unable to map vnic address to physical memory\n");
181 for (i = 0; i < max_vnics; i++) {
182 vnic = &bp->vnic_info[i];
184 /* Allocate rss table and hash key */
186 (void *)((char *)mz->addr + (entry_length * i));
187 memset(vnic->rss_table, -1, entry_length);
189 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
190 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
191 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table));
193 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
194 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
195 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
197 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
204 void bnxt_free_vnic_mem(struct bnxt *bp)
206 struct bnxt_vnic_info *vnic;
207 uint16_t max_vnics, i;
209 if (bp->vnic_info == NULL)
212 max_vnics = bp->max_vnics;
213 for (i = 0; i < max_vnics; i++) {
214 vnic = &bp->vnic_info[i];
215 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
216 PMD_DRV_LOG(ERR, "VNIC is not freed yet!\n");
217 /* TODO Call HWRM to free VNIC */
221 rte_free(bp->vnic_info);
222 bp->vnic_info = NULL;
225 int bnxt_alloc_vnic_mem(struct bnxt *bp)
227 struct bnxt_vnic_info *vnic_mem;
230 max_vnics = bp->max_vnics;
231 /* Allocate memory for VNIC pool and filter pool */
232 vnic_mem = rte_zmalloc("bnxt_vnic_info",
233 max_vnics * sizeof(struct bnxt_vnic_info), 0);
234 if (vnic_mem == NULL) {
235 PMD_DRV_LOG(ERR, "Failed to alloc memory for %d VNICs",
239 bp->vnic_info = vnic_mem;