xref: /dflybsd-src/sys/dev/netif/ath/ath_rate/onoe/onoe.c (revision 572ff6f6e8b95055988f178b6ba12ce77bb5b3c2)
1*572ff6f6SMatthew Dillon /*-
2*572ff6f6SMatthew Dillon  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3*572ff6f6SMatthew Dillon  * All rights reserved.
4*572ff6f6SMatthew Dillon  *
5*572ff6f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6*572ff6f6SMatthew Dillon  * modification, are permitted provided that the following conditions
7*572ff6f6SMatthew Dillon  * are met:
8*572ff6f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
9*572ff6f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer,
10*572ff6f6SMatthew Dillon  *    without modification.
11*572ff6f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*572ff6f6SMatthew Dillon  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13*572ff6f6SMatthew Dillon  *    redistribution must be conditioned upon including a substantially
14*572ff6f6SMatthew Dillon  *    similar Disclaimer requirement for further binary redistribution.
15*572ff6f6SMatthew Dillon  *
16*572ff6f6SMatthew Dillon  * NO WARRANTY
17*572ff6f6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*572ff6f6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*572ff6f6SMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20*572ff6f6SMatthew Dillon  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21*572ff6f6SMatthew Dillon  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22*572ff6f6SMatthew Dillon  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*572ff6f6SMatthew Dillon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*572ff6f6SMatthew Dillon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*572ff6f6SMatthew Dillon  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*572ff6f6SMatthew Dillon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27*572ff6f6SMatthew Dillon  * THE POSSIBILITY OF SUCH DAMAGES.
28*572ff6f6SMatthew Dillon  */
29*572ff6f6SMatthew Dillon 
30*572ff6f6SMatthew Dillon #include <sys/cdefs.h>
31*572ff6f6SMatthew Dillon __FBSDID("$FreeBSD$");
32*572ff6f6SMatthew Dillon 
33*572ff6f6SMatthew Dillon /*
34*572ff6f6SMatthew Dillon  * Atsushi Onoe's rate control algorithm.
35*572ff6f6SMatthew Dillon  */
36*572ff6f6SMatthew Dillon #include "opt_ath.h"
37*572ff6f6SMatthew Dillon #include "opt_inet.h"
38*572ff6f6SMatthew Dillon #include "opt_wlan.h"
39*572ff6f6SMatthew Dillon 
40*572ff6f6SMatthew Dillon #include <sys/param.h>
41*572ff6f6SMatthew Dillon #include <sys/systm.h>
42*572ff6f6SMatthew Dillon #include <sys/sysctl.h>
43*572ff6f6SMatthew Dillon #include <sys/kernel.h>
44*572ff6f6SMatthew Dillon #include <sys/lock.h>
45*572ff6f6SMatthew Dillon #include <sys/mutex.h>
46*572ff6f6SMatthew Dillon #include <sys/errno.h>
47*572ff6f6SMatthew Dillon 
48*572ff6f6SMatthew Dillon #include <machine/bus.h>
49*572ff6f6SMatthew Dillon #include <machine/resource.h>
50*572ff6f6SMatthew Dillon #include <sys/bus.h>
51*572ff6f6SMatthew Dillon 
52*572ff6f6SMatthew Dillon #include <sys/socket.h>
53*572ff6f6SMatthew Dillon 
54*572ff6f6SMatthew Dillon #include <net/if.h>
55*572ff6f6SMatthew Dillon #include <net/if_media.h>
56*572ff6f6SMatthew Dillon #include <net/if_arp.h>
57*572ff6f6SMatthew Dillon #include <net/ethernet.h>		/* XXX for ether_sprintf */
58*572ff6f6SMatthew Dillon 
59*572ff6f6SMatthew Dillon #include <net80211/ieee80211_var.h>
60*572ff6f6SMatthew Dillon 
61*572ff6f6SMatthew Dillon #include <net/bpf.h>
62*572ff6f6SMatthew Dillon 
63*572ff6f6SMatthew Dillon #ifdef INET
64*572ff6f6SMatthew Dillon #include <netinet/in.h>
65*572ff6f6SMatthew Dillon #include <netinet/if_ether.h>
66*572ff6f6SMatthew Dillon #endif
67*572ff6f6SMatthew Dillon 
68*572ff6f6SMatthew Dillon #include <dev/ath/if_athvar.h>
69*572ff6f6SMatthew Dillon #include <dev/ath/ath_rate/onoe/onoe.h>
70*572ff6f6SMatthew Dillon #include <dev/ath/ath_hal/ah_desc.h>
71*572ff6f6SMatthew Dillon 
72*572ff6f6SMatthew Dillon /*
73*572ff6f6SMatthew Dillon  * Default parameters for the rate control algorithm.  These are
74*572ff6f6SMatthew Dillon  * all tunable with sysctls.  The rate controller runs periodically
75*572ff6f6SMatthew Dillon  * (each ath_rateinterval ms) analyzing transmit statistics for each
76*572ff6f6SMatthew Dillon  * neighbor/station (when operating in station mode this is only the AP).
77*572ff6f6SMatthew Dillon  * If transmits look to be working well over a sampling period then
78*572ff6f6SMatthew Dillon  * it gives a "raise rate credit".  If transmits look to not be working
79*572ff6f6SMatthew Dillon  * well than it deducts a credit.  If the credits cross a threshold then
80*572ff6f6SMatthew Dillon  * the transmit rate is raised.  Various error conditions force the
81*572ff6f6SMatthew Dillon  * the transmit rate to be dropped.
82*572ff6f6SMatthew Dillon  *
83*572ff6f6SMatthew Dillon  * The decision to issue/deduct a credit is based on the errors and
84*572ff6f6SMatthew Dillon  * retries accumulated over the sampling period.  ath_rate_raise defines
85*572ff6f6SMatthew Dillon  * the percent of retransmits for which a credit is issued/deducted.
86*572ff6f6SMatthew Dillon  * ath_rate_raise_threshold defines the threshold on credits at which
87*572ff6f6SMatthew Dillon  * the transmit rate is increased.
88*572ff6f6SMatthew Dillon  *
89*572ff6f6SMatthew Dillon  * XXX this algorithm is flawed.
90*572ff6f6SMatthew Dillon  */
91*572ff6f6SMatthew Dillon static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
92*572ff6f6SMatthew Dillon static	int ath_rate_raise = 10;		/* add credit threshold */
93*572ff6f6SMatthew Dillon static	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
94*572ff6f6SMatthew Dillon 
95*572ff6f6SMatthew Dillon static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
96*572ff6f6SMatthew Dillon 			int rate);
97*572ff6f6SMatthew Dillon static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
98*572ff6f6SMatthew Dillon static void	ath_rate_ctl(void *, struct ieee80211_node *);
99*572ff6f6SMatthew Dillon 
100*572ff6f6SMatthew Dillon void
101*572ff6f6SMatthew Dillon ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
102*572ff6f6SMatthew Dillon {
103*572ff6f6SMatthew Dillon 	/* NB: assumed to be zero'd by caller */
104*572ff6f6SMatthew Dillon }
105*572ff6f6SMatthew Dillon 
106*572ff6f6SMatthew Dillon void
107*572ff6f6SMatthew Dillon ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
108*572ff6f6SMatthew Dillon {
109*572ff6f6SMatthew Dillon }
110*572ff6f6SMatthew Dillon 
111*572ff6f6SMatthew Dillon void
112*572ff6f6SMatthew Dillon ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
113*572ff6f6SMatthew Dillon 	int shortPreamble, size_t frameLen,
114*572ff6f6SMatthew Dillon 	u_int8_t *rix, int *try0, u_int8_t *txrate)
115*572ff6f6SMatthew Dillon {
116*572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
117*572ff6f6SMatthew Dillon 
118*572ff6f6SMatthew Dillon 	*rix = on->on_tx_rix0;
119*572ff6f6SMatthew Dillon 	*try0 = on->on_tx_try0;
120*572ff6f6SMatthew Dillon 	if (shortPreamble)
121*572ff6f6SMatthew Dillon 		*txrate = on->on_tx_rate0sp;
122*572ff6f6SMatthew Dillon 	else
123*572ff6f6SMatthew Dillon 		*txrate = on->on_tx_rate0;
124*572ff6f6SMatthew Dillon }
125*572ff6f6SMatthew Dillon 
126*572ff6f6SMatthew Dillon /*
127*572ff6f6SMatthew Dillon  * Get the TX rates.
128*572ff6f6SMatthew Dillon  *
129*572ff6f6SMatthew Dillon  * The short preamble bits aren't set here; the caller should augment
130*572ff6f6SMatthew Dillon  * the returned rate with the relevant preamble rate flag.
131*572ff6f6SMatthew Dillon  */
132*572ff6f6SMatthew Dillon void
133*572ff6f6SMatthew Dillon ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
134*572ff6f6SMatthew Dillon     uint8_t rix0, struct ath_rc_series *rc)
135*572ff6f6SMatthew Dillon {
136*572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
137*572ff6f6SMatthew Dillon 
138*572ff6f6SMatthew Dillon 	rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
139*572ff6f6SMatthew Dillon 
140*572ff6f6SMatthew Dillon 	rc[0].rix = on->on_tx_rate0;
141*572ff6f6SMatthew Dillon 	rc[1].rix = on->on_tx_rate1;
142*572ff6f6SMatthew Dillon 	rc[2].rix = on->on_tx_rate2;
143*572ff6f6SMatthew Dillon 	rc[3].rix = on->on_tx_rate3;
144*572ff6f6SMatthew Dillon 
145*572ff6f6SMatthew Dillon 	rc[0].tries = on->on_tx_try0;
146*572ff6f6SMatthew Dillon 	rc[1].tries = 2;
147*572ff6f6SMatthew Dillon 	rc[2].tries = 2;
148*572ff6f6SMatthew Dillon 	rc[3].tries = 2;
149*572ff6f6SMatthew Dillon }
150*572ff6f6SMatthew Dillon 
151*572ff6f6SMatthew Dillon void
152*572ff6f6SMatthew Dillon ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
153*572ff6f6SMatthew Dillon 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
154*572ff6f6SMatthew Dillon {
155*572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
156*572ff6f6SMatthew Dillon 
157*572ff6f6SMatthew Dillon 	ath_hal_setupxtxdesc(sc->sc_ah, ds
158*572ff6f6SMatthew Dillon 		, on->on_tx_rate1sp, 2	/* series 1 */
159*572ff6f6SMatthew Dillon 		, on->on_tx_rate2sp, 2	/* series 2 */
160*572ff6f6SMatthew Dillon 		, on->on_tx_rate3sp, 2	/* series 3 */
161*572ff6f6SMatthew Dillon 	);
162*572ff6f6SMatthew Dillon }
163*572ff6f6SMatthew Dillon 
164*572ff6f6SMatthew Dillon void
165*572ff6f6SMatthew Dillon ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
166*572ff6f6SMatthew Dillon 	const struct ath_rc_series *rc, const struct ath_tx_status *ts,
167*572ff6f6SMatthew Dillon 	int frame_size, int nframes, int nbad)
168*572ff6f6SMatthew Dillon {
169*572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
170*572ff6f6SMatthew Dillon 
171*572ff6f6SMatthew Dillon 	if (ts->ts_status == 0)
172*572ff6f6SMatthew Dillon 		on->on_tx_ok++;
173*572ff6f6SMatthew Dillon 	else
174*572ff6f6SMatthew Dillon 		on->on_tx_err++;
175*572ff6f6SMatthew Dillon 	on->on_tx_retr += ts->ts_shortretry
176*572ff6f6SMatthew Dillon 			+ ts->ts_longretry;
177*572ff6f6SMatthew Dillon 	if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
178*572ff6f6SMatthew Dillon 		ath_rate_ctl(sc, &an->an_node);
179*572ff6f6SMatthew Dillon 		on->on_ticks = ticks;
180*572ff6f6SMatthew Dillon 	}
181*572ff6f6SMatthew Dillon }
182*572ff6f6SMatthew Dillon 
183*572ff6f6SMatthew Dillon void
184*572ff6f6SMatthew Dillon ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
185*572ff6f6SMatthew Dillon {
186*572ff6f6SMatthew Dillon 	if (isnew)
187*572ff6f6SMatthew Dillon 		ath_rate_ctl_start(sc, &an->an_node);
188*572ff6f6SMatthew Dillon }
189*572ff6f6SMatthew Dillon 
190*572ff6f6SMatthew Dillon static void
191*572ff6f6SMatthew Dillon ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
192*572ff6f6SMatthew Dillon {
193*572ff6f6SMatthew Dillon 	struct ath_node *an = ATH_NODE(ni);
194*572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
195*572ff6f6SMatthew Dillon 	struct ieee80211vap *vap = ni->ni_vap;
196*572ff6f6SMatthew Dillon 	const HAL_RATE_TABLE *rt = sc->sc_currates;
197*572ff6f6SMatthew Dillon 	u_int8_t rix;
198*572ff6f6SMatthew Dillon 
199*572ff6f6SMatthew Dillon 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
200*572ff6f6SMatthew Dillon 
201*572ff6f6SMatthew Dillon 	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
202*572ff6f6SMatthew Dillon 	     "%s: set xmit rate to %dM", __func__,
203*572ff6f6SMatthew Dillon 	     ni->ni_rates.rs_nrates > 0 ?
204*572ff6f6SMatthew Dillon 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
205*572ff6f6SMatthew Dillon 
206*572ff6f6SMatthew Dillon 	/*
207*572ff6f6SMatthew Dillon 	 * Before associating a node has no rate set setup
208*572ff6f6SMatthew Dillon 	 * so we can't calculate any transmit codes to use.
209*572ff6f6SMatthew Dillon 	 * This is ok since we should never be sending anything
210*572ff6f6SMatthew Dillon 	 * but management frames and those always go at the
211*572ff6f6SMatthew Dillon 	 * lowest hardware rate.
212*572ff6f6SMatthew Dillon 	 */
213*572ff6f6SMatthew Dillon 	if (ni->ni_rates.rs_nrates == 0)
214*572ff6f6SMatthew Dillon 		goto done;
215*572ff6f6SMatthew Dillon 	on->on_rix = rate;
216*572ff6f6SMatthew Dillon 	ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
217*572ff6f6SMatthew Dillon 	on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
218*572ff6f6SMatthew Dillon 	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
219*572ff6f6SMatthew Dillon 
220*572ff6f6SMatthew Dillon 	on->on_tx_rate0sp = on->on_tx_rate0 |
221*572ff6f6SMatthew Dillon 		rt->info[on->on_tx_rix0].shortPreamble;
222*572ff6f6SMatthew Dillon 	if (sc->sc_mrretry) {
223*572ff6f6SMatthew Dillon 		/*
224*572ff6f6SMatthew Dillon 		 * Hardware supports multi-rate retry; setup two
225*572ff6f6SMatthew Dillon 		 * step-down retry rates and make the lowest rate
226*572ff6f6SMatthew Dillon 		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
227*572ff6f6SMatthew Dillon 		 * respectively (4 is set here, the rest are fixed
228*572ff6f6SMatthew Dillon 		 * in the xmit routine).
229*572ff6f6SMatthew Dillon 		 */
230*572ff6f6SMatthew Dillon 		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
231*572ff6f6SMatthew Dillon 		if (--rate >= 0) {
232*572ff6f6SMatthew Dillon 			rix = sc->sc_rixmap[
233*572ff6f6SMatthew Dillon 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
234*572ff6f6SMatthew Dillon 			on->on_tx_rate1 = rt->info[rix].rateCode;
235*572ff6f6SMatthew Dillon 			on->on_tx_rate1sp = on->on_tx_rate1 |
236*572ff6f6SMatthew Dillon 				rt->info[rix].shortPreamble;
237*572ff6f6SMatthew Dillon 		} else {
238*572ff6f6SMatthew Dillon 			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
239*572ff6f6SMatthew Dillon 		}
240*572ff6f6SMatthew Dillon 		if (--rate >= 0) {
241*572ff6f6SMatthew Dillon 			rix = sc->sc_rixmap[
242*572ff6f6SMatthew Dillon 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
243*572ff6f6SMatthew Dillon 			on->on_tx_rate2 = rt->info[rix].rateCode;
244*572ff6f6SMatthew Dillon 			on->on_tx_rate2sp = on->on_tx_rate2 |
245*572ff6f6SMatthew Dillon 				rt->info[rix].shortPreamble;
246*572ff6f6SMatthew Dillon 		} else {
247*572ff6f6SMatthew Dillon 			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
248*572ff6f6SMatthew Dillon 		}
249*572ff6f6SMatthew Dillon 		if (rate > 0) {
250*572ff6f6SMatthew Dillon 			/* NB: only do this if we didn't already do it above */
251*572ff6f6SMatthew Dillon 			on->on_tx_rate3 = rt->info[0].rateCode;
252*572ff6f6SMatthew Dillon 			on->on_tx_rate3sp =
253*572ff6f6SMatthew Dillon 				on->on_tx_rate3 | rt->info[0].shortPreamble;
254*572ff6f6SMatthew Dillon 		} else {
255*572ff6f6SMatthew Dillon 			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
256*572ff6f6SMatthew Dillon 		}
257*572ff6f6SMatthew Dillon 	} else {
258*572ff6f6SMatthew Dillon 		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
259*572ff6f6SMatthew Dillon 		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
260*572ff6f6SMatthew Dillon 		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
261*572ff6f6SMatthew Dillon 		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
262*572ff6f6SMatthew Dillon 	}
263*572ff6f6SMatthew Dillon done:
264*572ff6f6SMatthew Dillon 	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
265*572ff6f6SMatthew Dillon 
266*572ff6f6SMatthew Dillon 	on->on_interval = ath_rateinterval;
267*572ff6f6SMatthew Dillon 	if (vap->iv_opmode == IEEE80211_M_STA)
268*572ff6f6SMatthew Dillon 		on->on_interval /= 2;
269*572ff6f6SMatthew Dillon 	on->on_interval = (on->on_interval * hz) / 1000;
270*572ff6f6SMatthew Dillon }
271*572ff6f6SMatthew Dillon 
272*572ff6f6SMatthew Dillon /*
273*572ff6f6SMatthew Dillon  * Set the starting transmit rate for a node.
274*572ff6f6SMatthew Dillon  */
275*572ff6f6SMatthew Dillon static void
276*572ff6f6SMatthew Dillon ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
277*572ff6f6SMatthew Dillon {
278*572ff6f6SMatthew Dillon #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
279*572ff6f6SMatthew Dillon 	const struct ieee80211_txparam *tp = ni->ni_txparms;
280*572ff6f6SMatthew Dillon 	int srate;
281*572ff6f6SMatthew Dillon 
282*572ff6f6SMatthew Dillon 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
283*572ff6f6SMatthew Dillon 	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
284*572ff6f6SMatthew Dillon 		/*
285*572ff6f6SMatthew Dillon 		 * No fixed rate is requested. For 11b start with
286*572ff6f6SMatthew Dillon 		 * the highest negotiated rate; otherwise, for 11g
287*572ff6f6SMatthew Dillon 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
288*572ff6f6SMatthew Dillon 		 */
289*572ff6f6SMatthew Dillon 		srate = ni->ni_rates.rs_nrates - 1;
290*572ff6f6SMatthew Dillon 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
291*572ff6f6SMatthew Dillon 			/*
292*572ff6f6SMatthew Dillon 			 * Scan the negotiated rate set to find the
293*572ff6f6SMatthew Dillon 			 * closest rate.
294*572ff6f6SMatthew Dillon 			 */
295*572ff6f6SMatthew Dillon 			/* NB: the rate set is assumed sorted */
296*572ff6f6SMatthew Dillon 			for (; srate >= 0 && RATE(srate) > 72; srate--)
297*572ff6f6SMatthew Dillon 				;
298*572ff6f6SMatthew Dillon 		}
299*572ff6f6SMatthew Dillon 	} else {
300*572ff6f6SMatthew Dillon 		/*
301*572ff6f6SMatthew Dillon 		 * A fixed rate is to be used; ic_fixed_rate is the
302*572ff6f6SMatthew Dillon 		 * IEEE code for this rate (sans basic bit).  Convert this
303*572ff6f6SMatthew Dillon 		 * to the index into the negotiated rate set for
304*572ff6f6SMatthew Dillon 		 * the node.  We know the rate is there because the
305*572ff6f6SMatthew Dillon 		 * rate set is checked when the station associates.
306*572ff6f6SMatthew Dillon 		 */
307*572ff6f6SMatthew Dillon 		/* NB: the rate set is assumed sorted */
308*572ff6f6SMatthew Dillon 		srate = ni->ni_rates.rs_nrates - 1;
309*572ff6f6SMatthew Dillon 		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
310*572ff6f6SMatthew Dillon 			;
311*572ff6f6SMatthew Dillon 	}
312*572ff6f6SMatthew Dillon 	/*
313*572ff6f6SMatthew Dillon 	 * The selected rate may not be available due to races
314*572ff6f6SMatthew Dillon 	 * and mode settings.  Also orphaned nodes created in
315*572ff6f6SMatthew Dillon 	 * adhoc mode may not have any rate set so this lookup
316*572ff6f6SMatthew Dillon 	 * can fail.  This is not fatal.
317*572ff6f6SMatthew Dillon 	 */
318*572ff6f6SMatthew Dillon 	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
319*572ff6f6SMatthew Dillon #undef RATE
320*572ff6f6SMatthew Dillon }
321*572ff6f6SMatthew Dillon 
322*572ff6f6SMatthew Dillon /*
323*572ff6f6SMatthew Dillon  * Examine and potentially adjust the transmit rate.
324*572ff6f6SMatthew Dillon  */
325*572ff6f6SMatthew Dillon static void
326*572ff6f6SMatthew Dillon ath_rate_ctl(void *arg, struct ieee80211_node *ni)
327*572ff6f6SMatthew Dillon {
328*572ff6f6SMatthew Dillon 	struct ath_softc *sc = arg;
329*572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
330*572ff6f6SMatthew Dillon 	struct ieee80211_rateset *rs = &ni->ni_rates;
331*572ff6f6SMatthew Dillon 	int dir = 0, nrate, enough;
332*572ff6f6SMatthew Dillon 
333*572ff6f6SMatthew Dillon 	/*
334*572ff6f6SMatthew Dillon 	 * Rate control
335*572ff6f6SMatthew Dillon 	 * XXX: very primitive version.
336*572ff6f6SMatthew Dillon 	 */
337*572ff6f6SMatthew Dillon 	enough = (on->on_tx_ok + on->on_tx_err >= 10);
338*572ff6f6SMatthew Dillon 
339*572ff6f6SMatthew Dillon 	/* no packet reached -> down */
340*572ff6f6SMatthew Dillon 	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
341*572ff6f6SMatthew Dillon 		dir = -1;
342*572ff6f6SMatthew Dillon 
343*572ff6f6SMatthew Dillon 	/* all packets needs retry in average -> down */
344*572ff6f6SMatthew Dillon 	if (enough && on->on_tx_ok < on->on_tx_retr)
345*572ff6f6SMatthew Dillon 		dir = -1;
346*572ff6f6SMatthew Dillon 
347*572ff6f6SMatthew Dillon 	/* no error and less than rate_raise% of packets need retry -> up */
348*572ff6f6SMatthew Dillon 	if (enough && on->on_tx_err == 0 &&
349*572ff6f6SMatthew Dillon 	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
350*572ff6f6SMatthew Dillon 		dir = 1;
351*572ff6f6SMatthew Dillon 
352*572ff6f6SMatthew Dillon 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
353*572ff6f6SMatthew Dillon 	    "ok %d err %d retr %d upper %d dir %d",
354*572ff6f6SMatthew Dillon 	    on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
355*572ff6f6SMatthew Dillon 
356*572ff6f6SMatthew Dillon 	nrate = on->on_rix;
357*572ff6f6SMatthew Dillon 	switch (dir) {
358*572ff6f6SMatthew Dillon 	case 0:
359*572ff6f6SMatthew Dillon 		if (enough && on->on_tx_upper > 0)
360*572ff6f6SMatthew Dillon 			on->on_tx_upper--;
361*572ff6f6SMatthew Dillon 		break;
362*572ff6f6SMatthew Dillon 	case -1:
363*572ff6f6SMatthew Dillon 		if (nrate > 0) {
364*572ff6f6SMatthew Dillon 			nrate--;
365*572ff6f6SMatthew Dillon 			sc->sc_stats.ast_rate_drop++;
366*572ff6f6SMatthew Dillon 		}
367*572ff6f6SMatthew Dillon 		on->on_tx_upper = 0;
368*572ff6f6SMatthew Dillon 		break;
369*572ff6f6SMatthew Dillon 	case 1:
370*572ff6f6SMatthew Dillon 		/* raise rate if we hit rate_raise_threshold */
371*572ff6f6SMatthew Dillon 		if (++on->on_tx_upper < ath_rate_raise_threshold)
372*572ff6f6SMatthew Dillon 			break;
373*572ff6f6SMatthew Dillon 		on->on_tx_upper = 0;
374*572ff6f6SMatthew Dillon 		if (nrate + 1 < rs->rs_nrates) {
375*572ff6f6SMatthew Dillon 			nrate++;
376*572ff6f6SMatthew Dillon 			sc->sc_stats.ast_rate_raise++;
377*572ff6f6SMatthew Dillon 		}
378*572ff6f6SMatthew Dillon 		break;
379*572ff6f6SMatthew Dillon 	}
380*572ff6f6SMatthew Dillon 
381*572ff6f6SMatthew Dillon 	if (nrate != on->on_rix) {
382*572ff6f6SMatthew Dillon 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
383*572ff6f6SMatthew Dillon 		    "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
384*572ff6f6SMatthew Dillon 		    ni->ni_txrate / 2,
385*572ff6f6SMatthew Dillon 		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
386*572ff6f6SMatthew Dillon 		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
387*572ff6f6SMatthew Dillon 		ath_rate_update(sc, ni, nrate);
388*572ff6f6SMatthew Dillon 	} else if (enough)
389*572ff6f6SMatthew Dillon 		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
390*572ff6f6SMatthew Dillon }
391*572ff6f6SMatthew Dillon 
392*572ff6f6SMatthew Dillon static void
393*572ff6f6SMatthew Dillon ath_rate_sysctlattach(struct ath_softc *sc)
394*572ff6f6SMatthew Dillon {
395*572ff6f6SMatthew Dillon 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
396*572ff6f6SMatthew Dillon 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
397*572ff6f6SMatthew Dillon 
398*572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
399*572ff6f6SMatthew Dillon 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
400*572ff6f6SMatthew Dillon 		"rate control: operation interval (ms)");
401*572ff6f6SMatthew Dillon 	/* XXX bounds check values */
402*572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
403*572ff6f6SMatthew Dillon 		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
404*572ff6f6SMatthew Dillon 		"rate control: retry threshold to credit rate raise (%%)");
405*572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
406*572ff6f6SMatthew Dillon 		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
407*572ff6f6SMatthew Dillon 		"rate control: # good periods before raising rate");
408*572ff6f6SMatthew Dillon }
409*572ff6f6SMatthew Dillon 
410*572ff6f6SMatthew Dillon static int
411*572ff6f6SMatthew Dillon ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
412*572ff6f6SMatthew Dillon     struct ath_rateioctl *re)
413*572ff6f6SMatthew Dillon {
414*572ff6f6SMatthew Dillon 
415*572ff6f6SMatthew Dillon 	return (EINVAL);
416*572ff6f6SMatthew Dillon }
417*572ff6f6SMatthew Dillon 
418*572ff6f6SMatthew Dillon struct ath_ratectrl *
419*572ff6f6SMatthew Dillon ath_rate_attach(struct ath_softc *sc)
420*572ff6f6SMatthew Dillon {
421*572ff6f6SMatthew Dillon 	struct onoe_softc *osc;
422*572ff6f6SMatthew Dillon 
423*572ff6f6SMatthew Dillon 	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
424*572ff6f6SMatthew Dillon 	if (osc == NULL)
425*572ff6f6SMatthew Dillon 		return NULL;
426*572ff6f6SMatthew Dillon 	osc->arc.arc_space = sizeof(struct onoe_node);
427*572ff6f6SMatthew Dillon 	ath_rate_sysctlattach(sc);
428*572ff6f6SMatthew Dillon 
429*572ff6f6SMatthew Dillon 	return &osc->arc;
430*572ff6f6SMatthew Dillon }
431*572ff6f6SMatthew Dillon 
432*572ff6f6SMatthew Dillon void
433*572ff6f6SMatthew Dillon ath_rate_detach(struct ath_ratectrl *arc)
434*572ff6f6SMatthew Dillon {
435*572ff6f6SMatthew Dillon 	struct onoe_softc *osc = (struct onoe_softc *) arc;
436*572ff6f6SMatthew Dillon 
437*572ff6f6SMatthew Dillon 	free(osc, M_DEVBUF);
438*572ff6f6SMatthew Dillon }
439