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 HWRM_VNIC_RSS_CFG_INPUT_HASH_MODE_FLAGS_DEFAULT;
55 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
56 STAILQ_INIT(&vnic->filter);
57 STAILQ_INIT(&vnic->flow_list);
58 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
62 struct bnxt_vnic_info *bnxt_alloc_vnic(struct bnxt *bp)
64 struct bnxt_vnic_info *vnic;
66 /* Find the 1st unused vnic from the free_vnic_list pool*/
67 vnic = STAILQ_FIRST(&bp->free_vnic_list);
69 PMD_DRV_LOG(ERR, "No more free VNIC resources\n");
72 STAILQ_REMOVE_HEAD(&bp->free_vnic_list, next);
76 void bnxt_free_all_vnics(struct bnxt *bp)
78 struct bnxt_vnic_info *temp;
81 for (i = 0; i < bp->nr_vnics; i++) {
82 temp = &bp->vnic_info[i];
83 STAILQ_INSERT_TAIL(&bp->free_vnic_list, temp, next);
87 void bnxt_free_vnic_attributes(struct bnxt *bp)
89 struct bnxt_vnic_info *vnic;
92 for (i = 0; i < bp->max_vnics; i++) {
93 vnic = &bp->vnic_info[i];
94 if (vnic->rss_table) {
95 /* 'Unreserve' the rss_table */
98 vnic->rss_table = NULL;
101 if (vnic->rss_hash_key) {
102 /* 'Unreserve' the rss_hash_key */
105 vnic->rss_hash_key = NULL;
110 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
112 struct bnxt_vnic_info *vnic;
113 struct rte_pci_device *pdev = bp->pdev;
114 const struct rte_memzone *mz;
115 char mz_name[RTE_MEMZONE_NAMESIZE];
116 uint32_t entry_length = RTE_CACHE_LINE_ROUNDUP(
117 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table) +
119 BNXT_MAX_MC_ADDRS * ETHER_ADDR_LEN);
122 rte_iova_t mz_phys_addr;
124 max_vnics = bp->max_vnics;
125 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
126 "bnxt_%04x:%02x:%02x:%02x_vnicattr", pdev->addr.domain,
127 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
128 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
129 mz = rte_memzone_lookup(mz_name);
131 mz = rte_memzone_reserve(mz_name,
132 entry_length * max_vnics, SOCKET_ID_ANY,
134 RTE_MEMZONE_SIZE_HINT_ONLY |
135 RTE_MEMZONE_IOVA_CONTIG);
139 mz_phys_addr = mz->iova;
140 if ((unsigned long)mz->addr == mz_phys_addr) {
142 "Memzone physical address same as virtual.\n");
144 "Using rte_mem_virt2iova()\n");
145 mz_phys_addr = rte_mem_virt2iova(mz->addr);
146 if (mz_phys_addr == 0) {
148 "unable to map vnic address to physical memory\n");
153 for (i = 0; i < max_vnics; i++) {
154 vnic = &bp->vnic_info[i];
156 /* Allocate rss table and hash key */
158 (void *)((char *)mz->addr + (entry_length * i));
159 memset(vnic->rss_table, -1, entry_length);
161 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
162 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
163 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table));
165 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
166 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
167 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
169 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
176 void bnxt_free_vnic_mem(struct bnxt *bp)
178 struct bnxt_vnic_info *vnic;
179 uint16_t max_vnics, i;
181 if (bp->vnic_info == NULL)
184 max_vnics = bp->max_vnics;
185 for (i = 0; i < max_vnics; i++) {
186 vnic = &bp->vnic_info[i];
187 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
188 PMD_DRV_LOG(ERR, "VNIC is not freed yet!\n");
189 /* TODO Call HWRM to free VNIC */
193 rte_free(bp->vnic_info);
194 bp->vnic_info = NULL;
197 int bnxt_alloc_vnic_mem(struct bnxt *bp)
199 struct bnxt_vnic_info *vnic_mem;
202 max_vnics = bp->max_vnics;
203 /* Allocate memory for VNIC pool and filter pool */
204 vnic_mem = rte_zmalloc("bnxt_vnic_info",
205 max_vnics * sizeof(struct bnxt_vnic_info), 0);
206 if (vnic_mem == NULL) {
207 PMD_DRV_LOG(ERR, "Failed to alloc memory for %d VNICs",
211 bp->vnic_info = vnic_mem;