ath9k: Fix offchannel operation
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / channel.c
index c54a3df..a31f526 100644 (file)
 
 #include "ath9k.h"
 
-static const char *offchannel_state_string(enum ath_offchannel_state state)
-{
-#define case_rtn_string(val) case val: return #val
-
-       switch (state) {
-               case_rtn_string(ATH_OFFCHANNEL_IDLE);
-               case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
-               case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
-               case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
-               case_rtn_string(ATH_OFFCHANNEL_ROC_START);
-               case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
-               case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
-       default:
-               return "unknown";
-       }
-}
-
 /* Set/change channels.  If the channel is really being changed, it's done
  * by reseting the chip.  To accomplish this we must first cleanup any pending
  * DMA, then restart stuff.
@@ -118,51 +101,100 @@ static int ath_set_channel(struct ath_softc *sc)
        return 0;
 }
 
-static bool
-ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
-                             bool powersave)
+void ath_chanctx_init(struct ath_softc *sc)
 {
-       struct ieee80211_vif *vif = avp->vif;
-       struct ieee80211_sta *sta = NULL;
-       struct ieee80211_hdr_3addr *nullfunc;
-       struct ath_tx_control txctl;
-       struct sk_buff *skb;
-       int band = sc->cur_chan->chandef.chan->band;
+       struct ath_chanctx *ctx;
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       struct ieee80211_supported_band *sband;
+       struct ieee80211_channel *chan;
+       int i, j;
 
-       switch (vif->type) {
-       case NL80211_IFTYPE_STATION:
-               if (!vif->bss_conf.assoc)
-                       return false;
+       sband = &common->sbands[IEEE80211_BAND_2GHZ];
+       if (!sband->n_channels)
+               sband = &common->sbands[IEEE80211_BAND_5GHZ];
 
-               skb = ieee80211_nullfunc_get(sc->hw, vif);
-               if (!skb)
-                       return false;
+       chan = &sband->channels[0];
+       for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
+               ctx = &sc->chanctx[i];
+               cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
+               INIT_LIST_HEAD(&ctx->vifs);
+               ctx->txpower = ATH_TXPOWER_MAX;
+               for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
+                       INIT_LIST_HEAD(&ctx->acq[j]);
+       }
+}
 
-               nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
-               if (powersave)
-                       nullfunc->frame_control |=
-                               cpu_to_le16(IEEE80211_FCTL_PM);
+void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
+                            struct cfg80211_chan_def *chandef)
+{
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       bool cur_chan;
 
-               skb_set_queue_mapping(skb, IEEE80211_AC_VO);
-               if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
-                       dev_kfree_skb_any(skb);
-                       return false;
-               }
-               break;
+       spin_lock_bh(&sc->chan_lock);
+       if (chandef)
+               memcpy(&ctx->chandef, chandef, sizeof(*chandef));
+       cur_chan = sc->cur_chan == ctx;
+       spin_unlock_bh(&sc->chan_lock);
+
+       if (!cur_chan) {
+               ath_dbg(common, CHAN_CTX,
+                       "Current context differs from the new context\n");
+               return;
+       }
+
+       ath_set_channel(sc);
+}
+
+#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
+
+/**********************************************************/
+/* Functions to handle the channel context state machine. */
+/**********************************************************/
+
+static const char *offchannel_state_string(enum ath_offchannel_state state)
+{
+       switch (state) {
+               case_rtn_string(ATH_OFFCHANNEL_IDLE);
+               case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
+               case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
+               case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
+               case_rtn_string(ATH_OFFCHANNEL_ROC_START);
+               case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
+               case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
        default:
-               return false;
+               return "unknown";
        }
+}
 
-       memset(&txctl, 0, sizeof(txctl));
-       txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
-       txctl.sta = sta;
-       txctl.force_channel = true;
-       if (ath_tx_start(sc->hw, skb, &txctl)) {
-               ieee80211_free_txskb(sc->hw, skb);
-               return false;
+static const char *chanctx_event_string(enum ath_chanctx_event ev)
+{
+       switch (ev) {
+               case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
+               case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
+               case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
+               case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
+               case_rtn_string(ATH_CHANCTX_EVENT_ASSOC);
+               case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
+               case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
+               case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
+               case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
+               case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
+       default:
+               return "unknown";
        }
+}
 
-       return true;
+static const char *chanctx_state_string(enum ath_chanctx_state state)
+{
+       switch (state) {
+               case_rtn_string(ATH_CHANCTX_STATE_IDLE);
+               case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
+               case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
+               case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
+               case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
+       default:
+               return "unknown";
+       }
 }
 
 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
@@ -203,369 +235,132 @@ void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
        }
        if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
                return;
-       ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
-}
-
-static bool
-ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
-{
-       struct ath_vif *avp;
-       bool sent = false;
 
-       rcu_read_lock();
-       list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
-               if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
-                       sent = true;
+       if (ath9k_is_chanctx_enabled()) {
+               ath_chanctx_event(sc, NULL,
+                                 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
        }
-       rcu_read_unlock();
-
-       return sent;
 }
 
-static bool ath_chanctx_defer_switch(struct ath_softc *sc)
+static struct ath_chanctx *
+ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
 {
-       if (sc->cur_chan == &sc->offchannel.chan)
-               return false;
-
-       switch (sc->sched.state) {
-       case ATH_CHANCTX_STATE_SWITCH:
-               return false;
-       case ATH_CHANCTX_STATE_IDLE:
-               if (!sc->cur_chan->switch_after_beacon)
-                       return false;
-
-               sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
-               break;
-       default:
-               break;
-       }
+       int idx = ctx - &sc->chanctx[0];
 
-       return true;
+       return &sc->chanctx[!idx];
 }
 
-static void ath_chanctx_set_next(struct ath_softc *sc, bool force)
+static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
 {
+       struct ath_chanctx *prev, *cur;
        struct timespec ts;
-       bool measure_time = false;
-       bool send_ps = false;
-
-       spin_lock_bh(&sc->chan_lock);
-       if (!sc->next_chan) {
-               spin_unlock_bh(&sc->chan_lock);
-               return;
-       }
-
-       if (!force && ath_chanctx_defer_switch(sc)) {
-               spin_unlock_bh(&sc->chan_lock);
-               return;
-       }
-
-       if (sc->cur_chan != sc->next_chan) {
-               sc->cur_chan->stopped = true;
-               spin_unlock_bh(&sc->chan_lock);
+       u32 cur_tsf, prev_tsf, beacon_int;
+       s32 offset;
 
-               if (sc->next_chan == &sc->offchannel.chan) {
-                       getrawmonotonic(&ts);
-                       measure_time = true;
-               }
-               __ath9k_flush(sc->hw, ~0, true);
+       beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
 
-               if (ath_chanctx_send_ps_frame(sc, true))
-                       __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
+       cur = sc->cur_chan;
+       prev = ath_chanctx_get_next(sc, cur);
 
-               send_ps = true;
-               spin_lock_bh(&sc->chan_lock);
+       getrawmonotonic(&ts);
+       cur_tsf = (u32) cur->tsf_val +
+                 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
 
-               if (sc->cur_chan != &sc->offchannel.chan) {
-                       getrawmonotonic(&sc->cur_chan->tsf_ts);
-                       sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
-               }
-       }
-       sc->cur_chan = sc->next_chan;
-       sc->cur_chan->stopped = false;
-       sc->next_chan = NULL;
-       sc->sched.offchannel_duration = 0;
-       if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
-               sc->sched.state = ATH_CHANCTX_STATE_IDLE;
+       prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
+       prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
 
-       spin_unlock_bh(&sc->chan_lock);
+       /* Adjust the TSF time of the AP chanctx to keep its beacons
+        * at half beacon interval offset relative to the STA chanctx.
+        */
+       offset = cur_tsf - prev_tsf;
 
-       if (sc->sc_ah->chip_fullsleep ||
-           memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
-                  sizeof(sc->cur_chandef))) {
-               ath_set_channel(sc);
-               if (measure_time)
-                       sc->sched.channel_switch_time =
-                               ath9k_hw_get_tsf_offset(&ts, NULL);
-       }
-       if (send_ps)
-               ath_chanctx_send_ps_frame(sc, false);
+       /* Ignore stale data or spurious timestamps */
+       if (offset < 0 || offset > 3 * beacon_int)
+               return;
 
-       ath_offchannel_channel_change(sc);
-       ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
+       offset = beacon_int / 2 - (offset % beacon_int);
+       prev->tsf_val += offset;
 }
 
-void ath_chanctx_work(struct work_struct *work)
+/* Configure the TSF based hardware timer for a channel switch.
+ * Also set up backup software timer, in case the gen timer fails.
+ * This could be caused by a hardware reset.
+ */
+static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
 {
-       struct ath_softc *sc = container_of(work, struct ath_softc,
-                                           chanctx_work);
-       mutex_lock(&sc->mutex);
-       ath_chanctx_set_next(sc, false);
-       mutex_unlock(&sc->mutex);
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       struct ath_hw *ah = sc->sc_ah;
+
+       ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
+       tsf_time -= ath9k_hw_gettsf32(ah);
+       tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
+       mod_timer(&sc->sched.timer, jiffies + tsf_time);
+
+       ath_dbg(common, CHAN_CTX,
+               "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time));
 }
 
-void ath_chanctx_init(struct ath_softc *sc)
+void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
+                      enum ath_chanctx_event ev)
 {
+       struct ath_hw *ah = sc->sc_ah;
+       struct ath_common *common = ath9k_hw_common(ah);
+       struct ath_beacon_config *cur_conf;
+       struct ath_vif *avp = NULL;
        struct ath_chanctx *ctx;
-       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
-       struct ieee80211_supported_band *sband;
-       struct ieee80211_channel *chan;
-       int i, j;
+       u32 tsf_time;
+       u32 beacon_int;
+       bool noa_changed = false;
 
-       sband = &common->sbands[IEEE80211_BAND_2GHZ];
-       if (!sband->n_channels)
-               sband = &common->sbands[IEEE80211_BAND_5GHZ];
-
-       chan = &sband->channels[0];
-       for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
-               ctx = &sc->chanctx[i];
-               cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
-               INIT_LIST_HEAD(&ctx->vifs);
-               ctx->txpower = ATH_TXPOWER_MAX;
-               for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
-                       INIT_LIST_HEAD(&ctx->acq[j]);
-       }
-       ctx = &sc->offchannel.chan;
-       cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
-       INIT_LIST_HEAD(&ctx->vifs);
-       ctx->txpower = ATH_TXPOWER_MAX;
-       for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
-               INIT_LIST_HEAD(&ctx->acq[j]);
-       sc->offchannel.chan.offchannel = true;
-
-}
-
-void ath9k_chanctx_force_active(struct ieee80211_hw *hw,
-                               struct ieee80211_vif *vif)
-{
-       struct ath_softc *sc = hw->priv;
-       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
-       struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
-       bool changed = false;
-
-       if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
-               return;
-
-       if (!avp->chanctx)
-               return;
-
-       mutex_lock(&sc->mutex);
-
-       spin_lock_bh(&sc->chan_lock);
-       if (sc->next_chan || (sc->cur_chan != avp->chanctx)) {
-               sc->next_chan = avp->chanctx;
-               changed = true;
-       }
-       sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
-       spin_unlock_bh(&sc->chan_lock);
-
-       if (changed)
-               ath_chanctx_set_next(sc, true);
-
-       mutex_unlock(&sc->mutex);
-}
-
-void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
-                       struct cfg80211_chan_def *chandef)
-{
-       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
-
-       spin_lock_bh(&sc->chan_lock);
-
-       if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
-           (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
-               sc->sched.offchannel_pending = true;
-               spin_unlock_bh(&sc->chan_lock);
-               return;
-       }
-
-       sc->next_chan = ctx;
-       if (chandef) {
-               ctx->chandef = *chandef;
-               ath_dbg(common, CHAN_CTX,
-                       "Assigned next_chan to %d MHz\n", chandef->center_freq1);
-       }
-
-       if (sc->next_chan == &sc->offchannel.chan) {
-               sc->sched.offchannel_duration =
-                       TU_TO_USEC(sc->offchannel.duration) +
-                       sc->sched.channel_switch_time;
-
-               if (chandef) {
-                       ath_dbg(common, CHAN_CTX,
-                               "Offchannel duration for chan %d MHz : %u\n",
-                               chandef->center_freq1,
-                               sc->sched.offchannel_duration);
-               }
-       }
-       spin_unlock_bh(&sc->chan_lock);
-       ieee80211_queue_work(sc->hw, &sc->chanctx_work);
-}
-
-void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
-                            struct cfg80211_chan_def *chandef)
-{
-       bool cur_chan;
+       if (vif)
+               avp = (struct ath_vif *) vif->drv_priv;
 
        spin_lock_bh(&sc->chan_lock);
-       if (chandef)
-               memcpy(&ctx->chandef, chandef, sizeof(*chandef));
-       cur_chan = sc->cur_chan == ctx;
-       spin_unlock_bh(&sc->chan_lock);
-
-       if (!cur_chan)
-               return;
-
-       ath_set_channel(sc);
-}
-
-struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
-{
-       struct ath_chanctx *ctx;
-
-       ath_for_each_chanctx(sc, ctx) {
-               if (!ctx->assigned || list_empty(&ctx->vifs))
-                       continue;
-               if (active && !ctx->active)
-                       continue;
-
-               if (ctx->switch_after_beacon)
-                       return ctx;
-       }
-
-       return &sc->chanctx[0];
-}
-
-void ath_chanctx_offchan_switch(struct ath_softc *sc,
-                               struct ieee80211_channel *chan)
-{
-       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
-       struct cfg80211_chan_def chandef;
-
-       cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
-       ath_dbg(common, CHAN_CTX,
-               "Channel definition created: %d MHz\n", chandef.center_freq1);
-
-       ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
-}
-
-static struct ath_chanctx *
-ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
-{
-       int idx = ctx - &sc->chanctx[0];
-
-       return &sc->chanctx[!idx];
-}
-
-static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
-{
-       struct ath_chanctx *prev, *cur;
-       struct timespec ts;
-       u32 cur_tsf, prev_tsf, beacon_int;
-       s32 offset;
-
-       beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
-
-       cur = sc->cur_chan;
-       prev = ath_chanctx_get_next(sc, cur);
-
-       getrawmonotonic(&ts);
-       cur_tsf = (u32) cur->tsf_val +
-                 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
-
-       prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
-       prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
-
-       /* Adjust the TSF time of the AP chanctx to keep its beacons
-        * at half beacon interval offset relative to the STA chanctx.
-        */
-       offset = cur_tsf - prev_tsf;
-
-       /* Ignore stale data or spurious timestamps */
-       if (offset < 0 || offset > 3 * beacon_int)
-               return;
-
-       offset = beacon_int / 2 - (offset % beacon_int);
-       prev->tsf_val += offset;
-}
-
-void ath_chanctx_timer(unsigned long data)
-{
-       struct ath_softc *sc = (struct ath_softc *) data;
-
-       ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
-}
-
-/* Configure the TSF based hardware timer for a channel switch.
- * Also set up backup software timer, in case the gen timer fails.
- * This could be caused by a hardware reset.
- */
-static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
-{
-       struct ath_hw *ah = sc->sc_ah;
-
-#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
-       ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
-#endif
-       tsf_time -= ath9k_hw_gettsf32(ah);
-       tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
-       mod_timer(&sc->sched.timer, tsf_time);
-}
-
-void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
-                      enum ath_chanctx_event ev)
-{
-       struct ath_hw *ah = sc->sc_ah;
-       struct ath_common *common = ath9k_hw_common(ah);
-       struct ath_beacon_config *cur_conf;
-       struct ath_vif *avp = NULL;
-       struct ath_chanctx *ctx;
-       u32 tsf_time;
-       u32 beacon_int;
-       bool noa_changed = false;
-
-       if (vif)
-               avp = (struct ath_vif *) vif->drv_priv;
 
-       spin_lock_bh(&sc->chan_lock);
+       ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
+               sc->cur_chan->chandef.center_freq1,
+               chanctx_event_string(ev),
+               chanctx_state_string(sc->sched.state));
 
        switch (ev) {
        case ATH_CHANCTX_EVENT_BEACON_PREPARE:
                if (avp->offchannel_duration)
                        avp->offchannel_duration = 0;
 
-               if (avp->chanctx != sc->cur_chan)
+               if (avp->chanctx != sc->cur_chan) {
+                       ath_dbg(common, CHAN_CTX,
+                               "Contexts differ, not preparing beacon\n");
                        break;
+               }
 
                if (sc->sched.offchannel_pending) {
                        sc->sched.offchannel_pending = false;
                        sc->next_chan = &sc->offchannel.chan;
                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
+                       ath_dbg(common, CHAN_CTX,
+                               "Setting offchannel_pending to false\n");
                }
 
                ctx = ath_chanctx_get_next(sc, sc->cur_chan);
                if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
                        sc->next_chan = ctx;
                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
+                       ath_dbg(common, CHAN_CTX,
+                               "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
                }
 
                /* if the timer missed its window, use the next interval */
-               if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
+               if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
+                       ath_dbg(common, CHAN_CTX,
+                               "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
+               }
 
                if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
                        break;
 
+               ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
+
                sc->sched.beacon_pending = true;
                sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
 
@@ -582,13 +377,13 @@ void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
                    tsf_time - avp->periodic_noa_start > BIT(30))
                        avp->periodic_noa_duration = 0;
 
-               if (ctx->active && !avp->periodic_noa_duration) {
+               if (ctx->active) {
                        avp->periodic_noa_start = tsf_time;
                        avp->periodic_noa_duration =
                                TU_TO_USEC(cur_conf->beacon_interval) / 2 -
                                sc->sched.channel_switch_time;
                        noa_changed = true;
-               } else if (!ctx->active && avp->periodic_noa_duration) {
+               } else if (!ctx->active) {
                        avp->periodic_noa_duration = 0;
                        noa_changed = true;
                }
@@ -609,15 +404,28 @@ void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
 
                if (noa_changed)
                        avp->noa_index++;
+
+               ath_dbg(common, CHAN_CTX,
+                       "periodic_noa_duration: %d, periodic_noa_start: %d, noa_index: %d\n",
+                       avp->periodic_noa_duration,
+                       avp->periodic_noa_start,
+                       avp->noa_index);
+
                break;
        case ATH_CHANCTX_EVENT_BEACON_SENT:
-               if (!sc->sched.beacon_pending)
+               if (!sc->sched.beacon_pending) {
+                       ath_dbg(common, CHAN_CTX,
+                               "No pending beacon\n");
                        break;
+               }
 
                sc->sched.beacon_pending = false;
                if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
                        break;
 
+               ath_dbg(common, CHAN_CTX,
+                       "Move chanctx state to WAIT_FOR_TIMER\n");
+
                sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
                ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
                break;
@@ -629,6 +437,9 @@ void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
                    sc->sched.beacon_pending)
                        sc->sched.beacon_miss++;
 
+               ath_dbg(common, CHAN_CTX,
+                       "Move chanctx state to SWITCH\n");
+
                sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
                ieee80211_queue_work(sc->hw, &sc->chanctx_work);
                break;
@@ -657,6 +468,9 @@ void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
                    avp->chanctx != sc->cur_chan)
                        break;
 
+               ath_dbg(common, CHAN_CTX,
+                       "Move chanctx state from FORCE_ACTIVE to IDLE\n");
+
                sc->sched.state = ATH_CHANCTX_STATE_IDLE;
                /* fall through */
        case ATH_CHANCTX_EVENT_SWITCH:
@@ -672,6 +486,9 @@ void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
                sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
                cur_conf = &sc->cur_chan->beacon;
 
+               ath_dbg(common, CHAN_CTX,
+                       "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
+
                sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
 
                tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
@@ -711,11 +528,45 @@ void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
                sc->next_chan = ctx;
                ieee80211_queue_work(sc->hw, &sc->chanctx_work);
                break;
+       case ATH_CHANCTX_EVENT_ASSIGN:
+               /*
+                * When adding a new channel context, check if a scan
+                * is in progress and abort it since the addition of
+                * a new channel context is usually followed by VIF
+                * assignment, in which case we have to start multi-channel
+                * operation.
+                */
+               if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
+                       ath_dbg(common, CHAN_CTX,
+                               "Aborting HW scan to add new context\n");
+
+                       spin_unlock_bh(&sc->chan_lock);
+                       del_timer_sync(&sc->offchannel.timer);
+                       ath_scan_complete(sc, true);
+                       spin_lock_bh(&sc->chan_lock);
+               }
+               break;
+       case ATH_CHANCTX_EVENT_CHANGE:
+               break;
        }
 
        spin_unlock_bh(&sc->chan_lock);
 }
 
+void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
+                               enum ath_chanctx_event ev)
+{
+       if (sc->sched.beacon_pending)
+               ath_chanctx_event(sc, NULL, ev);
+}
+
+void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
+                               enum ath_chanctx_event ev)
+{
+       sc->sched.next_tbtt = ts;
+       ath_chanctx_event(sc, NULL, ev);
+}
+
 static int ath_scan_channel_duration(struct ath_softc *sc,
                                     struct ieee80211_channel *chan)
 {
@@ -727,39 +578,112 @@ static int ath_scan_channel_duration(struct ath_softc *sc,
        return (HZ / 16); /* ~60 ms */
 }
 
-static void
-ath_scan_next_channel(struct ath_softc *sc)
+static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
+                              struct cfg80211_chan_def *chandef)
 {
        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
-       struct cfg80211_scan_request *req = sc->offchannel.scan_req;
-       struct ieee80211_channel *chan;
 
-       if (sc->offchannel.scan_idx >= req->n_channels) {
-               ath_dbg(common, CHAN_CTX,
-                       "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
-                       sc->offchannel.scan_idx,
-                       req->n_channels);
+       spin_lock_bh(&sc->chan_lock);
 
-               sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
-               ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
-                                  NULL);
+       if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
+           (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
+               sc->sched.offchannel_pending = true;
+               if (chandef)
+                       ctx->chandef = *chandef;
+               spin_unlock_bh(&sc->chan_lock);
+               ath_dbg(common, CHAN_CTX,
+                       "Set offchannel_pending to true\n");
                return;
        }
 
-       ath_dbg(common, CHAN_CTX,
-               "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
-               sc->offchannel.scan_idx);
-
-       chan = req->channels[sc->offchannel.scan_idx++];
-       sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
-       sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
+       sc->next_chan = ctx;
+       if (chandef) {
+               ctx->chandef = *chandef;
+               ath_dbg(common, CHAN_CTX,
+                       "Assigned next_chan to %d MHz\n", chandef->center_freq1);
+       }
 
-       ath_chanctx_offchan_switch(sc, chan);
-}
+       if (sc->next_chan == &sc->offchannel.chan) {
+               sc->sched.offchannel_duration =
+                       jiffies_to_usecs(sc->offchannel.duration) +
+                       sc->sched.channel_switch_time;
 
-void ath_offchannel_next(struct ath_softc *sc)
-{
-       struct ieee80211_vif *vif;
+               if (chandef) {
+                       ath_dbg(common, CHAN_CTX,
+                               "Offchannel duration for chan %d MHz : %u\n",
+                               chandef->center_freq1,
+                               sc->sched.offchannel_duration);
+               }
+       }
+       spin_unlock_bh(&sc->chan_lock);
+       ieee80211_queue_work(sc->hw, &sc->chanctx_work);
+}
+
+static void ath_chanctx_offchan_switch(struct ath_softc *sc,
+                                      struct ieee80211_channel *chan)
+{
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       struct cfg80211_chan_def chandef;
+
+       cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
+       ath_dbg(common, CHAN_CTX,
+               "Channel definition created: %d MHz\n", chandef.center_freq1);
+
+       ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
+}
+
+static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
+                                                    bool active)
+{
+       struct ath_chanctx *ctx;
+
+       ath_for_each_chanctx(sc, ctx) {
+               if (!ctx->assigned || list_empty(&ctx->vifs))
+                       continue;
+               if (active && !ctx->active)
+                       continue;
+
+               if (ctx->switch_after_beacon)
+                       return ctx;
+       }
+
+       return &sc->chanctx[0];
+}
+
+static void
+ath_scan_next_channel(struct ath_softc *sc)
+{
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       struct cfg80211_scan_request *req = sc->offchannel.scan_req;
+       struct ieee80211_channel *chan;
+
+       if (sc->offchannel.scan_idx >= req->n_channels) {
+               ath_dbg(common, CHAN_CTX,
+                       "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
+                       "scan_idx: %d, n_channels: %d\n",
+                       sc->offchannel.scan_idx,
+                       req->n_channels);
+
+               sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
+               ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
+                                  NULL);
+               return;
+       }
+
+       ath_dbg(common, CHAN_CTX,
+               "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
+               sc->offchannel.scan_idx);
+
+       chan = req->channels[sc->offchannel.scan_idx++];
+       sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
+       sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
+
+       ath_chanctx_offchan_switch(sc, chan);
+}
+
+void ath_offchannel_next(struct ath_softc *sc)
+{
+       struct ieee80211_vif *vif;
 
        if (sc->offchannel.scan_req) {
                vif = sc->offchannel.scan_vif;
@@ -768,7 +692,8 @@ void ath_offchannel_next(struct ath_softc *sc)
        } else if (sc->offchannel.roc_vif) {
                vif = sc->offchannel.roc_vif;
                sc->offchannel.chan.txpower = vif->bss_conf.txpower;
-               sc->offchannel.duration = sc->offchannel.roc_duration;
+               sc->offchannel.duration =
+                       msecs_to_jiffies(sc->offchannel.roc_duration);
                sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
                ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
        } else {
@@ -860,17 +785,161 @@ static void ath_scan_channel_start(struct ath_softc *sc)
        }
 
        ath_dbg(common, CHAN_CTX,
-               "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
+               "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
 
        sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
        mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
 }
 
-void ath_offchannel_channel_change(struct ath_softc *sc)
+static void ath_chanctx_timer(unsigned long data)
+{
+       struct ath_softc *sc = (struct ath_softc *) data;
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+
+       ath_dbg(common, CHAN_CTX,
+               "Channel context timer invoked\n");
+
+       ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
+}
+
+static void ath_offchannel_timer(unsigned long data)
+{
+       struct ath_softc *sc = (struct ath_softc *)data;
+       struct ath_chanctx *ctx;
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+
+       ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
+               __func__, offchannel_state_string(sc->offchannel.state));
+
+       switch (sc->offchannel.state) {
+       case ATH_OFFCHANNEL_PROBE_WAIT:
+               if (!sc->offchannel.scan_req)
+                       return;
+
+               /* get first active channel context */
+               ctx = ath_chanctx_get_oper_chan(sc, true);
+               if (ctx->active) {
+                       ath_dbg(common, CHAN_CTX,
+                               "Switch to oper/active context, "
+                               "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
+
+                       sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
+                       ath_chanctx_switch(sc, ctx, NULL);
+                       mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
+                       break;
+               }
+               /* fall through */
+       case ATH_OFFCHANNEL_SUSPEND:
+               if (!sc->offchannel.scan_req)
+                       return;
+
+               ath_scan_next_channel(sc);
+               break;
+       case ATH_OFFCHANNEL_ROC_START:
+       case ATH_OFFCHANNEL_ROC_WAIT:
+               ctx = ath_chanctx_get_oper_chan(sc, false);
+               sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
+               ath_chanctx_switch(sc, ctx, NULL);
+               break;
+       default:
+               break;
+       }
+}
+
+static bool
+ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
+                             bool powersave)
+{
+       struct ieee80211_vif *vif = avp->vif;
+       struct ieee80211_sta *sta = NULL;
+       struct ieee80211_hdr_3addr *nullfunc;
+       struct ath_tx_control txctl;
+       struct sk_buff *skb;
+       int band = sc->cur_chan->chandef.chan->band;
+
+       switch (vif->type) {
+       case NL80211_IFTYPE_STATION:
+               if (!vif->bss_conf.assoc)
+                       return false;
+
+               skb = ieee80211_nullfunc_get(sc->hw, vif);
+               if (!skb)
+                       return false;
+
+               nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
+               if (powersave)
+                       nullfunc->frame_control |=
+                               cpu_to_le16(IEEE80211_FCTL_PM);
+
+               skb_set_queue_mapping(skb, IEEE80211_AC_VO);
+               if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
+                       dev_kfree_skb_any(skb);
+                       return false;
+               }
+               break;
+       default:
+               return false;
+       }
+
+       memset(&txctl, 0, sizeof(txctl));
+       txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
+       txctl.sta = sta;
+       txctl.force_channel = true;
+       if (ath_tx_start(sc->hw, skb, &txctl)) {
+               ieee80211_free_txskb(sc->hw, skb);
+               return false;
+       }
+
+       return true;
+}
+
+static bool
+ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
+{
+       struct ath_vif *avp;
+       bool sent = false;
+
+       rcu_read_lock();
+       list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
+               if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
+                       sent = true;
+       }
+       rcu_read_unlock();
+
+       return sent;
+}
+
+static bool ath_chanctx_defer_switch(struct ath_softc *sc)
 {
        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 
-       ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
+       if (sc->cur_chan == &sc->offchannel.chan)
+               return false;
+
+       switch (sc->sched.state) {
+       case ATH_CHANCTX_STATE_SWITCH:
+               return false;
+       case ATH_CHANCTX_STATE_IDLE:
+               if (!sc->cur_chan->switch_after_beacon)
+                       return false;
+
+               ath_dbg(common, CHAN_CTX,
+                       "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
+
+               sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
+               break;
+       default:
+               break;
+       }
+
+       return true;
+}
+
+static void ath_offchannel_channel_change(struct ath_softc *sc)
+{
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+
+       ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
                __func__, offchannel_state_string(sc->offchannel.state));
 
        switch (sc->offchannel.state) {
@@ -895,8 +964,8 @@ void ath_offchannel_channel_change(struct ath_softc *sc)
                        break;
 
                sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
-               mod_timer(&sc->offchannel.timer, jiffies +
-                         msecs_to_jiffies(sc->offchannel.duration));
+               mod_timer(&sc->offchannel.timer,
+                         jiffies + sc->offchannel.duration);
                ieee80211_ready_on_channel(sc->hw);
                break;
        case ATH_OFFCHANNEL_ROC_DONE:
@@ -907,47 +976,156 @@ void ath_offchannel_channel_change(struct ath_softc *sc)
        }
 }
 
-void ath_offchannel_timer(unsigned long data)
+void ath_chanctx_set_next(struct ath_softc *sc, bool force)
 {
-       struct ath_softc *sc = (struct ath_softc *)data;
-       struct ath_chanctx *ctx;
        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       struct timespec ts;
+       bool measure_time = false;
+       bool send_ps = false;
 
-       ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
-               __func__, offchannel_state_string(sc->offchannel.state));
+       spin_lock_bh(&sc->chan_lock);
+       if (!sc->next_chan) {
+               spin_unlock_bh(&sc->chan_lock);
+               return;
+       }
 
-       switch (sc->offchannel.state) {
-       case ATH_OFFCHANNEL_PROBE_WAIT:
-               if (!sc->offchannel.scan_req)
-                       return;
+       if (!force && ath_chanctx_defer_switch(sc)) {
+               spin_unlock_bh(&sc->chan_lock);
+               return;
+       }
 
-               /* get first active channel context */
-               ctx = ath_chanctx_get_oper_chan(sc, true);
-               if (ctx->active) {
-                       sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
-                       ath_chanctx_switch(sc, ctx, NULL);
-                       mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
-                       break;
+       ath_dbg(common, CHAN_CTX,
+               "%s: current: %d MHz, next: %d MHz\n",
+               __func__,
+               sc->cur_chan->chandef.center_freq1,
+               sc->next_chan->chandef.center_freq1);
+
+       if (sc->cur_chan != sc->next_chan) {
+               ath_dbg(common, CHAN_CTX,
+                       "Stopping current chanctx: %d\n",
+                       sc->cur_chan->chandef.center_freq1);
+               sc->cur_chan->stopped = true;
+               spin_unlock_bh(&sc->chan_lock);
+
+               if (sc->next_chan == &sc->offchannel.chan) {
+                       getrawmonotonic(&ts);
+                       measure_time = true;
                }
-               /* fall through */
-       case ATH_OFFCHANNEL_SUSPEND:
-               if (!sc->offchannel.scan_req)
-                       return;
+               __ath9k_flush(sc->hw, ~0, true);
 
-               ath_scan_next_channel(sc);
-               break;
-       case ATH_OFFCHANNEL_ROC_START:
-       case ATH_OFFCHANNEL_ROC_WAIT:
-               ctx = ath_chanctx_get_oper_chan(sc, false);
-               sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
-               ath_chanctx_switch(sc, ctx, NULL);
-               break;
-       default:
-               break;
+               if (ath_chanctx_send_ps_frame(sc, true))
+                       __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
+
+               send_ps = true;
+               spin_lock_bh(&sc->chan_lock);
+
+               if (sc->cur_chan != &sc->offchannel.chan) {
+                       getrawmonotonic(&sc->cur_chan->tsf_ts);
+                       sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
+               }
+       }
+       sc->cur_chan = sc->next_chan;
+       sc->cur_chan->stopped = false;
+       sc->next_chan = NULL;
+       sc->sched.offchannel_duration = 0;
+       if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
+               sc->sched.state = ATH_CHANCTX_STATE_IDLE;
+
+       spin_unlock_bh(&sc->chan_lock);
+
+       if (sc->sc_ah->chip_fullsleep ||
+           memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
+                  sizeof(sc->cur_chandef))) {
+               ath_dbg(common, CHAN_CTX,
+                       "%s: Set channel %d MHz\n",
+                       __func__, sc->cur_chan->chandef.center_freq1);
+               ath_set_channel(sc);
+               if (measure_time)
+                       sc->sched.channel_switch_time =
+                               ath9k_hw_get_tsf_offset(&ts, NULL);
        }
+       if (send_ps)
+               ath_chanctx_send_ps_frame(sc, false);
+
+       ath_offchannel_channel_change(sc);
+       ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
 }
 
-#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
+static void ath_chanctx_work(struct work_struct *work)
+{
+       struct ath_softc *sc = container_of(work, struct ath_softc,
+                                           chanctx_work);
+       mutex_lock(&sc->mutex);
+       ath_chanctx_set_next(sc, false);
+       mutex_unlock(&sc->mutex);
+}
+
+void ath9k_offchannel_init(struct ath_softc *sc)
+{
+       struct ath_chanctx *ctx;
+       struct ath_common *common = ath9k_hw_common(sc->sc_ah);
+       struct ieee80211_supported_band *sband;
+       struct ieee80211_channel *chan;
+       int i;
+
+       sband = &common->sbands[IEEE80211_BAND_2GHZ];
+       if (!sband->n_channels)
+               sband = &common->sbands[IEEE80211_BAND_5GHZ];
+
+       chan = &sband->channels[0];
+
+       ctx = &sc->offchannel.chan;
+       INIT_LIST_HEAD(&ctx->vifs);
+       ctx->txpower = ATH_TXPOWER_MAX;
+       cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
+
+       for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
+               INIT_LIST_HEAD(&ctx->acq[i]);
+
+       sc->offchannel.chan.offchannel = true;
+}
+
+void ath9k_init_channel_context(struct ath_softc *sc)
+{
+       INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
+
+       setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
+                   (unsigned long)sc);
+       setup_timer(&sc->sched.timer, ath_chanctx_timer,
+                   (unsigned long)sc);
+}
+
+void ath9k_deinit_channel_context(struct ath_softc *sc)
+{
+       cancel_work_sync(&sc->chanctx_work);
+}
+
+bool ath9k_is_chanctx_enabled(void)
+{
+       return (ath9k_use_chanctx == 1);
+}
+
+/********************/
+/* Queue management */
+/********************/
+
+void ath9k_chanctx_wake_queues(struct ath_softc *sc)
+{
+       struct ath_hw *ah = sc->sc_ah;
+       int i;
+
+       if (sc->cur_chan == &sc->offchannel.chan) {
+               ieee80211_wake_queue(sc->hw,
+                                    sc->hw->offchannel_tx_hw_queue);
+       } else {
+               for (i = 0; i < IEEE80211_NUM_ACS; i++)
+                       ieee80211_wake_queue(sc->hw,
+                                            sc->cur_chan->hw_queue_base + i);
+       }
+
+       if (ah->opmode == NL80211_IFTYPE_AP)
+               ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
+}
 
 /*****************/
 /* P2P Powersave */
@@ -992,6 +1170,81 @@ static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
        ath9k_update_p2p_ps_timer(sc, avp);
 }
 
+static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
+{
+       struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
+       u8 switch_time, ctwin;
+
+       /*
+        * Channel switch in multi-channel mode is deferred
+        * by a quarter beacon interval when handling
+        * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
+        * interface is guaranteed to be discoverable
+        * for that duration after a TBTT.
+        */
+       switch_time = cur_conf->beacon_interval / 4;
+
+       ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
+       if (ctwin && (ctwin < switch_time))
+               return ctwin;
+
+       if (switch_time < P2P_DEFAULT_CTWIN)
+               return 0;
+
+       return P2P_DEFAULT_CTWIN;
+}
+
+void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
+                         struct sk_buff *skb)
+{
+       static const u8 noa_ie_hdr[] = {
+               WLAN_EID_VENDOR_SPECIFIC,       /* type */
+               0,                              /* length */
+               0x50, 0x6f, 0x9a,               /* WFA OUI */
+               0x09,                           /* P2P subtype */
+               0x0c,                           /* Notice of Absence */
+               0x00,                           /* LSB of little-endian len */
+               0x00,                           /* MSB of little-endian len */
+       };
+
+       struct ieee80211_p2p_noa_attr *noa;
+       int noa_len, noa_desc, i = 0;
+       u8 *hdr;
+
+       if (!avp->offchannel_duration && !avp->periodic_noa_duration)
+               return;
+
+       noa_desc = !!avp->offchannel_duration + !!avp->periodic_noa_duration;
+       noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
+
+       hdr = skb_put(skb, sizeof(noa_ie_hdr));
+       memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
+       hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
+       hdr[7] = noa_len;
+
+       noa = (void *) skb_put(skb, noa_len);
+       memset(noa, 0, noa_len);
+
+       noa->index = avp->noa_index;
+       noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
+
+       if (avp->periodic_noa_duration) {
+               u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
+
+               noa->desc[i].count = 255;
+               noa->desc[i].start_time = cpu_to_le32(avp->periodic_noa_start);
+               noa->desc[i].duration = cpu_to_le32(avp->periodic_noa_duration);
+               noa->desc[i].interval = cpu_to_le32(interval);
+               i++;
+       }
+
+       if (avp->offchannel_duration) {
+               noa->desc[i].count = 1;
+               noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
+               noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
+       }
+}
+
 void ath9k_p2p_ps_timer(void *priv)
 {
        struct ath_softc *sc = priv;