]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ath/if_ath_beacon.c
MFV r322223: 8378 crash due to bp in-memory modification of nopwrite block
[FreeBSD/FreeBSD.git] / sys / dev / ath / if_ath_beacon.c
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_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>
83
84 #include <net80211/ieee80211_var.h>
85 #include <net80211/ieee80211_regdomain.h>
86 #ifdef IEEE80211_SUPPORT_SUPERG
87 #include <net80211/ieee80211_superg.h>
88 #endif
89
90 #include <net/bpf.h>
91
92 #ifdef INET
93 #include <netinet/in.h>
94 #include <netinet/if_ether.h>
95 #endif
96
97 #include <dev/ath/if_athvar.h>
98
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>
103
104 #ifdef ATH_TX99_DIAG
105 #include <dev/ath/ath_tx99/ath_tx99.h>
106 #endif
107
108 /*
109  * Setup a h/w transmit queue for beacons.
110  */
111 int
112 ath_beaconq_setup(struct ath_softc *sc)
113 {
114         struct ath_hal *ah = sc->sc_ah;
115         HAL_TXQ_INFO qi;
116
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;
123         if (sc->sc_isedma)
124                 qi.tqi_qflags |= HAL_TXQ_TXOKINT_ENABLE |
125                     HAL_TXQ_TXERRINT_ENABLE;
126
127         return ath_hal_setuptxqueue(ah, HAL_TX_QUEUE_BEACON, &qi);
128 }
129
130 /*
131  * Setup the transmit queue parameters for the beacon queue.
132  */
133 int
134 ath_beaconq_config(struct ath_softc *sc)
135 {
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;
139         HAL_TXQ_INFO qi;
140
141         ath_hal_gettxqueueprops(ah, sc->sc_bhalq, &qi);
142         if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
143             ic->ic_opmode == IEEE80211_M_MBSS) {
144                 /*
145                  * Always burst out beacon and CAB traffic.
146                  */
147                 qi.tqi_aifs = ATH_BEACON_AIFS_DEFAULT;
148                 qi.tqi_cwmin = ATH_BEACON_CWMIN_DEFAULT;
149                 qi.tqi_cwmax = ATH_BEACON_CWMAX_DEFAULT;
150         } else {
151                 struct wmeParams *wmep =
152                         &ic->ic_wme.wme_chanParams.cap_wmeParams[WME_AC_BE];
153                 /*
154                  * Adhoc mode; important thing is to use 2x cwmin.
155                  */
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);
159         }
160
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");
164                 return 0;
165         } else {
166                 ath_hal_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
167                 return 1;
168         }
169 #undef ATH_EXPONENT_TO_VALUE
170 }
171
172 /*
173  * Allocate and setup an initial beacon frame.
174  */
175 int
176 ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_node *ni)
177 {
178         struct ieee80211vap *vap = ni->ni_vap;
179         struct ath_vap *avp = ATH_VAP(vap);
180         struct ath_buf *bf;
181         struct mbuf *m;
182         int error;
183
184         bf = avp->av_bcbuf;
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);
189                 m_freem(bf->bf_m);
190                 bf->bf_m = NULL;
191         }
192         if (bf->bf_node != NULL) {
193                 ieee80211_free_node(bf->bf_node);
194                 bf->bf_node = NULL;
195         }
196
197         /*
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).
201          */
202         m = ieee80211_beacon_alloc(ni);
203         if (m == NULL) {
204                 device_printf(sc->sc_dev, "%s: cannot get mbuf\n", __func__);
205                 sc->sc_stats.ast_be_nombuf++;
206                 return ENOMEM;
207         }
208         error = bus_dmamap_load_mbuf_sg(sc->sc_dmat, bf->bf_dmamap, m,
209                                      bf->bf_segs, &bf->bf_nseg,
210                                      BUS_DMA_NOWAIT);
211         if (error != 0) {
212                 device_printf(sc->sc_dev,
213                     "%s: cannot map mbuf, bus_dmamap_load_mbuf_sg returns %d\n",
214                     __func__, error);
215                 m_freem(m);
216                 return error;
217         }
218
219         /*
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
223          * header.
224          */
225         if (sc->sc_stagbeacons && avp->av_bslot > 0) {
226                 uint64_t tsfadjust;
227                 struct ieee80211_frame *wh;
228
229                 /*
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.
238                  */
239                 tsfadjust = ni->ni_intval *
240                     (ATH_BCBUF - avp->av_bslot) / ATH_BCBUF;
241                 tsfadjust = htole64(tsfadjust << 10);   /* TU -> TSF */
242
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));
248
249                 wh = mtod(m, struct ieee80211_frame *);
250                 memcpy(&wh[1], &tsfadjust, sizeof(tsfadjust));
251         }
252         bf->bf_m = m;
253         bf->bf_node = ieee80211_ref_node(ni);
254
255         return 0;
256 }
257
258 /*
259  * Setup the beacon frame for transmit.
260  */
261 static void
262 ath_beacon_setup(struct ath_softc *sc, struct ath_buf *bf)
263 {
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;
271         struct ath_desc *ds;
272         int flags, antenna;
273         const HAL_RATE_TABLE *rt;
274         u_int8_t rix, rate;
275         HAL_DMA_ADDR bufAddrList[4];
276         uint32_t segLenList[4];
277         HAL_11N_RATE_SERIES rc[4];
278
279         DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: m %p len %u\n",
280                 __func__, m, m->m_len);
281
282         /* setup descriptors */
283         ds = bf->bf_desc;
284         bf->bf_last = bf;
285         bf->bf_lastds = ds;
286
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;
292                 /*
293                  * Let hardware handle antenna switching.
294                  */
295                 antenna = sc->sc_txantenna;
296         } else {
297                 ath_hal_settxdesclink(sc->sc_ah, ds, 0);
298                 /*
299                  * Switch antenna every 4 beacons.
300                  * XXX assumes two antenna
301                  */
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);
306                 else
307                         antenna = (sc->sc_stats.ast_be_xmit & 4 ? 2 : 1);
308         }
309
310         KASSERT(bf->bf_nseg == 1,
311                 ("multi-segment beacon frame; nseg %u", bf->bf_nseg));
312
313         /*
314          * Calculate rate code.
315          * XXX everything at min xmit rate
316          */
317         rix = 0;
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 */
333         );
334
335         /*
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().
339          */
340         if (sc->sc_isedma) {
341                 memset(&rc, 0, sizeof(rc));
342
343                 rc[0].ChSel = sc->sc_txchainmask;
344                 rc[0].Tries = 1;
345                 rc[0].Rate = rt->info[rix].rateCode;
346                 rc[0].RateIndex = rix;
347                 rc[0].tx_power_cap = 0x3f;
348                 rc[0].PktDuration =
349                     ath_hal_computetxtime(ah, rt, roundup(m->m_len, 4),
350                         rix, 0, AH_TRUE);
351                 ath_hal_set11nratescenario(ah, ds, 0, 0, rc, 4, flags);
352         }
353
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
360                 , bufAddrList
361                 , segLenList
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 */
367         );
368 #if 0
369         ath_desc_swap(ds);
370 #endif
371 #undef USE_SHPREAMBLE
372 }
373
374 void
375 ath_beacon_update(struct ieee80211vap *vap, int item)
376 {
377         struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off;
378
379         setbit(bo->bo_flags, item);
380 }
381
382 /*
383  * Handle a beacon miss.
384  */
385 void
386 ath_beacon_miss(struct ath_softc *sc)
387 {
388         HAL_SURVEY_SAMPLE hs;
389         HAL_BOOL ret;
390         uint32_t hangs;
391
392         bzero(&hs, sizeof(hs));
393
394         ret = ath_hal_get_mib_cycle_counts(sc->sc_ah, &hs);
395
396         if (ath_hal_gethangstate(sc->sc_ah, 0xffff, &hangs) && hangs != 0) {
397                 DPRINTF(sc, ATH_DEBUG_BEACON,
398                     "%s: hang=0x%08x\n",
399                     __func__,
400                     hangs);
401         }
402
403 #ifdef  ATH_DEBUG_ALQ
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);
406 #endif
407
408         DPRINTF(sc, ATH_DEBUG_BEACON,
409             "%s: valid=%d, txbusy=%u, rxbusy=%u, chanbusy=%u, "
410             "extchanbusy=%u, cyclecount=%u\n",
411             __func__,
412             ret,
413             hs.tx_busy,
414             hs.rx_busy,
415             hs.chan_busy,
416             hs.ext_chan_busy,
417             hs.cycle_count);
418 }
419
420 /*
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.
424  */
425 void
426 ath_beacon_proc(void *arg, int pending)
427 {
428         struct ath_softc *sc = arg;
429         struct ath_hal *ah = sc->sc_ah;
430         struct ieee80211vap *vap;
431         struct ath_buf *bf;
432         int slot, otherant;
433         uint32_t bfaddr;
434
435         DPRINTF(sc, ATH_DEBUG_BEACON_PROC, "%s: pending %u\n",
436                 __func__, pending);
437         /*
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.
443          */
444         if (ath_hal_numtxpending(ah, sc->sc_bhalq) != 0) {
445                 sc->sc_bmisscount++;
446                 sc->sc_stats.ast_be_missed++;
447                 ath_beacon_miss(sc);
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);
453                 return;
454         }
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;
460 #ifdef  ATH_DEBUG_ALQ
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);
463 #endif
464         }
465
466         if (sc->sc_stagbeacons) {                       /* staggered beacons */
467                 struct ieee80211com *ic = &sc->sc_ic;
468                 uint32_t tsftu;
469
470                 tsftu = ath_hal_gettsf32(ah) >> 10;
471                 /* XXX lintval */
472                 slot = ((tsftu % ic->ic_lintval) * ATH_BCBUF) / ic->ic_lintval;
473                 vap = sc->sc_bslot[(slot+1) % ATH_BCBUF];
474                 bfaddr = 0;
475                 if (vap != NULL && vap->iv_state >= IEEE80211_S_RUN) {
476                         bf = ath_beacon_generate(sc, vap);
477                         if (bf != NULL)
478                                 bfaddr = bf->bf_daddr;
479                 }
480         } else {                                        /* burst'd beacons */
481                 uint32_t *bflink = &bfaddr;
482
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);
487                                 /*
488                                  * XXX TODO: this should use settxdesclinkptr()
489                                  * otherwise it won't work for EDMA chipsets!
490                                  */
491                                 if (bf != NULL) {
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);
496                                 }
497                         }
498                 }
499                 /*
500                  * XXX TODO: this should use settxdesclinkptr()
501                  * otherwise it won't work for EDMA chipsets!
502                  */
503                 *bflink = 0;                            /* terminate list */
504         }
505
506         /*
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.
512          */
513         /* XXX locking */
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 */
519
520         /*
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
525          */
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;
531         }
532
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);
537
538         /* Program the new beacon frame if we have one for this interval */
539         if (bfaddr != 0) {
540                 /*
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.
544                  */
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);
550                         }
551                 }
552                 /* NB: cabq traffic should already be queued and primed */
553
554                 ath_hal_puttxbuf(ah, sc->sc_bhalq, bfaddr);
555                 ath_hal_txstart(ah, sc->sc_bhalq);
556
557                 sc->sc_stats.ast_be_xmit++;
558         }
559 }
560
561 static void
562 ath_beacon_cabq_start_edma(struct ath_softc *sc)
563 {
564         struct ath_buf *bf, *bf_last;
565         struct ath_txq *cabq = sc->sc_cabq;
566 #if 0
567         struct ath_buf *bfi;
568         int i = 0;
569 #endif
570
571         ATH_TXQ_LOCK_ASSERT(cabq);
572
573         if (TAILQ_EMPTY(&cabq->axq_q))
574                 return;
575         bf = TAILQ_FIRST(&cabq->axq_q);
576         bf_last = TAILQ_LAST(&cabq->axq_q, axq_q_s);
577
578         /*
579          * This is a dirty, dirty hack to push the contents of
580          * the cabq staging queue into the FIFO.
581          *
582          * This ideally should live in the EDMA code file
583          * and only push things into the CABQ if there's a FIFO
584          * slot.
585          *
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
593          * with EDMA.
594          */
595
596         /*
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
600          * beacon interval.
601          */
602         if (cabq->axq_fifo_depth >= HAL_TXFIFO_DEPTH) {
603                 device_printf(sc->sc_dev,
604                     "%s: Q%d: CAB FIFO queue=%d?\n",
605                     __func__,
606                     cabq->axq_qnum,
607                     cabq->axq_fifo_depth);
608                 return;
609         }
610
611         /*
612          * Ok, so here's the gymnastics reqiured to make this
613          * all sensible.
614          */
615
616         /*
617          * Tag the first/last buffer appropriately.
618          */
619         bf->bf_flags |= ATH_BUF_FIFOPTR;
620         bf_last->bf_flags |= ATH_BUF_FIFOEND;
621
622 #if 0
623         i = 0;
624         TAILQ_FOREACH(bfi, &cabq->axq_q, bf_list) {
625                 ath_printtxbuf(sc, bf, cabq->axq_qnum, i, 0);
626                 i++;
627         }
628 #endif
629
630         /*
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.
635          */
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;
639         cabq->axq_depth = 0;
640
641         /* Bump FIFO queue */
642         cabq->axq_fifo_depth++;
643
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;
647
648         /* NB: gated by beacon so safe to start here */
649         ath_hal_txstart(sc->sc_ah, cabq->axq_qnum);
650
651 }
652
653 static void
654 ath_beacon_cabq_start_legacy(struct ath_softc *sc)
655 {
656         struct ath_buf *bf;
657         struct ath_txq *cabq = sc->sc_cabq;
658
659         ATH_TXQ_LOCK_ASSERT(cabq);
660         if (TAILQ_EMPTY(&cabq->axq_q))
661                 return;
662         bf = TAILQ_FIRST(&cabq->axq_q);
663
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;
667
668         /* NB: gated by beacon so safe to start here */
669         ath_hal_txstart(sc->sc_ah, cabq->axq_qnum);
670 }
671
672 /*
673  * Start CABQ transmission - this assumes that all frames are prepped
674  * and ready in the CABQ.
675  */
676 void
677 ath_beacon_cabq_start(struct ath_softc *sc)
678 {
679         struct ath_txq *cabq = sc->sc_cabq;
680
681         ATH_TXQ_LOCK_ASSERT(cabq);
682
683         if (TAILQ_EMPTY(&cabq->axq_q))
684                 return;
685
686         if (sc->sc_isedma)
687                 ath_beacon_cabq_start_edma(sc);
688         else
689                 ath_beacon_cabq_start_legacy(sc);
690 }
691
692 struct ath_buf *
693 ath_beacon_generate(struct ath_softc *sc, struct ieee80211vap *vap)
694 {
695         struct ath_vap *avp = ATH_VAP(vap);
696         struct ath_txq *cabq = sc->sc_cabq;
697         struct ath_buf *bf;
698         struct mbuf *m;
699         int nmcastq, error;
700
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"));
704
705         /*
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).
710          */
711         bf = avp->av_bcbuf;
712         m = bf->bf_m;
713         /* XXX lock mcastq? */
714         nmcastq = avp->av_mcastq.axq_depth;
715
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,
721                                              BUS_DMA_NOWAIT);
722                 if (error != 0) {
723                         if_printf(vap->iv_ifp,
724                             "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
725                             __func__, error);
726                         return NULL;
727                 }
728         }
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) {
735                         /*
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
741                          */
742                         /*
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?
749                          *
750                          * More thought is required here.
751                          */
752                         /*
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.
756                          */
757                         ath_tx_draintxq(sc, cabq);
758                 }
759         }
760         ath_beacon_setup(sc, bf);
761         bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
762
763         /*
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.
767          */
768
769         /*
770          * Enable the CAB queue before the beacon queue to
771          * insure cab frames are triggered by this beacon.
772          */
773         if (vap->iv_bcn_off.bo_tim[4] & 1) {
774
775                 /* NB: only at DTIM */
776                 ATH_TXQ_LOCK(&avp->av_mcastq);
777                 if (nmcastq) {
778                         struct ath_buf *bfm, *bfc_last;
779
780                         /*
781                          * Move frames from the s/w mcast q to the h/w cab q.
782                          *
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?)
788                          */
789                         bfm = TAILQ_FIRST(&avp->av_mcastq.axq_q);
790                         ATH_TXQ_LOCK(cabq);
791
792                         /*
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.
798                          */
799                         bfc_last = ATH_TXQ_LAST(cabq, axq_q_s);
800                         if (bfc_last != NULL) {
801                                 ath_hal_settxdesclink(sc->sc_ah,
802                                     bfc_last->bf_lastds,
803                                     bfm->bf_daddr);
804                         }
805                         ath_txqmove(cabq, &avp->av_mcastq);
806                         ATH_TXQ_UNLOCK(cabq);
807                         /*
808                          * XXX not entirely accurate, in case a mcast
809                          * queue frame arrived before we grabbed the TX
810                          * lock.
811                          */
812                         sc->sc_stats.ast_cabq_xmit += nmcastq;
813                 }
814                 ATH_TXQ_UNLOCK(&avp->av_mcastq);
815         }
816         return bf;
817 }
818
819 void
820 ath_beacon_start_adhoc(struct ath_softc *sc, struct ieee80211vap *vap)
821 {
822         struct ath_vap *avp = ATH_VAP(vap);
823         struct ath_hal *ah = sc->sc_ah;
824         struct ath_buf *bf;
825         struct mbuf *m;
826         int error;
827
828         KASSERT(avp->av_bcbuf != NULL, ("no beacon buffer"));
829
830         /*
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).
835          */
836         bf = avp->av_bcbuf;
837         m = bf->bf_m;
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,
843                                              BUS_DMA_NOWAIT);
844                 if (error != 0) {
845                         if_printf(vap->iv_ifp,
846                             "%s: bus_dmamap_load_mbuf_sg failed, error %u\n",
847                             __func__, error);
848                         return;
849                 }
850         }
851         ath_beacon_setup(sc, bf);
852         bus_dmamap_sync(sc->sc_dmat, bf->bf_dmamap, BUS_DMASYNC_PREWRITE);
853
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);
857 }
858
859 /*
860  * Reclaim beacon resources and return buffer to the pool.
861  */
862 void
863 ath_beacon_return(struct ath_softc *sc, struct ath_buf *bf)
864 {
865
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);
870                 m_freem(bf->bf_m);
871                 bf->bf_m = NULL;
872         }
873         if (bf->bf_node != NULL) {
874                 ieee80211_free_node(bf->bf_node);
875                 bf->bf_node = NULL;
876         }
877         TAILQ_INSERT_TAIL(&sc->sc_bbuf, bf, bf_list);
878 }
879
880 /*
881  * Reclaim beacon resources.
882  */
883 void
884 ath_beacon_free(struct ath_softc *sc)
885 {
886         struct ath_buf *bf;
887
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);
894                         m_freem(bf->bf_m);
895                         bf->bf_m = NULL;
896                 }
897                 if (bf->bf_node != NULL) {
898                         ieee80211_free_node(bf->bf_node);
899                         bf->bf_node = NULL;
900                 }
901         }
902 }
903
904 /*
905  * Configure the beacon and sleep timers.
906  *
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.
909  *
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.
918  */
919 void
920 ath_beacon_config(struct ath_softc *sc, struct ieee80211vap *vap)
921 {
922 #define TSF_TO_TU(_h,_l) \
923         ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
924 #define FUDGE   2
925         struct ath_hal *ah = sc->sc_ah;
926         struct ath_vap *avp;
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;
932
933         if (vap == NULL)
934                 vap = TAILQ_FIRST(&ic->ic_vaps);        /* XXX */
935         /*
936          * Just ensure that we aren't being called when the last
937          * VAP is destroyed.
938          */
939         if (vap == NULL) {
940                 device_printf(sc->sc_dev, "%s: called with no VAPs\n",
941                     __func__);
942                 return;
943         }
944
945         /* Now that we have a vap, we can do this bit */
946         avp = ATH_VAP(vap);
947
948         ni = ieee80211_ref_node(vap->iv_bss);
949
950         ATH_LOCK(sc);
951         ath_power_set_power_state(sc, HAL_PM_AWAKE);
952         ATH_UNLOCK(sc);
953
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));
957
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));
961
962         tsf_beacon = ((uint64_t) le32dec(ni->ni_tstamp.data + 4)) << 32;
963         tsf_beacon |= le32dec(ni->ni_tstamp.data);
964
965         if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
966             ic->ic_opmode == IEEE80211_M_MBSS) {
967                 /*
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.
972                  */
973                 /* NB: the beacon interval is kept internally in TU's */
974                 intval = ni->ni_intval & HAL_BEACON_PERIOD;
975                 if (sc->sc_stagbeacons)
976                         intval /= ATH_BCBUF;
977         } else {
978                 /* NB: the beacon interval is kept internally in TU's */
979                 intval = ni->ni_intval & HAL_BEACON_PERIOD;
980         }
981
982         /*
983          * Note: rounding up to the next intval can cause problems with
984          * bad APs when we're in powersave mode.
985          *
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.
991          *
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.
996          */
997         if (nexttbtt == 0)              /* e.g. for ap mode */
998                 nexttbtt = intval;
999         else
1000                 nexttbtt = roundup(nexttbtt, intval);
1001
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;
1008
1009                 /*
1010                  * Setup dtim and cfp parameters according to
1011                  * last beacon we received (which may be none).
1012                  */
1013                 dtimperiod = ni->ni_dtim_period;
1014                 if (dtimperiod <= 0)            /* NB: 0 if not known */
1015                         dtimperiod = 1;
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 */
1020                 cfpcount = 0;
1021                 /*
1022                  * Pull nexttbtt forward to reflect the current
1023                  * TSF and calculate dtim+cfp state for the result.
1024                  */
1025                 tsf = ath_hal_gettsf64(ah);
1026                 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
1027
1028                 DPRINTF(sc, ATH_DEBUG_BEACON,
1029                     "%s: beacon tsf=%llu, hw tsf=%llu, nexttbtt=%u, tsftu=%u\n",
1030                     __func__,
1031                     (unsigned long long) tsf_beacon,
1032                     (unsigned long long) tsf,
1033                     nexttbtt,
1034                     tsftu);
1035                 DPRINTF(sc, ATH_DEBUG_BEACON,
1036                     "%s: beacon tsf=%llu, hw tsf=%llu, tsf delta=%lld\n",
1037                     __func__,
1038                     (unsigned long long) tsf_beacon,
1039                     (unsigned long long) tsf,
1040                     (long long) tsf -
1041                     (long long) tsf_beacon);
1042
1043                 DPRINTF(sc, ATH_DEBUG_BEACON,
1044                     "%s: nexttbtt=%llu, beacon tsf delta=%lld\n",
1045                     __func__,
1046                     (unsigned long long) nexttbtt,
1047                     (long long) ((long long) nexttbtt * 1024LL) - (long long) tsf_beacon);
1048
1049                 /* XXX cfpcount? */
1050
1051                 if (nexttbtt > tsftu) {
1052                         uint32_t countdiff, oldtbtt, remainder;
1053
1054                         oldtbtt = nexttbtt;
1055                         remainder = (nexttbtt - tsftu) % intval;
1056                         nexttbtt = tsftu + remainder;
1057
1058                         countdiff = (oldtbtt - nexttbtt) / intval % dtimperiod;
1059                         if (dtimcount > countdiff) {
1060                                 dtimcount -= countdiff;
1061                         } else {
1062                                 dtimcount += dtimperiod - countdiff;
1063                         }
1064                 } else { //nexttbtt <= tsftu
1065                         uint32_t countdiff, oldtbtt, remainder;
1066
1067                         oldtbtt = nexttbtt;
1068                         remainder = (tsftu - nexttbtt) % intval;
1069                         nexttbtt = tsftu - remainder + intval;
1070                         countdiff = (nexttbtt - oldtbtt) / intval % dtimperiod;
1071                         if (dtimcount > countdiff) {
1072                                 dtimcount -= countdiff;
1073                         } else {
1074                                 dtimcount += dtimperiod - countdiff;
1075                         }
1076                 }
1077
1078                 DPRINTF(sc, ATH_DEBUG_BEACON,
1079                     "%s: adj nexttbtt=%llu, rx tsf delta=%lld\n",
1080                     __func__,
1081                     (unsigned long long) nexttbtt,
1082                     (long long) ((long long)nexttbtt * 1024LL) - (long long)tsf);
1083
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;
1092 #if 0
1093                 /*
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
1101                  */
1102                 bs.bs_timoffset = ni->ni_timoff;
1103 #endif
1104                 /*
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.
1108                  */
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;
1114
1115                 /*
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.
1121                  *
1122                  * XXX fixed at 100ms
1123                  */
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);
1128
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"
1133                         , __func__
1134                         , tsf
1135                         , tsftu
1136                         , bs.bs_intval
1137                         , bs.bs_nexttbtt
1138                         , bs.bs_dtimperiod
1139                         , bs.bs_nextdtim
1140                         , bs.bs_bmissthreshold
1141                         , bs.bs_sleepduration
1142                         , bs.bs_cfpperiod
1143                         , bs.bs_cfpmaxduration
1144                         , bs.bs_cfpnext
1145                         , bs.bs_timoffset
1146                 );
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);
1151         } else {
1152                 ath_hal_intrset(ah, 0);
1153                 if (nexttbtt == intval)
1154                         intval |= HAL_BEACON_RESET_TSF;
1155                 if (ic->ic_opmode == IEEE80211_M_IBSS) {
1156                         /*
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
1161                          * deal with things.
1162                          */
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) {
1167                                 /*
1168                                  * Pull nexttbtt forward to reflect
1169                                  * the current TSF.
1170                                  */
1171                                 tsf = ath_hal_gettsf64(ah);
1172                                 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
1173                                 do {
1174                                         nexttbtt += intval;
1175                                 } while (nexttbtt < tsftu);
1176                         }
1177                         ath_beaconq_config(sc);
1178                 } else if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
1179                     ic->ic_opmode == IEEE80211_M_MBSS) {
1180                         /*
1181                          * In AP/mesh mode we enable the beacon timers
1182                          * and SWBA interrupts to prepare beacon frames.
1183                          */
1184                         intval |= HAL_BEACON_ENA;
1185                         sc->sc_imask |= HAL_INT_SWBA;   /* beacon prepare */
1186                         ath_beaconq_config(sc);
1187                 }
1188
1189                 /*
1190                  * Now dirty things because for now, the EDMA HAL has
1191                  * nexttbtt and intval is TU/8.
1192                  */
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);
1201                 } else
1202                         ath_hal_beaconinit(ah, nexttbtt, intval);
1203                 sc->sc_bmisscount = 0;
1204                 ath_hal_intrset(ah, sc->sc_imask);
1205                 /*
1206                  * When using a self-linked beacon descriptor in
1207                  * ibss mode load it once here.
1208                  */
1209                 if (ic->ic_opmode == IEEE80211_M_IBSS && sc->sc_hasveol)
1210                         ath_beacon_start_adhoc(sc, vap);
1211         }
1212         ieee80211_free_node(ni);
1213
1214         ATH_LOCK(sc);
1215         ath_power_restore_power_state(sc);
1216         ATH_UNLOCK(sc);
1217 #undef FUDGE
1218 #undef TSF_TO_TU
1219 }