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/bits.h> 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/types.h> 11 #include <linux/workqueue.h> 12 13 struct net_device; 14 15 /* Number of DIM profiles and period mode. */ 16 #define NET_DIM_PARAMS_NUM_PROFILES 5 17 #define NET_DIM_DEFAULT_RX_CQ_PKTS_FROM_EQE 256 18 #define NET_DIM_DEFAULT_TX_CQ_PKTS_FROM_EQE 128 19 #define NET_DIM_DEF_PROFILE_CQE 1 20 #define NET_DIM_DEF_PROFILE_EQE 1 21 22 /* 23 * Number of events between DIM iterations. 24 * Causes a moderation of the algorithm run. 25 */ 26 #define DIM_NEVENTS 64 27 28 /* 29 * Is a difference between values justifies taking an action. 30 * We consider 10% difference as significant. 31 */ 32 #define IS_SIGNIFICANT_DIFF(val, ref) \ 33 ((ref) && (((100UL * abs((val) - (ref))) / (ref)) > 10)) 34 35 /* 36 * Calculate the gap between two values. 37 * Take wrap-around and variable size into consideration. 38 */ 39 #define BIT_GAP(bits, end, start) ((((end) - (start)) + BIT_ULL(bits)) \ 40 & (BIT_ULL(bits) - 1)) 41 42 /** 43 * struct dim_cq_moder - Structure for CQ moderation values. 44 * Used for communications between DIM and its consumer. 45 * 46 * @usec: CQ timer suggestion (by DIM) 47 * @pkts: CQ packet counter suggestion (by DIM) 48 * @comps: Completion counter 49 * @cq_period_mode: CQ period count mode (from CQE/EQE) 50 * @rcu: for asynchronous kfree_rcu 51 */ 52 struct dim_cq_moder { 53 u16 usec; 54 u16 pkts; 55 u16 comps; 56 u8 cq_period_mode; 57 struct rcu_head rcu; 58 }; 59 60 #define DIM_PROFILE_RX BIT(0) /* support rx profile modification */ 61 #define DIM_PROFILE_TX BIT(1) /* support tx profile modification */ 62 63 #define DIM_COALESCE_USEC BIT(0) /* support usec field modification */ 64 #define DIM_COALESCE_PKTS BIT(1) /* support pkts field modification */ 65 #define DIM_COALESCE_COMPS BIT(2) /* support comps field modification */ 66 67 /** 68 * struct dim_irq_moder - Structure for irq moderation information. 69 * Used to collect irq moderation related information. 70 * 71 * @profile_flags: DIM_PROFILE_* 72 * @coal_flags: DIM_COALESCE_* for Rx and Tx 73 * @dim_rx_mode: Rx DIM period count mode: CQE or EQE 74 * @dim_tx_mode: Tx DIM period count mode: CQE or EQE 75 * @rx_profile: DIM profile list for Rx 76 * @tx_profile: DIM profile list for Tx 77 * @rx_dim_work: Rx DIM worker scheduled by net_dim() 78 * @tx_dim_work: Tx DIM worker scheduled by net_dim() 79 */ 80 struct dim_irq_moder { 81 u8 profile_flags; 82 u8 coal_flags; 83 u8 dim_rx_mode; 84 u8 dim_tx_mode; 85 struct dim_cq_moder __rcu *rx_profile; 86 struct dim_cq_moder __rcu *tx_profile; 87 void (*rx_dim_work)(struct work_struct *work); 88 void (*tx_dim_work)(struct work_struct *work); 89 }; 90 91 /** 92 * struct dim_sample - Structure for DIM sample data. 93 * Used for communications between DIM and its consumer. 94 * 95 * @time: Sample timestamp 96 * @pkt_ctr: Number of packets 97 * @byte_ctr: Number of bytes 98 * @event_ctr: Number of events 99 * @comp_ctr: Current completion counter 100 */ 101 struct dim_sample { 102 ktime_t time; 103 u32 pkt_ctr; 104 u32 byte_ctr; 105 u16 event_ctr; 106 u32 comp_ctr; 107 }; 108 109 /** 110 * struct dim_stats - Structure for DIM stats. 111 * Used for holding current measured rates. 112 * 113 * @ppms: Packets per msec 114 * @bpms: Bytes per msec 115 * @epms: Events per msec 116 * @cpms: Completions per msec 117 * @cpe_ratio: Ratio of completions to events 118 */ 119 struct dim_stats { 120 int ppms; /* packets per msec */ 121 int bpms; /* bytes per msec */ 122 int epms; /* events per msec */ 123 int cpms; /* completions per msec */ 124 int cpe_ratio; /* ratio of completions to events */ 125 }; 126 127 /** 128 * struct dim - Main structure for dynamic interrupt moderation (DIM). 129 * Used for holding all information about a specific DIM instance. 130 * 131 * @state: Algorithm state (see below) 132 * @prev_stats: Measured rates from previous iteration (for comparison) 133 * @start_sample: Sampled data at start of current iteration 134 * @measuring_sample: A &dim_sample that is used to update the current events 135 * @work: Work to perform on action required 136 * @priv: A pointer to the struct that points to dim 137 * @profile_ix: Current moderation profile 138 * @mode: CQ period count mode 139 * @tune_state: Algorithm tuning state (see below) 140 * @steps_right: Number of steps taken towards higher moderation 141 * @steps_left: Number of steps taken towards lower moderation 142 * @tired: Parking depth counter 143 */ 144 struct dim { 145 u8 state; 146 struct dim_stats prev_stats; 147 struct dim_sample start_sample; 148 struct dim_sample measuring_sample; 149 struct work_struct work; 150 void *priv; 151 u8 profile_ix; 152 u8 mode; 153 u8 tune_state; 154 u8 steps_right; 155 u8 steps_left; 156 u8 tired; 157 }; 158 159 /** 160 * enum dim_cq_period_mode - Modes for CQ period count 161 * 162 * @DIM_CQ_PERIOD_MODE_START_FROM_EQE: Start counting from EQE 163 * @DIM_CQ_PERIOD_MODE_START_FROM_CQE: Start counting from CQE (implies timer reset) 164 * @DIM_CQ_PERIOD_NUM_MODES: Number of modes 165 */ 166 enum dim_cq_period_mode { 167 DIM_CQ_PERIOD_MODE_START_FROM_EQE = 0x0, 168 DIM_CQ_PERIOD_MODE_START_FROM_CQE = 0x1, 169 DIM_CQ_PERIOD_NUM_MODES 170 }; 171 172 /** 173 * enum dim_state - DIM algorithm states 174 * 175 * These will determine if the algorithm is in a valid state to start an iteration. 176 * 177 * @DIM_START_MEASURE: This is the first iteration (also after applying a new profile) 178 * @DIM_MEASURE_IN_PROGRESS: Algorithm is already in progress - check if 179 * need to perform an action 180 * @DIM_APPLY_NEW_PROFILE: DIM consumer is currently applying a profile - no need to measure 181 */ 182 enum dim_state { 183 DIM_START_MEASURE, 184 DIM_MEASURE_IN_PROGRESS, 185 DIM_APPLY_NEW_PROFILE, 186 }; 187 188 /** 189 * enum dim_tune_state - DIM algorithm tune states 190 * 191 * These will determine which action the algorithm should perform. 192 * 193 * @DIM_PARKING_ON_TOP: Algorithm found a local top point - exit on significant difference 194 * @DIM_PARKING_TIRED: Algorithm found a deep top point - don't exit if tired > 0 195 * @DIM_GOING_RIGHT: Algorithm is currently trying higher moderation levels 196 * @DIM_GOING_LEFT: Algorithm is currently trying lower moderation levels 197 */ 198 enum dim_tune_state { 199 DIM_PARKING_ON_TOP, 200 DIM_PARKING_TIRED, 201 DIM_GOING_RIGHT, 202 DIM_GOING_LEFT, 203 }; 204 205 /** 206 * enum dim_stats_state - DIM algorithm statistics states 207 * 208 * These will determine the verdict of current iteration. 209 * 210 * @DIM_STATS_WORSE: Current iteration shows worse performance than before 211 * @DIM_STATS_SAME: Current iteration shows same performance than before 212 * @DIM_STATS_BETTER: Current iteration shows better performance than before 213 */ 214 enum dim_stats_state { 215 DIM_STATS_WORSE, 216 DIM_STATS_SAME, 217 DIM_STATS_BETTER, 218 }; 219 220 /** 221 * enum dim_step_result - DIM algorithm step results 222 * 223 * These describe the result of a step. 224 * 225 * @DIM_STEPPED: Performed a regular step 226 * @DIM_TOO_TIRED: Same kind of step was done multiple times - should go to 227 * tired parking 228 * @DIM_ON_EDGE: Stepped to the most left/right profile 229 */ 230 enum dim_step_result { 231 DIM_STEPPED, 232 DIM_TOO_TIRED, 233 DIM_ON_EDGE, 234 }; 235 236 /** 237 * net_dim_init_irq_moder - collect information to initialize irq moderation 238 * @dev: target network device 239 * @profile_flags: Rx or Tx profile modification capability 240 * @coal_flags: irq moderation params flags 241 * @rx_mode: CQ period mode for Rx 242 * @tx_mode: CQ period mode for Tx 243 * @rx_dim_work: Rx worker called after dim decision 244 * @tx_dim_work: Tx worker called after dim decision 245 * 246 * Return: 0 on success or a negative error code. 247 */ 248 int net_dim_init_irq_moder(struct net_device *dev, u8 profile_flags, 249 u8 coal_flags, u8 rx_mode, u8 tx_mode, 250 void (*rx_dim_work)(struct work_struct *work), 251 void (*tx_dim_work)(struct work_struct *work)); 252 253 /** 254 * net_dim_free_irq_moder - free fields for irq moderation 255 * @dev: target network device 256 */ 257 void net_dim_free_irq_moder(struct net_device *dev); 258 259 /** 260 * dim_on_top - check if current state is a good place to stop (top location) 261 * @dim: DIM context 262 * 263 * Check if current profile is a good place to park at. 264 * This will result in reducing the DIM checks frequency as we assume we 265 * shouldn't probably change profiles, unless traffic pattern wasn't changed. 266 */ 267 bool dim_on_top(struct dim *dim); 268 269 /** 270 * dim_turn - change profile altering direction 271 * @dim: DIM context 272 * 273 * Go left if we were going right and vice-versa. 274 * Do nothing if currently parking. 275 */ 276 void dim_turn(struct dim *dim); 277 278 /** 279 * dim_park_on_top - enter a parking state on a top location 280 * @dim: DIM context 281 * 282 * Enter parking state. 283 * Clear all movement history. 284 */ 285 void dim_park_on_top(struct dim *dim); 286 287 /** 288 * dim_park_tired - enter a tired parking state 289 * @dim: DIM context 290 * 291 * Enter parking state. 292 * Clear all movement history and cause DIM checks frequency to reduce. 293 */ 294 void dim_park_tired(struct dim *dim); 295 296 /** 297 * dim_calc_stats - calculate the difference between two samples 298 * @start: start sample 299 * @end: end sample 300 * @curr_stats: delta between samples 301 * 302 * Calculate the delta between two samples (in data rates). 303 * Takes into consideration counter wrap-around. 304 * Returned boolean indicates whether curr_stats are reliable. 305 */ 306 bool dim_calc_stats(struct dim_sample *start, struct dim_sample *end, 307 struct dim_stats *curr_stats); 308 309 /** 310 * dim_update_sample - set a sample's fields with given values 311 * @event_ctr: number of events to set 312 * @packets: number of packets to set 313 * @bytes: number of bytes to set 314 * @s: DIM sample 315 */ 316 static inline void 317 dim_update_sample(u16 event_ctr, u64 packets, u64 bytes, struct dim_sample *s) 318 { 319 s->time = ktime_get(); 320 s->pkt_ctr = packets; 321 s->byte_ctr = bytes; 322 s->event_ctr = event_ctr; 323 } 324 325 /** 326 * dim_update_sample_with_comps - set a sample's fields with given 327 * values including the completion parameter 328 * @event_ctr: number of events to set 329 * @packets: number of packets to set 330 * @bytes: number of bytes to set 331 * @comps: number of completions to set 332 * @s: DIM sample 333 */ 334 static inline void 335 dim_update_sample_with_comps(u16 event_ctr, u64 packets, u64 bytes, u64 comps, 336 struct dim_sample *s) 337 { 338 dim_update_sample(event_ctr, packets, bytes, s); 339 s->comp_ctr = comps; 340 } 341 342 /* Net DIM */ 343 344 /** 345 * net_dim_get_rx_moderation - provide a CQ moderation object for the given RX profile 346 * @cq_period_mode: CQ period mode 347 * @ix: Profile index 348 */ 349 struct dim_cq_moder net_dim_get_rx_moderation(u8 cq_period_mode, int ix); 350 351 /** 352 * net_dim_get_def_rx_moderation - provide the default RX moderation 353 * @cq_period_mode: CQ period mode 354 */ 355 struct dim_cq_moder net_dim_get_def_rx_moderation(u8 cq_period_mode); 356 357 /** 358 * net_dim_get_tx_moderation - provide a CQ moderation object for the given TX profile 359 * @cq_period_mode: CQ period mode 360 * @ix: Profile index 361 */ 362 struct dim_cq_moder net_dim_get_tx_moderation(u8 cq_period_mode, int ix); 363 364 /** 365 * net_dim_get_def_tx_moderation - provide the default TX moderation 366 * @cq_period_mode: CQ period mode 367 */ 368 struct dim_cq_moder net_dim_get_def_tx_moderation(u8 cq_period_mode); 369 370 /** 371 * net_dim - main DIM algorithm entry point 372 * @dim: DIM instance information 373 * @end_sample: Current data measurement 374 * 375 * Called by the consumer. 376 * This is the main logic of the algorithm, where data is processed in order 377 * to decide on next required action. 378 */ 379 void net_dim(struct dim *dim, struct dim_sample end_sample); 380 381 /* RDMA DIM */ 382 383 /* 384 * RDMA DIM profile: 385 * profile size must be of RDMA_DIM_PARAMS_NUM_PROFILES. 386 */ 387 #define RDMA_DIM_PARAMS_NUM_PROFILES 9 388 #define RDMA_DIM_START_PROFILE 0 389 390 /** 391 * rdma_dim - Runs the adaptive moderation. 392 * @dim: The moderation struct. 393 * @completions: The number of completions collected in this round. 394 * 395 * Each call to rdma_dim takes the latest amount of completions that 396 * have been collected and counts them as a new event. 397 * Once enough events have been collected the algorithm decides a new 398 * moderation level. 399 */ 400 void rdma_dim(struct dim *dim, u64 completions); 401 402 #endif /* DIM_H */ 403