blob: 366a4d9098e7bf7a5a692f95dd97ec49fa1587fa [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) &&
95 sc->spectral_mode == SPECTRAL_CHANSCAN)
96 ath9k_spectral_scan_trigger(hw);
97 }
98
99 return 0;
100}
101
102void ath_chanctx_init(struct ath_softc *sc)
103{
104 struct ath_chanctx *ctx;
105 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
106 struct ieee80211_supported_band *sband;
107 struct ieee80211_channel *chan;
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;
359
360 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
361 tsf_time -= ath9k_hw_gettsf32(ah);
362 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
Sujith Manoharan1a7c5b72014-08-27 12:07:25 +0530363 mod_timer(&sc->sched.timer, jiffies + tsf_time);
Sujith Manoharan878066e2014-08-27 12:07:24 +0530364
365 ath_dbg(common, CHAN_CTX,
366 "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time));
Felix Fietkau42eda112014-06-11 16:18:14 +0530367}
368
Sujith Manoharan828fe012014-10-17 07:40:25 +0530369static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
370 struct ath_chanctx *ctx,
371 struct ath_vif *avp)
372{
373 /*
374 * Clear the extend_absence flag if it had been
375 * set during the previous beacon transmission,
376 * since we need to revert to the normal NoA
377 * schedule.
378 */
379 if (ctx->active && sc->sched.extend_absence) {
380 avp->noa_duration = 0;
381 sc->sched.extend_absence = false;
382 }
383
384 /* If at least two consecutive beacons were missed on the STA
385 * chanctx, stay on the STA channel for one extra beacon period,
386 * to resync the timer properly.
387 */
388 if (ctx->active && sc->sched.beacon_miss >= 2) {
389 avp->noa_duration = 0;
390 sc->sched.extend_absence = true;
391 }
392}
393
Sujith Manoharana23152a2014-10-17 07:40:23 +0530394static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
395 struct ath_chanctx *ctx,
396 struct ath_vif *avp,
397 u32 tsf_time)
398{
399 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
400
401 avp->noa_index++;
402 avp->offchannel_start = tsf_time;
403 avp->offchannel_duration = sc->sched.offchannel_duration;
404
405 ath_dbg(common, CHAN_CTX,
406 "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n",
407 avp->offchannel_duration,
408 avp->offchannel_start,
409 avp->noa_index);
410
411 /*
412 * When multiple contexts are active, the NoA
413 * has to be recalculated and advertised after
414 * an offchannel operation.
415 */
416 if (ctx->active && avp->noa_duration)
417 avp->noa_duration = 0;
418}
419
Sujith Manoharan347a9562014-10-17 07:40:24 +0530420static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
421 struct ath_vif *avp,
422 struct ath_beacon_config *cur_conf,
423 u32 tsf_time,
424 u32 beacon_int)
425{
426 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
427
428 avp->noa_index++;
429 avp->noa_start = tsf_time;
430
431 if (sc->sched.extend_absence)
432 avp->noa_duration = (3 * beacon_int / 2) +
433 sc->sched.channel_switch_time;
434 else
435 avp->noa_duration =
436 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
437 sc->sched.channel_switch_time;
438
439 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
440 sc->sched.extend_absence)
441 avp->periodic_noa = false;
442 else
443 avp->periodic_noa = true;
444
445 ath_dbg(common, CHAN_CTX,
446 "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
447 avp->noa_duration,
448 avp->noa_start,
449 avp->noa_index,
450 avp->periodic_noa);
451}
452
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530453static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
454 struct ath_vif *avp,
455 u32 tsf_time,
456 u32 duration)
457{
458 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
459
460 avp->noa_index++;
461 avp->noa_start = tsf_time;
462 avp->periodic_noa = false;
463 avp->oneshot_noa = true;
464 avp->noa_duration = duration + sc->sched.channel_switch_time;
465
466 ath_dbg(common, CHAN_CTX,
467 "oneshot noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
468 avp->noa_duration,
469 avp->noa_start,
470 avp->noa_index,
471 avp->periodic_noa);
472}
473
Felix Fietkau748299f2014-06-11 16:18:04 +0530474void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
475 enum ath_chanctx_event ev)
476{
477 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530478 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530479 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530480 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530481 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530482 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530483 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530484
485 if (vif)
486 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530487
488 spin_lock_bh(&sc->chan_lock);
489
Sujith Manoharan878066e2014-08-27 12:07:24 +0530490 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
491 sc->cur_chan->chandef.center_freq1,
492 chanctx_event_string(ev),
493 chanctx_state_string(sc->sched.state));
494
Felix Fietkau748299f2014-06-11 16:18:04 +0530495 switch (ev) {
496 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530497 if (avp->offchannel_duration)
498 avp->offchannel_duration = 0;
499
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530500 if (avp->oneshot_noa) {
501 avp->noa_duration = 0;
502 avp->oneshot_noa = false;
503
504 ath_dbg(common, CHAN_CTX,
505 "Clearing oneshot NoA\n");
506 }
507
Sujith Manoharan878066e2014-08-27 12:07:24 +0530508 if (avp->chanctx != sc->cur_chan) {
509 ath_dbg(common, CHAN_CTX,
510 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530511 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530512 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530513
Sujith Manoharan367b3412014-09-05 09:50:57 +0530514 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530515 sc->sched.offchannel_pending = false;
516 sc->next_chan = &sc->offchannel.chan;
517 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530518 ath_dbg(common, CHAN_CTX,
519 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530520 }
521
522 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
523 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
524 sc->next_chan = ctx;
525 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530526 ath_dbg(common, CHAN_CTX,
527 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530528 }
529
530 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530531 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530532 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530533 ath_dbg(common, CHAN_CTX,
534 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
535 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530536
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530537 if (sc->sched.mgd_prepare_tx)
538 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
539
Sujith Manoharan8890d052014-10-17 07:40:14 +0530540 /*
541 * When a context becomes inactive, for example,
542 * disassociation of a station context, the NoA
543 * attribute needs to be removed from subsequent
544 * beacons.
545 */
546 if (!ctx->active && avp->noa_duration &&
547 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
548 avp->noa_duration = 0;
549 avp->periodic_noa = false;
550
551 ath_dbg(common, CHAN_CTX,
552 "Clearing NoA schedule\n");
553 }
554
Felix Fietkau748299f2014-06-11 16:18:04 +0530555 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
556 break;
557
Sujith Manoharan878066e2014-08-27 12:07:24 +0530558 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
559
Felix Fietkau748299f2014-06-11 16:18:04 +0530560 sc->sched.beacon_pending = true;
561 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530562
Felix Fietkau74148632014-06-11 16:18:11 +0530563 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530564 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
565
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530566 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530567 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530568 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530569 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530570
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530571 /*
572 * If an offchannel switch is scheduled to happen after
573 * a beacon transmission, update the NoA with one-shot
574 * values and increment the index.
575 */
576 if (sc->next_chan == &sc->offchannel.chan) {
Sujith Manoharana23152a2014-10-17 07:40:23 +0530577 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530578 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530579 }
580
Sujith Manoharan828fe012014-10-17 07:40:25 +0530581 ath_chanctx_handle_bmiss(sc, ctx, avp);
Sujith Manoharana2b28602014-09-15 11:25:50 +0530582
Sujith Manoharan0a019a52014-10-17 07:40:27 +0530583 /*
584 * If a mgd_prepare_tx() has been called by mac80211,
585 * a one-shot NoA needs to be sent. This can happen
586 * with one or more active channel contexts - in both
587 * cases, a new NoA schedule has to be advertised.
588 */
589 if (sc->sched.mgd_prepare_tx) {
590 ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
591 jiffies_to_usecs(HZ / 5));
592 break;
593 }
594
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530595 /* Prevent wrap-around issues */
596 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
597 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530598
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530599 /*
600 * If multiple contexts are active, start periodic
601 * NoA and increment the index for the first
602 * announcement.
603 */
604 if (ctx->active &&
Sujith Manoharan347a9562014-10-17 07:40:24 +0530605 (!avp->noa_duration || sc->sched.force_noa_update))
606 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
607 tsf_time, beacon_int);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530608
609 if (ctx->active && sc->sched.force_noa_update)
610 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530611
Felix Fietkau748299f2014-06-11 16:18:04 +0530612 break;
613 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530614 if (!sc->sched.beacon_pending) {
615 ath_dbg(common, CHAN_CTX,
616 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530617 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530618 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530619
620 sc->sched.beacon_pending = false;
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530621
622 if (sc->sched.mgd_prepare_tx) {
623 sc->sched.mgd_prepare_tx = false;
624 complete(&sc->go_beacon);
625 ath_dbg(common, CHAN_CTX,
626 "Beacon sent, complete go_beacon\n");
627 break;
628 }
629
Felix Fietkau748299f2014-06-11 16:18:04 +0530630 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
631 break;
632
Sujith Manoharan878066e2014-08-27 12:07:24 +0530633 ath_dbg(common, CHAN_CTX,
634 "Move chanctx state to WAIT_FOR_TIMER\n");
635
Felix Fietkau748299f2014-06-11 16:18:04 +0530636 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530637 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530638 break;
639 case ATH_CHANCTX_EVENT_TSF_TIMER:
640 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
641 break;
642
Felix Fietkauec70abe2014-06-11 16:18:12 +0530643 if (!sc->cur_chan->switch_after_beacon &&
644 sc->sched.beacon_pending)
645 sc->sched.beacon_miss++;
646
Sujith Manoharan878066e2014-08-27 12:07:24 +0530647 ath_dbg(common, CHAN_CTX,
648 "Move chanctx state to SWITCH\n");
649
Felix Fietkau748299f2014-06-11 16:18:04 +0530650 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
651 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
652 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530653 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530654 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
655 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530656 break;
657
Felix Fietkauec70abe2014-06-11 16:18:12 +0530658 sc->sched.beacon_pending = false;
659 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530660
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530661 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
662 !sc->cur_chan->tsf_val)
663 break;
664
665 ath_chanctx_adjust_tbtt_delta(sc);
666
Felix Fietkaua899b672014-06-11 16:18:13 +0530667 /* TSF time might have been updated by the incoming beacon,
668 * need update the channel switch timer to reflect the change.
669 */
670 tsf_time = sc->sched.switch_start_time;
671 tsf_time -= (u32) sc->cur_chan->tsf_val +
672 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
673 tsf_time += ath9k_hw_gettsf32(ah);
674
Felix Fietkau42eda112014-06-11 16:18:14 +0530675
676 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530677 break;
Sujith Manoharanb8f92792014-10-17 07:40:09 +0530678 case ATH_CHANCTX_EVENT_AUTHORIZED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530679 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
680 avp->chanctx != sc->cur_chan)
681 break;
682
Sujith Manoharan878066e2014-08-27 12:07:24 +0530683 ath_dbg(common, CHAN_CTX,
684 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
685
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530686 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
687 /* fall through */
688 case ATH_CHANCTX_EVENT_SWITCH:
689 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
690 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
691 sc->cur_chan->switch_after_beacon ||
692 sc->cur_chan == &sc->offchannel.chan)
693 break;
694
695 /* If this is a station chanctx, stay active for a half
696 * beacon period (minus channel switch time)
697 */
698 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530699 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530700
Sujith Manoharan878066e2014-08-27 12:07:24 +0530701 ath_dbg(common, CHAN_CTX,
702 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
703
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530704 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530705 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530706
707 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530708
709 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530710 sc->sched.beacon_miss = 0;
711 tsf_time *= 3;
712 }
713
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530714 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530715 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530716 sc->sched.switch_start_time = tsf_time;
717
Felix Fietkau42eda112014-06-11 16:18:14 +0530718 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530719 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530720 break;
721 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
722 if (sc->cur_chan == &sc->offchannel.chan ||
723 sc->cur_chan->switch_after_beacon)
724 break;
725
726 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
727 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
728 break;
729 case ATH_CHANCTX_EVENT_UNASSIGN:
730 if (sc->cur_chan->assigned) {
731 if (sc->next_chan && !sc->next_chan->assigned &&
732 sc->next_chan != &sc->offchannel.chan)
733 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
734 break;
735 }
736
737 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
738 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
739 if (!ctx->assigned)
740 break;
741
742 sc->next_chan = ctx;
743 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
744 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530745 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530746 /*
747 * When adding a new channel context, check if a scan
748 * is in progress and abort it since the addition of
749 * a new channel context is usually followed by VIF
750 * assignment, in which case we have to start multi-channel
751 * operation.
752 */
753 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
754 ath_dbg(common, CHAN_CTX,
755 "Aborting HW scan to add new context\n");
756
757 spin_unlock_bh(&sc->chan_lock);
758 del_timer_sync(&sc->offchannel.timer);
759 ath_scan_complete(sc, true);
760 spin_lock_bh(&sc->chan_lock);
761 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530762 break;
763 case ATH_CHANCTX_EVENT_CHANGE:
764 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530765 }
766
767 spin_unlock_bh(&sc->chan_lock);
768}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530769
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530770void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
771 enum ath_chanctx_event ev)
772{
773 if (sc->sched.beacon_pending)
774 ath_chanctx_event(sc, NULL, ev);
775}
776
Sujith Manoharana2b28602014-09-15 11:25:50 +0530777void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530778 enum ath_chanctx_event ev)
779{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530780 ath_chanctx_event(sc, NULL, ev);
781}
782
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530783static int ath_scan_channel_duration(struct ath_softc *sc,
784 struct ieee80211_channel *chan)
785{
786 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
787
788 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
789 return (HZ / 9); /* ~110 ms */
790
791 return (HZ / 16); /* ~60 ms */
792}
793
Sujith Manoharan922c9432014-08-23 13:29:15 +0530794static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
795 struct cfg80211_chan_def *chandef)
796{
797 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
798
799 spin_lock_bh(&sc->chan_lock);
800
801 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
802 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530803 if (chandef)
804 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530805
806 sc->sched.offchannel_pending = true;
807 sc->sched.wait_switch = true;
808 sc->sched.offchannel_duration =
809 jiffies_to_usecs(sc->offchannel.duration) +
810 sc->sched.channel_switch_time;
811
Sujith Manoharan922c9432014-08-23 13:29:15 +0530812 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530813 ath_dbg(common, CHAN_CTX,
814 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530815 return;
816 }
817
818 sc->next_chan = ctx;
819 if (chandef) {
820 ctx->chandef = *chandef;
821 ath_dbg(common, CHAN_CTX,
822 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
823 }
824
825 if (sc->next_chan == &sc->offchannel.chan) {
826 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530827 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530828 sc->sched.channel_switch_time;
829
830 if (chandef) {
831 ath_dbg(common, CHAN_CTX,
832 "Offchannel duration for chan %d MHz : %u\n",
833 chandef->center_freq1,
834 sc->sched.offchannel_duration);
835 }
836 }
837 spin_unlock_bh(&sc->chan_lock);
838 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
839}
840
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530841static void ath_chanctx_offchan_switch(struct ath_softc *sc,
842 struct ieee80211_channel *chan)
843{
844 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
845 struct cfg80211_chan_def chandef;
846
847 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
848 ath_dbg(common, CHAN_CTX,
849 "Channel definition created: %d MHz\n", chandef.center_freq1);
850
851 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
852}
853
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530854static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
855 bool active)
856{
857 struct ath_chanctx *ctx;
858
859 ath_for_each_chanctx(sc, ctx) {
860 if (!ctx->assigned || list_empty(&ctx->vifs))
861 continue;
862 if (active && !ctx->active)
863 continue;
864
865 if (ctx->switch_after_beacon)
866 return ctx;
867 }
868
869 return &sc->chanctx[0];
870}
871
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530872static void
873ath_scan_next_channel(struct ath_softc *sc)
874{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530875 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530876 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
877 struct ieee80211_channel *chan;
878
879 if (sc->offchannel.scan_idx >= req->n_channels) {
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_IDLE, "
882 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530883 sc->offchannel.scan_idx,
884 req->n_channels);
885
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530886 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
887 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
888 NULL);
889 return;
890 }
891
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530892 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530893 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530894 sc->offchannel.scan_idx);
895
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530896 chan = req->channels[sc->offchannel.scan_idx++];
897 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
898 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530899
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530900 ath_chanctx_offchan_switch(sc, chan);
901}
902
903void ath_offchannel_next(struct ath_softc *sc)
904{
905 struct ieee80211_vif *vif;
906
907 if (sc->offchannel.scan_req) {
908 vif = sc->offchannel.scan_vif;
909 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
910 ath_scan_next_channel(sc);
911 } else if (sc->offchannel.roc_vif) {
912 vif = sc->offchannel.roc_vif;
913 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530914 sc->offchannel.duration =
915 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530916 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
917 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
918 } else {
919 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
920 NULL);
921 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
922 if (sc->ps_idle)
923 ath_cancel_work(sc);
924 }
925}
926
927void ath_roc_complete(struct ath_softc *sc, bool abort)
928{
Sujith Manoharan4b60af42014-10-02 06:33:12 +0530929 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
930
931 if (abort)
932 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
933 else
934 ath_dbg(common, CHAN_CTX, "RoC expired\n");
935
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530936 sc->offchannel.roc_vif = NULL;
937 sc->offchannel.roc_chan = NULL;
Sujith Manoharana3503352014-10-17 07:40:26 +0530938 if (abort)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530939 ieee80211_remain_on_channel_expired(sc->hw);
940 ath_offchannel_next(sc);
941 ath9k_ps_restore(sc);
942}
943
944void ath_scan_complete(struct ath_softc *sc, bool abort)
945{
946 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
947
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530948 if (abort)
949 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
950 else
951 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
952
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530953 sc->offchannel.scan_req = NULL;
954 sc->offchannel.scan_vif = NULL;
955 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
956 ieee80211_scan_completed(sc->hw, abort);
957 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530958 spin_lock_bh(&sc->chan_lock);
959 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
960 sc->sched.force_noa_update = true;
961 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530962 ath_offchannel_next(sc);
963 ath9k_ps_restore(sc);
964}
965
966static void ath_scan_send_probe(struct ath_softc *sc,
967 struct cfg80211_ssid *ssid)
968{
969 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
970 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
971 struct ath_tx_control txctl = {};
972 struct sk_buff *skb;
973 struct ieee80211_tx_info *info;
974 int band = sc->offchannel.chan.chandef.chan->band;
975
976 skb = ieee80211_probereq_get(sc->hw, vif,
977 ssid->ssid, ssid->ssid_len, req->ie_len);
978 if (!skb)
979 return;
980
981 info = IEEE80211_SKB_CB(skb);
982 if (req->no_cck)
983 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
984
985 if (req->ie_len)
986 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
987
988 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
989
990 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
991 goto error;
992
993 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
994 txctl.force_channel = true;
995 if (ath_tx_start(sc->hw, skb, &txctl))
996 goto error;
997
998 return;
999
1000error:
1001 ieee80211_free_txskb(sc->hw, skb);
1002}
1003
1004static void ath_scan_channel_start(struct ath_softc *sc)
1005{
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301006 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301007 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1008 int i;
1009
1010 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1011 req->n_ssids) {
1012 for (i = 0; i < req->n_ssids; i++)
1013 ath_scan_send_probe(sc, &req->ssids[i]);
1014
1015 }
1016
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301017 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +05301018 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301019
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301020 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1021 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1022}
1023
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301024static void ath_chanctx_timer(unsigned long data)
1025{
1026 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +05301027 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1028
1029 ath_dbg(common, CHAN_CTX,
1030 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301031
1032 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1033}
1034
1035static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301036{
1037 struct ath_softc *sc = (struct ath_softc *)data;
1038 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301039 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1040
Sujith Manoharan878066e2014-08-27 12:07:24 +05301041 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301042 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301043
1044 switch (sc->offchannel.state) {
1045 case ATH_OFFCHANNEL_PROBE_WAIT:
1046 if (!sc->offchannel.scan_req)
1047 return;
1048
1049 /* get first active channel context */
1050 ctx = ath_chanctx_get_oper_chan(sc, true);
1051 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301052 ath_dbg(common, CHAN_CTX,
1053 "Switch to oper/active context, "
1054 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1055
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301056 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1057 ath_chanctx_switch(sc, ctx, NULL);
1058 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1059 break;
1060 }
1061 /* fall through */
1062 case ATH_OFFCHANNEL_SUSPEND:
1063 if (!sc->offchannel.scan_req)
1064 return;
1065
1066 ath_scan_next_channel(sc);
1067 break;
1068 case ATH_OFFCHANNEL_ROC_START:
1069 case ATH_OFFCHANNEL_ROC_WAIT:
1070 ctx = ath_chanctx_get_oper_chan(sc, false);
1071 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
Sujith Manoharana3503352014-10-17 07:40:26 +05301072 ieee80211_remain_on_channel_expired(sc->hw);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301073 ath_chanctx_switch(sc, ctx, NULL);
1074 break;
1075 default:
1076 break;
1077 }
1078}
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301079
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301080static bool
1081ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1082 bool powersave)
1083{
1084 struct ieee80211_vif *vif = avp->vif;
1085 struct ieee80211_sta *sta = NULL;
1086 struct ieee80211_hdr_3addr *nullfunc;
1087 struct ath_tx_control txctl;
1088 struct sk_buff *skb;
1089 int band = sc->cur_chan->chandef.chan->band;
1090
1091 switch (vif->type) {
1092 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +05301093 if (!avp->assoc)
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301094 return false;
1095
1096 skb = ieee80211_nullfunc_get(sc->hw, vif);
1097 if (!skb)
1098 return false;
1099
1100 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1101 if (powersave)
1102 nullfunc->frame_control |=
1103 cpu_to_le16(IEEE80211_FCTL_PM);
1104
1105 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1106 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1107 dev_kfree_skb_any(skb);
1108 return false;
1109 }
1110 break;
1111 default:
1112 return false;
1113 }
1114
1115 memset(&txctl, 0, sizeof(txctl));
1116 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1117 txctl.sta = sta;
1118 txctl.force_channel = true;
1119 if (ath_tx_start(sc->hw, skb, &txctl)) {
1120 ieee80211_free_txskb(sc->hw, skb);
1121 return false;
1122 }
1123
1124 return true;
1125}
1126
1127static bool
1128ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1129{
1130 struct ath_vif *avp;
1131 bool sent = false;
1132
1133 rcu_read_lock();
1134 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1135 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1136 sent = true;
1137 }
1138 rcu_read_unlock();
1139
1140 return sent;
1141}
1142
1143static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1144{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301145 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1146
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301147 if (sc->cur_chan == &sc->offchannel.chan)
1148 return false;
1149
1150 switch (sc->sched.state) {
1151 case ATH_CHANCTX_STATE_SWITCH:
1152 return false;
1153 case ATH_CHANCTX_STATE_IDLE:
1154 if (!sc->cur_chan->switch_after_beacon)
1155 return false;
1156
Sujith Manoharan878066e2014-08-27 12:07:24 +05301157 ath_dbg(common, CHAN_CTX,
1158 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1159
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301160 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1161 break;
1162 default:
1163 break;
1164 }
1165
1166 return true;
1167}
1168
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301169static void ath_offchannel_channel_change(struct ath_softc *sc)
1170{
1171 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1172
Sujith Manoharan878066e2014-08-27 12:07:24 +05301173 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301174 __func__, offchannel_state_string(sc->offchannel.state));
1175
1176 switch (sc->offchannel.state) {
1177 case ATH_OFFCHANNEL_PROBE_SEND:
1178 if (!sc->offchannel.scan_req)
1179 return;
1180
1181 if (sc->cur_chan->chandef.chan !=
1182 sc->offchannel.chan.chandef.chan)
1183 return;
1184
1185 ath_scan_channel_start(sc);
1186 break;
1187 case ATH_OFFCHANNEL_IDLE:
1188 if (!sc->offchannel.scan_req)
1189 return;
1190
1191 ath_scan_complete(sc, false);
1192 break;
1193 case ATH_OFFCHANNEL_ROC_START:
1194 if (sc->cur_chan != &sc->offchannel.chan)
1195 break;
1196
1197 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301198 mod_timer(&sc->offchannel.timer,
1199 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301200 ieee80211_ready_on_channel(sc->hw);
1201 break;
1202 case ATH_OFFCHANNEL_ROC_DONE:
1203 ath_roc_complete(sc, false);
1204 break;
1205 default:
1206 break;
1207 }
1208}
1209
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301210void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1211{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301212 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301213 struct ath_chanctx *old_ctx;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301214 struct timespec ts;
1215 bool measure_time = false;
1216 bool send_ps = false;
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301217 bool queues_stopped = false;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301218
1219 spin_lock_bh(&sc->chan_lock);
1220 if (!sc->next_chan) {
1221 spin_unlock_bh(&sc->chan_lock);
1222 return;
1223 }
1224
1225 if (!force && ath_chanctx_defer_switch(sc)) {
1226 spin_unlock_bh(&sc->chan_lock);
1227 return;
1228 }
1229
Sujith Manoharan878066e2014-08-27 12:07:24 +05301230 ath_dbg(common, CHAN_CTX,
1231 "%s: current: %d MHz, next: %d MHz\n",
1232 __func__,
1233 sc->cur_chan->chandef.center_freq1,
1234 sc->next_chan->chandef.center_freq1);
1235
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301236 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301237 ath_dbg(common, CHAN_CTX,
1238 "Stopping current chanctx: %d\n",
1239 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301240 sc->cur_chan->stopped = true;
1241 spin_unlock_bh(&sc->chan_lock);
1242
1243 if (sc->next_chan == &sc->offchannel.chan) {
1244 getrawmonotonic(&ts);
1245 measure_time = true;
1246 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301247
1248 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1249 queues_stopped = true;
1250
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301251 __ath9k_flush(sc->hw, ~0, true, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301252
1253 if (ath_chanctx_send_ps_frame(sc, true))
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301254 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1255 false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301256
1257 send_ps = true;
1258 spin_lock_bh(&sc->chan_lock);
1259
1260 if (sc->cur_chan != &sc->offchannel.chan) {
1261 getrawmonotonic(&sc->cur_chan->tsf_ts);
1262 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1263 }
1264 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301265 old_ctx = sc->cur_chan;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301266 sc->cur_chan = sc->next_chan;
1267 sc->cur_chan->stopped = false;
1268 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301269
1270 if (!sc->sched.offchannel_pending)
1271 sc->sched.offchannel_duration = 0;
1272
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301273 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1274 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1275
1276 spin_unlock_bh(&sc->chan_lock);
1277
1278 if (sc->sc_ah->chip_fullsleep ||
1279 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1280 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301281 ath_dbg(common, CHAN_CTX,
1282 "%s: Set channel %d MHz\n",
1283 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301284 ath_set_channel(sc);
1285 if (measure_time)
1286 sc->sched.channel_switch_time =
1287 ath9k_hw_get_tsf_offset(&ts, NULL);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301288 /*
1289 * A reset will ensure that all queues are woken up,
1290 * so there is no need to awaken them again.
1291 */
1292 goto out;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301293 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301294
1295 if (queues_stopped)
1296 ath9k_chanctx_wake_queues(sc, old_ctx);
1297out:
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301298 if (send_ps)
1299 ath_chanctx_send_ps_frame(sc, false);
1300
1301 ath_offchannel_channel_change(sc);
1302 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1303}
1304
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301305static void ath_chanctx_work(struct work_struct *work)
1306{
1307 struct ath_softc *sc = container_of(work, struct ath_softc,
1308 chanctx_work);
1309 mutex_lock(&sc->mutex);
1310 ath_chanctx_set_next(sc, false);
1311 mutex_unlock(&sc->mutex);
1312}
1313
Sujith Manoharane90e3022014-08-23 13:29:20 +05301314void ath9k_offchannel_init(struct ath_softc *sc)
1315{
1316 struct ath_chanctx *ctx;
1317 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1318 struct ieee80211_supported_band *sband;
1319 struct ieee80211_channel *chan;
1320 int i;
1321
1322 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1323 if (!sband->n_channels)
1324 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1325
1326 chan = &sband->channels[0];
1327
1328 ctx = &sc->offchannel.chan;
1329 INIT_LIST_HEAD(&ctx->vifs);
1330 ctx->txpower = ATH_TXPOWER_MAX;
1331 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1332
1333 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1334 INIT_LIST_HEAD(&ctx->acq[i]);
1335
1336 sc->offchannel.chan.offchannel = true;
1337}
1338
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301339void ath9k_init_channel_context(struct ath_softc *sc)
1340{
1341 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1342
1343 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1344 (unsigned long)sc);
1345 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1346 (unsigned long)sc);
Sujith Manoharanc6500ea2014-10-17 07:40:22 +05301347
1348 init_completion(&sc->go_beacon);
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301349}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301350
Sujith Manoharanea22df22014-08-23 13:29:07 +05301351void ath9k_deinit_channel_context(struct ath_softc *sc)
1352{
1353 cancel_work_sync(&sc->chanctx_work);
1354}
1355
Sujith Manoharan499afac2014-08-22 20:39:31 +05301356bool ath9k_is_chanctx_enabled(void)
1357{
1358 return (ath9k_use_chanctx == 1);
1359}
1360
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301361/********************/
1362/* Queue management */
1363/********************/
1364
Sujith Manoharana064eaa2014-10-02 06:33:18 +05301365void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1366{
1367 struct ath_hw *ah = sc->sc_ah;
1368 int i;
1369
1370 if (ctx == &sc->offchannel.chan) {
1371 ieee80211_stop_queue(sc->hw,
1372 sc->hw->offchannel_tx_hw_queue);
1373 } else {
1374 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1375 ieee80211_stop_queue(sc->hw,
1376 ctx->hw_queue_base + i);
1377 }
1378
1379 if (ah->opmode == NL80211_IFTYPE_AP)
1380 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1381}
1382
1383
Sujith Manoharanb3903152014-10-02 06:33:17 +05301384void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301385{
1386 struct ath_hw *ah = sc->sc_ah;
1387 int i;
1388
Sujith Manoharanb3903152014-10-02 06:33:17 +05301389 if (ctx == &sc->offchannel.chan) {
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301390 ieee80211_wake_queue(sc->hw,
1391 sc->hw->offchannel_tx_hw_queue);
1392 } else {
1393 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1394 ieee80211_wake_queue(sc->hw,
Sujith Manoharanb3903152014-10-02 06:33:17 +05301395 ctx->hw_queue_base + i);
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301396 }
1397
1398 if (ah->opmode == NL80211_IFTYPE_AP)
1399 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1400}
1401
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301402/*****************/
1403/* P2P Powersave */
1404/*****************/
1405
1406static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301407{
1408 struct ath_hw *ah = sc->sc_ah;
1409 s32 tsf, target_tsf;
1410
1411 if (!avp || !avp->noa.has_next_tsf)
1412 return;
1413
1414 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1415
1416 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1417
1418 target_tsf = avp->noa.next_tsf;
1419 if (!avp->noa.absent)
1420 target_tsf -= ATH_P2P_PS_STOP_TIME;
1421
1422 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1423 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1424
1425 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1426}
1427
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301428static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1429{
1430 struct ath_vif *avp = (void *)vif->drv_priv;
1431 u32 tsf;
1432
1433 if (!sc->p2p_ps_timer)
1434 return;
1435
1436 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1437 return;
1438
1439 sc->p2p_ps_vif = avp;
1440 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1441 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1442 ath9k_update_p2p_ps_timer(sc, avp);
1443}
1444
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301445static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1446{
1447 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1448 u8 switch_time, ctwin;
1449
1450 /*
1451 * Channel switch in multi-channel mode is deferred
1452 * by a quarter beacon interval when handling
1453 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1454 * interface is guaranteed to be discoverable
1455 * for that duration after a TBTT.
1456 */
1457 switch_time = cur_conf->beacon_interval / 4;
1458
1459 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1460 if (ctwin && (ctwin < switch_time))
1461 return ctwin;
1462
1463 if (switch_time < P2P_DEFAULT_CTWIN)
1464 return 0;
1465
1466 return P2P_DEFAULT_CTWIN;
1467}
1468
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301469void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1470 struct sk_buff *skb)
1471{
1472 static const u8 noa_ie_hdr[] = {
1473 WLAN_EID_VENDOR_SPECIFIC, /* type */
1474 0, /* length */
1475 0x50, 0x6f, 0x9a, /* WFA OUI */
1476 0x09, /* P2P subtype */
1477 0x0c, /* Notice of Absence */
1478 0x00, /* LSB of little-endian len */
1479 0x00, /* MSB of little-endian len */
1480 };
1481
1482 struct ieee80211_p2p_noa_attr *noa;
1483 int noa_len, noa_desc, i = 0;
1484 u8 *hdr;
1485
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301486 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301487 return;
1488
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301489 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301490 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1491
1492 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1493 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1494 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1495 hdr[7] = noa_len;
1496
1497 noa = (void *) skb_put(skb, noa_len);
1498 memset(noa, 0, noa_len);
1499
1500 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301501 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1502
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301503 if (avp->noa_duration) {
1504 if (avp->periodic_noa) {
1505 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1506 noa->desc[i].count = 255;
1507 noa->desc[i].interval = cpu_to_le32(interval);
1508 } else {
1509 noa->desc[i].count = 1;
1510 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301511
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301512 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1513 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301514 i++;
1515 }
1516
1517 if (avp->offchannel_duration) {
1518 noa->desc[i].count = 1;
1519 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1520 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1521 }
1522}
1523
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301524void ath9k_p2p_ps_timer(void *priv)
1525{
1526 struct ath_softc *sc = priv;
1527 struct ath_vif *avp = sc->p2p_ps_vif;
1528 struct ieee80211_vif *vif;
1529 struct ieee80211_sta *sta;
1530 struct ath_node *an;
1531 u32 tsf;
1532
1533 del_timer_sync(&sc->sched.timer);
1534 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1535 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1536
1537 if (!avp || avp->chanctx != sc->cur_chan)
1538 return;
1539
1540 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1541 if (!avp->noa.absent)
1542 tsf += ATH_P2P_PS_STOP_TIME;
1543
1544 if (!avp->noa.has_next_tsf ||
1545 avp->noa.next_tsf - tsf > BIT(31))
1546 ieee80211_update_p2p_noa(&avp->noa, tsf);
1547
1548 ath9k_update_p2p_ps_timer(sc, avp);
1549
1550 rcu_read_lock();
1551
1552 vif = avp->vif;
Sujith Manoharancb355822014-09-17 14:45:56 +05301553 sta = ieee80211_find_sta(vif, avp->bssid);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301554 if (!sta)
1555 goto out;
1556
1557 an = (void *) sta->drv_priv;
1558 if (an->sleeping == !!avp->noa.absent)
1559 goto out;
1560
1561 an->sleeping = avp->noa.absent;
1562 if (an->sleeping)
1563 ath_tx_aggr_sleep(sta, sc, an);
1564 else
1565 ath_tx_aggr_wakeup(sc, an);
1566
1567out:
1568 rcu_read_unlock();
1569}
1570
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301571void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1572 struct ieee80211_vif *vif)
1573{
1574 unsigned long flags;
1575
1576 spin_lock_bh(&sc->sc_pcu_lock);
1577 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1578 if (!(sc->ps_flags & PS_BEACON_SYNC))
1579 ath9k_update_p2p_ps(sc, vif);
1580 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1581 spin_unlock_bh(&sc->sc_pcu_lock);
1582}
1583
1584void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1585{
1586 if (sc->p2p_ps_vif)
1587 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1588}
1589
1590void ath9k_p2p_remove_vif(struct ath_softc *sc,
1591 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301592{
1593 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301594
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301595 spin_lock_bh(&sc->sc_pcu_lock);
1596 if (avp == sc->p2p_ps_vif) {
1597 sc->p2p_ps_vif = NULL;
1598 ath9k_update_p2p_ps_timer(sc, NULL);
1599 }
1600 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301601}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301602
1603int ath9k_init_p2p(struct ath_softc *sc)
1604{
1605 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1606 NULL, sc, AR_FIRST_NDP_TIMER);
1607 if (!sc->p2p_ps_timer)
1608 return -ENOMEM;
1609
1610 return 0;
1611}
1612
1613void ath9k_deinit_p2p(struct ath_softc *sc)
1614{
1615 if (sc->p2p_ps_timer)
1616 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1617}
1618
1619#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */