1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/mutex.h>
6 #include <linux/slab.h>
10 struct mlxsw_sp_kvdl
{
11 const struct mlxsw_sp_kvdl_ops
*kvdl_ops
;
12 struct mutex kvdl_lock
; /* Protects kvdl allocations */
14 /* priv has to be always the last item */
17 int mlxsw_sp_kvdl_init(struct mlxsw_sp
*mlxsw_sp
)
19 const struct mlxsw_sp_kvdl_ops
*kvdl_ops
= mlxsw_sp
->kvdl_ops
;
20 struct mlxsw_sp_kvdl
*kvdl
;
23 kvdl
= kzalloc(sizeof(*mlxsw_sp
->kvdl
) + kvdl_ops
->priv_size
,
27 mutex_init(&kvdl
->kvdl_lock
);
28 kvdl
->kvdl_ops
= kvdl_ops
;
29 mlxsw_sp
->kvdl
= kvdl
;
31 err
= kvdl_ops
->init(mlxsw_sp
, kvdl
->priv
);
37 mutex_destroy(&kvdl
->kvdl_lock
);
42 void mlxsw_sp_kvdl_fini(struct mlxsw_sp
*mlxsw_sp
)
44 struct mlxsw_sp_kvdl
*kvdl
= mlxsw_sp
->kvdl
;
46 kvdl
->kvdl_ops
->fini(mlxsw_sp
, kvdl
->priv
);
47 mutex_destroy(&kvdl
->kvdl_lock
);
51 int mlxsw_sp_kvdl_alloc(struct mlxsw_sp
*mlxsw_sp
,
52 enum mlxsw_sp_kvdl_entry_type type
,
53 unsigned int entry_count
, u32
*p_entry_index
)
55 struct mlxsw_sp_kvdl
*kvdl
= mlxsw_sp
->kvdl
;
58 mutex_lock(&kvdl
->kvdl_lock
);
59 err
= kvdl
->kvdl_ops
->alloc(mlxsw_sp
, kvdl
->priv
, type
,
60 entry_count
, p_entry_index
);
61 mutex_unlock(&kvdl
->kvdl_lock
);
66 void mlxsw_sp_kvdl_free(struct mlxsw_sp
*mlxsw_sp
,
67 enum mlxsw_sp_kvdl_entry_type type
,
68 unsigned int entry_count
, int entry_index
)
70 struct mlxsw_sp_kvdl
*kvdl
= mlxsw_sp
->kvdl
;
72 mutex_lock(&kvdl
->kvdl_lock
);
73 kvdl
->kvdl_ops
->free(mlxsw_sp
, kvdl
->priv
, type
,
74 entry_count
, entry_index
);
75 mutex_unlock(&kvdl
->kvdl_lock
);
78 int mlxsw_sp_kvdl_alloc_count_query(struct mlxsw_sp
*mlxsw_sp
,
79 enum mlxsw_sp_kvdl_entry_type type
,
80 unsigned int entry_count
,
81 unsigned int *p_alloc_count
)
83 struct mlxsw_sp_kvdl
*kvdl
= mlxsw_sp
->kvdl
;
85 return kvdl
->kvdl_ops
->alloc_size_query(mlxsw_sp
, kvdl
->priv
, type
,
86 entry_count
, p_alloc_count
);