2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for the Atheros Wireless LAN controller.
36 * This software is derived from work of Atsushi Onoe; his contribution
37 * is greatly appreciated.
43 * This is needed for register operations which are performed
44 * by the driver - eg, calls to ath_hal_gettsf32().
46 * It's also required for any AH_DEBUG checks in here, eg the
47 * module dependencies.
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysctl.h>
56 #include <sys/malloc.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>
65 #include <sys/endian.h>
66 #include <sys/kthread.h>
67 #include <sys/taskqueue.h>
69 #include <sys/module.h>
71 #include <sys/smp.h> /* for mp_ncpus */
73 #include <machine/bus.h>
76 #include <net/if_var.h>
77 #include <net/if_dl.h>
78 #include <net/if_media.h>
79 #include <net/if_types.h>
80 #include <net/if_arp.h>
81 #include <net/ethernet.h>
82 #include <net/if_llc.h>
84 #include <net80211/ieee80211_var.h>
85 #include <net80211/ieee80211_regdomain.h>
86 #ifdef IEEE80211_SUPPORT_SUPERG
87 #include <net80211/ieee80211_superg.h>
93 #include <netinet/in.h>
94 #include <netinet/if_ether.h>
97 #include <dev/ath/if_athvar.h>
99 #include <dev/ath/if_ath_debug.h>
100 #include <dev/ath/if_ath_misc.h>
101 #include <dev/ath/if_ath_tx.h>
102 #include <dev/ath/if_ath_beacon.h>
105 #include <dev/ath/ath_tx99/ath_tx99.h>
109 * Setup a h/w transmit queue for beacons.
112 ath_beaconq_setup(struct ath_softc *sc)
114 struct ath_hal *ah = sc->sc_ah;
117 memset(&qi, 0, sizeof(qi));
118 qi.tqi_aifs = HAL_TXQ_USEDEFAULT;
119 qi.tqi_cwmin = HAL_TXQ_USEDEFAULT;
120 qi.tqi_cwmax = HAL_TXQ_USEDEFAULT;
121 /* NB: for dynamic turbo, don't enable any other interrupts */
122 qi.tqi_qflags = HAL_TXQ_TXDESCINT_ENABLE;
124 qi.tqi_qflags |= HAL_TXQ_TXOKINT_ENABLE |
125 HAL_TXQ_TXERRINT_ENABLE;
127 return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
131 * Setup the transmit queue parameters for the beacon queue.
134 ath_beaconq_config(struct ath_softc *sc)
136 #define ATH_EXPONENT_TO_VALUE(v) ((1<<(v))-1)
137 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
138 struct ath_hal *ah = sc->sc_ah;
141 ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
142 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
143 ic->ic_opmode == IEEE80211_M_MBSS) {
145 * Always burst out beacon and CAB traffic.
147 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
148 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
149 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
151 struct wmeParams *wmep =
152 &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
154 * Adhoc mode; important thing is to use 2x cwmin.
156 qi.tqi_aifs = wmep->wmep_aifsn;
157 qi.tqi_cwmin = 2*ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmin);
158 qi.tqi_cwmax = ATH_EXPONENT_TO_VALUE(wmep->wmep_logcwmax);
161 if (!ath_hal_settxqueueprops(ah, sc->sc_bhalq, &qi)) {
162 device_printf(sc->sc_dev, "unable to update parameters for "
163 "beacon hardware queue!\n");
166 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
169 #undef ATH_EXPONENT_TO_VALUE
173 * Allocate and setup an initial beacon frame.
176 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
178 struct ieee80211vap *vap = ni->ni_vap;
179 struct ath_vap *avp = ATH_VAP(vap);
185 DPRINTF(sc, ATH_DEBUG_NODE, "%s: bf_m=%p, bf_node=%p\n",
186 __func__, bf->bf_m, bf->bf_node);
187 if (bf->bf_m != NULL) {
188 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
192 if (bf->bf_node != NULL) {
193 ieee80211_free_node(bf->bf_node);
198 * NB: the beacon data buffer must be 32-bit aligned;
199 * we assume the mbuf routines will return us something
200 * with this alignment (perhaps should assert).
202 m = ieee80211_beacon_alloc(ni, &avp->av_boff);
204 device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
205 sc->sc_stats.ast_be_nombuf++;
208 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
209 bf->bf_segs, &bf->bf_nseg,
212 device_printf(sc->sc_dev,
213 "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n",
220 * Calculate a TSF adjustment factor required for staggered
221 * beacons. Note that we assume the format of the beacon
222 * frame leaves the tstamp field immediately following the
225 if (sc->sc_stagbeacons && avp->av_bslot > 0) {
227 struct ieee80211_frame *wh;
230 * The beacon interval is in TU's; the TSF is in usecs.
231 * We figure out how many TU's to add to align the timestamp
232 * then convert to TSF units and handle byte swapping before
233 * inserting it in the frame. The hardware will then add this
234 * each time a beacon frame is sent. Note that we align vap's
235 * 1..N and leave vap 0 untouched. This means vap 0 has a
236 * timestamp in one beacon interval while the others get a
237 * timstamp aligned to the next interval.
239 tsfadjust = ni->ni_intval *
240 (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
241 tsfadjust = htole64(tsfadjust << 10); /* TU -> TSF */
243 DPRINTF(sc, ATH_DEBUG_BEACON,
244 "%s: %s beacons bslot %d intval %u tsfadjust %llu\n",
245 __func__, sc->sc_stagbeacons ? "stagger" : "burst",
246 avp->av_bslot, ni->ni_intval,
247 (long long unsigned) le64toh(tsfadjust));
249 wh = mtod(m, struct ieee80211_frame *);
250 memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
253 bf->bf_node = ieee80211_ref_node(ni);
259 * Setup the beacon frame for transmit.
262 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
264 #define USE_SHPREAMBLE(_ic) \
265 (((_ic)->ic_flags & (IEEE80211_F_SHPREAMBLE | IEEE80211_F_USEBARKER))\
266 == IEEE80211_F_SHPREAMBLE)
267 struct ieee80211_node *ni = bf->bf_node;
268 struct ieee80211com *ic = ni->ni_ic;
269 struct mbuf *m = bf->bf_m;
270 struct ath_hal *ah = sc->sc_ah;
273 const HAL_RATE_TABLE *rt;
275 HAL_DMA_ADDR bufAddrList[4];
276 uint32_t segLenList[4];
277 HAL_11N_RATE_SERIES rc[4];
279 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
280 __func__, m, m->m_len);
282 /* setup descriptors */
287 flags = HAL_TXDESC_NOACK;
288 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol) {
289 /* self-linked descriptor */
290 ath_hal_settxdesclink(sc->sc_ah, ds, bf->bf_daddr);
291 flags |= HAL_TXDESC_VEOL;
293 * Let hardware handle antenna switching.
295 antenna = sc->sc_txantenna;
297 ath_hal_settxdesclink(sc->sc_ah, ds, 0);
299 * Switch antenna every 4 beacons.
300 * XXX assumes two antenna
302 if (sc->sc_txantenna != 0)
303 antenna = sc->sc_txantenna;
304 else if (sc->sc_stagbeacons && sc->sc_nbcnvaps != 0)
305 antenna = ((sc->sc_stats.ast_be_xmit / sc->sc_nbcnvaps) & 4 ? 2 : 1);
307 antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
310 KASSERT(bf->bf_nseg == 1,
311 ("multi-segment beacon frame; nseg %u", bf->bf_nseg));
314 * Calculate rate code.
315 * XXX everything at min xmit rate
318 rt = sc->sc_currates;
319 rate = rt->info[rix].rateCode;
320 if (USE_SHPREAMBLE(ic))
321 rate |= rt->info[rix].shortPreamble;
322 ath_hal_setuptxdesc(ah, ds
323 , m->m_len + IEEE80211_CRC_LEN /* frame length */
324 , sizeof(struct ieee80211_frame)/* header length */
325 , HAL_PKT_TYPE_BEACON /* Atheros packet type */
326 , ieee80211_get_node_txpower(ni) /* txpower XXX */
327 , rate, 1 /* series 0 rate/tries */
328 , HAL_TXKEYIX_INVALID /* no encryption */
329 , antenna /* antenna mode */
330 , flags /* no ack, veol for beacons */
331 , 0 /* rts/cts rate */
332 , 0 /* rts/cts duration */
336 * The EDMA HAL currently assumes that _all_ rate control
337 * settings are done in ath_hal_set11nratescenario(), rather
338 * than in ath_hal_setuptxdesc().
341 memset(&rc, 0, sizeof(rc));
343 rc[0].ChSel = sc->sc_txchainmask;
345 rc[0].Rate = rt->info[rix].rateCode;
346 rc[0].RateIndex = rix;
347 rc[0].tx_power_cap = 0x3f;
349 ath_hal_computetxtime(ah, rt, roundup(m->m_len, 4),
351 ath_hal_set11nratescenario(ah, ds, 0, 0, rc, 4, flags);
354 /* NB: beacon's BufLen must be a multiple of 4 bytes */
355 segLenList[0] = roundup(m->m_len, 4);
356 segLenList[1] = segLenList[2] = segLenList[3] = 0;
357 bufAddrList[0] = bf->bf_segs[0].ds_addr;
358 bufAddrList[1] = bufAddrList[2] = bufAddrList[3] = 0;
359 ath_hal_filltxdesc(ah, ds
362 , 0 /* XXX desc id */
363 , sc->sc_bhalq /* hardware TXQ */
364 , AH_TRUE /* first segment */
365 , AH_TRUE /* last segment */
366 , ds /* first descriptor */
371 #undef USE_SHPREAMBLE
375 ath_beacon_update(struct ieee80211vap *vap, int item)
377 struct ieee80211_beacon_offsets *bo = &ATH_VAP(vap)->av_boff;
379 setbit(bo->bo_flags, item);
383 * Handle a beacon miss.
386 ath_beacon_miss(struct ath_softc *sc)
388 HAL_SURVEY_SAMPLE hs;
392 bzero(&hs, sizeof(hs));
394 ret = ath_hal_get_mib_cycle_counts(sc->sc_ah, &hs);
396 if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) && hangs != 0) {
397 DPRINTF(sc, ATH_DEBUG_BEACON,
404 if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_MISSED_BEACON))
405 if_ath_alq_post(&sc->sc_alq, ATH_ALQ_MISSED_BEACON, 0, NULL);
408 DPRINTF(sc, ATH_DEBUG_BEACON,
409 "%s: valid=%d, txbusy=%u, rxbusy=%u, chanbusy=%u, "
410 "extchanbusy=%u, cyclecount=%u\n",
421 * Transmit a beacon frame at SWBA. Dynamic updates to the
422 * frame contents are done as needed and the slot time is
423 * also adjusted based on current state.
426 ath_beacon_proc(void *arg, int pending)
428 struct ath_softc *sc = arg;
429 struct ath_hal *ah = sc->sc_ah;
430 struct ieee80211vap *vap;
435 DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
438 * Check if the previous beacon has gone out. If
439 * not don't try to post another, skip this period
440 * and wait for the next. Missed beacons indicate
441 * a problem and should not occur. If we miss too
442 * many consecutive beacons reset the device.
444 if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
446 sc->sc_stats.ast_be_missed++;
448 DPRINTF(sc, ATH_DEBUG_BEACON,
449 "%s: missed %u consecutive beacons\n",
450 __func__, sc->sc_bmisscount);
451 if (sc->sc_bmisscount >= ath_bstuck_threshold)
452 taskqueue_enqueue(sc->sc_tq, &sc->sc_bstucktask);
455 if (sc->sc_bmisscount != 0) {
456 DPRINTF(sc, ATH_DEBUG_BEACON,
457 "%s: resume beacon xmit after %u misses\n",
458 __func__, sc->sc_bmisscount);
459 sc->sc_bmisscount = 0;
461 if (if_ath_alq_checkdebug(&sc->sc_alq, ATH_ALQ_RESUME_BEACON))
462 if_ath_alq_post(&sc->sc_alq, ATH_ALQ_RESUME_BEACON, 0, NULL);
466 if (sc->sc_stagbeacons) { /* staggered beacons */
467 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
470 tsftu = ath_hal_gettsf32(ah) >> 10;
472 slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
473 vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
475 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
476 bf = ath_beacon_generate(sc, vap);
478 bfaddr = bf->bf_daddr;
480 } else { /* burst'd beacons */
481 uint32_t *bflink = &bfaddr;
483 for (slot = 0; slot < ATH_BCBUF; slot++) {
484 vap = sc->sc_bslot[slot];
485 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
486 bf = ath_beacon_generate(sc, vap);
488 * XXX TODO: this should use settxdesclinkptr()
489 * otherwise it won't work for EDMA chipsets!
492 /* XXX should do this using the ds */
493 *bflink = bf->bf_daddr;
494 ath_hal_gettxdesclinkptr(sc->sc_ah,
495 bf->bf_desc, &bflink);
500 * XXX TODO: this should use settxdesclinkptr()
501 * otherwise it won't work for EDMA chipsets!
503 *bflink = 0; /* terminate list */
507 * Handle slot time change when a non-ERP station joins/leaves
508 * an 11g network. The 802.11 layer notifies us via callback,
509 * we mark updateslot, then wait one beacon before effecting
510 * the change. This gives associated stations at least one
511 * beacon interval to note the state change.
514 if (sc->sc_updateslot == UPDATE) {
515 sc->sc_updateslot = COMMIT; /* commit next beacon */
516 sc->sc_slotupdate = slot;
517 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
518 ath_setslottime(sc); /* commit change to h/w */
521 * Check recent per-antenna transmit statistics and flip
522 * the default antenna if noticeably more frames went out
523 * on the non-default antenna.
524 * XXX assumes 2 anntenae
526 if (!sc->sc_diversity && (!sc->sc_stagbeacons || slot == 0)) {
527 otherant = sc->sc_defant & 1 ? 2 : 1;
528 if (sc->sc_ant_tx[otherant] > sc->sc_ant_tx[sc->sc_defant] + 2)
529 ath_setdefantenna(sc, otherant);
530 sc->sc_ant_tx[1] = sc->sc_ant_tx[2] = 0;
533 /* Program the CABQ with the contents of the CABQ txq and start it */
534 ATH_TXQ_LOCK(sc->sc_cabq);
535 ath_beacon_cabq_start(sc);
536 ATH_TXQ_UNLOCK(sc->sc_cabq);
538 /* Program the new beacon frame if we have one for this interval */
541 * Stop any current dma and put the new frame on the queue.
542 * This should never fail since we check above that no frames
543 * are still pending on the queue.
545 if (! sc->sc_isedma) {
546 if (!ath_hal_stoptxdma(ah, sc->sc_bhalq)) {
547 DPRINTF(sc, ATH_DEBUG_ANY,
548 "%s: beacon queue %u did not stop?\n",
549 __func__, sc->sc_bhalq);
552 /* NB: cabq traffic should already be queued and primed */
554 ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
555 ath_hal_txstart(ah, sc->sc_bhalq);
557 sc->sc_stats.ast_be_xmit++;
562 ath_beacon_cabq_start_edma(struct ath_softc *sc)
564 struct ath_buf *bf, *bf_last;
565 struct ath_txq *cabq = sc->sc_cabq;
571 ATH_TXQ_LOCK_ASSERT(cabq);
573 if (TAILQ_EMPTY(&cabq->axq_q))
575 bf = TAILQ_FIRST(&cabq->axq_q);
576 bf_last = TAILQ_LAST(&cabq->axq_q, axq_q_s);
579 * This is a dirty, dirty hack to push the contents of
580 * the cabq staging queue into the FIFO.
582 * This ideally should live in the EDMA code file
583 * and only push things into the CABQ if there's a FIFO
586 * We can't treat this like a normal TX queue because
587 * in the case of multi-VAP traffic, we may have to flush
588 * the CABQ each new (staggered) beacon that goes out.
589 * But for non-staggered beacons, we could in theory
590 * handle multicast traffic for all VAPs in one FIFO
591 * push. Just keep all of this in mind if you're wondering
592 * how to correctly/better handle multi-VAP CABQ traffic
597 * Is the CABQ FIFO free? If not, complain loudly and
598 * don't queue anything. Maybe we'll flush the CABQ
599 * traffic, maybe we won't. But that'll happen next
602 if (cabq->axq_fifo_depth >= HAL_TXFIFO_DEPTH) {
603 device_printf(sc->sc_dev,
604 "%s: Q%d: CAB FIFO queue=%d?\n",
607 cabq->axq_fifo_depth);
612 * Ok, so here's the gymnastics reqiured to make this
617 * Tag the first/last buffer appropriately.
619 bf->bf_flags |= ATH_BUF_FIFOPTR;
620 bf_last->bf_flags |= ATH_BUF_FIFOEND;
624 TAILQ_FOREACH(bfi, &cabq->axq_q, bf_list) {
625 ath_printtxbuf(sc, bf, cabq->axq_qnum, i, 0);
631 * We now need to push this set of frames onto the tail
632 * of the FIFO queue. We don't adjust the aggregate
633 * count, only the queue depth counter(s).
634 * We also need to blank the link pointer now.
636 TAILQ_CONCAT(&cabq->fifo.axq_q, &cabq->axq_q, bf_list);
637 cabq->axq_link = NULL;
638 cabq->fifo.axq_depth += cabq->axq_depth;
641 /* Bump FIFO queue */
642 cabq->axq_fifo_depth++;
644 /* Push the first entry into the hardware */
645 ath_hal_puttxbuf(sc->sc_ah, cabq->axq_qnum, bf->bf_daddr);
646 cabq->axq_flags |= ATH_TXQ_PUTRUNNING;
648 /* NB: gated by beacon so safe to start here */
649 ath_hal_txstart(sc->sc_ah, cabq->axq_qnum);
654 ath_beacon_cabq_start_legacy(struct ath_softc *sc)
657 struct ath_txq *cabq = sc->sc_cabq;
659 ATH_TXQ_LOCK_ASSERT(cabq);
660 if (TAILQ_EMPTY(&cabq->axq_q))
662 bf = TAILQ_FIRST(&cabq->axq_q);
664 /* Push the first entry into the hardware */
665 ath_hal_puttxbuf(sc->sc_ah, cabq->axq_qnum, bf->bf_daddr);
666 cabq->axq_flags |= ATH_TXQ_PUTRUNNING;
668 /* NB: gated by beacon so safe to start here */
669 ath_hal_txstart(sc->sc_ah, cabq->axq_qnum);
673 * Start CABQ transmission - this assumes that all frames are prepped
674 * and ready in the CABQ.
677 ath_beacon_cabq_start(struct ath_softc *sc)
679 struct ath_txq *cabq = sc->sc_cabq;
681 ATH_TXQ_LOCK_ASSERT(cabq);
683 if (TAILQ_EMPTY(&cabq->axq_q))
687 ath_beacon_cabq_start_edma(sc);
689 ath_beacon_cabq_start_legacy(sc);
693 ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
695 struct ath_vap *avp = ATH_VAP(vap);
696 struct ath_txq *cabq = sc->sc_cabq;
701 KASSERT(vap->iv_state >= IEEE80211_S_RUN,
702 ("not running, state %d", vap->iv_state));
703 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
706 * Update dynamic beacon contents. If this returns
707 * non-zero then we need to remap the memory because
708 * the beacon frame changed size (probably because
709 * of the TIM bitmap).
713 /* XXX lock mcastq? */
714 nmcastq = avp->av_mcastq.axq_depth;
716 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, nmcastq)) {
717 /* XXX too conservative? */
718 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
719 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
720 bf->bf_segs, &bf->bf_nseg,
723 if_printf(vap->iv_ifp,
724 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
729 if ((avp->av_boff.bo_tim[4] & 1) && cabq->axq_depth) {
730 DPRINTF(sc, ATH_DEBUG_BEACON,
731 "%s: cabq did not drain, mcastq %u cabq %u\n",
732 __func__, nmcastq, cabq->axq_depth);
733 sc->sc_stats.ast_cabq_busy++;
734 if (sc->sc_nvaps > 1 && sc->sc_stagbeacons) {
736 * CABQ traffic from a previous vap is still pending.
737 * We must drain the q before this beacon frame goes
738 * out as otherwise this vap's stations will get cab
739 * frames from a different vap.
740 * XXX could be slow causing us to miss DBA
743 * XXX TODO: this doesn't stop CABQ DMA - it assumes
744 * that since we're about to transmit a beacon, we've
745 * already stopped transmitting on the CABQ. But this
746 * doesn't at all mean that the CABQ DMA QCU will
747 * accept a new TXDP! So what, should we do a DMA
748 * stop? What if it fails?
750 * More thought is required here.
753 * XXX can we even stop TX DMA here? Check what the
754 * reference driver does for cabq for beacons, given
755 * that stopping TX requires RX is paused.
757 ath_tx_draintxq(sc, cabq);
760 ath_beacon_setup(sc, bf);
761 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
764 * Enable the CAB queue before the beacon queue to
765 * insure cab frames are triggered by this beacon.
767 if (avp->av_boff.bo_tim[4] & 1) {
769 /* NB: only at DTIM */
770 ATH_TXQ_LOCK(&avp->av_mcastq);
772 struct ath_buf *bfm, *bfc_last;
775 * Move frames from the s/w mcast q to the h/w cab q.
777 * XXX TODO: if we chain together multiple VAPs
778 * worth of CABQ traffic, should we keep the
779 * MORE data bit set on the last frame of each
780 * intermediary VAP (ie, only clear the MORE
781 * bit of the last frame on the last vap?)
783 bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
787 * If there's already a frame on the CABQ, we
788 * need to link to the end of the last frame.
789 * We can't use axq_link here because
790 * EDMA descriptors require some recalculation
791 * (checksum) to occur.
793 bfc_last = ATH_TXQ_LAST(cabq, axq_q_s);
794 if (bfc_last != NULL) {
795 ath_hal_settxdesclink(sc->sc_ah,
799 ath_txqmove(cabq, &avp->av_mcastq);
800 ATH_TXQ_UNLOCK(cabq);
802 * XXX not entirely accurate, in case a mcast
803 * queue frame arrived before we grabbed the TX
806 sc->sc_stats.ast_cabq_xmit += nmcastq;
808 ATH_TXQ_UNLOCK(&avp->av_mcastq);
814 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
816 struct ath_vap *avp = ATH_VAP(vap);
817 struct ath_hal *ah = sc->sc_ah;
822 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
825 * Update dynamic beacon contents. If this returns
826 * non-zero then we need to remap the memory because
827 * the beacon frame changed size (probably because
828 * of the TIM bitmap).
832 if (ieee80211_beacon_update(bf->bf_node, &avp->av_boff, m, 0)) {
833 /* XXX too conservative? */
834 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
835 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
836 bf->bf_segs, &bf->bf_nseg,
839 if_printf(vap->iv_ifp,
840 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
845 ath_beacon_setup(sc, bf);
846 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
848 /* NB: caller is known to have already stopped tx dma */
849 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
850 ath_hal_txstart(ah, sc->sc_bhalq);
854 * Reclaim beacon resources and return buffer to the pool.
857 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
860 DPRINTF(sc, ATH_DEBUG_NODE, "%s: free bf=%p, bf_m=%p, bf_node=%p\n",
861 __func__, bf, bf->bf_m, bf->bf_node);
862 if (bf->bf_m != NULL) {
863 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
867 if (bf->bf_node != NULL) {
868 ieee80211_free_node(bf->bf_node);
871 TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
875 * Reclaim beacon resources.
878 ath_beacon_free(struct ath_softc *sc)
882 TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
883 DPRINTF(sc, ATH_DEBUG_NODE,
884 "%s: free bf=%p, bf_m=%p, bf_node=%p\n",
885 __func__, bf, bf->bf_m, bf->bf_node);
886 if (bf->bf_m != NULL) {
887 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
891 if (bf->bf_node != NULL) {
892 ieee80211_free_node(bf->bf_node);
899 * Configure the beacon and sleep timers.
901 * When operating as an AP this resets the TSF and sets
902 * up the hardware to notify us when we need to issue beacons.
904 * When operating in station mode this sets up the beacon
905 * timers according to the timestamp of the last received
906 * beacon and the current TSF, configures PCF and DTIM
907 * handling, programs the sleep registers so the hardware
908 * will wakeup in time to receive beacons, and configures
909 * the beacon miss handling so we'll receive a BMISS
910 * interrupt when we stop seeing beacons from the AP
911 * we've associated with.
914 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
916 #define TSF_TO_TU(_h,_l) \
917 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
919 struct ath_hal *ah = sc->sc_ah;
920 struct ieee80211com *ic = sc->sc_ifp->if_l2com;
921 struct ieee80211_node *ni;
922 u_int32_t nexttbtt, intval, tsftu;
923 u_int32_t nexttbtt_u8, intval_u8;
924 u_int64_t tsf, tsf_beacon;
927 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */
929 * Just ensure that we aren't being called when the last
933 device_printf(sc->sc_dev, "%s: called with no VAPs\n",
938 ni = ieee80211_ref_node(vap->iv_bss);
941 ath_power_set_power_state(sc, HAL_PM_AWAKE);
944 /* extract tstamp from last beacon and convert to TU */
945 nexttbtt = TSF_TO_TU(LE_READ_4(ni->ni_tstamp.data + 4),
946 LE_READ_4(ni->ni_tstamp.data));
948 tsf_beacon = ((uint64_t) LE_READ_4(ni->ni_tstamp.data + 4)) << 32;
949 tsf_beacon |= LE_READ_4(ni->ni_tstamp.data);
951 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
952 ic->ic_opmode == IEEE80211_M_MBSS) {
954 * For multi-bss ap/mesh support beacons are either staggered
955 * evenly over N slots or burst together. For the former
956 * arrange for the SWBA to be delivered for each slot.
957 * Slots that are not occupied will generate nothing.
959 /* NB: the beacon interval is kept internally in TU's */
960 intval = ni->ni_intval & HAL_BEACON_PERIOD;
961 if (sc->sc_stagbeacons)
964 /* NB: the beacon interval is kept internally in TU's */
965 intval = ni->ni_intval & HAL_BEACON_PERIOD;
967 if (nexttbtt == 0) /* e.g. for ap mode */
969 else if (intval) /* NB: can be 0 for monitor mode */
970 nexttbtt = roundup(nexttbtt, intval);
971 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
972 __func__, nexttbtt, intval, ni->ni_intval);
973 if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
975 int dtimperiod, dtimcount;
976 int cfpperiod, cfpcount;
979 * Setup dtim and cfp parameters according to
980 * last beacon we received (which may be none).
982 dtimperiod = ni->ni_dtim_period;
983 if (dtimperiod <= 0) /* NB: 0 if not known */
985 dtimcount = ni->ni_dtim_count;
986 if (dtimcount >= dtimperiod) /* NB: sanity check */
987 dtimcount = 0; /* XXX? */
988 cfpperiod = 1; /* NB: no PCF support yet */
991 * Pull nexttbtt forward to reflect the current
992 * TSF and calculate dtim+cfp state for the result.
994 tsf = ath_hal_gettsf64(ah);
995 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
997 DPRINTF(sc, ATH_DEBUG_BEACON,
998 "%s: beacon tsf=%llu, hw tsf=%llu, nexttbtt=%u, tsftu=%u\n",
1000 (unsigned long long) tsf_beacon,
1001 (unsigned long long) tsf,
1004 DPRINTF(sc, ATH_DEBUG_BEACON,
1005 "%s: beacon tsf=%llu, hw tsf=%llu, tsf delta=%lld\n",
1007 (unsigned long long) tsf_beacon,
1008 (unsigned long long) tsf,
1010 (long long) tsf_beacon);
1012 DPRINTF(sc, ATH_DEBUG_BEACON,
1013 "%s: nexttbtt=%llu, beacon tsf delta=%lld\n",
1015 (unsigned long long) nexttbtt,
1016 (long long) ((long long) nexttbtt * 1024LL) - (long long) tsf_beacon);
1020 if (nexttbtt > tsftu) {
1021 uint32_t countdiff, oldtbtt, remainder;
1024 remainder = (nexttbtt - tsftu) % intval;
1025 nexttbtt = tsftu + remainder;
1027 countdiff = (oldtbtt - nexttbtt) / intval % dtimperiod;
1028 if (dtimcount > countdiff) {
1029 dtimcount -= countdiff;
1031 dtimcount += dtimperiod - countdiff;
1033 } else { //nexttbtt <= tsftu
1034 uint32_t countdiff, oldtbtt, remainder;
1037 remainder = (tsftu - nexttbtt) % intval;
1038 nexttbtt = tsftu - remainder + intval;
1039 countdiff = (nexttbtt - oldtbtt) / intval % dtimperiod;
1040 if (dtimcount > countdiff) {
1041 dtimcount -= countdiff;
1043 dtimcount += dtimperiod - countdiff;
1047 DPRINTF(sc, ATH_DEBUG_BEACON,
1048 "%s: adj nexttbtt=%llu, rx tsf delta=%lld\n",
1050 (unsigned long long) nexttbtt,
1051 (long long) ((long long)nexttbtt * 1024LL) - (long long)tsf);
1053 memset(&bs, 0, sizeof(bs));
1054 bs.bs_intval = intval;
1055 bs.bs_nexttbtt = nexttbtt;
1056 bs.bs_dtimperiod = dtimperiod*intval;
1057 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
1058 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
1059 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
1060 bs.bs_cfpmaxduration = 0;
1063 * The 802.11 layer records the offset to the DTIM
1064 * bitmap while receiving beacons; use it here to
1065 * enable h/w detection of our AID being marked in
1066 * the bitmap vector (to indicate frames for us are
1067 * pending at the AP).
1068 * XXX do DTIM handling in s/w to WAR old h/w bugs
1069 * XXX enable based on h/w rev for newer chips
1071 bs.bs_timoffset = ni->ni_timoff;
1074 * Calculate the number of consecutive beacons to miss
1075 * before taking a BMISS interrupt.
1076 * Note that we clamp the result to at most 10 beacons.
1078 bs.bs_bmissthreshold = vap->iv_bmissthreshold;
1079 if (bs.bs_bmissthreshold > 10)
1080 bs.bs_bmissthreshold = 10;
1081 else if (bs.bs_bmissthreshold <= 0)
1082 bs.bs_bmissthreshold = 1;
1085 * Calculate sleep duration. The configuration is
1086 * given in ms. We insure a multiple of the beacon
1087 * period is used. Also, if the sleep duration is
1088 * greater than the DTIM period then it makes senses
1089 * to make it a multiple of that.
1091 * XXX fixed at 100ms
1093 bs.bs_sleepduration =
1094 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
1095 if (bs.bs_sleepduration > bs.bs_dtimperiod)
1096 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
1098 DPRINTF(sc, ATH_DEBUG_BEACON,
1099 "%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u "
1100 "nextdtim %u bmiss %u sleep %u cfp:period %u "
1101 "maxdur %u next %u timoffset %u\n"
1109 , bs.bs_bmissthreshold
1110 , bs.bs_sleepduration
1112 , bs.bs_cfpmaxduration
1116 ath_hal_intrset(ah, 0);
1117 ath_hal_beacontimers(ah, &bs);
1118 sc->sc_imask |= HAL_INT_BMISS;
1119 ath_hal_intrset(ah, sc->sc_imask);
1121 ath_hal_intrset(ah, 0);
1122 if (nexttbtt == intval)
1123 intval |= HAL_BEACON_RESET_TSF;
1124 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1126 * In IBSS mode enable the beacon timers but only
1127 * enable SWBA interrupts if we need to manually
1128 * prepare beacon frames. Otherwise we use a
1129 * self-linked tx descriptor and let the hardware
1132 intval |= HAL_BEACON_ENA;
1133 if (!sc->sc_hasveol)
1134 sc->sc_imask |= HAL_INT_SWBA;
1135 if ((intval & HAL_BEACON_RESET_TSF) == 0) {
1137 * Pull nexttbtt forward to reflect
1140 tsf = ath_hal_gettsf64(ah);
1141 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
1144 } while (nexttbtt < tsftu);
1146 ath_beaconq_config(sc);
1147 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1148 ic->ic_opmode == IEEE80211_M_MBSS) {
1150 * In AP/mesh mode we enable the beacon timers
1151 * and SWBA interrupts to prepare beacon frames.
1153 intval |= HAL_BEACON_ENA;
1154 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */
1155 ath_beaconq_config(sc);
1159 * Now dirty things because for now, the EDMA HAL has
1160 * nexttbtt and intval is TU/8.
1162 if (sc->sc_isedma) {
1163 nexttbtt_u8 = (nexttbtt << 3);
1164 intval_u8 = (intval << 3);
1165 if (intval & HAL_BEACON_ENA)
1166 intval_u8 |= HAL_BEACON_ENA;
1167 if (intval & HAL_BEACON_RESET_TSF)
1168 intval_u8 |= HAL_BEACON_RESET_TSF;
1169 ath_hal_beaconinit(ah, nexttbtt_u8, intval_u8);
1171 ath_hal_beaconinit(ah, nexttbtt, intval);
1172 sc->sc_bmisscount = 0;
1173 ath_hal_intrset(ah, sc->sc_imask);
1175 * When using a self-linked beacon descriptor in
1176 * ibss mode load it once here.
1178 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
1179 ath_beacon_start_adhoc(sc, vap);
1181 ieee80211_free_node(ni);
1184 ath_power_restore_power_state(sc);