ath9k: Revamp wireless mode usage
[pandora-kernel.git] / drivers / net / wireless / ath9k / beacon.c
1 /*
2  * Copyright (c) 2008 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17  /* Implementation of beacon processing. */
18
19 #include <asm/unaligned.h>
20 #include "core.h"
21
22 /*
23  *  Configure parameters for the beacon queue
24  *
25  *  This function will modify certain transmit queue properties depending on
26  *  the operating mode of the station (AP or AdHoc).  Parameters are AIFS
27  *  settings and channel width min/max
28 */
29
30 static int ath_beaconq_config(struct ath_softc *sc)
31 {
32         struct ath_hal *ah = sc->sc_ah;
33         struct ath9k_tx_queue_info qi;
34
35         ath9k_hw_get_txq_props(ah, sc->sc_bhalq, &qi);
36         if (sc->sc_opmode == ATH9K_M_HOSTAP) {
37                 /* Always burst out beacon and CAB traffic. */
38                 qi.tqi_aifs = 1;
39                 qi.tqi_cwmin = 0;
40                 qi.tqi_cwmax = 0;
41         } else {
42                 /* Adhoc mode; important thing is to use 2x cwmin. */
43                 qi.tqi_aifs = sc->sc_beacon_qi.tqi_aifs;
44                 qi.tqi_cwmin = 2*sc->sc_beacon_qi.tqi_cwmin;
45                 qi.tqi_cwmax = sc->sc_beacon_qi.tqi_cwmax;
46         }
47
48         if (!ath9k_hw_set_txq_props(ah, sc->sc_bhalq, &qi)) {
49                 DPRINTF(sc, ATH_DBG_FATAL,
50                         "%s: unable to update h/w beacon queue parameters\n",
51                         __func__);
52                 return 0;
53         } else {
54                 ath9k_hw_resettxqueue(ah, sc->sc_bhalq); /* push to h/w */
55                 return 1;
56         }
57 }
58
59 /*
60  *  Setup the beacon frame for transmit.
61  *
62  *  Associates the beacon frame buffer with a transmit descriptor.  Will set
63  *  up all required antenna switch parameters, rate codes, and channel flags.
64  *  Beacons are always sent out at the lowest rate, and are not retried.
65 */
66
67 static void ath_beacon_setup(struct ath_softc *sc,
68         struct ath_vap *avp, struct ath_buf *bf)
69 {
70         struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
71         struct ath_hal *ah = sc->sc_ah;
72         struct ath_desc *ds;
73         int flags, antenna;
74         const struct ath9k_rate_table *rt;
75         u8 rix, rate;
76         int ctsrate = 0;
77         int ctsduration = 0;
78         struct ath9k_11n_rate_series  series[4];
79
80         DPRINTF(sc, ATH_DBG_BEACON, "%s: m %p len %u\n",
81                 __func__, skb, skb->len);
82
83         /* setup descriptors */
84         ds = bf->bf_desc;
85
86         flags = ATH9K_TXDESC_NOACK;
87
88         if (sc->sc_opmode == ATH9K_M_IBSS &&
89             (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
90                 ds->ds_link = bf->bf_daddr; /* self-linked */
91                 flags |= ATH9K_TXDESC_VEOL;
92                 /* Let hardware handle antenna switching. */
93                 antenna = 0;
94         } else {
95                 ds->ds_link = 0;
96                 /*
97                  * Switch antenna every beacon.
98                  * Should only switch every beacon period, not for every
99                  * SWBA's
100                  * XXX assumes two antenna
101                  */
102                 antenna = ((sc->ast_be_xmit / sc->sc_nbcnvaps) & 1 ? 2 : 1);
103         }
104
105         ds->ds_data = bf->bf_buf_addr;
106
107         /*
108          * Calculate rate code.
109          * XXX everything at min xmit rate
110          */
111         rix = 0;
112         rt = sc->sc_currates;
113         rate = rt->info[rix].rateCode;
114         if (sc->sc_flags & ATH_PREAMBLE_SHORT)
115                 rate |= rt->info[rix].shortPreamble;
116
117         ath9k_hw_set11n_txdesc(ah, ds
118                               , skb->len + FCS_LEN /* frame length */
119                               , ATH9K_PKT_TYPE_BEACON /* Atheros packet type */
120                               , avp->av_btxctl.txpower /* txpower XXX */
121                               , ATH9K_TXKEYIX_INVALID /* no encryption */
122                               , ATH9K_KEY_TYPE_CLEAR /* no encryption */
123                               , flags /* no ack, veol for beacons */
124                 );
125
126         /* NB: beacon's BufLen must be a multiple of 4 bytes */
127         ath9k_hw_filltxdesc(ah, ds
128                            , roundup(skb->len, 4) /* buffer length */
129                            , true /* first segment */
130                            , true /* last segment */
131                            , ds /* first descriptor */
132                 );
133
134         memzero(series, sizeof(struct ath9k_11n_rate_series) * 4);
135         series[0].Tries = 1;
136         series[0].Rate = rate;
137         series[0].ChSel = sc->sc_tx_chainmask;
138         series[0].RateFlags = (ctsrate) ? ATH9K_RATESERIES_RTS_CTS : 0;
139         ath9k_hw_set11n_ratescenario(ah, ds, ds, 0,
140                 ctsrate, ctsduration, series, 4, 0);
141 }
142
143 /* Move everything from the vap's mcast queue to the hardware cab queue.
144  * Caller must hold mcasq lock and cabq lock
145  * XXX MORE_DATA bit?
146  */
147 static void empty_mcastq_into_cabq(struct ath_hal *ah,
148         struct ath_txq *mcastq, struct ath_txq *cabq)
149 {
150         struct ath_buf *bfmcast;
151
152         BUG_ON(list_empty(&mcastq->axq_q));
153
154         bfmcast = list_first_entry(&mcastq->axq_q, struct ath_buf, list);
155
156         /* link the descriptors */
157         if (!cabq->axq_link)
158                 ath9k_hw_puttxbuf(ah, cabq->axq_qnum, bfmcast->bf_daddr);
159         else
160                 *cabq->axq_link = bfmcast->bf_daddr;
161
162         /* append the private vap mcast list to  the cabq */
163
164         cabq->axq_depth += mcastq->axq_depth;
165         cabq->axq_totalqueued += mcastq->axq_totalqueued;
166         cabq->axq_linkbuf = mcastq->axq_linkbuf;
167         cabq->axq_link = mcastq->axq_link;
168         list_splice_tail_init(&mcastq->axq_q, &cabq->axq_q);
169         mcastq->axq_depth = 0;
170         mcastq->axq_totalqueued = 0;
171         mcastq->axq_linkbuf = NULL;
172         mcastq->axq_link = NULL;
173 }
174
175 /* This is only run at DTIM. We move everything from the vap's mcast queue
176  * to the hardware cab queue. Caller must hold the mcastq lock. */
177 static void trigger_mcastq(struct ath_hal *ah,
178         struct ath_txq *mcastq, struct ath_txq *cabq)
179 {
180         spin_lock_bh(&cabq->axq_lock);
181
182         if (!list_empty(&mcastq->axq_q))
183                 empty_mcastq_into_cabq(ah, mcastq, cabq);
184
185         /* cabq is gated by beacon so it is safe to start here */
186         if (!list_empty(&cabq->axq_q))
187                 ath9k_hw_txstart(ah, cabq->axq_qnum);
188
189         spin_unlock_bh(&cabq->axq_lock);
190 }
191
192 /*
193  *  Generate beacon frame and queue cab data for a vap.
194  *
195  *  Updates the contents of the beacon frame.  It is assumed that the buffer for
196  *  the beacon frame has been allocated in the ATH object, and simply needs to
197  *  be filled for this cycle.  Also, any CAB (crap after beacon?) traffic will
198  *  be added to the beacon frame at this point.
199 */
200 static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
201 {
202         struct ath_hal *ah = sc->sc_ah;
203         struct ath_buf *bf;
204         struct ath_vap *avp;
205         struct sk_buff *skb;
206         int cabq_depth;
207         int mcastq_depth;
208         int is_beacon_dtim = 0;
209         unsigned int curlen;
210         struct ath_txq *cabq;
211         struct ath_txq *mcastq;
212         avp = sc->sc_vaps[if_id];
213
214         mcastq = &avp->av_mcastq;
215         cabq = sc->sc_cabq;
216
217         ASSERT(avp);
218
219         if (avp->av_bcbuf == NULL) {
220                 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
221                         __func__, avp, avp->av_bcbuf);
222                 return NULL;
223         }
224         bf = avp->av_bcbuf;
225         skb = (struct sk_buff *) bf->bf_mpdu;
226
227         /*
228          * Update dynamic beacon contents.  If this returns
229          * non-zero then we need to remap the memory because
230          * the beacon frame changed size (probably because
231          * of the TIM bitmap).
232          */
233         curlen = skb->len;
234
235         /* XXX: spin_lock_bh should not be used here, but sparse bitches
236          * otherwise. We should fix sparse :) */
237         spin_lock_bh(&mcastq->axq_lock);
238         mcastq_depth = avp->av_mcastq.axq_depth;
239
240         if (ath_update_beacon(sc, if_id, &avp->av_boff, skb, mcastq_depth) ==
241             1) {
242                 ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
243                                      get_dma_mem_context(bf, bf_dmacontext));
244                 bf->bf_buf_addr = ath_skb_map_single(sc, skb, PCI_DMA_TODEVICE,
245                         get_dma_mem_context(bf, bf_dmacontext));
246         } else {
247                 pci_dma_sync_single_for_cpu(sc->pdev,
248                                             bf->bf_buf_addr,
249                                             skb_tailroom(skb),
250                                             PCI_DMA_TODEVICE);
251         }
252
253         /*
254          * if the CABQ traffic from previous DTIM is pending and the current
255          *  beacon is also a DTIM.
256          *  1) if there is only one vap let the cab traffic continue.
257          *  2) if there are more than one vap and we are using staggered
258          *     beacons, then drain the cabq by dropping all the frames in
259          *     the cabq so that the current vaps cab traffic can be scheduled.
260          */
261         spin_lock_bh(&cabq->axq_lock);
262         cabq_depth = cabq->axq_depth;
263         spin_unlock_bh(&cabq->axq_lock);
264
265         is_beacon_dtim = avp->av_boff.bo_tim[4] & 1;
266
267         if (mcastq_depth && is_beacon_dtim && cabq_depth) {
268                 /*
269                  * Unlock the cabq lock as ath_tx_draintxq acquires
270                  * the lock again which is a common function and that
271                  * acquires txq lock inside.
272                  */
273                 if (sc->sc_nvaps > 1) {
274                         ath_tx_draintxq(sc, cabq, false);
275                         DPRINTF(sc, ATH_DBG_BEACON,
276                                 "%s: flush previous cabq traffic\n", __func__);
277                 }
278         }
279
280         /* Construct tx descriptor. */
281         ath_beacon_setup(sc, avp, bf);
282
283         /*
284          * Enable the CAB queue before the beacon queue to
285          * insure cab frames are triggered by this beacon.
286          */
287         if (is_beacon_dtim)
288                 trigger_mcastq(ah, mcastq, cabq);
289
290         spin_unlock_bh(&mcastq->axq_lock);
291         return bf;
292 }
293
294 /*
295  * Startup beacon transmission for adhoc mode when they are sent entirely
296  * by the hardware using the self-linked descriptor + veol trick.
297 */
298
299 static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
300 {
301         struct ath_hal *ah = sc->sc_ah;
302         struct ath_buf *bf;
303         struct ath_vap *avp;
304         struct sk_buff *skb;
305
306         avp = sc->sc_vaps[if_id];
307         ASSERT(avp);
308
309         if (avp->av_bcbuf == NULL) {
310                 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
311                         __func__, avp, avp != NULL ? avp->av_bcbuf : NULL);
312                 return;
313         }
314         bf = avp->av_bcbuf;
315         skb = (struct sk_buff *) bf->bf_mpdu;
316
317         /* Construct tx descriptor. */
318         ath_beacon_setup(sc, avp, bf);
319
320         /* NB: caller is known to have already stopped tx dma */
321         ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
322         ath9k_hw_txstart(ah, sc->sc_bhalq);
323         DPRINTF(sc, ATH_DBG_BEACON, "%s: TXDP%u = %llx (%p)\n", __func__,
324                 sc->sc_bhalq, ito64(bf->bf_daddr), bf->bf_desc);
325 }
326
327 /*
328  *  Setup a h/w transmit queue for beacons.
329  *
330  *  This function allocates an information structure (struct ath9k_txq_info)
331  *  on the stack, sets some specific parameters (zero out channel width
332  *  min/max, and enable aifs). The info structure does not need to be
333  *  persistant.
334 */
335
336 int ath_beaconq_setup(struct ath_hal *ah)
337 {
338         struct ath9k_tx_queue_info qi;
339
340         memzero(&qi, sizeof(qi));
341         qi.tqi_aifs = 1;
342         qi.tqi_cwmin = 0;
343         qi.tqi_cwmax = 0;
344         /* NB: don't enable any interrupts */
345         return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
346 }
347
348
349 /*
350  *  Allocate and setup an initial beacon frame.
351  *
352  *  Allocate a beacon state variable for a specific VAP instance created on
353  *  the ATH interface.  This routine also calculates the beacon "slot" for
354  *  staggared beacons in the mBSSID case.
355 */
356
357 int ath_beacon_alloc(struct ath_softc *sc, int if_id)
358 {
359         struct ath_vap *avp;
360         struct ieee80211_hdr *wh;
361         struct ath_buf *bf;
362         struct sk_buff *skb;
363
364         avp = sc->sc_vaps[if_id];
365         ASSERT(avp);
366
367         /* Allocate a beacon descriptor if we haven't done so. */
368         if (!avp->av_bcbuf) {
369                 /*
370                  * Allocate beacon state for hostap/ibss.  We know
371                  * a buffer is available.
372                  */
373
374                 avp->av_bcbuf = list_first_entry(&sc->sc_bbuf,
375                                 struct ath_buf, list);
376                 list_del(&avp->av_bcbuf->list);
377
378                 if (sc->sc_opmode == ATH9K_M_HOSTAP ||
379                     !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
380                         int slot;
381                         /*
382                          * Assign the vap to a beacon xmit slot. As
383                          * above, this cannot fail to find one.
384                          */
385                         avp->av_bslot = 0;
386                         for (slot = 0; slot < ATH_BCBUF; slot++)
387                                 if (sc->sc_bslot[slot] == ATH_IF_ID_ANY) {
388                                         /*
389                                          * XXX hack, space out slots to better
390                                          * deal with misses
391                                          */
392                                         if (slot+1 < ATH_BCBUF &&
393                                             sc->sc_bslot[slot+1] ==
394                                                 ATH_IF_ID_ANY) {
395                                                 avp->av_bslot = slot+1;
396                                                 break;
397                                         }
398                                         avp->av_bslot = slot;
399                                         /* NB: keep looking for a double slot */
400                                 }
401                         BUG_ON(sc->sc_bslot[avp->av_bslot] != ATH_IF_ID_ANY);
402                         sc->sc_bslot[avp->av_bslot] = if_id;
403                         sc->sc_nbcnvaps++;
404                 }
405         }
406
407         /* release the previous beacon frame , if it already exists. */
408         bf = avp->av_bcbuf;
409         if (bf->bf_mpdu != NULL) {
410                 skb = (struct sk_buff *)bf->bf_mpdu;
411                 ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
412                                      get_dma_mem_context(bf, bf_dmacontext));
413                 dev_kfree_skb_any(skb);
414                 bf->bf_mpdu = NULL;
415         }
416
417         /*
418          * NB: the beacon data buffer must be 32-bit aligned;
419          * we assume the wbuf routines will return us something
420          * with this alignment (perhaps should assert).
421          * FIXME: Fill avp->av_boff.bo_tim,avp->av_btxctl.txpower and
422          * avp->av_btxctl.shortPreamble
423          */
424         skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
425         if (skb == NULL) {
426                 DPRINTF(sc, ATH_DBG_BEACON, "%s: cannot get skb\n",
427                         __func__);
428                 return -ENOMEM;
429         }
430
431         /*
432          * Calculate a TSF adjustment factor required for
433          * staggered beacons.  Note that we assume the format
434          * of the beacon frame leaves the tstamp field immediately
435          * following the header.
436          */
437         if (avp->av_bslot > 0) {
438                 u64 tsfadjust;
439                 __le64 val;
440                 int intval;
441
442                 /* FIXME: Use default value for now: Sujith */
443
444                 intval = ATH_DEFAULT_BINTVAL;
445
446                 /*
447                  * The beacon interval is in TU's; the TSF in usecs.
448                  * We figure out how many TU's to add to align the
449                  * timestamp then convert to TSF units and handle
450                  * byte swapping before writing it in the frame.
451                  * The hardware will then add this each time a beacon
452                  * frame is sent.  Note that we align vap's 1..N
453                  * and leave vap 0 untouched.  This means vap 0
454                  * has a timestamp in one beacon interval while the
455                  * others get a timestamp aligned to the next interval.
456                  */
457                 tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
458                 val = cpu_to_le64(tsfadjust << 10);     /* TU->TSF */
459
460                 DPRINTF(sc, ATH_DBG_BEACON,
461                         "%s: %s beacons, bslot %d intval %u tsfadjust %llu\n",
462                         __func__, "stagger",
463                         avp->av_bslot, intval, (unsigned long long)tsfadjust);
464
465                 wh = (struct ieee80211_hdr *)skb->data;
466                 memcpy(&wh[1], &val, sizeof(val));
467         }
468
469         bf->bf_buf_addr = ath_skb_map_single(sc, skb, PCI_DMA_TODEVICE,
470                 get_dma_mem_context(bf, bf_dmacontext));
471         bf->bf_mpdu = skb;
472
473         return 0;
474 }
475
476 /*
477  *  Reclaim beacon resources and return buffer to the pool.
478  *
479  *  Checks the VAP to put the beacon frame buffer back to the ATH object
480  *  queue, and de-allocates any wbuf frames that were sent as CAB traffic.
481 */
482
483 void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
484 {
485         if (avp->av_bcbuf != NULL) {
486                 struct ath_buf *bf;
487
488                 if (avp->av_bslot != -1) {
489                         sc->sc_bslot[avp->av_bslot] = ATH_IF_ID_ANY;
490                         sc->sc_nbcnvaps--;
491                 }
492
493                 bf = avp->av_bcbuf;
494                 if (bf->bf_mpdu != NULL) {
495                         struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
496                         ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
497                                 get_dma_mem_context(bf, bf_dmacontext));
498                         dev_kfree_skb_any(skb);
499                         bf->bf_mpdu = NULL;
500                 }
501                 list_add_tail(&bf->list, &sc->sc_bbuf);
502
503                 avp->av_bcbuf = NULL;
504         }
505 }
506
507 /*
508  *  Reclaim beacon resources and return buffer to the pool.
509  *
510  *  This function will free any wbuf frames that are still attached to the
511  *  beacon buffers in the ATH object.  Note that this does not de-allocate
512  *  any wbuf objects that are in the transmit queue and have not yet returned
513  *  to the ATH object.
514 */
515
516 void ath_beacon_free(struct ath_softc *sc)
517 {
518         struct ath_buf *bf;
519
520         list_for_each_entry(bf, &sc->sc_bbuf, list) {
521                 if (bf->bf_mpdu != NULL) {
522                         struct sk_buff *skb = (struct sk_buff *) bf->bf_mpdu;
523                         ath_skb_unmap_single(sc, skb, PCI_DMA_TODEVICE,
524                                 get_dma_mem_context(bf, bf_dmacontext));
525                         dev_kfree_skb_any(skb);
526                         bf->bf_mpdu = NULL;
527                 }
528         }
529 }
530
531 /*
532  * Tasklet for Sending Beacons
533  *
534  * Transmit one or more beacon frames at SWBA.  Dynamic updates to the frame
535  * contents are done as needed and the slot time is also adjusted based on
536  * current state.
537  *
538  * This tasklet is not scheduled, it's called in ISR context.
539 */
540
541 void ath9k_beacon_tasklet(unsigned long data)
542 {
543 #define TSF_TO_TU(_h,_l)                                        \
544         ((((u32)(_h)) << 22) | (((u32)(_l)) >> 10))
545
546         struct ath_softc *sc = (struct ath_softc *)data;
547         struct ath_hal *ah = sc->sc_ah;
548         struct ath_buf *bf = NULL;
549         int slot, if_id;
550         u32 bfaddr;
551         u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
552         u32 show_cycles = 0;
553         u32 bc = 0; /* beacon count */
554         u64 tsf;
555         u32 tsftu;
556         u16 intval;
557
558         if (sc->sc_noreset) {
559                 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
560                                                             &rx_clear,
561                                                             &rx_frame,
562                                                             &tx_frame);
563         }
564
565         /*
566          * Check if the previous beacon has gone out.  If
567          * not don't try to post another, skip this period
568          * and wait for the next.  Missed beacons indicate
569          * a problem and should not occur.  If we miss too
570          * many consecutive beacons reset the device.
571          */
572         if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
573                 sc->sc_bmisscount++;
574                 /* XXX: doth needs the chanchange IE countdown decremented.
575                  *      We should consider adding a mac80211 call to indicate
576                  *      a beacon miss so appropriate action could be taken
577                  *      (in that layer).
578                  */
579                 if (sc->sc_bmisscount < BSTUCK_THRESH) {
580                         if (sc->sc_noreset) {
581                                 DPRINTF(sc, ATH_DBG_BEACON,
582                                         "%s: missed %u consecutive beacons\n",
583                                         __func__, sc->sc_bmisscount);
584                                 if (show_cycles) {
585                                         /*
586                                          * Display cycle counter stats
587                                          * from HW to aide in debug of
588                                          * stickiness.
589                                          */
590                                         DPRINTF(sc,
591                                                 ATH_DBG_BEACON,
592                                                 "%s: busy times: rx_clear=%d, "
593                                                 "rx_frame=%d, tx_frame=%d\n",
594                                                 __func__, rx_clear, rx_frame,
595                                                 tx_frame);
596                                 } else {
597                                         DPRINTF(sc,
598                                                 ATH_DBG_BEACON,
599                                                 "%s: unable to obtain "
600                                                 "busy times\n", __func__);
601                                 }
602                         } else {
603                                 DPRINTF(sc, ATH_DBG_BEACON,
604                                         "%s: missed %u consecutive beacons\n",
605                                         __func__, sc->sc_bmisscount);
606                         }
607                 } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
608                         if (sc->sc_noreset) {
609                                 if (sc->sc_bmisscount == BSTUCK_THRESH) {
610                                         DPRINTF(sc,
611                                                 ATH_DBG_BEACON,
612                                                 "%s: beacon is officially "
613                                                 "stuck\n", __func__);
614                                         ath9k_hw_dmaRegDump(ah);
615                                 }
616                         } else {
617                                 DPRINTF(sc, ATH_DBG_BEACON,
618                                         "%s: beacon is officially stuck\n",
619                                         __func__);
620                                 ath_bstuck_process(sc);
621                         }
622                 }
623
624                 return;
625         }
626         if (sc->sc_bmisscount != 0) {
627                 if (sc->sc_noreset) {
628                         DPRINTF(sc,
629                                 ATH_DBG_BEACON,
630                                 "%s: resume beacon xmit after %u misses\n",
631                                 __func__, sc->sc_bmisscount);
632                 } else {
633                         DPRINTF(sc, ATH_DBG_BEACON,
634                                 "%s: resume beacon xmit after %u misses\n",
635                                 __func__, sc->sc_bmisscount);
636                 }
637                 sc->sc_bmisscount = 0;
638         }
639
640         /*
641          * Generate beacon frames. we are sending frames
642          * staggered so calculate the slot for this frame based
643          * on the tsf to safeguard against missing an swba.
644          */
645
646         /* FIXME: Use default value for now - Sujith */
647         intval = ATH_DEFAULT_BINTVAL;
648
649         tsf = ath9k_hw_gettsf64(ah);
650         tsftu = TSF_TO_TU(tsf>>32, tsf);
651         slot = ((tsftu % intval) * ATH_BCBUF) / intval;
652         if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
653         DPRINTF(sc, ATH_DBG_BEACON,
654                         "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
655                         __func__, slot, (unsigned long long) tsf, tsftu,
656                         intval, if_id);
657         bfaddr = 0;
658         if (if_id != ATH_IF_ID_ANY) {
659                 bf = ath_beacon_generate(sc, if_id);
660                 if (bf != NULL) {
661                         bfaddr = bf->bf_daddr;
662                         bc = 1;
663                 }
664         }
665         /*
666          * Handle slot time change when a non-ERP station joins/leaves
667          * an 11g network.  The 802.11 layer notifies us via callback,
668          * we mark updateslot, then wait one beacon before effecting
669          * the change.  This gives associated stations at least one
670          * beacon interval to note the state change.
671          *
672          * NB: The slot time change state machine is clocked according
673          *     to whether we are bursting or staggering beacons.  We
674          *     recognize the request to update and record the current
675          *     slot then don't transition until that slot is reached
676          *     again.  If we miss a beacon for that slot then we'll be
677          *     slow to transition but we'll be sure at least one beacon
678          *     interval has passed.  When bursting slot is always left
679          *     set to ATH_BCBUF so this check is a noop.
680          */
681         /* XXX locking */
682         if (sc->sc_updateslot == UPDATE) {
683                 sc->sc_updateslot = COMMIT; /* commit next beacon */
684                 sc->sc_slotupdate = slot;
685         } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
686                 ath_setslottime(sc);        /* commit change to hardware */
687
688         if (bfaddr != 0) {
689                 /*
690                  * Stop any current dma and put the new frame(s) on the queue.
691                  * This should never fail since we check above that no frames
692                  * are still pending on the queue.
693                  */
694                 if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
695                         DPRINTF(sc, ATH_DBG_FATAL,
696                                 "%s: beacon queue %u did not stop?\n",
697                                 __func__, sc->sc_bhalq);
698                         /* NB: the HAL still stops DMA, so proceed */
699                 }
700
701                 /* NB: cabq traffic should already be queued and primed */
702                 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
703                 ath9k_hw_txstart(ah, sc->sc_bhalq);
704
705                 sc->ast_be_xmit += bc;     /* XXX per-vap? */
706         }
707 #undef TSF_TO_TU
708 }
709
710 /*
711  *  Tasklet for Beacon Stuck processing
712  *
713  *  Processing for Beacon Stuck.
714  *  Basically calls the ath_internal_reset function to reset the chip.
715 */
716
717 void ath_bstuck_process(struct ath_softc *sc)
718 {
719         DPRINTF(sc, ATH_DBG_BEACON,
720                 "%s: stuck beacon; resetting (bmiss count %u)\n",
721                 __func__, sc->sc_bmisscount);
722         ath_internal_reset(sc);
723 }
724
725 /*
726  * Configure the beacon and sleep timers.
727  *
728  * When operating as an AP this resets the TSF and sets
729  * up the hardware to notify us when we need to issue beacons.
730  *
731  * When operating in station mode this sets up the beacon
732  * timers according to the timestamp of the last received
733  * beacon and the current TSF, configures PCF and DTIM
734  * handling, programs the sleep registers so the hardware
735  * will wakeup in time to receive beacons, and configures
736  * the beacon miss handling so we'll receive a BMISS
737  * interrupt when we stop seeing beacons from the AP
738  * we've associated with.
739  */
740
741 void ath_beacon_config(struct ath_softc *sc, int if_id)
742 {
743 #define TSF_TO_TU(_h,_l)                                        \
744         ((((u32)(_h)) << 22) | (((u32)(_l)) >> 10))
745         struct ath_hal *ah = sc->sc_ah;
746         u32 nexttbtt, intval;
747         struct ath_beacon_config conf;
748         enum ath9k_opmode av_opmode;
749
750         if (if_id != ATH_IF_ID_ANY)
751                 av_opmode = sc->sc_vaps[if_id]->av_opmode;
752         else
753                 av_opmode = sc->sc_opmode;
754
755         memzero(&conf, sizeof(struct ath_beacon_config));
756
757         /* FIXME: Use default values for now - Sujith */
758         /* Query beacon configuration first */
759         /*
760          * Protocol stack doesn't support dynamic beacon configuration,
761          * use default configurations.
762          */
763         conf.beacon_interval = ATH_DEFAULT_BINTVAL;
764         conf.listen_interval = 1;
765         conf.dtim_period = conf.beacon_interval;
766         conf.dtim_count = 1;
767         conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
768
769         /* extract tstamp from last beacon and convert to TU */
770         nexttbtt = TSF_TO_TU(get_unaligned_le32(conf.u.last_tstamp + 4),
771                              get_unaligned_le32(conf.u.last_tstamp));
772         /* XXX conditionalize multi-bss support? */
773         if (sc->sc_opmode == ATH9K_M_HOSTAP) {
774                 /*
775                  * For multi-bss ap support beacons are either staggered
776                  * evenly over N slots or burst together.  For the former
777                  * arrange for the SWBA to be delivered for each slot.
778                  * Slots that are not occupied will generate nothing.
779                  */
780                 /* NB: the beacon interval is kept internally in TU's */
781                 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
782                 intval /= ATH_BCBUF;    /* for staggered beacons */
783         } else {
784                 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
785         }
786
787         if (nexttbtt == 0)      /* e.g. for ap mode */
788                 nexttbtt = intval;
789         else if (intval)        /* NB: can be 0 for monitor mode */
790                 nexttbtt = roundup(nexttbtt, intval);
791         DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
792                 __func__, nexttbtt, intval, conf.beacon_interval);
793         /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
794         if (sc->sc_opmode == ATH9K_M_STA) {
795                 struct ath9k_beacon_state bs;
796                 u64 tsf;
797                 u32 tsftu;
798                 int dtimperiod, dtimcount, sleepduration;
799                 int cfpperiod, cfpcount;
800
801                 /*
802                  * Setup dtim and cfp parameters according to
803                  * last beacon we received (which may be none).
804                  */
805                 dtimperiod = conf.dtim_period;
806                 if (dtimperiod <= 0)        /* NB: 0 if not known */
807                         dtimperiod = 1;
808                 dtimcount = conf.dtim_count;
809                 if (dtimcount >= dtimperiod)    /* NB: sanity check */
810                         dtimcount = 0;      /* XXX? */
811                 cfpperiod = 1;          /* NB: no PCF support yet */
812                 cfpcount = 0;
813
814                 sleepduration = conf.listen_interval * intval;
815                 if (sleepduration <= 0)
816                         sleepduration = intval;
817
818 #define FUDGE   2
819                 /*
820                  * Pull nexttbtt forward to reflect the current
821                  * TSF and calculate dtim+cfp state for the result.
822                  */
823                 tsf = ath9k_hw_gettsf64(ah);
824                 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
825                 do {
826                         nexttbtt += intval;
827                         if (--dtimcount < 0) {
828                                 dtimcount = dtimperiod - 1;
829                                 if (--cfpcount < 0)
830                                         cfpcount = cfpperiod - 1;
831                         }
832                 } while (nexttbtt < tsftu);
833 #undef FUDGE
834                 memzero(&bs, sizeof(bs));
835                 bs.bs_intval = intval;
836                 bs.bs_nexttbtt = nexttbtt;
837                 bs.bs_dtimperiod = dtimperiod*intval;
838                 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
839                 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
840                 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
841                 bs.bs_cfpmaxduration = 0;
842                 /*
843                  * Calculate the number of consecutive beacons to miss
844                  * before taking a BMISS interrupt.  The configuration
845                  * is specified in TU so we only need calculate based
846                  * on the beacon interval.  Note that we clamp the
847                  * result to at most 15 beacons.
848                  */
849                 if (sleepduration > intval) {
850                         bs.bs_bmissthreshold =
851                                 conf.listen_interval *
852                                         ATH_DEFAULT_BMISS_LIMIT / 2;
853                 } else {
854                         bs.bs_bmissthreshold =
855                                 DIV_ROUND_UP(conf.bmiss_timeout, intval);
856                         if (bs.bs_bmissthreshold > 15)
857                                 bs.bs_bmissthreshold = 15;
858                         else if (bs.bs_bmissthreshold <= 0)
859                                 bs.bs_bmissthreshold = 1;
860                 }
861
862                 /*
863                  * Calculate sleep duration.  The configuration is
864                  * given in ms.  We insure a multiple of the beacon
865                  * period is used.  Also, if the sleep duration is
866                  * greater than the DTIM period then it makes senses
867                  * to make it a multiple of that.
868                  *
869                  * XXX fixed at 100ms
870                  */
871
872                 bs.bs_sleepduration =
873                         roundup(IEEE80211_MS_TO_TU(100), sleepduration);
874                 if (bs.bs_sleepduration > bs.bs_dtimperiod)
875                         bs.bs_sleepduration = bs.bs_dtimperiod;
876
877                 DPRINTF(sc, ATH_DBG_BEACON,
878                         "%s: tsf %llu "
879                         "tsf:tu %u "
880                         "intval %u "
881                         "nexttbtt %u "
882                         "dtim %u "
883                         "nextdtim %u "
884                         "bmiss %u "
885                         "sleep %u "
886                         "cfp:period %u "
887                         "maxdur %u "
888                         "next %u "
889                         "timoffset %u\n"
890                         , __func__
891                         , (unsigned long long)tsf, tsftu
892                         , bs.bs_intval
893                         , bs.bs_nexttbtt
894                         , bs.bs_dtimperiod
895                         , bs.bs_nextdtim
896                         , bs.bs_bmissthreshold
897                         , bs.bs_sleepduration
898                         , bs.bs_cfpperiod
899                         , bs.bs_cfpmaxduration
900                         , bs.bs_cfpnext
901                         , bs.bs_timoffset
902                         );
903
904                 ath9k_hw_set_interrupts(ah, 0);
905                 ath9k_hw_set_sta_beacon_timers(ah, &bs);
906                 sc->sc_imask |= ATH9K_INT_BMISS;
907                 ath9k_hw_set_interrupts(ah, sc->sc_imask);
908         } else {
909                 u64 tsf;
910                 u32 tsftu;
911                 ath9k_hw_set_interrupts(ah, 0);
912                 if (nexttbtt == intval)
913                         intval |= ATH9K_BEACON_RESET_TSF;
914                 if (sc->sc_opmode == ATH9K_M_IBSS) {
915                         /*
916                          * Pull nexttbtt forward to reflect the current
917                          * TSF .
918                          */
919 #define FUDGE   2
920                         if (!(intval & ATH9K_BEACON_RESET_TSF)) {
921                                 tsf = ath9k_hw_gettsf64(ah);
922                                 tsftu = TSF_TO_TU((u32)(tsf>>32),
923                                         (u32)tsf) + FUDGE;
924                                 do {
925                                         nexttbtt += intval;
926                                 } while (nexttbtt < tsftu);
927                         }
928 #undef FUDGE
929                         DPRINTF(sc, ATH_DBG_BEACON,
930                                 "%s: IBSS nexttbtt %u intval %u (%u)\n",
931                                 __func__, nexttbtt,
932                                 intval & ~ATH9K_BEACON_RESET_TSF,
933                                 conf.beacon_interval);
934
935                         /*
936                          * In IBSS mode enable the beacon timers but only
937                          * enable SWBA interrupts if we need to manually
938                          * prepare beacon frames.  Otherwise we use a
939                          * self-linked tx descriptor and let the hardware
940                          * deal with things.
941                          */
942                         intval |= ATH9K_BEACON_ENA;
943                         if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
944                                 sc->sc_imask |= ATH9K_INT_SWBA;
945                         ath_beaconq_config(sc);
946                 } else if (sc->sc_opmode == ATH9K_M_HOSTAP) {
947                         /*
948                          * In AP mode we enable the beacon timers and
949                          * SWBA interrupts to prepare beacon frames.
950                          */
951                         intval |= ATH9K_BEACON_ENA;
952                         sc->sc_imask |= ATH9K_INT_SWBA;   /* beacon prepare */
953                         ath_beaconq_config(sc);
954                 }
955                 ath9k_hw_beaconinit(ah, nexttbtt, intval);
956                 sc->sc_bmisscount = 0;
957                 ath9k_hw_set_interrupts(ah, sc->sc_imask);
958                 /*
959                  * When using a self-linked beacon descriptor in
960                  * ibss mode load it once here.
961                  */
962                 if (sc->sc_opmode == ATH9K_M_IBSS &&
963                     (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
964                         ath_beacon_start_adhoc(sc, 0);
965         }
966 #undef TSF_TO_TU
967 }
968
969 /* Function to collect beacon rssi data and resync beacon if necessary */
970
971 void ath_beacon_sync(struct ath_softc *sc, int if_id)
972 {
973         /*
974          * Resync beacon timers using the tsf of the
975          * beacon frame we just received.
976          */
977         ath_beacon_config(sc, if_id);
978         sc->sc_beacons = 1;
979 }