1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
6 #ifndef __RC_MINSTREL_H
7 #define __RC_MINSTREL_H
9 #define EWMA_LEVEL 96 /* ewma weighting factor [/EWMA_DIV] */
11 #define SAMPLE_COLUMNS 10 /* number of columns in sample table */
13 /* scaled fraction values */
14 #define MINSTREL_SCALE 12
15 #define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
16 #define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
18 /* number of highest throughput rates to consider*/
19 #define MAX_THR_RATES 4
22 * Coefficients for moving average with noise filter (period=16),
25 * a1 = exp(-pi * sqrt(2) / period)
26 * coeff2 = 2 * a1 * cos(sqrt(2) * 2 * pi / period)
28 * coeff1 = 1 - coeff2 - coeff3
30 #define MINSTREL_AVG_COEFF1 (MINSTREL_FRAC(1, 1) - \
31 MINSTREL_AVG_COEFF2 - \
33 #define MINSTREL_AVG_COEFF2 0x00001499
34 #define MINSTREL_AVG_COEFF3 -0x0000092e
37 * Perform EWMA (Exponentially Weighted Moving Average) calculation
40 minstrel_ewma(int old, int new, int weight)
45 incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
50 static inline int minstrel_filter_avg_add(u16 *prev_1, u16 *prev_2, s32 in)
64 val = MINSTREL_AVG_COEFF1 * in;
65 val += MINSTREL_AVG_COEFF2 * out_1;
66 val += MINSTREL_AVG_COEFF3 * out_2;
67 val >>= MINSTREL_SCALE;
69 if (val > 1 << MINSTREL_SCALE)
70 val = 1 << MINSTREL_SCALE;
81 struct minstrel_rate_stats {
82 /* current / last sampling period attempts/success counters */
83 u16 attempts, last_attempts;
84 u16 success, last_success;
86 /* total attempts/success counters */
87 u32 att_hist, succ_hist;
89 /* prob_avg - moving average of prob */
93 /* maximum retry counts */
95 u8 retry_count_rtscts;
101 struct minstrel_rate {
106 u8 adjusted_retry_count;
108 unsigned int perfect_tx_time;
109 unsigned int ack_time;
113 struct minstrel_rate_stats stats;
116 struct minstrel_sta_info {
117 struct ieee80211_sta *sta;
119 unsigned long last_stats_update;
120 unsigned int sp_ack_dur;
121 unsigned int rate_avg;
123 unsigned int lowest_rix;
125 u8 max_tp_rate[MAX_THR_RATES];
127 unsigned int total_packets;
128 unsigned int sample_packets;
131 unsigned int sample_row;
132 unsigned int sample_column;
135 struct minstrel_rate *r;
142 struct minstrel_priv {
143 struct ieee80211_hw *hw;
149 unsigned int max_retry;
150 unsigned int segment_size;
151 unsigned int update_interval;
152 unsigned int lookaround_rate;
153 unsigned int lookaround_rate_mrr;
157 #ifdef CONFIG_MAC80211_DEBUGFS
159 * enable fixed rate processing per RC
160 * - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
161 * - write -1 to enable RC processing again
162 * - setting will be applied on next update
168 struct minstrel_debugfs_info {
173 extern const struct rate_control_ops mac80211_minstrel;
174 void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
176 /* Recalculate success probabilities and counters for a given rate using EWMA */
177 void minstrel_calc_rate_stats(struct minstrel_priv *mp,
178 struct minstrel_rate_stats *mrs);
179 int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_avg);
182 int minstrel_stats_open(struct inode *inode, struct file *file);
183 int minstrel_stats_csv_open(struct inode *inode, struct file *file);