Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / 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
360         ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
361         tsf_time -= ath9k_hw_gettsf32(ah);
362         tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
363         mod_timer(&sc->sched.timer, jiffies + tsf_time);
364
365         ath_dbg(common, CHAN_CTX,
366                 "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time));
367 }
368
369 static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
370                                      struct ath_chanctx *ctx,
371                                      struct ath_vif *avp)
372 {
373         /*
374          * Clear the extend_absence flag if it had been
375          * set during the previous beacon transmission,
376          * since we need to revert to the normal NoA
377          * schedule.
378          */
379         if (ctx->active && sc->sched.extend_absence) {
380                 avp->noa_duration = 0;
381                 sc->sched.extend_absence = false;
382         }
383
384         /* If at least two consecutive beacons were missed on the STA
385          * chanctx, stay on the STA channel for one extra beacon period,
386          * to resync the timer properly.
387          */
388         if (ctx->active && sc->sched.beacon_miss >= 2) {
389                 avp->noa_duration = 0;
390                 sc->sched.extend_absence = true;
391         }
392 }
393
394 static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
395                                        struct ath_chanctx *ctx,
396                                        struct ath_vif *avp,
397                                        u32 tsf_time)
398 {
399         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
400
401         avp->noa_index++;
402         avp->offchannel_start = tsf_time;
403         avp->offchannel_duration = sc->sched.offchannel_duration;
404
405         ath_dbg(common, CHAN_CTX,
406                 "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n",
407                 avp->offchannel_duration,
408                 avp->offchannel_start,
409                 avp->noa_index);
410
411         /*
412          * When multiple contexts are active, the NoA
413          * has to be recalculated and advertised after
414          * an offchannel operation.
415          */
416         if (ctx->active && avp->noa_duration)
417                 avp->noa_duration = 0;
418 }
419
420 static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
421                                          struct ath_vif *avp,
422                                          struct ath_beacon_config *cur_conf,
423                                          u32 tsf_time,
424                                          u32 beacon_int)
425 {
426         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
427
428         avp->noa_index++;
429         avp->noa_start = tsf_time;
430
431         if (sc->sched.extend_absence)
432                 avp->noa_duration = (3 * beacon_int / 2) +
433                         sc->sched.channel_switch_time;
434         else
435                 avp->noa_duration =
436                         TU_TO_USEC(cur_conf->beacon_interval) / 2 +
437                         sc->sched.channel_switch_time;
438
439         if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
440             sc->sched.extend_absence)
441                 avp->periodic_noa = false;
442         else
443                 avp->periodic_noa = true;
444
445         ath_dbg(common, CHAN_CTX,
446                 "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
447                 avp->noa_duration,
448                 avp->noa_start,
449                 avp->noa_index,
450                 avp->periodic_noa);
451 }
452
453 static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
454                                         struct ath_vif *avp,
455                                         u32 tsf_time,
456                                         u32 duration)
457 {
458         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
459
460         avp->noa_index++;
461         avp->noa_start = tsf_time;
462         avp->periodic_noa = false;
463         avp->oneshot_noa = true;
464         avp->noa_duration = duration + sc->sched.channel_switch_time;
465
466         ath_dbg(common, CHAN_CTX,
467                 "oneshot noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
468                 avp->noa_duration,
469                 avp->noa_start,
470                 avp->noa_index,
471                 avp->periodic_noa);
472 }
473
474 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
475                        enum ath_chanctx_event ev)
476 {
477         struct ath_hw *ah = sc->sc_ah;
478         struct ath_common *common = ath9k_hw_common(ah);
479         struct ath_beacon_config *cur_conf;
480         struct ath_vif *avp = NULL;
481         struct ath_chanctx *ctx;
482         u32 tsf_time;
483         u32 beacon_int;
484
485         if (vif)
486                 avp = (struct ath_vif *) vif->drv_priv;
487
488         spin_lock_bh(&sc->chan_lock);
489
490         ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
491                 sc->cur_chan->chandef.center_freq1,
492                 chanctx_event_string(ev),
493                 chanctx_state_string(sc->sched.state));
494
495         switch (ev) {
496         case ATH_CHANCTX_EVENT_BEACON_PREPARE:
497                 if (avp->offchannel_duration)
498                         avp->offchannel_duration = 0;
499
500                 if (avp->oneshot_noa) {
501                         avp->noa_duration = 0;
502                         avp->oneshot_noa = false;
503
504                         ath_dbg(common, CHAN_CTX,
505                                 "Clearing oneshot NoA\n");
506                 }
507
508                 if (avp->chanctx != sc->cur_chan) {
509                         ath_dbg(common, CHAN_CTX,
510                                 "Contexts differ, not preparing beacon\n");
511                         break;
512                 }
513
514                 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
515                         sc->sched.offchannel_pending = false;
516                         sc->next_chan = &sc->offchannel.chan;
517                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
518                         ath_dbg(common, CHAN_CTX,
519                                 "Setting offchannel_pending to false\n");
520                 }
521
522                 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
523                 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
524                         sc->next_chan = ctx;
525                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
526                         ath_dbg(common, CHAN_CTX,
527                                 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
528                 }
529
530                 /* if the timer missed its window, use the next interval */
531                 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
532                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
533                         ath_dbg(common, CHAN_CTX,
534                                 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
535                 }
536
537                 if (sc->sched.mgd_prepare_tx)
538                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
539
540                 /*
541                  * When a context becomes inactive, for example,
542                  * disassociation of a station context, the NoA
543                  * attribute needs to be removed from subsequent
544                  * beacons.
545                  */
546                 if (!ctx->active && avp->noa_duration &&
547                     sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
548                         avp->noa_duration = 0;
549                         avp->periodic_noa = false;
550
551                         ath_dbg(common, CHAN_CTX,
552                                 "Clearing NoA schedule\n");
553                 }
554
555                 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
556                         break;
557
558                 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
559
560                 sc->sched.beacon_pending = true;
561                 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
562
563                 cur_conf = &sc->cur_chan->beacon;
564                 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
565
566                 /* defer channel switch by a quarter beacon interval */
567                 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
568                 sc->sched.switch_start_time = tsf_time;
569                 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
570
571                 /*
572                  * If an offchannel switch is scheduled to happen after
573                  * a beacon transmission, update the NoA with one-shot
574                  * values and increment the index.
575                  */
576                 if (sc->next_chan == &sc->offchannel.chan) {
577                         ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
578                         break;
579                 }
580
581                 ath_chanctx_handle_bmiss(sc, ctx, avp);
582
583                 /*
584                  * If a mgd_prepare_tx() has been called by mac80211,
585                  * a one-shot NoA needs to be sent. This can happen
586                  * with one or more active channel contexts - in both
587                  * cases, a new NoA schedule has to be advertised.
588                  */
589                 if (sc->sched.mgd_prepare_tx) {
590                         ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
591                                                     jiffies_to_usecs(HZ / 5));
592                         break;
593                 }
594
595                 /* Prevent wrap-around issues */
596                 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
597                         avp->noa_duration = 0;
598
599                 /*
600                  * If multiple contexts are active, start periodic
601                  * NoA and increment the index for the first
602                  * announcement.
603                  */
604                 if (ctx->active &&
605                     (!avp->noa_duration || sc->sched.force_noa_update))
606                         ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
607                                                      tsf_time, beacon_int);
608
609                 if (ctx->active && sc->sched.force_noa_update)
610                         sc->sched.force_noa_update = false;
611
612                 break;
613         case ATH_CHANCTX_EVENT_BEACON_SENT:
614                 if (!sc->sched.beacon_pending) {
615                         ath_dbg(common, CHAN_CTX,
616                                 "No pending beacon\n");
617                         break;
618                 }
619
620                 sc->sched.beacon_pending = false;
621
622                 if (sc->sched.mgd_prepare_tx) {
623                         sc->sched.mgd_prepare_tx = false;
624                         complete(&sc->go_beacon);
625                         ath_dbg(common, CHAN_CTX,
626                                 "Beacon sent, complete go_beacon\n");
627                         break;
628                 }
629
630                 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
631                         break;
632
633                 ath_dbg(common, CHAN_CTX,
634                         "Move chanctx state to WAIT_FOR_TIMER\n");
635
636                 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
637                 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
638                 break;
639         case ATH_CHANCTX_EVENT_TSF_TIMER:
640                 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
641                         break;
642
643                 if (!sc->cur_chan->switch_after_beacon &&
644                     sc->sched.beacon_pending)
645                         sc->sched.beacon_miss++;
646
647                 ath_dbg(common, CHAN_CTX,
648                         "Move chanctx state to SWITCH\n");
649
650                 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
651                 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
652                 break;
653         case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
654                 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
655                     sc->cur_chan == &sc->offchannel.chan)
656                         break;
657
658                 sc->sched.beacon_pending = false;
659                 sc->sched.beacon_miss = 0;
660
661                 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
662                     !sc->sched.beacon_adjust ||
663                     !sc->cur_chan->tsf_val)
664                         break;
665
666                 ath_chanctx_adjust_tbtt_delta(sc);
667
668                 /* TSF time might have been updated by the incoming beacon,
669                  * need update the channel switch timer to reflect the change.
670                  */
671                 tsf_time = sc->sched.switch_start_time;
672                 tsf_time -= (u32) sc->cur_chan->tsf_val +
673                         ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
674                 tsf_time += ath9k_hw_gettsf32(ah);
675
676                 sc->sched.beacon_adjust = false;
677                 ath_chanctx_setup_timer(sc, tsf_time);
678                 break;
679         case ATH_CHANCTX_EVENT_AUTHORIZED:
680                 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
681                     avp->chanctx != sc->cur_chan)
682                         break;
683
684                 ath_dbg(common, CHAN_CTX,
685                         "Move chanctx state from FORCE_ACTIVE to IDLE\n");
686
687                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
688                 /* fall through */
689         case ATH_CHANCTX_EVENT_SWITCH:
690                 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
691                     sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
692                     sc->cur_chan->switch_after_beacon ||
693                     sc->cur_chan == &sc->offchannel.chan)
694                         break;
695
696                 /* If this is a station chanctx, stay active for a half
697                  * beacon period (minus channel switch time)
698                  */
699                 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
700                 cur_conf = &sc->cur_chan->beacon;
701
702                 ath_dbg(common, CHAN_CTX,
703                         "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
704
705                 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
706                 sc->sched.wait_switch = false;
707
708                 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
709
710                 if (sc->sched.extend_absence) {
711                         sc->sched.beacon_miss = 0;
712                         tsf_time *= 3;
713                 }
714
715                 tsf_time -= sc->sched.channel_switch_time;
716                 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
717                 sc->sched.switch_start_time = tsf_time;
718
719                 ath_chanctx_setup_timer(sc, tsf_time);
720                 sc->sched.beacon_pending = true;
721                 sc->sched.beacon_adjust = true;
722                 break;
723         case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
724                 if (sc->cur_chan == &sc->offchannel.chan ||
725                     sc->cur_chan->switch_after_beacon)
726                         break;
727
728                 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
729                 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
730                 break;
731         case ATH_CHANCTX_EVENT_UNASSIGN:
732                 if (sc->cur_chan->assigned) {
733                         if (sc->next_chan && !sc->next_chan->assigned &&
734                             sc->next_chan != &sc->offchannel.chan)
735                                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
736                         break;
737                 }
738
739                 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
740                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
741                 if (!ctx->assigned)
742                         break;
743
744                 sc->next_chan = ctx;
745                 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
746                 break;
747         case ATH_CHANCTX_EVENT_ASSIGN:
748                 break;
749         case ATH_CHANCTX_EVENT_CHANGE:
750                 break;
751         }
752
753         spin_unlock_bh(&sc->chan_lock);
754 }
755
756 void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
757                                 enum ath_chanctx_event ev)
758 {
759         if (sc->sched.beacon_pending)
760                 ath_chanctx_event(sc, NULL, ev);
761 }
762
763 void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
764                                 enum ath_chanctx_event ev)
765 {
766         ath_chanctx_event(sc, NULL, ev);
767 }
768
769 static int ath_scan_channel_duration(struct ath_softc *sc,
770                                      struct ieee80211_channel *chan)
771 {
772         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
773
774         if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
775                 return (HZ / 9); /* ~110 ms */
776
777         return (HZ / 16); /* ~60 ms */
778 }
779
780 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
781                                struct cfg80211_chan_def *chandef)
782 {
783         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
784
785         spin_lock_bh(&sc->chan_lock);
786
787         if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
788             (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
789                 if (chandef)
790                         ctx->chandef = *chandef;
791
792                 sc->sched.offchannel_pending = true;
793                 sc->sched.wait_switch = true;
794                 sc->sched.offchannel_duration =
795                         jiffies_to_usecs(sc->offchannel.duration) +
796                         sc->sched.channel_switch_time;
797
798                 spin_unlock_bh(&sc->chan_lock);
799                 ath_dbg(common, CHAN_CTX,
800                         "Set offchannel_pending to true\n");
801                 return;
802         }
803
804         sc->next_chan = ctx;
805         if (chandef) {
806                 ctx->chandef = *chandef;
807                 ath_dbg(common, CHAN_CTX,
808                         "Assigned next_chan to %d MHz\n", chandef->center_freq1);
809         }
810
811         if (sc->next_chan == &sc->offchannel.chan) {
812                 sc->sched.offchannel_duration =
813                         jiffies_to_usecs(sc->offchannel.duration) +
814                         sc->sched.channel_switch_time;
815
816                 if (chandef) {
817                         ath_dbg(common, CHAN_CTX,
818                                 "Offchannel duration for chan %d MHz : %u\n",
819                                 chandef->center_freq1,
820                                 sc->sched.offchannel_duration);
821                 }
822         }
823         spin_unlock_bh(&sc->chan_lock);
824         ieee80211_queue_work(sc->hw, &sc->chanctx_work);
825 }
826
827 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
828                                        struct ieee80211_channel *chan)
829 {
830         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
831         struct cfg80211_chan_def chandef;
832
833         cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
834         ath_dbg(common, CHAN_CTX,
835                 "Channel definition created: %d MHz\n", chandef.center_freq1);
836
837         ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
838 }
839
840 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
841                                                      bool active)
842 {
843         struct ath_chanctx *ctx;
844
845         ath_for_each_chanctx(sc, ctx) {
846                 if (!ctx->assigned || list_empty(&ctx->vifs))
847                         continue;
848                 if (active && !ctx->active)
849                         continue;
850
851                 if (ctx->switch_after_beacon)
852                         return ctx;
853         }
854
855         return &sc->chanctx[0];
856 }
857
858 static void
859 ath_scan_next_channel(struct ath_softc *sc)
860 {
861         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
862         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
863         struct ieee80211_channel *chan;
864
865         if (sc->offchannel.scan_idx >= req->n_channels) {
866                 ath_dbg(common, CHAN_CTX,
867                         "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
868                         "scan_idx: %d, n_channels: %d\n",
869                         sc->offchannel.scan_idx,
870                         req->n_channels);
871
872                 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
873                 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
874                                    NULL);
875                 return;
876         }
877
878         ath_dbg(common, CHAN_CTX,
879                 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
880                 sc->offchannel.scan_idx);
881
882         chan = req->channels[sc->offchannel.scan_idx++];
883         sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
884         sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
885
886         ath_chanctx_offchan_switch(sc, chan);
887 }
888
889 void ath_offchannel_next(struct ath_softc *sc)
890 {
891         struct ieee80211_vif *vif;
892
893         if (sc->offchannel.scan_req) {
894                 vif = sc->offchannel.scan_vif;
895                 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
896                 ath_scan_next_channel(sc);
897         } else if (sc->offchannel.roc_vif) {
898                 vif = sc->offchannel.roc_vif;
899                 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
900                 sc->offchannel.duration =
901                         msecs_to_jiffies(sc->offchannel.roc_duration);
902                 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
903                 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
904         } else {
905                 spin_lock_bh(&sc->chan_lock);
906                 sc->sched.offchannel_pending = false;
907                 sc->sched.wait_switch = false;
908                 spin_unlock_bh(&sc->chan_lock);
909
910                 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
911                                    NULL);
912                 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
913                 if (sc->ps_idle)
914                         ath_cancel_work(sc);
915         }
916 }
917
918 void ath_roc_complete(struct ath_softc *sc, bool abort)
919 {
920         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
921
922         if (abort)
923                 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
924         else
925                 ath_dbg(common, CHAN_CTX, "RoC expired\n");
926
927         sc->offchannel.roc_vif = NULL;
928         sc->offchannel.roc_chan = NULL;
929         ieee80211_remain_on_channel_expired(sc->hw);
930         ath_offchannel_next(sc);
931         ath9k_ps_restore(sc);
932 }
933
934 void ath_scan_complete(struct ath_softc *sc, bool abort)
935 {
936         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
937
938         if (abort)
939                 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
940         else
941                 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
942
943         sc->offchannel.scan_req = NULL;
944         sc->offchannel.scan_vif = NULL;
945         sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
946         ieee80211_scan_completed(sc->hw, abort);
947         clear_bit(ATH_OP_SCANNING, &common->op_flags);
948         spin_lock_bh(&sc->chan_lock);
949         if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
950                 sc->sched.force_noa_update = true;
951         spin_unlock_bh(&sc->chan_lock);
952         ath_offchannel_next(sc);
953         ath9k_ps_restore(sc);
954 }
955
956 static void ath_scan_send_probe(struct ath_softc *sc,
957                                 struct cfg80211_ssid *ssid)
958 {
959         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
960         struct ieee80211_vif *vif = sc->offchannel.scan_vif;
961         struct ath_tx_control txctl = {};
962         struct sk_buff *skb;
963         struct ieee80211_tx_info *info;
964         int band = sc->offchannel.chan.chandef.chan->band;
965
966         skb = ieee80211_probereq_get(sc->hw, vif->addr,
967                         ssid->ssid, ssid->ssid_len, req->ie_len);
968         if (!skb)
969                 return;
970
971         info = IEEE80211_SKB_CB(skb);
972         if (req->no_cck)
973                 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
974
975         if (req->ie_len)
976                 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
977
978         skb_set_queue_mapping(skb, IEEE80211_AC_VO);
979
980         if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
981                 goto error;
982
983         txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
984         txctl.force_channel = true;
985         if (ath_tx_start(sc->hw, skb, &txctl))
986                 goto error;
987
988         return;
989
990 error:
991         ieee80211_free_txskb(sc->hw, skb);
992 }
993
994 static void ath_scan_channel_start(struct ath_softc *sc)
995 {
996         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
997         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
998         int i;
999
1000         if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1001             req->n_ssids) {
1002                 for (i = 0; i < req->n_ssids; i++)
1003                         ath_scan_send_probe(sc, &req->ssids[i]);
1004
1005         }
1006
1007         ath_dbg(common, CHAN_CTX,
1008                 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1009
1010         sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1011         mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1012 }
1013
1014 static void ath_chanctx_timer(unsigned long data)
1015 {
1016         struct ath_softc *sc = (struct ath_softc *) data;
1017         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1018
1019         ath_dbg(common, CHAN_CTX,
1020                 "Channel context timer invoked\n");
1021
1022         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1023 }
1024
1025 static void ath_offchannel_timer(unsigned long data)
1026 {
1027         struct ath_softc *sc = (struct ath_softc *)data;
1028         struct ath_chanctx *ctx;
1029         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1030
1031         ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1032                 __func__, offchannel_state_string(sc->offchannel.state));
1033
1034         switch (sc->offchannel.state) {
1035         case ATH_OFFCHANNEL_PROBE_WAIT:
1036                 if (!sc->offchannel.scan_req)
1037                         return;
1038
1039                 /* get first active channel context */
1040                 ctx = ath_chanctx_get_oper_chan(sc, true);
1041                 if (ctx->active) {
1042                         ath_dbg(common, CHAN_CTX,
1043                                 "Switch to oper/active context, "
1044                                 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1045
1046                         sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1047                         ath_chanctx_switch(sc, ctx, NULL);
1048                         mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1049                         break;
1050                 }
1051                 /* fall through */
1052         case ATH_OFFCHANNEL_SUSPEND:
1053                 if (!sc->offchannel.scan_req)
1054                         return;
1055
1056                 ath_scan_next_channel(sc);
1057                 break;
1058         case ATH_OFFCHANNEL_ROC_START:
1059         case ATH_OFFCHANNEL_ROC_WAIT:
1060                 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1061                 ath_roc_complete(sc, false);
1062                 break;
1063         default:
1064                 break;
1065         }
1066 }
1067
1068 static bool
1069 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1070                               bool powersave)
1071 {
1072         struct ieee80211_vif *vif = avp->vif;
1073         struct ieee80211_sta *sta = NULL;
1074         struct ieee80211_hdr_3addr *nullfunc;
1075         struct ath_tx_control txctl;
1076         struct sk_buff *skb;
1077         int band = sc->cur_chan->chandef.chan->band;
1078
1079         switch (vif->type) {
1080         case NL80211_IFTYPE_STATION:
1081                 if (!avp->assoc)
1082                         return false;
1083
1084                 skb = ieee80211_nullfunc_get(sc->hw, vif);
1085                 if (!skb)
1086                         return false;
1087
1088                 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1089                 if (powersave)
1090                         nullfunc->frame_control |=
1091                                 cpu_to_le16(IEEE80211_FCTL_PM);
1092
1093                 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1094                 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1095                         dev_kfree_skb_any(skb);
1096                         return false;
1097                 }
1098                 break;
1099         default:
1100                 return false;
1101         }
1102
1103         memset(&txctl, 0, sizeof(txctl));
1104         txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1105         txctl.sta = sta;
1106         txctl.force_channel = true;
1107         if (ath_tx_start(sc->hw, skb, &txctl)) {
1108                 ieee80211_free_txskb(sc->hw, skb);
1109                 return false;
1110         }
1111
1112         return true;
1113 }
1114
1115 static bool
1116 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1117 {
1118         struct ath_vif *avp;
1119         bool sent = false;
1120
1121         rcu_read_lock();
1122         list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1123                 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1124                         sent = true;
1125         }
1126         rcu_read_unlock();
1127
1128         return sent;
1129 }
1130
1131 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1132 {
1133         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1134
1135         if (sc->cur_chan == &sc->offchannel.chan)
1136                 return false;
1137
1138         switch (sc->sched.state) {
1139         case ATH_CHANCTX_STATE_SWITCH:
1140                 return false;
1141         case ATH_CHANCTX_STATE_IDLE:
1142                 if (!sc->cur_chan->switch_after_beacon)
1143                         return false;
1144
1145                 ath_dbg(common, CHAN_CTX,
1146                         "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1147
1148                 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1149                 break;
1150         default:
1151                 break;
1152         }
1153
1154         return true;
1155 }
1156
1157 static void ath_offchannel_channel_change(struct ath_softc *sc)
1158 {
1159         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1160
1161         ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1162                 __func__, offchannel_state_string(sc->offchannel.state));
1163
1164         switch (sc->offchannel.state) {
1165         case ATH_OFFCHANNEL_PROBE_SEND:
1166                 if (!sc->offchannel.scan_req)
1167                         return;
1168
1169                 if (sc->cur_chan->chandef.chan !=
1170                     sc->offchannel.chan.chandef.chan)
1171                         return;
1172
1173                 ath_scan_channel_start(sc);
1174                 break;
1175         case ATH_OFFCHANNEL_IDLE:
1176                 if (!sc->offchannel.scan_req)
1177                         return;
1178
1179                 ath_scan_complete(sc, false);
1180                 break;
1181         case ATH_OFFCHANNEL_ROC_START:
1182                 if (sc->cur_chan != &sc->offchannel.chan)
1183                         break;
1184
1185                 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1186                 mod_timer(&sc->offchannel.timer,
1187                           jiffies + sc->offchannel.duration);
1188                 ieee80211_ready_on_channel(sc->hw);
1189                 break;
1190         case ATH_OFFCHANNEL_ROC_DONE:
1191                 break;
1192         default:
1193                 break;
1194         }
1195 }
1196
1197 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1198 {
1199         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1200         struct ath_chanctx *old_ctx;
1201         struct timespec ts;
1202         bool measure_time = false;
1203         bool send_ps = false;
1204         bool queues_stopped = false;
1205
1206         spin_lock_bh(&sc->chan_lock);
1207         if (!sc->next_chan) {
1208                 spin_unlock_bh(&sc->chan_lock);
1209                 return;
1210         }
1211
1212         if (!force && ath_chanctx_defer_switch(sc)) {
1213                 spin_unlock_bh(&sc->chan_lock);
1214                 return;
1215         }
1216
1217         ath_dbg(common, CHAN_CTX,
1218                 "%s: current: %d MHz, next: %d MHz\n",
1219                 __func__,
1220                 sc->cur_chan->chandef.center_freq1,
1221                 sc->next_chan->chandef.center_freq1);
1222
1223         if (sc->cur_chan != sc->next_chan) {
1224                 ath_dbg(common, CHAN_CTX,
1225                         "Stopping current chanctx: %d\n",
1226                         sc->cur_chan->chandef.center_freq1);
1227                 sc->cur_chan->stopped = true;
1228                 spin_unlock_bh(&sc->chan_lock);
1229
1230                 if (sc->next_chan == &sc->offchannel.chan) {
1231                         getrawmonotonic(&ts);
1232                         measure_time = true;
1233                 }
1234
1235                 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1236                 queues_stopped = true;
1237
1238                 __ath9k_flush(sc->hw, ~0, true, false, false);
1239
1240                 if (ath_chanctx_send_ps_frame(sc, true))
1241                         __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1242                                       false, false, false);
1243
1244                 send_ps = true;
1245                 spin_lock_bh(&sc->chan_lock);
1246
1247                 if (sc->cur_chan != &sc->offchannel.chan) {
1248                         getrawmonotonic(&sc->cur_chan->tsf_ts);
1249                         sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1250                 }
1251         }
1252         old_ctx = sc->cur_chan;
1253         sc->cur_chan = sc->next_chan;
1254         sc->cur_chan->stopped = false;
1255         sc->next_chan = NULL;
1256
1257         if (!sc->sched.offchannel_pending)
1258                 sc->sched.offchannel_duration = 0;
1259
1260         if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1261                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1262
1263         spin_unlock_bh(&sc->chan_lock);
1264
1265         if (sc->sc_ah->chip_fullsleep ||
1266             memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1267                    sizeof(sc->cur_chandef))) {
1268                 ath_dbg(common, CHAN_CTX,
1269                         "%s: Set channel %d MHz\n",
1270                         __func__, sc->cur_chan->chandef.center_freq1);
1271                 ath_set_channel(sc);
1272                 if (measure_time)
1273                         sc->sched.channel_switch_time =
1274                                 ath9k_hw_get_tsf_offset(&ts, NULL);
1275                 /*
1276                  * A reset will ensure that all queues are woken up,
1277                  * so there is no need to awaken them again.
1278                  */
1279                 goto out;
1280         }
1281
1282         if (queues_stopped)
1283                 ath9k_chanctx_wake_queues(sc, old_ctx);
1284 out:
1285         if (send_ps)
1286                 ath_chanctx_send_ps_frame(sc, false);
1287
1288         ath_offchannel_channel_change(sc);
1289         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1290 }
1291
1292 static void ath_chanctx_work(struct work_struct *work)
1293 {
1294         struct ath_softc *sc = container_of(work, struct ath_softc,
1295                                             chanctx_work);
1296         mutex_lock(&sc->mutex);
1297         ath_chanctx_set_next(sc, false);
1298         mutex_unlock(&sc->mutex);
1299 }
1300
1301 void ath9k_offchannel_init(struct ath_softc *sc)
1302 {
1303         struct ath_chanctx *ctx;
1304         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1305         struct ieee80211_supported_band *sband;
1306         struct ieee80211_channel *chan;
1307         int i;
1308
1309         sband = &common->sbands[IEEE80211_BAND_2GHZ];
1310         if (!sband->n_channels)
1311                 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1312
1313         chan = &sband->channels[0];
1314
1315         ctx = &sc->offchannel.chan;
1316         INIT_LIST_HEAD(&ctx->vifs);
1317         ctx->txpower = ATH_TXPOWER_MAX;
1318         cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1319
1320         for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1321                 INIT_LIST_HEAD(&ctx->acq[i]);
1322
1323         sc->offchannel.chan.offchannel = true;
1324 }
1325
1326 void ath9k_init_channel_context(struct ath_softc *sc)
1327 {
1328         INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1329
1330         setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1331                     (unsigned long)sc);
1332         setup_timer(&sc->sched.timer, ath_chanctx_timer,
1333                     (unsigned long)sc);
1334
1335         init_completion(&sc->go_beacon);
1336 }
1337
1338 void ath9k_deinit_channel_context(struct ath_softc *sc)
1339 {
1340         cancel_work_sync(&sc->chanctx_work);
1341 }
1342
1343 bool ath9k_is_chanctx_enabled(void)
1344 {
1345         return (ath9k_use_chanctx == 1);
1346 }
1347
1348 /********************/
1349 /* Queue management */
1350 /********************/
1351
1352 void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1353 {
1354         struct ath_hw *ah = sc->sc_ah;
1355         int i;
1356
1357         if (ctx == &sc->offchannel.chan) {
1358                 ieee80211_stop_queue(sc->hw,
1359                                      sc->hw->offchannel_tx_hw_queue);
1360         } else {
1361                 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1362                         ieee80211_stop_queue(sc->hw,
1363                                              ctx->hw_queue_base + i);
1364         }
1365
1366         if (ah->opmode == NL80211_IFTYPE_AP)
1367                 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1368 }
1369
1370
1371 void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1372 {
1373         struct ath_hw *ah = sc->sc_ah;
1374         int i;
1375
1376         if (ctx == &sc->offchannel.chan) {
1377                 ieee80211_wake_queue(sc->hw,
1378                                      sc->hw->offchannel_tx_hw_queue);
1379         } else {
1380                 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1381                         ieee80211_wake_queue(sc->hw,
1382                                              ctx->hw_queue_base + i);
1383         }
1384
1385         if (ah->opmode == NL80211_IFTYPE_AP)
1386                 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1387 }
1388
1389 /*****************/
1390 /* P2P Powersave */
1391 /*****************/
1392
1393 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1394 {
1395         struct ath_hw *ah = sc->sc_ah;
1396         s32 tsf, target_tsf;
1397
1398         if (!avp || !avp->noa.has_next_tsf)
1399                 return;
1400
1401         ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1402
1403         tsf = ath9k_hw_gettsf32(sc->sc_ah);
1404
1405         target_tsf = avp->noa.next_tsf;
1406         if (!avp->noa.absent)
1407                 target_tsf -= ATH_P2P_PS_STOP_TIME;
1408
1409         if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1410                 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1411
1412         ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1413 }
1414
1415 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1416 {
1417         struct ath_vif *avp = (void *)vif->drv_priv;
1418         u32 tsf;
1419
1420         if (!sc->p2p_ps_timer)
1421                 return;
1422
1423         if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1424                 return;
1425
1426         sc->p2p_ps_vif = avp;
1427         tsf = ath9k_hw_gettsf32(sc->sc_ah);
1428         ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1429         ath9k_update_p2p_ps_timer(sc, avp);
1430 }
1431
1432 static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1433 {
1434         struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1435         u8 switch_time, ctwin;
1436
1437         /*
1438          * Channel switch in multi-channel mode is deferred
1439          * by a quarter beacon interval when handling
1440          * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1441          * interface is guaranteed to be discoverable
1442          * for that duration after a TBTT.
1443          */
1444         switch_time = cur_conf->beacon_interval / 4;
1445
1446         ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1447         if (ctwin && (ctwin < switch_time))
1448                 return ctwin;
1449
1450         if (switch_time < P2P_DEFAULT_CTWIN)
1451                 return 0;
1452
1453         return P2P_DEFAULT_CTWIN;
1454 }
1455
1456 void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1457                           struct sk_buff *skb)
1458 {
1459         static const u8 noa_ie_hdr[] = {
1460                 WLAN_EID_VENDOR_SPECIFIC,       /* type */
1461                 0,                              /* length */
1462                 0x50, 0x6f, 0x9a,               /* WFA OUI */
1463                 0x09,                           /* P2P subtype */
1464                 0x0c,                           /* Notice of Absence */
1465                 0x00,                           /* LSB of little-endian len */
1466                 0x00,                           /* MSB of little-endian len */
1467         };
1468
1469         struct ieee80211_p2p_noa_attr *noa;
1470         int noa_len, noa_desc, i = 0;
1471         u8 *hdr;
1472
1473         if (!avp->offchannel_duration && !avp->noa_duration)
1474                 return;
1475
1476         noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
1477         noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1478
1479         hdr = skb_put(skb, sizeof(noa_ie_hdr));
1480         memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1481         hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1482         hdr[7] = noa_len;
1483
1484         noa = (void *) skb_put(skb, noa_len);
1485         memset(noa, 0, noa_len);
1486
1487         noa->index = avp->noa_index;
1488         noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1489
1490         if (avp->noa_duration) {
1491                 if (avp->periodic_noa) {
1492                         u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1493                         noa->desc[i].count = 255;
1494                         noa->desc[i].interval = cpu_to_le32(interval);
1495                 } else {
1496                         noa->desc[i].count = 1;
1497                 }
1498
1499                 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1500                 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
1501                 i++;
1502         }
1503
1504         if (avp->offchannel_duration) {
1505                 noa->desc[i].count = 1;
1506                 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1507                 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1508         }
1509 }
1510
1511 void ath9k_p2p_ps_timer(void *priv)
1512 {
1513         struct ath_softc *sc = priv;
1514         struct ath_vif *avp = sc->p2p_ps_vif;
1515         struct ieee80211_vif *vif;
1516         struct ieee80211_sta *sta;
1517         struct ath_node *an;
1518         u32 tsf;
1519
1520         del_timer_sync(&sc->sched.timer);
1521         ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1522         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1523
1524         if (!avp || avp->chanctx != sc->cur_chan)
1525                 return;
1526
1527         tsf = ath9k_hw_gettsf32(sc->sc_ah);
1528         if (!avp->noa.absent)
1529                 tsf += ATH_P2P_PS_STOP_TIME;
1530
1531         if (!avp->noa.has_next_tsf ||
1532             avp->noa.next_tsf - tsf > BIT(31))
1533                 ieee80211_update_p2p_noa(&avp->noa, tsf);
1534
1535         ath9k_update_p2p_ps_timer(sc, avp);
1536
1537         rcu_read_lock();
1538
1539         vif = avp->vif;
1540         sta = ieee80211_find_sta(vif, avp->bssid);
1541         if (!sta)
1542                 goto out;
1543
1544         an = (void *) sta->drv_priv;
1545         if (an->sleeping == !!avp->noa.absent)
1546                 goto out;
1547
1548         an->sleeping = avp->noa.absent;
1549         if (an->sleeping)
1550                 ath_tx_aggr_sleep(sta, sc, an);
1551         else
1552                 ath_tx_aggr_wakeup(sc, an);
1553
1554 out:
1555         rcu_read_unlock();
1556 }
1557
1558 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1559                                 struct ieee80211_vif *vif)
1560 {
1561         unsigned long flags;
1562
1563         spin_lock_bh(&sc->sc_pcu_lock);
1564         spin_lock_irqsave(&sc->sc_pm_lock, flags);
1565         if (!(sc->ps_flags & PS_BEACON_SYNC))
1566                 ath9k_update_p2p_ps(sc, vif);
1567         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1568         spin_unlock_bh(&sc->sc_pcu_lock);
1569 }
1570
1571 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1572 {
1573         if (sc->p2p_ps_vif)
1574                 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1575 }
1576
1577 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1578                           struct ieee80211_vif *vif)
1579 {
1580         struct ath_vif *avp = (void *)vif->drv_priv;
1581
1582         spin_lock_bh(&sc->sc_pcu_lock);
1583         if (avp == sc->p2p_ps_vif) {
1584                 sc->p2p_ps_vif = NULL;
1585                 ath9k_update_p2p_ps_timer(sc, NULL);
1586         }
1587         spin_unlock_bh(&sc->sc_pcu_lock);
1588 }
1589
1590 int ath9k_init_p2p(struct ath_softc *sc)
1591 {
1592         sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1593                                                NULL, sc, AR_FIRST_NDP_TIMER);
1594         if (!sc->p2p_ps_timer)
1595                 return -ENOMEM;
1596
1597         return 0;
1598 }
1599
1600 void ath9k_deinit_p2p(struct ath_softc *sc)
1601 {
1602         if (sc->p2p_ps_timer)
1603                 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1604 }
1605
1606 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */