1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2017-2026 Morse Micro
4 */
5 #include "mmrc.h"
6
7 /*
8 * The default packet size in bits used for calculated throughput of a given
9 * rate
10 */
11 #define DEFAULT_PACKET_SIZE_BITS 9600
12
13 /*
14 * The default packet size in bytes used for calculating retries for a given
15 * rate
16 */
17 #define DEFAULT_PACKET_SIZE_BYTES 1200
18
19 /* The sample frequencies at different stages */
20 #define LOOKAROUND_RATE_INIT 5
21 #define LOOKAROUND_RATE_NORMAL 50
22 #define LOOKAROUND_RATE_STABLE 100
23
24 /* The thresholds for stability stages */
25 #define STABILITY_CNT_THRESHOLD_INIT 20
26 #define STABILITY_CNT_THRESHOLD_NORMAL 50
27 #define STABILITY_CNT_THRESHOLD_STABLE 100
28
29 /* The backoff step size for the counter */
30 #define STABILITY_BACKOFF_STEP 2
31
32 /*
33 * The packet success threshold for attempting slower lookaround rates
34 */
35 /*
36 * Force a look around if there haven't been any for this number of cycles
37 */
38 #define LOOKAROUND_MAX_RC_CYCLES 5
39
40 /*
41 * Number of attempts for each lookaround rate within at most two RC cycles
42 * if there are enough packets
43 */
44 #define LOOKAROUND_RATE_ATTEMPTS 4
45
46 /*
47 * Limit the number of times we try to pick a theoretically better rate to
48 * sample. Necessary so we don't stall the CPU, due to constantly picking worse
49 * rates.
50 */
51 #define LOOKAROUND_FAIL_MAX 200
52
53 /*
54 * Initial and reset probability per rate in the table
55 * Changing this value will have a severe implication on the current heuristic
56 * It could mean that some rates will have better probability throughput even
57 * with no edivence and so will cause unexpected changes in the rate table
58 */
59 #define RATE_INIT_PROBABILITY 0
60
61 /*
62 * The lowest number of MPDUs within acknowledged AMPDUs that can be used for
63 * rate stats
64 */
65 #define AMPDU_STATS_MIN 2
66
67 /*
68 * The lowest number of stats to be used for processing in NORMAL lookaround
69 * mode
70 */
71 #define STATS_MIN_NORMAL 2
72
73 /*
74 * The lowest number of stats to be used for processing in INIT lookaround
75 * mode
76 */
77 #define STATS_MIN_INIT 1
78
79 /* The lowest probability value considered for recognising a dip */
80 #define PROBABILITY_DIP_MIN 20
81
82 /* The lowest probability value for recovering from a dip */
83 #define PROBABILITY_DIP_RECOVERY_MIN 40
84
85 /*
86 * The time cap on rate allocation for multiple attempts. If a single attempt
87 * exceeds this window, no additional attempts will be generated
88 */
89 #define MAX_WINDOW_ATTEMPT_TIME 4000
90
91 /* The time window for all rates in rate table */
92 #define RATE_WINDOW_MICROSECONDS 24000
93
94 /*
95 * EWMA is the alpha coefficient in the exponential weighting moving average
96 * filter used for probability updates.
97 *
98 * Y[n] = X[n] * (100 - EWMA) + (Y[n-1] * EWMA)
99 * -------------------------------------
100 * 100
101 *
102 */
103 #define EWMA 75
104
105 /*
106 * Evidence scaling to allow for one decimal place. Needed for low
107 * throughput, otherwise the history decays in a single cycle.
108 */
109 #define EVIDENCE_SCALE 5
110
111 /*
112 * Evidence maximum to ensure history doesn't decay too slowly when
113 * there is a lot of historical data.
114 */
115 #define EVIDENCE_MAX 100
116
117 /*
118 * This fixed point conversion multiplies a value by one and shifts it
119 * accordingly to account for the fixed point shifting at the return of a
120 * function
121 */
122 #define FP_8_MULT_1 256
123
124 /* Fixed point conversion for 2.1 * 2^8 used for 4MHz symbol multiplication */
125 #define FP_8_4MHZ 537
126
127 /* Fixed point conversion for 4.5 * 2^8 used for 8MHz symbol multiplication */
128 #define FP_8_8MHZ 1152
129
130 /* Fixed point conversion for 9.0 * 2^8 used for 16MHz symbol multiplication */
131 #define FP_8_16MHZ 2301
132
133 /*
134 * Fixed point conversion for 3.6 * 2^8 used for long guard symbol tx time
135 * multiplication
136 */
137 #define FP_8_LONG_GUARD_SYMBOL_TIME 1024
138
139 /*
140 * Fixed point conversion for 4.0 * 2^8 used for short guard symbol tx time
141 * multiplication
142 */
143 #define FP_8_SHORT_GUARD_SYMBOL_TIME 921
144
145 /*
146 * Shift value to shift back our FP conversions
147 */
148 #define FP_8_SHIFT 8
149
150 /*
151 * Limit to count of consecutive variations in one direction
152 */
153 #define MAX_VARIATION_DIRECTION 5
154
155 /*
156 * Threshold for considering consecutive variation direction as variation
157 * or not
158 */
159 #define VARIATION_DIRECTION_THRESHOLD 3
160
161 /* EWMA percentage value for averaging the best rate probability variation */
162 #define VARIATION_EWMA 95
163
164 /* Percentage variation regarded as minor */
165 #define MINOR_VARIATION_THRESHOLD 1
166
167 /* Percentage variation regarded as moderate */
168 #define MODERATE_VARIATION_THRESHOLD 3
169
170 /* Percentage variation regarded as significant */
171 #define SIGNIFICANT_VARIATION_THRESHOLD 5
172
173 /* If the best rate changes twice in this number of cycles, it is unstable */
174 #define BEST_RATE_UNSTABLE_THRESHOLD 4
175
176 /*
177 * Once the best rate is unchanged for this number of cycles it has
178 * converged
179 */
180 #define BEST_RATE_CONVERGED_THRESHOLD 10
181
182 /* RSSI threshold for short range */
183 #define MMRC_SHORT_RANGE_RSSI_LIMIT -70
184
185 /* RSSI threshold for mid range */
186 #define MMRC_MID_RANGE_RSSI_LIMIT -85
187
188 #define MMRC_MAX_BW(bw_caps) \
189 (((bw_caps) & BIT(MMRC_BW_16MHZ)) ? MMRC_BW_16MHZ : \
190 ((bw_caps) & BIT(MMRC_BW_8MHZ)) ? MMRC_BW_8MHZ : \
191 ((bw_caps) & BIT(MMRC_BW_4MHZ)) ? MMRC_BW_4MHZ : \
192 ((bw_caps) & BIT(MMRC_BW_2MHZ)) ? MMRC_BW_2MHZ : \
193 MMRC_BW_1MHZ)
194
195 /*
196 * This table stores the number of bits per symbols used for MCS0-MCS9 based
197 * on 20MHz and 1SS
198 */
199 static const u32 sym_table[10] = { 24, 36, 48, 72, 96, 144, 192, 216, 256, 288 };
200
201 /*
202 * Calculate which bit is the nth bit set in an integer based flag.
203 */
nth_bit(u16 in,u16 index)204 static u8 nth_bit(u16 in, u16 index)
205 {
206 u32 i;
207 u8 count = 0;
208
209 for (i = 0; count != index + 1; i++) {
210 if (((1u << i) & in) != 0)
211 count++;
212 }
213
214 return i - 1;
215 }
216
217 /*
218 * Calculate the input bit's index among all the set bits in an integer
219 * based flag.
220 */
bit_index(u16 in,u32 bit_pos)221 static u16 bit_index(u16 in, u32 bit_pos)
222 {
223 u16 i;
224 u16 index = 0;
225
226 for (i = 0; i != bit_pos + 1; i++) {
227 if (((1u << i) & in) != 0)
228 index++;
229 }
230
231 if (index == 0) {
232 /* Could not match bit pos to caps */
233 return 0;
234 }
235
236 return index - 1;
237 }
238
rows_from_sta_caps(struct mmrc_sta_capabilities * caps)239 static u16 rows_from_sta_caps(struct mmrc_sta_capabilities *caps)
240 {
241 u16 rows = 0;
242 u8 n_rates = hweight_long(caps->rates);
243
244 /* Taking MCS10 into account as it is relevant for 1 MHz entries */
245 if (caps->rates & BIT(MMRC_MCS10)) {
246 n_rates -= 1;
247 rows = 2;
248 }
249
250 rows += (hweight_long(caps->bandwidth) * n_rates *
251 hweight_long(caps->guard) *
252 hweight_long(caps->spatial_streams));
253
254 return rows;
255 }
256
rate_update_index(struct mmrc_table * tb,struct mmrc_rate * rate)257 static void rate_update_index(struct mmrc_table *tb, struct mmrc_rate *rate)
258 {
259 u16 index = 0;
260 /* Information about our rates */
261 u16 bw = hweight_long(tb->caps.bandwidth);
262 u16 streams = hweight_long(tb->caps.spatial_streams);
263 u16 guard = hweight_long(tb->caps.guard);
264 u16 rows = rows_from_sta_caps(&tb->caps);
265
266 index = bit_index(tb->caps.guard, rate->guard) +
267 bit_index(tb->caps.bandwidth, rate->bw) * guard +
268 bit_index(tb->caps.spatial_streams, rate->ss) * guard * bw +
269 bit_index(tb->caps.rates, rate->rate) * bw * streams * guard;
270
271 if (index >= rows)
272 index = 0;
273
274 rate->index = index;
275 }
276
get_rate_row(struct mmrc_table * tb,u16 index)277 static struct mmrc_rate get_rate_row(struct mmrc_table *tb, u16 index)
278 {
279 struct mmrc_rate rate;
280 u16 ss_index;
281
282 /* Information about our rates */
283 u16 mcs = hweight_long(tb->caps.rates);
284 u16 bw = hweight_long(tb->caps.bandwidth);
285 u16 streams = hweight_long(tb->caps.spatial_streams);
286 u16 guard = hweight_long(tb->caps.guard);
287 u16 total_caps = mcs * bw * streams * guard;
288
289 /* Find our MCS */
290 u16 rows = total_caps / mcs;
291 u16 mcs_index = index / rows;
292 u16 mcs_modulo = index % rows;
293
294 mcs = nth_bit(tb->caps.rates, mcs_index);
295
296 /* Find our spatial stream */
297 rows = rows / streams;
298 streams = nth_bit(tb->caps.spatial_streams, mcs_modulo / rows);
299
300 /* Find our bandwidth */
301 ss_index = index % rows;
302 rows = rows / bw;
303 bw = nth_bit(tb->caps.bandwidth, ss_index / rows);
304
305 /* Find our guard */
306 guard = nth_bit(tb->caps.guard, index % guard);
307
308 /* Add range checks to keep scan-build happy */
309 if (bw >= MMRC_BW_MAX)
310 bw = MMRC_BW_1MHZ;
311
312 if (guard >= MMRC_GUARD_MAX)
313 guard = MMRC_GUARD_LONG;
314
315 /* Validate guard against capability */
316 if (guard == MMRC_GUARD_SHORT &&
317 !(tb->caps.sgi_per_bw & SGI_PER_BW(bw)))
318 guard = MMRC_GUARD_LONG;
319
320 /* Create our rate row and send it */
321 rate.bw = MMRC_BW_TO_BITFIELD(bw);
322 rate.ss = MMRC_SS_TO_BITFIELD(streams);
323 rate.rate = MMRC_RATE_TO_BITFIELD(mcs);
324 rate.guard = MMRC_GUARD_TO_BITFIELD(guard);
325 rate.attempts = 0;
326 rate.flags = 0;
327
328 /* Update index as bw or guard may have changed */
329 rate_update_index(tb, &rate);
330
331 return rate;
332 }
333
mmrc_memory_required_for_caps(struct mmrc_sta_capabilities * caps)334 size_t mmrc_memory_required_for_caps(struct mmrc_sta_capabilities *caps)
335 {
336 return sizeof(struct mmrc_table) +
337 rows_from_sta_caps(caps) * sizeof(struct mmrc_stats_table);
338 }
339
calculate_bits_per_symbol(struct mmrc_rate * rate)340 static u32 calculate_bits_per_symbol(struct mmrc_rate *rate)
341 {
342 u32 bps;
343
344 /* If MCS10 is selected we return 2*MCS0 Symbols */
345 if (rate->rate == MMRC_MCS10)
346 return 6;
347
348 /* Confirm that the rate is valid for the sym_table lookup */
349 if (rate->rate >= MMRC_MCS_UNUSED) {
350 pr_err("%s: Invalid MCS rate %d for sym_table lookup\n",
351 __func__, rate->rate);
352 return 1;
353 }
354
355 /*
356 * Coversion from 20MHz as in sym_table to:
357 * 40MHz == x 2.1
358 * 80MHz == x 4.5
359 * 160MHz == x 9.0
360 */
361 bps = sym_table[rate->rate];
362 switch (rate->bw) {
363 case (MMRC_BW_4MHZ):
364 bps *= FP_8_4MHZ;
365 break;
366 case (MMRC_BW_8MHZ):
367 bps *= FP_8_8MHZ;
368 break;
369 case (MMRC_BW_16MHZ):
370 bps *= FP_8_16MHZ;
371 break;
372 case (MMRC_BW_1MHZ):
373 bps = sym_table[rate->rate] * 24 / 52;
374 bps *= FP_8_MULT_1;
375 break;
376 case (MMRC_BW_2MHZ):
377 case (MMRC_BW_MAX):
378 default:
379 bps *= FP_8_MULT_1;
380 break;
381 }
382 /* SS + 1 because mmrc_spatial_stream starts at 0 */
383 return ((rate->ss + 1) * bps) >> FP_8_SHIFT;
384 }
385
get_tx_time(struct mmrc_rate * rate)386 static u32 get_tx_time(struct mmrc_rate *rate)
387 {
388 u32 tx = 0;
389 u32 n_sym;
390 u32 avg_bits;
391
392 /* Calculate tx time based on a default packet size */
393 avg_bits = DEFAULT_PACKET_SIZE_BITS;
394
395 /* Number of bits per symbol for this rate */
396 n_sym = calculate_bits_per_symbol(rate);
397
398 /* In case of bad calcuation/parameter use lowest value */
399 n_sym = n_sym == 0 ? sym_table[0] : n_sym;
400
401 /* number of symbols in default packet size */
402 n_sym = avg_bits / n_sym;
403
404 /* tx is time to transmit average packet in us */
405 switch (rate->guard) {
406 case (MMRC_GUARD_LONG):
407 tx = n_sym * FP_8_LONG_GUARD_SYMBOL_TIME;
408 break;
409 case (MMRC_GUARD_SHORT):
410 tx = n_sym * FP_8_SHORT_GUARD_SYMBOL_TIME;
411 break;
412 default:
413 return 0;
414 }
415
416 return (tx * 10) >> FP_8_SHIFT;
417 }
418
mmrc_calculate_theoretical_throughput(struct mmrc_rate rate)419 u32 mmrc_calculate_theoretical_throughput(struct mmrc_rate rate)
420 {
421 static const u32 s1g_tpt_lgi[4][11] = {
422 { 300, 600, 900, 1200, 1800, 2400, 2700, 3000, 3600, 4000,
423 150 },
424 { 650, 1300, 1950, 2600, 3900, 5200, 5850, 6500, 7800, 0, 0 },
425 { 1350, 2700, 4050, 5400, 8100, 10800, 12150, 13500, 16200,
426 18000, 0 },
427 { 2925, 5850, 8775, 11700, 17550, 23400, 26325, 29250, 35100,
428 39000, 0 },
429 };
430
431 static const u32 s1g_tpt_sgi[4][11] = {
432 { 333, 666, 1000, 1333, 2000, 2666, 3000, 3333, 4000, 4444,
433 166 },
434 { 722, 1444, 2166, 2888, 4333, 5777, 6500, 7222, 8666, 0, 0 },
435 { 1500, 3000, 4500, 6000, 9000, 12000, 13500, 15000, 18000,
436 20000, 0 },
437 { 3250, 6500, 9750, 13000, 19500, 26000, 29250, 32500, 39000,
438 43333, 0 },
439 };
440
441 if (rate.guard)
442 return s1g_tpt_sgi[rate.bw][rate.rate] * 1000 * (rate.ss + 1);
443
444 return s1g_tpt_lgi[rate.bw][rate.rate] * 1000 * (rate.ss + 1);
445 }
446
calculate_throughput(struct mmrc_table * tb,u8 index)447 static u32 calculate_throughput(struct mmrc_table *tb, u8 index)
448 {
449 struct mmrc_rate rate = get_rate_row(tb, index);
450
451 /*
452 * Avoid the overflow (observed for 8MHz MCS9 rate: 43333) by dividing
453 * first before multiplying. Should not experience any loss of
454 * precision as the throughput is already multiplied by 1000 in
455 * mmrc_calculate_theoretical_throughput (returned as bits/sec)
456 */
457 if (tb->table[rate.index].prob < 10)
458 return 0;
459 else if (rate.index == tb->best_tp.index && tb->interference_likely)
460 /*
461 * Assist the best rate by increasing the probability by the
462 * averaged variation
463 */
464 return (mmrc_calculate_theoretical_throughput(rate) / 100) *
465 (tb->table[rate.index].prob + tb->probability_variation);
466 else
467 return (mmrc_calculate_theoretical_throughput(rate) / 100) *
468 tb->table[rate.index].prob;
469 }
470
validate_rate(struct mmrc_table * tb,struct mmrc_rate * rate)471 static bool validate_rate(struct mmrc_table *tb, struct mmrc_rate *rate)
472 {
473 if (rate->rate == MMRC_MCS10 &&
474 (rate->bw != MMRC_BW_1MHZ || rate->ss != MMRC_SPATIAL_STREAM_1)) {
475 /*
476 * 802.11ah does not support MCS10 with BW that is not 1MHz or
477 * not 1 spatial stream.
478 */
479 return false;
480 }
481
482 if (rate->rate == MMRC_MCS9 && rate->bw == MMRC_BW_2MHZ &&
483 rate->ss != MMRC_SPATIAL_STREAM_3) {
484 /*
485 * 802.11ah does not support MCS9 at 2MHz for 1, 2 or 4 spatial
486 * streams
487 */
488 return false;
489 }
490
491 if (rate->guard == MMRC_GUARD_SHORT &&
492 !(tb->caps.sgi_per_bw & SGI_PER_BW(rate->bw)))
493 return false;
494
495 return true;
496 }
497
find_baseline_index(struct mmrc_table * tb)498 static u16 find_baseline_index(struct mmrc_table *tb)
499 {
500 u32 i, theoretical_tp, min_theoretical_tp;
501 u16 row_count = rows_from_sta_caps(&tb->caps);
502 u16 min_theoretical_tp_index = 0;
503 struct mmrc_rate rate;
504
505 if (tb->caps.rates & BIT(MMRC_MCS10))
506 return 0;
507
508 min_theoretical_tp =
509 mmrc_calculate_theoretical_throughput(get_rate_row(tb, 0));
510 for (i = 0; i < row_count; i++) {
511 rate = get_rate_row(tb, i);
512 if (!validate_rate(tb, &rate))
513 continue;
514
515 theoretical_tp = mmrc_calculate_theoretical_throughput(rate);
516 if (min_theoretical_tp > theoretical_tp) {
517 min_theoretical_tp = theoretical_tp;
518 min_theoretical_tp_index = rate.index;
519 }
520 }
521
522 return min_theoretical_tp_index;
523 }
524
525 /*
526 * Fill out the remaining rates to be used once the best rate is selected.
527 * Normally the retry rates are one MCS lower than the previous, however in
528 * unconverged mode we limit the 3 respective retry rates to MCS 4, 2 and 0
529 * respectively. The last retry rate is always MCS 0
530 */
mmrc_fill_retry_rates(struct mmrc_table * tb)531 static void mmrc_fill_retry_rates(struct mmrc_table *tb)
532 {
533 tb->second_tp = tb->best_tp;
534 if (tb->second_tp.rate != MMRC_MCS0) {
535 tb->second_tp.rate--;
536 if (tb->unconverged && tb->second_tp.rate > MMRC_MCS4)
537 tb->second_tp.rate = MMRC_MCS4;
538 rate_update_index(tb, &tb->second_tp);
539 } else if (tb->second_tp.bw > MMRC_BW_1MHZ) {
540 tb->second_tp.bw--;
541 rate_update_index(tb, &tb->second_tp);
542 }
543
544 tb->best_prob = tb->second_tp;
545 if (tb->best_prob.rate != MMRC_MCS0) {
546 tb->best_prob.rate--;
547 if (tb->unconverged && tb->best_prob.rate > MMRC_MCS2)
548 tb->best_prob.rate = MMRC_MCS2;
549 rate_update_index(tb, &tb->best_prob);
550 } else if (tb->best_prob.bw > MMRC_BW_1MHZ) {
551 tb->best_prob.bw--;
552 rate_update_index(tb, &tb->best_prob);
553 }
554
555 tb->baseline = tb->best_prob;
556 if (tb->baseline.rate != MMRC_MCS0) {
557 tb->baseline.rate = MMRC_MCS0;
558 rate_update_index(tb, &tb->baseline);
559 } else if (tb->baseline.bw > MMRC_BW_1MHZ) {
560 tb->baseline.bw--;
561 rate_update_index(tb, &tb->baseline);
562 }
563 }
564
565 /*
566 * Updates the mmrc_table with the appropriate rate priority based on the
567 * latest update statistics
568 */
generate_table_priority(struct mmrc_table * tb,u32 new_stats)569 static void generate_table_priority(struct mmrc_table *tb, u32 new_stats)
570 {
571 u16 i;
572 u16 best_row = tb->best_tp.index;
573 u16 prev_best_row = best_row;
574 u8 prev_best_rate = tb->best_tp.rate;
575 u16 second_best_row = tb->second_tp.index;
576 u32 best_tp = calculate_throughput(tb, best_row);
577 u32 second_best_tp = calculate_throughput(tb, second_best_row);
578 u32 last_nonzero_prob = 0;
579 struct mmrc_rate tmp;
580 u32 tmp_tp;
581
582 /* Use fixed rate if set */
583 if (tb->fixed_rate.rate != MMRC_MCS_UNUSED) {
584 tb->best_tp = tb->fixed_rate;
585 tb->second_tp = tb->fixed_rate;
586 tb->best_prob = tb->fixed_rate;
587 return;
588 }
589
590 for (i = 0; i < rows_from_sta_caps(&tb->caps); i++) {
591 tmp = get_rate_row(tb, i);
592 if (!validate_rate(tb, &tmp))
593 continue;
594
595 if (tb->table[tmp.index].evidence == 0)
596 continue;
597
598 /*
599 * Besides better throughput, also consider this rate better if
600 * lower rates had worse probability. That indicates the rate
601 * itself is not the problem. Only do the probability check for
602 * rates up to the previous best rate.
603 */
604 tmp_tp = calculate_throughput(tb, tmp.index);
605
606 if (tmp_tp > best_tp ||
607 (tb->table[tmp.index].max_throughput <=
608 tb->table[prev_best_row].max_throughput &&
609 tb->table[tmp.index].prob >=
610 PROBABILITY_DIP_RECOVERY_MIN &&
611 tb->table[tmp.index].prob >
612 tb->table[last_nonzero_prob].prob)) {
613 second_best_row = best_row;
614 second_best_tp = best_tp;
615
616 best_tp = tmp_tp;
617 best_row = tmp.index;
618 } else if (tmp_tp > second_best_tp && best_row != tmp.index) {
619 second_best_tp = tmp_tp;
620 second_best_row = tmp.index;
621 }
622
623 if (tb->table[tmp.index].prob >= PROBABILITY_DIP_MIN &&
624 tb->table[tmp.index].max_throughput >=
625 tb->table[last_nonzero_prob].max_throughput)
626 last_nonzero_prob = tmp.index;
627 }
628
629 /* Only update rates and stability when there are new statistics */
630 if (!new_stats)
631 return;
632
633 tb->best_tp = get_rate_row(tb, best_row);
634 if (best_tp == 0 && tb->best_tp.rate > MMRC_MCS0) {
635 /* Drop one rate, as the best throughput is zero */
636 tb->best_tp.rate--;
637 rate_update_index(tb, &tb->best_tp);
638 }
639 tb->second_tp = get_rate_row(tb, second_best_row);
640 mmrc_fill_retry_rates(tb);
641
642 if (tb->best_tp.rate > MMRC_MCS1 && prev_best_row == best_row) {
643 /* Increase the counter when the best rate is not changed */
644 tb->stability_cnt++;
645 } else if (tb->stability_cnt > STABILITY_BACKOFF_STEP) {
646 /* Back off the counter when there is a new best rate */
647 tb->stability_cnt -= STABILITY_BACKOFF_STEP;
648 } else {
649 tb->stability_cnt = 0;
650 }
651
652 if (prev_best_row != best_row) {
653 s8 latest_best_rate_diff = prev_best_rate - tb->best_tp.rate;
654 u8 total_abs_best_rate_diff =
655 abs(tb->best_rate_diff[0] + tb->best_rate_diff[1] +
656 latest_best_rate_diff);
657
658 if (!tb->interference_likely) {
659 tb->probability_variation = 0;
660 if (!tb->unconverged &&
661 tb->best_rate_cycle_count <=
662 BEST_RATE_UNSTABLE_THRESHOLD &&
663 total_abs_best_rate_diff >= 2) {
664 /*
665 * Best rate has changed twice in a few cycles
666 * and moved at least 2 MCSs from where it was
667 * 3 best rate changes ago
668 */
669 tb->unconverged = true;
670 tb->newly_unconverged = true;
671 }
672 }
673 if (tb->unconverged && !tb->newly_unconverged &&
674 total_abs_best_rate_diff < 2) {
675 /*
676 * Best rate has been relatively stable (not moved more
677 * than 1 MCS after the last 3 rate changes), go back
678 * to converged
679 */
680 tb->unconverged = false;
681 }
682 tb->probability_variation_direction = 0;
683 tb->best_rate_cycle_count = 0;
684 tb->best_rate_diff[0] = tb->best_rate_diff[1];
685 tb->best_rate_diff[1] = latest_best_rate_diff;
686 } else {
687 tb->best_rate_cycle_count++;
688 if (tb->unconverged && !tb->newly_unconverged &&
689 tb->best_rate_cycle_count >=
690 BEST_RATE_CONVERGED_THRESHOLD) {
691 /*
692 * Best rate has been stable for a while, go back to
693 * converged
694 */
695 tb->unconverged = false;
696 }
697 }
698
699 if (tb->newly_unconverged)
700 tb->newly_unconverged = false;
701 }
702
calculate_attempt_time(struct mmrc_rate * rate,size_t size)703 static u32 calculate_attempt_time(struct mmrc_rate *rate, size_t size)
704 {
705 u32 time;
706
707 time = get_tx_time(rate);
708
709 if (size > DEFAULT_PACKET_SIZE_BYTES)
710 time = (time * ((size * 1000) / DEFAULT_PACKET_SIZE_BYTES)) /
711 1000;
712 else
713 time = (time * 1000) /
714 ((DEFAULT_PACKET_SIZE_BYTES * 1000) / size);
715
716 return time;
717 }
718
mmrc_calculate_rate_tx_time(struct mmrc_rate * rate,size_t size)719 u32 mmrc_calculate_rate_tx_time(struct mmrc_rate *rate, size_t size)
720 {
721 u8 i;
722 u32 total_time = 0;
723
724 for (i = 0; i < rate->attempts; i++)
725 total_time += calculate_attempt_time(rate, size);
726
727 return total_time;
728 }
729
730 /*
731 * Calculates the appropriate amount of additional attempts to make based on
732 * packet size and theoretical throughput.
733 */
calculate_remaining_attempts(struct mmrc_table * tb,struct mmrc_rate_table * rate,s32 * rem_time,size_t size)734 static void calculate_remaining_attempts(struct mmrc_table *tb,
735 struct mmrc_rate_table *rate,
736 s32 *rem_time, size_t size)
737 {
738 size_t i;
739
740 if (*rem_time <= 0)
741 return;
742
743 for (i = 0; i < MMRC_MAX_CHAIN_LENGTH; i++) {
744 u32 attempt_time;
745 u32 attempt;
746
747 if (rate->rates[i].rate == MMRC_MCS_UNUSED)
748 break;
749
750 /*
751 * The attempts for these rates were calculated in the initial
752 * attempt allocation
753 */
754 if (tb->table[rate->rates[i].index].prob < 20)
755 continue;
756
757 if (i == 0 && (calculate_throughput(tb, rate->rates[i].index) <
758 calculate_throughput(tb, tb->best_prob.index)))
759 continue;
760
761 attempt_time = calculate_attempt_time(&rate->rates[i], size);
762 if (!attempt_time)
763 continue;
764
765 attempt = (*rem_time / tb->caps.max_rates) / attempt_time;
766 attempt += rate->rates[i].attempts;
767
768 rate->rates[i].attempts = MMRC_ATTEMPTS_TO_BITFIELD(
769 attempt > MMRC_MAX_CHAIN_ATTEMPTS ?
770 MMRC_MAX_CHAIN_ATTEMPTS :
771 attempt);
772 }
773 }
774
775 /* Allocate initial attempts to all rates in a rate table */
allocate_initial_attempts(struct mmrc_rate_table * rate,s32 * rem_time,size_t size)776 static void allocate_initial_attempts(struct mmrc_rate_table *rate,
777 s32 *rem_time, size_t size)
778 {
779 u32 i;
780
781 for (i = 0; i < MMRC_MAX_CHAIN_LENGTH; i++) {
782 u32 attempt_time;
783
784 if (rate->rates[i].rate == MMRC_MCS_UNUSED)
785 break;
786
787 attempt_time = calculate_attempt_time(&rate->rates[i], size);
788
789 /*
790 * if the time for a single attempt is very long, lets just
791 * try once
792 */
793 if (attempt_time > MAX_WINDOW_ATTEMPT_TIME) {
794 *rem_time -= attempt_time;
795 rate->rates[i].attempts = MMRC_ATTEMPTS_TO_BITFIELD(1);
796 } else {
797 *rem_time -= attempt_time * 2;
798 rate->rates[i].attempts = MMRC_ATTEMPTS_TO_BITFIELD(2);
799 }
800 }
801 }
802
mmrc_get_rates(struct mmrc_table * tb,struct mmrc_rate_table * out,size_t size)803 void mmrc_get_rates(struct mmrc_table *tb, struct mmrc_rate_table *out,
804 size_t size)
805 {
806 u8 i;
807 u16 random_index;
808 struct mmrc_rate random;
809 struct mmrc_rate lookaround0 = tb->best_tp;
810 struct mmrc_rate lookaround1 = tb->second_tp;
811 bool is_lookaround;
812 int lookaround_index = -1;
813 int best_index = 0;
814 int random_tp = 0;
815 int best_tp;
816 int lookaround_fail_count;
817 bool try_current_lookaround = false;
818
819 s32 rem_time = RATE_WINDOW_MICROSECONDS;
820
821 memset(out, 0, sizeof(*out));
822
823 tb->lookaround_cnt = (tb->lookaround_cnt + 1) % tb->lookaround_wrap;
824 /*
825 * Look around if the counter wraps or there has been no look around
826 * for a number of rate control cycles.
827 */
828 is_lookaround = (tb->fixed_rate.rate == MMRC_MCS_UNUSED) &&
829 ((tb->lookaround_cnt == 0) ||
830 ((tb->last_lookaround_cycle +
831 LOOKAROUND_MAX_RC_CYCLES) <= tb->cycle_cnt));
832
833 /* Also skip sampling if we don't yet have data for our best rate */
834 if (tb->table[tb->best_tp.index].evidence == 0)
835 is_lookaround = false;
836
837 if (tb->lookaround_wrap != LOOKAROUND_RATE_STABLE) {
838 if (tb->stability_cnt >= tb->stability_cnt_threshold) {
839 tb->lookaround_wrap = LOOKAROUND_RATE_STABLE;
840 tb->stability_cnt_threshold =
841 STABILITY_CNT_THRESHOLD_STABLE;
842 tb->stability_cnt = STABILITY_CNT_THRESHOLD_STABLE * 2;
843 is_lookaround = false;
844 }
845 } else if (tb->stability_cnt < tb->stability_cnt_threshold) {
846 tb->stability_cnt_threshold = STABILITY_CNT_THRESHOLD_NORMAL;
847 tb->lookaround_wrap = LOOKAROUND_RATE_NORMAL;
848 tb->stability_cnt = 0;
849 }
850
851 /* Look around only when the fixed rate is not set */
852 if (is_lookaround) {
853 tb->total_lookaround++;
854 tb->forced_lookaround =
855 (tb->forced_lookaround + 1) % LOOKAROUND_RATE_NORMAL;
856 tb->last_lookaround_cycle = tb->cycle_cnt;
857
858 if (tb->current_lookaround_rate_attempts <
859 LOOKAROUND_RATE_ATTEMPTS)
860 try_current_lookaround = true;
861
862 best_tp = calculate_throughput(tb, tb->best_tp.index);
863
864 for (lookaround_fail_count = 0;
865 lookaround_fail_count < LOOKAROUND_FAIL_MAX;
866 lookaround_fail_count++) {
867 if (try_current_lookaround) {
868 random_index =
869 tb->current_lookaround_rate_index;
870 try_current_lookaround = false;
871 } else {
872 random_index = get_random_u32_below(
873 rows_from_sta_caps(&tb->caps));
874 }
875 random = get_rate_row(tb, random_index);
876
877 if (!validate_rate(tb, &random))
878 continue;
879
880 if (random.rate == MMRC_MCS10)
881 continue;
882
883 if (tb->table[random_index].evidence > 0)
884 random_tp =
885 calculate_throughput(tb, random_index);
886 else
887 random_tp =
888 mmrc_calculate_theoretical_throughput(
889 random);
890
891 /*
892 * Skip rates that can only be worse than the current
893 * best
894 */
895 if (random_tp <= best_tp)
896 continue;
897
898 /*
899 * Force looking up the rate no more that one MCS.
900 * It will avoid looking for rates with very low
901 * success rate. In case of better environment
902 * conditions MMRC will collect enough statistics to
903 * climb up the rates one by one.
904 */
905 if (random.rate > tb->best_tp.rate + 1 ||
906 random.bw > tb->best_tp.bw + 1 ||
907 (random.rate > tb->best_tp.rate &&
908 random.bw > tb->best_tp.bw))
909 continue;
910
911 if (tb->current_lookaround_rate_index == random_index) {
912 tb->current_lookaround_rate_attempts++;
913 } else {
914 tb->current_lookaround_rate_attempts = 0;
915 tb->current_lookaround_rate_index =
916 random_index;
917 }
918
919 break;
920 }
921
922 if (lookaround_fail_count >= LOOKAROUND_FAIL_MAX) {
923 is_lookaround = false;
924 tb->current_lookaround_rate_index = tb->best_tp.index;
925 } else {
926 lookaround0 = random;
927 lookaround1 = tb->best_tp;
928 lookaround_index = 0;
929 best_index = 1;
930 }
931 }
932
933 if (tb->caps.max_rates == 1) {
934 out->rates[0] = (is_lookaround) ? lookaround0 : tb->best_tp;
935 out->rates[1].rate = MMRC_MCS_UNUSED;
936 out->rates[2].rate = MMRC_MCS_UNUSED;
937 out->rates[3].rate = MMRC_MCS_UNUSED;
938 } else if (tb->caps.max_rates == 2) {
939 out->rates[0] = (is_lookaround) ? lookaround0 : tb->best_tp;
940 out->rates[1] = (is_lookaround) ? lookaround1 : tb->best_prob;
941 out->rates[2].rate = MMRC_MCS_UNUSED;
942 out->rates[3].rate = MMRC_MCS_UNUSED;
943 } else if (tb->caps.max_rates == 3) {
944 out->rates[0] = (is_lookaround) ? lookaround0 : tb->best_tp;
945 out->rates[1] = (is_lookaround) ? lookaround1 : tb->second_tp;
946 out->rates[2] = tb->best_prob;
947 out->rates[3].rate = MMRC_MCS_UNUSED;
948 } else {
949 out->rates[0] = (is_lookaround) ? lookaround0 : tb->best_tp;
950 out->rates[1] = (is_lookaround) ? lookaround1 : tb->second_tp;
951 out->rates[2] = tb->best_prob;
952 out->rates[3] = tb->baseline;
953 }
954
955 /* For fallback rates, set RTS/CTS */
956 for (i = 1; i < MMRC_MAX_CHAIN_LENGTH; i++)
957 out->rates[i].flags |= BIT(MMRC_FLAGS_CTS_RTS);
958
959 /* Allocate initial attempts for rate */
960 allocate_initial_attempts(out, &rem_time, size);
961
962 /* Calculate and allocate remaining attempts */
963 calculate_remaining_attempts(tb, out, &rem_time, size);
964
965 /* Enforce limits on each attempts */
966 for (i = 0; i < MMRC_MAX_CHAIN_LENGTH; i++) {
967 if (out->rates[i].rate != MMRC_MCS_UNUSED) {
968 out->rates[i].attempts =
969 out->rates[i].attempts == 0 ?
970 MMRC_ATTEMPTS_TO_BITFIELD(
971 MMRC_MIN_CHAIN_ATTEMPTS) :
972 out->rates[i].attempts;
973 out->rates[i].attempts =
974 out->rates[i].attempts >
975 MMRC_MAX_CHAIN_ATTEMPTS ?
976 MMRC_ATTEMPTS_TO_BITFIELD(
977 MMRC_MAX_CHAIN_ATTEMPTS) :
978 out->rates[i].attempts;
979 if (i == lookaround_index &&
980 tb->lookaround_wrap != LOOKAROUND_RATE_INIT)
981 out->rates[i].attempts =
982 MMRC_ATTEMPTS_TO_BITFIELD(1);
983 }
984 }
985
986 /*
987 * Give the best rate at least 2 attempts to keep peak throughput
988 * unless it is too low
989 */
990 if (out->rates[best_index].attempts == 1 &&
991 out->rates[best_index].rate > MMRC_MCS1)
992 out->rates[best_index].attempts = MMRC_ATTEMPTS_TO_BITFIELD(2);
993 else if (out->rates[best_index].rate <= MMRC_MCS1)
994 out->rates[best_index].attempts = 1;
995 }
996
calc_ewma_average(u32 avg,u32 latest,u32 weight)997 static u32 calc_ewma_average(u32 avg, u32 latest, u32 weight)
998 {
999 WARN_ON_ONCE(!(weight <= 100));
1000
1001 if (avg == 0)
1002 return latest;
1003
1004 return ((latest * (100 - weight)) + (avg * weight)) / 100;
1005 }
1006
mmrc_process_variation(struct mmrc_table * tb,u16 current_success,u32 index)1007 static void mmrc_process_variation(struct mmrc_table *tb, u16 current_success,
1008 u32 index)
1009 {
1010 u32 current_variation;
1011
1012 /*
1013 * Only process probability variation for the best rate. It is likely
1014 * the only rate to have enough data to see the variation and its
1015 * statistics are more affected because they are usually collected over
1016 * the full period.
1017 */
1018 if (index != tb->best_tp.index)
1019 return;
1020
1021 if (current_success == 0) {
1022 if (!tb->unconverged) {
1023 /*
1024 * Best rate is failing completely, go to unconverged
1025 * mode
1026 */
1027 tb->unconverged = true;
1028 tb->newly_unconverged = true;
1029 }
1030 return;
1031 }
1032
1033 if (tb->table[index].prob == 0)
1034 return;
1035
1036 /* Don't process variation while converging after association */
1037 if (tb->lookaround_wrap == LOOKAROUND_RATE_INIT)
1038 return;
1039
1040 current_variation = abs(current_success - tb->table[index].prob);
1041
1042 /* Calculate the EWMA of the probability variation */
1043 tb->probability_variation = calc_ewma_average(
1044 tb->probability_variation, current_variation, VARIATION_EWMA);
1045
1046 /*
1047 * Process the variation direction to distinguish converged and
1048 * unconverged scenarios
1049 */
1050 if (tb->probability_variation >= MODERATE_VARIATION_THRESHOLD ||
1051 tb->interference_likely) {
1052 if ((current_success - tb->table[index].prob) *
1053 tb->probability_variation_direction <
1054 0)
1055 tb->probability_variation_direction = 0;
1056 else if (current_success > tb->table[index].prob)
1057 tb->probability_variation_direction =
1058 min(tb->probability_variation_direction + 1,
1059 MAX_VARIATION_DIRECTION);
1060 else if (current_success < tb->table[index].prob)
1061 tb->probability_variation_direction =
1062 max(tb->probability_variation_direction - 1,
1063 -MAX_VARIATION_DIRECTION);
1064 }
1065
1066 if (tb->best_rate_cycle_count > VARIATION_DIRECTION_THRESHOLD &&
1067 tb->probability_variation >= SIGNIFICANT_VARIATION_THRESHOLD) {
1068 /*
1069 * Only enter interference mode if the best rate is stable for
1070 * enough cycles to determine the direction is random and not
1071 * in one direction only
1072 */
1073 if (abs(tb->probability_variation_direction) <=
1074 VARIATION_DIRECTION_THRESHOLD &&
1075 !tb->interference_likely) {
1076 tb->interference_likely = true;
1077 }
1078 } else if (tb->interference_likely &&
1079 (tb->probability_variation <= MINOR_VARIATION_THRESHOLD ||
1080 abs(tb->probability_variation_direction) ==
1081 MAX_VARIATION_DIRECTION)) {
1082 /*
1083 * Exit interference mode if the variability drops or the
1084 * direction stops being random
1085 */
1086 tb->interference_likely = false;
1087 }
1088 }
1089
mmrc_update(struct mmrc_table * tb)1090 void mmrc_update(struct mmrc_table *tb)
1091 {
1092 u32 i;
1093 u16 this_success;
1094 u32 scale;
1095 u32 scaled_ewma;
1096 u32 new_stats = 0;
1097 u32 attempts_for_stats;
1098 u32 success_for_stats;
1099 u32 min_stats;
1100 u32 throughput;
1101 u32 evidence_sent;
1102
1103 tb->cycle_cnt++;
1104
1105 /* Allow less minimum stats when converging */
1106 if (tb->lookaround_wrap != LOOKAROUND_RATE_INIT)
1107 min_stats = STATS_MIN_NORMAL;
1108 else
1109 min_stats = STATS_MIN_INIT;
1110
1111 for (i = 0; i < rows_from_sta_caps(&tb->caps); i++) {
1112 /* This algorithm is keeping track of the amount of evidence,
1113 * being packets that have been recently sent at this rate.
1114 * This value is smoothed with an EWMA function over time and
1115 * used to update the probability of a rate succeeding
1116 * dynamically. This method allows MMRC to react timely if a
1117 * new rate is used that hasn't been used recently
1118 */
1119
1120 /* Necessary to prevent a divide by 0 */
1121 if (tb->table[i].evidence == 0)
1122 scale = 0;
1123 else
1124 scale = ((tb->table[i].evidence * 2) * 100) /
1125 ((tb->table[i].sent * EVIDENCE_SCALE) +
1126 tb->table[i].evidence);
1127
1128 /* Restrict scale to appropriate values */
1129 if (scale > 100)
1130 scale = 100;
1131
1132 scaled_ewma = scale * EWMA / 100;
1133
1134 /*
1135 * Only count new packets for evidence if we will process
1136 * them
1137 */
1138 evidence_sent =
1139 tb->table[i].sent >= min_stats ? tb->table[i].sent : 0;
1140 tb->table[i].evidence = calc_ewma_average(
1141 tb->table[i].evidence, evidence_sent * EVIDENCE_SCALE,
1142 scaled_ewma);
1143
1144 if (tb->table[i].evidence > EVIDENCE_MAX)
1145 tb->table[i].evidence = EVIDENCE_MAX;
1146
1147 /* Try to use statistics from acknowledged AMPDUs first */
1148 attempts_for_stats = tb->table[i].back_mpdu_success +
1149 tb->table[i].back_mpdu_failure;
1150 success_for_stats = tb->table[i].back_mpdu_success;
1151
1152 /*
1153 * Use the full statistics if rates are not converged or there
1154 * were no AMPDUs for this rate or the remaining attempts are
1155 * less than half of what we have from AMPDUs.
1156 */
1157 if (!tb->table[i].have_sent_ampdus || tb->unconverged ||
1158 attempts_for_stats < AMPDU_STATS_MIN ||
1159 (tb->table[i].sent - attempts_for_stats <
1160 attempts_for_stats / 2)) {
1161 attempts_for_stats = tb->table[i].sent;
1162 success_for_stats = tb->table[i].sent_success;
1163 }
1164
1165 if (attempts_for_stats >= min_stats ||
1166 (attempts_for_stats > 0 && tb->table[i].prob > 0)) {
1167 new_stats = 1;
1168 this_success =
1169 (100 * success_for_stats) / attempts_for_stats;
1170
1171 if (scaled_ewma)
1172 mmrc_process_variation(tb, this_success, i);
1173
1174 tb->table[i].prob = calc_ewma_average(
1175 tb->table[i].prob, this_success, scaled_ewma);
1176
1177 /* Clear our sent statistics and update totals */
1178 tb->table[i].total_sent += tb->table[i].sent;
1179 tb->table[i].sent = 0;
1180
1181 tb->table[i].total_success += tb->table[i].sent_success;
1182 tb->table[i].sent_success = 0;
1183
1184 tb->table[i].back_mpdu_failure = 0;
1185 tb->table[i].back_mpdu_success = 0;
1186 tb->table[i].have_sent_ampdus = false;
1187 }
1188
1189 throughput = calculate_throughput(tb, i);
1190 if (tb->table[i].max_throughput < throughput)
1191 tb->table[i].max_throughput = throughput;
1192
1193 /*
1194 * Reset the running average windows if reached collector
1195 * limits
1196 */
1197 if (tb->table[i].sum_throughput > (0xFFFFFFFF - throughput)) {
1198 tb->table[i].sum_throughput /=
1199 tb->table[i].avg_throughput_counter;
1200 tb->table[i].avg_throughput_counter = 1;
1201 }
1202 /* Update the sum and counter so it will be possible later to
1203 * calculate the running average throughput
1204 */
1205 tb->table[i].sum_throughput += throughput;
1206 tb->table[i].avg_throughput_counter++;
1207 }
1208
1209 generate_table_priority(tb, new_stats);
1210
1211 /*
1212 * Switch to faster lookaround mode if rates drop low at very low
1213 * bandwidth or we are in unconverged mode. Switching at low bandwidth
1214 * and rate is to help recover quickly from rates where we would need
1215 * to fragment standard MTU size packets.
1216 */
1217 if (tb->lookaround_wrap != LOOKAROUND_RATE_INIT &&
1218 (tb->unconverged || (tb->best_tp.bw == MMRC_BW_1MHZ &&
1219 tb->best_tp.rate <= MMRC_MCS2))) {
1220 tb->lookaround_cnt = 0;
1221 tb->lookaround_wrap = LOOKAROUND_RATE_INIT;
1222 tb->stability_cnt_threshold = STABILITY_CNT_THRESHOLD_INIT;
1223 }
1224
1225 /*
1226 * If it is unlikely we can do the lookaround attempts in two RC cycles
1227 * choose a new rate
1228 */
1229 if (tb->current_lookaround_rate_attempts <=
1230 (LOOKAROUND_RATE_ATTEMPTS / 2))
1231 tb->current_lookaround_rate_attempts = LOOKAROUND_RATE_ATTEMPTS;
1232 }
1233
mmrc_feedback(struct mmrc_table * tb,struct mmrc_rate_table * rates,s32 retry_count,bool was_aggregated)1234 void mmrc_feedback(struct mmrc_table *tb, struct mmrc_rate_table *rates,
1235 s32 retry_count, bool was_aggregated)
1236 {
1237 s32 ind = retry_count;
1238 u32 i;
1239
1240 for (i = 0; i < MMRC_MAX_CHAIN_LENGTH; i++) {
1241 rate_update_index(tb, &rates->rates[i]);
1242 tb->table[rates->rates[i].index].have_sent_ampdus |=
1243 was_aggregated;
1244
1245 if ((s32)rates->rates[i].attempts < ind) {
1246 ind = ind - rates->rates[i].attempts;
1247 tb->table[rates->rates[i].index].sent +=
1248 rates->rates[i].attempts;
1249 if (was_aggregated) {
1250 tb->table[rates->rates[i].index]
1251 .back_mpdu_failure +=
1252 rates->rates[i].attempts;
1253 }
1254 } else {
1255 tb->table[rates->rates[i].index].sent += ind;
1256 tb->table[rates->rates[i].index].sent_success += 1;
1257 if (was_aggregated) {
1258 tb->table[rates->rates[i].index]
1259 .back_mpdu_success += 1;
1260 tb->table[rates->rates[i].index]
1261 .back_mpdu_failure +=
1262 ind > 1 ? ind - 1 : 0;
1263 }
1264 return;
1265 }
1266 }
1267 }
1268
1269 /*
1270 * Chooses a reasonable starting rate based on range (gathered from
1271 * RSSI measurements) or bandwidth. Then fills out the 3 retry rates
1272 * so a full set of rates is available.
1273 */
mmrc_init_rates(struct mmrc_table * tb,s8 rssi)1274 static void mmrc_init_rates(struct mmrc_table *tb, s8 rssi)
1275 {
1276 tb->best_tp.bw = MMRC_MAX_BW(tb->caps.bandwidth);
1277 if (tb->caps.sgi_per_bw & SGI_PER_BW(tb->best_tp.bw))
1278 tb->best_tp.guard = MMRC_GUARD_TO_BITFIELD(MMRC_GUARD_SHORT);
1279 else
1280 tb->best_tp.guard = MMRC_GUARD_TO_BITFIELD(MMRC_GUARD_LONG);
1281 tb->best_tp.rate = MMRC_RATE_TO_BITFIELD(MMRC_MCS0);
1282
1283 if (rssi >= MMRC_SHORT_RANGE_RSSI_LIMIT)
1284 tb->best_tp.rate = MMRC_RATE_TO_BITFIELD(MMRC_MCS7);
1285 else if (rssi < MMRC_SHORT_RANGE_RSSI_LIMIT &&
1286 rssi >= MMRC_MID_RANGE_RSSI_LIMIT)
1287 tb->best_tp.rate = MMRC_RATE_TO_BITFIELD(MMRC_MCS3);
1288 else if (tb->best_tp.bw == MMRC_BW_1MHZ ||
1289 tb->best_tp.bw == MMRC_BW_2MHZ)
1290 /*
1291 * To compensate for slow feedback when running with 1 and 2
1292 * MHz bandwidth, we start from MCS3 which will correspond to
1293 * reasonable feedback and will avoid resetting the rate table
1294 * evidence.
1295 */
1296 tb->best_tp.rate = MMRC_RATE_TO_BITFIELD(MMRC_MCS3);
1297
1298 tb->best_tp.ss = MMRC_SS_TO_BITFIELD(MMRC_SPATIAL_STREAM_1);
1299 rate_update_index(tb, &tb->best_tp);
1300 /* Init every rate in case they are needed to set the retry rates */
1301 tb->second_tp = tb->best_tp;
1302 tb->best_prob = tb->best_tp;
1303 tb->baseline = tb->best_tp;
1304 mmrc_fill_retry_rates(tb);
1305 }
1306
mmrc_sta_init(struct mmrc_table * tb,struct mmrc_sta_capabilities * caps,s8 rssi)1307 void mmrc_sta_init(struct mmrc_table *tb, struct mmrc_sta_capabilities *caps,
1308 s8 rssi)
1309 {
1310 u32 i;
1311 u16 row_count = rows_from_sta_caps(caps);
1312
1313 memset(tb, 0, mmrc_memory_required_for_caps(caps));
1314 memcpy(&tb->caps, caps, sizeof(tb->caps));
1315
1316 for (i = 0; i < row_count; i++) {
1317 tb->table[i].prob = RATE_INIT_PROBABILITY;
1318 tb->table[i].evidence = 0;
1319 tb->table[i].sum_throughput = 0;
1320 tb->table[i].avg_throughput_counter = 0;
1321 tb->table[i].max_throughput = 0;
1322 }
1323
1324 tb->fixed_rate.rate = MMRC_MCS_UNUSED;
1325 tb->cycle_cnt = 0;
1326 tb->last_lookaround_cycle = 0;
1327 tb->lookaround_cnt = 0;
1328 tb->lookaround_wrap = LOOKAROUND_RATE_INIT;
1329 tb->unconverged = true;
1330 tb->newly_unconverged = true;
1331 tb->stability_cnt_threshold = STABILITY_CNT_THRESHOLD_INIT;
1332 tb->baseline = get_rate_row(tb, find_baseline_index(tb));
1333 mmrc_init_rates(tb, rssi);
1334 }
1335
mmrc_set_fixed_rate(struct mmrc_table * tb,struct mmrc_rate fixed_rate)1336 bool mmrc_set_fixed_rate(struct mmrc_table *tb, struct mmrc_rate fixed_rate)
1337 {
1338 bool caps_support_rate = true;
1339
1340 /* Do not accept rate which does not support the STA capabilities */
1341 if ((BIT(fixed_rate.rate) & tb->caps.rates) == 0 ||
1342 (BIT(fixed_rate.bw) & tb->caps.bandwidth) == 0 ||
1343 (BIT(fixed_rate.ss) & tb->caps.spatial_streams) == 0 ||
1344 (BIT(fixed_rate.guard) & tb->caps.guard) == 0)
1345 caps_support_rate = false;
1346
1347 if (validate_rate(tb, &fixed_rate) && caps_support_rate) {
1348 tb->fixed_rate = fixed_rate;
1349 rate_update_index(tb, &tb->fixed_rate);
1350 return true;
1351 }
1352
1353 return false;
1354 }
1355