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 void prandom_bytes(void *dest_ptr, size_t len)
21 char *dest = (char *)dest_ptr;
31 memcpy(dest, &rb, len);
38 static 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;
54 vnic->rx_queue_cnt = 0;
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 *vnic;
81 if (bp->vnic_info == NULL)
84 for (i = 0; i < bp->max_vnics; i++) {
85 vnic = &bp->vnic_info[i];
86 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
87 vnic->rx_queue_cnt = 0;
91 void bnxt_free_vnic_attributes(struct bnxt *bp)
93 struct bnxt_vnic_info *vnic;
96 if (bp->vnic_info == NULL)
99 for (i = 0; i < bp->max_vnics; i++) {
100 vnic = &bp->vnic_info[i];
101 if (vnic->rss_table) {
102 /* 'Unreserve' the rss_table */
105 vnic->rss_table = NULL;
108 if (vnic->rss_hash_key) {
109 /* 'Unreserve' the rss_hash_key */
112 vnic->rss_hash_key = NULL;
117 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
119 struct bnxt_vnic_info *vnic;
120 struct rte_pci_device *pdev = bp->pdev;
121 const struct rte_memzone *mz;
122 char mz_name[RTE_MEMZONE_NAMESIZE];
123 uint32_t entry_length;
124 size_t rss_table_size;
127 rte_iova_t mz_phys_addr;
129 entry_length = HW_HASH_KEY_SIZE +
130 BNXT_MAX_MC_ADDRS * RTE_ETHER_ADDR_LEN;
132 if (BNXT_CHIP_THOR(bp))
133 rss_table_size = BNXT_RSS_TBL_SIZE_THOR *
134 2 * sizeof(*vnic->rss_table);
136 rss_table_size = HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
138 entry_length = RTE_CACHE_LINE_ROUNDUP(entry_length + rss_table_size);
140 max_vnics = bp->max_vnics;
141 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
142 "bnxt_" PCI_PRI_FMT "_vnicattr", pdev->addr.domain,
143 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
144 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
145 mz = rte_memzone_lookup(mz_name);
147 mz = rte_memzone_reserve(mz_name,
148 entry_length * max_vnics, SOCKET_ID_ANY,
150 RTE_MEMZONE_SIZE_HINT_ONLY |
151 RTE_MEMZONE_IOVA_CONTIG);
155 mz_phys_addr = mz->iova;
157 for (i = 0; i < max_vnics; i++) {
158 vnic = &bp->vnic_info[i];
160 /* Allocate rss table and hash key */
162 (void *)((char *)mz->addr + (entry_length * i));
163 memset(vnic->rss_table, -1, entry_length);
165 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
166 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
169 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
171 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
173 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
175 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
181 void bnxt_free_vnic_mem(struct bnxt *bp)
183 struct bnxt_vnic_info *vnic;
184 uint16_t max_vnics, i;
186 if (bp->vnic_info == NULL)
189 max_vnics = bp->max_vnics;
190 for (i = 0; i < max_vnics; i++) {
191 vnic = &bp->vnic_info[i];
192 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
193 PMD_DRV_LOG(ERR, "VNIC is not freed yet!\n");
194 /* TODO Call HWRM to free VNIC */
198 rte_free(bp->vnic_info);
199 bp->vnic_info = NULL;
202 int bnxt_alloc_vnic_mem(struct bnxt *bp)
204 struct bnxt_vnic_info *vnic_mem;
207 max_vnics = bp->max_vnics;
208 /* Allocate memory for VNIC pool and filter pool */
209 vnic_mem = rte_zmalloc("bnxt_vnic_info",
210 max_vnics * sizeof(struct bnxt_vnic_info), 0);
211 if (vnic_mem == NULL) {
212 PMD_DRV_LOG(ERR, "Failed to alloc memory for %d VNICs",
216 bp->vnic_info = vnic_mem;
221 int bnxt_vnic_grp_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
223 uint32_t size = sizeof(*vnic->fw_grp_ids) * bp->max_ring_grps;
225 vnic->fw_grp_ids = rte_zmalloc("vnic_fw_grp_ids", size, 0);
226 if (!vnic->fw_grp_ids) {
228 "Failed to alloc %d bytes for group ids\n",
232 memset(vnic->fw_grp_ids, -1, size);
237 uint16_t bnxt_rte_to_hwrm_hash_types(uint64_t rte_type)
239 uint16_t hwrm_type = 0;
241 if (rte_type & ETH_RSS_IPV4)
242 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
243 if (rte_type & ETH_RSS_NONFRAG_IPV4_TCP)
244 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
245 if (rte_type & ETH_RSS_NONFRAG_IPV4_UDP)
246 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
247 if (rte_type & ETH_RSS_IPV6)
248 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
249 if (rte_type & ETH_RSS_NONFRAG_IPV6_TCP)
250 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
251 if (rte_type & ETH_RSS_NONFRAG_IPV6_UDP)
252 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;