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_ic;
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);
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 = &vap->iv_bcn_off;
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_ic;
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, 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 ((vap->iv_bcn_off.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 * XXX TODO: tie into net80211 for quiet time IE update and program
765 * local AP timer if we require it. The process of updating the
766 * beacon will also update the IE with the relevant counters.
770 * Enable the CAB queue before the beacon queue to
771 * insure cab frames are triggered by this beacon.
773 if (vap->iv_bcn_off.bo_tim[4] & 1) {
775 /* NB: only at DTIM */
776 ATH_TXQ_LOCK(&avp->av_mcastq);
778 struct ath_buf *bfm, *bfc_last;
781 * Move frames from the s/w mcast q to the h/w cab q.
783 * XXX TODO: if we chain together multiple VAPs
784 * worth of CABQ traffic, should we keep the
785 * MORE data bit set on the last frame of each
786 * intermediary VAP (ie, only clear the MORE
787 * bit of the last frame on the last vap?)
789 bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
793 * If there's already a frame on the CABQ, we
794 * need to link to the end of the last frame.
795 * We can't use axq_link here because
796 * EDMA descriptors require some recalculation
797 * (checksum) to occur.
799 bfc_last = ATH_TXQ_LAST(cabq, axq_q_s);
800 if (bfc_last != NULL) {
801 ath_hal_settxdesclink(sc->sc_ah,
805 ath_txqmove(cabq, &avp->av_mcastq);
806 ATH_TXQ_UNLOCK(cabq);
808 * XXX not entirely accurate, in case a mcast
809 * queue frame arrived before we grabbed the TX
812 sc->sc_stats.ast_cabq_xmit += nmcastq;
814 ATH_TXQ_UNLOCK(&avp->av_mcastq);
820 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
822 struct ath_vap *avp = ATH_VAP(vap);
823 struct ath_hal *ah = sc->sc_ah;
828 KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
831 * Update dynamic beacon contents. If this returns
832 * non-zero then we need to remap the memory because
833 * the beacon frame changed size (probably because
834 * of the TIM bitmap).
838 if (ieee80211_beacon_update(bf->bf_node, m, 0)) {
839 /* XXX too conservative? */
840 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
841 error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
842 bf->bf_segs, &bf->bf_nseg,
845 if_printf(vap->iv_ifp,
846 "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
851 ath_beacon_setup(sc, bf);
852 bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
854 /* NB: caller is known to have already stopped tx dma */
855 ath_hal_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
856 ath_hal_txstart(ah, sc->sc_bhalq);
860 * Reclaim beacon resources and return buffer to the pool.
863 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
866 DPRINTF(sc, ATH_DEBUG_NODE, "%s: free bf=%p, bf_m=%p, bf_node=%p\n",
867 __func__, bf, bf->bf_m, bf->bf_node);
868 if (bf->bf_m != NULL) {
869 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
873 if (bf->bf_node != NULL) {
874 ieee80211_free_node(bf->bf_node);
877 TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
881 * Reclaim beacon resources.
884 ath_beacon_free(struct ath_softc *sc)
888 TAILQ_FOREACH(bf, &sc->sc_bbuf, bf_list) {
889 DPRINTF(sc, ATH_DEBUG_NODE,
890 "%s: free bf=%p, bf_m=%p, bf_node=%p\n",
891 __func__, bf, bf->bf_m, bf->bf_node);
892 if (bf->bf_m != NULL) {
893 bus_dmamap_unload(sc->sc_dmat, bf->bf_dmamap);
897 if (bf->bf_node != NULL) {
898 ieee80211_free_node(bf->bf_node);
905 * Configure the beacon and sleep timers.
907 * When operating as an AP this resets the TSF and sets
908 * up the hardware to notify us when we need to issue beacons.
910 * When operating in station mode this sets up the beacon
911 * timers according to the timestamp of the last received
912 * beacon and the current TSF, configures PCF and DTIM
913 * handling, programs the sleep registers so the hardware
914 * will wakeup in time to receive beacons, and configures
915 * the beacon miss handling so we'll receive a BMISS
916 * interrupt when we stop seeing beacons from the AP
917 * we've associated with.
920 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
922 #define TSF_TO_TU(_h,_l) \
923 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
925 struct ath_hal *ah = sc->sc_ah;
927 struct ieee80211com *ic = &sc->sc_ic;
928 struct ieee80211_node *ni;
929 u_int32_t nexttbtt, intval, tsftu;
930 u_int32_t nexttbtt_u8, intval_u8;
931 u_int64_t tsf, tsf_beacon;
934 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */
936 * Just ensure that we aren't being called when the last
940 device_printf(sc->sc_dev, "%s: called with no VAPs\n",
945 /* Now that we have a vap, we can do this bit */
948 ni = ieee80211_ref_node(vap->iv_bss);
951 ath_power_set_power_state(sc, HAL_PM_AWAKE);
954 /* Always clear the quiet IE timers; let the next update program them */
955 ath_hal_set_quiet(ah, 0, 0, 0, HAL_QUIET_DISABLE);
956 memset(&avp->quiet_ie, 0, sizeof(avp->quiet_ie));
958 /* extract tstamp from last beacon and convert to TU */
959 nexttbtt = TSF_TO_TU(le32dec(ni->ni_tstamp.data + 4),
960 le32dec(ni->ni_tstamp.data));
962 tsf_beacon = ((uint64_t) le32dec(ni->ni_tstamp.data + 4)) << 32;
963 tsf_beacon |= le32dec(ni->ni_tstamp.data);
965 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
966 ic->ic_opmode == IEEE80211_M_MBSS) {
968 * For multi-bss ap/mesh support beacons are either staggered
969 * evenly over N slots or burst together. For the former
970 * arrange for the SWBA to be delivered for each slot.
971 * Slots that are not occupied will generate nothing.
973 /* NB: the beacon interval is kept internally in TU's */
974 intval = ni->ni_intval & HAL_BEACON_PERIOD;
975 if (sc->sc_stagbeacons)
978 /* NB: the beacon interval is kept internally in TU's */
979 intval = ni->ni_intval & HAL_BEACON_PERIOD;
983 * Note: rounding up to the next intval can cause problems with
984 * bad APs when we're in powersave mode.
986 * In STA mode with powersave enabled, beacons are only received
987 * whenever the beacon timer fires to wake up the hardware.
988 * Now, if this is rounded up to the next intval, it assumes
989 * that the AP has started transmitting beacons at TSF values that
990 * are multiples of intval, versus say being 25 TU off.
992 * The specification (802.11-2012 10.1.3.2 - Beacon Generation in
993 * Infrastructure Networks) requires APs be beaconing at a
994 * mutiple of intval. So, if bintval=100, then we shouldn't
995 * get beacons at intervals other than around multiples of 100.
997 if (nexttbtt == 0) /* e.g. for ap mode */
1000 nexttbtt = roundup(nexttbtt, intval);
1002 DPRINTF(sc, ATH_DEBUG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
1003 __func__, nexttbtt, intval, ni->ni_intval);
1004 if (ic->ic_opmode == IEEE80211_M_STA && !sc->sc_swbmiss) {
1005 HAL_BEACON_STATE bs;
1006 int dtimperiod, dtimcount;
1007 int cfpperiod, cfpcount;
1010 * Setup dtim and cfp parameters according to
1011 * last beacon we received (which may be none).
1013 dtimperiod = ni->ni_dtim_period;
1014 if (dtimperiod <= 0) /* NB: 0 if not known */
1016 dtimcount = ni->ni_dtim_count;
1017 if (dtimcount >= dtimperiod) /* NB: sanity check */
1018 dtimcount = 0; /* XXX? */
1019 cfpperiod = 1; /* NB: no PCF support yet */
1022 * Pull nexttbtt forward to reflect the current
1023 * TSF and calculate dtim+cfp state for the result.
1025 tsf = ath_hal_gettsf64(ah);
1026 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
1028 DPRINTF(sc, ATH_DEBUG_BEACON,
1029 "%s: beacon tsf=%llu, hw tsf=%llu, nexttbtt=%u, tsftu=%u\n",
1031 (unsigned long long) tsf_beacon,
1032 (unsigned long long) tsf,
1035 DPRINTF(sc, ATH_DEBUG_BEACON,
1036 "%s: beacon tsf=%llu, hw tsf=%llu, tsf delta=%lld\n",
1038 (unsigned long long) tsf_beacon,
1039 (unsigned long long) tsf,
1041 (long long) tsf_beacon);
1043 DPRINTF(sc, ATH_DEBUG_BEACON,
1044 "%s: nexttbtt=%llu, beacon tsf delta=%lld\n",
1046 (unsigned long long) nexttbtt,
1047 (long long) ((long long) nexttbtt * 1024LL) - (long long) tsf_beacon);
1051 if (nexttbtt > tsftu) {
1052 uint32_t countdiff, oldtbtt, remainder;
1055 remainder = (nexttbtt - tsftu) % intval;
1056 nexttbtt = tsftu + remainder;
1058 countdiff = (oldtbtt - nexttbtt) / intval % dtimperiod;
1059 if (dtimcount > countdiff) {
1060 dtimcount -= countdiff;
1062 dtimcount += dtimperiod - countdiff;
1064 } else { //nexttbtt <= tsftu
1065 uint32_t countdiff, oldtbtt, remainder;
1068 remainder = (tsftu - nexttbtt) % intval;
1069 nexttbtt = tsftu - remainder + intval;
1070 countdiff = (nexttbtt - oldtbtt) / intval % dtimperiod;
1071 if (dtimcount > countdiff) {
1072 dtimcount -= countdiff;
1074 dtimcount += dtimperiod - countdiff;
1078 DPRINTF(sc, ATH_DEBUG_BEACON,
1079 "%s: adj nexttbtt=%llu, rx tsf delta=%lld\n",
1081 (unsigned long long) nexttbtt,
1082 (long long) ((long long)nexttbtt * 1024LL) - (long long)tsf);
1084 memset(&bs, 0, sizeof(bs));
1085 bs.bs_intval = intval;
1086 bs.bs_nexttbtt = nexttbtt;
1087 bs.bs_dtimperiod = dtimperiod*intval;
1088 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
1089 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
1090 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
1091 bs.bs_cfpmaxduration = 0;
1094 * The 802.11 layer records the offset to the DTIM
1095 * bitmap while receiving beacons; use it here to
1096 * enable h/w detection of our AID being marked in
1097 * the bitmap vector (to indicate frames for us are
1098 * pending at the AP).
1099 * XXX do DTIM handling in s/w to WAR old h/w bugs
1100 * XXX enable based on h/w rev for newer chips
1102 bs.bs_timoffset = ni->ni_timoff;
1105 * Calculate the number of consecutive beacons to miss
1106 * before taking a BMISS interrupt.
1107 * Note that we clamp the result to at most 10 beacons.
1109 bs.bs_bmissthreshold = vap->iv_bmissthreshold;
1110 if (bs.bs_bmissthreshold > 10)
1111 bs.bs_bmissthreshold = 10;
1112 else if (bs.bs_bmissthreshold <= 0)
1113 bs.bs_bmissthreshold = 1;
1116 * Calculate sleep duration. The configuration is
1117 * given in ms. We insure a multiple of the beacon
1118 * period is used. Also, if the sleep duration is
1119 * greater than the DTIM period then it makes senses
1120 * to make it a multiple of that.
1122 * XXX fixed at 100ms
1124 bs.bs_sleepduration =
1125 roundup(IEEE80211_MS_TO_TU(100), bs.bs_intval);
1126 if (bs.bs_sleepduration > bs.bs_dtimperiod)
1127 bs.bs_sleepduration = roundup(bs.bs_sleepduration, bs.bs_dtimperiod);
1129 DPRINTF(sc, ATH_DEBUG_BEACON,
1130 "%s: tsf %ju tsf:tu %u intval %u nexttbtt %u dtim %u "
1131 "nextdtim %u bmiss %u sleep %u cfp:period %u "
1132 "maxdur %u next %u timoffset %u\n"
1140 , bs.bs_bmissthreshold
1141 , bs.bs_sleepduration
1143 , bs.bs_cfpmaxduration
1147 ath_hal_intrset(ah, 0);
1148 ath_hal_beacontimers(ah, &bs);
1149 sc->sc_imask |= HAL_INT_BMISS;
1150 ath_hal_intrset(ah, sc->sc_imask);
1152 ath_hal_intrset(ah, 0);
1153 if (nexttbtt == intval)
1154 intval |= HAL_BEACON_RESET_TSF;
1155 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1157 * In IBSS mode enable the beacon timers but only
1158 * enable SWBA interrupts if we need to manually
1159 * prepare beacon frames. Otherwise we use a
1160 * self-linked tx descriptor and let the hardware
1163 intval |= HAL_BEACON_ENA;
1164 if (!sc->sc_hasveol)
1165 sc->sc_imask |= HAL_INT_SWBA;
1166 if ((intval & HAL_BEACON_RESET_TSF) == 0) {
1168 * Pull nexttbtt forward to reflect
1171 tsf = ath_hal_gettsf64(ah);
1172 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
1175 } while (nexttbtt < tsftu);
1177 ath_beaconq_config(sc);
1178 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1179 ic->ic_opmode == IEEE80211_M_MBSS) {
1181 * In AP/mesh mode we enable the beacon timers
1182 * and SWBA interrupts to prepare beacon frames.
1184 intval |= HAL_BEACON_ENA;
1185 sc->sc_imask |= HAL_INT_SWBA; /* beacon prepare */
1186 ath_beaconq_config(sc);
1190 * Now dirty things because for now, the EDMA HAL has
1191 * nexttbtt and intval is TU/8.
1193 if (sc->sc_isedma) {
1194 nexttbtt_u8 = (nexttbtt << 3);
1195 intval_u8 = (intval << 3);
1196 if (intval & HAL_BEACON_ENA)
1197 intval_u8 |= HAL_BEACON_ENA;
1198 if (intval & HAL_BEACON_RESET_TSF)
1199 intval_u8 |= HAL_BEACON_RESET_TSF;
1200 ath_hal_beaconinit(ah, nexttbtt_u8, intval_u8);
1202 ath_hal_beaconinit(ah, nexttbtt, intval);
1203 sc->sc_bmisscount = 0;
1204 ath_hal_intrset(ah, sc->sc_imask);
1206 * When using a self-linked beacon descriptor in
1207 * ibss mode load it once here.
1209 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
1210 ath_beacon_start_adhoc(sc, vap);
1212 ieee80211_free_node(ni);
1215 ath_power_restore_power_state(sc);