blob: 929dd70f48eb19f0ffbcd9ee2b80913d168f47e2 [file] [log] [blame]
Felix Fietkaufbbcd142014-06-11 16:17:49 +05301/*
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];
Sujith Manoharan5555c952014-10-17 07:40:11 +053069 r = ath_reset(sc, hchan);
Felix Fietkaufbbcd142014-06-11 16:17:49 +053070 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
Felix Fietkaufbbcd142014-06-11 16:17:49 +053086 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) &&
Oleksij Rempel8391f602014-11-06 08:53:19 +010095 sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
Oleksij Rempel67dc74f2014-11-06 08:53:30 +010096 ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
Felix Fietkaufbbcd142014-06-11 16:17:49 +053097 }
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;
Felix Fietkau04535312014-06-11 16:17:51 +0530108 int i, j;
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530109
Johannes Berg57fbcce2016-04-12 15:56:15 +0200110 sband = &common->sbands[NL80211_BAND_2GHZ];
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530111 if (!sband->n_channels)
Johannes Berg57fbcce2016-04-12 15:56:15 +0200112 sband = &common->sbands[NL80211_BAND_5GHZ];
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530113
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);
Felix Fietkaubc7e1be2014-06-11 16:17:50 +0530119 ctx->txpower = ATH_TXPOWER_MAX;
Sujith Manoharan2fae0d92014-10-17 07:40:15 +0530120 ctx->flush_timeout = HZ / 5; /* 200ms */
Felix Fietkau04535312014-06-11 16:17:51 +0530121 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
122 INIT_LIST_HEAD(&ctx->acq[j]);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530123 }
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530124}
125
Felix Fietkaubff11762014-06-11 16:17:52 +0530126void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
127 struct cfg80211_chan_def *chandef)
128{
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530129 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkaubff11762014-06-11 16:17:52 +0530130 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
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530138 if (!cur_chan) {
139 ath_dbg(common, CHAN_CTX,
140 "Current context differs from the new context\n");
Felix Fietkaubff11762014-06-11 16:17:52 +0530141 return;
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530142 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530143
144 ath_set_channel(sc);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530145}
Felix Fietkau78b21942014-06-11 16:17:55 +0530146
Sujith Manoharan27babf92014-08-23 13:29:16 +0530147#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
148
Sujith Manoharan26103b82014-10-17 07:40:21 +0530149/*************/
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
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +0530179/**********************************************************/
180/* Functions to handle the channel context state machine. */
181/**********************************************************/
182
Sujith Manoharan27babf92014-08-23 13:29:16 +0530183static const char *offchannel_state_string(enum ath_offchannel_state state)
184{
Sujith Manoharan27babf92014-08-23 13:29:16 +0530185 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
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530198static 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);
Sujith Manoharanb8f92792014-10-17 07:40:09 +0530205 case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530206 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
Geert Uytterhoeven344cd852016-01-15 14:41:30 +0100229static u32 chanctx_event_delta(struct ath_softc *sc)
Janusz Dziedzic02edab52015-11-27 09:37:17 +0100230{
231 u64 ms;
232 struct timespec ts, *old;
233
234 getrawmonotonic(&ts);
235 old = &sc->last_event_time;
236 ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
237 ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
238 sc->last_event_time = ts;
239
240 return (u32)ms;
241}
242
Sujith Manoharana09798f2014-08-23 13:29:21 +0530243void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
244{
245 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan67259d52014-10-17 07:40:16 +0530246 struct ath_chanctx *ictx;
Sujith Manoharana09798f2014-08-23 13:29:21 +0530247 struct ath_vif *avp;
248 bool active = false;
249 u8 n_active = 0;
250
251 if (!ctx)
252 return;
253
Sujith Manoharan290c8a72014-10-17 07:40:17 +0530254 if (ctx == &sc->offchannel.chan) {
255 spin_lock_bh(&sc->chan_lock);
256
257 if (likely(sc->sched.channel_switch_time))
258 ctx->flush_timeout =
259 usecs_to_jiffies(sc->sched.channel_switch_time);
260 else
261 ctx->flush_timeout =
262 msecs_to_jiffies(10);
263
264 spin_unlock_bh(&sc->chan_lock);
265
266 /*
267 * There is no need to iterate over the
268 * active/assigned channel contexts if
269 * the current context is offchannel.
270 */
271 return;
272 }
273
Sujith Manoharan67259d52014-10-17 07:40:16 +0530274 ictx = ctx;
275
Sujith Manoharana09798f2014-08-23 13:29:21 +0530276 list_for_each_entry(avp, &ctx->vifs, list) {
277 struct ieee80211_vif *vif = avp->vif;
278
279 switch (vif->type) {
280 case NL80211_IFTYPE_P2P_CLIENT:
281 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +0530282 if (avp->assoc)
Sujith Manoharana09798f2014-08-23 13:29:21 +0530283 active = true;
284 break;
285 default:
286 active = true;
287 break;
288 }
289 }
290 ctx->active = active;
291
292 ath_for_each_chanctx(sc, ctx) {
293 if (!ctx->assigned || list_empty(&ctx->vifs))
294 continue;
295 n_active++;
296 }
297
Sujith Manoharan67259d52014-10-17 07:40:16 +0530298 spin_lock_bh(&sc->chan_lock);
299
Sujith Manoharana09798f2014-08-23 13:29:21 +0530300 if (n_active <= 1) {
Sujith Manoharan67259d52014-10-17 07:40:16 +0530301 ictx->flush_timeout = HZ / 5;
Sujith Manoharana09798f2014-08-23 13:29:21 +0530302 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
Sujith Manoharan67259d52014-10-17 07:40:16 +0530303 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharana09798f2014-08-23 13:29:21 +0530304 return;
305 }
Sujith Manoharan67259d52014-10-17 07:40:16 +0530306
307 ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
308
309 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
310 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharana09798f2014-08-23 13:29:21 +0530311 return;
Sujith Manoharan67259d52014-10-17 07:40:16 +0530312 }
313
314 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharana09798f2014-08-23 13:29:21 +0530315
316 if (ath9k_is_chanctx_enabled()) {
317 ath_chanctx_event(sc, NULL,
318 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
319 }
320}
321
Felix Fietkau58b57372014-06-11 16:18:08 +0530322static struct ath_chanctx *
323ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
324{
325 int idx = ctx - &sc->chanctx[0];
326
327 return &sc->chanctx[!idx];
328}
329
330static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
331{
332 struct ath_chanctx *prev, *cur;
333 struct timespec ts;
334 u32 cur_tsf, prev_tsf, beacon_int;
335 s32 offset;
336
337 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
338
339 cur = sc->cur_chan;
340 prev = ath_chanctx_get_next(sc, cur);
341
Sujith Manoharan7f30eac2014-09-15 11:25:51 +0530342 if (!prev->switch_after_beacon)
343 return;
344
Felix Fietkau58b57372014-06-11 16:18:08 +0530345 getrawmonotonic(&ts);
346 cur_tsf = (u32) cur->tsf_val +
347 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
348
349 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
350 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
351
352 /* Adjust the TSF time of the AP chanctx to keep its beacons
353 * at half beacon interval offset relative to the STA chanctx.
354 */
355 offset = cur_tsf - prev_tsf;
356
357 /* Ignore stale data or spurious timestamps */
358 if (offset < 0 || offset > 3 * beacon_int)
359 return;
360
361 offset = beacon_int / 2 - (offset % beacon_int);
362 prev->tsf_val += offset;
363}
364
Felix Fietkau42eda112014-06-11 16:18:14 +0530365/* Configure the TSF based hardware timer for a channel switch.
366 * Also set up backup software timer, in case the gen timer fails.
367 * This could be caused by a hardware reset.
368 */
369static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
370{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530371 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau42eda112014-06-11 16:18:14 +0530372 struct ath_hw *ah = sc->sc_ah;
Janusz Dziedzic2f985532015-11-27 09:37:07 +0100373 unsigned long timeout;
Felix Fietkau42eda112014-06-11 16:18:14 +0530374
375 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
376 tsf_time -= ath9k_hw_gettsf32(ah);
Janusz Dziedzic2f985532015-11-27 09:37:07 +0100377 timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
378 mod_timer(&sc->sched.timer, jiffies + timeout);
Sujith Manoharan878066e2014-08-27 12:07:24 +0530379
380 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic2f985532015-11-27 09:37:07 +0100381 "Setup chanctx timer with timeout: %d (%d) ms\n",
382 tsf_time / 1000, jiffies_to_msecs(timeout));
Felix Fietkau42eda112014-06-11 16:18:14 +0530383}
384
Sujith Manoharan828fe012014-10-17 07:40:25 +0530385static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
386 struct ath_chanctx *ctx,
387 struct ath_vif *avp)
388{
389 /*
390 * Clear the extend_absence flag if it had been
391 * set during the previous beacon transmission,
392 * since we need to revert to the normal NoA
393 * schedule.
394 */
395 if (ctx->active && sc->sched.extend_absence) {
396 avp->noa_duration = 0;
397 sc->sched.extend_absence = false;
398 }
399
400 /* If at least two consecutive beacons were missed on the STA
401 * chanctx, stay on the STA channel for one extra beacon period,
402 * to resync the timer properly.
403 */
404 if (ctx->active && sc->sched.beacon_miss >= 2) {
405 avp->noa_duration = 0;
406 sc->sched.extend_absence = true;
407 }
408}
409
Sujith Manoharana23152a2014-10-17 07:40:23 +0530410static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
411 struct ath_chanctx *ctx,
412 struct ath_vif *avp,
413 u32 tsf_time)
414{
415 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
416
417 avp->noa_index++;
418 avp->offchannel_start = tsf_time;
419 avp->offchannel_duration = sc->sched.offchannel_duration;
420
421 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +0100422 "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
Sujith Manoharana23152a2014-10-17 07:40:23 +0530423 avp->offchannel_duration,
424 avp->offchannel_start,
425 avp->noa_index);
426
427 /*
428 * When multiple contexts are active, the NoA
429 * has to be recalculated and advertised after
430 * an offchannel operation.
431 */
432 if (ctx->active && avp->noa_duration)
433 avp->noa_duration = 0;
434}
435
Sujith Manoharan347a9562014-10-17 07:40:24 +0530436static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
437 struct ath_vif *avp,
438 struct ath_beacon_config *cur_conf,
439 u32 tsf_time,
440 u32 beacon_int)
441{
442 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
443
444 avp->noa_index++;
445 avp->noa_start = tsf_time;
446
447 if (sc->sched.extend_absence)
448 avp->noa_duration = (3 * beacon_int / 2) +
449 sc->sched.channel_switch_time;
450 else
451 avp->noa_duration =
452 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
453 sc->sched.channel_switch_time;
454
455 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
456 sc->sched.extend_absence)
457 avp->periodic_noa = false;
458 else
459 avp->periodic_noa = true;
460
461 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +0100462 "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
Sujith Manoharan347a9562014-10-17 07:40:24 +0530463 avp->noa_duration,
464 avp->noa_start,
465 avp->noa_index,
466 avp->periodic_noa);
467}
468
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530469static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
470 struct ath_vif *avp,
471 u32 tsf_time,
472 u32 duration)
473{
474 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
475
476 avp->noa_index++;
477 avp->noa_start = tsf_time;
478 avp->periodic_noa = false;
479 avp->oneshot_noa = true;
480 avp->noa_duration = duration + sc->sched.channel_switch_time;
481
482 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +0100483 "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530484 avp->noa_duration,
485 avp->noa_start,
486 avp->noa_index,
487 avp->periodic_noa);
488}
489
Felix Fietkau748299f2014-06-11 16:18:04 +0530490void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
491 enum ath_chanctx_event ev)
492{
493 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530494 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530495 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530496 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530497 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530498 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530499 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530500
501 if (vif)
502 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530503
504 spin_lock_bh(&sc->chan_lock);
505
Janusz Dziedzic02edab52015-11-27 09:37:17 +0100506 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
Sujith Manoharan878066e2014-08-27 12:07:24 +0530507 sc->cur_chan->chandef.center_freq1,
508 chanctx_event_string(ev),
Janusz Dziedzic02edab52015-11-27 09:37:17 +0100509 chanctx_state_string(sc->sched.state),
510 chanctx_event_delta(sc));
Sujith Manoharan878066e2014-08-27 12:07:24 +0530511
Felix Fietkau748299f2014-06-11 16:18:04 +0530512 switch (ev) {
513 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530514 if (avp->offchannel_duration)
515 avp->offchannel_duration = 0;
516
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530517 if (avp->oneshot_noa) {
518 avp->noa_duration = 0;
519 avp->oneshot_noa = false;
520
521 ath_dbg(common, CHAN_CTX,
522 "Clearing oneshot NoA\n");
523 }
524
Sujith Manoharan878066e2014-08-27 12:07:24 +0530525 if (avp->chanctx != sc->cur_chan) {
526 ath_dbg(common, CHAN_CTX,
527 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530528 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530529 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530530
Sujith Manoharan367b3412014-09-05 09:50:57 +0530531 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530532 sc->sched.offchannel_pending = false;
533 sc->next_chan = &sc->offchannel.chan;
534 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530535 ath_dbg(common, CHAN_CTX,
536 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530537 }
538
539 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
540 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
541 sc->next_chan = ctx;
542 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530543 ath_dbg(common, CHAN_CTX,
544 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530545 }
546
547 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530548 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530549 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530550 ath_dbg(common, CHAN_CTX,
551 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
552 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530553
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530554 if (sc->sched.mgd_prepare_tx)
555 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
556
Sujith Manoharan8890d052014-10-17 07:40:14 +0530557 /*
558 * When a context becomes inactive, for example,
559 * disassociation of a station context, the NoA
560 * attribute needs to be removed from subsequent
561 * beacons.
562 */
563 if (!ctx->active && avp->noa_duration &&
564 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
565 avp->noa_duration = 0;
566 avp->periodic_noa = false;
567
568 ath_dbg(common, CHAN_CTX,
569 "Clearing NoA schedule\n");
570 }
571
Felix Fietkau748299f2014-06-11 16:18:04 +0530572 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
573 break;
574
Sujith Manoharan878066e2014-08-27 12:07:24 +0530575 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
576
Felix Fietkau748299f2014-06-11 16:18:04 +0530577 sc->sched.beacon_pending = true;
578 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530579
Felix Fietkau74148632014-06-11 16:18:11 +0530580 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530581 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
582
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530583 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530584 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530585 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530586 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530587
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530588 /*
589 * If an offchannel switch is scheduled to happen after
590 * a beacon transmission, update the NoA with one-shot
591 * values and increment the index.
592 */
593 if (sc->next_chan == &sc->offchannel.chan) {
Sujith Manoharana23152a2014-10-17 07:40:23 +0530594 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530595 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530596 }
597
Sujith Manoharan828fe012014-10-17 07:40:25 +0530598 ath_chanctx_handle_bmiss(sc, ctx, avp);
Sujith Manoharana2b28602014-09-15 11:25:50 +0530599
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530600 /*
601 * If a mgd_prepare_tx() has been called by mac80211,
602 * a one-shot NoA needs to be sent. This can happen
603 * with one or more active channel contexts - in both
604 * cases, a new NoA schedule has to be advertised.
605 */
606 if (sc->sched.mgd_prepare_tx) {
607 ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
608 jiffies_to_usecs(HZ / 5));
609 break;
610 }
611
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530612 /* Prevent wrap-around issues */
613 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
614 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530615
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530616 /*
617 * If multiple contexts are active, start periodic
618 * NoA and increment the index for the first
619 * announcement.
620 */
621 if (ctx->active &&
Sujith Manoharan347a9562014-10-17 07:40:24 +0530622 (!avp->noa_duration || sc->sched.force_noa_update))
623 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
624 tsf_time, beacon_int);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530625
626 if (ctx->active && sc->sched.force_noa_update)
627 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530628
Felix Fietkau748299f2014-06-11 16:18:04 +0530629 break;
630 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530631 if (!sc->sched.beacon_pending) {
632 ath_dbg(common, CHAN_CTX,
633 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530634 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530635 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530636
637 sc->sched.beacon_pending = false;
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530638
639 if (sc->sched.mgd_prepare_tx) {
640 sc->sched.mgd_prepare_tx = false;
641 complete(&sc->go_beacon);
642 ath_dbg(common, CHAN_CTX,
643 "Beacon sent, complete go_beacon\n");
644 break;
645 }
646
Felix Fietkau748299f2014-06-11 16:18:04 +0530647 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
648 break;
649
Sujith Manoharan878066e2014-08-27 12:07:24 +0530650 ath_dbg(common, CHAN_CTX,
651 "Move chanctx state to WAIT_FOR_TIMER\n");
652
Felix Fietkau748299f2014-06-11 16:18:04 +0530653 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530654 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530655 break;
656 case ATH_CHANCTX_EVENT_TSF_TIMER:
657 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
658 break;
659
Felix Fietkauec70abe2014-06-11 16:18:12 +0530660 if (!sc->cur_chan->switch_after_beacon &&
661 sc->sched.beacon_pending)
662 sc->sched.beacon_miss++;
663
Sujith Manoharan878066e2014-08-27 12:07:24 +0530664 ath_dbg(common, CHAN_CTX,
665 "Move chanctx state to SWITCH\n");
666
Felix Fietkau748299f2014-06-11 16:18:04 +0530667 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
668 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
669 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530670 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530671 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
672 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530673 break;
674
Felix Fietkauec70abe2014-06-11 16:18:12 +0530675 sc->sched.beacon_pending = false;
676 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530677
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530678 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
Sujith Manoharand9092c92014-11-16 06:11:09 +0530679 !sc->sched.beacon_adjust ||
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530680 !sc->cur_chan->tsf_val)
681 break;
682
683 ath_chanctx_adjust_tbtt_delta(sc);
684
Felix Fietkaua899b672014-06-11 16:18:13 +0530685 /* TSF time might have been updated by the incoming beacon,
686 * need update the channel switch timer to reflect the change.
687 */
688 tsf_time = sc->sched.switch_start_time;
689 tsf_time -= (u32) sc->cur_chan->tsf_val +
690 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
691 tsf_time += ath9k_hw_gettsf32(ah);
692
Sujith Manoharand9092c92014-11-16 06:11:09 +0530693 sc->sched.beacon_adjust = false;
Felix Fietkau42eda112014-06-11 16:18:14 +0530694 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530695 break;
Sujith Manoharanb8f92792014-10-17 07:40:09 +0530696 case ATH_CHANCTX_EVENT_AUTHORIZED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530697 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
698 avp->chanctx != sc->cur_chan)
699 break;
700
Sujith Manoharan878066e2014-08-27 12:07:24 +0530701 ath_dbg(common, CHAN_CTX,
702 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
703
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530704 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
705 /* fall through */
706 case ATH_CHANCTX_EVENT_SWITCH:
707 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
708 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
709 sc->cur_chan->switch_after_beacon ||
710 sc->cur_chan == &sc->offchannel.chan)
711 break;
712
713 /* If this is a station chanctx, stay active for a half
714 * beacon period (minus channel switch time)
715 */
716 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530717 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530718
Sujith Manoharan878066e2014-08-27 12:07:24 +0530719 ath_dbg(common, CHAN_CTX,
720 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
721
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530722 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530723 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530724
725 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530726
727 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530728 sc->sched.beacon_miss = 0;
729 tsf_time *= 3;
730 }
731
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530732 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530733 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530734 sc->sched.switch_start_time = tsf_time;
735
Felix Fietkau42eda112014-06-11 16:18:14 +0530736 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530737 sc->sched.beacon_pending = true;
Sujith Manoharand9092c92014-11-16 06:11:09 +0530738 sc->sched.beacon_adjust = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530739 break;
740 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
741 if (sc->cur_chan == &sc->offchannel.chan ||
742 sc->cur_chan->switch_after_beacon)
743 break;
744
745 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
746 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
747 break;
748 case ATH_CHANCTX_EVENT_UNASSIGN:
749 if (sc->cur_chan->assigned) {
750 if (sc->next_chan && !sc->next_chan->assigned &&
751 sc->next_chan != &sc->offchannel.chan)
752 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
753 break;
754 }
755
756 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
757 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
758 if (!ctx->assigned)
759 break;
760
761 sc->next_chan = ctx;
762 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
763 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530764 case ATH_CHANCTX_EVENT_ASSIGN:
765 break;
766 case ATH_CHANCTX_EVENT_CHANGE:
767 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530768 }
769
770 spin_unlock_bh(&sc->chan_lock);
771}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530772
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530773void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
774 enum ath_chanctx_event ev)
775{
776 if (sc->sched.beacon_pending)
777 ath_chanctx_event(sc, NULL, ev);
778}
779
Sujith Manoharana2b28602014-09-15 11:25:50 +0530780void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530781 enum ath_chanctx_event ev)
782{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530783 ath_chanctx_event(sc, NULL, ev);
784}
785
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530786static int ath_scan_channel_duration(struct ath_softc *sc,
787 struct ieee80211_channel *chan)
788{
789 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
790
791 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
792 return (HZ / 9); /* ~110 ms */
793
794 return (HZ / 16); /* ~60 ms */
795}
796
Sujith Manoharan922c9432014-08-23 13:29:15 +0530797static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
798 struct cfg80211_chan_def *chandef)
799{
800 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
801
802 spin_lock_bh(&sc->chan_lock);
803
804 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
805 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530806 if (chandef)
807 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530808
809 sc->sched.offchannel_pending = true;
810 sc->sched.wait_switch = true;
811 sc->sched.offchannel_duration =
812 jiffies_to_usecs(sc->offchannel.duration) +
813 sc->sched.channel_switch_time;
814
Sujith Manoharan922c9432014-08-23 13:29:15 +0530815 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530816 ath_dbg(common, CHAN_CTX,
817 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530818 return;
819 }
820
821 sc->next_chan = ctx;
822 if (chandef) {
823 ctx->chandef = *chandef;
824 ath_dbg(common, CHAN_CTX,
825 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
826 }
827
828 if (sc->next_chan == &sc->offchannel.chan) {
829 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530830 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530831 sc->sched.channel_switch_time;
832
833 if (chandef) {
834 ath_dbg(common, CHAN_CTX,
835 "Offchannel duration for chan %d MHz : %u\n",
836 chandef->center_freq1,
837 sc->sched.offchannel_duration);
838 }
839 }
840 spin_unlock_bh(&sc->chan_lock);
841 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
842}
843
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530844static void ath_chanctx_offchan_switch(struct ath_softc *sc,
845 struct ieee80211_channel *chan)
846{
847 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
848 struct cfg80211_chan_def chandef;
849
850 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
851 ath_dbg(common, CHAN_CTX,
852 "Channel definition created: %d MHz\n", chandef.center_freq1);
853
854 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
855}
856
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530857static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
858 bool active)
859{
860 struct ath_chanctx *ctx;
861
862 ath_for_each_chanctx(sc, ctx) {
863 if (!ctx->assigned || list_empty(&ctx->vifs))
864 continue;
865 if (active && !ctx->active)
866 continue;
867
868 if (ctx->switch_after_beacon)
869 return ctx;
870 }
871
872 return &sc->chanctx[0];
873}
874
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530875static void
876ath_scan_next_channel(struct ath_softc *sc)
877{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530878 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530879 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
880 struct ieee80211_channel *chan;
881
882 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530883 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530884 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
885 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530886 sc->offchannel.scan_idx,
887 req->n_channels);
888
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530889 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
890 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
891 NULL);
892 return;
893 }
894
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530895 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530896 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530897 sc->offchannel.scan_idx);
898
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530899 chan = req->channels[sc->offchannel.scan_idx++];
900 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
901 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530902
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530903 ath_chanctx_offchan_switch(sc, chan);
904}
905
906void ath_offchannel_next(struct ath_softc *sc)
907{
908 struct ieee80211_vif *vif;
909
910 if (sc->offchannel.scan_req) {
911 vif = sc->offchannel.scan_vif;
912 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
913 ath_scan_next_channel(sc);
914 } else if (sc->offchannel.roc_vif) {
915 vif = sc->offchannel.roc_vif;
916 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530917 sc->offchannel.duration =
918 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530919 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
920 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
921 } else {
Sujith Manoharane21a1d82014-11-16 06:11:06 +0530922 spin_lock_bh(&sc->chan_lock);
923 sc->sched.offchannel_pending = false;
924 sc->sched.wait_switch = false;
925 spin_unlock_bh(&sc->chan_lock);
926
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530927 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
928 NULL);
929 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
930 if (sc->ps_idle)
931 ath_cancel_work(sc);
932 }
933}
934
Janusz.Dziedzic@tieto.comd83520b2015-07-21 11:11:41 +0200935void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530936{
Sujith Manoharan4b60af42014-10-02 06:33:12 +0530937 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
938
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530939 sc->offchannel.roc_vif = NULL;
940 sc->offchannel.roc_chan = NULL;
Janusz.Dziedzic@tieto.comd83520b2015-07-21 11:11:41 +0200941
942 switch (reason) {
943 case ATH_ROC_COMPLETE_ABORT:
944 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
945 ieee80211_remain_on_channel_expired(sc->hw);
946 break;
947 case ATH_ROC_COMPLETE_EXPIRE:
948 ath_dbg(common, CHAN_CTX, "RoC expired\n");
949 ieee80211_remain_on_channel_expired(sc->hw);
950 break;
951 case ATH_ROC_COMPLETE_CANCEL:
952 ath_dbg(common, CHAN_CTX, "RoC canceled\n");
953 break;
954 }
955
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530956 ath_offchannel_next(sc);
957 ath9k_ps_restore(sc);
958}
959
960void ath_scan_complete(struct ath_softc *sc, bool abort)
961{
962 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300963 struct cfg80211_scan_info info = {
964 .aborted = abort,
965 };
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530966
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530967 if (abort)
968 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
969 else
970 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
971
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530972 sc->offchannel.scan_req = NULL;
973 sc->offchannel.scan_vif = NULL;
974 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300975 ieee80211_scan_completed(sc->hw, &info);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530976 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530977 spin_lock_bh(&sc->chan_lock);
978 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
979 sc->sched.force_noa_update = true;
980 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530981 ath_offchannel_next(sc);
982 ath9k_ps_restore(sc);
983}
984
985static void ath_scan_send_probe(struct ath_softc *sc,
986 struct cfg80211_ssid *ssid)
987{
988 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
989 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
990 struct ath_tx_control txctl = {};
991 struct sk_buff *skb;
992 struct ieee80211_tx_info *info;
993 int band = sc->offchannel.chan.chandef.chan->band;
994
Johannes Berga344d672014-06-12 22:24:31 +0200995 skb = ieee80211_probereq_get(sc->hw, vif->addr,
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530996 ssid->ssid, ssid->ssid_len, req->ie_len);
997 if (!skb)
998 return;
999
1000 info = IEEE80211_SKB_CB(skb);
1001 if (req->no_cck)
1002 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1003
1004 if (req->ie_len)
1005 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
1006
1007 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1008
1009 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1010 goto error;
1011
1012 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301013 if (ath_tx_start(sc->hw, skb, &txctl))
1014 goto error;
1015
1016 return;
1017
1018error:
1019 ieee80211_free_txskb(sc->hw, skb);
1020}
1021
1022static void ath_scan_channel_start(struct ath_softc *sc)
1023{
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301024 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301025 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1026 int i;
1027
1028 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1029 req->n_ssids) {
1030 for (i = 0; i < req->n_ssids; i++)
1031 ath_scan_send_probe(sc, &req->ssids[i]);
1032
1033 }
1034
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301035 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +05301036 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301037
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301038 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1039 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1040}
1041
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301042static void ath_chanctx_timer(unsigned long data)
1043{
1044 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +05301045 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1046
1047 ath_dbg(common, CHAN_CTX,
1048 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301049
1050 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1051}
1052
1053static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301054{
1055 struct ath_softc *sc = (struct ath_softc *)data;
1056 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301057 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1058
Sujith Manoharan878066e2014-08-27 12:07:24 +05301059 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301060 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301061
1062 switch (sc->offchannel.state) {
1063 case ATH_OFFCHANNEL_PROBE_WAIT:
1064 if (!sc->offchannel.scan_req)
1065 return;
1066
1067 /* get first active channel context */
1068 ctx = ath_chanctx_get_oper_chan(sc, true);
1069 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301070 ath_dbg(common, CHAN_CTX,
1071 "Switch to oper/active context, "
1072 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1073
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301074 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1075 ath_chanctx_switch(sc, ctx, NULL);
1076 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1077 break;
1078 }
1079 /* fall through */
1080 case ATH_OFFCHANNEL_SUSPEND:
1081 if (!sc->offchannel.scan_req)
1082 return;
1083
1084 ath_scan_next_channel(sc);
1085 break;
1086 case ATH_OFFCHANNEL_ROC_START:
1087 case ATH_OFFCHANNEL_ROC_WAIT:
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301088 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
Janusz.Dziedzic@tieto.comd83520b2015-07-21 11:11:41 +02001089 ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301090 break;
1091 default:
1092 break;
1093 }
1094}
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301095
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301096static bool
1097ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1098 bool powersave)
1099{
1100 struct ieee80211_vif *vif = avp->vif;
1101 struct ieee80211_sta *sta = NULL;
1102 struct ieee80211_hdr_3addr *nullfunc;
1103 struct ath_tx_control txctl;
1104 struct sk_buff *skb;
1105 int band = sc->cur_chan->chandef.chan->band;
1106
1107 switch (vif->type) {
1108 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +05301109 if (!avp->assoc)
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301110 return false;
1111
1112 skb = ieee80211_nullfunc_get(sc->hw, vif);
1113 if (!skb)
1114 return false;
1115
1116 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1117 if (powersave)
1118 nullfunc->frame_control |=
1119 cpu_to_le16(IEEE80211_FCTL_PM);
1120
Janusz Dziedzicc1b7bea2015-11-27 09:37:11 +01001121 skb->priority = 7;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301122 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1123 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1124 dev_kfree_skb_any(skb);
1125 return false;
1126 }
1127 break;
1128 default:
1129 return false;
1130 }
1131
1132 memset(&txctl, 0, sizeof(txctl));
1133 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1134 txctl.sta = sta;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301135 if (ath_tx_start(sc->hw, skb, &txctl)) {
1136 ieee80211_free_txskb(sc->hw, skb);
1137 return false;
1138 }
1139
1140 return true;
1141}
1142
1143static bool
1144ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1145{
1146 struct ath_vif *avp;
1147 bool sent = false;
1148
1149 rcu_read_lock();
1150 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1151 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1152 sent = true;
1153 }
1154 rcu_read_unlock();
1155
1156 return sent;
1157}
1158
1159static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1160{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301161 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1162
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301163 if (sc->cur_chan == &sc->offchannel.chan)
1164 return false;
1165
1166 switch (sc->sched.state) {
1167 case ATH_CHANCTX_STATE_SWITCH:
1168 return false;
1169 case ATH_CHANCTX_STATE_IDLE:
1170 if (!sc->cur_chan->switch_after_beacon)
1171 return false;
1172
Sujith Manoharan878066e2014-08-27 12:07:24 +05301173 ath_dbg(common, CHAN_CTX,
1174 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1175
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301176 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1177 break;
1178 default:
1179 break;
1180 }
1181
1182 return true;
1183}
1184
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301185static void ath_offchannel_channel_change(struct ath_softc *sc)
1186{
1187 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1188
Sujith Manoharan878066e2014-08-27 12:07:24 +05301189 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301190 __func__, offchannel_state_string(sc->offchannel.state));
1191
1192 switch (sc->offchannel.state) {
1193 case ATH_OFFCHANNEL_PROBE_SEND:
1194 if (!sc->offchannel.scan_req)
1195 return;
1196
1197 if (sc->cur_chan->chandef.chan !=
1198 sc->offchannel.chan.chandef.chan)
1199 return;
1200
1201 ath_scan_channel_start(sc);
1202 break;
1203 case ATH_OFFCHANNEL_IDLE:
1204 if (!sc->offchannel.scan_req)
1205 return;
1206
1207 ath_scan_complete(sc, false);
1208 break;
1209 case ATH_OFFCHANNEL_ROC_START:
1210 if (sc->cur_chan != &sc->offchannel.chan)
1211 break;
1212
1213 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301214 mod_timer(&sc->offchannel.timer,
1215 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301216 ieee80211_ready_on_channel(sc->hw);
1217 break;
1218 case ATH_OFFCHANNEL_ROC_DONE:
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301219 break;
1220 default:
1221 break;
1222 }
1223}
1224
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301225void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1226{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301227 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301228 struct ath_chanctx *old_ctx;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301229 struct timespec ts;
1230 bool measure_time = false;
1231 bool send_ps = false;
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301232 bool queues_stopped = false;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301233
1234 spin_lock_bh(&sc->chan_lock);
1235 if (!sc->next_chan) {
1236 spin_unlock_bh(&sc->chan_lock);
1237 return;
1238 }
1239
1240 if (!force && ath_chanctx_defer_switch(sc)) {
1241 spin_unlock_bh(&sc->chan_lock);
1242 return;
1243 }
1244
Sujith Manoharan878066e2014-08-27 12:07:24 +05301245 ath_dbg(common, CHAN_CTX,
1246 "%s: current: %d MHz, next: %d MHz\n",
1247 __func__,
1248 sc->cur_chan->chandef.center_freq1,
1249 sc->next_chan->chandef.center_freq1);
1250
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301251 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301252 ath_dbg(common, CHAN_CTX,
1253 "Stopping current chanctx: %d\n",
1254 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301255 sc->cur_chan->stopped = true;
1256 spin_unlock_bh(&sc->chan_lock);
1257
1258 if (sc->next_chan == &sc->offchannel.chan) {
1259 getrawmonotonic(&ts);
1260 measure_time = true;
1261 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301262
1263 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1264 queues_stopped = true;
1265
Sujith Manoharan25f3bc72014-10-17 07:40:29 +05301266 __ath9k_flush(sc->hw, ~0, true, false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301267
1268 if (ath_chanctx_send_ps_frame(sc, true))
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301269 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
Sujith Manoharan25f3bc72014-10-17 07:40:29 +05301270 false, false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301271
1272 send_ps = true;
1273 spin_lock_bh(&sc->chan_lock);
1274
1275 if (sc->cur_chan != &sc->offchannel.chan) {
1276 getrawmonotonic(&sc->cur_chan->tsf_ts);
1277 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1278 }
1279 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301280 old_ctx = sc->cur_chan;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301281 sc->cur_chan = sc->next_chan;
1282 sc->cur_chan->stopped = false;
1283 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301284
1285 if (!sc->sched.offchannel_pending)
1286 sc->sched.offchannel_duration = 0;
1287
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301288 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1289 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1290
1291 spin_unlock_bh(&sc->chan_lock);
1292
1293 if (sc->sc_ah->chip_fullsleep ||
1294 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1295 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301296 ath_dbg(common, CHAN_CTX,
1297 "%s: Set channel %d MHz\n",
1298 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301299 ath_set_channel(sc);
1300 if (measure_time)
1301 sc->sched.channel_switch_time =
1302 ath9k_hw_get_tsf_offset(&ts, NULL);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301303 /*
1304 * A reset will ensure that all queues are woken up,
1305 * so there is no need to awaken them again.
1306 */
1307 goto out;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301308 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301309
1310 if (queues_stopped)
1311 ath9k_chanctx_wake_queues(sc, old_ctx);
1312out:
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301313 if (send_ps)
1314 ath_chanctx_send_ps_frame(sc, false);
1315
1316 ath_offchannel_channel_change(sc);
1317 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1318}
1319
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301320static void ath_chanctx_work(struct work_struct *work)
1321{
1322 struct ath_softc *sc = container_of(work, struct ath_softc,
1323 chanctx_work);
1324 mutex_lock(&sc->mutex);
1325 ath_chanctx_set_next(sc, false);
1326 mutex_unlock(&sc->mutex);
1327}
1328
Sujith Manoharane90e3022014-08-23 13:29:20 +05301329void ath9k_offchannel_init(struct ath_softc *sc)
1330{
1331 struct ath_chanctx *ctx;
1332 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1333 struct ieee80211_supported_band *sband;
1334 struct ieee80211_channel *chan;
1335 int i;
1336
Johannes Berg57fbcce2016-04-12 15:56:15 +02001337 sband = &common->sbands[NL80211_BAND_2GHZ];
Sujith Manoharane90e3022014-08-23 13:29:20 +05301338 if (!sband->n_channels)
Johannes Berg57fbcce2016-04-12 15:56:15 +02001339 sband = &common->sbands[NL80211_BAND_5GHZ];
Sujith Manoharane90e3022014-08-23 13:29:20 +05301340
1341 chan = &sband->channels[0];
1342
1343 ctx = &sc->offchannel.chan;
1344 INIT_LIST_HEAD(&ctx->vifs);
1345 ctx->txpower = ATH_TXPOWER_MAX;
1346 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1347
1348 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1349 INIT_LIST_HEAD(&ctx->acq[i]);
1350
1351 sc->offchannel.chan.offchannel = true;
1352}
1353
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301354void ath9k_init_channel_context(struct ath_softc *sc)
1355{
1356 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1357
1358 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1359 (unsigned long)sc);
1360 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1361 (unsigned long)sc);
Sujith Manoharanc6500ea2014-10-17 07:40:22 +05301362
1363 init_completion(&sc->go_beacon);
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301364}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301365
Sujith Manoharanea22df22014-08-23 13:29:07 +05301366void ath9k_deinit_channel_context(struct ath_softc *sc)
1367{
1368 cancel_work_sync(&sc->chanctx_work);
1369}
1370
Sujith Manoharan499afac2014-08-22 20:39:31 +05301371bool ath9k_is_chanctx_enabled(void)
1372{
1373 return (ath9k_use_chanctx == 1);
1374}
1375
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301376/********************/
1377/* Queue management */
1378/********************/
1379
Sujith Manoharana064eaa2014-10-02 06:33:18 +05301380void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1381{
1382 struct ath_hw *ah = sc->sc_ah;
1383 int i;
1384
1385 if (ctx == &sc->offchannel.chan) {
1386 ieee80211_stop_queue(sc->hw,
1387 sc->hw->offchannel_tx_hw_queue);
1388 } else {
1389 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1390 ieee80211_stop_queue(sc->hw,
1391 ctx->hw_queue_base + i);
1392 }
1393
1394 if (ah->opmode == NL80211_IFTYPE_AP)
1395 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1396}
1397
1398
Sujith Manoharanb3903152014-10-02 06:33:17 +05301399void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301400{
1401 struct ath_hw *ah = sc->sc_ah;
1402 int i;
1403
Sujith Manoharanb3903152014-10-02 06:33:17 +05301404 if (ctx == &sc->offchannel.chan) {
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301405 ieee80211_wake_queue(sc->hw,
1406 sc->hw->offchannel_tx_hw_queue);
1407 } else {
1408 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1409 ieee80211_wake_queue(sc->hw,
Sujith Manoharanb3903152014-10-02 06:33:17 +05301410 ctx->hw_queue_base + i);
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301411 }
1412
1413 if (ah->opmode == NL80211_IFTYPE_AP)
1414 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1415}
1416
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301417/*****************/
1418/* P2P Powersave */
1419/*****************/
1420
1421static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301422{
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +01001423 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301424 struct ath_hw *ah = sc->sc_ah;
Janusz Dziedzic631c45f2015-11-27 09:37:10 +01001425 u32 tsf, target_tsf;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301426
1427 if (!avp || !avp->noa.has_next_tsf)
1428 return;
1429
1430 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1431
1432 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1433
1434 target_tsf = avp->noa.next_tsf;
1435 if (!avp->noa.absent)
1436 target_tsf -= ATH_P2P_PS_STOP_TIME;
Janusz Dziedzicb77b59a2015-11-27 09:37:09 +01001437 else
1438 target_tsf += ATH_P2P_PS_STOP_TIME;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301439
1440 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1441 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1442
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +01001443 ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1444 __func__, avp->noa.absent, tsf, target_tsf,
1445 (target_tsf - tsf) / 1000);
1446
Janusz Dziedzic631c45f2015-11-27 09:37:10 +01001447 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301448}
1449
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301450static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1451{
1452 struct ath_vif *avp = (void *)vif->drv_priv;
1453 u32 tsf;
1454
1455 if (!sc->p2p_ps_timer)
1456 return;
1457
Kalle Valob9a96932015-11-27 09:37:14 +01001458 if (vif->type != NL80211_IFTYPE_STATION)
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301459 return;
1460
1461 sc->p2p_ps_vif = avp;
Janusz Dziedzicb10b7fb2015-11-27 09:37:13 +01001462
1463 if (sc->ps_flags & PS_BEACON_SYNC)
1464 return;
1465
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301466 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1467 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1468 ath9k_update_p2p_ps_timer(sc, avp);
1469}
1470
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301471static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1472{
1473 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1474 u8 switch_time, ctwin;
1475
1476 /*
1477 * Channel switch in multi-channel mode is deferred
1478 * by a quarter beacon interval when handling
1479 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1480 * interface is guaranteed to be discoverable
1481 * for that duration after a TBTT.
1482 */
1483 switch_time = cur_conf->beacon_interval / 4;
1484
1485 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1486 if (ctwin && (ctwin < switch_time))
1487 return ctwin;
1488
1489 if (switch_time < P2P_DEFAULT_CTWIN)
1490 return 0;
1491
1492 return P2P_DEFAULT_CTWIN;
1493}
1494
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301495void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1496 struct sk_buff *skb)
1497{
1498 static const u8 noa_ie_hdr[] = {
1499 WLAN_EID_VENDOR_SPECIFIC, /* type */
1500 0, /* length */
1501 0x50, 0x6f, 0x9a, /* WFA OUI */
1502 0x09, /* P2P subtype */
1503 0x0c, /* Notice of Absence */
1504 0x00, /* LSB of little-endian len */
1505 0x00, /* MSB of little-endian len */
1506 };
1507
1508 struct ieee80211_p2p_noa_attr *noa;
1509 int noa_len, noa_desc, i = 0;
1510 u8 *hdr;
1511
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301512 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301513 return;
1514
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301515 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301516 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1517
1518 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1519 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1520 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1521 hdr[7] = noa_len;
1522
1523 noa = (void *) skb_put(skb, noa_len);
1524 memset(noa, 0, noa_len);
1525
1526 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301527 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
Janusz Dziedzic3edbf0b2015-11-27 09:37:12 +01001528 if (noa->oppps_ctwindow)
1529 noa->oppps_ctwindow |= BIT(7);
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301530
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301531 if (avp->noa_duration) {
1532 if (avp->periodic_noa) {
1533 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1534 noa->desc[i].count = 255;
1535 noa->desc[i].interval = cpu_to_le32(interval);
1536 } else {
1537 noa->desc[i].count = 1;
1538 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301539
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301540 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1541 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301542 i++;
1543 }
1544
1545 if (avp->offchannel_duration) {
1546 noa->desc[i].count = 1;
1547 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1548 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1549 }
1550}
1551
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301552void ath9k_p2p_ps_timer(void *priv)
1553{
1554 struct ath_softc *sc = priv;
1555 struct ath_vif *avp = sc->p2p_ps_vif;
1556 struct ieee80211_vif *vif;
1557 struct ieee80211_sta *sta;
1558 struct ath_node *an;
1559 u32 tsf;
1560
1561 del_timer_sync(&sc->sched.timer);
1562 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1563 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1564
1565 if (!avp || avp->chanctx != sc->cur_chan)
1566 return;
1567
1568 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1569 if (!avp->noa.absent)
1570 tsf += ATH_P2P_PS_STOP_TIME;
Janusz Dziedzicb77b59a2015-11-27 09:37:09 +01001571 else
1572 tsf -= ATH_P2P_PS_STOP_TIME;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301573
1574 if (!avp->noa.has_next_tsf ||
1575 avp->noa.next_tsf - tsf > BIT(31))
1576 ieee80211_update_p2p_noa(&avp->noa, tsf);
1577
1578 ath9k_update_p2p_ps_timer(sc, avp);
1579
1580 rcu_read_lock();
1581
1582 vif = avp->vif;
Sujith Manoharancb355822014-09-17 14:45:56 +05301583 sta = ieee80211_find_sta(vif, avp->bssid);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301584 if (!sta)
1585 goto out;
1586
1587 an = (void *) sta->drv_priv;
1588 if (an->sleeping == !!avp->noa.absent)
1589 goto out;
1590
1591 an->sleeping = avp->noa.absent;
1592 if (an->sleeping)
1593 ath_tx_aggr_sleep(sta, sc, an);
1594 else
1595 ath_tx_aggr_wakeup(sc, an);
1596
1597out:
1598 rcu_read_unlock();
1599}
1600
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301601void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1602 struct ieee80211_vif *vif)
1603{
1604 unsigned long flags;
1605
1606 spin_lock_bh(&sc->sc_pcu_lock);
1607 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Janusz Dziedzicb10b7fb2015-11-27 09:37:13 +01001608 ath9k_update_p2p_ps(sc, vif);
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301609 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1610 spin_unlock_bh(&sc->sc_pcu_lock);
1611}
1612
1613void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1614{
1615 if (sc->p2p_ps_vif)
1616 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1617}
1618
1619void ath9k_p2p_remove_vif(struct ath_softc *sc,
1620 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301621{
1622 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301623
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301624 spin_lock_bh(&sc->sc_pcu_lock);
1625 if (avp == sc->p2p_ps_vif) {
1626 sc->p2p_ps_vif = NULL;
1627 ath9k_update_p2p_ps_timer(sc, NULL);
1628 }
1629 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301630}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301631
1632int ath9k_init_p2p(struct ath_softc *sc)
1633{
1634 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1635 NULL, sc, AR_FIRST_NDP_TIMER);
1636 if (!sc->p2p_ps_timer)
1637 return -ENOMEM;
1638
1639 return 0;
1640}
1641
1642void ath9k_deinit_p2p(struct ath_softc *sc)
1643{
1644 if (sc->p2p_ps_timer)
1645 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1646}
1647
1648#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */