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