ath - Basic re-port, base code compile
[dragonfly.git] / sys / dev / netif / ath / ath_rate / amrr / amrr.c
CommitLineData
572ff6f6 1/*-
193b341d
SZ
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 *
193b341d
SZ
37 */
38
572ff6f6 39#include <sys/cdefs.h>
572ff6f6 40
193b341d
SZ
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 */
572ff6f6
MD
47#include "opt_ath.h"
48#include "opt_inet.h"
49#include "opt_wlan.h"
193b341d
SZ
50
51#include <sys/param.h>
52#include <sys/systm.h>
53#include <sys/sysctl.h>
193b341d
SZ
54#include <sys/kernel.h>
55#include <sys/lock.h>
572ff6f6 56#include <sys/mutex.h>
193b341d 57#include <sys/errno.h>
193b341d 58#include <sys/bus.h>
193b341d
SZ
59#include <sys/socket.h>
60
61#include <net/if.h>
62#include <net/if_media.h>
63#include <net/if_arp.h>
64
5cd80a8c 65#include <netproto/802_11/ieee80211_var.h>
193b341d
SZ
66
67#include <net/bpf.h>
68
572ff6f6
MD
69#ifdef INET
70#include <netinet/in.h>
71#include <netinet/if_ether.h>
193b341d
SZ
72#endif
73
5cd80a8c
MD
74#include <dev/netif/ath/ath/if_athvar.h>
75#include <dev/netif/ath/ath_rate/amrr/amrr.h>
76#include <dev/netif/ath/ath_hal/ah_desc.h>
572ff6f6 77
193b341d
SZ
78static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
79static int ath_rate_max_success_threshold = 10;
80static int ath_rate_min_success_threshold = 1;
81
193b341d
SZ
82static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
83 int rate);
84static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
85static void ath_rate_ctl(void *, struct ieee80211_node *);
86
87void
88ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
89{
90 /* NB: assumed to be zero'd by caller */
193b341d
SZ
91}
92
93void
94ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
95{
96}
97
98void
99ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
100 int shortPreamble, size_t frameLen,
572ff6f6 101 u_int8_t *rix, int *try0, u_int8_t *txrate)
193b341d
SZ
102{
103 struct amrr_node *amn = ATH_NODE_AMRR(an);
104
105 *rix = amn->amn_tx_rix0;
106 *try0 = amn->amn_tx_try0;
107 if (shortPreamble)
108 *txrate = amn->amn_tx_rate0sp;
109 else
110 *txrate = amn->amn_tx_rate0;
111}
112
572ff6f6
MD
113/*
114 * Get the TX rates.
115 *
116 * The short preamble bits aren't set here; the caller should augment
117 * the returned rate with the relevant preamble rate flag.
118 */
119void
120ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
121 uint8_t rix0, struct ath_rc_series *rc)
122{
123 struct amrr_node *amn = ATH_NODE_AMRR(an);
124
125 rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
126
127 rc[0].rix = amn->amn_tx_rate0;
128 rc[1].rix = amn->amn_tx_rate1;
129 rc[2].rix = amn->amn_tx_rate2;
130 rc[3].rix = amn->amn_tx_rate3;
131
132 rc[0].tries = amn->amn_tx_try0;
133 rc[1].tries = amn->amn_tx_try1;
134 rc[2].tries = amn->amn_tx_try2;
135 rc[3].tries = amn->amn_tx_try3;
136}
137
138
193b341d
SZ
139void
140ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
572ff6f6 141 struct ath_desc *ds, int shortPreamble, u_int8_t rix)
193b341d
SZ
142{
143 struct amrr_node *amn = ATH_NODE_AMRR(an);
144
145 ath_hal_setupxtxdesc(sc->sc_ah, ds
146 , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
147 , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
148 , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
149 );
150}
151
152void
153ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
572ff6f6
MD
154 const struct ath_rc_series *rc, const struct ath_tx_status *ts,
155 int frame_size, int nframes, int nbad)
193b341d
SZ
156{
157 struct amrr_node *amn = ATH_NODE_AMRR(an);
8982d733
SZ
158 int sr = ts->ts_shortretry;
159 int lr = ts->ts_longretry;
193b341d
SZ
160 int retry_count = sr + lr;
161
162 amn->amn_tx_try0_cnt++;
163 if (retry_count == 1) {
164 amn->amn_tx_try1_cnt++;
165 } else if (retry_count == 2) {
166 amn->amn_tx_try1_cnt++;
167 amn->amn_tx_try2_cnt++;
168 } else if (retry_count == 3) {
169 amn->amn_tx_try1_cnt++;
170 amn->amn_tx_try2_cnt++;
171 amn->amn_tx_try3_cnt++;
172 } else if (retry_count > 3) {
173 amn->amn_tx_try1_cnt++;
174 amn->amn_tx_try2_cnt++;
175 amn->amn_tx_try3_cnt++;
176 amn->amn_tx_failure_cnt++;
177 }
572ff6f6
MD
178 if (amn->amn_interval != 0 &&
179 ticks - amn->amn_ticks > amn->amn_interval) {
180 ath_rate_ctl(sc, &an->an_node);
181 amn->amn_ticks = ticks;
182 }
193b341d
SZ
183}
184
185void
186ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
187{
188 if (isnew)
189 ath_rate_ctl_start(sc, &an->an_node);
190}
191
192static void
572ff6f6 193node_reset(struct amrr_node *amn)
193b341d
SZ
194{
195 amn->amn_tx_try0_cnt = 0;
196 amn->amn_tx_try1_cnt = 0;
197 amn->amn_tx_try2_cnt = 0;
198 amn->amn_tx_try3_cnt = 0;
199 amn->amn_tx_failure_cnt = 0;
200 amn->amn_success = 0;
201 amn->amn_recovery = 0;
202 amn->amn_success_threshold = ath_rate_min_success_threshold;
203}
204
205
206/**
207 * The code below assumes that we are dealing with hardware multi rate retry
208 * I have no idea what will happen if you try to use this module with another
209 * type of hardware. Your machine might catch fire or it might work with
210 * horrible performance...
211 */
212static void
213ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
214{
215 struct ath_node *an = ATH_NODE(ni);
216 struct amrr_node *amn = ATH_NODE_AMRR(an);
572ff6f6 217 struct ieee80211vap *vap = ni->ni_vap;
193b341d 218 const HAL_RATE_TABLE *rt = sc->sc_currates;
572ff6f6 219 u_int8_t rix;
193b341d
SZ
220
221 KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
222
572ff6f6
MD
223 IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
224 "%s: set xmit rate to %dM", __func__,
193b341d
SZ
225 ni->ni_rates.rs_nrates > 0 ?
226 (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
227
572ff6f6 228 amn->amn_rix = rate;
193b341d
SZ
229 /*
230 * Before associating a node has no rate set setup
231 * so we can't calculate any transmit codes to use.
232 * This is ok since we should never be sending anything
233 * but management frames and those always go at the
234 * lowest hardware rate.
235 */
236 if (ni->ni_rates.rs_nrates > 0) {
572ff6f6
MD
237 ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
238 amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
193b341d
SZ
239 amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
240 amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
241 rt->info[amn->amn_tx_rix0].shortPreamble;
242 if (sc->sc_mrretry) {
243 amn->amn_tx_try0 = 1;
244 amn->amn_tx_try1 = 1;
245 amn->amn_tx_try2 = 1;
246 amn->amn_tx_try3 = 1;
247 if (--rate >= 0) {
248 rix = sc->sc_rixmap[
249 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
250 amn->amn_tx_rate1 = rt->info[rix].rateCode;
251 amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
252 rt->info[rix].shortPreamble;
253 } else {
254 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
255 }
256 if (--rate >= 0) {
257 rix = sc->sc_rixmap[
258 ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
259 amn->amn_tx_rate2 = rt->info[rix].rateCode;
260 amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
261 rt->info[rix].shortPreamble;
262 } else {
263 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
264 }
265 if (rate > 0) {
266 /* NB: only do this if we didn't already do it above */
267 amn->amn_tx_rate3 = rt->info[0].rateCode;
268 amn->amn_tx_rate3sp =
269 amn->amn_tx_rate3 | rt->info[0].shortPreamble;
270 } else {
271 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
272 }
273 } else {
274 amn->amn_tx_try0 = ATH_TXMAXTRY;
275 /* theorically, these statements are useless because
276 * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
277 */
278 amn->amn_tx_try1 = 0;
279 amn->amn_tx_try2 = 0;
280 amn->amn_tx_try3 = 0;
281 amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
282 amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
283 amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
284 }
285 }
572ff6f6
MD
286 node_reset(amn);
287
288 amn->amn_interval = ath_rateinterval;
289 if (vap->iv_opmode == IEEE80211_M_STA)
290 amn->amn_interval /= 2;
291 amn->amn_interval = (amn->amn_interval * hz) / 1000;
193b341d
SZ
292}
293
294/*
295 * Set the starting transmit rate for a node.
296 */
297static void
298ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
299{
300#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
572ff6f6 301 const struct ieee80211_txparam *tp = ni->ni_txparms;
193b341d
SZ
302 int srate;
303
304 KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
572ff6f6 305 if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
193b341d
SZ
306 /*
307 * No fixed rate is requested. For 11b start with
308 * the highest negotiated rate; otherwise, for 11g
309 * and 11a, we start "in the middle" at 24Mb or 36Mb.
310 */
311 srate = ni->ni_rates.rs_nrates - 1;
312 if (sc->sc_curmode != IEEE80211_MODE_11B) {
313 /*
314 * Scan the negotiated rate set to find the
315 * closest rate.
316 */
317 /* NB: the rate set is assumed sorted */
318 for (; srate >= 0 && RATE(srate) > 72; srate--)
319 ;
193b341d
SZ
320 }
321 } else {
322 /*
572ff6f6
MD
323 * A fixed rate is to be used; ic_fixed_rate is the
324 * IEEE code for this rate (sans basic bit). Convert this
193b341d
SZ
325 * to the index into the negotiated rate set for
326 * the node. We know the rate is there because the
327 * rate set is checked when the station associates.
328 */
193b341d
SZ
329 /* NB: the rate set is assumed sorted */
330 srate = ni->ni_rates.rs_nrates - 1;
572ff6f6 331 for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
193b341d 332 ;
193b341d 333 }
572ff6f6
MD
334 /*
335 * The selected rate may not be available due to races
336 * and mode settings. Also orphaned nodes created in
337 * adhoc mode may not have any rate set so this lookup
338 * can fail. This is not fatal.
339 */
340 ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
193b341d
SZ
341#undef RATE
342}
343
193b341d
SZ
344/*
345 * Examine and potentially adjust the transmit rate.
346 */
347static void
348ath_rate_ctl(void *arg, struct ieee80211_node *ni)
349{
350 struct ath_softc *sc = arg;
351 struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
572ff6f6 352 int rix;
193b341d
SZ
353
354#define is_success(amn) \
355(amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
356#define is_enough(amn) \
357(amn->amn_tx_try0_cnt > 10)
358#define is_failure(amn) \
359(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
193b341d 360
572ff6f6 361 rix = amn->amn_rix;
193b341d 362
572ff6f6
MD
363 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
364 "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
365 amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
366 amn->amn_tx_try3_cnt, amn->amn_success_threshold);
193b341d
SZ
367 if (is_success (amn) && is_enough (amn)) {
368 amn->amn_success++;
369 if (amn->amn_success == amn->amn_success_threshold &&
572ff6f6 370 rix + 1 < ni->ni_rates.rs_nrates) {
193b341d
SZ
371 amn->amn_recovery = 1;
372 amn->amn_success = 0;
572ff6f6
MD
373 rix++;
374 IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
375 "increase rate to %d", rix);
193b341d
SZ
376 } else {
377 amn->amn_recovery = 0;
378 }
379 } else if (is_failure (amn)) {
380 amn->amn_success = 0;
572ff6f6 381 if (rix > 0) {
193b341d
SZ
382 if (amn->amn_recovery) {
383 /* recovery failure. */
384 amn->amn_success_threshold *= 2;
385 amn->amn_success_threshold = min (amn->amn_success_threshold,
386 (u_int)ath_rate_max_success_threshold);
572ff6f6
MD
387 IEEE80211_NOTE(ni->ni_vap,
388 IEEE80211_MSG_RATECTL, ni,
389 "decrease rate recovery thr: %d",
390 amn->amn_success_threshold);
193b341d
SZ
391 } else {
392 /* simple failure. */
393 amn->amn_success_threshold = ath_rate_min_success_threshold;
572ff6f6
MD
394 IEEE80211_NOTE(ni->ni_vap,
395 IEEE80211_MSG_RATECTL, ni,
396 "decrease rate normal thr: %d",
397 amn->amn_success_threshold);
193b341d
SZ
398 }
399 amn->amn_recovery = 0;
572ff6f6 400 rix--;
193b341d
SZ
401 } else {
402 amn->amn_recovery = 0;
403 }
404
405 }
572ff6f6 406 if (is_enough (amn) || rix != amn->amn_rix) {
193b341d
SZ
407 /* reset counters. */
408 amn->amn_tx_try0_cnt = 0;
409 amn->amn_tx_try1_cnt = 0;
410 amn->amn_tx_try2_cnt = 0;
411 amn->amn_tx_try3_cnt = 0;
412 amn->amn_tx_failure_cnt = 0;
413 }
572ff6f6
MD
414 if (rix != amn->amn_rix) {
415 ath_rate_update(sc, ni, rix);
193b341d
SZ
416 }
417}
418
572ff6f6
MD
419static int
420ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
421 struct ath_rateioctl *re)
193b341d 422{
193b341d 423
572ff6f6 424 return (EINVAL);
193b341d
SZ
425}
426
427static void
428ath_rate_sysctlattach(struct ath_softc *sc)
429{
3133c5e3
MD
430 struct sysctl_ctx_list *ctx = &sc->sc_sysctl_ctx;
431 struct sysctl_oid *tree = sc->sc_sysctl_tree;
193b341d
SZ
432
433 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
434 "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
435 "rate control: operation interval (ms)");
436 /* XXX bounds check values */
437 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
438 "max_sucess_threshold", CTLFLAG_RW,
439 &ath_rate_max_success_threshold, 0, "");
440 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
441 "min_sucess_threshold", CTLFLAG_RW,
442 &ath_rate_min_success_threshold, 0, "");
443}
444
445struct ath_ratectrl *
446ath_rate_attach(struct ath_softc *sc)
447{
448 struct amrr_softc *asc;
449
3133c5e3 450 asc = kmalloc(sizeof(struct amrr_softc), M_DEVBUF, M_INTWAIT|M_ZERO);
193b341d
SZ
451 if (asc == NULL)
452 return NULL;
453 asc->arc.arc_space = sizeof(struct amrr_node);
193b341d
SZ
454 ath_rate_sysctlattach(sc);
455
456 return &asc->arc;
457}
458
459void
460ath_rate_detach(struct ath_ratectrl *arc)
461{
572ff6f6 462 struct amrr_softc *asc = (struct amrr_softc *) arc;
193b341d 463
3133c5e3
MD
464 kfree(asc, M_DEVBUF);
465}
466
467/*
468 * Module glue.
469 */
470static int
471amrr_modevent(module_t mod, int type, void *unused)
472{
473 int error;
474
475 wlan_serialize_enter();
476
477 switch (type) {
478 case MOD_LOAD:
479 if (bootverbose) {
480 kprintf("ath_rate: <AMRR rate control "
481 "algorithm> version 0.1\n");
482 }
483 error = 0;
484 break;
485 case MOD_UNLOAD:
486 error = 0;
487 break;
488 default:
489 error = EINVAL;
490 break;
491 }
492 wlan_serialize_exit();
493
494 return error;
193b341d 495}
3133c5e3
MD
496
497static moduledata_t amrr_mod = {
498 "ath_rate",
499 amrr_modevent,
500 0
501};
502
503DECLARE_MODULE(ath_rate, amrr_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
504MODULE_VERSION(ath_rate, 1);
505MODULE_DEPEND(ath_rate, ath_hal, 1, 1, 1);
506MODULE_DEPEND(ath_rate, wlan, 1, 1, 1);