2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/mlx5/driver.h>
34 #include <linux/mlx5/fs.h>
35 #include <linux/rbtree.h>
36 #include "mlx5_core.h"
40 #define MLX5_FC_STATS_PERIOD msecs_to_jiffies(1000)
41 /* Max number of counters to query in bulk read is 32K */
42 #define MLX5_SW_MAX_COUNTERS_BULK BIT(15)
44 struct mlx5_fc_cache {
51 struct list_head list;
52 struct llist_node addlist;
53 struct llist_node dellist;
55 /* last{packets,bytes} members are used when calculating the delta since
64 struct mlx5_fc_cache cache ____cacheline_aligned_in_smp;
69 * It is the responsibility of the user to prevent concurrent calls or bad
70 * ordering to mlx5_fc_create(), mlx5_fc_destroy() and accessing a reference
72 * e.g en_tc.c is protected by RTNL lock of its caller, and will never call a
73 * dump (access to struct mlx5_fc) after a counter is destroyed.
75 * access to counter list:
76 * - create (user context)
77 * - mlx5_fc_create() only adds to an addlist to be used by
78 * mlx5_fc_stats_work(). addlist is a lockless single linked list
79 * that doesn't require any additional synchronization when adding single
81 * - spawn thread to do the actual destroy
83 * - destroy (user context)
84 * - add a counter to lockless dellist
85 * - spawn thread to do the actual del
87 * - dump (user context)
88 * user should not call dump after destroy
90 * - query (single thread workqueue context)
91 * destroy/dump - no conflict (see destroy)
92 * query/dump - packets and bytes might be inconsistent (since update is not
94 * query/create - no conflict (see create)
95 * since every create/destroy spawn the work, only after necessary time has
96 * elapsed, the thread will actually query the hardware.
99 static struct list_head *mlx5_fc_counters_lookup_next(struct mlx5_core_dev *dev,
102 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
103 unsigned long next_id = (unsigned long)id + 1;
104 struct mlx5_fc *counter;
108 /* skip counters that are in idr, but not yet in counters list */
109 idr_for_each_entry_continue_ul(&fc_stats->counters_idr,
110 counter, tmp, next_id) {
111 if (!list_empty(&counter->list))
116 return counter ? &counter->list : &fc_stats->counters;
119 static void mlx5_fc_stats_insert(struct mlx5_core_dev *dev,
120 struct mlx5_fc *counter)
122 struct list_head *next = mlx5_fc_counters_lookup_next(dev, counter->id);
124 list_add_tail(&counter->list, next);
127 static void mlx5_fc_stats_remove(struct mlx5_core_dev *dev,
128 struct mlx5_fc *counter)
130 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
132 list_del(&counter->list);
134 spin_lock(&fc_stats->counters_idr_lock);
135 WARN_ON(!idr_remove(&fc_stats->counters_idr, counter->id));
136 spin_unlock(&fc_stats->counters_idr_lock);
139 static int get_max_bulk_query_len(struct mlx5_core_dev *dev)
141 return min_t(int, MLX5_SW_MAX_COUNTERS_BULK,
142 (1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
145 static void update_counter_cache(int index, u32 *bulk_raw_data,
146 struct mlx5_fc_cache *cache)
148 void *stats = MLX5_ADDR_OF(query_flow_counter_out, bulk_raw_data,
149 flow_statistics[index]);
150 u64 packets = MLX5_GET64(traffic_counter, stats, packets);
151 u64 bytes = MLX5_GET64(traffic_counter, stats, octets);
153 if (cache->packets == packets)
156 cache->packets = packets;
157 cache->bytes = bytes;
158 cache->lastuse = jiffies;
161 static void mlx5_fc_stats_query_counter_range(struct mlx5_core_dev *dev,
162 struct mlx5_fc *first,
165 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
166 bool query_more_counters = (first->id <= last_id);
167 int max_bulk_len = get_max_bulk_query_len(dev);
168 u32 *data = fc_stats->bulk_query_out;
169 struct mlx5_fc *counter = first;
174 while (query_more_counters) {
175 /* first id must be aligned to 4 when using bulk query */
176 bulk_base_id = counter->id & ~0x3;
178 /* number of counters to query inc. the last counter */
179 bulk_len = min_t(int, max_bulk_len,
180 ALIGN(last_id - bulk_base_id + 1, 4));
182 err = mlx5_cmd_fc_bulk_query(dev, bulk_base_id, bulk_len,
185 mlx5_core_err(dev, "Error doing bulk query: %d\n", err);
188 query_more_counters = false;
190 list_for_each_entry_from(counter, &fc_stats->counters, list) {
191 int counter_index = counter->id - bulk_base_id;
192 struct mlx5_fc_cache *cache = &counter->cache;
194 if (counter->id >= bulk_base_id + bulk_len) {
195 query_more_counters = true;
199 update_counter_cache(counter_index, data, cache);
204 static void mlx5_free_fc(struct mlx5_core_dev *dev,
205 struct mlx5_fc *counter)
207 mlx5_cmd_fc_free(dev, counter->id);
211 static void mlx5_fc_stats_work(struct work_struct *work)
213 struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
214 priv.fc_stats.work.work);
215 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
216 /* Take dellist first to ensure that counters cannot be deleted before
219 struct llist_node *dellist = llist_del_all(&fc_stats->dellist);
220 struct llist_node *addlist = llist_del_all(&fc_stats->addlist);
221 struct mlx5_fc *counter = NULL, *last = NULL, *tmp;
222 unsigned long now = jiffies;
224 if (addlist || !list_empty(&fc_stats->counters))
225 queue_delayed_work(fc_stats->wq, &fc_stats->work,
226 fc_stats->sampling_interval);
228 llist_for_each_entry(counter, addlist, addlist)
229 mlx5_fc_stats_insert(dev, counter);
231 llist_for_each_entry_safe(counter, tmp, dellist, dellist) {
232 mlx5_fc_stats_remove(dev, counter);
234 mlx5_free_fc(dev, counter);
237 if (time_before(now, fc_stats->next_query) ||
238 list_empty(&fc_stats->counters))
240 last = list_last_entry(&fc_stats->counters, struct mlx5_fc, list);
242 counter = list_first_entry(&fc_stats->counters, struct mlx5_fc,
245 mlx5_fc_stats_query_counter_range(dev, counter, last->id);
247 fc_stats->next_query = now + fc_stats->sampling_interval;
250 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
252 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
253 struct mlx5_fc *counter;
256 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
258 return ERR_PTR(-ENOMEM);
259 INIT_LIST_HEAD(&counter->list);
261 err = mlx5_cmd_fc_alloc(dev, &counter->id);
266 u32 id = counter->id;
268 counter->cache.lastuse = jiffies;
269 counter->aging = true;
271 idr_preload(GFP_KERNEL);
272 spin_lock(&fc_stats->counters_idr_lock);
274 err = idr_alloc_u32(&fc_stats->counters_idr, counter, &id, id,
277 spin_unlock(&fc_stats->counters_idr_lock);
282 llist_add(&counter->addlist, &fc_stats->addlist);
284 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
290 mlx5_cmd_fc_free(dev, counter->id);
296 EXPORT_SYMBOL(mlx5_fc_create);
298 u32 mlx5_fc_id(struct mlx5_fc *counter)
302 EXPORT_SYMBOL(mlx5_fc_id);
304 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
306 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
311 if (counter->aging) {
312 llist_add(&counter->dellist, &fc_stats->dellist);
313 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
317 mlx5_free_fc(dev, counter);
319 EXPORT_SYMBOL(mlx5_fc_destroy);
321 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
323 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
327 spin_lock_init(&fc_stats->counters_idr_lock);
328 idr_init(&fc_stats->counters_idr);
329 INIT_LIST_HEAD(&fc_stats->counters);
330 init_llist_head(&fc_stats->addlist);
331 init_llist_head(&fc_stats->dellist);
333 max_bulk_len = get_max_bulk_query_len(dev);
334 max_out_len = mlx5_cmd_fc_get_bulk_query_out_len(max_bulk_len);
335 fc_stats->bulk_query_out = kzalloc(max_out_len, GFP_KERNEL);
336 if (!fc_stats->bulk_query_out)
339 fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
343 fc_stats->sampling_interval = MLX5_FC_STATS_PERIOD;
344 INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
349 kfree(fc_stats->bulk_query_out);
353 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
355 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
356 struct llist_node *tmplist;
357 struct mlx5_fc *counter;
360 cancel_delayed_work_sync(&dev->priv.fc_stats.work);
361 destroy_workqueue(dev->priv.fc_stats.wq);
362 dev->priv.fc_stats.wq = NULL;
364 kfree(fc_stats->bulk_query_out);
366 idr_destroy(&fc_stats->counters_idr);
368 tmplist = llist_del_all(&fc_stats->addlist);
369 llist_for_each_entry_safe(counter, tmp, tmplist, addlist)
370 mlx5_free_fc(dev, counter);
372 list_for_each_entry_safe(counter, tmp, &fc_stats->counters, list)
373 mlx5_free_fc(dev, counter);
376 int mlx5_fc_query(struct mlx5_core_dev *dev, struct mlx5_fc *counter,
377 u64 *packets, u64 *bytes)
379 return mlx5_cmd_fc_query(dev, counter->id, packets, bytes);
381 EXPORT_SYMBOL(mlx5_fc_query);
383 u64 mlx5_fc_query_lastuse(struct mlx5_fc *counter)
385 return counter->cache.lastuse;
388 void mlx5_fc_query_cached(struct mlx5_fc *counter,
389 u64 *bytes, u64 *packets, u64 *lastuse)
391 struct mlx5_fc_cache c;
395 *bytes = c.bytes - counter->lastbytes;
396 *packets = c.packets - counter->lastpackets;
397 *lastuse = c.lastuse;
399 counter->lastbytes = c.bytes;
400 counter->lastpackets = c.packets;
403 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,
404 struct delayed_work *dwork,
407 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
409 queue_delayed_work(fc_stats->wq, dwork, delay);
412 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
413 unsigned long interval)
415 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
417 fc_stats->sampling_interval = min_t(unsigned long, interval,
418 fc_stats->sampling_interval);