17f30db1eSPaul Blakey /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
27f30db1eSPaul Blakey /* Copyright (c) 2019 Mellanox Technologies */
37f30db1eSPaul Blakey 
47f30db1eSPaul Blakey #ifndef __MLX5_MAPPING_H__
57f30db1eSPaul Blakey #define __MLX5_MAPPING_H__
67f30db1eSPaul Blakey 
77f30db1eSPaul Blakey struct mapping_ctx;
87f30db1eSPaul Blakey 
97f30db1eSPaul Blakey int mapping_add(struct mapping_ctx *ctx, void *data, u32 *id);
107f30db1eSPaul Blakey int mapping_remove(struct mapping_ctx *ctx, u32 id);
117f30db1eSPaul Blakey int mapping_find(struct mapping_ctx *ctx, u32 id, void *data);
127f30db1eSPaul Blakey 
137f30db1eSPaul Blakey /* mapping uses an xarray to map data to ids in add(), and for find().
147f30db1eSPaul Blakey  * For locking, it uses a internal xarray spin lock for add()/remove(),
157f30db1eSPaul Blakey  * find() uses rcu_read_lock().
167f30db1eSPaul Blakey  * Choosing delayed_removal postpones the removal of a previously mapped
177f30db1eSPaul Blakey  * id by MAPPING_GRACE_PERIOD milliseconds.
187f30db1eSPaul Blakey  * This is to avoid races against hardware, where we mark the packet in
197f30db1eSPaul Blakey  * hardware with a previous id, and quick remove() and add() reusing the same
207f30db1eSPaul Blakey  * previous id. Then find() will get the new mapping instead of the old
217f30db1eSPaul Blakey  * which was used to mark the packet.
227f30db1eSPaul Blakey  */
237f30db1eSPaul Blakey struct mapping_ctx *mapping_create(size_t data_size, u32 max_id,
247f30db1eSPaul Blakey 				   bool delayed_removal);
257f30db1eSPaul Blakey void mapping_destroy(struct mapping_ctx *ctx);
267f30db1eSPaul Blakey 
27*5d5defd6SRoi Dayan /* adds mapping with an id or get an existing mapping with the same id
28*5d5defd6SRoi Dayan  */
29*5d5defd6SRoi Dayan struct mapping_ctx *
30*5d5defd6SRoi Dayan mapping_create_for_id(u64 id, u8 type, size_t data_size, u32 max_id, bool delayed_removal);
31*5d5defd6SRoi Dayan 
327f30db1eSPaul Blakey #endif /* __MLX5_MAPPING_H__ */
33