rc80211_minstrel.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  4. */
  5. #ifndef __RC_MINSTREL_H
  6. #define __RC_MINSTREL_H
  7. #define EWMA_LEVEL 96 /* ewma weighting factor [/EWMA_DIV] */
  8. #define EWMA_DIV 128
  9. #define SAMPLE_COLUMNS 10 /* number of columns in sample table */
  10. /* scaled fraction values */
  11. #define MINSTREL_SCALE 12
  12. #define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
  13. #define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
  14. /* number of highest throughput rates to consider*/
  15. #define MAX_THR_RATES 4
  16. /*
  17. * Coefficients for moving average with noise filter (period=16),
  18. * scaled by 10 bits
  19. *
  20. * a1 = exp(-pi * sqrt(2) / period)
  21. * coeff2 = 2 * a1 * cos(sqrt(2) * 2 * pi / period)
  22. * coeff3 = -sqr(a1)
  23. * coeff1 = 1 - coeff2 - coeff3
  24. */
  25. #define MINSTREL_AVG_COEFF1 (MINSTREL_FRAC(1, 1) - \
  26. MINSTREL_AVG_COEFF2 - \
  27. MINSTREL_AVG_COEFF3)
  28. #define MINSTREL_AVG_COEFF2 0x00001499
  29. #define MINSTREL_AVG_COEFF3 -0x0000092e
  30. /*
  31. * Perform EWMA (Exponentially Weighted Moving Average) calculation
  32. */
  33. static inline int
  34. minstrel_ewma(int old, int new, int weight)
  35. {
  36. int diff, incr;
  37. diff = new - old;
  38. incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
  39. return old + incr;
  40. }
  41. static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
  42. {
  43. s32 out_1 = *prev_1;
  44. s32 out_2 = *prev_2;
  45. s32 val;
  46. if (!in)
  47. in += 1;
  48. if (!out_1) {
  49. val = out_1 = in;
  50. goto out;
  51. }
  52. val = MINSTREL_AVG_COEFF1 * in;
  53. val += MINSTREL_AVG_COEFF2 * out_1;
  54. val += MINSTREL_AVG_COEFF3 * out_2;
  55. val >>= MINSTREL_SCALE;
  56. if (val > 1 << MINSTREL_SCALE)
  57. val = 1 << MINSTREL_SCALE;
  58. if (val < 0)
  59. val = 1;
  60. out:
  61. *prev_2 = out_1;
  62. *prev_1 = val;
  63. return val;
  64. }
  65. struct minstrel_rate_stats {
  66. /* current / last sampling period attempts/success counters */
  67. u16 attempts, last_attempts;
  68. u16 success, last_success;
  69. /* total attempts/success counters */
  70. u32 att_hist, succ_hist;
  71. /* prob_avg - moving average of prob */
  72. u16 prob_avg;
  73. u16 prob_avg_1;
  74. /* maximum retry counts */
  75. u8 retry_count;
  76. u8 retry_count_rtscts;
  77. u8 sample_skipped;
  78. bool retry_updated;
  79. };
  80. struct minstrel_rate {
  81. int bitrate;
  82. s8 rix;
  83. u8 retry_count_cts;
  84. u8 adjusted_retry_count;
  85. unsigned int perfect_tx_time;
  86. unsigned int ack_time;
  87. int sample_limit;
  88. struct minstrel_rate_stats stats;
  89. };
  90. struct minstrel_sta_info {
  91. struct ieee80211_sta *sta;
  92. unsigned long last_stats_update;
  93. unsigned int sp_ack_dur;
  94. unsigned int rate_avg;
  95. unsigned int lowest_rix;
  96. u8 max_tp_rate[MAX_THR_RATES];
  97. u8 max_prob_rate;
  98. unsigned int total_packets;
  99. unsigned int sample_packets;
  100. unsigned int sample_row;
  101. unsigned int sample_column;
  102. int n_rates;
  103. struct minstrel_rate *r;
  104. bool prev_sample;
  105. /* sampling table */
  106. u8 *sample_table;
  107. };
  108. struct minstrel_priv {
  109. struct ieee80211_hw *hw;
  110. bool has_mrr;
  111. bool new_avg;
  112. u32 sample_switch;
  113. unsigned int cw_min;
  114. unsigned int cw_max;
  115. unsigned int max_retry;
  116. unsigned int segment_size;
  117. unsigned int update_interval;
  118. unsigned int lookaround_rate;
  119. unsigned int lookaround_rate_mrr;
  120. u8 cck_rates[4];
  121. #ifdef CONFIG_MAC80211_DEBUGFS
  122. /*
  123. * enable fixed rate processing per RC
  124. * - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
  125. * - write -1 to enable RC processing again
  126. * - setting will be applied on next update
  127. */
  128. u32 fixed_rate_idx;
  129. #endif
  130. };
  131. struct minstrel_debugfs_info {
  132. size_t len;
  133. char buf[];
  134. };
  135. extern const struct rate_control_ops mac80211_minstrel;
  136. void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
  137. /* Recalculate success probabilities and counters for a given rate using EWMA */
  138. void minstrel_calc_rate_stats(struct minstrel_priv *mp,
  139. struct minstrel_rate_stats *mrs);
  140. int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_avg);
  141. /* debugfs */
  142. int minstrel_stats_open(struct inode *inode, struct file *file);
  143. int minstrel_stats_csv_open(struct inode *inode, struct file *file);
  144. #endif