Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (c) 2014 Qualcomm Atheros, Inc.
0003  *
0004  * Permission to use, copy, modify, and/or distribute this software for any
0005  * purpose with or without fee is hereby granted, provided that the above
0006  * copyright notice and this permission notice appear in all copies.
0007  *
0008  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0009  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0010  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0011  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0012  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0013  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0014  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0015  */
0016 
0017 #include "ath9k.h"
0018 
0019 /* Set/change channels.  If the channel is really being changed, it's done
0020  * by reseting the chip.  To accomplish this we must first cleanup any pending
0021  * DMA, then restart stuff.
0022  */
0023 static int ath_set_channel(struct ath_softc *sc)
0024 {
0025     struct ath_hw *ah = sc->sc_ah;
0026     struct ath_common *common = ath9k_hw_common(ah);
0027     struct ieee80211_hw *hw = sc->hw;
0028     struct ath9k_channel *hchan;
0029     struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
0030     struct ieee80211_channel *chan = chandef->chan;
0031     int pos = chan->hw_value;
0032     unsigned long flags;
0033     int old_pos = -1;
0034     int r;
0035 
0036     if (test_bit(ATH_OP_INVALID, &common->op_flags))
0037         return -EIO;
0038 
0039     if (ah->curchan)
0040         old_pos = ah->curchan - &ah->channels[0];
0041 
0042     ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
0043         chan->center_freq, chandef->width);
0044 
0045     /* update survey stats for the old channel before switching */
0046     spin_lock_irqsave(&common->cc_lock, flags);
0047     ath_update_survey_stats(sc);
0048     spin_unlock_irqrestore(&common->cc_lock, flags);
0049 
0050     ath9k_cmn_get_channel(hw, ah, chandef);
0051 
0052     /* If the operating channel changes, change the survey in-use flags
0053      * along with it.
0054      * Reset the survey data for the new channel, unless we're switching
0055      * back to the operating channel from an off-channel operation.
0056      */
0057     if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
0058         if (sc->cur_survey)
0059             sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
0060 
0061         sc->cur_survey = &sc->survey[pos];
0062 
0063         memset(sc->cur_survey, 0, sizeof(struct survey_info));
0064         sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
0065     } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
0066         memset(&sc->survey[pos], 0, sizeof(struct survey_info));
0067     }
0068 
0069     hchan = &sc->sc_ah->channels[pos];
0070     r = ath_reset(sc, hchan);
0071     if (r)
0072         return r;
0073 
0074     /* The most recent snapshot of channel->noisefloor for the old
0075      * channel is only available after the hardware reset. Copy it to
0076      * the survey stats now.
0077      */
0078     if (old_pos >= 0)
0079         ath_update_survey_nf(sc, old_pos);
0080 
0081     /* Enable radar pulse detection if on a DFS channel. Spectral
0082      * scanning and radar detection can not be used concurrently.
0083      */
0084     if (hw->conf.radar_enabled) {
0085         u32 rxfilter;
0086 
0087         rxfilter = ath9k_hw_getrxfilter(ah);
0088         rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
0089                 ATH9K_RX_FILTER_PHYERR;
0090         ath9k_hw_setrxfilter(ah, rxfilter);
0091         ath_dbg(common, DFS, "DFS enabled at freq %d\n",
0092             chan->center_freq);
0093     } else {
0094         /* perform spectral scan if requested. */
0095         if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
0096             sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
0097             ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
0098     }
0099 
0100     return 0;
0101 }
0102 
0103 void ath_chanctx_init(struct ath_softc *sc)
0104 {
0105     struct ath_chanctx *ctx;
0106     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0107     struct ieee80211_supported_band *sband;
0108     struct ieee80211_channel *chan;
0109     int i, j;
0110 
0111     sband = &common->sbands[NL80211_BAND_2GHZ];
0112     if (!sband->n_channels)
0113         sband = &common->sbands[NL80211_BAND_5GHZ];
0114 
0115     chan = &sband->channels[0];
0116     for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
0117         ctx = &sc->chanctx[i];
0118         cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
0119         INIT_LIST_HEAD(&ctx->vifs);
0120         ctx->txpower = ATH_TXPOWER_MAX;
0121         ctx->flush_timeout = HZ / 5; /* 200ms */
0122         for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) {
0123             INIT_LIST_HEAD(&ctx->acq[j].acq_new);
0124             INIT_LIST_HEAD(&ctx->acq[j].acq_old);
0125             spin_lock_init(&ctx->acq[j].lock);
0126         }
0127     }
0128 }
0129 
0130 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
0131                  struct cfg80211_chan_def *chandef)
0132 {
0133     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0134     bool cur_chan;
0135 
0136     spin_lock_bh(&sc->chan_lock);
0137     if (chandef)
0138         memcpy(&ctx->chandef, chandef, sizeof(*chandef));
0139     cur_chan = sc->cur_chan == ctx;
0140     spin_unlock_bh(&sc->chan_lock);
0141 
0142     if (!cur_chan) {
0143         ath_dbg(common, CHAN_CTX,
0144             "Current context differs from the new context\n");
0145         return;
0146     }
0147 
0148     ath_set_channel(sc);
0149 }
0150 
0151 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
0152 
0153 /*************/
0154 /* Utilities */
0155 /*************/
0156 
0157 struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
0158 {
0159     struct ath_chanctx *ctx;
0160     struct ath_vif *avp;
0161     struct ieee80211_vif *vif;
0162 
0163     spin_lock_bh(&sc->chan_lock);
0164 
0165     ath_for_each_chanctx(sc, ctx) {
0166         if (!ctx->active)
0167             continue;
0168 
0169         list_for_each_entry(avp, &ctx->vifs, list) {
0170             vif = avp->vif;
0171 
0172             if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
0173                 spin_unlock_bh(&sc->chan_lock);
0174                 return ctx;
0175             }
0176         }
0177     }
0178 
0179     spin_unlock_bh(&sc->chan_lock);
0180     return NULL;
0181 }
0182 
0183 /**********************************************************/
0184 /* Functions to handle the channel context state machine. */
0185 /**********************************************************/
0186 
0187 static const char *offchannel_state_string(enum ath_offchannel_state state)
0188 {
0189     switch (state) {
0190         case_rtn_string(ATH_OFFCHANNEL_IDLE);
0191         case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
0192         case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
0193         case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
0194         case_rtn_string(ATH_OFFCHANNEL_ROC_START);
0195         case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
0196         case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
0197     default:
0198         return "unknown";
0199     }
0200 }
0201 
0202 static const char *chanctx_event_string(enum ath_chanctx_event ev)
0203 {
0204     switch (ev) {
0205         case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
0206         case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
0207         case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
0208         case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
0209         case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
0210         case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
0211         case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
0212         case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
0213         case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
0214         case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
0215     default:
0216         return "unknown";
0217     }
0218 }
0219 
0220 static const char *chanctx_state_string(enum ath_chanctx_state state)
0221 {
0222     switch (state) {
0223         case_rtn_string(ATH_CHANCTX_STATE_IDLE);
0224         case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
0225         case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
0226         case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
0227         case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
0228     default:
0229         return "unknown";
0230     }
0231 }
0232 
0233 static u32 chanctx_event_delta(struct ath_softc *sc)
0234 {
0235     u64 ms;
0236     struct timespec64 ts, *old;
0237 
0238     ktime_get_raw_ts64(&ts);
0239     old = &sc->last_event_time;
0240     ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
0241     ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
0242     sc->last_event_time = ts;
0243 
0244     return (u32)ms;
0245 }
0246 
0247 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
0248 {
0249     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0250     struct ath_chanctx *ictx;
0251     struct ath_vif *avp;
0252     bool active = false;
0253     u8 n_active = 0;
0254 
0255     if (!ctx)
0256         return;
0257 
0258     if (ctx == &sc->offchannel.chan) {
0259         spin_lock_bh(&sc->chan_lock);
0260 
0261         if (likely(sc->sched.channel_switch_time))
0262             ctx->flush_timeout =
0263                 usecs_to_jiffies(sc->sched.channel_switch_time);
0264         else
0265             ctx->flush_timeout =
0266                 msecs_to_jiffies(10);
0267 
0268         spin_unlock_bh(&sc->chan_lock);
0269 
0270         /*
0271          * There is no need to iterate over the
0272          * active/assigned channel contexts if
0273          * the current context is offchannel.
0274          */
0275         return;
0276     }
0277 
0278     ictx = ctx;
0279 
0280     list_for_each_entry(avp, &ctx->vifs, list) {
0281         struct ieee80211_vif *vif = avp->vif;
0282 
0283         switch (vif->type) {
0284         case NL80211_IFTYPE_P2P_CLIENT:
0285         case NL80211_IFTYPE_STATION:
0286             if (avp->assoc)
0287                 active = true;
0288             break;
0289         default:
0290             active = true;
0291             break;
0292         }
0293     }
0294     ctx->active = active;
0295 
0296     ath_for_each_chanctx(sc, ctx) {
0297         if (!ctx->assigned || list_empty(&ctx->vifs))
0298             continue;
0299         n_active++;
0300     }
0301 
0302     spin_lock_bh(&sc->chan_lock);
0303 
0304     if (n_active <= 1) {
0305         ictx->flush_timeout = HZ / 5;
0306         clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
0307         spin_unlock_bh(&sc->chan_lock);
0308         return;
0309     }
0310 
0311     ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
0312 
0313     if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
0314         spin_unlock_bh(&sc->chan_lock);
0315         return;
0316     }
0317 
0318     spin_unlock_bh(&sc->chan_lock);
0319 
0320     if (ath9k_is_chanctx_enabled()) {
0321         ath_chanctx_event(sc, NULL,
0322                   ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
0323     }
0324 }
0325 
0326 static struct ath_chanctx *
0327 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
0328 {
0329     int idx = ctx - &sc->chanctx[0];
0330 
0331     return &sc->chanctx[!idx];
0332 }
0333 
0334 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
0335 {
0336     struct ath_chanctx *prev, *cur;
0337     struct timespec64 ts;
0338     u32 cur_tsf, prev_tsf, beacon_int;
0339     s32 offset;
0340 
0341     beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
0342 
0343     cur = sc->cur_chan;
0344     prev = ath_chanctx_get_next(sc, cur);
0345 
0346     if (!prev->switch_after_beacon)
0347         return;
0348 
0349     ktime_get_raw_ts64(&ts);
0350     cur_tsf = (u32) cur->tsf_val +
0351           ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
0352 
0353     prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
0354     prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
0355 
0356     /* Adjust the TSF time of the AP chanctx to keep its beacons
0357      * at half beacon interval offset relative to the STA chanctx.
0358      */
0359     offset = cur_tsf - prev_tsf;
0360 
0361     /* Ignore stale data or spurious timestamps */
0362     if (offset < 0 || offset > 3 * beacon_int)
0363         return;
0364 
0365     offset = beacon_int / 2 - (offset % beacon_int);
0366     prev->tsf_val += offset;
0367 }
0368 
0369 /* Configure the TSF based hardware timer for a channel switch.
0370  * Also set up backup software timer, in case the gen timer fails.
0371  * This could be caused by a hardware reset.
0372  */
0373 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
0374 {
0375     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0376     struct ath_hw *ah = sc->sc_ah;
0377     unsigned long timeout;
0378 
0379     ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
0380     tsf_time -= ath9k_hw_gettsf32(ah);
0381     timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
0382     mod_timer(&sc->sched.timer, jiffies + timeout);
0383 
0384     ath_dbg(common, CHAN_CTX,
0385         "Setup chanctx timer with timeout: %d (%d) ms\n",
0386         tsf_time / 1000, jiffies_to_msecs(timeout));
0387 }
0388 
0389 static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
0390                      struct ath_chanctx *ctx,
0391                      struct ath_vif *avp)
0392 {
0393     /*
0394      * Clear the extend_absence flag if it had been
0395      * set during the previous beacon transmission,
0396      * since we need to revert to the normal NoA
0397      * schedule.
0398      */
0399     if (ctx->active && sc->sched.extend_absence) {
0400         avp->noa_duration = 0;
0401         sc->sched.extend_absence = false;
0402     }
0403 
0404     /* If at least two consecutive beacons were missed on the STA
0405      * chanctx, stay on the STA channel for one extra beacon period,
0406      * to resync the timer properly.
0407      */
0408     if (ctx->active && sc->sched.beacon_miss >= 2) {
0409         avp->noa_duration = 0;
0410         sc->sched.extend_absence = true;
0411     }
0412 }
0413 
0414 static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
0415                        struct ath_chanctx *ctx,
0416                        struct ath_vif *avp,
0417                        u32 tsf_time)
0418 {
0419     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0420 
0421     avp->noa_index++;
0422     avp->offchannel_start = tsf_time;
0423     avp->offchannel_duration = sc->sched.offchannel_duration;
0424 
0425     ath_dbg(common, CHAN_CTX,
0426         "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
0427         avp->offchannel_duration,
0428         avp->offchannel_start,
0429         avp->noa_index);
0430 
0431     /*
0432      * When multiple contexts are active, the NoA
0433      * has to be recalculated and advertised after
0434      * an offchannel operation.
0435      */
0436     if (ctx->active && avp->noa_duration)
0437         avp->noa_duration = 0;
0438 }
0439 
0440 static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
0441                      struct ath_vif *avp,
0442                      struct ath_beacon_config *cur_conf,
0443                      u32 tsf_time,
0444                      u32 beacon_int)
0445 {
0446     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0447 
0448     avp->noa_index++;
0449     avp->noa_start = tsf_time;
0450 
0451     if (sc->sched.extend_absence)
0452         avp->noa_duration = (3 * beacon_int / 2) +
0453             sc->sched.channel_switch_time;
0454     else
0455         avp->noa_duration =
0456             TU_TO_USEC(cur_conf->beacon_interval) / 2 +
0457             sc->sched.channel_switch_time;
0458 
0459     if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
0460         sc->sched.extend_absence)
0461         avp->periodic_noa = false;
0462     else
0463         avp->periodic_noa = true;
0464 
0465     ath_dbg(common, CHAN_CTX,
0466         "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
0467         avp->noa_duration,
0468         avp->noa_start,
0469         avp->noa_index,
0470         avp->periodic_noa);
0471 }
0472 
0473 static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
0474                     struct ath_vif *avp,
0475                     u32 tsf_time,
0476                     u32 duration)
0477 {
0478     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0479 
0480     avp->noa_index++;
0481     avp->noa_start = tsf_time;
0482     avp->periodic_noa = false;
0483     avp->oneshot_noa = true;
0484     avp->noa_duration = duration + sc->sched.channel_switch_time;
0485 
0486     ath_dbg(common, CHAN_CTX,
0487         "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
0488         avp->noa_duration,
0489         avp->noa_start,
0490         avp->noa_index,
0491         avp->periodic_noa);
0492 }
0493 
0494 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
0495                enum ath_chanctx_event ev)
0496 {
0497     struct ath_hw *ah = sc->sc_ah;
0498     struct ath_common *common = ath9k_hw_common(ah);
0499     struct ath_beacon_config *cur_conf;
0500     struct ath_vif *avp = NULL;
0501     struct ath_chanctx *ctx;
0502     u32 tsf_time;
0503     u32 beacon_int;
0504 
0505     if (vif)
0506         avp = (struct ath_vif *) vif->drv_priv;
0507 
0508     spin_lock_bh(&sc->chan_lock);
0509 
0510     ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
0511         sc->cur_chan->chandef.center_freq1,
0512         chanctx_event_string(ev),
0513         chanctx_state_string(sc->sched.state),
0514         chanctx_event_delta(sc));
0515 
0516     switch (ev) {
0517     case ATH_CHANCTX_EVENT_BEACON_PREPARE:
0518         if (avp->offchannel_duration)
0519             avp->offchannel_duration = 0;
0520 
0521         if (avp->oneshot_noa) {
0522             avp->noa_duration = 0;
0523             avp->oneshot_noa = false;
0524 
0525             ath_dbg(common, CHAN_CTX,
0526                 "Clearing oneshot NoA\n");
0527         }
0528 
0529         if (avp->chanctx != sc->cur_chan) {
0530             ath_dbg(common, CHAN_CTX,
0531                 "Contexts differ, not preparing beacon\n");
0532             break;
0533         }
0534 
0535         if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
0536             sc->sched.offchannel_pending = false;
0537             sc->next_chan = &sc->offchannel.chan;
0538             sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
0539             ath_dbg(common, CHAN_CTX,
0540                 "Setting offchannel_pending to false\n");
0541         }
0542 
0543         ctx = ath_chanctx_get_next(sc, sc->cur_chan);
0544         if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
0545             sc->next_chan = ctx;
0546             sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
0547             ath_dbg(common, CHAN_CTX,
0548                 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
0549         }
0550 
0551         /* if the timer missed its window, use the next interval */
0552         if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
0553             sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
0554             ath_dbg(common, CHAN_CTX,
0555                 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
0556         }
0557 
0558         if (sc->sched.mgd_prepare_tx)
0559             sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
0560 
0561         /*
0562          * When a context becomes inactive, for example,
0563          * disassociation of a station context, the NoA
0564          * attribute needs to be removed from subsequent
0565          * beacons.
0566          */
0567         if (!ctx->active && avp->noa_duration &&
0568             sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
0569             avp->noa_duration = 0;
0570             avp->periodic_noa = false;
0571 
0572             ath_dbg(common, CHAN_CTX,
0573                 "Clearing NoA schedule\n");
0574         }
0575 
0576         if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
0577             break;
0578 
0579         ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
0580 
0581         sc->sched.beacon_pending = true;
0582         sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
0583 
0584         cur_conf = &sc->cur_chan->beacon;
0585         beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
0586 
0587         /* defer channel switch by a quarter beacon interval */
0588         tsf_time = sc->sched.next_tbtt + beacon_int / 4;
0589         sc->sched.switch_start_time = tsf_time;
0590         sc->cur_chan->last_beacon = sc->sched.next_tbtt;
0591 
0592         /*
0593          * If an offchannel switch is scheduled to happen after
0594          * a beacon transmission, update the NoA with one-shot
0595          * values and increment the index.
0596          */
0597         if (sc->next_chan == &sc->offchannel.chan) {
0598             ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
0599             break;
0600         }
0601 
0602         ath_chanctx_handle_bmiss(sc, ctx, avp);
0603 
0604         /*
0605          * If a mgd_prepare_tx() has been called by mac80211,
0606          * a one-shot NoA needs to be sent. This can happen
0607          * with one or more active channel contexts - in both
0608          * cases, a new NoA schedule has to be advertised.
0609          */
0610         if (sc->sched.mgd_prepare_tx) {
0611             ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
0612                             jiffies_to_usecs(HZ / 5));
0613             break;
0614         }
0615 
0616         /* Prevent wrap-around issues */
0617         if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
0618             avp->noa_duration = 0;
0619 
0620         /*
0621          * If multiple contexts are active, start periodic
0622          * NoA and increment the index for the first
0623          * announcement.
0624          */
0625         if (ctx->active &&
0626             (!avp->noa_duration || sc->sched.force_noa_update))
0627             ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
0628                              tsf_time, beacon_int);
0629 
0630         if (ctx->active && sc->sched.force_noa_update)
0631             sc->sched.force_noa_update = false;
0632 
0633         break;
0634     case ATH_CHANCTX_EVENT_BEACON_SENT:
0635         if (!sc->sched.beacon_pending) {
0636             ath_dbg(common, CHAN_CTX,
0637                 "No pending beacon\n");
0638             break;
0639         }
0640 
0641         sc->sched.beacon_pending = false;
0642 
0643         if (sc->sched.mgd_prepare_tx) {
0644             sc->sched.mgd_prepare_tx = false;
0645             complete(&sc->go_beacon);
0646             ath_dbg(common, CHAN_CTX,
0647                 "Beacon sent, complete go_beacon\n");
0648             break;
0649         }
0650 
0651         if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
0652             break;
0653 
0654         ath_dbg(common, CHAN_CTX,
0655             "Move chanctx state to WAIT_FOR_TIMER\n");
0656 
0657         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
0658         ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
0659         break;
0660     case ATH_CHANCTX_EVENT_TSF_TIMER:
0661         if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
0662             break;
0663 
0664         if (!sc->cur_chan->switch_after_beacon &&
0665             sc->sched.beacon_pending)
0666             sc->sched.beacon_miss++;
0667 
0668         ath_dbg(common, CHAN_CTX,
0669             "Move chanctx state to SWITCH\n");
0670 
0671         sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
0672         ieee80211_queue_work(sc->hw, &sc->chanctx_work);
0673         break;
0674     case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
0675         if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
0676             sc->cur_chan == &sc->offchannel.chan)
0677             break;
0678 
0679         sc->sched.beacon_pending = false;
0680         sc->sched.beacon_miss = 0;
0681 
0682         if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
0683             !sc->sched.beacon_adjust ||
0684             !sc->cur_chan->tsf_val)
0685             break;
0686 
0687         ath_chanctx_adjust_tbtt_delta(sc);
0688 
0689         /* TSF time might have been updated by the incoming beacon,
0690          * need update the channel switch timer to reflect the change.
0691          */
0692         tsf_time = sc->sched.switch_start_time;
0693         tsf_time -= (u32) sc->cur_chan->tsf_val +
0694             ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
0695         tsf_time += ath9k_hw_gettsf32(ah);
0696 
0697         sc->sched.beacon_adjust = false;
0698         ath_chanctx_setup_timer(sc, tsf_time);
0699         break;
0700     case ATH_CHANCTX_EVENT_AUTHORIZED:
0701         if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
0702             avp->chanctx != sc->cur_chan)
0703             break;
0704 
0705         ath_dbg(common, CHAN_CTX,
0706             "Move chanctx state from FORCE_ACTIVE to IDLE\n");
0707 
0708         sc->sched.state = ATH_CHANCTX_STATE_IDLE;
0709         fallthrough;
0710     case ATH_CHANCTX_EVENT_SWITCH:
0711         if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
0712             sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
0713             sc->cur_chan->switch_after_beacon ||
0714             sc->cur_chan == &sc->offchannel.chan)
0715             break;
0716 
0717         /* If this is a station chanctx, stay active for a half
0718          * beacon period (minus channel switch time)
0719          */
0720         sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
0721         cur_conf = &sc->cur_chan->beacon;
0722 
0723         ath_dbg(common, CHAN_CTX,
0724             "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
0725 
0726         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
0727         sc->sched.wait_switch = false;
0728 
0729         tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
0730 
0731         if (sc->sched.extend_absence) {
0732             sc->sched.beacon_miss = 0;
0733             tsf_time *= 3;
0734         }
0735 
0736         tsf_time -= sc->sched.channel_switch_time;
0737         tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
0738         sc->sched.switch_start_time = tsf_time;
0739 
0740         ath_chanctx_setup_timer(sc, tsf_time);
0741         sc->sched.beacon_pending = true;
0742         sc->sched.beacon_adjust = true;
0743         break;
0744     case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
0745         if (sc->cur_chan == &sc->offchannel.chan ||
0746             sc->cur_chan->switch_after_beacon)
0747             break;
0748 
0749         sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
0750         ieee80211_queue_work(sc->hw, &sc->chanctx_work);
0751         break;
0752     case ATH_CHANCTX_EVENT_UNASSIGN:
0753         if (sc->cur_chan->assigned) {
0754             if (sc->next_chan && !sc->next_chan->assigned &&
0755                 sc->next_chan != &sc->offchannel.chan)
0756                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
0757             break;
0758         }
0759 
0760         ctx = ath_chanctx_get_next(sc, sc->cur_chan);
0761         sc->sched.state = ATH_CHANCTX_STATE_IDLE;
0762         if (!ctx->assigned)
0763             break;
0764 
0765         sc->next_chan = ctx;
0766         ieee80211_queue_work(sc->hw, &sc->chanctx_work);
0767         break;
0768     case ATH_CHANCTX_EVENT_ASSIGN:
0769         break;
0770     case ATH_CHANCTX_EVENT_CHANGE:
0771         break;
0772     }
0773 
0774     spin_unlock_bh(&sc->chan_lock);
0775 }
0776 
0777 void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
0778                 enum ath_chanctx_event ev)
0779 {
0780     if (sc->sched.beacon_pending)
0781         ath_chanctx_event(sc, NULL, ev);
0782 }
0783 
0784 void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
0785                 enum ath_chanctx_event ev)
0786 {
0787     ath_chanctx_event(sc, NULL, ev);
0788 }
0789 
0790 static int ath_scan_channel_duration(struct ath_softc *sc,
0791                      struct ieee80211_channel *chan)
0792 {
0793     struct cfg80211_scan_request *req = sc->offchannel.scan_req;
0794 
0795     if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
0796         return (HZ / 9); /* ~110 ms */
0797 
0798     return (HZ / 16); /* ~60 ms */
0799 }
0800 
0801 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
0802                    struct cfg80211_chan_def *chandef)
0803 {
0804     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0805 
0806     spin_lock_bh(&sc->chan_lock);
0807 
0808     if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
0809         (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
0810         if (chandef)
0811             ctx->chandef = *chandef;
0812 
0813         sc->sched.offchannel_pending = true;
0814         sc->sched.wait_switch = true;
0815         sc->sched.offchannel_duration =
0816             jiffies_to_usecs(sc->offchannel.duration) +
0817             sc->sched.channel_switch_time;
0818 
0819         spin_unlock_bh(&sc->chan_lock);
0820         ath_dbg(common, CHAN_CTX,
0821             "Set offchannel_pending to true\n");
0822         return;
0823     }
0824 
0825     sc->next_chan = ctx;
0826     if (chandef) {
0827         ctx->chandef = *chandef;
0828         ath_dbg(common, CHAN_CTX,
0829             "Assigned next_chan to %d MHz\n", chandef->center_freq1);
0830     }
0831 
0832     if (sc->next_chan == &sc->offchannel.chan) {
0833         sc->sched.offchannel_duration =
0834             jiffies_to_usecs(sc->offchannel.duration) +
0835             sc->sched.channel_switch_time;
0836 
0837         if (chandef) {
0838             ath_dbg(common, CHAN_CTX,
0839                 "Offchannel duration for chan %d MHz : %u\n",
0840                 chandef->center_freq1,
0841                 sc->sched.offchannel_duration);
0842         }
0843     }
0844     spin_unlock_bh(&sc->chan_lock);
0845     ieee80211_queue_work(sc->hw, &sc->chanctx_work);
0846 }
0847 
0848 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
0849                        struct ieee80211_channel *chan)
0850 {
0851     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0852     struct cfg80211_chan_def chandef;
0853 
0854     cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
0855     ath_dbg(common, CHAN_CTX,
0856         "Channel definition created: %d MHz\n", chandef.center_freq1);
0857 
0858     ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
0859 }
0860 
0861 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
0862                              bool active)
0863 {
0864     struct ath_chanctx *ctx;
0865 
0866     ath_for_each_chanctx(sc, ctx) {
0867         if (!ctx->assigned || list_empty(&ctx->vifs))
0868             continue;
0869         if (active && !ctx->active)
0870             continue;
0871 
0872         if (ctx->switch_after_beacon)
0873             return ctx;
0874     }
0875 
0876     return &sc->chanctx[0];
0877 }
0878 
0879 static void
0880 ath_scan_next_channel(struct ath_softc *sc)
0881 {
0882     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0883     struct cfg80211_scan_request *req = sc->offchannel.scan_req;
0884     struct ieee80211_channel *chan;
0885 
0886     if (sc->offchannel.scan_idx >= req->n_channels) {
0887         ath_dbg(common, CHAN_CTX,
0888             "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
0889             "scan_idx: %d, n_channels: %d\n",
0890             sc->offchannel.scan_idx,
0891             req->n_channels);
0892 
0893         sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
0894         ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
0895                    NULL);
0896         return;
0897     }
0898 
0899     ath_dbg(common, CHAN_CTX,
0900         "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
0901         sc->offchannel.scan_idx);
0902 
0903     chan = req->channels[sc->offchannel.scan_idx++];
0904     sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
0905     sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
0906 
0907     ath_chanctx_offchan_switch(sc, chan);
0908 }
0909 
0910 void ath_offchannel_next(struct ath_softc *sc)
0911 {
0912     struct ieee80211_vif *vif;
0913 
0914     if (sc->offchannel.scan_req) {
0915         vif = sc->offchannel.scan_vif;
0916         sc->offchannel.chan.txpower = vif->bss_conf.txpower;
0917         ath_scan_next_channel(sc);
0918     } else if (sc->offchannel.roc_vif) {
0919         vif = sc->offchannel.roc_vif;
0920         sc->offchannel.chan.txpower = vif->bss_conf.txpower;
0921         sc->offchannel.duration =
0922             msecs_to_jiffies(sc->offchannel.roc_duration);
0923         sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
0924         ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
0925     } else {
0926         spin_lock_bh(&sc->chan_lock);
0927         sc->sched.offchannel_pending = false;
0928         sc->sched.wait_switch = false;
0929         spin_unlock_bh(&sc->chan_lock);
0930 
0931         ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
0932                    NULL);
0933         sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
0934         if (sc->ps_idle)
0935             ath_cancel_work(sc);
0936     }
0937 }
0938 
0939 void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
0940 {
0941     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0942 
0943     sc->offchannel.roc_vif = NULL;
0944     sc->offchannel.roc_chan = NULL;
0945 
0946     switch (reason) {
0947     case ATH_ROC_COMPLETE_ABORT:
0948         ath_dbg(common, CHAN_CTX, "RoC aborted\n");
0949         ieee80211_remain_on_channel_expired(sc->hw);
0950         break;
0951     case ATH_ROC_COMPLETE_EXPIRE:
0952         ath_dbg(common, CHAN_CTX, "RoC expired\n");
0953         ieee80211_remain_on_channel_expired(sc->hw);
0954         break;
0955     case ATH_ROC_COMPLETE_CANCEL:
0956         ath_dbg(common, CHAN_CTX, "RoC canceled\n");
0957         break;
0958     }
0959 
0960     ath_offchannel_next(sc);
0961     ath9k_ps_restore(sc);
0962 }
0963 
0964 void ath_scan_complete(struct ath_softc *sc, bool abort)
0965 {
0966     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
0967     struct cfg80211_scan_info info = {
0968         .aborted = abort,
0969     };
0970 
0971     if (abort)
0972         ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
0973     else
0974         ath_dbg(common, CHAN_CTX, "HW scan complete\n");
0975 
0976     sc->offchannel.scan_req = NULL;
0977     sc->offchannel.scan_vif = NULL;
0978     sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
0979     ieee80211_scan_completed(sc->hw, &info);
0980     clear_bit(ATH_OP_SCANNING, &common->op_flags);
0981     spin_lock_bh(&sc->chan_lock);
0982     if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
0983         sc->sched.force_noa_update = true;
0984     spin_unlock_bh(&sc->chan_lock);
0985     ath_offchannel_next(sc);
0986     ath9k_ps_restore(sc);
0987 }
0988 
0989 static void ath_scan_send_probe(struct ath_softc *sc,
0990                 struct cfg80211_ssid *ssid)
0991 {
0992     struct cfg80211_scan_request *req = sc->offchannel.scan_req;
0993     struct ieee80211_vif *vif = sc->offchannel.scan_vif;
0994     struct ath_tx_control txctl = {};
0995     struct sk_buff *skb;
0996     struct ieee80211_tx_info *info;
0997     int band = sc->offchannel.chan.chandef.chan->band;
0998 
0999     skb = ieee80211_probereq_get(sc->hw, vif->addr,
1000             ssid->ssid, ssid->ssid_len, req->ie_len);
1001     if (!skb)
1002         return;
1003 
1004     info = IEEE80211_SKB_CB(skb);
1005     if (req->no_cck)
1006         info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1007 
1008     if (req->ie_len)
1009         skb_put_data(skb, req->ie, req->ie_len);
1010 
1011     skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1012 
1013     if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1014         goto error;
1015 
1016     txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1017     if (ath_tx_start(sc->hw, skb, &txctl))
1018         goto error;
1019 
1020     return;
1021 
1022 error:
1023     ieee80211_free_txskb(sc->hw, skb);
1024 }
1025 
1026 static void ath_scan_channel_start(struct ath_softc *sc)
1027 {
1028     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1029     struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1030     int i;
1031 
1032     if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1033         req->n_ssids) {
1034         for (i = 0; i < req->n_ssids; i++)
1035             ath_scan_send_probe(sc, &req->ssids[i]);
1036 
1037     }
1038 
1039     ath_dbg(common, CHAN_CTX,
1040         "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1041 
1042     sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1043     mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1044 }
1045 
1046 static void ath_chanctx_timer(struct timer_list *t)
1047 {
1048     struct ath_softc *sc = from_timer(sc, t, sched.timer);
1049     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1050 
1051     ath_dbg(common, CHAN_CTX,
1052         "Channel context timer invoked\n");
1053 
1054     ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1055 }
1056 
1057 static void ath_offchannel_timer(struct timer_list *t)
1058 {
1059     struct ath_softc *sc = from_timer(sc, t, offchannel.timer);
1060     struct ath_chanctx *ctx;
1061     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1062 
1063     ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1064         __func__, offchannel_state_string(sc->offchannel.state));
1065 
1066     switch (sc->offchannel.state) {
1067     case ATH_OFFCHANNEL_PROBE_WAIT:
1068         if (!sc->offchannel.scan_req)
1069             return;
1070 
1071         /* get first active channel context */
1072         ctx = ath_chanctx_get_oper_chan(sc, true);
1073         if (ctx->active) {
1074             ath_dbg(common, CHAN_CTX,
1075                 "Switch to oper/active context, "
1076                 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1077 
1078             sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1079             ath_chanctx_switch(sc, ctx, NULL);
1080             mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1081             break;
1082         }
1083         fallthrough;
1084     case ATH_OFFCHANNEL_SUSPEND:
1085         if (!sc->offchannel.scan_req)
1086             return;
1087 
1088         ath_scan_next_channel(sc);
1089         break;
1090     case ATH_OFFCHANNEL_ROC_START:
1091     case ATH_OFFCHANNEL_ROC_WAIT:
1092         sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1093         ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
1094         break;
1095     default:
1096         break;
1097     }
1098 }
1099 
1100 static bool
1101 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1102                   bool powersave)
1103 {
1104     struct ieee80211_vif *vif = avp->vif;
1105     struct ieee80211_sta *sta = NULL;
1106     struct ieee80211_hdr_3addr *nullfunc;
1107     struct ath_tx_control txctl;
1108     struct sk_buff *skb;
1109     int band = sc->cur_chan->chandef.chan->band;
1110 
1111     switch (vif->type) {
1112     case NL80211_IFTYPE_STATION:
1113         if (!avp->assoc)
1114             return false;
1115 
1116         skb = ieee80211_nullfunc_get(sc->hw, vif, false);
1117         if (!skb)
1118             return false;
1119 
1120         nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1121         if (powersave)
1122             nullfunc->frame_control |=
1123                 cpu_to_le16(IEEE80211_FCTL_PM);
1124 
1125         skb->priority = 7;
1126         skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1127         if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1128             dev_kfree_skb_any(skb);
1129             return false;
1130         }
1131         break;
1132     default:
1133         return false;
1134     }
1135 
1136     memset(&txctl, 0, sizeof(txctl));
1137     txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1138     txctl.sta = sta;
1139     if (ath_tx_start(sc->hw, skb, &txctl)) {
1140         ieee80211_free_txskb(sc->hw, skb);
1141         return false;
1142     }
1143 
1144     return true;
1145 }
1146 
1147 static bool
1148 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1149 {
1150     struct ath_vif *avp;
1151     bool sent = false;
1152 
1153     rcu_read_lock();
1154     list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1155         if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1156             sent = true;
1157     }
1158     rcu_read_unlock();
1159 
1160     return sent;
1161 }
1162 
1163 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1164 {
1165     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1166 
1167     if (sc->cur_chan == &sc->offchannel.chan)
1168         return false;
1169 
1170     switch (sc->sched.state) {
1171     case ATH_CHANCTX_STATE_SWITCH:
1172         return false;
1173     case ATH_CHANCTX_STATE_IDLE:
1174         if (!sc->cur_chan->switch_after_beacon)
1175             return false;
1176 
1177         ath_dbg(common, CHAN_CTX,
1178             "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1179 
1180         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1181         break;
1182     default:
1183         break;
1184     }
1185 
1186     return true;
1187 }
1188 
1189 static void ath_offchannel_channel_change(struct ath_softc *sc)
1190 {
1191     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1192 
1193     ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1194         __func__, offchannel_state_string(sc->offchannel.state));
1195 
1196     switch (sc->offchannel.state) {
1197     case ATH_OFFCHANNEL_PROBE_SEND:
1198         if (!sc->offchannel.scan_req)
1199             return;
1200 
1201         if (sc->cur_chan->chandef.chan !=
1202             sc->offchannel.chan.chandef.chan)
1203             return;
1204 
1205         ath_scan_channel_start(sc);
1206         break;
1207     case ATH_OFFCHANNEL_IDLE:
1208         if (!sc->offchannel.scan_req)
1209             return;
1210 
1211         ath_scan_complete(sc, false);
1212         break;
1213     case ATH_OFFCHANNEL_ROC_START:
1214         if (sc->cur_chan != &sc->offchannel.chan)
1215             break;
1216 
1217         sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1218         mod_timer(&sc->offchannel.timer,
1219               jiffies + sc->offchannel.duration);
1220         ieee80211_ready_on_channel(sc->hw);
1221         break;
1222     case ATH_OFFCHANNEL_ROC_DONE:
1223         break;
1224     default:
1225         break;
1226     }
1227 }
1228 
1229 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1230 {
1231     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1232     struct ath_chanctx *old_ctx;
1233     struct timespec64 ts;
1234     bool measure_time = false;
1235     bool send_ps = false;
1236     bool queues_stopped = false;
1237 
1238     spin_lock_bh(&sc->chan_lock);
1239     if (!sc->next_chan) {
1240         spin_unlock_bh(&sc->chan_lock);
1241         return;
1242     }
1243 
1244     if (!force && ath_chanctx_defer_switch(sc)) {
1245         spin_unlock_bh(&sc->chan_lock);
1246         return;
1247     }
1248 
1249     ath_dbg(common, CHAN_CTX,
1250         "%s: current: %d MHz, next: %d MHz\n",
1251         __func__,
1252         sc->cur_chan->chandef.center_freq1,
1253         sc->next_chan->chandef.center_freq1);
1254 
1255     if (sc->cur_chan != sc->next_chan) {
1256         ath_dbg(common, CHAN_CTX,
1257             "Stopping current chanctx: %d\n",
1258             sc->cur_chan->chandef.center_freq1);
1259         sc->cur_chan->stopped = true;
1260         spin_unlock_bh(&sc->chan_lock);
1261 
1262         if (sc->next_chan == &sc->offchannel.chan) {
1263             ktime_get_raw_ts64(&ts);
1264             measure_time = true;
1265         }
1266 
1267         ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1268         queues_stopped = true;
1269 
1270         __ath9k_flush(sc->hw, ~0, true, false, false);
1271 
1272         if (ath_chanctx_send_ps_frame(sc, true))
1273             __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1274                       false, false, false);
1275 
1276         send_ps = true;
1277         spin_lock_bh(&sc->chan_lock);
1278 
1279         if (sc->cur_chan != &sc->offchannel.chan) {
1280             ktime_get_raw_ts64(&sc->cur_chan->tsf_ts);
1281             sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1282         }
1283     }
1284     old_ctx = sc->cur_chan;
1285     sc->cur_chan = sc->next_chan;
1286     sc->cur_chan->stopped = false;
1287     sc->next_chan = NULL;
1288 
1289     if (!sc->sched.offchannel_pending)
1290         sc->sched.offchannel_duration = 0;
1291 
1292     if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1293         sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1294 
1295     spin_unlock_bh(&sc->chan_lock);
1296 
1297     if (sc->sc_ah->chip_fullsleep ||
1298         memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1299            sizeof(sc->cur_chandef))) {
1300         ath_dbg(common, CHAN_CTX,
1301             "%s: Set channel %d MHz\n",
1302             __func__, sc->cur_chan->chandef.center_freq1);
1303         ath_set_channel(sc);
1304         if (measure_time)
1305             sc->sched.channel_switch_time =
1306                 ath9k_hw_get_tsf_offset(&ts, NULL);
1307         /*
1308          * A reset will ensure that all queues are woken up,
1309          * so there is no need to awaken them again.
1310          */
1311         goto out;
1312     }
1313 
1314     if (queues_stopped)
1315         ath9k_chanctx_wake_queues(sc, old_ctx);
1316 out:
1317     if (send_ps)
1318         ath_chanctx_send_ps_frame(sc, false);
1319 
1320     ath_offchannel_channel_change(sc);
1321     ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1322 }
1323 
1324 static void ath_chanctx_work(struct work_struct *work)
1325 {
1326     struct ath_softc *sc = container_of(work, struct ath_softc,
1327                         chanctx_work);
1328     mutex_lock(&sc->mutex);
1329     ath_chanctx_set_next(sc, false);
1330     mutex_unlock(&sc->mutex);
1331 }
1332 
1333 void ath9k_offchannel_init(struct ath_softc *sc)
1334 {
1335     struct ath_chanctx *ctx;
1336     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1337     struct ieee80211_supported_band *sband;
1338     struct ieee80211_channel *chan;
1339     int i;
1340 
1341     sband = &common->sbands[NL80211_BAND_2GHZ];
1342     if (!sband->n_channels)
1343         sband = &common->sbands[NL80211_BAND_5GHZ];
1344 
1345     chan = &sband->channels[0];
1346 
1347     ctx = &sc->offchannel.chan;
1348     INIT_LIST_HEAD(&ctx->vifs);
1349     ctx->txpower = ATH_TXPOWER_MAX;
1350     cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1351 
1352     for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
1353         INIT_LIST_HEAD(&ctx->acq[i].acq_new);
1354         INIT_LIST_HEAD(&ctx->acq[i].acq_old);
1355         spin_lock_init(&ctx->acq[i].lock);
1356     }
1357 
1358     sc->offchannel.chan.offchannel = true;
1359 }
1360 
1361 void ath9k_init_channel_context(struct ath_softc *sc)
1362 {
1363     INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1364 
1365     timer_setup(&sc->offchannel.timer, ath_offchannel_timer, 0);
1366     timer_setup(&sc->sched.timer, ath_chanctx_timer, 0);
1367 
1368     init_completion(&sc->go_beacon);
1369 }
1370 
1371 void ath9k_deinit_channel_context(struct ath_softc *sc)
1372 {
1373     cancel_work_sync(&sc->chanctx_work);
1374 }
1375 
1376 bool ath9k_is_chanctx_enabled(void)
1377 {
1378     return (ath9k_use_chanctx == 1);
1379 }
1380 
1381 /********************/
1382 /* Queue management */
1383 /********************/
1384 
1385 void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1386 {
1387     struct ath_hw *ah = sc->sc_ah;
1388     int i;
1389 
1390     if (ctx == &sc->offchannel.chan) {
1391         ieee80211_stop_queue(sc->hw,
1392                      sc->hw->offchannel_tx_hw_queue);
1393     } else {
1394         for (i = 0; i < IEEE80211_NUM_ACS; i++)
1395             ieee80211_stop_queue(sc->hw,
1396                          ctx->hw_queue_base + i);
1397     }
1398 
1399     if (ah->opmode == NL80211_IFTYPE_AP)
1400         ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1401 }
1402 
1403 
1404 void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1405 {
1406     struct ath_hw *ah = sc->sc_ah;
1407     int i;
1408 
1409     if (ctx == &sc->offchannel.chan) {
1410         ieee80211_wake_queue(sc->hw,
1411                      sc->hw->offchannel_tx_hw_queue);
1412     } else {
1413         for (i = 0; i < IEEE80211_NUM_ACS; i++)
1414             ieee80211_wake_queue(sc->hw,
1415                          ctx->hw_queue_base + i);
1416     }
1417 
1418     if (ah->opmode == NL80211_IFTYPE_AP)
1419         ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1420 }
1421 
1422 /*****************/
1423 /* P2P Powersave */
1424 /*****************/
1425 
1426 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1427 {
1428     struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1429     struct ath_hw *ah = sc->sc_ah;
1430     u32 tsf, target_tsf;
1431 
1432     if (!avp || !avp->noa.has_next_tsf)
1433         return;
1434 
1435     ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1436 
1437     tsf = ath9k_hw_gettsf32(sc->sc_ah);
1438 
1439     target_tsf = avp->noa.next_tsf;
1440     if (!avp->noa.absent)
1441         target_tsf -= ATH_P2P_PS_STOP_TIME;
1442     else
1443         target_tsf += ATH_P2P_PS_STOP_TIME;
1444 
1445     if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1446         target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1447 
1448     ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1449         __func__, avp->noa.absent, tsf, target_tsf,
1450         (target_tsf - tsf) / 1000);
1451 
1452     ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
1453 }
1454 
1455 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1456 {
1457     struct ath_vif *avp = (void *)vif->drv_priv;
1458     u32 tsf;
1459 
1460     if (!sc->p2p_ps_timer)
1461         return;
1462 
1463     if (vif->type != NL80211_IFTYPE_STATION)
1464         return;
1465 
1466     sc->p2p_ps_vif = avp;
1467 
1468     if (sc->ps_flags & PS_BEACON_SYNC)
1469         return;
1470 
1471     tsf = ath9k_hw_gettsf32(sc->sc_ah);
1472     ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1473     ath9k_update_p2p_ps_timer(sc, avp);
1474 }
1475 
1476 static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1477 {
1478     struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1479     u8 switch_time, ctwin;
1480 
1481     /*
1482      * Channel switch in multi-channel mode is deferred
1483      * by a quarter beacon interval when handling
1484      * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1485      * interface is guaranteed to be discoverable
1486      * for that duration after a TBTT.
1487      */
1488     switch_time = cur_conf->beacon_interval / 4;
1489 
1490     ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1491     if (ctwin && (ctwin < switch_time))
1492         return ctwin;
1493 
1494     if (switch_time < P2P_DEFAULT_CTWIN)
1495         return 0;
1496 
1497     return P2P_DEFAULT_CTWIN;
1498 }
1499 
1500 void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1501               struct sk_buff *skb)
1502 {
1503     static const u8 noa_ie_hdr[] = {
1504         WLAN_EID_VENDOR_SPECIFIC,   /* type */
1505         0,              /* length */
1506         0x50, 0x6f, 0x9a,       /* WFA OUI */
1507         0x09,               /* P2P subtype */
1508         0x0c,               /* Notice of Absence */
1509         0x00,               /* LSB of little-endian len */
1510         0x00,               /* MSB of little-endian len */
1511     };
1512 
1513     struct ieee80211_p2p_noa_attr *noa;
1514     int noa_len, noa_desc, i = 0;
1515     u8 *hdr;
1516 
1517     if (!avp->offchannel_duration && !avp->noa_duration)
1518         return;
1519 
1520     noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
1521     noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1522 
1523     hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
1524     hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1525     hdr[7] = noa_len;
1526 
1527     noa = skb_put_zero(skb, noa_len);
1528 
1529     noa->index = avp->noa_index;
1530     noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1531     if (noa->oppps_ctwindow)
1532         noa->oppps_ctwindow |= BIT(7);
1533 
1534     if (avp->noa_duration) {
1535         if (avp->periodic_noa) {
1536             u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1537             noa->desc[i].count = 255;
1538             noa->desc[i].interval = cpu_to_le32(interval);
1539         } else {
1540             noa->desc[i].count = 1;
1541         }
1542 
1543         noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1544         noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
1545         i++;
1546     }
1547 
1548     if (avp->offchannel_duration) {
1549         noa->desc[i].count = 1;
1550         noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1551         noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1552     }
1553 }
1554 
1555 void ath9k_p2p_ps_timer(void *priv)
1556 {
1557     struct ath_softc *sc = priv;
1558     struct ath_vif *avp = sc->p2p_ps_vif;
1559     struct ieee80211_vif *vif;
1560     struct ieee80211_sta *sta;
1561     struct ath_node *an;
1562     u32 tsf;
1563 
1564     del_timer_sync(&sc->sched.timer);
1565     ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1566     ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1567 
1568     if (!avp || avp->chanctx != sc->cur_chan)
1569         return;
1570 
1571     tsf = ath9k_hw_gettsf32(sc->sc_ah);
1572     if (!avp->noa.absent)
1573         tsf += ATH_P2P_PS_STOP_TIME;
1574     else
1575         tsf -= ATH_P2P_PS_STOP_TIME;
1576 
1577     if (!avp->noa.has_next_tsf ||
1578         avp->noa.next_tsf - tsf > BIT(31))
1579         ieee80211_update_p2p_noa(&avp->noa, tsf);
1580 
1581     ath9k_update_p2p_ps_timer(sc, avp);
1582 
1583     rcu_read_lock();
1584 
1585     vif = avp->vif;
1586     sta = ieee80211_find_sta(vif, avp->bssid);
1587     if (!sta)
1588         goto out;
1589 
1590     an = (void *) sta->drv_priv;
1591     if (an->sleeping == !!avp->noa.absent)
1592         goto out;
1593 
1594     an->sleeping = avp->noa.absent;
1595     if (an->sleeping)
1596         ath_tx_aggr_sleep(sta, sc, an);
1597     else
1598         ath_tx_aggr_wakeup(sc, an);
1599 
1600 out:
1601     rcu_read_unlock();
1602 }
1603 
1604 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1605                 struct ieee80211_vif *vif)
1606 {
1607     unsigned long flags;
1608 
1609     spin_lock_bh(&sc->sc_pcu_lock);
1610     spin_lock_irqsave(&sc->sc_pm_lock, flags);
1611     ath9k_update_p2p_ps(sc, vif);
1612     spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1613     spin_unlock_bh(&sc->sc_pcu_lock);
1614 }
1615 
1616 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1617 {
1618     if (sc->p2p_ps_vif)
1619         ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1620 }
1621 
1622 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1623               struct ieee80211_vif *vif)
1624 {
1625     struct ath_vif *avp = (void *)vif->drv_priv;
1626 
1627     spin_lock_bh(&sc->sc_pcu_lock);
1628     if (avp == sc->p2p_ps_vif) {
1629         sc->p2p_ps_vif = NULL;
1630         ath9k_update_p2p_ps_timer(sc, NULL);
1631     }
1632     spin_unlock_bh(&sc->sc_pcu_lock);
1633 }
1634 
1635 int ath9k_init_p2p(struct ath_softc *sc)
1636 {
1637     sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1638                            NULL, sc, AR_FIRST_NDP_TIMER);
1639     if (!sc->p2p_ps_timer)
1640         return -ENOMEM;
1641 
1642     return 0;
1643 }
1644 
1645 void ath9k_deinit_p2p(struct ath_softc *sc)
1646 {
1647     if (sc->p2p_ps_timer)
1648         ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1649 }
1650 
1651 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */