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