net/bnxt: support generic hash table
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_gen_hash.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #ifndef _ULP_GEN_HASH_H_
7 #define _ULP_GEN_HASH_H_
8
9 #include "bnxt.h"
10
11 #define ULP_GEN_HASH_MAX_TBL_SIZE       BIT(15)
12
13 /* Structure to store the hash key details */
14 struct ulp_gen_hash_key_entry {
15         uint32_t                mem_size;
16         uint32_t                data_size;
17         uint8_t                 *key_data;
18 };
19
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
33
34 /* Structure for the hash bucket details */
35 struct ulp_hash_bucket_entry {
36         uint64_t                *bucket;
37 };
38
39 /* Structure for the hash bucket details */
40 struct bit_alloc_list {
41         uint32_t                bsize;
42         uint64_t                *bdata;
43 };
44
45 /*
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.
49  */
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;
54
55         /* Hash table memory */
56         uint32_t                        hash_tbl_size;
57         uint32_t                        hash_bkt_num;
58         struct ulp_hash_bucket_entry    *hash_list;
59         uint32_t                        hash_mask;
60
61         /* Bit allocator - to allocate key_res index */
62         struct bit_alloc_list           bit_list;
63 };
64
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 */
70         uint32_t                        num_buckets;
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 */
75         uint32_t                        key_size;
76 };
77
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
82 };
83
84 /* structure to pass hash entry */
85 struct ulp_gen_hash_entry_params {
86         uint8_t                         *key_data;
87         uint32_t                        key_length;
88         enum ulp_gen_hash_search_flag   search_flag;
89         uint32_t                        hash_index;
90         uint32_t                        key_idx;
91 };
92
93 /*
94  * Initialize the Generic Hash table
95  *
96  * cparams [in] Pointer to hash create params list
97  * hash_tbl [out] the pointer to created hash table
98  *
99  * returns 0 on success
100  */
101 int32_t
102 ulp_gen_hash_tbl_list_init(struct ulp_hash_create_params *cparams,
103                            struct ulp_gen_hash_tbl **hash_tbl);
104
105 /*
106  * Free the generic hash table
107  *
108  * hash_tbl [in] the pointer to hash table
109  *
110  * returns 0 on success
111  */
112 int32_t
113 ulp_gen_hash_tbl_list_deinit(struct ulp_gen_hash_tbl *hash_tbl);
114
115 /*
116  * Search the generic hash table using key data
117  *
118  * hash_tbl [in] the pointer to hash table
119  * entry [in/out] pointer to hash entry details.
120  *
121  * returns 0 on success and marks search flag as found.
122  */
123 int32_t
124 ulp_gen_hash_tbl_list_key_search(struct ulp_gen_hash_tbl *hash_tbl,
125                                  struct ulp_gen_hash_entry_params *entry);
126
127 /*
128  * Search the generic hash table using hash index
129  *
130  * hash_tbl [in] the pointer to hash table
131  * entry [in/out] pointer to hash entry details.
132  *
133  * returns 0 on success and marks search flag as found.
134  */
135 int32_t
136 ulp_gen_hash_tbl_list_index_search(struct ulp_gen_hash_tbl *hash_tbl,
137                                    struct ulp_gen_hash_entry_params *entry);
138
139 /*
140  * Add the entry to the generic hash table
141  *
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.
145  *
146  * returns 0 on success
147  *
148  */
149 int32_t
150 ulp_gen_hash_tbl_list_add(struct ulp_gen_hash_tbl *hash_tbl,
151                           struct ulp_gen_hash_entry_params *entry);
152
153 /*
154  * Delete the entry in the generic hash table
155  *
156  * hash_tbl [in] the pointer to hash table
157  * entry [in] pointer to hash entry details. Fill the hash index details to be
158  * deleted.
159  *
160  * returns 0 on success
161  */
162 int32_t
163 ulp_gen_hash_tbl_list_del(struct ulp_gen_hash_tbl *hash_tbl,
164                           struct ulp_gen_hash_entry_params *entry);
165
166 #endif /* _ULP_GEN_HASH_H_ */