Merge 5.6-rc5 into tty-next
[linux/fpc-iii.git] / lib / dim / net_dim.c
bloba4db51c2126633c35c3f0ee789022a3562596b80
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /*
3 * Copyright (c) 2018, Mellanox Technologies inc. All rights reserved.
4 */
6 #include <linux/dim.h>
8 /*
9 * Net DIM profiles:
10 * There are different set of profiles for each CQ period mode.
11 * There are different set of profiles for RX/TX CQs.
12 * Each profile size must be of NET_DIM_PARAMS_NUM_PROFILES
14 #define NET_DIM_PARAMS_NUM_PROFILES 5
15 #define NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE 256
16 #define NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE 128
17 #define NET_DIM_DEF_PROFILE_CQE 1
18 #define NET_DIM_DEF_PROFILE_EQE 1
20 #define NET_DIM_RX_EQE_PROFILES { \
21 {1, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
22 {8, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
23 {64, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
24 {128, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
25 {256, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
28 #define NET_DIM_RX_CQE_PROFILES { \
29 {2, 256}, \
30 {8, 128}, \
31 {16, 64}, \
32 {32, 64}, \
33 {64, 64} \
36 #define NET_DIM_TX_EQE_PROFILES { \
37 {1, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
38 {8, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
39 {32, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
40 {64, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
41 {128, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE} \
44 #define NET_DIM_TX_CQE_PROFILES { \
45 {5, 128}, \
46 {8, 64}, \
47 {16, 32}, \
48 {32, 32}, \
49 {64, 32} \
52 static const struct dim_cq_moder
53 rx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
54 NET_DIM_RX_EQE_PROFILES,
55 NET_DIM_RX_CQE_PROFILES,
58 static const struct dim_cq_moder
59 tx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
60 NET_DIM_TX_EQE_PROFILES,
61 NET_DIM_TX_CQE_PROFILES,
64 struct dim_cq_moder
65 net_dim_get_rx_moderation(u8 cq_period_mode, int ix)
67 struct dim_cq_moder cq_moder = rx_profile[cq_period_mode][ix];
69 cq_moder.cq_period_mode = cq_period_mode;
70 return cq_moder;
72 EXPORT_SYMBOL(net_dim_get_rx_moderation);
74 struct dim_cq_moder
75 net_dim_get_def_rx_moderation(u8 cq_period_mode)
77 u8 profile_ix = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
78 NET_DIM_DEF_PROFILE_CQE : NET_DIM_DEF_PROFILE_EQE;
80 return net_dim_get_rx_moderation(cq_period_mode, profile_ix);
82 EXPORT_SYMBOL(net_dim_get_def_rx_moderation);
84 struct dim_cq_moder
85 net_dim_get_tx_moderation(u8 cq_period_mode, int ix)
87 struct dim_cq_moder cq_moder = tx_profile[cq_period_mode][ix];
89 cq_moder.cq_period_mode = cq_period_mode;
90 return cq_moder;
92 EXPORT_SYMBOL(net_dim_get_tx_moderation);
94 struct dim_cq_moder
95 net_dim_get_def_tx_moderation(u8 cq_period_mode)
97 u8 profile_ix = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
98 NET_DIM_DEF_PROFILE_CQE : NET_DIM_DEF_PROFILE_EQE;
100 return net_dim_get_tx_moderation(cq_period_mode, profile_ix);
102 EXPORT_SYMBOL(net_dim_get_def_tx_moderation);
104 static int net_dim_step(struct dim *dim)
106 if (dim->tired == (NET_DIM_PARAMS_NUM_PROFILES * 2))
107 return DIM_TOO_TIRED;
109 switch (dim->tune_state) {
110 case DIM_PARKING_ON_TOP:
111 case DIM_PARKING_TIRED:
112 break;
113 case DIM_GOING_RIGHT:
114 if (dim->profile_ix == (NET_DIM_PARAMS_NUM_PROFILES - 1))
115 return DIM_ON_EDGE;
116 dim->profile_ix++;
117 dim->steps_right++;
118 break;
119 case DIM_GOING_LEFT:
120 if (dim->profile_ix == 0)
121 return DIM_ON_EDGE;
122 dim->profile_ix--;
123 dim->steps_left++;
124 break;
127 dim->tired++;
128 return DIM_STEPPED;
131 static void net_dim_exit_parking(struct dim *dim)
133 dim->tune_state = dim->profile_ix ? DIM_GOING_LEFT : DIM_GOING_RIGHT;
134 net_dim_step(dim);
137 static int net_dim_stats_compare(struct dim_stats *curr,
138 struct dim_stats *prev)
140 if (!prev->bpms)
141 return curr->bpms ? DIM_STATS_BETTER : DIM_STATS_SAME;
143 if (IS_SIGNIFICANT_DIFF(curr->bpms, prev->bpms))
144 return (curr->bpms > prev->bpms) ? DIM_STATS_BETTER :
145 DIM_STATS_WORSE;
147 if (!prev->ppms)
148 return curr->ppms ? DIM_STATS_BETTER :
149 DIM_STATS_SAME;
151 if (IS_SIGNIFICANT_DIFF(curr->ppms, prev->ppms))
152 return (curr->ppms > prev->ppms) ? DIM_STATS_BETTER :
153 DIM_STATS_WORSE;
155 if (!prev->epms)
156 return DIM_STATS_SAME;
158 if (IS_SIGNIFICANT_DIFF(curr->epms, prev->epms))
159 return (curr->epms < prev->epms) ? DIM_STATS_BETTER :
160 DIM_STATS_WORSE;
162 return DIM_STATS_SAME;
165 static bool net_dim_decision(struct dim_stats *curr_stats, struct dim *dim)
167 int prev_state = dim->tune_state;
168 int prev_ix = dim->profile_ix;
169 int stats_res;
170 int step_res;
172 switch (dim->tune_state) {
173 case DIM_PARKING_ON_TOP:
174 stats_res = net_dim_stats_compare(curr_stats,
175 &dim->prev_stats);
176 if (stats_res != DIM_STATS_SAME)
177 net_dim_exit_parking(dim);
178 break;
180 case DIM_PARKING_TIRED:
181 dim->tired--;
182 if (!dim->tired)
183 net_dim_exit_parking(dim);
184 break;
186 case DIM_GOING_RIGHT:
187 case DIM_GOING_LEFT:
188 stats_res = net_dim_stats_compare(curr_stats,
189 &dim->prev_stats);
190 if (stats_res != DIM_STATS_BETTER)
191 dim_turn(dim);
193 if (dim_on_top(dim)) {
194 dim_park_on_top(dim);
195 break;
198 step_res = net_dim_step(dim);
199 switch (step_res) {
200 case DIM_ON_EDGE:
201 dim_park_on_top(dim);
202 break;
203 case DIM_TOO_TIRED:
204 dim_park_tired(dim);
205 break;
208 break;
211 if (prev_state != DIM_PARKING_ON_TOP ||
212 dim->tune_state != DIM_PARKING_ON_TOP)
213 dim->prev_stats = *curr_stats;
215 return dim->profile_ix != prev_ix;
218 void net_dim(struct dim *dim, struct dim_sample end_sample)
220 struct dim_stats curr_stats;
221 u16 nevents;
223 switch (dim->state) {
224 case DIM_MEASURE_IN_PROGRESS:
225 nevents = BIT_GAP(BITS_PER_TYPE(u16),
226 end_sample.event_ctr,
227 dim->start_sample.event_ctr);
228 if (nevents < DIM_NEVENTS)
229 break;
230 dim_calc_stats(&dim->start_sample, &end_sample, &curr_stats);
231 if (net_dim_decision(&curr_stats, dim)) {
232 dim->state = DIM_APPLY_NEW_PROFILE;
233 schedule_work(&dim->work);
234 break;
236 /* fall through */
237 case DIM_START_MEASURE:
238 dim_update_sample(end_sample.event_ctr, end_sample.pkt_ctr,
239 end_sample.byte_ctr, &dim->start_sample);
240 dim->state = DIM_MEASURE_IN_PROGRESS;
241 break;
242 case DIM_APPLY_NEW_PROFILE:
243 break;
246 EXPORT_SYMBOL(net_dim);