blob: 741510fb74cb99aa61f1cc1cc1f44da005a3d83b [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
Tal Gilboac3164d22017-05-15 14:13:16 +0300247 if (IS_SIGNIFICANT_DIFF(curr->ppms, prev->ppms))
Andy Gospodarek9a317422018-01-09 16:06:17 -0500248 return (curr->ppms > prev->ppms) ? NET_DIM_STATS_BETTER :
249 NET_DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300250
Tal Gilboac3164d22017-05-15 14:13:16 +0300251 if (IS_SIGNIFICANT_DIFF(curr->epms, prev->epms))
Andy Gospodarek9a317422018-01-09 16:06:17 -0500252 return (curr->epms < prev->epms) ? NET_DIM_STATS_BETTER :
253 NET_DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300254
Andy Gospodarek9a317422018-01-09 16:06:17 -0500255 return NET_DIM_STATS_SAME;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300256}
257
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500258static inline bool net_dim_decision(struct net_dim_stats *curr_stats,
259 struct net_dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300260{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500261 int prev_state = dim->tune_state;
262 int prev_ix = dim->profile_ix;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300263 int stats_res;
264 int step_res;
265
Andy Gospodarek9a317422018-01-09 16:06:17 -0500266 switch (dim->tune_state) {
267 case NET_DIM_PARKING_ON_TOP:
268 stats_res = net_dim_stats_compare(curr_stats, &dim->prev_stats);
269 if (stats_res != NET_DIM_STATS_SAME)
270 net_dim_exit_parking(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300271 break;
272
Andy Gospodarek9a317422018-01-09 16:06:17 -0500273 case NET_DIM_PARKING_TIRED:
274 dim->tired--;
275 if (!dim->tired)
276 net_dim_exit_parking(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300277 break;
278
Andy Gospodarek9a317422018-01-09 16:06:17 -0500279 case NET_DIM_GOING_RIGHT:
280 case NET_DIM_GOING_LEFT:
281 stats_res = net_dim_stats_compare(curr_stats, &dim->prev_stats);
282 if (stats_res != NET_DIM_STATS_BETTER)
283 net_dim_turn(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300284
Andy Gospodarek9a317422018-01-09 16:06:17 -0500285 if (net_dim_on_top(dim)) {
286 net_dim_park_on_top(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300287 break;
288 }
289
Andy Gospodarek9a317422018-01-09 16:06:17 -0500290 step_res = net_dim_step(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300291 switch (step_res) {
Andy Gospodarek9a317422018-01-09 16:06:17 -0500292 case NET_DIM_ON_EDGE:
293 net_dim_park_on_top(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300294 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500295 case NET_DIM_TOO_TIRED:
296 net_dim_park_tired(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300297 break;
298 }
299
300 break;
301 }
302
Andy Gospodarek9a317422018-01-09 16:06:17 -0500303 if ((prev_state != NET_DIM_PARKING_ON_TOP) ||
304 (dim->tune_state != NET_DIM_PARKING_ON_TOP))
305 dim->prev_stats = *curr_stats;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300306
Andy Gospodarek9a317422018-01-09 16:06:17 -0500307 return dim->profile_ix != prev_ix;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300308}
309
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500310static inline void net_dim_sample(u16 event_ctr,
311 u64 packets,
312 u64 bytes,
313 struct net_dim_sample *s)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300314{
315 s->time = ktime_get();
Andy Gospodarek138968e2018-01-09 16:06:14 -0500316 s->pkt_ctr = packets;
317 s->byte_ctr = bytes;
318 s->event_ctr = event_ctr;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300319}
320
Andy Gospodarek9a317422018-01-09 16:06:17 -0500321#define NET_DIM_NEVENTS 64
Tal Gilboa53acd762017-05-29 17:02:55 +0300322#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
323#define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) & (BIT_ULL(bits) - 1))
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300324
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500325static inline void net_dim_calc_stats(struct net_dim_sample *start,
326 struct net_dim_sample *end,
327 struct net_dim_stats *curr_stats)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300328{
329 /* u32 holds up to 71 minutes, should be enough */
330 u32 delta_us = ktime_us_delta(end->time, start->time);
Tal Gilboa53acd762017-05-29 17:02:55 +0300331 u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr);
332 u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr,
333 start->byte_ctr);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300334
Gil Rockah0bbcc0a2017-01-10 22:33:38 +0200335 if (!delta_us)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300336 return;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300337
Tal Gilboac3164d22017-05-15 14:13:16 +0300338 curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
339 curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
Andy Gospodarek9a317422018-01-09 16:06:17 -0500340 curr_stats->epms = DIV_ROUND_UP(NET_DIM_NEVENTS * USEC_PER_MSEC,
Tal Gilboac3164d22017-05-15 14:13:16 +0300341 delta_us);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300342}
343
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500344static inline void net_dim(struct net_dim *dim,
345 u16 event_ctr,
346 u64 packets,
347 u64 bytes)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300348{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500349 struct net_dim_sample end_sample;
350 struct net_dim_stats curr_stats;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300351 u16 nevents;
352
Andy Gospodarek9a317422018-01-09 16:06:17 -0500353 switch (dim->state) {
354 case NET_DIM_MEASURE_IN_PROGRESS:
Andy Gospodarek138968e2018-01-09 16:06:14 -0500355 nevents = BIT_GAP(BITS_PER_TYPE(u16), event_ctr,
Andy Gospodarek9a317422018-01-09 16:06:17 -0500356 dim->start_sample.event_ctr);
357 if (nevents < NET_DIM_NEVENTS)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300358 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500359 net_dim_sample(event_ctr, packets, bytes, &end_sample);
360 net_dim_calc_stats(&dim->start_sample, &end_sample,
361 &curr_stats);
362 if (net_dim_decision(&curr_stats, dim)) {
363 dim->state = NET_DIM_APPLY_NEW_PROFILE;
364 schedule_work(&dim->work);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300365 break;
366 }
367 /* fall through */
Andy Gospodarek9a317422018-01-09 16:06:17 -0500368 case NET_DIM_START_MEASURE:
369 net_dim_sample(event_ctr, packets, bytes, &dim->start_sample);
370 dim->state = NET_DIM_MEASURE_IN_PROGRESS;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300371 break;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500372 case NET_DIM_APPLY_NEW_PROFILE:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300373 break;
374 }
375}
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500376
377#endif /* NET_DIM_H */