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 89 #include <net/bpf.h> 90 91 #ifdef INET 92 #include <netinet/in.h> 93 #include <netinet/if_ether.h> 94 #endif 95 96 #include <dev/ath/if_athvar.h> 97 98 #include <dev/ath/if_ath_debug.h> 99 #include <dev/ath/if_ath_misc.h> 100 #include <dev/ath/if_ath_tx.h> 101 #include <dev/ath/if_ath_beacon.h> 102 103 #ifdef ATH_TX99_DIAG 104 #include <dev/ath/ath_tx99/ath_tx99.h> 105 #endif 106 107 /* 108 * Setup a h/w transmit queue for beacons. 109 */ 110 int 111 ath_beaconq_setup(struct ath_softc *sc) 112 { 113 struct ath_hal *ah = sc->sc_ah; 114 HAL_TXQ_INFO qi; 115 116 memset(&qi, 0, sizeof(qi)); 117 qi.tqi_aifs = HAL_TXQ_USEDEFAULT; 118 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT; 119 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT; 120 /* NB: for dynamic turbo, don't enable any other interrupts */ 121 qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE; 122 if (sc->sc_isedma) 123 qi.tqi_qflags |= HAL_TXQ_TXOKINT_ENABLE | 124 HAL_TXQ_TXERRINT_ENABLE; 125 126 return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi); 127 } 128 129 /* 130 * Setup the transmit queue parameters for the beacon queue. 131 */ 132 int 133 ath_beaconq_config(struct ath_softc *sc) 134 { 135 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1) 136 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 137 struct ath_hal *ah = sc->sc_ah; 138 HAL_TXQ_INFO qi; 139 140 ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi); 141 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 142 ic->ic_opmode == IEEE80211_M_MBSS) { 143 /* 144 * Always burst out beacon and CAB traffic. 145 */ 146 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT; 147 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT; 148 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT; 149 } else { 150 struct wmeParams *wmep = 151 &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE]; 152 /* 153 * Adhoc mode; important thing is to use 2x cwmin. 154 */ 155 qi.tqi_aifs = wmep->wmep_aifsn; 156 qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin); 157 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax); 158 } 159 160 if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) { 161 device_printf(sc->sc_dev, "unable to update parameters for " 162 "beacon hardware queue!\n"); 163 return 0; 164 } else { 165 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */ 166 return 1; 167 } 168 #undef ATH_EXPONENT_TO_VALUE 169 } 170 171 /* 172 * Allocate and setup an initial beacon frame. 173 */ 174 int 175 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni) 176 { 177 struct ieee80211vap *vap = ni->ni_vap; 178 struct ath_vap *avp = ATH_VAP(vap); 179 struct ath_buf *bf; 180 struct mbuf *m; 181 int error; 182 183 bf = avp->av_bcbuf; 184 DPRINTF(sc, ATH_DEBUG_NODE, "%s: bf_m=%p, bf_node=%p\n", 185 __func__, bf->bf_m, bf->bf_node); 186 if (bf->bf_m != NULL) { 187 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 188 m_freem(bf->bf_m); 189 bf->bf_m = NULL; 190 } 191 if (bf->bf_node != NULL) { 192 ieee80211_free_node(bf->bf_node); 193 bf->bf_node = NULL; 194 } 195 196 /* 197 * NB: the beacon data buffer must be 32-bit aligned; 198 * we assume the mbuf routines will return us something 199 * with this alignment (perhaps should assert). 200 */ 201 m = ieee80211_beacon_alloc(ni, &avp->av_boff); 202 if (m == NULL) { 203 device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__); 204 sc->sc_stats.ast_be_nombuf++; 205 return ENOMEM; 206 } 207 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 208 bf->bf_segs, &bf->bf_nseg, 209 BUS_DMA_NOWAIT); 210 if (error != 0) { 211 device_printf(sc->sc_dev, 212 "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n", 213 __func__, error); 214 m_freem(m); 215 return error; 216 } 217 218 /* 219 * Calculate a TSF adjustment factor required for staggered 220 * beacons. Note that we assume the format of the beacon 221 * frame leaves the tstamp field immediately following the 222 * header. 223 */ 224 if (sc->sc_stagbeacons && avp->av_bslot > 0) { 225 uint64_t tsfadjust; 226 struct ieee80211_frame *wh; 227 228 /* 229 * The beacon interval is in TU's; the TSF is in usecs. 230 * We figure out how many TU's to add to align the timestamp 231 * then convert to TSF units and handle byte swapping before 232 * inserting it in the frame. The hardware will then add this 233 * each time a beacon frame is sent. Note that we align vap's 234 * 1..N and leave vap 0 untouched. This means vap 0 has a 235 * timestamp in one beacon interval while the others get a 236 * timstamp aligned to the next interval. 237 */ 238 tsfadjust = ni->ni_intval * 239 (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF; 240 tsfadjust = htole64(tsfadjust << 10); /* TU -> TSF */ 241 242 DPRINTF(sc, ATH_DEBUG_BEACON, 243 "%s: %s beacons bslot %d intval %u tsfadjust %llu\n", 244 __func__, sc->sc_stagbeacons ? "stagger" : "burst", 245 avp->av_bslot, ni->ni_intval, 246 (long long unsigned) le64toh(tsfadjust)); 247 248 wh = mtod(m, struct ieee80211_frame *); 249 memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust)); 250 } 251 bf->bf_m = m; 252 bf->bf_node = ieee80211_ref_node(ni); 253 254 return 0; 255 } 256 257 /* 258 * Setup the beacon frame for transmit. 259 */ 260 static void 261 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf) 262 { 263 #define USE_SHPREAMBLE(_ic) \ 264 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\ 265 == IEEE80211_F_SHPREAMBLE) 266 struct ieee80211_node *ni = bf->bf_node; 267 struct ieee80211com *ic = ni->ni_ic; 268 struct mbuf *m = bf->bf_m; 269 struct ath_hal *ah = sc->sc_ah; 270 struct ath_desc *ds; 271 int flags, antenna; 272 const HAL_RATE_TABLE *rt; 273 u_int8_t rix, rate; 274 HAL_DMA_ADDR bufAddrList[4]; 275 uint32_t segLenList[4]; 276 HAL_11N_RATE_SERIES rc[4]; 277 278 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n", 279 __func__, m, m->m_len); 280 281 /* setup descriptors */ 282 ds = bf->bf_desc; 283 bf->bf_last = bf; 284 bf->bf_lastds = ds; 285 286 flags = HAL_TXDESC_NOACK; 287 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) { 288 /* self-linked descriptor */ 289 ath_hal_settxdesclink(sc->sc_ah, ds, bf->bf_daddr); 290 flags |= HAL_TXDESC_VEOL; 291 /* 292 * Let hardware handle antenna switching. 293 */ 294 antenna = sc->sc_txantenna; 295 } else { 296 ath_hal_settxdesclink(sc->sc_ah, ds, 0); 297 /* 298 * Switch antenna every 4 beacons. 299 * XXX assumes two antenna 300 */ 301 if (sc->sc_txantenna != 0) 302 antenna = sc->sc_txantenna; 303 else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0) 304 antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1); 305 else 306 antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1); 307 } 308 309 KASSERT(bf->bf_nseg == 1, 310 ("multi-segment beacon frame; nseg %u", bf->bf_nseg)); 311 312 /* 313 * Calculate rate code. 314 * XXX everything at min xmit rate 315 */ 316 rix = 0; 317 rt = sc->sc_currates; 318 rate = rt->info[rix].rateCode; 319 if (USE_SHPREAMBLE(ic)) 320 rate |= rt->info[rix].shortPreamble; 321 ath_hal_setuptxdesc(ah, ds 322 , m->m_len + IEEE80211_CRC_LEN /* frame length */ 323 , sizeof(struct ieee80211_frame)/* header length */ 324 , HAL_PKT_TYPE_BEACON /* Atheros packet type */ 325 , ni->ni_txpower /* txpower XXX */ 326 , rate, 1 /* series 0 rate/tries */ 327 , HAL_TXKEYIX_INVALID /* no encryption */ 328 , antenna /* antenna mode */ 329 , flags /* no ack, veol for beacons */ 330 , 0 /* rts/cts rate */ 331 , 0 /* rts/cts duration */ 332 ); 333 334 /* 335 * The EDMA HAL currently assumes that _all_ rate control 336 * settings are done in ath_hal_set11nratescenario(), rather 337 * than in ath_hal_setuptxdesc(). 338 */ 339 if (sc->sc_isedma) { 340 memset(&rc, 0, sizeof(rc)); 341 342 rc[0].ChSel = sc->sc_txchainmask; 343 rc[0].Tries = 1; 344 rc[0].Rate = rt->info[rix].rateCode; 345 rc[0].RateIndex = rix; 346 rc[0].tx_power_cap = 0x3f; 347 rc[0].PktDuration = 348 ath_hal_computetxtime(ah, rt, roundup(m->m_len, 4), 349 rix, 0); 350 ath_hal_set11nratescenario(ah, ds, 0, 0, rc, 4, flags); 351 } 352 353 /* NB: beacon's BufLen must be a multiple of 4 bytes */ 354 segLenList[0] = roundup(m->m_len, 4); 355 segLenList[1] = segLenList[2] = segLenList[3] = 0; 356 bufAddrList[0] = bf->bf_segs[0].ds_addr; 357 bufAddrList[1] = bufAddrList[2] = bufAddrList[3] = 0; 358 ath_hal_filltxdesc(ah, ds 359 , bufAddrList 360 , segLenList 361 , 0 /* XXX desc id */ 362 , sc->sc_bhalq /* hardware TXQ */ 363 , AH_TRUE /* first segment */ 364 , AH_TRUE /* last segment */ 365 , ds /* first descriptor */ 366 ); 367 #if 0 368 ath_desc_swap(ds); 369 #endif 370 #undef USE_SHPREAMBLE 371 } 372 373 void 374 ath_beacon_update(struct ieee80211vap *vap, int item) 375 { 376 struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff; 377 378 setbit(bo->bo_flags, item); 379 } 380 381 /* 382 * Transmit a beacon frame at SWBA. Dynamic updates to the 383 * frame contents are done as needed and the slot time is 384 * also adjusted based on current state. 385 */ 386 void 387 ath_beacon_proc(void *arg, int pending) 388 { 389 struct ath_softc *sc = arg; 390 struct ath_hal *ah = sc->sc_ah; 391 struct ieee80211vap *vap; 392 struct ath_buf *bf; 393 int slot, otherant; 394 uint32_t bfaddr; 395 396 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n", 397 __func__, pending); 398 /* 399 * Check if the previous beacon has gone out. If 400 * not don't try to post another, skip this period 401 * and wait for the next. Missed beacons indicate 402 * a problem and should not occur. If we miss too 403 * many consecutive beacons reset the device. 404 */ 405 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) { 406 sc->sc_bmisscount++; 407 sc->sc_stats.ast_be_missed++; 408 DPRINTF(sc, ATH_DEBUG_BEACON, 409 "%s: missed %u consecutive beacons\n", 410 __func__, sc->sc_bmisscount); 411 if (sc->sc_bmisscount >= ath_bstuck_threshold) 412 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask); 413 return; 414 } 415 if (sc->sc_bmisscount != 0) { 416 DPRINTF(sc, ATH_DEBUG_BEACON, 417 "%s: resume beacon xmit after %u misses\n", 418 __func__, sc->sc_bmisscount); 419 sc->sc_bmisscount = 0; 420 } 421 422 if (sc->sc_stagbeacons) { /* staggered beacons */ 423 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 424 uint32_t tsftu; 425 426 tsftu = ath_hal_gettsf32(ah) >> 10; 427 /* XXX lintval */ 428 slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval; 429 vap = sc->sc_bslot[(slot+1) % ATH_BCBUF]; 430 bfaddr = 0; 431 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) { 432 bf = ath_beacon_generate(sc, vap); 433 if (bf != NULL) 434 bfaddr = bf->bf_daddr; 435 } 436 } else { /* burst'd beacons */ 437 uint32_t *bflink = &bfaddr; 438 439 for (slot = 0; slot < ATH_BCBUF; slot++) { 440 vap = sc->sc_bslot[slot]; 441 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) { 442 bf = ath_beacon_generate(sc, vap); 443 if (bf != NULL) { 444 /* XXX should do this using the ds */ 445 *bflink = bf->bf_daddr; 446 ath_hal_gettxdesclinkptr(sc->sc_ah, 447 bf->bf_desc, &bflink); 448 } 449 } 450 } 451 *bflink = 0; /* terminate list */ 452 } 453 454 /* 455 * Handle slot time change when a non-ERP station joins/leaves 456 * an 11g network. The 802.11 layer notifies us via callback, 457 * we mark updateslot, then wait one beacon before effecting 458 * the change. This gives associated stations at least one 459 * beacon interval to note the state change. 460 */ 461 /* XXX locking */ 462 if (sc->sc_updateslot == UPDATE) { 463 sc->sc_updateslot = COMMIT; /* commit next beacon */ 464 sc->sc_slotupdate = slot; 465 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot) 466 ath_setslottime(sc); /* commit change to h/w */ 467 468 /* 469 * Check recent per-antenna transmit statistics and flip 470 * the default antenna if noticeably more frames went out 471 * on the non-default antenna. 472 * XXX assumes 2 anntenae 473 */ 474 if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) { 475 otherant = sc->sc_defant & 1 ? 2 : 1; 476 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2) 477 ath_setdefantenna(sc, otherant); 478 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0; 479 } 480 481 if (bfaddr != 0) { 482 /* 483 * Stop any current dma and put the new frame on the queue. 484 * This should never fail since we check above that no frames 485 * are still pending on the queue. 486 */ 487 if (! sc->sc_isedma) { 488 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) { 489 DPRINTF(sc, ATH_DEBUG_ANY, 490 "%s: beacon queue %u did not stop?\n", 491 __func__, sc->sc_bhalq); 492 } 493 } 494 /* NB: cabq traffic should already be queued and primed */ 495 496 ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr); 497 ath_hal_txstart(ah, sc->sc_bhalq); 498 499 sc->sc_stats.ast_be_xmit++; 500 } 501 } 502 503 struct ath_buf * 504 ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap) 505 { 506 struct ath_vap *avp = ATH_VAP(vap); 507 struct ath_txq *cabq = sc->sc_cabq; 508 struct ath_buf *bf; 509 struct mbuf *m; 510 int nmcastq, error; 511 512 KASSERT(vap->iv_state >= IEEE80211_S_RUN, 513 ("not running, state %d", vap->iv_state)); 514 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer")); 515 516 /* 517 * Update dynamic beacon contents. If this returns 518 * non-zero then we need to remap the memory because 519 * the beacon frame changed size (probably because 520 * of the TIM bitmap). 521 */ 522 bf = avp->av_bcbuf; 523 m = bf->bf_m; 524 /* XXX lock mcastq? */ 525 nmcastq = avp->av_mcastq.axq_depth; 526 527 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) { 528 /* XXX too conservative? */ 529 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 530 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 531 bf->bf_segs, &bf->bf_nseg, 532 BUS_DMA_NOWAIT); 533 if (error != 0) { 534 if_printf(vap->iv_ifp, 535 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n", 536 __func__, error); 537 return NULL; 538 } 539 } 540 if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) { 541 DPRINTF(sc, ATH_DEBUG_BEACON, 542 "%s: cabq did not drain, mcastq %u cabq %u\n", 543 __func__, nmcastq, cabq->axq_depth); 544 sc->sc_stats.ast_cabq_busy++; 545 if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) { 546 /* 547 * CABQ traffic from a previous vap is still pending. 548 * We must drain the q before this beacon frame goes 549 * out as otherwise this vap's stations will get cab 550 * frames from a different vap. 551 * XXX could be slow causing us to miss DBA 552 */ 553 ath_tx_draintxq(sc, cabq); 554 } 555 } 556 ath_beacon_setup(sc, bf); 557 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 558 559 /* 560 * Enable the CAB queue before the beacon queue to 561 * insure cab frames are triggered by this beacon. 562 */ 563 if (avp->av_boff.bo_tim[4] & 1) { 564 struct ath_hal *ah = sc->sc_ah; 565 566 /* NB: only at DTIM */ 567 ATH_TXQ_LOCK(cabq); 568 ATH_TXQ_LOCK(&avp->av_mcastq); 569 if (nmcastq) { 570 struct ath_buf *bfm; 571 572 /* 573 * Move frames from the s/w mcast q to the h/w cab q. 574 * XXX MORE_DATA bit 575 */ 576 bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q); 577 if (cabq->axq_link != NULL) { 578 *cabq->axq_link = bfm->bf_daddr; 579 } else 580 ath_hal_puttxbuf(ah, cabq->axq_qnum, 581 bfm->bf_daddr); 582 ath_txqmove(cabq, &avp->av_mcastq); 583 584 sc->sc_stats.ast_cabq_xmit += nmcastq; 585 } 586 /* NB: gated by beacon so safe to start here */ 587 if (! TAILQ_EMPTY(&(cabq->axq_q))) 588 ath_hal_txstart(ah, cabq->axq_qnum); 589 ATH_TXQ_UNLOCK(&avp->av_mcastq); 590 ATH_TXQ_UNLOCK(cabq); 591 } 592 return bf; 593 } 594 595 void 596 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap) 597 { 598 struct ath_vap *avp = ATH_VAP(vap); 599 struct ath_hal *ah = sc->sc_ah; 600 struct ath_buf *bf; 601 struct mbuf *m; 602 int error; 603 604 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer")); 605 606 /* 607 * Update dynamic beacon contents. If this returns 608 * non-zero then we need to remap the memory because 609 * the beacon frame changed size (probably because 610 * of the TIM bitmap). 611 */ 612 bf = avp->av_bcbuf; 613 m = bf->bf_m; 614 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) { 615 /* XXX too conservative? */ 616 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 617 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m, 618 bf->bf_segs, &bf->bf_nseg, 619 BUS_DMA_NOWAIT); 620 if (error != 0) { 621 if_printf(vap->iv_ifp, 622 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n", 623 __func__, error); 624 return; 625 } 626 } 627 ath_beacon_setup(sc, bf); 628 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE); 629 630 /* NB: caller is known to have already stopped tx dma */ 631 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr); 632 ath_hal_txstart(ah, sc->sc_bhalq); 633 } 634 635 /* 636 * Reclaim beacon resources and return buffer to the pool. 637 */ 638 void 639 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf) 640 { 641 642 DPRINTF(sc, ATH_DEBUG_NODE, "%s: free bf=%p, bf_m=%p, bf_node=%p\n", 643 __func__, bf, bf->bf_m, bf->bf_node); 644 if (bf->bf_m != NULL) { 645 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 646 m_freem(bf->bf_m); 647 bf->bf_m = NULL; 648 } 649 if (bf->bf_node != NULL) { 650 ieee80211_free_node(bf->bf_node); 651 bf->bf_node = NULL; 652 } 653 TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list); 654 } 655 656 /* 657 * Reclaim beacon resources. 658 */ 659 void 660 ath_beacon_free(struct ath_softc *sc) 661 { 662 struct ath_buf *bf; 663 664 TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) { 665 DPRINTF(sc, ATH_DEBUG_NODE, 666 "%s: free bf=%p, bf_m=%p, bf_node=%p\n", 667 __func__, bf, bf->bf_m, bf->bf_node); 668 if (bf->bf_m != NULL) { 669 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap); 670 m_freem(bf->bf_m); 671 bf->bf_m = NULL; 672 } 673 if (bf->bf_node != NULL) { 674 ieee80211_free_node(bf->bf_node); 675 bf->bf_node = NULL; 676 } 677 } 678 } 679 680 /* 681 * Configure the beacon and sleep timers. 682 * 683 * When operating as an AP this resets the TSF and sets 684 * up the hardware to notify us when we need to issue beacons. 685 * 686 * When operating in station mode this sets up the beacon 687 * timers according to the timestamp of the last received 688 * beacon and the current TSF, configures PCF and DTIM 689 * handling, programs the sleep registers so the hardware 690 * will wakeup in time to receive beacons, and configures 691 * the beacon miss handling so we'll receive a BMISS 692 * interrupt when we stop seeing beacons from the AP 693 * we've associated with. 694 */ 695 void 696 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap) 697 { 698 #define TSF_TO_TU(_h,_l) \ 699 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10)) 700 #define FUDGE 2 701 struct ath_hal *ah = sc->sc_ah; 702 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 703 struct ieee80211_node *ni; 704 u_int32_t nexttbtt, intval, tsftu; 705 u_int32_t nexttbtt_u8, intval_u8; 706 u_int64_t tsf; 707 708 if (vap == NULL) 709 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */ 710 ni = ieee80211_ref_node(vap->iv_bss); 711 712 /* extract tstamp from last beacon and convert to TU */ 713 nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4), 714 LE_READ_4(ni->ni_tstamp.data)); 715 if (ic->ic_opmode == IEEE80211_M_HOSTAP || 716 ic->ic_opmode == IEEE80211_M_MBSS) { 717 /* 718 * For multi-bss ap/mesh support beacons are either staggered 719 * evenly over N slots or burst together. For the former 720 * arrange for the SWBA to be delivered for each slot. 721 * Slots that are not occupied will generate nothing. 722 */ 723 /* NB: the beacon interval is kept internally in TU's */ 724 intval = ni->ni_intval & HAL_BEACON_PERIOD; 725 if (sc->sc_stagbeacons) 726 intval /= ATH_BCBUF; 727 } else { 728 /* NB: the beacon interval is kept internally in TU's */ 729 intval = ni->ni_intval & HAL_BEACON_PERIOD; 730 } 731 if (nexttbtt == 0) /* e.g. for ap mode */ 732 nexttbtt = intval; 733 else if (intval) /* NB: can be 0 for monitor mode */ 734 nexttbtt = roundup(nexttbtt, intval); 735 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n", 736 __func__, nexttbtt, intval, ni->ni_intval); 737 if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) { 738 HAL_BEACON_STATE bs; 739 int dtimperiod, dtimcount; 740 int cfpperiod, cfpcount; 741 742 /* 743 * Setup dtim and cfp parameters according to 744 * last beacon we received (which may be none). 745 */ 746 dtimperiod = ni->ni_dtim_period; 747 if (dtimperiod <= 0) /* NB: 0 if not known */ 748 dtimperiod = 1; 749 dtimcount = ni->ni_dtim_count; 750 if (dtimcount >= dtimperiod) /* NB: sanity check */ 751 dtimcount = 0; /* XXX? */ 752 cfpperiod = 1; /* NB: no PCF support yet */ 753 cfpcount = 0; 754 /* 755 * Pull nexttbtt forward to reflect the current 756 * TSF and calculate dtim+cfp state for the result. 757 */ 758 tsf = ath_hal_gettsf64(ah); 759 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE; 760 do { 761 nexttbtt += intval; 762 if (--dtimcount < 0) { 763 dtimcount = dtimperiod - 1; 764 if (--cfpcount < 0) 765 cfpcount = cfpperiod - 1; 766 } 767 } while (nexttbtt < tsftu); 768 memset(&bs, 0, sizeof(bs)); 769 bs.bs_intval = intval; 770 bs.bs_nexttbtt = nexttbtt; 771 bs.bs_dtimperiod = dtimperiod*intval; 772 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval; 773 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod; 774 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod; 775 bs.bs_cfpmaxduration = 0; 776 #if 0 777 /* 778 * The 802.11 layer records the offset to the DTIM 779 * bitmap while receiving beacons; use it here to 780 * enable h/w detection of our AID being marked in 781 * the bitmap vector (to indicate frames for us are 782 * pending at the AP). 783 * XXX do DTIM handling in s/w to WAR old h/w bugs 784 * XXX enable based on h/w rev for newer chips 785 */ 786 bs.bs_timoffset = ni->ni_timoff; 787 #endif 788 /* 789 * Calculate the number of consecutive beacons to miss 790 * before taking a BMISS interrupt. 791 * Note that we clamp the result to at most 10 beacons. 792 */ 793 bs.bs_bmissthreshold = vap->iv_bmissthreshold; 794 if (bs.bs_bmissthreshold > 10) 795 bs.bs_bmissthreshold = 10; 796 else if (bs.bs_bmissthreshold <= 0) 797 bs.bs_bmissthreshold = 1; 798 799 /* 800 * Calculate sleep duration. The configuration is 801 * given in ms. We insure a multiple of the beacon 802 * period is used. Also, if the sleep duration is 803 * greater than the DTIM period then it makes senses 804 * to make it a multiple of that. 805 * 806 * XXX fixed at 100ms 807 */ 808 bs.bs_sleepduration = 809 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval); 810 if (bs.bs_sleepduration > bs.bs_dtimperiod) 811 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod); 812 813 DPRINTF(sc, ATH_DEBUG_BEACON, 814 "%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u nextdtim %u bmiss %u sleep %u cfp:period %u maxdur %u next %u timoffset %u\n" 815 , __func__ 816 , tsf, tsftu 817 , bs.bs_intval 818 , bs.bs_nexttbtt 819 , bs.bs_dtimperiod 820 , bs.bs_nextdtim 821 , bs.bs_bmissthreshold 822 , bs.bs_sleepduration 823 , bs.bs_cfpperiod 824 , bs.bs_cfpmaxduration 825 , bs.bs_cfpnext 826 , bs.bs_timoffset 827 ); 828 ath_hal_intrset(ah, 0); 829 ath_hal_beacontimers(ah, &bs); 830 sc->sc_imask |= HAL_INT_BMISS; 831 ath_hal_intrset(ah, sc->sc_imask); 832 } else { 833 ath_hal_intrset(ah, 0); 834 if (nexttbtt == intval) 835 intval |= HAL_BEACON_RESET_TSF; 836 if (ic->ic_opmode == IEEE80211_M_IBSS) { 837 /* 838 * In IBSS mode enable the beacon timers but only 839 * enable SWBA interrupts if we need to manually 840 * prepare beacon frames. Otherwise we use a 841 * self-linked tx descriptor and let the hardware 842 * deal with things. 843 */ 844 intval |= HAL_BEACON_ENA; 845 if (!sc->sc_hasveol) 846 sc->sc_imask |= HAL_INT_SWBA; 847 if ((intval & HAL_BEACON_RESET_TSF) == 0) { 848 /* 849 * Pull nexttbtt forward to reflect 850 * the current TSF. 851 */ 852 tsf = ath_hal_gettsf64(ah); 853 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE; 854 do { 855 nexttbtt += intval; 856 } while (nexttbtt < tsftu); 857 } 858 ath_beaconq_config(sc); 859 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP || 860 ic->ic_opmode == IEEE80211_M_MBSS) { 861 /* 862 * In AP/mesh mode we enable the beacon timers 863 * and SWBA interrupts to prepare beacon frames. 864 */ 865 intval |= HAL_BEACON_ENA; 866 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */ 867 ath_beaconq_config(sc); 868 } 869 870 /* 871 * Now dirty things because for now, the EDMA HAL has 872 * nexttbtt and intval is TU/8. 873 */ 874 if (sc->sc_isedma) { 875 nexttbtt_u8 = (nexttbtt << 3); 876 intval_u8 = (intval << 3); 877 if (intval & HAL_BEACON_ENA) 878 intval_u8 |= HAL_BEACON_ENA; 879 if (intval & HAL_BEACON_RESET_TSF) 880 intval_u8 |= HAL_BEACON_RESET_TSF; 881 ath_hal_beaconinit(ah, nexttbtt_u8, intval_u8); 882 } else 883 ath_hal_beaconinit(ah, nexttbtt, intval); 884 sc->sc_bmisscount = 0; 885 ath_hal_intrset(ah, sc->sc_imask); 886 /* 887 * When using a self-linked beacon descriptor in 888 * ibss mode load it once here. 889 */ 890 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) 891 ath_beacon_start_adhoc(sc, vap); 892 } 893 sc->sc_syncbeacon = 0; 894 ieee80211_free_node(ni); 895 #undef FUDGE 896 #undef TSF_TO_TU 897 } 898