1 /*
2  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #ifndef __RC_MINSTREL_H
10 #define __RC_MINSTREL_H
11 
12 #define EWMA_LEVEL	96	/* ewma weighting factor [/EWMA_DIV] */
13 #define EWMA_DIV	128
14 #define SAMPLE_COLUMNS	10	/* number of columns in sample table */
15 
16 /* scaled fraction values */
17 #define MINSTREL_SCALE  12
18 #define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
19 #define MINSTREL_TRUNC(val) ((val) >> MINSTREL_SCALE)
20 
21 /* number of highest throughput rates to consider*/
22 #define MAX_THR_RATES 4
23 
24 /*
25  * Perform EWMA (Exponentially Weighted Moving Average) calculation
26  */
27 static inline int
minstrel_ewma(int old,int new,int weight)28 minstrel_ewma(int old, int new, int weight)
29 {
30 	int diff, incr;
31 
32 	diff = new - old;
33 	incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
34 
35 	return old + incr;
36 }
37 
38 /*
39  * Perform EWMV (Exponentially Weighted Moving Variance) calculation
40  */
41 static inline int
minstrel_ewmv(int old_ewmv,int cur_prob,int prob_ewma,int weight)42 minstrel_ewmv(int old_ewmv, int cur_prob, int prob_ewma, int weight)
43 {
44 	int diff, incr;
45 
46 	diff = cur_prob - prob_ewma;
47 	incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
48 	return weight * (old_ewmv + MINSTREL_TRUNC(diff * incr)) / EWMA_DIV;
49 }
50 
51 struct minstrel_rate_stats {
52 	/* current / last sampling period attempts/success counters */
53 	u16 attempts, last_attempts;
54 	u16 success, last_success;
55 
56 	/* total attempts/success counters */
57 	u32 att_hist, succ_hist;
58 
59 	/* statistis of packet delivery probability
60 	 *  prob_ewma - exponential weighted moving average of prob
61 	 *  prob_ewmsd - exp. weighted moving standard deviation of prob */
62 	u16 prob_ewma;
63 	u16 prob_ewmv;
64 
65 	/* maximum retry counts */
66 	u8 retry_count;
67 	u8 retry_count_rtscts;
68 
69 	u8 sample_skipped;
70 	bool retry_updated;
71 };
72 
73 struct minstrel_rate {
74 	int bitrate;
75 
76 	s8 rix;
77 	u8 retry_count_cts;
78 	u8 adjusted_retry_count;
79 
80 	unsigned int perfect_tx_time;
81 	unsigned int ack_time;
82 
83 	int sample_limit;
84 
85 	struct minstrel_rate_stats stats;
86 };
87 
88 struct minstrel_sta_info {
89 	struct ieee80211_sta *sta;
90 
91 	unsigned long last_stats_update;
92 	unsigned int sp_ack_dur;
93 	unsigned int rate_avg;
94 
95 	unsigned int lowest_rix;
96 
97 	u8 max_tp_rate[MAX_THR_RATES];
98 	u8 max_prob_rate;
99 	unsigned int total_packets;
100 	unsigned int sample_packets;
101 	int sample_deferred;
102 
103 	unsigned int sample_row;
104 	unsigned int sample_column;
105 
106 	int n_rates;
107 	struct minstrel_rate *r;
108 	bool prev_sample;
109 
110 	/* sampling table */
111 	u8 *sample_table;
112 
113 #ifdef CONFIG_MAC80211_DEBUGFS
114 	struct dentry *dbg_stats;
115 	struct dentry *dbg_stats_csv;
116 #endif
117 };
118 
119 struct minstrel_priv {
120 	struct ieee80211_hw *hw;
121 	bool has_mrr;
122 	unsigned int cw_min;
123 	unsigned int cw_max;
124 	unsigned int max_retry;
125 	unsigned int segment_size;
126 	unsigned int update_interval;
127 	unsigned int lookaround_rate;
128 	unsigned int lookaround_rate_mrr;
129 
130 	u8 cck_rates[4];
131 
132 #ifdef CONFIG_MAC80211_DEBUGFS
133 	/*
134 	 * enable fixed rate processing per RC
135 	 *   - write static index to debugfs:ieee80211/phyX/rc/fixed_rate_idx
136 	 *   - write -1 to enable RC processing again
137 	 *   - setting will be applied on next update
138 	 */
139 	u32 fixed_rate_idx;
140 	struct dentry *dbg_fixed_rate;
141 #endif
142 };
143 
144 struct minstrel_debugfs_info {
145 	size_t len;
146 	char buf[];
147 };
148 
149 /* Get EWMSD (Exponentially Weighted Moving Standard Deviation) * 10 */
150 static inline int
minstrel_get_ewmsd10(struct minstrel_rate_stats * mrs)151 minstrel_get_ewmsd10(struct minstrel_rate_stats *mrs)
152 {
153 	unsigned int ewmv = mrs->prob_ewmv;
154 	return int_sqrt(MINSTREL_TRUNC(ewmv * 1000 * 1000));
155 }
156 
157 extern const struct rate_control_ops mac80211_minstrel;
158 void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
159 void minstrel_remove_sta_debugfs(void *priv, void *priv_sta);
160 
161 /* Recalculate success probabilities and counters for a given rate using EWMA */
162 void minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs);
163 int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma);
164 
165 /* debugfs */
166 int minstrel_stats_open(struct inode *inode, struct file *file);
167 int minstrel_stats_csv_open(struct inode *inode, struct file *file);
168 ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos);
169 int minstrel_stats_release(struct inode *inode, struct file *file);
170 
171 #endif
172