1235679Sadrian/*- 2235679Sadrian * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3235679Sadrian * All rights reserved. 4235679Sadrian * 5235679Sadrian * Redistribution and use in source and binary forms, with or without 6235679Sadrian * modification, are permitted provided that the following conditions 7235679Sadrian * are met: 8235679Sadrian * 1. Redistributions of source code must retain the above copyright 9235679Sadrian * notice, this list of conditions and the following disclaimer, 10235679Sadrian * without modification. 11235679Sadrian * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12235679Sadrian * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13235679Sadrian * redistribution must be conditioned upon including a substantially 14235679Sadrian * similar Disclaimer requirement for further binary redistribution. 15235679Sadrian * 16235679Sadrian * NO WARRANTY 17235679Sadrian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18235679Sadrian * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19235679Sadrian * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20235679Sadrian * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21235679Sadrian * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22235679Sadrian * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23235679Sadrian * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24235679Sadrian * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25235679Sadrian * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26235679Sadrian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27235679Sadrian * THE POSSIBILITY OF SUCH DAMAGES. 28235679Sadrian */ 29235679Sadrian 30235679Sadrian#include <sys/cdefs.h> 31235679Sadrian__FBSDID("$FreeBSD$"); 32235679Sadrian 33235679Sadrian/* 34235679Sadrian * Driver for the Atheros Wireless LAN controller. 35235679Sadrian * 36235679Sadrian * This software is derived from work of Atsushi Onoe; his contribution 37235679Sadrian * is greatly appreciated. 38235679Sadrian */ 39235679Sadrian 40235679Sadrian#include "opt_inet.h" 41235679Sadrian#include "opt_ath.h" 42235679Sadrian/* 43235679Sadrian * This is needed for register operations which are performed 44235679Sadrian * by the driver - eg, calls to ath_hal_gettsf32(). 45235679Sadrian * 46235679Sadrian * It's also required for any AH_DEBUG checks in here, eg the 47235679Sadrian * module dependencies. 48235679Sadrian */ 49235679Sadrian#include "opt_ah.h" 50235679Sadrian#include "opt_wlan.h" 51235679Sadrian 52235679Sadrian#include <sys/param.h> 53235679Sadrian#include <sys/systm.h> 54235679Sadrian#include <sys/sysctl.h> 55235679Sadrian#include <sys/mbuf.h> 56235679Sadrian#include <sys/malloc.h> 57235679Sadrian#include <sys/lock.h> 58235679Sadrian#include <sys/mutex.h> 59235679Sadrian#include <sys/kernel.h> 60235679Sadrian#include <sys/socket.h> 61235679Sadrian#include <sys/sockio.h> 62235679Sadrian#include <sys/errno.h> 63235679Sadrian#include <sys/callout.h> 64235679Sadrian#include <sys/bus.h> 65235679Sadrian#include <sys/endian.h> 66235679Sadrian#include <sys/kthread.h> 67235679Sadrian#include <sys/taskqueue.h> 68235679Sadrian#include <sys/priv.h> 69235679Sadrian#include <sys/module.h> 70235679Sadrian#include <sys/ktr.h> 71235679Sadrian#include <sys/smp.h> /* for mp_ncpus */ 72235679Sadrian 73235679Sadrian#include <machine/bus.h> 74235679Sadrian 75235679Sadrian#include <net/if.h> 76257176Sglebius#include <net/if_var.h> 77235679Sadrian#include <net/if_dl.h> 78235679Sadrian#include <net/if_media.h> 79235679Sadrian#include <net/if_types.h> 80235679Sadrian#include <net/if_arp.h> 81235679Sadrian#include <net/ethernet.h> 82235679Sadrian#include <net/if_llc.h> 83235679Sadrian 84235679Sadrian#include <net80211/ieee80211_var.h> 85235679Sadrian#include <net80211/ieee80211_regdomain.h> 86235679Sadrian#ifdef IEEE80211_SUPPORT_SUPERG 87235679Sadrian#include <net80211/ieee80211_superg.h> 88235679Sadrian#endif 89235679Sadrian#ifdef IEEE80211_SUPPORT_TDMA 90235679Sadrian#include <net80211/ieee80211_tdma.h> 91235679Sadrian#endif 92235679Sadrian 93235679Sadrian#include <net/bpf.h> 94235679Sadrian 95235679Sadrian#ifdef INET 96235679Sadrian#include <netinet/in.h> 97235679Sadrian#include <netinet/if_ether.h> 98235679Sadrian#endif 99235679Sadrian 100235679Sadrian#include <dev/ath/if_athvar.h> 101235679Sadrian#include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */ 102235679Sadrian#include <dev/ath/ath_hal/ah_diagcodes.h> 103235679Sadrian 104235679Sadrian#include <dev/ath/if_ath_debug.h> 105235679Sadrian#include <dev/ath/if_ath_misc.h> 106235679Sadrian#include <dev/ath/if_ath_tsf.h> 107235679Sadrian#include <dev/ath/if_ath_tx.h> 108235679Sadrian#include <dev/ath/if_ath_sysctl.h> 109235679Sadrian#include <dev/ath/if_ath_led.h> 110235679Sadrian#include <dev/ath/if_ath_keycache.h> 111235679Sadrian#include <dev/ath/if_ath_rx.h> 112235679Sadrian#include <dev/ath/if_ath_beacon.h> 113235679Sadrian#include <dev/ath/if_athdfs.h> 114235679Sadrian 115235679Sadrian#ifdef ATH_TX99_DIAG 116235679Sadrian#include <dev/ath/ath_tx99/ath_tx99.h> 117235679Sadrian#endif 118235679Sadrian 119243592Sadrian#ifdef ATH_DEBUG_ALQ 120243592Sadrian#include <dev/ath/if_ath_alq.h> 121243592Sadrian#endif 122243592Sadrian 123235679Sadrian#ifdef IEEE80211_SUPPORT_TDMA 124235679Sadrian#include <dev/ath/if_ath_tdma.h> 125235679Sadrian 126235679Sadrianstatic void ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, 127235679Sadrian u_int32_t bintval); 128235679Sadrianstatic void ath_tdma_bintvalsetup(struct ath_softc *sc, 129235679Sadrian const struct ieee80211_tdma_state *tdma); 130235679Sadrian#endif /* IEEE80211_SUPPORT_TDMA */ 131235679Sadrian 132235679Sadrian#ifdef IEEE80211_SUPPORT_TDMA 133235679Sadrianstatic void 134235679Sadrianath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval) 135235679Sadrian{ 136235679Sadrian struct ath_hal *ah = sc->sc_ah; 137235679Sadrian HAL_BEACON_TIMERS bt; 138235679Sadrian 139235679Sadrian bt.bt_intval = bintval | HAL_BEACON_ENA; 140235679Sadrian bt.bt_nexttbtt = nexttbtt; 141235679Sadrian bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep; 142235679Sadrian bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep; 143235679Sadrian bt.bt_nextatim = nexttbtt+1; 144235679Sadrian /* Enables TBTT, DBA, SWBA timers by default */ 145235679Sadrian bt.bt_flags = 0; 146243592Sadrian#if 0 147243592Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 148243592Sadrian "%s: intval=%d (0x%08x) nexttbtt=%u (0x%08x), nextdba=%u (0x%08x), nextswba=%u (0x%08x),nextatim=%u (0x%08x)\n", 149243592Sadrian __func__, 150243592Sadrian bt.bt_intval, 151243592Sadrian bt.bt_intval, 152243592Sadrian bt.bt_nexttbtt, 153243592Sadrian bt.bt_nexttbtt, 154243592Sadrian bt.bt_nextdba, 155243592Sadrian bt.bt_nextdba, 156243592Sadrian bt.bt_nextswba, 157243592Sadrian bt.bt_nextswba, 158243592Sadrian bt.bt_nextatim, 159243592Sadrian bt.bt_nextatim); 160243592Sadrian#endif 161243592Sadrian 162243597Sadrian#ifdef ATH_DEBUG_ALQ 163243592Sadrian if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_SET)) { 164243592Sadrian struct if_ath_alq_tdma_timer_set t; 165243592Sadrian t.bt_intval = htobe32(bt.bt_intval); 166243592Sadrian t.bt_nexttbtt = htobe32(bt.bt_nexttbtt); 167243592Sadrian t.bt_nextdba = htobe32(bt.bt_nextdba); 168243592Sadrian t.bt_nextswba = htobe32(bt.bt_nextswba); 169243592Sadrian t.bt_nextatim = htobe32(bt.bt_nextatim); 170243592Sadrian t.bt_flags = htobe32(bt.bt_flags); 171243592Sadrian t.sc_tdmadbaprep = htobe32(sc->sc_tdmadbaprep); 172243592Sadrian t.sc_tdmaswbaprep = htobe32(sc->sc_tdmaswbaprep); 173243592Sadrian if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_SET, 174243592Sadrian sizeof(t), (char *) &t); 175243592Sadrian } 176243597Sadrian#endif 177243592Sadrian 178243592Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 179243592Sadrian "%s: nexttbtt=%u (0x%08x), nexttbtt tsf=%lld (0x%08llx)\n", 180243592Sadrian __func__, 181243592Sadrian bt.bt_nexttbtt, 182243592Sadrian bt.bt_nexttbtt, 183243592Sadrian (long long) ( ((u_int64_t) (bt.bt_nexttbtt)) << 10), 184243592Sadrian (long long) ( ((u_int64_t) (bt.bt_nexttbtt)) << 10)); 185235679Sadrian ath_hal_beaconsettimers(ah, &bt); 186235679Sadrian} 187235679Sadrian 188235679Sadrian/* 189235679Sadrian * Calculate the beacon interval. This is periodic in the 190235679Sadrian * superframe for the bss. We assume each station is configured 191235679Sadrian * identically wrt transmit rate so the guard time we calculate 192235679Sadrian * above will be the same on all stations. Note we need to 193235679Sadrian * factor in the xmit time because the hardware will schedule 194235679Sadrian * a frame for transmit if the start of the frame is within 195235679Sadrian * the burst time. When we get hardware that properly kills 196235679Sadrian * frames in the PCU we can reduce/eliminate the guard time. 197235679Sadrian * 198235679Sadrian * Roundup to 1024 is so we have 1 TU buffer in the guard time 199235679Sadrian * to deal with the granularity of the nexttbtt timer. 11n MAC's 200235679Sadrian * with 1us timer granularity should allow us to reduce/eliminate 201235679Sadrian * this. 202235679Sadrian */ 203235679Sadrianstatic void 204235679Sadrianath_tdma_bintvalsetup(struct ath_softc *sc, 205235679Sadrian const struct ieee80211_tdma_state *tdma) 206235679Sadrian{ 207235679Sadrian /* copy from vap state (XXX check all vaps have same value?) */ 208235679Sadrian sc->sc_tdmaslotlen = tdma->tdma_slotlen; 209235679Sadrian 210235679Sadrian sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) * 211235679Sadrian tdma->tdma_slotcnt, 1024); 212235679Sadrian sc->sc_tdmabintval >>= 10; /* TSF -> TU */ 213235679Sadrian if (sc->sc_tdmabintval & 1) 214235679Sadrian sc->sc_tdmabintval++; 215235679Sadrian 216235679Sadrian if (tdma->tdma_slot == 0) { 217235679Sadrian /* 218235679Sadrian * Only slot 0 beacons; other slots respond. 219235679Sadrian */ 220235679Sadrian sc->sc_imask |= HAL_INT_SWBA; 221235679Sadrian sc->sc_tdmaswba = 0; /* beacon immediately */ 222235679Sadrian } else { 223235679Sadrian /* XXX all vaps must be slot 0 or slot !0 */ 224235679Sadrian sc->sc_imask &= ~HAL_INT_SWBA; 225235679Sadrian } 226235679Sadrian} 227235679Sadrian 228235679Sadrian/* 229235679Sadrian * Max 802.11 overhead. This assumes no 4-address frames and 230235679Sadrian * the encapsulation done by ieee80211_encap (llc). We also 231235679Sadrian * include potential crypto overhead. 232235679Sadrian */ 233235679Sadrian#define IEEE80211_MAXOVERHEAD \ 234235679Sadrian (sizeof(struct ieee80211_qosframe) \ 235235679Sadrian + sizeof(struct llc) \ 236235679Sadrian + IEEE80211_ADDR_LEN \ 237235679Sadrian + IEEE80211_WEP_IVLEN \ 238235679Sadrian + IEEE80211_WEP_KIDLEN \ 239235679Sadrian + IEEE80211_WEP_CRCLEN \ 240235679Sadrian + IEEE80211_WEP_MICLEN \ 241235679Sadrian + IEEE80211_CRC_LEN) 242235679Sadrian 243235679Sadrian/* 244235679Sadrian * Setup initially for tdma operation. Start the beacon 245235679Sadrian * timers and enable SWBA if we are slot 0. Otherwise 246235679Sadrian * we wait for slot 0 to arrive so we can sync up before 247235679Sadrian * starting to transmit. 248235679Sadrian */ 249235679Sadrianvoid 250235679Sadrianath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap) 251235679Sadrian{ 252235679Sadrian struct ath_hal *ah = sc->sc_ah; 253287197Sglebius struct ieee80211com *ic = &sc->sc_ic; 254235679Sadrian const struct ieee80211_txparam *tp; 255235679Sadrian const struct ieee80211_tdma_state *tdma = NULL; 256235679Sadrian int rix; 257235679Sadrian 258235679Sadrian if (vap == NULL) { 259235679Sadrian vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */ 260235679Sadrian if (vap == NULL) { 261283744Sglebius device_printf(sc->sc_dev, "%s: no vaps?\n", __func__); 262235679Sadrian return; 263235679Sadrian } 264235679Sadrian } 265235679Sadrian /* XXX should take a locked ref to iv_bss */ 266235679Sadrian tp = vap->iv_bss->ni_txparms; 267235679Sadrian /* 268235679Sadrian * Calculate the guard time for each slot. This is the 269235679Sadrian * time to send a maximal-size frame according to the 270235679Sadrian * fixed/lowest transmit rate. Note that the interface 271235679Sadrian * mtu does not include the 802.11 overhead so we must 272235679Sadrian * tack that on (ath_hal_computetxtime includes the 273235679Sadrian * preamble and plcp in it's calculation). 274235679Sadrian */ 275235679Sadrian tdma = vap->iv_tdma; 276235679Sadrian if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 277235679Sadrian rix = ath_tx_findrix(sc, tp->ucastrate); 278235679Sadrian else 279235679Sadrian rix = ath_tx_findrix(sc, tp->mcastrate); 280235679Sadrian 281250865Sadrian /* 282250865Sadrian * If the chip supports enforcing TxOP on transmission, 283250865Sadrian * we can just delete the guard window. It isn't at all required. 284250865Sadrian */ 285250865Sadrian if (sc->sc_hasenforcetxop) { 286250865Sadrian sc->sc_tdmaguard = 0; 287250865Sadrian } else { 288250865Sadrian /* XXX short preamble assumed */ 289250865Sadrian /* XXX non-11n rate assumed */ 290250865Sadrian sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates, 291287197Sglebius vap->iv_ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE); 292250865Sadrian } 293250865Sadrian 294235679Sadrian ath_hal_intrset(ah, 0); 295235679Sadrian 296235679Sadrian ath_beaconq_config(sc); /* setup h/w beacon q */ 297235679Sadrian if (sc->sc_setcca) 298235679Sadrian ath_hal_setcca(ah, AH_FALSE); /* disable CCA */ 299235679Sadrian ath_tdma_bintvalsetup(sc, tdma); /* calculate beacon interval */ 300235679Sadrian ath_tdma_settimers(sc, sc->sc_tdmabintval, 301235679Sadrian sc->sc_tdmabintval | HAL_BEACON_RESET_TSF); 302235679Sadrian sc->sc_syncbeacon = 0; 303235679Sadrian 304235679Sadrian sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER; 305235679Sadrian sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER; 306235679Sadrian 307235679Sadrian ath_hal_intrset(ah, sc->sc_imask); 308235679Sadrian 309235679Sadrian DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u " 310235679Sadrian "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__, 311235679Sadrian tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt, 312235679Sadrian tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval, 313235679Sadrian sc->sc_tdmadbaprep); 314243592Sadrian 315243592Sadrian#ifdef ATH_DEBUG_ALQ 316243592Sadrian if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_CONFIG)) { 317243592Sadrian struct if_ath_alq_tdma_timer_config t; 318243592Sadrian 319243592Sadrian t.tdma_slot = htobe32(tdma->tdma_slot); 320243592Sadrian t.tdma_slotlen = htobe32(tdma->tdma_slotlen); 321243592Sadrian t.tdma_slotcnt = htobe32(tdma->tdma_slotcnt); 322243592Sadrian t.tdma_bintval = htobe32(tdma->tdma_bintval); 323243592Sadrian t.tdma_guard = htobe32(sc->sc_tdmaguard); 324243592Sadrian t.tdma_scbintval = htobe32(sc->sc_tdmabintval); 325243592Sadrian t.tdma_dbaprep = htobe32(sc->sc_tdmadbaprep); 326243592Sadrian 327243592Sadrian if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_TIMER_CONFIG, 328243592Sadrian sizeof(t), (char *) &t); 329243592Sadrian } 330243592Sadrian#endif /* ATH_DEBUG_ALQ */ 331235679Sadrian} 332235679Sadrian 333235679Sadrian/* 334235679Sadrian * Update tdma operation. Called from the 802.11 layer 335235679Sadrian * when a beacon is received from the TDMA station operating 336235679Sadrian * in the slot immediately preceding us in the bss. Use 337235679Sadrian * the rx timestamp for the beacon frame to update our 338235679Sadrian * beacon timers so we follow their schedule. Note that 339235679Sadrian * by using the rx timestamp we implicitly include the 340235679Sadrian * propagation delay in our schedule. 341243614Sadrian * 342243614Sadrian * XXX TODO: since the changes for the AR5416 and later chips 343243614Sadrian * involved changing the TSF/TU calculations, we need to make 344243614Sadrian * sure that various calculations wrap consistently. 345243614Sadrian * 346243614Sadrian * A lot of the problems stemmed from the calculations wrapping 347243614Sadrian * at 65,535 TU. Since a lot of the math is still being done in 348243614Sadrian * TU, please audit it to ensure that when the TU values programmed 349243614Sadrian * into the timers wrap at (2^31)-1 TSF, all the various terms 350243614Sadrian * wrap consistently. 351235679Sadrian */ 352235679Sadrianvoid 353235679Sadrianath_tdma_update(struct ieee80211_node *ni, 354235679Sadrian const struct ieee80211_tdma_param *tdma, int changed) 355235679Sadrian{ 356235679Sadrian#define TSF_TO_TU(_h,_l) \ 357235679Sadrian ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10)) 358235679Sadrian#define TU_TO_TSF(_tu) (((u_int64_t)(_tu)) << 10) 359235679Sadrian struct ieee80211vap *vap = ni->ni_vap; 360235679Sadrian struct ieee80211com *ic = ni->ni_ic; 361286835Sadrian struct ath_softc *sc = ic->ic_softc; 362235679Sadrian struct ath_hal *ah = sc->sc_ah; 363235679Sadrian const HAL_RATE_TABLE *rt = sc->sc_currates; 364243590Sadrian u_int64_t tsf, rstamp, nextslot, nexttbtt, nexttbtt_full; 365235679Sadrian u_int32_t txtime, nextslottu; 366235679Sadrian int32_t tudelta, tsfdelta; 367235679Sadrian const struct ath_rx_status *rs; 368235679Sadrian int rix; 369235679Sadrian 370235679Sadrian sc->sc_stats.ast_tdma_update++; 371235679Sadrian 372235679Sadrian /* 373235679Sadrian * Check for and adopt configuration changes. 374235679Sadrian */ 375235679Sadrian if (changed != 0) { 376235679Sadrian const struct ieee80211_tdma_state *ts = vap->iv_tdma; 377235679Sadrian 378235679Sadrian ath_tdma_bintvalsetup(sc, ts); 379235679Sadrian if (changed & TDMA_UPDATE_SLOTLEN) 380235679Sadrian ath_wme_update(ic); 381235679Sadrian 382235679Sadrian DPRINTF(sc, ATH_DEBUG_TDMA, 383235679Sadrian "%s: adopt slot %u slotcnt %u slotlen %u us " 384235679Sadrian "bintval %u TU\n", __func__, 385235679Sadrian ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen, 386235679Sadrian sc->sc_tdmabintval); 387235679Sadrian 388235679Sadrian /* XXX right? */ 389235679Sadrian ath_hal_intrset(ah, sc->sc_imask); 390235679Sadrian /* NB: beacon timers programmed below */ 391235679Sadrian } 392235679Sadrian 393235679Sadrian /* extend rx timestamp to 64 bits */ 394235679Sadrian rs = sc->sc_lastrs; 395235679Sadrian tsf = ath_hal_gettsf64(ah); 396235679Sadrian rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf); 397235679Sadrian /* 398235679Sadrian * The rx timestamp is set by the hardware on completing 399235679Sadrian * reception (at the point where the rx descriptor is DMA'd 400235679Sadrian * to the host). To find the start of our next slot we 401235679Sadrian * must adjust this time by the time required to send 402235679Sadrian * the packet just received. 403235679Sadrian */ 404235679Sadrian rix = rt->rateCodeToIndex[rs->rs_rate]; 405250865Sadrian 406250865Sadrian /* 407250865Sadrian * To calculate the packet duration for legacy rates, we 408250865Sadrian * only need the rix and preamble. 409250865Sadrian * 410250865Sadrian * For 11n non-aggregate frames, we also need the channel 411250865Sadrian * width and short/long guard interval. 412250865Sadrian * 413250865Sadrian * For 11n aggregate frames, the required hacks are a little 414250865Sadrian * more subtle. You need to figure out the frame duration 415250865Sadrian * for each frame, including the delimiters. However, when 416250865Sadrian * a frame isn't received successfully, we won't hear it 417250865Sadrian * (unless you enable reception of CRC errored frames), so 418250865Sadrian * your duration calculation is going to be off. 419250865Sadrian * 420250865Sadrian * However, we can assume that the beacon frames won't be 421250865Sadrian * transmitted as aggregate frames, so we should be okay. 422250865Sadrian * Just add a check to ensure that we aren't handed something 423250865Sadrian * bad. 424250865Sadrian * 425250865Sadrian * For ath_hal_pkt_txtime() - for 11n rates, shortPreamble is 426250865Sadrian * actually short guard interval. For legacy rates, 427250865Sadrian * it's short preamble. 428250865Sadrian */ 429250865Sadrian txtime = ath_hal_pkt_txtime(ah, rt, rs->rs_datalen, 430250865Sadrian rix, 431250865Sadrian !! (rs->rs_flags & HAL_RX_2040), 432250865Sadrian (rix & 0x80) ? 433250865Sadrian (! (rs->rs_flags & HAL_RX_GI)) : rt->info[rix].shortPreamble); 434235679Sadrian /* NB: << 9 is to cvt to TU and /2 */ 435235679Sadrian nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9); 436243592Sadrian 437243590Sadrian /* 438243590Sadrian * For 802.11n chips: nextslottu needs to be the full TSF space, 439243590Sadrian * not just 0..65535 TU. 440243590Sadrian */ 441243590Sadrian nextslottu = TSF_TO_TU(nextslot>>32, nextslot); 442235679Sadrian /* 443235679Sadrian * Retrieve the hardware NextTBTT in usecs 444235679Sadrian * and calculate the difference between what the 445235679Sadrian * other station thinks and what we have programmed. This 446235679Sadrian * lets us figure how to adjust our timers to match. The 447235679Sadrian * adjustments are done by pulling the TSF forward and possibly 448235679Sadrian * rewriting the beacon timers. 449235679Sadrian */ 450243426Sadrian /* 451243426Sadrian * The logic here assumes the nexttbtt counter is in TSF 452243426Sadrian * but the prr-11n NICs are in TU. The HAL shifts them 453243426Sadrian * to TSF but there's two important differences: 454243426Sadrian * 455243426Sadrian * + The TU->TSF values have 0's for the low 9 bits, and 456243426Sadrian * + The counter wraps at TU_TO_TSF(HAL_BEACON_PERIOD + 1) for 457243426Sadrian * the pre-11n NICs, but not for the 11n NICs. 458243426Sadrian * 459243426Sadrian * So for now, just make sure the nexttbtt value we get 460243426Sadrian * matches the second issue or once nexttbtt exceeds this 461243426Sadrian * value, tsfdelta ends up becoming very negative and all 462243426Sadrian * of the adjustments get very messed up. 463243426Sadrian */ 464243592Sadrian 465243590Sadrian /* 466243590Sadrian * We need to track the full nexttbtt rather than having it 467243590Sadrian * truncated at HAL_BEACON_PERIOD, as programming the 468243590Sadrian * nexttbtt (and related) registers for the 11n chips is 469243590Sadrian * actually going to take the full 32 bit space, rather than 470243590Sadrian * just 0..65535 TU. 471243590Sadrian */ 472243590Sadrian nexttbtt_full = ath_hal_getnexttbtt(ah); 473243590Sadrian nexttbtt = nexttbtt_full % (TU_TO_TSF(HAL_BEACON_PERIOD + 1)); 474235679Sadrian tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt); 475235679Sadrian 476235679Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 477243642Sadrian "rs->rstamp %llu rstamp %llu tsf %llu txtime %d, nextslot %llu, " 478243642Sadrian "nextslottu %d, nextslottume %d\n", 479265588Sadrian (unsigned long long) rs->rs_tstamp, 480265588Sadrian (unsigned long long) rstamp, 481265588Sadrian (unsigned long long) tsf, txtime, 482265588Sadrian (unsigned long long) nextslot, 483265588Sadrian nextslottu, TSF_TO_TU(nextslot >> 32, nextslot)); 484243642Sadrian DPRINTF(sc, ATH_DEBUG_TDMA, 485243642Sadrian " beacon tstamp: %llu (0x%016llx)\n", 486265588Sadrian (unsigned long long) le64toh(ni->ni_tstamp.tsf), 487265588Sadrian (unsigned long long) le64toh(ni->ni_tstamp.tsf)); 488243642Sadrian 489243642Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 490243592Sadrian "nexttbtt %llu (0x%08llx) tsfdelta %d avg +%d/-%d\n", 491265588Sadrian (unsigned long long) nexttbtt, 492243592Sadrian (long long) nexttbtt, 493243592Sadrian tsfdelta, 494235679Sadrian TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam)); 495235679Sadrian 496235679Sadrian if (tsfdelta < 0) { 497235679Sadrian TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 498235679Sadrian TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta); 499235679Sadrian tsfdelta = -tsfdelta % 1024; 500235679Sadrian nextslottu++; 501235679Sadrian } else if (tsfdelta > 0) { 502235679Sadrian TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta); 503235679Sadrian TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 504235679Sadrian tsfdelta = 1024 - (tsfdelta % 1024); 505235679Sadrian nextslottu++; 506235679Sadrian } else { 507235679Sadrian TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 508235679Sadrian TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 509235679Sadrian } 510243590Sadrian tudelta = nextslottu - TSF_TO_TU(nexttbtt_full >> 32, nexttbtt_full); 511235679Sadrian 512243642Sadrian#ifdef ATH_DEBUG_ALQ 513243642Sadrian if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_BEACON_STATE)) { 514243642Sadrian struct if_ath_alq_tdma_beacon_state t; 515243642Sadrian t.rx_tsf = htobe64(rstamp); 516243642Sadrian t.beacon_tsf = htobe64(le64toh(ni->ni_tstamp.tsf)); 517243642Sadrian t.tsf64 = htobe64(tsf); 518243642Sadrian t.nextslot_tsf = htobe64(nextslot); 519243642Sadrian t.nextslot_tu = htobe32(nextslottu); 520243642Sadrian t.txtime = htobe32(txtime); 521243642Sadrian if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_BEACON_STATE, 522243642Sadrian sizeof(t), (char *) &t); 523243642Sadrian } 524243642Sadrian 525243642Sadrian if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_TDMA_SLOT_CALC)) { 526243642Sadrian struct if_ath_alq_tdma_slot_calc t; 527243642Sadrian 528243642Sadrian t.nexttbtt = htobe64(nexttbtt_full); 529243642Sadrian t.next_slot = htobe64(nextslot); 530243642Sadrian t.tsfdelta = htobe32(tsfdelta); 531243642Sadrian t.avg_plus = htobe32(TDMA_AVG(sc->sc_avgtsfdeltap)); 532243642Sadrian t.avg_minus = htobe32(TDMA_AVG(sc->sc_avgtsfdeltam)); 533243642Sadrian 534243642Sadrian if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_SLOT_CALC, 535243642Sadrian sizeof(t), (char *) &t); 536243642Sadrian } 537243642Sadrian#endif 538243642Sadrian 539235679Sadrian /* 540235679Sadrian * Copy sender's timetstamp into tdma ie so they can 541235679Sadrian * calculate roundtrip time. We submit a beacon frame 542235679Sadrian * below after any timer adjustment. The frame goes out 543235679Sadrian * at the next TBTT so the sender can calculate the 544235679Sadrian * roundtrip by inspecting the tdma ie in our beacon frame. 545235679Sadrian * 546235679Sadrian * NB: This tstamp is subtlely preserved when 547235679Sadrian * IEEE80211_BEACON_TDMA is marked (e.g. when the 548235679Sadrian * slot position changes) because ieee80211_add_tdma 549235679Sadrian * skips over the data. 550235679Sadrian */ 551288095Sadrian memcpy(vap->iv_bcn_off.bo_tdma + 552235679Sadrian __offsetof(struct ieee80211_tdma_param, tdma_tstamp), 553235679Sadrian &ni->ni_tstamp.data, 8); 554235679Sadrian#if 0 555235679Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 556235679Sadrian "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n", 557235679Sadrian (unsigned long long) tsf, (unsigned long long) nextslot, 558235679Sadrian (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta); 559235679Sadrian#endif 560235679Sadrian /* 561235679Sadrian * Adjust the beacon timers only when pulling them forward 562235679Sadrian * or when going back by less than the beacon interval. 563235679Sadrian * Negative jumps larger than the beacon interval seem to 564235679Sadrian * cause the timers to stop and generally cause instability. 565235679Sadrian * This basically filters out jumps due to missed beacons. 566235679Sadrian */ 567235679Sadrian if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) { 568243592Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 569243592Sadrian "%s: calling ath_tdma_settimers; nextslottu=%d, bintval=%d\n", 570243592Sadrian __func__, 571243592Sadrian nextslottu, 572243592Sadrian sc->sc_tdmabintval); 573235679Sadrian ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval); 574235679Sadrian sc->sc_stats.ast_tdma_timers++; 575235679Sadrian } 576235679Sadrian if (tsfdelta > 0) { 577243427Sadrian uint64_t tsf; 578243427Sadrian 579243427Sadrian /* XXX should just teach ath_hal_adjusttsf() to do this */ 580243427Sadrian tsf = ath_hal_gettsf64(ah); 581243427Sadrian ath_hal_settsf64(ah, tsf + tsfdelta); 582243592Sadrian DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 583243592Sadrian "%s: calling ath_hal_adjusttsf: TSF=%llu, tsfdelta=%d\n", 584243592Sadrian __func__, 585265588Sadrian (unsigned long long) tsf, 586243592Sadrian tsfdelta); 587243592Sadrian 588243592Sadrian#ifdef ATH_DEBUG_ALQ 589243592Sadrian if (if_ath_alq_checkdebug(&sc->sc_alq, 590243592Sadrian ATH_ALQ_TDMA_TSF_ADJUST)) { 591243592Sadrian struct if_ath_alq_tdma_tsf_adjust t; 592243592Sadrian 593243592Sadrian t.tsfdelta = htobe32(tsfdelta); 594243614Sadrian t.tsf64_old = htobe64(tsf); 595243614Sadrian t.tsf64_new = htobe64(tsf + tsfdelta); 596243592Sadrian if_ath_alq_post(&sc->sc_alq, ATH_ALQ_TDMA_TSF_ADJUST, 597243592Sadrian sizeof(t), (char *) &t); 598243592Sadrian } 599243592Sadrian#endif /* ATH_DEBUG_ALQ */ 600235679Sadrian sc->sc_stats.ast_tdma_tsf++; 601235679Sadrian } 602235679Sadrian ath_tdma_beacon_send(sc, vap); /* prepare response */ 603235679Sadrian#undef TU_TO_TSF 604235679Sadrian#undef TSF_TO_TU 605235679Sadrian} 606235679Sadrian 607235679Sadrian/* 608235679Sadrian * Transmit a beacon frame at SWBA. Dynamic updates 609235679Sadrian * to the frame contents are done as needed. 610235679Sadrian */ 611235679Sadrianvoid 612235679Sadrianath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap) 613235679Sadrian{ 614235679Sadrian struct ath_hal *ah = sc->sc_ah; 615235679Sadrian struct ath_buf *bf; 616235679Sadrian int otherant; 617235679Sadrian 618235679Sadrian /* 619235679Sadrian * Check if the previous beacon has gone out. If 620235679Sadrian * not don't try to post another, skip this period 621235679Sadrian * and wait for the next. Missed beacons indicate 622235679Sadrian * a problem and should not occur. If we miss too 623235679Sadrian * many consecutive beacons reset the device. 624235679Sadrian */ 625235679Sadrian if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 626235679Sadrian sc->sc_bmisscount++; 627235679Sadrian DPRINTF(sc, ATH_DEBUG_BEACON, 628235679Sadrian "%s: missed %u consecutive beacons\n", 629235679Sadrian __func__, sc->sc_bmisscount); 630235679Sadrian if (sc->sc_bmisscount >= ath_bstuck_threshold) 631235679Sadrian taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 632235679Sadrian return; 633235679Sadrian } 634235679Sadrian if (sc->sc_bmisscount != 0) { 635235679Sadrian DPRINTF(sc, ATH_DEBUG_BEACON, 636235679Sadrian "%s: resume beacon xmit after %u misses\n", 637235679Sadrian __func__, sc->sc_bmisscount); 638235679Sadrian sc->sc_bmisscount = 0; 639235679Sadrian } 640235679Sadrian 641235679Sadrian /* 642235679Sadrian * Check recent per-antenna transmit statistics and flip 643235679Sadrian * the default antenna if noticeably more frames went out 644235679Sadrian * on the non-default antenna. 645235679Sadrian * XXX assumes 2 anntenae 646235679Sadrian */ 647235679Sadrian if (!sc->sc_diversity) { 648235679Sadrian otherant = sc->sc_defant & 1 ? 2 : 1; 649235679Sadrian if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 650235679Sadrian ath_setdefantenna(sc, otherant); 651235679Sadrian sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 652235679Sadrian } 653235679Sadrian 654235679Sadrian bf = ath_beacon_generate(sc, vap); 655248671Sadrian /* XXX We don't do cabq traffic, but just for completeness .. */ 656248671Sadrian ATH_TXQ_LOCK(sc->sc_cabq); 657248671Sadrian ath_beacon_cabq_start(sc); 658248671Sadrian ATH_TXQ_UNLOCK(sc->sc_cabq); 659248671Sadrian 660235679Sadrian if (bf != NULL) { 661235679Sadrian /* 662235679Sadrian * Stop any current dma and put the new frame on the queue. 663235679Sadrian * This should never fail since we check above that no frames 664235679Sadrian * are still pending on the queue. 665235679Sadrian */ 666248671Sadrian if ((! sc->sc_isedma) && 667248671Sadrian (! ath_hal_stoptxdma(ah, sc->sc_bhalq))) { 668235679Sadrian DPRINTF(sc, ATH_DEBUG_ANY, 669235679Sadrian "%s: beacon queue %u did not stop?\n", 670235679Sadrian __func__, sc->sc_bhalq); 671235679Sadrian /* NB: the HAL still stops DMA, so proceed */ 672235679Sadrian } 673235679Sadrian ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 674235679Sadrian ath_hal_txstart(ah, sc->sc_bhalq); 675235679Sadrian 676235679Sadrian sc->sc_stats.ast_be_xmit++; /* XXX per-vap? */ 677235679Sadrian 678235679Sadrian /* 679235679Sadrian * Record local TSF for our last send for use 680235679Sadrian * in arbitrating slot collisions. 681235679Sadrian */ 682235679Sadrian /* XXX should take a locked ref to iv_bss */ 683235679Sadrian vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah); 684235679Sadrian } 685235679Sadrian} 686235679Sadrian#endif /* IEEE80211_SUPPORT_TDMA */ 687