2 * Copyright (c) 2004 INRIA
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
14 * redistribution must be conditioned upon including a substantially
15 * similar Disclaimer requirement for further binary redistribution.
16 * 3. Neither the names of the above-listed copyright holders nor the names
17 * of any contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
28 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
29 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
30 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
33 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35 * THE POSSIBILITY OF SUCH DAMAGES.
37 * $FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.8.2.3 2006/02/24 19:51:11 sam Exp $
38 * $DragonFly: src/sys/dev/netif/ath/rate_amrr/amrr.c,v 1.2 2006/09/05 00:55:39 dillon Exp $
42 * AMRR rate control. See:
43 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
44 * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
45 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/sysctl.h>
51 #include <sys/module.h>
52 #include <sys/kernel.h>
54 #include <sys/errno.h>
55 #include <sys/serialize.h>
57 #include <machine/bus.h>
58 #include <machine/resource.h>
61 #include <sys/socket.h>
64 #include <net/if_media.h>
65 #include <net/if_arp.h>
67 #include <netproto/802_11/ieee80211_var.h>
71 #include <dev/netif/ath/ath/if_athvar.h>
72 #include <dev/netif/ath/rate_amrr/amrr.h>
73 #include <contrib/dev/ath/ah_desc.h>
77 #define DPRINTF(sc, _fmt, ...) do { \
78 if (sc->sc_debug & 0x10) \
79 printf(_fmt, __VA_ARGS__); \
82 #define DPRINTF(sc, _fmt, ...)
85 static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
86 static int ath_rate_max_success_threshold = 10;
87 static int ath_rate_min_success_threshold = 1;
89 static void ath_ratectl(void *);
90 static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
92 static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
93 static void ath_rate_ctl(void *, struct ieee80211_node *);
96 ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
98 /* NB: assumed to be zero'd by caller */
99 ath_rate_update(sc, &an->an_node, 0);
103 ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
108 ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
109 int shortPreamble, size_t frameLen,
110 uint8_t *rix, int *try0, uint8_t *txrate)
112 struct amrr_node *amn = ATH_NODE_AMRR(an);
114 *rix = amn->amn_tx_rix0;
115 *try0 = amn->amn_tx_try0;
117 *txrate = amn->amn_tx_rate0sp;
119 *txrate = amn->amn_tx_rate0;
123 ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
124 struct ath_desc *ds, int shortPreamble, uint8_t rix)
126 struct amrr_node *amn = ATH_NODE_AMRR(an);
128 ath_hal_setupxtxdesc(sc->sc_ah, ds
129 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
130 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
131 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
136 ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
137 const struct ath_desc *ds, const struct ath_desc *ds0)
139 struct amrr_node *amn = ATH_NODE_AMRR(an);
140 int sr = ds->ds_txstat.ts_shortretry;
141 int lr = ds->ds_txstat.ts_longretry;
142 int retry_count = sr + lr;
144 amn->amn_tx_try0_cnt++;
145 if (retry_count == 1) {
146 amn->amn_tx_try1_cnt++;
147 } else if (retry_count == 2) {
148 amn->amn_tx_try1_cnt++;
149 amn->amn_tx_try2_cnt++;
150 } else if (retry_count == 3) {
151 amn->amn_tx_try1_cnt++;
152 amn->amn_tx_try2_cnt++;
153 amn->amn_tx_try3_cnt++;
154 } else if (retry_count > 3) {
155 amn->amn_tx_try1_cnt++;
156 amn->amn_tx_try2_cnt++;
157 amn->amn_tx_try3_cnt++;
158 amn->amn_tx_failure_cnt++;
163 ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
166 ath_rate_ctl_start(sc, &an->an_node);
170 node_reset (struct amrr_node *amn)
172 amn->amn_tx_try0_cnt = 0;
173 amn->amn_tx_try1_cnt = 0;
174 amn->amn_tx_try2_cnt = 0;
175 amn->amn_tx_try3_cnt = 0;
176 amn->amn_tx_failure_cnt = 0;
177 amn->amn_success = 0;
178 amn->amn_recovery = 0;
179 amn->amn_success_threshold = ath_rate_min_success_threshold;
184 * The code below assumes that we are dealing with hardware multi rate retry
185 * I have no idea what will happen if you try to use this module with another
186 * type of hardware. Your machine might catch fire or it might work with
187 * horrible performance...
190 ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
192 struct ath_node *an = ATH_NODE(ni);
193 struct amrr_node *amn = ATH_NODE_AMRR(an);
194 const HAL_RATE_TABLE *rt = sc->sc_currates;
197 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
199 DPRINTF(sc, "%s: set xmit rate for %6D to %dM\n",
200 __func__, ni->ni_macaddr, ":",
201 ni->ni_rates.rs_nrates > 0 ?
202 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
204 ni->ni_txrate = rate;
206 * Before associating a node has no rate set setup
207 * so we can't calculate any transmit codes to use.
208 * This is ok since we should never be sending anything
209 * but management frames and those always go at the
210 * lowest hardware rate.
212 if (ni->ni_rates.rs_nrates > 0) {
213 amn->amn_tx_rix0 = sc->sc_rixmap[
214 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
215 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
216 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
217 rt->info[amn->amn_tx_rix0].shortPreamble;
218 if (sc->sc_mrretry) {
219 amn->amn_tx_try0 = 1;
220 amn->amn_tx_try1 = 1;
221 amn->amn_tx_try2 = 1;
222 amn->amn_tx_try3 = 1;
225 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
226 amn->amn_tx_rate1 = rt->info[rix].rateCode;
227 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
228 rt->info[rix].shortPreamble;
230 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
234 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
235 amn->amn_tx_rate2 = rt->info[rix].rateCode;
236 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
237 rt->info[rix].shortPreamble;
239 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
242 /* NB: only do this if we didn't already do it above */
243 amn->amn_tx_rate3 = rt->info[0].rateCode;
244 amn->amn_tx_rate3sp =
245 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
247 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
250 amn->amn_tx_try0 = ATH_TXMAXTRY;
251 /* theorically, these statements are useless because
252 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
254 amn->amn_tx_try1 = 0;
255 amn->amn_tx_try2 = 0;
256 amn->amn_tx_try3 = 0;
257 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
258 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
259 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
266 * Set the starting transmit rate for a node.
269 ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
271 #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
272 struct ieee80211com *ic = &sc->sc_ic;
275 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
276 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
278 * No fixed rate is requested. For 11b start with
279 * the highest negotiated rate; otherwise, for 11g
280 * and 11a, we start "in the middle" at 24Mb or 36Mb.
282 srate = ni->ni_rates.rs_nrates - 1;
283 if (sc->sc_curmode != IEEE80211_MODE_11B) {
285 * Scan the negotiated rate set to find the
288 /* NB: the rate set is assumed sorted */
289 for (; srate >= 0 && RATE(srate) > 72; srate--)
291 KASSERT(srate >= 0, ("bogus rate set"));
295 * A fixed rate is to be used; ic_fixed_rate is an
296 * index into the supported rate set. Convert this
297 * to the index into the negotiated rate set for
298 * the node. We know the rate is there because the
299 * rate set is checked when the station associates.
301 const struct ieee80211_rateset *rs =
302 &ic->ic_sup_rates[ic->ic_curmode];
303 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
304 /* NB: the rate set is assumed sorted */
305 srate = ni->ni_rates.rs_nrates - 1;
306 for (; srate >= 0 && RATE(srate) != r; srate--)
309 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
311 ath_rate_update(sc, ni, srate);
316 ath_rate_cb(void *arg, struct ieee80211_node *ni)
318 struct ath_softc *sc = arg;
320 ath_rate_update(sc, ni, 0);
324 * Reset the rate control state for each 802.11 state transition.
327 ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
329 struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
330 struct ieee80211com *ic = &sc->sc_ic;
331 struct ifnet *ifp = &ic->ic_if;
332 struct ieee80211_node *ni;
334 ASSERT_SERIALIZED(ifp->if_serializer);
336 if (state == IEEE80211_S_INIT) {
337 callout_stop(&asc->timer);
340 if (ic->ic_opmode == IEEE80211_M_STA) {
342 * Reset local xmit state; this is really only
343 * meaningful when operating in station mode.
346 if (state == IEEE80211_S_RUN) {
347 ath_rate_ctl_start(sc, ni);
349 ath_rate_update(sc, ni, 0);
353 * When operating as a station the node table holds
354 * the AP's that were discovered during scanning.
355 * For any other operating mode we want to reset the
356 * tx rate state of each node.
358 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
359 ath_rate_update(sc, ic->ic_bss, 0);
361 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
362 state == IEEE80211_S_RUN) {
365 * Start the background rate control thread if we
366 * are not configured to use a fixed xmit rate.
368 interval = ath_rateinterval;
369 if (ic->ic_opmode == IEEE80211_M_STA)
371 callout_reset(&asc->timer, (interval * hz) / 1000,
377 * Examine and potentially adjust the transmit rate.
380 ath_rate_ctl(void *arg, struct ieee80211_node *ni)
382 struct ath_softc *sc = arg;
383 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
386 #define is_success(amn) \
387 (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
388 #define is_enough(amn) \
389 (amn->amn_tx_try0_cnt > 10)
390 #define is_failure(amn) \
391 (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
392 #define is_max_rate(ni) \
393 ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
394 #define is_min_rate(ni) \
397 old_rate = ni->ni_txrate;
399 DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
400 amn->amn_tx_try0_cnt,
401 amn->amn_tx_try1_cnt,
402 amn->amn_tx_try2_cnt,
403 amn->amn_tx_try3_cnt,
404 amn->amn_success_threshold);
405 if (is_success (amn) && is_enough (amn)) {
407 if (amn->amn_success == amn->amn_success_threshold &&
409 amn->amn_recovery = 1;
410 amn->amn_success = 0;
412 DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
414 amn->amn_recovery = 0;
416 } else if (is_failure (amn)) {
417 amn->amn_success = 0;
418 if (!is_min_rate (ni)) {
419 if (amn->amn_recovery) {
420 /* recovery failure. */
421 amn->amn_success_threshold *= 2;
422 amn->amn_success_threshold = min (amn->amn_success_threshold,
423 (u_int)ath_rate_max_success_threshold);
424 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
426 /* simple failure. */
427 amn->amn_success_threshold = ath_rate_min_success_threshold;
428 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
430 amn->amn_recovery = 0;
433 amn->amn_recovery = 0;
437 if (is_enough (amn) || old_rate != ni->ni_txrate) {
438 /* reset counters. */
439 amn->amn_tx_try0_cnt = 0;
440 amn->amn_tx_try1_cnt = 0;
441 amn->amn_tx_try2_cnt = 0;
442 amn->amn_tx_try3_cnt = 0;
443 amn->amn_tx_failure_cnt = 0;
445 if (old_rate != ni->ni_txrate) {
446 ath_rate_update(sc, ni, ni->ni_txrate);
451 ath_ratectl(void *arg)
453 struct ifnet *ifp = arg;
454 struct ath_softc *sc = ifp->if_softc;
455 struct amrr_softc *asc = (struct amrr_softc *)sc->sc_rc;
456 struct ieee80211com *ic = &sc->sc_ic;
459 lwkt_serialize_enter(ifp->if_serializer);
461 if (ifp->if_flags & IFF_RUNNING) {
462 sc->sc_stats.ast_rate_calls++;
464 if (ic->ic_opmode == IEEE80211_M_STA)
465 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */
467 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
469 interval = ath_rateinterval;
470 if (ic->ic_opmode == IEEE80211_M_STA)
472 callout_reset(&asc->timer, (interval * hz) / 1000, ath_ratectl, ifp);
474 lwkt_serialize_exit(ifp->if_serializer);
478 ath_rate_sysctlattach(struct ath_softc *sc)
480 struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
481 struct sysctl_oid *tree = sc->sc_sysctl_tree;
483 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
484 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
485 "rate control: operation interval (ms)");
486 /* XXX bounds check values */
487 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
488 "max_sucess_threshold", CTLFLAG_RW,
489 &ath_rate_max_success_threshold, 0, "");
490 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
491 "min_sucess_threshold", CTLFLAG_RW,
492 &ath_rate_min_success_threshold, 0, "");
495 struct ath_ratectrl *
496 ath_rate_attach(struct ath_softc *sc)
498 struct amrr_softc *asc;
500 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
503 asc->arc.arc_space = sizeof(struct amrr_node);
504 callout_init(&asc->timer);
505 ath_rate_sysctlattach(sc);
511 ath_rate_detach(struct ath_ratectrl *arc)
513 struct amrr_softc *asc = (struct amrr_softc *)arc;
515 kfree(asc, M_DEVBUF);
519 ath_rate_stop(struct ath_ratectrl *arc)
521 struct amrr_softc *asc = (struct amrr_softc *)arc;
523 /* ASSERT_SERIALIZED */
524 callout_stop(&asc->timer);
531 amrr_modevent(module_t mod, int type, void *unused)
536 printf("ath_rate: <AMRR rate control algorithm> version 0.1\n");
544 static moduledata_t amrr_mod = {
549 DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
550 MODULE_VERSION(ath_rate, 1);
551 MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);