blob: 25a21fa389ff12b907e0c10bed7d48573b8b00a5 [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)
Felix Fietkaufbbcd142014-06-11 16:17:49 +053096 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:
746 break;
747 case ATH_CHANCTX_EVENT_CHANGE:
748 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530749 }
750
751 spin_unlock_bh(&sc->chan_lock);
752}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530753
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530754void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
755 enum ath_chanctx_event ev)
756{
757 if (sc->sched.beacon_pending)
758 ath_chanctx_event(sc, NULL, ev);
759}
760
Sujith Manoharana2b28602014-09-15 11:25:50 +0530761void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530762 enum ath_chanctx_event ev)
763{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530764 ath_chanctx_event(sc, NULL, ev);
765}
766
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530767static int ath_scan_channel_duration(struct ath_softc *sc,
768 struct ieee80211_channel *chan)
769{
770 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
771
772 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
773 return (HZ / 9); /* ~110 ms */
774
775 return (HZ / 16); /* ~60 ms */
776}
777
Sujith Manoharan922c9432014-08-23 13:29:15 +0530778static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
779 struct cfg80211_chan_def *chandef)
780{
781 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
782
783 spin_lock_bh(&sc->chan_lock);
784
785 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
786 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530787 if (chandef)
788 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530789
790 sc->sched.offchannel_pending = true;
791 sc->sched.wait_switch = true;
792 sc->sched.offchannel_duration =
793 jiffies_to_usecs(sc->offchannel.duration) +
794 sc->sched.channel_switch_time;
795
Sujith Manoharan922c9432014-08-23 13:29:15 +0530796 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530797 ath_dbg(common, CHAN_CTX,
798 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530799 return;
800 }
801
802 sc->next_chan = ctx;
803 if (chandef) {
804 ctx->chandef = *chandef;
805 ath_dbg(common, CHAN_CTX,
806 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
807 }
808
809 if (sc->next_chan == &sc->offchannel.chan) {
810 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530811 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530812 sc->sched.channel_switch_time;
813
814 if (chandef) {
815 ath_dbg(common, CHAN_CTX,
816 "Offchannel duration for chan %d MHz : %u\n",
817 chandef->center_freq1,
818 sc->sched.offchannel_duration);
819 }
820 }
821 spin_unlock_bh(&sc->chan_lock);
822 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
823}
824
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530825static void ath_chanctx_offchan_switch(struct ath_softc *sc,
826 struct ieee80211_channel *chan)
827{
828 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
829 struct cfg80211_chan_def chandef;
830
831 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
832 ath_dbg(common, CHAN_CTX,
833 "Channel definition created: %d MHz\n", chandef.center_freq1);
834
835 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
836}
837
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530838static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
839 bool active)
840{
841 struct ath_chanctx *ctx;
842
843 ath_for_each_chanctx(sc, ctx) {
844 if (!ctx->assigned || list_empty(&ctx->vifs))
845 continue;
846 if (active && !ctx->active)
847 continue;
848
849 if (ctx->switch_after_beacon)
850 return ctx;
851 }
852
853 return &sc->chanctx[0];
854}
855
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530856static void
857ath_scan_next_channel(struct ath_softc *sc)
858{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530859 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530860 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
861 struct ieee80211_channel *chan;
862
863 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530864 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530865 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
866 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530867 sc->offchannel.scan_idx,
868 req->n_channels);
869
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530870 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
871 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
872 NULL);
873 return;
874 }
875
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530876 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530877 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530878 sc->offchannel.scan_idx);
879
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530880 chan = req->channels[sc->offchannel.scan_idx++];
881 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
882 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530883
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530884 ath_chanctx_offchan_switch(sc, chan);
885}
886
887void ath_offchannel_next(struct ath_softc *sc)
888{
889 struct ieee80211_vif *vif;
890
891 if (sc->offchannel.scan_req) {
892 vif = sc->offchannel.scan_vif;
893 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
894 ath_scan_next_channel(sc);
895 } else if (sc->offchannel.roc_vif) {
896 vif = sc->offchannel.roc_vif;
897 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530898 sc->offchannel.duration =
899 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530900 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
901 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
902 } else {
903 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
904 NULL);
905 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
906 if (sc->ps_idle)
907 ath_cancel_work(sc);
908 }
909}
910
911void ath_roc_complete(struct ath_softc *sc, bool abort)
912{
Sujith Manoharan4b60af42014-10-02 06:33:12 +0530913 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
914
915 if (abort)
916 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
917 else
918 ath_dbg(common, CHAN_CTX, "RoC expired\n");
919
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530920 sc->offchannel.roc_vif = NULL;
921 sc->offchannel.roc_chan = NULL;
Sujith Manoharana3503352014-10-17 07:40:26 +0530922 if (abort)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530923 ieee80211_remain_on_channel_expired(sc->hw);
924 ath_offchannel_next(sc);
925 ath9k_ps_restore(sc);
926}
927
928void ath_scan_complete(struct ath_softc *sc, bool abort)
929{
930 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
931
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530932 if (abort)
933 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
934 else
935 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
936
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530937 sc->offchannel.scan_req = NULL;
938 sc->offchannel.scan_vif = NULL;
939 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
940 ieee80211_scan_completed(sc->hw, abort);
941 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530942 spin_lock_bh(&sc->chan_lock);
943 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
944 sc->sched.force_noa_update = true;
945 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530946 ath_offchannel_next(sc);
947 ath9k_ps_restore(sc);
948}
949
950static void ath_scan_send_probe(struct ath_softc *sc,
951 struct cfg80211_ssid *ssid)
952{
953 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
954 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
955 struct ath_tx_control txctl = {};
956 struct sk_buff *skb;
957 struct ieee80211_tx_info *info;
958 int band = sc->offchannel.chan.chandef.chan->band;
959
960 skb = ieee80211_probereq_get(sc->hw, vif,
961 ssid->ssid, ssid->ssid_len, req->ie_len);
962 if (!skb)
963 return;
964
965 info = IEEE80211_SKB_CB(skb);
966 if (req->no_cck)
967 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
968
969 if (req->ie_len)
970 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
971
972 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
973
974 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
975 goto error;
976
977 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
978 txctl.force_channel = true;
979 if (ath_tx_start(sc->hw, skb, &txctl))
980 goto error;
981
982 return;
983
984error:
985 ieee80211_free_txskb(sc->hw, skb);
986}
987
988static void ath_scan_channel_start(struct ath_softc *sc)
989{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530990 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530991 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
992 int i;
993
994 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
995 req->n_ssids) {
996 for (i = 0; i < req->n_ssids; i++)
997 ath_scan_send_probe(sc, &req->ssids[i]);
998
999 }
1000
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301001 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +05301002 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301003
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301004 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1005 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1006}
1007
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301008static void ath_chanctx_timer(unsigned long data)
1009{
1010 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +05301011 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1012
1013 ath_dbg(common, CHAN_CTX,
1014 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301015
1016 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1017}
1018
1019static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301020{
1021 struct ath_softc *sc = (struct ath_softc *)data;
1022 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301023 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1024
Sujith Manoharan878066e2014-08-27 12:07:24 +05301025 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301026 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301027
1028 switch (sc->offchannel.state) {
1029 case ATH_OFFCHANNEL_PROBE_WAIT:
1030 if (!sc->offchannel.scan_req)
1031 return;
1032
1033 /* get first active channel context */
1034 ctx = ath_chanctx_get_oper_chan(sc, true);
1035 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301036 ath_dbg(common, CHAN_CTX,
1037 "Switch to oper/active context, "
1038 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1039
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301040 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1041 ath_chanctx_switch(sc, ctx, NULL);
1042 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1043 break;
1044 }
1045 /* fall through */
1046 case ATH_OFFCHANNEL_SUSPEND:
1047 if (!sc->offchannel.scan_req)
1048 return;
1049
1050 ath_scan_next_channel(sc);
1051 break;
1052 case ATH_OFFCHANNEL_ROC_START:
1053 case ATH_OFFCHANNEL_ROC_WAIT:
1054 ctx = ath_chanctx_get_oper_chan(sc, false);
1055 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
Sujith Manoharana3503352014-10-17 07:40:26 +05301056 ieee80211_remain_on_channel_expired(sc->hw);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301057 ath_chanctx_switch(sc, ctx, NULL);
1058 break;
1059 default:
1060 break;
1061 }
1062}
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301063
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301064static bool
1065ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1066 bool powersave)
1067{
1068 struct ieee80211_vif *vif = avp->vif;
1069 struct ieee80211_sta *sta = NULL;
1070 struct ieee80211_hdr_3addr *nullfunc;
1071 struct ath_tx_control txctl;
1072 struct sk_buff *skb;
1073 int band = sc->cur_chan->chandef.chan->band;
1074
1075 switch (vif->type) {
1076 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +05301077 if (!avp->assoc)
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301078 return false;
1079
1080 skb = ieee80211_nullfunc_get(sc->hw, vif);
1081 if (!skb)
1082 return false;
1083
1084 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1085 if (powersave)
1086 nullfunc->frame_control |=
1087 cpu_to_le16(IEEE80211_FCTL_PM);
1088
1089 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1090 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1091 dev_kfree_skb_any(skb);
1092 return false;
1093 }
1094 break;
1095 default:
1096 return false;
1097 }
1098
1099 memset(&txctl, 0, sizeof(txctl));
1100 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1101 txctl.sta = sta;
1102 txctl.force_channel = true;
1103 if (ath_tx_start(sc->hw, skb, &txctl)) {
1104 ieee80211_free_txskb(sc->hw, skb);
1105 return false;
1106 }
1107
1108 return true;
1109}
1110
1111static bool
1112ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1113{
1114 struct ath_vif *avp;
1115 bool sent = false;
1116
1117 rcu_read_lock();
1118 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1119 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1120 sent = true;
1121 }
1122 rcu_read_unlock();
1123
1124 return sent;
1125}
1126
1127static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1128{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301129 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1130
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301131 if (sc->cur_chan == &sc->offchannel.chan)
1132 return false;
1133
1134 switch (sc->sched.state) {
1135 case ATH_CHANCTX_STATE_SWITCH:
1136 return false;
1137 case ATH_CHANCTX_STATE_IDLE:
1138 if (!sc->cur_chan->switch_after_beacon)
1139 return false;
1140
Sujith Manoharan878066e2014-08-27 12:07:24 +05301141 ath_dbg(common, CHAN_CTX,
1142 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1143
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301144 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1145 break;
1146 default:
1147 break;
1148 }
1149
1150 return true;
1151}
1152
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301153static void ath_offchannel_channel_change(struct ath_softc *sc)
1154{
1155 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1156
Sujith Manoharan878066e2014-08-27 12:07:24 +05301157 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301158 __func__, offchannel_state_string(sc->offchannel.state));
1159
1160 switch (sc->offchannel.state) {
1161 case ATH_OFFCHANNEL_PROBE_SEND:
1162 if (!sc->offchannel.scan_req)
1163 return;
1164
1165 if (sc->cur_chan->chandef.chan !=
1166 sc->offchannel.chan.chandef.chan)
1167 return;
1168
1169 ath_scan_channel_start(sc);
1170 break;
1171 case ATH_OFFCHANNEL_IDLE:
1172 if (!sc->offchannel.scan_req)
1173 return;
1174
1175 ath_scan_complete(sc, false);
1176 break;
1177 case ATH_OFFCHANNEL_ROC_START:
1178 if (sc->cur_chan != &sc->offchannel.chan)
1179 break;
1180
1181 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301182 mod_timer(&sc->offchannel.timer,
1183 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301184 ieee80211_ready_on_channel(sc->hw);
1185 break;
1186 case ATH_OFFCHANNEL_ROC_DONE:
1187 ath_roc_complete(sc, false);
1188 break;
1189 default:
1190 break;
1191 }
1192}
1193
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301194void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1195{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301196 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301197 struct ath_chanctx *old_ctx;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301198 struct timespec ts;
1199 bool measure_time = false;
1200 bool send_ps = false;
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301201 bool queues_stopped = false;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301202
1203 spin_lock_bh(&sc->chan_lock);
1204 if (!sc->next_chan) {
1205 spin_unlock_bh(&sc->chan_lock);
1206 return;
1207 }
1208
1209 if (!force && ath_chanctx_defer_switch(sc)) {
1210 spin_unlock_bh(&sc->chan_lock);
1211 return;
1212 }
1213
Sujith Manoharan878066e2014-08-27 12:07:24 +05301214 ath_dbg(common, CHAN_CTX,
1215 "%s: current: %d MHz, next: %d MHz\n",
1216 __func__,
1217 sc->cur_chan->chandef.center_freq1,
1218 sc->next_chan->chandef.center_freq1);
1219
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301220 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301221 ath_dbg(common, CHAN_CTX,
1222 "Stopping current chanctx: %d\n",
1223 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301224 sc->cur_chan->stopped = true;
1225 spin_unlock_bh(&sc->chan_lock);
1226
1227 if (sc->next_chan == &sc->offchannel.chan) {
1228 getrawmonotonic(&ts);
1229 measure_time = true;
1230 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301231
1232 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1233 queues_stopped = true;
1234
Sujith Manoharan25f3bc72014-10-17 07:40:29 +05301235 __ath9k_flush(sc->hw, ~0, true, false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301236
1237 if (ath_chanctx_send_ps_frame(sc, true))
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301238 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
Sujith Manoharan25f3bc72014-10-17 07:40:29 +05301239 false, false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301240
1241 send_ps = true;
1242 spin_lock_bh(&sc->chan_lock);
1243
1244 if (sc->cur_chan != &sc->offchannel.chan) {
1245 getrawmonotonic(&sc->cur_chan->tsf_ts);
1246 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1247 }
1248 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301249 old_ctx = sc->cur_chan;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301250 sc->cur_chan = sc->next_chan;
1251 sc->cur_chan->stopped = false;
1252 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301253
1254 if (!sc->sched.offchannel_pending)
1255 sc->sched.offchannel_duration = 0;
1256
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301257 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1258 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1259
1260 spin_unlock_bh(&sc->chan_lock);
1261
1262 if (sc->sc_ah->chip_fullsleep ||
1263 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1264 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301265 ath_dbg(common, CHAN_CTX,
1266 "%s: Set channel %d MHz\n",
1267 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301268 ath_set_channel(sc);
1269 if (measure_time)
1270 sc->sched.channel_switch_time =
1271 ath9k_hw_get_tsf_offset(&ts, NULL);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301272 /*
1273 * A reset will ensure that all queues are woken up,
1274 * so there is no need to awaken them again.
1275 */
1276 goto out;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301277 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301278
1279 if (queues_stopped)
1280 ath9k_chanctx_wake_queues(sc, old_ctx);
1281out:
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301282 if (send_ps)
1283 ath_chanctx_send_ps_frame(sc, false);
1284
1285 ath_offchannel_channel_change(sc);
1286 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1287}
1288
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301289static void ath_chanctx_work(struct work_struct *work)
1290{
1291 struct ath_softc *sc = container_of(work, struct ath_softc,
1292 chanctx_work);
1293 mutex_lock(&sc->mutex);
1294 ath_chanctx_set_next(sc, false);
1295 mutex_unlock(&sc->mutex);
1296}
1297
Sujith Manoharane90e3022014-08-23 13:29:20 +05301298void ath9k_offchannel_init(struct ath_softc *sc)
1299{
1300 struct ath_chanctx *ctx;
1301 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1302 struct ieee80211_supported_band *sband;
1303 struct ieee80211_channel *chan;
1304 int i;
1305
1306 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1307 if (!sband->n_channels)
1308 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1309
1310 chan = &sband->channels[0];
1311
1312 ctx = &sc->offchannel.chan;
1313 INIT_LIST_HEAD(&ctx->vifs);
1314 ctx->txpower = ATH_TXPOWER_MAX;
1315 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1316
1317 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1318 INIT_LIST_HEAD(&ctx->acq[i]);
1319
1320 sc->offchannel.chan.offchannel = true;
1321}
1322
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301323void ath9k_init_channel_context(struct ath_softc *sc)
1324{
1325 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1326
1327 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1328 (unsigned long)sc);
1329 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1330 (unsigned long)sc);
Sujith Manoharanc6500ea2014-10-17 07:40:22 +05301331
1332 init_completion(&sc->go_beacon);
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301333}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301334
Sujith Manoharanea22df22014-08-23 13:29:07 +05301335void ath9k_deinit_channel_context(struct ath_softc *sc)
1336{
1337 cancel_work_sync(&sc->chanctx_work);
1338}
1339
Sujith Manoharan499afac2014-08-22 20:39:31 +05301340bool ath9k_is_chanctx_enabled(void)
1341{
1342 return (ath9k_use_chanctx == 1);
1343}
1344
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301345/********************/
1346/* Queue management */
1347/********************/
1348
Sujith Manoharana064eaa2014-10-02 06:33:18 +05301349void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1350{
1351 struct ath_hw *ah = sc->sc_ah;
1352 int i;
1353
1354 if (ctx == &sc->offchannel.chan) {
1355 ieee80211_stop_queue(sc->hw,
1356 sc->hw->offchannel_tx_hw_queue);
1357 } else {
1358 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1359 ieee80211_stop_queue(sc->hw,
1360 ctx->hw_queue_base + i);
1361 }
1362
1363 if (ah->opmode == NL80211_IFTYPE_AP)
1364 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1365}
1366
1367
Sujith Manoharanb3903152014-10-02 06:33:17 +05301368void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301369{
1370 struct ath_hw *ah = sc->sc_ah;
1371 int i;
1372
Sujith Manoharanb3903152014-10-02 06:33:17 +05301373 if (ctx == &sc->offchannel.chan) {
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301374 ieee80211_wake_queue(sc->hw,
1375 sc->hw->offchannel_tx_hw_queue);
1376 } else {
1377 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1378 ieee80211_wake_queue(sc->hw,
Sujith Manoharanb3903152014-10-02 06:33:17 +05301379 ctx->hw_queue_base + i);
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301380 }
1381
1382 if (ah->opmode == NL80211_IFTYPE_AP)
1383 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1384}
1385
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301386/*****************/
1387/* P2P Powersave */
1388/*****************/
1389
1390static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301391{
1392 struct ath_hw *ah = sc->sc_ah;
1393 s32 tsf, target_tsf;
1394
1395 if (!avp || !avp->noa.has_next_tsf)
1396 return;
1397
1398 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1399
1400 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1401
1402 target_tsf = avp->noa.next_tsf;
1403 if (!avp->noa.absent)
1404 target_tsf -= ATH_P2P_PS_STOP_TIME;
1405
1406 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1407 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1408
1409 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1410}
1411
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301412static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1413{
1414 struct ath_vif *avp = (void *)vif->drv_priv;
1415 u32 tsf;
1416
1417 if (!sc->p2p_ps_timer)
1418 return;
1419
1420 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1421 return;
1422
1423 sc->p2p_ps_vif = avp;
1424 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1425 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1426 ath9k_update_p2p_ps_timer(sc, avp);
1427}
1428
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301429static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1430{
1431 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1432 u8 switch_time, ctwin;
1433
1434 /*
1435 * Channel switch in multi-channel mode is deferred
1436 * by a quarter beacon interval when handling
1437 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1438 * interface is guaranteed to be discoverable
1439 * for that duration after a TBTT.
1440 */
1441 switch_time = cur_conf->beacon_interval / 4;
1442
1443 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1444 if (ctwin && (ctwin < switch_time))
1445 return ctwin;
1446
1447 if (switch_time < P2P_DEFAULT_CTWIN)
1448 return 0;
1449
1450 return P2P_DEFAULT_CTWIN;
1451}
1452
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301453void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1454 struct sk_buff *skb)
1455{
1456 static const u8 noa_ie_hdr[] = {
1457 WLAN_EID_VENDOR_SPECIFIC, /* type */
1458 0, /* length */
1459 0x50, 0x6f, 0x9a, /* WFA OUI */
1460 0x09, /* P2P subtype */
1461 0x0c, /* Notice of Absence */
1462 0x00, /* LSB of little-endian len */
1463 0x00, /* MSB of little-endian len */
1464 };
1465
1466 struct ieee80211_p2p_noa_attr *noa;
1467 int noa_len, noa_desc, i = 0;
1468 u8 *hdr;
1469
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301470 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301471 return;
1472
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301473 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301474 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1475
1476 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1477 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1478 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1479 hdr[7] = noa_len;
1480
1481 noa = (void *) skb_put(skb, noa_len);
1482 memset(noa, 0, noa_len);
1483
1484 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301485 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1486
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301487 if (avp->noa_duration) {
1488 if (avp->periodic_noa) {
1489 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1490 noa->desc[i].count = 255;
1491 noa->desc[i].interval = cpu_to_le32(interval);
1492 } else {
1493 noa->desc[i].count = 1;
1494 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301495
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301496 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1497 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301498 i++;
1499 }
1500
1501 if (avp->offchannel_duration) {
1502 noa->desc[i].count = 1;
1503 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1504 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1505 }
1506}
1507
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301508void ath9k_p2p_ps_timer(void *priv)
1509{
1510 struct ath_softc *sc = priv;
1511 struct ath_vif *avp = sc->p2p_ps_vif;
1512 struct ieee80211_vif *vif;
1513 struct ieee80211_sta *sta;
1514 struct ath_node *an;
1515 u32 tsf;
1516
1517 del_timer_sync(&sc->sched.timer);
1518 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1519 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1520
1521 if (!avp || avp->chanctx != sc->cur_chan)
1522 return;
1523
1524 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1525 if (!avp->noa.absent)
1526 tsf += ATH_P2P_PS_STOP_TIME;
1527
1528 if (!avp->noa.has_next_tsf ||
1529 avp->noa.next_tsf - tsf > BIT(31))
1530 ieee80211_update_p2p_noa(&avp->noa, tsf);
1531
1532 ath9k_update_p2p_ps_timer(sc, avp);
1533
1534 rcu_read_lock();
1535
1536 vif = avp->vif;
Sujith Manoharancb355822014-09-17 14:45:56 +05301537 sta = ieee80211_find_sta(vif, avp->bssid);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301538 if (!sta)
1539 goto out;
1540
1541 an = (void *) sta->drv_priv;
1542 if (an->sleeping == !!avp->noa.absent)
1543 goto out;
1544
1545 an->sleeping = avp->noa.absent;
1546 if (an->sleeping)
1547 ath_tx_aggr_sleep(sta, sc, an);
1548 else
1549 ath_tx_aggr_wakeup(sc, an);
1550
1551out:
1552 rcu_read_unlock();
1553}
1554
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301555void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1556 struct ieee80211_vif *vif)
1557{
1558 unsigned long flags;
1559
1560 spin_lock_bh(&sc->sc_pcu_lock);
1561 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1562 if (!(sc->ps_flags & PS_BEACON_SYNC))
1563 ath9k_update_p2p_ps(sc, vif);
1564 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1565 spin_unlock_bh(&sc->sc_pcu_lock);
1566}
1567
1568void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1569{
1570 if (sc->p2p_ps_vif)
1571 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1572}
1573
1574void ath9k_p2p_remove_vif(struct ath_softc *sc,
1575 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301576{
1577 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301578
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301579 spin_lock_bh(&sc->sc_pcu_lock);
1580 if (avp == sc->p2p_ps_vif) {
1581 sc->p2p_ps_vif = NULL;
1582 ath9k_update_p2p_ps_timer(sc, NULL);
1583 }
1584 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301585}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301586
1587int ath9k_init_p2p(struct ath_softc *sc)
1588{
1589 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1590 NULL, sc, AR_FIRST_NDP_TIMER);
1591 if (!sc->p2p_ps_timer)
1592 return -ENOMEM;
1593
1594 return 0;
1595}
1596
1597void ath9k_deinit_p2p(struct ath_softc *sc)
1598{
1599 if (sc->p2p_ps_timer)
1600 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1601}
1602
1603#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */