linux/drivers/net/wireless/ath/ath9k/channel.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2014 Qualcomm Atheros, Inc.
   3 *
   4 * Permission to use, copy, modify, and/or distribute this software for any
   5 * purpose with or without fee is hereby granted, provided that the above
   6 * copyright notice and this permission notice appear in all copies.
   7 *
   8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15 */
  16
  17#include "ath9k.h"
  18
  19/* Set/change channels.  If the channel is really being changed, it's done
  20 * by reseting the chip.  To accomplish this we must first cleanup any pending
  21 * DMA, then restart stuff.
  22 */
  23static int ath_set_channel(struct ath_softc *sc)
  24{
  25        struct ath_hw *ah = sc->sc_ah;
  26        struct ath_common *common = ath9k_hw_common(ah);
  27        struct ieee80211_hw *hw = sc->hw;
  28        struct ath9k_channel *hchan;
  29        struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
  30        struct ieee80211_channel *chan = chandef->chan;
  31        int pos = chan->hw_value;
  32        int old_pos = -1;
  33        int r;
  34
  35        if (test_bit(ATH_OP_INVALID, &common->op_flags))
  36                return -EIO;
  37
  38        if (ah->curchan)
  39                old_pos = ah->curchan - &ah->channels[0];
  40
  41        ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
  42                chan->center_freq, chandef->width);
  43
  44        /* update survey stats for the old channel before switching */
  45        spin_lock_bh(&common->cc_lock);
  46        ath_update_survey_stats(sc);
  47        spin_unlock_bh(&common->cc_lock);
  48
  49        ath9k_cmn_get_channel(hw, ah, chandef);
  50
  51        /* If the operating channel changes, change the survey in-use flags
  52         * along with it.
  53         * Reset the survey data for the new channel, unless we're switching
  54         * back to the operating channel from an off-channel operation.
  55         */
  56        if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
  57                if (sc->cur_survey)
  58                        sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
  59
  60                sc->cur_survey = &sc->survey[pos];
  61
  62                memset(sc->cur_survey, 0, sizeof(struct survey_info));
  63                sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
  64        } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
  65                memset(&sc->survey[pos], 0, sizeof(struct survey_info));
  66        }
  67
  68        hchan = &sc->sc_ah->channels[pos];
  69        r = ath_reset(sc, hchan);
  70        if (r)
  71                return r;
  72
  73        /* The most recent snapshot of channel->noisefloor for the old
  74         * channel is only available after the hardware reset. Copy it to
  75         * the survey stats now.
  76         */
  77        if (old_pos >= 0)
  78                ath_update_survey_nf(sc, old_pos);
  79
  80        /* Enable radar pulse detection if on a DFS channel. Spectral
  81         * scanning and radar detection can not be used concurrently.
  82         */
  83        if (hw->conf.radar_enabled) {
  84                u32 rxfilter;
  85
  86                rxfilter = ath9k_hw_getrxfilter(ah);
  87                rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
  88                                ATH9K_RX_FILTER_PHYERR;
  89                ath9k_hw_setrxfilter(ah, rxfilter);
  90                ath_dbg(common, DFS, "DFS enabled at freq %d\n",
  91                        chan->center_freq);
  92        } else {
  93                /* perform spectral scan if requested. */
  94                if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
  95                        sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
  96                        ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
  97        }
  98
  99        return 0;
 100}
 101
 102void ath_chanctx_init(struct ath_softc *sc)
 103{
 104        struct ath_chanctx *ctx;
 105        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 106        struct ieee80211_supported_band *sband;
 107        struct ieee80211_channel *chan;
 108        int i, j;
 109
 110        sband = &common->sbands[NL80211_BAND_2GHZ];
 111        if (!sband->n_channels)
 112                sband = &common->sbands[NL80211_BAND_5GHZ];
 113
 114        chan = &sband->channels[0];
 115        for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
 116                ctx = &sc->chanctx[i];
 117                cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
 118                INIT_LIST_HEAD(&ctx->vifs);
 119                ctx->txpower = ATH_TXPOWER_MAX;
 120                ctx->flush_timeout = HZ / 5; /* 200ms */
 121                for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) {
 122                        INIT_LIST_HEAD(&ctx->acq[j].acq_new);
 123                        INIT_LIST_HEAD(&ctx->acq[j].acq_old);
 124                        spin_lock_init(&ctx->acq[j].lock);
 125                }
 126        }
 127}
 128
 129void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
 130                             struct cfg80211_chan_def *chandef)
 131{
 132        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 133        bool cur_chan;
 134
 135        spin_lock_bh(&sc->chan_lock);
 136        if (chandef)
 137                memcpy(&ctx->chandef, chandef, sizeof(*chandef));
 138        cur_chan = sc->cur_chan == ctx;
 139        spin_unlock_bh(&sc->chan_lock);
 140
 141        if (!cur_chan) {
 142                ath_dbg(common, CHAN_CTX,
 143                        "Current context differs from the new context\n");
 144                return;
 145        }
 146
 147        ath_set_channel(sc);
 148}
 149
 150#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
 151
 152/*************/
 153/* Utilities */
 154/*************/
 155
 156struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
 157{
 158        struct ath_chanctx *ctx;
 159        struct ath_vif *avp;
 160        struct ieee80211_vif *vif;
 161
 162        spin_lock_bh(&sc->chan_lock);
 163
 164        ath_for_each_chanctx(sc, ctx) {
 165                if (!ctx->active)
 166                        continue;
 167
 168                list_for_each_entry(avp, &ctx->vifs, list) {
 169                        vif = avp->vif;
 170
 171                        if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
 172                                spin_unlock_bh(&sc->chan_lock);
 173                                return ctx;
 174                        }
 175                }
 176        }
 177
 178        spin_unlock_bh(&sc->chan_lock);
 179        return NULL;
 180}
 181
 182/**********************************************************/
 183/* Functions to handle the channel context state machine. */
 184/**********************************************************/
 185
 186static const char *offchannel_state_string(enum ath_offchannel_state state)
 187{
 188        switch (state) {
 189                case_rtn_string(ATH_OFFCHANNEL_IDLE);
 190                case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
 191                case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
 192                case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
 193                case_rtn_string(ATH_OFFCHANNEL_ROC_START);
 194                case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
 195                case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
 196        default:
 197                return "unknown";
 198        }
 199}
 200
 201static const char *chanctx_event_string(enum ath_chanctx_event ev)
 202{
 203        switch (ev) {
 204                case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
 205                case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
 206                case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
 207                case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
 208                case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
 209                case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
 210                case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
 211                case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
 212                case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
 213                case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
 214        default:
 215                return "unknown";
 216        }
 217}
 218
 219static const char *chanctx_state_string(enum ath_chanctx_state state)
 220{
 221        switch (state) {
 222                case_rtn_string(ATH_CHANCTX_STATE_IDLE);
 223                case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
 224                case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
 225                case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
 226                case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
 227        default:
 228                return "unknown";
 229        }
 230}
 231
 232static u32 chanctx_event_delta(struct ath_softc *sc)
 233{
 234        u64 ms;
 235        struct timespec ts, *old;
 236
 237        getrawmonotonic(&ts);
 238        old = &sc->last_event_time;
 239        ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
 240        ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
 241        sc->last_event_time = ts;
 242
 243        return (u32)ms;
 244}
 245
 246void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
 247{
 248        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 249        struct ath_chanctx *ictx;
 250        struct ath_vif *avp;
 251        bool active = false;
 252        u8 n_active = 0;
 253
 254        if (!ctx)
 255                return;
 256
 257        if (ctx == &sc->offchannel.chan) {
 258                spin_lock_bh(&sc->chan_lock);
 259
 260                if (likely(sc->sched.channel_switch_time))
 261                        ctx->flush_timeout =
 262                                usecs_to_jiffies(sc->sched.channel_switch_time);
 263                else
 264                        ctx->flush_timeout =
 265                                msecs_to_jiffies(10);
 266
 267                spin_unlock_bh(&sc->chan_lock);
 268
 269                /*
 270                 * There is no need to iterate over the
 271                 * active/assigned channel contexts if
 272                 * the current context is offchannel.
 273                 */
 274                return;
 275        }
 276
 277        ictx = ctx;
 278
 279        list_for_each_entry(avp, &ctx->vifs, list) {
 280                struct ieee80211_vif *vif = avp->vif;
 281
 282                switch (vif->type) {
 283                case NL80211_IFTYPE_P2P_CLIENT:
 284                case NL80211_IFTYPE_STATION:
 285                        if (avp->assoc)
 286                                active = true;
 287                        break;
 288                default:
 289                        active = true;
 290                        break;
 291                }
 292        }
 293        ctx->active = active;
 294
 295        ath_for_each_chanctx(sc, ctx) {
 296                if (!ctx->assigned || list_empty(&ctx->vifs))
 297                        continue;
 298                n_active++;
 299        }
 300
 301        spin_lock_bh(&sc->chan_lock);
 302
 303        if (n_active <= 1) {
 304                ictx->flush_timeout = HZ / 5;
 305                clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
 306                spin_unlock_bh(&sc->chan_lock);
 307                return;
 308        }
 309
 310        ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
 311
 312        if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
 313                spin_unlock_bh(&sc->chan_lock);
 314                return;
 315        }
 316
 317        spin_unlock_bh(&sc->chan_lock);
 318
 319        if (ath9k_is_chanctx_enabled()) {
 320                ath_chanctx_event(sc, NULL,
 321                                  ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
 322        }
 323}
 324
 325static struct ath_chanctx *
 326ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
 327{
 328        int idx = ctx - &sc->chanctx[0];
 329
 330        return &sc->chanctx[!idx];
 331}
 332
 333static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
 334{
 335        struct ath_chanctx *prev, *cur;
 336        struct timespec ts;
 337        u32 cur_tsf, prev_tsf, beacon_int;
 338        s32 offset;
 339
 340        beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
 341
 342        cur = sc->cur_chan;
 343        prev = ath_chanctx_get_next(sc, cur);
 344
 345        if (!prev->switch_after_beacon)
 346                return;
 347
 348        getrawmonotonic(&ts);
 349        cur_tsf = (u32) cur->tsf_val +
 350                  ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
 351
 352        prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
 353        prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
 354
 355        /* Adjust the TSF time of the AP chanctx to keep its beacons
 356         * at half beacon interval offset relative to the STA chanctx.
 357         */
 358        offset = cur_tsf - prev_tsf;
 359
 360        /* Ignore stale data or spurious timestamps */
 361        if (offset < 0 || offset > 3 * beacon_int)
 362                return;
 363
 364        offset = beacon_int / 2 - (offset % beacon_int);
 365        prev->tsf_val += offset;
 366}
 367
 368/* Configure the TSF based hardware timer for a channel switch.
 369 * Also set up backup software timer, in case the gen timer fails.
 370 * This could be caused by a hardware reset.
 371 */
 372static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
 373{
 374        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 375        struct ath_hw *ah = sc->sc_ah;
 376        unsigned long timeout;
 377
 378        ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
 379        tsf_time -= ath9k_hw_gettsf32(ah);
 380        timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
 381        mod_timer(&sc->sched.timer, jiffies + timeout);
 382
 383        ath_dbg(common, CHAN_CTX,
 384                "Setup chanctx timer with timeout: %d (%d) ms\n",
 385                tsf_time / 1000, jiffies_to_msecs(timeout));
 386}
 387
 388static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
 389                                     struct ath_chanctx *ctx,
 390                                     struct ath_vif *avp)
 391{
 392        /*
 393         * Clear the extend_absence flag if it had been
 394         * set during the previous beacon transmission,
 395         * since we need to revert to the normal NoA
 396         * schedule.
 397         */
 398        if (ctx->active && sc->sched.extend_absence) {
 399                avp->noa_duration = 0;
 400                sc->sched.extend_absence = false;
 401        }
 402
 403        /* If at least two consecutive beacons were missed on the STA
 404         * chanctx, stay on the STA channel for one extra beacon period,
 405         * to resync the timer properly.
 406         */
 407        if (ctx->active && sc->sched.beacon_miss >= 2) {
 408                avp->noa_duration = 0;
 409                sc->sched.extend_absence = true;
 410        }
 411}
 412
 413static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
 414                                       struct ath_chanctx *ctx,
 415                                       struct ath_vif *avp,
 416                                       u32 tsf_time)
 417{
 418        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 419
 420        avp->noa_index++;
 421        avp->offchannel_start = tsf_time;
 422        avp->offchannel_duration = sc->sched.offchannel_duration;
 423
 424        ath_dbg(common, CHAN_CTX,
 425                "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
 426                avp->offchannel_duration,
 427                avp->offchannel_start,
 428                avp->noa_index);
 429
 430        /*
 431         * When multiple contexts are active, the NoA
 432         * has to be recalculated and advertised after
 433         * an offchannel operation.
 434         */
 435        if (ctx->active && avp->noa_duration)
 436                avp->noa_duration = 0;
 437}
 438
 439static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
 440                                         struct ath_vif *avp,
 441                                         struct ath_beacon_config *cur_conf,
 442                                         u32 tsf_time,
 443                                         u32 beacon_int)
 444{
 445        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 446
 447        avp->noa_index++;
 448        avp->noa_start = tsf_time;
 449
 450        if (sc->sched.extend_absence)
 451                avp->noa_duration = (3 * beacon_int / 2) +
 452                        sc->sched.channel_switch_time;
 453        else
 454                avp->noa_duration =
 455                        TU_TO_USEC(cur_conf->beacon_interval) / 2 +
 456                        sc->sched.channel_switch_time;
 457
 458        if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
 459            sc->sched.extend_absence)
 460                avp->periodic_noa = false;
 461        else
 462                avp->periodic_noa = true;
 463
 464        ath_dbg(common, CHAN_CTX,
 465                "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
 466                avp->noa_duration,
 467                avp->noa_start,
 468                avp->noa_index,
 469                avp->periodic_noa);
 470}
 471
 472static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
 473                                        struct ath_vif *avp,
 474                                        u32 tsf_time,
 475                                        u32 duration)
 476{
 477        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 478
 479        avp->noa_index++;
 480        avp->noa_start = tsf_time;
 481        avp->periodic_noa = false;
 482        avp->oneshot_noa = true;
 483        avp->noa_duration = duration + sc->sched.channel_switch_time;
 484
 485        ath_dbg(common, CHAN_CTX,
 486                "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
 487                avp->noa_duration,
 488                avp->noa_start,
 489                avp->noa_index,
 490                avp->periodic_noa);
 491}
 492
 493void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
 494                       enum ath_chanctx_event ev)
 495{
 496        struct ath_hw *ah = sc->sc_ah;
 497        struct ath_common *common = ath9k_hw_common(ah);
 498        struct ath_beacon_config *cur_conf;
 499        struct ath_vif *avp = NULL;
 500        struct ath_chanctx *ctx;
 501        u32 tsf_time;
 502        u32 beacon_int;
 503
 504        if (vif)
 505                avp = (struct ath_vif *) vif->drv_priv;
 506
 507        spin_lock_bh(&sc->chan_lock);
 508
 509        ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
 510                sc->cur_chan->chandef.center_freq1,
 511                chanctx_event_string(ev),
 512                chanctx_state_string(sc->sched.state),
 513                chanctx_event_delta(sc));
 514
 515        switch (ev) {
 516        case ATH_CHANCTX_EVENT_BEACON_PREPARE:
 517                if (avp->offchannel_duration)
 518                        avp->offchannel_duration = 0;
 519
 520                if (avp->oneshot_noa) {
 521                        avp->noa_duration = 0;
 522                        avp->oneshot_noa = false;
 523
 524                        ath_dbg(common, CHAN_CTX,
 525                                "Clearing oneshot NoA\n");
 526                }
 527
 528                if (avp->chanctx != sc->cur_chan) {
 529                        ath_dbg(common, CHAN_CTX,
 530                                "Contexts differ, not preparing beacon\n");
 531                        break;
 532                }
 533
 534                if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
 535                        sc->sched.offchannel_pending = false;
 536                        sc->next_chan = &sc->offchannel.chan;
 537                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 538                        ath_dbg(common, CHAN_CTX,
 539                                "Setting offchannel_pending to false\n");
 540                }
 541
 542                ctx = ath_chanctx_get_next(sc, sc->cur_chan);
 543                if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
 544                        sc->next_chan = ctx;
 545                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 546                        ath_dbg(common, CHAN_CTX,
 547                                "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
 548                }
 549
 550                /* if the timer missed its window, use the next interval */
 551                if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
 552                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 553                        ath_dbg(common, CHAN_CTX,
 554                                "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
 555                }
 556
 557                if (sc->sched.mgd_prepare_tx)
 558                        sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
 559
 560                /*
 561                 * When a context becomes inactive, for example,
 562                 * disassociation of a station context, the NoA
 563                 * attribute needs to be removed from subsequent
 564                 * beacons.
 565                 */
 566                if (!ctx->active && avp->noa_duration &&
 567                    sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
 568                        avp->noa_duration = 0;
 569                        avp->periodic_noa = false;
 570
 571                        ath_dbg(common, CHAN_CTX,
 572                                "Clearing NoA schedule\n");
 573                }
 574
 575                if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
 576                        break;
 577
 578                ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
 579
 580                sc->sched.beacon_pending = true;
 581                sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
 582
 583                cur_conf = &sc->cur_chan->beacon;
 584                beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
 585
 586                /* defer channel switch by a quarter beacon interval */
 587                tsf_time = sc->sched.next_tbtt + beacon_int / 4;
 588                sc->sched.switch_start_time = tsf_time;
 589                sc->cur_chan->last_beacon = sc->sched.next_tbtt;
 590
 591                /*
 592                 * If an offchannel switch is scheduled to happen after
 593                 * a beacon transmission, update the NoA with one-shot
 594                 * values and increment the index.
 595                 */
 596                if (sc->next_chan == &sc->offchannel.chan) {
 597                        ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
 598                        break;
 599                }
 600
 601                ath_chanctx_handle_bmiss(sc, ctx, avp);
 602
 603                /*
 604                 * If a mgd_prepare_tx() has been called by mac80211,
 605                 * a one-shot NoA needs to be sent. This can happen
 606                 * with one or more active channel contexts - in both
 607                 * cases, a new NoA schedule has to be advertised.
 608                 */
 609                if (sc->sched.mgd_prepare_tx) {
 610                        ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
 611                                                    jiffies_to_usecs(HZ / 5));
 612                        break;
 613                }
 614
 615                /* Prevent wrap-around issues */
 616                if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
 617                        avp->noa_duration = 0;
 618
 619                /*
 620                 * If multiple contexts are active, start periodic
 621                 * NoA and increment the index for the first
 622                 * announcement.
 623                 */
 624                if (ctx->active &&
 625                    (!avp->noa_duration || sc->sched.force_noa_update))
 626                        ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
 627                                                     tsf_time, beacon_int);
 628
 629                if (ctx->active && sc->sched.force_noa_update)
 630                        sc->sched.force_noa_update = false;
 631
 632                break;
 633        case ATH_CHANCTX_EVENT_BEACON_SENT:
 634                if (!sc->sched.beacon_pending) {
 635                        ath_dbg(common, CHAN_CTX,
 636                                "No pending beacon\n");
 637                        break;
 638                }
 639
 640                sc->sched.beacon_pending = false;
 641
 642                if (sc->sched.mgd_prepare_tx) {
 643                        sc->sched.mgd_prepare_tx = false;
 644                        complete(&sc->go_beacon);
 645                        ath_dbg(common, CHAN_CTX,
 646                                "Beacon sent, complete go_beacon\n");
 647                        break;
 648                }
 649
 650                if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
 651                        break;
 652
 653                ath_dbg(common, CHAN_CTX,
 654                        "Move chanctx state to WAIT_FOR_TIMER\n");
 655
 656                sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
 657                ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
 658                break;
 659        case ATH_CHANCTX_EVENT_TSF_TIMER:
 660                if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
 661                        break;
 662
 663                if (!sc->cur_chan->switch_after_beacon &&
 664                    sc->sched.beacon_pending)
 665                        sc->sched.beacon_miss++;
 666
 667                ath_dbg(common, CHAN_CTX,
 668                        "Move chanctx state to SWITCH\n");
 669
 670                sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
 671                ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 672                break;
 673        case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
 674                if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
 675                    sc->cur_chan == &sc->offchannel.chan)
 676                        break;
 677
 678                sc->sched.beacon_pending = false;
 679                sc->sched.beacon_miss = 0;
 680
 681                if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
 682                    !sc->sched.beacon_adjust ||
 683                    !sc->cur_chan->tsf_val)
 684                        break;
 685
 686                ath_chanctx_adjust_tbtt_delta(sc);
 687
 688                /* TSF time might have been updated by the incoming beacon,
 689                 * need update the channel switch timer to reflect the change.
 690                 */
 691                tsf_time = sc->sched.switch_start_time;
 692                tsf_time -= (u32) sc->cur_chan->tsf_val +
 693                        ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
 694                tsf_time += ath9k_hw_gettsf32(ah);
 695
 696                sc->sched.beacon_adjust = false;
 697                ath_chanctx_setup_timer(sc, tsf_time);
 698                break;
 699        case ATH_CHANCTX_EVENT_AUTHORIZED:
 700                if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
 701                    avp->chanctx != sc->cur_chan)
 702                        break;
 703
 704                ath_dbg(common, CHAN_CTX,
 705                        "Move chanctx state from FORCE_ACTIVE to IDLE\n");
 706
 707                sc->sched.state = ATH_CHANCTX_STATE_IDLE;
 708                /* fall through */
 709        case ATH_CHANCTX_EVENT_SWITCH:
 710                if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
 711                    sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
 712                    sc->cur_chan->switch_after_beacon ||
 713                    sc->cur_chan == &sc->offchannel.chan)
 714                        break;
 715
 716                /* If this is a station chanctx, stay active for a half
 717                 * beacon period (minus channel switch time)
 718                 */
 719                sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
 720                cur_conf = &sc->cur_chan->beacon;
 721
 722                ath_dbg(common, CHAN_CTX,
 723                        "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
 724
 725                sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
 726                sc->sched.wait_switch = false;
 727
 728                tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
 729
 730                if (sc->sched.extend_absence) {
 731                        sc->sched.beacon_miss = 0;
 732                        tsf_time *= 3;
 733                }
 734
 735                tsf_time -= sc->sched.channel_switch_time;
 736                tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
 737                sc->sched.switch_start_time = tsf_time;
 738
 739                ath_chanctx_setup_timer(sc, tsf_time);
 740                sc->sched.beacon_pending = true;
 741                sc->sched.beacon_adjust = true;
 742                break;
 743        case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
 744                if (sc->cur_chan == &sc->offchannel.chan ||
 745                    sc->cur_chan->switch_after_beacon)
 746                        break;
 747
 748                sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
 749                ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 750                break;
 751        case ATH_CHANCTX_EVENT_UNASSIGN:
 752                if (sc->cur_chan->assigned) {
 753                        if (sc->next_chan && !sc->next_chan->assigned &&
 754                            sc->next_chan != &sc->offchannel.chan)
 755                                sc->sched.state = ATH_CHANCTX_STATE_IDLE;
 756                        break;
 757                }
 758
 759                ctx = ath_chanctx_get_next(sc, sc->cur_chan);
 760                sc->sched.state = ATH_CHANCTX_STATE_IDLE;
 761                if (!ctx->assigned)
 762                        break;
 763
 764                sc->next_chan = ctx;
 765                ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 766                break;
 767        case ATH_CHANCTX_EVENT_ASSIGN:
 768                break;
 769        case ATH_CHANCTX_EVENT_CHANGE:
 770                break;
 771        }
 772
 773        spin_unlock_bh(&sc->chan_lock);
 774}
 775
 776void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
 777                                enum ath_chanctx_event ev)
 778{
 779        if (sc->sched.beacon_pending)
 780                ath_chanctx_event(sc, NULL, ev);
 781}
 782
 783void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
 784                                enum ath_chanctx_event ev)
 785{
 786        ath_chanctx_event(sc, NULL, ev);
 787}
 788
 789static int ath_scan_channel_duration(struct ath_softc *sc,
 790                                     struct ieee80211_channel *chan)
 791{
 792        struct cfg80211_scan_request *req = sc->offchannel.scan_req;
 793
 794        if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
 795                return (HZ / 9); /* ~110 ms */
 796
 797        return (HZ / 16); /* ~60 ms */
 798}
 799
 800static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
 801                               struct cfg80211_chan_def *chandef)
 802{
 803        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 804
 805        spin_lock_bh(&sc->chan_lock);
 806
 807        if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
 808            (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
 809                if (chandef)
 810                        ctx->chandef = *chandef;
 811
 812                sc->sched.offchannel_pending = true;
 813                sc->sched.wait_switch = true;
 814                sc->sched.offchannel_duration =
 815                        jiffies_to_usecs(sc->offchannel.duration) +
 816                        sc->sched.channel_switch_time;
 817
 818                spin_unlock_bh(&sc->chan_lock);
 819                ath_dbg(common, CHAN_CTX,
 820                        "Set offchannel_pending to true\n");
 821                return;
 822        }
 823
 824        sc->next_chan = ctx;
 825        if (chandef) {
 826                ctx->chandef = *chandef;
 827                ath_dbg(common, CHAN_CTX,
 828                        "Assigned next_chan to %d MHz\n", chandef->center_freq1);
 829        }
 830
 831        if (sc->next_chan == &sc->offchannel.chan) {
 832                sc->sched.offchannel_duration =
 833                        jiffies_to_usecs(sc->offchannel.duration) +
 834                        sc->sched.channel_switch_time;
 835
 836                if (chandef) {
 837                        ath_dbg(common, CHAN_CTX,
 838                                "Offchannel duration for chan %d MHz : %u\n",
 839                                chandef->center_freq1,
 840                                sc->sched.offchannel_duration);
 841                }
 842        }
 843        spin_unlock_bh(&sc->chan_lock);
 844        ieee80211_queue_work(sc->hw, &sc->chanctx_work);
 845}
 846
 847static void ath_chanctx_offchan_switch(struct ath_softc *sc,
 848                                       struct ieee80211_channel *chan)
 849{
 850        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 851        struct cfg80211_chan_def chandef;
 852
 853        cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
 854        ath_dbg(common, CHAN_CTX,
 855                "Channel definition created: %d MHz\n", chandef.center_freq1);
 856
 857        ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
 858}
 859
 860static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
 861                                                     bool active)
 862{
 863        struct ath_chanctx *ctx;
 864
 865        ath_for_each_chanctx(sc, ctx) {
 866                if (!ctx->assigned || list_empty(&ctx->vifs))
 867                        continue;
 868                if (active && !ctx->active)
 869                        continue;
 870
 871                if (ctx->switch_after_beacon)
 872                        return ctx;
 873        }
 874
 875        return &sc->chanctx[0];
 876}
 877
 878static void
 879ath_scan_next_channel(struct ath_softc *sc)
 880{
 881        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 882        struct cfg80211_scan_request *req = sc->offchannel.scan_req;
 883        struct ieee80211_channel *chan;
 884
 885        if (sc->offchannel.scan_idx >= req->n_channels) {
 886                ath_dbg(common, CHAN_CTX,
 887                        "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
 888                        "scan_idx: %d, n_channels: %d\n",
 889                        sc->offchannel.scan_idx,
 890                        req->n_channels);
 891
 892                sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
 893                ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
 894                                   NULL);
 895                return;
 896        }
 897
 898        ath_dbg(common, CHAN_CTX,
 899                "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
 900                sc->offchannel.scan_idx);
 901
 902        chan = req->channels[sc->offchannel.scan_idx++];
 903        sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
 904        sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
 905
 906        ath_chanctx_offchan_switch(sc, chan);
 907}
 908
 909void ath_offchannel_next(struct ath_softc *sc)
 910{
 911        struct ieee80211_vif *vif;
 912
 913        if (sc->offchannel.scan_req) {
 914                vif = sc->offchannel.scan_vif;
 915                sc->offchannel.chan.txpower = vif->bss_conf.txpower;
 916                ath_scan_next_channel(sc);
 917        } else if (sc->offchannel.roc_vif) {
 918                vif = sc->offchannel.roc_vif;
 919                sc->offchannel.chan.txpower = vif->bss_conf.txpower;
 920                sc->offchannel.duration =
 921                        msecs_to_jiffies(sc->offchannel.roc_duration);
 922                sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
 923                ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
 924        } else {
 925                spin_lock_bh(&sc->chan_lock);
 926                sc->sched.offchannel_pending = false;
 927                sc->sched.wait_switch = false;
 928                spin_unlock_bh(&sc->chan_lock);
 929
 930                ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
 931                                   NULL);
 932                sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
 933                if (sc->ps_idle)
 934                        ath_cancel_work(sc);
 935        }
 936}
 937
 938void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
 939{
 940        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 941
 942        sc->offchannel.roc_vif = NULL;
 943        sc->offchannel.roc_chan = NULL;
 944
 945        switch (reason) {
 946        case ATH_ROC_COMPLETE_ABORT:
 947                ath_dbg(common, CHAN_CTX, "RoC aborted\n");
 948                ieee80211_remain_on_channel_expired(sc->hw);
 949                break;
 950        case ATH_ROC_COMPLETE_EXPIRE:
 951                ath_dbg(common, CHAN_CTX, "RoC expired\n");
 952                ieee80211_remain_on_channel_expired(sc->hw);
 953                break;
 954        case ATH_ROC_COMPLETE_CANCEL:
 955                ath_dbg(common, CHAN_CTX, "RoC canceled\n");
 956                break;
 957        }
 958
 959        ath_offchannel_next(sc);
 960        ath9k_ps_restore(sc);
 961}
 962
 963void ath_scan_complete(struct ath_softc *sc, bool abort)
 964{
 965        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
 966        struct cfg80211_scan_info info = {
 967                .aborted = abort,
 968        };
 969
 970        if (abort)
 971                ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
 972        else
 973                ath_dbg(common, CHAN_CTX, "HW scan complete\n");
 974
 975        sc->offchannel.scan_req = NULL;
 976        sc->offchannel.scan_vif = NULL;
 977        sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
 978        ieee80211_scan_completed(sc->hw, &info);
 979        clear_bit(ATH_OP_SCANNING, &common->op_flags);
 980        spin_lock_bh(&sc->chan_lock);
 981        if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
 982                sc->sched.force_noa_update = true;
 983        spin_unlock_bh(&sc->chan_lock);
 984        ath_offchannel_next(sc);
 985        ath9k_ps_restore(sc);
 986}
 987
 988static void ath_scan_send_probe(struct ath_softc *sc,
 989                                struct cfg80211_ssid *ssid)
 990{
 991        struct cfg80211_scan_request *req = sc->offchannel.scan_req;
 992        struct ieee80211_vif *vif = sc->offchannel.scan_vif;
 993        struct ath_tx_control txctl = {};
 994        struct sk_buff *skb;
 995        struct ieee80211_tx_info *info;
 996        int band = sc->offchannel.chan.chandef.chan->band;
 997
 998        skb = ieee80211_probereq_get(sc->hw, vif->addr,
 999                        ssid->ssid, ssid->ssid_len, req->ie_len);
1000        if (!skb)
1001                return;
1002
1003        info = IEEE80211_SKB_CB(skb);
1004        if (req->no_cck)
1005                info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1006
1007        if (req->ie_len)
1008                skb_put_data(skb, req->ie, req->ie_len);
1009
1010        skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1011
1012        if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1013                goto error;
1014
1015        txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1016        if (ath_tx_start(sc->hw, skb, &txctl))
1017                goto error;
1018
1019        return;
1020
1021error:
1022        ieee80211_free_txskb(sc->hw, skb);
1023}
1024
1025static void ath_scan_channel_start(struct ath_softc *sc)
1026{
1027        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1028        struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1029        int i;
1030
1031        if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1032            req->n_ssids) {
1033                for (i = 0; i < req->n_ssids; i++)
1034                        ath_scan_send_probe(sc, &req->ssids[i]);
1035
1036        }
1037
1038        ath_dbg(common, CHAN_CTX,
1039                "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1040
1041        sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1042        mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1043}
1044
1045static void ath_chanctx_timer(unsigned long data)
1046{
1047        struct ath_softc *sc = (struct ath_softc *) data;
1048        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1049
1050        ath_dbg(common, CHAN_CTX,
1051                "Channel context timer invoked\n");
1052
1053        ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1054}
1055
1056static void ath_offchannel_timer(unsigned long data)
1057{
1058        struct ath_softc *sc = (struct ath_softc *)data;
1059        struct ath_chanctx *ctx;
1060        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1061
1062        ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1063                __func__, offchannel_state_string(sc->offchannel.state));
1064
1065        switch (sc->offchannel.state) {
1066        case ATH_OFFCHANNEL_PROBE_WAIT:
1067                if (!sc->offchannel.scan_req)
1068                        return;
1069
1070                /* get first active channel context */
1071                ctx = ath_chanctx_get_oper_chan(sc, true);
1072                if (ctx->active) {
1073                        ath_dbg(common, CHAN_CTX,
1074                                "Switch to oper/active context, "
1075                                "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1076
1077                        sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1078                        ath_chanctx_switch(sc, ctx, NULL);
1079                        mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1080                        break;
1081                }
1082                /* fall through */
1083        case ATH_OFFCHANNEL_SUSPEND:
1084                if (!sc->offchannel.scan_req)
1085                        return;
1086
1087                ath_scan_next_channel(sc);
1088                break;
1089        case ATH_OFFCHANNEL_ROC_START:
1090        case ATH_OFFCHANNEL_ROC_WAIT:
1091                sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1092                ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
1093                break;
1094        default:
1095                break;
1096        }
1097}
1098
1099static bool
1100ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1101                              bool powersave)
1102{
1103        struct ieee80211_vif *vif = avp->vif;
1104        struct ieee80211_sta *sta = NULL;
1105        struct ieee80211_hdr_3addr *nullfunc;
1106        struct ath_tx_control txctl;
1107        struct sk_buff *skb;
1108        int band = sc->cur_chan->chandef.chan->band;
1109
1110        switch (vif->type) {
1111        case NL80211_IFTYPE_STATION:
1112                if (!avp->assoc)
1113                        return false;
1114
1115                skb = ieee80211_nullfunc_get(sc->hw, vif);
1116                if (!skb)
1117                        return false;
1118
1119                nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1120                if (powersave)
1121                        nullfunc->frame_control |=
1122                                cpu_to_le16(IEEE80211_FCTL_PM);
1123
1124                skb->priority = 7;
1125                skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1126                if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1127                        dev_kfree_skb_any(skb);
1128                        return false;
1129                }
1130                break;
1131        default:
1132                return false;
1133        }
1134
1135        memset(&txctl, 0, sizeof(txctl));
1136        txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1137        txctl.sta = sta;
1138        if (ath_tx_start(sc->hw, skb, &txctl)) {
1139                ieee80211_free_txskb(sc->hw, skb);
1140                return false;
1141        }
1142
1143        return true;
1144}
1145
1146static bool
1147ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1148{
1149        struct ath_vif *avp;
1150        bool sent = false;
1151
1152        rcu_read_lock();
1153        list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1154                if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1155                        sent = true;
1156        }
1157        rcu_read_unlock();
1158
1159        return sent;
1160}
1161
1162static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1163{
1164        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1165
1166        if (sc->cur_chan == &sc->offchannel.chan)
1167                return false;
1168
1169        switch (sc->sched.state) {
1170        case ATH_CHANCTX_STATE_SWITCH:
1171                return false;
1172        case ATH_CHANCTX_STATE_IDLE:
1173                if (!sc->cur_chan->switch_after_beacon)
1174                        return false;
1175
1176                ath_dbg(common, CHAN_CTX,
1177                        "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1178
1179                sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1180                break;
1181        default:
1182                break;
1183        }
1184
1185        return true;
1186}
1187
1188static void ath_offchannel_channel_change(struct ath_softc *sc)
1189{
1190        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1191
1192        ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1193                __func__, offchannel_state_string(sc->offchannel.state));
1194
1195        switch (sc->offchannel.state) {
1196        case ATH_OFFCHANNEL_PROBE_SEND:
1197                if (!sc->offchannel.scan_req)
1198                        return;
1199
1200                if (sc->cur_chan->chandef.chan !=
1201                    sc->offchannel.chan.chandef.chan)
1202                        return;
1203
1204                ath_scan_channel_start(sc);
1205                break;
1206        case ATH_OFFCHANNEL_IDLE:
1207                if (!sc->offchannel.scan_req)
1208                        return;
1209
1210                ath_scan_complete(sc, false);
1211                break;
1212        case ATH_OFFCHANNEL_ROC_START:
1213                if (sc->cur_chan != &sc->offchannel.chan)
1214                        break;
1215
1216                sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1217                mod_timer(&sc->offchannel.timer,
1218                          jiffies + sc->offchannel.duration);
1219                ieee80211_ready_on_channel(sc->hw);
1220                break;
1221        case ATH_OFFCHANNEL_ROC_DONE:
1222                break;
1223        default:
1224                break;
1225        }
1226}
1227
1228void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1229{
1230        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1231        struct ath_chanctx *old_ctx;
1232        struct timespec ts;
1233        bool measure_time = false;
1234        bool send_ps = false;
1235        bool queues_stopped = false;
1236
1237        spin_lock_bh(&sc->chan_lock);
1238        if (!sc->next_chan) {
1239                spin_unlock_bh(&sc->chan_lock);
1240                return;
1241        }
1242
1243        if (!force && ath_chanctx_defer_switch(sc)) {
1244                spin_unlock_bh(&sc->chan_lock);
1245                return;
1246        }
1247
1248        ath_dbg(common, CHAN_CTX,
1249                "%s: current: %d MHz, next: %d MHz\n",
1250                __func__,
1251                sc->cur_chan->chandef.center_freq1,
1252                sc->next_chan->chandef.center_freq1);
1253
1254        if (sc->cur_chan != sc->next_chan) {
1255                ath_dbg(common, CHAN_CTX,
1256                        "Stopping current chanctx: %d\n",
1257                        sc->cur_chan->chandef.center_freq1);
1258                sc->cur_chan->stopped = true;
1259                spin_unlock_bh(&sc->chan_lock);
1260
1261                if (sc->next_chan == &sc->offchannel.chan) {
1262                        getrawmonotonic(&ts);
1263                        measure_time = true;
1264                }
1265
1266                ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1267                queues_stopped = true;
1268
1269                __ath9k_flush(sc->hw, ~0, true, false, false);
1270
1271                if (ath_chanctx_send_ps_frame(sc, true))
1272                        __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1273                                      false, false, false);
1274
1275                send_ps = true;
1276                spin_lock_bh(&sc->chan_lock);
1277
1278                if (sc->cur_chan != &sc->offchannel.chan) {
1279                        getrawmonotonic(&sc->cur_chan->tsf_ts);
1280                        sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1281                }
1282        }
1283        old_ctx = sc->cur_chan;
1284        sc->cur_chan = sc->next_chan;
1285        sc->cur_chan->stopped = false;
1286        sc->next_chan = NULL;
1287
1288        if (!sc->sched.offchannel_pending)
1289                sc->sched.offchannel_duration = 0;
1290
1291        if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1292                sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1293
1294        spin_unlock_bh(&sc->chan_lock);
1295
1296        if (sc->sc_ah->chip_fullsleep ||
1297            memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1298                   sizeof(sc->cur_chandef))) {
1299                ath_dbg(common, CHAN_CTX,
1300                        "%s: Set channel %d MHz\n",
1301                        __func__, sc->cur_chan->chandef.center_freq1);
1302                ath_set_channel(sc);
1303                if (measure_time)
1304                        sc->sched.channel_switch_time =
1305                                ath9k_hw_get_tsf_offset(&ts, NULL);
1306                /*
1307                 * A reset will ensure that all queues are woken up,
1308                 * so there is no need to awaken them again.
1309                 */
1310                goto out;
1311        }
1312
1313        if (queues_stopped)
1314                ath9k_chanctx_wake_queues(sc, old_ctx);
1315out:
1316        if (send_ps)
1317                ath_chanctx_send_ps_frame(sc, false);
1318
1319        ath_offchannel_channel_change(sc);
1320        ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1321}
1322
1323static void ath_chanctx_work(struct work_struct *work)
1324{
1325        struct ath_softc *sc = container_of(work, struct ath_softc,
1326                                            chanctx_work);
1327        mutex_lock(&sc->mutex);
1328        ath_chanctx_set_next(sc, false);
1329        mutex_unlock(&sc->mutex);
1330}
1331
1332void ath9k_offchannel_init(struct ath_softc *sc)
1333{
1334        struct ath_chanctx *ctx;
1335        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1336        struct ieee80211_supported_band *sband;
1337        struct ieee80211_channel *chan;
1338        int i;
1339
1340        sband = &common->sbands[NL80211_BAND_2GHZ];
1341        if (!sband->n_channels)
1342                sband = &common->sbands[NL80211_BAND_5GHZ];
1343
1344        chan = &sband->channels[0];
1345
1346        ctx = &sc->offchannel.chan;
1347        INIT_LIST_HEAD(&ctx->vifs);
1348        ctx->txpower = ATH_TXPOWER_MAX;
1349        cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1350
1351        for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
1352                INIT_LIST_HEAD(&ctx->acq[i].acq_new);
1353                INIT_LIST_HEAD(&ctx->acq[i].acq_old);
1354                spin_lock_init(&ctx->acq[i].lock);
1355        }
1356
1357        sc->offchannel.chan.offchannel = true;
1358}
1359
1360void ath9k_init_channel_context(struct ath_softc *sc)
1361{
1362        INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1363
1364        setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1365                    (unsigned long)sc);
1366        setup_timer(&sc->sched.timer, ath_chanctx_timer,
1367                    (unsigned long)sc);
1368
1369        init_completion(&sc->go_beacon);
1370}
1371
1372void ath9k_deinit_channel_context(struct ath_softc *sc)
1373{
1374        cancel_work_sync(&sc->chanctx_work);
1375}
1376
1377bool ath9k_is_chanctx_enabled(void)
1378{
1379        return (ath9k_use_chanctx == 1);
1380}
1381
1382/********************/
1383/* Queue management */
1384/********************/
1385
1386void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1387{
1388        struct ath_hw *ah = sc->sc_ah;
1389        int i;
1390
1391        if (ctx == &sc->offchannel.chan) {
1392                ieee80211_stop_queue(sc->hw,
1393                                     sc->hw->offchannel_tx_hw_queue);
1394        } else {
1395                for (i = 0; i < IEEE80211_NUM_ACS; i++)
1396                        ieee80211_stop_queue(sc->hw,
1397                                             ctx->hw_queue_base + i);
1398        }
1399
1400        if (ah->opmode == NL80211_IFTYPE_AP)
1401                ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1402}
1403
1404
1405void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1406{
1407        struct ath_hw *ah = sc->sc_ah;
1408        int i;
1409
1410        if (ctx == &sc->offchannel.chan) {
1411                ieee80211_wake_queue(sc->hw,
1412                                     sc->hw->offchannel_tx_hw_queue);
1413        } else {
1414                for (i = 0; i < IEEE80211_NUM_ACS; i++)
1415                        ieee80211_wake_queue(sc->hw,
1416                                             ctx->hw_queue_base + i);
1417        }
1418
1419        if (ah->opmode == NL80211_IFTYPE_AP)
1420                ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1421}
1422
1423/*****************/
1424/* P2P Powersave */
1425/*****************/
1426
1427static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1428{
1429        struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1430        struct ath_hw *ah = sc->sc_ah;
1431        u32 tsf, target_tsf;
1432
1433        if (!avp || !avp->noa.has_next_tsf)
1434                return;
1435
1436        ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1437
1438        tsf = ath9k_hw_gettsf32(sc->sc_ah);
1439
1440        target_tsf = avp->noa.next_tsf;
1441        if (!avp->noa.absent)
1442                target_tsf -= ATH_P2P_PS_STOP_TIME;
1443        else
1444                target_tsf += ATH_P2P_PS_STOP_TIME;
1445
1446        if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1447                target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1448
1449        ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1450                __func__, avp->noa.absent, tsf, target_tsf,
1451                (target_tsf - tsf) / 1000);
1452
1453        ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
1454}
1455
1456static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1457{
1458        struct ath_vif *avp = (void *)vif->drv_priv;
1459        u32 tsf;
1460
1461        if (!sc->p2p_ps_timer)
1462                return;
1463
1464        if (vif->type != NL80211_IFTYPE_STATION)
1465                return;
1466
1467        sc->p2p_ps_vif = avp;
1468
1469        if (sc->ps_flags & PS_BEACON_SYNC)
1470                return;
1471
1472        tsf = ath9k_hw_gettsf32(sc->sc_ah);
1473        ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1474        ath9k_update_p2p_ps_timer(sc, avp);
1475}
1476
1477static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1478{
1479        struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1480        u8 switch_time, ctwin;
1481
1482        /*
1483         * Channel switch in multi-channel mode is deferred
1484         * by a quarter beacon interval when handling
1485         * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1486         * interface is guaranteed to be discoverable
1487         * for that duration after a TBTT.
1488         */
1489        switch_time = cur_conf->beacon_interval / 4;
1490
1491        ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1492        if (ctwin && (ctwin < switch_time))
1493                return ctwin;
1494
1495        if (switch_time < P2P_DEFAULT_CTWIN)
1496                return 0;
1497
1498        return P2P_DEFAULT_CTWIN;
1499}
1500
1501void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1502                          struct sk_buff *skb)
1503{
1504        static const u8 noa_ie_hdr[] = {
1505                WLAN_EID_VENDOR_SPECIFIC,       /* type */
1506                0,                              /* length */
1507                0x50, 0x6f, 0x9a,               /* WFA OUI */
1508                0x09,                           /* P2P subtype */
1509                0x0c,                           /* Notice of Absence */
1510                0x00,                           /* LSB of little-endian len */
1511                0x00,                           /* MSB of little-endian len */
1512        };
1513
1514        struct ieee80211_p2p_noa_attr *noa;
1515        int noa_len, noa_desc, i = 0;
1516        u8 *hdr;
1517
1518        if (!avp->offchannel_duration && !avp->noa_duration)
1519                return;
1520
1521        noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
1522        noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1523
1524        hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
1525        hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1526        hdr[7] = noa_len;
1527
1528        noa = skb_put_zero(skb, noa_len);
1529
1530        noa->index = avp->noa_index;
1531        noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1532        if (noa->oppps_ctwindow)
1533                noa->oppps_ctwindow |= BIT(7);
1534
1535        if (avp->noa_duration) {
1536                if (avp->periodic_noa) {
1537                        u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1538                        noa->desc[i].count = 255;
1539                        noa->desc[i].interval = cpu_to_le32(interval);
1540                } else {
1541                        noa->desc[i].count = 1;
1542                }
1543
1544                noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1545                noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
1546                i++;
1547        }
1548
1549        if (avp->offchannel_duration) {
1550                noa->desc[i].count = 1;
1551                noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1552                noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1553        }
1554}
1555
1556void ath9k_p2p_ps_timer(void *priv)
1557{
1558        struct ath_softc *sc = priv;
1559        struct ath_vif *avp = sc->p2p_ps_vif;
1560        struct ieee80211_vif *vif;
1561        struct ieee80211_sta *sta;
1562        struct ath_node *an;
1563        u32 tsf;
1564
1565        del_timer_sync(&sc->sched.timer);
1566        ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1567        ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1568
1569        if (!avp || avp->chanctx != sc->cur_chan)
1570                return;
1571
1572        tsf = ath9k_hw_gettsf32(sc->sc_ah);
1573        if (!avp->noa.absent)
1574                tsf += ATH_P2P_PS_STOP_TIME;
1575        else
1576                tsf -= ATH_P2P_PS_STOP_TIME;
1577
1578        if (!avp->noa.has_next_tsf ||
1579            avp->noa.next_tsf - tsf > BIT(31))
1580                ieee80211_update_p2p_noa(&avp->noa, tsf);
1581
1582        ath9k_update_p2p_ps_timer(sc, avp);
1583
1584        rcu_read_lock();
1585
1586        vif = avp->vif;
1587        sta = ieee80211_find_sta(vif, avp->bssid);
1588        if (!sta)
1589                goto out;
1590
1591        an = (void *) sta->drv_priv;
1592        if (an->sleeping == !!avp->noa.absent)
1593                goto out;
1594
1595        an->sleeping = avp->noa.absent;
1596        if (an->sleeping)
1597                ath_tx_aggr_sleep(sta, sc, an);
1598        else
1599                ath_tx_aggr_wakeup(sc, an);
1600
1601out:
1602        rcu_read_unlock();
1603}
1604
1605void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1606                                struct ieee80211_vif *vif)
1607{
1608        unsigned long flags;
1609
1610        spin_lock_bh(&sc->sc_pcu_lock);
1611        spin_lock_irqsave(&sc->sc_pm_lock, flags);
1612        ath9k_update_p2p_ps(sc, vif);
1613        spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1614        spin_unlock_bh(&sc->sc_pcu_lock);
1615}
1616
1617void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1618{
1619        if (sc->p2p_ps_vif)
1620                ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1621}
1622
1623void ath9k_p2p_remove_vif(struct ath_softc *sc,
1624                          struct ieee80211_vif *vif)
1625{
1626        struct ath_vif *avp = (void *)vif->drv_priv;
1627
1628        spin_lock_bh(&sc->sc_pcu_lock);
1629        if (avp == sc->p2p_ps_vif) {
1630                sc->p2p_ps_vif = NULL;
1631                ath9k_update_p2p_ps_timer(sc, NULL);
1632        }
1633        spin_unlock_bh(&sc->sc_pcu_lock);
1634}
1635
1636int ath9k_init_p2p(struct ath_softc *sc)
1637{
1638        sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1639                                               NULL, sc, AR_FIRST_NDP_TIMER);
1640        if (!sc->p2p_ps_timer)
1641                return -ENOMEM;
1642
1643        return 0;
1644}
1645
1646void ath9k_deinit_p2p(struct ath_softc *sc)
1647{
1648        if (sc->p2p_ps_timer)
1649                ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1650}
1651
1652#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */
1653