xref: /dflybsd-src/sys/dev/netif/ath/ath_rate/amrr/amrr.c (revision df052c2a9588fe12c7a2df4e61e2bfa3f3e16ce0)
1572ff6f6SMatthew Dillon /*-
2572ff6f6SMatthew Dillon  * Copyright (c) 2004 INRIA
3572ff6f6SMatthew Dillon  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4572ff6f6SMatthew Dillon  * All rights reserved.
5572ff6f6SMatthew Dillon  *
6572ff6f6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
7572ff6f6SMatthew Dillon  * modification, are permitted provided that the following conditions
8572ff6f6SMatthew Dillon  * are met:
9572ff6f6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
10572ff6f6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer,
11572ff6f6SMatthew Dillon  *    without modification.
12572ff6f6SMatthew Dillon  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13572ff6f6SMatthew Dillon  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14572ff6f6SMatthew Dillon  *    redistribution must be conditioned upon including a substantially
15572ff6f6SMatthew Dillon  *    similar Disclaimer requirement for further binary redistribution.
16572ff6f6SMatthew Dillon  * 3. Neither the names of the above-listed copyright holders nor the names
17572ff6f6SMatthew Dillon  *    of any contributors may be used to endorse or promote products derived
18572ff6f6SMatthew Dillon  *    from this software without specific prior written permission.
19572ff6f6SMatthew Dillon  *
20572ff6f6SMatthew Dillon  * Alternatively, this software may be distributed under the terms of the
21572ff6f6SMatthew Dillon  * GNU General Public License ("GPL") version 2 as published by the Free
22572ff6f6SMatthew Dillon  * Software Foundation.
23572ff6f6SMatthew Dillon  *
24572ff6f6SMatthew Dillon  * NO WARRANTY
25572ff6f6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26572ff6f6SMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27*df052c2aSSascha Wildner  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTABILITY
28572ff6f6SMatthew Dillon  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29572ff6f6SMatthew Dillon  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
30572ff6f6SMatthew Dillon  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31572ff6f6SMatthew Dillon  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32572ff6f6SMatthew Dillon  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33572ff6f6SMatthew Dillon  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34572ff6f6SMatthew Dillon  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35572ff6f6SMatthew Dillon  * THE POSSIBILITY OF SUCH DAMAGES.
36572ff6f6SMatthew Dillon  *
37572ff6f6SMatthew Dillon  */
38572ff6f6SMatthew Dillon 
39572ff6f6SMatthew Dillon #include <sys/cdefs.h>
40848b370cSMatthew Dillon __FBSDID("$FreeBSD$");
41572ff6f6SMatthew Dillon 
42572ff6f6SMatthew Dillon /*
43572ff6f6SMatthew Dillon  * AMRR rate control. See:
44572ff6f6SMatthew Dillon  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
45572ff6f6SMatthew Dillon  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
46572ff6f6SMatthew Dillon  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
47572ff6f6SMatthew Dillon  */
48572ff6f6SMatthew Dillon #include "opt_ath.h"
49572ff6f6SMatthew Dillon #include "opt_inet.h"
50572ff6f6SMatthew Dillon #include "opt_wlan.h"
51572ff6f6SMatthew Dillon 
52572ff6f6SMatthew Dillon #include <sys/param.h>
53572ff6f6SMatthew Dillon #include <sys/systm.h>
54572ff6f6SMatthew Dillon #include <sys/sysctl.h>
55572ff6f6SMatthew Dillon #include <sys/kernel.h>
56572ff6f6SMatthew Dillon #include <sys/lock.h>
57572ff6f6SMatthew Dillon #include <sys/errno.h>
58848b370cSMatthew Dillon 
59dc249793SMatthew Dillon #if defined(__DragonFly__)
60dc249793SMatthew Dillon /* empty */
61dc249793SMatthew Dillon #else
62b14ca477SMatthew Dillon #include <machine/bus.h>
63b14ca477SMatthew Dillon #include <machine/resource.h>
64dc249793SMatthew Dillon #endif
65572ff6f6SMatthew Dillon #include <sys/bus.h>
66848b370cSMatthew Dillon 
67572ff6f6SMatthew Dillon #include <sys/socket.h>
68572ff6f6SMatthew Dillon 
69572ff6f6SMatthew Dillon #include <net/if.h>
70572ff6f6SMatthew Dillon #include <net/if_media.h>
71572ff6f6SMatthew Dillon #include <net/if_arp.h>
72572ff6f6SMatthew Dillon 
73dc249793SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
74572ff6f6SMatthew Dillon 
75572ff6f6SMatthew Dillon #include <net/bpf.h>
76572ff6f6SMatthew Dillon 
77572ff6f6SMatthew Dillon #ifdef INET
78572ff6f6SMatthew Dillon #include <netinet/in.h>
79572ff6f6SMatthew Dillon #include <netinet/if_ether.h>
80572ff6f6SMatthew Dillon #endif
81572ff6f6SMatthew Dillon 
82dc249793SMatthew Dillon #include <dev/netif/ath/ath/if_athvar.h>
83dc249793SMatthew Dillon #include <dev/netif/ath/ath_rate/amrr/amrr.h>
84dc249793SMatthew Dillon #include <dev/netif/ath/ath_hal/ah_desc.h>
85572ff6f6SMatthew Dillon 
86572ff6f6SMatthew Dillon static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
87572ff6f6SMatthew Dillon static	int ath_rate_max_success_threshold = 10;
88572ff6f6SMatthew Dillon static	int ath_rate_min_success_threshold = 1;
89572ff6f6SMatthew Dillon 
90572ff6f6SMatthew Dillon static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
91572ff6f6SMatthew Dillon 			int rate);
92572ff6f6SMatthew Dillon static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
93572ff6f6SMatthew Dillon static void	ath_rate_ctl(void *, struct ieee80211_node *);
94572ff6f6SMatthew Dillon 
95572ff6f6SMatthew Dillon void
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)96572ff6f6SMatthew Dillon ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
97572ff6f6SMatthew Dillon {
98572ff6f6SMatthew Dillon 	/* NB: assumed to be zero'd by caller */
99572ff6f6SMatthew Dillon }
100572ff6f6SMatthew Dillon 
101572ff6f6SMatthew Dillon void
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)102572ff6f6SMatthew Dillon ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
103572ff6f6SMatthew Dillon {
104572ff6f6SMatthew Dillon }
105572ff6f6SMatthew Dillon 
106572ff6f6SMatthew 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)107572ff6f6SMatthew Dillon ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
108572ff6f6SMatthew Dillon 	int shortPreamble, size_t frameLen,
109572ff6f6SMatthew Dillon 	u_int8_t *rix, int *try0, u_int8_t *txrate)
110572ff6f6SMatthew Dillon {
111572ff6f6SMatthew Dillon 	struct amrr_node *amn = ATH_NODE_AMRR(an);
112572ff6f6SMatthew Dillon 
113572ff6f6SMatthew Dillon 	*rix = amn->amn_tx_rix0;
114572ff6f6SMatthew Dillon 	*try0 = amn->amn_tx_try0;
115572ff6f6SMatthew Dillon 	if (shortPreamble)
116572ff6f6SMatthew Dillon 		*txrate = amn->amn_tx_rate0sp;
117572ff6f6SMatthew Dillon 	else
118572ff6f6SMatthew Dillon 		*txrate = amn->amn_tx_rate0;
119572ff6f6SMatthew Dillon }
120572ff6f6SMatthew Dillon 
121572ff6f6SMatthew Dillon /*
122572ff6f6SMatthew Dillon  * Get the TX rates.
123572ff6f6SMatthew Dillon  *
124572ff6f6SMatthew Dillon  * The short preamble bits aren't set here; the caller should augment
125572ff6f6SMatthew Dillon  * the returned rate with the relevant preamble rate flag.
126572ff6f6SMatthew Dillon  */
127572ff6f6SMatthew Dillon void
ath_rate_getxtxrates(struct ath_softc * sc,struct ath_node * an,uint8_t rix0,struct ath_rc_series * rc)128572ff6f6SMatthew Dillon ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
129572ff6f6SMatthew Dillon     uint8_t rix0, struct ath_rc_series *rc)
130572ff6f6SMatthew Dillon {
131572ff6f6SMatthew Dillon 	struct amrr_node *amn = ATH_NODE_AMRR(an);
132572ff6f6SMatthew Dillon 
133572ff6f6SMatthew Dillon 	rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
134572ff6f6SMatthew Dillon 
135572ff6f6SMatthew Dillon 	rc[0].rix = amn->amn_tx_rate0;
136572ff6f6SMatthew Dillon 	rc[1].rix = amn->amn_tx_rate1;
137572ff6f6SMatthew Dillon 	rc[2].rix = amn->amn_tx_rate2;
138572ff6f6SMatthew Dillon 	rc[3].rix = amn->amn_tx_rate3;
139572ff6f6SMatthew Dillon 
140572ff6f6SMatthew Dillon 	rc[0].tries = amn->amn_tx_try0;
141572ff6f6SMatthew Dillon 	rc[1].tries = amn->amn_tx_try1;
142572ff6f6SMatthew Dillon 	rc[2].tries = amn->amn_tx_try2;
143572ff6f6SMatthew Dillon 	rc[3].tries = amn->amn_tx_try3;
144572ff6f6SMatthew Dillon }
145572ff6f6SMatthew Dillon 
146572ff6f6SMatthew Dillon 
147572ff6f6SMatthew Dillon void
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)148572ff6f6SMatthew Dillon ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
149572ff6f6SMatthew Dillon 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
150572ff6f6SMatthew Dillon {
151572ff6f6SMatthew Dillon 	struct amrr_node *amn = ATH_NODE_AMRR(an);
152572ff6f6SMatthew Dillon 
153572ff6f6SMatthew Dillon 	ath_hal_setupxtxdesc(sc->sc_ah, ds
154572ff6f6SMatthew Dillon 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
155572ff6f6SMatthew Dillon 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
156572ff6f6SMatthew Dillon 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
157572ff6f6SMatthew Dillon 	);
158572ff6f6SMatthew Dillon }
159572ff6f6SMatthew Dillon 
160572ff6f6SMatthew 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)161572ff6f6SMatthew Dillon ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
162572ff6f6SMatthew Dillon 	const struct ath_rc_series *rc, const struct ath_tx_status *ts,
163572ff6f6SMatthew Dillon 	int frame_size, int nframes, int nbad)
164572ff6f6SMatthew Dillon {
165572ff6f6SMatthew Dillon 	struct amrr_node *amn = ATH_NODE_AMRR(an);
166572ff6f6SMatthew Dillon 	int sr = ts->ts_shortretry;
167572ff6f6SMatthew Dillon 	int lr = ts->ts_longretry;
168572ff6f6SMatthew Dillon 	int retry_count = sr + lr;
169572ff6f6SMatthew Dillon 
170572ff6f6SMatthew Dillon 	amn->amn_tx_try0_cnt++;
171572ff6f6SMatthew Dillon 	if (retry_count == 1) {
172572ff6f6SMatthew Dillon 		amn->amn_tx_try1_cnt++;
173572ff6f6SMatthew Dillon 	} else if (retry_count == 2) {
174572ff6f6SMatthew Dillon 		amn->amn_tx_try1_cnt++;
175572ff6f6SMatthew Dillon 		amn->amn_tx_try2_cnt++;
176572ff6f6SMatthew Dillon 	} else if (retry_count == 3) {
177572ff6f6SMatthew Dillon 		amn->amn_tx_try1_cnt++;
178572ff6f6SMatthew Dillon 		amn->amn_tx_try2_cnt++;
179572ff6f6SMatthew Dillon 		amn->amn_tx_try3_cnt++;
180572ff6f6SMatthew Dillon 	} else if (retry_count > 3) {
181572ff6f6SMatthew Dillon 		amn->amn_tx_try1_cnt++;
182572ff6f6SMatthew Dillon 		amn->amn_tx_try2_cnt++;
183572ff6f6SMatthew Dillon 		amn->amn_tx_try3_cnt++;
184572ff6f6SMatthew Dillon 		amn->amn_tx_failure_cnt++;
185572ff6f6SMatthew Dillon 	}
186572ff6f6SMatthew Dillon 	if (amn->amn_interval != 0 &&
187572ff6f6SMatthew Dillon 	    ticks - amn->amn_ticks > amn->amn_interval) {
188572ff6f6SMatthew Dillon 		ath_rate_ctl(sc, &an->an_node);
189572ff6f6SMatthew Dillon 		amn->amn_ticks = ticks;
190572ff6f6SMatthew Dillon 	}
191572ff6f6SMatthew Dillon }
192572ff6f6SMatthew Dillon 
193572ff6f6SMatthew Dillon void
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)194572ff6f6SMatthew Dillon ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
195572ff6f6SMatthew Dillon {
196572ff6f6SMatthew Dillon 	if (isnew)
197572ff6f6SMatthew Dillon 		ath_rate_ctl_start(sc, &an->an_node);
198572ff6f6SMatthew Dillon }
199572ff6f6SMatthew Dillon 
200572ff6f6SMatthew Dillon static void
node_reset(struct amrr_node * amn)201572ff6f6SMatthew Dillon node_reset(struct amrr_node *amn)
202572ff6f6SMatthew Dillon {
203572ff6f6SMatthew Dillon 	amn->amn_tx_try0_cnt = 0;
204572ff6f6SMatthew Dillon 	amn->amn_tx_try1_cnt = 0;
205572ff6f6SMatthew Dillon 	amn->amn_tx_try2_cnt = 0;
206572ff6f6SMatthew Dillon 	amn->amn_tx_try3_cnt = 0;
207572ff6f6SMatthew Dillon 	amn->amn_tx_failure_cnt = 0;
208572ff6f6SMatthew Dillon   	amn->amn_success = 0;
209572ff6f6SMatthew Dillon   	amn->amn_recovery = 0;
210572ff6f6SMatthew Dillon   	amn->amn_success_threshold = ath_rate_min_success_threshold;
211572ff6f6SMatthew Dillon }
212572ff6f6SMatthew Dillon 
213572ff6f6SMatthew Dillon 
214572ff6f6SMatthew Dillon /**
215572ff6f6SMatthew Dillon  * The code below assumes that we are dealing with hardware multi rate retry
216572ff6f6SMatthew Dillon  * I have no idea what will happen if you try to use this module with another
217572ff6f6SMatthew Dillon  * type of hardware. Your machine might catch fire or it might work with
218572ff6f6SMatthew Dillon  * horrible performance...
219572ff6f6SMatthew Dillon  */
220572ff6f6SMatthew Dillon static void
ath_rate_update(struct ath_softc * sc,struct ieee80211_node * ni,int rate)221572ff6f6SMatthew Dillon ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
222572ff6f6SMatthew Dillon {
223572ff6f6SMatthew Dillon 	struct ath_node *an = ATH_NODE(ni);
224572ff6f6SMatthew Dillon 	struct amrr_node *amn = ATH_NODE_AMRR(an);
225572ff6f6SMatthew Dillon 	struct ieee80211vap *vap = ni->ni_vap;
226572ff6f6SMatthew Dillon 	const HAL_RATE_TABLE *rt = sc->sc_currates;
227572ff6f6SMatthew Dillon 	u_int8_t rix;
228572ff6f6SMatthew Dillon 
229572ff6f6SMatthew Dillon 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
230572ff6f6SMatthew Dillon 
231572ff6f6SMatthew Dillon 	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
232572ff6f6SMatthew Dillon 	    "%s: set xmit rate to %dM", __func__,
233572ff6f6SMatthew Dillon 	    ni->ni_rates.rs_nrates > 0 ?
234572ff6f6SMatthew Dillon 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
235572ff6f6SMatthew Dillon 
236572ff6f6SMatthew Dillon 	amn->amn_rix = rate;
237572ff6f6SMatthew Dillon 	/*
238572ff6f6SMatthew Dillon 	 * Before associating a node has no rate set setup
239572ff6f6SMatthew Dillon 	 * so we can't calculate any transmit codes to use.
240572ff6f6SMatthew Dillon 	 * This is ok since we should never be sending anything
241572ff6f6SMatthew Dillon 	 * but management frames and those always go at the
242572ff6f6SMatthew Dillon 	 * lowest hardware rate.
243572ff6f6SMatthew Dillon 	 */
244572ff6f6SMatthew Dillon 	if (ni->ni_rates.rs_nrates > 0) {
245572ff6f6SMatthew Dillon 		ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
246572ff6f6SMatthew Dillon 		amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
247572ff6f6SMatthew Dillon 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
248572ff6f6SMatthew Dillon 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
249572ff6f6SMatthew Dillon 			rt->info[amn->amn_tx_rix0].shortPreamble;
250572ff6f6SMatthew Dillon 		if (sc->sc_mrretry) {
251572ff6f6SMatthew Dillon 			amn->amn_tx_try0 = 1;
252572ff6f6SMatthew Dillon 			amn->amn_tx_try1 = 1;
253572ff6f6SMatthew Dillon 			amn->amn_tx_try2 = 1;
254572ff6f6SMatthew Dillon 			amn->amn_tx_try3 = 1;
255572ff6f6SMatthew Dillon 			if (--rate >= 0) {
256572ff6f6SMatthew Dillon 				rix = sc->sc_rixmap[
257572ff6f6SMatthew Dillon 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
258572ff6f6SMatthew Dillon 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
259572ff6f6SMatthew Dillon 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
260572ff6f6SMatthew Dillon 					rt->info[rix].shortPreamble;
261572ff6f6SMatthew Dillon 			} else {
262572ff6f6SMatthew Dillon 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
263572ff6f6SMatthew Dillon 			}
264572ff6f6SMatthew Dillon 			if (--rate >= 0) {
265572ff6f6SMatthew Dillon 				rix = sc->sc_rixmap[
266572ff6f6SMatthew Dillon 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
267572ff6f6SMatthew Dillon 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
268572ff6f6SMatthew Dillon 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
269572ff6f6SMatthew Dillon 					rt->info[rix].shortPreamble;
270572ff6f6SMatthew Dillon 			} else {
271572ff6f6SMatthew Dillon 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
272572ff6f6SMatthew Dillon 			}
273572ff6f6SMatthew Dillon 			if (rate > 0) {
274572ff6f6SMatthew Dillon 				/* NB: only do this if we didn't already do it above */
275572ff6f6SMatthew Dillon 				amn->amn_tx_rate3 = rt->info[0].rateCode;
276572ff6f6SMatthew Dillon 				amn->amn_tx_rate3sp =
277572ff6f6SMatthew Dillon 					amn->amn_tx_rate3 | rt->info[0].shortPreamble;
278572ff6f6SMatthew Dillon 			} else {
279572ff6f6SMatthew Dillon 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
280572ff6f6SMatthew Dillon 			}
281572ff6f6SMatthew Dillon 		} else {
282572ff6f6SMatthew Dillon 			amn->amn_tx_try0 = ATH_TXMAXTRY;
283572ff6f6SMatthew Dillon 			/* theorically, these statements are useless because
284572ff6f6SMatthew Dillon 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
285572ff6f6SMatthew Dillon 			 */
286572ff6f6SMatthew Dillon 			amn->amn_tx_try1 = 0;
287572ff6f6SMatthew Dillon 			amn->amn_tx_try2 = 0;
288572ff6f6SMatthew Dillon 			amn->amn_tx_try3 = 0;
289572ff6f6SMatthew Dillon 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
290572ff6f6SMatthew Dillon 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
291572ff6f6SMatthew Dillon 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
292572ff6f6SMatthew Dillon 		}
293572ff6f6SMatthew Dillon 	}
294572ff6f6SMatthew Dillon 	node_reset(amn);
295572ff6f6SMatthew Dillon 
296572ff6f6SMatthew Dillon 	amn->amn_interval = ath_rateinterval;
297572ff6f6SMatthew Dillon 	if (vap->iv_opmode == IEEE80211_M_STA)
298572ff6f6SMatthew Dillon 		amn->amn_interval /= 2;
299572ff6f6SMatthew Dillon 	amn->amn_interval = (amn->amn_interval * hz) / 1000;
300572ff6f6SMatthew Dillon }
301572ff6f6SMatthew Dillon 
302572ff6f6SMatthew Dillon /*
303572ff6f6SMatthew Dillon  * Set the starting transmit rate for a node.
304572ff6f6SMatthew Dillon  */
305572ff6f6SMatthew Dillon static void
ath_rate_ctl_start(struct ath_softc * sc,struct ieee80211_node * ni)306572ff6f6SMatthew Dillon ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
307572ff6f6SMatthew Dillon {
308572ff6f6SMatthew Dillon #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
309572ff6f6SMatthew Dillon 	const struct ieee80211_txparam *tp = ni->ni_txparms;
310572ff6f6SMatthew Dillon 	int srate;
311572ff6f6SMatthew Dillon 
312572ff6f6SMatthew Dillon 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
313572ff6f6SMatthew Dillon 	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
314572ff6f6SMatthew Dillon 		/*
315572ff6f6SMatthew Dillon 		 * No fixed rate is requested. For 11b start with
316572ff6f6SMatthew Dillon 		 * the highest negotiated rate; otherwise, for 11g
317572ff6f6SMatthew Dillon 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
318572ff6f6SMatthew Dillon 		 */
319572ff6f6SMatthew Dillon 		srate = ni->ni_rates.rs_nrates - 1;
320572ff6f6SMatthew Dillon 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
321572ff6f6SMatthew Dillon 			/*
322572ff6f6SMatthew Dillon 			 * Scan the negotiated rate set to find the
323572ff6f6SMatthew Dillon 			 * closest rate.
324572ff6f6SMatthew Dillon 			 */
325572ff6f6SMatthew Dillon 			/* NB: the rate set is assumed sorted */
326572ff6f6SMatthew Dillon 			for (; srate >= 0 && RATE(srate) > 72; srate--)
327572ff6f6SMatthew Dillon 				;
328572ff6f6SMatthew Dillon 		}
329572ff6f6SMatthew Dillon 	} else {
330572ff6f6SMatthew Dillon 		/*
331572ff6f6SMatthew Dillon 		 * A fixed rate is to be used; ic_fixed_rate is the
332572ff6f6SMatthew Dillon 		 * IEEE code for this rate (sans basic bit).  Convert this
333572ff6f6SMatthew Dillon 		 * to the index into the negotiated rate set for
334572ff6f6SMatthew Dillon 		 * the node.  We know the rate is there because the
335572ff6f6SMatthew Dillon 		 * rate set is checked when the station associates.
336572ff6f6SMatthew Dillon 		 */
337572ff6f6SMatthew Dillon 		/* NB: the rate set is assumed sorted */
338572ff6f6SMatthew Dillon 		srate = ni->ni_rates.rs_nrates - 1;
339572ff6f6SMatthew Dillon 		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
340572ff6f6SMatthew Dillon 			;
341572ff6f6SMatthew Dillon 	}
342572ff6f6SMatthew Dillon 	/*
343572ff6f6SMatthew Dillon 	 * The selected rate may not be available due to races
344572ff6f6SMatthew Dillon 	 * and mode settings.  Also orphaned nodes created in
345572ff6f6SMatthew Dillon 	 * adhoc mode may not have any rate set so this lookup
346572ff6f6SMatthew Dillon 	 * can fail.  This is not fatal.
347572ff6f6SMatthew Dillon 	 */
348572ff6f6SMatthew Dillon 	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
349572ff6f6SMatthew Dillon #undef RATE
350572ff6f6SMatthew Dillon }
351572ff6f6SMatthew Dillon 
352572ff6f6SMatthew Dillon /*
353572ff6f6SMatthew Dillon  * Examine and potentially adjust the transmit rate.
354572ff6f6SMatthew Dillon  */
355572ff6f6SMatthew Dillon static void
ath_rate_ctl(void * arg,struct ieee80211_node * ni)356572ff6f6SMatthew Dillon ath_rate_ctl(void *arg, struct ieee80211_node *ni)
357572ff6f6SMatthew Dillon {
358572ff6f6SMatthew Dillon 	struct ath_softc *sc = arg;
359572ff6f6SMatthew Dillon 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
360572ff6f6SMatthew Dillon 	int rix;
361572ff6f6SMatthew Dillon 
362572ff6f6SMatthew Dillon #define is_success(amn) \
363572ff6f6SMatthew Dillon (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
364572ff6f6SMatthew Dillon #define is_enough(amn) \
365572ff6f6SMatthew Dillon (amn->amn_tx_try0_cnt > 10)
366572ff6f6SMatthew Dillon #define is_failure(amn) \
367572ff6f6SMatthew Dillon (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
368572ff6f6SMatthew Dillon 
369572ff6f6SMatthew Dillon 	rix = amn->amn_rix;
370572ff6f6SMatthew Dillon 
371572ff6f6SMatthew Dillon   	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
372572ff6f6SMatthew Dillon 	    "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
373572ff6f6SMatthew Dillon 	    amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
374572ff6f6SMatthew Dillon 	    amn->amn_tx_try3_cnt, amn->amn_success_threshold);
375572ff6f6SMatthew Dillon   	if (is_success (amn) && is_enough (amn)) {
376572ff6f6SMatthew Dillon 		amn->amn_success++;
377572ff6f6SMatthew Dillon 		if (amn->amn_success == amn->amn_success_threshold &&
378572ff6f6SMatthew Dillon 		    rix + 1 < ni->ni_rates.rs_nrates) {
379572ff6f6SMatthew Dillon   			amn->amn_recovery = 1;
380572ff6f6SMatthew Dillon   			amn->amn_success = 0;
381572ff6f6SMatthew Dillon   			rix++;
382572ff6f6SMatthew Dillon 			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
383572ff6f6SMatthew Dillon 			    "increase rate to %d", rix);
384572ff6f6SMatthew Dillon   		} else {
385572ff6f6SMatthew Dillon 			amn->amn_recovery = 0;
386572ff6f6SMatthew Dillon 		}
387572ff6f6SMatthew Dillon   	} else if (is_failure (amn)) {
388572ff6f6SMatthew Dillon   		amn->amn_success = 0;
389572ff6f6SMatthew Dillon 		if (rix > 0) {
390572ff6f6SMatthew Dillon   			if (amn->amn_recovery) {
391572ff6f6SMatthew Dillon   				/* recovery failure. */
392572ff6f6SMatthew Dillon   				amn->amn_success_threshold *= 2;
393572ff6f6SMatthew Dillon   				amn->amn_success_threshold = min (amn->amn_success_threshold,
394572ff6f6SMatthew Dillon 								  (u_int)ath_rate_max_success_threshold);
395572ff6f6SMatthew Dillon 				IEEE80211_NOTE(ni->ni_vap,
396572ff6f6SMatthew Dillon 				    IEEE80211_MSG_RATECTL, ni,
397572ff6f6SMatthew Dillon 				    "decrease rate recovery thr: %d",
398572ff6f6SMatthew Dillon 				    amn->amn_success_threshold);
399572ff6f6SMatthew Dillon   			} else {
400572ff6f6SMatthew Dillon   				/* simple failure. */
401572ff6f6SMatthew Dillon  				amn->amn_success_threshold = ath_rate_min_success_threshold;
402572ff6f6SMatthew Dillon 				IEEE80211_NOTE(ni->ni_vap,
403572ff6f6SMatthew Dillon 				    IEEE80211_MSG_RATECTL, ni,
404572ff6f6SMatthew Dillon 				    "decrease rate normal thr: %d",
405572ff6f6SMatthew Dillon 				    amn->amn_success_threshold);
406572ff6f6SMatthew Dillon   			}
407572ff6f6SMatthew Dillon 			amn->amn_recovery = 0;
408572ff6f6SMatthew Dillon   			rix--;
409572ff6f6SMatthew Dillon    		} else {
410572ff6f6SMatthew Dillon 			amn->amn_recovery = 0;
411572ff6f6SMatthew Dillon 		}
412572ff6f6SMatthew Dillon 
413572ff6f6SMatthew Dillon    	}
414572ff6f6SMatthew Dillon 	if (is_enough (amn) || rix != amn->amn_rix) {
415572ff6f6SMatthew Dillon 		/* reset counters. */
416572ff6f6SMatthew Dillon 		amn->amn_tx_try0_cnt = 0;
417572ff6f6SMatthew Dillon 		amn->amn_tx_try1_cnt = 0;
418572ff6f6SMatthew Dillon 		amn->amn_tx_try2_cnt = 0;
419572ff6f6SMatthew Dillon 		amn->amn_tx_try3_cnt = 0;
420572ff6f6SMatthew Dillon 		amn->amn_tx_failure_cnt = 0;
421572ff6f6SMatthew Dillon 	}
422572ff6f6SMatthew Dillon 	if (rix != amn->amn_rix) {
423572ff6f6SMatthew Dillon 		ath_rate_update(sc, ni, rix);
424572ff6f6SMatthew Dillon 	}
425572ff6f6SMatthew Dillon }
426572ff6f6SMatthew Dillon 
427572ff6f6SMatthew Dillon static int
ath_rate_fetch_node_stats(struct ath_softc * sc,struct ath_node * an,struct ath_rateioctl * re)428572ff6f6SMatthew Dillon ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
429572ff6f6SMatthew Dillon     struct ath_rateioctl *re)
430572ff6f6SMatthew Dillon {
431572ff6f6SMatthew Dillon 
432572ff6f6SMatthew Dillon 	return (EINVAL);
433572ff6f6SMatthew Dillon }
434572ff6f6SMatthew Dillon 
435572ff6f6SMatthew Dillon static void
ath_rate_sysctlattach(struct ath_softc * sc)436572ff6f6SMatthew Dillon ath_rate_sysctlattach(struct ath_softc *sc)
437572ff6f6SMatthew Dillon {
43826595b18SSascha Wildner 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
43926595b18SSascha Wildner 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
440572ff6f6SMatthew Dillon 
441572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
442572ff6f6SMatthew Dillon 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
443572ff6f6SMatthew Dillon 		"rate control: operation interval (ms)");
444572ff6f6SMatthew Dillon 	/* XXX bounds check values */
445572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
446572ff6f6SMatthew Dillon 		"max_sucess_threshold", CTLFLAG_RW,
447572ff6f6SMatthew Dillon 		&ath_rate_max_success_threshold, 0, "");
448572ff6f6SMatthew Dillon 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
449572ff6f6SMatthew Dillon 		"min_sucess_threshold", CTLFLAG_RW,
450572ff6f6SMatthew Dillon 		&ath_rate_min_success_threshold, 0, "");
451572ff6f6SMatthew Dillon }
452572ff6f6SMatthew Dillon 
453572ff6f6SMatthew Dillon struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)454572ff6f6SMatthew Dillon ath_rate_attach(struct ath_softc *sc)
455572ff6f6SMatthew Dillon {
456572ff6f6SMatthew Dillon 	struct amrr_softc *asc;
457572ff6f6SMatthew Dillon 
458dc249793SMatthew Dillon 	asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_INTWAIT|M_ZERO);
459572ff6f6SMatthew Dillon 	if (asc == NULL)
460572ff6f6SMatthew Dillon 		return NULL;
461572ff6f6SMatthew Dillon 	asc->arc.arc_space = sizeof(struct amrr_node);
462572ff6f6SMatthew Dillon 	ath_rate_sysctlattach(sc);
463572ff6f6SMatthew Dillon 
464572ff6f6SMatthew Dillon 	return &asc->arc;
465572ff6f6SMatthew Dillon }
466572ff6f6SMatthew Dillon 
467572ff6f6SMatthew Dillon void
ath_rate_detach(struct ath_ratectrl * arc)468572ff6f6SMatthew Dillon ath_rate_detach(struct ath_ratectrl *arc)
469572ff6f6SMatthew Dillon {
470572ff6f6SMatthew Dillon 	struct amrr_softc *asc = (struct amrr_softc *) arc;
471572ff6f6SMatthew Dillon 
472dc249793SMatthew Dillon 	kfree(asc, M_DEVBUF);
473572ff6f6SMatthew Dillon }
474dc249793SMatthew Dillon 
475dc249793SMatthew Dillon #if defined(__DragonFly__)
476dc249793SMatthew Dillon 
477dc249793SMatthew Dillon /*
478dc249793SMatthew Dillon  * Module glue.
479dc249793SMatthew Dillon  */
480dc249793SMatthew Dillon static int
amrr_modevent(module_t mod,int type,void * unused)481dc249793SMatthew Dillon amrr_modevent(module_t mod, int type, void *unused)
482dc249793SMatthew Dillon {
483dc249793SMatthew Dillon        int error;
484dc249793SMatthew Dillon 
485dc249793SMatthew Dillon        wlan_serialize_enter();
486dc249793SMatthew Dillon 
487dc249793SMatthew Dillon        switch (type) {
488dc249793SMatthew Dillon        case MOD_LOAD:
489dc249793SMatthew Dillon 	       if (bootverbose) {
490dc249793SMatthew Dillon 		       kprintf("ath_rate: <AMRR rate control "
491dc249793SMatthew Dillon 			       "algorithm> version 0.1\n");
492dc249793SMatthew Dillon 	       }
493dc249793SMatthew Dillon 	       error = 0;
494dc249793SMatthew Dillon 	       break;
495dc249793SMatthew Dillon        case MOD_UNLOAD:
496dc249793SMatthew Dillon 	       error = 0;
497dc249793SMatthew Dillon 	       break;
498dc249793SMatthew Dillon        default:
499dc249793SMatthew Dillon 	       error = EINVAL;
500dc249793SMatthew Dillon 	       break;
501dc249793SMatthew Dillon        }
502dc249793SMatthew Dillon        wlan_serialize_exit();
503dc249793SMatthew Dillon 
504dc249793SMatthew Dillon        return error;
505dc249793SMatthew Dillon }
506dc249793SMatthew Dillon 
507dc249793SMatthew Dillon static moduledata_t amrr_mod = {
508dc249793SMatthew Dillon 	"ath_rate",
509dc249793SMatthew Dillon        amrr_modevent,
510dc249793SMatthew Dillon        0
511dc249793SMatthew Dillon };
512dc249793SMatthew Dillon 
513dc249793SMatthew Dillon DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
514dc249793SMatthew Dillon MODULE_VERSION(ath_rate, 1);
515dc249793SMatthew Dillon MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);
516dc249793SMatthew Dillon MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
517dc249793SMatthew Dillon 
518dc249793SMatthew Dillon #endif
519