Merge tag 'powerpc-4.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / fs_counters.c
1 /*
2  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
33 #include <linux/mlx5/driver.h>
34 #include <linux/mlx5/fs.h>
35 #include <linux/rbtree.h>
36 #include "mlx5_core.h"
37 #include "fs_core.h"
38 #include "fs_cmd.h"
39
40 #define MLX5_FC_STATS_PERIOD msecs_to_jiffies(1000)
41
42 /* locking scheme:
43  *
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
46  * to struct mlx5_fc.
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.
49  *
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
55  *
56  * - destroy (user context)
57  *   - mark a counter as deleted
58  *   - spawn thread to do the actual del
59  *
60  * - dump (user context)
61  *   user should not call dump after destroy
62  *
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
66  *                atomic)
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.
70  */
71
72 static void mlx5_fc_stats_insert(struct rb_root *root, struct mlx5_fc *counter)
73 {
74         struct rb_node **new = &root->rb_node;
75         struct rb_node *parent = NULL;
76
77         while (*new) {
78                 struct mlx5_fc *this = rb_entry(*new, struct mlx5_fc, node);
79                 int result = counter->id - this->id;
80
81                 parent = *new;
82                 if (result < 0)
83                         new = &((*new)->rb_left);
84                 else
85                         new = &((*new)->rb_right);
86         }
87
88         /* Add new node and rebalance tree. */
89         rb_link_node(&counter->node, parent, new);
90         rb_insert_color(&counter->node, root);
91 }
92
93 static struct rb_node *mlx5_fc_stats_query(struct mlx5_core_dev *dev,
94                                            struct mlx5_fc *first,
95                                            u16 last_id)
96 {
97         struct mlx5_cmd_fc_bulk *b;
98         struct rb_node *node = NULL;
99         u16 afirst_id;
100         int num;
101         int err;
102         int max_bulk = 1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk);
103
104         /* first id must be aligned to 4 when using bulk query */
105         afirst_id = first->id & ~0x3;
106
107         /* number of counters to query inc. the last counter */
108         num = ALIGN(last_id - afirst_id + 1, 4);
109         if (num > max_bulk) {
110                 num = max_bulk;
111                 last_id = afirst_id + num - 1;
112         }
113
114         b = mlx5_cmd_fc_bulk_alloc(dev, afirst_id, num);
115         if (!b) {
116                 mlx5_core_err(dev, "Error allocating resources for bulk query\n");
117                 return NULL;
118         }
119
120         err = mlx5_cmd_fc_bulk_query(dev, b);
121         if (err) {
122                 mlx5_core_err(dev, "Error doing bulk query: %d\n", err);
123                 goto out;
124         }
125
126         for (node = &first->node; node; node = rb_next(node)) {
127                 struct mlx5_fc *counter = rb_entry(node, struct mlx5_fc, node);
128                 struct mlx5_fc_cache *c = &counter->cache;
129                 u64 packets;
130                 u64 bytes;
131
132                 if (counter->id > last_id)
133                         break;
134
135                 mlx5_cmd_fc_bulk_get(dev, b,
136                                      counter->id, &packets, &bytes);
137
138                 if (c->packets == packets)
139                         continue;
140
141                 c->packets = packets;
142                 c->bytes = bytes;
143                 c->lastuse = jiffies;
144         }
145
146 out:
147         mlx5_cmd_fc_bulk_free(b);
148
149         return node;
150 }
151
152 static void mlx5_fc_stats_work(struct work_struct *work)
153 {
154         struct mlx5_core_dev *dev = container_of(work, struct mlx5_core_dev,
155                                                  priv.fc_stats.work.work);
156         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
157         unsigned long now = jiffies;
158         struct mlx5_fc *counter = NULL;
159         struct mlx5_fc *last = NULL;
160         struct rb_node *node;
161         LIST_HEAD(tmplist);
162
163         spin_lock(&fc_stats->addlist_lock);
164
165         list_splice_tail_init(&fc_stats->addlist, &tmplist);
166
167         if (!list_empty(&tmplist) || !RB_EMPTY_ROOT(&fc_stats->counters))
168                 queue_delayed_work(fc_stats->wq, &fc_stats->work,
169                                    fc_stats->sampling_interval);
170
171         spin_unlock(&fc_stats->addlist_lock);
172
173         list_for_each_entry(counter, &tmplist, list)
174                 mlx5_fc_stats_insert(&fc_stats->counters, counter);
175
176         node = rb_first(&fc_stats->counters);
177         while (node) {
178                 counter = rb_entry(node, struct mlx5_fc, node);
179
180                 node = rb_next(node);
181
182                 if (counter->deleted) {
183                         rb_erase(&counter->node, &fc_stats->counters);
184
185                         mlx5_cmd_fc_free(dev, counter->id);
186
187                         kfree(counter);
188                         continue;
189                 }
190
191                 last = counter;
192         }
193
194         if (time_before(now, fc_stats->next_query) || !last)
195                 return;
196
197         node = rb_first(&fc_stats->counters);
198         while (node) {
199                 counter = rb_entry(node, struct mlx5_fc, node);
200
201                 node = mlx5_fc_stats_query(dev, counter, last->id);
202         }
203
204         fc_stats->next_query = now + fc_stats->sampling_interval;
205 }
206
207 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
208 {
209         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
210         struct mlx5_fc *counter;
211         int err;
212
213         counter = kzalloc(sizeof(*counter), GFP_KERNEL);
214         if (!counter)
215                 return ERR_PTR(-ENOMEM);
216
217         err = mlx5_cmd_fc_alloc(dev, &counter->id);
218         if (err)
219                 goto err_out;
220
221         if (aging) {
222                 counter->cache.lastuse = jiffies;
223                 counter->aging = true;
224
225                 spin_lock(&fc_stats->addlist_lock);
226                 list_add(&counter->list, &fc_stats->addlist);
227                 spin_unlock(&fc_stats->addlist_lock);
228
229                 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
230         }
231
232         return counter;
233
234 err_out:
235         kfree(counter);
236
237         return ERR_PTR(err);
238 }
239
240 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
241 {
242         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
243
244         if (!counter)
245                 return;
246
247         if (counter->aging) {
248                 counter->deleted = true;
249                 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
250                 return;
251         }
252
253         mlx5_cmd_fc_free(dev, counter->id);
254         kfree(counter);
255 }
256
257 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
258 {
259         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
260
261         fc_stats->counters = RB_ROOT;
262         INIT_LIST_HEAD(&fc_stats->addlist);
263         spin_lock_init(&fc_stats->addlist_lock);
264
265         fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
266         if (!fc_stats->wq)
267                 return -ENOMEM;
268
269         fc_stats->sampling_interval = MLX5_FC_STATS_PERIOD;
270         INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
271
272         return 0;
273 }
274
275 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
276 {
277         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
278         struct mlx5_fc *counter;
279         struct mlx5_fc *tmp;
280         struct rb_node *node;
281
282         cancel_delayed_work_sync(&dev->priv.fc_stats.work);
283         destroy_workqueue(dev->priv.fc_stats.wq);
284         dev->priv.fc_stats.wq = NULL;
285
286         list_for_each_entry_safe(counter, tmp, &fc_stats->addlist, list) {
287                 list_del(&counter->list);
288
289                 mlx5_cmd_fc_free(dev, counter->id);
290
291                 kfree(counter);
292         }
293
294         node = rb_first(&fc_stats->counters);
295         while (node) {
296                 counter = rb_entry(node, struct mlx5_fc, node);
297
298                 node = rb_next(node);
299
300                 rb_erase(&counter->node, &fc_stats->counters);
301
302                 mlx5_cmd_fc_free(dev, counter->id);
303
304                 kfree(counter);
305         }
306 }
307
308 void mlx5_fc_query_cached(struct mlx5_fc *counter,
309                           u64 *bytes, u64 *packets, u64 *lastuse)
310 {
311         struct mlx5_fc_cache c;
312
313         c = counter->cache;
314
315         *bytes = c.bytes - counter->lastbytes;
316         *packets = c.packets - counter->lastpackets;
317         *lastuse = c.lastuse;
318
319         counter->lastbytes = c.bytes;
320         counter->lastpackets = c.packets;
321 }
322
323 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,
324                               struct delayed_work *dwork,
325                               unsigned long delay)
326 {
327         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
328
329         queue_delayed_work(fc_stats->wq, dwork, delay);
330 }
331
332 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
333                                       unsigned long interval)
334 {
335         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
336
337         fc_stats->sampling_interval = min_t(unsigned long, interval,
338                                             fc_stats->sampling_interval);
339 }