if_ath_tdma.c revision 243427
1/*- 2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/dev/ath/if_ath_tdma.c 243427 2012-11-23 05:52:22Z adrian $"); 32 33/* 34 * Driver for the Atheros Wireless LAN controller. 35 * 36 * This software is derived from work of Atsushi Onoe; his contribution 37 * is greatly appreciated. 38 */ 39 40#include "opt_inet.h" 41#include "opt_ath.h" 42/* 43 * This is needed for register operations which are performed 44 * by the driver - eg, calls to ath_hal_gettsf32(). 45 * 46 * It's also required for any AH_DEBUG checks in here, eg the 47 * module dependencies. 48 */ 49#include "opt_ah.h" 50#include "opt_wlan.h" 51 52#include <sys/param.h> 53#include <sys/systm.h> 54#include <sys/sysctl.h> 55#include <sys/mbuf.h> 56#include <sys/malloc.h> 57#include <sys/lock.h> 58#include <sys/mutex.h> 59#include <sys/kernel.h> 60#include <sys/socket.h> 61#include <sys/sockio.h> 62#include <sys/errno.h> 63#include <sys/callout.h> 64#include <sys/bus.h> 65#include <sys/endian.h> 66#include <sys/kthread.h> 67#include <sys/taskqueue.h> 68#include <sys/priv.h> 69#include <sys/module.h> 70#include <sys/ktr.h> 71#include <sys/smp.h> /* for mp_ncpus */ 72 73#include <machine/bus.h> 74 75#include <net/if.h> 76#include <net/if_dl.h> 77#include <net/if_media.h> 78#include <net/if_types.h> 79#include <net/if_arp.h> 80#include <net/ethernet.h> 81#include <net/if_llc.h> 82 83#include <net80211/ieee80211_var.h> 84#include <net80211/ieee80211_regdomain.h> 85#ifdef IEEE80211_SUPPORT_SUPERG 86#include <net80211/ieee80211_superg.h> 87#endif 88#ifdef IEEE80211_SUPPORT_TDMA 89#include <net80211/ieee80211_tdma.h> 90#endif 91 92#include <net/bpf.h> 93 94#ifdef INET 95#include <netinet/in.h> 96#include <netinet/if_ether.h> 97#endif 98 99#include <dev/ath/if_athvar.h> 100#include <dev/ath/ath_hal/ah_devid.h> /* XXX for softled */ 101#include <dev/ath/ath_hal/ah_diagcodes.h> 102 103#include <dev/ath/if_ath_debug.h> 104#include <dev/ath/if_ath_misc.h> 105#include <dev/ath/if_ath_tsf.h> 106#include <dev/ath/if_ath_tx.h> 107#include <dev/ath/if_ath_sysctl.h> 108#include <dev/ath/if_ath_led.h> 109#include <dev/ath/if_ath_keycache.h> 110#include <dev/ath/if_ath_rx.h> 111#include <dev/ath/if_ath_beacon.h> 112#include <dev/ath/if_athdfs.h> 113 114#ifdef ATH_TX99_DIAG 115#include <dev/ath/ath_tx99/ath_tx99.h> 116#endif 117 118#ifdef IEEE80211_SUPPORT_TDMA 119#include <dev/ath/if_ath_tdma.h> 120 121static void ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, 122 u_int32_t bintval); 123static void ath_tdma_bintvalsetup(struct ath_softc *sc, 124 const struct ieee80211_tdma_state *tdma); 125#endif /* IEEE80211_SUPPORT_TDMA */ 126 127#ifdef IEEE80211_SUPPORT_TDMA 128static void 129ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, u_int32_t bintval) 130{ 131 struct ath_hal *ah = sc->sc_ah; 132 HAL_BEACON_TIMERS bt; 133 134 bt.bt_intval = bintval | HAL_BEACON_ENA; 135 bt.bt_nexttbtt = nexttbtt; 136 bt.bt_nextdba = (nexttbtt<<3) - sc->sc_tdmadbaprep; 137 bt.bt_nextswba = (nexttbtt<<3) - sc->sc_tdmaswbaprep; 138 bt.bt_nextatim = nexttbtt+1; 139 /* Enables TBTT, DBA, SWBA timers by default */ 140 bt.bt_flags = 0; 141 ath_hal_beaconsettimers(ah, &bt); 142} 143 144/* 145 * Calculate the beacon interval. This is periodic in the 146 * superframe for the bss. We assume each station is configured 147 * identically wrt transmit rate so the guard time we calculate 148 * above will be the same on all stations. Note we need to 149 * factor in the xmit time because the hardware will schedule 150 * a frame for transmit if the start of the frame is within 151 * the burst time. When we get hardware that properly kills 152 * frames in the PCU we can reduce/eliminate the guard time. 153 * 154 * Roundup to 1024 is so we have 1 TU buffer in the guard time 155 * to deal with the granularity of the nexttbtt timer. 11n MAC's 156 * with 1us timer granularity should allow us to reduce/eliminate 157 * this. 158 */ 159static void 160ath_tdma_bintvalsetup(struct ath_softc *sc, 161 const struct ieee80211_tdma_state *tdma) 162{ 163 /* copy from vap state (XXX check all vaps have same value?) */ 164 sc->sc_tdmaslotlen = tdma->tdma_slotlen; 165 166 sc->sc_tdmabintval = roundup((sc->sc_tdmaslotlen+sc->sc_tdmaguard) * 167 tdma->tdma_slotcnt, 1024); 168 sc->sc_tdmabintval >>= 10; /* TSF -> TU */ 169 if (sc->sc_tdmabintval & 1) 170 sc->sc_tdmabintval++; 171 172 if (tdma->tdma_slot == 0) { 173 /* 174 * Only slot 0 beacons; other slots respond. 175 */ 176 sc->sc_imask |= HAL_INT_SWBA; 177 sc->sc_tdmaswba = 0; /* beacon immediately */ 178 } else { 179 /* XXX all vaps must be slot 0 or slot !0 */ 180 sc->sc_imask &= ~HAL_INT_SWBA; 181 } 182} 183 184/* 185 * Max 802.11 overhead. This assumes no 4-address frames and 186 * the encapsulation done by ieee80211_encap (llc). We also 187 * include potential crypto overhead. 188 */ 189#define IEEE80211_MAXOVERHEAD \ 190 (sizeof(struct ieee80211_qosframe) \ 191 + sizeof(struct llc) \ 192 + IEEE80211_ADDR_LEN \ 193 + IEEE80211_WEP_IVLEN \ 194 + IEEE80211_WEP_KIDLEN \ 195 + IEEE80211_WEP_CRCLEN \ 196 + IEEE80211_WEP_MICLEN \ 197 + IEEE80211_CRC_LEN) 198 199/* 200 * Setup initially for tdma operation. Start the beacon 201 * timers and enable SWBA if we are slot 0. Otherwise 202 * we wait for slot 0 to arrive so we can sync up before 203 * starting to transmit. 204 */ 205void 206ath_tdma_config(struct ath_softc *sc, struct ieee80211vap *vap) 207{ 208 struct ath_hal *ah = sc->sc_ah; 209 struct ifnet *ifp = sc->sc_ifp; 210 struct ieee80211com *ic = ifp->if_l2com; 211 const struct ieee80211_txparam *tp; 212 const struct ieee80211_tdma_state *tdma = NULL; 213 int rix; 214 215 if (vap == NULL) { 216 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */ 217 if (vap == NULL) { 218 if_printf(ifp, "%s: no vaps?\n", __func__); 219 return; 220 } 221 } 222 /* XXX should take a locked ref to iv_bss */ 223 tp = vap->iv_bss->ni_txparms; 224 /* 225 * Calculate the guard time for each slot. This is the 226 * time to send a maximal-size frame according to the 227 * fixed/lowest transmit rate. Note that the interface 228 * mtu does not include the 802.11 overhead so we must 229 * tack that on (ath_hal_computetxtime includes the 230 * preamble and plcp in it's calculation). 231 */ 232 tdma = vap->iv_tdma; 233 if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 234 rix = ath_tx_findrix(sc, tp->ucastrate); 235 else 236 rix = ath_tx_findrix(sc, tp->mcastrate); 237 /* XXX short preamble assumed */ 238 sc->sc_tdmaguard = ath_hal_computetxtime(ah, sc->sc_currates, 239 ifp->if_mtu + IEEE80211_MAXOVERHEAD, rix, AH_TRUE); 240 241 ath_hal_intrset(ah, 0); 242 243 ath_beaconq_config(sc); /* setup h/w beacon q */ 244 if (sc->sc_setcca) 245 ath_hal_setcca(ah, AH_FALSE); /* disable CCA */ 246 ath_tdma_bintvalsetup(sc, tdma); /* calculate beacon interval */ 247 ath_tdma_settimers(sc, sc->sc_tdmabintval, 248 sc->sc_tdmabintval | HAL_BEACON_RESET_TSF); 249 sc->sc_syncbeacon = 0; 250 251 sc->sc_avgtsfdeltap = TDMA_DUMMY_MARKER; 252 sc->sc_avgtsfdeltam = TDMA_DUMMY_MARKER; 253 254 ath_hal_intrset(ah, sc->sc_imask); 255 256 DPRINTF(sc, ATH_DEBUG_TDMA, "%s: slot %u len %uus cnt %u " 257 "bsched %u guard %uus bintval %u TU dba prep %u\n", __func__, 258 tdma->tdma_slot, tdma->tdma_slotlen, tdma->tdma_slotcnt, 259 tdma->tdma_bintval, sc->sc_tdmaguard, sc->sc_tdmabintval, 260 sc->sc_tdmadbaprep); 261} 262 263/* 264 * Update tdma operation. Called from the 802.11 layer 265 * when a beacon is received from the TDMA station operating 266 * in the slot immediately preceding us in the bss. Use 267 * the rx timestamp for the beacon frame to update our 268 * beacon timers so we follow their schedule. Note that 269 * by using the rx timestamp we implicitly include the 270 * propagation delay in our schedule. 271 */ 272void 273ath_tdma_update(struct ieee80211_node *ni, 274 const struct ieee80211_tdma_param *tdma, int changed) 275{ 276#define TSF_TO_TU(_h,_l) \ 277 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10)) 278#define TU_TO_TSF(_tu) (((u_int64_t)(_tu)) << 10) 279 struct ieee80211vap *vap = ni->ni_vap; 280 struct ieee80211com *ic = ni->ni_ic; 281 struct ath_softc *sc = ic->ic_ifp->if_softc; 282 struct ath_hal *ah = sc->sc_ah; 283 const HAL_RATE_TABLE *rt = sc->sc_currates; 284 u_int64_t tsf, rstamp, nextslot, nexttbtt; 285 u_int32_t txtime, nextslottu; 286 int32_t tudelta, tsfdelta; 287 const struct ath_rx_status *rs; 288 int rix; 289 290 sc->sc_stats.ast_tdma_update++; 291 292 /* 293 * Check for and adopt configuration changes. 294 */ 295 if (changed != 0) { 296 const struct ieee80211_tdma_state *ts = vap->iv_tdma; 297 298 ath_tdma_bintvalsetup(sc, ts); 299 if (changed & TDMA_UPDATE_SLOTLEN) 300 ath_wme_update(ic); 301 302 DPRINTF(sc, ATH_DEBUG_TDMA, 303 "%s: adopt slot %u slotcnt %u slotlen %u us " 304 "bintval %u TU\n", __func__, 305 ts->tdma_slot, ts->tdma_slotcnt, ts->tdma_slotlen, 306 sc->sc_tdmabintval); 307 308 /* XXX right? */ 309 ath_hal_intrset(ah, sc->sc_imask); 310 /* NB: beacon timers programmed below */ 311 } 312 313 /* extend rx timestamp to 64 bits */ 314 rs = sc->sc_lastrs; 315 tsf = ath_hal_gettsf64(ah); 316 rstamp = ath_extend_tsf(sc, rs->rs_tstamp, tsf); 317 /* 318 * The rx timestamp is set by the hardware on completing 319 * reception (at the point where the rx descriptor is DMA'd 320 * to the host). To find the start of our next slot we 321 * must adjust this time by the time required to send 322 * the packet just received. 323 */ 324 rix = rt->rateCodeToIndex[rs->rs_rate]; 325 txtime = ath_hal_computetxtime(ah, rt, rs->rs_datalen, rix, 326 rt->info[rix].shortPreamble); 327 /* NB: << 9 is to cvt to TU and /2 */ 328 nextslot = (rstamp - txtime) + (sc->sc_tdmabintval << 9); 329 nextslottu = TSF_TO_TU(nextslot>>32, nextslot) & HAL_BEACON_PERIOD; 330 331 /* 332 * Retrieve the hardware NextTBTT in usecs 333 * and calculate the difference between what the 334 * other station thinks and what we have programmed. This 335 * lets us figure how to adjust our timers to match. The 336 * adjustments are done by pulling the TSF forward and possibly 337 * rewriting the beacon timers. 338 */ 339 /* 340 * The logic here assumes the nexttbtt counter is in TSF 341 * but the prr-11n NICs are in TU. The HAL shifts them 342 * to TSF but there's two important differences: 343 * 344 * + The TU->TSF values have 0's for the low 9 bits, and 345 * + The counter wraps at TU_TO_TSF(HAL_BEACON_PERIOD + 1) for 346 * the pre-11n NICs, but not for the 11n NICs. 347 * 348 * So for now, just make sure the nexttbtt value we get 349 * matches the second issue or once nexttbtt exceeds this 350 * value, tsfdelta ends up becoming very negative and all 351 * of the adjustments get very messed up. 352 */ 353 nexttbtt = ath_hal_getnexttbtt(ah) % (TU_TO_TSF(HAL_BEACON_PERIOD + 1)); 354 tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt); 355 356 DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 357 "tsfdelta %d avg +%d/-%d\n", tsfdelta, 358 TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam)); 359 360 if (tsfdelta < 0) { 361 TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 362 TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta); 363 tsfdelta = -tsfdelta % 1024; 364 nextslottu++; 365 } else if (tsfdelta > 0) { 366 TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta); 367 TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 368 tsfdelta = 1024 - (tsfdelta % 1024); 369 nextslottu++; 370 } else { 371 TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 372 TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 373 } 374 tudelta = nextslottu - TSF_TO_TU(nexttbtt >> 32, nexttbtt); 375 376 /* 377 * Copy sender's timetstamp into tdma ie so they can 378 * calculate roundtrip time. We submit a beacon frame 379 * below after any timer adjustment. The frame goes out 380 * at the next TBTT so the sender can calculate the 381 * roundtrip by inspecting the tdma ie in our beacon frame. 382 * 383 * NB: This tstamp is subtlely preserved when 384 * IEEE80211_BEACON_TDMA is marked (e.g. when the 385 * slot position changes) because ieee80211_add_tdma 386 * skips over the data. 387 */ 388 memcpy(ATH_VAP(vap)->av_boff.bo_tdma + 389 __offsetof(struct ieee80211_tdma_param, tdma_tstamp), 390 &ni->ni_tstamp.data, 8); 391#if 0 392 DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 393 "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n", 394 (unsigned long long) tsf, (unsigned long long) nextslot, 395 (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta); 396#endif 397 /* 398 * Adjust the beacon timers only when pulling them forward 399 * or when going back by less than the beacon interval. 400 * Negative jumps larger than the beacon interval seem to 401 * cause the timers to stop and generally cause instability. 402 * This basically filters out jumps due to missed beacons. 403 */ 404 if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) { 405 ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval); 406 sc->sc_stats.ast_tdma_timers++; 407 } 408 if (tsfdelta > 0) { 409 uint64_t tsf; 410 411 /* XXX should just teach ath_hal_adjusttsf() to do this */ 412 tsf = ath_hal_gettsf64(ah); 413 ath_hal_settsf64(ah, tsf + tsfdelta); 414 sc->sc_stats.ast_tdma_tsf++; 415 } 416 ath_tdma_beacon_send(sc, vap); /* prepare response */ 417#undef TU_TO_TSF 418#undef TSF_TO_TU 419} 420 421/* 422 * Transmit a beacon frame at SWBA. Dynamic updates 423 * to the frame contents are done as needed. 424 */ 425void 426ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap) 427{ 428 struct ath_hal *ah = sc->sc_ah; 429 struct ath_buf *bf; 430 int otherant; 431 432 /* 433 * Check if the previous beacon has gone out. If 434 * not don't try to post another, skip this period 435 * and wait for the next. Missed beacons indicate 436 * a problem and should not occur. If we miss too 437 * many consecutive beacons reset the device. 438 */ 439 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 440 sc->sc_bmisscount++; 441 DPRINTF(sc, ATH_DEBUG_BEACON, 442 "%s: missed %u consecutive beacons\n", 443 __func__, sc->sc_bmisscount); 444 if (sc->sc_bmisscount >= ath_bstuck_threshold) 445 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 446 return; 447 } 448 if (sc->sc_bmisscount != 0) { 449 DPRINTF(sc, ATH_DEBUG_BEACON, 450 "%s: resume beacon xmit after %u misses\n", 451 __func__, sc->sc_bmisscount); 452 sc->sc_bmisscount = 0; 453 } 454 455 /* 456 * Check recent per-antenna transmit statistics and flip 457 * the default antenna if noticeably more frames went out 458 * on the non-default antenna. 459 * XXX assumes 2 anntenae 460 */ 461 if (!sc->sc_diversity) { 462 otherant = sc->sc_defant & 1 ? 2 : 1; 463 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 464 ath_setdefantenna(sc, otherant); 465 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 466 } 467 468 bf = ath_beacon_generate(sc, vap); 469 if (bf != NULL) { 470 /* 471 * Stop any current dma and put the new frame on the queue. 472 * This should never fail since we check above that no frames 473 * are still pending on the queue. 474 */ 475 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 476 DPRINTF(sc, ATH_DEBUG_ANY, 477 "%s: beacon queue %u did not stop?\n", 478 __func__, sc->sc_bhalq); 479 /* NB: the HAL still stops DMA, so proceed */ 480 } 481 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 482 ath_hal_txstart(ah, sc->sc_bhalq); 483 484 sc->sc_stats.ast_be_xmit++; /* XXX per-vap? */ 485 486 /* 487 * Record local TSF for our last send for use 488 * in arbitrating slot collisions. 489 */ 490 /* XXX should take a locked ref to iv_bss */ 491 vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah); 492 } 493} 494#endif /* IEEE80211_SUPPORT_TDMA */ 495