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