onoe.c revision 170375
1138569Ssam/*-
2170375Ssam * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3138569Ssam * All rights reserved.
4138569Ssam *
5138569Ssam * Redistribution and use in source and binary forms, with or without
6138569Ssam * modification, are permitted provided that the following conditions
7138569Ssam * are met:
8138569Ssam * 1. Redistributions of source code must retain the above copyright
9138569Ssam *    notice, this list of conditions and the following disclaimer,
10138569Ssam *    without modification.
11138569Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12138569Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13138569Ssam *    redistribution must be conditioned upon including a substantially
14138569Ssam *    similar Disclaimer requirement for further binary redistribution.
15138569Ssam *
16138569Ssam * NO WARRANTY
17138569Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18138569Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19138569Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20138569Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21138569Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22138569Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23138569Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24138569Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25138569Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26138569Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27138569Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28138569Ssam */
29138569Ssam
30138569Ssam#include <sys/cdefs.h>
31138569Ssam__FBSDID("$FreeBSD: head/sys/dev/ath/ath_rate/onoe/onoe.c 170375 2007-06-06 15:49:16Z sam $");
32138569Ssam
33138569Ssam/*
34138569Ssam * Atsushi Onoe's rate control algorithm.
35138569Ssam */
36138569Ssam#include "opt_inet.h"
37138569Ssam
38138569Ssam#include <sys/param.h>
39138569Ssam#include <sys/systm.h>
40138569Ssam#include <sys/sysctl.h>
41138569Ssam#include <sys/module.h>
42138569Ssam#include <sys/kernel.h>
43138569Ssam#include <sys/lock.h>
44138569Ssam#include <sys/mutex.h>
45138569Ssam#include <sys/errno.h>
46138569Ssam
47138569Ssam#include <machine/bus.h>
48138569Ssam#include <machine/resource.h>
49138569Ssam#include <sys/bus.h>
50138569Ssam
51138569Ssam#include <sys/socket.h>
52138569Ssam
53138569Ssam#include <net/if.h>
54138569Ssam#include <net/if_media.h>
55138569Ssam#include <net/if_arp.h>
56138569Ssam#include <net/ethernet.h>		/* XXX for ether_sprintf */
57138569Ssam
58138569Ssam#include <net80211/ieee80211_var.h>
59138569Ssam
60138569Ssam#include <net/bpf.h>
61138569Ssam
62138569Ssam#ifdef INET
63138569Ssam#include <netinet/in.h>
64138569Ssam#include <netinet/if_ether.h>
65138569Ssam#endif
66138569Ssam
67138569Ssam#include <dev/ath/if_athvar.h>
68138569Ssam#include <dev/ath/ath_rate/onoe/onoe.h>
69138569Ssam#include <contrib/dev/ath/ah_desc.h>
70138569Ssam
71138569Ssam#define	ONOE_DEBUG
72138569Ssam#ifdef ONOE_DEBUG
73138569Ssamenum {
74138569Ssam	ATH_DEBUG_RATE		= 0x00000010,	/* rate control */
75138569Ssam};
76138569Ssam#define	DPRINTF(sc, _fmt, ...) do {				\
77138569Ssam	if (sc->sc_debug & ATH_DEBUG_RATE)			\
78138569Ssam		printf(_fmt, __VA_ARGS__);			\
79138569Ssam} while (0)
80138569Ssam#else
81138569Ssam#define	DPRINTF(sc, _fmt, ...)
82138569Ssam#endif
83138569Ssam
84138569Ssam/*
85138569Ssam * Default parameters for the rate control algorithm.  These are
86138569Ssam * all tunable with sysctls.  The rate controller runs periodically
87138569Ssam * (each ath_rateinterval ms) analyzing transmit statistics for each
88138569Ssam * neighbor/station (when operating in station mode this is only the AP).
89138569Ssam * If transmits look to be working well over a sampling period then
90138569Ssam * it gives a "raise rate credit".  If transmits look to not be working
91138569Ssam * well than it deducts a credit.  If the credits cross a threshold then
92138569Ssam * the transmit rate is raised.  Various error conditions force the
93138569Ssam * the transmit rate to be dropped.
94138569Ssam *
95138569Ssam * The decision to issue/deduct a credit is based on the errors and
96138569Ssam * retries accumulated over the sampling period.  ath_rate_raise defines
97138569Ssam * the percent of retransmits for which a credit is issued/deducted.
98138569Ssam * ath_rate_raise_threshold defines the threshold on credits at which
99138569Ssam * the transmit rate is increased.
100138569Ssam *
101138569Ssam * XXX this algorithm is flawed.
102138569Ssam */
103138569Ssamstatic	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
104138569Ssamstatic	int ath_rate_raise = 10;		/* add credit threshold */
105138569Ssamstatic	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
106138569Ssam
107138569Ssamstatic void	ath_ratectl(void *);
108138569Ssamstatic void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
109138569Ssam			int rate);
110138569Ssamstatic void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
111138569Ssamstatic void	ath_rate_ctl(void *, struct ieee80211_node *);
112138569Ssam
113138569Ssamvoid
114138569Ssamath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
115138569Ssam{
116138569Ssam	/* NB: assumed to be zero'd by caller */
117138569Ssam	ath_rate_update(sc, &an->an_node, 0);
118138569Ssam}
119138569Ssam
120138569Ssamvoid
121138569Ssamath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
122138569Ssam{
123138569Ssam}
124138569Ssam
125138569Ssamvoid
126138569Ssamath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
127144546Ssam	int shortPreamble, size_t frameLen,
128138569Ssam	u_int8_t *rix, int *try0, u_int8_t *txrate)
129138569Ssam{
130138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
131138569Ssam
132138569Ssam	*rix = on->on_tx_rix0;
133138569Ssam	*try0 = on->on_tx_try0;
134138569Ssam	if (shortPreamble)
135138569Ssam		*txrate = on->on_tx_rate0sp;
136138569Ssam	else
137138569Ssam		*txrate = on->on_tx_rate0;
138138569Ssam}
139138569Ssam
140138569Ssamvoid
141138569Ssamath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
142144546Ssam	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
143138569Ssam{
144138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
145138569Ssam
146138569Ssam	ath_hal_setupxtxdesc(sc->sc_ah, ds
147138569Ssam		, on->on_tx_rate1sp, 2	/* series 1 */
148138569Ssam		, on->on_tx_rate2sp, 2	/* series 2 */
149138569Ssam		, on->on_tx_rate3sp, 2	/* series 3 */
150138569Ssam	);
151138569Ssam}
152138569Ssam
153138569Ssamvoid
154144347Ssamath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
155165185Ssam	const struct ath_buf *bf)
156138569Ssam{
157138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
158165185Ssam	const struct ath_tx_status *ts = &bf->bf_status.ds_txstat;
159138569Ssam
160165185Ssam	if (ts->ts_status == 0)
161138569Ssam		on->on_tx_ok++;
162138569Ssam	else
163138569Ssam		on->on_tx_err++;
164165185Ssam	on->on_tx_retr += ts->ts_shortretry
165165185Ssam			+ ts->ts_longretry;
166138569Ssam}
167138569Ssam
168138569Ssamvoid
169138569Ssamath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
170138569Ssam{
171138569Ssam	if (isnew)
172138569Ssam		ath_rate_ctl_start(sc, &an->an_node);
173138569Ssam}
174138569Ssam
175138569Ssamstatic void
176138569Ssamath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
177138569Ssam{
178138569Ssam	struct ath_node *an = ATH_NODE(ni);
179138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
180138569Ssam	const HAL_RATE_TABLE *rt = sc->sc_currates;
181138569Ssam	u_int8_t rix;
182138569Ssam
183138569Ssam	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
184138569Ssam
185138569Ssam	DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
186138569Ssam	    __func__, ether_sprintf(ni->ni_macaddr),
187138569Ssam	    ni->ni_rates.rs_nrates > 0 ?
188138569Ssam		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
189138569Ssam
190138569Ssam	ni->ni_txrate = rate;
191138569Ssam	/*
192138569Ssam	 * Before associating a node has no rate set setup
193138569Ssam	 * so we can't calculate any transmit codes to use.
194138569Ssam	 * This is ok since we should never be sending anything
195138569Ssam	 * but management frames and those always go at the
196138569Ssam	 * lowest hardware rate.
197138569Ssam	 */
198138569Ssam	if (ni->ni_rates.rs_nrates == 0)
199138569Ssam		goto done;
200138569Ssam	on->on_tx_rix0 = sc->sc_rixmap[
201138569Ssam		ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
202138569Ssam	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
203138569Ssam
204138569Ssam	on->on_tx_rate0sp = on->on_tx_rate0 |
205138569Ssam		rt->info[on->on_tx_rix0].shortPreamble;
206138569Ssam	if (sc->sc_mrretry) {
207138569Ssam		/*
208138569Ssam		 * Hardware supports multi-rate retry; setup two
209138569Ssam		 * step-down retry rates and make the lowest rate
210138569Ssam		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
211138569Ssam		 * respectively (4 is set here, the rest are fixed
212138569Ssam		 * in the xmit routine).
213138569Ssam		 */
214138569Ssam		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
215138569Ssam		if (--rate >= 0) {
216138569Ssam			rix = sc->sc_rixmap[
217138569Ssam				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
218138569Ssam			on->on_tx_rate1 = rt->info[rix].rateCode;
219138569Ssam			on->on_tx_rate1sp = on->on_tx_rate1 |
220138569Ssam				rt->info[rix].shortPreamble;
221138569Ssam		} else {
222138569Ssam			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
223138569Ssam		}
224138569Ssam		if (--rate >= 0) {
225138569Ssam			rix = sc->sc_rixmap[
226138569Ssam				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
227138569Ssam			on->on_tx_rate2 = rt->info[rix].rateCode;
228138569Ssam			on->on_tx_rate2sp = on->on_tx_rate2 |
229138569Ssam				rt->info[rix].shortPreamble;
230138569Ssam		} else {
231138569Ssam			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
232138569Ssam		}
233138569Ssam		if (rate > 0) {
234138569Ssam			/* NB: only do this if we didn't already do it above */
235138569Ssam			on->on_tx_rate3 = rt->info[0].rateCode;
236138569Ssam			on->on_tx_rate3sp =
237155477Ssam				on->on_tx_rate3 | rt->info[0].shortPreamble;
238138569Ssam		} else {
239138569Ssam			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
240138569Ssam		}
241138569Ssam	} else {
242138569Ssam		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
243138569Ssam		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
244138569Ssam		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
245138569Ssam		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
246138569Ssam	}
247138569Ssamdone:
248138569Ssam	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
249138569Ssam}
250138569Ssam
251138569Ssam/*
252138569Ssam * Set the starting transmit rate for a node.
253138569Ssam */
254138569Ssamstatic void
255138569Ssamath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
256138569Ssam{
257138569Ssam#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
258138569Ssam	struct ieee80211com *ic = &sc->sc_ic;
259138569Ssam	int srate;
260138569Ssam
261138569Ssam	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
262148290Ssam	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
263138569Ssam		/*
264138569Ssam		 * No fixed rate is requested. For 11b start with
265138569Ssam		 * the highest negotiated rate; otherwise, for 11g
266138569Ssam		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
267138569Ssam		 */
268138569Ssam		srate = ni->ni_rates.rs_nrates - 1;
269138569Ssam		if (sc->sc_curmode != IEEE80211_MODE_11B) {
270138569Ssam			/*
271138569Ssam			 * Scan the negotiated rate set to find the
272138569Ssam			 * closest rate.
273138569Ssam			 */
274138569Ssam			/* NB: the rate set is assumed sorted */
275138569Ssam			for (; srate >= 0 && RATE(srate) > 72; srate--)
276138569Ssam				;
277138569Ssam			KASSERT(srate >= 0, ("bogus rate set"));
278138569Ssam		}
279138569Ssam	} else {
280138569Ssam		/*
281138569Ssam		 * A fixed rate is to be used; ic_fixed_rate is an
282138569Ssam		 * index into the supported rate set.  Convert this
283138569Ssam		 * to the index into the negotiated rate set for
284138569Ssam		 * the node.  We know the rate is there because the
285138569Ssam		 * rate set is checked when the station associates.
286138569Ssam		 */
287138569Ssam		const struct ieee80211_rateset *rs =
288138569Ssam			&ic->ic_sup_rates[ic->ic_curmode];
289138569Ssam		int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
290138569Ssam		/* NB: the rate set is assumed sorted */
291138569Ssam		srate = ni->ni_rates.rs_nrates - 1;
292138569Ssam		for (; srate >= 0 && RATE(srate) != r; srate--)
293138569Ssam			;
294138569Ssam		KASSERT(srate >= 0,
295138569Ssam			("fixed rate %d not in rate set", ic->ic_fixed_rate));
296138569Ssam	}
297138569Ssam	ath_rate_update(sc, ni, srate);
298138569Ssam#undef RATE
299138569Ssam}
300138569Ssam
301138569Ssamstatic void
302138569Ssamath_rate_cb(void *arg, struct ieee80211_node *ni)
303138569Ssam{
304144306Ssam	struct ath_softc *sc = arg;
305144306Ssam
306144306Ssam	ath_rate_update(sc, ni, 0);
307138569Ssam}
308138569Ssam
309138569Ssam/*
310138569Ssam * Reset the rate control state for each 802.11 state transition.
311138569Ssam */
312138569Ssamvoid
313138569Ssamath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
314138569Ssam{
315138569Ssam	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
316138569Ssam	struct ieee80211com *ic = &sc->sc_ic;
317138569Ssam	struct ieee80211_node *ni;
318138569Ssam
319138569Ssam	if (state == IEEE80211_S_INIT) {
320138569Ssam		callout_stop(&osc->timer);
321138569Ssam		return;
322138569Ssam	}
323138569Ssam	if (ic->ic_opmode == IEEE80211_M_STA) {
324138569Ssam		/*
325138569Ssam		 * Reset local xmit state; this is really only
326138569Ssam		 * meaningful when operating in station mode.
327138569Ssam		 */
328138569Ssam		ni = ic->ic_bss;
329138569Ssam		if (state == IEEE80211_S_RUN) {
330138569Ssam			ath_rate_ctl_start(sc, ni);
331138569Ssam		} else {
332138569Ssam			ath_rate_update(sc, ni, 0);
333138569Ssam		}
334138569Ssam	} else {
335138569Ssam		/*
336138569Ssam		 * When operating as a station the node table holds
337138569Ssam		 * the AP's that were discovered during scanning.
338138569Ssam		 * For any other operating mode we want to reset the
339138569Ssam		 * tx rate state of each node.
340138569Ssam		 */
341144306Ssam		ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
342138569Ssam		ath_rate_update(sc, ic->ic_bss, 0);
343138569Ssam	}
344148290Ssam	if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
345148290Ssam	    state == IEEE80211_S_RUN) {
346138569Ssam		int interval;
347138569Ssam		/*
348138569Ssam		 * Start the background rate control thread if we
349138569Ssam		 * are not configured to use a fixed xmit rate.
350138569Ssam		 */
351138569Ssam		interval = ath_rateinterval;
352138569Ssam		if (ic->ic_opmode == IEEE80211_M_STA)
353138569Ssam			interval /= 2;
354138569Ssam		callout_reset(&osc->timer, (interval * hz) / 1000,
355147256Sbrooks			ath_ratectl, sc->sc_ifp);
356138569Ssam	}
357138569Ssam}
358138569Ssam
359138569Ssam/*
360138569Ssam * Examine and potentially adjust the transmit rate.
361138569Ssam */
362138569Ssamstatic void
363138569Ssamath_rate_ctl(void *arg, struct ieee80211_node *ni)
364138569Ssam{
365138569Ssam	struct ath_softc *sc = arg;
366138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
367138569Ssam	struct ieee80211_rateset *rs = &ni->ni_rates;
368138569Ssam	int dir = 0, nrate, enough;
369138569Ssam
370138569Ssam	/*
371138569Ssam	 * Rate control
372138569Ssam	 * XXX: very primitive version.
373138569Ssam	 */
374138569Ssam	enough = (on->on_tx_ok + on->on_tx_err >= 10);
375138569Ssam
376138569Ssam	/* no packet reached -> down */
377138569Ssam	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
378138569Ssam		dir = -1;
379138569Ssam
380138569Ssam	/* all packets needs retry in average -> down */
381138569Ssam	if (enough && on->on_tx_ok < on->on_tx_retr)
382138569Ssam		dir = -1;
383138569Ssam
384138569Ssam	/* no error and less than rate_raise% of packets need retry -> up */
385138569Ssam	if (enough && on->on_tx_err == 0 &&
386138569Ssam	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
387138569Ssam		dir = 1;
388138569Ssam
389138569Ssam	DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
390138569Ssam		ether_sprintf(ni->ni_macaddr),
391138569Ssam		on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
392138569Ssam		on->on_tx_upper, dir);
393138569Ssam
394138569Ssam	nrate = ni->ni_txrate;
395138569Ssam	switch (dir) {
396138569Ssam	case 0:
397138569Ssam		if (enough && on->on_tx_upper > 0)
398138569Ssam			on->on_tx_upper--;
399138569Ssam		break;
400138569Ssam	case -1:
401138569Ssam		if (nrate > 0) {
402138569Ssam			nrate--;
403138569Ssam			sc->sc_stats.ast_rate_drop++;
404138569Ssam		}
405138569Ssam		on->on_tx_upper = 0;
406138569Ssam		break;
407138569Ssam	case 1:
408138569Ssam		/* raise rate if we hit rate_raise_threshold */
409138569Ssam		if (++on->on_tx_upper < ath_rate_raise_threshold)
410138569Ssam			break;
411138569Ssam		on->on_tx_upper = 0;
412138569Ssam		if (nrate + 1 < rs->rs_nrates) {
413138569Ssam			nrate++;
414138569Ssam			sc->sc_stats.ast_rate_raise++;
415138569Ssam		}
416138569Ssam		break;
417138569Ssam	}
418138569Ssam
419138569Ssam	if (nrate != ni->ni_txrate) {
420138569Ssam		DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
421138569Ssam		    __func__,
422138569Ssam		    (rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
423138569Ssam		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
424138569Ssam		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
425138569Ssam		ath_rate_update(sc, ni, nrate);
426138569Ssam	} else if (enough)
427138569Ssam		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
428138569Ssam}
429138569Ssam
430138569Ssamstatic void
431138569Ssamath_ratectl(void *arg)
432138569Ssam{
433138569Ssam	struct ifnet *ifp = arg;
434138569Ssam	struct ath_softc *sc = ifp->if_softc;
435138569Ssam	struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
436138569Ssam	struct ieee80211com *ic = &sc->sc_ic;
437138569Ssam	int interval;
438138569Ssam
439148887Srwatson	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
440138569Ssam		sc->sc_stats.ast_rate_calls++;
441138569Ssam
442138569Ssam		if (ic->ic_opmode == IEEE80211_M_STA)
443138569Ssam			ath_rate_ctl(sc, ic->ic_bss);	/* NB: no reference */
444140753Ssam		else
445140753Ssam			ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
446138569Ssam	}
447138569Ssam	interval = ath_rateinterval;
448138569Ssam	if (ic->ic_opmode == IEEE80211_M_STA)
449138569Ssam		interval /= 2;
450138569Ssam	callout_reset(&osc->timer, (interval * hz) / 1000,
451147256Sbrooks		ath_ratectl, sc->sc_ifp);
452138569Ssam}
453138569Ssam
454138569Ssamstatic void
455138569Ssamath_rate_sysctlattach(struct ath_softc *sc)
456138569Ssam{
457138569Ssam	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
458138569Ssam	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
459138569Ssam
460138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
461138569Ssam		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
462138569Ssam		"rate control: operation interval (ms)");
463138569Ssam	/* XXX bounds check values */
464138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
465138569Ssam		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
466138569Ssam		"rate control: retry threshold to credit rate raise (%%)");
467138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
468138569Ssam		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
469138569Ssam		"rate control: # good periods before raising rate");
470138569Ssam}
471138569Ssam
472138569Ssamstruct ath_ratectrl *
473138569Ssamath_rate_attach(struct ath_softc *sc)
474138569Ssam{
475138569Ssam	struct onoe_softc *osc;
476138569Ssam
477138569Ssam	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
478138569Ssam	if (osc == NULL)
479138569Ssam		return NULL;
480138569Ssam	osc->arc.arc_space = sizeof(struct onoe_node);
481138569Ssam	callout_init(&osc->timer, debug_mpsafenet ? CALLOUT_MPSAFE : 0);
482138569Ssam	ath_rate_sysctlattach(sc);
483138569Ssam
484138569Ssam	return &osc->arc;
485138569Ssam}
486138569Ssam
487138569Ssamvoid
488138569Ssamath_rate_detach(struct ath_ratectrl *arc)
489138569Ssam{
490138569Ssam	struct onoe_softc *osc = (struct onoe_softc *) arc;
491138569Ssam
492138569Ssam	callout_drain(&osc->timer);
493138569Ssam	free(osc, M_DEVBUF);
494138569Ssam}
495138569Ssam
496138569Ssam/*
497138569Ssam * Module glue.
498138569Ssam */
499138569Ssamstatic int
500138569Ssamonoe_modevent(module_t mod, int type, void *unused)
501138569Ssam{
502138569Ssam	switch (type) {
503138569Ssam	case MOD_LOAD:
504138569Ssam		if (bootverbose)
505138569Ssam			printf("ath_rate: <Atsushi Onoe's rate control algorithm>\n");
506138569Ssam		return 0;
507138569Ssam	case MOD_UNLOAD:
508138569Ssam		return 0;
509138569Ssam	}
510138569Ssam	return EINVAL;
511138569Ssam}
512138569Ssam
513138569Ssamstatic moduledata_t onoe_mod = {
514138569Ssam	"ath_rate",
515138569Ssam	onoe_modevent,
516138569Ssam	0
517138569Ssam};
518138569SsamDECLARE_MODULE(ath_rate, onoe_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
519138569SsamMODULE_VERSION(ath_rate, 1);
520138569SsamMODULE_DEPEND(ath_rate, wlan, 1, 1, 1);
521