1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
13 #include "hcapi_cfa_defs.h"
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;
19 static inline uint32_t SWAP_WORDS32(uint32_t val32)
21 return (((val32 & 0x0000ffff) << 16) |
22 ((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,
81 (key - (CFA_P4_EEM_KEY_MAX_SIZE - 1)),
82 CFA_P4_EEM_KEY_MAX_SIZE);
86 val1 = hcapi_cfa_crc32i(~val1, temp, 4);
91 static uint32_t hcapi_cfa_lookup3_hash(uint8_t *in_key)
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);
103 uint64_t hcapi_get_table_page(struct hcapi_cfa_em_table *mem,
113 * Use the level according to the num_level of page table
115 level = mem->num_lvl - 1;
117 addr = (uintptr_t)mem->pg_tbl[level].pg_va_tbl[page];
122 /** Approximation of HCAPI hcapi_cfa_key_hash()
127 uint64_t hcapi_cfa_p4_key_hash(uint64_t *key_data,
134 * Init the seeds if needed
136 if (!hcapi_cfa_lkup_init)
137 hcapi_cfa_seeds_init();
139 key0_hash = hcapi_cfa_crc32_hash(((uint8_t *)key_data) +
142 key1_hash = hcapi_cfa_lookup3_hash((uint8_t *)key_data);
144 return ((uint64_t)key0_hash) << 32 | (uint64_t)key1_hash;
147 static int hcapi_cfa_key_hw_op_put(struct hcapi_cfa_hwop *op,
148 struct hcapi_cfa_key_data *key_obj)
152 memcpy((uint8_t *)(uintptr_t)op->hw.base_addr +
160 static int hcapi_cfa_key_hw_op_get(struct hcapi_cfa_hwop *op,
161 struct hcapi_cfa_key_data *key_obj)
165 memcpy(key_obj->data,
166 (uint8_t *)(uintptr_t)op->hw.base_addr +
173 static int hcapi_cfa_key_hw_op_add(struct hcapi_cfa_hwop *op,
174 struct hcapi_cfa_key_data *key_obj)
177 struct cfa_p4_eem_64b_entry table_entry;
183 (uint8_t *)(uintptr_t)op->hw.base_addr +
188 * If this is entry is valid then report failure
190 if (table_entry.hdr.word1 & (1 << CFA_P4_EEM_ENTRY_VALID_SHIFT))
193 memcpy((uint8_t *)(uintptr_t)op->hw.base_addr +
201 static int hcapi_cfa_key_hw_op_del(struct hcapi_cfa_hwop *op,
202 struct hcapi_cfa_key_data *key_obj)
205 struct cfa_p4_eem_64b_entry table_entry;
211 (uint8_t *)(uintptr_t)op->hw.base_addr +
216 * If this is not a valid entry then report failure.
218 if (table_entry.hdr.word1 & (1 << CFA_P4_EEM_ENTRY_VALID_SHIFT)) {
220 * If a key has been provided then verify the key matches
221 * before deleting the entry.
223 if (key_obj->data != NULL) {
224 if (memcmp(&table_entry,
237 memset((uint8_t *)(uintptr_t)op->hw.base_addr +
246 /** Apporiximation of hcapi_cfa_key_hw_op()
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)
264 hcapi_get_table_page((struct hcapi_cfa_em_table *)
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;
270 if (op->hw.base_addr == 0)
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);
277 case HCAPI_CFA_HWOPS_GET: /**< Read from HW operation */
278 rc = hcapi_cfa_key_hw_op_get(op, key_obj);
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
290 rc = hcapi_cfa_key_hw_op_add(op, key_obj);
293 case HCAPI_CFA_HWOPS_DEL:
294 rc = hcapi_cfa_key_hw_op_del(op, key_obj);