New upstream version 17.11.4
[deb_dpdk.git] / drivers / net / bnxt / bnxt_vnic.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2014-2015 Broadcom Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Broadcom Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <inttypes.h>
35
36 #include <rte_memzone.h>
37 #include <rte_malloc.h>
38
39 #include "bnxt.h"
40 #include "bnxt_vnic.h"
41 #include "hsi_struct_def_dpdk.h"
42
43 /*
44  * VNIC Functions
45  */
46
47 static void prandom_bytes(void *dest_ptr, size_t len)
48 {
49         char *dest = (char *)dest_ptr;
50         uint64_t rb;
51
52         while (len) {
53                 rb = rte_rand();
54                 if (len >= 8) {
55                         memcpy(dest, &rb, 8);
56                         len -= 8;
57                         dest += 8;
58                 } else {
59                         memcpy(dest, &rb, len);
60                         dest += len;
61                         len = 0;
62                 }
63         }
64 }
65
66 void bnxt_init_vnics(struct bnxt *bp)
67 {
68         struct bnxt_vnic_info *vnic;
69         uint16_t max_vnics;
70         int i;
71
72         max_vnics = bp->max_vnics;
73         STAILQ_INIT(&bp->free_vnic_list);
74         for (i = 0; i < max_vnics; i++) {
75                 vnic = &bp->vnic_info[i];
76                 vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
77                 vnic->rss_rule = (uint16_t)HWRM_NA_SIGNATURE;
78                 vnic->cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
79                 vnic->lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
80
81                 prandom_bytes(vnic->rss_hash_key, HW_HASH_KEY_SIZE);
82                 STAILQ_INIT(&vnic->filter);
83                 STAILQ_INIT(&vnic->flow_list);
84                 STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic, next);
85         }
86         for (i = 0; i < MAX_FF_POOLS; i++)
87                 STAILQ_INIT(&bp->ff_pool[i]);
88 }
89
90 int bnxt_free_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic,
91                           int pool)
92 {
93         struct bnxt_vnic_info *temp;
94
95         temp = STAILQ_FIRST(&bp->ff_pool[pool]);
96         while (temp) {
97                 if (temp == vnic) {
98                         STAILQ_REMOVE(&bp->ff_pool[pool], vnic,
99                                       bnxt_vnic_info, next);
100                         vnic->fw_vnic_id = (uint16_t)HWRM_NA_SIGNATURE;
101                         STAILQ_INSERT_TAIL(&bp->free_vnic_list, vnic,
102                                            next);
103                         return 0;
104                 }
105                 temp = STAILQ_NEXT(temp, next);
106         }
107         RTE_LOG(ERR, PMD, "VNIC %p is not found in pool[%d]\n", vnic, pool);
108         return -EINVAL;
109 }
110
111 struct bnxt_vnic_info *bnxt_alloc_vnic(struct bnxt *bp)
112 {
113         struct bnxt_vnic_info *vnic;
114
115         /* Find the 1st unused vnic from the free_vnic_list pool*/
116         vnic = STAILQ_FIRST(&bp->free_vnic_list);
117         if (!vnic) {
118                 RTE_LOG(ERR, PMD, "No more free VNIC resources\n");
119                 return NULL;
120         }
121         STAILQ_REMOVE_HEAD(&bp->free_vnic_list, next);
122         return vnic;
123 }
124
125 void bnxt_free_all_vnics(struct bnxt *bp)
126 {
127         struct bnxt_vnic_info *temp, *next;
128         int i;
129
130         for (i = 0; i < MAX_FF_POOLS; i++) {
131                 temp = STAILQ_FIRST(&bp->ff_pool[i]);
132                 while (temp) {
133                         next = STAILQ_NEXT(temp, next);
134                         STAILQ_REMOVE(&bp->ff_pool[i], temp, bnxt_vnic_info,
135                                       next);
136                         STAILQ_INSERT_TAIL(&bp->free_vnic_list, temp, next);
137                         temp = next;
138                 }
139         }
140 }
141
142 void bnxt_free_vnic_attributes(struct bnxt *bp)
143 {
144         struct bnxt_vnic_info *vnic;
145
146         STAILQ_FOREACH(vnic, &bp->free_vnic_list, next) {
147                 if (vnic->rss_table) {
148                         /* 'Unreserve' the rss_table */
149                         /* N/A */
150
151                         vnic->rss_table = NULL;
152                 }
153
154                 if (vnic->rss_hash_key) {
155                         /* 'Unreserve' the rss_hash_key */
156                         /* N/A */
157
158                         vnic->rss_hash_key = NULL;
159                 }
160         }
161 }
162
163 int bnxt_alloc_vnic_attributes(struct bnxt *bp)
164 {
165         struct bnxt_vnic_info *vnic;
166         struct rte_pci_device *pdev = bp->pdev;
167         const struct rte_memzone *mz;
168         char mz_name[RTE_MEMZONE_NAMESIZE];
169         uint32_t entry_length = RTE_CACHE_LINE_ROUNDUP(
170                                 HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table) +
171                                 HW_HASH_KEY_SIZE +
172                                 BNXT_MAX_MC_ADDRS * ETHER_ADDR_LEN);
173         uint16_t max_vnics;
174         int i;
175         rte_iova_t mz_phys_addr;
176
177         max_vnics = bp->max_vnics;
178         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
179                  "bnxt_%04x:%02x:%02x:%02x_vnicattr", pdev->addr.domain,
180                  pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
181         mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
182         mz = rte_memzone_lookup(mz_name);
183         if (!mz) {
184                 mz = rte_memzone_reserve(mz_name,
185                                          entry_length * max_vnics,
186                                          SOCKET_ID_ANY,
187                                          RTE_MEMZONE_2MB |
188                                          RTE_MEMZONE_SIZE_HINT_ONLY);
189                 if (!mz)
190                         return -ENOMEM;
191         }
192         mz_phys_addr = mz->iova;
193         if ((unsigned long)mz->addr == mz_phys_addr) {
194                 RTE_LOG(WARNING, PMD,
195                         "Memzone physical address same as virtual.\n");
196                 RTE_LOG(WARNING, PMD,
197                         "Using rte_mem_virt2iova()\n");
198                 mz_phys_addr = rte_mem_virt2iova(mz->addr);
199                 if (mz_phys_addr == 0) {
200                         RTE_LOG(ERR, PMD,
201                         "unable to map vnic address to physical memory\n");
202                         return -ENOMEM;
203                 }
204         }
205
206         for (i = 0; i < max_vnics; i++) {
207                 vnic = &bp->vnic_info[i];
208
209                 /* Allocate rss table and hash key */
210                 vnic->rss_table =
211                         (void *)((char *)mz->addr + (entry_length * i));
212                 memset(vnic->rss_table, -1, entry_length);
213
214                 vnic->rss_table_dma_addr = mz_phys_addr + (entry_length * i);
215                 vnic->rss_hash_key = (void *)((char *)vnic->rss_table +
216                              HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table));
217
218                 vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr +
219                              HW_HASH_INDEX_SIZE * sizeof(*vnic->rss_table);
220                 vnic->mc_list = (void *)((char *)vnic->rss_hash_key +
221                                 HW_HASH_KEY_SIZE);
222                 vnic->mc_list_dma_addr = vnic->rss_hash_key_dma_addr +
223                                 HW_HASH_KEY_SIZE;
224         }
225
226         return 0;
227 }
228
229 void bnxt_free_vnic_mem(struct bnxt *bp)
230 {
231         struct bnxt_vnic_info *vnic;
232         uint16_t max_vnics, i;
233
234         if (bp->vnic_info == NULL)
235                 return;
236
237         max_vnics = bp->max_vnics;
238         for (i = 0; i < max_vnics; i++) {
239                 vnic = &bp->vnic_info[i];
240                 if (vnic->fw_vnic_id != (uint16_t)HWRM_NA_SIGNATURE) {
241                         RTE_LOG(ERR, PMD, "VNIC is not freed yet!\n");
242                         /* TODO Call HWRM to free VNIC */
243                 }
244         }
245
246         rte_free(bp->vnic_info);
247         bp->vnic_info = NULL;
248 }
249
250 int bnxt_alloc_vnic_mem(struct bnxt *bp)
251 {
252         struct bnxt_vnic_info *vnic_mem;
253         uint16_t max_vnics;
254
255         max_vnics = bp->max_vnics;
256         /* Allocate memory for VNIC pool and filter pool */
257         vnic_mem = rte_zmalloc("bnxt_vnic_info",
258                                max_vnics * sizeof(struct bnxt_vnic_info), 0);
259         if (vnic_mem == NULL) {
260                 RTE_LOG(ERR, PMD, "Failed to alloc memory for %d VNICs",
261                         max_vnics);
262                 return -ENOMEM;
263         }
264         bp->vnic_info = vnic_mem;
265         return 0;
266 }