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$");
32138569Ssam
33138569Ssam/*
34138569Ssam * Atsushi Onoe's rate control algorithm.
35138569Ssam */
36237529Sadrian#include "opt_ath.h"
37138569Ssam#include "opt_inet.h"
38178354Ssam#include "opt_wlan.h"
39138569Ssam
40138569Ssam#include <sys/param.h>
41138569Ssam#include <sys/systm.h>
42138569Ssam#include <sys/sysctl.h>
43138569Ssam#include <sys/kernel.h>
44138569Ssam#include <sys/lock.h>
45138569Ssam#include <sys/mutex.h>
46138569Ssam#include <sys/errno.h>
47138569Ssam
48138569Ssam#include <machine/bus.h>
49138569Ssam#include <machine/resource.h>
50138569Ssam#include <sys/bus.h>
51138569Ssam
52138569Ssam#include <sys/socket.h>
53138569Ssam
54138569Ssam#include <net/if.h>
55138569Ssam#include <net/if_media.h>
56138569Ssam#include <net/if_arp.h>
57138569Ssam#include <net/ethernet.h>		/* XXX for ether_sprintf */
58138569Ssam
59138569Ssam#include <net80211/ieee80211_var.h>
60138569Ssam
61138569Ssam#include <net/bpf.h>
62138569Ssam
63138569Ssam#ifdef INET
64138569Ssam#include <netinet/in.h>
65138569Ssam#include <netinet/if_ether.h>
66138569Ssam#endif
67138569Ssam
68138569Ssam#include <dev/ath/if_athvar.h>
69138569Ssam#include <dev/ath/ath_rate/onoe/onoe.h>
70185522Ssam#include <dev/ath/ath_hal/ah_desc.h>
71138569Ssam
72138569Ssam/*
73138569Ssam * Default parameters for the rate control algorithm.  These are
74138569Ssam * all tunable with sysctls.  The rate controller runs periodically
75138569Ssam * (each ath_rateinterval ms) analyzing transmit statistics for each
76138569Ssam * neighbor/station (when operating in station mode this is only the AP).
77138569Ssam * If transmits look to be working well over a sampling period then
78138569Ssam * it gives a "raise rate credit".  If transmits look to not be working
79138569Ssam * well than it deducts a credit.  If the credits cross a threshold then
80138569Ssam * the transmit rate is raised.  Various error conditions force the
81138569Ssam * the transmit rate to be dropped.
82138569Ssam *
83138569Ssam * The decision to issue/deduct a credit is based on the errors and
84138569Ssam * retries accumulated over the sampling period.  ath_rate_raise defines
85138569Ssam * the percent of retransmits for which a credit is issued/deducted.
86138569Ssam * ath_rate_raise_threshold defines the threshold on credits at which
87138569Ssam * the transmit rate is increased.
88138569Ssam *
89138569Ssam * XXX this algorithm is flawed.
90138569Ssam */
91138569Ssamstatic	int ath_rateinterval = 1000;		/* rate ctl interval (ms)  */
92138569Ssamstatic	int ath_rate_raise = 10;		/* add credit threshold */
93138569Ssamstatic	int ath_rate_raise_threshold = 10;	/* rate ctl raise threshold */
94138569Ssam
95138569Ssamstatic void	ath_rate_update(struct ath_softc *, struct ieee80211_node *,
96138569Ssam			int rate);
97138569Ssamstatic void	ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
98138569Ssamstatic void	ath_rate_ctl(void *, struct ieee80211_node *);
99138569Ssam
100138569Ssamvoid
101138569Ssamath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
102138569Ssam{
103138569Ssam	/* NB: assumed to be zero'd by caller */
104138569Ssam}
105138569Ssam
106138569Ssamvoid
107138569Ssamath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
108138569Ssam{
109138569Ssam}
110138569Ssam
111138569Ssamvoid
112138569Ssamath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
113144546Ssam	int shortPreamble, size_t frameLen,
114138569Ssam	u_int8_t *rix, int *try0, u_int8_t *txrate)
115138569Ssam{
116138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
117138569Ssam
118138569Ssam	*rix = on->on_tx_rix0;
119138569Ssam	*try0 = on->on_tx_try0;
120138569Ssam	if (shortPreamble)
121138569Ssam		*txrate = on->on_tx_rate0sp;
122138569Ssam	else
123138569Ssam		*txrate = on->on_tx_rate0;
124138569Ssam}
125138569Ssam
126218160Sadrian/*
127218160Sadrian * Get the TX rates.
128218160Sadrian *
129218160Sadrian * The short preamble bits aren't set here; the caller should augment
130218160Sadrian * the returned rate with the relevant preamble rate flag.
131218160Sadrian */
132138569Ssamvoid
133218160Sadrianath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
134227364Sadrian    uint8_t rix0, struct ath_rc_series *rc)
135218160Sadrian{
136218160Sadrian	struct onoe_node *on = ATH_NODE_ONOE(an);
137218160Sadrian
138227364Sadrian	rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
139218160Sadrian
140227364Sadrian	rc[0].rix = on->on_tx_rate0;
141227364Sadrian	rc[1].rix = on->on_tx_rate1;
142227364Sadrian	rc[2].rix = on->on_tx_rate2;
143227364Sadrian	rc[3].rix = on->on_tx_rate3;
144227364Sadrian
145227364Sadrian	rc[0].tries = on->on_tx_try0;
146227364Sadrian	rc[1].tries = 2;
147227364Sadrian	rc[2].tries = 2;
148227364Sadrian	rc[3].tries = 2;
149218160Sadrian}
150218160Sadrian
151218160Sadrianvoid
152138569Ssamath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
153144546Ssam	struct ath_desc *ds, int shortPreamble, u_int8_t rix)
154138569Ssam{
155138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
156138569Ssam
157138569Ssam	ath_hal_setupxtxdesc(sc->sc_ah, ds
158138569Ssam		, on->on_tx_rate1sp, 2	/* series 1 */
159138569Ssam		, on->on_tx_rate2sp, 2	/* series 2 */
160138569Ssam		, on->on_tx_rate3sp, 2	/* series 3 */
161138569Ssam	);
162138569Ssam}
163138569Ssam
164138569Ssamvoid
165144347Ssamath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
166227364Sadrian	const struct ath_rc_series *rc, const struct ath_tx_status *ts,
167227364Sadrian	int frame_size, int nframes, int nbad)
168138569Ssam{
169138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
170138569Ssam
171165185Ssam	if (ts->ts_status == 0)
172138569Ssam		on->on_tx_ok++;
173138569Ssam	else
174138569Ssam		on->on_tx_err++;
175165185Ssam	on->on_tx_retr += ts->ts_shortretry
176165185Ssam			+ ts->ts_longretry;
177178354Ssam	if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
178178354Ssam		ath_rate_ctl(sc, &an->an_node);
179178354Ssam		on->on_ticks = ticks;
180178354Ssam	}
181138569Ssam}
182138569Ssam
183138569Ssamvoid
184138569Ssamath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
185138569Ssam{
186138569Ssam	if (isnew)
187138569Ssam		ath_rate_ctl_start(sc, &an->an_node);
188138569Ssam}
189138569Ssam
190138569Ssamstatic void
191138569Ssamath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
192138569Ssam{
193138569Ssam	struct ath_node *an = ATH_NODE(ni);
194138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(an);
195178354Ssam	struct ieee80211vap *vap = ni->ni_vap;
196138569Ssam	const HAL_RATE_TABLE *rt = sc->sc_currates;
197138569Ssam	u_int8_t rix;
198138569Ssam
199138569Ssam	KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
200138569Ssam
201178354Ssam	IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
202178354Ssam	     "%s: set xmit rate to %dM", __func__,
203178354Ssam	     ni->ni_rates.rs_nrates > 0 ?
204138569Ssam		(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
205138569Ssam
206138569Ssam	/*
207138569Ssam	 * Before associating a node has no rate set setup
208138569Ssam	 * so we can't calculate any transmit codes to use.
209138569Ssam	 * This is ok since we should never be sending anything
210138569Ssam	 * but management frames and those always go at the
211138569Ssam	 * lowest hardware rate.
212138569Ssam	 */
213138569Ssam	if (ni->ni_rates.rs_nrates == 0)
214138569Ssam		goto done;
215178354Ssam	on->on_rix = rate;
216178354Ssam	ni->ni_txrate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
217178354Ssam	on->on_tx_rix0 = sc->sc_rixmap[ni->ni_txrate];
218138569Ssam	on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
219138569Ssam
220138569Ssam	on->on_tx_rate0sp = on->on_tx_rate0 |
221138569Ssam		rt->info[on->on_tx_rix0].shortPreamble;
222138569Ssam	if (sc->sc_mrretry) {
223138569Ssam		/*
224138569Ssam		 * Hardware supports multi-rate retry; setup two
225138569Ssam		 * step-down retry rates and make the lowest rate
226138569Ssam		 * be the ``last chance''.  We use 4, 2, 2, 2 tries
227138569Ssam		 * respectively (4 is set here, the rest are fixed
228138569Ssam		 * in the xmit routine).
229138569Ssam		 */
230138569Ssam		on->on_tx_try0 = 1 + 3;		/* 4 tries at rate 0 */
231138569Ssam		if (--rate >= 0) {
232138569Ssam			rix = sc->sc_rixmap[
233138569Ssam				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
234138569Ssam			on->on_tx_rate1 = rt->info[rix].rateCode;
235138569Ssam			on->on_tx_rate1sp = on->on_tx_rate1 |
236138569Ssam				rt->info[rix].shortPreamble;
237138569Ssam		} else {
238138569Ssam			on->on_tx_rate1 = on->on_tx_rate1sp = 0;
239138569Ssam		}
240138569Ssam		if (--rate >= 0) {
241138569Ssam			rix = sc->sc_rixmap[
242138569Ssam				ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
243138569Ssam			on->on_tx_rate2 = rt->info[rix].rateCode;
244138569Ssam			on->on_tx_rate2sp = on->on_tx_rate2 |
245138569Ssam				rt->info[rix].shortPreamble;
246138569Ssam		} else {
247138569Ssam			on->on_tx_rate2 = on->on_tx_rate2sp = 0;
248138569Ssam		}
249138569Ssam		if (rate > 0) {
250138569Ssam			/* NB: only do this if we didn't already do it above */
251138569Ssam			on->on_tx_rate3 = rt->info[0].rateCode;
252138569Ssam			on->on_tx_rate3sp =
253155477Ssam				on->on_tx_rate3 | rt->info[0].shortPreamble;
254138569Ssam		} else {
255138569Ssam			on->on_tx_rate3 = on->on_tx_rate3sp = 0;
256138569Ssam		}
257138569Ssam	} else {
258138569Ssam		on->on_tx_try0 = ATH_TXMAXTRY;	/* max tries at rate 0 */
259138569Ssam		on->on_tx_rate1 = on->on_tx_rate1sp = 0;
260138569Ssam		on->on_tx_rate2 = on->on_tx_rate2sp = 0;
261138569Ssam		on->on_tx_rate3 = on->on_tx_rate3sp = 0;
262138569Ssam	}
263138569Ssamdone:
264138569Ssam	on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
265178354Ssam
266178354Ssam	on->on_interval = ath_rateinterval;
267178354Ssam	if (vap->iv_opmode == IEEE80211_M_STA)
268178354Ssam		on->on_interval /= 2;
269178354Ssam	on->on_interval = (on->on_interval * hz) / 1000;
270138569Ssam}
271138569Ssam
272138569Ssam/*
273138569Ssam * Set the starting transmit rate for a node.
274138569Ssam */
275138569Ssamstatic void
276138569Ssamath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
277138569Ssam{
278138569Ssam#define	RATE(_ix)	(ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
279184347Ssam	const struct ieee80211_txparam *tp = ni->ni_txparms;
280138569Ssam	int srate;
281138569Ssam
282138569Ssam	KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
283178354Ssam	if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
284138569Ssam		/*
285138569Ssam		 * No fixed rate is requested. For 11b start with
286138569Ssam		 * the highest negotiated rate; otherwise, for 11g
287138569Ssam		 * and 11a, we start "in the middle" at 24Mb or 36Mb.
288138569Ssam		 */
289138569Ssam		srate = ni->ni_rates.rs_nrates - 1;
290138569Ssam		if (sc->sc_curmode != IEEE80211_MODE_11B) {
291138569Ssam			/*
292138569Ssam			 * Scan the negotiated rate set to find the
293138569Ssam			 * closest rate.
294138569Ssam			 */
295138569Ssam			/* NB: the rate set is assumed sorted */
296138569Ssam			for (; srate >= 0 && RATE(srate) > 72; srate--)
297138569Ssam				;
298138569Ssam		}
299138569Ssam	} else {
300138569Ssam		/*
301170530Ssam		 * A fixed rate is to be used; ic_fixed_rate is the
302170530Ssam		 * IEEE code for this rate (sans basic bit).  Convert this
303138569Ssam		 * to the index into the negotiated rate set for
304138569Ssam		 * the node.  We know the rate is there because the
305138569Ssam		 * rate set is checked when the station associates.
306138569Ssam		 */
307138569Ssam		/* NB: the rate set is assumed sorted */
308138569Ssam		srate = ni->ni_rates.rs_nrates - 1;
309178354Ssam		for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
310138569Ssam			;
311138569Ssam	}
312170530Ssam	/*
313170530Ssam	 * The selected rate may not be available due to races
314170530Ssam	 * and mode settings.  Also orphaned nodes created in
315170530Ssam	 * adhoc mode may not have any rate set so this lookup
316170530Ssam	 * can fail.  This is not fatal.
317170530Ssam	 */
318170530Ssam	ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
319138569Ssam#undef RATE
320138569Ssam}
321138569Ssam
322138569Ssam/*
323138569Ssam * Examine and potentially adjust the transmit rate.
324138569Ssam */
325138569Ssamstatic void
326138569Ssamath_rate_ctl(void *arg, struct ieee80211_node *ni)
327138569Ssam{
328138569Ssam	struct ath_softc *sc = arg;
329138569Ssam	struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
330138569Ssam	struct ieee80211_rateset *rs = &ni->ni_rates;
331138569Ssam	int dir = 0, nrate, enough;
332138569Ssam
333138569Ssam	/*
334138569Ssam	 * Rate control
335138569Ssam	 * XXX: very primitive version.
336138569Ssam	 */
337138569Ssam	enough = (on->on_tx_ok + on->on_tx_err >= 10);
338138569Ssam
339138569Ssam	/* no packet reached -> down */
340138569Ssam	if (on->on_tx_err > 0 && on->on_tx_ok == 0)
341138569Ssam		dir = -1;
342138569Ssam
343138569Ssam	/* all packets needs retry in average -> down */
344138569Ssam	if (enough && on->on_tx_ok < on->on_tx_retr)
345138569Ssam		dir = -1;
346138569Ssam
347138569Ssam	/* no error and less than rate_raise% of packets need retry -> up */
348138569Ssam	if (enough && on->on_tx_err == 0 &&
349138569Ssam	    on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
350138569Ssam		dir = 1;
351138569Ssam
352178354Ssam	IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
353178354Ssam	    "ok %d err %d retr %d upper %d dir %d",
354178354Ssam	    on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
355138569Ssam
356178354Ssam	nrate = on->on_rix;
357138569Ssam	switch (dir) {
358138569Ssam	case 0:
359138569Ssam		if (enough && on->on_tx_upper > 0)
360138569Ssam			on->on_tx_upper--;
361138569Ssam		break;
362138569Ssam	case -1:
363138569Ssam		if (nrate > 0) {
364138569Ssam			nrate--;
365138569Ssam			sc->sc_stats.ast_rate_drop++;
366138569Ssam		}
367138569Ssam		on->on_tx_upper = 0;
368138569Ssam		break;
369138569Ssam	case 1:
370138569Ssam		/* raise rate if we hit rate_raise_threshold */
371138569Ssam		if (++on->on_tx_upper < ath_rate_raise_threshold)
372138569Ssam			break;
373138569Ssam		on->on_tx_upper = 0;
374138569Ssam		if (nrate + 1 < rs->rs_nrates) {
375138569Ssam			nrate++;
376138569Ssam			sc->sc_stats.ast_rate_raise++;
377138569Ssam		}
378138569Ssam		break;
379138569Ssam	}
380138569Ssam
381178354Ssam	if (nrate != on->on_rix) {
382178354Ssam		IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
383178354Ssam		    "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
384178354Ssam		    ni->ni_txrate / 2,
385138569Ssam		    (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
386138569Ssam		    on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
387138569Ssam		ath_rate_update(sc, ni, nrate);
388138569Ssam	} else if (enough)
389138569Ssam		on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
390138569Ssam}
391138569Ssam
392138569Ssamstatic void
393138569Ssamath_rate_sysctlattach(struct ath_softc *sc)
394138569Ssam{
395138569Ssam	struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
396138569Ssam	struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
397138569Ssam
398138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
399138569Ssam		"rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
400138569Ssam		"rate control: operation interval (ms)");
401138569Ssam	/* XXX bounds check values */
402138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
403138569Ssam		"rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
404138569Ssam		"rate control: retry threshold to credit rate raise (%%)");
405138569Ssam	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
406138569Ssam		"rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
407138569Ssam		"rate control: # good periods before raising rate");
408138569Ssam}
409138569Ssam
410238633Sadrianstatic int
411238633Sadrianath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
412238633Sadrian    struct ath_rateioctl *re)
413238633Sadrian{
414238633Sadrian
415238633Sadrian	return (EINVAL);
416238633Sadrian}
417238633Sadrian
418138569Ssamstruct ath_ratectrl *
419138569Ssamath_rate_attach(struct ath_softc *sc)
420138569Ssam{
421138569Ssam	struct onoe_softc *osc;
422138569Ssam
423138569Ssam	osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
424138569Ssam	if (osc == NULL)
425138569Ssam		return NULL;
426138569Ssam	osc->arc.arc_space = sizeof(struct onoe_node);
427138569Ssam	ath_rate_sysctlattach(sc);
428138569Ssam
429138569Ssam	return &osc->arc;
430138569Ssam}
431138569Ssam
432138569Ssamvoid
433138569Ssamath_rate_detach(struct ath_ratectrl *arc)
434138569Ssam{
435138569Ssam	struct onoe_softc *osc = (struct onoe_softc *) arc;
436138569Ssam
437138569Ssam	free(osc, M_DEVBUF);
438138569Ssam}
439