f097be9dfbc8fc1554cc02ce5d5bfa9672aaad42
[dpdk.git] / drivers / net / bnxt / hcapi / cfa / hcapi_cfa_p4.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 Broadcom
3  * All rights reserved.
4  */
5 #include <inttypes.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include "lookup3.h"
11 #include "rand.h"
12
13 #include "hcapi_cfa_defs.h"
14
15 static uint32_t hcapi_cfa_lkup_lkup3_init_cfg;
16 static uint32_t hcapi_cfa_lkup_em_seed_mem[HCAPI_CFA_LKUP_SEED_MEM_SIZE];
17 static bool hcapi_cfa_lkup_init;
18
19 static inline uint32_t SWAP_WORDS32(uint32_t val32)
20 {
21         return (((val32 & 0x0000ffff) << 16) |
22                 ((val32 & 0xffff0000) >> 16));
23 }
24
25 static void hcapi_cfa_seeds_init(void)
26 {
27         int i;
28         uint32_t r;
29
30         if (hcapi_cfa_lkup_init)
31                 return;
32
33         hcapi_cfa_lkup_init = true;
34
35         /* Initialize the lfsr */
36         rand_init();
37
38         /* RX and TX use the same seed values */
39         hcapi_cfa_lkup_lkup3_init_cfg = SWAP_WORDS32(rand32());
40
41         for (i = 0; i < HCAPI_CFA_LKUP_SEED_MEM_SIZE / 2; i++) {
42                 r = SWAP_WORDS32(rand32());
43                 hcapi_cfa_lkup_em_seed_mem[i * 2] = r;
44                 r = SWAP_WORDS32(rand32());
45                 hcapi_cfa_lkup_em_seed_mem[i * 2 + 1] = (r & 0x1);
46         }
47 }
48
49 static uint32_t hcapi_cfa_crc32_hash(uint8_t *key)
50 {
51         int i;
52         uint32_t index;
53         uint32_t val1, val2;
54         uint8_t temp[4];
55         uint8_t *kptr = key;
56
57         /* Do byte-wise XOR of the 52-byte HASH key first. */
58         index = *key;
59         kptr--;
60
61         for (i = CFA_P4_EEM_KEY_MAX_SIZE - 2; i >= 0; i--) {
62                 index = index ^ *kptr;
63                 kptr--;
64         }
65
66         /* Get seeds */
67         val1 = hcapi_cfa_lkup_em_seed_mem[index * 2];
68         val2 = hcapi_cfa_lkup_em_seed_mem[index * 2 + 1];
69
70         temp[3] = (uint8_t)(val1 >> 24);
71         temp[2] = (uint8_t)(val1 >> 16);
72         temp[1] = (uint8_t)(val1 >> 8);
73         temp[0] = (uint8_t)(val1 & 0xff);
74         val1 = 0;
75
76         /* Start with seed */
77         if (!(val2 & 0x1))
78                 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
79
80         val1 = hcapi_cfa_crc32i(~val1,
81                       (key - (CFA_P4_EEM_KEY_MAX_SIZE - 1)),
82                       CFA_P4_EEM_KEY_MAX_SIZE);
83
84         /* End with seed */
85         if (val2 & 0x1)
86                 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
87
88         return val1;
89 }
90
91 static uint32_t hcapi_cfa_lookup3_hash(uint8_t *in_key)
92 {
93         uint32_t val1;
94
95         val1 = hashword(((const uint32_t *)(uintptr_t *)in_key) + 1,
96                          CFA_P4_EEM_KEY_MAX_SIZE / (sizeof(uint32_t)),
97                          hcapi_cfa_lkup_lkup3_init_cfg);
98
99         return val1;
100 }
101
102
103 uint64_t hcapi_get_table_page(struct hcapi_cfa_em_table *mem,
104                               uint32_t page)
105 {
106         int level = 0;
107         uint64_t addr;
108
109         if (mem == NULL)
110                 return 0;
111
112         /*
113          * Use the level according to the num_level of page table
114          */
115         level = mem->num_lvl - 1;
116
117         addr = (uintptr_t)mem->pg_tbl[level].pg_va_tbl[page];
118
119         return addr;
120 }
121
122 /** Approximation of HCAPI hcapi_cfa_key_hash()
123  *
124  * Return:
125  *
126  */
127 uint64_t hcapi_cfa_p4_key_hash(uint64_t *key_data,
128                                uint16_t bitlen)
129 {
130         uint32_t key0_hash;
131         uint32_t key1_hash;
132
133         /*
134          * Init the seeds if needed
135          */
136         if (!hcapi_cfa_lkup_init)
137                 hcapi_cfa_seeds_init();
138
139         key0_hash = hcapi_cfa_crc32_hash(((uint8_t *)key_data) +
140                                               (bitlen / 8) - 1);
141
142         key1_hash = hcapi_cfa_lookup3_hash((uint8_t *)key_data);
143
144         return ((uint64_t)key0_hash) << 32 | (uint64_t)key1_hash;
145 }
146
147 static int hcapi_cfa_key_hw_op_put(struct hcapi_cfa_hwop *op,
148                                    struct hcapi_cfa_key_data *key_obj)
149 {
150         int rc = 0;
151
152         memcpy((uint8_t *)(uintptr_t)op->hw.base_addr +
153                key_obj->offset,
154                key_obj->data,
155                key_obj->size);
156
157         return rc;
158 }
159
160 static int hcapi_cfa_key_hw_op_get(struct hcapi_cfa_hwop *op,
161                                    struct hcapi_cfa_key_data *key_obj)
162 {
163         int rc = 0;
164
165         memcpy(key_obj->data,
166                (uint8_t *)(uintptr_t)op->hw.base_addr +
167                key_obj->offset,
168                key_obj->size);
169
170         return rc;
171 }
172
173 static int hcapi_cfa_key_hw_op_add(struct hcapi_cfa_hwop *op,
174                                    struct hcapi_cfa_key_data *key_obj)
175 {
176         int rc = 0;
177         struct cfa_p4_eem_64b_entry table_entry;
178
179         /*
180          * Is entry free?
181          */
182         memcpy(&table_entry,
183                (uint8_t *)(uintptr_t)op->hw.base_addr +
184                key_obj->offset,
185                key_obj->size);
186
187         /*
188          * If this is entry is valid then report failure
189          */
190         if (table_entry.hdr.word1 & (1 << CFA_P4_EEM_ENTRY_VALID_SHIFT))
191                 return -1;
192
193         memcpy((uint8_t *)(uintptr_t)op->hw.base_addr +
194                key_obj->offset,
195                key_obj->data,
196                key_obj->size);
197
198         return rc;
199 }
200
201 static int hcapi_cfa_key_hw_op_del(struct hcapi_cfa_hwop *op,
202                                    struct hcapi_cfa_key_data *key_obj)
203 {
204         int rc = 0;
205         struct cfa_p4_eem_64b_entry table_entry;
206
207         /*
208          * Read entry
209          */
210         memcpy(&table_entry,
211                (uint8_t *)(uintptr_t)op->hw.base_addr +
212                key_obj->offset,
213                key_obj->size);
214
215         /*
216          * If this is not a valid entry then report failure.
217          */
218         if (table_entry.hdr.word1 & (1 << CFA_P4_EEM_ENTRY_VALID_SHIFT)) {
219                 /*
220                  * If a key has been provided then verify the key matches
221                  * before deleting the entry.
222                  */
223                 if (key_obj->data != NULL) {
224                         if (memcmp(&table_entry,
225                                    key_obj->data,
226                                    key_obj->size) != 0)
227                                 return -1;
228                 }
229         } else {
230                 return -1;
231         }
232
233
234         /*
235          * Delete entry
236          */
237         memset((uint8_t *)(uintptr_t)op->hw.base_addr +
238                key_obj->offset,
239                0,
240                key_obj->size);
241
242         return rc;
243 }
244
245
246 /** Apporiximation of hcapi_cfa_key_hw_op()
247  *
248  *
249  */
250 int hcapi_cfa_key_hw_op(struct hcapi_cfa_hwop *op,
251                         struct hcapi_cfa_key_tbl *key_tbl,
252                         struct hcapi_cfa_key_data *key_obj,
253                         struct hcapi_cfa_key_loc *key_loc)
254 {
255         int rc = 0;
256
257         if (op == NULL ||
258             key_tbl == NULL ||
259             key_obj == NULL ||
260             key_loc == NULL)
261                 return -1;
262
263         op->hw.base_addr =
264                 hcapi_get_table_page((struct hcapi_cfa_em_table *)
265                                      key_tbl->base0,
266                                      key_obj->offset / key_tbl->page_size);
267         /* Offset is adjusted to be the offset into the page */
268         key_obj->offset = key_obj->offset % key_tbl->page_size;
269
270         if (op->hw.base_addr == 0)
271                 return -1;
272
273         switch (op->opcode) {
274         case HCAPI_CFA_HWOPS_PUT: /**< Write to HW operation */
275                 rc = hcapi_cfa_key_hw_op_put(op, key_obj);
276                 break;
277         case HCAPI_CFA_HWOPS_GET: /**< Read from HW operation */
278                 rc = hcapi_cfa_key_hw_op_get(op, key_obj);
279                 break;
280         case HCAPI_CFA_HWOPS_ADD:
281                 /**< For operations which require more than
282                  * simple writes to HW, this operation is used. The
283                  * distinction with this operation when compared
284                  * to the PUT ops is that this operation is used
285                  * in conjunction with the HCAPI_CFA_HWOPS_DEL
286                  * op to remove the operations issued by the
287                  * ADD OP.
288                  */
289
290                 rc = hcapi_cfa_key_hw_op_add(op, key_obj);
291
292                 break;
293         case HCAPI_CFA_HWOPS_DEL:
294                 rc = hcapi_cfa_key_hw_op_del(op, key_obj);
295                 break;
296         default:
297                 rc = -1;
298                 break;
299         }
300
301         return rc;
302 }