1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
6 #ifndef _ULP_GEN_HASH_H_
7 #define _ULP_GEN_HASH_H_
11 #define ULP_GEN_HASH_MAX_TBL_SIZE BIT(15)
13 /* Structure to store the hash key details */
14 struct ulp_gen_hash_key_entry {
20 /* Macros for bucket entries */
21 #define ULP_HASH_BUCKET_VALID 0x8000
22 #define ULP_HASH_BUCKET_IDX_MSK 0x7FFF
23 #define ULP_HASH_BUCKET_ROW_SZ 4
24 #define ULP_HASH_BUCKET_INUSE(x) ((*(x)) & (ULP_HASH_BUCKET_VALID))
25 #define ULP_HASH_BUCKET_MARK_INUSE(x, y) \
26 ((*(x)) = ((y) & ULP_HASH_BUCKET_IDX_MSK) | (ULP_HASH_BUCKET_VALID))
27 #define ULP_HASH_BUCKET_CLEAR(x) ((*(x)) = 0)
28 #define ULP_HASH_BUCKET_INDEX(x) ((*(x)) & (ULP_HASH_BUCKET_IDX_MSK))
29 #define ULP_HASH_INDEX_CALC(id1, id2) (((id1) << 16) | ((id2) & 0xFFFF))
30 #define ULP_HASH_GET_H_INDEX(x) (((x) >> 16) & 0xFFFF)
31 #define ULP_HASH_GET_B_INDEX(x) ((x) & 0xFFFF)
32 #define ULP_HASH_BUCKET_INVAL -1
34 /* Structure for the hash bucket details */
35 struct ulp_hash_bucket_entry {
39 /* Structure for the hash bucket details */
40 struct bit_alloc_list {
46 * Structure to store the generic tbl container
47 * The ref count and byte data contain list of "num_elem" elements.
48 * The size of each entry in byte_data is of size byte_data_size.
50 struct ulp_gen_hash_tbl {
51 /* memory to store hash key */
52 uint32_t num_key_entries;
53 struct ulp_gen_hash_key_entry key_tbl;
55 /* Hash table memory */
56 uint32_t hash_tbl_size;
57 uint32_t hash_bkt_num;
58 struct ulp_hash_bucket_entry *hash_list;
61 /* Bit allocator - to allocate key_res index */
62 struct bit_alloc_list bit_list;
65 /* structure to pass hash creation params */
66 struct ulp_hash_create_params {
67 /* this is size of the hash tbl - try to keep it to power of 2.*/
68 uint32_t num_hash_tbl_entries;
69 /* Bucket size must be multiple of 4 */
71 /* This is size of hash key and data - try to keep it to power of 2 */
72 /* This value has to be less than 2^15 */
73 uint32_t num_key_entries;
74 /* the size of the hash key in bytes */
78 enum ulp_gen_hash_search_flag {
79 ULP_GEN_HASH_SEARCH_MISSED = 1,
80 ULP_GEN_HASH_SEARCH_FOUND = 2,
81 ULP_GEN_HASH_SEARCH_FULL = 3
84 /* structure to pass hash entry */
85 struct ulp_gen_hash_entry_params {
88 enum ulp_gen_hash_search_flag search_flag;
94 * Initialize the Generic Hash table
96 * cparams [in] Pointer to hash create params list
97 * hash_tbl [out] the pointer to created hash table
99 * returns 0 on success
102 ulp_gen_hash_tbl_list_init(struct ulp_hash_create_params *cparams,
103 struct ulp_gen_hash_tbl **hash_tbl);
106 * Free the generic hash table
108 * hash_tbl [in] the pointer to hash table
110 * returns 0 on success
113 ulp_gen_hash_tbl_list_deinit(struct ulp_gen_hash_tbl *hash_tbl);
116 * Search the generic hash table using key data
118 * hash_tbl [in] the pointer to hash table
119 * entry [in/out] pointer to hash entry details.
121 * returns 0 on success and marks search flag as found.
124 ulp_gen_hash_tbl_list_key_search(struct ulp_gen_hash_tbl *hash_tbl,
125 struct ulp_gen_hash_entry_params *entry);
128 * Search the generic hash table using hash index
130 * hash_tbl [in] the pointer to hash table
131 * entry [in/out] pointer to hash entry details.
133 * returns 0 on success and marks search flag as found.
136 ulp_gen_hash_tbl_list_index_search(struct ulp_gen_hash_tbl *hash_tbl,
137 struct ulp_gen_hash_entry_params *entry);
140 * Add the entry to the generic hash table
142 * hash_tbl [in] the pointer to hash table
143 * entry [in/out] pointer to hash entry details. Fill the hash index and
144 * key data details to be added.
146 * returns 0 on success
150 ulp_gen_hash_tbl_list_add(struct ulp_gen_hash_tbl *hash_tbl,
151 struct ulp_gen_hash_entry_params *entry);
154 * Delete the entry in the generic hash table
156 * hash_tbl [in] the pointer to hash table
157 * entry [in] pointer to hash entry details. Fill the hash index details to be
160 * returns 0 on success
163 ulp_gen_hash_tbl_list_del(struct ulp_gen_hash_tbl *hash_tbl,
164 struct ulp_gen_hash_entry_params *entry);
166 #endif /* _ULP_GEN_HASH_H_ */