ath9k: Isolate ath9k_use_chanctx module parameter
[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 static const char *offchannel_state_string(enum ath_offchannel_state state)
20 {
21 #define case_rtn_string(val) case val: return #val
22
23         switch (state) {
24                 case_rtn_string(ATH_OFFCHANNEL_IDLE);
25                 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
26                 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
27                 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
28                 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
29                 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
30                 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
31         default:
32                 return "unknown";
33         }
34 }
35
36 /* Set/change channels.  If the channel is really being changed, it's done
37  * by reseting the chip.  To accomplish this we must first cleanup any pending
38  * DMA, then restart stuff.
39  */
40 static int ath_set_channel(struct ath_softc *sc)
41 {
42         struct ath_hw *ah = sc->sc_ah;
43         struct ath_common *common = ath9k_hw_common(ah);
44         struct ieee80211_hw *hw = sc->hw;
45         struct ath9k_channel *hchan;
46         struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
47         struct ieee80211_channel *chan = chandef->chan;
48         int pos = chan->hw_value;
49         int old_pos = -1;
50         int r;
51
52         if (test_bit(ATH_OP_INVALID, &common->op_flags))
53                 return -EIO;
54
55         if (ah->curchan)
56                 old_pos = ah->curchan - &ah->channels[0];
57
58         ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
59                 chan->center_freq, chandef->width);
60
61         /* update survey stats for the old channel before switching */
62         spin_lock_bh(&common->cc_lock);
63         ath_update_survey_stats(sc);
64         spin_unlock_bh(&common->cc_lock);
65
66         ath9k_cmn_get_channel(hw, ah, chandef);
67
68         /* If the operating channel changes, change the survey in-use flags
69          * along with it.
70          * Reset the survey data for the new channel, unless we're switching
71          * back to the operating channel from an off-channel operation.
72          */
73         if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
74                 if (sc->cur_survey)
75                         sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
76
77                 sc->cur_survey = &sc->survey[pos];
78
79                 memset(sc->cur_survey, 0, sizeof(struct survey_info));
80                 sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
81         } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
82                 memset(&sc->survey[pos], 0, sizeof(struct survey_info));
83         }
84
85         hchan = &sc->sc_ah->channels[pos];
86         r = ath_reset_internal(sc, hchan);
87         if (r)
88                 return r;
89
90         /* The most recent snapshot of channel->noisefloor for the old
91          * channel is only available after the hardware reset. Copy it to
92          * the survey stats now.
93          */
94         if (old_pos >= 0)
95                 ath_update_survey_nf(sc, old_pos);
96
97         /* Enable radar pulse detection if on a DFS channel. Spectral
98          * scanning and radar detection can not be used concurrently.
99          */
100         if (hw->conf.radar_enabled) {
101                 u32 rxfilter;
102
103                 /* set HW specific DFS configuration */
104                 ath9k_hw_set_radar_params(ah);
105                 rxfilter = ath9k_hw_getrxfilter(ah);
106                 rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
107                                 ATH9K_RX_FILTER_PHYERR;
108                 ath9k_hw_setrxfilter(ah, rxfilter);
109                 ath_dbg(common, DFS, "DFS enabled at freq %d\n",
110                         chan->center_freq);
111         } else {
112                 /* perform spectral scan if requested. */
113                 if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
114                         sc->spectral_mode == SPECTRAL_CHANSCAN)
115                         ath9k_spectral_scan_trigger(hw);
116         }
117
118         return 0;
119 }
120
121 static bool
122 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
123                               bool powersave)
124 {
125         struct ieee80211_vif *vif = avp->vif;
126         struct ieee80211_sta *sta = NULL;
127         struct ieee80211_hdr_3addr *nullfunc;
128         struct ath_tx_control txctl;
129         struct sk_buff *skb;
130         int band = sc->cur_chan->chandef.chan->band;
131
132         switch (vif->type) {
133         case NL80211_IFTYPE_STATION:
134                 if (!vif->bss_conf.assoc)
135                         return false;
136
137                 skb = ieee80211_nullfunc_get(sc->hw, vif);
138                 if (!skb)
139                         return false;
140
141                 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
142                 if (powersave)
143                         nullfunc->frame_control |=
144                                 cpu_to_le16(IEEE80211_FCTL_PM);
145
146                 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
147                 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
148                         dev_kfree_skb_any(skb);
149                         return false;
150                 }
151                 break;
152         default:
153                 return false;
154         }
155
156         memset(&txctl, 0, sizeof(txctl));
157         txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
158         txctl.sta = sta;
159         txctl.force_channel = true;
160         if (ath_tx_start(sc->hw, skb, &txctl)) {
161                 ieee80211_free_txskb(sc->hw, skb);
162                 return false;
163         }
164
165         return true;
166 }
167
168 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
169 {
170         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
171         struct ath_vif *avp;
172         bool active = false;
173         u8 n_active = 0;
174
175         if (!ctx)
176                 return;
177
178         list_for_each_entry(avp, &ctx->vifs, list) {
179                 struct ieee80211_vif *vif = avp->vif;
180
181                 switch (vif->type) {
182                 case NL80211_IFTYPE_P2P_CLIENT:
183                 case NL80211_IFTYPE_STATION:
184                         if (vif->bss_conf.assoc)
185                                 active = true;
186                         break;
187                 default:
188                         active = true;
189                         break;
190                 }
191         }
192         ctx->active = active;
193
194         ath_for_each_chanctx(sc, ctx) {
195                 if (!ctx->assigned || list_empty(&ctx->vifs))
196                         continue;
197                 n_active++;
198         }
199
200         if (n_active <= 1) {
201                 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
202                 return;
203         }
204         if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
205                 return;
206         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
207 }
208
209 static bool
210 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
211 {
212         struct ath_vif *avp;
213         bool sent = false;
214
215         rcu_read_lock();
216         list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
217                 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
218                         sent = true;
219         }
220         rcu_read_unlock();
221
222         return sent;
223 }
224
225 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
226 {
227         if (sc->cur_chan == &sc->offchannel.chan)
228                 return false;
229
230         switch (sc->sched.state) {
231         case ATH_CHANCTX_STATE_SWITCH:
232                 return false;
233         case ATH_CHANCTX_STATE_IDLE:
234                 if (!sc->cur_chan->switch_after_beacon)
235                         return false;
236
237                 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
238                 break;
239         default:
240                 break;
241         }
242
243         return true;
244 }
245
246 static void ath_chanctx_set_next(struct ath_softc *sc, bool force)
247 {
248         struct timespec ts;
249         bool measure_time = false;
250         bool send_ps = false;
251
252         spin_lock_bh(&sc->chan_lock);
253         if (!sc->next_chan) {
254                 spin_unlock_bh(&sc->chan_lock);
255                 return;
256         }
257
258         if (!force && ath_chanctx_defer_switch(sc)) {
259                 spin_unlock_bh(&sc->chan_lock);
260                 return;
261         }
262
263         if (sc->cur_chan != sc->next_chan) {
264                 sc->cur_chan->stopped = true;
265                 spin_unlock_bh(&sc->chan_lock);
266
267                 if (sc->next_chan == &sc->offchannel.chan) {
268                         getrawmonotonic(&ts);
269                         measure_time = true;
270                 }
271                 __ath9k_flush(sc->hw, ~0, true);
272
273                 if (ath_chanctx_send_ps_frame(sc, true))
274                         __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
275
276                 send_ps = true;
277                 spin_lock_bh(&sc->chan_lock);
278
279                 if (sc->cur_chan != &sc->offchannel.chan) {
280                         getrawmonotonic(&sc->cur_chan->tsf_ts);
281                         sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
282                 }
283         }
284         sc->cur_chan = sc->next_chan;
285         sc->cur_chan->stopped = false;
286         sc->next_chan = NULL;
287         sc->sched.offchannel_duration = 0;
288         if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
289                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
290
291         spin_unlock_bh(&sc->chan_lock);
292
293         if (sc->sc_ah->chip_fullsleep ||
294             memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
295                    sizeof(sc->cur_chandef))) {
296                 ath_set_channel(sc);
297                 if (measure_time)
298                         sc->sched.channel_switch_time =
299                                 ath9k_hw_get_tsf_offset(&ts, NULL);
300         }
301         if (send_ps)
302                 ath_chanctx_send_ps_frame(sc, false);
303
304         ath_offchannel_channel_change(sc);
305         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
306 }
307
308 void ath_chanctx_work(struct work_struct *work)
309 {
310         struct ath_softc *sc = container_of(work, struct ath_softc,
311                                             chanctx_work);
312         mutex_lock(&sc->mutex);
313         ath_chanctx_set_next(sc, false);
314         mutex_unlock(&sc->mutex);
315 }
316
317 void ath_chanctx_init(struct ath_softc *sc)
318 {
319         struct ath_chanctx *ctx;
320         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
321         struct ieee80211_supported_band *sband;
322         struct ieee80211_channel *chan;
323         int i, j;
324
325         sband = &common->sbands[IEEE80211_BAND_2GHZ];
326         if (!sband->n_channels)
327                 sband = &common->sbands[IEEE80211_BAND_5GHZ];
328
329         chan = &sband->channels[0];
330         for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
331                 ctx = &sc->chanctx[i];
332                 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
333                 INIT_LIST_HEAD(&ctx->vifs);
334                 ctx->txpower = ATH_TXPOWER_MAX;
335                 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
336                         INIT_LIST_HEAD(&ctx->acq[j]);
337         }
338         ctx = &sc->offchannel.chan;
339         cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
340         INIT_LIST_HEAD(&ctx->vifs);
341         ctx->txpower = ATH_TXPOWER_MAX;
342         for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
343                 INIT_LIST_HEAD(&ctx->acq[j]);
344         sc->offchannel.chan.offchannel = true;
345
346 }
347
348 void ath9k_chanctx_force_active(struct ieee80211_hw *hw,
349                                 struct ieee80211_vif *vif)
350 {
351         struct ath_softc *sc = hw->priv;
352         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
353         struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
354         bool changed = false;
355
356         if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
357                 return;
358
359         if (!avp->chanctx)
360                 return;
361
362         mutex_lock(&sc->mutex);
363
364         spin_lock_bh(&sc->chan_lock);
365         if (sc->next_chan || (sc->cur_chan != avp->chanctx)) {
366                 sc->next_chan = avp->chanctx;
367                 changed = true;
368         }
369         sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
370         spin_unlock_bh(&sc->chan_lock);
371
372         if (changed)
373                 ath_chanctx_set_next(sc, true);
374
375         mutex_unlock(&sc->mutex);
376 }
377
378 void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
379                         struct cfg80211_chan_def *chandef)
380 {
381         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
382
383         spin_lock_bh(&sc->chan_lock);
384
385         if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
386             (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
387                 sc->sched.offchannel_pending = true;
388                 spin_unlock_bh(&sc->chan_lock);
389                 return;
390         }
391
392         sc->next_chan = ctx;
393         if (chandef) {
394                 ctx->chandef = *chandef;
395                 ath_dbg(common, CHAN_CTX,
396                         "Assigned next_chan to %d MHz\n", chandef->center_freq1);
397         }
398
399         if (sc->next_chan == &sc->offchannel.chan) {
400                 sc->sched.offchannel_duration =
401                         TU_TO_USEC(sc->offchannel.duration) +
402                         sc->sched.channel_switch_time;
403
404                 if (chandef) {
405                         ath_dbg(common, CHAN_CTX,
406                                 "Offchannel duration for chan %d MHz : %u\n",
407                                 chandef->center_freq1,
408                                 sc->sched.offchannel_duration);
409                 }
410         }
411         spin_unlock_bh(&sc->chan_lock);
412         ieee80211_queue_work(sc->hw, &sc->chanctx_work);
413 }
414
415 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
416                              struct cfg80211_chan_def *chandef)
417 {
418         bool cur_chan;
419
420         spin_lock_bh(&sc->chan_lock);
421         if (chandef)
422                 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
423         cur_chan = sc->cur_chan == ctx;
424         spin_unlock_bh(&sc->chan_lock);
425
426         if (!cur_chan)
427                 return;
428
429         ath_set_channel(sc);
430 }
431
432 struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
433 {
434         struct ath_chanctx *ctx;
435
436         ath_for_each_chanctx(sc, ctx) {
437                 if (!ctx->assigned || list_empty(&ctx->vifs))
438                         continue;
439                 if (active && !ctx->active)
440                         continue;
441
442                 if (ctx->switch_after_beacon)
443                         return ctx;
444         }
445
446         return &sc->chanctx[0];
447 }
448
449 void ath_chanctx_offchan_switch(struct ath_softc *sc,
450                                 struct ieee80211_channel *chan)
451 {
452         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
453         struct cfg80211_chan_def chandef;
454
455         cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
456         ath_dbg(common, CHAN_CTX,
457                 "Channel definition created: %d MHz\n", chandef.center_freq1);
458
459         ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
460 }
461
462 static struct ath_chanctx *
463 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
464 {
465         int idx = ctx - &sc->chanctx[0];
466
467         return &sc->chanctx[!idx];
468 }
469
470 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
471 {
472         struct ath_chanctx *prev, *cur;
473         struct timespec ts;
474         u32 cur_tsf, prev_tsf, beacon_int;
475         s32 offset;
476
477         beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
478
479         cur = sc->cur_chan;
480         prev = ath_chanctx_get_next(sc, cur);
481
482         getrawmonotonic(&ts);
483         cur_tsf = (u32) cur->tsf_val +
484                   ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
485
486         prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
487         prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
488
489         /* Adjust the TSF time of the AP chanctx to keep its beacons
490          * at half beacon interval offset relative to the STA chanctx.
491          */
492         offset = cur_tsf - prev_tsf;
493
494         /* Ignore stale data or spurious timestamps */
495         if (offset < 0 || offset > 3 * beacon_int)
496                 return;
497
498         offset = beacon_int / 2 - (offset % beacon_int);
499         prev->tsf_val += offset;
500 }
501
502 void ath_chanctx_timer(unsigned long data)
503 {
504         struct ath_softc *sc = (struct ath_softc *) data;
505
506         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
507 }
508
509 /* Configure the TSF based hardware timer for a channel switch.
510  * Also set up backup software timer, in case the gen timer fails.
511  * This could be caused by a hardware reset.
512  */
513 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
514 {
515         struct ath_hw *ah = sc->sc_ah;
516
517 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
518         ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
519 #endif
520         tsf_time -= ath9k_hw_gettsf32(ah);
521         tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
522         mod_timer(&sc->sched.timer, tsf_time);
523 }
524
525 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
526                        enum ath_chanctx_event ev)
527 {
528         struct ath_hw *ah = sc->sc_ah;
529         struct ath_common *common = ath9k_hw_common(ah);
530         struct ath_beacon_config *cur_conf;
531         struct ath_vif *avp = NULL;
532         struct ath_chanctx *ctx;
533         u32 tsf_time;
534         u32 beacon_int;
535         bool noa_changed = false;
536
537         if (vif)
538                 avp = (struct ath_vif *) vif->drv_priv;
539
540         spin_lock_bh(&sc->chan_lock);
541
542         switch (ev) {
543         case ATH_CHANCTX_EVENT_BEACON_PREPARE:
544                 if (avp->offchannel_duration)
545                         avp->offchannel_duration = 0;
546
547                 if (avp->chanctx != sc->cur_chan)
548                         break;
549
550                 if (sc->sched.offchannel_pending) {
551                         sc->sched.offchannel_pending = false;
552                         sc->next_chan = &sc->offchannel.chan;
553                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
554                 }
555
556                 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
557                 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
558                         sc->next_chan = ctx;
559                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
560                 }
561
562                 /* if the timer missed its window, use the next interval */
563                 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
564                         sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
565
566                 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
567                         break;
568
569                 sc->sched.beacon_pending = true;
570                 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
571
572                 cur_conf = &sc->cur_chan->beacon;
573                 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
574
575                 /* defer channel switch by a quarter beacon interval */
576                 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
577                 sc->sched.switch_start_time = tsf_time;
578                 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
579
580                 /* Prevent wrap-around issues */
581                 if (avp->periodic_noa_duration &&
582                     tsf_time - avp->periodic_noa_start > BIT(30))
583                         avp->periodic_noa_duration = 0;
584
585                 if (ctx->active && !avp->periodic_noa_duration) {
586                         avp->periodic_noa_start = tsf_time;
587                         avp->periodic_noa_duration =
588                                 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
589                                 sc->sched.channel_switch_time;
590                         noa_changed = true;
591                 } else if (!ctx->active && avp->periodic_noa_duration) {
592                         avp->periodic_noa_duration = 0;
593                         noa_changed = true;
594                 }
595
596                 /* If at least two consecutive beacons were missed on the STA
597                  * chanctx, stay on the STA channel for one extra beacon period,
598                  * to resync the timer properly.
599                  */
600                 if (ctx->active && sc->sched.beacon_miss >= 2)
601                         sc->sched.offchannel_duration = 3 * beacon_int / 2;
602
603                 if (sc->sched.offchannel_duration) {
604                         noa_changed = true;
605                         avp->offchannel_start = tsf_time;
606                         avp->offchannel_duration =
607                                 sc->sched.offchannel_duration;
608                 }
609
610                 if (noa_changed)
611                         avp->noa_index++;
612                 break;
613         case ATH_CHANCTX_EVENT_BEACON_SENT:
614                 if (!sc->sched.beacon_pending)
615                         break;
616
617                 sc->sched.beacon_pending = false;
618                 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
619                         break;
620
621                 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
622                 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
623                 break;
624         case ATH_CHANCTX_EVENT_TSF_TIMER:
625                 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
626                         break;
627
628                 if (!sc->cur_chan->switch_after_beacon &&
629                     sc->sched.beacon_pending)
630                         sc->sched.beacon_miss++;
631
632                 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
633                 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
634                 break;
635         case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
636                 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
637                     sc->cur_chan == &sc->offchannel.chan)
638                         break;
639
640                 ath_chanctx_adjust_tbtt_delta(sc);
641                 sc->sched.beacon_pending = false;
642                 sc->sched.beacon_miss = 0;
643
644                 /* TSF time might have been updated by the incoming beacon,
645                  * need update the channel switch timer to reflect the change.
646                  */
647                 tsf_time = sc->sched.switch_start_time;
648                 tsf_time -= (u32) sc->cur_chan->tsf_val +
649                         ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
650                 tsf_time += ath9k_hw_gettsf32(ah);
651
652
653                 ath_chanctx_setup_timer(sc, tsf_time);
654                 break;
655         case ATH_CHANCTX_EVENT_ASSOC:
656                 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
657                     avp->chanctx != sc->cur_chan)
658                         break;
659
660                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
661                 /* fall through */
662         case ATH_CHANCTX_EVENT_SWITCH:
663                 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
664                     sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
665                     sc->cur_chan->switch_after_beacon ||
666                     sc->cur_chan == &sc->offchannel.chan)
667                         break;
668
669                 /* If this is a station chanctx, stay active for a half
670                  * beacon period (minus channel switch time)
671                  */
672                 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
673                 cur_conf = &sc->cur_chan->beacon;
674
675                 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
676
677                 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
678                 if (sc->sched.beacon_miss >= 2) {
679                         sc->sched.beacon_miss = 0;
680                         tsf_time *= 3;
681                 }
682
683                 tsf_time -= sc->sched.channel_switch_time;
684                 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
685                 sc->sched.switch_start_time = tsf_time;
686
687                 ath_chanctx_setup_timer(sc, tsf_time);
688                 sc->sched.beacon_pending = true;
689                 break;
690         case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
691                 if (sc->cur_chan == &sc->offchannel.chan ||
692                     sc->cur_chan->switch_after_beacon)
693                         break;
694
695                 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
696                 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
697                 break;
698         case ATH_CHANCTX_EVENT_UNASSIGN:
699                 if (sc->cur_chan->assigned) {
700                         if (sc->next_chan && !sc->next_chan->assigned &&
701                             sc->next_chan != &sc->offchannel.chan)
702                                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
703                         break;
704                 }
705
706                 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
707                 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
708                 if (!ctx->assigned)
709                         break;
710
711                 sc->next_chan = ctx;
712                 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
713                 break;
714         }
715
716         spin_unlock_bh(&sc->chan_lock);
717 }
718
719 static int ath_scan_channel_duration(struct ath_softc *sc,
720                                      struct ieee80211_channel *chan)
721 {
722         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
723
724         if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
725                 return (HZ / 9); /* ~110 ms */
726
727         return (HZ / 16); /* ~60 ms */
728 }
729
730 static void
731 ath_scan_next_channel(struct ath_softc *sc)
732 {
733         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
734         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
735         struct ieee80211_channel *chan;
736
737         if (sc->offchannel.scan_idx >= req->n_channels) {
738                 ath_dbg(common, CHAN_CTX,
739                         "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
740                         sc->offchannel.scan_idx,
741                         req->n_channels);
742
743                 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
744                 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
745                                    NULL);
746                 return;
747         }
748
749         ath_dbg(common, CHAN_CTX,
750                 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
751                 sc->offchannel.scan_idx);
752
753         chan = req->channels[sc->offchannel.scan_idx++];
754         sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
755         sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
756
757         ath_chanctx_offchan_switch(sc, chan);
758 }
759
760 void ath_offchannel_next(struct ath_softc *sc)
761 {
762         struct ieee80211_vif *vif;
763
764         if (sc->offchannel.scan_req) {
765                 vif = sc->offchannel.scan_vif;
766                 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
767                 ath_scan_next_channel(sc);
768         } else if (sc->offchannel.roc_vif) {
769                 vif = sc->offchannel.roc_vif;
770                 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
771                 sc->offchannel.duration = sc->offchannel.roc_duration;
772                 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
773                 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
774         } else {
775                 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
776                                    NULL);
777                 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
778                 if (sc->ps_idle)
779                         ath_cancel_work(sc);
780         }
781 }
782
783 void ath_roc_complete(struct ath_softc *sc, bool abort)
784 {
785         sc->offchannel.roc_vif = NULL;
786         sc->offchannel.roc_chan = NULL;
787         if (!abort)
788                 ieee80211_remain_on_channel_expired(sc->hw);
789         ath_offchannel_next(sc);
790         ath9k_ps_restore(sc);
791 }
792
793 void ath_scan_complete(struct ath_softc *sc, bool abort)
794 {
795         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
796
797         if (abort)
798                 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
799         else
800                 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
801
802         sc->offchannel.scan_req = NULL;
803         sc->offchannel.scan_vif = NULL;
804         sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
805         ieee80211_scan_completed(sc->hw, abort);
806         clear_bit(ATH_OP_SCANNING, &common->op_flags);
807         ath_offchannel_next(sc);
808         ath9k_ps_restore(sc);
809 }
810
811 static void ath_scan_send_probe(struct ath_softc *sc,
812                                 struct cfg80211_ssid *ssid)
813 {
814         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
815         struct ieee80211_vif *vif = sc->offchannel.scan_vif;
816         struct ath_tx_control txctl = {};
817         struct sk_buff *skb;
818         struct ieee80211_tx_info *info;
819         int band = sc->offchannel.chan.chandef.chan->band;
820
821         skb = ieee80211_probereq_get(sc->hw, vif,
822                         ssid->ssid, ssid->ssid_len, req->ie_len);
823         if (!skb)
824                 return;
825
826         info = IEEE80211_SKB_CB(skb);
827         if (req->no_cck)
828                 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
829
830         if (req->ie_len)
831                 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
832
833         skb_set_queue_mapping(skb, IEEE80211_AC_VO);
834
835         if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
836                 goto error;
837
838         txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
839         txctl.force_channel = true;
840         if (ath_tx_start(sc->hw, skb, &txctl))
841                 goto error;
842
843         return;
844
845 error:
846         ieee80211_free_txskb(sc->hw, skb);
847 }
848
849 static void ath_scan_channel_start(struct ath_softc *sc)
850 {
851         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
852         struct cfg80211_scan_request *req = sc->offchannel.scan_req;
853         int i;
854
855         if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
856             req->n_ssids) {
857                 for (i = 0; i < req->n_ssids; i++)
858                         ath_scan_send_probe(sc, &req->ssids[i]);
859
860         }
861
862         ath_dbg(common, CHAN_CTX,
863                 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
864
865         sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
866         mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
867 }
868
869 void ath_offchannel_channel_change(struct ath_softc *sc)
870 {
871         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
872
873         ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
874                 __func__, offchannel_state_string(sc->offchannel.state));
875
876         switch (sc->offchannel.state) {
877         case ATH_OFFCHANNEL_PROBE_SEND:
878                 if (!sc->offchannel.scan_req)
879                         return;
880
881                 if (sc->cur_chan->chandef.chan !=
882                     sc->offchannel.chan.chandef.chan)
883                         return;
884
885                 ath_scan_channel_start(sc);
886                 break;
887         case ATH_OFFCHANNEL_IDLE:
888                 if (!sc->offchannel.scan_req)
889                         return;
890
891                 ath_scan_complete(sc, false);
892                 break;
893         case ATH_OFFCHANNEL_ROC_START:
894                 if (sc->cur_chan != &sc->offchannel.chan)
895                         break;
896
897                 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
898                 mod_timer(&sc->offchannel.timer, jiffies +
899                           msecs_to_jiffies(sc->offchannel.duration));
900                 ieee80211_ready_on_channel(sc->hw);
901                 break;
902         case ATH_OFFCHANNEL_ROC_DONE:
903                 ath_roc_complete(sc, false);
904                 break;
905         default:
906                 break;
907         }
908 }
909
910 void ath_offchannel_timer(unsigned long data)
911 {
912         struct ath_softc *sc = (struct ath_softc *)data;
913         struct ath_chanctx *ctx;
914         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
915
916         ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
917                 __func__, offchannel_state_string(sc->offchannel.state));
918
919         switch (sc->offchannel.state) {
920         case ATH_OFFCHANNEL_PROBE_WAIT:
921                 if (!sc->offchannel.scan_req)
922                         return;
923
924                 /* get first active channel context */
925                 ctx = ath_chanctx_get_oper_chan(sc, true);
926                 if (ctx->active) {
927                         sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
928                         ath_chanctx_switch(sc, ctx, NULL);
929                         mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
930                         break;
931                 }
932                 /* fall through */
933         case ATH_OFFCHANNEL_SUSPEND:
934                 if (!sc->offchannel.scan_req)
935                         return;
936
937                 ath_scan_next_channel(sc);
938                 break;
939         case ATH_OFFCHANNEL_ROC_START:
940         case ATH_OFFCHANNEL_ROC_WAIT:
941                 ctx = ath_chanctx_get_oper_chan(sc, false);
942                 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
943                 ath_chanctx_switch(sc, ctx, NULL);
944                 break;
945         default:
946                 break;
947         }
948 }
949
950 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
951
952 bool ath9k_is_chanctx_enabled(void)
953 {
954         return (ath9k_use_chanctx == 1);
955 }
956
957 /*****************/
958 /* P2P Powersave */
959 /*****************/
960
961 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
962 {
963         struct ath_hw *ah = sc->sc_ah;
964         s32 tsf, target_tsf;
965
966         if (!avp || !avp->noa.has_next_tsf)
967                 return;
968
969         ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
970
971         tsf = ath9k_hw_gettsf32(sc->sc_ah);
972
973         target_tsf = avp->noa.next_tsf;
974         if (!avp->noa.absent)
975                 target_tsf -= ATH_P2P_PS_STOP_TIME;
976
977         if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
978                 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
979
980         ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
981 }
982
983 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
984 {
985         struct ath_vif *avp = (void *)vif->drv_priv;
986         u32 tsf;
987
988         if (!sc->p2p_ps_timer)
989                 return;
990
991         if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
992                 return;
993
994         sc->p2p_ps_vif = avp;
995         tsf = ath9k_hw_gettsf32(sc->sc_ah);
996         ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
997         ath9k_update_p2p_ps_timer(sc, avp);
998 }
999
1000 void ath9k_p2p_ps_timer(void *priv)
1001 {
1002         struct ath_softc *sc = priv;
1003         struct ath_vif *avp = sc->p2p_ps_vif;
1004         struct ieee80211_vif *vif;
1005         struct ieee80211_sta *sta;
1006         struct ath_node *an;
1007         u32 tsf;
1008
1009         del_timer_sync(&sc->sched.timer);
1010         ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1011         ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1012
1013         if (!avp || avp->chanctx != sc->cur_chan)
1014                 return;
1015
1016         tsf = ath9k_hw_gettsf32(sc->sc_ah);
1017         if (!avp->noa.absent)
1018                 tsf += ATH_P2P_PS_STOP_TIME;
1019
1020         if (!avp->noa.has_next_tsf ||
1021             avp->noa.next_tsf - tsf > BIT(31))
1022                 ieee80211_update_p2p_noa(&avp->noa, tsf);
1023
1024         ath9k_update_p2p_ps_timer(sc, avp);
1025
1026         rcu_read_lock();
1027
1028         vif = avp->vif;
1029         sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1030         if (!sta)
1031                 goto out;
1032
1033         an = (void *) sta->drv_priv;
1034         if (an->sleeping == !!avp->noa.absent)
1035                 goto out;
1036
1037         an->sleeping = avp->noa.absent;
1038         if (an->sleeping)
1039                 ath_tx_aggr_sleep(sta, sc, an);
1040         else
1041                 ath_tx_aggr_wakeup(sc, an);
1042
1043 out:
1044         rcu_read_unlock();
1045 }
1046
1047 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1048                                 struct ieee80211_vif *vif)
1049 {
1050         unsigned long flags;
1051
1052         spin_lock_bh(&sc->sc_pcu_lock);
1053         spin_lock_irqsave(&sc->sc_pm_lock, flags);
1054         if (!(sc->ps_flags & PS_BEACON_SYNC))
1055                 ath9k_update_p2p_ps(sc, vif);
1056         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1057         spin_unlock_bh(&sc->sc_pcu_lock);
1058 }
1059
1060 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1061 {
1062         if (sc->p2p_ps_vif)
1063                 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1064 }
1065
1066 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1067                           struct ieee80211_vif *vif)
1068 {
1069         struct ath_vif *avp = (void *)vif->drv_priv;
1070
1071         spin_lock_bh(&sc->sc_pcu_lock);
1072         if (avp == sc->p2p_ps_vif) {
1073                 sc->p2p_ps_vif = NULL;
1074                 ath9k_update_p2p_ps_timer(sc, NULL);
1075         }
1076         spin_unlock_bh(&sc->sc_pcu_lock);
1077 }
1078
1079 int ath9k_init_p2p(struct ath_softc *sc)
1080 {
1081         sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1082                                                NULL, sc, AR_FIRST_NDP_TIMER);
1083         if (!sc->p2p_ps_timer)
1084                 return -ENOMEM;
1085
1086         return 0;
1087 }
1088
1089 void ath9k_deinit_p2p(struct ath_softc *sc)
1090 {
1091         if (sc->p2p_ps_timer)
1092                 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1093 }
1094
1095 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */