linux/dim: Rename externally exposed macros
[muen/linux.git] / include / linux / dim.h
1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2019 Mellanox Technologies. */
3
4 #ifndef DIM_H
5 #define DIM_H
6
7 #include <linux/module.h>
8
9 #define DIM_NEVENTS 64
10
11 /* more than 10% difference */
12 #define IS_SIGNIFICANT_DIFF(val, ref) \
13         (((100UL * abs((val) - (ref))) / (ref)) > 10)
14 #define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) \
15 & (BIT_ULL(bits) - 1))
16
17 struct net_dim_cq_moder {
18         u16 usec;
19         u16 pkts;
20         u8 cq_period_mode;
21 };
22
23 struct net_dim_sample {
24         ktime_t time;
25         u32 pkt_ctr;
26         u32 byte_ctr;
27         u16 event_ctr;
28 };
29
30 struct dim_stats {
31         int ppms; /* packets per msec */
32         int bpms; /* bytes per msec */
33         int epms; /* events per msec */
34 };
35
36 struct net_dim { /* Dynamic Interrupt Moderation */
37         u8 state;
38         struct dim_stats prev_stats;
39         struct net_dim_sample start_sample;
40         struct work_struct work;
41         u8 profile_ix;
42         u8 mode;
43         u8 tune_state;
44         u8 steps_right;
45         u8 steps_left;
46         u8 tired;
47 };
48
49 enum {
50         DIM_CQ_PERIOD_MODE_START_FROM_EQE = 0x0,
51         DIM_CQ_PERIOD_MODE_START_FROM_CQE = 0x1,
52         DIM_CQ_PERIOD_NUM_MODES
53 };
54
55 enum {
56         DIM_START_MEASURE,
57         DIM_MEASURE_IN_PROGRESS,
58         DIM_APPLY_NEW_PROFILE,
59 };
60
61 enum {
62         DIM_PARKING_ON_TOP,
63         DIM_PARKING_TIRED,
64         DIM_GOING_RIGHT,
65         DIM_GOING_LEFT,
66 };
67
68 enum {
69         DIM_STATS_WORSE,
70         DIM_STATS_SAME,
71         DIM_STATS_BETTER,
72 };
73
74 enum {
75         DIM_STEPPED,
76         DIM_TOO_TIRED,
77         DIM_ON_EDGE,
78 };
79
80 static inline bool dim_on_top(struct net_dim *dim)
81 {
82         switch (dim->tune_state) {
83         case DIM_PARKING_ON_TOP:
84         case DIM_PARKING_TIRED:
85                 return true;
86         case DIM_GOING_RIGHT:
87                 return (dim->steps_left > 1) && (dim->steps_right == 1);
88         default: /* DIM_GOING_LEFT */
89                 return (dim->steps_right > 1) && (dim->steps_left == 1);
90         }
91 }
92
93 static inline void dim_turn(struct net_dim *dim)
94 {
95         switch (dim->tune_state) {
96         case DIM_PARKING_ON_TOP:
97         case DIM_PARKING_TIRED:
98                 break;
99         case DIM_GOING_RIGHT:
100                 dim->tune_state = DIM_GOING_LEFT;
101                 dim->steps_left = 0;
102                 break;
103         case DIM_GOING_LEFT:
104                 dim->tune_state = DIM_GOING_RIGHT;
105                 dim->steps_right = 0;
106                 break;
107         }
108 }
109
110 static inline void dim_park_on_top(struct net_dim *dim)
111 {
112         dim->steps_right  = 0;
113         dim->steps_left   = 0;
114         dim->tired        = 0;
115         dim->tune_state   = DIM_PARKING_ON_TOP;
116 }
117
118 static inline void dim_park_tired(struct net_dim *dim)
119 {
120         dim->steps_right  = 0;
121         dim->steps_left   = 0;
122         dim->tune_state   = DIM_PARKING_TIRED;
123 }
124
125 static inline void
126 net_dim_sample(u16 event_ctr, u64 packets, u64 bytes, struct net_dim_sample *s)
127 {
128         s->time      = ktime_get();
129         s->pkt_ctr   = packets;
130         s->byte_ctr  = bytes;
131         s->event_ctr = event_ctr;
132 }
133
134 static inline void
135 dim_calc_stats(struct net_dim_sample *start, struct net_dim_sample *end,
136                struct dim_stats *curr_stats)
137 {
138         /* u32 holds up to 71 minutes, should be enough */
139         u32 delta_us = ktime_us_delta(end->time, start->time);
140         u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr);
141         u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr,
142                              start->byte_ctr);
143
144         if (!delta_us)
145                 return;
146
147         curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
148         curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
149         curr_stats->epms = DIV_ROUND_UP(DIM_NEVENTS * USEC_PER_MSEC,
150                                         delta_us);
151 }
152
153 #endif /* DIM_H */