blob: 17d5236759bdc716faa2f341073f482f274f52a0 [file] [log] [blame]
Tal Gilboa4f75da32019-01-10 17:33:17 +02001// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2/*
3 * Copyright (c) 2019, Mellanox Technologies inc. All rights reserved.
4 */
5
6#include <linux/dim.h>
7
8bool dim_on_top(struct dim *dim)
9{
10 switch (dim->tune_state) {
11 case DIM_PARKING_ON_TOP:
12 case DIM_PARKING_TIRED:
13 return true;
14 case DIM_GOING_RIGHT:
15 return (dim->steps_left > 1) && (dim->steps_right == 1);
16 default: /* DIM_GOING_LEFT */
17 return (dim->steps_right > 1) && (dim->steps_left == 1);
18 }
19}
20EXPORT_SYMBOL(dim_on_top);
21
22void dim_turn(struct dim *dim)
23{
24 switch (dim->tune_state) {
25 case DIM_PARKING_ON_TOP:
26 case DIM_PARKING_TIRED:
27 break;
28 case DIM_GOING_RIGHT:
29 dim->tune_state = DIM_GOING_LEFT;
30 dim->steps_left = 0;
31 break;
32 case DIM_GOING_LEFT:
33 dim->tune_state = DIM_GOING_RIGHT;
34 dim->steps_right = 0;
35 break;
36 }
37}
38EXPORT_SYMBOL(dim_turn);
39
40void dim_park_on_top(struct dim *dim)
41{
42 dim->steps_right = 0;
43 dim->steps_left = 0;
44 dim->tired = 0;
45 dim->tune_state = DIM_PARKING_ON_TOP;
46}
47EXPORT_SYMBOL(dim_park_on_top);
48
49void dim_park_tired(struct dim *dim)
50{
51 dim->steps_right = 0;
52 dim->steps_left = 0;
53 dim->tune_state = DIM_PARKING_TIRED;
54}
55EXPORT_SYMBOL(dim_park_tired);
56
57void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
58 struct dim_stats *curr_stats)
59{
60 /* u32 holds up to 71 minutes, should be enough */
61 u32 delta_us = ktime_us_delta(end->time, start->time);
62 u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr);
63 u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr,
64 start->byte_ctr);
65
66 if (!delta_us)
67 return;
68
69 curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
70 curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
71 curr_stats->epms = DIV_ROUND_UP(DIM_NEVENTS * USEC_PER_MSEC,
72 delta_us);
73}
74EXPORT_SYMBOL(dim_calc_stats);