net/mlx5e: Move Q counters allocation and drop RQ to init_rx
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum2_kvdl.c
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */
3
4 #include <linux/kernel.h>
5 #include <linux/bitops.h>
6
7 #include "spectrum.h"
8 #include "core.h"
9 #include "reg.h"
10 #include "resources.h"
11
12 struct mlxsw_sp2_kvdl_part_info {
13         u8 res_type;
14         /* For each defined partititon we need to know how many
15          * usage bits we need and how many indexes there are
16          * represented by a single bit. This could be got from FW
17          * querying appropriate resources. So have the resource
18          * ids for for this purpose in partition definition.
19          */
20         enum mlxsw_res_id usage_bit_count_res_id;
21         enum mlxsw_res_id index_range_res_id;
22 };
23
24 #define MLXSW_SP2_KVDL_PART_INFO(_entry_type, _res_type,                        \
25                                  _usage_bit_count_res_id, _index_range_res_id)  \
26 [MLXSW_SP_KVDL_ENTRY_TYPE_##_entry_type] = {                                    \
27         .res_type = _res_type,                                                  \
28         .usage_bit_count_res_id = MLXSW_RES_ID_##_usage_bit_count_res_id,       \
29         .index_range_res_id = MLXSW_RES_ID_##_index_range_res_id,               \
30 }
31
32 static const struct mlxsw_sp2_kvdl_part_info mlxsw_sp2_kvdl_parts_info[] = {
33         MLXSW_SP2_KVDL_PART_INFO(ADJ, 0x21, KVD_SIZE, MAX_KVD_LINEAR_RANGE),
34         MLXSW_SP2_KVDL_PART_INFO(ACTSET, 0x23, MAX_KVD_ACTION_SETS,
35                                  MAX_KVD_ACTION_SETS),
36         MLXSW_SP2_KVDL_PART_INFO(PBS, 0x24, KVD_SIZE, KVD_SIZE),
37         MLXSW_SP2_KVDL_PART_INFO(MCRIGR, 0x26, KVD_SIZE, KVD_SIZE),
38 };
39
40 #define MLXSW_SP2_KVDL_PARTS_INFO_LEN ARRAY_SIZE(mlxsw_sp2_kvdl_parts_info)
41
42 struct mlxsw_sp2_kvdl_part {
43         const struct mlxsw_sp2_kvdl_part_info *info;
44         unsigned int usage_bit_count;
45         unsigned int indexes_per_usage_bit;
46         unsigned int last_allocated_bit;
47         unsigned long usage[0]; /* Usage bits */
48 };
49
50 struct mlxsw_sp2_kvdl {
51         struct mlxsw_sp2_kvdl_part *parts[MLXSW_SP2_KVDL_PARTS_INFO_LEN];
52 };
53
54 static int mlxsw_sp2_kvdl_part_find_zero_bits(struct mlxsw_sp2_kvdl_part *part,
55                                               unsigned int bit_count,
56                                               unsigned int *p_bit)
57 {
58         unsigned int start_bit;
59         unsigned int bit;
60         unsigned int i;
61         bool wrap = false;
62
63         start_bit = part->last_allocated_bit + 1;
64         if (start_bit == part->usage_bit_count)
65                 start_bit = 0;
66         bit = start_bit;
67 again:
68         bit = find_next_zero_bit(part->usage, part->usage_bit_count, bit);
69         if (!wrap && bit + bit_count >= part->usage_bit_count) {
70                 wrap = true;
71                 bit = 0;
72                 goto again;
73         }
74         if (wrap && bit + bit_count >= start_bit)
75                 return -ENOBUFS;
76         for (i = 0; i < bit_count; i++) {
77                 if (test_bit(bit + i, part->usage)) {
78                         bit += bit_count;
79                         goto again;
80                 }
81         }
82         *p_bit = bit;
83         return 0;
84 }
85
86 static int mlxsw_sp2_kvdl_part_alloc(struct mlxsw_sp2_kvdl_part *part,
87                                      unsigned int size,
88                                      u32 *p_kvdl_index)
89 {
90         unsigned int bit_count;
91         unsigned int bit;
92         unsigned int i;
93         int err;
94
95         bit_count = DIV_ROUND_UP(size, part->indexes_per_usage_bit);
96         err = mlxsw_sp2_kvdl_part_find_zero_bits(part, bit_count, &bit);
97         if (err)
98                 return err;
99         for (i = 0; i < bit_count; i++)
100                 __set_bit(bit + i, part->usage);
101         *p_kvdl_index = bit * part->indexes_per_usage_bit;
102         return 0;
103 }
104
105 static int mlxsw_sp2_kvdl_rec_del(struct mlxsw_sp *mlxsw_sp, u8 res_type,
106                                   u16 size, u32 kvdl_index)
107 {
108         char *iedr_pl;
109         int err;
110
111         iedr_pl = kmalloc(MLXSW_REG_IEDR_LEN, GFP_KERNEL);
112         if (!iedr_pl)
113                 return -ENOMEM;
114
115         mlxsw_reg_iedr_pack(iedr_pl);
116         mlxsw_reg_iedr_rec_pack(iedr_pl, 0, res_type, size, kvdl_index);
117         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(iedr), iedr_pl);
118         kfree(iedr_pl);
119         return err;
120 }
121
122 static void mlxsw_sp2_kvdl_part_free(struct mlxsw_sp *mlxsw_sp,
123                                      struct mlxsw_sp2_kvdl_part *part,
124                                      unsigned int size, u32 kvdl_index)
125 {
126         unsigned int bit_count;
127         unsigned int bit;
128         unsigned int i;
129         int err;
130
131         /* We need to ask FW to delete previously used KVD linear index */
132         err = mlxsw_sp2_kvdl_rec_del(mlxsw_sp, part->info->res_type,
133                                      size, kvdl_index);
134         if (err)
135                 return;
136
137         bit_count = DIV_ROUND_UP(size, part->indexes_per_usage_bit);
138         bit = kvdl_index / part->indexes_per_usage_bit;
139         for (i = 0; i < bit_count; i++)
140                 __clear_bit(bit + i, part->usage);
141 }
142
143 static int mlxsw_sp2_kvdl_alloc(struct mlxsw_sp *mlxsw_sp, void *priv,
144                                 enum mlxsw_sp_kvdl_entry_type type,
145                                 unsigned int entry_count,
146                                 u32 *p_entry_index)
147 {
148         unsigned int size = entry_count * mlxsw_sp_kvdl_entry_size(type);
149         struct mlxsw_sp2_kvdl *kvdl = priv;
150         struct mlxsw_sp2_kvdl_part *part = kvdl->parts[type];
151
152         return mlxsw_sp2_kvdl_part_alloc(part, size, p_entry_index);
153 }
154
155 static void mlxsw_sp2_kvdl_free(struct mlxsw_sp *mlxsw_sp, void *priv,
156                                 enum mlxsw_sp_kvdl_entry_type type,
157                                 unsigned int entry_count,
158                                 int entry_index)
159 {
160         unsigned int size = entry_count * mlxsw_sp_kvdl_entry_size(type);
161         struct mlxsw_sp2_kvdl *kvdl = priv;
162         struct mlxsw_sp2_kvdl_part *part = kvdl->parts[type];
163
164         return mlxsw_sp2_kvdl_part_free(mlxsw_sp, part, size, entry_index);
165 }
166
167 static int mlxsw_sp2_kvdl_alloc_size_query(struct mlxsw_sp *mlxsw_sp,
168                                            void *priv,
169                                            enum mlxsw_sp_kvdl_entry_type type,
170                                            unsigned int entry_count,
171                                            unsigned int *p_alloc_count)
172 {
173         *p_alloc_count = entry_count;
174         return 0;
175 }
176
177 static struct mlxsw_sp2_kvdl_part *
178 mlxsw_sp2_kvdl_part_init(struct mlxsw_sp *mlxsw_sp,
179                          const struct mlxsw_sp2_kvdl_part_info *info)
180 {
181         unsigned int indexes_per_usage_bit;
182         struct mlxsw_sp2_kvdl_part *part;
183         unsigned int index_range;
184         unsigned int usage_bit_count;
185         size_t usage_size;
186
187         if (!mlxsw_core_res_valid(mlxsw_sp->core,
188                                   info->usage_bit_count_res_id) ||
189             !mlxsw_core_res_valid(mlxsw_sp->core,
190                                   info->index_range_res_id))
191                 return ERR_PTR(-EIO);
192         usage_bit_count = mlxsw_core_res_get(mlxsw_sp->core,
193                                              info->usage_bit_count_res_id);
194         index_range = mlxsw_core_res_get(mlxsw_sp->core,
195                                          info->index_range_res_id);
196
197         /* For some partitions, one usage bit represents a group of indexes.
198          * That's why we compute the number of indexes per usage bit here,
199          * according to queried resources.
200          */
201         indexes_per_usage_bit = index_range / usage_bit_count;
202
203         usage_size = BITS_TO_LONGS(usage_bit_count) * sizeof(unsigned long);
204         part = kzalloc(sizeof(*part) + usage_size, GFP_KERNEL);
205         if (!part)
206                 return ERR_PTR(-ENOMEM);
207         part->info = info;
208         part->usage_bit_count = usage_bit_count;
209         part->indexes_per_usage_bit = indexes_per_usage_bit;
210         part->last_allocated_bit = usage_bit_count - 1;
211         return part;
212 }
213
214 static void mlxsw_sp2_kvdl_part_fini(struct mlxsw_sp2_kvdl_part *part)
215 {
216         kfree(part);
217 }
218
219 static int mlxsw_sp2_kvdl_parts_init(struct mlxsw_sp *mlxsw_sp,
220                                      struct mlxsw_sp2_kvdl *kvdl)
221 {
222         const struct mlxsw_sp2_kvdl_part_info *info;
223         int i;
224         int err;
225
226         for (i = 0; i < MLXSW_SP2_KVDL_PARTS_INFO_LEN; i++) {
227                 info = &mlxsw_sp2_kvdl_parts_info[i];
228                 kvdl->parts[i] = mlxsw_sp2_kvdl_part_init(mlxsw_sp, info);
229                 if (IS_ERR(kvdl->parts[i])) {
230                         err = PTR_ERR(kvdl->parts[i]);
231                         goto err_kvdl_part_init;
232                 }
233         }
234         return 0;
235
236 err_kvdl_part_init:
237         for (i--; i >= 0; i--)
238                 mlxsw_sp2_kvdl_part_fini(kvdl->parts[i]);
239         return err;
240 }
241
242 static void mlxsw_sp2_kvdl_parts_fini(struct mlxsw_sp2_kvdl *kvdl)
243 {
244         int i;
245
246         for (i = 0; i < MLXSW_SP2_KVDL_PARTS_INFO_LEN; i++)
247                 mlxsw_sp2_kvdl_part_fini(kvdl->parts[i]);
248 }
249
250 static int mlxsw_sp2_kvdl_init(struct mlxsw_sp *mlxsw_sp, void *priv)
251 {
252         struct mlxsw_sp2_kvdl *kvdl = priv;
253
254         return mlxsw_sp2_kvdl_parts_init(mlxsw_sp, kvdl);
255 }
256
257 static void mlxsw_sp2_kvdl_fini(struct mlxsw_sp *mlxsw_sp, void *priv)
258 {
259         struct mlxsw_sp2_kvdl *kvdl = priv;
260
261         mlxsw_sp2_kvdl_parts_fini(kvdl);
262 }
263
264 const struct mlxsw_sp_kvdl_ops mlxsw_sp2_kvdl_ops = {
265         .priv_size = sizeof(struct mlxsw_sp2_kvdl),
266         .init = mlxsw_sp2_kvdl_init,
267         .fini = mlxsw_sp2_kvdl_fini,
268         .alloc = mlxsw_sp2_kvdl_alloc,
269         .free = mlxsw_sp2_kvdl_free,
270         .alloc_size_query = mlxsw_sp2_kvdl_alloc_size_query,
271 };