1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev * Copyright 2017 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev *
4*b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev *
11*b843c749SSergey Zigachev * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev *
14*b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*b843c749SSergey Zigachev * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev *
22*b843c749SSergey Zigachev */
23*b843c749SSergey Zigachev
24*b843c749SSergey Zigachev #ifndef _LINUX_CHASH_H
25*b843c749SSergey Zigachev #define _LINUX_CHASH_H
26*b843c749SSergey Zigachev
27*b843c749SSergey Zigachev #include <linux/types.h>
28*b843c749SSergey Zigachev #include <linux/hash.h>
29*b843c749SSergey Zigachev #include <linux/bug.h>
30*b843c749SSergey Zigachev #include <asm/bitsperlong.h>
31*b843c749SSergey Zigachev
32*b843c749SSergey Zigachev #if BITS_PER_LONG == 32
33*b843c749SSergey Zigachev # define _CHASH_LONG_SHIFT 5
34*b843c749SSergey Zigachev #elif BITS_PER_LONG == 64
35*b843c749SSergey Zigachev # define _CHASH_LONG_SHIFT 6
36*b843c749SSergey Zigachev #else
37*b843c749SSergey Zigachev # error "Unexpected BITS_PER_LONG"
38*b843c749SSergey Zigachev #endif
39*b843c749SSergey Zigachev
40*b843c749SSergey Zigachev struct __chash_table {
41*b843c749SSergey Zigachev u8 bits;
42*b843c749SSergey Zigachev u8 key_size;
43*b843c749SSergey Zigachev unsigned int value_size;
44*b843c749SSergey Zigachev u32 size_mask;
45*b843c749SSergey Zigachev unsigned long *occup_bitmap, *valid_bitmap;
46*b843c749SSergey Zigachev union {
47*b843c749SSergey Zigachev u32 *keys32;
48*b843c749SSergey Zigachev u64 *keys64;
49*b843c749SSergey Zigachev };
50*b843c749SSergey Zigachev u8 *values;
51*b843c749SSergey Zigachev
52*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
53*b843c749SSergey Zigachev u64 hits, hits_steps, hits_time_ns;
54*b843c749SSergey Zigachev u64 miss, miss_steps, miss_time_ns;
55*b843c749SSergey Zigachev u64 relocs, reloc_dist;
56*b843c749SSergey Zigachev #endif
57*b843c749SSergey Zigachev };
58*b843c749SSergey Zigachev
59*b843c749SSergey Zigachev #define __CHASH_BITMAP_SIZE(bits) \
60*b843c749SSergey Zigachev (((1 << (bits)) + BITS_PER_LONG - 1) / BITS_PER_LONG)
61*b843c749SSergey Zigachev #define __CHASH_ARRAY_SIZE(bits, size) \
62*b843c749SSergey Zigachev ((((size) << (bits)) + sizeof(long) - 1) / sizeof(long))
63*b843c749SSergey Zigachev
64*b843c749SSergey Zigachev #define __CHASH_DATA_SIZE(bits, key_size, value_size) \
65*b843c749SSergey Zigachev (__CHASH_BITMAP_SIZE(bits) * 2 + \
66*b843c749SSergey Zigachev __CHASH_ARRAY_SIZE(bits, key_size) + \
67*b843c749SSergey Zigachev __CHASH_ARRAY_SIZE(bits, value_size))
68*b843c749SSergey Zigachev
69*b843c749SSergey Zigachev #define STRUCT_CHASH_TABLE(bits, key_size, value_size) \
70*b843c749SSergey Zigachev struct { \
71*b843c749SSergey Zigachev struct __chash_table table; \
72*b843c749SSergey Zigachev unsigned long data \
73*b843c749SSergey Zigachev [__CHASH_DATA_SIZE(bits, key_size, value_size)];\
74*b843c749SSergey Zigachev }
75*b843c749SSergey Zigachev
76*b843c749SSergey Zigachev /**
77*b843c749SSergey Zigachev * struct chash_table - Dynamically allocated closed hash table
78*b843c749SSergey Zigachev *
79*b843c749SSergey Zigachev * Use this struct for dynamically allocated hash tables (using
80*b843c749SSergey Zigachev * chash_table_alloc and chash_table_free), where the size is
81*b843c749SSergey Zigachev * determined at runtime.
82*b843c749SSergey Zigachev */
83*b843c749SSergey Zigachev struct chash_table {
84*b843c749SSergey Zigachev struct __chash_table table;
85*b843c749SSergey Zigachev unsigned long *data;
86*b843c749SSergey Zigachev };
87*b843c749SSergey Zigachev
88*b843c749SSergey Zigachev /**
89*b843c749SSergey Zigachev * DECLARE_CHASH_TABLE - macro to declare a closed hash table
90*b843c749SSergey Zigachev * @table: name of the declared hash table
91*b843c749SSergey Zigachev * @bts: Table size will be 2^bits entries
92*b843c749SSergey Zigachev * @key_sz: Size of hash keys in bytes, 4 or 8
93*b843c749SSergey Zigachev * @val_sz: Size of data values in bytes, can be 0
94*b843c749SSergey Zigachev *
95*b843c749SSergey Zigachev * This declares the hash table variable with a static size.
96*b843c749SSergey Zigachev *
97*b843c749SSergey Zigachev * The closed hash table stores key-value pairs with low memory and
98*b843c749SSergey Zigachev * lookup overhead. In operation it performs no dynamic memory
99*b843c749SSergey Zigachev * management. The data being stored does not require any
100*b843c749SSergey Zigachev * list_heads. The hash table performs best with small @val_sz and as
101*b843c749SSergey Zigachev * long as some space (about 50%) is left free in the table. But the
102*b843c749SSergey Zigachev * table can still work reasonably efficiently even when filled up to
103*b843c749SSergey Zigachev * about 90%. If bigger data items need to be stored and looked up,
104*b843c749SSergey Zigachev * store the pointer to it as value in the hash table.
105*b843c749SSergey Zigachev *
106*b843c749SSergey Zigachev * @val_sz may be 0. This can be useful when all the stored
107*b843c749SSergey Zigachev * information is contained in the key itself and the fact that it is
108*b843c749SSergey Zigachev * in the hash table (or not).
109*b843c749SSergey Zigachev */
110*b843c749SSergey Zigachev #define DECLARE_CHASH_TABLE(table, bts, key_sz, val_sz) \
111*b843c749SSergey Zigachev STRUCT_CHASH_TABLE(bts, key_sz, val_sz) table
112*b843c749SSergey Zigachev
113*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
114*b843c749SSergey Zigachev #define __CHASH_STATS_INIT(prefix), \
115*b843c749SSergey Zigachev prefix.hits = 0, \
116*b843c749SSergey Zigachev prefix.hits_steps = 0, \
117*b843c749SSergey Zigachev prefix.hits_time_ns = 0, \
118*b843c749SSergey Zigachev prefix.miss = 0, \
119*b843c749SSergey Zigachev prefix.miss_steps = 0, \
120*b843c749SSergey Zigachev prefix.miss_time_ns = 0, \
121*b843c749SSergey Zigachev prefix.relocs = 0, \
122*b843c749SSergey Zigachev prefix.reloc_dist = 0
123*b843c749SSergey Zigachev #else
124*b843c749SSergey Zigachev #define __CHASH_STATS_INIT(prefix)
125*b843c749SSergey Zigachev #endif
126*b843c749SSergey Zigachev
127*b843c749SSergey Zigachev #define __CHASH_TABLE_INIT(prefix, data, bts, key_sz, val_sz) \
128*b843c749SSergey Zigachev prefix.bits = (bts), \
129*b843c749SSergey Zigachev prefix.key_size = (key_sz), \
130*b843c749SSergey Zigachev prefix.value_size = (val_sz), \
131*b843c749SSergey Zigachev prefix.size_mask = ((1 << bts) - 1), \
132*b843c749SSergey Zigachev prefix.occup_bitmap = &data[0], \
133*b843c749SSergey Zigachev prefix.valid_bitmap = &data \
134*b843c749SSergey Zigachev [__CHASH_BITMAP_SIZE(bts)], \
135*b843c749SSergey Zigachev prefix.keys64 = (u64 *)&data \
136*b843c749SSergey Zigachev [__CHASH_BITMAP_SIZE(bts) * 2], \
137*b843c749SSergey Zigachev prefix.values = (u8 *)&data \
138*b843c749SSergey Zigachev [__CHASH_BITMAP_SIZE(bts) * 2 + \
139*b843c749SSergey Zigachev __CHASH_ARRAY_SIZE(bts, key_sz)] \
140*b843c749SSergey Zigachev __CHASH_STATS_INIT(prefix)
141*b843c749SSergey Zigachev
142*b843c749SSergey Zigachev /**
143*b843c749SSergey Zigachev * DEFINE_CHASH_TABLE - macro to define and initialize a closed hash table
144*b843c749SSergey Zigachev * @tbl: name of the declared hash table
145*b843c749SSergey Zigachev * @bts: Table size will be 2^bits entries
146*b843c749SSergey Zigachev * @key_sz: Size of hash keys in bytes, 4 or 8
147*b843c749SSergey Zigachev * @val_sz: Size of data values in bytes, can be 0
148*b843c749SSergey Zigachev *
149*b843c749SSergey Zigachev * Note: the macro can be used for global and local hash table variables.
150*b843c749SSergey Zigachev */
151*b843c749SSergey Zigachev #define DEFINE_CHASH_TABLE(tbl, bts, key_sz, val_sz) \
152*b843c749SSergey Zigachev DECLARE_CHASH_TABLE(tbl, bts, key_sz, val_sz) = { \
153*b843c749SSergey Zigachev .table = { \
154*b843c749SSergey Zigachev __CHASH_TABLE_INIT(, (tbl).data, bts, key_sz, val_sz) \
155*b843c749SSergey Zigachev }, \
156*b843c749SSergey Zigachev .data = {0} \
157*b843c749SSergey Zigachev }
158*b843c749SSergey Zigachev
159*b843c749SSergey Zigachev /**
160*b843c749SSergey Zigachev * INIT_CHASH_TABLE - Initialize a hash table declared by DECLARE_CHASH_TABLE
161*b843c749SSergey Zigachev * @tbl: name of the declared hash table
162*b843c749SSergey Zigachev * @bts: Table size will be 2^bits entries
163*b843c749SSergey Zigachev * @key_sz: Size of hash keys in bytes, 4 or 8
164*b843c749SSergey Zigachev * @val_sz: Size of data values in bytes, can be 0
165*b843c749SSergey Zigachev */
166*b843c749SSergey Zigachev #define INIT_CHASH_TABLE(tbl, bts, key_sz, val_sz) \
167*b843c749SSergey Zigachev __CHASH_TABLE_INIT(((tbl).table), (tbl).data, bts, key_sz, val_sz)
168*b843c749SSergey Zigachev
169*b843c749SSergey Zigachev int chash_table_alloc(struct chash_table *table, u8 bits, u8 key_size,
170*b843c749SSergey Zigachev unsigned int value_size, gfp_t gfp_mask);
171*b843c749SSergey Zigachev void chash_table_free(struct chash_table *table);
172*b843c749SSergey Zigachev
173*b843c749SSergey Zigachev /**
174*b843c749SSergey Zigachev * chash_table_dump_stats - Dump statistics of a closed hash table
175*b843c749SSergey Zigachev * @tbl: Pointer to the table structure
176*b843c749SSergey Zigachev *
177*b843c749SSergey Zigachev * Dumps some performance statistics of the table gathered in operation
178*b843c749SSergey Zigachev * in the kernel log using pr_debug. If CONFIG_DYNAMIC_DEBUG is enabled,
179*b843c749SSergey Zigachev * user must turn on messages for chash.c (file chash.c +p).
180*b843c749SSergey Zigachev */
181*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
182*b843c749SSergey Zigachev #define chash_table_dump_stats(tbl) __chash_table_dump_stats(&(*tbl).table)
183*b843c749SSergey Zigachev
184*b843c749SSergey Zigachev void __chash_table_dump_stats(struct __chash_table *table);
185*b843c749SSergey Zigachev #else
186*b843c749SSergey Zigachev #define chash_table_dump_stats(tbl)
187*b843c749SSergey Zigachev #endif
188*b843c749SSergey Zigachev
189*b843c749SSergey Zigachev /**
190*b843c749SSergey Zigachev * chash_table_reset_stats - Reset statistics of a closed hash table
191*b843c749SSergey Zigachev * @tbl: Pointer to the table structure
192*b843c749SSergey Zigachev */
193*b843c749SSergey Zigachev #ifdef CONFIG_CHASH_STATS
194*b843c749SSergey Zigachev #define chash_table_reset_stats(tbl) __chash_table_reset_stats(&(*tbl).table)
195*b843c749SSergey Zigachev
__chash_table_reset_stats(struct __chash_table * table)196*b843c749SSergey Zigachev static inline void __chash_table_reset_stats(struct __chash_table *table)
197*b843c749SSergey Zigachev {
198*b843c749SSergey Zigachev (void)table __CHASH_STATS_INIT((*table));
199*b843c749SSergey Zigachev }
200*b843c749SSergey Zigachev #else
201*b843c749SSergey Zigachev #define chash_table_reset_stats(tbl)
202*b843c749SSergey Zigachev #endif
203*b843c749SSergey Zigachev
204*b843c749SSergey Zigachev /**
205*b843c749SSergey Zigachev * chash_table_copy_in - Copy a new value into the hash table
206*b843c749SSergey Zigachev * @tbl: Pointer to the table structure
207*b843c749SSergey Zigachev * @key: Key of the entry to add or update
208*b843c749SSergey Zigachev * @value: Pointer to value to copy, may be NULL
209*b843c749SSergey Zigachev *
210*b843c749SSergey Zigachev * If @key already has an entry, its value is replaced. Otherwise a
211*b843c749SSergey Zigachev * new entry is added. If @value is NULL, the value is left unchanged
212*b843c749SSergey Zigachev * or uninitialized. Returns 1 if an entry already existed, 0 if a new
213*b843c749SSergey Zigachev * entry was added or %-ENOMEM if there was no free space in the
214*b843c749SSergey Zigachev * table.
215*b843c749SSergey Zigachev */
216*b843c749SSergey Zigachev #define chash_table_copy_in(tbl, key, value) \
217*b843c749SSergey Zigachev __chash_table_copy_in(&(*tbl).table, key, value)
218*b843c749SSergey Zigachev
219*b843c749SSergey Zigachev int __chash_table_copy_in(struct __chash_table *table, u64 key,
220*b843c749SSergey Zigachev const void *value);
221*b843c749SSergey Zigachev
222*b843c749SSergey Zigachev /**
223*b843c749SSergey Zigachev * chash_table_copy_out - Copy a value out of the hash table
224*b843c749SSergey Zigachev * @tbl: Pointer to the table structure
225*b843c749SSergey Zigachev * @key: Key of the entry to find
226*b843c749SSergey Zigachev * @value: Pointer to value to copy, may be NULL
227*b843c749SSergey Zigachev *
228*b843c749SSergey Zigachev * If @value is not NULL and the table has a non-0 value_size, the
229*b843c749SSergey Zigachev * value at @key is copied to @value. Returns the slot index of the
230*b843c749SSergey Zigachev * entry or %-EINVAL if @key was not found.
231*b843c749SSergey Zigachev */
232*b843c749SSergey Zigachev #define chash_table_copy_out(tbl, key, value) \
233*b843c749SSergey Zigachev __chash_table_copy_out(&(*tbl).table, key, value, false)
234*b843c749SSergey Zigachev
235*b843c749SSergey Zigachev int __chash_table_copy_out(struct __chash_table *table, u64 key,
236*b843c749SSergey Zigachev void *value, bool remove);
237*b843c749SSergey Zigachev
238*b843c749SSergey Zigachev /**
239*b843c749SSergey Zigachev * chash_table_remove - Remove an entry from the hash table
240*b843c749SSergey Zigachev * @tbl: Pointer to the table structure
241*b843c749SSergey Zigachev * @key: Key of the entry to find
242*b843c749SSergey Zigachev * @value: Pointer to value to copy, may be NULL
243*b843c749SSergey Zigachev *
244*b843c749SSergey Zigachev * If @value is not NULL and the table has a non-0 value_size, the
245*b843c749SSergey Zigachev * value at @key is copied to @value. The entry is removed from the
246*b843c749SSergey Zigachev * table. Returns the slot index of the removed entry or %-EINVAL if
247*b843c749SSergey Zigachev * @key was not found.
248*b843c749SSergey Zigachev */
249*b843c749SSergey Zigachev #define chash_table_remove(tbl, key, value) \
250*b843c749SSergey Zigachev __chash_table_copy_out(&(*tbl).table, key, value, true)
251*b843c749SSergey Zigachev
252*b843c749SSergey Zigachev /*
253*b843c749SSergey Zigachev * Low level iterator API used internally by the above functions.
254*b843c749SSergey Zigachev */
255*b843c749SSergey Zigachev struct chash_iter {
256*b843c749SSergey Zigachev struct __chash_table *table;
257*b843c749SSergey Zigachev unsigned long mask;
258*b843c749SSergey Zigachev int slot;
259*b843c749SSergey Zigachev };
260*b843c749SSergey Zigachev
261*b843c749SSergey Zigachev /**
262*b843c749SSergey Zigachev * CHASH_ITER_INIT - Initialize a hash table iterator
263*b843c749SSergey Zigachev * @tbl: Pointer to hash table to iterate over
264*b843c749SSergey Zigachev * @s: Initial slot number
265*b843c749SSergey Zigachev */
266*b843c749SSergey Zigachev #define CHASH_ITER_INIT(table, s) { \
267*b843c749SSergey Zigachev table, \
268*b843c749SSergey Zigachev 1UL << ((s) & (BITS_PER_LONG - 1)), \
269*b843c749SSergey Zigachev s \
270*b843c749SSergey Zigachev }
271*b843c749SSergey Zigachev /**
272*b843c749SSergey Zigachev * CHASH_ITER_SET - Set hash table iterator to new slot
273*b843c749SSergey Zigachev * @iter: Iterator
274*b843c749SSergey Zigachev * @s: Slot number
275*b843c749SSergey Zigachev */
276*b843c749SSergey Zigachev #define CHASH_ITER_SET(iter, s) \
277*b843c749SSergey Zigachev (iter).mask = 1UL << ((s) & (BITS_PER_LONG - 1)), \
278*b843c749SSergey Zigachev (iter).slot = (s)
279*b843c749SSergey Zigachev /**
280*b843c749SSergey Zigachev * CHASH_ITER_INC - Increment hash table iterator
281*b843c749SSergey Zigachev * @table: Hash table to iterate over
282*b843c749SSergey Zigachev *
283*b843c749SSergey Zigachev * Wraps around at the end.
284*b843c749SSergey Zigachev */
285*b843c749SSergey Zigachev #define CHASH_ITER_INC(iter) do { \
286*b843c749SSergey Zigachev (iter).mask = (iter).mask << 1 | \
287*b843c749SSergey Zigachev (iter).mask >> (BITS_PER_LONG - 1); \
288*b843c749SSergey Zigachev (iter).slot = ((iter).slot + 1) & (iter).table->size_mask; \
289*b843c749SSergey Zigachev } while (0)
290*b843c749SSergey Zigachev
chash_iter_is_valid(const struct chash_iter iter)291*b843c749SSergey Zigachev static inline bool chash_iter_is_valid(const struct chash_iter iter)
292*b843c749SSergey Zigachev {
293*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
294*b843c749SSergey Zigachev return !!(iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &
295*b843c749SSergey Zigachev iter.mask);
296*b843c749SSergey Zigachev }
chash_iter_is_empty(const struct chash_iter iter)297*b843c749SSergey Zigachev static inline bool chash_iter_is_empty(const struct chash_iter iter)
298*b843c749SSergey Zigachev {
299*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
300*b843c749SSergey Zigachev return !(iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &
301*b843c749SSergey Zigachev iter.mask);
302*b843c749SSergey Zigachev }
303*b843c749SSergey Zigachev
chash_iter_set_valid(const struct chash_iter iter)304*b843c749SSergey Zigachev static inline void chash_iter_set_valid(const struct chash_iter iter)
305*b843c749SSergey Zigachev {
306*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
307*b843c749SSergey Zigachev iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] |= iter.mask;
308*b843c749SSergey Zigachev iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] |= iter.mask;
309*b843c749SSergey Zigachev }
chash_iter_set_invalid(const struct chash_iter iter)310*b843c749SSergey Zigachev static inline void chash_iter_set_invalid(const struct chash_iter iter)
311*b843c749SSergey Zigachev {
312*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
313*b843c749SSergey Zigachev iter.table->valid_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &= ~iter.mask;
314*b843c749SSergey Zigachev }
chash_iter_set_empty(const struct chash_iter iter)315*b843c749SSergey Zigachev static inline void chash_iter_set_empty(const struct chash_iter iter)
316*b843c749SSergey Zigachev {
317*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
318*b843c749SSergey Zigachev iter.table->occup_bitmap[iter.slot >> _CHASH_LONG_SHIFT] &= ~iter.mask;
319*b843c749SSergey Zigachev }
320*b843c749SSergey Zigachev
chash_iter_key32(const struct chash_iter iter)321*b843c749SSergey Zigachev static inline u32 chash_iter_key32(const struct chash_iter iter)
322*b843c749SSergey Zigachev {
323*b843c749SSergey Zigachev BUG_ON(iter.table->key_size != 4);
324*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
325*b843c749SSergey Zigachev return iter.table->keys32[iter.slot];
326*b843c749SSergey Zigachev }
chash_iter_key64(const struct chash_iter iter)327*b843c749SSergey Zigachev static inline u64 chash_iter_key64(const struct chash_iter iter)
328*b843c749SSergey Zigachev {
329*b843c749SSergey Zigachev BUG_ON(iter.table->key_size != 8);
330*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
331*b843c749SSergey Zigachev return iter.table->keys64[iter.slot];
332*b843c749SSergey Zigachev }
chash_iter_key(const struct chash_iter iter)333*b843c749SSergey Zigachev static inline u64 chash_iter_key(const struct chash_iter iter)
334*b843c749SSergey Zigachev {
335*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
336*b843c749SSergey Zigachev return (iter.table->key_size == 4) ?
337*b843c749SSergey Zigachev iter.table->keys32[iter.slot] : iter.table->keys64[iter.slot];
338*b843c749SSergey Zigachev }
339*b843c749SSergey Zigachev
chash_iter_hash32(const struct chash_iter iter)340*b843c749SSergey Zigachev static inline u32 chash_iter_hash32(const struct chash_iter iter)
341*b843c749SSergey Zigachev {
342*b843c749SSergey Zigachev BUG_ON(iter.table->key_size != 4);
343*b843c749SSergey Zigachev return hash_32(chash_iter_key32(iter), iter.table->bits);
344*b843c749SSergey Zigachev }
345*b843c749SSergey Zigachev
chash_iter_hash64(const struct chash_iter iter)346*b843c749SSergey Zigachev static inline u32 chash_iter_hash64(const struct chash_iter iter)
347*b843c749SSergey Zigachev {
348*b843c749SSergey Zigachev BUG_ON(iter.table->key_size != 8);
349*b843c749SSergey Zigachev return hash_64(chash_iter_key64(iter), iter.table->bits);
350*b843c749SSergey Zigachev }
351*b843c749SSergey Zigachev
chash_iter_hash(const struct chash_iter iter)352*b843c749SSergey Zigachev static inline u32 chash_iter_hash(const struct chash_iter iter)
353*b843c749SSergey Zigachev {
354*b843c749SSergey Zigachev return (iter.table->key_size == 4) ?
355*b843c749SSergey Zigachev hash_32(chash_iter_key32(iter), iter.table->bits) :
356*b843c749SSergey Zigachev hash_64(chash_iter_key64(iter), iter.table->bits);
357*b843c749SSergey Zigachev }
358*b843c749SSergey Zigachev
chash_iter_value(const struct chash_iter iter)359*b843c749SSergey Zigachev static inline void *chash_iter_value(const struct chash_iter iter)
360*b843c749SSergey Zigachev {
361*b843c749SSergey Zigachev BUG_ON((unsigned)iter.slot >= (1 << iter.table->bits));
362*b843c749SSergey Zigachev return iter.table->values +
363*b843c749SSergey Zigachev ((unsigned long)iter.slot * iter.table->value_size);
364*b843c749SSergey Zigachev }
365*b843c749SSergey Zigachev
366*b843c749SSergey Zigachev #endif /* _LINUX_CHASH_H */
367