blob: 447b397ca62e9586d695d7b34da322eb1cf9dd3d [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
Felix Fietkau748299f2014-06-11 16:18:04 +0530453void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
454 enum ath_chanctx_event ev)
455{
456 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530457 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530458 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530459 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530460 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530461 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530462 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530463
464 if (vif)
465 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530466
467 spin_lock_bh(&sc->chan_lock);
468
Sujith Manoharan878066e2014-08-27 12:07:24 +0530469 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
470 sc->cur_chan->chandef.center_freq1,
471 chanctx_event_string(ev),
472 chanctx_state_string(sc->sched.state));
473
Felix Fietkau748299f2014-06-11 16:18:04 +0530474 switch (ev) {
475 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530476 if (avp->offchannel_duration)
477 avp->offchannel_duration = 0;
478
Sujith Manoharan878066e2014-08-27 12:07:24 +0530479 if (avp->chanctx != sc->cur_chan) {
480 ath_dbg(common, CHAN_CTX,
481 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530482 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530483 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530484
Sujith Manoharan367b3412014-09-05 09:50:57 +0530485 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530486 sc->sched.offchannel_pending = false;
487 sc->next_chan = &sc->offchannel.chan;
488 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530489 ath_dbg(common, CHAN_CTX,
490 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530491 }
492
493 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
494 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
495 sc->next_chan = ctx;
496 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530497 ath_dbg(common, CHAN_CTX,
498 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530499 }
500
501 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530502 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530503 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530504 ath_dbg(common, CHAN_CTX,
505 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
506 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530507
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530508 if (sc->sched.mgd_prepare_tx)
509 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
510
Sujith Manoharan8890d052014-10-17 07:40:14 +0530511 /*
512 * When a context becomes inactive, for example,
513 * disassociation of a station context, the NoA
514 * attribute needs to be removed from subsequent
515 * beacons.
516 */
517 if (!ctx->active && avp->noa_duration &&
518 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
519 avp->noa_duration = 0;
520 avp->periodic_noa = false;
521
522 ath_dbg(common, CHAN_CTX,
523 "Clearing NoA schedule\n");
524 }
525
Felix Fietkau748299f2014-06-11 16:18:04 +0530526 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
527 break;
528
Sujith Manoharan878066e2014-08-27 12:07:24 +0530529 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
530
Felix Fietkau748299f2014-06-11 16:18:04 +0530531 sc->sched.beacon_pending = true;
532 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530533
Felix Fietkau74148632014-06-11 16:18:11 +0530534 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530535 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
536
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530537 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530538 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530539 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530540 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530541
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530542 /*
543 * If an offchannel switch is scheduled to happen after
544 * a beacon transmission, update the NoA with one-shot
545 * values and increment the index.
546 */
547 if (sc->next_chan == &sc->offchannel.chan) {
Sujith Manoharana23152a2014-10-17 07:40:23 +0530548 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530549 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530550 }
551
Sujith Manoharan828fe012014-10-17 07:40:25 +0530552 ath_chanctx_handle_bmiss(sc, ctx, avp);
Sujith Manoharana2b28602014-09-15 11:25:50 +0530553
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530554 /* Prevent wrap-around issues */
555 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
556 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530557
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530558 /*
559 * If multiple contexts are active, start periodic
560 * NoA and increment the index for the first
561 * announcement.
562 */
563 if (ctx->active &&
Sujith Manoharan347a9562014-10-17 07:40:24 +0530564 (!avp->noa_duration || sc->sched.force_noa_update))
565 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
566 tsf_time, beacon_int);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530567
568 if (ctx->active && sc->sched.force_noa_update)
569 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530570
Felix Fietkau748299f2014-06-11 16:18:04 +0530571 break;
572 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530573 if (!sc->sched.beacon_pending) {
574 ath_dbg(common, CHAN_CTX,
575 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530576 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530577 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530578
579 sc->sched.beacon_pending = false;
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530580
581 if (sc->sched.mgd_prepare_tx) {
582 sc->sched.mgd_prepare_tx = false;
583 complete(&sc->go_beacon);
584 ath_dbg(common, CHAN_CTX,
585 "Beacon sent, complete go_beacon\n");
586 break;
587 }
588
Felix Fietkau748299f2014-06-11 16:18:04 +0530589 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
590 break;
591
Sujith Manoharan878066e2014-08-27 12:07:24 +0530592 ath_dbg(common, CHAN_CTX,
593 "Move chanctx state to WAIT_FOR_TIMER\n");
594
Felix Fietkau748299f2014-06-11 16:18:04 +0530595 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530596 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530597 break;
598 case ATH_CHANCTX_EVENT_TSF_TIMER:
599 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
600 break;
601
Felix Fietkauec70abe2014-06-11 16:18:12 +0530602 if (!sc->cur_chan->switch_after_beacon &&
603 sc->sched.beacon_pending)
604 sc->sched.beacon_miss++;
605
Sujith Manoharan878066e2014-08-27 12:07:24 +0530606 ath_dbg(common, CHAN_CTX,
607 "Move chanctx state to SWITCH\n");
608
Felix Fietkau748299f2014-06-11 16:18:04 +0530609 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
610 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
611 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530612 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530613 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
614 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530615 break;
616
Felix Fietkauec70abe2014-06-11 16:18:12 +0530617 sc->sched.beacon_pending = false;
618 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530619
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530620 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
621 !sc->cur_chan->tsf_val)
622 break;
623
624 ath_chanctx_adjust_tbtt_delta(sc);
625
Felix Fietkaua899b672014-06-11 16:18:13 +0530626 /* TSF time might have been updated by the incoming beacon,
627 * need update the channel switch timer to reflect the change.
628 */
629 tsf_time = sc->sched.switch_start_time;
630 tsf_time -= (u32) sc->cur_chan->tsf_val +
631 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
632 tsf_time += ath9k_hw_gettsf32(ah);
633
Felix Fietkau42eda112014-06-11 16:18:14 +0530634
635 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530636 break;
Sujith Manoharanb8f92792014-10-17 07:40:09 +0530637 case ATH_CHANCTX_EVENT_AUTHORIZED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530638 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
639 avp->chanctx != sc->cur_chan)
640 break;
641
Sujith Manoharan878066e2014-08-27 12:07:24 +0530642 ath_dbg(common, CHAN_CTX,
643 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
644
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530645 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
646 /* fall through */
647 case ATH_CHANCTX_EVENT_SWITCH:
648 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
649 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
650 sc->cur_chan->switch_after_beacon ||
651 sc->cur_chan == &sc->offchannel.chan)
652 break;
653
654 /* If this is a station chanctx, stay active for a half
655 * beacon period (minus channel switch time)
656 */
657 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530658 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530659
Sujith Manoharan878066e2014-08-27 12:07:24 +0530660 ath_dbg(common, CHAN_CTX,
661 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
662
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530663 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530664 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530665
666 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530667
668 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530669 sc->sched.beacon_miss = 0;
670 tsf_time *= 3;
671 }
672
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530673 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530674 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530675 sc->sched.switch_start_time = tsf_time;
676
Felix Fietkau42eda112014-06-11 16:18:14 +0530677 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530678 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530679 break;
680 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
681 if (sc->cur_chan == &sc->offchannel.chan ||
682 sc->cur_chan->switch_after_beacon)
683 break;
684
685 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
686 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
687 break;
688 case ATH_CHANCTX_EVENT_UNASSIGN:
689 if (sc->cur_chan->assigned) {
690 if (sc->next_chan && !sc->next_chan->assigned &&
691 sc->next_chan != &sc->offchannel.chan)
692 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
693 break;
694 }
695
696 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
697 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
698 if (!ctx->assigned)
699 break;
700
701 sc->next_chan = ctx;
702 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
703 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530704 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530705 /*
706 * When adding a new channel context, check if a scan
707 * is in progress and abort it since the addition of
708 * a new channel context is usually followed by VIF
709 * assignment, in which case we have to start multi-channel
710 * operation.
711 */
712 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
713 ath_dbg(common, CHAN_CTX,
714 "Aborting HW scan to add new context\n");
715
716 spin_unlock_bh(&sc->chan_lock);
717 del_timer_sync(&sc->offchannel.timer);
718 ath_scan_complete(sc, true);
719 spin_lock_bh(&sc->chan_lock);
720 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530721 break;
722 case ATH_CHANCTX_EVENT_CHANGE:
723 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530724 }
725
726 spin_unlock_bh(&sc->chan_lock);
727}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530728
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530729void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
730 enum ath_chanctx_event ev)
731{
732 if (sc->sched.beacon_pending)
733 ath_chanctx_event(sc, NULL, ev);
734}
735
Sujith Manoharana2b28602014-09-15 11:25:50 +0530736void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530737 enum ath_chanctx_event ev)
738{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530739 ath_chanctx_event(sc, NULL, ev);
740}
741
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530742static int ath_scan_channel_duration(struct ath_softc *sc,
743 struct ieee80211_channel *chan)
744{
745 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
746
747 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
748 return (HZ / 9); /* ~110 ms */
749
750 return (HZ / 16); /* ~60 ms */
751}
752
Sujith Manoharan922c9432014-08-23 13:29:15 +0530753static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
754 struct cfg80211_chan_def *chandef)
755{
756 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
757
758 spin_lock_bh(&sc->chan_lock);
759
760 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
761 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530762 if (chandef)
763 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530764
765 sc->sched.offchannel_pending = true;
766 sc->sched.wait_switch = true;
767 sc->sched.offchannel_duration =
768 jiffies_to_usecs(sc->offchannel.duration) +
769 sc->sched.channel_switch_time;
770
Sujith Manoharan922c9432014-08-23 13:29:15 +0530771 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530772 ath_dbg(common, CHAN_CTX,
773 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530774 return;
775 }
776
777 sc->next_chan = ctx;
778 if (chandef) {
779 ctx->chandef = *chandef;
780 ath_dbg(common, CHAN_CTX,
781 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
782 }
783
784 if (sc->next_chan == &sc->offchannel.chan) {
785 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530786 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530787 sc->sched.channel_switch_time;
788
789 if (chandef) {
790 ath_dbg(common, CHAN_CTX,
791 "Offchannel duration for chan %d MHz : %u\n",
792 chandef->center_freq1,
793 sc->sched.offchannel_duration);
794 }
795 }
796 spin_unlock_bh(&sc->chan_lock);
797 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
798}
799
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530800static void ath_chanctx_offchan_switch(struct ath_softc *sc,
801 struct ieee80211_channel *chan)
802{
803 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
804 struct cfg80211_chan_def chandef;
805
806 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
807 ath_dbg(common, CHAN_CTX,
808 "Channel definition created: %d MHz\n", chandef.center_freq1);
809
810 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
811}
812
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530813static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
814 bool active)
815{
816 struct ath_chanctx *ctx;
817
818 ath_for_each_chanctx(sc, ctx) {
819 if (!ctx->assigned || list_empty(&ctx->vifs))
820 continue;
821 if (active && !ctx->active)
822 continue;
823
824 if (ctx->switch_after_beacon)
825 return ctx;
826 }
827
828 return &sc->chanctx[0];
829}
830
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530831static void
832ath_scan_next_channel(struct ath_softc *sc)
833{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530834 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530835 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
836 struct ieee80211_channel *chan;
837
838 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530839 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530840 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
841 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530842 sc->offchannel.scan_idx,
843 req->n_channels);
844
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530845 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
846 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
847 NULL);
848 return;
849 }
850
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530851 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530852 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530853 sc->offchannel.scan_idx);
854
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530855 chan = req->channels[sc->offchannel.scan_idx++];
856 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
857 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530858
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530859 ath_chanctx_offchan_switch(sc, chan);
860}
861
862void ath_offchannel_next(struct ath_softc *sc)
863{
864 struct ieee80211_vif *vif;
865
866 if (sc->offchannel.scan_req) {
867 vif = sc->offchannel.scan_vif;
868 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
869 ath_scan_next_channel(sc);
870 } else if (sc->offchannel.roc_vif) {
871 vif = sc->offchannel.roc_vif;
872 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530873 sc->offchannel.duration =
874 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530875 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
876 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
877 } else {
878 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
879 NULL);
880 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
881 if (sc->ps_idle)
882 ath_cancel_work(sc);
883 }
884}
885
886void ath_roc_complete(struct ath_softc *sc, bool abort)
887{
Sujith Manoharan4b60af42014-10-02 06:33:12 +0530888 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
889
890 if (abort)
891 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
892 else
893 ath_dbg(common, CHAN_CTX, "RoC expired\n");
894
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530895 sc->offchannel.roc_vif = NULL;
896 sc->offchannel.roc_chan = NULL;
Sujith Manoharana3503352014-10-17 07:40:26 +0530897 if (abort)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530898 ieee80211_remain_on_channel_expired(sc->hw);
899 ath_offchannel_next(sc);
900 ath9k_ps_restore(sc);
901}
902
903void ath_scan_complete(struct ath_softc *sc, bool abort)
904{
905 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
906
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530907 if (abort)
908 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
909 else
910 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
911
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530912 sc->offchannel.scan_req = NULL;
913 sc->offchannel.scan_vif = NULL;
914 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
915 ieee80211_scan_completed(sc->hw, abort);
916 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530917 spin_lock_bh(&sc->chan_lock);
918 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
919 sc->sched.force_noa_update = true;
920 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530921 ath_offchannel_next(sc);
922 ath9k_ps_restore(sc);
923}
924
925static void ath_scan_send_probe(struct ath_softc *sc,
926 struct cfg80211_ssid *ssid)
927{
928 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
929 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
930 struct ath_tx_control txctl = {};
931 struct sk_buff *skb;
932 struct ieee80211_tx_info *info;
933 int band = sc->offchannel.chan.chandef.chan->band;
934
935 skb = ieee80211_probereq_get(sc->hw, vif,
936 ssid->ssid, ssid->ssid_len, req->ie_len);
937 if (!skb)
938 return;
939
940 info = IEEE80211_SKB_CB(skb);
941 if (req->no_cck)
942 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
943
944 if (req->ie_len)
945 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
946
947 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
948
949 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
950 goto error;
951
952 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
953 txctl.force_channel = true;
954 if (ath_tx_start(sc->hw, skb, &txctl))
955 goto error;
956
957 return;
958
959error:
960 ieee80211_free_txskb(sc->hw, skb);
961}
962
963static void ath_scan_channel_start(struct ath_softc *sc)
964{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530965 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530966 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
967 int i;
968
969 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
970 req->n_ssids) {
971 for (i = 0; i < req->n_ssids; i++)
972 ath_scan_send_probe(sc, &req->ssids[i]);
973
974 }
975
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530976 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530977 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530978
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530979 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
980 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
981}
982
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530983static void ath_chanctx_timer(unsigned long data)
984{
985 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530986 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
987
988 ath_dbg(common, CHAN_CTX,
989 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530990
991 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
992}
993
994static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530995{
996 struct ath_softc *sc = (struct ath_softc *)data;
997 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530998 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
999
Sujith Manoharan878066e2014-08-27 12:07:24 +05301000 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +05301001 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301002
1003 switch (sc->offchannel.state) {
1004 case ATH_OFFCHANNEL_PROBE_WAIT:
1005 if (!sc->offchannel.scan_req)
1006 return;
1007
1008 /* get first active channel context */
1009 ctx = ath_chanctx_get_oper_chan(sc, true);
1010 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301011 ath_dbg(common, CHAN_CTX,
1012 "Switch to oper/active context, "
1013 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1014
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301015 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1016 ath_chanctx_switch(sc, ctx, NULL);
1017 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1018 break;
1019 }
1020 /* fall through */
1021 case ATH_OFFCHANNEL_SUSPEND:
1022 if (!sc->offchannel.scan_req)
1023 return;
1024
1025 ath_scan_next_channel(sc);
1026 break;
1027 case ATH_OFFCHANNEL_ROC_START:
1028 case ATH_OFFCHANNEL_ROC_WAIT:
1029 ctx = ath_chanctx_get_oper_chan(sc, false);
1030 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
Sujith Manoharana3503352014-10-17 07:40:26 +05301031 ieee80211_remain_on_channel_expired(sc->hw);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +05301032 ath_chanctx_switch(sc, ctx, NULL);
1033 break;
1034 default:
1035 break;
1036 }
1037}
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301038
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301039static bool
1040ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1041 bool powersave)
1042{
1043 struct ieee80211_vif *vif = avp->vif;
1044 struct ieee80211_sta *sta = NULL;
1045 struct ieee80211_hdr_3addr *nullfunc;
1046 struct ath_tx_control txctl;
1047 struct sk_buff *skb;
1048 int band = sc->cur_chan->chandef.chan->band;
1049
1050 switch (vif->type) {
1051 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +05301052 if (!avp->assoc)
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301053 return false;
1054
1055 skb = ieee80211_nullfunc_get(sc->hw, vif);
1056 if (!skb)
1057 return false;
1058
1059 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1060 if (powersave)
1061 nullfunc->frame_control |=
1062 cpu_to_le16(IEEE80211_FCTL_PM);
1063
1064 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1065 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1066 dev_kfree_skb_any(skb);
1067 return false;
1068 }
1069 break;
1070 default:
1071 return false;
1072 }
1073
1074 memset(&txctl, 0, sizeof(txctl));
1075 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1076 txctl.sta = sta;
1077 txctl.force_channel = true;
1078 if (ath_tx_start(sc->hw, skb, &txctl)) {
1079 ieee80211_free_txskb(sc->hw, skb);
1080 return false;
1081 }
1082
1083 return true;
1084}
1085
1086static bool
1087ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1088{
1089 struct ath_vif *avp;
1090 bool sent = false;
1091
1092 rcu_read_lock();
1093 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1094 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1095 sent = true;
1096 }
1097 rcu_read_unlock();
1098
1099 return sent;
1100}
1101
1102static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1103{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301104 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1105
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301106 if (sc->cur_chan == &sc->offchannel.chan)
1107 return false;
1108
1109 switch (sc->sched.state) {
1110 case ATH_CHANCTX_STATE_SWITCH:
1111 return false;
1112 case ATH_CHANCTX_STATE_IDLE:
1113 if (!sc->cur_chan->switch_after_beacon)
1114 return false;
1115
Sujith Manoharan878066e2014-08-27 12:07:24 +05301116 ath_dbg(common, CHAN_CTX,
1117 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1118
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301119 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1120 break;
1121 default:
1122 break;
1123 }
1124
1125 return true;
1126}
1127
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301128static void ath_offchannel_channel_change(struct ath_softc *sc)
1129{
1130 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1131
Sujith Manoharan878066e2014-08-27 12:07:24 +05301132 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301133 __func__, offchannel_state_string(sc->offchannel.state));
1134
1135 switch (sc->offchannel.state) {
1136 case ATH_OFFCHANNEL_PROBE_SEND:
1137 if (!sc->offchannel.scan_req)
1138 return;
1139
1140 if (sc->cur_chan->chandef.chan !=
1141 sc->offchannel.chan.chandef.chan)
1142 return;
1143
1144 ath_scan_channel_start(sc);
1145 break;
1146 case ATH_OFFCHANNEL_IDLE:
1147 if (!sc->offchannel.scan_req)
1148 return;
1149
1150 ath_scan_complete(sc, false);
1151 break;
1152 case ATH_OFFCHANNEL_ROC_START:
1153 if (sc->cur_chan != &sc->offchannel.chan)
1154 break;
1155
1156 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301157 mod_timer(&sc->offchannel.timer,
1158 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301159 ieee80211_ready_on_channel(sc->hw);
1160 break;
1161 case ATH_OFFCHANNEL_ROC_DONE:
1162 ath_roc_complete(sc, false);
1163 break;
1164 default:
1165 break;
1166 }
1167}
1168
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301169void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1170{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301171 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301172 struct ath_chanctx *old_ctx;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301173 struct timespec ts;
1174 bool measure_time = false;
1175 bool send_ps = false;
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301176 bool queues_stopped = false;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301177
1178 spin_lock_bh(&sc->chan_lock);
1179 if (!sc->next_chan) {
1180 spin_unlock_bh(&sc->chan_lock);
1181 return;
1182 }
1183
1184 if (!force && ath_chanctx_defer_switch(sc)) {
1185 spin_unlock_bh(&sc->chan_lock);
1186 return;
1187 }
1188
Sujith Manoharan878066e2014-08-27 12:07:24 +05301189 ath_dbg(common, CHAN_CTX,
1190 "%s: current: %d MHz, next: %d MHz\n",
1191 __func__,
1192 sc->cur_chan->chandef.center_freq1,
1193 sc->next_chan->chandef.center_freq1);
1194
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301195 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301196 ath_dbg(common, CHAN_CTX,
1197 "Stopping current chanctx: %d\n",
1198 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301199 sc->cur_chan->stopped = true;
1200 spin_unlock_bh(&sc->chan_lock);
1201
1202 if (sc->next_chan == &sc->offchannel.chan) {
1203 getrawmonotonic(&ts);
1204 measure_time = true;
1205 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301206
1207 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1208 queues_stopped = true;
1209
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301210 __ath9k_flush(sc->hw, ~0, true, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301211
1212 if (ath_chanctx_send_ps_frame(sc, true))
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301213 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1214 false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301215
1216 send_ps = true;
1217 spin_lock_bh(&sc->chan_lock);
1218
1219 if (sc->cur_chan != &sc->offchannel.chan) {
1220 getrawmonotonic(&sc->cur_chan->tsf_ts);
1221 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1222 }
1223 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301224 old_ctx = sc->cur_chan;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301225 sc->cur_chan = sc->next_chan;
1226 sc->cur_chan->stopped = false;
1227 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301228
1229 if (!sc->sched.offchannel_pending)
1230 sc->sched.offchannel_duration = 0;
1231
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301232 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1233 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1234
1235 spin_unlock_bh(&sc->chan_lock);
1236
1237 if (sc->sc_ah->chip_fullsleep ||
1238 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1239 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301240 ath_dbg(common, CHAN_CTX,
1241 "%s: Set channel %d MHz\n",
1242 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301243 ath_set_channel(sc);
1244 if (measure_time)
1245 sc->sched.channel_switch_time =
1246 ath9k_hw_get_tsf_offset(&ts, NULL);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301247 /*
1248 * A reset will ensure that all queues are woken up,
1249 * so there is no need to awaken them again.
1250 */
1251 goto out;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301252 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301253
1254 if (queues_stopped)
1255 ath9k_chanctx_wake_queues(sc, old_ctx);
1256out:
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301257 if (send_ps)
1258 ath_chanctx_send_ps_frame(sc, false);
1259
1260 ath_offchannel_channel_change(sc);
1261 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1262}
1263
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301264static void ath_chanctx_work(struct work_struct *work)
1265{
1266 struct ath_softc *sc = container_of(work, struct ath_softc,
1267 chanctx_work);
1268 mutex_lock(&sc->mutex);
1269 ath_chanctx_set_next(sc, false);
1270 mutex_unlock(&sc->mutex);
1271}
1272
Sujith Manoharane90e3022014-08-23 13:29:20 +05301273void ath9k_offchannel_init(struct ath_softc *sc)
1274{
1275 struct ath_chanctx *ctx;
1276 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1277 struct ieee80211_supported_band *sband;
1278 struct ieee80211_channel *chan;
1279 int i;
1280
1281 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1282 if (!sband->n_channels)
1283 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1284
1285 chan = &sband->channels[0];
1286
1287 ctx = &sc->offchannel.chan;
1288 INIT_LIST_HEAD(&ctx->vifs);
1289 ctx->txpower = ATH_TXPOWER_MAX;
1290 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1291
1292 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1293 INIT_LIST_HEAD(&ctx->acq[i]);
1294
1295 sc->offchannel.chan.offchannel = true;
1296}
1297
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301298void ath9k_init_channel_context(struct ath_softc *sc)
1299{
1300 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1301
1302 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1303 (unsigned long)sc);
1304 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1305 (unsigned long)sc);
Sujith Manoharanc6500ea2014-10-17 07:40:22 +05301306
1307 init_completion(&sc->go_beacon);
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301308}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301309
Sujith Manoharanea22df22014-08-23 13:29:07 +05301310void ath9k_deinit_channel_context(struct ath_softc *sc)
1311{
1312 cancel_work_sync(&sc->chanctx_work);
1313}
1314
Sujith Manoharan499afac2014-08-22 20:39:31 +05301315bool ath9k_is_chanctx_enabled(void)
1316{
1317 return (ath9k_use_chanctx == 1);
1318}
1319
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301320/********************/
1321/* Queue management */
1322/********************/
1323
Sujith Manoharana064eaa2014-10-02 06:33:18 +05301324void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1325{
1326 struct ath_hw *ah = sc->sc_ah;
1327 int i;
1328
1329 if (ctx == &sc->offchannel.chan) {
1330 ieee80211_stop_queue(sc->hw,
1331 sc->hw->offchannel_tx_hw_queue);
1332 } else {
1333 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1334 ieee80211_stop_queue(sc->hw,
1335 ctx->hw_queue_base + i);
1336 }
1337
1338 if (ah->opmode == NL80211_IFTYPE_AP)
1339 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1340}
1341
1342
Sujith Manoharanb3903152014-10-02 06:33:17 +05301343void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301344{
1345 struct ath_hw *ah = sc->sc_ah;
1346 int i;
1347
Sujith Manoharanb3903152014-10-02 06:33:17 +05301348 if (ctx == &sc->offchannel.chan) {
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301349 ieee80211_wake_queue(sc->hw,
1350 sc->hw->offchannel_tx_hw_queue);
1351 } else {
1352 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1353 ieee80211_wake_queue(sc->hw,
Sujith Manoharanb3903152014-10-02 06:33:17 +05301354 ctx->hw_queue_base + i);
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301355 }
1356
1357 if (ah->opmode == NL80211_IFTYPE_AP)
1358 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1359}
1360
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301361/*****************/
1362/* P2P Powersave */
1363/*****************/
1364
1365static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301366{
1367 struct ath_hw *ah = sc->sc_ah;
1368 s32 tsf, target_tsf;
1369
1370 if (!avp || !avp->noa.has_next_tsf)
1371 return;
1372
1373 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1374
1375 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1376
1377 target_tsf = avp->noa.next_tsf;
1378 if (!avp->noa.absent)
1379 target_tsf -= ATH_P2P_PS_STOP_TIME;
1380
1381 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1382 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1383
1384 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1385}
1386
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301387static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1388{
1389 struct ath_vif *avp = (void *)vif->drv_priv;
1390 u32 tsf;
1391
1392 if (!sc->p2p_ps_timer)
1393 return;
1394
1395 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1396 return;
1397
1398 sc->p2p_ps_vif = avp;
1399 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1400 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1401 ath9k_update_p2p_ps_timer(sc, avp);
1402}
1403
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301404static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1405{
1406 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1407 u8 switch_time, ctwin;
1408
1409 /*
1410 * Channel switch in multi-channel mode is deferred
1411 * by a quarter beacon interval when handling
1412 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1413 * interface is guaranteed to be discoverable
1414 * for that duration after a TBTT.
1415 */
1416 switch_time = cur_conf->beacon_interval / 4;
1417
1418 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1419 if (ctwin && (ctwin < switch_time))
1420 return ctwin;
1421
1422 if (switch_time < P2P_DEFAULT_CTWIN)
1423 return 0;
1424
1425 return P2P_DEFAULT_CTWIN;
1426}
1427
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301428void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1429 struct sk_buff *skb)
1430{
1431 static const u8 noa_ie_hdr[] = {
1432 WLAN_EID_VENDOR_SPECIFIC, /* type */
1433 0, /* length */
1434 0x50, 0x6f, 0x9a, /* WFA OUI */
1435 0x09, /* P2P subtype */
1436 0x0c, /* Notice of Absence */
1437 0x00, /* LSB of little-endian len */
1438 0x00, /* MSB of little-endian len */
1439 };
1440
1441 struct ieee80211_p2p_noa_attr *noa;
1442 int noa_len, noa_desc, i = 0;
1443 u8 *hdr;
1444
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301445 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301446 return;
1447
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301448 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301449 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1450
1451 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1452 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1453 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1454 hdr[7] = noa_len;
1455
1456 noa = (void *) skb_put(skb, noa_len);
1457 memset(noa, 0, noa_len);
1458
1459 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301460 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1461
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301462 if (avp->noa_duration) {
1463 if (avp->periodic_noa) {
1464 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1465 noa->desc[i].count = 255;
1466 noa->desc[i].interval = cpu_to_le32(interval);
1467 } else {
1468 noa->desc[i].count = 1;
1469 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301470
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301471 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1472 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301473 i++;
1474 }
1475
1476 if (avp->offchannel_duration) {
1477 noa->desc[i].count = 1;
1478 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1479 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1480 }
1481}
1482
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301483void ath9k_p2p_ps_timer(void *priv)
1484{
1485 struct ath_softc *sc = priv;
1486 struct ath_vif *avp = sc->p2p_ps_vif;
1487 struct ieee80211_vif *vif;
1488 struct ieee80211_sta *sta;
1489 struct ath_node *an;
1490 u32 tsf;
1491
1492 del_timer_sync(&sc->sched.timer);
1493 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1494 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1495
1496 if (!avp || avp->chanctx != sc->cur_chan)
1497 return;
1498
1499 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1500 if (!avp->noa.absent)
1501 tsf += ATH_P2P_PS_STOP_TIME;
1502
1503 if (!avp->noa.has_next_tsf ||
1504 avp->noa.next_tsf - tsf > BIT(31))
1505 ieee80211_update_p2p_noa(&avp->noa, tsf);
1506
1507 ath9k_update_p2p_ps_timer(sc, avp);
1508
1509 rcu_read_lock();
1510
1511 vif = avp->vif;
Sujith Manoharancb355822014-09-17 14:45:56 +05301512 sta = ieee80211_find_sta(vif, avp->bssid);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301513 if (!sta)
1514 goto out;
1515
1516 an = (void *) sta->drv_priv;
1517 if (an->sleeping == !!avp->noa.absent)
1518 goto out;
1519
1520 an->sleeping = avp->noa.absent;
1521 if (an->sleeping)
1522 ath_tx_aggr_sleep(sta, sc, an);
1523 else
1524 ath_tx_aggr_wakeup(sc, an);
1525
1526out:
1527 rcu_read_unlock();
1528}
1529
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301530void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1531 struct ieee80211_vif *vif)
1532{
1533 unsigned long flags;
1534
1535 spin_lock_bh(&sc->sc_pcu_lock);
1536 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1537 if (!(sc->ps_flags & PS_BEACON_SYNC))
1538 ath9k_update_p2p_ps(sc, vif);
1539 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1540 spin_unlock_bh(&sc->sc_pcu_lock);
1541}
1542
1543void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1544{
1545 if (sc->p2p_ps_vif)
1546 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1547}
1548
1549void ath9k_p2p_remove_vif(struct ath_softc *sc,
1550 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301551{
1552 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301553
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301554 spin_lock_bh(&sc->sc_pcu_lock);
1555 if (avp == sc->p2p_ps_vif) {
1556 sc->p2p_ps_vif = NULL;
1557 ath9k_update_p2p_ps_timer(sc, NULL);
1558 }
1559 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301560}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301561
1562int ath9k_init_p2p(struct ath_softc *sc)
1563{
1564 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1565 NULL, sc, AR_FIRST_NDP_TIMER);
1566 if (!sc->p2p_ps_timer)
1567 return -ENOMEM;
1568
1569 return 0;
1570}
1571
1572void ath9k_deinit_p2p(struct ath_softc *sc)
1573{
1574 if (sc->p2p_ps_timer)
1575 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1576}
1577
1578#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */