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