Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / microchip / sparx5 / sparx5_pool.c
blobb4b280c6138bda5bbb06c77d561db242aa0f4cad
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Microchip Sparx5 Switch driver
4 * Copyright (c) 2023 Microchip Technology Inc. and its subsidiaries.
5 */
7 #include "sparx5_main_regs.h"
8 #include "sparx5_main.h"
10 static u32 sparx5_pool_id_to_idx(u32 id)
12 return --id;
15 u32 sparx5_pool_idx_to_id(u32 idx)
17 return ++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)
29 return -EINVAL;
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;
40 int i;
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;
49 return -ENOSPC;
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,
56 u32 *id)
58 struct sparx5_pool_entry *e_itr;
59 int i, ret = -ENOSPC;
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)
64 ret = i;
65 /* Tc index already in use ? */
66 if (e_itr->idx == idx && e_itr->ref_cnt > 0) {
67 ret = i;
68 break;
72 /* Did we find a free entry? */
73 if (ret >= 0) {
74 *id = sparx5_pool_idx_to_id(ret);
75 e_itr = (pool + ret);
76 e_itr->idx = idx;
77 return ++e_itr->ref_cnt;
80 return ret;