Home | History | Annotate | Line # | Download | only in ic
athrate-onoe.c revision 1.8
      1  1.8   dyoung /*	$NetBSD: athrate-onoe.c,v 1.8 2006/03/02 03:38:45 dyoung Exp $ */
      2  1.7  xtraeme 
      3  1.1   dyoung /*-
      4  1.1   dyoung  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5  1.1   dyoung  * All rights reserved.
      6  1.1   dyoung  *
      7  1.1   dyoung  * Redistribution and use in source and binary forms, with or without
      8  1.1   dyoung  * modification, are permitted provided that the following conditions
      9  1.1   dyoung  * are met:
     10  1.1   dyoung  * 1. Redistributions of source code must retain the above copyright
     11  1.1   dyoung  *    notice, this list of conditions and the following disclaimer,
     12  1.1   dyoung  *    without modification.
     13  1.1   dyoung  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     14  1.1   dyoung  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
     15  1.1   dyoung  *    redistribution must be conditioned upon including a substantially
     16  1.1   dyoung  *    similar Disclaimer requirement for further binary redistribution.
     17  1.1   dyoung  * 3. Neither the names of the above-listed copyright holders nor the names
     18  1.1   dyoung  *    of any contributors may be used to endorse or promote products derived
     19  1.1   dyoung  *    from this software without specific prior written permission.
     20  1.1   dyoung  *
     21  1.1   dyoung  * Alternatively, this software may be distributed under the terms of the
     22  1.1   dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     23  1.1   dyoung  * Software Foundation.
     24  1.1   dyoung  *
     25  1.1   dyoung  * NO WARRANTY
     26  1.1   dyoung  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     27  1.1   dyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     28  1.1   dyoung  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
     29  1.1   dyoung  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
     30  1.1   dyoung  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
     31  1.1   dyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1   dyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1   dyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     34  1.1   dyoung  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1   dyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     36  1.1   dyoung  * THE POSSIBILITY OF SUCH DAMAGES.
     37  1.1   dyoung  */
     38  1.1   dyoung 
     39  1.1   dyoung #include <sys/cdefs.h>
     40  1.2   dyoung #ifdef __FreeBSD__
     41  1.6    skrll __FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
     42  1.2   dyoung #endif
     43  1.2   dyoung #ifdef __NetBSD__
     44  1.8   dyoung __KERNEL_RCSID(0, "$NetBSD: athrate-onoe.c,v 1.8 2006/03/02 03:38:45 dyoung Exp $");
     45  1.2   dyoung #endif
     46  1.1   dyoung 
     47  1.1   dyoung /*
     48  1.1   dyoung  * Atsushi Onoe's rate control algorithm.
     49  1.1   dyoung  */
     50  1.1   dyoung #include "opt_inet.h"
     51  1.1   dyoung 
     52  1.1   dyoung #include <sys/param.h>
     53  1.1   dyoung #include <sys/systm.h>
     54  1.1   dyoung #include <sys/sysctl.h>
     55  1.1   dyoung #include <sys/kernel.h>
     56  1.1   dyoung #include <sys/lock.h>
     57  1.1   dyoung #include <sys/errno.h>
     58  1.3   martin #include <sys/device.h>
     59  1.1   dyoung 
     60  1.1   dyoung #include <machine/bus.h>
     61  1.1   dyoung 
     62  1.1   dyoung #include <sys/socket.h>
     63  1.1   dyoung 
     64  1.1   dyoung #include <net/if.h>
     65  1.1   dyoung #include <net/if_media.h>
     66  1.1   dyoung #include <net/if_arp.h>
     67  1.2   dyoung #include <net/if_ether.h>		/* XXX for ether_sprintf */
     68  1.1   dyoung 
     69  1.1   dyoung #include <net80211/ieee80211_var.h>
     70  1.1   dyoung 
     71  1.1   dyoung #include <net/bpf.h>
     72  1.1   dyoung 
     73  1.1   dyoung #ifdef INET
     74  1.1   dyoung #include <netinet/in.h>
     75  1.1   dyoung #endif
     76  1.1   dyoung 
     77  1.2   dyoung #include <dev/ic/ath_netbsd.h>
     78  1.2   dyoung #include <dev/ic/athvar.h>
     79  1.2   dyoung #include <dev/ic/athrate-onoe.h>
     80  1.2   dyoung #include <contrib/dev/ic/athhal_desc.h>
     81  1.1   dyoung 
     82  1.1   dyoung #define	ONOE_DEBUG
     83  1.1   dyoung #ifdef ONOE_DEBUG
     84  1.1   dyoung enum {
     85  1.1   dyoung 	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
     86  1.1   dyoung };
     87  1.1   dyoung #define	DPRINTF(sc, _fmt, ...) do {				\
     88  1.1   dyoung 	if (sc->sc_debug & ATH_DEBUG_RATE)			\
     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 /*
     96  1.1   dyoung  * Default parameters for the rate control algorithm.  These are
     97  1.1   dyoung  * all tunable with sysctls.  The rate controller runs periodically
     98  1.1   dyoung  * (each ath_rateinterval ms) analyzing transmit statistics for each
     99  1.1   dyoung  * neighbor/station (when operating in station mode this is only the AP).
    100  1.1   dyoung  * If transmits look to be working well over a sampling period then
    101  1.1   dyoung  * it gives a "raise rate credit".  If transmits look to not be working
    102  1.1   dyoung  * well than it deducts a credit.  If the credits cross a threshold then
    103  1.1   dyoung  * the transmit rate is raised.  Various error conditions force the
    104  1.1   dyoung  * the transmit rate to be dropped.
    105  1.1   dyoung  *
    106  1.1   dyoung  * The decision to issue/deduct a credit is based on the errors and
    107  1.1   dyoung  * retries accumulated over the sampling period.  ath_rate_raise defines
    108  1.1   dyoung  * the percent of retransmits for which a credit is issued/deducted.
    109  1.1   dyoung  * ath_rate_raise_threshold defines the threshold on credits at which
    110  1.1   dyoung  * the transmit rate is increased.
    111  1.1   dyoung  *
    112  1.1   dyoung  * XXX this algorithm is flawed.
    113  1.1   dyoung  */
    114  1.1   dyoung static	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
    115  1.1   dyoung static	int ath_rate_raise = 10;		/* add credit threshold */
    116  1.1   dyoung static	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
    117  1.1   dyoung 
    118  1.1   dyoung static void	ath_ratectl(void *);
    119  1.1   dyoung static void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
    120  1.1   dyoung 			int rate);
    121  1.1   dyoung static void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
    122  1.1   dyoung static void	ath_rate_ctl(void *, struct ieee80211_node *);
    123  1.1   dyoung 
    124  1.1   dyoung void
    125  1.1   dyoung ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
    126  1.1   dyoung {
    127  1.1   dyoung 	/* NB: assumed to be zero'd by caller */
    128  1.1   dyoung 	ath_rate_update(sc, &an->an_node, 0);
    129  1.1   dyoung }
    130  1.1   dyoung 
    131  1.1   dyoung void
    132  1.1   dyoung ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
    133  1.1   dyoung {
    134  1.1   dyoung }
    135  1.1   dyoung 
    136  1.1   dyoung void
    137  1.1   dyoung ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
    138  1.1   dyoung 	int shortPreamble, size_t frameLen,
    139  1.1   dyoung 	u_int8_t *rix, int *try0, u_int8_t *txrate)
    140  1.1   dyoung {
    141  1.1   dyoung 	struct onoe_node *on = ATH_NODE_ONOE(an);
    142  1.1   dyoung 
    143  1.1   dyoung 	*rix = on->on_tx_rix0;
    144  1.1   dyoung 	*try0 = on->on_tx_try0;
    145  1.1   dyoung 	if (shortPreamble)
    146  1.1   dyoung 		*txrate = on->on_tx_rate0sp;
    147  1.1   dyoung 	else
    148  1.1   dyoung 		*txrate = on->on_tx_rate0;
    149  1.1   dyoung }
    150  1.1   dyoung 
    151  1.1   dyoung void
    152  1.1   dyoung ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
    153  1.1   dyoung 	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
    154  1.1   dyoung {
    155  1.1   dyoung 	struct onoe_node *on = ATH_NODE_ONOE(an);
    156  1.1   dyoung 
    157  1.1   dyoung 	ath_hal_setupxtxdesc(sc->sc_ah, ds
    158  1.1   dyoung 		, on->on_tx_rate1sp, 2	/* series 1 */
    159  1.1   dyoung 		, on->on_tx_rate2sp, 2	/* series 2 */
    160  1.1   dyoung 		, on->on_tx_rate3sp, 2	/* series 3 */
    161  1.1   dyoung 	);
    162  1.1   dyoung }
    163  1.1   dyoung 
    164  1.1   dyoung void
    165  1.1   dyoung ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
    166  1.1   dyoung 	const struct ath_desc *ds, const struct ath_desc *ds0)
    167  1.1   dyoung {
    168  1.1   dyoung 	struct onoe_node *on = ATH_NODE_ONOE(an);
    169  1.1   dyoung 
    170  1.1   dyoung 	if (ds->ds_txstat.ts_status == 0)
    171  1.1   dyoung 		on->on_tx_ok++;
    172  1.1   dyoung 	else
    173  1.1   dyoung 		on->on_tx_err++;
    174  1.1   dyoung 	on->on_tx_retr += ds->ds_txstat.ts_shortretry
    175  1.1   dyoung 			+ ds->ds_txstat.ts_longretry;
    176  1.1   dyoung }
    177  1.1   dyoung 
    178  1.1   dyoung void
    179  1.1   dyoung ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
    180  1.1   dyoung {
    181  1.1   dyoung 	if (isnew)
    182  1.1   dyoung 		ath_rate_ctl_start(sc, &an->an_node);
    183  1.1   dyoung }
    184  1.1   dyoung 
    185  1.1   dyoung static void
    186  1.1   dyoung ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
    187  1.1   dyoung {
    188  1.1   dyoung 	struct ath_node *an = ATH_NODE(ni);
    189  1.1   dyoung 	struct onoe_node *on = ATH_NODE_ONOE(an);
    190  1.1   dyoung 	const HAL_RATE_TABLE *rt = sc->sc_currates;
    191  1.1   dyoung 	u_int8_t rix;
    192  1.1   dyoung 
    193  1.1   dyoung 	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
    194  1.1   dyoung 
    195  1.1   dyoung 	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
    196  1.1   dyoung 	    __func__, ether_sprintf(ni->ni_macaddr),
    197  1.1   dyoung 	    ni->ni_rates.rs_nrates > 0 ?
    198  1.1   dyoung 		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
    199  1.1   dyoung 
    200  1.1   dyoung 	ni->ni_txrate = rate;
    201  1.1   dyoung 	/*
    202  1.1   dyoung 	 * Before associating a node has no rate set setup
    203  1.1   dyoung 	 * so we can't calculate any transmit codes to use.
    204  1.1   dyoung 	 * This is ok since we should never be sending anything
    205  1.1   dyoung 	 * but management frames and those always go at the
    206  1.1   dyoung 	 * lowest hardware rate.
    207  1.1   dyoung 	 */
    208  1.1   dyoung 	if (ni->ni_rates.rs_nrates == 0)
    209  1.1   dyoung 		goto done;
    210  1.1   dyoung 	on->on_tx_rix0 = sc->sc_rixmap[
    211  1.1   dyoung 		ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
    212  1.1   dyoung 	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
    213  1.1   dyoung 
    214  1.1   dyoung 	on->on_tx_rate0sp = on->on_tx_rate0 |
    215  1.1   dyoung 		rt->info[on->on_tx_rix0].shortPreamble;
    216  1.1   dyoung 	if (sc->sc_mrretry) {
    217  1.1   dyoung 		/*
    218  1.1   dyoung 		 * Hardware supports multi-rate retry; setup two
    219  1.1   dyoung 		 * step-down retry rates and make the lowest rate
    220  1.1   dyoung 		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
    221  1.1   dyoung 		 * respectively (4 is set here, the rest are fixed
    222  1.1   dyoung 		 * in the xmit routine).
    223  1.1   dyoung 		 */
    224  1.1   dyoung 		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
    225  1.1   dyoung 		if (--rate >= 0) {
    226  1.1   dyoung 			rix = sc->sc_rixmap[
    227  1.1   dyoung 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    228  1.1   dyoung 			on->on_tx_rate1 = rt->info[rix].rateCode;
    229  1.1   dyoung 			on->on_tx_rate1sp = on->on_tx_rate1 |
    230  1.1   dyoung 				rt->info[rix].shortPreamble;
    231  1.1   dyoung 		} else {
    232  1.1   dyoung 			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
    233  1.1   dyoung 		}
    234  1.1   dyoung 		if (--rate >= 0) {
    235  1.1   dyoung 			rix = sc->sc_rixmap[
    236  1.1   dyoung 				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
    237  1.1   dyoung 			on->on_tx_rate2 = rt->info[rix].rateCode;
    238  1.1   dyoung 			on->on_tx_rate2sp = on->on_tx_rate2 |
    239  1.1   dyoung 				rt->info[rix].shortPreamble;
    240  1.1   dyoung 		} else {
    241  1.1   dyoung 			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
    242  1.1   dyoung 		}
    243  1.1   dyoung 		if (rate > 0) {
    244  1.1   dyoung 			/* NB: only do this if we didn't already do it above */
    245  1.1   dyoung 			on->on_tx_rate3 = rt->info[0].rateCode;
    246  1.1   dyoung 			on->on_tx_rate3sp =
    247  1.8   dyoung 				an->an_tx_rate3 | rt->info[0].shortPreamble;
    248  1.1   dyoung 		} else {
    249  1.1   dyoung 			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
    250  1.1   dyoung 		}
    251  1.1   dyoung 	} else {
    252  1.1   dyoung 		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
    253  1.1   dyoung 		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
    254  1.1   dyoung 		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
    255  1.1   dyoung 		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
    256  1.1   dyoung 	}
    257  1.1   dyoung done:
    258  1.1   dyoung 	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
    259  1.1   dyoung }
    260  1.1   dyoung 
    261  1.1   dyoung /*
    262  1.1   dyoung  * Set the starting transmit rate for a node.
    263  1.1   dyoung  */
    264  1.1   dyoung static void
    265  1.1   dyoung ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
    266  1.1   dyoung {
    267  1.1   dyoung #define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
    268  1.1   dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    269  1.1   dyoung 	int srate;
    270  1.1   dyoung 
    271  1.1   dyoung 	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
    272  1.6    skrll 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
    273  1.1   dyoung 		/*
    274  1.1   dyoung 		 * No fixed rate is requested. For 11b start with
    275  1.1   dyoung 		 * the highest negotiated rate; otherwise, for 11g
    276  1.1   dyoung 		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
    277  1.1   dyoung 		 */
    278  1.1   dyoung 		srate = ni->ni_rates.rs_nrates - 1;
    279  1.1   dyoung 		if (sc->sc_curmode != IEEE80211_MODE_11B) {
    280  1.1   dyoung 			/*
    281  1.1   dyoung 			 * Scan the negotiated rate set to find the
    282  1.1   dyoung 			 * closest rate.
    283  1.1   dyoung 			 */
    284  1.1   dyoung 			/* NB: the rate set is assumed sorted */
    285  1.1   dyoung 			for (; srate >= 0 && RATE(srate) > 72; srate--)
    286  1.1   dyoung 				;
    287  1.1   dyoung 			KASSERT(srate >= 0, ("bogus rate set"));
    288  1.1   dyoung 		}
    289  1.1   dyoung 	} else {
    290  1.1   dyoung 		/*
    291  1.1   dyoung 		 * A fixed rate is to be used; ic_fixed_rate is an
    292  1.1   dyoung 		 * index into the supported rate set.  Convert this
    293  1.1   dyoung 		 * to the index into the negotiated rate set for
    294  1.1   dyoung 		 * the node.  We know the rate is there because the
    295  1.1   dyoung 		 * rate set is checked when the station associates.
    296  1.1   dyoung 		 */
    297  1.1   dyoung 		const struct ieee80211_rateset *rs =
    298  1.1   dyoung 			&ic->ic_sup_rates[ic->ic_curmode];
    299  1.1   dyoung 		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
    300  1.1   dyoung 		/* NB: the rate set is assumed sorted */
    301  1.1   dyoung 		srate = ni->ni_rates.rs_nrates - 1;
    302  1.1   dyoung 		for (; srate >= 0 && RATE(srate) != r; srate--)
    303  1.1   dyoung 			;
    304  1.1   dyoung 		KASSERT(srate >= 0,
    305  1.1   dyoung 			("fixed rate %d not in rate set", ic->ic_fixed_rate));
    306  1.1   dyoung 	}
    307  1.1   dyoung 	ath_rate_update(sc, ni, srate);
    308  1.1   dyoung #undef RATE
    309  1.1   dyoung }
    310  1.1   dyoung 
    311  1.1   dyoung static void
    312  1.1   dyoung ath_rate_cb(void *arg, struct ieee80211_node *ni)
    313  1.1   dyoung {
    314  1.1   dyoung 	struct ath_softc *sc = arg;
    315  1.1   dyoung 
    316  1.1   dyoung 	ath_rate_update(sc, ni, 0);
    317  1.1   dyoung }
    318  1.1   dyoung 
    319  1.1   dyoung /*
    320  1.1   dyoung  * Reset the rate control state for each 802.11 state transition.
    321  1.1   dyoung  */
    322  1.1   dyoung void
    323  1.1   dyoung ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
    324  1.1   dyoung {
    325  1.1   dyoung 	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
    326  1.1   dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    327  1.1   dyoung 	struct ieee80211_node *ni;
    328  1.1   dyoung 
    329  1.1   dyoung 	if (state == IEEE80211_S_INIT) {
    330  1.1   dyoung 		callout_stop(&osc->timer);
    331  1.1   dyoung 		return;
    332  1.1   dyoung 	}
    333  1.1   dyoung 	if (ic->ic_opmode == IEEE80211_M_STA) {
    334  1.1   dyoung 		/*
    335  1.1   dyoung 		 * Reset local xmit state; this is really only
    336  1.1   dyoung 		 * meaningful when operating in station mode.
    337  1.1   dyoung 		 */
    338  1.1   dyoung 		ni = ic->ic_bss;
    339  1.1   dyoung 		if (state == IEEE80211_S_RUN) {
    340  1.1   dyoung 			ath_rate_ctl_start(sc, ni);
    341  1.1   dyoung 		} else {
    342  1.1   dyoung 			ath_rate_update(sc, ni, 0);
    343  1.1   dyoung 		}
    344  1.1   dyoung 	} else {
    345  1.1   dyoung 		/*
    346  1.1   dyoung 		 * When operating as a station the node table holds
    347  1.1   dyoung 		 * the AP's that were discovered during scanning.
    348  1.1   dyoung 		 * For any other operating mode we want to reset the
    349  1.1   dyoung 		 * tx rate state of each node.
    350  1.1   dyoung 		 */
    351  1.1   dyoung 		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
    352  1.1   dyoung 		ath_rate_update(sc, ic->ic_bss, 0);
    353  1.1   dyoung 	}
    354  1.6    skrll 	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
    355  1.6    skrll 	    state == IEEE80211_S_RUN) {
    356  1.1   dyoung 		int interval;
    357  1.1   dyoung 		/*
    358  1.1   dyoung 		 * Start the background rate control thread if we
    359  1.1   dyoung 		 * are not configured to use a fixed xmit rate.
    360  1.1   dyoung 		 */
    361  1.1   dyoung 		interval = ath_rateinterval;
    362  1.1   dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    363  1.1   dyoung 			interval /= 2;
    364  1.1   dyoung 		callout_reset(&osc->timer, (interval * hz) / 1000,
    365  1.1   dyoung 			ath_ratectl, &sc->sc_if);
    366  1.1   dyoung 	}
    367  1.1   dyoung }
    368  1.1   dyoung 
    369  1.1   dyoung /*
    370  1.1   dyoung  * Examine and potentially adjust the transmit rate.
    371  1.1   dyoung  */
    372  1.1   dyoung static void
    373  1.1   dyoung ath_rate_ctl(void *arg, struct ieee80211_node *ni)
    374  1.1   dyoung {
    375  1.1   dyoung 	struct ath_softc *sc = arg;
    376  1.1   dyoung 	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
    377  1.1   dyoung 	struct ieee80211_rateset *rs = &ni->ni_rates;
    378  1.1   dyoung 	int dir = 0, nrate, enough;
    379  1.1   dyoung 
    380  1.1   dyoung 	/*
    381  1.1   dyoung 	 * Rate control
    382  1.1   dyoung 	 * XXX: very primitive version.
    383  1.1   dyoung 	 */
    384  1.1   dyoung 	enough = (on->on_tx_ok + on->on_tx_err >= 10);
    385  1.1   dyoung 
    386  1.1   dyoung 	/* no packet reached -> down */
    387  1.1   dyoung 	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
    388  1.1   dyoung 		dir = -1;
    389  1.1   dyoung 
    390  1.1   dyoung 	/* all packets needs retry in average -> down */
    391  1.1   dyoung 	if (enough && on->on_tx_ok < on->on_tx_retr)
    392  1.1   dyoung 		dir = -1;
    393  1.1   dyoung 
    394  1.1   dyoung 	/* no error and less than rate_raise% of packets need retry -> up */
    395  1.1   dyoung 	if (enough && on->on_tx_err == 0 &&
    396  1.1   dyoung 	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
    397  1.1   dyoung 		dir = 1;
    398  1.1   dyoung 
    399  1.1   dyoung 	DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
    400  1.1   dyoung 		ether_sprintf(ni->ni_macaddr),
    401  1.1   dyoung 		on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
    402  1.1   dyoung 		on->on_tx_upper, dir);
    403  1.1   dyoung 
    404  1.1   dyoung 	nrate = ni->ni_txrate;
    405  1.1   dyoung 	switch (dir) {
    406  1.1   dyoung 	case 0:
    407  1.1   dyoung 		if (enough && on->on_tx_upper > 0)
    408  1.1   dyoung 			on->on_tx_upper--;
    409  1.1   dyoung 		break;
    410  1.1   dyoung 	case -1:
    411  1.1   dyoung 		if (nrate > 0) {
    412  1.1   dyoung 			nrate--;
    413  1.1   dyoung 			sc->sc_stats.ast_rate_drop++;
    414  1.1   dyoung 		}
    415  1.1   dyoung 		on->on_tx_upper = 0;
    416  1.1   dyoung 		break;
    417  1.1   dyoung 	case 1:
    418  1.1   dyoung 		/* raise rate if we hit rate_raise_threshold */
    419  1.1   dyoung 		if (++on->on_tx_upper < ath_rate_raise_threshold)
    420  1.1   dyoung 			break;
    421  1.1   dyoung 		on->on_tx_upper = 0;
    422  1.1   dyoung 		if (nrate + 1 < rs->rs_nrates) {
    423  1.1   dyoung 			nrate++;
    424  1.1   dyoung 			sc->sc_stats.ast_rate_raise++;
    425  1.1   dyoung 		}
    426  1.1   dyoung 		break;
    427  1.1   dyoung 	}
    428  1.1   dyoung 
    429  1.1   dyoung 	if (nrate != ni->ni_txrate) {
    430  1.1   dyoung 		DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
    431  1.1   dyoung 		    __func__,
    432  1.1   dyoung 		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
    433  1.1   dyoung 		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
    434  1.1   dyoung 		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
    435  1.1   dyoung 		ath_rate_update(sc, ni, nrate);
    436  1.1   dyoung 	} else if (enough)
    437  1.1   dyoung 		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
    438  1.1   dyoung }
    439  1.1   dyoung 
    440  1.1   dyoung static void
    441  1.1   dyoung ath_ratectl(void *arg)
    442  1.1   dyoung {
    443  1.1   dyoung 	struct ifnet *ifp = arg;
    444  1.1   dyoung 	struct ath_softc *sc = ifp->if_softc;
    445  1.1   dyoung 	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
    446  1.1   dyoung 	struct ieee80211com *ic = &sc->sc_ic;
    447  1.1   dyoung 	int interval;
    448  1.1   dyoung 
    449  1.1   dyoung 	if (ifp->if_flags & IFF_RUNNING) {
    450  1.1   dyoung 		sc->sc_stats.ast_rate_calls++;
    451  1.1   dyoung 
    452  1.1   dyoung 		if (ic->ic_opmode == IEEE80211_M_STA)
    453  1.1   dyoung 			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
    454  1.1   dyoung 		else
    455  1.1   dyoung 			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
    456  1.1   dyoung 	}
    457  1.1   dyoung 	interval = ath_rateinterval;
    458  1.1   dyoung 	if (ic->ic_opmode == IEEE80211_M_STA)
    459  1.1   dyoung 		interval /= 2;
    460  1.1   dyoung 	callout_reset(&osc->timer, (interval * hz) / 1000,
    461  1.1   dyoung 		ath_ratectl, &sc->sc_if);
    462  1.1   dyoung }
    463  1.1   dyoung 
    464  1.1   dyoung static void
    465  1.1   dyoung ath_rate_sysctlattach(struct ath_softc *sc)
    466  1.1   dyoung {
    467  1.2   dyoung 	struct sysctllog **clog = &sc->sc_sysctllog;
    468  1.2   dyoung 	const struct sysctlnode *cnode, *rnode;
    469  1.2   dyoung 
    470  1.2   dyoung 	if ((rnode = ath_sysctl_treetop(NULL)) == NULL)
    471  1.2   dyoung 		return;
    472  1.1   dyoung 
    473  1.2   dyoung 	SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_interval",
    474  1.2   dyoung 	    "rate control: operation interval (ms)", rateinterval);
    475  1.1   dyoung 	/* XXX bounds check values */
    476  1.2   dyoung 	SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise",
    477  1.2   dyoung 	    "rate control: retry threshold to credit rate raise (%%)",
    478  1.2   dyoung 	    rate_raise);
    479  1.2   dyoung 	SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise_threshold",
    480  1.2   dyoung 	    "rate control: # good periods before raising rate",
    481  1.2   dyoung 	    rate_raise_threshold);
    482  1.1   dyoung }
    483  1.1   dyoung 
    484  1.1   dyoung struct ath_ratectrl *
    485  1.1   dyoung ath_rate_attach(struct ath_softc *sc)
    486  1.1   dyoung {
    487  1.1   dyoung 	struct onoe_softc *osc;
    488  1.1   dyoung 
    489  1.1   dyoung 	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
    490  1.1   dyoung 	if (osc == NULL)
    491  1.1   dyoung 		return NULL;
    492  1.1   dyoung 	osc->arc.arc_space = sizeof(struct onoe_node);
    493  1.2   dyoung 	ATH_CALLOUT_INIT(&osc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
    494  1.1   dyoung 	ath_rate_sysctlattach(sc);
    495  1.1   dyoung 
    496  1.1   dyoung 	return &osc->arc;
    497  1.1   dyoung }
    498  1.1   dyoung 
    499  1.1   dyoung void
    500  1.1   dyoung ath_rate_detach(struct ath_ratectrl *arc)
    501  1.1   dyoung {
    502  1.1   dyoung 	struct onoe_softc *osc = (struct onoe_softc *) arc;
    503  1.1   dyoung 
    504  1.2   dyoung 	callout_stop(&osc->timer);
    505  1.1   dyoung 	free(osc, M_DEVBUF);
    506  1.1   dyoung }
    507