xref: /OK3568_Linux_fs/kernel/net/mac80211/rc80211_minstrel.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
5*4882a593Smuzhiyun  * it under the terms of the GNU General Public License version 2 as
6*4882a593Smuzhiyun  * published by the Free Software Foundation.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Based on minstrel.c:
9*4882a593Smuzhiyun  *   Copyright (C) 2005-2007 Derek Smithies <derek@indranet.co.nz>
10*4882a593Smuzhiyun  *   Sponsored by Indranet Technologies Ltd
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Based on sample.c:
13*4882a593Smuzhiyun  *   Copyright (c) 2005 John Bicket
14*4882a593Smuzhiyun  *   All rights reserved.
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *   Redistribution and use in source and binary forms, with or without
17*4882a593Smuzhiyun  *   modification, are permitted provided that the following conditions
18*4882a593Smuzhiyun  *   are met:
19*4882a593Smuzhiyun  *   1. Redistributions of source code must retain the above copyright
20*4882a593Smuzhiyun  *      notice, this list of conditions and the following disclaimer,
21*4882a593Smuzhiyun  *      without modification.
22*4882a593Smuzhiyun  *   2. Redistributions in binary form must reproduce at minimum a disclaimer
23*4882a593Smuzhiyun  *      similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
24*4882a593Smuzhiyun  *      redistribution must be conditioned upon including a substantially
25*4882a593Smuzhiyun  *      similar Disclaimer requirement for further binary redistribution.
26*4882a593Smuzhiyun  *   3. Neither the names of the above-listed copyright holders nor the names
27*4882a593Smuzhiyun  *      of any contributors may be used to endorse or promote products derived
28*4882a593Smuzhiyun  *      from this software without specific prior written permission.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  *   Alternatively, this software may be distributed under the terms of the
31*4882a593Smuzhiyun  *   GNU General Public License ("GPL") version 2 as published by the Free
32*4882a593Smuzhiyun  *   Software Foundation.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *   NO WARRANTY
35*4882a593Smuzhiyun  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36*4882a593Smuzhiyun  *   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37*4882a593Smuzhiyun  *   LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
38*4882a593Smuzhiyun  *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
39*4882a593Smuzhiyun  *   THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
40*4882a593Smuzhiyun  *   OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41*4882a593Smuzhiyun  *   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
42*4882a593Smuzhiyun  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
43*4882a593Smuzhiyun  *   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44*4882a593Smuzhiyun  *   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
45*4882a593Smuzhiyun  *   THE POSSIBILITY OF SUCH DAMAGES.
46*4882a593Smuzhiyun  */
47*4882a593Smuzhiyun #include <linux/netdevice.h>
48*4882a593Smuzhiyun #include <linux/types.h>
49*4882a593Smuzhiyun #include <linux/skbuff.h>
50*4882a593Smuzhiyun #include <linux/debugfs.h>
51*4882a593Smuzhiyun #include <linux/random.h>
52*4882a593Smuzhiyun #include <linux/ieee80211.h>
53*4882a593Smuzhiyun #include <linux/slab.h>
54*4882a593Smuzhiyun #include <net/mac80211.h>
55*4882a593Smuzhiyun #include "rate.h"
56*4882a593Smuzhiyun #include "rc80211_minstrel.h"
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define SAMPLE_TBL(_mi, _idx, _col) \
59*4882a593Smuzhiyun 		_mi->sample_table[(_idx * SAMPLE_COLUMNS) + _col]
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* convert mac80211 rate index to local array index */
62*4882a593Smuzhiyun static inline int
rix_to_ndx(struct minstrel_sta_info * mi,int rix)63*4882a593Smuzhiyun rix_to_ndx(struct minstrel_sta_info *mi, int rix)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	int i = rix;
66*4882a593Smuzhiyun 	for (i = rix; i >= 0; i--)
67*4882a593Smuzhiyun 		if (mi->r[i].rix == rix)
68*4882a593Smuzhiyun 			break;
69*4882a593Smuzhiyun 	return i;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /* return current EMWA throughput */
minstrel_get_tp_avg(struct minstrel_rate * mr,int prob_avg)73*4882a593Smuzhiyun int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_avg)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	int usecs;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	usecs = mr->perfect_tx_time;
78*4882a593Smuzhiyun 	if (!usecs)
79*4882a593Smuzhiyun 		usecs = 1000000;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	/* reset thr. below 10% success */
82*4882a593Smuzhiyun 	if (mr->stats.prob_avg < MINSTREL_FRAC(10, 100))
83*4882a593Smuzhiyun 		return 0;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (prob_avg > MINSTREL_FRAC(90, 100))
86*4882a593Smuzhiyun 		return MINSTREL_TRUNC(100000 * (MINSTREL_FRAC(90, 100) / usecs));
87*4882a593Smuzhiyun 	else
88*4882a593Smuzhiyun 		return MINSTREL_TRUNC(100000 * (prob_avg / usecs));
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun /* find & sort topmost throughput rates */
92*4882a593Smuzhiyun static inline void
minstrel_sort_best_tp_rates(struct minstrel_sta_info * mi,int i,u8 * tp_list)93*4882a593Smuzhiyun minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	int j;
96*4882a593Smuzhiyun 	struct minstrel_rate_stats *tmp_mrs;
97*4882a593Smuzhiyun 	struct minstrel_rate_stats *cur_mrs = &mi->r[i].stats;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	for (j = MAX_THR_RATES; j > 0; --j) {
100*4882a593Smuzhiyun 		tmp_mrs = &mi->r[tp_list[j - 1]].stats;
101*4882a593Smuzhiyun 		if (minstrel_get_tp_avg(&mi->r[i], cur_mrs->prob_avg) <=
102*4882a593Smuzhiyun 		    minstrel_get_tp_avg(&mi->r[tp_list[j - 1]], tmp_mrs->prob_avg))
103*4882a593Smuzhiyun 			break;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	if (j < MAX_THR_RATES - 1)
107*4882a593Smuzhiyun 		memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
108*4882a593Smuzhiyun 	if (j < MAX_THR_RATES)
109*4882a593Smuzhiyun 		tp_list[j] = i;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun static void
minstrel_set_rate(struct minstrel_sta_info * mi,struct ieee80211_sta_rates * ratetbl,int offset,int idx)113*4882a593Smuzhiyun minstrel_set_rate(struct minstrel_sta_info *mi, struct ieee80211_sta_rates *ratetbl,
114*4882a593Smuzhiyun 		  int offset, int idx)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	struct minstrel_rate *r = &mi->r[idx];
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	ratetbl->rate[offset].idx = r->rix;
119*4882a593Smuzhiyun 	ratetbl->rate[offset].count = r->adjusted_retry_count;
120*4882a593Smuzhiyun 	ratetbl->rate[offset].count_cts = r->retry_count_cts;
121*4882a593Smuzhiyun 	ratetbl->rate[offset].count_rts = r->stats.retry_count_rtscts;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun static void
minstrel_update_rates(struct minstrel_priv * mp,struct minstrel_sta_info * mi)125*4882a593Smuzhiyun minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	struct ieee80211_sta_rates *ratetbl;
128*4882a593Smuzhiyun 	int i = 0;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	ratetbl = kzalloc(sizeof(*ratetbl), GFP_ATOMIC);
131*4882a593Smuzhiyun 	if (!ratetbl)
132*4882a593Smuzhiyun 		return;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* Start with max_tp_rate */
135*4882a593Smuzhiyun 	minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[0]);
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (mp->hw->max_rates >= 3) {
138*4882a593Smuzhiyun 		/* At least 3 tx rates supported, use max_tp_rate2 next */
139*4882a593Smuzhiyun 		minstrel_set_rate(mi, ratetbl, i++, mi->max_tp_rate[1]);
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (mp->hw->max_rates >= 2) {
143*4882a593Smuzhiyun 		/* At least 2 tx rates supported, use max_prob_rate next */
144*4882a593Smuzhiyun 		minstrel_set_rate(mi, ratetbl, i++, mi->max_prob_rate);
145*4882a593Smuzhiyun 	}
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* Use lowest rate last */
148*4882a593Smuzhiyun 	ratetbl->rate[i].idx = mi->lowest_rix;
149*4882a593Smuzhiyun 	ratetbl->rate[i].count = mp->max_retry;
150*4882a593Smuzhiyun 	ratetbl->rate[i].count_cts = mp->max_retry;
151*4882a593Smuzhiyun 	ratetbl->rate[i].count_rts = mp->max_retry;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	rate_control_set_rates(mp->hw, mi->sta, ratetbl);
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /*
157*4882a593Smuzhiyun * Recalculate statistics and counters of a given rate
158*4882a593Smuzhiyun */
159*4882a593Smuzhiyun void
minstrel_calc_rate_stats(struct minstrel_priv * mp,struct minstrel_rate_stats * mrs)160*4882a593Smuzhiyun minstrel_calc_rate_stats(struct minstrel_priv *mp,
161*4882a593Smuzhiyun 			 struct minstrel_rate_stats *mrs)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	unsigned int cur_prob;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (unlikely(mrs->attempts > 0)) {
166*4882a593Smuzhiyun 		mrs->sample_skipped = 0;
167*4882a593Smuzhiyun 		cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
168*4882a593Smuzhiyun 		if (mp->new_avg) {
169*4882a593Smuzhiyun 			minstrel_filter_avg_add(&mrs->prob_avg,
170*4882a593Smuzhiyun 						&mrs->prob_avg_1, cur_prob);
171*4882a593Smuzhiyun 		} else if (unlikely(!mrs->att_hist)) {
172*4882a593Smuzhiyun 			mrs->prob_avg = cur_prob;
173*4882a593Smuzhiyun 		} else {
174*4882a593Smuzhiyun 			/*update exponential weighted moving avarage */
175*4882a593Smuzhiyun 			mrs->prob_avg = minstrel_ewma(mrs->prob_avg,
176*4882a593Smuzhiyun 						      cur_prob,
177*4882a593Smuzhiyun 						      EWMA_LEVEL);
178*4882a593Smuzhiyun 		}
179*4882a593Smuzhiyun 		mrs->att_hist += mrs->attempts;
180*4882a593Smuzhiyun 		mrs->succ_hist += mrs->success;
181*4882a593Smuzhiyun 	} else {
182*4882a593Smuzhiyun 		mrs->sample_skipped++;
183*4882a593Smuzhiyun 	}
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	mrs->last_success = mrs->success;
186*4882a593Smuzhiyun 	mrs->last_attempts = mrs->attempts;
187*4882a593Smuzhiyun 	mrs->success = 0;
188*4882a593Smuzhiyun 	mrs->attempts = 0;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun static void
minstrel_update_stats(struct minstrel_priv * mp,struct minstrel_sta_info * mi)192*4882a593Smuzhiyun minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	u8 tmp_tp_rate[MAX_THR_RATES];
195*4882a593Smuzhiyun 	u8 tmp_prob_rate = 0;
196*4882a593Smuzhiyun 	int i, tmp_cur_tp, tmp_prob_tp;
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	for (i = 0; i < MAX_THR_RATES; i++)
199*4882a593Smuzhiyun 	    tmp_tp_rate[i] = 0;
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	for (i = 0; i < mi->n_rates; i++) {
202*4882a593Smuzhiyun 		struct minstrel_rate *mr = &mi->r[i];
203*4882a593Smuzhiyun 		struct minstrel_rate_stats *mrs = &mi->r[i].stats;
204*4882a593Smuzhiyun 		struct minstrel_rate_stats *tmp_mrs = &mi->r[tmp_prob_rate].stats;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 		/* Update statistics of success probability per rate */
207*4882a593Smuzhiyun 		minstrel_calc_rate_stats(mp, mrs);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		/* Sample less often below the 10% chance of success.
210*4882a593Smuzhiyun 		 * Sample less often above the 95% chance of success. */
211*4882a593Smuzhiyun 		if (mrs->prob_avg > MINSTREL_FRAC(95, 100) ||
212*4882a593Smuzhiyun 		    mrs->prob_avg < MINSTREL_FRAC(10, 100)) {
213*4882a593Smuzhiyun 			mr->adjusted_retry_count = mrs->retry_count >> 1;
214*4882a593Smuzhiyun 			if (mr->adjusted_retry_count > 2)
215*4882a593Smuzhiyun 				mr->adjusted_retry_count = 2;
216*4882a593Smuzhiyun 			mr->sample_limit = 4;
217*4882a593Smuzhiyun 		} else {
218*4882a593Smuzhiyun 			mr->sample_limit = -1;
219*4882a593Smuzhiyun 			mr->adjusted_retry_count = mrs->retry_count;
220*4882a593Smuzhiyun 		}
221*4882a593Smuzhiyun 		if (!mr->adjusted_retry_count)
222*4882a593Smuzhiyun 			mr->adjusted_retry_count = 2;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		minstrel_sort_best_tp_rates(mi, i, tmp_tp_rate);
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 		/* To determine the most robust rate (max_prob_rate) used at
227*4882a593Smuzhiyun 		 * 3rd mmr stage we distinct between two cases:
228*4882a593Smuzhiyun 		 * (1) if any success probabilitiy >= 95%, out of those rates
229*4882a593Smuzhiyun 		 * choose the maximum throughput rate as max_prob_rate
230*4882a593Smuzhiyun 		 * (2) if all success probabilities < 95%, the rate with
231*4882a593Smuzhiyun 		 * highest success probability is chosen as max_prob_rate */
232*4882a593Smuzhiyun 		if (mrs->prob_avg >= MINSTREL_FRAC(95, 100)) {
233*4882a593Smuzhiyun 			tmp_cur_tp = minstrel_get_tp_avg(mr, mrs->prob_avg);
234*4882a593Smuzhiyun 			tmp_prob_tp = minstrel_get_tp_avg(&mi->r[tmp_prob_rate],
235*4882a593Smuzhiyun 							  tmp_mrs->prob_avg);
236*4882a593Smuzhiyun 			if (tmp_cur_tp >= tmp_prob_tp)
237*4882a593Smuzhiyun 				tmp_prob_rate = i;
238*4882a593Smuzhiyun 		} else {
239*4882a593Smuzhiyun 			if (mrs->prob_avg >= tmp_mrs->prob_avg)
240*4882a593Smuzhiyun 				tmp_prob_rate = i;
241*4882a593Smuzhiyun 		}
242*4882a593Smuzhiyun 	}
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* Assign the new rate set */
245*4882a593Smuzhiyun 	memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
246*4882a593Smuzhiyun 	mi->max_prob_rate = tmp_prob_rate;
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun #ifdef CONFIG_MAC80211_DEBUGFS
249*4882a593Smuzhiyun 	/* use fixed index if set */
250*4882a593Smuzhiyun 	if (mp->fixed_rate_idx != -1) {
251*4882a593Smuzhiyun 		mi->max_tp_rate[0] = mp->fixed_rate_idx;
252*4882a593Smuzhiyun 		mi->max_tp_rate[1] = mp->fixed_rate_idx;
253*4882a593Smuzhiyun 		mi->max_prob_rate = mp->fixed_rate_idx;
254*4882a593Smuzhiyun 	}
255*4882a593Smuzhiyun #endif
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/* Reset update timer */
258*4882a593Smuzhiyun 	mi->last_stats_update = jiffies;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	minstrel_update_rates(mp, mi);
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun static void
minstrel_tx_status(void * priv,struct ieee80211_supported_band * sband,void * priv_sta,struct ieee80211_tx_status * st)264*4882a593Smuzhiyun minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
265*4882a593Smuzhiyun 		   void *priv_sta, struct ieee80211_tx_status *st)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = st->info;
268*4882a593Smuzhiyun 	struct minstrel_priv *mp = priv;
269*4882a593Smuzhiyun 	struct minstrel_sta_info *mi = priv_sta;
270*4882a593Smuzhiyun 	struct ieee80211_tx_rate *ar = info->status.rates;
271*4882a593Smuzhiyun 	int i, ndx;
272*4882a593Smuzhiyun 	int success;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	success = !!(info->flags & IEEE80211_TX_STAT_ACK);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
277*4882a593Smuzhiyun 		if (ar[i].idx < 0 || !ar[i].count)
278*4882a593Smuzhiyun 			break;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		ndx = rix_to_ndx(mi, ar[i].idx);
281*4882a593Smuzhiyun 		if (ndx < 0)
282*4882a593Smuzhiyun 			continue;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 		mi->r[ndx].stats.attempts += ar[i].count;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 		if ((i != IEEE80211_TX_MAX_RATES - 1) && (ar[i + 1].idx < 0))
287*4882a593Smuzhiyun 			mi->r[ndx].stats.success += success;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (time_after(jiffies, mi->last_stats_update +
291*4882a593Smuzhiyun 				mp->update_interval / (mp->new_avg ? 2 : 1)))
292*4882a593Smuzhiyun 		minstrel_update_stats(mp, mi);
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun static inline unsigned int
minstrel_get_retry_count(struct minstrel_rate * mr,struct ieee80211_tx_info * info)297*4882a593Smuzhiyun minstrel_get_retry_count(struct minstrel_rate *mr,
298*4882a593Smuzhiyun 			 struct ieee80211_tx_info *info)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	u8 retry = mr->adjusted_retry_count;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	if (info->control.use_rts)
303*4882a593Smuzhiyun 		retry = max_t(u8, 2, min(mr->stats.retry_count_rtscts, retry));
304*4882a593Smuzhiyun 	else if (info->control.use_cts_prot)
305*4882a593Smuzhiyun 		retry = max_t(u8, 2, min(mr->retry_count_cts, retry));
306*4882a593Smuzhiyun 	return retry;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun static int
minstrel_get_next_sample(struct minstrel_sta_info * mi)311*4882a593Smuzhiyun minstrel_get_next_sample(struct minstrel_sta_info *mi)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	unsigned int sample_ndx;
314*4882a593Smuzhiyun 	sample_ndx = SAMPLE_TBL(mi, mi->sample_row, mi->sample_column);
315*4882a593Smuzhiyun 	mi->sample_row++;
316*4882a593Smuzhiyun 	if ((int) mi->sample_row >= mi->n_rates) {
317*4882a593Smuzhiyun 		mi->sample_row = 0;
318*4882a593Smuzhiyun 		mi->sample_column++;
319*4882a593Smuzhiyun 		if (mi->sample_column >= SAMPLE_COLUMNS)
320*4882a593Smuzhiyun 			mi->sample_column = 0;
321*4882a593Smuzhiyun 	}
322*4882a593Smuzhiyun 	return sample_ndx;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun static void
minstrel_get_rate(void * priv,struct ieee80211_sta * sta,void * priv_sta,struct ieee80211_tx_rate_control * txrc)326*4882a593Smuzhiyun minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
327*4882a593Smuzhiyun 		  void *priv_sta, struct ieee80211_tx_rate_control *txrc)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct sk_buff *skb = txrc->skb;
330*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
331*4882a593Smuzhiyun 	struct minstrel_sta_info *mi = priv_sta;
332*4882a593Smuzhiyun 	struct minstrel_priv *mp = priv;
333*4882a593Smuzhiyun 	struct ieee80211_tx_rate *rate = &info->control.rates[0];
334*4882a593Smuzhiyun 	struct minstrel_rate *msr, *mr;
335*4882a593Smuzhiyun 	unsigned int ndx;
336*4882a593Smuzhiyun 	bool mrr_capable;
337*4882a593Smuzhiyun 	bool prev_sample;
338*4882a593Smuzhiyun 	int delta;
339*4882a593Smuzhiyun 	int sampling_ratio;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	/* check multi-rate-retry capabilities & adjust lookaround_rate */
342*4882a593Smuzhiyun 	mrr_capable = mp->has_mrr &&
343*4882a593Smuzhiyun 		      !txrc->rts &&
344*4882a593Smuzhiyun 		      !txrc->bss_conf->use_cts_prot;
345*4882a593Smuzhiyun 	if (mrr_capable)
346*4882a593Smuzhiyun 		sampling_ratio = mp->lookaround_rate_mrr;
347*4882a593Smuzhiyun 	else
348*4882a593Smuzhiyun 		sampling_ratio = mp->lookaround_rate;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	/* increase sum packet counter */
351*4882a593Smuzhiyun 	mi->total_packets++;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun #ifdef CONFIG_MAC80211_DEBUGFS
354*4882a593Smuzhiyun 	if (mp->fixed_rate_idx != -1)
355*4882a593Smuzhiyun 		return;
356*4882a593Smuzhiyun #endif
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/* Don't use EAPOL frames for sampling on non-mrr hw */
359*4882a593Smuzhiyun 	if (mp->hw->max_rates == 1 &&
360*4882a593Smuzhiyun 	    (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
361*4882a593Smuzhiyun 		return;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	delta = (mi->total_packets * sampling_ratio / 100) -
364*4882a593Smuzhiyun 			mi->sample_packets;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	/* delta < 0: no sampling required */
367*4882a593Smuzhiyun 	prev_sample = mi->prev_sample;
368*4882a593Smuzhiyun 	mi->prev_sample = false;
369*4882a593Smuzhiyun 	if (delta < 0 || (!mrr_capable && prev_sample))
370*4882a593Smuzhiyun 		return;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (mi->total_packets >= 10000) {
373*4882a593Smuzhiyun 		mi->sample_packets = 0;
374*4882a593Smuzhiyun 		mi->total_packets = 0;
375*4882a593Smuzhiyun 	} else if (delta > mi->n_rates * 2) {
376*4882a593Smuzhiyun 		/* With multi-rate retry, not every planned sample
377*4882a593Smuzhiyun 		 * attempt actually gets used, due to the way the retry
378*4882a593Smuzhiyun 		 * chain is set up - [max_tp,sample,prob,lowest] for
379*4882a593Smuzhiyun 		 * sample_rate < max_tp.
380*4882a593Smuzhiyun 		 *
381*4882a593Smuzhiyun 		 * If there's too much sampling backlog and the link
382*4882a593Smuzhiyun 		 * starts getting worse, minstrel would start bursting
383*4882a593Smuzhiyun 		 * out lots of sampling frames, which would result
384*4882a593Smuzhiyun 		 * in a large throughput loss. */
385*4882a593Smuzhiyun 		mi->sample_packets += (delta - mi->n_rates * 2);
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	/* get next random rate sample */
389*4882a593Smuzhiyun 	ndx = minstrel_get_next_sample(mi);
390*4882a593Smuzhiyun 	msr = &mi->r[ndx];
391*4882a593Smuzhiyun 	mr = &mi->r[mi->max_tp_rate[0]];
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	/* Decide if direct ( 1st mrr stage) or indirect (2nd mrr stage)
394*4882a593Smuzhiyun 	 * rate sampling method should be used.
395*4882a593Smuzhiyun 	 * Respect such rates that are not sampled for 20 interations.
396*4882a593Smuzhiyun 	 */
397*4882a593Smuzhiyun 	if (msr->perfect_tx_time < mr->perfect_tx_time ||
398*4882a593Smuzhiyun 	    msr->stats.sample_skipped >= 20) {
399*4882a593Smuzhiyun 		if (!msr->sample_limit)
400*4882a593Smuzhiyun 			return;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 		mi->sample_packets++;
403*4882a593Smuzhiyun 		if (msr->sample_limit > 0)
404*4882a593Smuzhiyun 			msr->sample_limit--;
405*4882a593Smuzhiyun 	}
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	/* If we're not using MRR and the sampling rate already
408*4882a593Smuzhiyun 	 * has a probability of >95%, we shouldn't be attempting
409*4882a593Smuzhiyun 	 * to use it, as this only wastes precious airtime */
410*4882a593Smuzhiyun 	if (!mrr_capable &&
411*4882a593Smuzhiyun 	   (mi->r[ndx].stats.prob_avg > MINSTREL_FRAC(95, 100)))
412*4882a593Smuzhiyun 		return;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	mi->prev_sample = true;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	rate->idx = mi->r[ndx].rix;
417*4882a593Smuzhiyun 	rate->count = minstrel_get_retry_count(&mi->r[ndx], info);
418*4882a593Smuzhiyun 	info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun static void
calc_rate_durations(enum nl80211_band band,struct minstrel_rate * d,struct ieee80211_rate * rate,struct cfg80211_chan_def * chandef)423*4882a593Smuzhiyun calc_rate_durations(enum nl80211_band band,
424*4882a593Smuzhiyun 		    struct minstrel_rate *d,
425*4882a593Smuzhiyun 		    struct ieee80211_rate *rate,
426*4882a593Smuzhiyun 		    struct cfg80211_chan_def *chandef)
427*4882a593Smuzhiyun {
428*4882a593Smuzhiyun 	int erp = !!(rate->flags & IEEE80211_RATE_ERP_G);
429*4882a593Smuzhiyun 	int shift = ieee80211_chandef_get_shift(chandef);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	d->perfect_tx_time = ieee80211_frame_duration(band, 1200,
432*4882a593Smuzhiyun 			DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
433*4882a593Smuzhiyun 			shift);
434*4882a593Smuzhiyun 	d->ack_time = ieee80211_frame_duration(band, 10,
435*4882a593Smuzhiyun 			DIV_ROUND_UP(rate->bitrate, 1 << shift), erp, 1,
436*4882a593Smuzhiyun 			shift);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun static void
init_sample_table(struct minstrel_sta_info * mi)440*4882a593Smuzhiyun init_sample_table(struct minstrel_sta_info *mi)
441*4882a593Smuzhiyun {
442*4882a593Smuzhiyun 	unsigned int i, col, new_idx;
443*4882a593Smuzhiyun 	u8 rnd[8];
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	mi->sample_column = 0;
446*4882a593Smuzhiyun 	mi->sample_row = 0;
447*4882a593Smuzhiyun 	memset(mi->sample_table, 0xff, SAMPLE_COLUMNS * mi->n_rates);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	for (col = 0; col < SAMPLE_COLUMNS; col++) {
450*4882a593Smuzhiyun 		prandom_bytes(rnd, sizeof(rnd));
451*4882a593Smuzhiyun 		for (i = 0; i < mi->n_rates; i++) {
452*4882a593Smuzhiyun 			new_idx = (i + rnd[i & 7]) % mi->n_rates;
453*4882a593Smuzhiyun 			while (SAMPLE_TBL(mi, new_idx, col) != 0xff)
454*4882a593Smuzhiyun 				new_idx = (new_idx + 1) % mi->n_rates;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 			SAMPLE_TBL(mi, new_idx, col) = i;
457*4882a593Smuzhiyun 		}
458*4882a593Smuzhiyun 	}
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun static void
minstrel_rate_init(void * priv,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * priv_sta)462*4882a593Smuzhiyun minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
463*4882a593Smuzhiyun 		   struct cfg80211_chan_def *chandef,
464*4882a593Smuzhiyun 		   struct ieee80211_sta *sta, void *priv_sta)
465*4882a593Smuzhiyun {
466*4882a593Smuzhiyun 	struct minstrel_sta_info *mi = priv_sta;
467*4882a593Smuzhiyun 	struct minstrel_priv *mp = priv;
468*4882a593Smuzhiyun 	struct ieee80211_rate *ctl_rate;
469*4882a593Smuzhiyun 	unsigned int i, n = 0;
470*4882a593Smuzhiyun 	unsigned int t_slot = 9; /* FIXME: get real slot time */
471*4882a593Smuzhiyun 	u32 rate_flags;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	mi->sta = sta;
474*4882a593Smuzhiyun 	mi->lowest_rix = rate_lowest_index(sband, sta);
475*4882a593Smuzhiyun 	ctl_rate = &sband->bitrates[mi->lowest_rix];
476*4882a593Smuzhiyun 	mi->sp_ack_dur = ieee80211_frame_duration(sband->band, 10,
477*4882a593Smuzhiyun 				ctl_rate->bitrate,
478*4882a593Smuzhiyun 				!!(ctl_rate->flags & IEEE80211_RATE_ERP_G), 1,
479*4882a593Smuzhiyun 				ieee80211_chandef_get_shift(chandef));
480*4882a593Smuzhiyun 
481*4882a593Smuzhiyun 	rate_flags = ieee80211_chandef_rate_flags(&mp->hw->conf.chandef);
482*4882a593Smuzhiyun 	memset(mi->max_tp_rate, 0, sizeof(mi->max_tp_rate));
483*4882a593Smuzhiyun 	mi->max_prob_rate = 0;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	for (i = 0; i < sband->n_bitrates; i++) {
486*4882a593Smuzhiyun 		struct minstrel_rate *mr = &mi->r[n];
487*4882a593Smuzhiyun 		struct minstrel_rate_stats *mrs = &mi->r[n].stats;
488*4882a593Smuzhiyun 		unsigned int tx_time = 0, tx_time_cts = 0, tx_time_rtscts = 0;
489*4882a593Smuzhiyun 		unsigned int tx_time_single;
490*4882a593Smuzhiyun 		unsigned int cw = mp->cw_min;
491*4882a593Smuzhiyun 		int shift;
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 		if (!rate_supported(sta, sband->band, i))
494*4882a593Smuzhiyun 			continue;
495*4882a593Smuzhiyun 		if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
496*4882a593Smuzhiyun 			continue;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		n++;
499*4882a593Smuzhiyun 		memset(mr, 0, sizeof(*mr));
500*4882a593Smuzhiyun 		memset(mrs, 0, sizeof(*mrs));
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun 		mr->rix = i;
503*4882a593Smuzhiyun 		shift = ieee80211_chandef_get_shift(chandef);
504*4882a593Smuzhiyun 		mr->bitrate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
505*4882a593Smuzhiyun 					   (1 << shift) * 5);
506*4882a593Smuzhiyun 		calc_rate_durations(sband->band, mr, &sband->bitrates[i],
507*4882a593Smuzhiyun 				    chandef);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		/* calculate maximum number of retransmissions before
510*4882a593Smuzhiyun 		 * fallback (based on maximum segment size) */
511*4882a593Smuzhiyun 		mr->sample_limit = -1;
512*4882a593Smuzhiyun 		mrs->retry_count = 1;
513*4882a593Smuzhiyun 		mr->retry_count_cts = 1;
514*4882a593Smuzhiyun 		mrs->retry_count_rtscts = 1;
515*4882a593Smuzhiyun 		tx_time = mr->perfect_tx_time + mi->sp_ack_dur;
516*4882a593Smuzhiyun 		do {
517*4882a593Smuzhiyun 			/* add one retransmission */
518*4882a593Smuzhiyun 			tx_time_single = mr->ack_time + mr->perfect_tx_time;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 			/* contention window */
521*4882a593Smuzhiyun 			tx_time_single += (t_slot * cw) >> 1;
522*4882a593Smuzhiyun 			cw = min((cw << 1) | 1, mp->cw_max);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 			tx_time += tx_time_single;
525*4882a593Smuzhiyun 			tx_time_cts += tx_time_single + mi->sp_ack_dur;
526*4882a593Smuzhiyun 			tx_time_rtscts += tx_time_single + 2 * mi->sp_ack_dur;
527*4882a593Smuzhiyun 			if ((tx_time_cts < mp->segment_size) &&
528*4882a593Smuzhiyun 				(mr->retry_count_cts < mp->max_retry))
529*4882a593Smuzhiyun 				mr->retry_count_cts++;
530*4882a593Smuzhiyun 			if ((tx_time_rtscts < mp->segment_size) &&
531*4882a593Smuzhiyun 				(mrs->retry_count_rtscts < mp->max_retry))
532*4882a593Smuzhiyun 				mrs->retry_count_rtscts++;
533*4882a593Smuzhiyun 		} while ((tx_time < mp->segment_size) &&
534*4882a593Smuzhiyun 				(++mr->stats.retry_count < mp->max_retry));
535*4882a593Smuzhiyun 		mr->adjusted_retry_count = mrs->retry_count;
536*4882a593Smuzhiyun 		if (!(sband->bitrates[i].flags & IEEE80211_RATE_ERP_G))
537*4882a593Smuzhiyun 			mr->retry_count_cts = mrs->retry_count;
538*4882a593Smuzhiyun 	}
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	for (i = n; i < sband->n_bitrates; i++) {
541*4882a593Smuzhiyun 		struct minstrel_rate *mr = &mi->r[i];
542*4882a593Smuzhiyun 		mr->rix = -1;
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	mi->n_rates = n;
546*4882a593Smuzhiyun 	mi->last_stats_update = jiffies;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	init_sample_table(mi);
549*4882a593Smuzhiyun 	minstrel_update_rates(mp, mi);
550*4882a593Smuzhiyun }
551*4882a593Smuzhiyun 
minstrel_get_expected_throughput(void * priv_sta)552*4882a593Smuzhiyun static u32 minstrel_get_expected_throughput(void *priv_sta)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun 	struct minstrel_sta_info *mi = priv_sta;
555*4882a593Smuzhiyun 	struct minstrel_rate_stats *tmp_mrs;
556*4882a593Smuzhiyun 	int idx = mi->max_tp_rate[0];
557*4882a593Smuzhiyun 	int tmp_cur_tp;
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	/* convert pkt per sec in kbps (1200 is the average pkt size used for
560*4882a593Smuzhiyun 	 * computing cur_tp
561*4882a593Smuzhiyun 	 */
562*4882a593Smuzhiyun 	tmp_mrs = &mi->r[idx].stats;
563*4882a593Smuzhiyun 	tmp_cur_tp = minstrel_get_tp_avg(&mi->r[idx], tmp_mrs->prob_avg) * 10;
564*4882a593Smuzhiyun 	tmp_cur_tp = tmp_cur_tp * 1200 * 8 / 1024;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	return tmp_cur_tp;
567*4882a593Smuzhiyun }
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun const struct rate_control_ops mac80211_minstrel = {
570*4882a593Smuzhiyun 	.tx_status_ext = minstrel_tx_status,
571*4882a593Smuzhiyun 	.get_rate = minstrel_get_rate,
572*4882a593Smuzhiyun 	.rate_init = minstrel_rate_init,
573*4882a593Smuzhiyun 	.get_expected_throughput = minstrel_get_expected_throughput,
574*4882a593Smuzhiyun };
575