1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum1_kvdl.c
3  * Copyright (c) 2018 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2018 Jiri Pirko <jiri@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/bitops.h>
37 
38 #include "spectrum.h"
39 
40 #define MLXSW_SP1_KVDL_SINGLE_BASE 0
41 #define MLXSW_SP1_KVDL_SINGLE_SIZE 16384
42 #define MLXSW_SP1_KVDL_SINGLE_END \
43 	(MLXSW_SP1_KVDL_SINGLE_SIZE + MLXSW_SP1_KVDL_SINGLE_BASE - 1)
44 
45 #define MLXSW_SP1_KVDL_CHUNKS_BASE \
46 	(MLXSW_SP1_KVDL_SINGLE_BASE + MLXSW_SP1_KVDL_SINGLE_SIZE)
47 #define MLXSW_SP1_KVDL_CHUNKS_SIZE 49152
48 #define MLXSW_SP1_KVDL_CHUNKS_END \
49 	(MLXSW_SP1_KVDL_CHUNKS_SIZE + MLXSW_SP1_KVDL_CHUNKS_BASE - 1)
50 
51 #define MLXSW_SP1_KVDL_LARGE_CHUNKS_BASE \
52 	(MLXSW_SP1_KVDL_CHUNKS_BASE + MLXSW_SP1_KVDL_CHUNKS_SIZE)
53 #define MLXSW_SP1_KVDL_LARGE_CHUNKS_SIZE \
54 	(MLXSW_SP_KVD_LINEAR_SIZE - MLXSW_SP1_KVDL_LARGE_CHUNKS_BASE)
55 #define MLXSW_SP1_KVDL_LARGE_CHUNKS_END \
56 	(MLXSW_SP1_KVDL_LARGE_CHUNKS_SIZE + MLXSW_SP1_KVDL_LARGE_CHUNKS_BASE - 1)
57 
58 #define MLXSW_SP1_KVDL_SINGLE_ALLOC_SIZE 1
59 #define MLXSW_SP1_KVDL_CHUNKS_ALLOC_SIZE 32
60 #define MLXSW_SP1_KVDL_LARGE_CHUNKS_ALLOC_SIZE 512
61 
62 struct mlxsw_sp1_kvdl_part_info {
63 	unsigned int part_index;
64 	unsigned int start_index;
65 	unsigned int end_index;
66 	unsigned int alloc_size;
67 	enum mlxsw_sp_resource_id resource_id;
68 };
69 
70 enum mlxsw_sp1_kvdl_part_id {
71 	MLXSW_SP1_KVDL_PART_ID_SINGLE,
72 	MLXSW_SP1_KVDL_PART_ID_CHUNKS,
73 	MLXSW_SP1_KVDL_PART_ID_LARGE_CHUNKS,
74 };
75 
76 #define MLXSW_SP1_KVDL_PART_INFO(id)				\
77 [MLXSW_SP1_KVDL_PART_ID_##id] = {				\
78 	.start_index = MLXSW_SP1_KVDL_##id##_BASE,		\
79 	.end_index = MLXSW_SP1_KVDL_##id##_END,			\
80 	.alloc_size = MLXSW_SP1_KVDL_##id##_ALLOC_SIZE,		\
81 	.resource_id = MLXSW_SP_RESOURCE_KVD_LINEAR_##id,	\
82 }
83 
84 static const struct mlxsw_sp1_kvdl_part_info mlxsw_sp1_kvdl_parts_info[] = {
85 	MLXSW_SP1_KVDL_PART_INFO(SINGLE),
86 	MLXSW_SP1_KVDL_PART_INFO(CHUNKS),
87 	MLXSW_SP1_KVDL_PART_INFO(LARGE_CHUNKS),
88 };
89 
90 #define MLXSW_SP1_KVDL_PARTS_INFO_LEN ARRAY_SIZE(mlxsw_sp1_kvdl_parts_info)
91 
92 struct mlxsw_sp1_kvdl_part {
93 	struct mlxsw_sp1_kvdl_part_info info;
94 	unsigned long usage[0];	/* Entries */
95 };
96 
97 struct mlxsw_sp1_kvdl {
98 	struct mlxsw_sp1_kvdl_part *parts[MLXSW_SP1_KVDL_PARTS_INFO_LEN];
99 };
100 
101 static struct mlxsw_sp1_kvdl_part *
102 mlxsw_sp1_kvdl_alloc_size_part(struct mlxsw_sp1_kvdl *kvdl,
103 			       unsigned int alloc_size)
104 {
105 	struct mlxsw_sp1_kvdl_part *part, *min_part = NULL;
106 	int i;
107 
108 	for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++) {
109 		part = kvdl->parts[i];
110 		if (alloc_size <= part->info.alloc_size &&
111 		    (!min_part ||
112 		     part->info.alloc_size <= min_part->info.alloc_size))
113 			min_part = part;
114 	}
115 
116 	return min_part ?: ERR_PTR(-ENOBUFS);
117 }
118 
119 static struct mlxsw_sp1_kvdl_part *
120 mlxsw_sp1_kvdl_index_part(struct mlxsw_sp1_kvdl *kvdl, u32 kvdl_index)
121 {
122 	struct mlxsw_sp1_kvdl_part *part;
123 	int i;
124 
125 	for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++) {
126 		part = kvdl->parts[i];
127 		if (kvdl_index >= part->info.start_index &&
128 		    kvdl_index <= part->info.end_index)
129 			return part;
130 	}
131 
132 	return ERR_PTR(-EINVAL);
133 }
134 
135 static u32
136 mlxsw_sp1_kvdl_to_kvdl_index(const struct mlxsw_sp1_kvdl_part_info *info,
137 			     unsigned int entry_index)
138 {
139 	return info->start_index + entry_index * info->alloc_size;
140 }
141 
142 static unsigned int
143 mlxsw_sp1_kvdl_to_entry_index(const struct mlxsw_sp1_kvdl_part_info *info,
144 			      u32 kvdl_index)
145 {
146 	return (kvdl_index - info->start_index) / info->alloc_size;
147 }
148 
149 static int mlxsw_sp1_kvdl_part_alloc(struct mlxsw_sp1_kvdl_part *part,
150 				     u32 *p_kvdl_index)
151 {
152 	const struct mlxsw_sp1_kvdl_part_info *info = &part->info;
153 	unsigned int entry_index, nr_entries;
154 
155 	nr_entries = (info->end_index - info->start_index + 1) /
156 		     info->alloc_size;
157 	entry_index = find_first_zero_bit(part->usage, nr_entries);
158 	if (entry_index == nr_entries)
159 		return -ENOBUFS;
160 	__set_bit(entry_index, part->usage);
161 
162 	*p_kvdl_index = mlxsw_sp1_kvdl_to_kvdl_index(info, entry_index);
163 
164 	return 0;
165 }
166 
167 static void mlxsw_sp1_kvdl_part_free(struct mlxsw_sp1_kvdl_part *part,
168 				     u32 kvdl_index)
169 {
170 	const struct mlxsw_sp1_kvdl_part_info *info = &part->info;
171 	unsigned int entry_index;
172 
173 	entry_index = mlxsw_sp1_kvdl_to_entry_index(info, kvdl_index);
174 	__clear_bit(entry_index, part->usage);
175 }
176 
177 static int mlxsw_sp1_kvdl_alloc(struct mlxsw_sp *mlxsw_sp, void *priv,
178 				unsigned int entry_count,
179 				u32 *p_entry_index)
180 {
181 	struct mlxsw_sp1_kvdl *kvdl = priv;
182 	struct mlxsw_sp1_kvdl_part *part;
183 
184 	/* Find partition with smallest allocation size satisfying the
185 	 * requested size.
186 	 */
187 	part = mlxsw_sp1_kvdl_alloc_size_part(kvdl, entry_count);
188 	if (IS_ERR(part))
189 		return PTR_ERR(part);
190 
191 	return mlxsw_sp1_kvdl_part_alloc(part, p_entry_index);
192 }
193 
194 static void mlxsw_sp1_kvdl_free(struct mlxsw_sp *mlxsw_sp, void *priv,
195 				int entry_index)
196 {
197 	struct mlxsw_sp1_kvdl *kvdl = priv;
198 	struct mlxsw_sp1_kvdl_part *part;
199 
200 	part = mlxsw_sp1_kvdl_index_part(kvdl, entry_index);
201 	if (IS_ERR(part))
202 		return;
203 	mlxsw_sp1_kvdl_part_free(part, entry_index);
204 }
205 
206 static int mlxsw_sp1_kvdl_alloc_size_query(struct mlxsw_sp *mlxsw_sp,
207 					   void *priv, unsigned int entry_count,
208 					   unsigned int *p_alloc_size)
209 {
210 	struct mlxsw_sp1_kvdl *kvdl = priv;
211 	struct mlxsw_sp1_kvdl_part *part;
212 
213 	part = mlxsw_sp1_kvdl_alloc_size_part(kvdl, entry_count);
214 	if (IS_ERR(part))
215 		return PTR_ERR(part);
216 
217 	*p_alloc_size = part->info.alloc_size;
218 
219 	return 0;
220 }
221 
222 static void mlxsw_sp1_kvdl_part_update(struct mlxsw_sp1_kvdl_part *part,
223 				       struct mlxsw_sp1_kvdl_part *part_prev,
224 				       unsigned int size)
225 {
226 	if (!part_prev) {
227 		part->info.end_index = size - 1;
228 	} else {
229 		part->info.start_index = part_prev->info.end_index + 1;
230 		part->info.end_index = part->info.start_index + size - 1;
231 	}
232 }
233 
234 static struct mlxsw_sp1_kvdl_part *
235 mlxsw_sp1_kvdl_part_init(struct mlxsw_sp *mlxsw_sp,
236 			 const struct mlxsw_sp1_kvdl_part_info *info,
237 			 struct mlxsw_sp1_kvdl_part *part_prev)
238 {
239 	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
240 	struct mlxsw_sp1_kvdl_part *part;
241 	bool need_update = true;
242 	unsigned int nr_entries;
243 	size_t usage_size;
244 	u64 resource_size;
245 	int err;
246 
247 	err = devlink_resource_size_get(devlink, info->resource_id,
248 					&resource_size);
249 	if (err) {
250 		need_update = false;
251 		resource_size = info->end_index - info->start_index + 1;
252 	}
253 
254 	nr_entries = div_u64(resource_size, info->alloc_size);
255 	usage_size = BITS_TO_LONGS(nr_entries) * sizeof(unsigned long);
256 	part = kzalloc(sizeof(*part) + usage_size, GFP_KERNEL);
257 	if (!part)
258 		return ERR_PTR(-ENOMEM);
259 
260 	memcpy(&part->info, info, sizeof(part->info));
261 
262 	if (need_update)
263 		mlxsw_sp1_kvdl_part_update(part, part_prev, resource_size);
264 	return part;
265 }
266 
267 static void mlxsw_sp1_kvdl_part_fini(struct mlxsw_sp1_kvdl_part *part)
268 {
269 	kfree(part);
270 }
271 
272 static int mlxsw_sp1_kvdl_parts_init(struct mlxsw_sp *mlxsw_sp,
273 				     struct mlxsw_sp1_kvdl *kvdl)
274 {
275 	const struct mlxsw_sp1_kvdl_part_info *info;
276 	struct mlxsw_sp1_kvdl_part *part_prev = NULL;
277 	int err, i;
278 
279 	for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++) {
280 		info = &mlxsw_sp1_kvdl_parts_info[i];
281 		kvdl->parts[i] = mlxsw_sp1_kvdl_part_init(mlxsw_sp, info,
282 							  part_prev);
283 		if (IS_ERR(kvdl->parts[i])) {
284 			err = PTR_ERR(kvdl->parts[i]);
285 			goto err_kvdl_part_init;
286 		}
287 		part_prev = kvdl->parts[i];
288 	}
289 	return 0;
290 
291 err_kvdl_part_init:
292 	for (i--; i >= 0; i--)
293 		mlxsw_sp1_kvdl_part_fini(kvdl->parts[i]);
294 	return err;
295 }
296 
297 static void mlxsw_sp1_kvdl_parts_fini(struct mlxsw_sp1_kvdl *kvdl)
298 {
299 	int i;
300 
301 	for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++)
302 		mlxsw_sp1_kvdl_part_fini(kvdl->parts[i]);
303 }
304 
305 static u64 mlxsw_sp1_kvdl_part_occ(struct mlxsw_sp1_kvdl_part *part)
306 {
307 	const struct mlxsw_sp1_kvdl_part_info *info = &part->info;
308 	unsigned int nr_entries;
309 	int bit = -1;
310 	u64 occ = 0;
311 
312 	nr_entries = (info->end_index -
313 		      info->start_index + 1) /
314 		      info->alloc_size;
315 	while ((bit = find_next_bit(part->usage, nr_entries, bit + 1))
316 		< nr_entries)
317 		occ += info->alloc_size;
318 	return occ;
319 }
320 
321 static u64 mlxsw_sp1_kvdl_occ_get(void *priv)
322 {
323 	const struct mlxsw_sp1_kvdl *kvdl = priv;
324 	u64 occ = 0;
325 	int i;
326 
327 	for (i = 0; i < MLXSW_SP1_KVDL_PARTS_INFO_LEN; i++)
328 		occ += mlxsw_sp1_kvdl_part_occ(kvdl->parts[i]);
329 
330 	return occ;
331 }
332 
333 static u64 mlxsw_sp1_kvdl_single_occ_get(void *priv)
334 {
335 	const struct mlxsw_sp1_kvdl *kvdl = priv;
336 	struct mlxsw_sp1_kvdl_part *part;
337 
338 	part = kvdl->parts[MLXSW_SP1_KVDL_PART_ID_SINGLE];
339 	return mlxsw_sp1_kvdl_part_occ(part);
340 }
341 
342 static u64 mlxsw_sp1_kvdl_chunks_occ_get(void *priv)
343 {
344 	const struct mlxsw_sp1_kvdl *kvdl = priv;
345 	struct mlxsw_sp1_kvdl_part *part;
346 
347 	part = kvdl->parts[MLXSW_SP1_KVDL_PART_ID_CHUNKS];
348 	return mlxsw_sp1_kvdl_part_occ(part);
349 }
350 
351 static u64 mlxsw_sp1_kvdl_large_chunks_occ_get(void *priv)
352 {
353 	const struct mlxsw_sp1_kvdl *kvdl = priv;
354 	struct mlxsw_sp1_kvdl_part *part;
355 
356 	part = kvdl->parts[MLXSW_SP1_KVDL_PART_ID_LARGE_CHUNKS];
357 	return mlxsw_sp1_kvdl_part_occ(part);
358 }
359 
360 static int mlxsw_sp1_kvdl_init(struct mlxsw_sp *mlxsw_sp, void *priv)
361 {
362 	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
363 	struct mlxsw_sp1_kvdl *kvdl = priv;
364 	int err;
365 
366 	err = mlxsw_sp1_kvdl_parts_init(mlxsw_sp, kvdl);
367 	if (err)
368 		return err;
369 	devlink_resource_occ_get_register(devlink,
370 					  MLXSW_SP_RESOURCE_KVD_LINEAR,
371 					  mlxsw_sp1_kvdl_occ_get,
372 					  kvdl);
373 	devlink_resource_occ_get_register(devlink,
374 					  MLXSW_SP_RESOURCE_KVD_LINEAR_SINGLE,
375 					  mlxsw_sp1_kvdl_single_occ_get,
376 					  kvdl);
377 	devlink_resource_occ_get_register(devlink,
378 					  MLXSW_SP_RESOURCE_KVD_LINEAR_CHUNKS,
379 					  mlxsw_sp1_kvdl_chunks_occ_get,
380 					  kvdl);
381 	devlink_resource_occ_get_register(devlink,
382 					  MLXSW_SP_RESOURCE_KVD_LINEAR_LARGE_CHUNKS,
383 					  mlxsw_sp1_kvdl_large_chunks_occ_get,
384 					  kvdl);
385 	return 0;
386 }
387 
388 static void mlxsw_sp1_kvdl_fini(struct mlxsw_sp *mlxsw_sp, void *priv)
389 {
390 	struct devlink *devlink = priv_to_devlink(mlxsw_sp->core);
391 	struct mlxsw_sp1_kvdl *kvdl = priv;
392 
393 	devlink_resource_occ_get_unregister(devlink,
394 					    MLXSW_SP_RESOURCE_KVD_LINEAR_LARGE_CHUNKS);
395 	devlink_resource_occ_get_unregister(devlink,
396 					    MLXSW_SP_RESOURCE_KVD_LINEAR_CHUNKS);
397 	devlink_resource_occ_get_unregister(devlink,
398 					    MLXSW_SP_RESOURCE_KVD_LINEAR_SINGLE);
399 	devlink_resource_occ_get_unregister(devlink,
400 					    MLXSW_SP_RESOURCE_KVD_LINEAR);
401 	mlxsw_sp1_kvdl_parts_fini(kvdl);
402 }
403 
404 const struct mlxsw_sp_kvdl_ops mlxsw_sp1_kvdl_ops = {
405 	.priv_size = sizeof(struct mlxsw_sp1_kvdl),
406 	.init = mlxsw_sp1_kvdl_init,
407 	.fini = mlxsw_sp1_kvdl_fini,
408 	.alloc = mlxsw_sp1_kvdl_alloc,
409 	.free = mlxsw_sp1_kvdl_free,
410 	.alloc_size_query = mlxsw_sp1_kvdl_alloc_size_query,
411 };
412 
413 int mlxsw_sp1_kvdl_resources_register(struct mlxsw_core *mlxsw_core)
414 {
415 	struct devlink *devlink = priv_to_devlink(mlxsw_core);
416 	static struct devlink_resource_size_params size_params;
417 	u32 kvdl_max_size;
418 	int err;
419 
420 	kvdl_max_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
421 			MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) -
422 			MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE);
423 
424 	devlink_resource_size_params_init(&size_params, 0, kvdl_max_size,
425 					  MLXSW_SP1_KVDL_SINGLE_ALLOC_SIZE,
426 					  DEVLINK_RESOURCE_UNIT_ENTRY);
427 	err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR_SINGLES,
428 					MLXSW_SP1_KVDL_SINGLE_SIZE,
429 					MLXSW_SP_RESOURCE_KVD_LINEAR_SINGLE,
430 					MLXSW_SP_RESOURCE_KVD_LINEAR,
431 					&size_params);
432 	if (err)
433 		return err;
434 
435 	devlink_resource_size_params_init(&size_params, 0, kvdl_max_size,
436 					  MLXSW_SP1_KVDL_CHUNKS_ALLOC_SIZE,
437 					  DEVLINK_RESOURCE_UNIT_ENTRY);
438 	err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR_CHUNKS,
439 					MLXSW_SP1_KVDL_CHUNKS_SIZE,
440 					MLXSW_SP_RESOURCE_KVD_LINEAR_CHUNKS,
441 					MLXSW_SP_RESOURCE_KVD_LINEAR,
442 					&size_params);
443 	if (err)
444 		return err;
445 
446 	devlink_resource_size_params_init(&size_params, 0, kvdl_max_size,
447 					  MLXSW_SP1_KVDL_LARGE_CHUNKS_ALLOC_SIZE,
448 					  DEVLINK_RESOURCE_UNIT_ENTRY);
449 	err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR_LARGE_CHUNKS,
450 					MLXSW_SP1_KVDL_LARGE_CHUNKS_SIZE,
451 					MLXSW_SP_RESOURCE_KVD_LINEAR_LARGE_CHUNKS,
452 					MLXSW_SP_RESOURCE_KVD_LINEAR,
453 					&size_params);
454 	return err;
455 }
456