xref: /freebsd/sys/dev/mlx5/mlx5_core/mlx5_fs_counters.c (revision c4f32e7a354eeebd503714d0e2be8be87562df1c)
1 /*-
2  * Copyright (c) 2013-2019, Mellanox Technologies, Ltd.  All rights reserved.
3  * Copyright (c) 2022 NVIDIA corporation & affiliates.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <dev/mlx5/driver.h>
28 #include <dev/mlx5/fs.h>
29 #include <linux/rbtree.h>
30 #include <dev/mlx5/mlx5_core/mlx5_core.h>
31 #include <dev/mlx5/mlx5_core/fs_core.h>
32 #include <dev/mlx5/mlx5_core/fs_cmd.h>
33 
34 #define MLX5_FC_STATS_PERIOD msecs_to_jiffies(1000)
35 #define MLX5_FC_BULK_QUERY_ALLOC_PERIOD msecs_to_jiffies(180 * 1000)
36 /* Max number of counters to query in bulk read is 32K */
37 #define MLX5_SW_MAX_COUNTERS_BULK BIT(15)
38 #define MLX5_INIT_COUNTERS_BULK 8
39 #define MLX5_FC_POOL_MAX_THRESHOLD BIT(18)
40 #define MLX5_FC_POOL_USED_BUFF_RATIO 10
41 
42 struct mlx5_fc_cache {
43 	u64 packets;
44 	u64 bytes;
45 	u64 lastuse;
46 };
47 
48 struct mlx5_fc {
49 	struct list_head list;
50 	struct llist_node addlist;
51 	struct llist_node dellist;
52 
53 	/* last{packets,bytes} members are used when calculating the delta since
54 	 * last reading
55 	 */
56 	u64 lastpackets;
57 	u64 lastbytes;
58 
59 	struct mlx5_fc_bulk *bulk;
60 	u32 id;
61 	bool aging;
62 
63 	struct mlx5_fc_cache cache ____cacheline_aligned_in_smp;
64 };
65 
66 static void mlx5_fc_pool_init(struct mlx5_fc_pool *fc_pool, struct mlx5_core_dev *dev);
67 static void mlx5_fc_pool_cleanup(struct mlx5_fc_pool *fc_pool);
68 static struct mlx5_fc *mlx5_fc_pool_acquire_counter(struct mlx5_fc_pool *fc_pool);
69 static void mlx5_fc_pool_release_counter(struct mlx5_fc_pool *fc_pool, struct mlx5_fc *fc);
70 
71 /* locking scheme:
72  *
73  * It is the responsibility of the user to prevent concurrent calls or bad
74  * ordering to mlx5_fc_create(), mlx5_fc_destroy() and accessing a reference
75  * to struct mlx5_fc.
76  * e.g en_tc.c is protected by RTNL lock of its caller, and will never call a
77  * dump (access to struct mlx5_fc) after a counter is destroyed.
78  *
79  * access to counter list:
80  * - create (user context)
81  *   - mlx5_fc_create() only adds to an addlist to be used by
82  *     mlx5_fc_stats_work(). addlist is a lockless single linked list
83  *     that doesn't require any additional synchronization when adding single
84  *     node.
85  *   - spawn thread to do the actual destroy
86  *
87  * - destroy (user context)
88  *   - add a counter to lockless dellist
89  *   - spawn thread to do the actual del
90  *
91  * - dump (user context)
92  *   user should not call dump after destroy
93  *
94  * - query (single thread workqueue context)
95  *   destroy/dump - no conflict (see destroy)
96  *   query/dump - packets and bytes might be inconsistent (since update is not
97  *                atomic)
98  *   query/create - no conflict (see create)
99  *   since every create/destroy spawn the work, only after necessary time has
100  *   elapsed, the thread will actually query the hardware.
101  */
102 
mlx5_fc_counters_lookup_next(struct mlx5_core_dev * dev,u32 id)103 static struct list_head *mlx5_fc_counters_lookup_next(struct mlx5_core_dev *dev,
104 						      u32 id)
105 {
106 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
107 	struct mlx5_fc *counter;
108 	int next_id = id + 1;
109 
110 	rcu_read_lock();
111 	/* skip counters that are in idr, but not yet in counters list */
112 	while ((counter = idr_get_next(&fc_stats->counters_idr, &next_id)) != NULL &&
113 		list_empty(&counter->list))
114 		next_id++;
115 	rcu_read_unlock();
116 
117 	return counter ? &counter->list : &fc_stats->counters;
118 }
119 
mlx5_fc_stats_insert(struct mlx5_core_dev * dev,struct mlx5_fc * counter)120 static void mlx5_fc_stats_insert(struct mlx5_core_dev *dev,
121 				 struct mlx5_fc *counter)
122 {
123 	struct list_head *next = mlx5_fc_counters_lookup_next(dev, counter->id);
124 
125 	list_add_tail(&counter->list, next);
126 }
127 
mlx5_fc_stats_remove(struct mlx5_core_dev * dev,struct mlx5_fc * counter)128 static void mlx5_fc_stats_remove(struct mlx5_core_dev *dev,
129 				 struct mlx5_fc *counter)
130 {
131 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
132 
133 	list_del(&counter->list);
134 
135 	spin_lock(&fc_stats->counters_idr_lock);
136 	WARN_ON(!idr_remove(&fc_stats->counters_idr, counter->id));
137 	spin_unlock(&fc_stats->counters_idr_lock);
138 }
139 
get_init_bulk_query_len(struct mlx5_core_dev * dev)140 static int get_init_bulk_query_len(struct mlx5_core_dev *dev)
141 {
142 	return min_t(int, MLX5_INIT_COUNTERS_BULK,
143 		     (1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
144 }
145 
get_max_bulk_query_len(struct mlx5_core_dev * dev)146 static int get_max_bulk_query_len(struct mlx5_core_dev *dev)
147 {
148 	return min_t(int, MLX5_SW_MAX_COUNTERS_BULK,
149 		     (1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
150 }
151 
update_counter_cache(int index,u32 * bulk_raw_data,struct mlx5_fc_cache * cache)152 static void update_counter_cache(int index, u32 *bulk_raw_data,
153 				 struct mlx5_fc_cache *cache)
154 {
155 	void *stats = MLX5_ADDR_OF(query_flow_counter_out, bulk_raw_data,
156 			     flow_statistics[index]);
157 	u64 packets = MLX5_GET64(traffic_counter, stats, packets);
158 	u64 bytes = MLX5_GET64(traffic_counter, stats, octets);
159 
160 	if (cache->packets == packets)
161 		return;
162 
163 	cache->packets = packets;
164 	cache->bytes = bytes;
165 	cache->lastuse = jiffies;
166 }
167 
mlx5_fc_stats_query_counter_range(struct mlx5_core_dev * dev,struct mlx5_fc * first,u32 last_id)168 static void mlx5_fc_stats_query_counter_range(struct mlx5_core_dev *dev,
169 					      struct mlx5_fc *first,
170 					      u32 last_id)
171 {
172 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
173 	bool query_more_counters = (first->id <= last_id);
174 	int cur_bulk_len = fc_stats->bulk_query_len;
175 	u32 *data = fc_stats->bulk_query_out;
176 	struct mlx5_fc *counter = first;
177 	u32 bulk_base_id;
178 	int bulk_len;
179 	int err;
180 
181 	while (query_more_counters) {
182 		/* first id must be aligned to 4 when using bulk query */
183 		bulk_base_id = counter->id & ~0x3;
184 
185 		/* number of counters to query inc. the last counter */
186 		bulk_len = min_t(int, cur_bulk_len,
187 				 ALIGN(last_id - bulk_base_id + 1, 4));
188 
189 		err = mlx5_cmd_fc_bulk_query(dev, bulk_base_id, bulk_len,
190 					     data);
191 		if (err) {
192 			mlx5_core_err(dev, "Error doing bulk query: %d\n", err);
193 			return;
194 		}
195 		query_more_counters = false;
196 
197 		list_for_each_entry_from(counter, &fc_stats->counters, list) {
198 			int counter_index = counter->id - bulk_base_id;
199 			struct mlx5_fc_cache *cache = &counter->cache;
200 
201 			if (counter->id >= bulk_base_id + bulk_len) {
202 				query_more_counters = true;
203 				break;
204 			}
205 
206 			update_counter_cache(counter_index, data, cache);
207 		}
208 	}
209 }
210 
mlx5_fc_free(struct mlx5_core_dev * dev,struct mlx5_fc * counter)211 static void mlx5_fc_free(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
212 {
213 	mlx5_cmd_fc_free(dev, counter->id);
214 	kfree(counter);
215 }
216 
mlx5_fc_release(struct mlx5_core_dev * dev,struct mlx5_fc * counter)217 static void mlx5_fc_release(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
218 {
219 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
220 
221 	if (counter->bulk)
222 		mlx5_fc_pool_release_counter(&fc_stats->fc_pool, counter);
223 	else
224 		mlx5_fc_free(dev, counter);
225 }
226 
mlx5_fc_stats_bulk_query_size_increase(struct mlx5_core_dev * dev)227 static void mlx5_fc_stats_bulk_query_size_increase(struct mlx5_core_dev *dev)
228 {
229 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
230 	int max_bulk_len = get_max_bulk_query_len(dev);
231 	unsigned long now = jiffies;
232 	u32 *bulk_query_out_tmp;
233 	int max_out_len;
234 
235 	if (fc_stats->bulk_query_alloc_failed &&
236 	    time_before(now, fc_stats->next_bulk_query_alloc))
237 		return;
238 
239 	max_out_len = mlx5_cmd_fc_get_bulk_query_out_len(max_bulk_len);
240 	bulk_query_out_tmp = kzalloc(max_out_len, GFP_KERNEL);
241 	if (!bulk_query_out_tmp) {
242 		mlx5_core_warn(dev,
243 			       "Can't increase flow counters bulk query buffer size, insufficient memory, bulk_size(%d)\n",
244 			       max_bulk_len);
245 		fc_stats->bulk_query_alloc_failed = true;
246 		fc_stats->next_bulk_query_alloc =
247 			now + MLX5_FC_BULK_QUERY_ALLOC_PERIOD;
248 		return;
249 	}
250 
251 	kfree(fc_stats->bulk_query_out);
252 	fc_stats->bulk_query_out = bulk_query_out_tmp;
253 	fc_stats->bulk_query_len = max_bulk_len;
254 	if (fc_stats->bulk_query_alloc_failed) {
255 		mlx5_core_info(dev,
256 			       "Flow counters bulk query buffer size increased, bulk_size(%d)\n",
257 			       max_bulk_len);
258 		fc_stats->bulk_query_alloc_failed = false;
259 	}
260 }
261 
mlx5_fc_stats_work(struct work_struct * work)262 static void mlx5_fc_stats_work(struct work_struct *work)
263 {
264 	struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
265 						 priv.fc_stats.work.work);
266 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
267 	/* Take dellist first to ensure that counters cannot be deleted before
268 	 * they are inserted.
269 	 */
270 	struct llist_node *dellist = llist_del_all(&fc_stats->dellist);
271 	struct llist_node *addlist = llist_del_all(&fc_stats->addlist);
272 	struct mlx5_fc *counter = NULL, *last = NULL, *tmp;
273 	unsigned long now = jiffies;
274 
275 	if (addlist || !list_empty(&fc_stats->counters))
276 		queue_delayed_work(fc_stats->wq, &fc_stats->work,
277 				   fc_stats->sampling_interval);
278 
279 	llist_for_each_entry(counter, addlist, addlist) {
280 		mlx5_fc_stats_insert(dev, counter);
281 		fc_stats->num_counters++;
282 	}
283 
284 	llist_for_each_entry_safe(counter, tmp, dellist, dellist) {
285 		mlx5_fc_stats_remove(dev, counter);
286 
287 		mlx5_fc_release(dev, counter);
288 		fc_stats->num_counters--;
289 	}
290 
291 	if (fc_stats->bulk_query_len < get_max_bulk_query_len(dev) &&
292 	    fc_stats->num_counters > get_init_bulk_query_len(dev))
293 		mlx5_fc_stats_bulk_query_size_increase(dev);
294 
295 	if (time_before(now, fc_stats->next_query) ||
296 	    list_empty(&fc_stats->counters))
297 		return;
298 	last = list_last_entry(&fc_stats->counters, struct mlx5_fc, list);
299 
300 	counter = list_first_entry(&fc_stats->counters, struct mlx5_fc,
301 				   list);
302 	if (counter)
303 		mlx5_fc_stats_query_counter_range(dev, counter, last->id);
304 
305 	fc_stats->next_query = now + fc_stats->sampling_interval;
306 }
307 
mlx5_fc_single_alloc(struct mlx5_core_dev * dev)308 static struct mlx5_fc *mlx5_fc_single_alloc(struct mlx5_core_dev *dev)
309 {
310 	struct mlx5_fc *counter;
311 	int err;
312 
313 	counter = kzalloc(sizeof(*counter), GFP_KERNEL);
314 	if (!counter)
315 		return ERR_PTR(-ENOMEM);
316 
317 	err = mlx5_cmd_fc_alloc(dev, &counter->id);
318 	if (err) {
319 		kfree(counter);
320 		return ERR_PTR(err);
321 	}
322 
323 	return counter;
324 }
325 
mlx5_fc_acquire(struct mlx5_core_dev * dev,bool aging)326 static struct mlx5_fc *mlx5_fc_acquire(struct mlx5_core_dev *dev, bool aging)
327 {
328 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
329 	struct mlx5_fc *counter;
330 
331 	if (aging && MLX5_CAP_GEN(dev, flow_counter_bulk_alloc) != 0) {
332 		counter = mlx5_fc_pool_acquire_counter(&fc_stats->fc_pool);
333 		if (!IS_ERR(counter))
334 			return counter;
335 	}
336 
337 	return mlx5_fc_single_alloc(dev);
338 }
339 
mlx5_fc_create_ex(struct mlx5_core_dev * dev,bool aging)340 struct mlx5_fc *mlx5_fc_create_ex(struct mlx5_core_dev *dev, bool aging)
341 {
342 	struct mlx5_fc *counter = mlx5_fc_acquire(dev, aging);
343 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
344 	int err = 0;
345 
346 	if (IS_ERR(counter))
347 		return counter;
348 
349 	INIT_LIST_HEAD(&counter->list);
350 	counter->aging = aging;
351 
352 	if (aging) {
353 		u32 id = counter->id;
354 
355 		counter->cache.lastuse = jiffies;
356 		counter->lastbytes = counter->cache.bytes;
357 		counter->lastpackets = counter->cache.packets;
358 
359 		idr_preload(GFP_KERNEL);
360 		spin_lock(&fc_stats->counters_idr_lock);
361 
362 		err = idr_alloc(&fc_stats->counters_idr, counter, id, id + 1,
363 				GFP_NOWAIT);
364 
365 		spin_unlock(&fc_stats->counters_idr_lock);
366 		idr_preload_end();
367 		if (err < 0 || err != id)
368 			goto err_out_alloc;
369 
370 		llist_add(&counter->addlist, &fc_stats->addlist);
371 	}
372 
373 	return counter;
374 
375 err_out_alloc:
376 	mlx5_fc_release(dev, counter);
377 	return ERR_PTR(err);
378 }
379 
mlx5_fc_create(struct mlx5_core_dev * dev,bool aging)380 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
381 {
382 	struct mlx5_fc *counter = mlx5_fc_create_ex(dev, aging);
383 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
384 
385 	if (aging)
386 		mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
387 	return counter;
388 }
389 EXPORT_SYMBOL(mlx5_fc_create);
390 
mlx5_fc_id(struct mlx5_fc * counter)391 u32 mlx5_fc_id(struct mlx5_fc *counter)
392 {
393 	return counter->id;
394 }
395 EXPORT_SYMBOL(mlx5_fc_id);
396 
mlx5_fc_destroy(struct mlx5_core_dev * dev,struct mlx5_fc * counter)397 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
398 {
399 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
400 
401 	if (!counter)
402 		return;
403 
404 	if (counter->aging) {
405 		llist_add(&counter->dellist, &fc_stats->dellist);
406 		mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
407 		return;
408 	}
409 
410 	mlx5_fc_release(dev, counter);
411 }
412 EXPORT_SYMBOL(mlx5_fc_destroy);
413 
mlx5_init_fc_stats(struct mlx5_core_dev * dev)414 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
415 {
416 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
417 	int init_bulk_len;
418 	int init_out_len;
419 
420 	spin_lock_init(&fc_stats->counters_idr_lock);
421 	idr_init(&fc_stats->counters_idr);
422 	INIT_LIST_HEAD(&fc_stats->counters);
423 	init_llist_head(&fc_stats->addlist);
424 	init_llist_head(&fc_stats->dellist);
425 
426 	init_bulk_len = get_init_bulk_query_len(dev);
427 	init_out_len = mlx5_cmd_fc_get_bulk_query_out_len(init_bulk_len);
428 	fc_stats->bulk_query_out = kzalloc(init_out_len, GFP_KERNEL);
429 	if (!fc_stats->bulk_query_out)
430 		return -ENOMEM;
431 	fc_stats->bulk_query_len = init_bulk_len;
432 
433 	fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
434 	if (!fc_stats->wq)
435 		goto err_wq_create;
436 
437 	fc_stats->sampling_interval = MLX5_FC_STATS_PERIOD;
438 	INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
439 
440 	mlx5_fc_pool_init(&fc_stats->fc_pool, dev);
441 	return 0;
442 
443 err_wq_create:
444 	kfree(fc_stats->bulk_query_out);
445 	return -ENOMEM;
446 }
447 
mlx5_cleanup_fc_stats(struct mlx5_core_dev * dev)448 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
449 {
450 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
451 	struct llist_node *tmplist;
452 	struct mlx5_fc *counter;
453 	struct mlx5_fc *tmp;
454 
455 	if (!dev->priv.fc_stats.wq)
456 		return;
457 
458 	cancel_delayed_work_sync(&dev->priv.fc_stats.work);
459 	destroy_workqueue(dev->priv.fc_stats.wq);
460 	dev->priv.fc_stats.wq = NULL;
461 
462 	tmplist = llist_del_all(&fc_stats->addlist);
463 	llist_for_each_entry_safe(counter, tmp, tmplist, addlist)
464 		mlx5_fc_release(dev, counter);
465 
466 	list_for_each_entry_safe(counter, tmp, &fc_stats->counters, list)
467 		mlx5_fc_release(dev, counter);
468 
469 	mlx5_fc_pool_cleanup(&fc_stats->fc_pool);
470 	idr_destroy(&fc_stats->counters_idr);
471 	kfree(fc_stats->bulk_query_out);
472 }
473 
mlx5_fc_query(struct mlx5_core_dev * dev,struct mlx5_fc * counter,u64 * packets,u64 * bytes)474 int mlx5_fc_query(struct mlx5_core_dev *dev, struct mlx5_fc *counter,
475 		  u64 *packets, u64 *bytes)
476 {
477 	return mlx5_cmd_fc_query(dev, counter->id, packets, bytes);
478 }
479 EXPORT_SYMBOL(mlx5_fc_query);
480 
mlx5_fc_query_lastuse(struct mlx5_fc * counter)481 u64 mlx5_fc_query_lastuse(struct mlx5_fc *counter)
482 {
483 	return counter->cache.lastuse;
484 }
485 
mlx5_fc_query_cached(struct mlx5_fc * counter,u64 * bytes,u64 * packets,u64 * lastuse)486 void mlx5_fc_query_cached(struct mlx5_fc *counter,
487 			  u64 *bytes, u64 *packets, u64 *lastuse)
488 {
489 	struct mlx5_fc_cache c;
490 
491 	c = counter->cache;
492 
493 	*bytes = c.bytes - counter->lastbytes;
494 	*packets = c.packets - counter->lastpackets;
495 	*lastuse = c.lastuse;
496 
497 	counter->lastbytes = c.bytes;
498 	counter->lastpackets = c.packets;
499 }
500 
mlx5_fc_queue_stats_work(struct mlx5_core_dev * dev,struct delayed_work * dwork,unsigned long delay)501 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,
502 			      struct delayed_work *dwork,
503 			      unsigned long delay)
504 {
505 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
506 
507 	queue_delayed_work(fc_stats->wq, dwork, delay);
508 }
509 
mlx5_fc_update_sampling_interval(struct mlx5_core_dev * dev,unsigned long interval)510 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
511 				      unsigned long interval)
512 {
513 	struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
514 
515 	fc_stats->sampling_interval = min_t(unsigned long, interval,
516 					    fc_stats->sampling_interval);
517 }
518 
519 /* Flow counter bluks */
520 
521 struct mlx5_fc_bulk {
522 	struct list_head pool_list;
523 	u32 base_id;
524 	int bulk_len;
525 	unsigned long *bitmask;
526 	struct mlx5_fc fcs[];
527 };
528 
mlx5_fc_init(struct mlx5_fc * counter,struct mlx5_fc_bulk * bulk,u32 id)529 static void mlx5_fc_init(struct mlx5_fc *counter, struct mlx5_fc_bulk *bulk,
530 			 u32 id)
531 {
532 	counter->bulk = bulk;
533 	counter->id = id;
534 }
535 
mlx5_fc_bulk_get_free_fcs_amount(struct mlx5_fc_bulk * bulk)536 static int mlx5_fc_bulk_get_free_fcs_amount(struct mlx5_fc_bulk *bulk)
537 {
538 	return bitmap_weight(bulk->bitmask, bulk->bulk_len);
539 }
540 
mlx5_fc_bulk_create(struct mlx5_core_dev * dev)541 static struct mlx5_fc_bulk *mlx5_fc_bulk_create(struct mlx5_core_dev *dev)
542 {
543 	enum mlx5_fc_bulk_alloc_bitmask alloc_bitmask;
544 	struct mlx5_fc_bulk *bulk;
545 	int err = -ENOMEM;
546 	int bulk_len;
547 	u32 base_id;
548 	int i;
549 
550 	alloc_bitmask = MLX5_CAP_GEN(dev, flow_counter_bulk_alloc);
551 	bulk_len = alloc_bitmask > 0 ? MLX5_FC_BULK_NUM_FCS(alloc_bitmask) : 1;
552 
553 	bulk = kvzalloc(struct_size(bulk, fcs, bulk_len), GFP_KERNEL);
554 	if (!bulk)
555 		goto err_alloc_bulk;
556 
557 	bulk->bitmask = kvcalloc(BITS_TO_LONGS(bulk_len), sizeof(unsigned long),
558 				 GFP_KERNEL);
559 	if (!bulk->bitmask)
560 		goto err_alloc_bitmask;
561 
562 	err = mlx5_cmd_fc_bulk_alloc(dev, alloc_bitmask, &base_id);
563 	if (err)
564 		goto err_mlx5_cmd_bulk_alloc;
565 
566 	bulk->base_id = base_id;
567 	bulk->bulk_len = bulk_len;
568 	for (i = 0; i < bulk_len; i++) {
569 		mlx5_fc_init(&bulk->fcs[i], bulk, base_id + i);
570 		set_bit(i, bulk->bitmask);
571 	}
572 
573 	return bulk;
574 
575 err_mlx5_cmd_bulk_alloc:
576 	kvfree(bulk->bitmask);
577 err_alloc_bitmask:
578 	kvfree(bulk);
579 err_alloc_bulk:
580 	return ERR_PTR(err);
581 }
582 
583 static int
mlx5_fc_bulk_destroy(struct mlx5_core_dev * dev,struct mlx5_fc_bulk * bulk)584 mlx5_fc_bulk_destroy(struct mlx5_core_dev *dev, struct mlx5_fc_bulk *bulk)
585 {
586 	if (mlx5_fc_bulk_get_free_fcs_amount(bulk) < bulk->bulk_len) {
587 		mlx5_core_err(dev, "Freeing bulk before all counters were released\n");
588 		return -EBUSY;
589 	}
590 
591 	mlx5_cmd_fc_free(dev, bulk->base_id);
592 	kvfree(bulk->bitmask);
593 	kvfree(bulk);
594 
595 	return 0;
596 }
597 
mlx5_fc_bulk_acquire_fc(struct mlx5_fc_bulk * bulk)598 static struct mlx5_fc *mlx5_fc_bulk_acquire_fc(struct mlx5_fc_bulk *bulk)
599 {
600 	int free_fc_index = find_first_bit(bulk->bitmask, bulk->bulk_len);
601 
602 	if (free_fc_index >= bulk->bulk_len)
603 		return ERR_PTR(-ENOSPC);
604 
605 	clear_bit(free_fc_index, bulk->bitmask);
606 	return &bulk->fcs[free_fc_index];
607 }
608 
mlx5_fc_bulk_release_fc(struct mlx5_fc_bulk * bulk,struct mlx5_fc * fc)609 static int mlx5_fc_bulk_release_fc(struct mlx5_fc_bulk *bulk, struct mlx5_fc *fc)
610 {
611 	int fc_index = fc->id - bulk->base_id;
612 
613 	if (test_bit(fc_index, bulk->bitmask))
614 		return -EINVAL;
615 
616 	set_bit(fc_index, bulk->bitmask);
617 	return 0;
618 }
619 
620 /* Flow counters pool API */
621 
mlx5_fc_pool_init(struct mlx5_fc_pool * fc_pool,struct mlx5_core_dev * dev)622 static void mlx5_fc_pool_init(struct mlx5_fc_pool *fc_pool, struct mlx5_core_dev *dev)
623 {
624 	fc_pool->dev = dev;
625 	mutex_init(&fc_pool->pool_lock);
626 	INIT_LIST_HEAD(&fc_pool->fully_used);
627 	INIT_LIST_HEAD(&fc_pool->partially_used);
628 	INIT_LIST_HEAD(&fc_pool->unused);
629 	fc_pool->available_fcs = 0;
630 	fc_pool->used_fcs = 0;
631 	fc_pool->threshold = 0;
632 }
633 
mlx5_fc_pool_cleanup(struct mlx5_fc_pool * fc_pool)634 static void mlx5_fc_pool_cleanup(struct mlx5_fc_pool *fc_pool)
635 {
636 	struct mlx5_core_dev *dev = fc_pool->dev;
637 	struct mlx5_fc_bulk *bulk;
638 	struct mlx5_fc_bulk *tmp;
639 
640 	list_for_each_entry_safe(bulk, tmp, &fc_pool->fully_used, pool_list)
641 		mlx5_fc_bulk_destroy(dev, bulk);
642 	list_for_each_entry_safe(bulk, tmp, &fc_pool->partially_used, pool_list)
643 		mlx5_fc_bulk_destroy(dev, bulk);
644 	list_for_each_entry_safe(bulk, tmp, &fc_pool->unused, pool_list)
645 		mlx5_fc_bulk_destroy(dev, bulk);
646 }
647 
mlx5_fc_pool_update_threshold(struct mlx5_fc_pool * fc_pool)648 static void mlx5_fc_pool_update_threshold(struct mlx5_fc_pool *fc_pool)
649 {
650 	fc_pool->threshold = min_t(int, MLX5_FC_POOL_MAX_THRESHOLD,
651 				   fc_pool->used_fcs / MLX5_FC_POOL_USED_BUFF_RATIO);
652 }
653 
654 static struct mlx5_fc_bulk *
mlx5_fc_pool_alloc_new_bulk(struct mlx5_fc_pool * fc_pool)655 mlx5_fc_pool_alloc_new_bulk(struct mlx5_fc_pool *fc_pool)
656 {
657 	struct mlx5_core_dev *dev = fc_pool->dev;
658 	struct mlx5_fc_bulk *new_bulk;
659 
660 	new_bulk = mlx5_fc_bulk_create(dev);
661 	if (!IS_ERR(new_bulk))
662 		fc_pool->available_fcs += new_bulk->bulk_len;
663 	mlx5_fc_pool_update_threshold(fc_pool);
664 	return new_bulk;
665 }
666 
667 static void
mlx5_fc_pool_free_bulk(struct mlx5_fc_pool * fc_pool,struct mlx5_fc_bulk * bulk)668 mlx5_fc_pool_free_bulk(struct mlx5_fc_pool *fc_pool, struct mlx5_fc_bulk *bulk)
669 {
670 	struct mlx5_core_dev *dev = fc_pool->dev;
671 
672 	fc_pool->available_fcs -= bulk->bulk_len;
673 	mlx5_fc_bulk_destroy(dev, bulk);
674 	mlx5_fc_pool_update_threshold(fc_pool);
675 }
676 
677 static struct mlx5_fc *
mlx5_fc_pool_acquire_from_list(struct list_head * src_list,struct list_head * next_list,bool move_non_full_bulk)678 mlx5_fc_pool_acquire_from_list(struct list_head *src_list,
679 			       struct list_head *next_list,
680 			       bool move_non_full_bulk)
681 {
682 	struct mlx5_fc_bulk *bulk;
683 	struct mlx5_fc *fc;
684 
685 	if (list_empty(src_list))
686 		return ERR_PTR(-ENODATA);
687 
688 	bulk = list_first_entry(src_list, struct mlx5_fc_bulk, pool_list);
689 	fc = mlx5_fc_bulk_acquire_fc(bulk);
690 	if (move_non_full_bulk || mlx5_fc_bulk_get_free_fcs_amount(bulk) == 0)
691 		list_move(&bulk->pool_list, next_list);
692 	return fc;
693 }
694 
695 static struct mlx5_fc *
mlx5_fc_pool_acquire_counter(struct mlx5_fc_pool * fc_pool)696 mlx5_fc_pool_acquire_counter(struct mlx5_fc_pool *fc_pool)
697 {
698 	struct mlx5_fc_bulk *new_bulk;
699 	struct mlx5_fc *fc;
700 
701 	mutex_lock(&fc_pool->pool_lock);
702 
703 	fc = mlx5_fc_pool_acquire_from_list(&fc_pool->partially_used,
704 					    &fc_pool->fully_used, false);
705 	if (IS_ERR(fc))
706 		fc = mlx5_fc_pool_acquire_from_list(&fc_pool->unused,
707 						    &fc_pool->partially_used,
708 						    true);
709 	if (IS_ERR(fc)) {
710 		new_bulk = mlx5_fc_pool_alloc_new_bulk(fc_pool);
711 		if (IS_ERR(new_bulk)) {
712 			fc = ERR_CAST(new_bulk);
713 			goto out;
714 		}
715 		fc = mlx5_fc_bulk_acquire_fc(new_bulk);
716 		list_add(&new_bulk->pool_list, &fc_pool->partially_used);
717 	}
718 	fc_pool->available_fcs--;
719 	fc_pool->used_fcs++;
720 
721 out:
722 	mutex_unlock(&fc_pool->pool_lock);
723 	return fc;
724 }
725 
726 static void
mlx5_fc_pool_release_counter(struct mlx5_fc_pool * fc_pool,struct mlx5_fc * fc)727 mlx5_fc_pool_release_counter(struct mlx5_fc_pool *fc_pool, struct mlx5_fc *fc)
728 {
729 	struct mlx5_core_dev *dev = fc_pool->dev;
730 	struct mlx5_fc_bulk *bulk = fc->bulk;
731 	int bulk_free_fcs_amount;
732 
733 	mutex_lock(&fc_pool->pool_lock);
734 
735 	if (mlx5_fc_bulk_release_fc(bulk, fc)) {
736 		mlx5_core_warn(dev, "Attempted to release a counter which is not acquired\n");
737 		goto unlock;
738 	}
739 
740 	fc_pool->available_fcs++;
741 	fc_pool->used_fcs--;
742 
743 	bulk_free_fcs_amount = mlx5_fc_bulk_get_free_fcs_amount(bulk);
744 	if (bulk_free_fcs_amount == 1)
745 		list_move_tail(&bulk->pool_list, &fc_pool->partially_used);
746 	if (bulk_free_fcs_amount == bulk->bulk_len) {
747 		list_del(&bulk->pool_list);
748 		if (fc_pool->available_fcs > fc_pool->threshold)
749 			mlx5_fc_pool_free_bulk(fc_pool, bulk);
750 		else
751 			list_add(&bulk->pool_list, &fc_pool->unused);
752 	}
753 
754 unlock:
755 	mutex_unlock(&fc_pool->pool_lock);
756 }
757