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