2008-01-19 09:17:50

by Bruno Randolf

[permalink] [raw]
Subject: [PATCH 1/3] ath5k: better beacon timer calculation

update ath5k_beacon_update_timers() for better beacon timer calculation in a
variety of situations. most important is the possibility to call it with the
timestamp of a received beacon, when we detected that a HW merge has happened
and we need to reconfigure the beacon timers based on that.

we call this from the mac80211 callback reset_tsf now instead of beacon_update,
and there will be more use of it in the next patch.

drivers/net/wireless/ath5k/base.c: Changes-licensed-under: 3-Clause-BSD

Signed-off-by: Bruno Randolf <[email protected]>
---

drivers/net/wireless/ath5k/base.c | 109 +++++++++++++++++++++++++++++++------
1 files changed, 92 insertions(+), 17 deletions(-)


diff --git a/drivers/net/wireless/ath5k/base.c b/drivers/net/wireless/ath5k/base.c
index 784b359..ae3d470 100644
--- a/drivers/net/wireless/ath5k/base.c
+++ b/drivers/net/wireless/ath5k/base.c
@@ -290,6 +290,7 @@ static int ath5k_beacon_setup(struct ath5k_softc *sc,
struct ieee80211_tx_control *ctl);
static void ath5k_beacon_send(struct ath5k_softc *sc);
static void ath5k_beacon_config(struct ath5k_softc *sc);
+static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);

static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
{
@@ -1980,34 +1981,102 @@ ath5k_beacon_send(struct ath5k_softc *sc)
}


+/**
+ * ath5k_beacon_update_timers - update beacon timers
+ *
+ * @sc: struct ath5k_softc pointer we are operating on
+ * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
+ * beacon timer update based on the current HW TSF.
+ *
+ * Calculate the next target beacon transmit time (TBTT) based on the timestamp
+ * of a received beacon or the current local hardware TSF and write it to the
+ * beacon timer registers.
+ *
+ * This is called in a variety of situations, e.g. when a beacon is received,
+ * when a HW merge has been detected, but also when an new IBSS is created or
+ * when we otherwise know we have to update the timers, but we keep it in this
+ * function to have it all together in one place.
+ */
static void
-ath5k_beacon_update_timers(struct ath5k_softc *sc)
+ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
{
struct ath5k_hw *ah = sc->ah;
- u32 uninitialized_var(nexttbtt), intval, tsftu;
- u64 tsf;
+ u32 nexttbtt, intval, hw_tu, bc_tu;
+ u64 hw_tsf;

intval = sc->bintval & AR5K_BEACON_PERIOD;
if (WARN_ON(!intval))
return;

- /* current TSF converted to TU */
- tsf = ath5k_hw_get_tsf64(ah);
- tsftu = TSF_TO_TU(tsf);
+ /* beacon TSF converted to TU */
+ bc_tu = TSF_TO_TU(bc_tsf);

- /*
- * Pull nexttbtt forward to reflect the current
- * TSF. Add one intval otherwise the timespan
- * can be too short for ibss merges.
- */
- nexttbtt = tsftu + 2 * intval;
+ /* current TSF converted to TU */
+ hw_tsf = ath5k_hw_get_tsf64(ah);
+ hw_tu = TSF_TO_TU(hw_tsf);

- ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
- "hw tsftu %u nexttbtt %u intval %u\n", tsftu, nexttbtt, intval);
+#define FUDGE 3
+ /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
+ if (bc_tsf == -1) {
+ /*
+ * no beacons received, called internally.
+ * just need to refresh timers based on HW TSF.
+ */
+ nexttbtt = roundup(hw_tu + FUDGE, intval);
+ } else if (bc_tsf == 0) {
+ /*
+ * no beacon received, probably called by ath5k_reset_tsf().
+ * reset TSF to start with 0.
+ */
+ nexttbtt = intval;
+ intval |= AR5K_BEACON_RESET_TSF;
+ } else if (bc_tsf > hw_tsf) {
+ /*
+ * beacon received, SW merge happend but HW TSF not yet updated.
+ * not possible to reconfigure timers yet, but next time we
+ * receive a beacon with the same BSSID, the hardware will
+ * automatically update the TSF and then we need to reconfigure
+ * the timers.
+ */
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
+ "need to wait for HW TSF sync\n");
+ return;
+ } else {
+ /*
+ * most important case for beacon synchronization between STA.
+ *
+ * beacon received and HW TSF has been already updated by HW.
+ * update next TBTT based on the TSF of the beacon, but make
+ * sure it is ahead of our local TSF timer.
+ */
+ nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
+ }
+#undef FUDGE

intval |= AR5K_BEACON_ENA;
-
ath5k_hw_init_beacon(ah, nexttbtt, intval);
+
+ /*
+ * debugging output last in order to preserve the time critical aspect
+ * of this function
+ */
+ if (bc_tsf == -1)
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
+ "reconfigured timers based on HW TSF\n");
+ else if (bc_tsf == 0)
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
+ "reset HW TSF and timers\n");
+ else
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
+ "updated timers based on beacon TSF\n");
+
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
+ "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
+ bc_tsf, hw_tsf, bc_tu, hw_tu, nexttbtt);
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
+ intval & AR5K_BEACON_PERIOD,
+ intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
+ intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
}


@@ -2041,7 +2110,6 @@ ath5k_beacon_config(struct ath5k_softc *sc)
* only once here.
*/
ath5k_beaconq_config(sc);
- ath5k_beacon_update_timers(sc);

if (!ath5k_hw_hasveol(ah))
sc->imask |= AR5K_INT_SWBA;
@@ -2791,7 +2859,14 @@ ath5k_reset_tsf(struct ieee80211_hw *hw)
{
struct ath5k_softc *sc = hw->priv;

- ath5k_hw_reset_tsf(sc->ah);
+ /*
+ * in IBSS mode we need to update the beacon timers too.
+ * this will also reset the TSF if we call it with 0
+ */
+ if (sc->opmode == IEEE80211_IF_TYPE_IBSS)
+ ath5k_beacon_update_timers(sc, 0);
+ else
+ ath5k_hw_reset_tsf(sc->ah);
}

static int



2008-01-24 14:48:16

by Johannes Berg

[permalink] [raw]
Subject: Re: [PATCH 2/3] ath5k: use SWBA to detect IBSS HW merges


On Sat, 2008-01-19 at 18:18 +0900, Bruno Randolf wrote:

> time und check if a HW merge (automatic TSF update) has happened on every
> received beacon with the same BSSID.

I don't know who is smoking what (I suspect Atheros folks...), but
calling a TSF adoption a "merge" is pretty out of line IMHO when you
also describe some non-standard BSSID changes as "merge".

johannes


Attachments:
signature.asc (828.00 B)
This is a digitally signed message part

2008-01-19 09:18:10

by Bruno Randolf

[permalink] [raw]
Subject: [PATCH 2/3] ath5k: use SWBA to detect IBSS HW merges

use SWBA (software beacon alert) interrupts to keep track of the next beacon
time und check if a HW merge (automatic TSF update) has happened on every
received beacon with the same BSSID.

this is necessary because the atheros hardware will silently update the local
TSF in IBSS mode, but not its beacon timers. if the TSF is ahead of the beacon
timers no beacons are sent until the timers wrap around (typically after about
1 minute).

this solution is not very nice, since we have to look into every beacon, but
there is apparently no other way to detect HW merges.

drivers/net/wireless/ath5k/base.c: Changes-licensed-under: 3-Clause-BSD
drivers/net/wireless/ath5k/base.h: Changes-licensed-under: 3-Clause-BSD

Signed-off-by: Bruno Randolf <[email protected]>
---

drivers/net/wireless/ath5k/base.c | 79 +++++++++++++++++++++++++++++++------
drivers/net/wireless/ath5k/base.h | 1
2 files changed, 67 insertions(+), 13 deletions(-)


diff --git a/drivers/net/wireless/ath5k/base.c b/drivers/net/wireless/ath5k/base.c
index ae3d470..a47e0f3 100644
--- a/drivers/net/wireless/ath5k/base.c
+++ b/drivers/net/wireless/ath5k/base.c
@@ -1629,6 +1629,34 @@ ath5k_rx_decrypted(struct ath5k_softc *sc, struct ath5k_desc *ds,
return 0;
}

+
+static void
+ath5k_check_ibss_hw_merge(struct ath5k_softc *sc, struct sk_buff *skb)
+{
+ u32 hw_tu;
+ struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
+
+ if ((mgmt->frame_control & IEEE80211_FCTL_FTYPE) ==
+ IEEE80211_FTYPE_MGMT &&
+ (mgmt->frame_control & IEEE80211_FCTL_STYPE) ==
+ IEEE80211_STYPE_BEACON &&
+ mgmt->u.beacon.capab_info & WLAN_CAPABILITY_IBSS &&
+ memcmp(mgmt->bssid, sc->ah->ah_bssid, ETH_ALEN) == 0) {
+ /*
+ * Received an IBSS beacon with the same BSSID. Hardware might
+ * have updated the TSF, check if we need to update timers.
+ */
+ hw_tu = TSF_TO_TU(ath5k_hw_get_tsf64(sc->ah));
+ if (hw_tu >= sc->nexttbtt) {
+ ath5k_beacon_update_timers(sc,
+ mgmt->u.beacon.timestamp);
+ ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
+ "detected HW merge from received beacon\n");
+ }
+ }
+}
+
+
static void
ath5k_tasklet_rx(unsigned long data)
{
@@ -1763,6 +1791,10 @@ accept:

ath5k_debug_dump_skb(sc, skb, "RX ", 0);

+ /* check beacons in IBSS mode */
+ if (sc->opmode == IEEE80211_IF_TYPE_IBSS)
+ ath5k_check_ibss_hw_merge(sc, skb);
+
__ieee80211_rx(sc->hw, skb, &rxs);
sc->led_rxrate = ds->ds_rxstat.rs_rate;
ath5k_led_event(sc, ATH_LED_RX);
@@ -2053,6 +2085,8 @@ ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
}
#undef FUDGE

+ sc->nexttbtt = nexttbtt;
+
intval |= AR5K_BEACON_ENA;
ath5k_hw_init_beacon(ah, nexttbtt, intval);

@@ -2080,16 +2114,19 @@ ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
}


-/*
- * Configure the beacon timers and interrupts based on the operating mode
+/**
+ * ath5k_beacon_config - Configure the beacon queues and interrupts
+ *
+ * @sc: struct ath5k_softc pointer we are operating on
*
* When operating in station mode we want to receive a BMISS interrupt when we
* stop seeing beacons from the AP we've associated with so we can look for
* another AP to associate with.
*
- * In IBSS mode we need to configure the beacon timers and use a self-linked tx
- * descriptor if possible. If the hardware cannot deal with that we enable SWBA
- * interrupts to send the beacons from the interrupt handler.
+ * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
+ * interrupts to detect HW merges only.
+ *
+ * AP mode is missing.
*/
static void
ath5k_beacon_config(struct ath5k_softc *sc)
@@ -2103,17 +2140,17 @@ ath5k_beacon_config(struct ath5k_softc *sc)
sc->imask |= AR5K_INT_BMISS;
} else if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
/*
- * In IBSS mode enable the beacon timers but only enable SWBA
- * interrupts if we need to manually prepare beacon frames.
- * Otherwise we use a self-linked tx descriptor and let the
- * hardware deal with things. In that case we have to load it
+ * In IBSS mode we use a self-linked tx descriptor and let the
+ * hardware send the beacons automatically. We have to load it
* only once here.
+ * We use the SWBA interrupt only to keep track of the beacon
+ * timers in order to detect HW merges (automatic TSF updates).
*/
ath5k_beaconq_config(sc);

- if (!ath5k_hw_hasveol(ah))
- sc->imask |= AR5K_INT_SWBA;
- else
+ sc->imask |= AR5K_INT_SWBA;
+
+ if (ath5k_hw_hasveol(ah))
ath5k_beacon_send(sc);
}
/* TODO else AP */
@@ -2316,8 +2353,24 @@ ath5k_intr(int irq, void *dev_id)
* Handle beacon transmission directly; deferring
* this is too slow to meet timing constraints
* under load.
+ *
+ * In IBSS mode we use this interrupt just to
+ * keep track of the next TBTT (target beacon
+ * transmission time) in order to detect hardware
+ * merges (TSF updates).
*/
- ath5k_beacon_send(sc);
+ if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
+ /* XXX: only if VEOL suppported */
+ u64 tsf = ath5k_hw_get_tsf64(ah);
+ sc->nexttbtt += sc->bintval;
+ ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
+ "SWBA nexttbtt: %x hw_tu: %x "
+ "TSF: %llx\n",
+ sc->nexttbtt,
+ TSF_TO_TU(tsf), tsf);
+ } else {
+ ath5k_beacon_send(sc);
+ }
}
if (status & AR5K_INT_RXEOL) {
/*
diff --git a/drivers/net/wireless/ath5k/base.h b/drivers/net/wireless/ath5k/base.h
index 20c9469..8287ae7 100644
--- a/drivers/net/wireless/ath5k/base.h
+++ b/drivers/net/wireless/ath5k/base.h
@@ -166,6 +166,7 @@ struct ath5k_softc {
bmisscount, /* missed beacon transmits */
bintval, /* beacon interval in TU */
bsent;
+ unsigned int nexttbtt; /* next beacon time in TU */

struct timer_list calib_tim; /* calibration timer */
};


2008-01-25 06:07:30

by Bruno Randolf

[permalink] [raw]
Subject: Re: [PATCH 2/3] ath5k: use SWBA to detect IBSS HW merges

On Thursday 24 January 2008 18:18:15 Johannes Berg wrote:
> On Sat, 2008-01-19 at 18:18 +0900, Bruno Randolf wrote:
> > time und check if a HW merge (automatic TSF update) has happened on every
> > received beacon with the same BSSID.
>
> I don't know who is smoking what (I suspect Atheros folks...), but
> calling a TSF adoption a "merge" is pretty out of line IMHO when you
> also describe some non-standard BSSID changes as "merge".

hmm, i agree, that calling that a "merge" is a bit over the top. i was using
this term as somebody mentioned it in
http://madwifi.org/wiki/DevDocs/AdhocMerge, but i agree that "TSF update" is
more to the point and clearer.

bruno


2008-01-19 09:18:30

by Bruno Randolf

[permalink] [raw]
Subject: [PATCH 3/3] ath5k: configure backoff for IBSS beacon queue

in "11.1.2.2 Beacon generation in an IBSS" the IEEE802.11 standard says=
, each
STA should... "b) Calculate a random delay uniformly distributed in the=
range
between zero and twice aCWmin =C3=97 aSlotTime,".

configure cwmin and cwmax of the beacon queue in IBSS mode according to=
this.
unfortunately beacon backoff does not work reliably yet, so i suspect w=
e have a
problem somewhere else, since the same settings (and similar beacon tim=
er
configuration) work for madwifi.

drivers/net/wireless/ath5k/base.c: Changes-licensed-under: 3-Claus=
e-BSD

Signed-off-by: Bruno Randolf <[email protected]>
---

drivers/net/wireless/ath5k/base.c | 14 ++++++++++++--
1 files changed, 12 insertions(+), 2 deletions(-)


diff --git a/drivers/net/wireless/ath5k/base.c b/drivers/net/wireless/a=
th5k/base.c
index a47e0f3..5bb1b04 100644
--- a/drivers/net/wireless/ath5k/base.c
+++ b/drivers/net/wireless/ath5k/base.c
@@ -1447,8 +1447,7 @@ ath5k_beaconq_config(struct ath5k_softc *sc)
ret =3D ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
if (ret)
return ret;
- if (sc->opmode =3D=3D IEEE80211_IF_TYPE_AP ||
- sc->opmode =3D=3D IEEE80211_IF_TYPE_IBSS) {
+ if (sc->opmode =3D=3D IEEE80211_IF_TYPE_AP) {
/*
* Always burst out beacon and CAB traffic
* (aifs =3D cwmin =3D cwmax =3D 0)
@@ -1456,8 +1455,19 @@ ath5k_beaconq_config(struct ath5k_softc *sc)
qi.tqi_aifs =3D 0;
qi.tqi_cw_min =3D 0;
qi.tqi_cw_max =3D 0;
+ } else if (sc->opmode =3D=3D IEEE80211_IF_TYPE_IBSS) {
+ /*
+ * Adhoc mode; backoff between 0 and (2 * cw_min).
+ */
+ qi.tqi_aifs =3D 0;
+ qi.tqi_cw_min =3D 0;
+ qi.tqi_cw_max =3D 2 * ah->ah_cw_min;
}
=20
+ ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
+ "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
+ qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
+
ret =3D ath5k_hw_setup_tx_queueprops(ah, sc->bhalq, &qi);
if (ret) {
ATH5K_ERR(sc, "%s: unable to update parameters for beacon "

2008-01-19 19:33:22

by Nick Kossifidis

[permalink] [raw]
Subject: Re: [PATCH 3/3] ath5k: configure backoff for IBSS beacon queue

> + qi.tqi_cw_max = 2 * ah->ah_cw_min;

Have you checked ath5k_hw_reset_tx_queue ? cw_min/cw_max is
recalculated there, maybe we are doing something wrong there...





--
GPG ID: 0xD21DB2DB
As you read this post global entropy rises. Have Fun ;-)
Nick

2008-01-22 01:32:28

by Nick Kossifidis

[permalink] [raw]
Subject: Re: [PATCH 2/3] ath5k: use SWBA to detect IBSS HW merges

> + /* XXX: only if VEOL suppported */

Hmm, seems we have to fix this in hw.c, veol seems to be supported on
5211 and above but i'm not sure (i'll check madwifi's capability bits
for each of my cards and implement get_capability better)...

--
GPG ID: 0xD21DB2DB
As you read this post global entropy rises. Have Fun ;-)
Nick