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