blob: c6e130da683fb4f9cc036a9d5937658f049fc56a [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 Manoharana23152a2014-10-17 07:40:23 +0530369static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
370 struct ath_chanctx *ctx,
371 struct ath_vif *avp,
372 u32 tsf_time)
373{
374 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
375
376 avp->noa_index++;
377 avp->offchannel_start = tsf_time;
378 avp->offchannel_duration = sc->sched.offchannel_duration;
379
380 ath_dbg(common, CHAN_CTX,
381 "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n",
382 avp->offchannel_duration,
383 avp->offchannel_start,
384 avp->noa_index);
385
386 /*
387 * When multiple contexts are active, the NoA
388 * has to be recalculated and advertised after
389 * an offchannel operation.
390 */
391 if (ctx->active && avp->noa_duration)
392 avp->noa_duration = 0;
393}
394
Felix Fietkau748299f2014-06-11 16:18:04 +0530395void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
396 enum ath_chanctx_event ev)
397{
398 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530399 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530400 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530401 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530402 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530403 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530404 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530405
406 if (vif)
407 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530408
409 spin_lock_bh(&sc->chan_lock);
410
Sujith Manoharan878066e2014-08-27 12:07:24 +0530411 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
412 sc->cur_chan->chandef.center_freq1,
413 chanctx_event_string(ev),
414 chanctx_state_string(sc->sched.state));
415
Felix Fietkau748299f2014-06-11 16:18:04 +0530416 switch (ev) {
417 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530418 if (avp->offchannel_duration)
419 avp->offchannel_duration = 0;
420
Sujith Manoharan878066e2014-08-27 12:07:24 +0530421 if (avp->chanctx != sc->cur_chan) {
422 ath_dbg(common, CHAN_CTX,
423 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530424 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530425 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530426
Sujith Manoharan367b3412014-09-05 09:50:57 +0530427 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530428 sc->sched.offchannel_pending = false;
429 sc->next_chan = &sc->offchannel.chan;
430 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530431 ath_dbg(common, CHAN_CTX,
432 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530433 }
434
435 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
436 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
437 sc->next_chan = ctx;
438 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530439 ath_dbg(common, CHAN_CTX,
440 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530441 }
442
443 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530444 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530445 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530446 ath_dbg(common, CHAN_CTX,
447 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
448 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530449
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530450 if (sc->sched.mgd_prepare_tx)
451 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
452
Sujith Manoharan8890d052014-10-17 07:40:14 +0530453 /*
454 * When a context becomes inactive, for example,
455 * disassociation of a station context, the NoA
456 * attribute needs to be removed from subsequent
457 * beacons.
458 */
459 if (!ctx->active && avp->noa_duration &&
460 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
461 avp->noa_duration = 0;
462 avp->periodic_noa = false;
463
464 ath_dbg(common, CHAN_CTX,
465 "Clearing NoA schedule\n");
466 }
467
Felix Fietkau748299f2014-06-11 16:18:04 +0530468 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
469 break;
470
Sujith Manoharan878066e2014-08-27 12:07:24 +0530471 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
472
Felix Fietkau748299f2014-06-11 16:18:04 +0530473 sc->sched.beacon_pending = true;
474 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530475
Felix Fietkau74148632014-06-11 16:18:11 +0530476 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530477 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
478
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530479 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530480 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530481 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530482 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530483
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530484 /*
485 * If an offchannel switch is scheduled to happen after
486 * a beacon transmission, update the NoA with one-shot
487 * values and increment the index.
488 */
489 if (sc->next_chan == &sc->offchannel.chan) {
Sujith Manoharana23152a2014-10-17 07:40:23 +0530490 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530491 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530492 }
493
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530494 /*
495 * Clear the extend_absence flag if it had been
496 * set during the previous beacon transmission,
497 * since we need to revert to the normal NoA
498 * schedule.
499 */
500 if (ctx->active && sc->sched.extend_absence) {
501 avp->noa_duration = 0;
502 sc->sched.extend_absence = false;
503 }
504
505 /* If at least two consecutive beacons were missed on the STA
506 * chanctx, stay on the STA channel for one extra beacon period,
507 * to resync the timer properly.
508 */
509 if (ctx->active && sc->sched.beacon_miss >= 2) {
510 avp->noa_duration = 0;
511 sc->sched.extend_absence = true;
512 }
Sujith Manoharana2b28602014-09-15 11:25:50 +0530513
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530514 /* Prevent wrap-around issues */
515 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
516 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530517
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530518 /*
519 * If multiple contexts are active, start periodic
520 * NoA and increment the index for the first
521 * announcement.
522 */
523 if (ctx->active &&
524 (!avp->noa_duration || sc->sched.force_noa_update)) {
525 avp->noa_index++;
526 avp->noa_start = tsf_time;
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530527
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530528 if (sc->sched.extend_absence)
529 avp->noa_duration = (3 * beacon_int / 2) +
530 sc->sched.channel_switch_time;
531 else
532 avp->noa_duration =
533 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
534 sc->sched.channel_switch_time;
535
536 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
537 sc->sched.extend_absence)
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530538 avp->periodic_noa = false;
539 else
540 avp->periodic_noa = true;
541
542 ath_dbg(common, CHAN_CTX,
543 "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
544 avp->noa_duration,
545 avp->noa_start,
546 avp->noa_index,
547 avp->periodic_noa);
548 }
549
550 if (ctx->active && sc->sched.force_noa_update)
551 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530552
Felix Fietkau748299f2014-06-11 16:18:04 +0530553 break;
554 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530555 if (!sc->sched.beacon_pending) {
556 ath_dbg(common, CHAN_CTX,
557 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530558 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530559 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530560
561 sc->sched.beacon_pending = false;
Sujith Manoharanc6500ea2014-10-17 07:40:22 +0530562
563 if (sc->sched.mgd_prepare_tx) {
564 sc->sched.mgd_prepare_tx = false;
565 complete(&sc->go_beacon);
566 ath_dbg(common, CHAN_CTX,
567 "Beacon sent, complete go_beacon\n");
568 break;
569 }
570
Felix Fietkau748299f2014-06-11 16:18:04 +0530571 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
572 break;
573
Sujith Manoharan878066e2014-08-27 12:07:24 +0530574 ath_dbg(common, CHAN_CTX,
575 "Move chanctx state to WAIT_FOR_TIMER\n");
576
Felix Fietkau748299f2014-06-11 16:18:04 +0530577 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530578 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530579 break;
580 case ATH_CHANCTX_EVENT_TSF_TIMER:
581 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
582 break;
583
Felix Fietkauec70abe2014-06-11 16:18:12 +0530584 if (!sc->cur_chan->switch_after_beacon &&
585 sc->sched.beacon_pending)
586 sc->sched.beacon_miss++;
587
Sujith Manoharan878066e2014-08-27 12:07:24 +0530588 ath_dbg(common, CHAN_CTX,
589 "Move chanctx state to SWITCH\n");
590
Felix Fietkau748299f2014-06-11 16:18:04 +0530591 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
592 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
593 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530594 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530595 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
596 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530597 break;
598
Felix Fietkauec70abe2014-06-11 16:18:12 +0530599 sc->sched.beacon_pending = false;
600 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530601
Sujith Manoharanbe247c12014-10-17 07:40:10 +0530602 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
603 !sc->cur_chan->tsf_val)
604 break;
605
606 ath_chanctx_adjust_tbtt_delta(sc);
607
Felix Fietkaua899b672014-06-11 16:18:13 +0530608 /* TSF time might have been updated by the incoming beacon,
609 * need update the channel switch timer to reflect the change.
610 */
611 tsf_time = sc->sched.switch_start_time;
612 tsf_time -= (u32) sc->cur_chan->tsf_val +
613 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
614 tsf_time += ath9k_hw_gettsf32(ah);
615
Felix Fietkau42eda112014-06-11 16:18:14 +0530616
617 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530618 break;
Sujith Manoharanb8f92792014-10-17 07:40:09 +0530619 case ATH_CHANCTX_EVENT_AUTHORIZED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530620 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
621 avp->chanctx != sc->cur_chan)
622 break;
623
Sujith Manoharan878066e2014-08-27 12:07:24 +0530624 ath_dbg(common, CHAN_CTX,
625 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
626
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530627 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
628 /* fall through */
629 case ATH_CHANCTX_EVENT_SWITCH:
630 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
631 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
632 sc->cur_chan->switch_after_beacon ||
633 sc->cur_chan == &sc->offchannel.chan)
634 break;
635
636 /* If this is a station chanctx, stay active for a half
637 * beacon period (minus channel switch time)
638 */
639 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530640 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530641
Sujith Manoharan878066e2014-08-27 12:07:24 +0530642 ath_dbg(common, CHAN_CTX,
643 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
644
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530645 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530646 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530647
648 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530649
650 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530651 sc->sched.beacon_miss = 0;
652 tsf_time *= 3;
653 }
654
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530655 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530656 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530657 sc->sched.switch_start_time = tsf_time;
658
Felix Fietkau42eda112014-06-11 16:18:14 +0530659 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530660 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530661 break;
662 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
663 if (sc->cur_chan == &sc->offchannel.chan ||
664 sc->cur_chan->switch_after_beacon)
665 break;
666
667 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
668 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
669 break;
670 case ATH_CHANCTX_EVENT_UNASSIGN:
671 if (sc->cur_chan->assigned) {
672 if (sc->next_chan && !sc->next_chan->assigned &&
673 sc->next_chan != &sc->offchannel.chan)
674 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
675 break;
676 }
677
678 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
679 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
680 if (!ctx->assigned)
681 break;
682
683 sc->next_chan = ctx;
684 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
685 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530686 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530687 /*
688 * When adding a new channel context, check if a scan
689 * is in progress and abort it since the addition of
690 * a new channel context is usually followed by VIF
691 * assignment, in which case we have to start multi-channel
692 * operation.
693 */
694 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
695 ath_dbg(common, CHAN_CTX,
696 "Aborting HW scan to add new context\n");
697
698 spin_unlock_bh(&sc->chan_lock);
699 del_timer_sync(&sc->offchannel.timer);
700 ath_scan_complete(sc, true);
701 spin_lock_bh(&sc->chan_lock);
702 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530703 break;
704 case ATH_CHANCTX_EVENT_CHANGE:
705 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530706 }
707
708 spin_unlock_bh(&sc->chan_lock);
709}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530710
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530711void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
712 enum ath_chanctx_event ev)
713{
714 if (sc->sched.beacon_pending)
715 ath_chanctx_event(sc, NULL, ev);
716}
717
Sujith Manoharana2b28602014-09-15 11:25:50 +0530718void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530719 enum ath_chanctx_event ev)
720{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530721 ath_chanctx_event(sc, NULL, ev);
722}
723
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530724static int ath_scan_channel_duration(struct ath_softc *sc,
725 struct ieee80211_channel *chan)
726{
727 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
728
729 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
730 return (HZ / 9); /* ~110 ms */
731
732 return (HZ / 16); /* ~60 ms */
733}
734
Sujith Manoharan922c9432014-08-23 13:29:15 +0530735static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
736 struct cfg80211_chan_def *chandef)
737{
738 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
739
740 spin_lock_bh(&sc->chan_lock);
741
742 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
743 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530744 if (chandef)
745 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530746
747 sc->sched.offchannel_pending = true;
748 sc->sched.wait_switch = true;
749 sc->sched.offchannel_duration =
750 jiffies_to_usecs(sc->offchannel.duration) +
751 sc->sched.channel_switch_time;
752
Sujith Manoharan922c9432014-08-23 13:29:15 +0530753 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530754 ath_dbg(common, CHAN_CTX,
755 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530756 return;
757 }
758
759 sc->next_chan = ctx;
760 if (chandef) {
761 ctx->chandef = *chandef;
762 ath_dbg(common, CHAN_CTX,
763 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
764 }
765
766 if (sc->next_chan == &sc->offchannel.chan) {
767 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530768 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530769 sc->sched.channel_switch_time;
770
771 if (chandef) {
772 ath_dbg(common, CHAN_CTX,
773 "Offchannel duration for chan %d MHz : %u\n",
774 chandef->center_freq1,
775 sc->sched.offchannel_duration);
776 }
777 }
778 spin_unlock_bh(&sc->chan_lock);
779 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
780}
781
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530782static void ath_chanctx_offchan_switch(struct ath_softc *sc,
783 struct ieee80211_channel *chan)
784{
785 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
786 struct cfg80211_chan_def chandef;
787
788 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
789 ath_dbg(common, CHAN_CTX,
790 "Channel definition created: %d MHz\n", chandef.center_freq1);
791
792 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
793}
794
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530795static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
796 bool active)
797{
798 struct ath_chanctx *ctx;
799
800 ath_for_each_chanctx(sc, ctx) {
801 if (!ctx->assigned || list_empty(&ctx->vifs))
802 continue;
803 if (active && !ctx->active)
804 continue;
805
806 if (ctx->switch_after_beacon)
807 return ctx;
808 }
809
810 return &sc->chanctx[0];
811}
812
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530813static void
814ath_scan_next_channel(struct ath_softc *sc)
815{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530816 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530817 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
818 struct ieee80211_channel *chan;
819
820 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530821 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530822 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
823 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530824 sc->offchannel.scan_idx,
825 req->n_channels);
826
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530827 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
828 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
829 NULL);
830 return;
831 }
832
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530833 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530834 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530835 sc->offchannel.scan_idx);
836
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530837 chan = req->channels[sc->offchannel.scan_idx++];
838 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
839 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530840
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530841 ath_chanctx_offchan_switch(sc, chan);
842}
843
844void ath_offchannel_next(struct ath_softc *sc)
845{
846 struct ieee80211_vif *vif;
847
848 if (sc->offchannel.scan_req) {
849 vif = sc->offchannel.scan_vif;
850 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
851 ath_scan_next_channel(sc);
852 } else if (sc->offchannel.roc_vif) {
853 vif = sc->offchannel.roc_vif;
854 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530855 sc->offchannel.duration =
856 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530857 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
858 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
859 } else {
860 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
861 NULL);
862 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
863 if (sc->ps_idle)
864 ath_cancel_work(sc);
865 }
866}
867
868void ath_roc_complete(struct ath_softc *sc, bool abort)
869{
Sujith Manoharan4b60af42014-10-02 06:33:12 +0530870 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
871
872 if (abort)
873 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
874 else
875 ath_dbg(common, CHAN_CTX, "RoC expired\n");
876
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530877 sc->offchannel.roc_vif = NULL;
878 sc->offchannel.roc_chan = NULL;
879 if (!abort)
880 ieee80211_remain_on_channel_expired(sc->hw);
881 ath_offchannel_next(sc);
882 ath9k_ps_restore(sc);
883}
884
885void ath_scan_complete(struct ath_softc *sc, bool abort)
886{
887 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
888
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530889 if (abort)
890 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
891 else
892 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
893
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530894 sc->offchannel.scan_req = NULL;
895 sc->offchannel.scan_vif = NULL;
896 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
897 ieee80211_scan_completed(sc->hw, abort);
898 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530899 spin_lock_bh(&sc->chan_lock);
900 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
901 sc->sched.force_noa_update = true;
902 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530903 ath_offchannel_next(sc);
904 ath9k_ps_restore(sc);
905}
906
907static void ath_scan_send_probe(struct ath_softc *sc,
908 struct cfg80211_ssid *ssid)
909{
910 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
911 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
912 struct ath_tx_control txctl = {};
913 struct sk_buff *skb;
914 struct ieee80211_tx_info *info;
915 int band = sc->offchannel.chan.chandef.chan->band;
916
917 skb = ieee80211_probereq_get(sc->hw, vif,
918 ssid->ssid, ssid->ssid_len, req->ie_len);
919 if (!skb)
920 return;
921
922 info = IEEE80211_SKB_CB(skb);
923 if (req->no_cck)
924 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
925
926 if (req->ie_len)
927 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
928
929 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
930
931 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
932 goto error;
933
934 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
935 txctl.force_channel = true;
936 if (ath_tx_start(sc->hw, skb, &txctl))
937 goto error;
938
939 return;
940
941error:
942 ieee80211_free_txskb(sc->hw, skb);
943}
944
945static void ath_scan_channel_start(struct ath_softc *sc)
946{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530947 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530948 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
949 int i;
950
951 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
952 req->n_ssids) {
953 for (i = 0; i < req->n_ssids; i++)
954 ath_scan_send_probe(sc, &req->ssids[i]);
955
956 }
957
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530958 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530959 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530960
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530961 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
962 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
963}
964
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530965static void ath_chanctx_timer(unsigned long data)
966{
967 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530968 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
969
970 ath_dbg(common, CHAN_CTX,
971 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530972
973 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
974}
975
976static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530977{
978 struct ath_softc *sc = (struct ath_softc *)data;
979 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530980 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
981
Sujith Manoharan878066e2014-08-27 12:07:24 +0530982 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530983 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530984
985 switch (sc->offchannel.state) {
986 case ATH_OFFCHANNEL_PROBE_WAIT:
987 if (!sc->offchannel.scan_req)
988 return;
989
990 /* get first active channel context */
991 ctx = ath_chanctx_get_oper_chan(sc, true);
992 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +0530993 ath_dbg(common, CHAN_CTX,
994 "Switch to oper/active context, "
995 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
996
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530997 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
998 ath_chanctx_switch(sc, ctx, NULL);
999 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1000 break;
1001 }
1002 /* fall through */
1003 case ATH_OFFCHANNEL_SUSPEND:
1004 if (!sc->offchannel.scan_req)
1005 return;
1006
1007 ath_scan_next_channel(sc);
1008 break;
1009 case ATH_OFFCHANNEL_ROC_START:
1010 case ATH_OFFCHANNEL_ROC_WAIT:
1011 ctx = ath_chanctx_get_oper_chan(sc, false);
1012 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1013 ath_chanctx_switch(sc, ctx, NULL);
1014 break;
1015 default:
1016 break;
1017 }
1018}
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301019
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301020static bool
1021ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1022 bool powersave)
1023{
1024 struct ieee80211_vif *vif = avp->vif;
1025 struct ieee80211_sta *sta = NULL;
1026 struct ieee80211_hdr_3addr *nullfunc;
1027 struct ath_tx_control txctl;
1028 struct sk_buff *skb;
1029 int band = sc->cur_chan->chandef.chan->band;
1030
1031 switch (vif->type) {
1032 case NL80211_IFTYPE_STATION:
Sujith Manoharancb355822014-09-17 14:45:56 +05301033 if (!avp->assoc)
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301034 return false;
1035
1036 skb = ieee80211_nullfunc_get(sc->hw, vif);
1037 if (!skb)
1038 return false;
1039
1040 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1041 if (powersave)
1042 nullfunc->frame_control |=
1043 cpu_to_le16(IEEE80211_FCTL_PM);
1044
1045 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1046 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1047 dev_kfree_skb_any(skb);
1048 return false;
1049 }
1050 break;
1051 default:
1052 return false;
1053 }
1054
1055 memset(&txctl, 0, sizeof(txctl));
1056 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1057 txctl.sta = sta;
1058 txctl.force_channel = true;
1059 if (ath_tx_start(sc->hw, skb, &txctl)) {
1060 ieee80211_free_txskb(sc->hw, skb);
1061 return false;
1062 }
1063
1064 return true;
1065}
1066
1067static bool
1068ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1069{
1070 struct ath_vif *avp;
1071 bool sent = false;
1072
1073 rcu_read_lock();
1074 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1075 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1076 sent = true;
1077 }
1078 rcu_read_unlock();
1079
1080 return sent;
1081}
1082
1083static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1084{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301085 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1086
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301087 if (sc->cur_chan == &sc->offchannel.chan)
1088 return false;
1089
1090 switch (sc->sched.state) {
1091 case ATH_CHANCTX_STATE_SWITCH:
1092 return false;
1093 case ATH_CHANCTX_STATE_IDLE:
1094 if (!sc->cur_chan->switch_after_beacon)
1095 return false;
1096
Sujith Manoharan878066e2014-08-27 12:07:24 +05301097 ath_dbg(common, CHAN_CTX,
1098 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1099
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301100 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1101 break;
1102 default:
1103 break;
1104 }
1105
1106 return true;
1107}
1108
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301109static void ath_offchannel_channel_change(struct ath_softc *sc)
1110{
1111 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1112
Sujith Manoharan878066e2014-08-27 12:07:24 +05301113 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301114 __func__, offchannel_state_string(sc->offchannel.state));
1115
1116 switch (sc->offchannel.state) {
1117 case ATH_OFFCHANNEL_PROBE_SEND:
1118 if (!sc->offchannel.scan_req)
1119 return;
1120
1121 if (sc->cur_chan->chandef.chan !=
1122 sc->offchannel.chan.chandef.chan)
1123 return;
1124
1125 ath_scan_channel_start(sc);
1126 break;
1127 case ATH_OFFCHANNEL_IDLE:
1128 if (!sc->offchannel.scan_req)
1129 return;
1130
1131 ath_scan_complete(sc, false);
1132 break;
1133 case ATH_OFFCHANNEL_ROC_START:
1134 if (sc->cur_chan != &sc->offchannel.chan)
1135 break;
1136
1137 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301138 mod_timer(&sc->offchannel.timer,
1139 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301140 ieee80211_ready_on_channel(sc->hw);
1141 break;
1142 case ATH_OFFCHANNEL_ROC_DONE:
1143 ath_roc_complete(sc, false);
1144 break;
1145 default:
1146 break;
1147 }
1148}
1149
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301150void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1151{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301152 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301153 struct ath_chanctx *old_ctx;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301154 struct timespec ts;
1155 bool measure_time = false;
1156 bool send_ps = false;
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301157 bool queues_stopped = false;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301158
1159 spin_lock_bh(&sc->chan_lock);
1160 if (!sc->next_chan) {
1161 spin_unlock_bh(&sc->chan_lock);
1162 return;
1163 }
1164
1165 if (!force && ath_chanctx_defer_switch(sc)) {
1166 spin_unlock_bh(&sc->chan_lock);
1167 return;
1168 }
1169
Sujith Manoharan878066e2014-08-27 12:07:24 +05301170 ath_dbg(common, CHAN_CTX,
1171 "%s: current: %d MHz, next: %d MHz\n",
1172 __func__,
1173 sc->cur_chan->chandef.center_freq1,
1174 sc->next_chan->chandef.center_freq1);
1175
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301176 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301177 ath_dbg(common, CHAN_CTX,
1178 "Stopping current chanctx: %d\n",
1179 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301180 sc->cur_chan->stopped = true;
1181 spin_unlock_bh(&sc->chan_lock);
1182
1183 if (sc->next_chan == &sc->offchannel.chan) {
1184 getrawmonotonic(&ts);
1185 measure_time = true;
1186 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301187
1188 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1189 queues_stopped = true;
1190
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301191 __ath9k_flush(sc->hw, ~0, true, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301192
1193 if (ath_chanctx_send_ps_frame(sc, true))
Sujith Manoharane2d389b2014-10-17 07:40:18 +05301194 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1195 false, false);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301196
1197 send_ps = true;
1198 spin_lock_bh(&sc->chan_lock);
1199
1200 if (sc->cur_chan != &sc->offchannel.chan) {
1201 getrawmonotonic(&sc->cur_chan->tsf_ts);
1202 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1203 }
1204 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301205 old_ctx = sc->cur_chan;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301206 sc->cur_chan = sc->next_chan;
1207 sc->cur_chan->stopped = false;
1208 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301209
1210 if (!sc->sched.offchannel_pending)
1211 sc->sched.offchannel_duration = 0;
1212
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301213 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1214 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1215
1216 spin_unlock_bh(&sc->chan_lock);
1217
1218 if (sc->sc_ah->chip_fullsleep ||
1219 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1220 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301221 ath_dbg(common, CHAN_CTX,
1222 "%s: Set channel %d MHz\n",
1223 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301224 ath_set_channel(sc);
1225 if (measure_time)
1226 sc->sched.channel_switch_time =
1227 ath9k_hw_get_tsf_offset(&ts, NULL);
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301228 /*
1229 * A reset will ensure that all queues are woken up,
1230 * so there is no need to awaken them again.
1231 */
1232 goto out;
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301233 }
Sujith Manoharane2cba8d2014-10-02 06:33:20 +05301234
1235 if (queues_stopped)
1236 ath9k_chanctx_wake_queues(sc, old_ctx);
1237out:
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301238 if (send_ps)
1239 ath_chanctx_send_ps_frame(sc, false);
1240
1241 ath_offchannel_channel_change(sc);
1242 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1243}
1244
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301245static void ath_chanctx_work(struct work_struct *work)
1246{
1247 struct ath_softc *sc = container_of(work, struct ath_softc,
1248 chanctx_work);
1249 mutex_lock(&sc->mutex);
1250 ath_chanctx_set_next(sc, false);
1251 mutex_unlock(&sc->mutex);
1252}
1253
Sujith Manoharane90e3022014-08-23 13:29:20 +05301254void ath9k_offchannel_init(struct ath_softc *sc)
1255{
1256 struct ath_chanctx *ctx;
1257 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1258 struct ieee80211_supported_band *sband;
1259 struct ieee80211_channel *chan;
1260 int i;
1261
1262 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1263 if (!sband->n_channels)
1264 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1265
1266 chan = &sband->channels[0];
1267
1268 ctx = &sc->offchannel.chan;
1269 INIT_LIST_HEAD(&ctx->vifs);
1270 ctx->txpower = ATH_TXPOWER_MAX;
1271 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1272
1273 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1274 INIT_LIST_HEAD(&ctx->acq[i]);
1275
1276 sc->offchannel.chan.offchannel = true;
1277}
1278
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301279void ath9k_init_channel_context(struct ath_softc *sc)
1280{
1281 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1282
1283 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1284 (unsigned long)sc);
1285 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1286 (unsigned long)sc);
Sujith Manoharanc6500ea2014-10-17 07:40:22 +05301287
1288 init_completion(&sc->go_beacon);
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301289}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301290
Sujith Manoharanea22df22014-08-23 13:29:07 +05301291void ath9k_deinit_channel_context(struct ath_softc *sc)
1292{
1293 cancel_work_sync(&sc->chanctx_work);
1294}
1295
Sujith Manoharan499afac2014-08-22 20:39:31 +05301296bool ath9k_is_chanctx_enabled(void)
1297{
1298 return (ath9k_use_chanctx == 1);
1299}
1300
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301301/********************/
1302/* Queue management */
1303/********************/
1304
Sujith Manoharana064eaa2014-10-02 06:33:18 +05301305void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1306{
1307 struct ath_hw *ah = sc->sc_ah;
1308 int i;
1309
1310 if (ctx == &sc->offchannel.chan) {
1311 ieee80211_stop_queue(sc->hw,
1312 sc->hw->offchannel_tx_hw_queue);
1313 } else {
1314 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1315 ieee80211_stop_queue(sc->hw,
1316 ctx->hw_queue_base + i);
1317 }
1318
1319 if (ah->opmode == NL80211_IFTYPE_AP)
1320 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1321}
1322
1323
Sujith Manoharanb3903152014-10-02 06:33:17 +05301324void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301325{
1326 struct ath_hw *ah = sc->sc_ah;
1327 int i;
1328
Sujith Manoharanb3903152014-10-02 06:33:17 +05301329 if (ctx == &sc->offchannel.chan) {
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301330 ieee80211_wake_queue(sc->hw,
1331 sc->hw->offchannel_tx_hw_queue);
1332 } else {
1333 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1334 ieee80211_wake_queue(sc->hw,
Sujith Manoharanb3903152014-10-02 06:33:17 +05301335 ctx->hw_queue_base + i);
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301336 }
1337
1338 if (ah->opmode == NL80211_IFTYPE_AP)
1339 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1340}
1341
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301342/*****************/
1343/* P2P Powersave */
1344/*****************/
1345
1346static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301347{
1348 struct ath_hw *ah = sc->sc_ah;
1349 s32 tsf, target_tsf;
1350
1351 if (!avp || !avp->noa.has_next_tsf)
1352 return;
1353
1354 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1355
1356 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1357
1358 target_tsf = avp->noa.next_tsf;
1359 if (!avp->noa.absent)
1360 target_tsf -= ATH_P2P_PS_STOP_TIME;
1361
1362 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1363 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1364
1365 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1366}
1367
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301368static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1369{
1370 struct ath_vif *avp = (void *)vif->drv_priv;
1371 u32 tsf;
1372
1373 if (!sc->p2p_ps_timer)
1374 return;
1375
1376 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1377 return;
1378
1379 sc->p2p_ps_vif = avp;
1380 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1381 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1382 ath9k_update_p2p_ps_timer(sc, avp);
1383}
1384
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301385static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1386{
1387 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1388 u8 switch_time, ctwin;
1389
1390 /*
1391 * Channel switch in multi-channel mode is deferred
1392 * by a quarter beacon interval when handling
1393 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1394 * interface is guaranteed to be discoverable
1395 * for that duration after a TBTT.
1396 */
1397 switch_time = cur_conf->beacon_interval / 4;
1398
1399 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1400 if (ctwin && (ctwin < switch_time))
1401 return ctwin;
1402
1403 if (switch_time < P2P_DEFAULT_CTWIN)
1404 return 0;
1405
1406 return P2P_DEFAULT_CTWIN;
1407}
1408
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301409void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1410 struct sk_buff *skb)
1411{
1412 static const u8 noa_ie_hdr[] = {
1413 WLAN_EID_VENDOR_SPECIFIC, /* type */
1414 0, /* length */
1415 0x50, 0x6f, 0x9a, /* WFA OUI */
1416 0x09, /* P2P subtype */
1417 0x0c, /* Notice of Absence */
1418 0x00, /* LSB of little-endian len */
1419 0x00, /* MSB of little-endian len */
1420 };
1421
1422 struct ieee80211_p2p_noa_attr *noa;
1423 int noa_len, noa_desc, i = 0;
1424 u8 *hdr;
1425
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301426 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301427 return;
1428
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301429 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301430 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1431
1432 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1433 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1434 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1435 hdr[7] = noa_len;
1436
1437 noa = (void *) skb_put(skb, noa_len);
1438 memset(noa, 0, noa_len);
1439
1440 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301441 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1442
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301443 if (avp->noa_duration) {
1444 if (avp->periodic_noa) {
1445 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1446 noa->desc[i].count = 255;
1447 noa->desc[i].interval = cpu_to_le32(interval);
1448 } else {
1449 noa->desc[i].count = 1;
1450 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301451
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301452 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1453 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301454 i++;
1455 }
1456
1457 if (avp->offchannel_duration) {
1458 noa->desc[i].count = 1;
1459 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1460 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1461 }
1462}
1463
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301464void ath9k_p2p_ps_timer(void *priv)
1465{
1466 struct ath_softc *sc = priv;
1467 struct ath_vif *avp = sc->p2p_ps_vif;
1468 struct ieee80211_vif *vif;
1469 struct ieee80211_sta *sta;
1470 struct ath_node *an;
1471 u32 tsf;
1472
1473 del_timer_sync(&sc->sched.timer);
1474 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1475 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1476
1477 if (!avp || avp->chanctx != sc->cur_chan)
1478 return;
1479
1480 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1481 if (!avp->noa.absent)
1482 tsf += ATH_P2P_PS_STOP_TIME;
1483
1484 if (!avp->noa.has_next_tsf ||
1485 avp->noa.next_tsf - tsf > BIT(31))
1486 ieee80211_update_p2p_noa(&avp->noa, tsf);
1487
1488 ath9k_update_p2p_ps_timer(sc, avp);
1489
1490 rcu_read_lock();
1491
1492 vif = avp->vif;
Sujith Manoharancb355822014-09-17 14:45:56 +05301493 sta = ieee80211_find_sta(vif, avp->bssid);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301494 if (!sta)
1495 goto out;
1496
1497 an = (void *) sta->drv_priv;
1498 if (an->sleeping == !!avp->noa.absent)
1499 goto out;
1500
1501 an->sleeping = avp->noa.absent;
1502 if (an->sleeping)
1503 ath_tx_aggr_sleep(sta, sc, an);
1504 else
1505 ath_tx_aggr_wakeup(sc, an);
1506
1507out:
1508 rcu_read_unlock();
1509}
1510
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301511void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1512 struct ieee80211_vif *vif)
1513{
1514 unsigned long flags;
1515
1516 spin_lock_bh(&sc->sc_pcu_lock);
1517 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1518 if (!(sc->ps_flags & PS_BEACON_SYNC))
1519 ath9k_update_p2p_ps(sc, vif);
1520 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1521 spin_unlock_bh(&sc->sc_pcu_lock);
1522}
1523
1524void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1525{
1526 if (sc->p2p_ps_vif)
1527 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1528}
1529
1530void ath9k_p2p_remove_vif(struct ath_softc *sc,
1531 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301532{
1533 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301534
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301535 spin_lock_bh(&sc->sc_pcu_lock);
1536 if (avp == sc->p2p_ps_vif) {
1537 sc->p2p_ps_vif = NULL;
1538 ath9k_update_p2p_ps_timer(sc, NULL);
1539 }
1540 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301541}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301542
1543int ath9k_init_p2p(struct ath_softc *sc)
1544{
1545 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1546 NULL, sc, AR_FIRST_NDP_TIMER);
1547 if (!sc->p2p_ps_timer)
1548 return -ENOMEM;
1549
1550 return 0;
1551}
1552
1553void ath9k_deinit_p2p(struct ath_softc *sc)
1554{
1555 if (sc->p2p_ps_timer)
1556 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1557}
1558
1559#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */