amrr.c revision 237529
121308Sache/*-
221308Sache * Copyright (c) 2004 INRIA
321308Sache * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
421308Sache * All rights reserved.
558310Sache *
621308Sache * Redistribution and use in source and binary forms, with or without
721308Sache * modification, are permitted provided that the following conditions
821308Sache * are met:
921308Sache * 1. Redistributions of source code must retain the above copyright
1021308Sache *    notice, this list of conditions and the following disclaimer,
1121308Sache *    without modification.
1221308Sache * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1321308Sache *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1421308Sache *    redistribution must be conditioned upon including a substantially
1521308Sache *    similar Disclaimer requirement for further binary redistribution.
1621308Sache * 3. Neither the names of the above-listed copyright holders nor the names
1721308Sache *    of any contributors may be used to endorse or promote products derived
1821308Sache *    from this software without specific prior written permission.
1921308Sache *
2021308Sache * Alternatively, this software may be distributed under the terms of the
2121308Sache * GNU General Public License ("GPL") version 2 as published by the Free
2221308Sache * Software Foundation.
2321308Sache *
2421308Sache * NO WARRANTY
2521308Sache * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2621308Sache * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2721308Sache * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2821308Sache * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2921308Sache * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
3021308Sache * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3121308Sache * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3221308Sache * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
3321308Sache * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3421308Sache * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3521308Sache * THE POSSIBILITY OF SUCH DAMAGES.
3621308Sache *
3721308Sache */
3821308Sache
3921308Sache#include <sys/cdefs.h>
4021308Sache__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/amrr/amrr.c 237529 2012-06-24 08:47:19Z adrian $");
4121308Sache
4221308Sache/*
4321308Sache * AMRR rate control. See:
4421308Sache * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
4521308Sache * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
4621308Sache *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
4721308Sache */
4821308Sache#include "opt_ath.h"
4921308Sache#include "opt_inet.h"
5021308Sache#include "opt_wlan.h"
5121308Sache
5221308Sache#include <sys/param.h>
5321308Sache#include <sys/systm.h>
5421308Sache#include <sys/sysctl.h>
5521308Sache#include <sys/kernel.h>
5621308Sache#include <sys/lock.h>
5721308Sache#include <sys/mutex.h>
5821308Sache#include <sys/errno.h>
5921308Sache
6021308Sache#include <machine/bus.h>
6121308Sache#include <machine/resource.h>
6221308Sache#include <sys/bus.h>
6321308Sache
6421308Sache#include <sys/socket.h>
6521308Sache
6621308Sache#include <net/if.h>
6721308Sache#include <net/if_media.h>
6821308Sache#include <net/if_arp.h>
6921308Sache
7021308Sache#include <net80211/ieee80211_var.h>
7121308Sache
7221308Sache#include <net/bpf.h>
7321308Sache
7421308Sache#ifdef INET
7521308Sache#include <netinet/in.h>
7621308Sache#include <netinet/if_ether.h>
7721308Sache#endif
7821308Sache
7921308Sache#include <dev/ath/if_athvar.h>
8021308Sache#include <dev/ath/ath_rate/amrr/amrr.h>
8121308Sache#include <dev/ath/ath_hal/ah_desc.h>
8221308Sache
8321308Sachestatic	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
8421308Sachestatic	int ath_rate_max_success_threshold = 10;
8521308Sachestatic	int ath_rate_min_success_threshold = 1;
8621308Sache
8721308Sachestatic void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
8821308Sache			int rate);
8921308Sachestatic void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
9021308Sachestatic void	ath_rate_ctl(void *, struct ieee80211_node *);
9121308Sache
9221308Sachevoid
9321308Sacheath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
9421308Sache{
9521308Sache	/* NB: assumed to be zero'd by caller */
9621308Sache}
9721308Sache
9821308Sachevoid
9921308Sacheath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
10021308Sache{
10121308Sache}
10221308Sache
10321308Sachevoid
10421308Sacheath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
10521308Sache	int shortPreamble, size_t frameLen,
10621308Sache	u_int8_t *rix, int *try0, u_int8_t *txrate)
10721308Sache{
10821308Sache	struct amrr_node *amn = ATH_NODE_AMRR(an);
10921308Sache
11021308Sache	*rix = amn->amn_tx_rix0;
11121308Sache	*try0 = amn->amn_tx_try0;
11221308Sache	if (shortPreamble)
11321308Sache		*txrate = amn->amn_tx_rate0sp;
11421308Sache	else
11521308Sache		*txrate = amn->amn_tx_rate0;
11621308Sache}
11721308Sache
11821308Sache/*
11921308Sache * Get the TX rates.
12021308Sache *
12121308Sache * The short preamble bits aren't set here; the caller should augment
12221308Sache * the returned rate with the relevant preamble rate flag.
12321308Sache */
12421308Sachevoid
12521308Sacheath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
12621308Sache    uint8_t rix0, struct ath_rc_series *rc)
12721308Sache{
12821308Sache	struct amrr_node *amn = ATH_NODE_AMRR(an);
12921308Sache
13021308Sache	rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
13121308Sache
13221308Sache	rc[0].rix = amn->amn_tx_rate0;
13321308Sache	rc[1].rix = amn->amn_tx_rate1;
13421308Sache	rc[2].rix = amn->amn_tx_rate2;
13521308Sache	rc[3].rix = amn->amn_tx_rate3;
13621308Sache
13721308Sache	rc[0].tries = amn->amn_tx_try0;
13821308Sache	rc[1].tries = amn->amn_tx_try1;
13921308Sache	rc[2].tries = amn->amn_tx_try2;
14021308Sache	rc[3].tries = amn->amn_tx_try3;
14121308Sache}
14221308Sache
14321308Sache
14421308Sachevoid
14521308Sacheath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
14621308Sache	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
14721308Sache{
14821308Sache	struct amrr_node *amn = ATH_NODE_AMRR(an);
14921308Sache
15021308Sache	ath_hal_setupxtxdesc(sc->sc_ah, ds
15121308Sache		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
15221308Sache		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
15321308Sache		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
15421308Sache	);
15521308Sache}
15621308Sache
15721308Sachevoid
15821308Sacheath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
15921308Sache	const struct ath_rc_series *rc, const struct ath_tx_status *ts,
16021308Sache	int frame_size, int nframes, int nbad)
16121308Sache{
16221308Sache	struct amrr_node *amn = ATH_NODE_AMRR(an);
16321308Sache	int sr = ts->ts_shortretry;
16421308Sache	int lr = ts->ts_longretry;
16521308Sache	int retry_count = sr + lr;
16621308Sache
16721308Sache	amn->amn_tx_try0_cnt++;
16821308Sache	if (retry_count == 1) {
16921308Sache		amn->amn_tx_try1_cnt++;
17021308Sache	} else if (retry_count == 2) {
17121308Sache		amn->amn_tx_try1_cnt++;
17221308Sache		amn->amn_tx_try2_cnt++;
17321308Sache	} else if (retry_count == 3) {
17421308Sache		amn->amn_tx_try1_cnt++;
17521308Sache		amn->amn_tx_try2_cnt++;
17621308Sache		amn->amn_tx_try3_cnt++;
17721308Sache	} else if (retry_count > 3) {
17821308Sache		amn->amn_tx_try1_cnt++;
17921308Sache		amn->amn_tx_try2_cnt++;
18021308Sache		amn->amn_tx_try3_cnt++;
18121308Sache		amn->amn_tx_failure_cnt++;
18221308Sache	}
18321308Sache	if (amn->amn_interval != 0 &&
18421308Sache	    ticks - amn->amn_ticks > amn->amn_interval) {
18521308Sache		ath_rate_ctl(sc, &an->an_node);
18621308Sache		amn->amn_ticks = ticks;
18721308Sache	}
18821308Sache}
18921308Sache
19021308Sachevoid
19121308Sacheath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
19221308Sache{
19321308Sache	if (isnew)
19421308Sache		ath_rate_ctl_start(sc, &an->an_node);
19521308Sache}
19621308Sache
19721308Sachestatic void
19821308Sachenode_reset(struct amrr_node *amn)
19921308Sache{
20021308Sache	amn->amn_tx_try0_cnt = 0;
20121308Sache	amn->amn_tx_try1_cnt = 0;
20221308Sache	amn->amn_tx_try2_cnt = 0;
20321308Sache	amn->amn_tx_try3_cnt = 0;
20421308Sache	amn->amn_tx_failure_cnt = 0;
20521308Sache  	amn->amn_success = 0;
20621308Sache  	amn->amn_recovery = 0;
20721308Sache  	amn->amn_success_threshold = ath_rate_min_success_threshold;
20821308Sache}
20921308Sache
21021308Sache
21121308Sache/**
21221308Sache * The code below assumes that we are dealing with hardware multi rate retry
21321308Sache * I have no idea what will happen if you try to use this module with another
21421308Sache * type of hardware. Your machine might catch fire or it might work with
21521308Sache * horrible performance...
21621308Sache */
21721308Sachestatic void
21821308Sacheath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
21921308Sache{
22021308Sache	struct ath_node *an = ATH_NODE(ni);
22121308Sache	struct amrr_node *amn = ATH_NODE_AMRR(an);
22221308Sache	struct ieee80211vap *vap = ni->ni_vap;
22321308Sache	const HAL_RATE_TABLE *rt = sc->sc_currates;
22421308Sache	u_int8_t rix;
22521308Sache
22621308Sache	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
22721308Sache
22821308Sache	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
22921308Sache	    "%s: set xmit rate to %dM", __func__,
23021308Sache	    ni->ni_rates.rs_nrates > 0 ?
23121308Sache		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
23221308Sache
23321308Sache	amn->amn_rix = rate;
23421308Sache	/*
23521308Sache	 * Before associating a node has no rate set setup
23621308Sache	 * so we can't calculate any transmit codes to use.
23721308Sache	 * This is ok since we should never be sending anything
23821308Sache	 * but management frames and those always go at the
23921308Sache	 * lowest hardware rate.
24021308Sache	 */
24121308Sache	if (ni->ni_rates.rs_nrates > 0) {
24221308Sache		ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
24321308Sache		amn->amn_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
24421308Sache		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
24521308Sache		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
24621308Sache			rt->info[amn->amn_tx_rix0].shortPreamble;
24721308Sache		if (sc->sc_mrretry) {
24821308Sache			amn->amn_tx_try0 = 1;
24921308Sache			amn->amn_tx_try1 = 1;
25021308Sache			amn->amn_tx_try2 = 1;
25121308Sache			amn->amn_tx_try3 = 1;
25221308Sache			if (--rate >= 0) {
25321308Sache				rix = sc->sc_rixmap[
25421308Sache						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
25521308Sache				amn->amn_tx_rate1 = rt->info[rix].rateCode;
25621308Sache				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
25721308Sache					rt->info[rix].shortPreamble;
25821308Sache			} else {
25921308Sache				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
26021308Sache			}
26121308Sache			if (--rate >= 0) {
26221308Sache				rix = sc->sc_rixmap[
26321308Sache						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
26421308Sache				amn->amn_tx_rate2 = rt->info[rix].rateCode;
26521308Sache				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
26621308Sache					rt->info[rix].shortPreamble;
26721308Sache			} else {
26821308Sache				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
26921308Sache			}
27021308Sache			if (rate > 0) {
27121308Sache				/* NB: only do this if we didn't already do it above */
27221308Sache				amn->amn_tx_rate3 = rt->info[0].rateCode;
27321308Sache				amn->amn_tx_rate3sp =
27421308Sache					amn->amn_tx_rate3 | rt->info[0].shortPreamble;
27521308Sache			} else {
27621308Sache				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
27721308Sache			}
27821308Sache		} else {
27921308Sache			amn->amn_tx_try0 = ATH_TXMAXTRY;
28021308Sache			/* theorically, these statements are useless because
28121308Sache			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
28221308Sache			 */
28321308Sache			amn->amn_tx_try1 = 0;
28421308Sache			amn->amn_tx_try2 = 0;
28521308Sache			amn->amn_tx_try3 = 0;
28621308Sache			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
28721308Sache			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
28821308Sache			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
28921308Sache		}
29021308Sache	}
29121308Sache	node_reset(amn);
29221308Sache
29321308Sache	amn->amn_interval = ath_rateinterval;
29421308Sache	if (vap->iv_opmode == IEEE80211_M_STA)
29521308Sache		amn->amn_interval /= 2;
29621308Sache	amn->amn_interval = (amn->amn_interval * hz) / 1000;
29721308Sache}
29821308Sache
29921308Sache/*
30021308Sache * Set the starting transmit rate for a node.
30121308Sache */
30221308Sachestatic void
30321308Sacheath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
30421308Sache{
30521308Sache#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
30621308Sache	const struct ieee80211_txparam *tp = ni->ni_txparms;
30721308Sache	int srate;
30858310Sache
30921308Sache	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
31021308Sache	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
31121308Sache		/*
31221308Sache		 * No fixed rate is requested. For 11b start with
31321308Sache		 * the highest negotiated rate; otherwise, for 11g
31421308Sache		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
31521308Sache		 */
31621308Sache		srate = ni->ni_rates.rs_nrates - 1;
31721308Sache		if (sc->sc_curmode != IEEE80211_MODE_11B) {
31821308Sache			/*
31921308Sache			 * Scan the negotiated rate set to find the
32021308Sache			 * closest rate.
32121308Sache			 */
32221308Sache			/* NB: the rate set is assumed sorted */
32321308Sache			for (; srate >= 0 && RATE(srate) > 72; srate--)
32421308Sache				;
32521308Sache		}
32621308Sache	} else {
32721308Sache		/*
32821308Sache		 * A fixed rate is to be used; ic_fixed_rate is the
32921308Sache		 * IEEE code for this rate (sans basic bit).  Convert this
33021308Sache		 * to the index into the negotiated rate set for
33121308Sache		 * the node.  We know the rate is there because the
33221308Sache		 * rate set is checked when the station associates.
33321308Sache		 */
33421308Sache		/* NB: the rate set is assumed sorted */
33521308Sache		srate = ni->ni_rates.rs_nrates - 1;
33621308Sache		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
33721308Sache			;
33821308Sache	}
33921308Sache	/*
340	 * The selected rate may not be available due to races
341	 * and mode settings.  Also orphaned nodes created in
342	 * adhoc mode may not have any rate set so this lookup
343	 * can fail.  This is not fatal.
344	 */
345	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
346#undef RATE
347}
348
349/*
350 * Examine and potentially adjust the transmit rate.
351 */
352static void
353ath_rate_ctl(void *arg, struct ieee80211_node *ni)
354{
355	struct ath_softc *sc = arg;
356	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
357	int rix;
358
359#define is_success(amn) \
360(amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
361#define is_enough(amn) \
362(amn->amn_tx_try0_cnt > 10)
363#define is_failure(amn) \
364(amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
365
366	rix = amn->amn_rix;
367
368  	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
369	    "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
370	    amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
371	    amn->amn_tx_try3_cnt, amn->amn_success_threshold);
372  	if (is_success (amn) && is_enough (amn)) {
373		amn->amn_success++;
374		if (amn->amn_success == amn->amn_success_threshold &&
375		    rix + 1 < ni->ni_rates.rs_nrates) {
376  			amn->amn_recovery = 1;
377  			amn->amn_success = 0;
378  			rix++;
379			IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
380			    "increase rate to %d", rix);
381  		} else {
382			amn->amn_recovery = 0;
383		}
384  	} else if (is_failure (amn)) {
385  		amn->amn_success = 0;
386		if (rix > 0) {
387  			if (amn->amn_recovery) {
388  				/* recovery failure. */
389  				amn->amn_success_threshold *= 2;
390  				amn->amn_success_threshold = min (amn->amn_success_threshold,
391								  (u_int)ath_rate_max_success_threshold);
392				IEEE80211_NOTE(ni->ni_vap,
393				    IEEE80211_MSG_RATECTL, ni,
394				    "decrease rate recovery thr: %d",
395				    amn->amn_success_threshold);
396  			} else {
397  				/* simple failure. */
398 				amn->amn_success_threshold = ath_rate_min_success_threshold;
399				IEEE80211_NOTE(ni->ni_vap,
400				    IEEE80211_MSG_RATECTL, ni,
401				    "decrease rate normal thr: %d",
402				    amn->amn_success_threshold);
403  			}
404			amn->amn_recovery = 0;
405  			rix--;
406   		} else {
407			amn->amn_recovery = 0;
408		}
409
410   	}
411	if (is_enough (amn) || rix != amn->amn_rix) {
412		/* reset counters. */
413		amn->amn_tx_try0_cnt = 0;
414		amn->amn_tx_try1_cnt = 0;
415		amn->amn_tx_try2_cnt = 0;
416		amn->amn_tx_try3_cnt = 0;
417		amn->amn_tx_failure_cnt = 0;
418	}
419	if (rix != amn->amn_rix) {
420		ath_rate_update(sc, ni, rix);
421	}
422}
423
424static void
425ath_rate_sysctlattach(struct ath_softc *sc)
426{
427	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
428	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
429
430	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
431		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
432		"rate control: operation interval (ms)");
433	/* XXX bounds check values */
434	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
435		"max_sucess_threshold", CTLFLAG_RW,
436		&ath_rate_max_success_threshold, 0, "");
437	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
438		"min_sucess_threshold", CTLFLAG_RW,
439		&ath_rate_min_success_threshold, 0, "");
440}
441
442struct ath_ratectrl *
443ath_rate_attach(struct ath_softc *sc)
444{
445	struct amrr_softc *asc;
446
447	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
448	if (asc == NULL)
449		return NULL;
450	asc->arc.arc_space = sizeof(struct amrr_node);
451	ath_rate_sysctlattach(sc);
452
453	return &asc->arc;
454}
455
456void
457ath_rate_detach(struct ath_ratectrl *arc)
458{
459	struct amrr_softc *asc = (struct amrr_softc *) arc;
460
461	free(asc, M_DEVBUF);
462}
463