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