ath5k: fix txop limit handling
[deliverable/linux.git] / drivers / net / wireless / ath / ath9k / beacon.c
CommitLineData
f078f209 1/*
5b68138e 2 * Copyright (c) 2008-2011 Atheros Communications Inc.
f078f209
LR
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
b7f080cf 17#include <linux/dma-mapping.h>
394cf0a1 18#include "ath9k.h"
f078f209 19
5379c8a2
S
20#define FUDGE 2
21
ba4903f9
FF
22static void ath9k_reset_beacon_status(struct ath_softc *sc)
23{
24 sc->beacon.tx_processed = false;
25 sc->beacon.tx_last = false;
26}
27
f078f209 28/*
f078f209
LR
29 * This function will modify certain transmit queue properties depending on
30 * the operating mode of the station (AP or AdHoc). Parameters are AIFS
31 * settings and channel width min/max
32*/
94db2936 33int ath_beaconq_config(struct ath_softc *sc)
f078f209 34{
cbe61d8a 35 struct ath_hw *ah = sc->sc_ah;
c46917bb 36 struct ath_common *common = ath9k_hw_common(ah);
94db2936 37 struct ath9k_tx_queue_info qi, qi_be;
066dae93 38 struct ath_txq *txq;
f078f209 39
b77f483f 40 ath9k_hw_get_txq_props(ah, sc->beacon.beaconq, &qi);
2660b81a 41 if (sc->sc_ah->opmode == NL80211_IFTYPE_AP) {
f078f209
LR
42 /* Always burst out beacon and CAB traffic. */
43 qi.tqi_aifs = 1;
44 qi.tqi_cwmin = 0;
45 qi.tqi_cwmax = 0;
46 } else {
47 /* Adhoc mode; important thing is to use 2x cwmin. */
066dae93
FF
48 txq = sc->tx.txq_map[WME_AC_BE];
49 ath9k_hw_get_txq_props(ah, txq->axq_qnum, &qi_be);
94db2936 50 qi.tqi_aifs = qi_be.tqi_aifs;
d202caff
VN
51 if (ah->slottime == ATH9K_SLOT_TIME_20)
52 qi.tqi_cwmin = 2*qi_be.tqi_cwmin;
53 else
54 qi.tqi_cwmin = 4*qi_be.tqi_cwmin;
94db2936 55 qi.tqi_cwmax = qi_be.tqi_cwmax;
f078f209
LR
56 }
57
b77f483f 58 if (!ath9k_hw_set_txq_props(ah, sc->beacon.beaconq, &qi)) {
3800276a
JP
59 ath_err(common,
60 "Unable to update h/w beacon queue parameters\n");
f078f209
LR
61 return 0;
62 } else {
9fc9ab0a 63 ath9k_hw_resettxqueue(ah, sc->beacon.beaconq);
f078f209
LR
64 return 1;
65 }
66}
67
68/*
f078f209 69 * Associates the beacon frame buffer with a transmit descriptor. Will set
dd347f2f
FF
70 * up rate codes, and channel flags. Beacons are always sent out at the
71 * lowest rate, and are not retried.
f078f209 72*/
d47a61aa 73static void ath_beacon_setup(struct ath_softc *sc, struct ieee80211_vif *vif,
64b84010 74 struct ath_buf *bf, int rateidx)
f078f209 75{
a22be22a 76 struct sk_buff *skb = bf->bf_mpdu;
cbe61d8a 77 struct ath_hw *ah = sc->sc_ah;
43c27613 78 struct ath_common *common = ath9k_hw_common(ah);
493cf04f 79 struct ath_tx_info info;
545750d3 80 struct ieee80211_supported_band *sband;
493cf04f 81 u8 chainmask = ah->txchainmask;
545750d3 82 u8 rate = 0;
f078f209 83
ba4903f9
FF
84 ath9k_reset_beacon_status(sc);
85
545750d3 86 sband = &sc->sbands[common->hw->conf.channel->band];
64b84010 87 rate = sband->bitrates[rateidx].hw_value;
d47a61aa 88 if (vif->bss_conf.use_short_preamble)
64b84010 89 rate |= sband->bitrates[rateidx].hw_value_short;
9fc9ab0a 90
493cf04f
FF
91 memset(&info, 0, sizeof(info));
92 info.pkt_len = skb->len + FCS_LEN;
93 info.type = ATH9K_PKT_TYPE_BEACON;
94 info.txpower = MAX_RATE_POWER;
95 info.keyix = ATH9K_TXKEYIX_INVALID;
96 info.keytype = ATH9K_KEY_TYPE_CLEAR;
cd484aeb 97 info.flags = ATH9K_TXDESC_NOACK | ATH9K_TXDESC_CLRDMASK;
493cf04f
FF
98
99 info.buf_addr[0] = bf->bf_buf_addr;
100 info.buf_len[0] = roundup(skb->len, 4);
101
102 info.is_first = true;
103 info.is_last = true;
104
105 info.qcu = sc->beacon.beaconq;
106
107 info.rates[0].Tries = 1;
108 info.rates[0].Rate = rate;
109 info.rates[0].ChSel = ath_txchainmask_reduction(sc, chainmask, rate);
110
111 ath9k_hw_set_txdesc(ah, bf->bf_desc, &info);
f078f209
LR
112}
113
28d16708
FF
114static void ath_tx_cabq(struct ieee80211_hw *hw, struct sk_buff *skb)
115{
9ac58615 116 struct ath_softc *sc = hw->priv;
28d16708
FF
117 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
118 struct ath_tx_control txctl;
119
120 memset(&txctl, 0, sizeof(struct ath_tx_control));
121 txctl.txq = sc->beacon.cabq;
122
d2182b69 123 ath_dbg(common, XMIT, "transmitting CABQ packet, skb: %p\n", skb);
28d16708
FF
124
125 if (ath_tx_start(hw, skb, &txctl) != 0) {
d2182b69 126 ath_dbg(common, XMIT, "CABQ TX failed\n");
28d16708
FF
127 dev_kfree_skb_any(skb);
128 }
129}
130
c52f33d0 131static struct ath_buf *ath_beacon_generate(struct ieee80211_hw *hw,
2c3db3d5 132 struct ieee80211_vif *vif)
f078f209 133{
9ac58615 134 struct ath_softc *sc = hw->priv;
c46917bb 135 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
f078f209 136 struct ath_buf *bf;
17d7904d 137 struct ath_vif *avp;
f078f209 138 struct sk_buff *skb;
f078f209 139 struct ath_txq *cabq;
147583c0 140 struct ieee80211_tx_info *info;
980b24da
S
141 int cabq_depth;
142
ba4903f9
FF
143 ath9k_reset_beacon_status(sc);
144
5640b08e 145 avp = (void *)vif->drv_priv;
b77f483f 146 cabq = sc->beacon.cabq;
f078f209 147
014cf3bb 148 if ((avp->av_bcbuf == NULL) || !avp->is_bslot_active)
f078f209 149 return NULL;
980b24da 150
9fc9ab0a
S
151 /* Release the old beacon first */
152
f078f209 153 bf = avp->av_bcbuf;
a22be22a 154 skb = bf->bf_mpdu;
a8fff50e 155 if (skb) {
c1739eb3 156 dma_unmap_single(sc->dev, bf->bf_buf_addr,
9fc9ab0a 157 skb->len, DMA_TO_DEVICE);
3fbb9d95 158 dev_kfree_skb_any(skb);
6cf9e995 159 bf->bf_buf_addr = 0;
a8fff50e 160 }
f078f209 161
9fc9ab0a
S
162 /* Get a new beacon from mac80211 */
163
c52f33d0 164 skb = ieee80211_beacon_get(hw, vif);
a8fff50e
JM
165 bf->bf_mpdu = skb;
166 if (skb == NULL)
167 return NULL;
4ed96f04
JM
168 ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp =
169 avp->tsf_adjust;
980b24da 170
147583c0
JM
171 info = IEEE80211_SKB_CB(skb);
172 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
173 /*
174 * TODO: make sure the seq# gets assigned properly (vs. other
175 * TX frames)
176 */
980b24da 177 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
b77f483f 178 sc->tx.seq_no += 0x10;
147583c0 179 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
b77f483f 180 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
147583c0 181 }
980b24da 182
c1739eb3
BG
183 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
184 skb->len, DMA_TO_DEVICE);
7da3c55c 185 if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
f8316df1
LR
186 dev_kfree_skb_any(skb);
187 bf->bf_mpdu = NULL;
6cf9e995 188 bf->bf_buf_addr = 0;
3800276a 189 ath_err(common, "dma_mapping_error on beaconing\n");
f8316df1
LR
190 return NULL;
191 }
f078f209 192
c52f33d0 193 skb = ieee80211_get_buffered_bc(hw, vif);
f078f209 194
f078f209
LR
195 /*
196 * if the CABQ traffic from previous DTIM is pending and the current
197 * beacon is also a DTIM.
17d7904d
S
198 * 1) if there is only one vif let the cab traffic continue.
199 * 2) if there are more than one vif and we are using staggered
f078f209 200 * beacons, then drain the cabq by dropping all the frames in
17d7904d 201 * the cabq so that the current vifs cab traffic can be scheduled.
f078f209
LR
202 */
203 spin_lock_bh(&cabq->axq_lock);
204 cabq_depth = cabq->axq_depth;
205 spin_unlock_bh(&cabq->axq_lock);
206
e022edbd 207 if (skb && cabq_depth) {
17d7904d 208 if (sc->nvifs > 1) {
d2182b69 209 ath_dbg(common, BEACON,
226afe68 210 "Flushing previous cabq traffic\n");
9fc9ab0a 211 ath_draintxq(sc, cabq, false);
f078f209
LR
212 }
213 }
214
d47a61aa 215 ath_beacon_setup(sc, vif, bf, info->control.rates[0].idx);
f078f209 216
e022edbd 217 while (skb) {
c52f33d0
JM
218 ath_tx_cabq(hw, skb);
219 skb = ieee80211_get_buffered_bc(hw, vif);
e022edbd 220 }
f078f209 221
f078f209
LR
222 return bf;
223}
224
9ac58615 225int ath_beacon_alloc(struct ath_softc *sc, struct ieee80211_vif *vif)
f078f209 226{
c46917bb 227 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
17d7904d 228 struct ath_vif *avp;
f078f209
LR
229 struct ath_buf *bf;
230 struct sk_buff *skb;
9814f6b3 231 struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
459f5f90 232 __le64 tstamp;
f078f209 233
5640b08e 234 avp = (void *)vif->drv_priv;
f078f209
LR
235
236 /* Allocate a beacon descriptor if we haven't done so. */
237 if (!avp->av_bcbuf) {
980b24da
S
238 /* Allocate beacon state for hostap/ibss. We know
239 * a buffer is available. */
b77f483f 240 avp->av_bcbuf = list_first_entry(&sc->beacon.bbuf,
980b24da 241 struct ath_buf, list);
f078f209
LR
242 list_del(&avp->av_bcbuf->list);
243
4801416c 244 if (ath9k_uses_beacons(vif->type)) {
f078f209
LR
245 int slot;
246 /*
17d7904d 247 * Assign the vif to a beacon xmit slot. As
f078f209
LR
248 * above, this cannot fail to find one.
249 */
250 avp->av_bslot = 0;
251 for (slot = 0; slot < ATH_BCBUF; slot++)
2c3db3d5 252 if (sc->beacon.bslot[slot] == NULL) {
f078f209 253 avp->av_bslot = slot;
014cf3bb 254 avp->is_bslot_active = false;
774610e4 255
f078f209 256 /* NB: keep looking for a double slot */
774610e4
FF
257 if (slot == 0 || !sc->beacon.bslot[slot-1])
258 break;
f078f209 259 }
2c3db3d5
JM
260 BUG_ON(sc->beacon.bslot[avp->av_bslot] != NULL);
261 sc->beacon.bslot[avp->av_bslot] = vif;
17d7904d 262 sc->nbcnvifs++;
f078f209
LR
263 }
264 }
265
9fc9ab0a 266 /* release the previous beacon frame, if it already exists. */
f078f209
LR
267 bf = avp->av_bcbuf;
268 if (bf->bf_mpdu != NULL) {
a22be22a 269 skb = bf->bf_mpdu;
c1739eb3 270 dma_unmap_single(sc->dev, bf->bf_buf_addr,
9fc9ab0a 271 skb->len, DMA_TO_DEVICE);
f078f209
LR
272 dev_kfree_skb_any(skb);
273 bf->bf_mpdu = NULL;
6cf9e995 274 bf->bf_buf_addr = 0;
f078f209
LR
275 }
276
9fc9ab0a 277 /* NB: the beacon data buffer must be 32-bit aligned. */
5640b08e 278 skb = ieee80211_beacon_get(sc->hw, vif);
d7e86c32 279 if (skb == NULL)
f078f209 280 return -ENOMEM;
f078f209 281
459f5f90 282 tstamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
dd347f2f 283 sc->beacon.bc_tstamp = (u32) le64_to_cpu(tstamp);
4ed96f04 284 /* Calculate a TSF adjustment factor required for staggered beacons. */
f078f209
LR
285 if (avp->av_bslot > 0) {
286 u64 tsfadjust;
f078f209
LR
287 int intval;
288
9814f6b3 289 intval = cur_conf->beacon_interval ? : ATH_DEFAULT_BINTVAL;
f078f209
LR
290
291 /*
4ed96f04
JM
292 * Calculate the TSF offset for this beacon slot, i.e., the
293 * number of usecs that need to be added to the timestamp field
294 * in Beacon and Probe Response frames. Beacon slot 0 is
295 * processed at the correct offset, so it does not require TSF
296 * adjustment. Other slots are adjusted to get the timestamp
297 * close to the TBTT for the BSS.
f078f209 298 */
dd347f2f
FF
299 tsfadjust = TU_TO_USEC(intval * avp->av_bslot) / ATH_BCBUF;
300 avp->tsf_adjust = cpu_to_le64(tsfadjust);
f078f209 301
d2182b69 302 ath_dbg(common, BEACON,
226afe68
JP
303 "stagger beacons, bslot %d intval %u tsfadjust %llu\n",
304 avp->av_bslot, intval, (unsigned long long)tsfadjust);
f078f209 305
4ed96f04
JM
306 ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp =
307 avp->tsf_adjust;
308 } else
309 avp->tsf_adjust = cpu_to_le64(0);
f078f209 310
f8316df1 311 bf->bf_mpdu = skb;
c1739eb3
BG
312 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
313 skb->len, DMA_TO_DEVICE);
7da3c55c 314 if (unlikely(dma_mapping_error(sc->dev, bf->bf_buf_addr))) {
f8316df1
LR
315 dev_kfree_skb_any(skb);
316 bf->bf_mpdu = NULL;
6cf9e995 317 bf->bf_buf_addr = 0;
3800276a 318 ath_err(common, "dma_mapping_error on beacon alloc\n");
f8316df1
LR
319 return -ENOMEM;
320 }
014cf3bb 321 avp->is_bslot_active = true;
f078f209
LR
322
323 return 0;
324}
325
17d7904d 326void ath_beacon_return(struct ath_softc *sc, struct ath_vif *avp)
f078f209
LR
327{
328 if (avp->av_bcbuf != NULL) {
329 struct ath_buf *bf;
330
f60c49b6 331 avp->is_bslot_active = false;
f078f209 332 if (avp->av_bslot != -1) {
2c3db3d5 333 sc->beacon.bslot[avp->av_bslot] = NULL;
17d7904d 334 sc->nbcnvifs--;
66da4241 335 avp->av_bslot = -1;
f078f209
LR
336 }
337
338 bf = avp->av_bcbuf;
339 if (bf->bf_mpdu != NULL) {
a22be22a 340 struct sk_buff *skb = bf->bf_mpdu;
c1739eb3 341 dma_unmap_single(sc->dev, bf->bf_buf_addr,
9fc9ab0a 342 skb->len, DMA_TO_DEVICE);
f078f209
LR
343 dev_kfree_skb_any(skb);
344 bf->bf_mpdu = NULL;
6cf9e995 345 bf->bf_buf_addr = 0;
f078f209 346 }
b77f483f 347 list_add_tail(&bf->list, &sc->beacon.bbuf);
f078f209
LR
348
349 avp->av_bcbuf = NULL;
350 }
351}
352
9fc9ab0a 353void ath_beacon_tasklet(unsigned long data)
f078f209 354{
f078f209 355 struct ath_softc *sc = (struct ath_softc *)data;
9814f6b3 356 struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
cbe61d8a 357 struct ath_hw *ah = sc->sc_ah;
c46917bb 358 struct ath_common *common = ath9k_hw_common(ah);
f078f209 359 struct ath_buf *bf = NULL;
2c3db3d5 360 struct ieee80211_vif *vif;
98f0a5eb 361 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
2c3db3d5 362 int slot;
c6820f1e 363 u32 bfaddr, bc = 0;
f078f209 364
4e7fb718
RM
365 if (work_pending(&sc->hw_reset_work)) {
366 ath_dbg(common, RESET,
367 "reset work is pending, skip beaconing now\n");
368 return;
369 }
f078f209
LR
370 /*
371 * Check if the previous beacon has gone out. If
372 * not don't try to post another, skip this period
373 * and wait for the next. Missed beacons indicate
374 * a problem and should not occur. If we miss too
375 * many consecutive beacons reset the device.
376 */
b77f483f
S
377 if (ath9k_hw_numtxpending(ah, sc->beacon.beaconq) != 0) {
378 sc->beacon.bmisscnt++;
9546aae0 379
b381fa32
FF
380 if (!ath9k_hw_check_alive(ah))
381 ieee80211_queue_work(sc->hw, &sc->hw_check_work);
382
c944daf4 383 if (sc->beacon.bmisscnt < BSTUCK_THRESH * sc->nbcnvifs) {
d2182b69 384 ath_dbg(common, BSTUCK,
226afe68
JP
385 "missed %u consecutive beacons\n",
386 sc->beacon.bmisscnt);
efff395e 387 ath9k_hw_stop_dma_queue(ah, sc->beacon.beaconq);
87c510fe
FF
388 if (sc->beacon.bmisscnt > 3)
389 ath9k_hw_bstuck_nfcal(ah);
b77f483f 390 } else if (sc->beacon.bmisscnt >= BSTUCK_THRESH) {
d2182b69 391 ath_dbg(common, BSTUCK, "beacon is officially stuck\n");
4e7fb718 392 sc->beacon.bmisscnt = 0;
781b14a3 393 set_bit(SC_OP_TSF_RESET, &sc->sc_flags);
236de514 394 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
f078f209 395 }
9546aae0 396
f078f209
LR
397 return;
398 }
980b24da 399
f078f209
LR
400 /*
401 * Generate beacon frames. we are sending frames
402 * staggered so calculate the slot for this frame based
403 * on the tsf to safeguard against missing an swba.
404 */
405
f078f209 406
c6820f1e
FF
407 if (ah->opmode == NL80211_IFTYPE_AP) {
408 u16 intval;
409 u32 tsftu;
410 u64 tsf;
411
412 intval = cur_conf->beacon_interval ? : ATH_DEFAULT_BINTVAL;
413 tsf = ath9k_hw_gettsf64(ah);
414 tsf += TU_TO_USEC(ah->config.sw_beacon_response_time);
415 tsftu = TSF_TO_TU((tsf * ATH_BCBUF) >>32, tsf * ATH_BCBUF);
416 slot = (tsftu % (intval * ATH_BCBUF)) / intval;
417 vif = sc->beacon.bslot[slot];
418
d2182b69 419 ath_dbg(common, BEACON,
c6820f1e
FF
420 "slot %d [tsf %llu tsftu %u intval %u] vif %p\n",
421 slot, tsf, tsftu / ATH_BCBUF, intval, vif);
422 } else {
423 slot = 0;
424 vif = sc->beacon.bslot[slot];
425 }
980b24da 426
980b24da 427
f078f209 428 bfaddr = 0;
2c3db3d5 429 if (vif) {
7545daf4 430 bf = ath_beacon_generate(sc->hw, vif);
f078f209
LR
431 if (bf != NULL) {
432 bfaddr = bf->bf_daddr;
433 bc = 1;
434 }
c944daf4
FF
435
436 if (sc->beacon.bmisscnt != 0) {
d2182b69 437 ath_dbg(common, BSTUCK,
c944daf4
FF
438 "resume beacon xmit after %u misses\n",
439 sc->beacon.bmisscnt);
440 sc->beacon.bmisscnt = 0;
441 }
f078f209 442 }
9546aae0 443
f078f209
LR
444 /*
445 * Handle slot time change when a non-ERP station joins/leaves
446 * an 11g network. The 802.11 layer notifies us via callback,
447 * we mark updateslot, then wait one beacon before effecting
448 * the change. This gives associated stations at least one
449 * beacon interval to note the state change.
450 *
451 * NB: The slot time change state machine is clocked according
452 * to whether we are bursting or staggering beacons. We
453 * recognize the request to update and record the current
454 * slot then don't transition until that slot is reached
455 * again. If we miss a beacon for that slot then we'll be
456 * slow to transition but we'll be sure at least one beacon
457 * interval has passed. When bursting slot is always left
458 * set to ATH_BCBUF so this check is a noop.
459 */
b77f483f
S
460 if (sc->beacon.updateslot == UPDATE) {
461 sc->beacon.updateslot = COMMIT; /* commit next beacon */
462 sc->beacon.slotupdate = slot;
463 } else if (sc->beacon.updateslot == COMMIT && sc->beacon.slotupdate == slot) {
0005baf4
FF
464 ah->slottime = sc->beacon.slottime;
465 ath9k_hw_init_global_settings(ah);
b77f483f 466 sc->beacon.updateslot = OK;
ff37e337 467 }
f078f209 468 if (bfaddr != 0) {
f078f209 469 /* NB: cabq traffic should already be queued and primed */
b77f483f 470 ath9k_hw_puttxbuf(ah, sc->beacon.beaconq, bfaddr);
98f0a5eb
MSS
471
472 if (!edma)
473 ath9k_hw_txstart(ah, sc->beacon.beaconq);
f078f209 474
17d7904d 475 sc->beacon.ast_be_xmit += bc; /* XXX per-vif? */
f078f209 476 }
f078f209
LR
477}
478
21526d57
LR
479static void ath9k_beacon_init(struct ath_softc *sc,
480 u32 next_beacon,
481 u32 beacon_period)
482{
781b14a3 483 if (test_bit(SC_OP_TSF_RESET, &sc->sc_flags)) {
21526d57 484 ath9k_ps_wakeup(sc);
dd347f2f
FF
485 ath9k_hw_reset_tsf(sc->sc_ah);
486 }
21526d57
LR
487
488 ath9k_hw_beaconinit(sc->sc_ah, next_beacon, beacon_period);
489
781b14a3 490 if (test_bit(SC_OP_TSF_RESET, &sc->sc_flags)) {
21526d57 491 ath9k_ps_restore(sc);
781b14a3 492 clear_bit(SC_OP_TSF_RESET, &sc->sc_flags);
dd347f2f 493 }
21526d57
LR
494}
495
f078f209 496/*
5379c8a2
S
497 * For multi-bss ap support beacons are either staggered evenly over N slots or
498 * burst together. For the former arrange for the SWBA to be delivered for each
499 * slot. Slots that are not occupied will generate nothing.
f078f209 500 */
5379c8a2 501static void ath_beacon_config_ap(struct ath_softc *sc,
d31e20af 502 struct ath_beacon_config *conf)
f078f209 503{
3069168c 504 struct ath_hw *ah = sc->sc_ah;
980b24da 505 u32 nexttbtt, intval;
f078f209 506
5379c8a2 507 /* NB: the beacon interval is kept internally in TU's */
f29f5c08 508 intval = TU_TO_USEC(conf->beacon_interval);
5379c8a2
S
509 intval /= ATH_BCBUF; /* for staggered beacons */
510 nexttbtt = intval;
d8728ee9 511
5379c8a2
S
512 /*
513 * In AP mode we enable the beacon timers and SWBA interrupts to
514 * prepare beacon frames.
515 */
3069168c 516 ah->imask |= ATH9K_INT_SWBA;
5379c8a2 517 ath_beaconq_config(sc);
f078f209 518
5379c8a2 519 /* Set the computed AP beacon timers */
f078f209 520
4df3071e 521 ath9k_hw_disable_interrupts(ah);
781b14a3 522 set_bit(SC_OP_TSF_RESET, &sc->sc_flags);
21526d57 523 ath9k_beacon_init(sc, nexttbtt, intval);
5379c8a2 524 sc->beacon.bmisscnt = 0;
72d874c6 525 ath9k_hw_set_interrupts(ah);
b037b693 526 ath9k_hw_enable_interrupts(ah);
5379c8a2 527}
459f5f90 528
5379c8a2
S
529/*
530 * This sets up the beacon timers according to the timestamp of the last
531 * received beacon and the current TSF, configures PCF and DTIM
532 * handling, programs the sleep registers so the hardware will wakeup in
533 * time to receive beacons, and configures the beacon miss handling so
534 * we'll receive a BMISS interrupt when we stop seeing beacons from the AP
535 * we've associated with.
536 */
537static void ath_beacon_config_sta(struct ath_softc *sc,
d31e20af 538 struct ath_beacon_config *conf)
5379c8a2 539{
3069168c
PR
540 struct ath_hw *ah = sc->sc_ah;
541 struct ath_common *common = ath9k_hw_common(ah);
5379c8a2
S
542 struct ath9k_beacon_state bs;
543 int dtimperiod, dtimcount, sleepduration;
544 int cfpperiod, cfpcount;
545 u32 nexttbtt = 0, intval, tsftu;
546 u64 tsf;
267a9012 547 int num_beacons, offset, dtim_dec_count, cfp_dec_count;
5379c8a2 548
1a20034a
SB
549 /* No need to configure beacon if we are not associated */
550 if (!common->curaid) {
d2182b69 551 ath_dbg(common, BEACON,
226afe68 552 "STA is not yet associated..skipping beacon config\n");
1a20034a
SB
553 return;
554 }
555
5379c8a2 556 memset(&bs, 0, sizeof(bs));
f29f5c08 557 intval = conf->beacon_interval;
5379c8a2
S
558
559 /*
560 * Setup dtim and cfp parameters according to
561 * last beacon we received (which may be none).
562 */
563 dtimperiod = conf->dtim_period;
5379c8a2
S
564 dtimcount = conf->dtim_count;
565 if (dtimcount >= dtimperiod) /* NB: sanity check */
566 dtimcount = 0;
567 cfpperiod = 1; /* NB: no PCF support yet */
568 cfpcount = 0;
569
570 sleepduration = conf->listen_interval * intval;
5379c8a2
S
571
572 /*
573 * Pull nexttbtt forward to reflect the current
574 * TSF and calculate dtim+cfp state for the result.
575 */
3069168c 576 tsf = ath9k_hw_gettsf64(ah);
5379c8a2 577 tsftu = TSF_TO_TU(tsf>>32, tsf) + FUDGE;
267a9012
JM
578
579 num_beacons = tsftu / intval + 1;
580 offset = tsftu % intval;
581 nexttbtt = tsftu - offset;
582 if (offset)
5379c8a2 583 nexttbtt += intval;
267a9012
JM
584
585 /* DTIM Beacon every dtimperiod Beacon */
586 dtim_dec_count = num_beacons % dtimperiod;
587 /* CFP every cfpperiod DTIM Beacon */
588 cfp_dec_count = (num_beacons / dtimperiod) % cfpperiod;
589 if (dtim_dec_count)
590 cfp_dec_count++;
591
592 dtimcount -= dtim_dec_count;
593 if (dtimcount < 0)
594 dtimcount += dtimperiod;
595
596 cfpcount -= cfp_dec_count;
597 if (cfpcount < 0)
598 cfpcount += cfpperiod;
5379c8a2
S
599
600 bs.bs_intval = intval;
601 bs.bs_nexttbtt = nexttbtt;
602 bs.bs_dtimperiod = dtimperiod*intval;
603 bs.bs_nextdtim = bs.bs_nexttbtt + dtimcount*intval;
604 bs.bs_cfpperiod = cfpperiod*bs.bs_dtimperiod;
605 bs.bs_cfpnext = bs.bs_nextdtim + cfpcount*bs.bs_dtimperiod;
606 bs.bs_cfpmaxduration = 0;
607
608 /*
609 * Calculate the number of consecutive beacons to miss* before taking
610 * a BMISS interrupt. The configuration is specified in TU so we only
611 * need calculate based on the beacon interval. Note that we clamp the
612 * result to at most 15 beacons.
613 */
614 if (sleepduration > intval) {
615 bs.bs_bmissthreshold = conf->listen_interval *
616 ATH_DEFAULT_BMISS_LIMIT / 2;
f078f209 617 } else {
5379c8a2
S
618 bs.bs_bmissthreshold = DIV_ROUND_UP(conf->bmiss_timeout, intval);
619 if (bs.bs_bmissthreshold > 15)
620 bs.bs_bmissthreshold = 15;
621 else if (bs.bs_bmissthreshold <= 0)
622 bs.bs_bmissthreshold = 1;
f078f209
LR
623 }
624
5379c8a2
S
625 /*
626 * Calculate sleep duration. The configuration is given in ms.
627 * We ensure a multiple of the beacon period is used. Also, if the sleep
628 * duration is greater than the DTIM period then it makes senses
629 * to make it a multiple of that.
630 *
631 * XXX fixed at 100ms
632 */
980b24da 633
5379c8a2
S
634 bs.bs_sleepduration = roundup(IEEE80211_MS_TO_TU(100), sleepduration);
635 if (bs.bs_sleepduration > bs.bs_dtimperiod)
636 bs.bs_sleepduration = bs.bs_dtimperiod;
980b24da 637
5379c8a2
S
638 /* TSF out of range threshold fixed at 1 second */
639 bs.bs_tsfoor_threshold = ATH9K_TSFOOR_THRESHOLD;
f078f209 640
d2182b69
JP
641 ath_dbg(common, BEACON, "tsf: %llu tsftu: %u\n", tsf, tsftu);
642 ath_dbg(common, BEACON,
226afe68
JP
643 "bmiss: %u sleep: %u cfp-period: %u maxdur: %u next: %u\n",
644 bs.bs_bmissthreshold, bs.bs_sleepduration,
645 bs.bs_cfpperiod, bs.bs_cfpmaxduration, bs.bs_cfpnext);
f078f209 646
5379c8a2 647 /* Set the computed STA beacon timers */
980b24da 648
4df3071e 649 ath9k_hw_disable_interrupts(ah);
3069168c
PR
650 ath9k_hw_set_sta_beacon_timers(ah, &bs);
651 ah->imask |= ATH9K_INT_BMISS;
deb75188 652
72d874c6 653 ath9k_hw_set_interrupts(ah);
e8fe7336 654 ath9k_hw_enable_interrupts(ah);
5379c8a2 655}
f078f209 656
5379c8a2 657static void ath_beacon_config_adhoc(struct ath_softc *sc,
ee832d3e 658 struct ath_beacon_config *conf)
5379c8a2 659{
3069168c
PR
660 struct ath_hw *ah = sc->sc_ah;
661 struct ath_common *common = ath9k_hw_common(ah);
81168e50 662 u32 tsf, intval, nexttbtt;
dd347f2f 663
ba4903f9 664 ath9k_reset_beacon_status(sc);
781b14a3 665 if (!test_bit(SC_OP_BEACONS, &sc->sc_flags))
de5f8fc3 666 ath9k_hw_settsf64(ah, sc->beacon.bc_tstamp);
ba4903f9 667
f29f5c08 668 intval = TU_TO_USEC(conf->beacon_interval);
81168e50
FD
669 tsf = roundup(ath9k_hw_gettsf32(ah) + TU_TO_USEC(FUDGE), intval);
670 nexttbtt = tsf + intval;
f078f209 671
d2182b69 672 ath_dbg(common, BEACON, "IBSS nexttbtt %u intval %u (%u)\n",
226afe68 673 nexttbtt, intval, conf->beacon_interval);
9fc9ab0a 674
5379c8a2
S
675 /*
676 * In IBSS mode enable the beacon timers but only enable SWBA interrupts
677 * if we need to manually prepare beacon frames. Otherwise we use a
678 * self-linked tx descriptor and let the hardware deal with things.
679 */
a65e4cb4 680 ah->imask |= ATH9K_INT_SWBA;
9fc9ab0a 681
5379c8a2
S
682 ath_beaconq_config(sc);
683
684 /* Set the computed ADHOC beacon timers */
685
4df3071e 686 ath9k_hw_disable_interrupts(ah);
21526d57 687 ath9k_beacon_init(sc, nexttbtt, intval);
5379c8a2 688 sc->beacon.bmisscnt = 0;
e8fe7336 689
72d874c6 690 ath9k_hw_set_interrupts(ah);
e8fe7336 691 ath9k_hw_enable_interrupts(ah);
f078f209
LR
692}
693
99e4d43a
RM
694static bool ath9k_allow_beacon_config(struct ath_softc *sc,
695 struct ieee80211_vif *vif)
f078f209 696{
6b96f93e 697 struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
c46917bb 698 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
99e4d43a
RM
699 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
700 struct ath_vif *avp = (void *)vif->drv_priv;
5379c8a2 701
99e4d43a
RM
702 /*
703 * Can not have different beacon interval on multiple
704 * AP interface case
705 */
706 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
707 (sc->nbcnvifs > 1) &&
708 (vif->type == NL80211_IFTYPE_AP) &&
709 (cur_conf->beacon_interval != bss_conf->beacon_int)) {
d2182b69
JP
710 ath_dbg(common, CONFIG,
711 "Changing beacon interval of multiple AP interfaces !\n");
99e4d43a
RM
712 return false;
713 }
714 /*
715 * Can not configure station vif's beacon config
716 * while on AP opmode
717 */
718 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
719 (vif->type != NL80211_IFTYPE_AP)) {
d2182b69 720 ath_dbg(common, CONFIG,
99e4d43a
RM
721 "STA vif's beacon not allowed on AP mode\n");
722 return false;
723 }
724 /*
725 * Do not allow beacon config if HW was already configured
726 * with another STA vif
727 */
728 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
729 (vif->type == NL80211_IFTYPE_STATION) &&
781b14a3 730 test_bit(SC_OP_BEACONS, &sc->sc_flags) &&
99e4d43a 731 !avp->primary_sta_vif) {
d2182b69 732 ath_dbg(common, CONFIG,
99e4d43a
RM
733 "Beacon already configured for a station interface\n");
734 return false;
ee832d3e 735 }
99e4d43a
RM
736 return true;
737}
738
739void ath_beacon_config(struct ath_softc *sc, struct ieee80211_vif *vif)
740{
741 struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
742 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
ee832d3e 743
99e4d43a
RM
744 if (!ath9k_allow_beacon_config(sc, vif))
745 return;
746
747 /* Setup the beacon configuration parameters */
748 cur_conf->beacon_interval = bss_conf->beacon_int;
749 cur_conf->dtim_period = bss_conf->dtim_period;
4801416c
BG
750 cur_conf->listen_interval = 1;
751 cur_conf->dtim_count = 1;
752 cur_conf->bmiss_timeout =
753 ATH_DEFAULT_BMISS_LIMIT * cur_conf->beacon_interval;
6b96f93e 754
c4f9f16b
VT
755 /*
756 * It looks like mac80211 may end up using beacon interval of zero in
757 * some cases (at least for mesh point). Avoid getting into an
758 * infinite loop by using a bit safer value instead. To be safe,
759 * do sanity check on beacon interval for all operating modes.
760 */
761 if (cur_conf->beacon_interval == 0)
762 cur_conf->beacon_interval = 100;
6b96f93e 763
ee832d3e 764 /*
3a2329f2
MSS
765 * We don't parse dtim period from mac80211 during the driver
766 * initialization as it breaks association with hidden-ssid
767 * AP and it causes latency in roaming
ee832d3e
MSS
768 */
769 if (cur_conf->dtim_period == 0)
770 cur_conf->dtim_period = 1;
771
99e4d43a 772 ath_set_beacon(sc);
99e4d43a
RM
773}
774
8e22ad32
RM
775static bool ath_has_valid_bslot(struct ath_softc *sc)
776{
777 struct ath_vif *avp;
778 int slot;
779 bool found = false;
780
781 for (slot = 0; slot < ATH_BCBUF; slot++) {
782 if (sc->beacon.bslot[slot]) {
783 avp = (void *)sc->beacon.bslot[slot]->drv_priv;
784 if (avp->is_bslot_active) {
785 found = true;
786 break;
787 }
788 }
789 }
790 return found;
791}
792
793
99e4d43a
RM
794void ath_set_beacon(struct ath_softc *sc)
795{
796 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
797 struct ath_beacon_config *cur_conf = &sc->cur_beacon_conf;
798
26cd322b 799 switch (sc->sc_ah->opmode) {
6b96f93e 800 case NL80211_IFTYPE_AP:
8e22ad32
RM
801 if (ath_has_valid_bslot(sc))
802 ath_beacon_config_ap(sc, cur_conf);
6b96f93e
VT
803 break;
804 case NL80211_IFTYPE_ADHOC:
805 case NL80211_IFTYPE_MESH_POINT:
ee832d3e 806 ath_beacon_config_adhoc(sc, cur_conf);
6b96f93e
VT
807 break;
808 case NL80211_IFTYPE_STATION:
809 ath_beacon_config_sta(sc, cur_conf);
810 break;
811 default:
d2182b69 812 ath_dbg(common, CONFIG, "Unsupported beaconing mode\n");
6b96f93e
VT
813 return;
814 }
815
781b14a3 816 set_bit(SC_OP_BEACONS, &sc->sc_flags);
f078f209 817}
014cf3bb
RM
818
819void ath9k_set_beaconing_status(struct ath_softc *sc, bool status)
820{
821 struct ath_hw *ah = sc->sc_ah;
014cf3bb 822
ed2578cd 823 if (!ath_has_valid_bslot(sc)) {
781b14a3 824 clear_bit(SC_OP_BEACONS, &sc->sc_flags);
99e4d43a 825 return;
ed2578cd 826 }
99e4d43a
RM
827
828 ath9k_ps_wakeup(sc);
829 if (status) {
830 /* Re-enable beaconing */
831 ah->imask |= ATH9K_INT_SWBA;
72d874c6 832 ath9k_hw_set_interrupts(ah);
014cf3bb
RM
833 } else {
834 /* Disable SWBA interrupt */
835 ah->imask &= ~ATH9K_INT_SWBA;
72d874c6 836 ath9k_hw_set_interrupts(ah);
014cf3bb 837 tasklet_kill(&sc->bcon_tasklet);
efff395e 838 ath9k_hw_stop_dma_queue(ah, sc->beacon.beaconq);
014cf3bb
RM
839 }
840 ath9k_ps_restore(sc);
841}
This page took 0.6509 seconds and 5 git commands to generate.