blob: 4e009ec193ef6d13e761ec41e8f30cec1065511f [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>
Tal Gilboa0e589832018-11-04 19:07:02 +020038#include <linux/dim.h>
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030039
Andy Gospodarek9a317422018-01-09 16:06:17 -050040#define NET_DIM_PARAMS_NUM_PROFILES 5
Tal Gilboa0e589832018-11-04 19:07:02 +020041/* Netdev dynamic interrupt moderation profiles */
Andy Gospodarek9a317422018-01-09 16:06:17 -050042#define NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE 256
Tal Gilboa623ad752018-04-24 13:36:02 +030043#define NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE 128
Andy Gospodarek9a317422018-01-09 16:06:17 -050044#define NET_DIM_DEF_PROFILE_CQE 1
45#define NET_DIM_DEF_PROFILE_EQE 1
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030046
Andy Gospodarek9a317422018-01-09 16:06:17 -050047/* All profiles sizes must be NET_PARAMS_DIM_NUM_PROFILES */
Tal Gilboa623ad752018-04-24 13:36:02 +030048#define NET_DIM_RX_EQE_PROFILES { \
Andy Gospodarek9a317422018-01-09 16:06:17 -050049 {1, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
50 {8, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
51 {64, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
52 {128, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
53 {256, NET_DIM_DEFAULT_RX_CQ_MODERATION_PKTS_FROM_EQE}, \
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030054}
55
Tal Gilboa623ad752018-04-24 13:36:02 +030056#define NET_DIM_RX_CQE_PROFILES { \
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030057 {2, 256}, \
58 {8, 128}, \
59 {16, 64}, \
60 {32, 64}, \
61 {64, 64} \
62}
63
Tal Gilboa623ad752018-04-24 13:36:02 +030064#define NET_DIM_TX_EQE_PROFILES { \
65 {1, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
66 {8, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
67 {32, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
68 {64, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE}, \
69 {128, NET_DIM_DEFAULT_TX_CQ_MODERATION_PKTS_FROM_EQE} \
70}
71
72#define NET_DIM_TX_CQE_PROFILES { \
73 {5, 128}, \
74 {8, 64}, \
75 {16, 32}, \
76 {32, 32}, \
77 {64, 32} \
78}
79
Tal Gilboa8960b382019-01-31 16:44:48 +020080static const struct dim_cq_moder
Tal Gilboac002bd52018-11-05 12:07:52 +020081rx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
Tal Gilboa623ad752018-04-24 13:36:02 +030082 NET_DIM_RX_EQE_PROFILES,
83 NET_DIM_RX_CQE_PROFILES,
84};
85
Tal Gilboa8960b382019-01-31 16:44:48 +020086static const struct dim_cq_moder
Tal Gilboac002bd52018-11-05 12:07:52 +020087tx_profile[DIM_CQ_PERIOD_NUM_MODES][NET_DIM_PARAMS_NUM_PROFILES] = {
Tal Gilboa623ad752018-04-24 13:36:02 +030088 NET_DIM_TX_EQE_PROFILES,
89 NET_DIM_TX_CQE_PROFILES,
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030090};
91
Tal Gilboa8960b382019-01-31 16:44:48 +020092static inline struct dim_cq_moder
Tal Gilboa026a8072018-04-24 13:36:01 +030093net_dim_get_rx_moderation(u8 cq_period_mode, int ix)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030094{
Tal Gilboa8960b382019-01-31 16:44:48 +020095 struct dim_cq_moder cq_moder = rx_profile[cq_period_mode][ix];
Tal Gilboa0088cbb2017-09-26 16:20:43 +030096
Tal Gilboa0088cbb2017-09-26 16:20:43 +030097 cq_moder.cq_period_mode = cq_period_mode;
98 return cq_moder;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +030099}
100
Tal Gilboa8960b382019-01-31 16:44:48 +0200101static inline struct dim_cq_moder
Tal Gilboa623ad752018-04-24 13:36:02 +0300102net_dim_get_def_rx_moderation(u8 cq_period_mode)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300103{
Tal Gilboac002bd52018-11-05 12:07:52 +0200104 u8 profile_ix = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
Tal Gilboa623ad752018-04-24 13:36:02 +0300105 NET_DIM_DEF_PROFILE_CQE : NET_DIM_DEF_PROFILE_EQE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300106
Tal Gilboa623ad752018-04-24 13:36:02 +0300107 return net_dim_get_rx_moderation(cq_period_mode, profile_ix);
108}
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300109
Tal Gilboa8960b382019-01-31 16:44:48 +0200110static inline struct dim_cq_moder
Tal Gilboa623ad752018-04-24 13:36:02 +0300111net_dim_get_tx_moderation(u8 cq_period_mode, int ix)
112{
Tal Gilboa8960b382019-01-31 16:44:48 +0200113 struct dim_cq_moder cq_moder = tx_profile[cq_period_mode][ix];
Tal Gilboa623ad752018-04-24 13:36:02 +0300114
115 cq_moder.cq_period_mode = cq_period_mode;
116 return cq_moder;
117}
118
Tal Gilboa8960b382019-01-31 16:44:48 +0200119static inline struct dim_cq_moder
Tal Gilboa623ad752018-04-24 13:36:02 +0300120net_dim_get_def_tx_moderation(u8 cq_period_mode)
121{
Tal Gilboac002bd52018-11-05 12:07:52 +0200122 u8 profile_ix = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ?
Tal Gilboa623ad752018-04-24 13:36:02 +0300123 NET_DIM_DEF_PROFILE_CQE : NET_DIM_DEF_PROFILE_EQE;
124
125 return net_dim_get_tx_moderation(cq_period_mode, profile_ix);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300126}
127
Tal Gilboa8960b382019-01-31 16:44:48 +0200128static inline int net_dim_step(struct dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300129{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500130 if (dim->tired == (NET_DIM_PARAMS_NUM_PROFILES * 2))
Tal Gilboa449986e2018-11-05 11:57:10 +0200131 return DIM_TOO_TIRED;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300132
Andy Gospodarek9a317422018-01-09 16:06:17 -0500133 switch (dim->tune_state) {
Tal Gilboa449986e2018-11-05 11:57:10 +0200134 case DIM_PARKING_ON_TOP:
135 case DIM_PARKING_TIRED:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300136 break;
Tal Gilboa449986e2018-11-05 11:57:10 +0200137 case DIM_GOING_RIGHT:
Andy Gospodarek9a317422018-01-09 16:06:17 -0500138 if (dim->profile_ix == (NET_DIM_PARAMS_NUM_PROFILES - 1))
Tal Gilboa449986e2018-11-05 11:57:10 +0200139 return DIM_ON_EDGE;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500140 dim->profile_ix++;
141 dim->steps_right++;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300142 break;
Tal Gilboa449986e2018-11-05 11:57:10 +0200143 case DIM_GOING_LEFT:
Andy Gospodarek9a317422018-01-09 16:06:17 -0500144 if (dim->profile_ix == 0)
Tal Gilboa449986e2018-11-05 11:57:10 +0200145 return DIM_ON_EDGE;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500146 dim->profile_ix--;
147 dim->steps_left++;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300148 break;
149 }
150
Andy Gospodarek9a317422018-01-09 16:06:17 -0500151 dim->tired++;
Tal Gilboa449986e2018-11-05 11:57:10 +0200152 return DIM_STEPPED;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300153}
154
Tal Gilboa8960b382019-01-31 16:44:48 +0200155static inline void net_dim_exit_parking(struct dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300156{
Tal Gilboa449986e2018-11-05 11:57:10 +0200157 dim->tune_state = dim->profile_ix ? DIM_GOING_LEFT :
158 DIM_GOING_RIGHT;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500159 net_dim_step(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300160}
161
Tal Gilboa449986e2018-11-05 11:57:10 +0200162static inline int net_dim_stats_compare(struct dim_stats *curr,
163 struct dim_stats *prev)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300164{
Tal Gilboac3164d22017-05-15 14:13:16 +0300165 if (!prev->bpms)
Tal Gilboa449986e2018-11-05 11:57:10 +0200166 return curr->bpms ? DIM_STATS_BETTER :
167 DIM_STATS_SAME;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300168
Tal Gilboac3164d22017-05-15 14:13:16 +0300169 if (IS_SIGNIFICANT_DIFF(curr->bpms, prev->bpms))
Tal Gilboa449986e2018-11-05 11:57:10 +0200170 return (curr->bpms > prev->bpms) ? DIM_STATS_BETTER :
171 DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300172
Talat Batheesh51656742018-01-17 23:13:16 +0200173 if (!prev->ppms)
Tal Gilboa449986e2018-11-05 11:57:10 +0200174 return curr->ppms ? DIM_STATS_BETTER :
175 DIM_STATS_SAME;
Talat Batheesh51656742018-01-17 23:13:16 +0200176
Tal Gilboac3164d22017-05-15 14:13:16 +0300177 if (IS_SIGNIFICANT_DIFF(curr->ppms, prev->ppms))
Tal Gilboa449986e2018-11-05 11:57:10 +0200178 return (curr->ppms > prev->ppms) ? DIM_STATS_BETTER :
179 DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300180
Talat Batheesh51656742018-01-17 23:13:16 +0200181 if (!prev->epms)
Tal Gilboa449986e2018-11-05 11:57:10 +0200182 return DIM_STATS_SAME;
Talat Batheesh51656742018-01-17 23:13:16 +0200183
Tal Gilboac3164d22017-05-15 14:13:16 +0300184 if (IS_SIGNIFICANT_DIFF(curr->epms, prev->epms))
Tal Gilboa449986e2018-11-05 11:57:10 +0200185 return (curr->epms < prev->epms) ? DIM_STATS_BETTER :
186 DIM_STATS_WORSE;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300187
Tal Gilboa449986e2018-11-05 11:57:10 +0200188 return DIM_STATS_SAME;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300189}
190
Tal Gilboa449986e2018-11-05 11:57:10 +0200191static inline bool net_dim_decision(struct dim_stats *curr_stats,
Tal Gilboa8960b382019-01-31 16:44:48 +0200192 struct dim *dim)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300193{
Andy Gospodarek9a317422018-01-09 16:06:17 -0500194 int prev_state = dim->tune_state;
195 int prev_ix = dim->profile_ix;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300196 int stats_res;
197 int step_res;
198
Andy Gospodarek9a317422018-01-09 16:06:17 -0500199 switch (dim->tune_state) {
Tal Gilboa449986e2018-11-05 11:57:10 +0200200 case DIM_PARKING_ON_TOP:
Andy Gospodarek9a317422018-01-09 16:06:17 -0500201 stats_res = net_dim_stats_compare(curr_stats, &dim->prev_stats);
Tal Gilboa449986e2018-11-05 11:57:10 +0200202 if (stats_res != DIM_STATS_SAME)
Andy Gospodarek9a317422018-01-09 16:06:17 -0500203 net_dim_exit_parking(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300204 break;
205
Tal Gilboa449986e2018-11-05 11:57:10 +0200206 case DIM_PARKING_TIRED:
Andy Gospodarek9a317422018-01-09 16:06:17 -0500207 dim->tired--;
208 if (!dim->tired)
209 net_dim_exit_parking(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300210 break;
211
Tal Gilboa449986e2018-11-05 11:57:10 +0200212 case DIM_GOING_RIGHT:
213 case DIM_GOING_LEFT:
Andy Gospodarek9a317422018-01-09 16:06:17 -0500214 stats_res = net_dim_stats_compare(curr_stats, &dim->prev_stats);
Tal Gilboa449986e2018-11-05 11:57:10 +0200215 if (stats_res != DIM_STATS_BETTER)
216 dim_turn(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300217
Tal Gilboa449986e2018-11-05 11:57:10 +0200218 if (dim_on_top(dim)) {
219 dim_park_on_top(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300220 break;
221 }
222
Andy Gospodarek9a317422018-01-09 16:06:17 -0500223 step_res = net_dim_step(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300224 switch (step_res) {
Tal Gilboa449986e2018-11-05 11:57:10 +0200225 case DIM_ON_EDGE:
226 dim_park_on_top(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300227 break;
Tal Gilboa449986e2018-11-05 11:57:10 +0200228 case DIM_TOO_TIRED:
229 dim_park_tired(dim);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300230 break;
231 }
232
233 break;
234 }
235
Tal Gilboa449986e2018-11-05 11:57:10 +0200236 if (prev_state != DIM_PARKING_ON_TOP ||
237 dim->tune_state != DIM_PARKING_ON_TOP)
Andy Gospodarek9a317422018-01-09 16:06:17 -0500238 dim->prev_stats = *curr_stats;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300239
Andy Gospodarek9a317422018-01-09 16:06:17 -0500240 return dim->profile_ix != prev_ix;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300241}
242
Tal Gilboa8960b382019-01-31 16:44:48 +0200243static inline void net_dim(struct dim *dim,
244 struct dim_sample end_sample)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300245{
Tal Gilboa449986e2018-11-05 11:57:10 +0200246 struct dim_stats curr_stats;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300247 u16 nevents;
248
Andy Gospodarek9a317422018-01-09 16:06:17 -0500249 switch (dim->state) {
Tal Gilboac002bd52018-11-05 12:07:52 +0200250 case DIM_MEASURE_IN_PROGRESS:
Andy Gospodarek8115b752018-01-09 16:06:19 -0500251 nevents = BIT_GAP(BITS_PER_TYPE(u16),
252 end_sample.event_ctr,
Andy Gospodarek9a317422018-01-09 16:06:17 -0500253 dim->start_sample.event_ctr);
Tal Gilboa449986e2018-11-05 11:57:10 +0200254 if (nevents < DIM_NEVENTS)
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300255 break;
Tal Gilboa449986e2018-11-05 11:57:10 +0200256 dim_calc_stats(&dim->start_sample, &end_sample, &curr_stats);
Andy Gospodarek9a317422018-01-09 16:06:17 -0500257 if (net_dim_decision(&curr_stats, dim)) {
Tal Gilboac002bd52018-11-05 12:07:52 +0200258 dim->state = DIM_APPLY_NEW_PROFILE;
Andy Gospodarek9a317422018-01-09 16:06:17 -0500259 schedule_work(&dim->work);
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300260 break;
261 }
262 /* fall through */
Tal Gilboac002bd52018-11-05 12:07:52 +0200263 case DIM_START_MEASURE:
Tal Gilboa8960b382019-01-31 16:44:48 +0200264 dim_update_sample(end_sample.event_ctr, end_sample.pkt_ctr,
265 end_sample.byte_ctr, &dim->start_sample);
Tal Gilboac002bd52018-11-05 12:07:52 +0200266 dim->state = DIM_MEASURE_IN_PROGRESS;
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300267 break;
Tal Gilboac002bd52018-11-05 12:07:52 +0200268 case DIM_APPLY_NEW_PROFILE:
Gil Rockahcb3c7fd2016-06-23 17:02:41 +0300269 break;
270 }
271}
Andy Gospodarek4c4dbb42018-01-09 16:06:18 -0500272
273#endif /* NET_DIM_H */