blob: f8d952ca3f8599250704b6db0ba699a43076d03e [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
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*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070029static int ath_beaconq_config(struct ath_softc *sc)
30{
31 struct ath_hal *ah = sc->sc_ah;
Sujithea9880f2008-08-07 10:53:10 +053032 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070033
Sujithea9880f2008-08-07 10:53:10 +053034 ath9k_hw_get_txq_props(ah, sc->sc_bhalq, &qi);
Sujithb4696c8b2008-08-11 14:04:52 +053035 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070036 /* 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
Sujithea9880f2008-08-07 10:53:10 +053047 if (!ath9k_hw_set_txq_props(ah, sc->sc_bhalq, &qi)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070048 DPRINTF(sc, ATH_DBG_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*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070065static void ath_beacon_setup(struct ath_softc *sc,
Sujith980b24d2008-09-17 10:15:09 +053066 struct ath_vap *avp, struct ath_buf *bf)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070067{
68 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
69 struct ath_hal *ah = sc->sc_ah;
70 struct ath_desc *ds;
Sujith980b24d2008-09-17 10:15:09 +053071 struct ath9k_11n_rate_series series[4];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070072 const struct ath9k_rate_table *rt;
Sujith980b24d2008-09-17 10:15:09 +053073 int flags, antenna;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070074 u8 rix, rate;
75 int ctsrate = 0;
76 int ctsduration = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070077
78 DPRINTF(sc, ATH_DBG_BEACON, "%s: m %p len %u\n",
79 __func__, skb, skb->len);
80
81 /* setup descriptors */
82 ds = bf->bf_desc;
83
84 flags = ATH9K_TXDESC_NOACK;
85
Sujithb4696c8b2008-08-11 14:04:52 +053086 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
Sujith60b67f52008-08-07 10:52:38 +053087 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070088 ds->ds_link = bf->bf_daddr; /* self-linked */
89 flags |= ATH9K_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 antenna = ((sc->ast_be_xmit / sc->sc_nbcnvaps) & 1 ? 2 : 1);
101 }
102
103 ds->ds_data = bf->bf_buf_addr;
104
105 /*
106 * Calculate rate code.
107 * XXX everything at min xmit rate
108 */
Sujith86b89ee2008-08-07 10:54:57 +0530109 rix = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700110 rt = sc->sc_currates;
111 rate = rt->info[rix].rateCode;
Sujith672840a2008-08-11 14:05:08 +0530112 if (sc->sc_flags & SC_OP_PREAMBLE_SHORT)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700113 rate |= rt->info[rix].shortPreamble;
114
Sujithff9b6622008-08-14 13:27:16 +0530115 ath9k_hw_set11n_txdesc(ah, ds,
Sujith980b24d2008-09-17 10:15:09 +0530116 skb->len + FCS_LEN, /* frame length */
117 ATH9K_PKT_TYPE_BEACON, /* Atheros packet type */
Sujithff9b6622008-08-14 13:27:16 +0530118 avp->av_btxctl.txpower, /* txpower XXX */
Sujith980b24d2008-09-17 10:15:09 +0530119 ATH9K_TXKEYIX_INVALID, /* no encryption */
120 ATH9K_KEY_TYPE_CLEAR, /* no encryption */
121 flags /* no ack,
122 veol for beacons */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700123 );
124
125 /* NB: beacon's BufLen must be a multiple of 4 bytes */
Sujithff9b6622008-08-14 13:27:16 +0530126 ath9k_hw_filltxdesc(ah, ds,
127 roundup(skb->len, 4), /* buffer length */
Sujith980b24d2008-09-17 10:15:09 +0530128 true, /* first segment */
129 true, /* last segment */
130 ds /* first descriptor */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700131 );
132
133 memzero(series, sizeof(struct ath9k_11n_rate_series) * 4);
134 series[0].Tries = 1;
135 series[0].Rate = rate;
136 series[0].ChSel = sc->sc_tx_chainmask;
137 series[0].RateFlags = (ctsrate) ? ATH9K_RATESERIES_RTS_CTS : 0;
138 ath9k_hw_set11n_ratescenario(ah, ds, ds, 0,
139 ctsrate, ctsduration, series, 4, 0);
140}
141
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700142/*
143 * Generate beacon frame and queue cab data for a vap.
144 *
145 * Updates the contents of the beacon frame. It is assumed that the buffer for
146 * the beacon frame has been allocated in the ATH object, and simply needs to
147 * be filled for this cycle. Also, any CAB (crap after beacon?) traffic will
148 * be added to the beacon frame at this point.
149*/
150static struct ath_buf *ath_beacon_generate(struct ath_softc *sc, int if_id)
151{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700152 struct ath_buf *bf;
153 struct ath_vap *avp;
154 struct sk_buff *skb;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700155 struct ath_txq *cabq;
Jouni Malinen147583c2008-08-11 14:01:50 +0300156 struct ieee80211_tx_info *info;
Sujith980b24d2008-09-17 10:15:09 +0530157 int cabq_depth;
158
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700159 avp = sc->sc_vaps[if_id];
Sujith980b24d2008-09-17 10:15:09 +0530160 ASSERT(avp);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700161
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700162 cabq = sc->sc_cabq;
163
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700164 if (avp->av_bcbuf == NULL) {
165 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
166 __func__, avp, avp->av_bcbuf);
167 return NULL;
168 }
Sujith980b24d2008-09-17 10:15:09 +0530169
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700170 bf = avp->av_bcbuf;
Sujith980b24d2008-09-17 10:15:09 +0530171 skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300172 if (skb) {
173 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
174 skb_end_pointer(skb) - skb->head,
175 PCI_DMA_TODEVICE);
176 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700177
Jouni Malinena8fff502008-08-11 14:01:48 +0300178 skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
179 bf->bf_mpdu = skb;
180 if (skb == NULL)
181 return NULL;
Sujith980b24d2008-09-17 10:15:09 +0530182
Jouni Malinen147583c2008-08-11 14:01:50 +0300183 info = IEEE80211_SKB_CB(skb);
184 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
185 /*
186 * TODO: make sure the seq# gets assigned properly (vs. other
187 * TX frames)
188 */
Sujith980b24d2008-09-17 10:15:09 +0530189 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Jouni Malinen147583c2008-08-11 14:01:50 +0300190 sc->seq_no += 0x10;
191 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
192 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
193 }
Sujith980b24d2008-09-17 10:15:09 +0530194
Jouni Malinena8fff502008-08-11 14:01:48 +0300195 bf->bf_buf_addr = bf->bf_dmacontext =
196 pci_map_single(sc->pdev, skb->data,
197 skb_end_pointer(skb) - skb->head,
198 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700199
Jouni Malinene022edb2008-08-22 17:31:33 +0300200 skb = ieee80211_get_buffered_bc(sc->hw, avp->av_if_data);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700201
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700202 /*
203 * if the CABQ traffic from previous DTIM is pending and the current
204 * beacon is also a DTIM.
205 * 1) if there is only one vap let the cab traffic continue.
206 * 2) if there are more than one vap and we are using staggered
207 * beacons, then drain the cabq by dropping all the frames in
208 * the cabq so that the current vaps cab traffic can be scheduled.
209 */
210 spin_lock_bh(&cabq->axq_lock);
211 cabq_depth = cabq->axq_depth;
212 spin_unlock_bh(&cabq->axq_lock);
213
Jouni Malinene022edb2008-08-22 17:31:33 +0300214 if (skb && cabq_depth) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700215 /*
216 * Unlock the cabq lock as ath_tx_draintxq acquires
217 * the lock again which is a common function and that
218 * acquires txq lock inside.
219 */
220 if (sc->sc_nvaps > 1) {
221 ath_tx_draintxq(sc, cabq, false);
222 DPRINTF(sc, ATH_DBG_BEACON,
223 "%s: flush previous cabq traffic\n", __func__);
224 }
225 }
226
227 /* Construct tx descriptor. */
228 ath_beacon_setup(sc, avp, bf);
229
230 /*
231 * Enable the CAB queue before the beacon queue to
232 * insure cab frames are triggered by this beacon.
233 */
Jouni Malinene022edb2008-08-22 17:31:33 +0300234 while (skb) {
235 ath_tx_cabq(sc, skb);
236 skb = ieee80211_get_buffered_bc(sc->hw, avp->av_if_data);
237 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700238
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700239 return bf;
240}
241
242/*
243 * Startup beacon transmission for adhoc mode when they are sent entirely
244 * by the hardware using the self-linked descriptor + veol trick.
245*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700246static void ath_beacon_start_adhoc(struct ath_softc *sc, int if_id)
247{
248 struct ath_hal *ah = sc->sc_ah;
249 struct ath_buf *bf;
250 struct ath_vap *avp;
251 struct sk_buff *skb;
252
253 avp = sc->sc_vaps[if_id];
254 ASSERT(avp);
255
256 if (avp->av_bcbuf == NULL) {
257 DPRINTF(sc, ATH_DBG_BEACON, "%s: avp=%p av_bcbuf=%p\n",
258 __func__, avp, avp != NULL ? avp->av_bcbuf : NULL);
259 return;
260 }
261 bf = avp->av_bcbuf;
262 skb = (struct sk_buff *) bf->bf_mpdu;
263
264 /* Construct tx descriptor. */
265 ath_beacon_setup(sc, avp, bf);
266
267 /* NB: caller is known to have already stopped tx dma */
268 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bf->bf_daddr);
269 ath9k_hw_txstart(ah, sc->sc_bhalq);
270 DPRINTF(sc, ATH_DBG_BEACON, "%s: TXDP%u = %llx (%p)\n", __func__,
271 sc->sc_bhalq, ito64(bf->bf_daddr), bf->bf_desc);
272}
273
274/*
275 * Setup a h/w transmit queue for beacons.
276 *
277 * This function allocates an information structure (struct ath9k_txq_info)
278 * on the stack, sets some specific parameters (zero out channel width
279 * min/max, and enable aifs). The info structure does not need to be
280 * persistant.
281*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700282int ath_beaconq_setup(struct ath_hal *ah)
283{
Sujithea9880f2008-08-07 10:53:10 +0530284 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700285
286 memzero(&qi, sizeof(qi));
287 qi.tqi_aifs = 1;
288 qi.tqi_cwmin = 0;
289 qi.tqi_cwmax = 0;
290 /* NB: don't enable any interrupts */
291 return ath9k_hw_setuptxqueue(ah, ATH9K_TX_QUEUE_BEACON, &qi);
292}
293
294
295/*
296 * Allocate and setup an initial beacon frame.
297 *
298 * Allocate a beacon state variable for a specific VAP instance created on
299 * the ATH interface. This routine also calculates the beacon "slot" for
300 * staggared beacons in the mBSSID case.
301*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700302int ath_beacon_alloc(struct ath_softc *sc, int if_id)
303{
304 struct ath_vap *avp;
Sujith980b24d2008-09-17 10:15:09 +0530305 struct ieee80211_hdr *hdr;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700306 struct ath_buf *bf;
307 struct sk_buff *skb;
308
309 avp = sc->sc_vaps[if_id];
310 ASSERT(avp);
311
312 /* Allocate a beacon descriptor if we haven't done so. */
313 if (!avp->av_bcbuf) {
Sujith980b24d2008-09-17 10:15:09 +0530314 /* Allocate beacon state for hostap/ibss. We know
315 * a buffer is available. */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700316
317 avp->av_bcbuf = list_first_entry(&sc->sc_bbuf,
Sujith980b24d2008-09-17 10:15:09 +0530318 struct ath_buf, list);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700319 list_del(&avp->av_bcbuf->list);
320
Sujithb4696c8b2008-08-11 14:04:52 +0530321 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
Sujith60b67f52008-08-07 10:52:38 +0530322 !(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700323 int slot;
324 /*
325 * Assign the vap to a beacon xmit slot. As
326 * above, this cannot fail to find one.
327 */
328 avp->av_bslot = 0;
329 for (slot = 0; slot < ATH_BCBUF; slot++)
330 if (sc->sc_bslot[slot] == ATH_IF_ID_ANY) {
331 /*
332 * XXX hack, space out slots to better
333 * deal with misses
334 */
335 if (slot+1 < ATH_BCBUF &&
336 sc->sc_bslot[slot+1] ==
337 ATH_IF_ID_ANY) {
338 avp->av_bslot = slot+1;
339 break;
340 }
341 avp->av_bslot = slot;
342 /* NB: keep looking for a double slot */
343 }
344 BUG_ON(sc->sc_bslot[avp->av_bslot] != ATH_IF_ID_ANY);
345 sc->sc_bslot[avp->av_bslot] = if_id;
346 sc->sc_nbcnvaps++;
347 }
348 }
349
350 /* release the previous beacon frame , if it already exists. */
351 bf = avp->av_bcbuf;
352 if (bf->bf_mpdu != NULL) {
353 skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300354 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
355 skb_end_pointer(skb) - skb->head,
356 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700357 dev_kfree_skb_any(skb);
358 bf->bf_mpdu = NULL;
359 }
360
361 /*
Sujith980b24d2008-09-17 10:15:09 +0530362 * NB: the beacon data buffer must be 32-bit aligned.
Jouni Malinene022edb2008-08-22 17:31:33 +0300363 * FIXME: Fill avp->av_btxctl.txpower and
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700364 * avp->av_btxctl.shortPreamble
365 */
366 skb = ieee80211_beacon_get(sc->hw, avp->av_if_data);
367 if (skb == NULL) {
368 DPRINTF(sc, ATH_DBG_BEACON, "%s: cannot get skb\n",
369 __func__);
370 return -ENOMEM;
371 }
372
373 /*
374 * Calculate a TSF adjustment factor required for
375 * staggered beacons. Note that we assume the format
376 * of the beacon frame leaves the tstamp field immediately
377 * following the header.
378 */
379 if (avp->av_bslot > 0) {
380 u64 tsfadjust;
381 __le64 val;
382 int intval;
383
Jouni Malinena8fff502008-08-11 14:01:48 +0300384 intval = sc->hw->conf.beacon_int ?
385 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700386
387 /*
388 * The beacon interval is in TU's; the TSF in usecs.
389 * We figure out how many TU's to add to align the
390 * timestamp then convert to TSF units and handle
391 * byte swapping before writing it in the frame.
392 * The hardware will then add this each time a beacon
393 * frame is sent. Note that we align vap's 1..N
394 * and leave vap 0 untouched. This means vap 0
395 * has a timestamp in one beacon interval while the
396 * others get a timestamp aligned to the next interval.
397 */
398 tsfadjust = (intval * (ATH_BCBUF - avp->av_bslot)) / ATH_BCBUF;
399 val = cpu_to_le64(tsfadjust << 10); /* TU->TSF */
400
401 DPRINTF(sc, ATH_DBG_BEACON,
402 "%s: %s beacons, bslot %d intval %u tsfadjust %llu\n",
403 __func__, "stagger",
404 avp->av_bslot, intval, (unsigned long long)tsfadjust);
405
Sujith980b24d2008-09-17 10:15:09 +0530406 hdr = (struct ieee80211_hdr *)skb->data;
407 memcpy(&hdr[1], &val, sizeof(val));
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700408 }
409
Jouni Malinena8fff502008-08-11 14:01:48 +0300410 bf->bf_buf_addr = bf->bf_dmacontext =
411 pci_map_single(sc->pdev, skb->data,
412 skb_end_pointer(skb) - skb->head,
413 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700414 bf->bf_mpdu = skb;
415
416 return 0;
417}
418
419/*
420 * Reclaim beacon resources and return buffer to the pool.
421 *
422 * Checks the VAP to put the beacon frame buffer back to the ATH object
Sujith980b24d2008-09-17 10:15:09 +0530423 * queue, and de-allocates any skbs that were sent as CAB traffic.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700424*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700425void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp)
426{
427 if (avp->av_bcbuf != NULL) {
428 struct ath_buf *bf;
429
430 if (avp->av_bslot != -1) {
431 sc->sc_bslot[avp->av_bslot] = ATH_IF_ID_ANY;
432 sc->sc_nbcnvaps--;
433 }
434
435 bf = avp->av_bcbuf;
436 if (bf->bf_mpdu != NULL) {
437 struct sk_buff *skb = (struct sk_buff *)bf->bf_mpdu;
Jouni Malinena8fff502008-08-11 14:01:48 +0300438 pci_unmap_single(sc->pdev, bf->bf_dmacontext,
439 skb_end_pointer(skb) - skb->head,
440 PCI_DMA_TODEVICE);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700441 dev_kfree_skb_any(skb);
442 bf->bf_mpdu = NULL;
443 }
444 list_add_tail(&bf->list, &sc->sc_bbuf);
445
446 avp->av_bcbuf = NULL;
447 }
448}
449
450/*
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700451 * Tasklet for Sending Beacons
452 *
453 * Transmit one or more beacon frames at SWBA. Dynamic updates to the frame
454 * contents are done as needed and the slot time is also adjusted based on
455 * current state.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700456*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700457void ath9k_beacon_tasklet(unsigned long data)
458{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700459 struct ath_softc *sc = (struct ath_softc *)data;
460 struct ath_hal *ah = sc->sc_ah;
461 struct ath_buf *bf = NULL;
462 int slot, if_id;
463 u32 bfaddr;
464 u32 rx_clear = 0, rx_frame = 0, tx_frame = 0;
465 u32 show_cycles = 0;
466 u32 bc = 0; /* beacon count */
467 u64 tsf;
468 u32 tsftu;
469 u16 intval;
470
Sujith98deeea2008-08-11 14:05:46 +0530471 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700472 show_cycles = ath9k_hw_GetMibCycleCountsPct(ah,
473 &rx_clear,
474 &rx_frame,
475 &tx_frame);
476 }
477
478 /*
479 * Check if the previous beacon has gone out. If
480 * not don't try to post another, skip this period
481 * and wait for the next. Missed beacons indicate
482 * a problem and should not occur. If we miss too
483 * many consecutive beacons reset the device.
Sujith980b24d2008-09-17 10:15:09 +0530484 *
485 * FIXME: Clean up this mess !!
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700486 */
487 if (ath9k_hw_numtxpending(ah, sc->sc_bhalq) != 0) {
488 sc->sc_bmisscount++;
489 /* XXX: doth needs the chanchange IE countdown decremented.
490 * We should consider adding a mac80211 call to indicate
491 * a beacon miss so appropriate action could be taken
492 * (in that layer).
493 */
494 if (sc->sc_bmisscount < BSTUCK_THRESH) {
Sujith98deeea2008-08-11 14:05:46 +0530495 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700496 DPRINTF(sc, ATH_DBG_BEACON,
497 "%s: missed %u consecutive beacons\n",
498 __func__, sc->sc_bmisscount);
499 if (show_cycles) {
500 /*
Sujith980b24d2008-09-17 10:15:09 +0530501 * Display cycle counter stats from HW
502 * to aide in debug of stickiness.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700503 */
Sujith980b24d2008-09-17 10:15:09 +0530504 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700505 "%s: busy times: rx_clear=%d, "
506 "rx_frame=%d, tx_frame=%d\n",
507 __func__, rx_clear, rx_frame,
508 tx_frame);
509 } else {
Sujith980b24d2008-09-17 10:15:09 +0530510 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700511 "%s: unable to obtain "
512 "busy times\n", __func__);
513 }
514 } else {
515 DPRINTF(sc, ATH_DBG_BEACON,
516 "%s: missed %u consecutive beacons\n",
517 __func__, sc->sc_bmisscount);
518 }
519 } else if (sc->sc_bmisscount >= BSTUCK_THRESH) {
Sujith98deeea2008-08-11 14:05:46 +0530520 if (sc->sc_flags & SC_OP_NO_RESET) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700521 if (sc->sc_bmisscount == BSTUCK_THRESH) {
Sujith980b24d2008-09-17 10:15:09 +0530522 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700523 "%s: beacon is officially "
524 "stuck\n", __func__);
525 ath9k_hw_dmaRegDump(ah);
526 }
527 } else {
528 DPRINTF(sc, ATH_DBG_BEACON,
529 "%s: beacon is officially stuck\n",
530 __func__);
531 ath_bstuck_process(sc);
532 }
533 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700534 return;
535 }
Sujith980b24d2008-09-17 10:15:09 +0530536
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700537 if (sc->sc_bmisscount != 0) {
Sujith98deeea2008-08-11 14:05:46 +0530538 if (sc->sc_flags & SC_OP_NO_RESET) {
Sujith980b24d2008-09-17 10:15:09 +0530539 DPRINTF(sc, ATH_DBG_BEACON,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700540 "%s: resume beacon xmit after %u misses\n",
541 __func__, sc->sc_bmisscount);
542 } else {
543 DPRINTF(sc, ATH_DBG_BEACON,
544 "%s: resume beacon xmit after %u misses\n",
545 __func__, sc->sc_bmisscount);
546 }
547 sc->sc_bmisscount = 0;
548 }
549
550 /*
551 * Generate beacon frames. we are sending frames
552 * staggered so calculate the slot for this frame based
553 * on the tsf to safeguard against missing an swba.
554 */
555
Jouni Malinena8fff502008-08-11 14:01:48 +0300556 intval = sc->hw->conf.beacon_int ?
557 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700558
559 tsf = ath9k_hw_gettsf64(ah);
560 tsftu = TSF_TO_TU(tsf>>32, tsf);
561 slot = ((tsftu % intval) * ATH_BCBUF) / intval;
562 if_id = sc->sc_bslot[(slot + 1) % ATH_BCBUF];
Sujith980b24d2008-09-17 10:15:09 +0530563
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700564 DPRINTF(sc, ATH_DBG_BEACON,
Sujith980b24d2008-09-17 10:15:09 +0530565 "%s: slot %d [tsf %llu tsftu %u intval %u] if_id %d\n",
566 __func__, slot, (unsigned long long)tsf, tsftu,
567 intval, if_id);
568
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700569 bfaddr = 0;
570 if (if_id != ATH_IF_ID_ANY) {
571 bf = ath_beacon_generate(sc, if_id);
572 if (bf != NULL) {
573 bfaddr = bf->bf_daddr;
574 bc = 1;
575 }
576 }
577 /*
578 * Handle slot time change when a non-ERP station joins/leaves
579 * an 11g network. The 802.11 layer notifies us via callback,
580 * we mark updateslot, then wait one beacon before effecting
581 * the change. This gives associated stations at least one
582 * beacon interval to note the state change.
583 *
584 * NB: The slot time change state machine is clocked according
585 * to whether we are bursting or staggering beacons. We
586 * recognize the request to update and record the current
587 * slot then don't transition until that slot is reached
588 * again. If we miss a beacon for that slot then we'll be
589 * slow to transition but we'll be sure at least one beacon
590 * interval has passed. When bursting slot is always left
591 * set to ATH_BCBUF so this check is a noop.
592 */
593 /* XXX locking */
594 if (sc->sc_updateslot == UPDATE) {
595 sc->sc_updateslot = COMMIT; /* commit next beacon */
596 sc->sc_slotupdate = slot;
597 } else if (sc->sc_updateslot == COMMIT && sc->sc_slotupdate == slot)
598 ath_setslottime(sc); /* commit change to hardware */
599
600 if (bfaddr != 0) {
601 /*
602 * Stop any current dma and put the new frame(s) on the queue.
603 * This should never fail since we check above that no frames
604 * are still pending on the queue.
605 */
606 if (!ath9k_hw_stoptxdma(ah, sc->sc_bhalq)) {
607 DPRINTF(sc, ATH_DBG_FATAL,
608 "%s: beacon queue %u did not stop?\n",
609 __func__, sc->sc_bhalq);
610 /* NB: the HAL still stops DMA, so proceed */
611 }
612
613 /* NB: cabq traffic should already be queued and primed */
614 ath9k_hw_puttxbuf(ah, sc->sc_bhalq, bfaddr);
615 ath9k_hw_txstart(ah, sc->sc_bhalq);
616
617 sc->ast_be_xmit += bc; /* XXX per-vap? */
618 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700619}
620
621/*
622 * Tasklet for Beacon Stuck processing
623 *
624 * Processing for Beacon Stuck.
Sujith980b24d2008-09-17 10:15:09 +0530625 * Basically resets the chip.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700626*/
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700627void ath_bstuck_process(struct ath_softc *sc)
628{
629 DPRINTF(sc, ATH_DBG_BEACON,
630 "%s: stuck beacon; resetting (bmiss count %u)\n",
631 __func__, sc->sc_bmisscount);
Sujithf45144e2008-08-11 14:02:53 +0530632 ath_reset(sc, false);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700633}
634
635/*
636 * Configure the beacon and sleep timers.
637 *
638 * When operating as an AP this resets the TSF and sets
639 * up the hardware to notify us when we need to issue beacons.
640 *
641 * When operating in station mode this sets up the beacon
642 * timers according to the timestamp of the last received
643 * beacon and the current TSF, configures PCF and DTIM
644 * handling, programs the sleep registers so the hardware
645 * will wakeup in time to receive beacons, and configures
646 * the beacon miss handling so we'll receive a BMISS
647 * interrupt when we stop seeing beacons from the AP
648 * we've associated with.
649 */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700650void ath_beacon_config(struct ath_softc *sc, int if_id)
651{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700652 struct ath_hal *ah = sc->sc_ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700653 struct ath_beacon_config conf;
654 enum ath9k_opmode av_opmode;
Sujith980b24d2008-09-17 10:15:09 +0530655 u32 nexttbtt, intval;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700656
657 if (if_id != ATH_IF_ID_ANY)
658 av_opmode = sc->sc_vaps[if_id]->av_opmode;
659 else
Sujithb4696c8b2008-08-11 14:04:52 +0530660 av_opmode = sc->sc_ah->ah_opmode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700661
662 memzero(&conf, sizeof(struct ath_beacon_config));
663
Jouni Malinena8fff502008-08-11 14:01:48 +0300664 conf.beacon_interval = sc->hw->conf.beacon_int ?
665 sc->hw->conf.beacon_int : ATH_DEFAULT_BINTVAL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700666 conf.listen_interval = 1;
667 conf.dtim_period = conf.beacon_interval;
668 conf.dtim_count = 1;
669 conf.bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf.beacon_interval;
670
671 /* extract tstamp from last beacon and convert to TU */
672 nexttbtt = TSF_TO_TU(get_unaligned_le32(conf.u.last_tstamp + 4),
673 get_unaligned_le32(conf.u.last_tstamp));
674 /* XXX conditionalize multi-bss support? */
Sujithb4696c8b2008-08-11 14:04:52 +0530675 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700676 /*
677 * For multi-bss ap support beacons are either staggered
678 * evenly over N slots or burst together. For the former
679 * arrange for the SWBA to be delivered for each slot.
680 * Slots that are not occupied will generate nothing.
681 */
682 /* NB: the beacon interval is kept internally in TU's */
683 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
684 intval /= ATH_BCBUF; /* for staggered beacons */
685 } else {
686 intval = conf.beacon_interval & ATH9K_BEACON_PERIOD;
687 }
688
Sujith980b24d2008-09-17 10:15:09 +0530689 if (nexttbtt == 0) /* e.g. for ap mode */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700690 nexttbtt = intval;
Sujith980b24d2008-09-17 10:15:09 +0530691 else if (intval) /* NB: can be 0 for monitor mode */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700692 nexttbtt = roundup(nexttbtt, intval);
Sujith980b24d2008-09-17 10:15:09 +0530693
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700694 DPRINTF(sc, ATH_DBG_BEACON, "%s: nexttbtt %u intval %u (%u)\n",
695 __func__, nexttbtt, intval, conf.beacon_interval);
Sujith980b24d2008-09-17 10:15:09 +0530696
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700697 /* Check for ATH9K_M_HOSTAP and sc_nostabeacons for WDS client */
Sujithb4696c8b2008-08-11 14:04:52 +0530698 if (sc->sc_ah->ah_opmode == ATH9K_M_STA) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700699 struct ath9k_beacon_state bs;
700 u64 tsf;
701 u32 tsftu;
702 int dtimperiod, dtimcount, sleepduration;
703 int cfpperiod, cfpcount;
704
705 /*
706 * Setup dtim and cfp parameters according to
707 * last beacon we received (which may be none).
708 */
709 dtimperiod = conf.dtim_period;
Sujith980b24d2008-09-17 10:15:09 +0530710 if (dtimperiod <= 0) /* NB: 0 if not known */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700711 dtimperiod = 1;
712 dtimcount = conf.dtim_count;
Sujith980b24d2008-09-17 10:15:09 +0530713 if (dtimcount >= dtimperiod) /* NB: sanity check */
714 dtimcount = 0;
715 cfpperiod = 1; /* NB: no PCF support yet */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700716 cfpcount = 0;
717
718 sleepduration = conf.listen_interval * intval;
719 if (sleepduration <= 0)
720 sleepduration = intval;
721
Sujith980b24d2008-09-17 10:15:09 +0530722#define FUDGE 2
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700723 /*
724 * Pull nexttbtt forward to reflect the current
725 * TSF and calculate dtim+cfp state for the result.
726 */
727 tsf = ath9k_hw_gettsf64(ah);
728 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
729 do {
730 nexttbtt += intval;
731 if (--dtimcount < 0) {
732 dtimcount = dtimperiod - 1;
733 if (--cfpcount < 0)
734 cfpcount = cfpperiod - 1;
735 }
736 } while (nexttbtt < tsftu);
737#undef FUDGE
738 memzero(&bs, sizeof(bs));
739 bs.bs_intval = intval;
740 bs.bs_nexttbtt = nexttbtt;
741 bs.bs_dtimperiod = dtimperiod*intval;
742 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
743 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
744 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
745 bs.bs_cfpmaxduration = 0;
Sujith980b24d2008-09-17 10:15:09 +0530746
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700747 /*
748 * Calculate the number of consecutive beacons to miss
749 * before taking a BMISS interrupt. The configuration
750 * is specified in TU so we only need calculate based
751 * on the beacon interval. Note that we clamp the
752 * result to at most 15 beacons.
753 */
754 if (sleepduration > intval) {
Sujith980b24d2008-09-17 10:15:09 +0530755 bs.bs_bmissthreshold = conf.listen_interval *
756 ATH_DEFAULT_BMISS_LIMIT / 2;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700757 } else {
758 bs.bs_bmissthreshold =
759 DIV_ROUND_UP(conf.bmiss_timeout, intval);
760 if (bs.bs_bmissthreshold > 15)
761 bs.bs_bmissthreshold = 15;
762 else if (bs.bs_bmissthreshold <= 0)
763 bs.bs_bmissthreshold = 1;
764 }
765
766 /*
767 * Calculate sleep duration. The configuration is
768 * given in ms. We insure a multiple of the beacon
769 * period is used. Also, if the sleep duration is
770 * greater than the DTIM period then it makes senses
771 * to make it a multiple of that.
772 *
773 * XXX fixed at 100ms
774 */
775
Sujith980b24d2008-09-17 10:15:09 +0530776 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100),
777 sleepduration);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700778 if (bs.bs_sleepduration > bs.bs_dtimperiod)
779 bs.bs_sleepduration = bs.bs_dtimperiod;
780
781 DPRINTF(sc, ATH_DBG_BEACON,
782 "%s: tsf %llu "
783 "tsf:tu %u "
784 "intval %u "
785 "nexttbtt %u "
786 "dtim %u "
787 "nextdtim %u "
788 "bmiss %u "
789 "sleep %u "
790 "cfp:period %u "
791 "maxdur %u "
792 "next %u "
Sujithff9b6622008-08-14 13:27:16 +0530793 "timoffset %u\n",
794 __func__,
795 (unsigned long long)tsf, tsftu,
796 bs.bs_intval,
797 bs.bs_nexttbtt,
798 bs.bs_dtimperiod,
799 bs.bs_nextdtim,
800 bs.bs_bmissthreshold,
801 bs.bs_sleepduration,
802 bs.bs_cfpperiod,
803 bs.bs_cfpmaxduration,
804 bs.bs_cfpnext,
805 bs.bs_timoffset
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700806 );
807
808 ath9k_hw_set_interrupts(ah, 0);
809 ath9k_hw_set_sta_beacon_timers(ah, &bs);
810 sc->sc_imask |= ATH9K_INT_BMISS;
811 ath9k_hw_set_interrupts(ah, sc->sc_imask);
812 } else {
813 u64 tsf;
814 u32 tsftu;
815 ath9k_hw_set_interrupts(ah, 0);
816 if (nexttbtt == intval)
817 intval |= ATH9K_BEACON_RESET_TSF;
Sujithb4696c8b2008-08-11 14:04:52 +0530818 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700819 /*
820 * Pull nexttbtt forward to reflect the current
Sujith980b24d2008-09-17 10:15:09 +0530821 * TSF
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700822 */
Sujith980b24d2008-09-17 10:15:09 +0530823#define FUDGE 2
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700824 if (!(intval & ATH9K_BEACON_RESET_TSF)) {
825 tsf = ath9k_hw_gettsf64(ah);
826 tsftu = TSF_TO_TU((u32)(tsf>>32),
827 (u32)tsf) + FUDGE;
828 do {
829 nexttbtt += intval;
830 } while (nexttbtt < tsftu);
831 }
832#undef FUDGE
833 DPRINTF(sc, ATH_DBG_BEACON,
834 "%s: IBSS nexttbtt %u intval %u (%u)\n",
835 __func__, nexttbtt,
836 intval & ~ATH9K_BEACON_RESET_TSF,
837 conf.beacon_interval);
838
839 /*
840 * In IBSS mode enable the beacon timers but only
841 * enable SWBA interrupts if we need to manually
842 * prepare beacon frames. Otherwise we use a
843 * self-linked tx descriptor and let the hardware
844 * deal with things.
845 */
846 intval |= ATH9K_BEACON_ENA;
Sujith60b67f52008-08-07 10:52:38 +0530847 if (!(ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700848 sc->sc_imask |= ATH9K_INT_SWBA;
849 ath_beaconq_config(sc);
Sujithb4696c8b2008-08-11 14:04:52 +0530850 } else if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700851 /*
852 * In AP mode we enable the beacon timers and
853 * SWBA interrupts to prepare beacon frames.
854 */
855 intval |= ATH9K_BEACON_ENA;
856 sc->sc_imask |= ATH9K_INT_SWBA; /* beacon prepare */
857 ath_beaconq_config(sc);
858 }
859 ath9k_hw_beaconinit(ah, nexttbtt, intval);
860 sc->sc_bmisscount = 0;
861 ath9k_hw_set_interrupts(ah, sc->sc_imask);
862 /*
863 * When using a self-linked beacon descriptor in
864 * ibss mode load it once here.
865 */
Sujithb4696c8b2008-08-11 14:04:52 +0530866 if (sc->sc_ah->ah_opmode == ATH9K_M_IBSS &&
Sujith60b67f52008-08-07 10:52:38 +0530867 (ah->ah_caps.hw_caps & ATH9K_HW_CAP_VEOL))
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700868 ath_beacon_start_adhoc(sc, 0);
869 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700870}
871
872/* Function to collect beacon rssi data and resync beacon if necessary */
873
874void ath_beacon_sync(struct ath_softc *sc, int if_id)
875{
876 /*
877 * Resync beacon timers using the tsf of the
878 * beacon frame we just received.
879 */
880 ath_beacon_config(sc, if_id);
Sujith672840a2008-08-11 14:05:08 +0530881 sc->sc_flags |= SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700882}