1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#include <linux/kernel.h>
36#include <linux/bitops.h>
37
38#include "spectrum_cnt.h"
39
40#define MLXSW_SP_COUNTER_POOL_BANK_SIZE 4096
41
42struct mlxsw_sp_counter_sub_pool {
43 unsigned int base_index;
44 unsigned int size;
45 unsigned int entry_size;
46 unsigned int bank_count;
47};
48
49struct mlxsw_sp_counter_pool {
50 unsigned int pool_size;
51 unsigned long *usage;
52 struct mlxsw_sp_counter_sub_pool *sub_pools;
53};
54
55static struct mlxsw_sp_counter_sub_pool mlxsw_sp_counter_sub_pools[] = {
56 [MLXSW_SP_COUNTER_SUB_POOL_FLOW] = {
57 .bank_count = 6,
58 },
59 [MLXSW_SP_COUNTER_SUB_POOL_RIF] = {
60 .bank_count = 2,
61 }
62};
63
64static int mlxsw_sp_counter_pool_validate(struct mlxsw_sp *mlxsw_sp)
65{
66 unsigned int total_bank_config = 0;
67 unsigned int pool_size;
68 int i;
69
70 pool_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, COUNTER_POOL_SIZE);
71
72 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_counter_sub_pools); i++)
73 total_bank_config += mlxsw_sp_counter_sub_pools[i].bank_count;
74 if (total_bank_config > pool_size / MLXSW_SP_COUNTER_POOL_BANK_SIZE + 1)
75 return -EINVAL;
76 return 0;
77}
78
79static int mlxsw_sp_counter_sub_pools_prepare(struct mlxsw_sp *mlxsw_sp)
80{
81 struct mlxsw_sp_counter_sub_pool *sub_pool;
82
83
84 sub_pool = &mlxsw_sp_counter_sub_pools[MLXSW_SP_COUNTER_SUB_POOL_FLOW];
85 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_SIZE_PACKETS_BYTES))
86 return -EIO;
87 sub_pool->entry_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
88 COUNTER_SIZE_PACKETS_BYTES);
89
90 sub_pool = &mlxsw_sp_counter_sub_pools[MLXSW_SP_COUNTER_SUB_POOL_RIF];
91 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_SIZE_ROUTER_BASIC))
92 return -EIO;
93 sub_pool->entry_size = MLXSW_CORE_RES_GET(mlxsw_sp->core,
94 COUNTER_SIZE_ROUTER_BASIC);
95 return 0;
96}
97
98int mlxsw_sp_counter_pool_init(struct mlxsw_sp *mlxsw_sp)
99{
100 struct mlxsw_sp_counter_sub_pool *sub_pool;
101 struct mlxsw_sp_counter_pool *pool;
102 unsigned int base_index;
103 unsigned int map_size;
104 int i;
105 int err;
106
107 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, COUNTER_POOL_SIZE))
108 return -EIO;
109
110 err = mlxsw_sp_counter_pool_validate(mlxsw_sp);
111 if (err)
112 return err;
113
114 err = mlxsw_sp_counter_sub_pools_prepare(mlxsw_sp);
115 if (err)
116 return err;
117
118 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
119 if (!pool)
120 return -ENOMEM;
121
122 pool->pool_size = MLXSW_CORE_RES_GET(mlxsw_sp->core, COUNTER_POOL_SIZE);
123 map_size = BITS_TO_LONGS(pool->pool_size) * sizeof(unsigned long);
124
125 pool->usage = kzalloc(map_size, GFP_KERNEL);
126 if (!pool->usage) {
127 err = -ENOMEM;
128 goto err_usage_alloc;
129 }
130
131 pool->sub_pools = mlxsw_sp_counter_sub_pools;
132
133
134
135 base_index = 0;
136 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_counter_sub_pools); i++) {
137 sub_pool = &pool->sub_pools[i];
138 sub_pool->size = sub_pool->bank_count *
139 MLXSW_SP_COUNTER_POOL_BANK_SIZE;
140 sub_pool->base_index = base_index;
141 base_index += sub_pool->size;
142
143 if (sub_pool->base_index + sub_pool->size > pool->pool_size)
144 sub_pool->size = pool->pool_size - sub_pool->base_index;
145 }
146
147 mlxsw_sp->counter_pool = pool;
148 return 0;
149
150err_usage_alloc:
151 kfree(pool);
152 return err;
153}
154
155void mlxsw_sp_counter_pool_fini(struct mlxsw_sp *mlxsw_sp)
156{
157 struct mlxsw_sp_counter_pool *pool = mlxsw_sp->counter_pool;
158
159 WARN_ON(find_first_bit(pool->usage, pool->pool_size) !=
160 pool->pool_size);
161 kfree(pool->usage);
162 kfree(pool);
163}
164
165int mlxsw_sp_counter_alloc(struct mlxsw_sp *mlxsw_sp,
166 enum mlxsw_sp_counter_sub_pool_id sub_pool_id,
167 unsigned int *p_counter_index)
168{
169 struct mlxsw_sp_counter_pool *pool = mlxsw_sp->counter_pool;
170 struct mlxsw_sp_counter_sub_pool *sub_pool;
171 unsigned int entry_index;
172 unsigned int stop_index;
173 int i;
174
175 sub_pool = &mlxsw_sp_counter_sub_pools[sub_pool_id];
176 stop_index = sub_pool->base_index + sub_pool->size;
177 entry_index = sub_pool->base_index;
178
179 entry_index = find_next_zero_bit(pool->usage, stop_index, entry_index);
180 if (entry_index == stop_index)
181 return -ENOBUFS;
182
183
184
185 if (entry_index + sub_pool->entry_size > stop_index)
186 return -ENOBUFS;
187 for (i = 0; i < sub_pool->entry_size; i++)
188 __set_bit(entry_index + i, pool->usage);
189
190 *p_counter_index = entry_index;
191 return 0;
192}
193
194void mlxsw_sp_counter_free(struct mlxsw_sp *mlxsw_sp,
195 enum mlxsw_sp_counter_sub_pool_id sub_pool_id,
196 unsigned int counter_index)
197{
198 struct mlxsw_sp_counter_pool *pool = mlxsw_sp->counter_pool;
199 struct mlxsw_sp_counter_sub_pool *sub_pool;
200 int i;
201
202 if (WARN_ON(counter_index >= pool->pool_size))
203 return;
204 sub_pool = &mlxsw_sp_counter_sub_pools[sub_pool_id];
205 for (i = 0; i < sub_pool->entry_size; i++)
206 __clear_bit(counter_index + i, pool->usage);
207}
208