#include <sys/cdefs.h>
#ifdef __FreeBSD__
__FBSDID("$FreeBSD: src/sys/dev/ath/ath_rate/onoe/onoe.c,v 1.10 2005/08/09 10:19:43 rwatson Exp $");
#endif
#ifdef __NetBSD__
__KERNEL_RCSID(0, "$NetBSD: athrate-onoe.c,v 1.16 2019/11/10 21:16:35 chs Exp $");
#endif
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/device.h>
#include <sys/bus.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_media.h>
#include <net/if_arp.h>
#include <net/if_ether.h>
#include <net80211/ieee80211_var.h>
#include <net/bpf.h>
#ifdef INET
#include <netinet/in.h>
#endif
#include "ah_desc.h"
#include <dev/ic/ath_netbsd.h>
#include <dev/ic/athvar.h>
#include <dev/ic/athrate-onoe.h>
#include <external/isc/atheros_hal/dist/ah.h>
#ifndef ONOE_DEBUG
#define ONOE_DEBUG
#endif
#ifdef ONOE_DEBUG
enum {
ATH_DEBUG_RATE = 0x00000010,
};
#define DPRINTF(sc, _fmt, ...) do { \
if (sc->sc_debug & ATH_DEBUG_RATE) \
printf(_fmt, __VA_ARGS__); \
} while (0)
#else
#define DPRINTF(sc, _fmt, ...)
#endif
static int ath_rateinterval = 1000;
static int ath_rate_raise = 10;
static int ath_rate_raise_threshold = 10;
static void ath_ratectl(void *);
static void ath_rate_update(struct ath_softc *, struct ieee80211_node *,
int rate);
static void ath_rate_ctl_start(struct ath_softc *, struct ieee80211_node *);
static void ath_rate_ctl(void *, struct ieee80211_node *);
void
ath_rate_node_init(struct ath_softc *sc, struct ath_node *an)
{
ath_rate_update(sc, &an->an_node, 0);
}
void
ath_rate_node_cleanup(struct ath_softc *sc, struct ath_node *an)
{
}
void
ath_rate_findrate(struct ath_softc *sc, struct ath_node *an,
int shortPreamble, size_t frameLen,
u_int8_t *rix, int *try0, u_int8_t *txrate)
{
struct onoe_node *on = ATH_NODE_ONOE(an);
*rix = on->on_tx_rix0;
*try0 = on->on_tx_try0;
if (shortPreamble)
*txrate = on->on_tx_rate0sp;
else
*txrate = on->on_tx_rate0;
}
void
ath_rate_setupxtxdesc(struct ath_softc *sc, struct ath_node *an,
struct ath_desc *ds, int shortPreamble, u_int8_t rix)
{
struct onoe_node *on = ATH_NODE_ONOE(an);
ath_hal_setupxtxdesc(sc->sc_ah, ds
, on->on_tx_rate1sp, 2
, on->on_tx_rate2sp, 2
, on->on_tx_rate3sp, 2
);
}
void
ath_rate_tx_complete(struct ath_softc *sc, struct ath_node *an,
const struct ath_desc *ds, const struct ath_desc *ds0)
{
struct onoe_node *on = ATH_NODE_ONOE(an);
if (ds->ds_txstat.ts_status == 0)
on->on_tx_ok++;
else
on->on_tx_err++;
on->on_tx_retr += ds->ds_txstat.ts_shortretry
+ ds->ds_txstat.ts_longretry;
}
void
ath_rate_newassoc(struct ath_softc *sc, struct ath_node *an, int isnew)
{
if (isnew)
ath_rate_ctl_start(sc, &an->an_node);
}
static void
ath_rate_update(struct ath_softc *sc, struct ieee80211_node *ni, int rate)
{
struct ath_node *an = ATH_NODE(ni);
struct onoe_node *on = ATH_NODE_ONOE(an);
const HAL_RATE_TABLE *rt = sc->sc_currates;
u_int8_t rix;
KASSERTMSG(rt != NULL, "no rate table, mode %u", sc->sc_curmode);
DPRINTF(sc, "%s: set xmit rate for %s to %dM\n",
__func__, ether_sprintf(ni->ni_macaddr),
ni->ni_rates.rs_nrates > 0 ?
(ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL) / 2 : 0);
ni->ni_txrate = rate;
if (ni->ni_rates.rs_nrates == 0)
goto done;
on->on_tx_rix0 = sc->sc_rixmap[
ni->ni_rates.rs_rates[rate] & IEEE80211_RATE_VAL];
on->on_tx_rate0 = rt->info[on->on_tx_rix0].rateCode;
on->on_tx_rate0sp = on->on_tx_rate0 |
rt->info[on->on_tx_rix0].shortPreamble;
if (sc->sc_mrretry) {
on->on_tx_try0 = 1 + 3;
if (--rate >= 0) {
rix = sc->sc_rixmap[
ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
on->on_tx_rate1 = rt->info[rix].rateCode;
on->on_tx_rate1sp = on->on_tx_rate1 |
rt->info[rix].shortPreamble;
} else {
on->on_tx_rate1 = on->on_tx_rate1sp = 0;
}
if (--rate >= 0) {
rix = sc->sc_rixmap[
ni->ni_rates.rs_rates[rate]&IEEE80211_RATE_VAL];
on->on_tx_rate2 = rt->info[rix].rateCode;
on->on_tx_rate2sp = on->on_tx_rate2 |
rt->info[rix].shortPreamble;
} else {
on->on_tx_rate2 = on->on_tx_rate2sp = 0;
}
if (rate > 0) {
on->on_tx_rate3 = rt->info[0].rateCode;
on->on_tx_rate3sp =
on->on_tx_rate3 | rt->info[0].shortPreamble;
} else {
on->on_tx_rate3 = on->on_tx_rate3sp = 0;
}
} else {
on->on_tx_try0 = ATH_TXMAXTRY;
on->on_tx_rate1 = on->on_tx_rate1sp = 0;
on->on_tx_rate2 = on->on_tx_rate2sp = 0;
on->on_tx_rate3 = on->on_tx_rate3sp = 0;
}
done:
on->on_tx_ok = on->on_tx_err = on->on_tx_retr = on->on_tx_upper = 0;
}
static void
ath_rate_ctl_start(struct ath_softc *sc, struct ieee80211_node *ni)
{
#define RATE(_ix) (ni->ni_rates.rs_rates[(_ix)] & IEEE80211_RATE_VAL)
struct ieee80211com *ic = &sc->sc_ic;
int srate;
KASSERTMSG(ni->ni_rates.rs_nrates > 0, "no rates");
if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
srate = ni->ni_rates.rs_nrates - 1;
if (sc->sc_curmode != IEEE80211_MODE_11B) {
for (; srate >= 0 && RATE(srate) > 72; srate--)
;
KASSERTMSG(srate >= 0, "bogus rate set");
}
} else {
const struct ieee80211_rateset *rs =
&ic->ic_sup_rates[ic->ic_curmode];
int r = rs->rs_rates[ic->ic_fixed_rate] & IEEE80211_RATE_VAL;
srate = ni->ni_rates.rs_nrates - 1;
for (; srate >= 0 && RATE(srate) != r; srate--)
;
KASSERTMSG(srate >= 0,
"fixed rate %d not in rate set", ic->ic_fixed_rate);
}
ath_rate_update(sc, ni, srate);
#undef RATE
}
static void
ath_rate_cb(void *arg, struct ieee80211_node *ni)
{
struct ath_softc *sc = arg;
ath_rate_update(sc, ni, 0);
}
void
ath_rate_newstate(struct ath_softc *sc, enum ieee80211_state state)
{
struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
struct ieee80211com *ic = &sc->sc_ic;
struct ieee80211_node *ni;
if (state == IEEE80211_S_INIT) {
callout_stop(&osc->timer);
return;
}
if (ic->ic_opmode == IEEE80211_M_STA) {
ni = ic->ic_bss;
if (state == IEEE80211_S_RUN) {
ath_rate_ctl_start(sc, ni);
} else {
ath_rate_update(sc, ni, 0);
}
} else {
ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_cb, sc);
ath_rate_update(sc, ic->ic_bss, 0);
}
if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE &&
state == IEEE80211_S_RUN) {
int interval;
interval = ath_rateinterval;
if (ic->ic_opmode == IEEE80211_M_STA)
interval /= 2;
callout_reset(&osc->timer, (interval * hz) / 1000,
ath_ratectl, &sc->sc_if);
}
}
static void
ath_rate_ctl(void *arg, struct ieee80211_node *ni)
{
struct ath_softc *sc = arg;
struct onoe_node *on = ATH_NODE_ONOE(ATH_NODE(ni));
struct ieee80211_rateset *rs = &ni->ni_rates;
int dir = 0, nrate, enough;
enough = (on->on_tx_ok + on->on_tx_err >= 10);
if (on->on_tx_err > 0 && on->on_tx_ok == 0)
dir = -1;
if (enough && on->on_tx_ok < on->on_tx_retr)
dir = -1;
if (enough && on->on_tx_err == 0 &&
on->on_tx_retr < (on->on_tx_ok * ath_rate_raise) / 100)
dir = 1;
DPRINTF(sc, "%s: ok %d err %d retr %d upper %d dir %d\n",
ether_sprintf(ni->ni_macaddr),
on->on_tx_ok, on->on_tx_err, on->on_tx_retr,
on->on_tx_upper, dir);
nrate = ni->ni_txrate;
switch (dir) {
case 0:
if (enough && on->on_tx_upper > 0)
on->on_tx_upper--;
break;
case -1:
if (nrate > 0) {
nrate--;
sc->sc_stats.ast_rate_drop++;
}
on->on_tx_upper = 0;
break;
case 1:
if (++on->on_tx_upper < ath_rate_raise_threshold)
break;
on->on_tx_upper = 0;
if (nrate + 1 < rs->rs_nrates) {
nrate++;
sc->sc_stats.ast_rate_raise++;
}
break;
}
if (nrate != ni->ni_txrate) {
DPRINTF(sc, "%s: %dM -> %dM (%d ok, %d err, %d retr)\n",
__func__,
(rs->rs_rates[ni->ni_txrate] & IEEE80211_RATE_VAL) / 2,
(rs->rs_rates[nrate] & IEEE80211_RATE_VAL) / 2,
on->on_tx_ok, on->on_tx_err, on->on_tx_retr);
ath_rate_update(sc, ni, nrate);
} else if (enough)
on->on_tx_ok = on->on_tx_err = on->on_tx_retr = 0;
}
static void
ath_ratectl(void *arg)
{
struct ifnet *ifp = arg;
struct ath_softc *sc = ifp->if_softc;
struct onoe_softc *osc = (struct onoe_softc *) sc->sc_rc;
struct ieee80211com *ic = &sc->sc_ic;
int interval;
if (ifp->if_flags & IFF_RUNNING) {
sc->sc_stats.ast_rate_calls++;
if (ic->ic_opmode == IEEE80211_M_STA)
ath_rate_ctl(sc, ic->ic_bss);
else
ieee80211_iterate_nodes(&ic->ic_sta, ath_rate_ctl, sc);
}
interval = ath_rateinterval;
if (ic->ic_opmode == IEEE80211_M_STA)
interval /= 2;
callout_reset(&osc->timer, (interval * hz) / 1000,
ath_ratectl, &sc->sc_if);
}
static void
ath_rate_sysctlattach(struct ath_softc *sc)
{
struct sysctllog **clog = &sc->sc_sysctllog;
const struct sysctlnode *cnode, *rnode;
if ((rnode = ath_sysctl_treetop(NULL)) == NULL)
return;
SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_interval",
"rate control: operation interval (ms)", rateinterval);
SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise",
"rate control: retry threshold to credit rate raise (%%)",
rate_raise);
SYSCTL_GLOBAL_INT(CTLFLAG_READWRITE, "rate_raise_threshold",
"rate control: # good periods before raising rate",
rate_raise_threshold);
}
struct ath_ratectrl *
ath_rate_attach(struct ath_softc *sc)
{
struct onoe_softc *osc;
osc = malloc(sizeof(struct onoe_softc), M_DEVBUF, M_WAITOK|M_ZERO);
osc->arc.arc_space = sizeof(struct onoe_node);
callout_init(&osc->timer, 0);
ath_rate_sysctlattach(sc);
return &osc->arc;
}
void
ath_rate_detach(struct ath_ratectrl *arc)
{
struct onoe_softc *osc = (struct onoe_softc *) arc;
callout_stop(&osc->timer);
free(osc, M_DEVBUF);
}