]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/ath9k/beacon.c
ath9k: Miscellaneous fixes
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / ath9k / beacon.c
CommitLineData
f078f209
LR
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
30static int ath_beaconq_config(struct ath_softc *sc)
31{
32 struct ath_hal *ah = sc->sc_ah;
ea9880fb 33 struct ath9k_tx_queue_info qi;
f078f209 34
ea9880fb 35 ath9k_hw_get_txq_props(ah, sc->sc_bhalq, &qi);
b4696c8b 36 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
f078f209
LR
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
ea9880fb 48 if (!ath9k_hw_set_txq_props(ah, sc->sc_bhalq, &qi)) {
f078f209
LR
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
67static 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
b4696c8b 88 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
60b67f51 89 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
f078f209
LR
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 */
86b89eed 111 rix = 0;
f078f209
LR
112 rt = sc->sc_currates;
113 rate = rt->info[rix].rateCode;
672840ac 114 if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
f078f209
LR
115 rate |= rt->info[rix].shortPreamble;
116
ff9b662d
S
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 */
f078f209
LR
124 );
125
126 /* NB: beacon's BufLen must be a multiple of 4 bytes */
ff9b662d
S
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 */
f078f209
LR
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 */
147static 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
a8fff50e 175/* TODO: use ieee80211_get_buffered_bc() to fetch power saved mcast frames */
f078f209
LR
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. */
178static 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*/
201static 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;
f078f209
LR
210 struct ath_txq *cabq;
211 struct ath_txq *mcastq;
147583c0 212 struct ieee80211_tx_info *info;
f078f209
LR
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;
a8fff50e
JM
227 if (skb) {
228 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
229 skb_end_pointer(skb) - skb->head,
230 PCI_DMA_TODEVICE);
231 }
f078f209 232
a8fff50e
JM
233 skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
234 bf->bf_mpdu = skb;
235 if (skb == NULL)
236 return NULL;
147583c0
JM
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 }
a8fff50e
JM
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);
f078f209 252
a8fff50e 253 /* TODO: convert to use ieee80211_get_buffered_bc() */
f078f209
LR
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
f078f209
LR
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
a8fff50e
JM
271 if (avp->av_boff.bo_tim)
272 is_beacon_dtim = avp->av_boff.bo_tim[4] & 1;
f078f209
LR
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
306static 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
343int ath_beaconq_setup(struct ath_hal *ah)
344{
ea9880fb 345 struct ath9k_tx_queue_info qi;
f078f209
LR
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
364int 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
b4696c8b 385 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
60b67f51 386 !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
f078f209
LR
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;
a8fff50e
JM
418 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
419 skb_end_pointer(skb) - skb->head,
420 PCI_DMA_TODEVICE);
f078f209
LR
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
a8fff50e
JM
450 intval = sc->hw->conf.beacon_int ?
451 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
f078f209
LR
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
a8fff50e
JM
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);
f078f209
LR
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
492void 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;
a8fff50e
JM
505 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
506 skb_end_pointer(skb) - skb->head,
507 PCI_DMA_TODEVICE);
f078f209
LR
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
526void 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;
a8fff50e
JM
533 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
534 skb_end_pointer(skb) - skb->head,
535 PCI_DMA_TODEVICE);
f078f209
LR
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
552void ath9k_beacon_tasklet(unsigned long data)
553{
ff9b662d 554#define TSF_TO_TU(_h,_l) \
f078f209
LR
555 ((((u32)(_h)) << 22) | (((u32)(_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 u32 bfaddr;
562 u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
563 u32 show_cycles = 0;
564 u32 bc = 0; /* beacon count */
565 u64 tsf;
566 u32 tsftu;
567 u16 intval;
568
98deeea0 569 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209
LR
570 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
571 &rx_clear,
572 &rx_frame,
573 &tx_frame);
574 }
575
576 /*
577 * Check if the previous beacon has gone out. If
578 * not don't try to post another, skip this period
579 * and wait for the next. Missed beacons indicate
580 * a problem and should not occur. If we miss too
581 * many consecutive beacons reset the device.
582 */
583 if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
584 sc->sc_bmisscount++;
585 /* XXX: doth needs the chanchange IE countdown decremented.
586 * We should consider adding a mac80211 call to indicate
587 * a beacon miss so appropriate action could be taken
588 * (in that layer).
589 */
590 if (sc->sc_bmisscount < BSTUCK_THRESH) {
98deeea0 591 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209
LR
592 DPRINTF(sc, ATH_DBG_BEACON,
593 "%s: missed %u consecutive beacons\n",
594 __func__, sc->sc_bmisscount);
595 if (show_cycles) {
596 /*
597 * Display cycle counter stats
598 * from HW to aide in debug of
599 * stickiness.
600 */
601 DPRINTF(sc,
602 ATH_DBG_BEACON,
603 "%s: busy times: rx_clear=%d, "
604 "rx_frame=%d, tx_frame=%d\n",
605 __func__, rx_clear, rx_frame,
606 tx_frame);
607 } else {
608 DPRINTF(sc,
609 ATH_DBG_BEACON,
610 "%s: unable to obtain "
611 "busy times\n", __func__);
612 }
613 } else {
614 DPRINTF(sc, ATH_DBG_BEACON,
615 "%s: missed %u consecutive beacons\n",
616 __func__, sc->sc_bmisscount);
617 }
618 } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
98deeea0 619 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209
LR
620 if (sc->sc_bmisscount == BSTUCK_THRESH) {
621 DPRINTF(sc,
622 ATH_DBG_BEACON,
623 "%s: beacon is officially "
624 "stuck\n", __func__);
625 ath9k_hw_dmaRegDump(ah);
626 }
627 } else {
628 DPRINTF(sc, ATH_DBG_BEACON,
629 "%s: beacon is officially stuck\n",
630 __func__);
631 ath_bstuck_process(sc);
632 }
633 }
634
635 return;
636 }
637 if (sc->sc_bmisscount != 0) {
98deeea0 638 if (sc->sc_flags & SC_OP_NO_RESET) {
f078f209
LR
639 DPRINTF(sc,
640 ATH_DBG_BEACON,
641 "%s: resume beacon xmit after %u misses\n",
642 __func__, sc->sc_bmisscount);
643 } else {
644 DPRINTF(sc, ATH_DBG_BEACON,
645 "%s: resume beacon xmit after %u misses\n",
646 __func__, sc->sc_bmisscount);
647 }
648 sc->sc_bmisscount = 0;
649 }
650
651 /*
652 * Generate beacon frames. we are sending frames
653 * staggered so calculate the slot for this frame based
654 * on the tsf to safeguard against missing an swba.
655 */
656
a8fff50e
JM
657 intval = sc->hw->conf.beacon_int ?
658 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
f078f209
LR
659
660 tsf = ath9k_hw_gettsf64(ah);
661 tsftu = TSF_TO_TU(tsf>>32, tsf);
662 slot = ((tsftu % intval) * ATH_BCBUF) / intval;
663 if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
664 DPRINTF(sc, ATH_DBG_BEACON,
665 "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
666 __func__, slot, (unsigned long long) tsf, tsftu,
667 intval, if_id);
668 bfaddr = 0;
669 if (if_id != ATH_IF_ID_ANY) {
670 bf = ath_beacon_generate(sc, if_id);
671 if (bf != NULL) {
672 bfaddr = bf->bf_daddr;
673 bc = 1;
674 }
675 }
676 /*
677 * Handle slot time change when a non-ERP station joins/leaves
678 * an 11g network. The 802.11 layer notifies us via callback,
679 * we mark updateslot, then wait one beacon before effecting
680 * the change. This gives associated stations at least one
681 * beacon interval to note the state change.
682 *
683 * NB: The slot time change state machine is clocked according
684 * to whether we are bursting or staggering beacons. We
685 * recognize the request to update and record the current
686 * slot then don't transition until that slot is reached
687 * again. If we miss a beacon for that slot then we'll be
688 * slow to transition but we'll be sure at least one beacon
689 * interval has passed. When bursting slot is always left
690 * set to ATH_BCBUF so this check is a noop.
691 */
692 /* XXX locking */
693 if (sc->sc_updateslot == UPDATE) {
694 sc->sc_updateslot = COMMIT; /* commit next beacon */
695 sc->sc_slotupdate = slot;
696 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
697 ath_setslottime(sc); /* commit change to hardware */
698
699 if (bfaddr != 0) {
700 /*
701 * Stop any current dma and put the new frame(s) on the queue.
702 * This should never fail since we check above that no frames
703 * are still pending on the queue.
704 */
705 if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
706 DPRINTF(sc, ATH_DBG_FATAL,
707 "%s: beacon queue %u did not stop?\n",
708 __func__, sc->sc_bhalq);
709 /* NB: the HAL still stops DMA, so proceed */
710 }
711
712 /* NB: cabq traffic should already be queued and primed */
713 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
714 ath9k_hw_txstart(ah, sc->sc_bhalq);
715
716 sc->ast_be_xmit += bc; /* XXX per-vap? */
717 }
718#undef TSF_TO_TU
719}
720
721/*
722 * Tasklet for Beacon Stuck processing
723 *
724 * Processing for Beacon Stuck.
725 * Basically calls the ath_internal_reset function to reset the chip.
726*/
727
728void ath_bstuck_process(struct ath_softc *sc)
729{
730 DPRINTF(sc, ATH_DBG_BEACON,
731 "%s: stuck beacon; resetting (bmiss count %u)\n",
732 __func__, sc->sc_bmisscount);
f45144ef 733 ath_reset(sc, false);
f078f209
LR
734}
735
736/*
737 * Configure the beacon and sleep timers.
738 *
739 * When operating as an AP this resets the TSF and sets
740 * up the hardware to notify us when we need to issue beacons.
741 *
742 * When operating in station mode this sets up the beacon
743 * timers according to the timestamp of the last received
744 * beacon and the current TSF, configures PCF and DTIM
745 * handling, programs the sleep registers so the hardware
746 * will wakeup in time to receive beacons, and configures
747 * the beacon miss handling so we'll receive a BMISS
748 * interrupt when we stop seeing beacons from the AP
749 * we've associated with.
750 */
751
752void ath_beacon_config(struct ath_softc *sc, int if_id)
753{
754#define TSF_TO_TU(_h,_l) \
755 ((((u32)(_h)) << 22) | (((u32)(_l)) >> 10))
756 struct ath_hal *ah = sc->sc_ah;
757 u32 nexttbtt, intval;
758 struct ath_beacon_config conf;
759 enum ath9k_opmode av_opmode;
760
761 if (if_id != ATH_IF_ID_ANY)
762 av_opmode = sc->sc_vaps[if_id]->av_opmode;
763 else
b4696c8b 764 av_opmode = sc->sc_ah->ah_opmode;
f078f209
LR
765
766 memzero(&conf, sizeof(struct ath_beacon_config));
767
768 /* FIXME: Use default values for now - Sujith */
769 /* Query beacon configuration first */
770 /*
771 * Protocol stack doesn't support dynamic beacon configuration,
772 * use default configurations.
773 */
a8fff50e
JM
774 conf.beacon_interval = sc->hw->conf.beacon_int ?
775 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
f078f209
LR
776 conf.listen_interval = 1;
777 conf.dtim_period = conf.beacon_interval;
778 conf.dtim_count = 1;
779 conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
780
781 /* extract tstamp from last beacon and convert to TU */
782 nexttbtt = TSF_TO_TU(get_unaligned_le32(conf.u.last_tstamp + 4),
783 get_unaligned_le32(conf.u.last_tstamp));
784 /* XXX conditionalize multi-bss support? */
b4696c8b 785 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
f078f209
LR
786 /*
787 * For multi-bss ap support beacons are either staggered
788 * evenly over N slots or burst together. For the former
789 * arrange for the SWBA to be delivered for each slot.
790 * Slots that are not occupied will generate nothing.
791 */
792 /* NB: the beacon interval is kept internally in TU's */
793 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
794 intval /= ATH_BCBUF; /* for staggered beacons */
795 } else {
796 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
797 }
798
799 if (nexttbtt == 0) /* e.g. for ap mode */
800 nexttbtt = intval;
801 else if (intval) /* NB: can be 0 for monitor mode */
802 nexttbtt = roundup(nexttbtt, intval);
803 DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
804 __func__, nexttbtt, intval, conf.beacon_interval);
805 /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
b4696c8b 806 if (sc->sc_ah->ah_opmode == ATH9K_M_STA) {
f078f209
LR
807 struct ath9k_beacon_state bs;
808 u64 tsf;
809 u32 tsftu;
810 int dtimperiod, dtimcount, sleepduration;
811 int cfpperiod, cfpcount;
812
813 /*
814 * Setup dtim and cfp parameters according to
815 * last beacon we received (which may be none).
816 */
817 dtimperiod = conf.dtim_period;
818 if (dtimperiod <= 0) /* NB: 0 if not known */
819 dtimperiod = 1;
820 dtimcount = conf.dtim_count;
821 if (dtimcount >= dtimperiod) /* NB: sanity check */
822 dtimcount = 0; /* XXX? */
823 cfpperiod = 1; /* NB: no PCF support yet */
824 cfpcount = 0;
825
826 sleepduration = conf.listen_interval * intval;
827 if (sleepduration <= 0)
828 sleepduration = intval;
829
830#define FUDGE 2
831 /*
832 * Pull nexttbtt forward to reflect the current
833 * TSF and calculate dtim+cfp state for the result.
834 */
835 tsf = ath9k_hw_gettsf64(ah);
836 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
837 do {
838 nexttbtt += intval;
839 if (--dtimcount < 0) {
840 dtimcount = dtimperiod - 1;
841 if (--cfpcount < 0)
842 cfpcount = cfpperiod - 1;
843 }
844 } while (nexttbtt < tsftu);
845#undef FUDGE
846 memzero(&bs, sizeof(bs));
847 bs.bs_intval = intval;
848 bs.bs_nexttbtt = nexttbtt;
849 bs.bs_dtimperiod = dtimperiod*intval;
850 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
851 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
852 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
853 bs.bs_cfpmaxduration = 0;
854 /*
855 * Calculate the number of consecutive beacons to miss
856 * before taking a BMISS interrupt. The configuration
857 * is specified in TU so we only need calculate based
858 * on the beacon interval. Note that we clamp the
859 * result to at most 15 beacons.
860 */
861 if (sleepduration > intval) {
862 bs.bs_bmissthreshold =
863 conf.listen_interval *
864 ATH_DEFAULT_BMISS_LIMIT / 2;
865 } else {
866 bs.bs_bmissthreshold =
867 DIV_ROUND_UP(conf.bmiss_timeout, intval);
868 if (bs.bs_bmissthreshold > 15)
869 bs.bs_bmissthreshold = 15;
870 else if (bs.bs_bmissthreshold <= 0)
871 bs.bs_bmissthreshold = 1;
872 }
873
874 /*
875 * Calculate sleep duration. The configuration is
876 * given in ms. We insure a multiple of the beacon
877 * period is used. Also, if the sleep duration is
878 * greater than the DTIM period then it makes senses
879 * to make it a multiple of that.
880 *
881 * XXX fixed at 100ms
882 */
883
884 bs.bs_sleepduration =
885 roundup(IEEE80211_MS_TO_TU(100), sleepduration);
886 if (bs.bs_sleepduration > bs.bs_dtimperiod)
887 bs.bs_sleepduration = bs.bs_dtimperiod;
888
889 DPRINTF(sc, ATH_DBG_BEACON,
890 "%s: tsf %llu "
891 "tsf:tu %u "
892 "intval %u "
893 "nexttbtt %u "
894 "dtim %u "
895 "nextdtim %u "
896 "bmiss %u "
897 "sleep %u "
898 "cfp:period %u "
899 "maxdur %u "
900 "next %u "
ff9b662d
S
901 "timoffset %u\n",
902 __func__,
903 (unsigned long long)tsf, tsftu,
904 bs.bs_intval,
905 bs.bs_nexttbtt,
906 bs.bs_dtimperiod,
907 bs.bs_nextdtim,
908 bs.bs_bmissthreshold,
909 bs.bs_sleepduration,
910 bs.bs_cfpperiod,
911 bs.bs_cfpmaxduration,
912 bs.bs_cfpnext,
913 bs.bs_timoffset
f078f209
LR
914 );
915
916 ath9k_hw_set_interrupts(ah, 0);
917 ath9k_hw_set_sta_beacon_timers(ah, &bs);
918 sc->sc_imask |= ATH9K_INT_BMISS;
919 ath9k_hw_set_interrupts(ah, sc->sc_imask);
920 } else {
921 u64 tsf;
922 u32 tsftu;
923 ath9k_hw_set_interrupts(ah, 0);
924 if (nexttbtt == intval)
925 intval |= ATH9K_BEACON_RESET_TSF;
b4696c8b 926 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
f078f209
LR
927 /*
928 * Pull nexttbtt forward to reflect the current
929 * TSF .
930 */
931#define FUDGE 2
932 if (!(intval & ATH9K_BEACON_RESET_TSF)) {
933 tsf = ath9k_hw_gettsf64(ah);
934 tsftu = TSF_TO_TU((u32)(tsf>>32),
935 (u32)tsf) + FUDGE;
936 do {
937 nexttbtt += intval;
938 } while (nexttbtt < tsftu);
939 }
940#undef FUDGE
941 DPRINTF(sc, ATH_DBG_BEACON,
942 "%s: IBSS nexttbtt %u intval %u (%u)\n",
943 __func__, nexttbtt,
944 intval & ~ATH9K_BEACON_RESET_TSF,
945 conf.beacon_interval);
946
947 /*
948 * In IBSS mode enable the beacon timers but only
949 * enable SWBA interrupts if we need to manually
950 * prepare beacon frames. Otherwise we use a
951 * self-linked tx descriptor and let the hardware
952 * deal with things.
953 */
954 intval |= ATH9K_BEACON_ENA;
60b67f51 955 if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
f078f209
LR
956 sc->sc_imask |= ATH9K_INT_SWBA;
957 ath_beaconq_config(sc);
b4696c8b 958 } else if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
f078f209
LR
959 /*
960 * In AP mode we enable the beacon timers and
961 * SWBA interrupts to prepare beacon frames.
962 */
963 intval |= ATH9K_BEACON_ENA;
964 sc->sc_imask |= ATH9K_INT_SWBA; /* beacon prepare */
965 ath_beaconq_config(sc);
966 }
967 ath9k_hw_beaconinit(ah, nexttbtt, intval);
968 sc->sc_bmisscount = 0;
969 ath9k_hw_set_interrupts(ah, sc->sc_imask);
970 /*
971 * When using a self-linked beacon descriptor in
972 * ibss mode load it once here.
973 */
b4696c8b 974 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
60b67f51 975 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
f078f209
LR
976 ath_beacon_start_adhoc(sc, 0);
977 }
978#undef TSF_TO_TU
979}
980
981/* Function to collect beacon rssi data and resync beacon if necessary */
982
983void ath_beacon_sync(struct ath_softc *sc, int if_id)
984{
985 /*
986 * Resync beacon timers using the tsf of the
987 * beacon frame we just received.
988 */
989 ath_beacon_config(sc, if_id);
672840ac 990 sc->sc_flags |= SC_OP_BEACONS;
f078f209 991}