xref: /dragonfly/sys/dev/netif/ath/ath_rate/onoe/onoe.c (revision df052c2a)
1572ff6f6SMatthew Dillon /*-
2572ff6f6SMatthew Dillon  * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3572ff6f6SMatthew Dillon  * All rights reserved.
4572ff6f6SMatthew Dillon  *
5572ff6f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6572ff6f6SMatthew Dillon  * modification, are permitted provided that the following conditions
7572ff6f6SMatthew Dillon  * are met:
8572ff6f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
9572ff6f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer,
10572ff6f6SMatthew Dillon  *    without modification.
11572ff6f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12572ff6f6SMatthew Dillon  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13572ff6f6SMatthew Dillon  *    redistribution must be conditioned upon including a substantially
14572ff6f6SMatthew Dillon  *    similar Disclaimer requirement for further binary redistribution.
15572ff6f6SMatthew Dillon  *
16572ff6f6SMatthew Dillon  * NO WARRANTY
17572ff6f6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18572ff6f6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*df052c2aSSascha Wildner  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY
20572ff6f6SMatthew Dillon  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21572ff6f6SMatthew Dillon  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22572ff6f6SMatthew Dillon  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23572ff6f6SMatthew Dillon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24572ff6f6SMatthew Dillon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25572ff6f6SMatthew Dillon  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26572ff6f6SMatthew Dillon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27572ff6f6SMatthew Dillon  * THE POSSIBILITY OF SUCH DAMAGES.
28572ff6f6SMatthew Dillon  */
29572ff6f6SMatthew Dillon 
30572ff6f6SMatthew Dillon #include <sys/cdefs.h>
31848b370cSMatthew Dillon __FBSDID("$FreeBSD$");
32572ff6f6SMatthew Dillon 
33572ff6f6SMatthew Dillon /*
34572ff6f6SMatthew Dillon  * Atsushi Onoe's rate control algorithm.
35572ff6f6SMatthew Dillon  */
36572ff6f6SMatthew Dillon #include "opt_ath.h"
37572ff6f6SMatthew Dillon #include "opt_inet.h"
38572ff6f6SMatthew Dillon #include "opt_wlan.h"
39572ff6f6SMatthew Dillon 
40572ff6f6SMatthew Dillon #include <sys/param.h>
41572ff6f6SMatthew Dillon #include <sys/systm.h>
42572ff6f6SMatthew Dillon #include <sys/sysctl.h>
43572ff6f6SMatthew Dillon #include <sys/kernel.h>
44572ff6f6SMatthew Dillon #include <sys/lock.h>
45572ff6f6SMatthew Dillon #include <sys/errno.h>
46848b370cSMatthew Dillon 
47dc249793SMatthew Dillon #if defined(__DragonFly__)
48dc249793SMatthew Dillon /* empty */
49dc249793SMatthew Dillon #else
50b14ca477SMatthew Dillon #include <machine/bus.h>
51b14ca477SMatthew Dillon #include <machine/resource.h>
52dc249793SMatthew Dillon #endif
53572ff6f6SMatthew Dillon #include <sys/bus.h>
54848b370cSMatthew Dillon 
55572ff6f6SMatthew Dillon #include <sys/socket.h>
56572ff6f6SMatthew Dillon 
57572ff6f6SMatthew Dillon #include <net/if.h>
58572ff6f6SMatthew Dillon #include <net/if_media.h>
59572ff6f6SMatthew Dillon #include <net/if_arp.h>
60572ff6f6SMatthew Dillon #include <net/ethernet.h>		/* XXX for ether_sprintf */
61572ff6f6SMatthew Dillon 
62dc249793SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
63572ff6f6SMatthew Dillon 
64572ff6f6SMatthew Dillon #include <net/bpf.h>
65572ff6f6SMatthew Dillon 
66572ff6f6SMatthew Dillon #ifdef INET
67572ff6f6SMatthew Dillon #include <netinet/in.h>
68572ff6f6SMatthew Dillon #include <netinet/if_ether.h>
69572ff6f6SMatthew Dillon #endif
70572ff6f6SMatthew Dillon 
71dc249793SMatthew Dillon #include <dev/netif/ath/ath/if_athvar.h>
72dc249793SMatthew Dillon #include <dev/netif/ath/ath_rate/onoe/onoe.h>
73dc249793SMatthew Dillon #include <dev/netif/ath/ath_hal/ah_desc.h>
74572ff6f6SMatthew Dillon 
75572ff6f6SMatthew Dillon /*
76572ff6f6SMatthew Dillon  * Default parameters for the rate control algorithm.  These are
77572ff6f6SMatthew Dillon  * all tunable with sysctls.  The rate controller runs periodically
78572ff6f6SMatthew Dillon  * (each ath_rateinterval ms) analyzing transmit statistics for each
79572ff6f6SMatthew Dillon  * neighbor/station (when operating in station mode this is only the AP).
80572ff6f6SMatthew Dillon  * If transmits look to be working well over a sampling period then
81572ff6f6SMatthew Dillon  * it gives a "raise rate credit".  If transmits look to not be working
82572ff6f6SMatthew Dillon  * well than it deducts a credit.  If the credits cross a threshold then
83572ff6f6SMatthew Dillon  * the transmit rate is raised.  Various error conditions force the
84572ff6f6SMatthew Dillon  * the transmit rate to be dropped.
85572ff6f6SMatthew Dillon  *
86572ff6f6SMatthew Dillon  * The decision to issue/deduct a credit is based on the errors and
87572ff6f6SMatthew Dillon  * retries accumulated over the sampling period.  ath_rate_raise defines
88572ff6f6SMatthew Dillon  * the percent of retransmits for which a credit is issued/deducted.
89572ff6f6SMatthew Dillon  * ath_rate_raise_threshold defines the threshold on credits at which
90572ff6f6SMatthew Dillon  * the transmit rate is increased.
91572ff6f6SMatthew Dillon  *
92572ff6f6SMatthew Dillon  * XXX this algorithm is flawed.
93572ff6f6SMatthew Dillon  */
94572ff6f6SMatthew Dillon static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
95572ff6f6SMatthew Dillon static	int ath_rate_raise = 10;		/* add credit threshold */
96572ff6f6SMatthew Dillon static	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
97572ff6f6SMatthew Dillon 
98572ff6f6SMatthew Dillon static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
99572ff6f6SMatthew Dillon 			int rate);
100572ff6f6SMatthew Dillon static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
101572ff6f6SMatthew Dillon static void	ath_rate_ctl(void *, struct ieee80211_node *);
102572ff6f6SMatthew Dillon 
103572ff6f6SMatthew Dillon void
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)104572ff6f6SMatthew Dillon ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
105572ff6f6SMatthew Dillon {
106572ff6f6SMatthew Dillon 	/* NB: assumed to be zero'd by caller */
107572ff6f6SMatthew Dillon }
108572ff6f6SMatthew Dillon 
109572ff6f6SMatthew Dillon void
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)110572ff6f6SMatthew Dillon ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
111572ff6f6SMatthew Dillon {
112572ff6f6SMatthew Dillon }
113572ff6f6SMatthew Dillon 
114572ff6f6SMatthew Dillon void
ath_rate_findrate(struct ath_softc * sc,struct ath_node * an,int shortPreamble,size_t frameLen,u_int8_t * rix,int * try0,u_int8_t * txrate)115572ff6f6SMatthew Dillon ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
116572ff6f6SMatthew Dillon 	int shortPreamble, size_t frameLen,
117572ff6f6SMatthew Dillon 	u_int8_t *rix, int *try0, u_int8_t *txrate)
118572ff6f6SMatthew Dillon {
119572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
120572ff6f6SMatthew Dillon 
121572ff6f6SMatthew Dillon 	*rix = on->on_tx_rix0;
122572ff6f6SMatthew Dillon 	*try0 = on->on_tx_try0;
123572ff6f6SMatthew Dillon 	if (shortPreamble)
124572ff6f6SMatthew Dillon 		*txrate = on->on_tx_rate0sp;
125572ff6f6SMatthew Dillon 	else
126572ff6f6SMatthew Dillon 		*txrate = on->on_tx_rate0;
127572ff6f6SMatthew Dillon }
128572ff6f6SMatthew Dillon 
129572ff6f6SMatthew Dillon /*
130572ff6f6SMatthew Dillon  * Get the TX rates.
131572ff6f6SMatthew Dillon  *
132572ff6f6SMatthew Dillon  * The short preamble bits aren't set here; the caller should augment
133572ff6f6SMatthew Dillon  * the returned rate with the relevant preamble rate flag.
134572ff6f6SMatthew Dillon  */
135572ff6f6SMatthew Dillon void
ath_rate_getxtxrates(struct ath_softc * sc,struct ath_node * an,uint8_t rix0,struct ath_rc_series * rc)136572ff6f6SMatthew Dillon ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
137572ff6f6SMatthew Dillon     uint8_t rix0, struct ath_rc_series *rc)
138572ff6f6SMatthew Dillon {
139572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
140572ff6f6SMatthew Dillon 
141572ff6f6SMatthew Dillon 	rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
142572ff6f6SMatthew Dillon 
143572ff6f6SMatthew Dillon 	rc[0].rix = on->on_tx_rate0;
144572ff6f6SMatthew Dillon 	rc[1].rix = on->on_tx_rate1;
145572ff6f6SMatthew Dillon 	rc[2].rix = on->on_tx_rate2;
146572ff6f6SMatthew Dillon 	rc[3].rix = on->on_tx_rate3;
147572ff6f6SMatthew Dillon 
148572ff6f6SMatthew Dillon 	rc[0].tries = on->on_tx_try0;
149572ff6f6SMatthew Dillon 	rc[1].tries = 2;
150572ff6f6SMatthew Dillon 	rc[2].tries = 2;
151572ff6f6SMatthew Dillon 	rc[3].tries = 2;
152572ff6f6SMatthew Dillon }
153572ff6f6SMatthew Dillon 
154572ff6f6SMatthew Dillon void
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)155572ff6f6SMatthew Dillon ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
156572ff6f6SMatthew Dillon 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
157572ff6f6SMatthew Dillon {
158572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
159572ff6f6SMatthew Dillon 
160572ff6f6SMatthew Dillon 	ath_hal_setupxtxdesc(sc->sc_ah, ds
161572ff6f6SMatthew Dillon 		, on->on_tx_rate1sp, 2	/* series 1 */
162572ff6f6SMatthew Dillon 		, on->on_tx_rate2sp, 2	/* series 2 */
163572ff6f6SMatthew Dillon 		, on->on_tx_rate3sp, 2	/* series 3 */
164572ff6f6SMatthew Dillon 	);
165572ff6f6SMatthew Dillon }
166572ff6f6SMatthew Dillon 
167572ff6f6SMatthew Dillon void
ath_rate_tx_complete(struct ath_softc * sc,struct ath_node * an,const struct ath_rc_series * rc,const struct ath_tx_status * ts,int frame_size,int nframes,int nbad)168572ff6f6SMatthew Dillon ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
169572ff6f6SMatthew Dillon 	const struct ath_rc_series *rc, const struct ath_tx_status *ts,
170572ff6f6SMatthew Dillon 	int frame_size, int nframes, int nbad)
171572ff6f6SMatthew Dillon {
172572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
173572ff6f6SMatthew Dillon 
174572ff6f6SMatthew Dillon 	if (ts->ts_status == 0)
175572ff6f6SMatthew Dillon 		on->on_tx_ok++;
176572ff6f6SMatthew Dillon 	else
177572ff6f6SMatthew Dillon 		on->on_tx_err++;
178572ff6f6SMatthew Dillon 	on->on_tx_retr += ts->ts_shortretry
179572ff6f6SMatthew Dillon 			+ ts->ts_longretry;
180572ff6f6SMatthew Dillon 	if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
181572ff6f6SMatthew Dillon 		ath_rate_ctl(sc, &an->an_node);
182572ff6f6SMatthew Dillon 		on->on_ticks = ticks;
183572ff6f6SMatthew Dillon 	}
184572ff6f6SMatthew Dillon }
185572ff6f6SMatthew Dillon 
186572ff6f6SMatthew Dillon void
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)187572ff6f6SMatthew Dillon ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
188572ff6f6SMatthew Dillon {
189572ff6f6SMatthew Dillon 	if (isnew)
190572ff6f6SMatthew Dillon 		ath_rate_ctl_start(sc, &an->an_node);
191572ff6f6SMatthew Dillon }
192572ff6f6SMatthew Dillon 
193572ff6f6SMatthew Dillon static void
ath_rate_update(struct ath_softc * sc,struct ieee80211_node * ni,int rate)194572ff6f6SMatthew Dillon ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
195572ff6f6SMatthew Dillon {
196572ff6f6SMatthew Dillon 	struct ath_node *an = ATH_NODE(ni);
197572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(an);
198572ff6f6SMatthew Dillon 	struct ieee80211vap *vap = ni->ni_vap;
199572ff6f6SMatthew Dillon 	const HAL_RATE_TABLE *rt = sc->sc_currates;
200572ff6f6SMatthew Dillon 	u_int8_t rix;
201572ff6f6SMatthew Dillon 
202572ff6f6SMatthew Dillon 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
203572ff6f6SMatthew Dillon 
204572ff6f6SMatthew Dillon 	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
205572ff6f6SMatthew Dillon 	     "%s: set xmit rate to %dM", __func__,
206572ff6f6SMatthew Dillon 	     ni->ni_rates.rs_nrates > 0 ?
207572ff6f6SMatthew Dillon 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
208572ff6f6SMatthew Dillon 
209572ff6f6SMatthew Dillon 	/*
210572ff6f6SMatthew Dillon 	 * Before associating a node has no rate set setup
211572ff6f6SMatthew Dillon 	 * so we can't calculate any transmit codes to use.
212572ff6f6SMatthew Dillon 	 * This is ok since we should never be sending anything
213572ff6f6SMatthew Dillon 	 * but management frames and those always go at the
214572ff6f6SMatthew Dillon 	 * lowest hardware rate.
215572ff6f6SMatthew Dillon 	 */
216572ff6f6SMatthew Dillon 	if (ni->ni_rates.rs_nrates == 0)
217572ff6f6SMatthew Dillon 		goto done;
218572ff6f6SMatthew Dillon 	on->on_rix = rate;
219572ff6f6SMatthew Dillon 	ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
220572ff6f6SMatthew Dillon 	on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
221572ff6f6SMatthew Dillon 	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
222572ff6f6SMatthew Dillon 
223572ff6f6SMatthew Dillon 	on->on_tx_rate0sp = on->on_tx_rate0 |
224572ff6f6SMatthew Dillon 		rt->info[on->on_tx_rix0].shortPreamble;
225572ff6f6SMatthew Dillon 	if (sc->sc_mrretry) {
226572ff6f6SMatthew Dillon 		/*
227572ff6f6SMatthew Dillon 		 * Hardware supports multi-rate retry; setup two
228572ff6f6SMatthew Dillon 		 * step-down retry rates and make the lowest rate
229572ff6f6SMatthew Dillon 		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
230572ff6f6SMatthew Dillon 		 * respectively (4 is set here, the rest are fixed
231572ff6f6SMatthew Dillon 		 * in the xmit routine).
232572ff6f6SMatthew Dillon 		 */
233572ff6f6SMatthew Dillon 		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
234572ff6f6SMatthew Dillon 		if (--rate >= 0) {
235572ff6f6SMatthew Dillon 			rix = sc->sc_rixmap[
236572ff6f6SMatthew Dillon 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
237572ff6f6SMatthew Dillon 			on->on_tx_rate1 = rt->info[rix].rateCode;
238572ff6f6SMatthew Dillon 			on->on_tx_rate1sp = on->on_tx_rate1 |
239572ff6f6SMatthew Dillon 				rt->info[rix].shortPreamble;
240572ff6f6SMatthew Dillon 		} else {
241572ff6f6SMatthew Dillon 			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
242572ff6f6SMatthew Dillon 		}
243572ff6f6SMatthew Dillon 		if (--rate >= 0) {
244572ff6f6SMatthew Dillon 			rix = sc->sc_rixmap[
245572ff6f6SMatthew Dillon 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
246572ff6f6SMatthew Dillon 			on->on_tx_rate2 = rt->info[rix].rateCode;
247572ff6f6SMatthew Dillon 			on->on_tx_rate2sp = on->on_tx_rate2 |
248572ff6f6SMatthew Dillon 				rt->info[rix].shortPreamble;
249572ff6f6SMatthew Dillon 		} else {
250572ff6f6SMatthew Dillon 			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
251572ff6f6SMatthew Dillon 		}
252572ff6f6SMatthew Dillon 		if (rate > 0) {
253572ff6f6SMatthew Dillon 			/* NB: only do this if we didn't already do it above */
254572ff6f6SMatthew Dillon 			on->on_tx_rate3 = rt->info[0].rateCode;
255572ff6f6SMatthew Dillon 			on->on_tx_rate3sp =
256572ff6f6SMatthew Dillon 				on->on_tx_rate3 | rt->info[0].shortPreamble;
257572ff6f6SMatthew Dillon 		} else {
258572ff6f6SMatthew Dillon 			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
259572ff6f6SMatthew Dillon 		}
260572ff6f6SMatthew Dillon 	} else {
261572ff6f6SMatthew Dillon 		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
262572ff6f6SMatthew Dillon 		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
263572ff6f6SMatthew Dillon 		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
264572ff6f6SMatthew Dillon 		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
265572ff6f6SMatthew Dillon 	}
266572ff6f6SMatthew Dillon done:
267572ff6f6SMatthew Dillon 	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
268572ff6f6SMatthew Dillon 
269572ff6f6SMatthew Dillon 	on->on_interval = ath_rateinterval;
270572ff6f6SMatthew Dillon 	if (vap->iv_opmode == IEEE80211_M_STA)
271572ff6f6SMatthew Dillon 		on->on_interval /= 2;
272572ff6f6SMatthew Dillon 	on->on_interval = (on->on_interval * hz) / 1000;
273572ff6f6SMatthew Dillon }
274572ff6f6SMatthew Dillon 
275572ff6f6SMatthew Dillon /*
276572ff6f6SMatthew Dillon  * Set the starting transmit rate for a node.
277572ff6f6SMatthew Dillon  */
278572ff6f6SMatthew Dillon static void
ath_rate_ctl_start(struct ath_softc * sc,struct ieee80211_node * ni)279572ff6f6SMatthew Dillon ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
280572ff6f6SMatthew Dillon {
281572ff6f6SMatthew Dillon #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
282572ff6f6SMatthew Dillon 	const struct ieee80211_txparam *tp = ni->ni_txparms;
283572ff6f6SMatthew Dillon 	int srate;
284572ff6f6SMatthew Dillon 
285572ff6f6SMatthew Dillon 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
286572ff6f6SMatthew Dillon 	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
287572ff6f6SMatthew Dillon 		/*
288572ff6f6SMatthew Dillon 		 * No fixed rate is requested. For 11b start with
289572ff6f6SMatthew Dillon 		 * the highest negotiated rate; otherwise, for 11g
290572ff6f6SMatthew Dillon 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
291572ff6f6SMatthew Dillon 		 */
292572ff6f6SMatthew Dillon 		srate = ni->ni_rates.rs_nrates - 1;
293572ff6f6SMatthew Dillon 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
294572ff6f6SMatthew Dillon 			/*
295572ff6f6SMatthew Dillon 			 * Scan the negotiated rate set to find the
296572ff6f6SMatthew Dillon 			 * closest rate.
297572ff6f6SMatthew Dillon 			 */
298572ff6f6SMatthew Dillon 			/* NB: the rate set is assumed sorted */
299572ff6f6SMatthew Dillon 			for (; srate >= 0 && RATE(srate) > 72; srate--)
300572ff6f6SMatthew Dillon 				;
301572ff6f6SMatthew Dillon 		}
302572ff6f6SMatthew Dillon 	} else {
303572ff6f6SMatthew Dillon 		/*
304572ff6f6SMatthew Dillon 		 * A fixed rate is to be used; ic_fixed_rate is the
305572ff6f6SMatthew Dillon 		 * IEEE code for this rate (sans basic bit).  Convert this
306572ff6f6SMatthew Dillon 		 * to the index into the negotiated rate set for
307572ff6f6SMatthew Dillon 		 * the node.  We know the rate is there because the
308572ff6f6SMatthew Dillon 		 * rate set is checked when the station associates.
309572ff6f6SMatthew Dillon 		 */
310572ff6f6SMatthew Dillon 		/* NB: the rate set is assumed sorted */
311572ff6f6SMatthew Dillon 		srate = ni->ni_rates.rs_nrates - 1;
312572ff6f6SMatthew Dillon 		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
313572ff6f6SMatthew Dillon 			;
314572ff6f6SMatthew Dillon 	}
315572ff6f6SMatthew Dillon 	/*
316572ff6f6SMatthew Dillon 	 * The selected rate may not be available due to races
317572ff6f6SMatthew Dillon 	 * and mode settings.  Also orphaned nodes created in
318572ff6f6SMatthew Dillon 	 * adhoc mode may not have any rate set so this lookup
319572ff6f6SMatthew Dillon 	 * can fail.  This is not fatal.
320572ff6f6SMatthew Dillon 	 */
321572ff6f6SMatthew Dillon 	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
322572ff6f6SMatthew Dillon #undef RATE
323572ff6f6SMatthew Dillon }
324572ff6f6SMatthew Dillon 
325572ff6f6SMatthew Dillon /*
326572ff6f6SMatthew Dillon  * Examine and potentially adjust the transmit rate.
327572ff6f6SMatthew Dillon  */
328572ff6f6SMatthew Dillon static void
ath_rate_ctl(void * arg,struct ieee80211_node * ni)329572ff6f6SMatthew Dillon ath_rate_ctl(void *arg, struct ieee80211_node *ni)
330572ff6f6SMatthew Dillon {
331572ff6f6SMatthew Dillon 	struct ath_softc *sc = arg;
332572ff6f6SMatthew Dillon 	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
333572ff6f6SMatthew Dillon 	struct ieee80211_rateset *rs = &ni->ni_rates;
334572ff6f6SMatthew Dillon 	int dir = 0, nrate, enough;
335572ff6f6SMatthew Dillon 
336572ff6f6SMatthew Dillon 	/*
337572ff6f6SMatthew Dillon 	 * Rate control
338572ff6f6SMatthew Dillon 	 * XXX: very primitive version.
339572ff6f6SMatthew Dillon 	 */
340572ff6f6SMatthew Dillon 	enough = (on->on_tx_ok + on->on_tx_err >= 10);
341572ff6f6SMatthew Dillon 
342572ff6f6SMatthew Dillon 	/* no packet reached -> down */
343572ff6f6SMatthew Dillon 	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
344572ff6f6SMatthew Dillon 		dir = -1;
345572ff6f6SMatthew Dillon 
346572ff6f6SMatthew Dillon 	/* all packets needs retry in average -> down */
347572ff6f6SMatthew Dillon 	if (enough && on->on_tx_ok < on->on_tx_retr)
348572ff6f6SMatthew Dillon 		dir = -1;
349572ff6f6SMatthew Dillon 
350572ff6f6SMatthew Dillon 	/* no error and less than rate_raise% of packets need retry -> up */
351572ff6f6SMatthew Dillon 	if (enough && on->on_tx_err == 0 &&
352572ff6f6SMatthew Dillon 	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
353572ff6f6SMatthew Dillon 		dir = 1;
354572ff6f6SMatthew Dillon 
355572ff6f6SMatthew Dillon 	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
356572ff6f6SMatthew Dillon 	    "ok %d err %d retr %d upper %d dir %d",
357572ff6f6SMatthew Dillon 	    on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
358572ff6f6SMatthew Dillon 
359572ff6f6SMatthew Dillon 	nrate = on->on_rix;
360572ff6f6SMatthew Dillon 	switch (dir) {
361572ff6f6SMatthew Dillon 	case 0:
362572ff6f6SMatthew Dillon 		if (enough && on->on_tx_upper > 0)
363572ff6f6SMatthew Dillon 			on->on_tx_upper--;
364572ff6f6SMatthew Dillon 		break;
365572ff6f6SMatthew Dillon 	case -1:
366572ff6f6SMatthew Dillon 		if (nrate > 0) {
367572ff6f6SMatthew Dillon 			nrate--;
368572ff6f6SMatthew Dillon 			sc->sc_stats.ast_rate_drop++;
369572ff6f6SMatthew Dillon 		}
370572ff6f6SMatthew Dillon 		on->on_tx_upper = 0;
371572ff6f6SMatthew Dillon 		break;
372572ff6f6SMatthew Dillon 	case 1:
373572ff6f6SMatthew Dillon 		/* raise rate if we hit rate_raise_threshold */
374572ff6f6SMatthew Dillon 		if (++on->on_tx_upper < ath_rate_raise_threshold)
375572ff6f6SMatthew Dillon 			break;
376572ff6f6SMatthew Dillon 		on->on_tx_upper = 0;
377572ff6f6SMatthew Dillon 		if (nrate + 1 < rs->rs_nrates) {
378572ff6f6SMatthew Dillon 			nrate++;
379572ff6f6SMatthew Dillon 			sc->sc_stats.ast_rate_raise++;
380572ff6f6SMatthew Dillon 		}
381572ff6f6SMatthew Dillon 		break;
382572ff6f6SMatthew Dillon 	}
383572ff6f6SMatthew Dillon 
384572ff6f6SMatthew Dillon 	if (nrate != on->on_rix) {
385572ff6f6SMatthew Dillon 		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
386572ff6f6SMatthew Dillon 		    "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
387572ff6f6SMatthew Dillon 		    ni->ni_txrate / 2,
388572ff6f6SMatthew Dillon 		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
389572ff6f6SMatthew Dillon 		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
390572ff6f6SMatthew Dillon 		ath_rate_update(sc, ni, nrate);
391572ff6f6SMatthew Dillon 	} else if (enough)
392572ff6f6SMatthew Dillon 		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
393572ff6f6SMatthew Dillon }
394572ff6f6SMatthew Dillon 
395572ff6f6SMatthew Dillon static void
ath_rate_sysctlattach(struct ath_softc * sc)396572ff6f6SMatthew Dillon ath_rate_sysctlattach(struct ath_softc *sc)
397572ff6f6SMatthew Dillon {
39826595b18SSascha Wildner 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
39926595b18SSascha Wildner 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
400572ff6f6SMatthew Dillon 
401572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
402572ff6f6SMatthew Dillon 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
403572ff6f6SMatthew Dillon 		"rate control: operation interval (ms)");
404572ff6f6SMatthew Dillon 	/* XXX bounds check values */
405572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
406572ff6f6SMatthew Dillon 		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
407572ff6f6SMatthew Dillon 		"rate control: retry threshold to credit rate raise (%%)");
408572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
409572ff6f6SMatthew Dillon 		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
410572ff6f6SMatthew Dillon 		"rate control: # good periods before raising rate");
411572ff6f6SMatthew Dillon }
412572ff6f6SMatthew Dillon 
413572ff6f6SMatthew Dillon static int
ath_rate_fetch_node_stats(struct ath_softc * sc,struct ath_node * an,struct ath_rateioctl * re)414572ff6f6SMatthew Dillon ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
415572ff6f6SMatthew Dillon     struct ath_rateioctl *re)
416572ff6f6SMatthew Dillon {
417572ff6f6SMatthew Dillon 
418572ff6f6SMatthew Dillon 	return (EINVAL);
419572ff6f6SMatthew Dillon }
420572ff6f6SMatthew Dillon 
421572ff6f6SMatthew Dillon struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)422572ff6f6SMatthew Dillon ath_rate_attach(struct ath_softc *sc)
423572ff6f6SMatthew Dillon {
424572ff6f6SMatthew Dillon 	struct onoe_softc *osc;
425572ff6f6SMatthew Dillon 
426dc249793SMatthew Dillon 	osc = kmalloc(sizeof(struct onoe_softc), M_DEVBUF, M_INTWAIT|M_ZERO);
427572ff6f6SMatthew Dillon 	if (osc == NULL)
428572ff6f6SMatthew Dillon 		return NULL;
429572ff6f6SMatthew Dillon 	osc->arc.arc_space = sizeof(struct onoe_node);
430572ff6f6SMatthew Dillon 	ath_rate_sysctlattach(sc);
431572ff6f6SMatthew Dillon 
432572ff6f6SMatthew Dillon 	return &osc->arc;
433572ff6f6SMatthew Dillon }
434572ff6f6SMatthew Dillon 
435572ff6f6SMatthew Dillon void
ath_rate_detach(struct ath_ratectrl * arc)436572ff6f6SMatthew Dillon ath_rate_detach(struct ath_ratectrl *arc)
437572ff6f6SMatthew Dillon {
438572ff6f6SMatthew Dillon 	struct onoe_softc *osc = (struct onoe_softc *) arc;
439572ff6f6SMatthew Dillon 
440dc249793SMatthew Dillon 	kfree(osc, M_DEVBUF);
441572ff6f6SMatthew Dillon }
442dc249793SMatthew Dillon 
443dc249793SMatthew Dillon #if defined(__DragonFly__)
444dc249793SMatthew Dillon 
445dc249793SMatthew Dillon /*
446dc249793SMatthew Dillon  * Module glue.
447dc249793SMatthew Dillon  */
448dc249793SMatthew Dillon static int
onoe_modevent(module_t mod,int type,void * unused)449dc249793SMatthew Dillon onoe_modevent(module_t mod, int type, void *unused)
450dc249793SMatthew Dillon {
451dc249793SMatthew Dillon        int error;
452dc249793SMatthew Dillon 
453dc249793SMatthew Dillon        wlan_serialize_enter();
454dc249793SMatthew Dillon 
455dc249793SMatthew Dillon        switch (type) {
456dc249793SMatthew Dillon        case MOD_LOAD:
457dc249793SMatthew Dillon 	       if (bootverbose) {
458dc249793SMatthew Dillon 		       kprintf("ath_rate: <Atsushi Onoe's rate "
459dc249793SMatthew Dillon 			       "control algorithm>\n");
460dc249793SMatthew Dillon 	       }
461dc249793SMatthew Dillon 	       error = 0;
462dc249793SMatthew Dillon 	       break;
463dc249793SMatthew Dillon        case MOD_UNLOAD:
464dc249793SMatthew Dillon 	       error = 0;
465dc249793SMatthew Dillon 	       break;
466dc249793SMatthew Dillon        default:
467dc249793SMatthew Dillon 	       error = EINVAL;
468dc249793SMatthew Dillon 	       break;
469dc249793SMatthew Dillon        }
470dc249793SMatthew Dillon        wlan_serialize_exit();
471dc249793SMatthew Dillon 
472dc249793SMatthew Dillon        return error;
473dc249793SMatthew Dillon }
474dc249793SMatthew Dillon 
475dc249793SMatthew Dillon static moduledata_t onoe_mod = {
476dc249793SMatthew Dillon        "ath_rate",
477dc249793SMatthew Dillon        onoe_modevent,
478dc249793SMatthew Dillon        0
479dc249793SMatthew Dillon };
480dc249793SMatthew Dillon 
481dc249793SMatthew Dillon DECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
482dc249793SMatthew Dillon MODULE_VERSION(ath_rate, 1);
483dc249793SMatthew Dillon MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);
484dc249793SMatthew Dillon MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
485dc249793SMatthew Dillon 
486dc249793SMatthew Dillon #endif
487