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)
44 * It is the responsibility of the user to prevent concurrent calls or bad
45 * ordering to mlx5_fc_create(), mlx5_fc_destroy() and accessing a reference
47 * e.g en_tc.c is protected by RTNL lock of its caller, and will never call a
48 * dump (access to struct mlx5_fc) after a counter is destroyed.
50 * access to counter list:
51 * - create (user context)
52 * - mlx5_fc_create() only adds to an addlist to be used by
53 * mlx5_fc_stats_query_work(). addlist is protected by a spinlock.
54 * - spawn thread to do the actual destroy
56 * - destroy (user context)
57 * - mark a counter as deleted
58 * - spawn thread to do the actual del
60 * - dump (user context)
61 * user should not call dump after destroy
63 * - query (single thread workqueue context)
64 * destroy/dump - no conflict (see destroy)
65 * query/dump - packets and bytes might be inconsistent (since update is not
67 * query/create - no conflict (see create)
68 * since every create/destroy spawn the work, only after necessary time has
69 * elapsed, the thread will actually query the hardware.
72 static void mlx5_fc_stats_insert(struct rb_root *root, struct mlx5_fc *counter)
74 struct rb_node **new = &root->rb_node;
75 struct rb_node *parent = NULL;
78 struct mlx5_fc *this = container_of(*new, struct mlx5_fc, node);
79 int result = counter->id - this->id;
83 new = &((*new)->rb_left);
85 new = &((*new)->rb_right);
88 /* Add new node and rebalance tree. */
89 rb_link_node(&counter->node, parent, new);
90 rb_insert_color(&counter->node, root);
93 static void mlx5_fc_stats_work(struct work_struct *work)
95 struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
96 priv.fc_stats.work.work);
97 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
98 unsigned long now = jiffies;
99 struct mlx5_fc *counter;
100 struct rb_node *node;
104 spin_lock(&fc_stats->addlist_lock);
106 list_splice_tail_init(&fc_stats->addlist, &tmplist);
108 if (!list_empty(&tmplist) || !RB_EMPTY_ROOT(&fc_stats->counters))
109 queue_delayed_work(fc_stats->wq, &fc_stats->work, MLX5_FC_STATS_PERIOD);
111 spin_unlock(&fc_stats->addlist_lock);
113 list_for_each_entry(counter, &tmplist, list)
114 mlx5_fc_stats_insert(&fc_stats->counters, counter);
116 node = rb_first(&fc_stats->counters);
118 struct mlx5_fc_cache *c;
122 counter = rb_entry(node, struct mlx5_fc, node);
125 node = rb_next(node);
127 if (counter->deleted) {
128 rb_erase(&counter->node, &fc_stats->counters);
130 mlx5_cmd_fc_free(dev, counter->id);
136 if (time_before(now, fc_stats->next_query))
139 err = mlx5_cmd_fc_query(dev, counter->id, &packets, &bytes);
141 pr_err("Error querying stats for counter id %d\n",
146 if (packets == c->packets)
149 c->lastuse = jiffies;
150 c->packets = packets;
154 if (time_after_eq(now, fc_stats->next_query))
155 fc_stats->next_query = now + MLX5_FC_STATS_PERIOD;
158 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
160 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
161 struct mlx5_fc *counter;
164 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
166 return ERR_PTR(-ENOMEM);
168 err = mlx5_cmd_fc_alloc(dev, &counter->id);
173 counter->aging = true;
175 spin_lock(&fc_stats->addlist_lock);
176 list_add(&counter->list, &fc_stats->addlist);
177 spin_unlock(&fc_stats->addlist_lock);
179 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
190 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
192 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
197 if (counter->aging) {
198 counter->deleted = true;
199 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
203 mlx5_cmd_fc_free(dev, counter->id);
207 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
209 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
211 fc_stats->counters = RB_ROOT;
212 INIT_LIST_HEAD(&fc_stats->addlist);
213 spin_lock_init(&fc_stats->addlist_lock);
215 fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
219 INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
224 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
226 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
227 struct mlx5_fc *counter;
229 struct rb_node *node;
231 cancel_delayed_work_sync(&dev->priv.fc_stats.work);
232 destroy_workqueue(dev->priv.fc_stats.wq);
233 dev->priv.fc_stats.wq = NULL;
235 list_for_each_entry_safe(counter, tmp, &fc_stats->addlist, list) {
236 list_del(&counter->list);
238 mlx5_cmd_fc_free(dev, counter->id);
243 node = rb_first(&fc_stats->counters);
245 counter = rb_entry(node, struct mlx5_fc, node);
247 node = rb_next(node);
249 rb_erase(&counter->node, &fc_stats->counters);
251 mlx5_cmd_fc_free(dev, counter->id);
257 void mlx5_fc_query_cached(struct mlx5_fc *counter,
258 u64 *bytes, u64 *packets, u64 *lastuse)
260 struct mlx5_fc_cache c;
264 *bytes = c.bytes - counter->lastbytes;
265 *packets = c.packets - counter->lastpackets;
266 *lastuse = c.lastuse;
268 counter->lastbytes = c.bytes;
269 counter->lastpackets = c.packets;