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