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