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