blob: bebeaad897cc406d550175603a19a1c15e582942 [file] [log] [blame]
Gil Rockahcb3c7fd2016-06-23 17:02:41 +03001/*
2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
Andy Gospodarekb9c872f2018-01-09 16:06:16 -05003 * Copyright (c) 2017-2018, Broadcom Limited. All rights reserved.
Gil Rockahcb3c7fd2016-06-23 17:02:41 +03004 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -050034#ifndef NET_DIM_H
35#define NET_DIM_H
36
37#include <linux/module.h>
38
39struct net_dim_cq_moder {
40 u16 usec;
41 u16 pkts;
42 u8 cq_period_mode;
43};
44
45struct net_dim_sample {
46 ktime_t time;
47 u32 pkt_ctr;
48 u32 byte_ctr;
49 u16 event_ctr;
50};
51
52struct net_dim_stats {
53 int ppms; /* packets per msec */
54 int bpms; /* bytes per msec */
55 int epms; /* events per msec */
56};
57
58struct net_dim { /* Adaptive Moderation */
59 u8 state;
60 struct net_dim_stats prev_stats;
61 struct net_dim_sample start_sample;
62 struct work_struct work;
63 u8 profile_ix;
64 u8 mode;
65 u8 tune_state;
66 u8 steps_right;
67 u8 steps_left;
68 u8 tired;
69};
70
71enum {
72 NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE = 0x0,
73 NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE = 0x1,
74 NET_DIM_CQ_PERIOD_NUM_MODES
75};
76
77/* Adaptive moderation logic */
78enum {
79 NET_DIM_START_MEASURE,
80 NET_DIM_MEASURE_IN_PROGRESS,
81 NET_DIM_APPLY_NEW_PROFILE,
82};
83
84enum {
85 NET_DIM_PARKING_ON_TOP,
86 NET_DIM_PARKING_TIRED,
87 NET_DIM_GOING_RIGHT,
88 NET_DIM_GOING_LEFT,
89};
90
91enum {
92 NET_DIM_STATS_WORSE,
93 NET_DIM_STATS_SAME,
94 NET_DIM_STATS_BETTER,
95};
96
97enum {
98 NET_DIM_STEPPED,
99 NET_DIM_TOO_TIRED,
100 NET_DIM_ON_EDGE,
101};
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300102
Andy Gospodarek9a317422018-01-09 16:06:17 -0500103#define NET_DIM_PARAMS_NUM_PROFILES 5
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300104/* Adaptive moderation profiles */
Andy Gospodarek9a317422018-01-09 16:06:17 -0500105#define NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE 256
106#define NET_DIM_DEF_PROFILE_CQE 1
107#define NET_DIM_DEF_PROFILE_EQE 1
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300108
Andy Gospodarek9a317422018-01-09 16:06:17 -0500109/* All profiles sizes must be NET_PARAMS_DIM_NUM_PROFILES */
110#define NET_DIM_EQE_PROFILES { \
111 {1, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
112 {8, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
113 {64, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
114 {128, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
115 {256, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300116}
117
Andy Gospodarek9a317422018-01-09 16:06:17 -0500118#define NET_DIM_CQE_PROFILES { \
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300119 {2, 256}, \
120 {8, 128}, \
121 {16, 64}, \
122 {32, 64}, \
123 {64, 64} \
124}
125
Andy Gospodarek9a317422018-01-09 16:06:17 -0500126static const struct net_dim_cq_moder
127profile[NET_DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
128 NET_DIM_EQE_PROFILES,
129 NET_DIM_CQE_PROFILES,
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300130};
131
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500132static inline struct net_dim_cq_moder net_dim_get_profile(u8 cq_period_mode,
133 int ix)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300134{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500135 struct net_dim_cq_moder cq_moder;
Tal Gilboa0088cbb2017-09-26 16:20:43 +0300136
137 cq_moder = profile[cq_period_mode][ix];
138 cq_moder.cq_period_mode = cq_period_mode;
139 return cq_moder;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300140}
141
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500142static inline struct net_dim_cq_moder net_dim_get_def_profile(u8 rx_cq_period_mode)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300143{
144 int default_profile_ix;
145
Andy Gospodarek9a317422018-01-09 16:06:17 -0500146 if (rx_cq_period_mode == NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE)
147 default_profile_ix = NET_DIM_DEF_PROFILE_CQE;
148 else /* NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE */
149 default_profile_ix = NET_DIM_DEF_PROFILE_EQE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300150
Andy Gospodarek9a317422018-01-09 16:06:17 -0500151 return net_dim_get_profile(rx_cq_period_mode, default_profile_ix);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300152}
153
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500154static inline bool net_dim_on_top(struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300155{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500156 switch (dim->tune_state) {
157 case NET_DIM_PARKING_ON_TOP:
158 case NET_DIM_PARKING_TIRED:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300159 return true;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500160 case NET_DIM_GOING_RIGHT:
161 return (dim->steps_left > 1) && (dim->steps_right == 1);
162 default: /* NET_DIM_GOING_LEFT */
163 return (dim->steps_right > 1) && (dim->steps_left == 1);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300164 }
165}
166
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500167static inline void net_dim_turn(struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300168{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500169 switch (dim->tune_state) {
170 case NET_DIM_PARKING_ON_TOP:
171 case NET_DIM_PARKING_TIRED:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300172 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500173 case NET_DIM_GOING_RIGHT:
174 dim->tune_state = NET_DIM_GOING_LEFT;
175 dim->steps_left = 0;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300176 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500177 case NET_DIM_GOING_LEFT:
178 dim->tune_state = NET_DIM_GOING_RIGHT;
179 dim->steps_right = 0;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300180 break;
181 }
182}
183
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500184static inline int net_dim_step(struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300185{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500186 if (dim->tired == (NET_DIM_PARAMS_NUM_PROFILES * 2))
187 return NET_DIM_TOO_TIRED;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300188
Andy Gospodarek9a317422018-01-09 16:06:17 -0500189 switch (dim->tune_state) {
190 case NET_DIM_PARKING_ON_TOP:
191 case NET_DIM_PARKING_TIRED:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300192 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500193 case NET_DIM_GOING_RIGHT:
194 if (dim->profile_ix == (NET_DIM_PARAMS_NUM_PROFILES - 1))
195 return NET_DIM_ON_EDGE;
196 dim->profile_ix++;
197 dim->steps_right++;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300198 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500199 case NET_DIM_GOING_LEFT:
200 if (dim->profile_ix == 0)
201 return NET_DIM_ON_EDGE;
202 dim->profile_ix--;
203 dim->steps_left++;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300204 break;
205 }
206
Andy Gospodarek9a317422018-01-09 16:06:17 -0500207 dim->tired++;
208 return NET_DIM_STEPPED;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300209}
210
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500211static inline void net_dim_park_on_top(struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300212{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500213 dim->steps_right = 0;
214 dim->steps_left = 0;
215 dim->tired = 0;
216 dim->tune_state = NET_DIM_PARKING_ON_TOP;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300217}
218
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500219static inline void net_dim_park_tired(struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300220{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500221 dim->steps_right = 0;
222 dim->steps_left = 0;
223 dim->tune_state = NET_DIM_PARKING_TIRED;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300224}
225
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500226static inline void net_dim_exit_parking(struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300227{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500228 dim->tune_state = dim->profile_ix ? NET_DIM_GOING_LEFT :
229 NET_DIM_GOING_RIGHT;
230 net_dim_step(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300231}
232
Tal Gilboac3164d22017-05-15 14:13:16 +0300233#define IS_SIGNIFICANT_DIFF(val, ref) \
234 (((100 * abs((val) - (ref))) / (ref)) > 10) /* more than 10% difference */
235
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500236static inline int net_dim_stats_compare(struct net_dim_stats *curr,
237 struct net_dim_stats *prev)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300238{
Tal Gilboac3164d22017-05-15 14:13:16 +0300239 if (!prev->bpms)
Andy Gospodarek9a317422018-01-09 16:06:17 -0500240 return curr->bpms ? NET_DIM_STATS_BETTER :
241 NET_DIM_STATS_SAME;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300242
Tal Gilboac3164d22017-05-15 14:13:16 +0300243 if (IS_SIGNIFICANT_DIFF(curr->bpms, prev->bpms))
Andy Gospodarek9a317422018-01-09 16:06:17 -0500244 return (curr->bpms > prev->bpms) ? NET_DIM_STATS_BETTER :
245 NET_DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300246
Talat Batheesh51656742018-01-17 23:13:16 +0200247 if (!prev->ppms)
248 return curr->ppms ? NET_DIM_STATS_BETTER :
249 NET_DIM_STATS_SAME;
250
Tal Gilboac3164d22017-05-15 14:13:16 +0300251 if (IS_SIGNIFICANT_DIFF(curr->ppms, prev->ppms))
Andy Gospodarek9a317422018-01-09 16:06:17 -0500252 return (curr->ppms > prev->ppms) ? NET_DIM_STATS_BETTER :
253 NET_DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300254
Talat Batheesh51656742018-01-17 23:13:16 +0200255 if (!prev->epms)
256 return NET_DIM_STATS_SAME;
257
Tal Gilboac3164d22017-05-15 14:13:16 +0300258 if (IS_SIGNIFICANT_DIFF(curr->epms, prev->epms))
Andy Gospodarek9a317422018-01-09 16:06:17 -0500259 return (curr->epms < prev->epms) ? NET_DIM_STATS_BETTER :
260 NET_DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300261
Andy Gospodarek9a317422018-01-09 16:06:17 -0500262 return NET_DIM_STATS_SAME;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300263}
264
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500265static inline bool net_dim_decision(struct net_dim_stats *curr_stats,
266 struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300267{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500268 int prev_state = dim->tune_state;
269 int prev_ix = dim->profile_ix;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300270 int stats_res;
271 int step_res;
272
Andy Gospodarek9a317422018-01-09 16:06:17 -0500273 switch (dim->tune_state) {
274 case NET_DIM_PARKING_ON_TOP:
275 stats_res = net_dim_stats_compare(curr_stats, &dim->prev_stats);
276 if (stats_res != NET_DIM_STATS_SAME)
277 net_dim_exit_parking(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300278 break;
279
Andy Gospodarek9a317422018-01-09 16:06:17 -0500280 case NET_DIM_PARKING_TIRED:
281 dim->tired--;
282 if (!dim->tired)
283 net_dim_exit_parking(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300284 break;
285
Andy Gospodarek9a317422018-01-09 16:06:17 -0500286 case NET_DIM_GOING_RIGHT:
287 case NET_DIM_GOING_LEFT:
288 stats_res = net_dim_stats_compare(curr_stats, &dim->prev_stats);
289 if (stats_res != NET_DIM_STATS_BETTER)
290 net_dim_turn(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300291
Andy Gospodarek9a317422018-01-09 16:06:17 -0500292 if (net_dim_on_top(dim)) {
293 net_dim_park_on_top(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300294 break;
295 }
296
Andy Gospodarek9a317422018-01-09 16:06:17 -0500297 step_res = net_dim_step(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300298 switch (step_res) {
Andy Gospodarek9a317422018-01-09 16:06:17 -0500299 case NET_DIM_ON_EDGE:
300 net_dim_park_on_top(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300301 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500302 case NET_DIM_TOO_TIRED:
303 net_dim_park_tired(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300304 break;
305 }
306
307 break;
308 }
309
Andy Gospodarek9a317422018-01-09 16:06:17 -0500310 if ((prev_state != NET_DIM_PARKING_ON_TOP) ||
311 (dim->tune_state != NET_DIM_PARKING_ON_TOP))
312 dim->prev_stats = *curr_stats;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300313
Andy Gospodarek9a317422018-01-09 16:06:17 -0500314 return dim->profile_ix != prev_ix;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300315}
316
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500317static inline void net_dim_sample(u16 event_ctr,
318 u64 packets,
319 u64 bytes,
320 struct net_dim_sample *s)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300321{
322 s->time = ktime_get();
Andy Gospodarek138968e2018-01-09 16:06:14 -0500323 s->pkt_ctr = packets;
324 s->byte_ctr = bytes;
325 s->event_ctr = event_ctr;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300326}
327
Andy Gospodarek9a317422018-01-09 16:06:17 -0500328#define NET_DIM_NEVENTS 64
Tal Gilboa53acd762017-05-29 17:02:55 +0300329#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
330#define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) & (BIT_ULL(bits) - 1))
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300331
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500332static inline void net_dim_calc_stats(struct net_dim_sample *start,
333 struct net_dim_sample *end,
334 struct net_dim_stats *curr_stats)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300335{
336 /* u32 holds up to 71 minutes, should be enough */
337 u32 delta_us = ktime_us_delta(end->time, start->time);
Tal Gilboa53acd762017-05-29 17:02:55 +0300338 u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr);
339 u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr,
340 start->byte_ctr);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300341
Gil Rockah0bbcc0a2017-01-10 22:33:38 +0200342 if (!delta_us)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300343 return;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300344
Tal Gilboac3164d22017-05-15 14:13:16 +0300345 curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
346 curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
Andy Gospodarek9a317422018-01-09 16:06:17 -0500347 curr_stats->epms = DIV_ROUND_UP(NET_DIM_NEVENTS * USEC_PER_MSEC,
Tal Gilboac3164d22017-05-15 14:13:16 +0300348 delta_us);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300349}
350
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500351static inline void net_dim(struct net_dim *dim,
Andy Gospodarek8115b752018-01-09 16:06:19 -0500352 struct net_dim_sample end_sample)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300353{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500354 struct net_dim_stats curr_stats;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300355 u16 nevents;
356
Andy Gospodarek9a317422018-01-09 16:06:17 -0500357 switch (dim->state) {
358 case NET_DIM_MEASURE_IN_PROGRESS:
Andy Gospodarek8115b752018-01-09 16:06:19 -0500359 nevents = BIT_GAP(BITS_PER_TYPE(u16),
360 end_sample.event_ctr,
Andy Gospodarek9a317422018-01-09 16:06:17 -0500361 dim->start_sample.event_ctr);
362 if (nevents < NET_DIM_NEVENTS)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300363 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500364 net_dim_calc_stats(&dim->start_sample, &end_sample,
365 &curr_stats);
366 if (net_dim_decision(&curr_stats, dim)) {
367 dim->state = NET_DIM_APPLY_NEW_PROFILE;
368 schedule_work(&dim->work);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300369 break;
370 }
371 /* fall through */
Andy Gospodarek9a317422018-01-09 16:06:17 -0500372 case NET_DIM_START_MEASURE:
Andy Gospodarek9a317422018-01-09 16:06:17 -0500373 dim->state = NET_DIM_MEASURE_IN_PROGRESS;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300374 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500375 case NET_DIM_APPLY_NEW_PROFILE:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300376 break;
377 }
378}
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500379
380#endif /* NET_DIM_H */