Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Qualcomm Atheros, 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 | #include "ath9k.h" |
| 18 | |
| 19 | /* Set/change channels. If the channel is really being changed, it's done |
| 20 | * by reseting the chip. To accomplish this we must first cleanup any pending |
| 21 | * DMA, then restart stuff. |
| 22 | */ |
| 23 | static int ath_set_channel(struct ath_softc *sc) |
| 24 | { |
| 25 | struct ath_hw *ah = sc->sc_ah; |
| 26 | struct ath_common *common = ath9k_hw_common(ah); |
| 27 | struct ieee80211_hw *hw = sc->hw; |
| 28 | struct ath9k_channel *hchan; |
| 29 | struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef; |
| 30 | struct ieee80211_channel *chan = chandef->chan; |
| 31 | int pos = chan->hw_value; |
| 32 | int old_pos = -1; |
| 33 | int r; |
| 34 | |
| 35 | if (test_bit(ATH_OP_INVALID, &common->op_flags)) |
| 36 | return -EIO; |
| 37 | |
| 38 | if (ah->curchan) |
| 39 | old_pos = ah->curchan - &ah->channels[0]; |
| 40 | |
| 41 | ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n", |
| 42 | chan->center_freq, chandef->width); |
| 43 | |
| 44 | /* update survey stats for the old channel before switching */ |
| 45 | spin_lock_bh(&common->cc_lock); |
| 46 | ath_update_survey_stats(sc); |
| 47 | spin_unlock_bh(&common->cc_lock); |
| 48 | |
| 49 | ath9k_cmn_get_channel(hw, ah, chandef); |
| 50 | |
| 51 | /* If the operating channel changes, change the survey in-use flags |
| 52 | * along with it. |
| 53 | * Reset the survey data for the new channel, unless we're switching |
| 54 | * back to the operating channel from an off-channel operation. |
| 55 | */ |
| 56 | if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) { |
| 57 | if (sc->cur_survey) |
| 58 | sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE; |
| 59 | |
| 60 | sc->cur_survey = &sc->survey[pos]; |
| 61 | |
| 62 | memset(sc->cur_survey, 0, sizeof(struct survey_info)); |
| 63 | sc->cur_survey->filled |= SURVEY_INFO_IN_USE; |
| 64 | } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) { |
| 65 | memset(&sc->survey[pos], 0, sizeof(struct survey_info)); |
| 66 | } |
| 67 | |
| 68 | hchan = &sc->sc_ah->channels[pos]; |
| 69 | r = ath_reset_internal(sc, hchan); |
| 70 | if (r) |
| 71 | return r; |
| 72 | |
| 73 | /* The most recent snapshot of channel->noisefloor for the old |
| 74 | * channel is only available after the hardware reset. Copy it to |
| 75 | * the survey stats now. |
| 76 | */ |
| 77 | if (old_pos >= 0) |
| 78 | ath_update_survey_nf(sc, old_pos); |
| 79 | |
| 80 | /* Enable radar pulse detection if on a DFS channel. Spectral |
| 81 | * scanning and radar detection can not be used concurrently. |
| 82 | */ |
| 83 | if (hw->conf.radar_enabled) { |
| 84 | u32 rxfilter; |
| 85 | |
| 86 | /* set HW specific DFS configuration */ |
| 87 | ath9k_hw_set_radar_params(ah); |
| 88 | rxfilter = ath9k_hw_getrxfilter(ah); |
| 89 | rxfilter |= ATH9K_RX_FILTER_PHYRADAR | |
| 90 | ATH9K_RX_FILTER_PHYERR; |
| 91 | ath9k_hw_setrxfilter(ah, rxfilter); |
| 92 | ath_dbg(common, DFS, "DFS enabled at freq %d\n", |
| 93 | chan->center_freq); |
| 94 | } else { |
| 95 | /* perform spectral scan if requested. */ |
| 96 | if (test_bit(ATH_OP_SCANNING, &common->op_flags) && |
| 97 | sc->spectral_mode == SPECTRAL_CHANSCAN) |
| 98 | ath9k_spectral_scan_trigger(hw); |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Felix Fietkau | c083ce9 | 2014-06-11 16:17:54 +0530 | [diff] [blame] | 104 | void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx) |
| 105 | { |
Felix Fietkau | 26f16c2 | 2014-06-11 16:18:00 +0530 | [diff] [blame] | 106 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
Felix Fietkau | c083ce9 | 2014-06-11 16:17:54 +0530 | [diff] [blame] | 107 | struct ath_vif *avp; |
| 108 | bool active = false; |
Felix Fietkau | 26f16c2 | 2014-06-11 16:18:00 +0530 | [diff] [blame] | 109 | u8 n_active = 0; |
Felix Fietkau | c083ce9 | 2014-06-11 16:17:54 +0530 | [diff] [blame] | 110 | |
| 111 | if (!ctx) |
| 112 | return; |
| 113 | |
| 114 | list_for_each_entry(avp, &ctx->vifs, list) { |
| 115 | struct ieee80211_vif *vif = avp->vif; |
| 116 | |
| 117 | switch (vif->type) { |
| 118 | case NL80211_IFTYPE_P2P_CLIENT: |
| 119 | case NL80211_IFTYPE_STATION: |
| 120 | if (vif->bss_conf.assoc) |
| 121 | active = true; |
| 122 | break; |
| 123 | default: |
| 124 | active = true; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | ctx->active = active; |
Felix Fietkau | 26f16c2 | 2014-06-11 16:18:00 +0530 | [diff] [blame] | 129 | |
| 130 | ath_for_each_chanctx(sc, ctx) { |
| 131 | if (!ctx->assigned || list_empty(&ctx->vifs)) |
| 132 | continue; |
| 133 | n_active++; |
| 134 | } |
| 135 | |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 136 | if (n_active <= 1) { |
Felix Fietkau | 26f16c2 | 2014-06-11 16:18:00 +0530 | [diff] [blame] | 137 | clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags); |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) |
| 141 | return; |
| 142 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL); |
Felix Fietkau | c083ce9 | 2014-06-11 16:17:54 +0530 | [diff] [blame] | 143 | } |
| 144 | |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 145 | void ath_chanctx_init(struct ath_softc *sc) |
| 146 | { |
| 147 | struct ath_chanctx *ctx; |
| 148 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
| 149 | struct ieee80211_supported_band *sband; |
| 150 | struct ieee80211_channel *chan; |
Felix Fietkau | 0453531 | 2014-06-11 16:17:51 +0530 | [diff] [blame] | 151 | int i, j; |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 152 | |
| 153 | sband = &common->sbands[IEEE80211_BAND_2GHZ]; |
| 154 | if (!sband->n_channels) |
| 155 | sband = &common->sbands[IEEE80211_BAND_5GHZ]; |
| 156 | |
| 157 | chan = &sband->channels[0]; |
| 158 | for (i = 0; i < ATH9K_NUM_CHANCTX; i++) { |
| 159 | ctx = &sc->chanctx[i]; |
| 160 | cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20); |
| 161 | INIT_LIST_HEAD(&ctx->vifs); |
Felix Fietkau | bc7e1be | 2014-06-11 16:17:50 +0530 | [diff] [blame] | 162 | ctx->txpower = ATH_TXPOWER_MAX; |
Felix Fietkau | 0453531 | 2014-06-11 16:17:51 +0530 | [diff] [blame] | 163 | for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) |
| 164 | INIT_LIST_HEAD(&ctx->acq[j]); |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 165 | } |
Felix Fietkau | 78b2194 | 2014-06-11 16:17:55 +0530 | [diff] [blame] | 166 | ctx = &sc->offchannel.chan; |
| 167 | cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20); |
| 168 | INIT_LIST_HEAD(&ctx->vifs); |
| 169 | ctx->txpower = ATH_TXPOWER_MAX; |
| 170 | for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) |
| 171 | INIT_LIST_HEAD(&ctx->acq[j]); |
| 172 | sc->offchannel.chan.offchannel = true; |
| 173 | |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 174 | } |
| 175 | |
Felix Fietkau | bff1176 | 2014-06-11 16:17:52 +0530 | [diff] [blame] | 176 | void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx, |
| 177 | struct cfg80211_chan_def *chandef) |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 178 | { |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 179 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 180 | |
Felix Fietkau | bff1176 | 2014-06-11 16:17:52 +0530 | [diff] [blame] | 181 | spin_lock_bh(&sc->chan_lock); |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 182 | |
| 183 | if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) && |
| 184 | (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) { |
| 185 | sc->sched.offchannel_pending = true; |
| 186 | spin_unlock_bh(&sc->chan_lock); |
| 187 | return; |
| 188 | } |
| 189 | |
Felix Fietkau | bff1176 | 2014-06-11 16:17:52 +0530 | [diff] [blame] | 190 | sc->next_chan = ctx; |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 191 | if (chandef) { |
Felix Fietkau | bff1176 | 2014-06-11 16:17:52 +0530 | [diff] [blame] | 192 | ctx->chandef = *chandef; |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 193 | ath_dbg(common, CHAN_CTX, |
| 194 | "Assigned next_chan to %d MHz\n", chandef->center_freq1); |
| 195 | } |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 196 | |
| 197 | if (sc->next_chan == &sc->offchannel.chan) { |
| 198 | sc->sched.offchannel_duration = |
| 199 | TU_TO_USEC(sc->offchannel.duration) + |
| 200 | sc->sched.channel_switch_time; |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 201 | |
| 202 | if (chandef) { |
| 203 | ath_dbg(common, CHAN_CTX, |
| 204 | "Offchannel duration for chan %d MHz : %u\n", |
| 205 | chandef->center_freq1, |
| 206 | sc->sched.offchannel_duration); |
| 207 | } |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 208 | } |
Felix Fietkau | bff1176 | 2014-06-11 16:17:52 +0530 | [diff] [blame] | 209 | spin_unlock_bh(&sc->chan_lock); |
| 210 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); |
| 211 | } |
| 212 | |
| 213 | void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx, |
| 214 | struct cfg80211_chan_def *chandef) |
| 215 | { |
| 216 | bool cur_chan; |
| 217 | |
| 218 | spin_lock_bh(&sc->chan_lock); |
| 219 | if (chandef) |
| 220 | memcpy(&ctx->chandef, chandef, sizeof(*chandef)); |
| 221 | cur_chan = sc->cur_chan == ctx; |
| 222 | spin_unlock_bh(&sc->chan_lock); |
| 223 | |
| 224 | if (!cur_chan) |
| 225 | return; |
| 226 | |
| 227 | ath_set_channel(sc); |
Felix Fietkau | fbbcd14 | 2014-06-11 16:17:49 +0530 | [diff] [blame] | 228 | } |
Felix Fietkau | 78b2194 | 2014-06-11 16:17:55 +0530 | [diff] [blame] | 229 | |
Rajkumar Manoharan | c4dc0d0 | 2014-06-11 16:17:58 +0530 | [diff] [blame] | 230 | struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active) |
Felix Fietkau | 78b2194 | 2014-06-11 16:17:55 +0530 | [diff] [blame] | 231 | { |
Rajkumar Manoharan | c4dc0d0 | 2014-06-11 16:17:58 +0530 | [diff] [blame] | 232 | struct ath_chanctx *ctx; |
Felix Fietkau | 78b2194 | 2014-06-11 16:17:55 +0530 | [diff] [blame] | 233 | |
Rajkumar Manoharan | c4dc0d0 | 2014-06-11 16:17:58 +0530 | [diff] [blame] | 234 | ath_for_each_chanctx(sc, ctx) { |
| 235 | if (!ctx->assigned || list_empty(&ctx->vifs)) |
| 236 | continue; |
| 237 | if (active && !ctx->active) |
| 238 | continue; |
| 239 | |
Felix Fietkau | 8eab2510 | 2014-06-11 16:18:07 +0530 | [diff] [blame] | 240 | if (ctx->switch_after_beacon) |
| 241 | return ctx; |
Felix Fietkau | 78b2194 | 2014-06-11 16:17:55 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return &sc->chanctx[0]; |
| 245 | } |
| 246 | |
Felix Fietkau | 58b5737 | 2014-06-11 16:18:08 +0530 | [diff] [blame] | 247 | static struct ath_chanctx * |
| 248 | ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx) |
| 249 | { |
| 250 | int idx = ctx - &sc->chanctx[0]; |
| 251 | |
| 252 | return &sc->chanctx[!idx]; |
| 253 | } |
| 254 | |
| 255 | static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc) |
| 256 | { |
| 257 | struct ath_chanctx *prev, *cur; |
| 258 | struct timespec ts; |
| 259 | u32 cur_tsf, prev_tsf, beacon_int; |
| 260 | s32 offset; |
| 261 | |
| 262 | beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval); |
| 263 | |
| 264 | cur = sc->cur_chan; |
| 265 | prev = ath_chanctx_get_next(sc, cur); |
| 266 | |
| 267 | getrawmonotonic(&ts); |
| 268 | cur_tsf = (u32) cur->tsf_val + |
| 269 | ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts); |
| 270 | |
| 271 | prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf; |
| 272 | prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts); |
| 273 | |
| 274 | /* Adjust the TSF time of the AP chanctx to keep its beacons |
| 275 | * at half beacon interval offset relative to the STA chanctx. |
| 276 | */ |
| 277 | offset = cur_tsf - prev_tsf; |
| 278 | |
| 279 | /* Ignore stale data or spurious timestamps */ |
| 280 | if (offset < 0 || offset > 3 * beacon_int) |
| 281 | return; |
| 282 | |
| 283 | offset = beacon_int / 2 - (offset % beacon_int); |
| 284 | prev->tsf_val += offset; |
| 285 | } |
| 286 | |
Felix Fietkau | 42eda11 | 2014-06-11 16:18:14 +0530 | [diff] [blame] | 287 | /* Configure the TSF based hardware timer for a channel switch. |
| 288 | * Also set up backup software timer, in case the gen timer fails. |
| 289 | * This could be caused by a hardware reset. |
| 290 | */ |
| 291 | static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time) |
| 292 | { |
| 293 | struct ath_hw *ah = sc->sc_ah; |
| 294 | |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 295 | #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT |
Felix Fietkau | 42eda11 | 2014-06-11 16:18:14 +0530 | [diff] [blame] | 296 | ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000); |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 297 | #endif |
Felix Fietkau | 42eda11 | 2014-06-11 16:18:14 +0530 | [diff] [blame] | 298 | tsf_time -= ath9k_hw_gettsf32(ah); |
| 299 | tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1; |
| 300 | mod_timer(&sc->sched.timer, tsf_time); |
| 301 | } |
| 302 | |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 303 | void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif, |
| 304 | enum ath_chanctx_event ev) |
| 305 | { |
| 306 | struct ath_hw *ah = sc->sc_ah; |
Felix Fietkau | 58b5737 | 2014-06-11 16:18:08 +0530 | [diff] [blame] | 307 | struct ath_common *common = ath9k_hw_common(ah); |
Felix Fietkau | 7414863 | 2014-06-11 16:18:11 +0530 | [diff] [blame] | 308 | struct ath_beacon_config *cur_conf; |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 309 | struct ath_vif *avp = NULL; |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 310 | struct ath_chanctx *ctx; |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 311 | u32 tsf_time; |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 312 | u32 beacon_int; |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 313 | bool noa_changed = false; |
| 314 | |
| 315 | if (vif) |
| 316 | avp = (struct ath_vif *) vif->drv_priv; |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 317 | |
| 318 | spin_lock_bh(&sc->chan_lock); |
| 319 | |
| 320 | switch (ev) { |
| 321 | case ATH_CHANCTX_EVENT_BEACON_PREPARE: |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 322 | if (avp->offchannel_duration) |
| 323 | avp->offchannel_duration = 0; |
| 324 | |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 325 | if (avp->chanctx != sc->cur_chan) |
| 326 | break; |
| 327 | |
| 328 | if (sc->sched.offchannel_pending) { |
| 329 | sc->sched.offchannel_pending = false; |
| 330 | sc->next_chan = &sc->offchannel.chan; |
| 331 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; |
| 332 | } |
| 333 | |
| 334 | ctx = ath_chanctx_get_next(sc, sc->cur_chan); |
| 335 | if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) { |
| 336 | sc->next_chan = ctx; |
| 337 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; |
| 338 | } |
| 339 | |
| 340 | /* if the timer missed its window, use the next interval */ |
| 341 | if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) |
| 342 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; |
| 343 | |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 344 | if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) |
| 345 | break; |
| 346 | |
| 347 | sc->sched.beacon_pending = true; |
| 348 | sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER); |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 349 | |
Felix Fietkau | 7414863 | 2014-06-11 16:18:11 +0530 | [diff] [blame] | 350 | cur_conf = &sc->cur_chan->beacon; |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 351 | beacon_int = TU_TO_USEC(cur_conf->beacon_interval); |
| 352 | |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 353 | /* defer channel switch by a quarter beacon interval */ |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 354 | tsf_time = sc->sched.next_tbtt + beacon_int / 4; |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 355 | sc->sched.switch_start_time = tsf_time; |
Felix Fietkau | 58b5737 | 2014-06-11 16:18:08 +0530 | [diff] [blame] | 356 | sc->cur_chan->last_beacon = sc->sched.next_tbtt; |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 357 | |
Felix Fietkau | 7414863 | 2014-06-11 16:18:11 +0530 | [diff] [blame] | 358 | /* Prevent wrap-around issues */ |
| 359 | if (avp->periodic_noa_duration && |
| 360 | tsf_time - avp->periodic_noa_start > BIT(30)) |
| 361 | avp->periodic_noa_duration = 0; |
| 362 | |
| 363 | if (ctx->active && !avp->periodic_noa_duration) { |
| 364 | avp->periodic_noa_start = tsf_time; |
| 365 | avp->periodic_noa_duration = |
| 366 | TU_TO_USEC(cur_conf->beacon_interval) / 2 - |
| 367 | sc->sched.channel_switch_time; |
| 368 | noa_changed = true; |
| 369 | } else if (!ctx->active && avp->periodic_noa_duration) { |
| 370 | avp->periodic_noa_duration = 0; |
| 371 | noa_changed = true; |
| 372 | } |
| 373 | |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 374 | /* If at least two consecutive beacons were missed on the STA |
| 375 | * chanctx, stay on the STA channel for one extra beacon period, |
| 376 | * to resync the timer properly. |
| 377 | */ |
| 378 | if (ctx->active && sc->sched.beacon_miss >= 2) |
| 379 | sc->sched.offchannel_duration = 3 * beacon_int / 2; |
| 380 | |
Felix Fietkau | 3ae07d3 | 2014-06-11 16:18:06 +0530 | [diff] [blame] | 381 | if (sc->sched.offchannel_duration) { |
| 382 | noa_changed = true; |
| 383 | avp->offchannel_start = tsf_time; |
| 384 | avp->offchannel_duration = |
| 385 | sc->sched.offchannel_duration; |
| 386 | } |
| 387 | |
| 388 | if (noa_changed) |
| 389 | avp->noa_index++; |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 390 | break; |
| 391 | case ATH_CHANCTX_EVENT_BEACON_SENT: |
| 392 | if (!sc->sched.beacon_pending) |
| 393 | break; |
| 394 | |
| 395 | sc->sched.beacon_pending = false; |
| 396 | if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) |
| 397 | break; |
| 398 | |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 399 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER; |
Felix Fietkau | 42eda11 | 2014-06-11 16:18:14 +0530 | [diff] [blame] | 400 | ath_chanctx_setup_timer(sc, sc->sched.switch_start_time); |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 401 | break; |
| 402 | case ATH_CHANCTX_EVENT_TSF_TIMER: |
| 403 | if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER) |
| 404 | break; |
| 405 | |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 406 | if (!sc->cur_chan->switch_after_beacon && |
| 407 | sc->sched.beacon_pending) |
| 408 | sc->sched.beacon_miss++; |
| 409 | |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 410 | sc->sched.state = ATH_CHANCTX_STATE_SWITCH; |
| 411 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); |
| 412 | break; |
Felix Fietkau | 58b5737 | 2014-06-11 16:18:08 +0530 | [diff] [blame] | 413 | case ATH_CHANCTX_EVENT_BEACON_RECEIVED: |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 414 | if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) || |
| 415 | sc->cur_chan == &sc->offchannel.chan) |
Felix Fietkau | 58b5737 | 2014-06-11 16:18:08 +0530 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | ath_chanctx_adjust_tbtt_delta(sc); |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 419 | sc->sched.beacon_pending = false; |
| 420 | sc->sched.beacon_miss = 0; |
Felix Fietkau | a899b67 | 2014-06-11 16:18:13 +0530 | [diff] [blame] | 421 | |
| 422 | /* TSF time might have been updated by the incoming beacon, |
| 423 | * need update the channel switch timer to reflect the change. |
| 424 | */ |
| 425 | tsf_time = sc->sched.switch_start_time; |
| 426 | tsf_time -= (u32) sc->cur_chan->tsf_val + |
| 427 | ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL); |
| 428 | tsf_time += ath9k_hw_gettsf32(ah); |
| 429 | |
Felix Fietkau | 42eda11 | 2014-06-11 16:18:14 +0530 | [diff] [blame] | 430 | |
| 431 | ath_chanctx_setup_timer(sc, tsf_time); |
Felix Fietkau | 58b5737 | 2014-06-11 16:18:08 +0530 | [diff] [blame] | 432 | break; |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 433 | case ATH_CHANCTX_EVENT_ASSOC: |
| 434 | if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE || |
| 435 | avp->chanctx != sc->cur_chan) |
| 436 | break; |
| 437 | |
| 438 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; |
| 439 | /* fall through */ |
| 440 | case ATH_CHANCTX_EVENT_SWITCH: |
| 441 | if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) || |
| 442 | sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE || |
| 443 | sc->cur_chan->switch_after_beacon || |
| 444 | sc->cur_chan == &sc->offchannel.chan) |
| 445 | break; |
| 446 | |
| 447 | /* If this is a station chanctx, stay active for a half |
| 448 | * beacon period (minus channel switch time) |
| 449 | */ |
| 450 | sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan); |
Felix Fietkau | 7414863 | 2014-06-11 16:18:11 +0530 | [diff] [blame] | 451 | cur_conf = &sc->cur_chan->beacon; |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 452 | |
| 453 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER; |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 454 | |
| 455 | tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2; |
| 456 | if (sc->sched.beacon_miss >= 2) { |
| 457 | sc->sched.beacon_miss = 0; |
| 458 | tsf_time *= 3; |
| 459 | } |
| 460 | |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 461 | tsf_time -= sc->sched.channel_switch_time; |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 462 | tsf_time += ath9k_hw_gettsf32(sc->sc_ah); |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 463 | sc->sched.switch_start_time = tsf_time; |
| 464 | |
Felix Fietkau | 42eda11 | 2014-06-11 16:18:14 +0530 | [diff] [blame] | 465 | ath_chanctx_setup_timer(sc, tsf_time); |
Felix Fietkau | ec70abe | 2014-06-11 16:18:12 +0530 | [diff] [blame] | 466 | sc->sched.beacon_pending = true; |
Felix Fietkau | 73fa2f2 | 2014-06-11 16:18:10 +0530 | [diff] [blame] | 467 | break; |
| 468 | case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL: |
| 469 | if (sc->cur_chan == &sc->offchannel.chan || |
| 470 | sc->cur_chan->switch_after_beacon) |
| 471 | break; |
| 472 | |
| 473 | sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan); |
| 474 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); |
| 475 | break; |
| 476 | case ATH_CHANCTX_EVENT_UNASSIGN: |
| 477 | if (sc->cur_chan->assigned) { |
| 478 | if (sc->next_chan && !sc->next_chan->assigned && |
| 479 | sc->next_chan != &sc->offchannel.chan) |
| 480 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | ctx = ath_chanctx_get_next(sc, sc->cur_chan); |
| 485 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; |
| 486 | if (!ctx->assigned) |
| 487 | break; |
| 488 | |
| 489 | sc->next_chan = ctx; |
| 490 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); |
| 491 | break; |
Felix Fietkau | 748299f | 2014-06-11 16:18:04 +0530 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | spin_unlock_bh(&sc->chan_lock); |
| 495 | } |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 496 | |
Sujith Manoharan | 22dc0de | 2014-08-23 13:29:12 +0530 | [diff] [blame] | 497 | #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT |
| 498 | |
| 499 | static const char *offchannel_state_string(enum ath_offchannel_state state) |
| 500 | { |
| 501 | #define case_rtn_string(val) case val: return #val |
| 502 | |
| 503 | switch (state) { |
| 504 | case_rtn_string(ATH_OFFCHANNEL_IDLE); |
| 505 | case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND); |
| 506 | case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT); |
| 507 | case_rtn_string(ATH_OFFCHANNEL_SUSPEND); |
| 508 | case_rtn_string(ATH_OFFCHANNEL_ROC_START); |
| 509 | case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT); |
| 510 | case_rtn_string(ATH_OFFCHANNEL_ROC_DONE); |
| 511 | default: |
| 512 | return "unknown"; |
| 513 | } |
| 514 | } |
| 515 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 516 | static int ath_scan_channel_duration(struct ath_softc *sc, |
| 517 | struct ieee80211_channel *chan) |
| 518 | { |
| 519 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; |
| 520 | |
| 521 | if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR)) |
| 522 | return (HZ / 9); /* ~110 ms */ |
| 523 | |
| 524 | return (HZ / 16); /* ~60 ms */ |
| 525 | } |
| 526 | |
Sujith Manoharan | 344ae6a | 2014-08-23 13:29:13 +0530 | [diff] [blame^] | 527 | static void ath_chanctx_offchan_switch(struct ath_softc *sc, |
| 528 | struct ieee80211_channel *chan) |
| 529 | { |
| 530 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
| 531 | struct cfg80211_chan_def chandef; |
| 532 | |
| 533 | cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT); |
| 534 | ath_dbg(common, CHAN_CTX, |
| 535 | "Channel definition created: %d MHz\n", chandef.center_freq1); |
| 536 | |
| 537 | ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef); |
| 538 | } |
| 539 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 540 | static void |
| 541 | ath_scan_next_channel(struct ath_softc *sc) |
| 542 | { |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 543 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 544 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; |
| 545 | struct ieee80211_channel *chan; |
| 546 | |
| 547 | if (sc->offchannel.scan_idx >= req->n_channels) { |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 548 | ath_dbg(common, CHAN_CTX, |
| 549 | "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n", |
| 550 | sc->offchannel.scan_idx, |
| 551 | req->n_channels); |
| 552 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 553 | sc->offchannel.state = ATH_OFFCHANNEL_IDLE; |
| 554 | ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false), |
| 555 | NULL); |
| 556 | return; |
| 557 | } |
| 558 | |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 559 | ath_dbg(common, CHAN_CTX, |
| 560 | "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n", |
| 561 | sc->offchannel.scan_idx); |
| 562 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 563 | chan = req->channels[sc->offchannel.scan_idx++]; |
| 564 | sc->offchannel.duration = ath_scan_channel_duration(sc, chan); |
| 565 | sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND; |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 566 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 567 | ath_chanctx_offchan_switch(sc, chan); |
| 568 | } |
| 569 | |
| 570 | void ath_offchannel_next(struct ath_softc *sc) |
| 571 | { |
| 572 | struct ieee80211_vif *vif; |
| 573 | |
| 574 | if (sc->offchannel.scan_req) { |
| 575 | vif = sc->offchannel.scan_vif; |
| 576 | sc->offchannel.chan.txpower = vif->bss_conf.txpower; |
| 577 | ath_scan_next_channel(sc); |
| 578 | } else if (sc->offchannel.roc_vif) { |
| 579 | vif = sc->offchannel.roc_vif; |
| 580 | sc->offchannel.chan.txpower = vif->bss_conf.txpower; |
| 581 | sc->offchannel.duration = sc->offchannel.roc_duration; |
| 582 | sc->offchannel.state = ATH_OFFCHANNEL_ROC_START; |
| 583 | ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan); |
| 584 | } else { |
| 585 | ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false), |
| 586 | NULL); |
| 587 | sc->offchannel.state = ATH_OFFCHANNEL_IDLE; |
| 588 | if (sc->ps_idle) |
| 589 | ath_cancel_work(sc); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | void ath_roc_complete(struct ath_softc *sc, bool abort) |
| 594 | { |
| 595 | sc->offchannel.roc_vif = NULL; |
| 596 | sc->offchannel.roc_chan = NULL; |
| 597 | if (!abort) |
| 598 | ieee80211_remain_on_channel_expired(sc->hw); |
| 599 | ath_offchannel_next(sc); |
| 600 | ath9k_ps_restore(sc); |
| 601 | } |
| 602 | |
| 603 | void ath_scan_complete(struct ath_softc *sc, bool abort) |
| 604 | { |
| 605 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
| 606 | |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 607 | if (abort) |
| 608 | ath_dbg(common, CHAN_CTX, "HW scan aborted\n"); |
| 609 | else |
| 610 | ath_dbg(common, CHAN_CTX, "HW scan complete\n"); |
| 611 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 612 | sc->offchannel.scan_req = NULL; |
| 613 | sc->offchannel.scan_vif = NULL; |
| 614 | sc->offchannel.state = ATH_OFFCHANNEL_IDLE; |
| 615 | ieee80211_scan_completed(sc->hw, abort); |
| 616 | clear_bit(ATH_OP_SCANNING, &common->op_flags); |
| 617 | ath_offchannel_next(sc); |
| 618 | ath9k_ps_restore(sc); |
| 619 | } |
| 620 | |
| 621 | static void ath_scan_send_probe(struct ath_softc *sc, |
| 622 | struct cfg80211_ssid *ssid) |
| 623 | { |
| 624 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; |
| 625 | struct ieee80211_vif *vif = sc->offchannel.scan_vif; |
| 626 | struct ath_tx_control txctl = {}; |
| 627 | struct sk_buff *skb; |
| 628 | struct ieee80211_tx_info *info; |
| 629 | int band = sc->offchannel.chan.chandef.chan->band; |
| 630 | |
| 631 | skb = ieee80211_probereq_get(sc->hw, vif, |
| 632 | ssid->ssid, ssid->ssid_len, req->ie_len); |
| 633 | if (!skb) |
| 634 | return; |
| 635 | |
| 636 | info = IEEE80211_SKB_CB(skb); |
| 637 | if (req->no_cck) |
| 638 | info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE; |
| 639 | |
| 640 | if (req->ie_len) |
| 641 | memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len); |
| 642 | |
| 643 | skb_set_queue_mapping(skb, IEEE80211_AC_VO); |
| 644 | |
| 645 | if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL)) |
| 646 | goto error; |
| 647 | |
| 648 | txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO]; |
| 649 | txctl.force_channel = true; |
| 650 | if (ath_tx_start(sc->hw, skb, &txctl)) |
| 651 | goto error; |
| 652 | |
| 653 | return; |
| 654 | |
| 655 | error: |
| 656 | ieee80211_free_txskb(sc->hw, skb); |
| 657 | } |
| 658 | |
| 659 | static void ath_scan_channel_start(struct ath_softc *sc) |
| 660 | { |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 661 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 662 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; |
| 663 | int i; |
| 664 | |
| 665 | if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) && |
| 666 | req->n_ssids) { |
| 667 | for (i = 0; i < req->n_ssids; i++) |
| 668 | ath_scan_send_probe(sc, &req->ssids[i]); |
| 669 | |
| 670 | } |
| 671 | |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 672 | ath_dbg(common, CHAN_CTX, |
| 673 | "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n"); |
| 674 | |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 675 | sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT; |
| 676 | mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration); |
| 677 | } |
| 678 | |
Sujith Manoharan | 705d0bf | 2014-08-23 13:29:06 +0530 | [diff] [blame] | 679 | static void ath_chanctx_timer(unsigned long data) |
| 680 | { |
| 681 | struct ath_softc *sc = (struct ath_softc *) data; |
| 682 | |
| 683 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER); |
| 684 | } |
| 685 | |
| 686 | static void ath_offchannel_timer(unsigned long data) |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 687 | { |
| 688 | struct ath_softc *sc = (struct ath_softc *)data; |
| 689 | struct ath_chanctx *ctx; |
Sujith Manoharan | bc81d43 | 2014-08-22 20:39:27 +0530 | [diff] [blame] | 690 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
| 691 | |
| 692 | ath_dbg(common, CHAN_CTX, "%s: state: %s\n", |
| 693 | __func__, offchannel_state_string(sc->offchannel.state)); |
Sujith Manoharan | dfcbb3e | 2014-08-22 20:39:26 +0530 | [diff] [blame] | 694 | |
| 695 | switch (sc->offchannel.state) { |
| 696 | case ATH_OFFCHANNEL_PROBE_WAIT: |
| 697 | if (!sc->offchannel.scan_req) |
| 698 | return; |
| 699 | |
| 700 | /* get first active channel context */ |
| 701 | ctx = ath_chanctx_get_oper_chan(sc, true); |
| 702 | if (ctx->active) { |
| 703 | sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND; |
| 704 | ath_chanctx_switch(sc, ctx, NULL); |
| 705 | mod_timer(&sc->offchannel.timer, jiffies + HZ / 10); |
| 706 | break; |
| 707 | } |
| 708 | /* fall through */ |
| 709 | case ATH_OFFCHANNEL_SUSPEND: |
| 710 | if (!sc->offchannel.scan_req) |
| 711 | return; |
| 712 | |
| 713 | ath_scan_next_channel(sc); |
| 714 | break; |
| 715 | case ATH_OFFCHANNEL_ROC_START: |
| 716 | case ATH_OFFCHANNEL_ROC_WAIT: |
| 717 | ctx = ath_chanctx_get_oper_chan(sc, false); |
| 718 | sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE; |
| 719 | ath_chanctx_switch(sc, ctx, NULL); |
| 720 | break; |
| 721 | default: |
| 722 | break; |
| 723 | } |
| 724 | } |
Sujith Manoharan | 2471adf | 2014-08-22 20:39:29 +0530 | [diff] [blame] | 725 | |
Sujith Manoharan | 6d7cbd7 | 2014-08-23 13:29:10 +0530 | [diff] [blame] | 726 | static bool |
| 727 | ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp, |
| 728 | bool powersave) |
| 729 | { |
| 730 | struct ieee80211_vif *vif = avp->vif; |
| 731 | struct ieee80211_sta *sta = NULL; |
| 732 | struct ieee80211_hdr_3addr *nullfunc; |
| 733 | struct ath_tx_control txctl; |
| 734 | struct sk_buff *skb; |
| 735 | int band = sc->cur_chan->chandef.chan->band; |
| 736 | |
| 737 | switch (vif->type) { |
| 738 | case NL80211_IFTYPE_STATION: |
| 739 | if (!vif->bss_conf.assoc) |
| 740 | return false; |
| 741 | |
| 742 | skb = ieee80211_nullfunc_get(sc->hw, vif); |
| 743 | if (!skb) |
| 744 | return false; |
| 745 | |
| 746 | nullfunc = (struct ieee80211_hdr_3addr *) skb->data; |
| 747 | if (powersave) |
| 748 | nullfunc->frame_control |= |
| 749 | cpu_to_le16(IEEE80211_FCTL_PM); |
| 750 | |
| 751 | skb_set_queue_mapping(skb, IEEE80211_AC_VO); |
| 752 | if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) { |
| 753 | dev_kfree_skb_any(skb); |
| 754 | return false; |
| 755 | } |
| 756 | break; |
| 757 | default: |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | memset(&txctl, 0, sizeof(txctl)); |
| 762 | txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO]; |
| 763 | txctl.sta = sta; |
| 764 | txctl.force_channel = true; |
| 765 | if (ath_tx_start(sc->hw, skb, &txctl)) { |
| 766 | ieee80211_free_txskb(sc->hw, skb); |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | return true; |
| 771 | } |
| 772 | |
| 773 | static bool |
| 774 | ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave) |
| 775 | { |
| 776 | struct ath_vif *avp; |
| 777 | bool sent = false; |
| 778 | |
| 779 | rcu_read_lock(); |
| 780 | list_for_each_entry(avp, &sc->cur_chan->vifs, list) { |
| 781 | if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave)) |
| 782 | sent = true; |
| 783 | } |
| 784 | rcu_read_unlock(); |
| 785 | |
| 786 | return sent; |
| 787 | } |
| 788 | |
| 789 | static bool ath_chanctx_defer_switch(struct ath_softc *sc) |
| 790 | { |
| 791 | if (sc->cur_chan == &sc->offchannel.chan) |
| 792 | return false; |
| 793 | |
| 794 | switch (sc->sched.state) { |
| 795 | case ATH_CHANCTX_STATE_SWITCH: |
| 796 | return false; |
| 797 | case ATH_CHANCTX_STATE_IDLE: |
| 798 | if (!sc->cur_chan->switch_after_beacon) |
| 799 | return false; |
| 800 | |
| 801 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; |
| 802 | break; |
| 803 | default: |
| 804 | break; |
| 805 | } |
| 806 | |
| 807 | return true; |
| 808 | } |
| 809 | |
Sujith Manoharan | 55254ee | 2014-08-23 13:29:11 +0530 | [diff] [blame] | 810 | static void ath_offchannel_channel_change(struct ath_softc *sc) |
| 811 | { |
| 812 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
| 813 | |
| 814 | ath_dbg(common, CHAN_CTX, "%s: state: %s\n", |
| 815 | __func__, offchannel_state_string(sc->offchannel.state)); |
| 816 | |
| 817 | switch (sc->offchannel.state) { |
| 818 | case ATH_OFFCHANNEL_PROBE_SEND: |
| 819 | if (!sc->offchannel.scan_req) |
| 820 | return; |
| 821 | |
| 822 | if (sc->cur_chan->chandef.chan != |
| 823 | sc->offchannel.chan.chandef.chan) |
| 824 | return; |
| 825 | |
| 826 | ath_scan_channel_start(sc); |
| 827 | break; |
| 828 | case ATH_OFFCHANNEL_IDLE: |
| 829 | if (!sc->offchannel.scan_req) |
| 830 | return; |
| 831 | |
| 832 | ath_scan_complete(sc, false); |
| 833 | break; |
| 834 | case ATH_OFFCHANNEL_ROC_START: |
| 835 | if (sc->cur_chan != &sc->offchannel.chan) |
| 836 | break; |
| 837 | |
| 838 | sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT; |
| 839 | mod_timer(&sc->offchannel.timer, jiffies + |
| 840 | msecs_to_jiffies(sc->offchannel.duration)); |
| 841 | ieee80211_ready_on_channel(sc->hw); |
| 842 | break; |
| 843 | case ATH_OFFCHANNEL_ROC_DONE: |
| 844 | ath_roc_complete(sc, false); |
| 845 | break; |
| 846 | default: |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | |
Sujith Manoharan | 6d7cbd7 | 2014-08-23 13:29:10 +0530 | [diff] [blame] | 851 | void ath_chanctx_set_next(struct ath_softc *sc, bool force) |
| 852 | { |
| 853 | struct timespec ts; |
| 854 | bool measure_time = false; |
| 855 | bool send_ps = false; |
| 856 | |
| 857 | spin_lock_bh(&sc->chan_lock); |
| 858 | if (!sc->next_chan) { |
| 859 | spin_unlock_bh(&sc->chan_lock); |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | if (!force && ath_chanctx_defer_switch(sc)) { |
| 864 | spin_unlock_bh(&sc->chan_lock); |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | if (sc->cur_chan != sc->next_chan) { |
| 869 | sc->cur_chan->stopped = true; |
| 870 | spin_unlock_bh(&sc->chan_lock); |
| 871 | |
| 872 | if (sc->next_chan == &sc->offchannel.chan) { |
| 873 | getrawmonotonic(&ts); |
| 874 | measure_time = true; |
| 875 | } |
| 876 | __ath9k_flush(sc->hw, ~0, true); |
| 877 | |
| 878 | if (ath_chanctx_send_ps_frame(sc, true)) |
| 879 | __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false); |
| 880 | |
| 881 | send_ps = true; |
| 882 | spin_lock_bh(&sc->chan_lock); |
| 883 | |
| 884 | if (sc->cur_chan != &sc->offchannel.chan) { |
| 885 | getrawmonotonic(&sc->cur_chan->tsf_ts); |
| 886 | sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah); |
| 887 | } |
| 888 | } |
| 889 | sc->cur_chan = sc->next_chan; |
| 890 | sc->cur_chan->stopped = false; |
| 891 | sc->next_chan = NULL; |
| 892 | sc->sched.offchannel_duration = 0; |
| 893 | if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE) |
| 894 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; |
| 895 | |
| 896 | spin_unlock_bh(&sc->chan_lock); |
| 897 | |
| 898 | if (sc->sc_ah->chip_fullsleep || |
| 899 | memcmp(&sc->cur_chandef, &sc->cur_chan->chandef, |
| 900 | sizeof(sc->cur_chandef))) { |
| 901 | ath_set_channel(sc); |
| 902 | if (measure_time) |
| 903 | sc->sched.channel_switch_time = |
| 904 | ath9k_hw_get_tsf_offset(&ts, NULL); |
| 905 | } |
| 906 | if (send_ps) |
| 907 | ath_chanctx_send_ps_frame(sc, false); |
| 908 | |
| 909 | ath_offchannel_channel_change(sc); |
| 910 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH); |
| 911 | } |
| 912 | |
Sujith Manoharan | 0e62f8b | 2014-08-23 13:29:08 +0530 | [diff] [blame] | 913 | static void ath_chanctx_work(struct work_struct *work) |
| 914 | { |
| 915 | struct ath_softc *sc = container_of(work, struct ath_softc, |
| 916 | chanctx_work); |
| 917 | mutex_lock(&sc->mutex); |
| 918 | ath_chanctx_set_next(sc, false); |
| 919 | mutex_unlock(&sc->mutex); |
| 920 | } |
| 921 | |
Sujith Manoharan | 705d0bf | 2014-08-23 13:29:06 +0530 | [diff] [blame] | 922 | void ath9k_init_channel_context(struct ath_softc *sc) |
| 923 | { |
| 924 | INIT_WORK(&sc->chanctx_work, ath_chanctx_work); |
| 925 | |
| 926 | setup_timer(&sc->offchannel.timer, ath_offchannel_timer, |
| 927 | (unsigned long)sc); |
| 928 | setup_timer(&sc->sched.timer, ath_chanctx_timer, |
| 929 | (unsigned long)sc); |
| 930 | } |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 931 | |
Sujith Manoharan | ea22df2 | 2014-08-23 13:29:07 +0530 | [diff] [blame] | 932 | void ath9k_deinit_channel_context(struct ath_softc *sc) |
| 933 | { |
| 934 | cancel_work_sync(&sc->chanctx_work); |
| 935 | } |
| 936 | |
Sujith Manoharan | 499afac | 2014-08-22 20:39:31 +0530 | [diff] [blame] | 937 | bool ath9k_is_chanctx_enabled(void) |
| 938 | { |
| 939 | return (ath9k_use_chanctx == 1); |
| 940 | } |
| 941 | |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 942 | /*****************/ |
| 943 | /* P2P Powersave */ |
| 944 | /*****************/ |
| 945 | |
| 946 | static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp) |
Sujith Manoharan | 2471adf | 2014-08-22 20:39:29 +0530 | [diff] [blame] | 947 | { |
| 948 | struct ath_hw *ah = sc->sc_ah; |
| 949 | s32 tsf, target_tsf; |
| 950 | |
| 951 | if (!avp || !avp->noa.has_next_tsf) |
| 952 | return; |
| 953 | |
| 954 | ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer); |
| 955 | |
| 956 | tsf = ath9k_hw_gettsf32(sc->sc_ah); |
| 957 | |
| 958 | target_tsf = avp->noa.next_tsf; |
| 959 | if (!avp->noa.absent) |
| 960 | target_tsf -= ATH_P2P_PS_STOP_TIME; |
| 961 | |
| 962 | if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME) |
| 963 | target_tsf = tsf + ATH_P2P_PS_STOP_TIME; |
| 964 | |
| 965 | ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000); |
| 966 | } |
| 967 | |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 968 | static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif) |
| 969 | { |
| 970 | struct ath_vif *avp = (void *)vif->drv_priv; |
| 971 | u32 tsf; |
| 972 | |
| 973 | if (!sc->p2p_ps_timer) |
| 974 | return; |
| 975 | |
| 976 | if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p) |
| 977 | return; |
| 978 | |
| 979 | sc->p2p_ps_vif = avp; |
| 980 | tsf = ath9k_hw_gettsf32(sc->sc_ah); |
| 981 | ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf); |
| 982 | ath9k_update_p2p_ps_timer(sc, avp); |
| 983 | } |
| 984 | |
Sujith Manoharan | 2471adf | 2014-08-22 20:39:29 +0530 | [diff] [blame] | 985 | void ath9k_p2p_ps_timer(void *priv) |
| 986 | { |
| 987 | struct ath_softc *sc = priv; |
| 988 | struct ath_vif *avp = sc->p2p_ps_vif; |
| 989 | struct ieee80211_vif *vif; |
| 990 | struct ieee80211_sta *sta; |
| 991 | struct ath_node *an; |
| 992 | u32 tsf; |
| 993 | |
| 994 | del_timer_sync(&sc->sched.timer); |
| 995 | ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer); |
| 996 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER); |
| 997 | |
| 998 | if (!avp || avp->chanctx != sc->cur_chan) |
| 999 | return; |
| 1000 | |
| 1001 | tsf = ath9k_hw_gettsf32(sc->sc_ah); |
| 1002 | if (!avp->noa.absent) |
| 1003 | tsf += ATH_P2P_PS_STOP_TIME; |
| 1004 | |
| 1005 | if (!avp->noa.has_next_tsf || |
| 1006 | avp->noa.next_tsf - tsf > BIT(31)) |
| 1007 | ieee80211_update_p2p_noa(&avp->noa, tsf); |
| 1008 | |
| 1009 | ath9k_update_p2p_ps_timer(sc, avp); |
| 1010 | |
| 1011 | rcu_read_lock(); |
| 1012 | |
| 1013 | vif = avp->vif; |
| 1014 | sta = ieee80211_find_sta(vif, vif->bss_conf.bssid); |
| 1015 | if (!sta) |
| 1016 | goto out; |
| 1017 | |
| 1018 | an = (void *) sta->drv_priv; |
| 1019 | if (an->sleeping == !!avp->noa.absent) |
| 1020 | goto out; |
| 1021 | |
| 1022 | an->sleeping = avp->noa.absent; |
| 1023 | if (an->sleeping) |
| 1024 | ath_tx_aggr_sleep(sta, sc, an); |
| 1025 | else |
| 1026 | ath_tx_aggr_wakeup(sc, an); |
| 1027 | |
| 1028 | out: |
| 1029 | rcu_read_unlock(); |
| 1030 | } |
| 1031 | |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 1032 | void ath9k_p2p_bss_info_changed(struct ath_softc *sc, |
| 1033 | struct ieee80211_vif *vif) |
| 1034 | { |
| 1035 | unsigned long flags; |
| 1036 | |
| 1037 | spin_lock_bh(&sc->sc_pcu_lock); |
| 1038 | spin_lock_irqsave(&sc->sc_pm_lock, flags); |
| 1039 | if (!(sc->ps_flags & PS_BEACON_SYNC)) |
| 1040 | ath9k_update_p2p_ps(sc, vif); |
| 1041 | spin_unlock_irqrestore(&sc->sc_pm_lock, flags); |
| 1042 | spin_unlock_bh(&sc->sc_pcu_lock); |
| 1043 | } |
| 1044 | |
| 1045 | void ath9k_p2p_beacon_sync(struct ath_softc *sc) |
| 1046 | { |
| 1047 | if (sc->p2p_ps_vif) |
| 1048 | ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif); |
| 1049 | } |
| 1050 | |
| 1051 | void ath9k_p2p_remove_vif(struct ath_softc *sc, |
| 1052 | struct ieee80211_vif *vif) |
Sujith Manoharan | 2471adf | 2014-08-22 20:39:29 +0530 | [diff] [blame] | 1053 | { |
| 1054 | struct ath_vif *avp = (void *)vif->drv_priv; |
Sujith Manoharan | 2471adf | 2014-08-22 20:39:29 +0530 | [diff] [blame] | 1055 | |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 1056 | spin_lock_bh(&sc->sc_pcu_lock); |
| 1057 | if (avp == sc->p2p_ps_vif) { |
| 1058 | sc->p2p_ps_vif = NULL; |
| 1059 | ath9k_update_p2p_ps_timer(sc, NULL); |
| 1060 | } |
| 1061 | spin_unlock_bh(&sc->sc_pcu_lock); |
Sujith Manoharan | 2471adf | 2014-08-22 20:39:29 +0530 | [diff] [blame] | 1062 | } |
Sujith Manoharan | c7dd40c | 2014-08-22 20:39:30 +0530 | [diff] [blame] | 1063 | |
| 1064 | int ath9k_init_p2p(struct ath_softc *sc) |
| 1065 | { |
| 1066 | sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer, |
| 1067 | NULL, sc, AR_FIRST_NDP_TIMER); |
| 1068 | if (!sc->p2p_ps_timer) |
| 1069 | return -ENOMEM; |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | void ath9k_deinit_p2p(struct ath_softc *sc) |
| 1075 | { |
| 1076 | if (sc->p2p_ps_timer) |
| 1077 | ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer); |
| 1078 | } |
| 1079 | |
| 1080 | #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */ |