Home | History | Annotate | Line # | Download | only in ic
athrate-amrr.c revision 1.5.4.1
      1  1.5.4.1  rpaulo /*	$NetBSD: athrate-amrr.c,v 1.5.4.1 2006/09/09 02:50:01 rpaulo Exp $ */
      2  1.5.4.1  rpaulo 
      3      1.1  dyoung /*-
      4      1.1  dyoung  * Copyright (c) 2004 INRIA
      5      1.1  dyoung  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      6      1.1  dyoung  * All rights reserved.
      7      1.1  dyoung  *
      8      1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      9      1.1  dyoung  * modification, are permitted provided that the following conditions
     10      1.1  dyoung  * are met:
     11      1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     12      1.1  dyoung  *    notice, this list of conditions and the following disclaimer,
     13      1.1  dyoung  *    without modification.
     14      1.1  dyoung  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     15      1.1  dyoung  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     16      1.1  dyoung  *    redistribution must be conditioned upon including a substantially
     17      1.1  dyoung  *    similar Disclaimer requirement for further binary redistribution.
     18      1.1  dyoung  * 3. Neither the names of the above-listed copyright holders nor the names
     19      1.1  dyoung  *    of any contributors may be used to endorse or promote products derived
     20      1.1  dyoung  *    from this software without specific prior written permission.
     21      1.1  dyoung  *
     22      1.1  dyoung  * Alternatively, this software may be distributed under the terms of the
     23      1.1  dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     24      1.1  dyoung  * Software Foundation.
     25      1.1  dyoung  *
     26      1.1  dyoung  * NO WARRANTY
     27      1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     28      1.1  dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     29      1.1  dyoung  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     30      1.1  dyoung  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     31      1.1  dyoung  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     32      1.1  dyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33      1.1  dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34      1.1  dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     35      1.1  dyoung  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36      1.1  dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     37      1.1  dyoung  * THE POSSIBILITY OF SUCH DAMAGES.
     38      1.1  dyoung  *
     39      1.1  dyoung  */
     40      1.1  dyoung 
     41      1.1  dyoung #include <sys/cdefs.h>
     42  1.5.4.1  rpaulo #ifdef __FreeBSD__
     43      1.5   skrll __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/amrr/amrr.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
     44  1.5.4.1  rpaulo #endif
     45  1.5.4.1  rpaulo #ifdef __NetBSD__
     46  1.5.4.1  rpaulo __KERNEL_RCSID(0, "$NetBSD: athrate-amrr.c,v 1.5.4.1 2006/09/09 02:50:01 rpaulo Exp $");
     47  1.5.4.1  rpaulo #endif
     48      1.1  dyoung 
     49      1.1  dyoung /*
     50      1.1  dyoung  * AMRR rate control. See:
     51      1.1  dyoung  * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
     52      1.1  dyoung  * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
     53      1.1  dyoung  *    Mathieu Lacage, Hossein Manshaei, Thierry Turletti
     54      1.1  dyoung  */
     55      1.1  dyoung #include "opt_inet.h"
     56      1.1  dyoung 
     57      1.1  dyoung #include <sys/param.h>
     58      1.1  dyoung #include <sys/systm.h>
     59      1.1  dyoung #include <sys/sysctl.h>
     60      1.1  dyoung #include <sys/kernel.h>
     61      1.1  dyoung #include <sys/lock.h>
     62      1.1  dyoung #include <sys/errno.h>
     63      1.1  dyoung 
     64      1.1  dyoung #include <machine/bus.h>
     65      1.1  dyoung 
     66      1.1  dyoung #include <sys/socket.h>
     67      1.1  dyoung 
     68      1.1  dyoung #include <net/if.h>
     69      1.1  dyoung #include <net/if_media.h>
     70      1.1  dyoung #include <net/if_arp.h>
     71      1.2  dyoung #include <net/if_ether.h>		/* XXX for ether_sprintf */
     72      1.1  dyoung 
     73      1.1  dyoung #include <net80211/ieee80211_var.h>
     74      1.1  dyoung 
     75      1.1  dyoung #include <net/bpf.h>
     76      1.1  dyoung 
     77      1.1  dyoung #ifdef INET
     78      1.1  dyoung #include <netinet/in.h>
     79      1.1  dyoung #endif
     80      1.1  dyoung 
     81      1.2  dyoung #include <dev/ic/athvar.h>
     82      1.2  dyoung #include <dev/ic/athrate-amrr.h>
     83  1.5.4.1  rpaulo #include <contrib/dev/ath/ah_desc.h>
     84      1.1  dyoung 
     85      1.1  dyoung #define	AMRR_DEBUG
     86      1.1  dyoung #ifdef AMRR_DEBUG
     87      1.1  dyoung #define	DPRINTF(sc, _fmt, ...) do {					\
     88      1.1  dyoung 	if (sc->sc_debug & 0x10)					\
     89      1.1  dyoung 		printf(_fmt, __VA_ARGS__);				\
     90      1.1  dyoung } while (0)
     91      1.1  dyoung #else
     92      1.1  dyoung #define	DPRINTF(sc, _fmt, ...)
     93      1.1  dyoung #endif
     94      1.1  dyoung 
     95      1.1  dyoung static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
     96      1.1  dyoung static	int ath_rate_max_success_threshold = 10;
     97      1.1  dyoung static	int ath_rate_min_success_threshold = 1;
     98      1.1  dyoung 
     99      1.1  dyoung static void	ath_ratectl(void *);
    100      1.1  dyoung static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
    101      1.1  dyoung 			int rate);
    102      1.1  dyoung static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
    103      1.1  dyoung static void	ath_rate_ctl(void *, struct ieee80211_node *);
    104      1.1  dyoung 
    105      1.1  dyoung void
    106      1.1  dyoung ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
    107      1.1  dyoung {
    108      1.1  dyoung 	/* NB: assumed to be zero'd by caller */
    109      1.1  dyoung 	ath_rate_update(sc, &an->an_node, 0);
    110      1.1  dyoung }
    111      1.1  dyoung 
    112      1.1  dyoung void
    113      1.1  dyoung ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
    114      1.1  dyoung {
    115      1.1  dyoung }
    116      1.1  dyoung 
    117      1.1  dyoung void
    118      1.1  dyoung ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
    119      1.1  dyoung 	int shortPreamble, size_t frameLen,
    120      1.1  dyoung 	u_int8_t *rix, int *try0, u_int8_t *txrate)
    121      1.1  dyoung {
    122      1.1  dyoung 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    123      1.1  dyoung 
    124      1.1  dyoung 	*rix = amn->amn_tx_rix0;
    125      1.1  dyoung 	*try0 = amn->amn_tx_try0;
    126      1.1  dyoung 	if (shortPreamble)
    127      1.1  dyoung 		*txrate = amn->amn_tx_rate0sp;
    128      1.1  dyoung 	else
    129      1.1  dyoung 		*txrate = amn->amn_tx_rate0;
    130      1.1  dyoung }
    131      1.1  dyoung 
    132      1.1  dyoung void
    133      1.1  dyoung ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
    134      1.1  dyoung 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
    135      1.1  dyoung {
    136      1.1  dyoung 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    137      1.1  dyoung 
    138      1.1  dyoung 	ath_hal_setupxtxdesc(sc->sc_ah, ds
    139      1.1  dyoung 		, amn->amn_tx_rate1sp, amn->amn_tx_try1	/* series 1 */
    140      1.1  dyoung 		, amn->amn_tx_rate2sp, amn->amn_tx_try2	/* series 2 */
    141      1.1  dyoung 		, amn->amn_tx_rate3sp, amn->amn_tx_try3	/* series 3 */
    142      1.1  dyoung 	);
    143      1.1  dyoung }
    144      1.1  dyoung 
    145      1.1  dyoung void
    146      1.1  dyoung ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
    147      1.1  dyoung 	const struct ath_desc *ds, const struct ath_desc *ds0)
    148      1.1  dyoung {
    149      1.1  dyoung 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    150      1.1  dyoung 	int sr = ds->ds_txstat.ts_shortretry;
    151      1.1  dyoung 	int lr = ds->ds_txstat.ts_longretry;
    152      1.1  dyoung 	int retry_count = sr + lr;
    153      1.1  dyoung 
    154      1.1  dyoung 	amn->amn_tx_try0_cnt++;
    155      1.1  dyoung 	if (retry_count == 1) {
    156      1.1  dyoung 		amn->amn_tx_try1_cnt++;
    157      1.1  dyoung 	} else if (retry_count == 2) {
    158      1.1  dyoung 		amn->amn_tx_try1_cnt++;
    159      1.1  dyoung 		amn->amn_tx_try2_cnt++;
    160      1.1  dyoung 	} else if (retry_count == 3) {
    161      1.1  dyoung 		amn->amn_tx_try1_cnt++;
    162      1.1  dyoung 		amn->amn_tx_try2_cnt++;
    163      1.1  dyoung 		amn->amn_tx_try3_cnt++;
    164      1.1  dyoung 	} else if (retry_count > 3) {
    165      1.1  dyoung 		amn->amn_tx_try1_cnt++;
    166      1.1  dyoung 		amn->amn_tx_try2_cnt++;
    167      1.1  dyoung 		amn->amn_tx_try3_cnt++;
    168      1.1  dyoung 		amn->amn_tx_failure_cnt++;
    169      1.1  dyoung 	}
    170      1.1  dyoung }
    171      1.1  dyoung 
    172      1.1  dyoung void
    173      1.1  dyoung ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
    174      1.1  dyoung {
    175      1.1  dyoung 	if (isnew)
    176      1.1  dyoung 		ath_rate_ctl_start(sc, &an->an_node);
    177      1.1  dyoung }
    178      1.1  dyoung 
    179      1.1  dyoung static void
    180      1.1  dyoung node_reset (struct amrr_node *amn)
    181      1.1  dyoung {
    182      1.1  dyoung 	amn->amn_tx_try0_cnt = 0;
    183      1.1  dyoung 	amn->amn_tx_try1_cnt = 0;
    184      1.1  dyoung 	amn->amn_tx_try2_cnt = 0;
    185      1.1  dyoung 	amn->amn_tx_try3_cnt = 0;
    186      1.1  dyoung 	amn->amn_tx_failure_cnt = 0;
    187      1.1  dyoung   	amn->amn_success = 0;
    188      1.1  dyoung   	amn->amn_recovery = 0;
    189      1.1  dyoung   	amn->amn_success_threshold = ath_rate_min_success_threshold;
    190      1.1  dyoung }
    191      1.1  dyoung 
    192      1.1  dyoung 
    193      1.1  dyoung /**
    194      1.1  dyoung  * The code below assumes that we are dealing with hardware multi rate retry
    195      1.1  dyoung  * I have no idea what will happen if you try to use this module with another
    196      1.1  dyoung  * type of hardware. Your machine might catch fire or it might work with
    197      1.1  dyoung  * horrible performance...
    198      1.1  dyoung  */
    199      1.1  dyoung static void
    200      1.1  dyoung ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
    201      1.1  dyoung {
    202      1.1  dyoung 	struct ath_node *an = ATH_NODE(ni);
    203      1.1  dyoung 	struct amrr_node *amn = ATH_NODE_AMRR(an);
    204      1.1  dyoung 	const HAL_RATE_TABLE *rt = sc->sc_currates;
    205      1.1  dyoung 	u_int8_t rix;
    206      1.1  dyoung 
    207      1.1  dyoung 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
    208      1.1  dyoung 
    209      1.1  dyoung 	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
    210      1.1  dyoung 	    __func__, ether_sprintf(ni->ni_macaddr),
    211      1.1  dyoung 	    ni->ni_rates.rs_nrates > 0 ?
    212      1.1  dyoung 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
    213      1.1  dyoung 
    214      1.1  dyoung 	ni->ni_txrate = rate;
    215      1.1  dyoung 	/*
    216      1.1  dyoung 	 * Before associating a node has no rate set setup
    217      1.1  dyoung 	 * so we can't calculate any transmit codes to use.
    218      1.1  dyoung 	 * This is ok since we should never be sending anything
    219      1.1  dyoung 	 * but management frames and those always go at the
    220      1.1  dyoung 	 * lowest hardware rate.
    221      1.1  dyoung 	 */
    222      1.1  dyoung 	if (ni->ni_rates.rs_nrates > 0) {
    223      1.1  dyoung 		amn->amn_tx_rix0 = sc->sc_rixmap[
    224      1.1  dyoung 					       ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
    225      1.1  dyoung 		amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
    226      1.1  dyoung 		amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
    227      1.1  dyoung 			rt->info[amn->amn_tx_rix0].shortPreamble;
    228      1.1  dyoung 		if (sc->sc_mrretry) {
    229      1.1  dyoung 			amn->amn_tx_try0 = 1;
    230      1.1  dyoung 			amn->amn_tx_try1 = 1;
    231      1.1  dyoung 			amn->amn_tx_try2 = 1;
    232      1.1  dyoung 			amn->amn_tx_try3 = 1;
    233      1.1  dyoung 			if (--rate >= 0) {
    234      1.1  dyoung 				rix = sc->sc_rixmap[
    235      1.1  dyoung 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    236      1.1  dyoung 				amn->amn_tx_rate1 = rt->info[rix].rateCode;
    237      1.1  dyoung 				amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
    238      1.1  dyoung 					rt->info[rix].shortPreamble;
    239      1.1  dyoung 			} else {
    240      1.1  dyoung 				amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
    241      1.1  dyoung 			}
    242      1.1  dyoung 			if (--rate >= 0) {
    243      1.1  dyoung 				rix = sc->sc_rixmap[
    244      1.1  dyoung 						    ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    245      1.1  dyoung 				amn->amn_tx_rate2 = rt->info[rix].rateCode;
    246      1.1  dyoung 				amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
    247      1.1  dyoung 					rt->info[rix].shortPreamble;
    248      1.1  dyoung 			} else {
    249      1.1  dyoung 				amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
    250      1.1  dyoung 			}
    251      1.1  dyoung 			if (rate > 0) {
    252      1.1  dyoung 				/* NB: only do this if we didn't already do it above */
    253      1.1  dyoung 				amn->amn_tx_rate3 = rt->info[0].rateCode;
    254      1.1  dyoung 				amn->amn_tx_rate3sp =
    255  1.5.4.1  rpaulo 					an->an_tx_rate3 | rt->info[0].shortPreamble;
    256      1.1  dyoung 			} else {
    257      1.1  dyoung 				amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
    258      1.1  dyoung 			}
    259      1.1  dyoung 		} else {
    260      1.1  dyoung 			amn->amn_tx_try0 = ATH_TXMAXTRY;
    261      1.1  dyoung 			/* theorically, these statements are useless because
    262      1.1  dyoung 			 *  the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
    263      1.1  dyoung 			 */
    264      1.1  dyoung 			amn->amn_tx_try1 = 0;
    265      1.1  dyoung 			amn->amn_tx_try2 = 0;
    266      1.1  dyoung 			amn->amn_tx_try3 = 0;
    267      1.1  dyoung 			amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
    268      1.1  dyoung 			amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
    269      1.1  dyoung 			amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
    270      1.1  dyoung 		}
    271      1.1  dyoung 	}
    272      1.1  dyoung 	node_reset (amn);
    273      1.1  dyoung }
    274      1.1  dyoung 
    275      1.1  dyoung /*
    276      1.1  dyoung  * Set the starting transmit rate for a node.
    277      1.1  dyoung  */
    278      1.1  dyoung static void
    279      1.1  dyoung ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
    280      1.1  dyoung {
    281      1.1  dyoung #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
    282      1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    283      1.1  dyoung 	int srate;
    284      1.1  dyoung 
    285      1.1  dyoung 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
    286      1.5   skrll 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
    287      1.1  dyoung 		/*
    288      1.1  dyoung 		 * No fixed rate is requested. For 11b start with
    289      1.1  dyoung 		 * the highest negotiated rate; otherwise, for 11g
    290      1.1  dyoung 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
    291      1.1  dyoung 		 */
    292      1.1  dyoung 		srate = ni->ni_rates.rs_nrates - 1;
    293      1.1  dyoung 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
    294      1.1  dyoung 			/*
    295      1.1  dyoung 			 * Scan the negotiated rate set to find the
    296      1.1  dyoung 			 * closest rate.
    297      1.1  dyoung 			 */
    298      1.1  dyoung 			/* NB: the rate set is assumed sorted */
    299      1.1  dyoung 			for (; srate >= 0 && RATE(srate) > 72; srate--)
    300      1.1  dyoung 				;
    301      1.1  dyoung 			KASSERT(srate >= 0, ("bogus rate set"));
    302      1.1  dyoung 		}
    303      1.1  dyoung 	} else {
    304      1.1  dyoung 		/*
    305      1.1  dyoung 		 * A fixed rate is to be used; ic_fixed_rate is an
    306      1.1  dyoung 		 * index into the supported rate set.  Convert this
    307      1.1  dyoung 		 * to the index into the negotiated rate set for
    308      1.1  dyoung 		 * the node.  We know the rate is there because the
    309      1.1  dyoung 		 * rate set is checked when the station associates.
    310      1.1  dyoung 		 */
    311      1.1  dyoung 		const struct ieee80211_rateset *rs =
    312      1.1  dyoung 			&ic->ic_sup_rates[ic->ic_curmode];
    313      1.1  dyoung 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    314      1.1  dyoung 		/* NB: the rate set is assumed sorted */
    315      1.1  dyoung 		srate = ni->ni_rates.rs_nrates - 1;
    316      1.1  dyoung 		for (; srate >= 0 && RATE(srate) != r; srate--)
    317      1.1  dyoung 			;
    318      1.1  dyoung 		KASSERT(srate >= 0,
    319      1.1  dyoung 			("fixed rate %d not in rate set", ic->ic_fixed_rate));
    320      1.1  dyoung 	}
    321      1.1  dyoung 	ath_rate_update(sc, ni, srate);
    322      1.1  dyoung #undef RATE
    323      1.1  dyoung }
    324      1.1  dyoung 
    325      1.1  dyoung static void
    326      1.1  dyoung ath_rate_cb(void *arg, struct ieee80211_node *ni)
    327      1.1  dyoung {
    328      1.1  dyoung 	struct ath_softc *sc = arg;
    329      1.1  dyoung 
    330      1.1  dyoung 	ath_rate_update(sc, ni, 0);
    331      1.1  dyoung }
    332      1.1  dyoung 
    333      1.1  dyoung /*
    334      1.1  dyoung  * Reset the rate control state for each 802.11 state transition.
    335      1.1  dyoung  */
    336      1.1  dyoung void
    337      1.1  dyoung ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
    338      1.1  dyoung {
    339      1.1  dyoung 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
    340      1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    341      1.1  dyoung 	struct ieee80211_node *ni;
    342      1.1  dyoung 
    343      1.1  dyoung 	if (state == IEEE80211_S_INIT) {
    344      1.1  dyoung 		callout_stop(&asc->timer);
    345      1.1  dyoung 		return;
    346      1.1  dyoung 	}
    347      1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA) {
    348      1.1  dyoung 		/*
    349      1.1  dyoung 		 * Reset local xmit state; this is really only
    350      1.1  dyoung 		 * meaningful when operating in station mode.
    351      1.1  dyoung 		 */
    352      1.1  dyoung 		ni = ic->ic_bss;
    353      1.1  dyoung 		if (state == IEEE80211_S_RUN) {
    354      1.1  dyoung 			ath_rate_ctl_start(sc, ni);
    355      1.1  dyoung 		} else {
    356      1.1  dyoung 			ath_rate_update(sc, ni, 0);
    357      1.1  dyoung 		}
    358      1.1  dyoung 	} else {
    359      1.1  dyoung 		/*
    360      1.1  dyoung 		 * When operating as a station the node table holds
    361      1.1  dyoung 		 * the AP's that were discovered during scanning.
    362      1.1  dyoung 		 * For any other operating mode we want to reset the
    363      1.1  dyoung 		 * tx rate state of each node.
    364      1.1  dyoung 		 */
    365      1.1  dyoung 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
    366      1.1  dyoung 		ath_rate_update(sc, ic->ic_bss, 0);
    367      1.1  dyoung 	}
    368      1.5   skrll 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
    369      1.5   skrll 	    state == IEEE80211_S_RUN) {
    370      1.1  dyoung 		int interval;
    371      1.1  dyoung 		/*
    372      1.1  dyoung 		 * Start the background rate control thread if we
    373      1.1  dyoung 		 * are not configured to use a fixed xmit rate.
    374      1.1  dyoung 		 */
    375      1.1  dyoung 		interval = ath_rateinterval;
    376      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    377      1.1  dyoung 			interval /= 2;
    378      1.1  dyoung 		callout_reset(&asc->timer, (interval * hz) / 1000,
    379      1.1  dyoung 			ath_ratectl, &sc->sc_if);
    380      1.1  dyoung 	}
    381      1.1  dyoung }
    382      1.1  dyoung 
    383      1.1  dyoung /*
    384      1.1  dyoung  * Examine and potentially adjust the transmit rate.
    385      1.1  dyoung  */
    386      1.1  dyoung static void
    387      1.1  dyoung ath_rate_ctl(void *arg, struct ieee80211_node *ni)
    388      1.1  dyoung {
    389      1.1  dyoung 	struct ath_softc *sc = arg;
    390      1.1  dyoung 	struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
    391      1.1  dyoung 	int old_rate;
    392      1.1  dyoung 
    393      1.1  dyoung #define is_success(amn) \
    394      1.1  dyoung (amn->amn_tx_try1_cnt  < (amn->amn_tx_try0_cnt/10))
    395      1.1  dyoung #define is_enough(amn) \
    396      1.1  dyoung (amn->amn_tx_try0_cnt > 10)
    397      1.1  dyoung #define is_failure(amn) \
    398      1.1  dyoung (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
    399      1.1  dyoung #define is_max_rate(ni) \
    400      1.1  dyoung ((ni->ni_txrate + 1) >= ni->ni_rates.rs_nrates)
    401      1.1  dyoung #define is_min_rate(ni) \
    402      1.1  dyoung (ni->ni_txrate == 0)
    403      1.1  dyoung 
    404      1.1  dyoung 	old_rate = ni->ni_txrate;
    405      1.1  dyoung 
    406      1.1  dyoung   	DPRINTF (sc, "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d\n",
    407      1.1  dyoung 		 amn->amn_tx_try0_cnt,
    408      1.1  dyoung 		 amn->amn_tx_try1_cnt,
    409      1.1  dyoung 		 amn->amn_tx_try2_cnt,
    410      1.1  dyoung 		 amn->amn_tx_try3_cnt,
    411      1.1  dyoung 		 amn->amn_success_threshold);
    412      1.1  dyoung   	if (is_success (amn) && is_enough (amn)) {
    413      1.1  dyoung 		amn->amn_success++;
    414      1.1  dyoung 		if (amn->amn_success == amn->amn_success_threshold &&
    415      1.1  dyoung   		    !is_max_rate (ni)) {
    416      1.1  dyoung   			amn->amn_recovery = 1;
    417      1.1  dyoung   			amn->amn_success = 0;
    418      1.1  dyoung   			ni->ni_txrate++;
    419      1.1  dyoung 			DPRINTF (sc, "increase rate to %d\n", ni->ni_txrate);
    420      1.1  dyoung   		} else {
    421      1.1  dyoung 			amn->amn_recovery = 0;
    422      1.1  dyoung 		}
    423      1.1  dyoung   	} else if (is_failure (amn)) {
    424      1.1  dyoung   		amn->amn_success = 0;
    425      1.1  dyoung   		if (!is_min_rate (ni)) {
    426      1.1  dyoung   			if (amn->amn_recovery) {
    427      1.1  dyoung   				/* recovery failure. */
    428      1.1  dyoung   				amn->amn_success_threshold *= 2;
    429      1.1  dyoung   				amn->amn_success_threshold = min (amn->amn_success_threshold,
    430      1.1  dyoung 								  (u_int)ath_rate_max_success_threshold);
    431      1.1  dyoung  				DPRINTF (sc, "decrease rate recovery thr: %d\n", amn->amn_success_threshold);
    432      1.1  dyoung   			} else {
    433      1.1  dyoung   				/* simple failure. */
    434      1.1  dyoung  				amn->amn_success_threshold = ath_rate_min_success_threshold;
    435      1.1  dyoung  				DPRINTF (sc, "decrease rate normal thr: %d\n", amn->amn_success_threshold);
    436      1.1  dyoung   			}
    437      1.1  dyoung 			amn->amn_recovery = 0;
    438      1.1  dyoung   			ni->ni_txrate--;
    439      1.1  dyoung    		} else {
    440      1.1  dyoung 			amn->amn_recovery = 0;
    441      1.1  dyoung 		}
    442      1.1  dyoung 
    443      1.1  dyoung    	}
    444      1.1  dyoung 	if (is_enough (amn) || old_rate != ni->ni_txrate) {
    445      1.1  dyoung 		/* reset counters. */
    446      1.1  dyoung 		amn->amn_tx_try0_cnt = 0;
    447      1.1  dyoung 		amn->amn_tx_try1_cnt = 0;
    448      1.1  dyoung 		amn->amn_tx_try2_cnt = 0;
    449      1.1  dyoung 		amn->amn_tx_try3_cnt = 0;
    450      1.1  dyoung 		amn->amn_tx_failure_cnt = 0;
    451      1.1  dyoung 	}
    452      1.1  dyoung 	if (old_rate != ni->ni_txrate) {
    453      1.1  dyoung 		ath_rate_update(sc, ni, ni->ni_txrate);
    454      1.1  dyoung 	}
    455      1.1  dyoung }
    456      1.1  dyoung 
    457      1.1  dyoung static void
    458      1.1  dyoung ath_ratectl(void *arg)
    459      1.1  dyoung {
    460      1.1  dyoung 	struct ifnet *ifp = arg;
    461      1.1  dyoung 	struct ath_softc *sc = ifp->if_softc;
    462      1.1  dyoung 	struct amrr_softc *asc = (struct amrr_softc *) sc->sc_rc;
    463      1.1  dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    464      1.1  dyoung 	int interval;
    465      1.1  dyoung 
    466      1.5   skrll 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
    467      1.1  dyoung 		sc->sc_stats.ast_rate_calls++;
    468      1.1  dyoung 
    469      1.1  dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    470      1.1  dyoung 			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
    471      1.1  dyoung 		else
    472      1.1  dyoung 			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
    473      1.1  dyoung 	}
    474      1.1  dyoung 	interval = ath_rateinterval;
    475      1.1  dyoung 	if (ic->ic_opmode == IEEE80211_M_STA)
    476      1.1  dyoung 		interval /= 2;
    477      1.1  dyoung 	callout_reset(&asc->timer, (interval * hz) / 1000,
    478      1.1  dyoung 		ath_ratectl, &sc->sc_if);
    479      1.1  dyoung }
    480      1.1  dyoung 
    481      1.1  dyoung static void
    482      1.1  dyoung ath_rate_sysctlattach(struct ath_softc *sc)
    483      1.1  dyoung {
    484      1.1  dyoung 	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
    485      1.1  dyoung 	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
    486      1.1  dyoung 
    487      1.1  dyoung 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    488      1.1  dyoung 		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
    489      1.1  dyoung 		"rate control: operation interval (ms)");
    490      1.1  dyoung 	/* XXX bounds check values */
    491      1.1  dyoung 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    492      1.1  dyoung 		"max_sucess_threshold", CTLFLAG_RW,
    493      1.1  dyoung 		&ath_rate_max_success_threshold, 0, "");
    494      1.1  dyoung 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
    495      1.1  dyoung 		"min_sucess_threshold", CTLFLAG_RW,
    496      1.1  dyoung 		&ath_rate_min_success_threshold, 0, "");
    497      1.1  dyoung }
    498      1.1  dyoung 
    499      1.1  dyoung struct ath_ratectrl *
    500      1.1  dyoung ath_rate_attach(struct ath_softc *sc)
    501      1.1  dyoung {
    502      1.1  dyoung 	struct amrr_softc *asc;
    503      1.1  dyoung 
    504      1.1  dyoung 	asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
    505      1.1  dyoung 	if (asc == NULL)
    506      1.1  dyoung 		return NULL;
    507      1.1  dyoung 	asc->arc.arc_space = sizeof(struct amrr_node);
    508      1.1  dyoung 	callout_init(&asc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
    509      1.1  dyoung 	ath_rate_sysctlattach(sc);
    510      1.1  dyoung 
    511      1.1  dyoung 	return &asc->arc;
    512      1.1  dyoung }
    513      1.1  dyoung 
    514      1.1  dyoung void
    515      1.1  dyoung ath_rate_detach(struct ath_ratectrl *arc)
    516      1.1  dyoung {
    517      1.1  dyoung 	struct amrr_softc *asc = (struct amrr_softc *) arc;
    518      1.1  dyoung 
    519      1.1  dyoung 	callout_drain(&asc->timer);
    520      1.1  dyoung 	free(asc, M_DEVBUF);
    521      1.1  dyoung }
    522