105f1e03fSSam Leffler /*-
2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3718cf2ccSPedro F. Giffuni *
405f1e03fSSam Leffler * Copyright (c) 2004 INRIA
51f1d7810SSam Leffler * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
605f1e03fSSam Leffler * All rights reserved.
705f1e03fSSam Leffler *
805f1e03fSSam Leffler * Redistribution and use in source and binary forms, with or without
905f1e03fSSam Leffler * modification, are permitted provided that the following conditions
1005f1e03fSSam Leffler * are met:
1105f1e03fSSam Leffler * 1. Redistributions of source code must retain the above copyright
1205f1e03fSSam Leffler * notice, this list of conditions and the following disclaimer,
1305f1e03fSSam Leffler * without modification.
1405f1e03fSSam Leffler * 2. Redistributions in binary form must reproduce at minimum a disclaimer
1505f1e03fSSam Leffler * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
1605f1e03fSSam Leffler * redistribution must be conditioned upon including a substantially
1705f1e03fSSam Leffler * similar Disclaimer requirement for further binary redistribution.
1805f1e03fSSam Leffler * 3. Neither the names of the above-listed copyright holders nor the names
1905f1e03fSSam Leffler * of any contributors may be used to endorse or promote products derived
2005f1e03fSSam Leffler * from this software without specific prior written permission.
2105f1e03fSSam Leffler *
2205f1e03fSSam Leffler * Alternatively, this software may be distributed under the terms of the
2305f1e03fSSam Leffler * GNU General Public License ("GPL") version 2 as published by the Free
2405f1e03fSSam Leffler * Software Foundation.
2505f1e03fSSam Leffler *
2605f1e03fSSam Leffler * NO WARRANTY
2705f1e03fSSam Leffler * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2805f1e03fSSam Leffler * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2905f1e03fSSam Leffler * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
3005f1e03fSSam Leffler * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
3105f1e03fSSam Leffler * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
3205f1e03fSSam Leffler * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3305f1e03fSSam Leffler * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3405f1e03fSSam Leffler * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
3505f1e03fSSam Leffler * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3605f1e03fSSam Leffler * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3705f1e03fSSam Leffler * THE POSSIBILITY OF SUCH DAMAGES.
3805f1e03fSSam Leffler *
3905f1e03fSSam Leffler */
4005f1e03fSSam Leffler
4105f1e03fSSam Leffler #include <sys/cdefs.h>
4205f1e03fSSam Leffler /*
4305f1e03fSSam Leffler * AMRR rate control. See:
4405f1e03fSSam Leffler * http://www-sop.inria.fr/rapports/sophia/RR-5208.html
4505f1e03fSSam Leffler * "IEEE 802.11 Rate Adaptation: A Practical Approach" by
4605f1e03fSSam Leffler * Mathieu Lacage, Hossein Manshaei, Thierry Turletti
4705f1e03fSSam Leffler */
48c312fb4aSAdrian Chadd #include "opt_ath.h"
4905f1e03fSSam Leffler #include "opt_inet.h"
50b032f27cSSam Leffler #include "opt_wlan.h"
5105f1e03fSSam Leffler
5205f1e03fSSam Leffler #include <sys/param.h>
5305f1e03fSSam Leffler #include <sys/systm.h>
5405f1e03fSSam Leffler #include <sys/sysctl.h>
5505f1e03fSSam Leffler #include <sys/kernel.h>
5605f1e03fSSam Leffler #include <sys/lock.h>
5705f1e03fSSam Leffler #include <sys/mutex.h>
5805f1e03fSSam Leffler #include <sys/errno.h>
5905f1e03fSSam Leffler
6005f1e03fSSam Leffler #include <machine/bus.h>
6105f1e03fSSam Leffler #include <machine/resource.h>
6205f1e03fSSam Leffler #include <sys/bus.h>
6305f1e03fSSam Leffler
6405f1e03fSSam Leffler #include <sys/socket.h>
6505f1e03fSSam Leffler
6605f1e03fSSam Leffler #include <net/if.h>
6705f1e03fSSam Leffler #include <net/if_media.h>
6805f1e03fSSam Leffler #include <net/if_arp.h>
6905f1e03fSSam Leffler
7005f1e03fSSam Leffler #include <net80211/ieee80211_var.h>
7105f1e03fSSam Leffler
7205f1e03fSSam Leffler #include <net/bpf.h>
7305f1e03fSSam Leffler
7405f1e03fSSam Leffler #ifdef INET
7505f1e03fSSam Leffler #include <netinet/in.h>
7605f1e03fSSam Leffler #include <netinet/if_ether.h>
7705f1e03fSSam Leffler #endif
7805f1e03fSSam Leffler
7905f1e03fSSam Leffler #include <dev/ath/if_athvar.h>
8005f1e03fSSam Leffler #include <dev/ath/ath_rate/amrr/amrr.h>
8133644623SSam Leffler #include <dev/ath/ath_hal/ah_desc.h>
8205f1e03fSSam Leffler
8305f1e03fSSam Leffler static int ath_rateinterval = 1000; /* rate ctl interval (ms) */
8405f1e03fSSam Leffler static int ath_rate_max_success_threshold = 10;
8505f1e03fSSam Leffler static int ath_rate_min_success_threshold = 1;
8605f1e03fSSam Leffler
8705f1e03fSSam Leffler static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
8805f1e03fSSam Leffler int rate);
8905f1e03fSSam Leffler static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
9005f1e03fSSam Leffler static void ath_rate_ctl(void *, struct ieee80211_node *);
9105f1e03fSSam Leffler
9205f1e03fSSam Leffler void
ath_rate_node_init(struct ath_softc * sc,struct ath_node * an)9305f1e03fSSam Leffler ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
9405f1e03fSSam Leffler {
9505f1e03fSSam Leffler /* NB: assumed to be zero'd by caller */
9605f1e03fSSam Leffler }
9705f1e03fSSam Leffler
9805f1e03fSSam Leffler void
ath_rate_node_cleanup(struct ath_softc * sc,struct ath_node * an)9905f1e03fSSam Leffler ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
10005f1e03fSSam Leffler {
10105f1e03fSSam Leffler }
10205f1e03fSSam Leffler
10305f1e03fSSam 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)10405f1e03fSSam Leffler ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
105cce63444SAdrian Chadd int shortPreamble, size_t frameLen, int tid, int is_aggr,
106cce63444SAdrian Chadd u_int8_t *rix, int *try0, u_int8_t *txrate, int *maxdur,
107cce63444SAdrian Chadd int *maxpktlen)
10805f1e03fSSam Leffler {
10905f1e03fSSam Leffler struct amrr_node *amn = ATH_NODE_AMRR(an);
11005f1e03fSSam Leffler
11105f1e03fSSam Leffler *rix = amn->amn_tx_rix0;
11205f1e03fSSam Leffler *try0 = amn->amn_tx_try0;
11305f1e03fSSam Leffler if (shortPreamble)
11405f1e03fSSam Leffler *txrate = amn->amn_tx_rate0sp;
11505f1e03fSSam Leffler else
11605f1e03fSSam Leffler *txrate = amn->amn_tx_rate0;
11784f950a5SAdrian Chadd maxdur = -1;
118cce63444SAdrian Chadd maxpktlen = -1;
11905f1e03fSSam Leffler }
12005f1e03fSSam Leffler
121710c3778SAdrian Chadd /*
122710c3778SAdrian Chadd * Get the TX rates.
123710c3778SAdrian Chadd *
124710c3778SAdrian Chadd * The short preamble bits aren't set here; the caller should augment
125710c3778SAdrian Chadd * the returned rate with the relevant preamble rate flag.
126710c3778SAdrian Chadd */
127710c3778SAdrian Chadd void
ath_rate_getxtxrates(struct ath_softc * sc,struct ath_node * an,uint8_t rix0,int is_aggr,struct ath_rc_series * rc)128710c3778SAdrian Chadd ath_rate_getxtxrates(struct ath_softc *sc, struct ath_node *an,
129051ea90cSAdrian Chadd uint8_t rix0, int is_aggr, struct ath_rc_series *rc)
130710c3778SAdrian Chadd {
131710c3778SAdrian Chadd struct amrr_node *amn = ATH_NODE_AMRR(an);
132710c3778SAdrian Chadd
133eb6f0de0SAdrian Chadd rc[0].flags = rc[1].flags = rc[2].flags = rc[3].flags = 0;
134710c3778SAdrian Chadd
135eb6f0de0SAdrian Chadd rc[0].rix = amn->amn_tx_rate0;
136eb6f0de0SAdrian Chadd rc[1].rix = amn->amn_tx_rate1;
137eb6f0de0SAdrian Chadd rc[2].rix = amn->amn_tx_rate2;
138eb6f0de0SAdrian Chadd rc[3].rix = amn->amn_tx_rate3;
139eb6f0de0SAdrian Chadd
140eb6f0de0SAdrian Chadd rc[0].tries = amn->amn_tx_try0;
141eb6f0de0SAdrian Chadd rc[1].tries = amn->amn_tx_try1;
142eb6f0de0SAdrian Chadd rc[2].tries = amn->amn_tx_try2;
143eb6f0de0SAdrian Chadd rc[3].tries = amn->amn_tx_try3;
144710c3778SAdrian Chadd }
145710c3778SAdrian Chadd
14605f1e03fSSam Leffler void
ath_rate_setupxtxdesc(struct ath_softc * sc,struct ath_node * an,struct ath_desc * ds,int shortPreamble,u_int8_t rix)14705f1e03fSSam Leffler ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
148a4d8dd10SSam Leffler struct ath_desc *ds, int shortPreamble, u_int8_t rix)
14905f1e03fSSam Leffler {
15005f1e03fSSam Leffler struct amrr_node *amn = ATH_NODE_AMRR(an);
15105f1e03fSSam Leffler
15205f1e03fSSam Leffler ath_hal_setupxtxdesc(sc->sc_ah, ds
15305f1e03fSSam Leffler , amn->amn_tx_rate1sp, amn->amn_tx_try1 /* series 1 */
15405f1e03fSSam Leffler , amn->amn_tx_rate2sp, amn->amn_tx_try2 /* series 2 */
15505f1e03fSSam Leffler , amn->amn_tx_rate3sp, amn->amn_tx_try3 /* series 3 */
15605f1e03fSSam Leffler );
15705f1e03fSSam Leffler }
15805f1e03fSSam Leffler
15905f1e03fSSam 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)16022233301SSam Leffler ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
161eb6f0de0SAdrian Chadd const struct ath_rc_series *rc, const struct ath_tx_status *ts,
162cce63444SAdrian Chadd int frame_size, int rc_framesize, int nframes, int nbad)
16305f1e03fSSam Leffler {
16405f1e03fSSam Leffler struct amrr_node *amn = ATH_NODE_AMRR(an);
16565f9edeeSSam Leffler int sr = ts->ts_shortretry;
16665f9edeeSSam Leffler int lr = ts->ts_longretry;
16705f1e03fSSam Leffler int retry_count = sr + lr;
16805f1e03fSSam Leffler
16905f1e03fSSam Leffler amn->amn_tx_try0_cnt++;
17005f1e03fSSam Leffler if (retry_count == 1) {
17105f1e03fSSam Leffler amn->amn_tx_try1_cnt++;
17205f1e03fSSam Leffler } else if (retry_count == 2) {
17305f1e03fSSam Leffler amn->amn_tx_try1_cnt++;
17405f1e03fSSam Leffler amn->amn_tx_try2_cnt++;
17505f1e03fSSam Leffler } else if (retry_count == 3) {
17605f1e03fSSam Leffler amn->amn_tx_try1_cnt++;
17705f1e03fSSam Leffler amn->amn_tx_try2_cnt++;
17805f1e03fSSam Leffler amn->amn_tx_try3_cnt++;
17905f1e03fSSam Leffler } else if (retry_count > 3) {
18005f1e03fSSam Leffler amn->amn_tx_try1_cnt++;
18105f1e03fSSam Leffler amn->amn_tx_try2_cnt++;
18205f1e03fSSam Leffler amn->amn_tx_try3_cnt++;
18305f1e03fSSam Leffler amn->amn_tx_failure_cnt++;
18405f1e03fSSam Leffler }
185b032f27cSSam Leffler if (amn->amn_interval != 0 &&
186b032f27cSSam Leffler ticks - amn->amn_ticks > amn->amn_interval) {
187b032f27cSSam Leffler ath_rate_ctl(sc, &an->an_node);
188b032f27cSSam Leffler amn->amn_ticks = ticks;
189b032f27cSSam Leffler }
19005f1e03fSSam Leffler }
19105f1e03fSSam Leffler
19205f1e03fSSam Leffler void
ath_rate_newassoc(struct ath_softc * sc,struct ath_node * an,int isnew)19305f1e03fSSam Leffler ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
19405f1e03fSSam Leffler {
19505f1e03fSSam Leffler if (isnew)
19605f1e03fSSam Leffler ath_rate_ctl_start(sc, &an->an_node);
19705f1e03fSSam Leffler }
19805f1e03fSSam Leffler
1997d450faaSAdrian Chadd void
ath_rate_update_rx_rssi(struct ath_softc * sc,struct ath_node * an,int rssi)2007d450faaSAdrian Chadd ath_rate_update_rx_rssi(struct ath_softc *sc, struct ath_node *an, int rssi)
2017d450faaSAdrian Chadd {
2027d450faaSAdrian Chadd }
2037d450faaSAdrian Chadd
20405f1e03fSSam Leffler static void
node_reset(struct amrr_node * amn)20505f1e03fSSam Leffler node_reset(struct amrr_node *amn)
20605f1e03fSSam Leffler {
20705f1e03fSSam Leffler amn->amn_tx_try0_cnt = 0;
20805f1e03fSSam Leffler amn->amn_tx_try1_cnt = 0;
20905f1e03fSSam Leffler amn->amn_tx_try2_cnt = 0;
21005f1e03fSSam Leffler amn->amn_tx_try3_cnt = 0;
21105f1e03fSSam Leffler amn->amn_tx_failure_cnt = 0;
21205f1e03fSSam Leffler amn->amn_success = 0;
21305f1e03fSSam Leffler amn->amn_recovery = 0;
21405f1e03fSSam Leffler amn->amn_success_threshold = ath_rate_min_success_threshold;
21505f1e03fSSam Leffler }
21605f1e03fSSam Leffler
21705f1e03fSSam Leffler /**
21805f1e03fSSam Leffler * The code below assumes that we are dealing with hardware multi rate retry
21905f1e03fSSam Leffler * I have no idea what will happen if you try to use this module with another
22005f1e03fSSam Leffler * type of hardware. Your machine might catch fire or it might work with
22105f1e03fSSam Leffler * horrible performance...
22205f1e03fSSam Leffler */
22305f1e03fSSam Leffler static void
ath_rate_update(struct ath_softc * sc,struct ieee80211_node * ni,int rate)22405f1e03fSSam Leffler ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
22505f1e03fSSam Leffler {
22605f1e03fSSam Leffler struct ath_node *an = ATH_NODE(ni);
22705f1e03fSSam Leffler struct amrr_node *amn = ATH_NODE_AMRR(an);
228b032f27cSSam Leffler struct ieee80211vap *vap = ni->ni_vap;
22905f1e03fSSam Leffler const HAL_RATE_TABLE *rt = sc->sc_currates;
23005f1e03fSSam Leffler u_int8_t rix;
23105f1e03fSSam Leffler
23205f1e03fSSam Leffler KASSERT(rt != NULL, ("no rate table, mode %u", sc->sc_curmode));
23305f1e03fSSam Leffler
234b032f27cSSam Leffler IEEE80211_NOTE(vap, IEEE80211_MSG_RATECTL, ni,
235b032f27cSSam Leffler "%s: set xmit rate to %dM", __func__,
23605f1e03fSSam Leffler ni->ni_rates.rs_nrates > 0 ?
23705f1e03fSSam Leffler (ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
23805f1e03fSSam Leffler
239b032f27cSSam Leffler amn->amn_rix = rate;
24005f1e03fSSam Leffler /*
24105f1e03fSSam Leffler * Before associating a node has no rate set setup
24205f1e03fSSam Leffler * so we can't calculate any transmit codes to use.
24305f1e03fSSam Leffler * This is ok since we should never be sending anything
24405f1e03fSSam Leffler * but management frames and those always go at the
24505f1e03fSSam Leffler * lowest hardware rate.
24605f1e03fSSam Leffler */
24705f1e03fSSam Leffler if (ni->ni_rates.rs_nrates > 0) {
248*70674500SAdrian Chadd uint8_t dot11rate;
249*70674500SAdrian Chadd
250*70674500SAdrian Chadd dot11rate = ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL;
251*70674500SAdrian Chadd amn->amn_tx_rix0 = sc->sc_rixmap[dot11rate];
252*70674500SAdrian Chadd ieee80211_node_set_txrate_dot11rate(ni, dot11rate);
25305f1e03fSSam Leffler amn->amn_tx_rate0 = rt->info[amn->amn_tx_rix0].rateCode;
25405f1e03fSSam Leffler amn->amn_tx_rate0sp = amn->amn_tx_rate0 |
25505f1e03fSSam Leffler rt->info[amn->amn_tx_rix0].shortPreamble;
25605f1e03fSSam Leffler if (sc->sc_mrretry) {
25705f1e03fSSam Leffler amn->amn_tx_try0 = 1;
25805f1e03fSSam Leffler amn->amn_tx_try1 = 1;
25905f1e03fSSam Leffler amn->amn_tx_try2 = 1;
26005f1e03fSSam Leffler amn->amn_tx_try3 = 1;
26105f1e03fSSam Leffler if (--rate >= 0) {
26205f1e03fSSam Leffler rix = sc->sc_rixmap[
26305f1e03fSSam Leffler ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
26405f1e03fSSam Leffler amn->amn_tx_rate1 = rt->info[rix].rateCode;
26505f1e03fSSam Leffler amn->amn_tx_rate1sp = amn->amn_tx_rate1 |
26605f1e03fSSam Leffler rt->info[rix].shortPreamble;
26705f1e03fSSam Leffler } else {
26805f1e03fSSam Leffler amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
26905f1e03fSSam Leffler }
27005f1e03fSSam Leffler if (--rate >= 0) {
27105f1e03fSSam Leffler rix = sc->sc_rixmap[
27205f1e03fSSam Leffler ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
27305f1e03fSSam Leffler amn->amn_tx_rate2 = rt->info[rix].rateCode;
27405f1e03fSSam Leffler amn->amn_tx_rate2sp = amn->amn_tx_rate2 |
27505f1e03fSSam Leffler rt->info[rix].shortPreamble;
27605f1e03fSSam Leffler } else {
27705f1e03fSSam Leffler amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
27805f1e03fSSam Leffler }
27905f1e03fSSam Leffler if (rate > 0) {
28005f1e03fSSam Leffler /* NB: only do this if we didn't already do it above */
28105f1e03fSSam Leffler amn->amn_tx_rate3 = rt->info[0].rateCode;
28205f1e03fSSam Leffler amn->amn_tx_rate3sp =
28355f63772SSam Leffler amn->amn_tx_rate3 | rt->info[0].shortPreamble;
28405f1e03fSSam Leffler } else {
28505f1e03fSSam Leffler amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
28605f1e03fSSam Leffler }
28705f1e03fSSam Leffler } else {
28805f1e03fSSam Leffler amn->amn_tx_try0 = ATH_TXMAXTRY;
28905f1e03fSSam Leffler /* theorically, these statements are useless because
29005f1e03fSSam Leffler * the code which uses them tests for an_tx_try0 == ATH_TXMAXTRY
29105f1e03fSSam Leffler */
29205f1e03fSSam Leffler amn->amn_tx_try1 = 0;
29305f1e03fSSam Leffler amn->amn_tx_try2 = 0;
29405f1e03fSSam Leffler amn->amn_tx_try3 = 0;
29505f1e03fSSam Leffler amn->amn_tx_rate1 = amn->amn_tx_rate1sp = 0;
29605f1e03fSSam Leffler amn->amn_tx_rate2 = amn->amn_tx_rate2sp = 0;
29705f1e03fSSam Leffler amn->amn_tx_rate3 = amn->amn_tx_rate3sp = 0;
29805f1e03fSSam Leffler }
29905f1e03fSSam Leffler }
30005f1e03fSSam Leffler node_reset(amn);
301b032f27cSSam Leffler
302b032f27cSSam Leffler amn->amn_interval = ath_rateinterval;
303b032f27cSSam Leffler if (vap->iv_opmode == IEEE80211_M_STA)
304b032f27cSSam Leffler amn->amn_interval /= 2;
305b032f27cSSam Leffler amn->amn_interval = (amn->amn_interval * hz) / 1000;
30605f1e03fSSam Leffler }
30705f1e03fSSam Leffler
30805f1e03fSSam Leffler /*
30905f1e03fSSam Leffler * Set the starting transmit rate for a node.
31005f1e03fSSam Leffler */
31105f1e03fSSam Leffler static void
ath_rate_ctl_start(struct ath_softc * sc,struct ieee80211_node * ni)31205f1e03fSSam Leffler ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
31305f1e03fSSam Leffler {
31405f1e03fSSam Leffler #define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
315c62362cbSSam Leffler const struct ieee80211_txparam *tp = ni->ni_txparms;
31605f1e03fSSam Leffler int srate;
31705f1e03fSSam Leffler
31805f1e03fSSam Leffler KASSERT(ni->ni_rates.rs_nrates > 0, ("no rates"));
319b032f27cSSam Leffler if (tp == NULL || tp->ucastrate == IEEE80211_FIXED_RATE_NONE) {
32005f1e03fSSam Leffler /*
32105f1e03fSSam Leffler * No fixed rate is requested. For 11b start with
32205f1e03fSSam Leffler * the highest negotiated rate; otherwise, for 11g
32305f1e03fSSam Leffler * and 11a, we start "in the middle" at 24Mb or 36Mb.
32405f1e03fSSam Leffler */
32505f1e03fSSam Leffler srate = ni->ni_rates.rs_nrates - 1;
32605f1e03fSSam Leffler if (sc->sc_curmode != IEEE80211_MODE_11B) {
32705f1e03fSSam Leffler /*
32805f1e03fSSam Leffler * Scan the negotiated rate set to find the
32905f1e03fSSam Leffler * closest rate.
33005f1e03fSSam Leffler */
33105f1e03fSSam Leffler /* NB: the rate set is assumed sorted */
33205f1e03fSSam Leffler for (; srate >= 0 && RATE(srate) > 72; srate--)
33305f1e03fSSam Leffler ;
33405f1e03fSSam Leffler }
33505f1e03fSSam Leffler } else {
33605f1e03fSSam Leffler /*
33768e8e04eSSam Leffler * A fixed rate is to be used; ic_fixed_rate is the
33868e8e04eSSam Leffler * IEEE code for this rate (sans basic bit). Convert this
33905f1e03fSSam Leffler * to the index into the negotiated rate set for
34005f1e03fSSam Leffler * the node. We know the rate is there because the
34105f1e03fSSam Leffler * rate set is checked when the station associates.
34205f1e03fSSam Leffler */
34305f1e03fSSam Leffler /* NB: the rate set is assumed sorted */
34405f1e03fSSam Leffler srate = ni->ni_rates.rs_nrates - 1;
345b032f27cSSam Leffler for (; srate >= 0 && RATE(srate) != tp->ucastrate; srate--)
34605f1e03fSSam Leffler ;
34705f1e03fSSam Leffler }
34868e8e04eSSam Leffler /*
34968e8e04eSSam Leffler * The selected rate may not be available due to races
35068e8e04eSSam Leffler * and mode settings. Also orphaned nodes created in
35168e8e04eSSam Leffler * adhoc mode may not have any rate set so this lookup
35268e8e04eSSam Leffler * can fail. This is not fatal.
35368e8e04eSSam Leffler */
35468e8e04eSSam Leffler ath_rate_update(sc, ni, srate < 0 ? 0 : srate);
35505f1e03fSSam Leffler #undef RATE
35605f1e03fSSam Leffler }
35705f1e03fSSam Leffler
35805f1e03fSSam Leffler /*
35905f1e03fSSam Leffler * Examine and potentially adjust the transmit rate.
36005f1e03fSSam Leffler */
36105f1e03fSSam Leffler static void
ath_rate_ctl(void * arg,struct ieee80211_node * ni)36205f1e03fSSam Leffler ath_rate_ctl(void *arg, struct ieee80211_node *ni)
36305f1e03fSSam Leffler {
36405f1e03fSSam Leffler struct ath_softc *sc = arg;
36505f1e03fSSam Leffler struct amrr_node *amn = ATH_NODE_AMRR(ATH_NODE (ni));
366b032f27cSSam Leffler int rix;
36705f1e03fSSam Leffler
36805f1e03fSSam Leffler #define is_success(amn) \
36905f1e03fSSam Leffler (amn->amn_tx_try1_cnt < (amn->amn_tx_try0_cnt/10))
37005f1e03fSSam Leffler #define is_enough(amn) \
37105f1e03fSSam Leffler (amn->amn_tx_try0_cnt > 10)
37205f1e03fSSam Leffler #define is_failure(amn) \
37305f1e03fSSam Leffler (amn->amn_tx_try1_cnt > (amn->amn_tx_try0_cnt/3))
37405f1e03fSSam Leffler
375b032f27cSSam Leffler rix = amn->amn_rix;
37605f1e03fSSam Leffler
377b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
378b032f27cSSam Leffler "cnt0: %d cnt1: %d cnt2: %d cnt3: %d -- threshold: %d",
379b032f27cSSam Leffler amn->amn_tx_try0_cnt, amn->amn_tx_try1_cnt, amn->amn_tx_try2_cnt,
380b032f27cSSam Leffler amn->amn_tx_try3_cnt, amn->amn_success_threshold);
38105f1e03fSSam Leffler if (is_success (amn) && is_enough (amn)) {
38205f1e03fSSam Leffler amn->amn_success++;
38305f1e03fSSam Leffler if (amn->amn_success == amn->amn_success_threshold &&
384b032f27cSSam Leffler rix + 1 < ni->ni_rates.rs_nrates) {
38505f1e03fSSam Leffler amn->amn_recovery = 1;
38605f1e03fSSam Leffler amn->amn_success = 0;
387b032f27cSSam Leffler rix++;
388b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap, IEEE80211_MSG_RATECTL, ni,
389b032f27cSSam Leffler "increase rate to %d", rix);
39005f1e03fSSam Leffler } else {
39105f1e03fSSam Leffler amn->amn_recovery = 0;
39205f1e03fSSam Leffler }
39305f1e03fSSam Leffler } else if (is_failure (amn)) {
39405f1e03fSSam Leffler amn->amn_success = 0;
395b032f27cSSam Leffler if (rix > 0) {
39605f1e03fSSam Leffler if (amn->amn_recovery) {
39705f1e03fSSam Leffler /* recovery failure. */
39805f1e03fSSam Leffler amn->amn_success_threshold *= 2;
39905f1e03fSSam Leffler amn->amn_success_threshold = min (amn->amn_success_threshold,
40005f1e03fSSam Leffler (u_int)ath_rate_max_success_threshold);
401b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap,
402b032f27cSSam Leffler IEEE80211_MSG_RATECTL, ni,
403b032f27cSSam Leffler "decrease rate recovery thr: %d",
404b032f27cSSam Leffler amn->amn_success_threshold);
40505f1e03fSSam Leffler } else {
40605f1e03fSSam Leffler /* simple failure. */
40705f1e03fSSam Leffler amn->amn_success_threshold = ath_rate_min_success_threshold;
408b032f27cSSam Leffler IEEE80211_NOTE(ni->ni_vap,
409b032f27cSSam Leffler IEEE80211_MSG_RATECTL, ni,
410b032f27cSSam Leffler "decrease rate normal thr: %d",
411b032f27cSSam Leffler amn->amn_success_threshold);
41205f1e03fSSam Leffler }
41305f1e03fSSam Leffler amn->amn_recovery = 0;
414b032f27cSSam Leffler rix--;
41505f1e03fSSam Leffler } else {
41605f1e03fSSam Leffler amn->amn_recovery = 0;
41705f1e03fSSam Leffler }
41805f1e03fSSam Leffler }
419b032f27cSSam Leffler if (is_enough (amn) || rix != amn->amn_rix) {
42005f1e03fSSam Leffler /* reset counters. */
42105f1e03fSSam Leffler amn->amn_tx_try0_cnt = 0;
42205f1e03fSSam Leffler amn->amn_tx_try1_cnt = 0;
42305f1e03fSSam Leffler amn->amn_tx_try2_cnt = 0;
42405f1e03fSSam Leffler amn->amn_tx_try3_cnt = 0;
42505f1e03fSSam Leffler amn->amn_tx_failure_cnt = 0;
42605f1e03fSSam Leffler }
427b032f27cSSam Leffler if (rix != amn->amn_rix) {
428b032f27cSSam Leffler ath_rate_update(sc, ni, rix);
42905f1e03fSSam Leffler }
43005f1e03fSSam Leffler }
43105f1e03fSSam Leffler
4322d20d655SAdrian Chadd static int
ath_rate_fetch_node_stats(struct ath_softc * sc,struct ath_node * an,struct ath_rateioctl * re)4332d20d655SAdrian Chadd ath_rate_fetch_node_stats(struct ath_softc *sc, struct ath_node *an,
4342d20d655SAdrian Chadd struct ath_rateioctl *re)
4352d20d655SAdrian Chadd {
4362d20d655SAdrian Chadd
4372d20d655SAdrian Chadd return (EINVAL);
4382d20d655SAdrian Chadd }
4392d20d655SAdrian Chadd
44005f1e03fSSam Leffler static void
ath_rate_sysctlattach(struct ath_softc * sc)44105f1e03fSSam Leffler ath_rate_sysctlattach(struct ath_softc *sc)
44205f1e03fSSam Leffler {
44305f1e03fSSam Leffler struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev);
44405f1e03fSSam Leffler struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev);
44505f1e03fSSam Leffler
44605f1e03fSSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
44705f1e03fSSam Leffler "rate_interval", CTLFLAG_RW, &ath_rateinterval, 0,
44805f1e03fSSam Leffler "rate control: operation interval (ms)");
44905f1e03fSSam Leffler /* XXX bounds check values */
45005f1e03fSSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
45105f1e03fSSam Leffler "max_sucess_threshold", CTLFLAG_RW,
45205f1e03fSSam Leffler &ath_rate_max_success_threshold, 0, "");
45305f1e03fSSam Leffler SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
45405f1e03fSSam Leffler "min_sucess_threshold", CTLFLAG_RW,
45505f1e03fSSam Leffler &ath_rate_min_success_threshold, 0, "");
45605f1e03fSSam Leffler }
45705f1e03fSSam Leffler
45805f1e03fSSam Leffler struct ath_ratectrl *
ath_rate_attach(struct ath_softc * sc)45905f1e03fSSam Leffler ath_rate_attach(struct ath_softc *sc)
46005f1e03fSSam Leffler {
46105f1e03fSSam Leffler struct amrr_softc *asc;
46205f1e03fSSam Leffler
46305f1e03fSSam Leffler asc = malloc(sizeof(struct amrr_softc), M_DEVBUF, M_NOWAIT|M_ZERO);
46405f1e03fSSam Leffler if (asc == NULL)
46505f1e03fSSam Leffler return NULL;
46605f1e03fSSam Leffler asc->arc.arc_space = sizeof(struct amrr_node);
46705f1e03fSSam Leffler ath_rate_sysctlattach(sc);
46805f1e03fSSam Leffler
46905f1e03fSSam Leffler return &asc->arc;
47005f1e03fSSam Leffler }
47105f1e03fSSam Leffler
47205f1e03fSSam Leffler void
ath_rate_detach(struct ath_ratectrl * arc)47305f1e03fSSam Leffler ath_rate_detach(struct ath_ratectrl *arc)
47405f1e03fSSam Leffler {
47505f1e03fSSam Leffler struct amrr_softc *asc = (struct amrr_softc *) arc;
47605f1e03fSSam Leffler
47705f1e03fSSam Leffler free(asc, M_DEVBUF);
47805f1e03fSSam Leffler }
479