net/bnxt: add Truflow flush-timer to alloc table scope
[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 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 static 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                 vnic->rx_queue_cnt = 0;
55
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 *vnic;
79         unsigned int i;
80
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;
85         }
86 }
87
88 void bnxt_free_vnic_attributes(struct bnxt *bp)
89 {
90         struct bnxt_vnic_info *vnic;
91         unsigned int i;
92
93         if (bp->vnic_info == NULL)
94                 return;
95
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 */
100                         /* N/A */
101
102                         vnic->rss_table = NULL;
103                 }
104
105                 if (vnic->rss_hash_key) {
106                         /* 'Unreserve' the rss_hash_key */
107                         /* N/A */
108
109                         vnic->rss_hash_key = NULL;
110                 }
111         }
112 }
113
114 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
115 {
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;
122         uint16_t max_vnics;
123         int i;
124         rte_iova_t mz_phys_addr;
125
126         entry_length = HW_HASH_KEY_SIZE +
127                        BNXT_MAX_MC_ADDRS * RTE_ETHER_ADDR_LEN;
128
129         if (BNXT_CHIP_THOR(bp))
130                 rss_table_size = BNXT_RSS_TBL_SIZE_THOR *
131                                  2 * sizeof(*vnic->rss_table);
132         else
133                 rss_table_size = HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
134
135         entry_length = RTE_CACHE_LINE_ROUNDUP(entry_length + rss_table_size);
136
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);
143         if (!mz) {
144                 mz = rte_memzone_reserve(mz_name,
145                                 entry_length * max_vnics, SOCKET_ID_ANY,
146                                 RTE_MEMZONE_2MB |
147                                 RTE_MEMZONE_SIZE_HINT_ONLY |
148                                 RTE_MEMZONE_IOVA_CONTIG);
149                 if (!mz)
150                         return -ENOMEM;
151         }
152         mz_phys_addr = mz->iova;
153
154         for (i = 0; i < max_vnics; i++) {
155                 vnic = &bp->vnic_info[i];
156
157                 /* Allocate rss table and hash key */
158                 vnic->rss_table =
159                         (void *)((char *)mz->addr + (entry_length * i));
160                 memset(vnic->rss_table, -1, entry_length);
161
162                 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
163                 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
164                                               rss_table_size);
165
166                 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
167                                               rss_table_size;
168                 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
169                                 HW_HASH_KEY_SIZE);
170                 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
171                                 HW_HASH_KEY_SIZE;
172                 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
173         }
174
175         return 0;
176 }
177
178 void bnxt_free_vnic_mem(struct bnxt *bp)
179 {
180         struct bnxt_vnic_info *vnic;
181         uint16_t max_vnics, i;
182
183         if (bp->vnic_info == NULL)
184                 return;
185
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 */
192                 }
193         }
194
195         rte_free(bp->vnic_info);
196         bp->vnic_info = NULL;
197 }
198
199 int bnxt_alloc_vnic_mem(struct bnxt *bp)
200 {
201         struct bnxt_vnic_info *vnic_mem;
202         uint16_t max_vnics;
203
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",
210                         max_vnics);
211                 return -ENOMEM;
212         }
213         bp->vnic_info = vnic_mem;
214         bnxt_init_vnics(bp);
215         return 0;
216 }
217
218 int bnxt_vnic_grp_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic)
219 {
220         uint32_t size = sizeof(*vnic->fw_grp_ids) * bp->max_ring_grps;
221
222         vnic->fw_grp_ids = rte_zmalloc("vnic_fw_grp_ids", size, 0);
223         if (!vnic->fw_grp_ids) {
224                 PMD_DRV_LOG(ERR,
225                             "Failed to alloc %d bytes for group ids\n",
226                             size);
227                 return -ENOMEM;
228         }
229         memset(vnic->fw_grp_ids, -1, size);
230
231         return 0;
232 }
233
234 uint16_t bnxt_rte_to_hwrm_hash_types(uint64_t rte_type)
235 {
236         uint16_t hwrm_type = 0;
237
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;
250
251         return hwrm_type;
252 }