1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
13 #include "hcapi_cfa.h"
14 #include "hcapi_cfa_defs.h"
16 static uint32_t hcapi_cfa_lkup_lkup3_init_cfg;
17 static uint32_t hcapi_cfa_lkup_em_seed_mem[HCAPI_CFA_LKUP_SEED_MEM_SIZE];
18 static bool hcapi_cfa_lkup_init;
20 static inline uint32_t SWAP_WORDS32(uint32_t val32)
22 return (((val32 & 0x0000ffff) << 16) | ((val32 & 0xffff0000) >> 16));
25 static void hcapi_cfa_seeds_init(void)
30 if (hcapi_cfa_lkup_init)
33 hcapi_cfa_lkup_init = true;
35 /* Initialize the lfsr */
38 /* RX and TX use the same seed values */
39 hcapi_cfa_lkup_lkup3_init_cfg = SWAP_WORDS32(rand32());
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);
49 static uint32_t hcapi_cfa_crc32_hash(uint8_t *key)
57 /* Do byte-wise XOR of the 52-byte HASH key first. */
61 for (i = CFA_P4_EEM_KEY_MAX_SIZE - 2; i >= 0; i--) {
62 index = index ^ *kptr;
67 val1 = hcapi_cfa_lkup_em_seed_mem[index * 2];
68 val2 = hcapi_cfa_lkup_em_seed_mem[index * 2 + 1];
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);
78 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
80 val1 = hcapi_cfa_crc32i(~val1, (key - (CFA_P4_EEM_KEY_MAX_SIZE - 1)),
81 CFA_P4_EEM_KEY_MAX_SIZE);
85 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
90 static uint32_t hcapi_cfa_lookup3_hash(uint8_t *in_key)
94 val1 = hashword(((uint32_t *)in_key) + 1,
95 CFA_P4_EEM_KEY_MAX_SIZE / (sizeof(uint32_t)),
96 hcapi_cfa_lkup_lkup3_init_cfg);
101 uint64_t hcapi_get_table_page(struct hcapi_cfa_em_table *mem, uint32_t page)
110 * Use the level according to the num_level of page table
112 level = mem->num_lvl - 1;
114 addr = (uint64_t)mem->pg_tbl[level].pg_va_tbl[page];
119 /** Approximation of HCAPI hcapi_cfa_key_hash()
124 uint64_t hcapi_cfa_p4_key_hash(uint64_t *key_data,
131 * Init the seeds if needed
133 if (!hcapi_cfa_lkup_init)
134 hcapi_cfa_seeds_init();
137 hcapi_cfa_crc32_hash(((uint8_t *)key_data) + (bitlen / 8) - 1);
139 key1_hash = hcapi_cfa_lookup3_hash((uint8_t *)key_data);
141 return ((uint64_t)key0_hash) << 32 | (uint64_t)key1_hash;
144 static int hcapi_cfa_p4_key_hw_op_put(struct hcapi_cfa_hwop *op,
145 struct hcapi_cfa_key_data *key_obj)
149 memcpy((uint8_t *)(uintptr_t)op->hw.base_addr + key_obj->offset,
150 key_obj->data, key_obj->size);
155 static int hcapi_cfa_p4_key_hw_op_get(struct hcapi_cfa_hwop *op,
156 struct hcapi_cfa_key_data *key_obj)
160 memcpy(key_obj->data,
161 (uint8_t *)(uintptr_t)op->hw.base_addr + key_obj->offset,
167 static int hcapi_cfa_p4_key_hw_op_add(struct hcapi_cfa_hwop *op,
168 struct hcapi_cfa_key_data *key_obj)
171 struct cfa_p4_eem_64b_entry table_entry;
177 (uint8_t *)(uintptr_t)op->hw.base_addr + key_obj->offset,
181 * If this is entry is valid then report failure
183 if (table_entry.hdr.word1 & (1 << CFA_P4_EEM_ENTRY_VALID_SHIFT))
186 memcpy((uint8_t *)(uintptr_t)op->hw.base_addr + key_obj->offset,
187 key_obj->data, key_obj->size);
192 static int hcapi_cfa_p4_key_hw_op_del(struct hcapi_cfa_hwop *op,
193 struct hcapi_cfa_key_data *key_obj)
196 struct cfa_p4_eem_64b_entry table_entry;
202 (uint8_t *)(uintptr_t)op->hw.base_addr + key_obj->offset,
206 * If this is not a valid entry then report failure.
208 if (table_entry.hdr.word1 & (1 << CFA_P4_EEM_ENTRY_VALID_SHIFT)) {
210 * If a key has been provided then verify the key matches
211 * before deleting the entry.
213 if (key_obj->data != NULL) {
214 if (memcmp(&table_entry, key_obj->data,
225 memset((uint8_t *)(uintptr_t)op->hw.base_addr + key_obj->offset, 0, key_obj->size);
230 /** Approximation of hcapi_cfa_key_hw_op()
234 static int hcapi_cfa_p4_key_hw_op(struct hcapi_cfa_hwop *op,
235 struct hcapi_cfa_key_tbl *key_tbl,
236 struct hcapi_cfa_key_data *key_obj,
237 struct hcapi_cfa_key_loc *key_loc)
240 struct hcapi_cfa_em_table *em_tbl;
243 if (op == NULL || key_tbl == NULL || key_obj == NULL || key_loc == NULL)
246 page = key_obj->offset / key_tbl->page_size;
247 em_tbl = (struct hcapi_cfa_em_table *)key_tbl->base0;
248 op->hw.base_addr = hcapi_get_table_page(em_tbl, page);
249 /* Offset is adjusted to be the offset into the page */
250 key_obj->offset = key_obj->offset % key_tbl->page_size;
252 if (op->hw.base_addr == 0)
255 switch (op->opcode) {
256 case HCAPI_CFA_HWOPS_PUT: /**< Write to HW operation */
257 rc = hcapi_cfa_p4_key_hw_op_put(op, key_obj);
259 case HCAPI_CFA_HWOPS_GET: /**< Read from HW operation */
260 rc = hcapi_cfa_p4_key_hw_op_get(op, key_obj);
262 case HCAPI_CFA_HWOPS_ADD:
263 /**< For operations which require more then simple
264 * writes to HW, this operation is used. The
265 * distinction with this operation when compared
266 * to the PUT ops is that this operation is used
267 * in conjunction with the HCAPI_CFA_HWOPS_DEL
268 * op to remove the operations issued by the
272 rc = hcapi_cfa_p4_key_hw_op_add(op, key_obj);
275 case HCAPI_CFA_HWOPS_DEL:
276 rc = hcapi_cfa_p4_key_hw_op_del(op, key_obj);
286 const struct hcapi_cfa_devops cfa_p4_devops = {
287 .hcapi_cfa_key_hash = hcapi_cfa_p4_key_hash,
288 .hcapi_cfa_key_hw_op = hcapi_cfa_p4_key_hw_op,