Merge branch 'odp_fixes' into rdma.git for-next
[linux-2.6-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 /* Max number of counters to query in bulk read is 32K */
42 #define MLX5_SW_MAX_COUNTERS_BULK BIT(15)
43
44 struct mlx5_fc_cache {
45         u64 packets;
46         u64 bytes;
47         u64 lastuse;
48 };
49
50 struct mlx5_fc {
51         struct list_head list;
52         struct llist_node addlist;
53         struct llist_node dellist;
54
55         /* last{packets,bytes} members are used when calculating the delta since
56          * last reading
57          */
58         u64 lastpackets;
59         u64 lastbytes;
60
61         u32 id;
62         bool aging;
63
64         struct mlx5_fc_cache cache ____cacheline_aligned_in_smp;
65 };
66
67 /* locking scheme:
68  *
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
71  * to struct mlx5_fc.
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.
74  *
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
80  *     node.
81  *   - spawn thread to do the actual destroy
82  *
83  * - destroy (user context)
84  *   - add a counter to lockless dellist
85  *   - spawn thread to do the actual del
86  *
87  * - dump (user context)
88  *   user should not call dump after destroy
89  *
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
93  *                atomic)
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.
97  */
98
99 static struct list_head *mlx5_fc_counters_lookup_next(struct mlx5_core_dev *dev,
100                                                       u32 id)
101 {
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;
105         unsigned long tmp;
106
107         rcu_read_lock();
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))
112                         break;
113         }
114         rcu_read_unlock();
115
116         return counter ? &counter->list : &fc_stats->counters;
117 }
118
119 static void mlx5_fc_stats_insert(struct mlx5_core_dev *dev,
120                                  struct mlx5_fc *counter)
121 {
122         struct list_head *next = mlx5_fc_counters_lookup_next(dev, counter->id);
123
124         list_add_tail(&counter->list, next);
125 }
126
127 static void mlx5_fc_stats_remove(struct mlx5_core_dev *dev,
128                                  struct mlx5_fc *counter)
129 {
130         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
131
132         list_del(&counter->list);
133
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);
137 }
138
139 static int get_max_bulk_query_len(struct mlx5_core_dev *dev)
140 {
141         return min_t(int, MLX5_SW_MAX_COUNTERS_BULK,
142                           (1 << MLX5_CAP_GEN(dev, log_max_flow_counter_bulk)));
143 }
144
145 static void update_counter_cache(int index, u32 *bulk_raw_data,
146                                  struct mlx5_fc_cache *cache)
147 {
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);
152
153         if (cache->packets == packets)
154                 return;
155
156         cache->packets = packets;
157         cache->bytes = bytes;
158         cache->lastuse = jiffies;
159 }
160
161 static void mlx5_fc_stats_query_counter_range(struct mlx5_core_dev *dev,
162                                               struct mlx5_fc *first,
163                                               u32 last_id)
164 {
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;
170         u32 bulk_base_id;
171         int bulk_len;
172         int err;
173
174         while (query_more_counters) {
175                 /* first id must be aligned to 4 when using bulk query */
176                 bulk_base_id = counter->id & ~0x3;
177
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));
181
182                 err = mlx5_cmd_fc_bulk_query(dev, bulk_base_id, bulk_len,
183                                              data);
184                 if (err) {
185                         mlx5_core_err(dev, "Error doing bulk query: %d\n", err);
186                         return;
187                 }
188                 query_more_counters = false;
189
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;
193
194                         if (counter->id >= bulk_base_id + bulk_len) {
195                                 query_more_counters = true;
196                                 break;
197                         }
198
199                         update_counter_cache(counter_index, data, cache);
200                 }
201         }
202 }
203
204 static void mlx5_free_fc(struct mlx5_core_dev *dev,
205                          struct mlx5_fc *counter)
206 {
207         mlx5_cmd_fc_free(dev, counter->id);
208         kfree(counter);
209 }
210
211 static void mlx5_fc_stats_work(struct work_struct *work)
212 {
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
217          * they are inserted.
218          */
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;
223
224         if (addlist || !list_empty(&fc_stats->counters))
225                 queue_delayed_work(fc_stats->wq, &fc_stats->work,
226                                    fc_stats->sampling_interval);
227
228         llist_for_each_entry(counter, addlist, addlist)
229                 mlx5_fc_stats_insert(dev, counter);
230
231         llist_for_each_entry_safe(counter, tmp, dellist, dellist) {
232                 mlx5_fc_stats_remove(dev, counter);
233
234                 mlx5_free_fc(dev, counter);
235         }
236
237         if (time_before(now, fc_stats->next_query) ||
238             list_empty(&fc_stats->counters))
239                 return;
240         last = list_last_entry(&fc_stats->counters, struct mlx5_fc, list);
241
242         counter = list_first_entry(&fc_stats->counters, struct mlx5_fc,
243                                    list);
244         if (counter)
245                 mlx5_fc_stats_query_counter_range(dev, counter, last->id);
246
247         fc_stats->next_query = now + fc_stats->sampling_interval;
248 }
249
250 struct mlx5_fc *mlx5_fc_create(struct mlx5_core_dev *dev, bool aging)
251 {
252         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
253         struct mlx5_fc *counter;
254         int err;
255
256         counter = kzalloc(sizeof(*counter), GFP_KERNEL);
257         if (!counter)
258                 return ERR_PTR(-ENOMEM);
259         INIT_LIST_HEAD(&counter->list);
260
261         err = mlx5_cmd_fc_alloc(dev, &counter->id);
262         if (err)
263                 goto err_out;
264
265         if (aging) {
266                 u32 id = counter->id;
267
268                 counter->cache.lastuse = jiffies;
269                 counter->aging = true;
270
271                 idr_preload(GFP_KERNEL);
272                 spin_lock(&fc_stats->counters_idr_lock);
273
274                 err = idr_alloc_u32(&fc_stats->counters_idr, counter, &id, id,
275                                     GFP_NOWAIT);
276
277                 spin_unlock(&fc_stats->counters_idr_lock);
278                 idr_preload_end();
279                 if (err)
280                         goto err_out_alloc;
281
282                 llist_add(&counter->addlist, &fc_stats->addlist);
283
284                 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
285         }
286
287         return counter;
288
289 err_out_alloc:
290         mlx5_cmd_fc_free(dev, counter->id);
291 err_out:
292         kfree(counter);
293
294         return ERR_PTR(err);
295 }
296 EXPORT_SYMBOL(mlx5_fc_create);
297
298 u32 mlx5_fc_id(struct mlx5_fc *counter)
299 {
300         return counter->id;
301 }
302 EXPORT_SYMBOL(mlx5_fc_id);
303
304 void mlx5_fc_destroy(struct mlx5_core_dev *dev, struct mlx5_fc *counter)
305 {
306         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
307
308         if (!counter)
309                 return;
310
311         if (counter->aging) {
312                 llist_add(&counter->dellist, &fc_stats->dellist);
313                 mod_delayed_work(fc_stats->wq, &fc_stats->work, 0);
314                 return;
315         }
316
317         mlx5_free_fc(dev, counter);
318 }
319 EXPORT_SYMBOL(mlx5_fc_destroy);
320
321 int mlx5_init_fc_stats(struct mlx5_core_dev *dev)
322 {
323         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
324         int max_bulk_len;
325         int max_out_len;
326
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);
332
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)
337                 return -ENOMEM;
338
339         fc_stats->wq = create_singlethread_workqueue("mlx5_fc");
340         if (!fc_stats->wq)
341                 goto err_wq_create;
342
343         fc_stats->sampling_interval = MLX5_FC_STATS_PERIOD;
344         INIT_DELAYED_WORK(&fc_stats->work, mlx5_fc_stats_work);
345
346         return 0;
347
348 err_wq_create:
349         kfree(fc_stats->bulk_query_out);
350         return -ENOMEM;
351 }
352
353 void mlx5_cleanup_fc_stats(struct mlx5_core_dev *dev)
354 {
355         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
356         struct llist_node *tmplist;
357         struct mlx5_fc *counter;
358         struct mlx5_fc *tmp;
359
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;
363
364         kfree(fc_stats->bulk_query_out);
365
366         idr_destroy(&fc_stats->counters_idr);
367
368         tmplist = llist_del_all(&fc_stats->addlist);
369         llist_for_each_entry_safe(counter, tmp, tmplist, addlist)
370                 mlx5_free_fc(dev, counter);
371
372         list_for_each_entry_safe(counter, tmp, &fc_stats->counters, list)
373                 mlx5_free_fc(dev, counter);
374 }
375
376 int mlx5_fc_query(struct mlx5_core_dev *dev, struct mlx5_fc *counter,
377                   u64 *packets, u64 *bytes)
378 {
379         return mlx5_cmd_fc_query(dev, counter->id, packets, bytes);
380 }
381 EXPORT_SYMBOL(mlx5_fc_query);
382
383 u64 mlx5_fc_query_lastuse(struct mlx5_fc *counter)
384 {
385         return counter->cache.lastuse;
386 }
387
388 void mlx5_fc_query_cached(struct mlx5_fc *counter,
389                           u64 *bytes, u64 *packets, u64 *lastuse)
390 {
391         struct mlx5_fc_cache c;
392
393         c = counter->cache;
394
395         *bytes = c.bytes - counter->lastbytes;
396         *packets = c.packets - counter->lastpackets;
397         *lastuse = c.lastuse;
398
399         counter->lastbytes = c.bytes;
400         counter->lastpackets = c.packets;
401 }
402
403 void mlx5_fc_queue_stats_work(struct mlx5_core_dev *dev,
404                               struct delayed_work *dwork,
405                               unsigned long delay)
406 {
407         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
408
409         queue_delayed_work(fc_stats->wq, dwork, delay);
410 }
411
412 void mlx5_fc_update_sampling_interval(struct mlx5_core_dev *dev,
413                                       unsigned long interval)
414 {
415         struct mlx5_fc_stats *fc_stats = &dev->priv.fc_stats;
416
417         fc_stats->sampling_interval = min_t(unsigned long, interval,
418                                             fc_stats->sampling_interval);
419 }