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 "mlx5_core.h"
39 #define MLX5_FC_STATS_PERIOD msecs_to_jiffies(1000)
43 * It is the responsibility of the user to prevent concurrent calls or bad
44 * ordering to mlx5_fc_create(), mlx5_fc_destroy() and accessing a reference
46 * e.g en_tc.c is protected by RTNL lock of its caller, and will never call a
47 * dump (access to struct mlx5_fc) after a counter is destroyed.
49 * access to counter list:
50 * - create (user context)
51 * - mlx5_fc_create() only adds to an addlist to be used by
52 * mlx5_fc_stats_query_work(). addlist is protected by a spinlock.
53 * - spawn thread to do the actual destroy
55 * - destroy (user context)
56 * - mark a counter as deleted
57 * - spawn thread to do the actual del
59 * - dump (user context)
60 * user should not call dump after destroy
62 * - query (single thread workqueue context)
63 * destroy/dump - no conflict (see destroy)
64 * query/dump - packets and bytes might be inconsistent (since update is not
66 * query/create - no conflict (see create)
67 * since every create/destroy spawn the work, only after necessary time has
68 * elapsed, the thread will actually query the hardware.
71 static void mlx5_fc_stats_work(struct work_struct *work)
73 struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
74 priv.fc_stats.work.work);
75 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
76 unsigned long now = jiffies;
77 struct mlx5_fc *counter;
81 spin_lock(&fc_stats->addlist_lock);
83 list_splice_tail_init(&fc_stats->addlist, &fc_stats->list);
85 if (!list_empty(&fc_stats->list))
86 queue_delayed_work(fc_stats->wq, &fc_stats->work, MLX5_FC_STATS_PERIOD);
88 spin_unlock(&fc_stats->addlist_lock);
90 list_for_each_entry_safe(counter, tmp, &fc_stats->list, list) {
91 struct mlx5_fc_cache *c = &counter->cache;
95 if (counter->deleted) {
96 list_del(&counter->list);
98 mlx5_cmd_fc_free(dev, counter->id);
104 if (time_before(now, fc_stats->next_query))
107 err = mlx5_cmd_fc_query(dev, counter->id, &packets, &bytes);
109 pr_err("Error querying stats for counter id %d\n",
114 if (packets == c->packets)
117 c->lastuse = jiffies;
118 c->packets = packets;
122 if (time_after_eq(now, fc_stats->next_query))
123 fc_stats->next_query = now + MLX5_FC_STATS_PERIOD;
126 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
128 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
129 struct mlx5_fc *counter;
132 counter = kzalloc(sizeof(*counter), GFP_KERNEL);
134 return ERR_PTR(-ENOMEM);
136 err = mlx5_cmd_fc_alloc(dev, &counter->id);
141 counter->aging = true;
143 spin_lock(&fc_stats->addlist_lock);
144 list_add(&counter->list, &fc_stats->addlist);
145 spin_unlock(&fc_stats->addlist_lock);
147 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
158 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
160 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
165 if (counter->aging) {
166 counter->deleted = true;
167 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
171 mlx5_cmd_fc_free(dev, counter->id);
175 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
177 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
179 INIT_LIST_HEAD(&fc_stats->list);
180 INIT_LIST_HEAD(&fc_stats->addlist);
181 spin_lock_init(&fc_stats->addlist_lock);
183 fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
187 INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
192 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
194 struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
195 struct mlx5_fc *counter;
198 cancel_delayed_work_sync(&dev->priv.fc_stats.work);
199 destroy_workqueue(dev->priv.fc_stats.wq);
200 dev->priv.fc_stats.wq = NULL;
202 list_splice_tail_init(&fc_stats->addlist, &fc_stats->list);
204 list_for_each_entry_safe(counter, tmp, &fc_stats->list, list) {
205 list_del(&counter->list);
207 mlx5_cmd_fc_free(dev, counter->id);
213 void mlx5_fc_query_cached(struct mlx5_fc *counter,
214 u64 *bytes, u64 *packets, u64 *lastuse)
216 struct mlx5_fc_cache c;
220 *bytes = c.bytes - counter->lastbytes;
221 *packets = c.packets - counter->lastpackets;
222 *lastuse = c.lastuse;
224 counter->lastbytes = c.bytes;
225 counter->lastpackets = c.packets;