xref: /freebsd/sys/dev/ath/if_ath_tdma.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
1a35dae8dSAdrian Chadd /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4a35dae8dSAdrian Chadd  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5a35dae8dSAdrian Chadd  * All rights reserved.
6a35dae8dSAdrian Chadd  *
7a35dae8dSAdrian Chadd  * Redistribution and use in source and binary forms, with or without
8a35dae8dSAdrian Chadd  * modification, are permitted provided that the following conditions
9a35dae8dSAdrian Chadd  * are met:
10a35dae8dSAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
11a35dae8dSAdrian Chadd  *    notice, this list of conditions and the following disclaimer,
12a35dae8dSAdrian Chadd  *    without modification.
13a35dae8dSAdrian Chadd  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14a35dae8dSAdrian Chadd  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
15a35dae8dSAdrian Chadd  *    redistribution must be conditioned upon including a substantially
16a35dae8dSAdrian Chadd  *    similar Disclaimer requirement for further binary redistribution.
17a35dae8dSAdrian Chadd  *
18a35dae8dSAdrian Chadd  * NO WARRANTY
19a35dae8dSAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20a35dae8dSAdrian Chadd  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21a35dae8dSAdrian Chadd  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
22a35dae8dSAdrian Chadd  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23a35dae8dSAdrian Chadd  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
24a35dae8dSAdrian Chadd  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a35dae8dSAdrian Chadd  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a35dae8dSAdrian Chadd  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27a35dae8dSAdrian Chadd  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a35dae8dSAdrian Chadd  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29a35dae8dSAdrian Chadd  * THE POSSIBILITY OF SUCH DAMAGES.
30a35dae8dSAdrian Chadd  */
31a35dae8dSAdrian Chadd 
32a35dae8dSAdrian Chadd #include <sys/cdefs.h>
33a35dae8dSAdrian Chadd __FBSDID("$FreeBSD$");
34a35dae8dSAdrian Chadd 
35a35dae8dSAdrian Chadd /*
36a35dae8dSAdrian Chadd  * Driver for the Atheros Wireless LAN controller.
37a35dae8dSAdrian Chadd  *
38a35dae8dSAdrian Chadd  * This software is derived from work of Atsushi Onoe; his contribution
39a35dae8dSAdrian Chadd  * is greatly appreciated.
40a35dae8dSAdrian Chadd  */
41a35dae8dSAdrian Chadd 
42a35dae8dSAdrian Chadd #include "opt_inet.h"
43a35dae8dSAdrian Chadd #include "opt_ath.h"
44a35dae8dSAdrian Chadd /*
45a35dae8dSAdrian Chadd  * This is needed for register operations which are performed
46a35dae8dSAdrian Chadd  * by the driver - eg, calls to ath_hal_gettsf32().
47a35dae8dSAdrian Chadd  *
48a35dae8dSAdrian Chadd  * It's also required for any AH_DEBUG checks in here, eg the
49a35dae8dSAdrian Chadd  * module dependencies.
50a35dae8dSAdrian Chadd  */
51a35dae8dSAdrian Chadd #include "opt_ah.h"
52a35dae8dSAdrian Chadd #include "opt_wlan.h"
53a35dae8dSAdrian Chadd 
54a35dae8dSAdrian Chadd #include <sys/param.h>
55a35dae8dSAdrian Chadd #include <sys/systm.h>
56a35dae8dSAdrian Chadd #include <sys/sysctl.h>
57a35dae8dSAdrian Chadd #include <sys/mbuf.h>
58a35dae8dSAdrian Chadd #include <sys/malloc.h>
59a35dae8dSAdrian Chadd #include <sys/lock.h>
60a35dae8dSAdrian Chadd #include <sys/mutex.h>
61a35dae8dSAdrian Chadd #include <sys/kernel.h>
62a35dae8dSAdrian Chadd #include <sys/socket.h>
63a35dae8dSAdrian Chadd #include <sys/sockio.h>
64a35dae8dSAdrian Chadd #include <sys/errno.h>
65a35dae8dSAdrian Chadd #include <sys/callout.h>
66a35dae8dSAdrian Chadd #include <sys/bus.h>
67a35dae8dSAdrian Chadd #include <sys/endian.h>
68a35dae8dSAdrian Chadd #include <sys/kthread.h>
69a35dae8dSAdrian Chadd #include <sys/taskqueue.h>
70a35dae8dSAdrian Chadd #include <sys/priv.h>
71a35dae8dSAdrian Chadd #include <sys/module.h>
72a35dae8dSAdrian Chadd #include <sys/ktr.h>
73a35dae8dSAdrian Chadd #include <sys/smp.h>	/* for mp_ncpus */
74a35dae8dSAdrian Chadd 
75a35dae8dSAdrian Chadd #include <machine/bus.h>
76a35dae8dSAdrian Chadd 
77a35dae8dSAdrian Chadd #include <net/if.h>
7876039bc8SGleb Smirnoff #include <net/if_var.h>
79a35dae8dSAdrian Chadd #include <net/if_dl.h>
80a35dae8dSAdrian Chadd #include <net/if_media.h>
81a35dae8dSAdrian Chadd #include <net/if_types.h>
82a35dae8dSAdrian Chadd #include <net/if_arp.h>
83a35dae8dSAdrian Chadd #include <net/ethernet.h>
84a35dae8dSAdrian Chadd #include <net/if_llc.h>
85a35dae8dSAdrian Chadd 
86a35dae8dSAdrian Chadd #include <net80211/ieee80211_var.h>
87a35dae8dSAdrian Chadd #include <net80211/ieee80211_regdomain.h>
88a35dae8dSAdrian Chadd #ifdef IEEE80211_SUPPORT_SUPERG
89a35dae8dSAdrian Chadd #include <net80211/ieee80211_superg.h>
90a35dae8dSAdrian Chadd #endif
91a35dae8dSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
92a35dae8dSAdrian Chadd #include <net80211/ieee80211_tdma.h>
93a35dae8dSAdrian Chadd #endif
94a35dae8dSAdrian Chadd 
95a35dae8dSAdrian Chadd #include <net/bpf.h>
96a35dae8dSAdrian Chadd 
97a35dae8dSAdrian Chadd #ifdef INET
98a35dae8dSAdrian Chadd #include <netinet/in.h>
99a35dae8dSAdrian Chadd #include <netinet/if_ether.h>
100a35dae8dSAdrian Chadd #endif
101a35dae8dSAdrian Chadd 
102a35dae8dSAdrian Chadd #include <dev/ath/if_athvar.h>
103a35dae8dSAdrian Chadd #include <dev/ath/ath_hal/ah_devid.h>		/* XXX for softled */
104a35dae8dSAdrian Chadd #include <dev/ath/ath_hal/ah_diagcodes.h>
105a35dae8dSAdrian Chadd 
106a35dae8dSAdrian Chadd #include <dev/ath/if_ath_debug.h>
107a35dae8dSAdrian Chadd #include <dev/ath/if_ath_misc.h>
108a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tsf.h>
109a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tx.h>
110a35dae8dSAdrian Chadd #include <dev/ath/if_ath_sysctl.h>
111a35dae8dSAdrian Chadd #include <dev/ath/if_ath_led.h>
112a35dae8dSAdrian Chadd #include <dev/ath/if_ath_keycache.h>
113a35dae8dSAdrian Chadd #include <dev/ath/if_ath_rx.h>
114a35dae8dSAdrian Chadd #include <dev/ath/if_ath_beacon.h>
115a35dae8dSAdrian Chadd #include <dev/ath/if_athdfs.h>
116a35dae8dSAdrian Chadd 
117a35dae8dSAdrian Chadd #ifdef ATH_TX99_DIAG
118a35dae8dSAdrian Chadd #include <dev/ath/ath_tx99/ath_tx99.h>
119a35dae8dSAdrian Chadd #endif
120a35dae8dSAdrian Chadd 
1214bda23f9SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1224bda23f9SAdrian Chadd #include <dev/ath/if_ath_alq.h>
1234bda23f9SAdrian Chadd #endif
1244bda23f9SAdrian Chadd 
125a35dae8dSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
126a35dae8dSAdrian Chadd #include <dev/ath/if_ath_tdma.h>
127a35dae8dSAdrian Chadd 
128a35dae8dSAdrian Chadd static void	ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt,
129a35dae8dSAdrian Chadd 		    u_int32_t bintval);
130a35dae8dSAdrian Chadd static void	ath_tdma_bintvalsetup(struct ath_softc *sc,
131a35dae8dSAdrian Chadd 		    const struct ieee80211_tdma_state *tdma);
132a35dae8dSAdrian Chadd #endif /* IEEE80211_SUPPORT_TDMA */
133a35dae8dSAdrian Chadd 
134a35dae8dSAdrian Chadd #ifdef IEEE80211_SUPPORT_TDMA
135a35dae8dSAdrian Chadd static void
136a35dae8dSAdrian Chadd ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval)
137a35dae8dSAdrian Chadd {
138a35dae8dSAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
139a35dae8dSAdrian Chadd 	HAL_BEACON_TIMERS bt;
140a35dae8dSAdrian Chadd 
141a35dae8dSAdrian Chadd 	bt.bt_intval = bintval | HAL_BEACON_ENA;
142a35dae8dSAdrian Chadd 	bt.bt_nexttbtt = nexttbtt;
143a35dae8dSAdrian Chadd 	bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep;
144a35dae8dSAdrian Chadd 	bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep;
145a35dae8dSAdrian Chadd 	bt.bt_nextatim = nexttbtt+1;
146a35dae8dSAdrian Chadd 	/* Enables TBTT, DBA, SWBA timers by default */
147a35dae8dSAdrian Chadd 	bt.bt_flags = 0;
1484bda23f9SAdrian Chadd #if 0
1494bda23f9SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
1504bda23f9SAdrian Chadd 	    "%s: intval=%d (0x%08x) nexttbtt=%u (0x%08x), nextdba=%u (0x%08x), nextswba=%u (0x%08x),nextatim=%u (0x%08x)\n",
1514bda23f9SAdrian Chadd 	    __func__,
1524bda23f9SAdrian Chadd 	    bt.bt_intval,
1534bda23f9SAdrian Chadd 	    bt.bt_intval,
1544bda23f9SAdrian Chadd 	    bt.bt_nexttbtt,
1554bda23f9SAdrian Chadd 	    bt.bt_nexttbtt,
1564bda23f9SAdrian Chadd 	    bt.bt_nextdba,
1574bda23f9SAdrian Chadd 	    bt.bt_nextdba,
1584bda23f9SAdrian Chadd 	    bt.bt_nextswba,
1594bda23f9SAdrian Chadd 	    bt.bt_nextswba,
1604bda23f9SAdrian Chadd 	    bt.bt_nextatim,
1614bda23f9SAdrian Chadd 	    bt.bt_nextatim);
1624bda23f9SAdrian Chadd #endif
1634bda23f9SAdrian Chadd 
164584295fcSAdrian Chadd #ifdef	ATH_DEBUG_ALQ
1654bda23f9SAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_SET)) {
1664bda23f9SAdrian Chadd 		struct if_ath_alq_tdma_timer_set t;
1674bda23f9SAdrian Chadd 		t.bt_intval = htobe32(bt.bt_intval);
1684bda23f9SAdrian Chadd 		t.bt_nexttbtt = htobe32(bt.bt_nexttbtt);
1694bda23f9SAdrian Chadd 		t.bt_nextdba = htobe32(bt.bt_nextdba);
1704bda23f9SAdrian Chadd 		t.bt_nextswba = htobe32(bt.bt_nextswba);
1714bda23f9SAdrian Chadd 		t.bt_nextatim = htobe32(bt.bt_nextatim);
1724bda23f9SAdrian Chadd 		t.bt_flags = htobe32(bt.bt_flags);
1734bda23f9SAdrian Chadd 		t.sc_tdmadbaprep = htobe32(sc->sc_tdmadbaprep);
1744bda23f9SAdrian Chadd 		t.sc_tdmaswbaprep = htobe32(sc->sc_tdmaswbaprep);
1754bda23f9SAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_SET,
1764bda23f9SAdrian Chadd 		    sizeof(t), (char *) &t);
1774bda23f9SAdrian Chadd 	}
178584295fcSAdrian Chadd #endif
1794bda23f9SAdrian Chadd 
1804bda23f9SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
1814bda23f9SAdrian Chadd 	    "%s: nexttbtt=%u (0x%08x), nexttbtt tsf=%lld (0x%08llx)\n",
1824bda23f9SAdrian Chadd 	    __func__,
1834bda23f9SAdrian Chadd 	    bt.bt_nexttbtt,
1844bda23f9SAdrian Chadd 	    bt.bt_nexttbtt,
1854bda23f9SAdrian Chadd 	    (long long) ( ((u_int64_t) (bt.bt_nexttbtt)) << 10),
1864bda23f9SAdrian Chadd 	    (long long) ( ((u_int64_t) (bt.bt_nexttbtt)) << 10));
187a35dae8dSAdrian Chadd 	ath_hal_beaconsettimers(ah, &bt);
188a35dae8dSAdrian Chadd }
189a35dae8dSAdrian Chadd 
190a35dae8dSAdrian Chadd /*
191a35dae8dSAdrian Chadd  * Calculate the beacon interval.  This is periodic in the
192a35dae8dSAdrian Chadd  * superframe for the bss.  We assume each station is configured
193a35dae8dSAdrian Chadd  * identically wrt transmit rate so the guard time we calculate
194a35dae8dSAdrian Chadd  * above will be the same on all stations.  Note we need to
195a35dae8dSAdrian Chadd  * factor in the xmit time because the hardware will schedule
196a35dae8dSAdrian Chadd  * a frame for transmit if the start of the frame is within
197a35dae8dSAdrian Chadd  * the burst time.  When we get hardware that properly kills
198a35dae8dSAdrian Chadd  * frames in the PCU we can reduce/eliminate the guard time.
199a35dae8dSAdrian Chadd  *
200a35dae8dSAdrian Chadd  * Roundup to 1024 is so we have 1 TU buffer in the guard time
201a35dae8dSAdrian Chadd  * to deal with the granularity of the nexttbtt timer.  11n MAC's
202a35dae8dSAdrian Chadd  * with 1us timer granularity should allow us to reduce/eliminate
203a35dae8dSAdrian Chadd  * this.
204a35dae8dSAdrian Chadd  */
205a35dae8dSAdrian Chadd static void
206a35dae8dSAdrian Chadd ath_tdma_bintvalsetup(struct ath_softc *sc,
207a35dae8dSAdrian Chadd 	const struct ieee80211_tdma_state *tdma)
208a35dae8dSAdrian Chadd {
209a35dae8dSAdrian Chadd 	/* copy from vap state (XXX check all vaps have same value?) */
210a35dae8dSAdrian Chadd 	sc->sc_tdmaslotlen = tdma->tdma_slotlen;
211a35dae8dSAdrian Chadd 
212a35dae8dSAdrian Chadd 	sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) *
213a35dae8dSAdrian Chadd 		tdma->tdma_slotcnt, 1024);
214a35dae8dSAdrian Chadd 	sc->sc_tdmabintval >>= 10;		/* TSF -> TU */
215a35dae8dSAdrian Chadd 	if (sc->sc_tdmabintval & 1)
216a35dae8dSAdrian Chadd 		sc->sc_tdmabintval++;
217a35dae8dSAdrian Chadd 
218a35dae8dSAdrian Chadd 	if (tdma->tdma_slot == 0) {
219a35dae8dSAdrian Chadd 		/*
220a35dae8dSAdrian Chadd 		 * Only slot 0 beacons; other slots respond.
221a35dae8dSAdrian Chadd 		 */
222a35dae8dSAdrian Chadd 		sc->sc_imask |= HAL_INT_SWBA;
223a35dae8dSAdrian Chadd 		sc->sc_tdmaswba = 0;		/* beacon immediately */
224a35dae8dSAdrian Chadd 	} else {
225a35dae8dSAdrian Chadd 		/* XXX all vaps must be slot 0 or slot !0 */
226a35dae8dSAdrian Chadd 		sc->sc_imask &= ~HAL_INT_SWBA;
227a35dae8dSAdrian Chadd 	}
228a35dae8dSAdrian Chadd }
229a35dae8dSAdrian Chadd 
230a35dae8dSAdrian Chadd /*
231a35dae8dSAdrian Chadd  * Max 802.11 overhead.  This assumes no 4-address frames and
232a35dae8dSAdrian Chadd  * the encapsulation done by ieee80211_encap (llc).  We also
233a35dae8dSAdrian Chadd  * include potential crypto overhead.
234a35dae8dSAdrian Chadd  */
235a35dae8dSAdrian Chadd #define	IEEE80211_MAXOVERHEAD \
236a35dae8dSAdrian Chadd 	(sizeof(struct ieee80211_qosframe) \
237a35dae8dSAdrian Chadd 	 + sizeof(struct llc) \
238a35dae8dSAdrian Chadd 	 + IEEE80211_ADDR_LEN \
239a35dae8dSAdrian Chadd 	 + IEEE80211_WEP_IVLEN \
240a35dae8dSAdrian Chadd 	 + IEEE80211_WEP_KIDLEN \
241a35dae8dSAdrian Chadd 	 + IEEE80211_WEP_CRCLEN \
242a35dae8dSAdrian Chadd 	 + IEEE80211_WEP_MICLEN \
243a35dae8dSAdrian Chadd 	 + IEEE80211_CRC_LEN)
244a35dae8dSAdrian Chadd 
245a35dae8dSAdrian Chadd /*
246a35dae8dSAdrian Chadd  * Setup initially for tdma operation.  Start the beacon
247a35dae8dSAdrian Chadd  * timers and enable SWBA if we are slot 0.  Otherwise
248a35dae8dSAdrian Chadd  * we wait for slot 0 to arrive so we can sync up before
249a35dae8dSAdrian Chadd  * starting to transmit.
250a35dae8dSAdrian Chadd  */
251a35dae8dSAdrian Chadd void
252a35dae8dSAdrian Chadd ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap)
253a35dae8dSAdrian Chadd {
254a35dae8dSAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
2557a79cebfSGleb Smirnoff 	struct ieee80211com *ic = &sc->sc_ic;
256a35dae8dSAdrian Chadd 	const struct ieee80211_txparam *tp;
257a35dae8dSAdrian Chadd 	const struct ieee80211_tdma_state *tdma = NULL;
258a35dae8dSAdrian Chadd 	int rix;
259a35dae8dSAdrian Chadd 
260a35dae8dSAdrian Chadd 	if (vap == NULL) {
261a35dae8dSAdrian Chadd 		vap = TAILQ_FIRST(&ic->ic_vaps);   /* XXX */
262a35dae8dSAdrian Chadd 		if (vap == NULL) {
26376e6fd5dSGleb Smirnoff 			device_printf(sc->sc_dev, "%s: no vaps?\n", __func__);
264a35dae8dSAdrian Chadd 			return;
265a35dae8dSAdrian Chadd 		}
266a35dae8dSAdrian Chadd 	}
267a35dae8dSAdrian Chadd 	/* XXX should take a locked ref to iv_bss */
268a35dae8dSAdrian Chadd 	tp = vap->iv_bss->ni_txparms;
269a35dae8dSAdrian Chadd 	/*
270a35dae8dSAdrian Chadd 	 * Calculate the guard time for each slot.  This is the
271a35dae8dSAdrian Chadd 	 * time to send a maximal-size frame according to the
272a35dae8dSAdrian Chadd 	 * fixed/lowest transmit rate.  Note that the interface
273a35dae8dSAdrian Chadd 	 * mtu does not include the 802.11 overhead so we must
274a35dae8dSAdrian Chadd 	 * tack that on (ath_hal_computetxtime includes the
27528323addSBryan Drewery 	 * preamble and plcp in its calculation).
276a35dae8dSAdrian Chadd 	 */
277a35dae8dSAdrian Chadd 	tdma = vap->iv_tdma;
278a35dae8dSAdrian Chadd 	if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE)
279a35dae8dSAdrian Chadd 		rix = ath_tx_findrix(sc, tp->ucastrate);
280a35dae8dSAdrian Chadd 	else
281a35dae8dSAdrian Chadd 		rix = ath_tx_findrix(sc, tp->mcastrate);
282dd6a574eSAdrian Chadd 
283dd6a574eSAdrian Chadd 	/*
284dd6a574eSAdrian Chadd 	 * If the chip supports enforcing TxOP on transmission,
285dd6a574eSAdrian Chadd 	 * we can just delete the guard window.  It isn't at all required.
286dd6a574eSAdrian Chadd 	 */
287dd6a574eSAdrian Chadd 	if (sc->sc_hasenforcetxop) {
288dd6a574eSAdrian Chadd 		sc->sc_tdmaguard = 0;
289dd6a574eSAdrian Chadd 	} else {
290a35dae8dSAdrian Chadd 		/* XXX short preamble assumed */
291dd6a574eSAdrian Chadd 		/* XXX non-11n rate assumed */
292a35dae8dSAdrian Chadd 		sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates,
293ec22a3a2SJustin Hibbits 		    if_getmtu(vap->iv_ifp) + IEEE80211_MAXOVERHEAD, rix, AH_TRUE,
2947ff1939dSAdrian Chadd 		    AH_TRUE);
295dd6a574eSAdrian Chadd 	}
296a35dae8dSAdrian Chadd 
297a35dae8dSAdrian Chadd 	ath_hal_intrset(ah, 0);
298a35dae8dSAdrian Chadd 
299a35dae8dSAdrian Chadd 	ath_beaconq_config(sc);			/* setup h/w beacon q */
300a35dae8dSAdrian Chadd 	if (sc->sc_setcca)
301a35dae8dSAdrian Chadd 		ath_hal_setcca(ah, AH_FALSE);	/* disable CCA */
302a35dae8dSAdrian Chadd 	ath_tdma_bintvalsetup(sc, tdma);	/* calculate beacon interval */
303a35dae8dSAdrian Chadd 	ath_tdma_settimers(sc, sc->sc_tdmabintval,
304a35dae8dSAdrian Chadd 		sc->sc_tdmabintval | HAL_BEACON_RESET_TSF);
305a35dae8dSAdrian Chadd 	sc->sc_syncbeacon = 0;
306a35dae8dSAdrian Chadd 
307a35dae8dSAdrian Chadd 	sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER;
308a35dae8dSAdrian Chadd 	sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER;
309a35dae8dSAdrian Chadd 
310a35dae8dSAdrian Chadd 	ath_hal_intrset(ah, sc->sc_imask);
311a35dae8dSAdrian Chadd 
312a35dae8dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u "
313a35dae8dSAdrian Chadd 	    "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__,
314a35dae8dSAdrian Chadd 	    tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt,
315a35dae8dSAdrian Chadd 	    tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval,
316a35dae8dSAdrian Chadd 	    sc->sc_tdmadbaprep);
3174bda23f9SAdrian Chadd 
3184bda23f9SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
3194bda23f9SAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_CONFIG)) {
3204bda23f9SAdrian Chadd 		struct if_ath_alq_tdma_timer_config t;
3214bda23f9SAdrian Chadd 
3224bda23f9SAdrian Chadd 		t.tdma_slot = htobe32(tdma->tdma_slot);
3234bda23f9SAdrian Chadd 		t.tdma_slotlen = htobe32(tdma->tdma_slotlen);
3244bda23f9SAdrian Chadd 		t.tdma_slotcnt = htobe32(tdma->tdma_slotcnt);
3254bda23f9SAdrian Chadd 		t.tdma_bintval = htobe32(tdma->tdma_bintval);
3264bda23f9SAdrian Chadd 		t.tdma_guard = htobe32(sc->sc_tdmaguard);
3274bda23f9SAdrian Chadd 		t.tdma_scbintval = htobe32(sc->sc_tdmabintval);
3284bda23f9SAdrian Chadd 		t.tdma_dbaprep = htobe32(sc->sc_tdmadbaprep);
3294bda23f9SAdrian Chadd 
3304bda23f9SAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_CONFIG,
3314bda23f9SAdrian Chadd 		    sizeof(t), (char *) &t);
3324bda23f9SAdrian Chadd 	}
3334bda23f9SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
334a35dae8dSAdrian Chadd }
335a35dae8dSAdrian Chadd 
336a35dae8dSAdrian Chadd /*
337a35dae8dSAdrian Chadd  * Update tdma operation.  Called from the 802.11 layer
338a35dae8dSAdrian Chadd  * when a beacon is received from the TDMA station operating
339a35dae8dSAdrian Chadd  * in the slot immediately preceding us in the bss.  Use
340a35dae8dSAdrian Chadd  * the rx timestamp for the beacon frame to update our
341a35dae8dSAdrian Chadd  * beacon timers so we follow their schedule.  Note that
342a35dae8dSAdrian Chadd  * by using the rx timestamp we implicitly include the
343a35dae8dSAdrian Chadd  * propagation delay in our schedule.
344821311eaSAdrian Chadd  *
345821311eaSAdrian Chadd  * XXX TODO: since the changes for the AR5416 and later chips
346821311eaSAdrian Chadd  * involved changing the TSF/TU calculations, we need to make
347821311eaSAdrian Chadd  * sure that various calculations wrap consistently.
348821311eaSAdrian Chadd  *
349821311eaSAdrian Chadd  * A lot of the problems stemmed from the calculations wrapping
350821311eaSAdrian Chadd  * at 65,535 TU.  Since a lot of the math is still being done in
351821311eaSAdrian Chadd  * TU, please audit it to ensure that when the TU values programmed
352821311eaSAdrian Chadd  * into the timers wrap at (2^31)-1 TSF, all the various terms
353821311eaSAdrian Chadd  * wrap consistently.
354a35dae8dSAdrian Chadd  */
355a35dae8dSAdrian Chadd void
356a35dae8dSAdrian Chadd ath_tdma_update(struct ieee80211_node *ni,
357a35dae8dSAdrian Chadd 	const struct ieee80211_tdma_param *tdma, int changed)
358a35dae8dSAdrian Chadd {
359a35dae8dSAdrian Chadd #define	TSF_TO_TU(_h,_l) \
360a35dae8dSAdrian Chadd 	((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
361a35dae8dSAdrian Chadd #define	TU_TO_TSF(_tu)	(((u_int64_t)(_tu)) << 10)
362a35dae8dSAdrian Chadd 	struct ieee80211vap *vap = ni->ni_vap;
363a35dae8dSAdrian Chadd 	struct ieee80211com *ic = ni->ni_ic;
3643797bf08SAdrian Chadd 	struct ath_softc *sc = ic->ic_softc;
365a35dae8dSAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
366a35dae8dSAdrian Chadd 	const HAL_RATE_TABLE *rt = sc->sc_currates;
367e6f1a34aSAdrian Chadd 	u_int64_t tsf, rstamp, nextslot, nexttbtt, nexttbtt_full;
368a35dae8dSAdrian Chadd 	u_int32_t txtime, nextslottu;
369a35dae8dSAdrian Chadd 	int32_t tudelta, tsfdelta;
370a35dae8dSAdrian Chadd 	const struct ath_rx_status *rs;
371a35dae8dSAdrian Chadd 	int rix;
372a35dae8dSAdrian Chadd 
373a35dae8dSAdrian Chadd 	sc->sc_stats.ast_tdma_update++;
374a35dae8dSAdrian Chadd 
375a35dae8dSAdrian Chadd 	/*
376a35dae8dSAdrian Chadd 	 * Check for and adopt configuration changes.
377a35dae8dSAdrian Chadd 	 */
378a35dae8dSAdrian Chadd 	if (changed != 0) {
379a35dae8dSAdrian Chadd 		const struct ieee80211_tdma_state *ts = vap->iv_tdma;
380a35dae8dSAdrian Chadd 
381a35dae8dSAdrian Chadd 		ath_tdma_bintvalsetup(sc, ts);
382a35dae8dSAdrian Chadd 		if (changed & TDMA_UPDATE_SLOTLEN)
383a35dae8dSAdrian Chadd 			ath_wme_update(ic);
384a35dae8dSAdrian Chadd 
385a35dae8dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA,
386a35dae8dSAdrian Chadd 		    "%s: adopt slot %u slotcnt %u slotlen %u us "
387a35dae8dSAdrian Chadd 		    "bintval %u TU\n", __func__,
388a35dae8dSAdrian Chadd 		    ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen,
389a35dae8dSAdrian Chadd 		    sc->sc_tdmabintval);
390a35dae8dSAdrian Chadd 
391a35dae8dSAdrian Chadd 		/* XXX right? */
392a35dae8dSAdrian Chadd 		ath_hal_intrset(ah, sc->sc_imask);
393a35dae8dSAdrian Chadd 		/* NB: beacon timers programmed below */
394a35dae8dSAdrian Chadd 	}
395a35dae8dSAdrian Chadd 
396a35dae8dSAdrian Chadd 	/* extend rx timestamp to 64 bits */
397a35dae8dSAdrian Chadd 	rs = sc->sc_lastrs;
398a35dae8dSAdrian Chadd 	tsf = ath_hal_gettsf64(ah);
399a35dae8dSAdrian Chadd 	rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf);
400a35dae8dSAdrian Chadd 	/*
401a35dae8dSAdrian Chadd 	 * The rx timestamp is set by the hardware on completing
402a35dae8dSAdrian Chadd 	 * reception (at the point where the rx descriptor is DMA'd
403a35dae8dSAdrian Chadd 	 * to the host).  To find the start of our next slot we
404a35dae8dSAdrian Chadd 	 * must adjust this time by the time required to send
405a35dae8dSAdrian Chadd 	 * the packet just received.
406a35dae8dSAdrian Chadd 	 */
407a35dae8dSAdrian Chadd 	rix = rt->rateCodeToIndex[rs->rs_rate];
408dd6a574eSAdrian Chadd 
409dd6a574eSAdrian Chadd 	/*
410dd6a574eSAdrian Chadd 	 * To calculate the packet duration for legacy rates, we
411dd6a574eSAdrian Chadd 	 * only need the rix and preamble.
412dd6a574eSAdrian Chadd 	 *
413dd6a574eSAdrian Chadd 	 * For 11n non-aggregate frames, we also need the channel
414dd6a574eSAdrian Chadd 	 * width and short/long guard interval.
415dd6a574eSAdrian Chadd 	 *
416dd6a574eSAdrian Chadd 	 * For 11n aggregate frames, the required hacks are a little
417dd6a574eSAdrian Chadd 	 * more subtle.  You need to figure out the frame duration
418dd6a574eSAdrian Chadd 	 * for each frame, including the delimiters.  However, when
419dd6a574eSAdrian Chadd 	 * a frame isn't received successfully, we won't hear it
420dd6a574eSAdrian Chadd 	 * (unless you enable reception of CRC errored frames), so
421dd6a574eSAdrian Chadd 	 * your duration calculation is going to be off.
422dd6a574eSAdrian Chadd 	 *
423dd6a574eSAdrian Chadd 	 * However, we can assume that the beacon frames won't be
424dd6a574eSAdrian Chadd 	 * transmitted as aggregate frames, so we should be okay.
425dd6a574eSAdrian Chadd 	 * Just add a check to ensure that we aren't handed something
426dd6a574eSAdrian Chadd 	 * bad.
427dd6a574eSAdrian Chadd 	 *
428dd6a574eSAdrian Chadd 	 * For ath_hal_pkt_txtime() - for 11n rates, shortPreamble is
429dd6a574eSAdrian Chadd 	 * actually short guard interval. For legacy rates,
430dd6a574eSAdrian Chadd 	 * it's short preamble.
431dd6a574eSAdrian Chadd 	 */
432dd6a574eSAdrian Chadd 	txtime = ath_hal_pkt_txtime(ah, rt, rs->rs_datalen,
433dd6a574eSAdrian Chadd 	    rix,
434dd6a574eSAdrian Chadd 	    !! (rs->rs_flags & HAL_RX_2040),
435dd6a574eSAdrian Chadd 	    (rix & 0x80) ?
4367ff1939dSAdrian Chadd 	      (! (rs->rs_flags & HAL_RX_GI)) : rt->info[rix].shortPreamble,
4377ff1939dSAdrian Chadd 	    AH_TRUE);
438a35dae8dSAdrian Chadd 	/* NB: << 9 is to cvt to TU and /2 */
439a35dae8dSAdrian Chadd 	nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9);
4404bda23f9SAdrian Chadd 
441e6f1a34aSAdrian Chadd 	/*
442e6f1a34aSAdrian Chadd 	 * For 802.11n chips: nextslottu needs to be the full TSF space,
443e6f1a34aSAdrian Chadd 	 * not just 0..65535 TU.
444e6f1a34aSAdrian Chadd 	 */
445e6f1a34aSAdrian Chadd 	nextslottu = TSF_TO_TU(nextslot>>32, nextslot);
446a35dae8dSAdrian Chadd 	/*
447a35dae8dSAdrian Chadd 	 * Retrieve the hardware NextTBTT in usecs
448a35dae8dSAdrian Chadd 	 * and calculate the difference between what the
449a35dae8dSAdrian Chadd 	 * other station thinks and what we have programmed.  This
450a35dae8dSAdrian Chadd 	 * lets us figure how to adjust our timers to match.  The
451a35dae8dSAdrian Chadd 	 * adjustments are done by pulling the TSF forward and possibly
452a35dae8dSAdrian Chadd 	 * rewriting the beacon timers.
453a35dae8dSAdrian Chadd 	 */
454ddee9211SAdrian Chadd 	/*
455ddee9211SAdrian Chadd 	 * The logic here assumes the nexttbtt counter is in TSF
456ddee9211SAdrian Chadd 	 * but the prr-11n NICs are in TU.  The HAL shifts them
457ddee9211SAdrian Chadd 	 * to TSF but there's two important differences:
458ddee9211SAdrian Chadd 	 *
459ddee9211SAdrian Chadd 	 * + The TU->TSF values have 0's for the low 9 bits, and
460ddee9211SAdrian Chadd 	 * + The counter wraps at TU_TO_TSF(HAL_BEACON_PERIOD + 1) for
461ddee9211SAdrian Chadd 	 *   the pre-11n NICs, but not for the 11n NICs.
462ddee9211SAdrian Chadd 	 *
463ddee9211SAdrian Chadd 	 * So for now, just make sure the nexttbtt value we get
464ddee9211SAdrian Chadd 	 * matches the second issue or once nexttbtt exceeds this
465ddee9211SAdrian Chadd 	 * value, tsfdelta ends up becoming very negative and all
466ddee9211SAdrian Chadd 	 * of the adjustments get very messed up.
467ddee9211SAdrian Chadd 	 */
468e6f1a34aSAdrian Chadd 
469e6f1a34aSAdrian Chadd 	/*
470e6f1a34aSAdrian Chadd 	 * We need to track the full nexttbtt rather than having it
471e6f1a34aSAdrian Chadd 	 * truncated at HAL_BEACON_PERIOD, as programming the
472e6f1a34aSAdrian Chadd 	 * nexttbtt (and related) registers for the 11n chips is
473e6f1a34aSAdrian Chadd 	 * actually going to take the full 32 bit space, rather than
474e6f1a34aSAdrian Chadd 	 * just 0..65535 TU.
475e6f1a34aSAdrian Chadd 	 */
476e6f1a34aSAdrian Chadd 	nexttbtt_full = ath_hal_getnexttbtt(ah);
477e6f1a34aSAdrian Chadd 	nexttbtt = nexttbtt_full % (TU_TO_TSF(HAL_BEACON_PERIOD + 1));
478a35dae8dSAdrian Chadd 	tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt);
479a35dae8dSAdrian Chadd 
480a35dae8dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
4817c783791SAdrian Chadd 	    "rs->rstamp %llu rstamp %llu tsf %llu txtime %d, nextslot %llu, "
4827c783791SAdrian Chadd 	    "nextslottu %d, nextslottume %d\n",
483e3665aeeSAdrian Chadd 	    (unsigned long long) rs->rs_tstamp,
484e3665aeeSAdrian Chadd 	    (unsigned long long) rstamp,
485e3665aeeSAdrian Chadd 	    (unsigned long long) tsf, txtime,
486e3665aeeSAdrian Chadd 	    (unsigned long long) nextslot,
487e3665aeeSAdrian Chadd 	    nextslottu, TSF_TO_TU(nextslot >> 32, nextslot));
4887c783791SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA,
4897c783791SAdrian Chadd 	    "  beacon tstamp: %llu (0x%016llx)\n",
490e3665aeeSAdrian Chadd 	    (unsigned long long) le64toh(ni->ni_tstamp.tsf),
491e3665aeeSAdrian Chadd 	    (unsigned long long) le64toh(ni->ni_tstamp.tsf));
4927c783791SAdrian Chadd 
4937c783791SAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
4944bda23f9SAdrian Chadd 	    "nexttbtt %llu (0x%08llx) tsfdelta %d avg +%d/-%d\n",
495e3665aeeSAdrian Chadd 	    (unsigned long long) nexttbtt,
4964bda23f9SAdrian Chadd 	    (long long) nexttbtt,
4974bda23f9SAdrian Chadd 	    tsfdelta,
498a35dae8dSAdrian Chadd 	    TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam));
499a35dae8dSAdrian Chadd 
500a35dae8dSAdrian Chadd 	if (tsfdelta < 0) {
501a35dae8dSAdrian Chadd 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
502a35dae8dSAdrian Chadd 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta);
503a35dae8dSAdrian Chadd 		tsfdelta = -tsfdelta % 1024;
504a35dae8dSAdrian Chadd 		nextslottu++;
505a35dae8dSAdrian Chadd 	} else if (tsfdelta > 0) {
506a35dae8dSAdrian Chadd 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta);
507a35dae8dSAdrian Chadd 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
508a35dae8dSAdrian Chadd 		tsfdelta = 1024 - (tsfdelta % 1024);
509a35dae8dSAdrian Chadd 		nextslottu++;
510a35dae8dSAdrian Chadd 	} else {
511a35dae8dSAdrian Chadd 		TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0);
512a35dae8dSAdrian Chadd 		TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0);
513a35dae8dSAdrian Chadd 	}
514e6f1a34aSAdrian Chadd 	tudelta = nextslottu - TSF_TO_TU(nexttbtt_full >> 32, nexttbtt_full);
515a35dae8dSAdrian Chadd 
5167c783791SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
5177c783791SAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_BEACON_STATE)) {
5187c783791SAdrian Chadd 		struct if_ath_alq_tdma_beacon_state t;
5197c783791SAdrian Chadd 		t.rx_tsf = htobe64(rstamp);
5207c783791SAdrian Chadd 		t.beacon_tsf = htobe64(le64toh(ni->ni_tstamp.tsf));
5217c783791SAdrian Chadd 		t.tsf64 = htobe64(tsf);
5227c783791SAdrian Chadd 		t.nextslot_tsf = htobe64(nextslot);
5237c783791SAdrian Chadd 		t.nextslot_tu = htobe32(nextslottu);
5247c783791SAdrian Chadd 		t.txtime = htobe32(txtime);
5257c783791SAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_BEACON_STATE,
5267c783791SAdrian Chadd 		    sizeof(t), (char *) &t);
5277c783791SAdrian Chadd 	}
5287c783791SAdrian Chadd 
5297c783791SAdrian Chadd 	if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_SLOT_CALC)) {
5307c783791SAdrian Chadd 		struct if_ath_alq_tdma_slot_calc t;
5317c783791SAdrian Chadd 
5327c783791SAdrian Chadd 		t.nexttbtt = htobe64(nexttbtt_full);
5337c783791SAdrian Chadd 		t.next_slot = htobe64(nextslot);
5347c783791SAdrian Chadd 		t.tsfdelta = htobe32(tsfdelta);
5357c783791SAdrian Chadd 		t.avg_plus = htobe32(TDMA_AVG(sc->sc_avgtsfdeltap));
5367c783791SAdrian Chadd 		t.avg_minus = htobe32(TDMA_AVG(sc->sc_avgtsfdeltam));
5377c783791SAdrian Chadd 
5387c783791SAdrian Chadd 		if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_SLOT_CALC,
5397c783791SAdrian Chadd 		    sizeof(t), (char *) &t);
5407c783791SAdrian Chadd 	}
5417c783791SAdrian Chadd #endif
5427c783791SAdrian Chadd 
543a35dae8dSAdrian Chadd 	/*
544a35dae8dSAdrian Chadd 	 * Copy sender's timetstamp into tdma ie so they can
545a35dae8dSAdrian Chadd 	 * calculate roundtrip time.  We submit a beacon frame
546a35dae8dSAdrian Chadd 	 * below after any timer adjustment.  The frame goes out
547a35dae8dSAdrian Chadd 	 * at the next TBTT so the sender can calculate the
548a35dae8dSAdrian Chadd 	 * roundtrip by inspecting the tdma ie in our beacon frame.
549a35dae8dSAdrian Chadd 	 *
550a35dae8dSAdrian Chadd 	 * NB: This tstamp is subtlely preserved when
551a35dae8dSAdrian Chadd 	 *     IEEE80211_BEACON_TDMA is marked (e.g. when the
552a35dae8dSAdrian Chadd 	 *     slot position changes) because ieee80211_add_tdma
553a35dae8dSAdrian Chadd 	 *     skips over the data.
554a35dae8dSAdrian Chadd 	 */
5550cf00015SAdrian Chadd 	memcpy(vap->iv_bcn_off.bo_tdma +
556a35dae8dSAdrian Chadd 		__offsetof(struct ieee80211_tdma_param, tdma_tstamp),
557a35dae8dSAdrian Chadd 		&ni->ni_tstamp.data, 8);
558a35dae8dSAdrian Chadd #if 0
559a35dae8dSAdrian Chadd 	DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
560a35dae8dSAdrian Chadd 	    "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n",
561a35dae8dSAdrian Chadd 	    (unsigned long long) tsf, (unsigned long long) nextslot,
562a35dae8dSAdrian Chadd 	    (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta);
563a35dae8dSAdrian Chadd #endif
564a35dae8dSAdrian Chadd 	/*
565a35dae8dSAdrian Chadd 	 * Adjust the beacon timers only when pulling them forward
566a35dae8dSAdrian Chadd 	 * or when going back by less than the beacon interval.
567a35dae8dSAdrian Chadd 	 * Negative jumps larger than the beacon interval seem to
568a35dae8dSAdrian Chadd 	 * cause the timers to stop and generally cause instability.
569a35dae8dSAdrian Chadd 	 * This basically filters out jumps due to missed beacons.
570a35dae8dSAdrian Chadd 	 */
571a35dae8dSAdrian Chadd 	if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) {
5724bda23f9SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
5734bda23f9SAdrian Chadd 		    "%s: calling ath_tdma_settimers; nextslottu=%d, bintval=%d\n",
5744bda23f9SAdrian Chadd 		    __func__,
5754bda23f9SAdrian Chadd 		    nextslottu,
5764bda23f9SAdrian Chadd 		    sc->sc_tdmabintval);
577a35dae8dSAdrian Chadd 		ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval);
578a35dae8dSAdrian Chadd 		sc->sc_stats.ast_tdma_timers++;
579a35dae8dSAdrian Chadd 	}
580a35dae8dSAdrian Chadd 	if (tsfdelta > 0) {
58184dd5933SAdrian Chadd 		uint64_t tsf;
58284dd5933SAdrian Chadd 
58384dd5933SAdrian Chadd 		/* XXX should just teach ath_hal_adjusttsf() to do this */
58484dd5933SAdrian Chadd 		tsf = ath_hal_gettsf64(ah);
58584dd5933SAdrian Chadd 		ath_hal_settsf64(ah, tsf + tsfdelta);
5864bda23f9SAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_TDMA_TIMER,
5874bda23f9SAdrian Chadd 		    "%s: calling ath_hal_adjusttsf: TSF=%llu, tsfdelta=%d\n",
5884bda23f9SAdrian Chadd 		    __func__,
589e3665aeeSAdrian Chadd 		    (unsigned long long) tsf,
5904bda23f9SAdrian Chadd 		    tsfdelta);
5914bda23f9SAdrian Chadd 
5924bda23f9SAdrian Chadd #ifdef	ATH_DEBUG_ALQ
5934bda23f9SAdrian Chadd 		if (if_ath_alq_checkdebug(&sc->sc_alq,
5944bda23f9SAdrian Chadd 		    ATH_ALQ_TDMA_TSF_ADJUST)) {
5954bda23f9SAdrian Chadd 			struct if_ath_alq_tdma_tsf_adjust t;
5964bda23f9SAdrian Chadd 
5974bda23f9SAdrian Chadd 			t.tsfdelta = htobe32(tsfdelta);
598821311eaSAdrian Chadd 			t.tsf64_old = htobe64(tsf);
599821311eaSAdrian Chadd 			t.tsf64_new = htobe64(tsf + tsfdelta);
6004bda23f9SAdrian Chadd 			if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_TSF_ADJUST,
6014bda23f9SAdrian Chadd 			    sizeof(t), (char *) &t);
6024bda23f9SAdrian Chadd 		}
6034bda23f9SAdrian Chadd #endif	/* ATH_DEBUG_ALQ */
604a35dae8dSAdrian Chadd 		sc->sc_stats.ast_tdma_tsf++;
605a35dae8dSAdrian Chadd 	}
606a35dae8dSAdrian Chadd 	ath_tdma_beacon_send(sc, vap);		/* prepare response */
607a35dae8dSAdrian Chadd #undef TU_TO_TSF
608a35dae8dSAdrian Chadd #undef TSF_TO_TU
609a35dae8dSAdrian Chadd }
610a35dae8dSAdrian Chadd 
611a35dae8dSAdrian Chadd /*
612a35dae8dSAdrian Chadd  * Transmit a beacon frame at SWBA.  Dynamic updates
613a35dae8dSAdrian Chadd  * to the frame contents are done as needed.
614a35dae8dSAdrian Chadd  */
615a35dae8dSAdrian Chadd void
616a35dae8dSAdrian Chadd ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap)
617a35dae8dSAdrian Chadd {
618a35dae8dSAdrian Chadd 	struct ath_hal *ah = sc->sc_ah;
619a35dae8dSAdrian Chadd 	struct ath_buf *bf;
620a35dae8dSAdrian Chadd 	int otherant;
621a35dae8dSAdrian Chadd 
622a35dae8dSAdrian Chadd 	/*
623a35dae8dSAdrian Chadd 	 * Check if the previous beacon has gone out.  If
624a35dae8dSAdrian Chadd 	 * not don't try to post another, skip this period
625a35dae8dSAdrian Chadd 	 * and wait for the next.  Missed beacons indicate
626a35dae8dSAdrian Chadd 	 * a problem and should not occur.  If we miss too
627a35dae8dSAdrian Chadd 	 * many consecutive beacons reset the device.
628a35dae8dSAdrian Chadd 	 */
629a35dae8dSAdrian Chadd 	if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
630a35dae8dSAdrian Chadd 		sc->sc_bmisscount++;
631a35dae8dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_BEACON,
632a35dae8dSAdrian Chadd 			"%s: missed %u consecutive beacons\n",
633a35dae8dSAdrian Chadd 			__func__, sc->sc_bmisscount);
634a35dae8dSAdrian Chadd 		if (sc->sc_bmisscount >= ath_bstuck_threshold)
635a35dae8dSAdrian Chadd 			taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
636a35dae8dSAdrian Chadd 		return;
637a35dae8dSAdrian Chadd 	}
638a35dae8dSAdrian Chadd 	if (sc->sc_bmisscount != 0) {
639a35dae8dSAdrian Chadd 		DPRINTF(sc, ATH_DEBUG_BEACON,
640a35dae8dSAdrian Chadd 			"%s: resume beacon xmit after %u misses\n",
641a35dae8dSAdrian Chadd 			__func__, sc->sc_bmisscount);
642a35dae8dSAdrian Chadd 		sc->sc_bmisscount = 0;
643a35dae8dSAdrian Chadd 	}
644a35dae8dSAdrian Chadd 
645a35dae8dSAdrian Chadd 	/*
646a35dae8dSAdrian Chadd 	 * Check recent per-antenna transmit statistics and flip
647a35dae8dSAdrian Chadd 	 * the default antenna if noticeably more frames went out
648a35dae8dSAdrian Chadd 	 * on the non-default antenna.
649a35dae8dSAdrian Chadd 	 * XXX assumes 2 anntenae
650a35dae8dSAdrian Chadd 	 */
651a35dae8dSAdrian Chadd 	if (!sc->sc_diversity) {
652a35dae8dSAdrian Chadd 		otherant = sc->sc_defant & 1 ? 2 : 1;
653a35dae8dSAdrian Chadd 		if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
654a35dae8dSAdrian Chadd 			ath_setdefantenna(sc, otherant);
655a35dae8dSAdrian Chadd 		sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
656a35dae8dSAdrian Chadd 	}
657a35dae8dSAdrian Chadd 
658a35dae8dSAdrian Chadd 	bf = ath_beacon_generate(sc, vap);
659b837332dSAdrian Chadd 	/* XXX We don't do cabq traffic, but just for completeness .. */
660b837332dSAdrian Chadd 	ATH_TXQ_LOCK(sc->sc_cabq);
661b837332dSAdrian Chadd 	ath_beacon_cabq_start(sc);
662b837332dSAdrian Chadd 	ATH_TXQ_UNLOCK(sc->sc_cabq);
663b837332dSAdrian Chadd 
664a35dae8dSAdrian Chadd 	if (bf != NULL) {
665a35dae8dSAdrian Chadd 		/*
666a35dae8dSAdrian Chadd 		 * Stop any current dma and put the new frame on the queue.
667a35dae8dSAdrian Chadd 		 * This should never fail since we check above that no frames
668a35dae8dSAdrian Chadd 		 * are still pending on the queue.
669a35dae8dSAdrian Chadd 		 */
670b837332dSAdrian Chadd 		if ((! sc->sc_isedma) &&
671b837332dSAdrian Chadd 		    (! ath_hal_stoptxdma(ah, sc->sc_bhalq))) {
672a35dae8dSAdrian Chadd 			DPRINTF(sc, ATH_DEBUG_ANY,
673a35dae8dSAdrian Chadd 				"%s: beacon queue %u did not stop?\n",
674a35dae8dSAdrian Chadd 				__func__, sc->sc_bhalq);
675a35dae8dSAdrian Chadd 			/* NB: the HAL still stops DMA, so proceed */
676a35dae8dSAdrian Chadd 		}
677a35dae8dSAdrian Chadd 		ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
678a35dae8dSAdrian Chadd 		ath_hal_txstart(ah, sc->sc_bhalq);
679a35dae8dSAdrian Chadd 
680a35dae8dSAdrian Chadd 		sc->sc_stats.ast_be_xmit++;		/* XXX per-vap? */
681a35dae8dSAdrian Chadd 
682a35dae8dSAdrian Chadd 		/*
683a35dae8dSAdrian Chadd 		 * Record local TSF for our last send for use
684a35dae8dSAdrian Chadd 		 * in arbitrating slot collisions.
685a35dae8dSAdrian Chadd 		 */
686a35dae8dSAdrian Chadd 		/* XXX should take a locked ref to iv_bss */
687a35dae8dSAdrian Chadd 		vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah);
688a35dae8dSAdrian Chadd 	}
689a35dae8dSAdrian Chadd }
690a35dae8dSAdrian Chadd #endif /* IEEE80211_SUPPORT_TDMA */
691