net: add rte prefix to ether structures
[dpdk.git] / drivers / net / bnxt / bnxt_vnic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7
8 #include <rte_memzone.h>
9 #include <rte_malloc.h>
10
11 #include "bnxt.h"
12 #include "bnxt_vnic.h"
13 #include "hsi_struct_def_dpdk.h"
14
15 /*
16  * VNIC Functions
17  */
18
19 static void prandom_bytes(void *dest_ptr, size_t len)
20 {
21         char *dest = (char *)dest_ptr;
22         uint64_t rb;
23
24         while (len) {
25                 rb = rte_rand();
26                 if (len >= 8) {
27                         memcpy(dest, &rb, 8);
28                         len -= 8;
29                         dest += 8;
30                 } else {
31                         memcpy(dest, &rb, len);
32                         dest += len;
33                         len = 0;
34                 }
35         }
36 }
37
38 void bnxt_init_vnics(struct bnxt *bp)
39 {
40         struct bnxt_vnic_info *vnic;
41         uint16_t max_vnics;
42         int i;
43
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;
52                 vnic->hash_mode =
53                         HWRM_VNIC_RSS_CFG_INPUT_HASH_MODE_FLAGS_DEFAULT;
54
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);
59         }
60 }
61
62 struct bnxt_vnic_info *bnxt_alloc_vnic(struct bnxt *bp)
63 {
64         struct bnxt_vnic_info *vnic;
65
66         /* Find the 1st unused vnic from the free_vnic_list pool*/
67         vnic = STAILQ_FIRST(&bp->free_vnic_list);
68         if (!vnic) {
69                 PMD_DRV_LOG(ERR, "No more free VNIC resources\n");
70                 return NULL;
71         }
72         STAILQ_REMOVE_HEAD(&bp->free_vnic_list, next);
73         return vnic;
74 }
75
76 void bnxt_free_all_vnics(struct bnxt *bp)
77 {
78         struct bnxt_vnic_info *temp;
79         unsigned int i;
80
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);
84         }
85 }
86
87 void bnxt_free_vnic_attributes(struct bnxt *bp)
88 {
89         struct bnxt_vnic_info *vnic;
90         unsigned int i;
91
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 */
96                         /* N/A */
97
98                         vnic->rss_table = NULL;
99                 }
100
101                 if (vnic->rss_hash_key) {
102                         /* 'Unreserve' the rss_hash_key */
103                         /* N/A */
104
105                         vnic->rss_hash_key = NULL;
106                 }
107         }
108 }
109
110 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
111 {
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) +
118                                 HW_HASH_KEY_SIZE +
119                                 BNXT_MAX_MC_ADDRS * ETHER_ADDR_LEN);
120         uint16_t max_vnics;
121         int i;
122         rte_iova_t mz_phys_addr;
123
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);
130         if (!mz) {
131                 mz = rte_memzone_reserve(mz_name,
132                                 entry_length * max_vnics, SOCKET_ID_ANY,
133                                 RTE_MEMZONE_2MB |
134                                 RTE_MEMZONE_SIZE_HINT_ONLY |
135                                 RTE_MEMZONE_IOVA_CONTIG);
136                 if (!mz)
137                         return -ENOMEM;
138         }
139         mz_phys_addr = mz->iova;
140         if ((unsigned long)mz->addr == mz_phys_addr) {
141                 PMD_DRV_LOG(WARNING,
142                         "Memzone physical address same as virtual.\n");
143                 PMD_DRV_LOG(WARNING,
144                         "Using rte_mem_virt2iova()\n");
145                 mz_phys_addr = rte_mem_virt2iova(mz->addr);
146                 if (mz_phys_addr == 0) {
147                         PMD_DRV_LOG(ERR,
148                         "unable to map vnic address to physical memory\n");
149                         return -ENOMEM;
150                 }
151         }
152
153         for (i = 0; i < max_vnics; i++) {
154                 vnic = &bp->vnic_info[i];
155
156                 /* Allocate rss table and hash key */
157                 vnic->rss_table =
158                         (void *)((char *)mz->addr + (entry_length * i));
159                 memset(vnic->rss_table, -1, entry_length);
160
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));
164
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 +
168                                 HW_HASH_KEY_SIZE);
169                 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
170                                 HW_HASH_KEY_SIZE;
171         }
172
173         return 0;
174 }
175
176 void bnxt_free_vnic_mem(struct bnxt *bp)
177 {
178         struct bnxt_vnic_info *vnic;
179         uint16_t max_vnics, i;
180
181         if (bp->vnic_info == NULL)
182                 return;
183
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 */
190                 }
191         }
192
193         rte_free(bp->vnic_info);
194         bp->vnic_info = NULL;
195 }
196
197 int bnxt_alloc_vnic_mem(struct bnxt *bp)
198 {
199         struct bnxt_vnic_info *vnic_mem;
200         uint16_t max_vnics;
201
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",
208                         max_vnics);
209                 return -ENOMEM;
210         }
211         bp->vnic_info = vnic_mem;
212         return 0;
213 }