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 for (i = 0; i < bp->max_vnics; i++) {
82 vnic = &bp->vnic_info[i];
83 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
84 vnic->rx_queue_cnt = 0;
88 void bnxt_free_vnic_attributes(struct bnxt *bp)
90 struct bnxt_vnic_info *vnic;
93 if (bp->vnic_info == NULL)
96 for (i = 0; i < bp->max_vnics; i++) {
97 vnic = &bp->vnic_info[i];
98 if (vnic->rss_table) {
99 /* 'Unreserve' the rss_table */
102 vnic->rss_table = NULL;
105 if (vnic->rss_hash_key) {
106 /* 'Unreserve' the rss_hash_key */
109 vnic->rss_hash_key = NULL;
114 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
116 struct bnxt_vnic_info *vnic;
117 struct rte_pci_device *pdev = bp->pdev;
118 const struct rte_memzone *mz;
119 char mz_name[RTE_MEMZONE_NAMESIZE];
120 uint32_t entry_length;
121 size_t rss_table_size;
124 rte_iova_t mz_phys_addr;
126 entry_length = HW_HASH_KEY_SIZE +
127 BNXT_MAX_MC_ADDRS * RTE_ETHER_ADDR_LEN;
129 if (BNXT_CHIP_THOR(bp))
130 rss_table_size = BNXT_RSS_TBL_SIZE_THOR *
131 2 * sizeof(*vnic->rss_table);
133 rss_table_size = HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
135 entry_length = RTE_CACHE_LINE_ROUNDUP(entry_length + rss_table_size);
137 max_vnics = bp->max_vnics;
138 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
139 "bnxt_" PCI_PRI_FMT "_vnicattr", pdev->addr.domain,
140 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
141 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
142 mz = rte_memzone_lookup(mz_name);
144 mz = rte_memzone_reserve(mz_name,
145 entry_length * max_vnics, SOCKET_ID_ANY,
147 RTE_MEMZONE_SIZE_HINT_ONLY |
148 RTE_MEMZONE_IOVA_CONTIG);
152 mz_phys_addr = mz->iova;
154 for (i = 0; i < max_vnics; i++) {
155 vnic = &bp->vnic_info[i];
157 /* Allocate rss table and hash key */
159 (void *)((char *)mz->addr + (entry_length * i));
160 memset(vnic->rss_table, -1, entry_length);
162 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
163 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
166 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
168 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
170 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
172 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
178 void bnxt_free_vnic_mem(struct bnxt *bp)
180 struct bnxt_vnic_info *vnic;
181 uint16_t max_vnics, i;
183 if (bp->vnic_info == NULL)
186 max_vnics = bp->max_vnics;
187 for (i = 0; i < max_vnics; i++) {
188 vnic = &bp->vnic_info[i];
189 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
190 PMD_DRV_LOG(ERR, "VNIC is not freed yet!\n");
191 /* TODO Call HWRM to free VNIC */
195 rte_free(bp->vnic_info);
196 bp->vnic_info = NULL;
199 int bnxt_alloc_vnic_mem(struct bnxt *bp)
201 struct bnxt_vnic_info *vnic_mem;
204 max_vnics = bp->max_vnics;
205 /* Allocate memory for VNIC pool and filter pool */
206 vnic_mem = rte_zmalloc("bnxt_vnic_info",
207 max_vnics * sizeof(struct bnxt_vnic_info), 0);
208 if (vnic_mem == NULL) {
209 PMD_DRV_LOG(ERR, "Failed to alloc memory for %d VNICs",
213 bp->vnic_info = vnic_mem;
218 int bnxt_vnic_grp_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
220 uint32_t size = sizeof(*vnic->fw_grp_ids) * bp->max_ring_grps;
222 vnic->fw_grp_ids = rte_zmalloc("vnic_fw_grp_ids", size, 0);
223 if (!vnic->fw_grp_ids) {
225 "Failed to alloc %d bytes for group ids\n",
229 memset(vnic->fw_grp_ids, -1, size);
234 uint16_t bnxt_rte_to_hwrm_hash_types(uint64_t rte_type)
236 uint16_t hwrm_type = 0;
238 if (rte_type & ETH_RSS_IPV4)
239 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
240 if (rte_type & ETH_RSS_NONFRAG_IPV4_TCP)
241 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
242 if (rte_type & ETH_RSS_NONFRAG_IPV4_UDP)
243 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
244 if (rte_type & ETH_RSS_IPV6)
245 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
246 if (rte_type & ETH_RSS_NONFRAG_IPV6_TCP)
247 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
248 if (rte_type & ETH_RSS_NONFRAG_IPV6_UDP)
249 hwrm_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;