1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip Sparx5 Switch driver
4 * Copyright (c) 2023 Microchip Technology Inc. and its subsidiaries.
7 #include "sparx5_main_regs.h"
8 #include "sparx5_main.h"
10 static u32
sparx5_pool_id_to_idx(u32 id
)
15 u32
sparx5_pool_idx_to_id(u32 idx
)
20 /* Release resource from pool.
21 * Return reference count on success, otherwise return error.
23 int sparx5_pool_put(struct sparx5_pool_entry
*pool
, int size
, u32 id
)
25 struct sparx5_pool_entry
*e_itr
;
27 e_itr
= (pool
+ sparx5_pool_id_to_idx(id
));
28 if (e_itr
->ref_cnt
== 0)
31 return --e_itr
->ref_cnt
;
34 /* Get resource from pool.
35 * Return reference count on success, otherwise return error.
37 int sparx5_pool_get(struct sparx5_pool_entry
*pool
, int size
, u32
*id
)
39 struct sparx5_pool_entry
*e_itr
;
42 for (i
= 0, e_itr
= pool
; i
< size
; i
++, e_itr
++) {
43 if (e_itr
->ref_cnt
== 0) {
44 *id
= sparx5_pool_idx_to_id(i
);
45 return ++e_itr
->ref_cnt
;
52 /* Get resource from pool that matches index.
53 * Return reference count on success, otherwise return error.
55 int sparx5_pool_get_with_idx(struct sparx5_pool_entry
*pool
, int size
, u32 idx
,
58 struct sparx5_pool_entry
*e_itr
;
61 for (i
= 0, e_itr
= pool
; i
< size
; i
++, e_itr
++) {
62 /* Pool index of first free entry */
63 if (e_itr
->ref_cnt
== 0 && ret
== -ENOSPC
)
65 /* Tc index already in use ? */
66 if (e_itr
->idx
== idx
&& e_itr
->ref_cnt
> 0) {
72 /* Did we find a free entry? */
74 *id
= sparx5_pool_idx_to_id(ret
);
77 return ++e_itr
->ref_cnt
;