2 * Copyright (c) 2008 Atheros Communications Inc.
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.
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.
17 /* Implementation of beacon processing. */
22 * Configure parameters for the beacon queue
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
29 static int ath_beaconq_config(struct ath_softc
*sc
)
31 struct ath_hal
*ah
= sc
->sc_ah
;
32 struct hal_txq_info qi
;
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. */
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
;
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",
53 ath9k_hw_resettxqueue(ah
, sc
->sc_bhalq
); /* push to h/w */
59 * Setup the beacon frame for transmit.
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.
66 static void ath_beacon_setup(struct ath_softc
*sc
,
67 struct ath_vap
*avp
, struct ath_buf
*bf
)
69 struct sk_buff
*skb
= (struct sk_buff
*)bf
->bf_mpdu
;
70 struct ath_hal
*ah
= sc
->sc_ah
;
73 const struct hal_rate_table
*rt
;
77 struct hal_11n_rate_series series
[4];
79 DPRINTF(sc
, ATH_DEBUG_BEACON
, "%s: m %p len %u\n",
80 __func__
, skb
, skb
->len
);
82 /* setup descriptors */
85 flags
= HAL_TXDESC_NOACK
;
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. */
95 * Switch antenna every beacon.
96 * Should only switch every beacon period, not for every
98 * XXX assumes two antenna
100 if (sc
->sc_stagbeacons
)
101 antenna
= ((sc
->ast_be_xmit
/
102 sc
->sc_nbcnvaps
) & 1 ? 2 : 1);
104 antenna
= (sc
->ast_be_xmit
& 1 ? 2 : 1);
107 ds
->ds_data
= bf
->bf_buf_addr
;
110 * Calculate rate code.
111 * XXX everything at min xmit rate
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
;
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 */
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 */
136 memzero(series
, sizeof(struct hal_11n_rate_series
) * 4);
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);
145 /* Move everything from the vap's mcast queue to the hardware cab queue.
146 * Caller must hold mcasq lock and cabq lock
149 static void empty_mcastq_into_cabq(struct ath_hal
*ah
,
150 struct ath_txq
*mcastq
, struct ath_txq
*cabq
)
152 struct ath_buf
*bfmcast
;
154 BUG_ON(list_empty(&mcastq
->axq_q
));
156 bfmcast
= list_first_entry(&mcastq
->axq_q
, struct ath_buf
, list
);
158 /* link the descriptors */
160 ath9k_hw_puttxbuf(ah
, cabq
->axq_qnum
, bfmcast
->bf_daddr
);
162 *cabq
->axq_link
= bfmcast
->bf_daddr
;
164 /* append the private vap mcast list to the cabq */
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
;
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
)
182 spin_lock_bh(&cabq
->axq_lock
);
184 if (!list_empty(&mcastq
->axq_q
))
185 empty_mcastq_into_cabq(ah
, mcastq
, cabq
);
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
);
191 spin_unlock_bh(&cabq
->axq_lock
);
195 * Generate beacon frame and queue cab data for a vap.
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.
202 static struct ath_buf
*ath_beacon_generate(struct ath_softc
*sc
, int if_id
)
204 struct ath_hal
*ah
= sc
->sc_ah
;
210 int is_beacon_dtim
= 0;
212 struct ath_txq
*cabq
;
213 struct ath_txq
*mcastq
;
214 avp
= sc
->sc_vaps
[if_id
];
216 mcastq
= &avp
->av_mcastq
;
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
);
227 skb
= (struct sk_buff
*) bf
->bf_mpdu
;
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).
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
;
242 if (ath_update_beacon(sc
, if_id
, &avp
->av_boff
, skb
, mcastq_depth
) ==
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
));
249 pci_dma_sync_single_for_cpu(sc
->pdev
,
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.
263 spin_lock_bh(&cabq
->axq_lock
);
264 cabq_depth
= cabq
->axq_depth
;
265 spin_unlock_bh(&cabq
->axq_lock
);
267 is_beacon_dtim
= avp
->av_boff
.bo_tim
[4] & 1;
269 if (mcastq_depth
&& is_beacon_dtim
&& cabq_depth
) {
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.
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__
);
282 /* Construct tx descriptor. */
283 ath_beacon_setup(sc
, avp
, bf
);
286 * Enable the CAB queue before the beacon queue to
287 * insure cab frames are triggered by this beacon.
290 trigger_mcastq(ah
, mcastq
, cabq
);
292 spin_unlock_bh(&mcastq
->axq_lock
);
297 * Startup beacon transmission for adhoc mode when they are sent entirely
298 * by the hardware using the self-linked descriptor + veol trick.
301 static void ath_beacon_start_adhoc(struct ath_softc
*sc
, int if_id
)
303 struct ath_hal
*ah
= sc
->sc_ah
;
308 avp
= sc
->sc_vaps
[if_id
];
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
);
317 skb
= (struct sk_buff
*) bf
->bf_mpdu
;
319 /* Construct tx descriptor. */
320 ath_beacon_setup(sc
, avp
, bf
);
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
);
330 * Setup a h/w transmit queue for beacons.
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
338 int ath_beaconq_setup(struct ath_hal
*ah
)
340 struct hal_txq_info qi
;
342 memzero(&qi
, sizeof(qi
));
346 /* NB: don't enable any interrupts */
347 return ath9k_hw_setuptxqueue(ah
, HAL_TX_QUEUE_BEACON
, &qi
);
352 * Allocate and setup an initial beacon frame.
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.
359 int ath_beacon_alloc(struct ath_softc
*sc
, int if_id
)
362 struct ieee80211_hdr
*wh
;
366 avp
= sc
->sc_vaps
[if_id
];
369 /* Allocate a beacon descriptor if we haven't done so. */
370 if (!avp
->av_bcbuf
) {
372 * Allocate beacon state for hostap/ibss. We know
373 * a buffer is available.
376 avp
->av_bcbuf
= list_first_entry(&sc
->sc_bbuf
,
377 struct ath_buf
, list
);
378 list_del(&avp
->av_bcbuf
->list
);
380 if (sc
->sc_opmode
== HAL_M_HOSTAP
|| !sc
->sc_hasveol
) {
383 * Assign the vap to a beacon xmit slot. As
384 * above, this cannot fail to find one.
387 for (slot
= 0; slot
< ATH_BCBUF
; slot
++)
388 if (sc
->sc_bslot
[slot
] == ATH_IF_ID_ANY
) {
390 * XXX hack, space out slots to better
393 if (slot
+1 < ATH_BCBUF
&&
394 sc
->sc_bslot
[slot
+1] ==
396 avp
->av_bslot
= slot
+1;
399 avp
->av_bslot
= slot
;
400 /* NB: keep looking for a double slot */
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
;
409 /* release the previous beacon frame , if it already exists. */
411 if (bf
->bf_mpdu
!= NULL
) {
412 struct ath_xmit_status tx_status
;
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
));
418 tx_status
.retries
= 0;
419 ath_tx_complete(sc
, skb
, &tx_status
, NULL
);
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).
428 skb
= ath_get_beacon(sc
, if_id
, &avp
->av_boff
, &avp
->av_btxctl
);
430 DPRINTF(sc
, ATH_DEBUG_BEACON
, "%s: cannot get skb\n",
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.
441 if (sc
->sc_stagbeacons
&& avp
->av_bslot
> 0) {
445 /* FIXME: Use default value for now: Sujith */
447 intval
= ATH_DEFAULT_BINTVAL
;
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.
460 tsfadjust
= (intval
* (ATH_BCBUF
- avp
->av_bslot
)) / ATH_BCBUF
;
461 tsfadjust
= cpu_to_le64(tsfadjust
<<10); /* TU->TSF */
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
);
468 wh
= (struct ieee80211_hdr
*)skb
->data
;
469 memcpy(&wh
[1], &tsfadjust
, sizeof(tsfadjust
));
472 bf
->bf_buf_addr
= ath_skb_map_single(sc
, skb
, PCI_DMA_TODEVICE
,
473 get_dma_mem_context(bf
, bf_dmacontext
));
480 * Reclaim beacon resources and return buffer to the pool.
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.
486 void ath_beacon_return(struct ath_softc
*sc
, struct ath_vap
*avp
)
488 if (avp
->av_bcbuf
!= NULL
) {
491 if (avp
->av_bslot
!= -1) {
492 sc
->sc_bslot
[avp
->av_bslot
] = ATH_IF_ID_ANY
;
497 if (bf
->bf_mpdu
!= NULL
) {
498 struct sk_buff
*skb
= (struct sk_buff
*) bf
->bf_mpdu
;
499 struct ath_xmit_status tx_status
;
501 ath_skb_unmap_single(sc
, skb
, PCI_DMA_TODEVICE
,
502 get_dma_mem_context(bf
, bf_dmacontext
));
504 tx_status
.retries
= 0;
505 ath_tx_complete(sc
, skb
, &tx_status
, NULL
);
508 list_add_tail(&bf
->list
, &sc
->sc_bbuf
);
510 avp
->av_bcbuf
= NULL
;
515 * Reclaim beacon resources and return buffer to the pool.
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
523 void ath_beacon_free(struct ath_softc
*sc
)
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
;
532 ath_skb_unmap_single(sc
, skb
, PCI_DMA_TODEVICE
,
533 get_dma_mem_context(bf
, bf_dmacontext
));
535 tx_status
.retries
= 0;
536 ath_tx_complete(sc
, skb
, &tx_status
, NULL
);
543 * Tasklet for Sending Beacons
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
549 * This tasklet is not scheduled, it's called in ISR context.
552 void ath9k_beacon_tasklet(unsigned long data
)
554 #define TSF_TO_TU(_h,_l) \
555 ((((u_int32_t)(_h)) << 22) | (((u_int32_t)(_l)) >> 10))
557 struct ath_softc
*sc
= (struct ath_softc
*)data
;
558 struct ath_hal
*ah
= sc
->sc_ah
;
559 struct ath_buf
*bf
= NULL
;
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 */
566 if (sc
->sc_noreset
) {
567 show_cycles
= ath9k_hw_GetMibCycleCountsPct(ah
,
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.
580 if (ath9k_hw_numtxpending(ah
, sc
->sc_bhalq
) != 0) {
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
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
);
594 * Display cycle counter stats
595 * from HW to aide in debug of
600 "%s: busy times: rx_clear=%d, "
601 "rx_frame=%d, tx_frame=%d\n",
602 __func__
, rx_clear
, rx_frame
,
607 "%s: unable to obtain "
608 "busy times\n", __func__
);
611 DPRINTF(sc
, ATH_DEBUG_BEACON
,
612 "%s: missed %u consecutive beacons\n",
613 __func__
, sc
->sc_bmisscount
);
615 } else if (sc
->sc_bmisscount
>= BSTUCK_THRESH
) {
616 if (sc
->sc_noreset
) {
617 if (sc
->sc_bmisscount
== BSTUCK_THRESH
) {
620 "%s: beacon is officially "
621 "stuck\n", __func__
);
622 ath9k_hw_dmaRegDump(ah
);
625 DPRINTF(sc
, ATH_DEBUG_BEACON
,
626 "%s: beacon is officially stuck\n",
628 ath_bstuck_process(sc
);
634 if (sc
->sc_bmisscount
!= 0) {
635 if (sc
->sc_noreset
) {
638 "%s: resume beacon xmit after %u misses\n",
639 __func__
, sc
->sc_bmisscount
);
641 DPRINTF(sc
, ATH_DEBUG_BEACON
,
642 "%s: resume beacon xmit after %u misses\n",
643 __func__
, sc
->sc_bmisscount
);
645 sc
->sc_bmisscount
= 0;
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.
655 if (sc
->sc_stagbeacons
) {
656 /* staggered beacons */
661 /* FIXME: Use default value for now - Sujith */
662 intval
= ATH_DEFAULT_BINTVAL
;
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
,
673 if (if_id
!= ATH_IF_ID_ANY
) {
674 bf
= ath_beacon_generate(sc
, if_id
);
676 bfaddr
= bf
->bf_daddr
;
681 /* XXX: Clean this up, move work to a helper */
682 /* burst'd beacons */
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
);
691 *bflink
= bf
->bf_daddr
;
692 bflink
= &bf
->bf_desc
->ds_link
;
697 *bflink
= 0; /* link of last frame */
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.
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.
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 */
722 if ((!sc
->sc_stagbeacons
|| slot
== 0) && (!sc
->sc_diversity
)) {
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
729 * XXX assumes 2 anntenae
731 otherant
= sc
->sc_defant
& 1 ? 2 : 1;
732 if (sc
->sc_ant_tx
[otherant
] > sc
->sc_ant_tx
[sc
->sc_defant
] +
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
);
740 sc
->sc_ant_tx
[1] = sc
->sc_ant_tx
[2] = 0;
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.
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 */
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
);
760 sc
->ast_be_xmit
+= bc
; /* XXX per-vap? */
766 * Tasklet for Beacon Stuck processing
768 * Processing for Beacon Stuck.
769 * Basically calls the ath_internal_reset function to reset the chip.
772 void ath_bstuck_process(struct ath_softc
*sc
)
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
);
781 * Configure the beacon and sleep timers.
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.
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.
796 void ath_beacon_config(struct ath_softc
*sc
, int if_id
)
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
;
805 if (if_id
!= ATH_IF_ID_ANY
)
806 av_opmode
= sc
->sc_vaps
[if_id
]->av_opmode
;
808 av_opmode
= sc
->sc_opmode
;
810 memzero(&conf
, sizeof(struct ath_beacon_config
));
812 /* FIXME: Use default values for now - Sujith */
813 /* Query beacon configuration first */
815 * Protocol stack doesn't support dynamic beacon configuration,
816 * use default configurations.
818 conf
.beacon_interval
= ATH_DEFAULT_BINTVAL
;
819 conf
.listen_interval
= 1;
820 conf
.dtim_period
= conf
.beacon_interval
;
822 conf
.bmiss_timeout
= ATH_DEFAULT_BMISS_LIMIT
* conf
.beacon_interval
;
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
) {
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.
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
))
843 intval
= conf
.beacon_interval
& HAL_BEACON_PERIOD
;
846 if (nexttbtt
== 0) /* e.g. for ap mode */
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
;
860 int dtimperiod
, dtimcount
, sleepduration
;
861 int cfpperiod
, cfpcount
;
864 * Setup dtim and cfp parameters according to
865 * last beacon we received (which may be none).
867 dtimperiod
= conf
.dtim_period
;
868 if (dtimperiod
<= 0) /* NB: 0 if not known */
870 dtimcount
= conf
.dtim_count
;
871 if (dtimcount
>= dtimperiod
) /* NB: sanity check */
872 dtimcount
= 0; /* XXX? */
873 cfpperiod
= 1; /* NB: no PCF support yet */
876 sleepduration
= conf
.listen_interval
* intval
;
877 if (sleepduration
<= 0)
878 sleepduration
= intval
;
882 * Pull nexttbtt forward to reflect the current
883 * TSF and calculate dtim+cfp state for the result.
885 tsf
= ath9k_hw_gettsf64(ah
);
886 tsftu
= TSF_TO_TU(tsf
>>32, tsf
) + FUDGE
;
889 if (--dtimcount
< 0) {
890 dtimcount
= dtimperiod
- 1;
892 cfpcount
= cfpperiod
- 1;
894 } while (nexttbtt
< tsftu
);
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;
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.
911 if (sleepduration
> intval
) {
912 bs
.bs_bmissthreshold
=
913 conf
.listen_interval
*
914 ATH_DEFAULT_BMISS_LIMIT
/ 2;
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;
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.
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
;
939 DPRINTF(sc
, ATH_DEBUG_BEACON
,
953 , (unsigned long long)tsf
, tsftu
958 , bs
.bs_bmissthreshold
959 , bs
.bs_sleepduration
961 , bs
.bs_cfpmaxduration
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
);
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
) {
980 * Pull nexttbtt forward to reflect the current
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
;
990 } while (nexttbtt
< tsftu
);
993 DPRINTF(sc
, ATH_DEBUG_BEACON
,
994 "%s: IBSS nexttbtt %u intval %u (%u)\n",
996 intval
& ~HAL_BEACON_RESET_TSF
,
997 conf
.beacon_interval
);
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
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
) {
1012 * In AP mode we enable the beacon timers and
1013 * SWBA interrupts to prepare beacon frames.
1015 intval
|= HAL_BEACON_ENA
;
1016 sc
->sc_imask
|= HAL_INT_SWBA
; /* beacon prepare */
1017 ath_beaconq_config(sc
);
1019 ath9k_hw_beaconinit(ah
, nexttbtt
, intval
);
1020 sc
->sc_bmisscount
= 0;
1021 ath9k_hw_set_interrupts(ah
, sc
->sc_imask
);
1023 * When using a self-linked beacon descriptor in
1024 * ibss mode load it once here.
1026 if (sc
->sc_opmode
== HAL_M_IBSS
&& sc
->sc_hasveol
)
1027 ath_beacon_start_adhoc(sc
, 0);
1032 /* Function to collect beacon rssi data and resync beacon if necessary */
1034 void ath_beacon_sync(struct ath_softc
*sc
, int if_id
)
1037 * Resync beacon timers using the tsf of the
1038 * beacon frame we just received.
1040 ath_beacon_config(sc
, if_id
);