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$"); 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 121 static void ath_tdma_settimers(struct ath_softc *sc, u_int32_t nexttbtt, 122 u_int32_t bintval); 123 static 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 128 static void 129 ath_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 */ 159 static void 160 ath_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 */ 205 void 206 ath_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 */ 272 void 273 ath_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 nexttbtt = ath_hal_getnexttbtt(ah); 340 tsfdelta = (int32_t)((nextslot % TU_TO_TSF(HAL_BEACON_PERIOD + 1)) - nexttbtt); 341 342 DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 343 "tsfdelta %d avg +%d/-%d\n", tsfdelta, 344 TDMA_AVG(sc->sc_avgtsfdeltap), TDMA_AVG(sc->sc_avgtsfdeltam)); 345 346 if (tsfdelta < 0) { 347 TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 348 TDMA_SAMPLE(sc->sc_avgtsfdeltam, -tsfdelta); 349 tsfdelta = -tsfdelta % 1024; 350 nextslottu++; 351 } else if (tsfdelta > 0) { 352 TDMA_SAMPLE(sc->sc_avgtsfdeltap, tsfdelta); 353 TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 354 tsfdelta = 1024 - (tsfdelta % 1024); 355 nextslottu++; 356 } else { 357 TDMA_SAMPLE(sc->sc_avgtsfdeltap, 0); 358 TDMA_SAMPLE(sc->sc_avgtsfdeltam, 0); 359 } 360 tudelta = nextslottu - TSF_TO_TU(nexttbtt >> 32, nexttbtt); 361 362 /* 363 * Copy sender's timetstamp into tdma ie so they can 364 * calculate roundtrip time. We submit a beacon frame 365 * below after any timer adjustment. The frame goes out 366 * at the next TBTT so the sender can calculate the 367 * roundtrip by inspecting the tdma ie in our beacon frame. 368 * 369 * NB: This tstamp is subtlely preserved when 370 * IEEE80211_BEACON_TDMA is marked (e.g. when the 371 * slot position changes) because ieee80211_add_tdma 372 * skips over the data. 373 */ 374 memcpy(ATH_VAP(vap)->av_boff.bo_tdma + 375 __offsetof(struct ieee80211_tdma_param, tdma_tstamp), 376 &ni->ni_tstamp.data, 8); 377 #if 0 378 DPRINTF(sc, ATH_DEBUG_TDMA_TIMER, 379 "tsf %llu nextslot %llu (%d, %d) nextslottu %u nexttbtt %llu (%d)\n", 380 (unsigned long long) tsf, (unsigned long long) nextslot, 381 (int)(nextslot - tsf), tsfdelta, nextslottu, nexttbtt, tudelta); 382 #endif 383 /* 384 * Adjust the beacon timers only when pulling them forward 385 * or when going back by less than the beacon interval. 386 * Negative jumps larger than the beacon interval seem to 387 * cause the timers to stop and generally cause instability. 388 * This basically filters out jumps due to missed beacons. 389 */ 390 if (tudelta != 0 && (tudelta > 0 || -tudelta < sc->sc_tdmabintval)) { 391 ath_tdma_settimers(sc, nextslottu, sc->sc_tdmabintval); 392 sc->sc_stats.ast_tdma_timers++; 393 } 394 if (tsfdelta > 0) { 395 ath_hal_adjusttsf(ah, tsfdelta); 396 sc->sc_stats.ast_tdma_tsf++; 397 } 398 ath_tdma_beacon_send(sc, vap); /* prepare response */ 399 #undef TU_TO_TSF 400 #undef TSF_TO_TU 401 } 402 403 /* 404 * Transmit a beacon frame at SWBA. Dynamic updates 405 * to the frame contents are done as needed. 406 */ 407 void 408 ath_tdma_beacon_send(struct ath_softc *sc, struct ieee80211vap *vap) 409 { 410 struct ath_hal *ah = sc->sc_ah; 411 struct ath_buf *bf; 412 int otherant; 413 414 /* 415 * Check if the previous beacon has gone out. If 416 * not don't try to post another, skip this period 417 * and wait for the next. Missed beacons indicate 418 * a problem and should not occur. If we miss too 419 * many consecutive beacons reset the device. 420 */ 421 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 422 sc->sc_bmisscount++; 423 DPRINTF(sc, ATH_DEBUG_BEACON, 424 "%s: missed %u consecutive beacons\n", 425 __func__, sc->sc_bmisscount); 426 if (sc->sc_bmisscount >= ath_bstuck_threshold) 427 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 428 return; 429 } 430 if (sc->sc_bmisscount != 0) { 431 DPRINTF(sc, ATH_DEBUG_BEACON, 432 "%s: resume beacon xmit after %u misses\n", 433 __func__, sc->sc_bmisscount); 434 sc->sc_bmisscount = 0; 435 } 436 437 /* 438 * Check recent per-antenna transmit statistics and flip 439 * the default antenna if noticeably more frames went out 440 * on the non-default antenna. 441 * XXX assumes 2 anntenae 442 */ 443 if (!sc->sc_diversity) { 444 otherant = sc->sc_defant & 1 ? 2 : 1; 445 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 446 ath_setdefantenna(sc, otherant); 447 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 448 } 449 450 bf = ath_beacon_generate(sc, vap); 451 if (bf != NULL) { 452 /* 453 * Stop any current dma and put the new frame on the queue. 454 * This should never fail since we check above that no frames 455 * are still pending on the queue. 456 */ 457 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 458 DPRINTF(sc, ATH_DEBUG_ANY, 459 "%s: beacon queue %u did not stop?\n", 460 __func__, sc->sc_bhalq); 461 /* NB: the HAL still stops DMA, so proceed */ 462 } 463 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 464 ath_hal_txstart(ah, sc->sc_bhalq); 465 466 sc->sc_stats.ast_be_xmit++; /* XXX per-vap? */ 467 468 /* 469 * Record local TSF for our last send for use 470 * in arbitrating slot collisions. 471 */ 472 /* XXX should take a locked ref to iv_bss */ 473 vap->iv_bss->ni_tstamp.tsf = ath_hal_gettsf64(ah); 474 } 475 } 476 #endif /* IEEE80211_SUPPORT_TDMA */ 477