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