ath - use ifnet serialization calls instead of directly calling lwkt
[dragonfly.git] / sys / dev / netif / ath / rate_amrr / amrr.c
CommitLineData
193b341d
SZ
1/*
2 * Copyright (c) 2004 INRIA
3 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
19 *
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.
23 *
24 * NO WARRANTY
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.
36 *
8982d733
SZ
37 * $FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.12 2006/12/13 19:34:34 sam Exp $
38 * $DragonFly: src/sys/dev/netif/ath/rate_amrr/amrr.c,v 1.5 2007/02/22 05:17:09 sephe Exp $
193b341d
SZ
39 */
40
41/*
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
46 */
47
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>
53#include <sys/lock.h>
54#include <sys/errno.h>
55#include <sys/serialize.h>
193b341d
SZ
56#include <sys/bus.h>
57
58#include <sys/socket.h>
59
60#include <net/if.h>
61#include <net/if_media.h>
62#include <net/if_arp.h>
63
64#include <netproto/802_11/ieee80211_var.h>
65
66#include <net/bpf.h>
67
68#include <dev/netif/ath/ath/if_athvar.h>
69#include <dev/netif/ath/rate_amrr/amrr.h>
3f720b20 70#include <dev/netif/ath/hal/ath_hal/ah_desc.h>
193b341d
SZ
71
72#define AMRR_DEBUG
73#ifdef AMRR_DEBUG
74#define DPRINTF(sc, _fmt, ...) do { \
75 if (sc->sc_debug & 0x10) \
e3869ec7 76 kprintf(_fmt, __VA_ARGS__); \
193b341d
SZ
77} while (0)
78#else
79#define DPRINTF(sc, _fmt, ...)
80#endif
81
82static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
83static int ath_rate_max_success_threshold = 10;
84static int ath_rate_min_success_threshold = 1;
85
86static void ath_ratectl(void *);
87static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
88 int rate);
89static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
90static void ath_rate_ctl(void *, struct ieee80211_node *);
91
92void
93ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
94{
95 /* NB: assumed to be zero'd by caller */
96 ath_rate_update(sc, &an->an_node, 0);
97}
98
99void
100ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
101{
102}
103
104void
105ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
106 int shortPreamble, size_t frameLen,
107 uint8_t *rix, int *try0, uint8_t *txrate)
108{
109 struct amrr_node *amn = ATH_NODE_AMRR(an);
110
111 *rix = amn->amn_tx_rix0;
112 *try0 = amn->amn_tx_try0;
113 if (shortPreamble)
114 *txrate = amn->amn_tx_rate0sp;
115 else
116 *txrate = amn->amn_tx_rate0;
117}
118
119void
120ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
121 struct ath_desc *ds, int shortPreamble, uint8_t rix)
122{
123 struct amrr_node *amn = ATH_NODE_AMRR(an);
124
125 ath_hal_setupxtxdesc(sc->sc_ah, ds
126 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
127 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
128 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
129 );
130}
131
132void
133ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
8982d733 134 const struct ath_buf *bf)
193b341d
SZ
135{
136 struct amrr_node *amn = ATH_NODE_AMRR(an);
8982d733
SZ
137 const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
138 int sr = ts->ts_shortretry;
139 int lr = ts->ts_longretry;
193b341d
SZ
140 int retry_count = sr + lr;
141
142 amn->amn_tx_try0_cnt++;
143 if (retry_count == 1) {
144 amn->amn_tx_try1_cnt++;
145 } else if (retry_count == 2) {
146 amn->amn_tx_try1_cnt++;
147 amn->amn_tx_try2_cnt++;
148 } else if (retry_count == 3) {
149 amn->amn_tx_try1_cnt++;
150 amn->amn_tx_try2_cnt++;
151 amn->amn_tx_try3_cnt++;
152 } else if (retry_count > 3) {
153 amn->amn_tx_try1_cnt++;
154 amn->amn_tx_try2_cnt++;
155 amn->amn_tx_try3_cnt++;
156 amn->amn_tx_failure_cnt++;
157 }
158}
159
160void
161ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
162{
163 if (isnew)
164 ath_rate_ctl_start(sc, &an->an_node);
165}
166
167static void
168node_reset (struct amrr_node *amn)
169{
170 amn->amn_tx_try0_cnt = 0;
171 amn->amn_tx_try1_cnt = 0;
172 amn->amn_tx_try2_cnt = 0;
173 amn->amn_tx_try3_cnt = 0;
174 amn->amn_tx_failure_cnt = 0;
175 amn->amn_success = 0;
176 amn->amn_recovery = 0;
177 amn->amn_success_threshold = ath_rate_min_success_threshold;
178}
179
180
181/**
182 * The code below assumes that we are dealing with hardware multi rate retry
183 * I have no idea what will happen if you try to use this module with another
184 * type of hardware. Your machine might catch fire or it might work with
185 * horrible performance...
186 */
187static void
188ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
189{
190 struct ath_node *an = ATH_NODE(ni);
191 struct amrr_node *amn = ATH_NODE_AMRR(an);
192 const HAL_RATE_TABLE *rt = sc->sc_currates;
193 uint8_t rix;
194
195 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
196
197 DPRINTF(sc, "%s: set xmit rate for %6D to %dM\n",
198 __func__, ni->ni_macaddr, ":",
199 ni->ni_rates.rs_nrates > 0 ?
200 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
201
202 ni->ni_txrate = rate;
203 /*
204 * Before associating a node has no rate set setup
205 * so we can't calculate any transmit codes to use.
206 * This is ok since we should never be sending anything
207 * but management frames and those always go at the
208 * lowest hardware rate.
209 */
210 if (ni->ni_rates.rs_nrates > 0) {
211 amn->amn_tx_rix0 = sc->sc_rixmap[
212 ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
213 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
214 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
215 rt->info[amn->amn_tx_rix0].shortPreamble;
216 if (sc->sc_mrretry) {
217 amn->amn_tx_try0 = 1;
218 amn->amn_tx_try1 = 1;
219 amn->amn_tx_try2 = 1;
220 amn->amn_tx_try3 = 1;
221 if (--rate >= 0) {
222 rix = sc->sc_rixmap[
223 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
224 amn->amn_tx_rate1 = rt->info[rix].rateCode;
225 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
226 rt->info[rix].shortPreamble;
227 } else {
228 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
229 }
230 if (--rate >= 0) {
231 rix = sc->sc_rixmap[
232 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
233 amn->amn_tx_rate2 = rt->info[rix].rateCode;
234 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
235 rt->info[rix].shortPreamble;
236 } else {
237 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
238 }
239 if (rate > 0) {
240 /* NB: only do this if we didn't already do it above */
241 amn->amn_tx_rate3 = rt->info[0].rateCode;
242 amn->amn_tx_rate3sp =
243 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
244 } else {
245 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
246 }
247 } else {
248 amn->amn_tx_try0 = ATH_TXMAXTRY;
249 /* theorically, these statements are useless because
250 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
251 */
252 amn->amn_tx_try1 = 0;
253 amn->amn_tx_try2 = 0;
254 amn->amn_tx_try3 = 0;
255 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
256 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
257 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
258 }
259 }
260 node_reset (amn);
261}
262
263/*
264 * Set the starting transmit rate for a node.
265 */
266static void
267ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
268{
269#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
270 struct ieee80211com *ic = &sc->sc_ic;
271 int srate;
272
273 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
274 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
275 /*
276 * No fixed rate is requested. For 11b start with
277 * the highest negotiated rate; otherwise, for 11g
278 * and 11a, we start "in the middle" at 24Mb or 36Mb.
279 */
280 srate = ni->ni_rates.rs_nrates - 1;
281 if (sc->sc_curmode != IEEE80211_MODE_11B) {
282 /*
283 * Scan the negotiated rate set to find the
284 * closest rate.
285 */
286 /* NB: the rate set is assumed sorted */
287 for (; srate >= 0 && RATE(srate) > 72; srate--)
288 ;
289 KASSERT(srate >= 0, ("bogus rate set"));
290 }
291 } else {
292 /*
293 * A fixed rate is to be used; ic_fixed_rate is an
294 * index into the supported rate set. Convert this
295 * to the index into the negotiated rate set for
296 * the node. We know the rate is there because the
297 * rate set is checked when the station associates.
298 */
299 const struct ieee80211_rateset *rs =
300 &ic->ic_sup_rates[ic->ic_curmode];
301 int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
302 /* NB: the rate set is assumed sorted */
303 srate = ni->ni_rates.rs_nrates - 1;
304 for (; srate >= 0 && RATE(srate) != r; srate--)
305 ;
306 KASSERT(srate >= 0,
307 ("fixed rate %d not in rate set", ic->ic_fixed_rate));
308 }
309 ath_rate_update(sc, ni, srate);
310#undef RATE
311}
312
313static void
314ath_rate_cb(void *arg, struct ieee80211_node *ni)
315{
316 struct ath_softc *sc = arg;
317
318 ath_rate_update(sc, ni, 0);
319}
320
321/*
322 * Reset the rate control state for each 802.11 state transition.
323 */
324void
325ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
326{
327 struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
328 struct ieee80211com *ic = &sc->sc_ic;
329 struct ifnet *ifp = &ic->ic_if;
330 struct ieee80211_node *ni;
331
332 ASSERT_SERIALIZED(ifp->if_serializer);
333
334 if (state == IEEE80211_S_INIT) {
335 callout_stop(&asc->timer);
336 return;
337 }
338 if (ic->ic_opmode == IEEE80211_M_STA) {
339 /*
340 * Reset local xmit state; this is really only
341 * meaningful when operating in station mode.
342 */
343 ni = ic->ic_bss;
344 if (state == IEEE80211_S_RUN) {
345 ath_rate_ctl_start(sc, ni);
346 } else {
347 ath_rate_update(sc, ni, 0);
348 }
349 } else {
350 /*
351 * When operating as a station the node table holds
352 * the AP's that were discovered during scanning.
353 * For any other operating mode we want to reset the
354 * tx rate state of each node.
355 */
356 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
357 ath_rate_update(sc, ic->ic_bss, 0);
358 }
359 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
360 state == IEEE80211_S_RUN) {
361 int interval;
362 /*
363 * Start the background rate control thread if we
364 * are not configured to use a fixed xmit rate.
365 */
366 interval = ath_rateinterval;
367 if (ic->ic_opmode == IEEE80211_M_STA)
368 interval /= 2;
369 callout_reset(&asc->timer, (interval * hz) / 1000,
370 ath_ratectl, ifp);
371 }
372}
373
374/*
375 * Examine and potentially adjust the transmit rate.
376 */
377static void
378ath_rate_ctl(void *arg, struct ieee80211_node *ni)
379{
380 struct ath_softc *sc = arg;
381 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
382 int old_rate;
383
384#define is_success(amn) \
385(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
386#define is_enough(amn) \
387(amn->amn_tx_try0_cnt > 10)
388#define is_failure(amn) \
389(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
390#define is_max_rate(ni) \
391((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
392#define is_min_rate(ni) \
393(ni->ni_txrate == 0)
394
395 old_rate = ni->ni_txrate;
396
397 DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
398 amn->amn_tx_try0_cnt,
399 amn->amn_tx_try1_cnt,
400 amn->amn_tx_try2_cnt,
401 amn->amn_tx_try3_cnt,
402 amn->amn_success_threshold);
403 if (is_success (amn) && is_enough (amn)) {
404 amn->amn_success++;
405 if (amn->amn_success == amn->amn_success_threshold &&
406 !is_max_rate (ni)) {
407 amn->amn_recovery = 1;
408 amn->amn_success = 0;
409 ni->ni_txrate++;
410 DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
411 } else {
412 amn->amn_recovery = 0;
413 }
414 } else if (is_failure (amn)) {
415 amn->amn_success = 0;
416 if (!is_min_rate (ni)) {
417 if (amn->amn_recovery) {
418 /* recovery failure. */
419 amn->amn_success_threshold *= 2;
420 amn->amn_success_threshold = min (amn->amn_success_threshold,
421 (u_int)ath_rate_max_success_threshold);
422 DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
423 } else {
424 /* simple failure. */
425 amn->amn_success_threshold = ath_rate_min_success_threshold;
426 DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
427 }
428 amn->amn_recovery = 0;
429 ni->ni_txrate--;
430 } else {
431 amn->amn_recovery = 0;
432 }
433
434 }
435 if (is_enough (amn) || old_rate != ni->ni_txrate) {
436 /* reset counters. */
437 amn->amn_tx_try0_cnt = 0;
438 amn->amn_tx_try1_cnt = 0;
439 amn->amn_tx_try2_cnt = 0;
440 amn->amn_tx_try3_cnt = 0;
441 amn->amn_tx_failure_cnt = 0;
442 }
443 if (old_rate != ni->ni_txrate) {
444 ath_rate_update(sc, ni, ni->ni_txrate);
445 }
446}
447
448static void
449ath_ratectl(void *arg)
450{
451 struct ifnet *ifp = arg;
452 struct ath_softc *sc = ifp->if_softc;
453 struct amrr_softc *asc = (struct amrr_softc *)sc->sc_rc;
454 struct ieee80211com *ic = &sc->sc_ic;
455 int interval;
456
19996047 457 ifnet_serialize_all(ifp);
193b341d
SZ
458
459 if (ifp->if_flags & IFF_RUNNING) {
460 sc->sc_stats.ast_rate_calls++;
461
462 if (ic->ic_opmode == IEEE80211_M_STA)
463 ath_rate_ctl(sc, ic->ic_bss); /* NB: no reference */
464 else
465 ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
466 }
467 interval = ath_rateinterval;
468 if (ic->ic_opmode == IEEE80211_M_STA)
469 interval /= 2;
470 callout_reset(&asc->timer, (interval * hz) / 1000, ath_ratectl, ifp);
471
19996047 472 ifnet_deserialize_all(ifp);
193b341d
SZ
473}
474
475static void
476ath_rate_sysctlattach(struct ath_softc *sc)
477{
478 struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
479 struct sysctl_oid *tree = sc->sc_sysctl_tree;
480
481 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
482 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
483 "rate control: operation interval (ms)");
484 /* XXX bounds check values */
485 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
486 "max_sucess_threshold", CTLFLAG_RW,
487 &ath_rate_max_success_threshold, 0, "");
488 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
489 "min_sucess_threshold", CTLFLAG_RW,
490 &ath_rate_min_success_threshold, 0, "");
491}
492
493struct ath_ratectrl *
494ath_rate_attach(struct ath_softc *sc)
495{
496 struct amrr_softc *asc;
497
efda3bd0 498 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
193b341d
SZ
499 if (asc == NULL)
500 return NULL;
501 asc->arc.arc_space = sizeof(struct amrr_node);
502 callout_init(&asc->timer);
503 ath_rate_sysctlattach(sc);
504
505 return &asc->arc;
506}
507
508void
509ath_rate_detach(struct ath_ratectrl *arc)
510{
511 struct amrr_softc *asc = (struct amrr_softc *)arc;
512
efda3bd0 513 kfree(asc, M_DEVBUF);
193b341d
SZ
514}
515
516void
517ath_rate_stop(struct ath_ratectrl *arc)
518{
519 struct amrr_softc *asc = (struct amrr_softc *)arc;
520
521 /* ASSERT_SERIALIZED */
522 callout_stop(&asc->timer);
523}
524
525/*
526 * Module glue.
527 */
528static int
529amrr_modevent(module_t mod, int type, void *unused)
530{
531 switch (type) {
532 case MOD_LOAD:
533 if (bootverbose)
e3869ec7 534 kprintf("ath_rate: <AMRR rate control algorithm> version 0.1\n");
193b341d
SZ
535 return 0;
536 case MOD_UNLOAD:
537 return 0;
538 }
539 return EINVAL;
540}
541
542static moduledata_t amrr_mod = {
543 "ath_rate",
544 amrr_modevent,
545 0
546};
547DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
548MODULE_VERSION(ath_rate, 1);
549MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);