105f1e03fSSam Leffler /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4515d617eSSam Leffler * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
505f1e03fSSam Leffler * All rights reserved.
605f1e03fSSam Leffler *
705f1e03fSSam Leffler * Redistribution and use in source and binary forms, with or without
805f1e03fSSam Leffler * modification, are permitted provided that the following conditions
905f1e03fSSam Leffler * are met:
1005f1e03fSSam Leffler * 1. Redistributions of source code must retain the above copyright
1105f1e03fSSam Leffler * notice, this list of conditions and the following disclaimer,
1205f1e03fSSam Leffler * without modification.
1305f1e03fSSam Leffler * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1405f1e03fSSam Leffler * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1505f1e03fSSam Leffler * redistribution must be conditioned upon including a substantially
1605f1e03fSSam Leffler * similar Disclaimer requirement for further binary redistribution.
1705f1e03fSSam Leffler *
1805f1e03fSSam Leffler * NO WARRANTY
1905f1e03fSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2005f1e03fSSam Leffler * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2105f1e03fSSam Leffler * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
2205f1e03fSSam Leffler * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2305f1e03fSSam Leffler * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
2405f1e03fSSam Leffler * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2505f1e03fSSam Leffler * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2605f1e03fSSam Leffler * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
2705f1e03fSSam Leffler * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2805f1e03fSSam Leffler * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2905f1e03fSSam Leffler * THE POSSIBILITY OF SUCH DAMAGES.
3005f1e03fSSam Leffler */
3105f1e03fSSam Leffler
3205f1e03fSSam Leffler #include <sys/cdefs.h>
3305f1e03fSSam Leffler /*
3405f1e03fSSam Leffler * Atsushi Onoe's rate control algorithm.
3505f1e03fSSam Leffler */
36c312fb4aSAdrian Chadd #include "opt_ath.h"
3705f1e03fSSam Leffler #include "opt_inet.h"
38b032f27cSSam Leffler #include "opt_wlan.h"
3905f1e03fSSam Leffler
4005f1e03fSSam Leffler #include <sys/param.h>
4105f1e03fSSam Leffler #include <sys/systm.h>
4205f1e03fSSam Leffler #include <sys/sysctl.h>
4305f1e03fSSam Leffler #include <sys/kernel.h>
4405f1e03fSSam Leffler #include <sys/lock.h>
4505f1e03fSSam Leffler #include <sys/mutex.h>
4605f1e03fSSam Leffler #include <sys/errno.h>
4705f1e03fSSam Leffler
4805f1e03fSSam Leffler #include <machine/bus.h>
4905f1e03fSSam Leffler #include <machine/resource.h>
5005f1e03fSSam Leffler #include <sys/bus.h>
5105f1e03fSSam Leffler
5205f1e03fSSam Leffler #include <sys/socket.h>
5305f1e03fSSam Leffler
5405f1e03fSSam Leffler #include <net/if.h>
5505f1e03fSSam Leffler #include <net/if_media.h>
5605f1e03fSSam Leffler #include <net/if_arp.h>
5705f1e03fSSam Leffler #include <net/ethernet.h> /* XXX for ether_sprintf */
5805f1e03fSSam Leffler
5905f1e03fSSam Leffler #include <net80211/ieee80211_var.h>
6005f1e03fSSam Leffler
6105f1e03fSSam Leffler #include <net/bpf.h>
6205f1e03fSSam Leffler
6305f1e03fSSam Leffler #ifdef INET
6405f1e03fSSam Leffler #include <netinet/in.h>
6505f1e03fSSam Leffler #include <netinet/if_ether.h>
6605f1e03fSSam Leffler #endif
6705f1e03fSSam Leffler
6805f1e03fSSam Leffler #include <dev/ath/if_athvar.h>
6905f1e03fSSam Leffler #include <dev/ath/ath_rate/onoe/onoe.h>
7033644623SSam Leffler #include <dev/ath/ath_hal/ah_desc.h>
7105f1e03fSSam Leffler
7205f1e03fSSam Leffler /*
7305f1e03fSSam Leffler * Default parameters for the rate control algorithm. These are
7405f1e03fSSam Leffler * all tunable with sysctls. The rate controller runs periodically
7505f1e03fSSam Leffler * (each ath_rateinterval ms) analyzing transmit statistics for each
7605f1e03fSSam Leffler * neighbor/station (when operating in station mode this is only the AP).
7705f1e03fSSam Leffler * If transmits look to be working well over a sampling period then
7805f1e03fSSam Leffler * it gives a "raise rate credit". If transmits look to not be working
7905f1e03fSSam Leffler * well than it deducts a credit. If the credits cross a threshold then
8005f1e03fSSam Leffler * the transmit rate is raised. Various error conditions force the
8105f1e03fSSam Leffler * the transmit rate to be dropped.
8205f1e03fSSam Leffler *
8305f1e03fSSam Leffler * The decision to issue/deduct a credit is based on the errors and
8405f1e03fSSam Leffler * retries accumulated over the sampling period. ath_rate_raise defines
8505f1e03fSSam Leffler * the percent of retransmits for which a credit is issued/deducted.
8605f1e03fSSam Leffler * ath_rate_raise_threshold defines the threshold on credits at which
8705f1e03fSSam Leffler * the transmit rate is increased.
8805f1e03fSSam Leffler *
8905f1e03fSSam Leffler * XXX this algorithm is flawed.
9005f1e03fSSam Leffler */
9105f1e03fSSam Leffler static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
9205f1e03fSSam Leffler static int ath_rate_raise = 10; /* add credit threshold */
9305f1e03fSSam Leffler static int ath_rate_raise_threshold = 10; /* rate ctl raise threshold */
9405f1e03fSSam Leffler
9505f1e03fSSam Leffler static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
9605f1e03fSSam Leffler int rate);
9705f1e03fSSam Leffler static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
9805f1e03fSSam Leffler static void ath_rate_ctl(void *, struct ieee80211_node *);
9905f1e03fSSam Leffler
10005f1e03fSSam Leffler void
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)10105f1e03fSSam Leffler ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
10205f1e03fSSam Leffler {
10305f1e03fSSam Leffler /* NB: assumed to be zero'd by caller */
10405f1e03fSSam Leffler }
10505f1e03fSSam Leffler
10605f1e03fSSam Leffler void
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)10705f1e03fSSam Leffler ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
10805f1e03fSSam Leffler {
10905f1e03fSSam Leffler }
11005f1e03fSSam Leffler
11105f1e03fSSam Leffler void
ath_rate_findrate(struct ath_softc * sc,struct ath_node * an,int shortPreamble,size_t frameLen,int tid,int is_aggr,u_int8_t * rix,int * try0,u_int8_t * txrate,int * maxdur,int * maxpktlen)11205f1e03fSSam Leffler ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
113cce63444SAdrian Chadd int shortPreamble, size_t frameLen, int tid, int is_aggr,
114cce63444SAdrian Chadd u_int8_t *rix, int *try0, u_int8_t *txrate, int *maxdur,
115cce63444SAdrian Chadd int *maxpktlen)
11605f1e03fSSam Leffler {
11705f1e03fSSam Leffler struct onoe_node *on = ATH_NODE_ONOE(an);
11805f1e03fSSam Leffler
11905f1e03fSSam Leffler *rix = on->on_tx_rix0;
12005f1e03fSSam Leffler *try0 = on->on_tx_try0;
12105f1e03fSSam Leffler if (shortPreamble)
12205f1e03fSSam Leffler *txrate = on->on_tx_rate0sp;
12305f1e03fSSam Leffler else
12405f1e03fSSam Leffler *txrate = on->on_tx_rate0;
12584f950a5SAdrian Chadd *maxdur = -1;
126cce63444SAdrian Chadd *maxpktlen = -1;
12705f1e03fSSam Leffler }
12805f1e03fSSam Leffler
129710c3778SAdrian Chadd /*
130710c3778SAdrian Chadd * Get the TX rates.
131710c3778SAdrian Chadd *
132710c3778SAdrian Chadd * The short preamble bits aren't set here; the caller should augment
133710c3778SAdrian Chadd * the returned rate with the relevant preamble rate flag.
134710c3778SAdrian Chadd */
135710c3778SAdrian Chadd void
ath_rate_getxtxrates(struct ath_softc * sc,struct ath_node * an,uint8_t rix0,int is_aggr,struct ath_rc_series * rc)136710c3778SAdrian Chadd ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
137051ea90cSAdrian Chadd uint8_t rix0, int is_aggr, struct ath_rc_series *rc)
138710c3778SAdrian Chadd {
139710c3778SAdrian Chadd struct onoe_node *on = ATH_NODE_ONOE(an);
140710c3778SAdrian Chadd
141eb6f0de0SAdrian Chadd rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
142710c3778SAdrian Chadd
143eb6f0de0SAdrian Chadd rc[0].rix = on->on_tx_rate0;
144eb6f0de0SAdrian Chadd rc[1].rix = on->on_tx_rate1;
145eb6f0de0SAdrian Chadd rc[2].rix = on->on_tx_rate2;
146eb6f0de0SAdrian Chadd rc[3].rix = on->on_tx_rate3;
147eb6f0de0SAdrian Chadd
148eb6f0de0SAdrian Chadd rc[0].tries = on->on_tx_try0;
149eb6f0de0SAdrian Chadd rc[1].tries = 2;
150eb6f0de0SAdrian Chadd rc[2].tries = 2;
151eb6f0de0SAdrian Chadd rc[3].tries = 2;
152710c3778SAdrian Chadd }
153710c3778SAdrian Chadd
15405f1e03fSSam Leffler void
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)15505f1e03fSSam Leffler ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
156a4d8dd10SSam Leffler struct ath_desc *ds, int shortPreamble, u_int8_t rix)
15705f1e03fSSam Leffler {
15805f1e03fSSam Leffler struct onoe_node *on = ATH_NODE_ONOE(an);
15905f1e03fSSam Leffler
16005f1e03fSSam Leffler ath_hal_setupxtxdesc(sc->sc_ah, ds
16105f1e03fSSam Leffler , on->on_tx_rate1sp, 2 /* series 1 */
16205f1e03fSSam Leffler , on->on_tx_rate2sp, 2 /* series 2 */
16305f1e03fSSam Leffler , on->on_tx_rate3sp, 2 /* series 3 */
16405f1e03fSSam Leffler );
16505f1e03fSSam Leffler }
16605f1e03fSSam Leffler
16705f1e03fSSam Leffler void
ath_rate_tx_complete(struct ath_softc * sc,struct ath_node * an,const struct ath_rc_series * rc,const struct ath_tx_status * ts,int frame_size,int rc_framesize,int nframes,int nbad)16822233301SSam Leffler ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
169eb6f0de0SAdrian Chadd const struct ath_rc_series *rc, const struct ath_tx_status *ts,
170cce63444SAdrian Chadd int frame_size, int rc_framesize, int nframes, int nbad)
17105f1e03fSSam Leffler {
17205f1e03fSSam Leffler struct onoe_node *on = ATH_NODE_ONOE(an);
17305f1e03fSSam Leffler
17465f9edeeSSam Leffler if (ts->ts_status == 0)
17505f1e03fSSam Leffler on->on_tx_ok++;
17605f1e03fSSam Leffler else
17705f1e03fSSam Leffler on->on_tx_err++;
17865f9edeeSSam Leffler on->on_tx_retr += ts->ts_shortretry
17965f9edeeSSam Leffler + ts->ts_longretry;
180b032f27cSSam Leffler if (on->on_interval != 0 && ticks - on->on_ticks > on->on_interval) {
181b032f27cSSam Leffler ath_rate_ctl(sc, &an->an_node);
182b032f27cSSam Leffler on->on_ticks = ticks;
183b032f27cSSam Leffler }
18405f1e03fSSam Leffler }
18505f1e03fSSam Leffler
18605f1e03fSSam Leffler void
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)18705f1e03fSSam Leffler ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
18805f1e03fSSam Leffler {
18905f1e03fSSam Leffler if (isnew)
19005f1e03fSSam Leffler ath_rate_ctl_start(sc, &an->an_node);
19105f1e03fSSam Leffler }
19205f1e03fSSam Leffler
1937d450faaSAdrian Chadd void
ath_rate_update_rx_rssi(struct ath_softc * sc,struct ath_node * an,int rssi)1947d450faaSAdrian Chadd ath_rate_update_rx_rssi(struct ath_softc *sc, struct ath_node *an, int rssi)
1957d450faaSAdrian Chadd {
1967d450faaSAdrian Chadd }
1977d450faaSAdrian Chadd
19805f1e03fSSam Leffler static void
ath_rate_update(struct ath_softc * sc,struct ieee80211_node * ni,int rate)19905f1e03fSSam Leffler ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
20005f1e03fSSam Leffler {
20105f1e03fSSam Leffler struct ath_node *an = ATH_NODE(ni);
20205f1e03fSSam Leffler struct onoe_node *on = ATH_NODE_ONOE(an);
203b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap;
20405f1e03fSSam Leffler const HAL_RATE_TABLE *rt = sc->sc_currates;
20505f1e03fSSam Leffler u_int8_t rix;
206*70674500SAdrian Chadd uint8_t dot11rate;
20705f1e03fSSam Leffler
20805f1e03fSSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
20905f1e03fSSam Leffler
210b032f27cSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
211b032f27cSSam Leffler "%s: set xmit rate to %dM", __func__,
21205f1e03fSSam Leffler ni->ni_rates.rs_nrates > 0 ?
21305f1e03fSSam Leffler (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
21405f1e03fSSam Leffler
21505f1e03fSSam Leffler /*
21605f1e03fSSam Leffler * Before associating a node has no rate set setup
21705f1e03fSSam Leffler * so we can't calculate any transmit codes to use.
21805f1e03fSSam Leffler * This is ok since we should never be sending anything
21905f1e03fSSam Leffler * but management frames and those always go at the
22005f1e03fSSam Leffler * lowest hardware rate.
22105f1e03fSSam Leffler */
22205f1e03fSSam Leffler if (ni->ni_rates.rs_nrates == 0)
22305f1e03fSSam Leffler goto done;
224b032f27cSSam Leffler on->on_rix = rate;
225*70674500SAdrian Chadd dot11rate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
226*70674500SAdrian Chadd ieee80211_node_set_txrate_dot11rate(ni, dot11rate);
227*70674500SAdrian Chadd on->on_tx_rix0 = sc->sc_rixmap[dot11rate];
22805f1e03fSSam Leffler on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
22905f1e03fSSam Leffler
23005f1e03fSSam Leffler on->on_tx_rate0sp = on->on_tx_rate0 |
23105f1e03fSSam Leffler rt->info[on->on_tx_rix0].shortPreamble;
23205f1e03fSSam Leffler if (sc->sc_mrretry) {
23305f1e03fSSam Leffler /*
23405f1e03fSSam Leffler * Hardware supports multi-rate retry; setup two
23505f1e03fSSam Leffler * step-down retry rates and make the lowest rate
23605f1e03fSSam Leffler * be the ``last chance''. We use 4, 2, 2, 2 tries
23705f1e03fSSam Leffler * respectively (4 is set here, the rest are fixed
23805f1e03fSSam Leffler * in the xmit routine).
23905f1e03fSSam Leffler */
24005f1e03fSSam Leffler on->on_tx_try0 = 1 + 3; /* 4 tries at rate 0 */
24105f1e03fSSam Leffler if (--rate >= 0) {
24205f1e03fSSam Leffler rix = sc->sc_rixmap[
24305f1e03fSSam Leffler ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
24405f1e03fSSam Leffler on->on_tx_rate1 = rt->info[rix].rateCode;
24505f1e03fSSam Leffler on->on_tx_rate1sp = on->on_tx_rate1 |
24605f1e03fSSam Leffler rt->info[rix].shortPreamble;
24705f1e03fSSam Leffler } else {
24805f1e03fSSam Leffler on->on_tx_rate1 = on->on_tx_rate1sp = 0;
24905f1e03fSSam Leffler }
25005f1e03fSSam Leffler if (--rate >= 0) {
25105f1e03fSSam Leffler rix = sc->sc_rixmap[
25205f1e03fSSam Leffler ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
25305f1e03fSSam Leffler on->on_tx_rate2 = rt->info[rix].rateCode;
25405f1e03fSSam Leffler on->on_tx_rate2sp = on->on_tx_rate2 |
25505f1e03fSSam Leffler rt->info[rix].shortPreamble;
25605f1e03fSSam Leffler } else {
25705f1e03fSSam Leffler on->on_tx_rate2 = on->on_tx_rate2sp = 0;
25805f1e03fSSam Leffler }
25905f1e03fSSam Leffler if (rate > 0) {
26005f1e03fSSam Leffler /* NB: only do this if we didn't already do it above */
26105f1e03fSSam Leffler on->on_tx_rate3 = rt->info[0].rateCode;
26205f1e03fSSam Leffler on->on_tx_rate3sp =
26355f63772SSam Leffler on->on_tx_rate3 | rt->info[0].shortPreamble;
26405f1e03fSSam Leffler } else {
26505f1e03fSSam Leffler on->on_tx_rate3 = on->on_tx_rate3sp = 0;
26605f1e03fSSam Leffler }
26705f1e03fSSam Leffler } else {
26805f1e03fSSam Leffler on->on_tx_try0 = ATH_TXMAXTRY; /* max tries at rate 0 */
26905f1e03fSSam Leffler on->on_tx_rate1 = on->on_tx_rate1sp = 0;
27005f1e03fSSam Leffler on->on_tx_rate2 = on->on_tx_rate2sp = 0;
27105f1e03fSSam Leffler on->on_tx_rate3 = on->on_tx_rate3sp = 0;
27205f1e03fSSam Leffler }
27305f1e03fSSam Leffler done:
27405f1e03fSSam Leffler on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
275b032f27cSSam Leffler
276b032f27cSSam Leffler on->on_interval = ath_rateinterval;
277b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_STA)
278b032f27cSSam Leffler on->on_interval /= 2;
279b032f27cSSam Leffler on->on_interval = (on->on_interval * hz) / 1000;
28005f1e03fSSam Leffler }
28105f1e03fSSam Leffler
28205f1e03fSSam Leffler /*
28305f1e03fSSam Leffler * Set the starting transmit rate for a node.
28405f1e03fSSam Leffler */
28505f1e03fSSam Leffler static void
ath_rate_ctl_start(struct ath_softc * sc,struct ieee80211_node * ni)28605f1e03fSSam Leffler ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
28705f1e03fSSam Leffler {
28805f1e03fSSam Leffler #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
289c62362cbSSam Leffler const struct ieee80211_txparam *tp = ni->ni_txparms;
29005f1e03fSSam Leffler int srate;
29105f1e03fSSam Leffler
29205f1e03fSSam Leffler KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
293b032f27cSSam Leffler if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
29405f1e03fSSam Leffler /*
29505f1e03fSSam Leffler * No fixed rate is requested. For 11b start with
29605f1e03fSSam Leffler * the highest negotiated rate; otherwise, for 11g
29705f1e03fSSam Leffler * and 11a, we start "in the middle" at 24Mb or 36Mb.
29805f1e03fSSam Leffler */
29905f1e03fSSam Leffler srate = ni->ni_rates.rs_nrates - 1;
30005f1e03fSSam Leffler if (sc->sc_curmode != IEEE80211_MODE_11B) {
30105f1e03fSSam Leffler /*
30205f1e03fSSam Leffler * Scan the negotiated rate set to find the
30305f1e03fSSam Leffler * closest rate.
30405f1e03fSSam Leffler */
30505f1e03fSSam Leffler /* NB: the rate set is assumed sorted */
30605f1e03fSSam Leffler for (; srate >= 0 && RATE(srate) > 72; srate--)
30705f1e03fSSam Leffler ;
30805f1e03fSSam Leffler }
30905f1e03fSSam Leffler } else {
31005f1e03fSSam Leffler /*
31168e8e04eSSam Leffler * A fixed rate is to be used; ic_fixed_rate is the
31268e8e04eSSam Leffler * IEEE code for this rate (sans basic bit). Convert this
31305f1e03fSSam Leffler * to the index into the negotiated rate set for
31405f1e03fSSam Leffler * the node. We know the rate is there because the
31505f1e03fSSam Leffler * rate set is checked when the station associates.
31605f1e03fSSam Leffler */
31705f1e03fSSam Leffler /* NB: the rate set is assumed sorted */
31805f1e03fSSam Leffler srate = ni->ni_rates.rs_nrates - 1;
319b032f27cSSam Leffler for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
32005f1e03fSSam Leffler ;
32105f1e03fSSam Leffler }
32268e8e04eSSam Leffler /*
32368e8e04eSSam Leffler * The selected rate may not be available due to races
32468e8e04eSSam Leffler * and mode settings. Also orphaned nodes created in
32568e8e04eSSam Leffler * adhoc mode may not have any rate set so this lookup
32668e8e04eSSam Leffler * can fail. This is not fatal.
32768e8e04eSSam Leffler */
32868e8e04eSSam Leffler ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
32905f1e03fSSam Leffler #undef RATE
33005f1e03fSSam Leffler }
33105f1e03fSSam Leffler
33205f1e03fSSam Leffler /*
33305f1e03fSSam Leffler * Examine and potentially adjust the transmit rate.
33405f1e03fSSam Leffler */
33505f1e03fSSam Leffler static void
ath_rate_ctl(void * arg,struct ieee80211_node * ni)33605f1e03fSSam Leffler ath_rate_ctl(void *arg, struct ieee80211_node *ni)
33705f1e03fSSam Leffler {
33805f1e03fSSam Leffler struct ath_softc *sc = arg;
33905f1e03fSSam Leffler struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
34005f1e03fSSam Leffler struct ieee80211_rateset *rs = &ni->ni_rates;
34105f1e03fSSam Leffler int dir = 0, nrate, enough;
34205f1e03fSSam Leffler
34305f1e03fSSam Leffler /*
34405f1e03fSSam Leffler * Rate control
34505f1e03fSSam Leffler * XXX: very primitive version.
34605f1e03fSSam Leffler */
34705f1e03fSSam Leffler enough = (on->on_tx_ok + on->on_tx_err >= 10);
34805f1e03fSSam Leffler
34905f1e03fSSam Leffler /* no packet reached -> down */
35005f1e03fSSam Leffler if (on->on_tx_err > 0 && on->on_tx_ok == 0)
35105f1e03fSSam Leffler dir = -1;
35205f1e03fSSam Leffler
35305f1e03fSSam Leffler /* all packets needs retry in average -> down */
35405f1e03fSSam Leffler if (enough && on->on_tx_ok < on->on_tx_retr)
35505f1e03fSSam Leffler dir = -1;
35605f1e03fSSam Leffler
35705f1e03fSSam Leffler /* no error and less than rate_raise% of packets need retry -> up */
35805f1e03fSSam Leffler if (enough && on->on_tx_err == 0 &&
35905f1e03fSSam Leffler on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
36005f1e03fSSam Leffler dir = 1;
36105f1e03fSSam Leffler
362b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
363b032f27cSSam Leffler "ok %d err %d retr %d upper %d dir %d",
364b032f27cSSam Leffler on->on_tx_ok, on->on_tx_err, on->on_tx_retr, on->on_tx_upper, dir);
36505f1e03fSSam Leffler
366b032f27cSSam Leffler nrate = on->on_rix;
36705f1e03fSSam Leffler switch (dir) {
36805f1e03fSSam Leffler case 0:
36905f1e03fSSam Leffler if (enough && on->on_tx_upper > 0)
37005f1e03fSSam Leffler on->on_tx_upper--;
37105f1e03fSSam Leffler break;
37205f1e03fSSam Leffler case -1:
37305f1e03fSSam Leffler if (nrate > 0) {
37405f1e03fSSam Leffler nrate--;
37505f1e03fSSam Leffler sc->sc_stats.ast_rate_drop++;
37605f1e03fSSam Leffler }
37705f1e03fSSam Leffler on->on_tx_upper = 0;
37805f1e03fSSam Leffler break;
37905f1e03fSSam Leffler case 1:
38005f1e03fSSam Leffler /* raise rate if we hit rate_raise_threshold */
38105f1e03fSSam Leffler if (++on->on_tx_upper < ath_rate_raise_threshold)
38205f1e03fSSam Leffler break;
38305f1e03fSSam Leffler on->on_tx_upper = 0;
38405f1e03fSSam Leffler if (nrate + 1 < rs->rs_nrates) {
38505f1e03fSSam Leffler nrate++;
38605f1e03fSSam Leffler sc->sc_stats.ast_rate_raise++;
38705f1e03fSSam Leffler }
38805f1e03fSSam Leffler break;
38905f1e03fSSam Leffler }
39005f1e03fSSam Leffler
391b032f27cSSam Leffler if (nrate != on->on_rix) {
392b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
393b032f27cSSam Leffler "%s: %dM -> %dM (%d ok, %d err, %d retr)", __func__,
394*70674500SAdrian Chadd ieee80211_node_get_txrate_kbit(ni) / 1000,
39505f1e03fSSam Leffler (rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
39605f1e03fSSam Leffler on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
39705f1e03fSSam Leffler ath_rate_update(sc, ni, nrate);
39805f1e03fSSam Leffler } else if (enough)
39905f1e03fSSam Leffler on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
40005f1e03fSSam Leffler }
40105f1e03fSSam Leffler
40205f1e03fSSam Leffler static void
ath_rate_sysctlattach(struct ath_softc * sc)40305f1e03fSSam Leffler ath_rate_sysctlattach(struct ath_softc *sc)
40405f1e03fSSam Leffler {
40505f1e03fSSam Leffler struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
40605f1e03fSSam Leffler struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
40705f1e03fSSam Leffler
40805f1e03fSSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
40905f1e03fSSam Leffler "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
41005f1e03fSSam Leffler "rate control: operation interval (ms)");
41105f1e03fSSam Leffler /* XXX bounds check values */
41205f1e03fSSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
41305f1e03fSSam Leffler "rate_raise", CTLFLAG_RW, &ath_rate_raise, 0,
41405f1e03fSSam Leffler "rate control: retry threshold to credit rate raise (%%)");
41505f1e03fSSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
41605f1e03fSSam Leffler "rate_raise_threshold", CTLFLAG_RW, &ath_rate_raise_threshold,0,
41705f1e03fSSam Leffler "rate control: # good periods before raising rate");
41805f1e03fSSam Leffler }
41905f1e03fSSam Leffler
4202d20d655SAdrian Chadd static int
ath_rate_fetch_node_stats(struct ath_softc * sc,struct ath_node * an,struct ath_rateioctl * re)4212d20d655SAdrian Chadd ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
4222d20d655SAdrian Chadd struct ath_rateioctl *re)
4232d20d655SAdrian Chadd {
4242d20d655SAdrian Chadd
4252d20d655SAdrian Chadd return (EINVAL);
4262d20d655SAdrian Chadd }
4272d20d655SAdrian Chadd
42805f1e03fSSam Leffler struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)42905f1e03fSSam Leffler ath_rate_attach(struct ath_softc *sc)
43005f1e03fSSam Leffler {
43105f1e03fSSam Leffler struct onoe_softc *osc;
43205f1e03fSSam Leffler
43305f1e03fSSam Leffler osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
43405f1e03fSSam Leffler if (osc == NULL)
43505f1e03fSSam Leffler return NULL;
43605f1e03fSSam Leffler osc->arc.arc_space = sizeof(struct onoe_node);
43705f1e03fSSam Leffler ath_rate_sysctlattach(sc);
43805f1e03fSSam Leffler
43905f1e03fSSam Leffler return &osc->arc;
44005f1e03fSSam Leffler }
44105f1e03fSSam Leffler
44205f1e03fSSam Leffler void
ath_rate_detach(struct ath_ratectrl * arc)44305f1e03fSSam Leffler ath_rate_detach(struct ath_ratectrl *arc)
44405f1e03fSSam Leffler {
44505f1e03fSSam Leffler struct onoe_softc *osc = (struct onoe_softc *) arc;
44605f1e03fSSam Leffler
44705f1e03fSSam Leffler free(osc, M_DEVBUF);
44805f1e03fSSam Leffler }
449