+ break;
+ }
+ return entry;
+}
+
+static struct mlx5_cache_entry *
+cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
+{
+ struct mlx5_cache_entry *entry;
+
+ rte_rwlock_read_lock(&list->lock);
+ entry = __cache_lookup(list, ctx, reuse);
+ rte_rwlock_read_unlock(&list->lock);
+ return entry;
+}
+
+struct mlx5_cache_entry *
+mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
+{
+ return cache_lookup(list, ctx, false);
+}
+
+struct mlx5_cache_entry *
+mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
+{
+ struct mlx5_cache_entry *entry;
+ uint32_t prev_gen_cnt = 0;
+
+ MLX5_ASSERT(list);
+ prev_gen_cnt = __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE);
+ /* Lookup with read lock, reuse if found. */
+ entry = cache_lookup(list, ctx, true);
+ if (entry)
+ return entry;
+ /* Not found, append with write lock - block read from other threads. */
+ rte_rwlock_write_lock(&list->lock);
+ /* If list changed by other threads before lock, search again. */
+ if (prev_gen_cnt != __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE)) {
+ /* Lookup and reuse w/o read lock. */
+ entry = __cache_lookup(list, ctx, true);
+ if (entry)
+ goto done;
+ }
+ entry = list->cb_create(list, entry, ctx);
+ if (!entry) {
+ DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
+ list->name, (void *)entry);
+ goto done;
+ }
+ entry->ref_cnt = 1;
+ LIST_INSERT_HEAD(&list->head, entry, next);
+ __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_RELEASE);
+ __atomic_add_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
+ DRV_LOG(DEBUG, "Cache list %s entry %p new: %u.",
+ list->name, (void *)entry, entry->ref_cnt);
+done:
+ rte_rwlock_write_unlock(&list->lock);
+ return entry;
+}
+
+int
+mlx5_cache_unregister(struct mlx5_cache_list *list,
+ struct mlx5_cache_entry *entry)
+{
+ rte_rwlock_write_lock(&list->lock);
+ MLX5_ASSERT(entry && entry->next.le_prev);
+ DRV_LOG(DEBUG, "Cache list %s entry %p ref--: %u.",
+ list->name, (void *)entry, entry->ref_cnt);
+ if (--entry->ref_cnt) {
+ rte_rwlock_write_unlock(&list->lock);
+ return 1;