blob: dddaaeac9433bd25eeb26c0d6aa6d9f95371e788 [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
110 sband = &common->sbands[IEEE80211_BAND_2GHZ];
111 if (!sband->n_channels)
112 sband = &common->sbands[IEEE80211_BAND_5GHZ];
113
114 chan = &sband->channels[0];
115 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
116 ctx = &sc->chanctx[i];
117 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
118 INIT_LIST_HEAD(&ctx->vifs);
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
Sujith Manoharana09798f2014-08-23 13:29:21 +0530229void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
230{
231 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan67259d52014-10-17 07:40:16 +0530232 struct ath_chanctx *ictx;
Sujith Manoharana09798f2014-08-23 13:29:21 +0530233 struct ath_vif *avp;
234 bool active = false;
235 u8 n_active = 0;
236
237 if (!ctx)
238 return;
239
Sujith Manoharan290c8a72014-10-17 07:40:17 +0530240 if (ctx == &sc->offchannel.chan) {
241 spin_lock_bh(&sc->chan_lock);
242
243 if (likely(sc->sched.channel_switch_time))
244 ctx->flush_timeout =
245 usecs_to_jiffies(sc->sched.channel_switch_time);
246 else
247 ctx->flush_timeout =
248 msecs_to_jiffies(10);
249
250 spin_unlock_bh(&sc->chan_lock);
251
252 /*
253 * There is no need to iterate over the
254 * active/assigned channel contexts if
255 * the current context is offchannel.
256 */
257 return;
258 }
259
Sujith Manoharan67259d52014-10-17 07:40:16 +0530260 ictx = ctx;
261
Sujith Manoharana09798f2014-08-23 13:29:21 +0530262 list_for_each_entry(avp, &ctx->vifs, list) {
263 struct ieee80211_vif *vif = avp->vif;
264
265 switch (vif->type) {
266 case NL80211_IFTYPE_P2P_CLIENT:
267 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +0530268 if (avp->assoc)
Sujith Manoharana09798f2014-08-23 13:29:21 +0530269 active = true;
270 break;
271 default:
272 active = true;
273 break;
274 }
275 }
276 ctx->active = active;
277
278 ath_for_each_chanctx(sc, ctx) {
279 if (!ctx->assigned || list_empty(&ctx->vifs))
280 continue;
281 n_active++;
282 }
283
Sujith Manoharan67259d52014-10-17 07:40:16 +0530284 spin_lock_bh(&sc->chan_lock);
285
Sujith Manoharana09798f2014-08-23 13:29:21 +0530286 if (n_active <= 1) {
Sujith Manoharan67259d52014-10-17 07:40:16 +0530287 ictx->flush_timeout = HZ / 5;
Sujith Manoharana09798f2014-08-23 13:29:21 +0530288 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
Sujith Manoharan67259d52014-10-17 07:40:16 +0530289 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharana09798f2014-08-23 13:29:21 +0530290 return;
291 }
Sujith Manoharan67259d52014-10-17 07:40:16 +0530292
293 ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
294
295 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
296 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharana09798f2014-08-23 13:29:21 +0530297 return;
Sujith Manoharan67259d52014-10-17 07:40:16 +0530298 }
299
300 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharana09798f2014-08-23 13:29:21 +0530301
302 if (ath9k_is_chanctx_enabled()) {
303 ath_chanctx_event(sc, NULL,
304 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
305 }
306}
307
Felix Fietkau58b57372014-06-11 16:18:08 +0530308static struct ath_chanctx *
309ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
310{
311 int idx = ctx - &sc->chanctx[0];
312
313 return &sc->chanctx[!idx];
314}
315
316static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
317{
318 struct ath_chanctx *prev, *cur;
319 struct timespec ts;
320 u32 cur_tsf, prev_tsf, beacon_int;
321 s32 offset;
322
323 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
324
325 cur = sc->cur_chan;
326 prev = ath_chanctx_get_next(sc, cur);
327
Sujith Manoharan7f30eac2014-09-15 11:25:51 +0530328 if (!prev->switch_after_beacon)
329 return;
330
Felix Fietkau58b57372014-06-11 16:18:08 +0530331 getrawmonotonic(&ts);
332 cur_tsf = (u32) cur->tsf_val +
333 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
334
335 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
336 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
337
338 /* Adjust the TSF time of the AP chanctx to keep its beacons
339 * at half beacon interval offset relative to the STA chanctx.
340 */
341 offset = cur_tsf - prev_tsf;
342
343 /* Ignore stale data or spurious timestamps */
344 if (offset < 0 || offset > 3 * beacon_int)
345 return;
346
347 offset = beacon_int / 2 - (offset % beacon_int);
348 prev->tsf_val += offset;
349}
350
Felix Fietkau42eda112014-06-11 16:18:14 +0530351/* Configure the TSF based hardware timer for a channel switch.
352 * Also set up backup software timer, in case the gen timer fails.
353 * This could be caused by a hardware reset.
354 */
355static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
356{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530357 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau42eda112014-06-11 16:18:14 +0530358 struct ath_hw *ah = sc->sc_ah;
Janusz Dziedzic2f985532015-11-27 09:37:07 +0100359 unsigned long timeout;
Felix Fietkau42eda112014-06-11 16:18:14 +0530360
361 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
362 tsf_time -= ath9k_hw_gettsf32(ah);
Janusz Dziedzic2f985532015-11-27 09:37:07 +0100363 timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
364 mod_timer(&sc->sched.timer, jiffies + timeout);
Sujith Manoharan878066e2014-08-27 12:07:24 +0530365
366 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic2f985532015-11-27 09:37:07 +0100367 "Setup chanctx timer with timeout: %d (%d) ms\n",
368 tsf_time / 1000, jiffies_to_msecs(timeout));
Felix Fietkau42eda112014-06-11 16:18:14 +0530369}
370
Sujith Manoharan828fe012014-10-17 07:40:25 +0530371static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
372 struct ath_chanctx *ctx,
373 struct ath_vif *avp)
374{
375 /*
376 * Clear the extend_absence flag if it had been
377 * set during the previous beacon transmission,
378 * since we need to revert to the normal NoA
379 * schedule.
380 */
381 if (ctx->active && sc->sched.extend_absence) {
382 avp->noa_duration = 0;
383 sc->sched.extend_absence = false;
384 }
385
386 /* If at least two consecutive beacons were missed on the STA
387 * chanctx, stay on the STA channel for one extra beacon period,
388 * to resync the timer properly.
389 */
390 if (ctx->active && sc->sched.beacon_miss >= 2) {
391 avp->noa_duration = 0;
392 sc->sched.extend_absence = true;
393 }
394}
395
Sujith Manoharana23152a2014-10-17 07:40:23 +0530396static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
397 struct ath_chanctx *ctx,
398 struct ath_vif *avp,
399 u32 tsf_time)
400{
401 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
402
403 avp->noa_index++;
404 avp->offchannel_start = tsf_time;
405 avp->offchannel_duration = sc->sched.offchannel_duration;
406
407 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +0100408 "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
Sujith Manoharana23152a2014-10-17 07:40:23 +0530409 avp->offchannel_duration,
410 avp->offchannel_start,
411 avp->noa_index);
412
413 /*
414 * When multiple contexts are active, the NoA
415 * has to be recalculated and advertised after
416 * an offchannel operation.
417 */
418 if (ctx->active && avp->noa_duration)
419 avp->noa_duration = 0;
420}
421
Sujith Manoharan347a9562014-10-17 07:40:24 +0530422static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
423 struct ath_vif *avp,
424 struct ath_beacon_config *cur_conf,
425 u32 tsf_time,
426 u32 beacon_int)
427{
428 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
429
430 avp->noa_index++;
431 avp->noa_start = tsf_time;
432
433 if (sc->sched.extend_absence)
434 avp->noa_duration = (3 * beacon_int / 2) +
435 sc->sched.channel_switch_time;
436 else
437 avp->noa_duration =
438 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
439 sc->sched.channel_switch_time;
440
441 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
442 sc->sched.extend_absence)
443 avp->periodic_noa = false;
444 else
445 avp->periodic_noa = true;
446
447 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +0100448 "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
Sujith Manoharan347a9562014-10-17 07:40:24 +0530449 avp->noa_duration,
450 avp->noa_start,
451 avp->noa_index,
452 avp->periodic_noa);
453}
454
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530455static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
456 struct ath_vif *avp,
457 u32 tsf_time,
458 u32 duration)
459{
460 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
461
462 avp->noa_index++;
463 avp->noa_start = tsf_time;
464 avp->periodic_noa = false;
465 avp->oneshot_noa = true;
466 avp->noa_duration = duration + sc->sched.channel_switch_time;
467
468 ath_dbg(common, CHAN_CTX,
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +0100469 "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530470 avp->noa_duration,
471 avp->noa_start,
472 avp->noa_index,
473 avp->periodic_noa);
474}
475
Felix Fietkau748299f2014-06-11 16:18:04 +0530476void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
477 enum ath_chanctx_event ev)
478{
479 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530480 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530481 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530482 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530483 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530484 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530485 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530486
487 if (vif)
488 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530489
490 spin_lock_bh(&sc->chan_lock);
491
Sujith Manoharan878066e2014-08-27 12:07:24 +0530492 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
493 sc->cur_chan->chandef.center_freq1,
494 chanctx_event_string(ev),
495 chanctx_state_string(sc->sched.state));
496
Felix Fietkau748299f2014-06-11 16:18:04 +0530497 switch (ev) {
498 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530499 if (avp->offchannel_duration)
500 avp->offchannel_duration = 0;
501
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530502 if (avp->oneshot_noa) {
503 avp->noa_duration = 0;
504 avp->oneshot_noa = false;
505
506 ath_dbg(common, CHAN_CTX,
507 "Clearing oneshot NoA\n");
508 }
509
Sujith Manoharan878066e2014-08-27 12:07:24 +0530510 if (avp->chanctx != sc->cur_chan) {
511 ath_dbg(common, CHAN_CTX,
512 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530513 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530514 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530515
Sujith Manoharan367b3412014-09-05 09:50:57 +0530516 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530517 sc->sched.offchannel_pending = false;
518 sc->next_chan = &sc->offchannel.chan;
519 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530520 ath_dbg(common, CHAN_CTX,
521 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530522 }
523
524 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
525 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
526 sc->next_chan = ctx;
527 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530528 ath_dbg(common, CHAN_CTX,
529 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530530 }
531
532 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530533 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530534 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530535 ath_dbg(common, CHAN_CTX,
536 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
537 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530538
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530539 if (sc->sched.mgd_prepare_tx)
540 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
541
Sujith Manoharan8890d052014-10-17 07:40:14 +0530542 /*
543 * When a context becomes inactive, for example,
544 * disassociation of a station context, the NoA
545 * attribute needs to be removed from subsequent
546 * beacons.
547 */
548 if (!ctx->active && avp->noa_duration &&
549 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
550 avp->noa_duration = 0;
551 avp->periodic_noa = false;
552
553 ath_dbg(common, CHAN_CTX,
554 "Clearing NoA schedule\n");
555 }
556
Felix Fietkau748299f2014-06-11 16:18:04 +0530557 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
558 break;
559
Sujith Manoharan878066e2014-08-27 12:07:24 +0530560 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
561
Felix Fietkau748299f2014-06-11 16:18:04 +0530562 sc->sched.beacon_pending = true;
563 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530564
Felix Fietkau74148632014-06-11 16:18:11 +0530565 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530566 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
567
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530568 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530569 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530570 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530571 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530572
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530573 /*
574 * If an offchannel switch is scheduled to happen after
575 * a beacon transmission, update the NoA with one-shot
576 * values and increment the index.
577 */
578 if (sc->next_chan == &sc->offchannel.chan) {
Sujith Manoharana23152a2014-10-17 07:40:23 +0530579 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530580 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530581 }
582
Sujith Manoharan828fe012014-10-17 07:40:25 +0530583 ath_chanctx_handle_bmiss(sc, ctx, avp);
Sujith Manoharana2b28602014-09-15 11:25:50 +0530584
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530585 /*
586 * If a mgd_prepare_tx() has been called by mac80211,
587 * a one-shot NoA needs to be sent. This can happen
588 * with one or more active channel contexts - in both
589 * cases, a new NoA schedule has to be advertised.
590 */
591 if (sc->sched.mgd_prepare_tx) {
592 ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
593 jiffies_to_usecs(HZ / 5));
594 break;
595 }
596
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530597 /* Prevent wrap-around issues */
598 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
599 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530600
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530601 /*
602 * If multiple contexts are active, start periodic
603 * NoA and increment the index for the first
604 * announcement.
605 */
606 if (ctx->active &&
Sujith Manoharan347a9562014-10-17 07:40:24 +0530607 (!avp->noa_duration || sc->sched.force_noa_update))
608 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
609 tsf_time, beacon_int);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530610
611 if (ctx->active && sc->sched.force_noa_update)
612 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530613
Felix Fietkau748299f2014-06-11 16:18:04 +0530614 break;
615 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530616 if (!sc->sched.beacon_pending) {
617 ath_dbg(common, CHAN_CTX,
618 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530619 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530620 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530621
622 sc->sched.beacon_pending = false;
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530623
624 if (sc->sched.mgd_prepare_tx) {
625 sc->sched.mgd_prepare_tx = false;
626 complete(&sc->go_beacon);
627 ath_dbg(common, CHAN_CTX,
628 "Beacon sent, complete go_beacon\n");
629 break;
630 }
631
Felix Fietkau748299f2014-06-11 16:18:04 +0530632 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
633 break;
634
Sujith Manoharan878066e2014-08-27 12:07:24 +0530635 ath_dbg(common, CHAN_CTX,
636 "Move chanctx state to WAIT_FOR_TIMER\n");
637
Felix Fietkau748299f2014-06-11 16:18:04 +0530638 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530639 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530640 break;
641 case ATH_CHANCTX_EVENT_TSF_TIMER:
642 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
643 break;
644
Felix Fietkauec70abe2014-06-11 16:18:12 +0530645 if (!sc->cur_chan->switch_after_beacon &&
646 sc->sched.beacon_pending)
647 sc->sched.beacon_miss++;
648
Sujith Manoharan878066e2014-08-27 12:07:24 +0530649 ath_dbg(common, CHAN_CTX,
650 "Move chanctx state to SWITCH\n");
651
Felix Fietkau748299f2014-06-11 16:18:04 +0530652 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
653 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
654 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530655 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530656 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
657 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530658 break;
659
Felix Fietkauec70abe2014-06-11 16:18:12 +0530660 sc->sched.beacon_pending = false;
661 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530662
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530663 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
Sujith Manoharand9092c92014-11-16 06:11:09 +0530664 !sc->sched.beacon_adjust ||
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530665 !sc->cur_chan->tsf_val)
666 break;
667
668 ath_chanctx_adjust_tbtt_delta(sc);
669
Felix Fietkaua899b672014-06-11 16:18:13 +0530670 /* TSF time might have been updated by the incoming beacon,
671 * need update the channel switch timer to reflect the change.
672 */
673 tsf_time = sc->sched.switch_start_time;
674 tsf_time -= (u32) sc->cur_chan->tsf_val +
675 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
676 tsf_time += ath9k_hw_gettsf32(ah);
677
Sujith Manoharand9092c92014-11-16 06:11:09 +0530678 sc->sched.beacon_adjust = false;
Felix Fietkau42eda112014-06-11 16:18:14 +0530679 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530680 break;
Sujith Manoharanb8f92792014-10-17 07:40:09 +0530681 case ATH_CHANCTX_EVENT_AUTHORIZED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530682 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
683 avp->chanctx != sc->cur_chan)
684 break;
685
Sujith Manoharan878066e2014-08-27 12:07:24 +0530686 ath_dbg(common, CHAN_CTX,
687 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
688
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530689 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
690 /* fall through */
691 case ATH_CHANCTX_EVENT_SWITCH:
692 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
693 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
694 sc->cur_chan->switch_after_beacon ||
695 sc->cur_chan == &sc->offchannel.chan)
696 break;
697
698 /* If this is a station chanctx, stay active for a half
699 * beacon period (minus channel switch time)
700 */
701 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530702 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530703
Sujith Manoharan878066e2014-08-27 12:07:24 +0530704 ath_dbg(common, CHAN_CTX,
705 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
706
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530707 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530708 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530709
710 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530711
712 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530713 sc->sched.beacon_miss = 0;
714 tsf_time *= 3;
715 }
716
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530717 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530718 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530719 sc->sched.switch_start_time = tsf_time;
720
Felix Fietkau42eda112014-06-11 16:18:14 +0530721 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530722 sc->sched.beacon_pending = true;
Sujith Manoharand9092c92014-11-16 06:11:09 +0530723 sc->sched.beacon_adjust = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530724 break;
725 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
726 if (sc->cur_chan == &sc->offchannel.chan ||
727 sc->cur_chan->switch_after_beacon)
728 break;
729
730 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
731 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
732 break;
733 case ATH_CHANCTX_EVENT_UNASSIGN:
734 if (sc->cur_chan->assigned) {
735 if (sc->next_chan && !sc->next_chan->assigned &&
736 sc->next_chan != &sc->offchannel.chan)
737 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
738 break;
739 }
740
741 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
742 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
743 if (!ctx->assigned)
744 break;
745
746 sc->next_chan = ctx;
747 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
748 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530749 case ATH_CHANCTX_EVENT_ASSIGN:
750 break;
751 case ATH_CHANCTX_EVENT_CHANGE:
752 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530753 }
754
755 spin_unlock_bh(&sc->chan_lock);
756}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530757
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530758void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
759 enum ath_chanctx_event ev)
760{
761 if (sc->sched.beacon_pending)
762 ath_chanctx_event(sc, NULL, ev);
763}
764
Sujith Manoharana2b28602014-09-15 11:25:50 +0530765void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530766 enum ath_chanctx_event ev)
767{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530768 ath_chanctx_event(sc, NULL, ev);
769}
770
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530771static int ath_scan_channel_duration(struct ath_softc *sc,
772 struct ieee80211_channel *chan)
773{
774 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
775
776 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
777 return (HZ / 9); /* ~110 ms */
778
779 return (HZ / 16); /* ~60 ms */
780}
781
Sujith Manoharan922c9432014-08-23 13:29:15 +0530782static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
783 struct cfg80211_chan_def *chandef)
784{
785 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
786
787 spin_lock_bh(&sc->chan_lock);
788
789 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
790 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530791 if (chandef)
792 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530793
794 sc->sched.offchannel_pending = true;
795 sc->sched.wait_switch = true;
796 sc->sched.offchannel_duration =
797 jiffies_to_usecs(sc->offchannel.duration) +
798 sc->sched.channel_switch_time;
799
Sujith Manoharan922c9432014-08-23 13:29:15 +0530800 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530801 ath_dbg(common, CHAN_CTX,
802 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530803 return;
804 }
805
806 sc->next_chan = ctx;
807 if (chandef) {
808 ctx->chandef = *chandef;
809 ath_dbg(common, CHAN_CTX,
810 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
811 }
812
813 if (sc->next_chan == &sc->offchannel.chan) {
814 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530815 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530816 sc->sched.channel_switch_time;
817
818 if (chandef) {
819 ath_dbg(common, CHAN_CTX,
820 "Offchannel duration for chan %d MHz : %u\n",
821 chandef->center_freq1,
822 sc->sched.offchannel_duration);
823 }
824 }
825 spin_unlock_bh(&sc->chan_lock);
826 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
827}
828
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530829static void ath_chanctx_offchan_switch(struct ath_softc *sc,
830 struct ieee80211_channel *chan)
831{
832 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
833 struct cfg80211_chan_def chandef;
834
835 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
836 ath_dbg(common, CHAN_CTX,
837 "Channel definition created: %d MHz\n", chandef.center_freq1);
838
839 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
840}
841
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530842static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
843 bool active)
844{
845 struct ath_chanctx *ctx;
846
847 ath_for_each_chanctx(sc, ctx) {
848 if (!ctx->assigned || list_empty(&ctx->vifs))
849 continue;
850 if (active && !ctx->active)
851 continue;
852
853 if (ctx->switch_after_beacon)
854 return ctx;
855 }
856
857 return &sc->chanctx[0];
858}
859
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530860static void
861ath_scan_next_channel(struct ath_softc *sc)
862{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530863 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530864 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
865 struct ieee80211_channel *chan;
866
867 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530868 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530869 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
870 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530871 sc->offchannel.scan_idx,
872 req->n_channels);
873
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530874 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
875 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
876 NULL);
877 return;
878 }
879
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530880 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530881 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530882 sc->offchannel.scan_idx);
883
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530884 chan = req->channels[sc->offchannel.scan_idx++];
885 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
886 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530887
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530888 ath_chanctx_offchan_switch(sc, chan);
889}
890
891void ath_offchannel_next(struct ath_softc *sc)
892{
893 struct ieee80211_vif *vif;
894
895 if (sc->offchannel.scan_req) {
896 vif = sc->offchannel.scan_vif;
897 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
898 ath_scan_next_channel(sc);
899 } else if (sc->offchannel.roc_vif) {
900 vif = sc->offchannel.roc_vif;
901 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530902 sc->offchannel.duration =
903 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530904 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
905 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
906 } else {
Sujith Manoharane21a1d82014-11-16 06:11:06 +0530907 spin_lock_bh(&sc->chan_lock);
908 sc->sched.offchannel_pending = false;
909 sc->sched.wait_switch = false;
910 spin_unlock_bh(&sc->chan_lock);
911
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530912 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
913 NULL);
914 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
915 if (sc->ps_idle)
916 ath_cancel_work(sc);
917 }
918}
919
Janusz.Dziedzic@tieto.comd83520b2015-07-21 11:11:41 +0200920void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530921{
Sujith Manoharan4b60af42014-10-02 06:33:12 +0530922 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
923
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530924 sc->offchannel.roc_vif = NULL;
925 sc->offchannel.roc_chan = NULL;
Janusz.Dziedzic@tieto.comd83520b2015-07-21 11:11:41 +0200926
927 switch (reason) {
928 case ATH_ROC_COMPLETE_ABORT:
929 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
930 ieee80211_remain_on_channel_expired(sc->hw);
931 break;
932 case ATH_ROC_COMPLETE_EXPIRE:
933 ath_dbg(common, CHAN_CTX, "RoC expired\n");
934 ieee80211_remain_on_channel_expired(sc->hw);
935 break;
936 case ATH_ROC_COMPLETE_CANCEL:
937 ath_dbg(common, CHAN_CTX, "RoC canceled\n");
938 break;
939 }
940
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530941 ath_offchannel_next(sc);
942 ath9k_ps_restore(sc);
943}
944
945void ath_scan_complete(struct ath_softc *sc, bool abort)
946{
947 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
948
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530949 if (abort)
950 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
951 else
952 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
953
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530954 sc->offchannel.scan_req = NULL;
955 sc->offchannel.scan_vif = NULL;
956 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
957 ieee80211_scan_completed(sc->hw, abort);
958 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530959 spin_lock_bh(&sc->chan_lock);
960 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
961 sc->sched.force_noa_update = true;
962 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530963 ath_offchannel_next(sc);
964 ath9k_ps_restore(sc);
965}
966
967static void ath_scan_send_probe(struct ath_softc *sc,
968 struct cfg80211_ssid *ssid)
969{
970 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
971 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
972 struct ath_tx_control txctl = {};
973 struct sk_buff *skb;
974 struct ieee80211_tx_info *info;
975 int band = sc->offchannel.chan.chandef.chan->band;
976
Johannes Berga344d672014-06-12 22:24:31 +0200977 skb = ieee80211_probereq_get(sc->hw, vif->addr,
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530978 ssid->ssid, ssid->ssid_len, req->ie_len);
979 if (!skb)
980 return;
981
982 info = IEEE80211_SKB_CB(skb);
983 if (req->no_cck)
984 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
985
986 if (req->ie_len)
987 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
988
989 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
990
991 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
992 goto error;
993
994 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
995 txctl.force_channel = true;
996 if (ath_tx_start(sc->hw, skb, &txctl))
997 goto error;
998
999 return;
1000
1001error:
1002 ieee80211_free_txskb(sc->hw, skb);
1003}
1004
1005static void ath_scan_channel_start(struct ath_softc *sc)
1006{
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301007 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301008 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1009 int i;
1010
1011 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1012 req->n_ssids) {
1013 for (i = 0; i < req->n_ssids; i++)
1014 ath_scan_send_probe(sc, &req->ssids[i]);
1015
1016 }
1017
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301018 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +05301019 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301020
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301021 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1022 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1023}
1024
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301025static void ath_chanctx_timer(unsigned long data)
1026{
1027 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +05301028 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1029
1030 ath_dbg(common, CHAN_CTX,
1031 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301032
1033 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1034}
1035
1036static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301037{
1038 struct ath_softc *sc = (struct ath_softc *)data;
1039 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301040 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1041
Sujith Manoharan878066e2014-08-27 12:07:24 +05301042 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301043 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301044
1045 switch (sc->offchannel.state) {
1046 case ATH_OFFCHANNEL_PROBE_WAIT:
1047 if (!sc->offchannel.scan_req)
1048 return;
1049
1050 /* get first active channel context */
1051 ctx = ath_chanctx_get_oper_chan(sc, true);
1052 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301053 ath_dbg(common, CHAN_CTX,
1054 "Switch to oper/active context, "
1055 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1056
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301057 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1058 ath_chanctx_switch(sc, ctx, NULL);
1059 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1060 break;
1061 }
1062 /* fall through */
1063 case ATH_OFFCHANNEL_SUSPEND:
1064 if (!sc->offchannel.scan_req)
1065 return;
1066
1067 ath_scan_next_channel(sc);
1068 break;
1069 case ATH_OFFCHANNEL_ROC_START:
1070 case ATH_OFFCHANNEL_ROC_WAIT:
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301071 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
Janusz.Dziedzic@tieto.comd83520b2015-07-21 11:11:41 +02001072 ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301073 break;
1074 default:
1075 break;
1076 }
1077}
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301078
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301079static bool
1080ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1081 bool powersave)
1082{
1083 struct ieee80211_vif *vif = avp->vif;
1084 struct ieee80211_sta *sta = NULL;
1085 struct ieee80211_hdr_3addr *nullfunc;
1086 struct ath_tx_control txctl;
1087 struct sk_buff *skb;
1088 int band = sc->cur_chan->chandef.chan->band;
1089
1090 switch (vif->type) {
1091 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +05301092 if (!avp->assoc)
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301093 return false;
1094
1095 skb = ieee80211_nullfunc_get(sc->hw, vif);
1096 if (!skb)
1097 return false;
1098
1099 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1100 if (powersave)
1101 nullfunc->frame_control |=
1102 cpu_to_le16(IEEE80211_FCTL_PM);
1103
1104 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1105 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1106 dev_kfree_skb_any(skb);
1107 return false;
1108 }
1109 break;
1110 default:
1111 return false;
1112 }
1113
1114 memset(&txctl, 0, sizeof(txctl));
1115 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1116 txctl.sta = sta;
1117 txctl.force_channel = true;
1118 if (ath_tx_start(sc->hw, skb, &txctl)) {
1119 ieee80211_free_txskb(sc->hw, skb);
1120 return false;
1121 }
1122
1123 return true;
1124}
1125
1126static bool
1127ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1128{
1129 struct ath_vif *avp;
1130 bool sent = false;
1131
1132 rcu_read_lock();
1133 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1134 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1135 sent = true;
1136 }
1137 rcu_read_unlock();
1138
1139 return sent;
1140}
1141
1142static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1143{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301144 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1145
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301146 if (sc->cur_chan == &sc->offchannel.chan)
1147 return false;
1148
1149 switch (sc->sched.state) {
1150 case ATH_CHANCTX_STATE_SWITCH:
1151 return false;
1152 case ATH_CHANCTX_STATE_IDLE:
1153 if (!sc->cur_chan->switch_after_beacon)
1154 return false;
1155
Sujith Manoharan878066e2014-08-27 12:07:24 +05301156 ath_dbg(common, CHAN_CTX,
1157 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1158
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301159 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1160 break;
1161 default:
1162 break;
1163 }
1164
1165 return true;
1166}
1167
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301168static void ath_offchannel_channel_change(struct ath_softc *sc)
1169{
1170 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1171
Sujith Manoharan878066e2014-08-27 12:07:24 +05301172 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301173 __func__, offchannel_state_string(sc->offchannel.state));
1174
1175 switch (sc->offchannel.state) {
1176 case ATH_OFFCHANNEL_PROBE_SEND:
1177 if (!sc->offchannel.scan_req)
1178 return;
1179
1180 if (sc->cur_chan->chandef.chan !=
1181 sc->offchannel.chan.chandef.chan)
1182 return;
1183
1184 ath_scan_channel_start(sc);
1185 break;
1186 case ATH_OFFCHANNEL_IDLE:
1187 if (!sc->offchannel.scan_req)
1188 return;
1189
1190 ath_scan_complete(sc, false);
1191 break;
1192 case ATH_OFFCHANNEL_ROC_START:
1193 if (sc->cur_chan != &sc->offchannel.chan)
1194 break;
1195
1196 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301197 mod_timer(&sc->offchannel.timer,
1198 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301199 ieee80211_ready_on_channel(sc->hw);
1200 break;
1201 case ATH_OFFCHANNEL_ROC_DONE:
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301202 break;
1203 default:
1204 break;
1205 }
1206}
1207
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301208void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1209{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301210 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301211 struct ath_chanctx *old_ctx;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301212 struct timespec ts;
1213 bool measure_time = false;
1214 bool send_ps = false;
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301215 bool queues_stopped = false;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301216
1217 spin_lock_bh(&sc->chan_lock);
1218 if (!sc->next_chan) {
1219 spin_unlock_bh(&sc->chan_lock);
1220 return;
1221 }
1222
1223 if (!force && ath_chanctx_defer_switch(sc)) {
1224 spin_unlock_bh(&sc->chan_lock);
1225 return;
1226 }
1227
Sujith Manoharan878066e2014-08-27 12:07:24 +05301228 ath_dbg(common, CHAN_CTX,
1229 "%s: current: %d MHz, next: %d MHz\n",
1230 __func__,
1231 sc->cur_chan->chandef.center_freq1,
1232 sc->next_chan->chandef.center_freq1);
1233
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301234 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301235 ath_dbg(common, CHAN_CTX,
1236 "Stopping current chanctx: %d\n",
1237 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301238 sc->cur_chan->stopped = true;
1239 spin_unlock_bh(&sc->chan_lock);
1240
1241 if (sc->next_chan == &sc->offchannel.chan) {
1242 getrawmonotonic(&ts);
1243 measure_time = true;
1244 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301245
1246 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1247 queues_stopped = true;
1248
Sujith Manoharan25f3bc72014-10-17 07:40:29 +05301249 __ath9k_flush(sc->hw, ~0, true, false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301250
1251 if (ath_chanctx_send_ps_frame(sc, true))
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301252 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
Sujith Manoharan25f3bc72014-10-17 07:40:29 +05301253 false, false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301254
1255 send_ps = true;
1256 spin_lock_bh(&sc->chan_lock);
1257
1258 if (sc->cur_chan != &sc->offchannel.chan) {
1259 getrawmonotonic(&sc->cur_chan->tsf_ts);
1260 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1261 }
1262 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301263 old_ctx = sc->cur_chan;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301264 sc->cur_chan = sc->next_chan;
1265 sc->cur_chan->stopped = false;
1266 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301267
1268 if (!sc->sched.offchannel_pending)
1269 sc->sched.offchannel_duration = 0;
1270
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301271 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1272 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1273
1274 spin_unlock_bh(&sc->chan_lock);
1275
1276 if (sc->sc_ah->chip_fullsleep ||
1277 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1278 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301279 ath_dbg(common, CHAN_CTX,
1280 "%s: Set channel %d MHz\n",
1281 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301282 ath_set_channel(sc);
1283 if (measure_time)
1284 sc->sched.channel_switch_time =
1285 ath9k_hw_get_tsf_offset(&ts, NULL);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301286 /*
1287 * A reset will ensure that all queues are woken up,
1288 * so there is no need to awaken them again.
1289 */
1290 goto out;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301291 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301292
1293 if (queues_stopped)
1294 ath9k_chanctx_wake_queues(sc, old_ctx);
1295out:
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301296 if (send_ps)
1297 ath_chanctx_send_ps_frame(sc, false);
1298
1299 ath_offchannel_channel_change(sc);
1300 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1301}
1302
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301303static void ath_chanctx_work(struct work_struct *work)
1304{
1305 struct ath_softc *sc = container_of(work, struct ath_softc,
1306 chanctx_work);
1307 mutex_lock(&sc->mutex);
1308 ath_chanctx_set_next(sc, false);
1309 mutex_unlock(&sc->mutex);
1310}
1311
Sujith Manoharane90e3022014-08-23 13:29:20 +05301312void ath9k_offchannel_init(struct ath_softc *sc)
1313{
1314 struct ath_chanctx *ctx;
1315 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1316 struct ieee80211_supported_band *sband;
1317 struct ieee80211_channel *chan;
1318 int i;
1319
1320 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1321 if (!sband->n_channels)
1322 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1323
1324 chan = &sband->channels[0];
1325
1326 ctx = &sc->offchannel.chan;
1327 INIT_LIST_HEAD(&ctx->vifs);
1328 ctx->txpower = ATH_TXPOWER_MAX;
1329 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1330
1331 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1332 INIT_LIST_HEAD(&ctx->acq[i]);
1333
1334 sc->offchannel.chan.offchannel = true;
1335}
1336
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301337void ath9k_init_channel_context(struct ath_softc *sc)
1338{
1339 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1340
1341 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1342 (unsigned long)sc);
1343 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1344 (unsigned long)sc);
Sujith Manoharanc6500ea2014-10-17 07:40:22 +05301345
1346 init_completion(&sc->go_beacon);
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301347}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301348
Sujith Manoharanea22df22014-08-23 13:29:07 +05301349void ath9k_deinit_channel_context(struct ath_softc *sc)
1350{
1351 cancel_work_sync(&sc->chanctx_work);
1352}
1353
Sujith Manoharan499afac2014-08-22 20:39:31 +05301354bool ath9k_is_chanctx_enabled(void)
1355{
1356 return (ath9k_use_chanctx == 1);
1357}
1358
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301359/********************/
1360/* Queue management */
1361/********************/
1362
Sujith Manoharana064eaa2014-10-02 06:33:18 +05301363void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1364{
1365 struct ath_hw *ah = sc->sc_ah;
1366 int i;
1367
1368 if (ctx == &sc->offchannel.chan) {
1369 ieee80211_stop_queue(sc->hw,
1370 sc->hw->offchannel_tx_hw_queue);
1371 } else {
1372 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1373 ieee80211_stop_queue(sc->hw,
1374 ctx->hw_queue_base + i);
1375 }
1376
1377 if (ah->opmode == NL80211_IFTYPE_AP)
1378 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1379}
1380
1381
Sujith Manoharanb3903152014-10-02 06:33:17 +05301382void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301383{
1384 struct ath_hw *ah = sc->sc_ah;
1385 int i;
1386
Sujith Manoharanb3903152014-10-02 06:33:17 +05301387 if (ctx == &sc->offchannel.chan) {
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301388 ieee80211_wake_queue(sc->hw,
1389 sc->hw->offchannel_tx_hw_queue);
1390 } else {
1391 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1392 ieee80211_wake_queue(sc->hw,
Sujith Manoharanb3903152014-10-02 06:33:17 +05301393 ctx->hw_queue_base + i);
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301394 }
1395
1396 if (ah->opmode == NL80211_IFTYPE_AP)
1397 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1398}
1399
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301400/*****************/
1401/* P2P Powersave */
1402/*****************/
1403
1404static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301405{
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +01001406 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301407 struct ath_hw *ah = sc->sc_ah;
1408 s32 tsf, target_tsf;
1409
1410 if (!avp || !avp->noa.has_next_tsf)
1411 return;
1412
1413 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1414
1415 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1416
1417 target_tsf = avp->noa.next_tsf;
1418 if (!avp->noa.absent)
1419 target_tsf -= ATH_P2P_PS_STOP_TIME;
1420
1421 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1422 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1423
Janusz Dziedzic58bb9ca842015-11-27 09:37:06 +01001424 ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1425 __func__, avp->noa.absent, tsf, target_tsf,
1426 (target_tsf - tsf) / 1000);
1427
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301428 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1429}
1430
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301431static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1432{
1433 struct ath_vif *avp = (void *)vif->drv_priv;
1434 u32 tsf;
1435
1436 if (!sc->p2p_ps_timer)
1437 return;
1438
1439 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1440 return;
1441
1442 sc->p2p_ps_vif = avp;
1443 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1444 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1445 ath9k_update_p2p_ps_timer(sc, avp);
1446}
1447
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301448static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1449{
1450 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1451 u8 switch_time, ctwin;
1452
1453 /*
1454 * Channel switch in multi-channel mode is deferred
1455 * by a quarter beacon interval when handling
1456 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1457 * interface is guaranteed to be discoverable
1458 * for that duration after a TBTT.
1459 */
1460 switch_time = cur_conf->beacon_interval / 4;
1461
1462 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1463 if (ctwin && (ctwin < switch_time))
1464 return ctwin;
1465
1466 if (switch_time < P2P_DEFAULT_CTWIN)
1467 return 0;
1468
1469 return P2P_DEFAULT_CTWIN;
1470}
1471
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301472void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1473 struct sk_buff *skb)
1474{
1475 static const u8 noa_ie_hdr[] = {
1476 WLAN_EID_VENDOR_SPECIFIC, /* type */
1477 0, /* length */
1478 0x50, 0x6f, 0x9a, /* WFA OUI */
1479 0x09, /* P2P subtype */
1480 0x0c, /* Notice of Absence */
1481 0x00, /* LSB of little-endian len */
1482 0x00, /* MSB of little-endian len */
1483 };
1484
1485 struct ieee80211_p2p_noa_attr *noa;
1486 int noa_len, noa_desc, i = 0;
1487 u8 *hdr;
1488
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301489 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301490 return;
1491
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301492 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301493 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1494
1495 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1496 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1497 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1498 hdr[7] = noa_len;
1499
1500 noa = (void *) skb_put(skb, noa_len);
1501 memset(noa, 0, noa_len);
1502
1503 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301504 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1505
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301506 if (avp->noa_duration) {
1507 if (avp->periodic_noa) {
1508 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1509 noa->desc[i].count = 255;
1510 noa->desc[i].interval = cpu_to_le32(interval);
1511 } else {
1512 noa->desc[i].count = 1;
1513 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301514
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301515 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1516 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301517 i++;
1518 }
1519
1520 if (avp->offchannel_duration) {
1521 noa->desc[i].count = 1;
1522 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1523 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1524 }
1525}
1526
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301527void ath9k_p2p_ps_timer(void *priv)
1528{
1529 struct ath_softc *sc = priv;
1530 struct ath_vif *avp = sc->p2p_ps_vif;
1531 struct ieee80211_vif *vif;
1532 struct ieee80211_sta *sta;
1533 struct ath_node *an;
1534 u32 tsf;
1535
1536 del_timer_sync(&sc->sched.timer);
1537 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1538 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1539
1540 if (!avp || avp->chanctx != sc->cur_chan)
1541 return;
1542
1543 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1544 if (!avp->noa.absent)
1545 tsf += ATH_P2P_PS_STOP_TIME;
1546
1547 if (!avp->noa.has_next_tsf ||
1548 avp->noa.next_tsf - tsf > BIT(31))
1549 ieee80211_update_p2p_noa(&avp->noa, tsf);
1550
1551 ath9k_update_p2p_ps_timer(sc, avp);
1552
1553 rcu_read_lock();
1554
1555 vif = avp->vif;
Sujith Manoharancb355822014-09-17 14:45:56 +05301556 sta = ieee80211_find_sta(vif, avp->bssid);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301557 if (!sta)
1558 goto out;
1559
1560 an = (void *) sta->drv_priv;
1561 if (an->sleeping == !!avp->noa.absent)
1562 goto out;
1563
1564 an->sleeping = avp->noa.absent;
1565 if (an->sleeping)
1566 ath_tx_aggr_sleep(sta, sc, an);
1567 else
1568 ath_tx_aggr_wakeup(sc, an);
1569
1570out:
1571 rcu_read_unlock();
1572}
1573
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301574void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1575 struct ieee80211_vif *vif)
1576{
1577 unsigned long flags;
1578
1579 spin_lock_bh(&sc->sc_pcu_lock);
1580 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1581 if (!(sc->ps_flags & PS_BEACON_SYNC))
1582 ath9k_update_p2p_ps(sc, vif);
1583 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1584 spin_unlock_bh(&sc->sc_pcu_lock);
1585}
1586
1587void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1588{
1589 if (sc->p2p_ps_vif)
1590 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1591}
1592
1593void ath9k_p2p_remove_vif(struct ath_softc *sc,
1594 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301595{
1596 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301597
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301598 spin_lock_bh(&sc->sc_pcu_lock);
1599 if (avp == sc->p2p_ps_vif) {
1600 sc->p2p_ps_vif = NULL;
1601 ath9k_update_p2p_ps_timer(sc, NULL);
1602 }
1603 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301604}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301605
1606int ath9k_init_p2p(struct ath_softc *sc)
1607{
1608 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1609 NULL, sc, AR_FIRST_NDP_TIMER);
1610 if (!sc->p2p_ps_timer)
1611 return -ENOMEM;
1612
1613 return 0;
1614}
1615
1616void ath9k_deinit_p2p(struct ath_softc *sc)
1617{
1618 if (sc->p2p_ps_timer)
1619 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1620}
1621
1622#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */