OSDN Git Service

ath9k: Add IEEE80211_HW_RX_INCLUDES_FCS to hw flags
[uclinux-h8/linux.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_ah->ah_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_ah->ah_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 & SC_OP_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 /* TODO: use ieee80211_get_buffered_bc() to fetch power saved mcast frames */
176 /* This is only run at DTIM. We move everything from the vap's mcast queue
177  * to the hardware cab queue. Caller must hold the mcastq lock. */
178 static void trigger_mcastq(struct ath_hal *ah,
179         struct ath_txq *mcastq, struct ath_txq *cabq)
180 {
181         spin_lock_bh(&cabq->axq_lock);
182
183         if (!list_empty(&mcastq->axq_q))
184                 empty_mcastq_into_cabq(ah, mcastq, cabq);
185
186         /* cabq is gated by beacon so it is safe to start here */
187         if (!list_empty(&cabq->axq_q))
188                 ath9k_hw_txstart(ah, cabq->axq_qnum);
189
190         spin_unlock_bh(&cabq->axq_lock);
191 }
192
193 /*
194  *  Generate beacon frame and queue cab data for a vap.
195  *
196  *  Updates the contents of the beacon frame.  It is assumed that the buffer for
197  *  the beacon frame has been allocated in the ATH object, and simply needs to
198  *  be filled for this cycle.  Also, any CAB (crap after beacon?) traffic will
199  *  be added to the beacon frame at this point.
200 */
201 static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
202 {
203         struct ath_hal *ah = sc->sc_ah;
204         struct ath_buf *bf;
205         struct ath_vap *avp;
206         struct sk_buff *skb;
207         int cabq_depth;
208         int mcastq_depth;
209         int is_beacon_dtim = 0;
210         struct ath_txq *cabq;
211         struct ath_txq *mcastq;
212         struct ieee80211_tx_info *info;
213         avp = sc->sc_vaps[if_id];
214
215         mcastq = &avp->av_mcastq;
216         cabq = sc->sc_cabq;
217
218         ASSERT(avp);
219
220         if (avp->av_bcbuf == NULL) {
221                 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
222                         __func__, avp, avp->av_bcbuf);
223                 return NULL;
224         }
225         bf = avp->av_bcbuf;
226         skb = (struct sk_buff *) bf->bf_mpdu;
227         if (skb) {
228                 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
229                                  skb_end_pointer(skb) - skb->head,
230                                  PCI_DMA_TODEVICE);
231         }
232
233         skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
234         bf->bf_mpdu = skb;
235         if (skb == NULL)
236                 return NULL;
237         info = IEEE80211_SKB_CB(skb);
238         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
239                 /*
240                  * TODO: make sure the seq# gets assigned properly (vs. other
241                  * TX frames)
242                  */
243                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
244                 sc->seq_no += 0x10;
245                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
246                 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
247         }
248         bf->bf_buf_addr = bf->bf_dmacontext =
249                 pci_map_single(sc->pdev, skb->data,
250                                skb_end_pointer(skb) - skb->head,
251                                PCI_DMA_TODEVICE);
252
253         /* TODO: convert to use ieee80211_get_buffered_bc() */
254         /* XXX: spin_lock_bh should not be used here, but sparse bitches
255          * otherwise. We should fix sparse :) */
256         spin_lock_bh(&mcastq->axq_lock);
257         mcastq_depth = avp->av_mcastq.axq_depth;
258
259         /*
260          * if the CABQ traffic from previous DTIM is pending and the current
261          *  beacon is also a DTIM.
262          *  1) if there is only one vap let the cab traffic continue.
263          *  2) if there are more than one vap and we are using staggered
264          *     beacons, then drain the cabq by dropping all the frames in
265          *     the cabq so that the current vaps cab traffic can be scheduled.
266          */
267         spin_lock_bh(&cabq->axq_lock);
268         cabq_depth = cabq->axq_depth;
269         spin_unlock_bh(&cabq->axq_lock);
270
271         if (avp->av_boff.bo_tim)
272                 is_beacon_dtim = avp->av_boff.bo_tim[4] & 1;
273
274         if (mcastq_depth && is_beacon_dtim && cabq_depth) {
275                 /*
276                  * Unlock the cabq lock as ath_tx_draintxq acquires
277                  * the lock again which is a common function and that
278                  * acquires txq lock inside.
279                  */
280                 if (sc->sc_nvaps > 1) {
281                         ath_tx_draintxq(sc, cabq, false);
282                         DPRINTF(sc, ATH_DBG_BEACON,
283                                 "%s: flush previous cabq traffic\n", __func__);
284                 }
285         }
286
287         /* Construct tx descriptor. */
288         ath_beacon_setup(sc, avp, bf);
289
290         /*
291          * Enable the CAB queue before the beacon queue to
292          * insure cab frames are triggered by this beacon.
293          */
294         if (is_beacon_dtim)
295                 trigger_mcastq(ah, mcastq, cabq);
296
297         spin_unlock_bh(&mcastq->axq_lock);
298         return bf;
299 }
300
301 /*
302  * Startup beacon transmission for adhoc mode when they are sent entirely
303  * by the hardware using the self-linked descriptor + veol trick.
304 */
305
306 static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
307 {
308         struct ath_hal *ah = sc->sc_ah;
309         struct ath_buf *bf;
310         struct ath_vap *avp;
311         struct sk_buff *skb;
312
313         avp = sc->sc_vaps[if_id];
314         ASSERT(avp);
315
316         if (avp->av_bcbuf == NULL) {
317                 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
318                         __func__, avp, avp != NULL ? avp->av_bcbuf : NULL);
319                 return;
320         }
321         bf = avp->av_bcbuf;
322         skb = (struct sk_buff *) bf->bf_mpdu;
323
324         /* Construct tx descriptor. */
325         ath_beacon_setup(sc, avp, bf);
326
327         /* NB: caller is known to have already stopped tx dma */
328         ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
329         ath9k_hw_txstart(ah, sc->sc_bhalq);
330         DPRINTF(sc, ATH_DBG_BEACON, "%s: TXDP%u = %llx (%p)\n", __func__,
331                 sc->sc_bhalq, ito64(bf->bf_daddr), bf->bf_desc);
332 }
333
334 /*
335  *  Setup a h/w transmit queue for beacons.
336  *
337  *  This function allocates an information structure (struct ath9k_txq_info)
338  *  on the stack, sets some specific parameters (zero out channel width
339  *  min/max, and enable aifs). The info structure does not need to be
340  *  persistant.
341 */
342
343 int ath_beaconq_setup(struct ath_hal *ah)
344 {
345         struct ath9k_tx_queue_info qi;
346
347         memzero(&qi, sizeof(qi));
348         qi.tqi_aifs = 1;
349         qi.tqi_cwmin = 0;
350         qi.tqi_cwmax = 0;
351         /* NB: don't enable any interrupts */
352         return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
353 }
354
355
356 /*
357  *  Allocate and setup an initial beacon frame.
358  *
359  *  Allocate a beacon state variable for a specific VAP instance created on
360  *  the ATH interface.  This routine also calculates the beacon "slot" for
361  *  staggared beacons in the mBSSID case.
362 */
363
364 int ath_beacon_alloc(struct ath_softc *sc, int if_id)
365 {
366         struct ath_vap *avp;
367         struct ieee80211_hdr *wh;
368         struct ath_buf *bf;
369         struct sk_buff *skb;
370
371         avp = sc->sc_vaps[if_id];
372         ASSERT(avp);
373
374         /* Allocate a beacon descriptor if we haven't done so. */
375         if (!avp->av_bcbuf) {
376                 /*
377                  * Allocate beacon state for hostap/ibss.  We know
378                  * a buffer is available.
379                  */
380
381                 avp->av_bcbuf = list_first_entry(&sc->sc_bbuf,
382                                 struct ath_buf, list);
383                 list_del(&avp->av_bcbuf->list);
384
385                 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
386                     !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
387                         int slot;
388                         /*
389                          * Assign the vap to a beacon xmit slot. As
390                          * above, this cannot fail to find one.
391                          */
392                         avp->av_bslot = 0;
393                         for (slot = 0; slot < ATH_BCBUF; slot++)
394                                 if (sc->sc_bslot[slot] == ATH_IF_ID_ANY) {
395                                         /*
396                                          * XXX hack, space out slots to better
397                                          * deal with misses
398                                          */
399                                         if (slot+1 < ATH_BCBUF &&
400                                             sc->sc_bslot[slot+1] ==
401                                                 ATH_IF_ID_ANY) {
402                                                 avp->av_bslot = slot+1;
403                                                 break;
404                                         }
405                                         avp->av_bslot = slot;
406                                         /* NB: keep looking for a double slot */
407                                 }
408                         BUG_ON(sc->sc_bslot[avp->av_bslot] != ATH_IF_ID_ANY);
409                         sc->sc_bslot[avp->av_bslot] = if_id;
410                         sc->sc_nbcnvaps++;
411                 }
412         }
413
414         /* release the previous beacon frame , if it already exists. */
415         bf = avp->av_bcbuf;
416         if (bf->bf_mpdu != NULL) {
417                 skb = (struct sk_buff *)bf->bf_mpdu;
418                 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
419                                  skb_end_pointer(skb) - skb->head,
420                                  PCI_DMA_TODEVICE);
421                 dev_kfree_skb_any(skb);
422                 bf->bf_mpdu = NULL;
423         }
424
425         /*
426          * NB: the beacon data buffer must be 32-bit aligned;
427          * we assume the wbuf routines will return us something
428          * with this alignment (perhaps should assert).
429          * FIXME: Fill avp->av_boff.bo_tim,avp->av_btxctl.txpower and
430          * avp->av_btxctl.shortPreamble
431          */
432         skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
433         if (skb == NULL) {
434                 DPRINTF(sc, ATH_DBG_BEACON, "%s: cannot get skb\n",
435                         __func__);
436                 return -ENOMEM;
437         }
438
439         /*
440          * Calculate a TSF adjustment factor required for
441          * staggered beacons.  Note that we assume the format
442          * of the beacon frame leaves the tstamp field immediately
443          * following the header.
444          */
445         if (avp->av_bslot > 0) {
446                 u64 tsfadjust;
447                 __le64 val;
448                 int intval;
449
450                 intval = sc->hw->conf.beacon_int ?
451                         sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
452
453                 /*
454                  * The beacon interval is in TU's; the TSF in usecs.
455                  * We figure out how many TU's to add to align the
456                  * timestamp then convert to TSF units and handle
457                  * byte swapping before writing it in the frame.
458                  * The hardware will then add this each time a beacon
459                  * frame is sent.  Note that we align vap's 1..N
460                  * and leave vap 0 untouched.  This means vap 0
461                  * has a timestamp in one beacon interval while the
462                  * others get a timestamp aligned to the next interval.
463                  */
464                 tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
465                 val = cpu_to_le64(tsfadjust << 10);     /* TU->TSF */
466
467                 DPRINTF(sc, ATH_DBG_BEACON,
468                         "%s: %s beacons, bslot %d intval %u tsfadjust %llu\n",
469                         __func__, "stagger",
470                         avp->av_bslot, intval, (unsigned long long)tsfadjust);
471
472                 wh = (struct ieee80211_hdr *)skb->data;
473                 memcpy(&wh[1], &val, sizeof(val));
474         }
475
476         bf->bf_buf_addr = bf->bf_dmacontext =
477                 pci_map_single(sc->pdev, skb->data,
478                                skb_end_pointer(skb) - skb->head,
479                                PCI_DMA_TODEVICE);
480         bf->bf_mpdu = skb;
481
482         return 0;
483 }
484
485 /*
486  *  Reclaim beacon resources and return buffer to the pool.
487  *
488  *  Checks the VAP to put the beacon frame buffer back to the ATH object
489  *  queue, and de-allocates any wbuf frames that were sent as CAB traffic.
490 */
491
492 void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
493 {
494         if (avp->av_bcbuf != NULL) {
495                 struct ath_buf *bf;
496
497                 if (avp->av_bslot != -1) {
498                         sc->sc_bslot[avp->av_bslot] = ATH_IF_ID_ANY;
499                         sc->sc_nbcnvaps--;
500                 }
501
502                 bf = avp->av_bcbuf;
503                 if (bf->bf_mpdu != NULL) {
504                         struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
505                         pci_unmap_single(sc->pdev, bf->bf_dmacontext,
506                                          skb_end_pointer(skb) - skb->head,
507                                          PCI_DMA_TODEVICE);
508                         dev_kfree_skb_any(skb);
509                         bf->bf_mpdu = NULL;
510                 }
511                 list_add_tail(&bf->list, &sc->sc_bbuf);
512
513                 avp->av_bcbuf = NULL;
514         }
515 }
516
517 /*
518  *  Reclaim beacon resources and return buffer to the pool.
519  *
520  *  This function will free any wbuf frames that are still attached to the
521  *  beacon buffers in the ATH object.  Note that this does not de-allocate
522  *  any wbuf objects that are in the transmit queue and have not yet returned
523  *  to the ATH object.
524 */
525
526 void ath_beacon_free(struct ath_softc *sc)
527 {
528         struct ath_buf *bf;
529
530         list_for_each_entry(bf, &sc->sc_bbuf, list) {
531                 if (bf->bf_mpdu != NULL) {
532                         struct sk_buff *skb = (struct sk_buff *) bf->bf_mpdu;
533                         pci_unmap_single(sc->pdev, bf->bf_dmacontext,
534                                          skb_end_pointer(skb) - skb->head,
535                                          PCI_DMA_TODEVICE);
536                         dev_kfree_skb_any(skb);
537                         bf->bf_mpdu = NULL;
538                 }
539         }
540 }
541
542 /*
543  * Tasklet for Sending Beacons
544  *
545  * Transmit one or more beacon frames at SWBA.  Dynamic updates to the frame
546  * contents are done as needed and the slot time is also adjusted based on
547  * current state.
548  *
549  * This tasklet is not scheduled, it's called in ISR context.
550 */
551
552 void ath9k_beacon_tasklet(unsigned long data)
553 {
554         struct ath_softc *sc = (struct ath_softc *)data;
555         struct ath_hal *ah = sc->sc_ah;
556         struct ath_buf *bf = NULL;
557         int slot, if_id;
558         u32 bfaddr;
559         u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
560         u32 show_cycles = 0;
561         u32 bc = 0; /* beacon count */
562         u64 tsf;
563         u32 tsftu;
564         u16 intval;
565
566         if (sc->sc_flags & SC_OP_NO_RESET) {
567                 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
568                                                             &rx_clear,
569                                                             &rx_frame,
570                                                             &tx_frame);
571         }
572
573         /*
574          * Check if the previous beacon has gone out.  If
575          * not don't try to post another, skip this period
576          * and wait for the next.  Missed beacons indicate
577          * a problem and should not occur.  If we miss too
578          * many consecutive beacons reset the device.
579          */
580         if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
581                 sc->sc_bmisscount++;
582                 /* XXX: doth needs the chanchange IE countdown decremented.
583                  *      We should consider adding a mac80211 call to indicate
584                  *      a beacon miss so appropriate action could be taken
585                  *      (in that layer).
586                  */
587                 if (sc->sc_bmisscount < BSTUCK_THRESH) {
588                         if (sc->sc_flags & SC_OP_NO_RESET) {
589                                 DPRINTF(sc, ATH_DBG_BEACON,
590                                         "%s: missed %u consecutive beacons\n",
591                                         __func__, sc->sc_bmisscount);
592                                 if (show_cycles) {
593                                         /*
594                                          * Display cycle counter stats
595                                          * from HW to aide in debug of
596                                          * stickiness.
597                                          */
598                                         DPRINTF(sc,
599                                                 ATH_DBG_BEACON,
600                                                 "%s: busy times: rx_clear=%d, "
601                                                 "rx_frame=%d, tx_frame=%d\n",
602                                                 __func__, rx_clear, rx_frame,
603                                                 tx_frame);
604                                 } else {
605                                         DPRINTF(sc,
606                                                 ATH_DBG_BEACON,
607                                                 "%s: unable to obtain "
608                                                 "busy times\n", __func__);
609                                 }
610                         } else {
611                                 DPRINTF(sc, ATH_DBG_BEACON,
612                                         "%s: missed %u consecutive beacons\n",
613                                         __func__, sc->sc_bmisscount);
614                         }
615                 } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
616                         if (sc->sc_flags & SC_OP_NO_RESET) {
617                                 if (sc->sc_bmisscount == BSTUCK_THRESH) {
618                                         DPRINTF(sc,
619                                                 ATH_DBG_BEACON,
620                                                 "%s: beacon is officially "
621                                                 "stuck\n", __func__);
622                                         ath9k_hw_dmaRegDump(ah);
623                                 }
624                         } else {
625                                 DPRINTF(sc, ATH_DBG_BEACON,
626                                         "%s: beacon is officially stuck\n",
627                                         __func__);
628                                 ath_bstuck_process(sc);
629                         }
630                 }
631
632                 return;
633         }
634         if (sc->sc_bmisscount != 0) {
635                 if (sc->sc_flags & SC_OP_NO_RESET) {
636                         DPRINTF(sc,
637                                 ATH_DBG_BEACON,
638                                 "%s: resume beacon xmit after %u misses\n",
639                                 __func__, sc->sc_bmisscount);
640                 } else {
641                         DPRINTF(sc, ATH_DBG_BEACON,
642                                 "%s: resume beacon xmit after %u misses\n",
643                                 __func__, sc->sc_bmisscount);
644                 }
645                 sc->sc_bmisscount = 0;
646         }
647
648         /*
649          * Generate beacon frames. we are sending frames
650          * staggered so calculate the slot for this frame based
651          * on the tsf to safeguard against missing an swba.
652          */
653
654         intval = sc->hw->conf.beacon_int ?
655                 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
656
657         tsf = ath9k_hw_gettsf64(ah);
658         tsftu = TSF_TO_TU(tsf>>32, tsf);
659         slot = ((tsftu % intval) * ATH_BCBUF) / intval;
660         if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
661         DPRINTF(sc, ATH_DBG_BEACON,
662                         "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
663                         __func__, slot, (unsigned long long) tsf, tsftu,
664                         intval, if_id);
665         bfaddr = 0;
666         if (if_id != ATH_IF_ID_ANY) {
667                 bf = ath_beacon_generate(sc, if_id);
668                 if (bf != NULL) {
669                         bfaddr = bf->bf_daddr;
670                         bc = 1;
671                 }
672         }
673         /*
674          * Handle slot time change when a non-ERP station joins/leaves
675          * an 11g network.  The 802.11 layer notifies us via callback,
676          * we mark updateslot, then wait one beacon before effecting
677          * the change.  This gives associated stations at least one
678          * beacon interval to note the state change.
679          *
680          * NB: The slot time change state machine is clocked according
681          *     to whether we are bursting or staggering beacons.  We
682          *     recognize the request to update and record the current
683          *     slot then don't transition until that slot is reached
684          *     again.  If we miss a beacon for that slot then we'll be
685          *     slow to transition but we'll be sure at least one beacon
686          *     interval has passed.  When bursting slot is always left
687          *     set to ATH_BCBUF so this check is a noop.
688          */
689         /* XXX locking */
690         if (sc->sc_updateslot == UPDATE) {
691                 sc->sc_updateslot = COMMIT; /* commit next beacon */
692                 sc->sc_slotupdate = slot;
693         } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
694                 ath_setslottime(sc);        /* commit change to hardware */
695
696         if (bfaddr != 0) {
697                 /*
698                  * Stop any current dma and put the new frame(s) on the queue.
699                  * This should never fail since we check above that no frames
700                  * are still pending on the queue.
701                  */
702                 if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
703                         DPRINTF(sc, ATH_DBG_FATAL,
704                                 "%s: beacon queue %u did not stop?\n",
705                                 __func__, sc->sc_bhalq);
706                         /* NB: the HAL still stops DMA, so proceed */
707                 }
708
709                 /* NB: cabq traffic should already be queued and primed */
710                 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
711                 ath9k_hw_txstart(ah, sc->sc_bhalq);
712
713                 sc->ast_be_xmit += bc;     /* XXX per-vap? */
714         }
715 }
716
717 /*
718  *  Tasklet for Beacon Stuck processing
719  *
720  *  Processing for Beacon Stuck.
721  *  Basically calls the ath_internal_reset function to reset the chip.
722 */
723
724 void ath_bstuck_process(struct ath_softc *sc)
725 {
726         DPRINTF(sc, ATH_DBG_BEACON,
727                 "%s: stuck beacon; resetting (bmiss count %u)\n",
728                 __func__, sc->sc_bmisscount);
729         ath_reset(sc, false);
730 }
731
732 /*
733  * Configure the beacon and sleep timers.
734  *
735  * When operating as an AP this resets the TSF and sets
736  * up the hardware to notify us when we need to issue beacons.
737  *
738  * When operating in station mode this sets up the beacon
739  * timers according to the timestamp of the last received
740  * beacon and the current TSF, configures PCF and DTIM
741  * handling, programs the sleep registers so the hardware
742  * will wakeup in time to receive beacons, and configures
743  * the beacon miss handling so we'll receive a BMISS
744  * interrupt when we stop seeing beacons from the AP
745  * we've associated with.
746  */
747
748 void ath_beacon_config(struct ath_softc *sc, int if_id)
749 {
750         struct ath_hal *ah = sc->sc_ah;
751         u32 nexttbtt, intval;
752         struct ath_beacon_config conf;
753         enum ath9k_opmode av_opmode;
754
755         if (if_id != ATH_IF_ID_ANY)
756                 av_opmode = sc->sc_vaps[if_id]->av_opmode;
757         else
758                 av_opmode = sc->sc_ah->ah_opmode;
759
760         memzero(&conf, sizeof(struct ath_beacon_config));
761
762         /* FIXME: Use default values for now - Sujith */
763         /* Query beacon configuration first */
764         /*
765          * Protocol stack doesn't support dynamic beacon configuration,
766          * use default configurations.
767          */
768         conf.beacon_interval = sc->hw->conf.beacon_int ?
769                 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
770         conf.listen_interval = 1;
771         conf.dtim_period = conf.beacon_interval;
772         conf.dtim_count = 1;
773         conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
774
775         /* extract tstamp from last beacon and convert to TU */
776         nexttbtt = TSF_TO_TU(get_unaligned_le32(conf.u.last_tstamp + 4),
777                              get_unaligned_le32(conf.u.last_tstamp));
778         /* XXX conditionalize multi-bss support? */
779         if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
780                 /*
781                  * For multi-bss ap support beacons are either staggered
782                  * evenly over N slots or burst together.  For the former
783                  * arrange for the SWBA to be delivered for each slot.
784                  * Slots that are not occupied will generate nothing.
785                  */
786                 /* NB: the beacon interval is kept internally in TU's */
787                 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
788                 intval /= ATH_BCBUF;    /* for staggered beacons */
789         } else {
790                 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
791         }
792
793         if (nexttbtt == 0)      /* e.g. for ap mode */
794                 nexttbtt = intval;
795         else if (intval)        /* NB: can be 0 for monitor mode */
796                 nexttbtt = roundup(nexttbtt, intval);
797         DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
798                 __func__, nexttbtt, intval, conf.beacon_interval);
799         /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
800         if (sc->sc_ah->ah_opmode == ATH9K_M_STA) {
801                 struct ath9k_beacon_state bs;
802                 u64 tsf;
803                 u32 tsftu;
804                 int dtimperiod, dtimcount, sleepduration;
805                 int cfpperiod, cfpcount;
806
807                 /*
808                  * Setup dtim and cfp parameters according to
809                  * last beacon we received (which may be none).
810                  */
811                 dtimperiod = conf.dtim_period;
812                 if (dtimperiod <= 0)        /* NB: 0 if not known */
813                         dtimperiod = 1;
814                 dtimcount = conf.dtim_count;
815                 if (dtimcount >= dtimperiod)    /* NB: sanity check */
816                         dtimcount = 0;      /* XXX? */
817                 cfpperiod = 1;          /* NB: no PCF support yet */
818                 cfpcount = 0;
819
820                 sleepduration = conf.listen_interval * intval;
821                 if (sleepduration <= 0)
822                         sleepduration = intval;
823
824 #define FUDGE   2
825                 /*
826                  * Pull nexttbtt forward to reflect the current
827                  * TSF and calculate dtim+cfp state for the result.
828                  */
829                 tsf = ath9k_hw_gettsf64(ah);
830                 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
831                 do {
832                         nexttbtt += intval;
833                         if (--dtimcount < 0) {
834                                 dtimcount = dtimperiod - 1;
835                                 if (--cfpcount < 0)
836                                         cfpcount = cfpperiod - 1;
837                         }
838                 } while (nexttbtt < tsftu);
839 #undef FUDGE
840                 memzero(&bs, sizeof(bs));
841                 bs.bs_intval = intval;
842                 bs.bs_nexttbtt = nexttbtt;
843                 bs.bs_dtimperiod = dtimperiod*intval;
844                 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
845                 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
846                 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
847                 bs.bs_cfpmaxduration = 0;
848                 /*
849                  * Calculate the number of consecutive beacons to miss
850                  * before taking a BMISS interrupt.  The configuration
851                  * is specified in TU so we only need calculate based
852                  * on the beacon interval.  Note that we clamp the
853                  * result to at most 15 beacons.
854                  */
855                 if (sleepduration > intval) {
856                         bs.bs_bmissthreshold =
857                                 conf.listen_interval *
858                                         ATH_DEFAULT_BMISS_LIMIT / 2;
859                 } else {
860                         bs.bs_bmissthreshold =
861                                 DIV_ROUND_UP(conf.bmiss_timeout, intval);
862                         if (bs.bs_bmissthreshold > 15)
863                                 bs.bs_bmissthreshold = 15;
864                         else if (bs.bs_bmissthreshold <= 0)
865                                 bs.bs_bmissthreshold = 1;
866                 }
867
868                 /*
869                  * Calculate sleep duration.  The configuration is
870                  * given in ms.  We insure a multiple of the beacon
871                  * period is used.  Also, if the sleep duration is
872                  * greater than the DTIM period then it makes senses
873                  * to make it a multiple of that.
874                  *
875                  * XXX fixed at 100ms
876                  */
877
878                 bs.bs_sleepduration =
879                         roundup(IEEE80211_MS_TO_TU(100), sleepduration);
880                 if (bs.bs_sleepduration > bs.bs_dtimperiod)
881                         bs.bs_sleepduration = bs.bs_dtimperiod;
882
883                 DPRINTF(sc, ATH_DBG_BEACON,
884                         "%s: tsf %llu "
885                         "tsf:tu %u "
886                         "intval %u "
887                         "nexttbtt %u "
888                         "dtim %u "
889                         "nextdtim %u "
890                         "bmiss %u "
891                         "sleep %u "
892                         "cfp:period %u "
893                         "maxdur %u "
894                         "next %u "
895                         "timoffset %u\n",
896                         __func__,
897                         (unsigned long long)tsf, tsftu,
898                         bs.bs_intval,
899                         bs.bs_nexttbtt,
900                         bs.bs_dtimperiod,
901                         bs.bs_nextdtim,
902                         bs.bs_bmissthreshold,
903                         bs.bs_sleepduration,
904                         bs.bs_cfpperiod,
905                         bs.bs_cfpmaxduration,
906                         bs.bs_cfpnext,
907                         bs.bs_timoffset
908                         );
909
910                 ath9k_hw_set_interrupts(ah, 0);
911                 ath9k_hw_set_sta_beacon_timers(ah, &bs);
912                 sc->sc_imask |= ATH9K_INT_BMISS;
913                 ath9k_hw_set_interrupts(ah, sc->sc_imask);
914         } else {
915                 u64 tsf;
916                 u32 tsftu;
917                 ath9k_hw_set_interrupts(ah, 0);
918                 if (nexttbtt == intval)
919                         intval |= ATH9K_BEACON_RESET_TSF;
920                 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
921                         /*
922                          * Pull nexttbtt forward to reflect the current
923                          * TSF .
924                          */
925 #define FUDGE   2
926                         if (!(intval & ATH9K_BEACON_RESET_TSF)) {
927                                 tsf = ath9k_hw_gettsf64(ah);
928                                 tsftu = TSF_TO_TU((u32)(tsf>>32),
929                                         (u32)tsf) + FUDGE;
930                                 do {
931                                         nexttbtt += intval;
932                                 } while (nexttbtt < tsftu);
933                         }
934 #undef FUDGE
935                         DPRINTF(sc, ATH_DBG_BEACON,
936                                 "%s: IBSS nexttbtt %u intval %u (%u)\n",
937                                 __func__, nexttbtt,
938                                 intval & ~ATH9K_BEACON_RESET_TSF,
939                                 conf.beacon_interval);
940
941                         /*
942                          * In IBSS mode enable the beacon timers but only
943                          * enable SWBA interrupts if we need to manually
944                          * prepare beacon frames.  Otherwise we use a
945                          * self-linked tx descriptor and let the hardware
946                          * deal with things.
947                          */
948                         intval |= ATH9K_BEACON_ENA;
949                         if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
950                                 sc->sc_imask |= ATH9K_INT_SWBA;
951                         ath_beaconq_config(sc);
952                 } else if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
953                         /*
954                          * In AP mode we enable the beacon timers and
955                          * SWBA interrupts to prepare beacon frames.
956                          */
957                         intval |= ATH9K_BEACON_ENA;
958                         sc->sc_imask |= ATH9K_INT_SWBA;   /* beacon prepare */
959                         ath_beaconq_config(sc);
960                 }
961                 ath9k_hw_beaconinit(ah, nexttbtt, intval);
962                 sc->sc_bmisscount = 0;
963                 ath9k_hw_set_interrupts(ah, sc->sc_imask);
964                 /*
965                  * When using a self-linked beacon descriptor in
966                  * ibss mode load it once here.
967                  */
968                 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
969                     (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
970                         ath_beacon_start_adhoc(sc, 0);
971         }
972 }
973
974 /* Function to collect beacon rssi data and resync beacon if necessary */
975
976 void ath_beacon_sync(struct ath_softc *sc, int if_id)
977 {
978         /*
979          * Resync beacon timers using the tsf of the
980          * beacon frame we just received.
981          */
982         ath_beacon_config(sc, if_id);
983         sc->sc_flags |= SC_OP_BEACONS;
984 }