networking: make skb_push & __skb_push return void pointers
[linux-2.6-block.git] / drivers / net / wireless / ath / ath9k / channel.c
CommitLineData
fbbcd146
FF
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 */
23static 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];
5555c955 69 r = ath_reset(sc, hchan);
fbbcd146
FF
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
fbbcd146
FF
86 rxfilter = ath9k_hw_getrxfilter(ah);
87 rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
88 ATH9K_RX_FILTER_PHYERR;
89 ath9k_hw_setrxfilter(ah, rxfilter);
90 ath_dbg(common, DFS, "DFS enabled at freq %d\n",
91 chan->center_freq);
92 } else {
93 /* perform spectral scan if requested. */
94 if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
8391f601 95 sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
67dc74f1 96 ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
fbbcd146
FF
97 }
98
99 return 0;
100}
101
102void ath_chanctx_init(struct ath_softc *sc)
103{
104 struct ath_chanctx *ctx;
105 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
106 struct ieee80211_supported_band *sband;
107 struct ieee80211_channel *chan;
0453531e 108 int i, j;
fbbcd146 109
57fbcce3 110 sband = &common->sbands[NL80211_BAND_2GHZ];
fbbcd146 111 if (!sband->n_channels)
57fbcce3 112 sband = &common->sbands[NL80211_BAND_5GHZ];
fbbcd146
FF
113
114 chan = &sband->channels[0];
115 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
116 ctx = &sc->chanctx[i];
117 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
118 INIT_LIST_HEAD(&ctx->vifs);
bc7e1be7 119 ctx->txpower = ATH_TXPOWER_MAX;
2fae0d9f 120 ctx->flush_timeout = HZ / 5; /* 200ms */
63fefa05
THJ
121 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) {
122 INIT_LIST_HEAD(&ctx->acq[j].acq_new);
123 INIT_LIST_HEAD(&ctx->acq[j].acq_old);
124 spin_lock_init(&ctx->acq[j].lock);
125 }
fbbcd146 126 }
fbbcd146
FF
127}
128
bff11766
FF
129void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
130 struct cfg80211_chan_def *chandef)
131{
4c7e9aee 132 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
bff11766
FF
133 bool cur_chan;
134
135 spin_lock_bh(&sc->chan_lock);
136 if (chandef)
137 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
138 cur_chan = sc->cur_chan == ctx;
139 spin_unlock_bh(&sc->chan_lock);
140
4c7e9aee
SM
141 if (!cur_chan) {
142 ath_dbg(common, CHAN_CTX,
143 "Current context differs from the new context\n");
bff11766 144 return;
4c7e9aee 145 }
bff11766
FF
146
147 ath_set_channel(sc);
fbbcd146 148}
78b21949 149
27babf9f
SM
150#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
151
26103b8d
SM
152/*************/
153/* Utilities */
154/*************/
155
156struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
157{
158 struct ath_chanctx *ctx;
159 struct ath_vif *avp;
160 struct ieee80211_vif *vif;
161
162 spin_lock_bh(&sc->chan_lock);
163
164 ath_for_each_chanctx(sc, ctx) {
165 if (!ctx->active)
166 continue;
167
168 list_for_each_entry(avp, &ctx->vifs, list) {
169 vif = avp->vif;
170
171 if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
172 spin_unlock_bh(&sc->chan_lock);
173 return ctx;
174 }
175 }
176 }
177
178 spin_unlock_bh(&sc->chan_lock);
179 return NULL;
180}
181
0e08b5fb
SM
182/**********************************************************/
183/* Functions to handle the channel context state machine. */
184/**********************************************************/
185
27babf9f
SM
186static const char *offchannel_state_string(enum ath_offchannel_state state)
187{
27babf9f
SM
188 switch (state) {
189 case_rtn_string(ATH_OFFCHANNEL_IDLE);
190 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
191 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
192 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
193 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
194 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
195 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
196 default:
197 return "unknown";
198 }
199}
200
5a8cbec7
SM
201static const char *chanctx_event_string(enum ath_chanctx_event ev)
202{
203 switch (ev) {
204 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
205 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
206 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
207 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
b8f9279b 208 case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
5a8cbec7
SM
209 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
210 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
211 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
212 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
213 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
214 default:
215 return "unknown";
216 }
217}
218
219static const char *chanctx_state_string(enum ath_chanctx_state state)
220{
221 switch (state) {
222 case_rtn_string(ATH_CHANCTX_STATE_IDLE);
223 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
224 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
225 case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
226 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
227 default:
228 return "unknown";
229 }
230}
231
344cd850 232static u32 chanctx_event_delta(struct ath_softc *sc)
02edab5b
JD
233{
234 u64 ms;
235 struct timespec ts, *old;
236
237 getrawmonotonic(&ts);
238 old = &sc->last_event_time;
239 ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
240 ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
241 sc->last_event_time = ts;
242
243 return (u32)ms;
244}
245
a09798f4
SM
246void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
247{
248 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
67259d51 249 struct ath_chanctx *ictx;
a09798f4
SM
250 struct ath_vif *avp;
251 bool active = false;
252 u8 n_active = 0;
253
254 if (!ctx)
255 return;
256
290c8a77
SM
257 if (ctx == &sc->offchannel.chan) {
258 spin_lock_bh(&sc->chan_lock);
259
260 if (likely(sc->sched.channel_switch_time))
261 ctx->flush_timeout =
262 usecs_to_jiffies(sc->sched.channel_switch_time);
263 else
264 ctx->flush_timeout =
265 msecs_to_jiffies(10);
266
267 spin_unlock_bh(&sc->chan_lock);
268
269 /*
270 * There is no need to iterate over the
271 * active/assigned channel contexts if
272 * the current context is offchannel.
273 */
274 return;
275 }
276
67259d51
SM
277 ictx = ctx;
278
a09798f4
SM
279 list_for_each_entry(avp, &ctx->vifs, list) {
280 struct ieee80211_vif *vif = avp->vif;
281
282 switch (vif->type) {
283 case NL80211_IFTYPE_P2P_CLIENT:
284 case NL80211_IFTYPE_STATION:
cb35582a 285 if (avp->assoc)
a09798f4
SM
286 active = true;
287 break;
288 default:
289 active = true;
290 break;
291 }
292 }
293 ctx->active = active;
294
295 ath_for_each_chanctx(sc, ctx) {
296 if (!ctx->assigned || list_empty(&ctx->vifs))
297 continue;
298 n_active++;
299 }
300
67259d51
SM
301 spin_lock_bh(&sc->chan_lock);
302
a09798f4 303 if (n_active <= 1) {
67259d51 304 ictx->flush_timeout = HZ / 5;
a09798f4 305 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
67259d51 306 spin_unlock_bh(&sc->chan_lock);
a09798f4
SM
307 return;
308 }
67259d51
SM
309
310 ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
311
312 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
313 spin_unlock_bh(&sc->chan_lock);
a09798f4 314 return;
67259d51
SM
315 }
316
317 spin_unlock_bh(&sc->chan_lock);
a09798f4
SM
318
319 if (ath9k_is_chanctx_enabled()) {
320 ath_chanctx_event(sc, NULL,
321 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
322 }
323}
324
58b57375
FF
325static struct ath_chanctx *
326ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
327{
328 int idx = ctx - &sc->chanctx[0];
329
330 return &sc->chanctx[!idx];
331}
332
333static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
334{
335 struct ath_chanctx *prev, *cur;
336 struct timespec ts;
337 u32 cur_tsf, prev_tsf, beacon_int;
338 s32 offset;
339
340 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
341
342 cur = sc->cur_chan;
343 prev = ath_chanctx_get_next(sc, cur);
344
7f30eac9
SM
345 if (!prev->switch_after_beacon)
346 return;
347
58b57375
FF
348 getrawmonotonic(&ts);
349 cur_tsf = (u32) cur->tsf_val +
350 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
351
352 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
353 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
354
355 /* Adjust the TSF time of the AP chanctx to keep its beacons
356 * at half beacon interval offset relative to the STA chanctx.
357 */
358 offset = cur_tsf - prev_tsf;
359
360 /* Ignore stale data or spurious timestamps */
361 if (offset < 0 || offset > 3 * beacon_int)
362 return;
363
364 offset = beacon_int / 2 - (offset % beacon_int);
365 prev->tsf_val += offset;
366}
367
42eda115
FF
368/* Configure the TSF based hardware timer for a channel switch.
369 * Also set up backup software timer, in case the gen timer fails.
370 * This could be caused by a hardware reset.
371 */
372static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
373{
878066e7 374 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
42eda115 375 struct ath_hw *ah = sc->sc_ah;
2f985539 376 unsigned long timeout;
42eda115
FF
377
378 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
379 tsf_time -= ath9k_hw_gettsf32(ah);
2f985539
JD
380 timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
381 mod_timer(&sc->sched.timer, jiffies + timeout);
878066e7
SM
382
383 ath_dbg(common, CHAN_CTX,
2f985539
JD
384 "Setup chanctx timer with timeout: %d (%d) ms\n",
385 tsf_time / 1000, jiffies_to_msecs(timeout));
42eda115
FF
386}
387
828fe01a
SM
388static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
389 struct ath_chanctx *ctx,
390 struct ath_vif *avp)
391{
392 /*
393 * Clear the extend_absence flag if it had been
394 * set during the previous beacon transmission,
395 * since we need to revert to the normal NoA
396 * schedule.
397 */
398 if (ctx->active && sc->sched.extend_absence) {
399 avp->noa_duration = 0;
400 sc->sched.extend_absence = false;
401 }
402
403 /* If at least two consecutive beacons were missed on the STA
404 * chanctx, stay on the STA channel for one extra beacon period,
405 * to resync the timer properly.
406 */
407 if (ctx->active && sc->sched.beacon_miss >= 2) {
408 avp->noa_duration = 0;
409 sc->sched.extend_absence = true;
410 }
411}
412
a23152a8
SM
413static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
414 struct ath_chanctx *ctx,
415 struct ath_vif *avp,
416 u32 tsf_time)
417{
418 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
419
420 avp->noa_index++;
421 avp->offchannel_start = tsf_time;
422 avp->offchannel_duration = sc->sched.offchannel_duration;
423
424 ath_dbg(common, CHAN_CTX,
58bb9ca8 425 "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
a23152a8
SM
426 avp->offchannel_duration,
427 avp->offchannel_start,
428 avp->noa_index);
429
430 /*
431 * When multiple contexts are active, the NoA
432 * has to be recalculated and advertised after
433 * an offchannel operation.
434 */
435 if (ctx->active && avp->noa_duration)
436 avp->noa_duration = 0;
437}
438
347a9566
SM
439static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
440 struct ath_vif *avp,
441 struct ath_beacon_config *cur_conf,
442 u32 tsf_time,
443 u32 beacon_int)
444{
445 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
446
447 avp->noa_index++;
448 avp->noa_start = tsf_time;
449
450 if (sc->sched.extend_absence)
451 avp->noa_duration = (3 * beacon_int / 2) +
452 sc->sched.channel_switch_time;
453 else
454 avp->noa_duration =
455 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
456 sc->sched.channel_switch_time;
457
458 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
459 sc->sched.extend_absence)
460 avp->periodic_noa = false;
461 else
462 avp->periodic_noa = true;
463
464 ath_dbg(common, CHAN_CTX,
58bb9ca8 465 "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
347a9566
SM
466 avp->noa_duration,
467 avp->noa_start,
468 avp->noa_index,
469 avp->periodic_noa);
470}
471
0a019a58
SM
472static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
473 struct ath_vif *avp,
474 u32 tsf_time,
475 u32 duration)
476{
477 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
478
479 avp->noa_index++;
480 avp->noa_start = tsf_time;
481 avp->periodic_noa = false;
482 avp->oneshot_noa = true;
483 avp->noa_duration = duration + sc->sched.channel_switch_time;
484
485 ath_dbg(common, CHAN_CTX,
58bb9ca8 486 "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
0a019a58
SM
487 avp->noa_duration,
488 avp->noa_start,
489 avp->noa_index,
490 avp->periodic_noa);
491}
492
748299f2
FF
493void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
494 enum ath_chanctx_event ev)
495{
496 struct ath_hw *ah = sc->sc_ah;
58b57375 497 struct ath_common *common = ath9k_hw_common(ah);
7414863e 498 struct ath_beacon_config *cur_conf;
3ae07d39 499 struct ath_vif *avp = NULL;
73fa2f26 500 struct ath_chanctx *ctx;
748299f2 501 u32 tsf_time;
ec70abe1 502 u32 beacon_int;
3ae07d39
FF
503
504 if (vif)
505 avp = (struct ath_vif *) vif->drv_priv;
748299f2
FF
506
507 spin_lock_bh(&sc->chan_lock);
508
02edab5b 509 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
878066e7
SM
510 sc->cur_chan->chandef.center_freq1,
511 chanctx_event_string(ev),
02edab5b
JD
512 chanctx_state_string(sc->sched.state),
513 chanctx_event_delta(sc));
878066e7 514
748299f2
FF
515 switch (ev) {
516 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
3ae07d39
FF
517 if (avp->offchannel_duration)
518 avp->offchannel_duration = 0;
519
0a019a58
SM
520 if (avp->oneshot_noa) {
521 avp->noa_duration = 0;
522 avp->oneshot_noa = false;
523
524 ath_dbg(common, CHAN_CTX,
525 "Clearing oneshot NoA\n");
526 }
527
878066e7
SM
528 if (avp->chanctx != sc->cur_chan) {
529 ath_dbg(common, CHAN_CTX,
530 "Contexts differ, not preparing beacon\n");
73fa2f26 531 break;
878066e7 532 }
73fa2f26 533
367b341e 534 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
73fa2f26
FF
535 sc->sched.offchannel_pending = false;
536 sc->next_chan = &sc->offchannel.chan;
537 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
878066e7
SM
538 ath_dbg(common, CHAN_CTX,
539 "Setting offchannel_pending to false\n");
73fa2f26
FF
540 }
541
542 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
543 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
544 sc->next_chan = ctx;
545 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
878066e7
SM
546 ath_dbg(common, CHAN_CTX,
547 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
73fa2f26
FF
548 }
549
550 /* if the timer missed its window, use the next interval */
878066e7 551 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
73fa2f26 552 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
878066e7
SM
553 ath_dbg(common, CHAN_CTX,
554 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
555 }
73fa2f26 556
c6500ea2
SM
557 if (sc->sched.mgd_prepare_tx)
558 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
559
8890d05f
SM
560 /*
561 * When a context becomes inactive, for example,
562 * disassociation of a station context, the NoA
563 * attribute needs to be removed from subsequent
564 * beacons.
565 */
566 if (!ctx->active && avp->noa_duration &&
567 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
568 avp->noa_duration = 0;
569 avp->periodic_noa = false;
570
571 ath_dbg(common, CHAN_CTX,
572 "Clearing NoA schedule\n");
573 }
574
748299f2
FF
575 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
576 break;
577
878066e7
SM
578 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
579
748299f2
FF
580 sc->sched.beacon_pending = true;
581 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
3ae07d39 582
7414863e 583 cur_conf = &sc->cur_chan->beacon;
ec70abe1
FF
584 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
585
3ae07d39 586 /* defer channel switch by a quarter beacon interval */
ec70abe1 587 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
3ae07d39 588 sc->sched.switch_start_time = tsf_time;
58b57375 589 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
3ae07d39 590
d0975edd
SM
591 /*
592 * If an offchannel switch is scheduled to happen after
593 * a beacon transmission, update the NoA with one-shot
594 * values and increment the index.
595 */
596 if (sc->next_chan == &sc->offchannel.chan) {
a23152a8 597 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
d0975edd 598 break;
3ae07d39
FF
599 }
600
828fe01a 601 ath_chanctx_handle_bmiss(sc, ctx, avp);
a2b28601 602
0a019a58
SM
603 /*
604 * If a mgd_prepare_tx() has been called by mac80211,
605 * a one-shot NoA needs to be sent. This can happen
606 * with one or more active channel contexts - in both
607 * cases, a new NoA schedule has to be advertised.
608 */
609 if (sc->sched.mgd_prepare_tx) {
610 ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
611 jiffies_to_usecs(HZ / 5));
612 break;
613 }
614
d0975edd
SM
615 /* Prevent wrap-around issues */
616 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
617 avp->noa_duration = 0;
618
619 /*
620 * If multiple contexts are active, start periodic
621 * NoA and increment the index for the first
622 * announcement.
623 */
624 if (ctx->active &&
347a9566
SM
625 (!avp->noa_duration || sc->sched.force_noa_update))
626 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
627 tsf_time, beacon_int);
d0975edd
SM
628
629 if (ctx->active && sc->sched.force_noa_update)
630 sc->sched.force_noa_update = false;
878066e7 631
748299f2
FF
632 break;
633 case ATH_CHANCTX_EVENT_BEACON_SENT:
878066e7
SM
634 if (!sc->sched.beacon_pending) {
635 ath_dbg(common, CHAN_CTX,
636 "No pending beacon\n");
748299f2 637 break;
878066e7 638 }
748299f2
FF
639
640 sc->sched.beacon_pending = false;
c6500ea2
SM
641
642 if (sc->sched.mgd_prepare_tx) {
643 sc->sched.mgd_prepare_tx = false;
644 complete(&sc->go_beacon);
645 ath_dbg(common, CHAN_CTX,
646 "Beacon sent, complete go_beacon\n");
647 break;
648 }
649
748299f2
FF
650 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
651 break;
652
878066e7
SM
653 ath_dbg(common, CHAN_CTX,
654 "Move chanctx state to WAIT_FOR_TIMER\n");
655
748299f2 656 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
42eda115 657 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
748299f2
FF
658 break;
659 case ATH_CHANCTX_EVENT_TSF_TIMER:
660 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
661 break;
662
ec70abe1
FF
663 if (!sc->cur_chan->switch_after_beacon &&
664 sc->sched.beacon_pending)
665 sc->sched.beacon_miss++;
666
878066e7
SM
667 ath_dbg(common, CHAN_CTX,
668 "Move chanctx state to SWITCH\n");
669
748299f2
FF
670 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
671 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
672 break;
58b57375 673 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
73fa2f26
FF
674 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
675 sc->cur_chan == &sc->offchannel.chan)
58b57375
FF
676 break;
677
ec70abe1
FF
678 sc->sched.beacon_pending = false;
679 sc->sched.beacon_miss = 0;
a899b678 680
be247c1f 681 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
d9092c98 682 !sc->sched.beacon_adjust ||
be247c1f
SM
683 !sc->cur_chan->tsf_val)
684 break;
685
686 ath_chanctx_adjust_tbtt_delta(sc);
687
a899b678
FF
688 /* TSF time might have been updated by the incoming beacon,
689 * need update the channel switch timer to reflect the change.
690 */
691 tsf_time = sc->sched.switch_start_time;
692 tsf_time -= (u32) sc->cur_chan->tsf_val +
693 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
694 tsf_time += ath9k_hw_gettsf32(ah);
695
d9092c98 696 sc->sched.beacon_adjust = false;
42eda115 697 ath_chanctx_setup_timer(sc, tsf_time);
58b57375 698 break;
b8f9279b 699 case ATH_CHANCTX_EVENT_AUTHORIZED:
73fa2f26
FF
700 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
701 avp->chanctx != sc->cur_chan)
702 break;
703
878066e7
SM
704 ath_dbg(common, CHAN_CTX,
705 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
706
73fa2f26
FF
707 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
708 /* fall through */
709 case ATH_CHANCTX_EVENT_SWITCH:
710 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
711 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
712 sc->cur_chan->switch_after_beacon ||
713 sc->cur_chan == &sc->offchannel.chan)
714 break;
715
716 /* If this is a station chanctx, stay active for a half
717 * beacon period (minus channel switch time)
718 */
719 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
7414863e 720 cur_conf = &sc->cur_chan->beacon;
73fa2f26 721
878066e7
SM
722 ath_dbg(common, CHAN_CTX,
723 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
724
73fa2f26 725 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
367b341e 726 sc->sched.wait_switch = false;
ec70abe1
FF
727
728 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
167bf96d
SM
729
730 if (sc->sched.extend_absence) {
ec70abe1
FF
731 sc->sched.beacon_miss = 0;
732 tsf_time *= 3;
733 }
734
73fa2f26 735 tsf_time -= sc->sched.channel_switch_time;
ec70abe1 736 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
73fa2f26
FF
737 sc->sched.switch_start_time = tsf_time;
738
42eda115 739 ath_chanctx_setup_timer(sc, tsf_time);
ec70abe1 740 sc->sched.beacon_pending = true;
d9092c98 741 sc->sched.beacon_adjust = true;
73fa2f26
FF
742 break;
743 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
744 if (sc->cur_chan == &sc->offchannel.chan ||
745 sc->cur_chan->switch_after_beacon)
746 break;
747
748 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
749 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
750 break;
751 case ATH_CHANCTX_EVENT_UNASSIGN:
752 if (sc->cur_chan->assigned) {
753 if (sc->next_chan && !sc->next_chan->assigned &&
754 sc->next_chan != &sc->offchannel.chan)
755 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
756 break;
757 }
758
759 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
760 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
761 if (!ctx->assigned)
762 break;
763
764 sc->next_chan = ctx;
765 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
766 break;
02da18b7
SM
767 case ATH_CHANCTX_EVENT_ASSIGN:
768 break;
769 case ATH_CHANCTX_EVENT_CHANGE:
770 break;
748299f2
FF
771 }
772
773 spin_unlock_bh(&sc->chan_lock);
774}
dfcbb3e8 775
70b06dac
SM
776void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
777 enum ath_chanctx_event ev)
778{
779 if (sc->sched.beacon_pending)
780 ath_chanctx_event(sc, NULL, ev);
781}
782
a2b28601 783void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
70b06dac
SM
784 enum ath_chanctx_event ev)
785{
70b06dac
SM
786 ath_chanctx_event(sc, NULL, ev);
787}
788
dfcbb3e8
SM
789static int ath_scan_channel_duration(struct ath_softc *sc,
790 struct ieee80211_channel *chan)
791{
792 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
793
794 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
795 return (HZ / 9); /* ~110 ms */
796
797 return (HZ / 16); /* ~60 ms */
798}
799
922c943d
SM
800static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
801 struct cfg80211_chan_def *chandef)
802{
803 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
804
805 spin_lock_bh(&sc->chan_lock);
806
807 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
808 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
da0162f3
SM
809 if (chandef)
810 ctx->chandef = *chandef;
cbc775db
SM
811
812 sc->sched.offchannel_pending = true;
813 sc->sched.wait_switch = true;
814 sc->sched.offchannel_duration =
815 jiffies_to_usecs(sc->offchannel.duration) +
816 sc->sched.channel_switch_time;
817
922c943d 818 spin_unlock_bh(&sc->chan_lock);
da0162f3
SM
819 ath_dbg(common, CHAN_CTX,
820 "Set offchannel_pending to true\n");
922c943d
SM
821 return;
822 }
823
824 sc->next_chan = ctx;
825 if (chandef) {
826 ctx->chandef = *chandef;
827 ath_dbg(common, CHAN_CTX,
828 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
829 }
830
831 if (sc->next_chan == &sc->offchannel.chan) {
832 sc->sched.offchannel_duration =
bb628eb9 833 jiffies_to_usecs(sc->offchannel.duration) +
922c943d
SM
834 sc->sched.channel_switch_time;
835
836 if (chandef) {
837 ath_dbg(common, CHAN_CTX,
838 "Offchannel duration for chan %d MHz : %u\n",
839 chandef->center_freq1,
840 sc->sched.offchannel_duration);
841 }
842 }
843 spin_unlock_bh(&sc->chan_lock);
844 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
845}
846
344ae6ab
SM
847static void ath_chanctx_offchan_switch(struct ath_softc *sc,
848 struct ieee80211_channel *chan)
849{
850 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
851 struct cfg80211_chan_def chandef;
852
853 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
854 ath_dbg(common, CHAN_CTX,
855 "Channel definition created: %d MHz\n", chandef.center_freq1);
856
857 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
858}
859
98f411b8
SM
860static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
861 bool active)
862{
863 struct ath_chanctx *ctx;
864
865 ath_for_each_chanctx(sc, ctx) {
866 if (!ctx->assigned || list_empty(&ctx->vifs))
867 continue;
868 if (active && !ctx->active)
869 continue;
870
871 if (ctx->switch_after_beacon)
872 return ctx;
873 }
874
875 return &sc->chanctx[0];
876}
877
dfcbb3e8
SM
878static void
879ath_scan_next_channel(struct ath_softc *sc)
880{
bc81d43a 881 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
dfcbb3e8
SM
882 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
883 struct ieee80211_channel *chan;
884
885 if (sc->offchannel.scan_idx >= req->n_channels) {
bc81d43a 886 ath_dbg(common, CHAN_CTX,
878066e7
SM
887 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
888 "scan_idx: %d, n_channels: %d\n",
bc81d43a
SM
889 sc->offchannel.scan_idx,
890 req->n_channels);
891
dfcbb3e8
SM
892 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
893 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
894 NULL);
895 return;
896 }
897
bc81d43a 898 ath_dbg(common, CHAN_CTX,
878066e7 899 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
bc81d43a
SM
900 sc->offchannel.scan_idx);
901
dfcbb3e8
SM
902 chan = req->channels[sc->offchannel.scan_idx++];
903 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
904 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
bc81d43a 905
dfcbb3e8
SM
906 ath_chanctx_offchan_switch(sc, chan);
907}
908
909void ath_offchannel_next(struct ath_softc *sc)
910{
911 struct ieee80211_vif *vif;
912
913 if (sc->offchannel.scan_req) {
914 vif = sc->offchannel.scan_vif;
915 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
916 ath_scan_next_channel(sc);
917 } else if (sc->offchannel.roc_vif) {
918 vif = sc->offchannel.roc_vif;
919 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
bb628eb9
SM
920 sc->offchannel.duration =
921 msecs_to_jiffies(sc->offchannel.roc_duration);
dfcbb3e8
SM
922 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
923 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
924 } else {
e21a1d8b
SM
925 spin_lock_bh(&sc->chan_lock);
926 sc->sched.offchannel_pending = false;
927 sc->sched.wait_switch = false;
928 spin_unlock_bh(&sc->chan_lock);
929
dfcbb3e8
SM
930 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
931 NULL);
932 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
933 if (sc->ps_idle)
934 ath_cancel_work(sc);
935 }
936}
937
d83520b7 938void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
dfcbb3e8 939{
4b60af4a
SM
940 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
941
d83520b7
JD
942 sc->offchannel.roc_vif = NULL;
943 sc->offchannel.roc_chan = NULL;
944
945 switch (reason) {
946 case ATH_ROC_COMPLETE_ABORT:
4b60af4a 947 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
d83520b7
JD
948 ieee80211_remain_on_channel_expired(sc->hw);
949 break;
950 case ATH_ROC_COMPLETE_EXPIRE:
4b60af4a 951 ath_dbg(common, CHAN_CTX, "RoC expired\n");
d83520b7
JD
952 ieee80211_remain_on_channel_expired(sc->hw);
953 break;
954 case ATH_ROC_COMPLETE_CANCEL:
955 ath_dbg(common, CHAN_CTX, "RoC canceled\n");
956 break;
957 }
4b60af4a 958
dfcbb3e8
SM
959 ath_offchannel_next(sc);
960 ath9k_ps_restore(sc);
961}
962
963void ath_scan_complete(struct ath_softc *sc, bool abort)
964{
965 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
7947d3e0
AS
966 struct cfg80211_scan_info info = {
967 .aborted = abort,
968 };
dfcbb3e8 969
bc81d43a
SM
970 if (abort)
971 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
972 else
973 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
974
dfcbb3e8
SM
975 sc->offchannel.scan_req = NULL;
976 sc->offchannel.scan_vif = NULL;
977 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
7947d3e0 978 ieee80211_scan_completed(sc->hw, &info);
dfcbb3e8 979 clear_bit(ATH_OP_SCANNING, &common->op_flags);
d0975edd
SM
980 spin_lock_bh(&sc->chan_lock);
981 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
982 sc->sched.force_noa_update = true;
983 spin_unlock_bh(&sc->chan_lock);
dfcbb3e8
SM
984 ath_offchannel_next(sc);
985 ath9k_ps_restore(sc);
986}
987
988static void ath_scan_send_probe(struct ath_softc *sc,
989 struct cfg80211_ssid *ssid)
990{
991 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
992 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
993 struct ath_tx_control txctl = {};
994 struct sk_buff *skb;
995 struct ieee80211_tx_info *info;
996 int band = sc->offchannel.chan.chandef.chan->band;
997
a344d677 998 skb = ieee80211_probereq_get(sc->hw, vif->addr,
dfcbb3e8
SM
999 ssid->ssid, ssid->ssid_len, req->ie_len);
1000 if (!skb)
1001 return;
1002
1003 info = IEEE80211_SKB_CB(skb);
1004 if (req->no_cck)
1005 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1006
1007 if (req->ie_len)
59ae1d12 1008 skb_put_data(skb, req->ie, req->ie_len);
dfcbb3e8
SM
1009
1010 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1011
1012 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1013 goto error;
1014
1015 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
dfcbb3e8
SM
1016 if (ath_tx_start(sc->hw, skb, &txctl))
1017 goto error;
1018
1019 return;
1020
1021error:
1022 ieee80211_free_txskb(sc->hw, skb);
1023}
1024
1025static void ath_scan_channel_start(struct ath_softc *sc)
1026{
bc81d43a 1027 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
dfcbb3e8
SM
1028 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1029 int i;
1030
1031 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1032 req->n_ssids) {
1033 for (i = 0; i < req->n_ssids; i++)
1034 ath_scan_send_probe(sc, &req->ssids[i]);
1035
1036 }
1037
bc81d43a 1038 ath_dbg(common, CHAN_CTX,
878066e7 1039 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
bc81d43a 1040
dfcbb3e8
SM
1041 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1042 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1043}
1044
705d0bf8
SM
1045static void ath_chanctx_timer(unsigned long data)
1046{
1047 struct ath_softc *sc = (struct ath_softc *) data;
878066e7
SM
1048 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1049
1050 ath_dbg(common, CHAN_CTX,
1051 "Channel context timer invoked\n");
705d0bf8
SM
1052
1053 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1054}
1055
1056static void ath_offchannel_timer(unsigned long data)
dfcbb3e8
SM
1057{
1058 struct ath_softc *sc = (struct ath_softc *)data;
1059 struct ath_chanctx *ctx;
bc81d43a
SM
1060 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1061
878066e7 1062 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
bc81d43a 1063 __func__, offchannel_state_string(sc->offchannel.state));
dfcbb3e8
SM
1064
1065 switch (sc->offchannel.state) {
1066 case ATH_OFFCHANNEL_PROBE_WAIT:
1067 if (!sc->offchannel.scan_req)
1068 return;
1069
1070 /* get first active channel context */
1071 ctx = ath_chanctx_get_oper_chan(sc, true);
1072 if (ctx->active) {
878066e7
SM
1073 ath_dbg(common, CHAN_CTX,
1074 "Switch to oper/active context, "
1075 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1076
dfcbb3e8
SM
1077 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1078 ath_chanctx_switch(sc, ctx, NULL);
1079 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1080 break;
1081 }
1082 /* fall through */
1083 case ATH_OFFCHANNEL_SUSPEND:
1084 if (!sc->offchannel.scan_req)
1085 return;
1086
1087 ath_scan_next_channel(sc);
1088 break;
1089 case ATH_OFFCHANNEL_ROC_START:
1090 case ATH_OFFCHANNEL_ROC_WAIT:
dfcbb3e8 1091 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
d83520b7 1092 ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
dfcbb3e8
SM
1093 break;
1094 default:
1095 break;
1096 }
1097}
2471adff 1098
6d7cbd77
SM
1099static bool
1100ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1101 bool powersave)
1102{
1103 struct ieee80211_vif *vif = avp->vif;
1104 struct ieee80211_sta *sta = NULL;
1105 struct ieee80211_hdr_3addr *nullfunc;
1106 struct ath_tx_control txctl;
1107 struct sk_buff *skb;
1108 int band = sc->cur_chan->chandef.chan->band;
1109
1110 switch (vif->type) {
1111 case NL80211_IFTYPE_STATION:
cb35582a 1112 if (!avp->assoc)
6d7cbd77
SM
1113 return false;
1114
1115 skb = ieee80211_nullfunc_get(sc->hw, vif);
1116 if (!skb)
1117 return false;
1118
1119 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1120 if (powersave)
1121 nullfunc->frame_control |=
1122 cpu_to_le16(IEEE80211_FCTL_PM);
1123
c1b7bea0 1124 skb->priority = 7;
6d7cbd77
SM
1125 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1126 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1127 dev_kfree_skb_any(skb);
1128 return false;
1129 }
1130 break;
1131 default:
1132 return false;
1133 }
1134
1135 memset(&txctl, 0, sizeof(txctl));
1136 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1137 txctl.sta = sta;
6d7cbd77
SM
1138 if (ath_tx_start(sc->hw, skb, &txctl)) {
1139 ieee80211_free_txskb(sc->hw, skb);
1140 return false;
1141 }
1142
1143 return true;
1144}
1145
1146static bool
1147ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1148{
1149 struct ath_vif *avp;
1150 bool sent = false;
1151
1152 rcu_read_lock();
1153 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1154 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1155 sent = true;
1156 }
1157 rcu_read_unlock();
1158
1159 return sent;
1160}
1161
1162static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1163{
878066e7
SM
1164 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1165
6d7cbd77
SM
1166 if (sc->cur_chan == &sc->offchannel.chan)
1167 return false;
1168
1169 switch (sc->sched.state) {
1170 case ATH_CHANCTX_STATE_SWITCH:
1171 return false;
1172 case ATH_CHANCTX_STATE_IDLE:
1173 if (!sc->cur_chan->switch_after_beacon)
1174 return false;
1175
878066e7
SM
1176 ath_dbg(common, CHAN_CTX,
1177 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1178
6d7cbd77
SM
1179 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1180 break;
1181 default:
1182 break;
1183 }
1184
1185 return true;
1186}
1187
55254eea
SM
1188static void ath_offchannel_channel_change(struct ath_softc *sc)
1189{
1190 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1191
878066e7 1192 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
55254eea
SM
1193 __func__, offchannel_state_string(sc->offchannel.state));
1194
1195 switch (sc->offchannel.state) {
1196 case ATH_OFFCHANNEL_PROBE_SEND:
1197 if (!sc->offchannel.scan_req)
1198 return;
1199
1200 if (sc->cur_chan->chandef.chan !=
1201 sc->offchannel.chan.chandef.chan)
1202 return;
1203
1204 ath_scan_channel_start(sc);
1205 break;
1206 case ATH_OFFCHANNEL_IDLE:
1207 if (!sc->offchannel.scan_req)
1208 return;
1209
1210 ath_scan_complete(sc, false);
1211 break;
1212 case ATH_OFFCHANNEL_ROC_START:
1213 if (sc->cur_chan != &sc->offchannel.chan)
1214 break;
1215
1216 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
bb628eb9
SM
1217 mod_timer(&sc->offchannel.timer,
1218 jiffies + sc->offchannel.duration);
55254eea
SM
1219 ieee80211_ready_on_channel(sc->hw);
1220 break;
1221 case ATH_OFFCHANNEL_ROC_DONE:
55254eea
SM
1222 break;
1223 default:
1224 break;
1225 }
1226}
1227
6d7cbd77
SM
1228void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1229{
878066e7 1230 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
e2cba8d7 1231 struct ath_chanctx *old_ctx;
6d7cbd77
SM
1232 struct timespec ts;
1233 bool measure_time = false;
1234 bool send_ps = false;
e2cba8d7 1235 bool queues_stopped = false;
6d7cbd77
SM
1236
1237 spin_lock_bh(&sc->chan_lock);
1238 if (!sc->next_chan) {
1239 spin_unlock_bh(&sc->chan_lock);
1240 return;
1241 }
1242
1243 if (!force && ath_chanctx_defer_switch(sc)) {
1244 spin_unlock_bh(&sc->chan_lock);
1245 return;
1246 }
1247
878066e7
SM
1248 ath_dbg(common, CHAN_CTX,
1249 "%s: current: %d MHz, next: %d MHz\n",
1250 __func__,
1251 sc->cur_chan->chandef.center_freq1,
1252 sc->next_chan->chandef.center_freq1);
1253
6d7cbd77 1254 if (sc->cur_chan != sc->next_chan) {
878066e7
SM
1255 ath_dbg(common, CHAN_CTX,
1256 "Stopping current chanctx: %d\n",
1257 sc->cur_chan->chandef.center_freq1);
6d7cbd77
SM
1258 sc->cur_chan->stopped = true;
1259 spin_unlock_bh(&sc->chan_lock);
1260
1261 if (sc->next_chan == &sc->offchannel.chan) {
1262 getrawmonotonic(&ts);
1263 measure_time = true;
1264 }
e2cba8d7
SM
1265
1266 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1267 queues_stopped = true;
1268
25f3bc7d 1269 __ath9k_flush(sc->hw, ~0, true, false, false);
6d7cbd77
SM
1270
1271 if (ath_chanctx_send_ps_frame(sc, true))
e2d389b5 1272 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
25f3bc7d 1273 false, false, false);
6d7cbd77
SM
1274
1275 send_ps = true;
1276 spin_lock_bh(&sc->chan_lock);
1277
1278 if (sc->cur_chan != &sc->offchannel.chan) {
1279 getrawmonotonic(&sc->cur_chan->tsf_ts);
1280 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1281 }
1282 }
e2cba8d7 1283 old_ctx = sc->cur_chan;
6d7cbd77
SM
1284 sc->cur_chan = sc->next_chan;
1285 sc->cur_chan->stopped = false;
1286 sc->next_chan = NULL;
124130d7
SM
1287
1288 if (!sc->sched.offchannel_pending)
1289 sc->sched.offchannel_duration = 0;
1290
6d7cbd77
SM
1291 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1292 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1293
1294 spin_unlock_bh(&sc->chan_lock);
1295
1296 if (sc->sc_ah->chip_fullsleep ||
1297 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1298 sizeof(sc->cur_chandef))) {
878066e7
SM
1299 ath_dbg(common, CHAN_CTX,
1300 "%s: Set channel %d MHz\n",
1301 __func__, sc->cur_chan->chandef.center_freq1);
6d7cbd77
SM
1302 ath_set_channel(sc);
1303 if (measure_time)
1304 sc->sched.channel_switch_time =
1305 ath9k_hw_get_tsf_offset(&ts, NULL);
e2cba8d7
SM
1306 /*
1307 * A reset will ensure that all queues are woken up,
1308 * so there is no need to awaken them again.
1309 */
1310 goto out;
6d7cbd77 1311 }
e2cba8d7
SM
1312
1313 if (queues_stopped)
1314 ath9k_chanctx_wake_queues(sc, old_ctx);
1315out:
6d7cbd77
SM
1316 if (send_ps)
1317 ath_chanctx_send_ps_frame(sc, false);
1318
1319 ath_offchannel_channel_change(sc);
1320 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1321}
1322
0e62f8b7
SM
1323static void ath_chanctx_work(struct work_struct *work)
1324{
1325 struct ath_softc *sc = container_of(work, struct ath_softc,
1326 chanctx_work);
1327 mutex_lock(&sc->mutex);
1328 ath_chanctx_set_next(sc, false);
1329 mutex_unlock(&sc->mutex);
1330}
1331
e90e302a
SM
1332void ath9k_offchannel_init(struct ath_softc *sc)
1333{
1334 struct ath_chanctx *ctx;
1335 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1336 struct ieee80211_supported_band *sband;
1337 struct ieee80211_channel *chan;
1338 int i;
1339
57fbcce3 1340 sband = &common->sbands[NL80211_BAND_2GHZ];
e90e302a 1341 if (!sband->n_channels)
57fbcce3 1342 sband = &common->sbands[NL80211_BAND_5GHZ];
e90e302a
SM
1343
1344 chan = &sband->channels[0];
1345
1346 ctx = &sc->offchannel.chan;
1347 INIT_LIST_HEAD(&ctx->vifs);
1348 ctx->txpower = ATH_TXPOWER_MAX;
1349 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1350
63fefa05
THJ
1351 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
1352 INIT_LIST_HEAD(&ctx->acq[i].acq_new);
1353 INIT_LIST_HEAD(&ctx->acq[i].acq_old);
1354 spin_lock_init(&ctx->acq[i].lock);
1355 }
e90e302a
SM
1356
1357 sc->offchannel.chan.offchannel = true;
1358}
1359
705d0bf8
SM
1360void ath9k_init_channel_context(struct ath_softc *sc)
1361{
1362 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1363
1364 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1365 (unsigned long)sc);
1366 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1367 (unsigned long)sc);
c6500ea2
SM
1368
1369 init_completion(&sc->go_beacon);
705d0bf8 1370}
c7dd40c9 1371
ea22df29
SM
1372void ath9k_deinit_channel_context(struct ath_softc *sc)
1373{
1374 cancel_work_sync(&sc->chanctx_work);
1375}
1376
499afacc
SM
1377bool ath9k_is_chanctx_enabled(void)
1378{
1379 return (ath9k_use_chanctx == 1);
1380}
1381
0e08b5fb
SM
1382/********************/
1383/* Queue management */
1384/********************/
1385
a064eaa1
SM
1386void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1387{
1388 struct ath_hw *ah = sc->sc_ah;
1389 int i;
1390
1391 if (ctx == &sc->offchannel.chan) {
1392 ieee80211_stop_queue(sc->hw,
1393 sc->hw->offchannel_tx_hw_queue);
1394 } else {
1395 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1396 ieee80211_stop_queue(sc->hw,
1397 ctx->hw_queue_base + i);
1398 }
1399
1400 if (ah->opmode == NL80211_IFTYPE_AP)
1401 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1402}
1403
1404
b3903153 1405void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
0e08b5fb
SM
1406{
1407 struct ath_hw *ah = sc->sc_ah;
1408 int i;
1409
b3903153 1410 if (ctx == &sc->offchannel.chan) {
0e08b5fb
SM
1411 ieee80211_wake_queue(sc->hw,
1412 sc->hw->offchannel_tx_hw_queue);
1413 } else {
1414 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1415 ieee80211_wake_queue(sc->hw,
b3903153 1416 ctx->hw_queue_base + i);
0e08b5fb
SM
1417 }
1418
1419 if (ah->opmode == NL80211_IFTYPE_AP)
1420 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1421}
1422
c7dd40c9
SM
1423/*****************/
1424/* P2P Powersave */
1425/*****************/
1426
1427static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
2471adff 1428{
58bb9ca8 1429 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2471adff 1430 struct ath_hw *ah = sc->sc_ah;
631c45f4 1431 u32 tsf, target_tsf;
2471adff
SM
1432
1433 if (!avp || !avp->noa.has_next_tsf)
1434 return;
1435
1436 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1437
1438 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1439
1440 target_tsf = avp->noa.next_tsf;
1441 if (!avp->noa.absent)
1442 target_tsf -= ATH_P2P_PS_STOP_TIME;
b77b59ae
JD
1443 else
1444 target_tsf += ATH_P2P_PS_STOP_TIME;
2471adff
SM
1445
1446 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1447 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1448
58bb9ca8
JD
1449 ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1450 __func__, avp->noa.absent, tsf, target_tsf,
1451 (target_tsf - tsf) / 1000);
1452
631c45f4 1453 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
2471adff
SM
1454}
1455
c7dd40c9
SM
1456static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1457{
1458 struct ath_vif *avp = (void *)vif->drv_priv;
1459 u32 tsf;
1460
1461 if (!sc->p2p_ps_timer)
1462 return;
1463
b9a9693f 1464 if (vif->type != NL80211_IFTYPE_STATION)
c7dd40c9
SM
1465 return;
1466
1467 sc->p2p_ps_vif = avp;
b10b7fb3
JD
1468
1469 if (sc->ps_flags & PS_BEACON_SYNC)
1470 return;
1471
c7dd40c9
SM
1472 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1473 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1474 ath9k_update_p2p_ps_timer(sc, avp);
1475}
1476
fdcf1bd4
SM
1477static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1478{
1479 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1480 u8 switch_time, ctwin;
1481
1482 /*
1483 * Channel switch in multi-channel mode is deferred
1484 * by a quarter beacon interval when handling
1485 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1486 * interface is guaranteed to be discoverable
1487 * for that duration after a TBTT.
1488 */
1489 switch_time = cur_conf->beacon_interval / 4;
1490
1491 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1492 if (ctwin && (ctwin < switch_time))
1493 return ctwin;
1494
1495 if (switch_time < P2P_DEFAULT_CTWIN)
1496 return 0;
1497
1498 return P2P_DEFAULT_CTWIN;
1499}
1500
11e39a4e
SM
1501void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1502 struct sk_buff *skb)
1503{
1504 static const u8 noa_ie_hdr[] = {
1505 WLAN_EID_VENDOR_SPECIFIC, /* type */
1506 0, /* length */
1507 0x50, 0x6f, 0x9a, /* WFA OUI */
1508 0x09, /* P2P subtype */
1509 0x0c, /* Notice of Absence */
1510 0x00, /* LSB of little-endian len */
1511 0x00, /* MSB of little-endian len */
1512 };
1513
1514 struct ieee80211_p2p_noa_attr *noa;
1515 int noa_len, noa_desc, i = 0;
1516 u8 *hdr;
1517
d0975edd 1518 if (!avp->offchannel_duration && !avp->noa_duration)
11e39a4e
SM
1519 return;
1520
d0975edd 1521 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
11e39a4e
SM
1522 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1523
59ae1d12 1524 hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
11e39a4e
SM
1525 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1526 hdr[7] = noa_len;
1527
b080db58 1528 noa = skb_put_zero(skb, noa_len);
11e39a4e
SM
1529
1530 noa->index = avp->noa_index;
fdcf1bd4 1531 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
3edbf0ba
JD
1532 if (noa->oppps_ctwindow)
1533 noa->oppps_ctwindow |= BIT(7);
fdcf1bd4 1534
d0975edd
SM
1535 if (avp->noa_duration) {
1536 if (avp->periodic_noa) {
1537 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1538 noa->desc[i].count = 255;
1539 noa->desc[i].interval = cpu_to_le32(interval);
1540 } else {
1541 noa->desc[i].count = 1;
1542 }
11e39a4e 1543
d0975edd
SM
1544 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1545 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
11e39a4e
SM
1546 i++;
1547 }
1548
1549 if (avp->offchannel_duration) {
1550 noa->desc[i].count = 1;
1551 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1552 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1553 }
1554}
1555
2471adff
SM
1556void ath9k_p2p_ps_timer(void *priv)
1557{
1558 struct ath_softc *sc = priv;
1559 struct ath_vif *avp = sc->p2p_ps_vif;
1560 struct ieee80211_vif *vif;
1561 struct ieee80211_sta *sta;
1562 struct ath_node *an;
1563 u32 tsf;
1564
1565 del_timer_sync(&sc->sched.timer);
1566 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1567 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1568
1569 if (!avp || avp->chanctx != sc->cur_chan)
1570 return;
1571
1572 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1573 if (!avp->noa.absent)
1574 tsf += ATH_P2P_PS_STOP_TIME;
b77b59ae
JD
1575 else
1576 tsf -= ATH_P2P_PS_STOP_TIME;
2471adff
SM
1577
1578 if (!avp->noa.has_next_tsf ||
1579 avp->noa.next_tsf - tsf > BIT(31))
1580 ieee80211_update_p2p_noa(&avp->noa, tsf);
1581
1582 ath9k_update_p2p_ps_timer(sc, avp);
1583
1584 rcu_read_lock();
1585
1586 vif = avp->vif;
cb35582a 1587 sta = ieee80211_find_sta(vif, avp->bssid);
2471adff
SM
1588 if (!sta)
1589 goto out;
1590
1591 an = (void *) sta->drv_priv;
1592 if (an->sleeping == !!avp->noa.absent)
1593 goto out;
1594
1595 an->sleeping = avp->noa.absent;
1596 if (an->sleeping)
1597 ath_tx_aggr_sleep(sta, sc, an);
1598 else
1599 ath_tx_aggr_wakeup(sc, an);
1600
1601out:
1602 rcu_read_unlock();
1603}
1604
c7dd40c9
SM
1605void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1606 struct ieee80211_vif *vif)
1607{
1608 unsigned long flags;
1609
1610 spin_lock_bh(&sc->sc_pcu_lock);
1611 spin_lock_irqsave(&sc->sc_pm_lock, flags);
b10b7fb3 1612 ath9k_update_p2p_ps(sc, vif);
c7dd40c9
SM
1613 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1614 spin_unlock_bh(&sc->sc_pcu_lock);
1615}
1616
1617void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1618{
1619 if (sc->p2p_ps_vif)
1620 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1621}
1622
1623void ath9k_p2p_remove_vif(struct ath_softc *sc,
1624 struct ieee80211_vif *vif)
2471adff
SM
1625{
1626 struct ath_vif *avp = (void *)vif->drv_priv;
2471adff 1627
c7dd40c9
SM
1628 spin_lock_bh(&sc->sc_pcu_lock);
1629 if (avp == sc->p2p_ps_vif) {
1630 sc->p2p_ps_vif = NULL;
1631 ath9k_update_p2p_ps_timer(sc, NULL);
1632 }
1633 spin_unlock_bh(&sc->sc_pcu_lock);
1634}
1635
1636int ath9k_init_p2p(struct ath_softc *sc)
1637{
1638 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1639 NULL, sc, AR_FIRST_NDP_TIMER);
2471adff 1640 if (!sc->p2p_ps_timer)
c7dd40c9 1641 return -ENOMEM;
2471adff 1642
c7dd40c9
SM
1643 return 0;
1644}
2471adff 1645
c7dd40c9
SM
1646void ath9k_deinit_p2p(struct ath_softc *sc)
1647{
1648 if (sc->p2p_ps_timer)
1649 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
2471adff 1650}
c7dd40c9
SM
1651
1652#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */