blob: e2712b08c7975f2d1f9664d55b4f788c90b2a435 [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];
69 r = ath_reset_internal(sc, hchan);
70 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;
Felix Fietkau04535312014-06-11 16:17:51 +0530120 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
121 INIT_LIST_HEAD(&ctx->acq[j]);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530122 }
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530123}
124
Felix Fietkaubff11762014-06-11 16:17:52 +0530125void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
126 struct cfg80211_chan_def *chandef)
127{
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530128 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkaubff11762014-06-11 16:17:52 +0530129 bool cur_chan;
130
131 spin_lock_bh(&sc->chan_lock);
132 if (chandef)
133 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
134 cur_chan = sc->cur_chan == ctx;
135 spin_unlock_bh(&sc->chan_lock);
136
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530137 if (!cur_chan) {
138 ath_dbg(common, CHAN_CTX,
139 "Current context differs from the new context\n");
Felix Fietkaubff11762014-06-11 16:17:52 +0530140 return;
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530141 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530142
143 ath_set_channel(sc);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530144}
Felix Fietkau78b21942014-06-11 16:17:55 +0530145
Sujith Manoharan27babf92014-08-23 13:29:16 +0530146#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
147
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +0530148/**********************************************************/
149/* Functions to handle the channel context state machine. */
150/**********************************************************/
151
Sujith Manoharan27babf92014-08-23 13:29:16 +0530152static const char *offchannel_state_string(enum ath_offchannel_state state)
153{
Sujith Manoharan27babf92014-08-23 13:29:16 +0530154 switch (state) {
155 case_rtn_string(ATH_OFFCHANNEL_IDLE);
156 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
157 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
158 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
159 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
160 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
161 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
162 default:
163 return "unknown";
164 }
165}
166
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530167static const char *chanctx_event_string(enum ath_chanctx_event ev)
168{
169 switch (ev) {
170 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
171 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
172 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
173 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
174 case_rtn_string(ATH_CHANCTX_EVENT_ASSOC);
175 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
176 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
177 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
178 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
179 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
180 default:
181 return "unknown";
182 }
183}
184
185static const char *chanctx_state_string(enum ath_chanctx_state state)
186{
187 switch (state) {
188 case_rtn_string(ATH_CHANCTX_STATE_IDLE);
189 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
190 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
191 case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
192 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
193 default:
194 return "unknown";
195 }
196}
197
Sujith Manoharana09798f2014-08-23 13:29:21 +0530198void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
199{
200 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
201 struct ath_vif *avp;
202 bool active = false;
203 u8 n_active = 0;
204
205 if (!ctx)
206 return;
207
208 list_for_each_entry(avp, &ctx->vifs, list) {
209 struct ieee80211_vif *vif = avp->vif;
210
211 switch (vif->type) {
212 case NL80211_IFTYPE_P2P_CLIENT:
213 case NL80211_IFTYPE_STATION:
214 if (vif->bss_conf.assoc)
215 active = true;
216 break;
217 default:
218 active = true;
219 break;
220 }
221 }
222 ctx->active = active;
223
224 ath_for_each_chanctx(sc, ctx) {
225 if (!ctx->assigned || list_empty(&ctx->vifs))
226 continue;
227 n_active++;
228 }
229
230 if (n_active <= 1) {
231 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
232 return;
233 }
234 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
235 return;
236
237 if (ath9k_is_chanctx_enabled()) {
238 ath_chanctx_event(sc, NULL,
239 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
240 }
241}
242
Felix Fietkau58b57372014-06-11 16:18:08 +0530243static struct ath_chanctx *
244ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
245{
246 int idx = ctx - &sc->chanctx[0];
247
248 return &sc->chanctx[!idx];
249}
250
251static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
252{
253 struct ath_chanctx *prev, *cur;
254 struct timespec ts;
255 u32 cur_tsf, prev_tsf, beacon_int;
256 s32 offset;
257
258 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
259
260 cur = sc->cur_chan;
261 prev = ath_chanctx_get_next(sc, cur);
262
263 getrawmonotonic(&ts);
264 cur_tsf = (u32) cur->tsf_val +
265 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
266
267 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
268 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
269
270 /* Adjust the TSF time of the AP chanctx to keep its beacons
271 * at half beacon interval offset relative to the STA chanctx.
272 */
273 offset = cur_tsf - prev_tsf;
274
275 /* Ignore stale data or spurious timestamps */
276 if (offset < 0 || offset > 3 * beacon_int)
277 return;
278
279 offset = beacon_int / 2 - (offset % beacon_int);
280 prev->tsf_val += offset;
281}
282
Felix Fietkau42eda112014-06-11 16:18:14 +0530283/* Configure the TSF based hardware timer for a channel switch.
284 * Also set up backup software timer, in case the gen timer fails.
285 * This could be caused by a hardware reset.
286 */
287static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
288{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530289 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau42eda112014-06-11 16:18:14 +0530290 struct ath_hw *ah = sc->sc_ah;
291
292 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
293 tsf_time -= ath9k_hw_gettsf32(ah);
294 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
Sujith Manoharan1a7c5b72014-08-27 12:07:25 +0530295 mod_timer(&sc->sched.timer, jiffies + tsf_time);
Sujith Manoharan878066e2014-08-27 12:07:24 +0530296
297 ath_dbg(common, CHAN_CTX,
298 "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time));
Felix Fietkau42eda112014-06-11 16:18:14 +0530299}
300
Felix Fietkau748299f2014-06-11 16:18:04 +0530301void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
302 enum ath_chanctx_event ev)
303{
304 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530305 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530306 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530307 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530308 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530309 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530310 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530311
312 if (vif)
313 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530314
315 spin_lock_bh(&sc->chan_lock);
316
Sujith Manoharan878066e2014-08-27 12:07:24 +0530317 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
318 sc->cur_chan->chandef.center_freq1,
319 chanctx_event_string(ev),
320 chanctx_state_string(sc->sched.state));
321
Felix Fietkau748299f2014-06-11 16:18:04 +0530322 switch (ev) {
323 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530324 if (avp->offchannel_duration)
325 avp->offchannel_duration = 0;
326
Sujith Manoharan878066e2014-08-27 12:07:24 +0530327 if (avp->chanctx != sc->cur_chan) {
328 ath_dbg(common, CHAN_CTX,
329 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530330 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530331 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530332
Sujith Manoharan367b3412014-09-05 09:50:57 +0530333 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530334 sc->sched.offchannel_pending = false;
335 sc->next_chan = &sc->offchannel.chan;
336 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530337 ath_dbg(common, CHAN_CTX,
338 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530339 }
340
341 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
342 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
343 sc->next_chan = ctx;
344 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530345 ath_dbg(common, CHAN_CTX,
346 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530347 }
348
349 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530350 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530351 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530352 ath_dbg(common, CHAN_CTX,
353 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
354 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530355
Felix Fietkau748299f2014-06-11 16:18:04 +0530356 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
357 break;
358
Sujith Manoharan878066e2014-08-27 12:07:24 +0530359 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
360
Felix Fietkau748299f2014-06-11 16:18:04 +0530361 sc->sched.beacon_pending = true;
362 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530363
Felix Fietkau74148632014-06-11 16:18:11 +0530364 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530365 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
366
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530367 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530368 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530369 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530370 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530371
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530372 /*
373 * If an offchannel switch is scheduled to happen after
374 * a beacon transmission, update the NoA with one-shot
375 * values and increment the index.
376 */
377 if (sc->next_chan == &sc->offchannel.chan) {
378 avp->noa_index++;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530379 avp->offchannel_start = tsf_time;
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530380 avp->offchannel_duration = sc->sched.offchannel_duration;
381
382 ath_dbg(common, CHAN_CTX,
383 "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n",
384 avp->offchannel_duration,
385 avp->offchannel_start,
386 avp->noa_index);
387
388 /*
389 * When multiple contexts are active, the NoA
390 * has to be recalculated and advertised after
391 * an offchannel operation.
392 */
393 if (ctx->active && avp->noa_duration)
394 avp->noa_duration = 0;
395
396 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530397 }
398
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530399 /*
400 * Clear the extend_absence flag if it had been
401 * set during the previous beacon transmission,
402 * since we need to revert to the normal NoA
403 * schedule.
404 */
405 if (ctx->active && sc->sched.extend_absence) {
406 avp->noa_duration = 0;
407 sc->sched.extend_absence = false;
408 }
409
410 /* If at least two consecutive beacons were missed on the STA
411 * chanctx, stay on the STA channel for one extra beacon period,
412 * to resync the timer properly.
413 */
414 if (ctx->active && sc->sched.beacon_miss >= 2) {
415 avp->noa_duration = 0;
416 sc->sched.extend_absence = true;
417 }
Sujith Manoharana2b28602014-09-15 11:25:50 +0530418
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530419 /* Prevent wrap-around issues */
420 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
421 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530422
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530423 /*
424 * If multiple contexts are active, start periodic
425 * NoA and increment the index for the first
426 * announcement.
427 */
428 if (ctx->active &&
429 (!avp->noa_duration || sc->sched.force_noa_update)) {
430 avp->noa_index++;
431 avp->noa_start = tsf_time;
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530432
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530433 if (sc->sched.extend_absence)
434 avp->noa_duration = (3 * beacon_int / 2) +
435 sc->sched.channel_switch_time;
436 else
437 avp->noa_duration =
438 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
439 sc->sched.channel_switch_time;
440
441 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
442 sc->sched.extend_absence)
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530443 avp->periodic_noa = false;
444 else
445 avp->periodic_noa = true;
446
447 ath_dbg(common, CHAN_CTX,
448 "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
449 avp->noa_duration,
450 avp->noa_start,
451 avp->noa_index,
452 avp->periodic_noa);
453 }
454
455 if (ctx->active && sc->sched.force_noa_update)
456 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530457
Felix Fietkau748299f2014-06-11 16:18:04 +0530458 break;
459 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530460 if (!sc->sched.beacon_pending) {
461 ath_dbg(common, CHAN_CTX,
462 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530463 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530464 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530465
466 sc->sched.beacon_pending = false;
467 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
468 break;
469
Sujith Manoharan878066e2014-08-27 12:07:24 +0530470 ath_dbg(common, CHAN_CTX,
471 "Move chanctx state to WAIT_FOR_TIMER\n");
472
Felix Fietkau748299f2014-06-11 16:18:04 +0530473 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530474 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530475 break;
476 case ATH_CHANCTX_EVENT_TSF_TIMER:
477 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
478 break;
479
Felix Fietkauec70abe2014-06-11 16:18:12 +0530480 if (!sc->cur_chan->switch_after_beacon &&
481 sc->sched.beacon_pending)
482 sc->sched.beacon_miss++;
483
Sujith Manoharan878066e2014-08-27 12:07:24 +0530484 ath_dbg(common, CHAN_CTX,
485 "Move chanctx state to SWITCH\n");
486
Felix Fietkau748299f2014-06-11 16:18:04 +0530487 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
488 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
489 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530490 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530491 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
492 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530493 break;
494
495 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530496 sc->sched.beacon_pending = false;
497 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530498
499 /* TSF time might have been updated by the incoming beacon,
500 * need update the channel switch timer to reflect the change.
501 */
502 tsf_time = sc->sched.switch_start_time;
503 tsf_time -= (u32) sc->cur_chan->tsf_val +
504 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
505 tsf_time += ath9k_hw_gettsf32(ah);
506
Felix Fietkau42eda112014-06-11 16:18:14 +0530507
508 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530509 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530510 case ATH_CHANCTX_EVENT_ASSOC:
511 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
512 avp->chanctx != sc->cur_chan)
513 break;
514
Sujith Manoharan878066e2014-08-27 12:07:24 +0530515 ath_dbg(common, CHAN_CTX,
516 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
517
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530518 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
519 /* fall through */
520 case ATH_CHANCTX_EVENT_SWITCH:
521 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
522 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
523 sc->cur_chan->switch_after_beacon ||
524 sc->cur_chan == &sc->offchannel.chan)
525 break;
526
527 /* If this is a station chanctx, stay active for a half
528 * beacon period (minus channel switch time)
529 */
530 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530531 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530532
Sujith Manoharan878066e2014-08-27 12:07:24 +0530533 ath_dbg(common, CHAN_CTX,
534 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
535
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530536 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530537 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530538
539 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530540
541 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530542 sc->sched.beacon_miss = 0;
543 tsf_time *= 3;
544 }
545
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530546 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530547 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530548 sc->sched.switch_start_time = tsf_time;
549
Felix Fietkau42eda112014-06-11 16:18:14 +0530550 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530551 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530552 break;
553 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
554 if (sc->cur_chan == &sc->offchannel.chan ||
555 sc->cur_chan->switch_after_beacon)
556 break;
557
558 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
559 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
560 break;
561 case ATH_CHANCTX_EVENT_UNASSIGN:
562 if (sc->cur_chan->assigned) {
563 if (sc->next_chan && !sc->next_chan->assigned &&
564 sc->next_chan != &sc->offchannel.chan)
565 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
566 break;
567 }
568
569 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
570 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
571 if (!ctx->assigned)
572 break;
573
574 sc->next_chan = ctx;
575 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
576 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530577 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530578 /*
579 * When adding a new channel context, check if a scan
580 * is in progress and abort it since the addition of
581 * a new channel context is usually followed by VIF
582 * assignment, in which case we have to start multi-channel
583 * operation.
584 */
585 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
586 ath_dbg(common, CHAN_CTX,
587 "Aborting HW scan to add new context\n");
588
589 spin_unlock_bh(&sc->chan_lock);
590 del_timer_sync(&sc->offchannel.timer);
591 ath_scan_complete(sc, true);
592 spin_lock_bh(&sc->chan_lock);
593 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530594 break;
595 case ATH_CHANCTX_EVENT_CHANGE:
596 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530597 }
598
599 spin_unlock_bh(&sc->chan_lock);
600}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530601
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530602void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
603 enum ath_chanctx_event ev)
604{
605 if (sc->sched.beacon_pending)
606 ath_chanctx_event(sc, NULL, ev);
607}
608
Sujith Manoharana2b28602014-09-15 11:25:50 +0530609void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530610 enum ath_chanctx_event ev)
611{
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530612 ath_chanctx_event(sc, NULL, ev);
613}
614
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530615static int ath_scan_channel_duration(struct ath_softc *sc,
616 struct ieee80211_channel *chan)
617{
618 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
619
620 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
621 return (HZ / 9); /* ~110 ms */
622
623 return (HZ / 16); /* ~60 ms */
624}
625
Sujith Manoharan922c9432014-08-23 13:29:15 +0530626static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
627 struct cfg80211_chan_def *chandef)
628{
629 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
630
631 spin_lock_bh(&sc->chan_lock);
632
633 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
634 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530635 if (chandef)
636 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530637
638 sc->sched.offchannel_pending = true;
639 sc->sched.wait_switch = true;
640 sc->sched.offchannel_duration =
641 jiffies_to_usecs(sc->offchannel.duration) +
642 sc->sched.channel_switch_time;
643
Sujith Manoharan922c9432014-08-23 13:29:15 +0530644 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530645 ath_dbg(common, CHAN_CTX,
646 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530647 return;
648 }
649
650 sc->next_chan = ctx;
651 if (chandef) {
652 ctx->chandef = *chandef;
653 ath_dbg(common, CHAN_CTX,
654 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
655 }
656
657 if (sc->next_chan == &sc->offchannel.chan) {
658 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530659 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530660 sc->sched.channel_switch_time;
661
662 if (chandef) {
663 ath_dbg(common, CHAN_CTX,
664 "Offchannel duration for chan %d MHz : %u\n",
665 chandef->center_freq1,
666 sc->sched.offchannel_duration);
667 }
668 }
669 spin_unlock_bh(&sc->chan_lock);
670 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
671}
672
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530673static void ath_chanctx_offchan_switch(struct ath_softc *sc,
674 struct ieee80211_channel *chan)
675{
676 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
677 struct cfg80211_chan_def chandef;
678
679 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
680 ath_dbg(common, CHAN_CTX,
681 "Channel definition created: %d MHz\n", chandef.center_freq1);
682
683 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
684}
685
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530686static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
687 bool active)
688{
689 struct ath_chanctx *ctx;
690
691 ath_for_each_chanctx(sc, ctx) {
692 if (!ctx->assigned || list_empty(&ctx->vifs))
693 continue;
694 if (active && !ctx->active)
695 continue;
696
697 if (ctx->switch_after_beacon)
698 return ctx;
699 }
700
701 return &sc->chanctx[0];
702}
703
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530704static void
705ath_scan_next_channel(struct ath_softc *sc)
706{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530707 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530708 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
709 struct ieee80211_channel *chan;
710
711 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530712 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530713 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
714 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530715 sc->offchannel.scan_idx,
716 req->n_channels);
717
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530718 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
719 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
720 NULL);
721 return;
722 }
723
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530724 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530725 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530726 sc->offchannel.scan_idx);
727
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530728 chan = req->channels[sc->offchannel.scan_idx++];
729 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
730 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530731
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530732 ath_chanctx_offchan_switch(sc, chan);
733}
734
735void ath_offchannel_next(struct ath_softc *sc)
736{
737 struct ieee80211_vif *vif;
738
739 if (sc->offchannel.scan_req) {
740 vif = sc->offchannel.scan_vif;
741 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
742 ath_scan_next_channel(sc);
743 } else if (sc->offchannel.roc_vif) {
744 vif = sc->offchannel.roc_vif;
745 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530746 sc->offchannel.duration =
747 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530748 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
749 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
750 } else {
751 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
752 NULL);
753 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
754 if (sc->ps_idle)
755 ath_cancel_work(sc);
756 }
757}
758
759void ath_roc_complete(struct ath_softc *sc, bool abort)
760{
761 sc->offchannel.roc_vif = NULL;
762 sc->offchannel.roc_chan = NULL;
763 if (!abort)
764 ieee80211_remain_on_channel_expired(sc->hw);
765 ath_offchannel_next(sc);
766 ath9k_ps_restore(sc);
767}
768
769void ath_scan_complete(struct ath_softc *sc, bool abort)
770{
771 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
772
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530773 if (abort)
774 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
775 else
776 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
777
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530778 sc->offchannel.scan_req = NULL;
779 sc->offchannel.scan_vif = NULL;
780 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
781 ieee80211_scan_completed(sc->hw, abort);
782 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530783 spin_lock_bh(&sc->chan_lock);
784 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
785 sc->sched.force_noa_update = true;
786 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530787 ath_offchannel_next(sc);
788 ath9k_ps_restore(sc);
789}
790
791static void ath_scan_send_probe(struct ath_softc *sc,
792 struct cfg80211_ssid *ssid)
793{
794 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
795 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
796 struct ath_tx_control txctl = {};
797 struct sk_buff *skb;
798 struct ieee80211_tx_info *info;
799 int band = sc->offchannel.chan.chandef.chan->band;
800
801 skb = ieee80211_probereq_get(sc->hw, vif,
802 ssid->ssid, ssid->ssid_len, req->ie_len);
803 if (!skb)
804 return;
805
806 info = IEEE80211_SKB_CB(skb);
807 if (req->no_cck)
808 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
809
810 if (req->ie_len)
811 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
812
813 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
814
815 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
816 goto error;
817
818 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
819 txctl.force_channel = true;
820 if (ath_tx_start(sc->hw, skb, &txctl))
821 goto error;
822
823 return;
824
825error:
826 ieee80211_free_txskb(sc->hw, skb);
827}
828
829static void ath_scan_channel_start(struct ath_softc *sc)
830{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530831 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530832 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
833 int i;
834
835 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
836 req->n_ssids) {
837 for (i = 0; i < req->n_ssids; i++)
838 ath_scan_send_probe(sc, &req->ssids[i]);
839
840 }
841
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530842 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530843 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530844
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530845 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
846 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
847}
848
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530849static void ath_chanctx_timer(unsigned long data)
850{
851 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530852 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
853
854 ath_dbg(common, CHAN_CTX,
855 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530856
857 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
858}
859
860static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530861{
862 struct ath_softc *sc = (struct ath_softc *)data;
863 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530864 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
865
Sujith Manoharan878066e2014-08-27 12:07:24 +0530866 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530867 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530868
869 switch (sc->offchannel.state) {
870 case ATH_OFFCHANNEL_PROBE_WAIT:
871 if (!sc->offchannel.scan_req)
872 return;
873
874 /* get first active channel context */
875 ctx = ath_chanctx_get_oper_chan(sc, true);
876 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +0530877 ath_dbg(common, CHAN_CTX,
878 "Switch to oper/active context, "
879 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
880
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530881 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
882 ath_chanctx_switch(sc, ctx, NULL);
883 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
884 break;
885 }
886 /* fall through */
887 case ATH_OFFCHANNEL_SUSPEND:
888 if (!sc->offchannel.scan_req)
889 return;
890
891 ath_scan_next_channel(sc);
892 break;
893 case ATH_OFFCHANNEL_ROC_START:
894 case ATH_OFFCHANNEL_ROC_WAIT:
895 ctx = ath_chanctx_get_oper_chan(sc, false);
896 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
897 ath_chanctx_switch(sc, ctx, NULL);
898 break;
899 default:
900 break;
901 }
902}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530903
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530904static bool
905ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
906 bool powersave)
907{
908 struct ieee80211_vif *vif = avp->vif;
909 struct ieee80211_sta *sta = NULL;
910 struct ieee80211_hdr_3addr *nullfunc;
911 struct ath_tx_control txctl;
912 struct sk_buff *skb;
913 int band = sc->cur_chan->chandef.chan->band;
914
915 switch (vif->type) {
916 case NL80211_IFTYPE_STATION:
917 if (!vif->bss_conf.assoc)
918 return false;
919
920 skb = ieee80211_nullfunc_get(sc->hw, vif);
921 if (!skb)
922 return false;
923
924 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
925 if (powersave)
926 nullfunc->frame_control |=
927 cpu_to_le16(IEEE80211_FCTL_PM);
928
929 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
930 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
931 dev_kfree_skb_any(skb);
932 return false;
933 }
934 break;
935 default:
936 return false;
937 }
938
939 memset(&txctl, 0, sizeof(txctl));
940 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
941 txctl.sta = sta;
942 txctl.force_channel = true;
943 if (ath_tx_start(sc->hw, skb, &txctl)) {
944 ieee80211_free_txskb(sc->hw, skb);
945 return false;
946 }
947
948 return true;
949}
950
951static bool
952ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
953{
954 struct ath_vif *avp;
955 bool sent = false;
956
957 rcu_read_lock();
958 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
959 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
960 sent = true;
961 }
962 rcu_read_unlock();
963
964 return sent;
965}
966
967static bool ath_chanctx_defer_switch(struct ath_softc *sc)
968{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530969 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
970
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530971 if (sc->cur_chan == &sc->offchannel.chan)
972 return false;
973
974 switch (sc->sched.state) {
975 case ATH_CHANCTX_STATE_SWITCH:
976 return false;
977 case ATH_CHANCTX_STATE_IDLE:
978 if (!sc->cur_chan->switch_after_beacon)
979 return false;
980
Sujith Manoharan878066e2014-08-27 12:07:24 +0530981 ath_dbg(common, CHAN_CTX,
982 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
983
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530984 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
985 break;
986 default:
987 break;
988 }
989
990 return true;
991}
992
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530993static void ath_offchannel_channel_change(struct ath_softc *sc)
994{
995 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
996
Sujith Manoharan878066e2014-08-27 12:07:24 +0530997 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530998 __func__, offchannel_state_string(sc->offchannel.state));
999
1000 switch (sc->offchannel.state) {
1001 case ATH_OFFCHANNEL_PROBE_SEND:
1002 if (!sc->offchannel.scan_req)
1003 return;
1004
1005 if (sc->cur_chan->chandef.chan !=
1006 sc->offchannel.chan.chandef.chan)
1007 return;
1008
1009 ath_scan_channel_start(sc);
1010 break;
1011 case ATH_OFFCHANNEL_IDLE:
1012 if (!sc->offchannel.scan_req)
1013 return;
1014
1015 ath_scan_complete(sc, false);
1016 break;
1017 case ATH_OFFCHANNEL_ROC_START:
1018 if (sc->cur_chan != &sc->offchannel.chan)
1019 break;
1020
1021 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301022 mod_timer(&sc->offchannel.timer,
1023 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301024 ieee80211_ready_on_channel(sc->hw);
1025 break;
1026 case ATH_OFFCHANNEL_ROC_DONE:
1027 ath_roc_complete(sc, false);
1028 break;
1029 default:
1030 break;
1031 }
1032}
1033
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301034void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1035{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301036 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301037 struct timespec ts;
1038 bool measure_time = false;
1039 bool send_ps = false;
1040
1041 spin_lock_bh(&sc->chan_lock);
1042 if (!sc->next_chan) {
1043 spin_unlock_bh(&sc->chan_lock);
1044 return;
1045 }
1046
1047 if (!force && ath_chanctx_defer_switch(sc)) {
1048 spin_unlock_bh(&sc->chan_lock);
1049 return;
1050 }
1051
Sujith Manoharan878066e2014-08-27 12:07:24 +05301052 ath_dbg(common, CHAN_CTX,
1053 "%s: current: %d MHz, next: %d MHz\n",
1054 __func__,
1055 sc->cur_chan->chandef.center_freq1,
1056 sc->next_chan->chandef.center_freq1);
1057
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301058 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301059 ath_dbg(common, CHAN_CTX,
1060 "Stopping current chanctx: %d\n",
1061 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301062 sc->cur_chan->stopped = true;
1063 spin_unlock_bh(&sc->chan_lock);
1064
1065 if (sc->next_chan == &sc->offchannel.chan) {
1066 getrawmonotonic(&ts);
1067 measure_time = true;
1068 }
1069 __ath9k_flush(sc->hw, ~0, true);
1070
1071 if (ath_chanctx_send_ps_frame(sc, true))
1072 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
1073
1074 send_ps = true;
1075 spin_lock_bh(&sc->chan_lock);
1076
1077 if (sc->cur_chan != &sc->offchannel.chan) {
1078 getrawmonotonic(&sc->cur_chan->tsf_ts);
1079 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1080 }
1081 }
1082 sc->cur_chan = sc->next_chan;
1083 sc->cur_chan->stopped = false;
1084 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301085
1086 if (!sc->sched.offchannel_pending)
1087 sc->sched.offchannel_duration = 0;
1088
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301089 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1090 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1091
1092 spin_unlock_bh(&sc->chan_lock);
1093
1094 if (sc->sc_ah->chip_fullsleep ||
1095 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1096 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301097 ath_dbg(common, CHAN_CTX,
1098 "%s: Set channel %d MHz\n",
1099 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301100 ath_set_channel(sc);
1101 if (measure_time)
1102 sc->sched.channel_switch_time =
1103 ath9k_hw_get_tsf_offset(&ts, NULL);
1104 }
1105 if (send_ps)
1106 ath_chanctx_send_ps_frame(sc, false);
1107
1108 ath_offchannel_channel_change(sc);
1109 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1110}
1111
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301112static void ath_chanctx_work(struct work_struct *work)
1113{
1114 struct ath_softc *sc = container_of(work, struct ath_softc,
1115 chanctx_work);
1116 mutex_lock(&sc->mutex);
1117 ath_chanctx_set_next(sc, false);
1118 mutex_unlock(&sc->mutex);
1119}
1120
Sujith Manoharane90e3022014-08-23 13:29:20 +05301121void ath9k_offchannel_init(struct ath_softc *sc)
1122{
1123 struct ath_chanctx *ctx;
1124 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1125 struct ieee80211_supported_band *sband;
1126 struct ieee80211_channel *chan;
1127 int i;
1128
1129 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1130 if (!sband->n_channels)
1131 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1132
1133 chan = &sband->channels[0];
1134
1135 ctx = &sc->offchannel.chan;
1136 INIT_LIST_HEAD(&ctx->vifs);
1137 ctx->txpower = ATH_TXPOWER_MAX;
1138 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1139
1140 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1141 INIT_LIST_HEAD(&ctx->acq[i]);
1142
1143 sc->offchannel.chan.offchannel = true;
1144}
1145
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301146void ath9k_init_channel_context(struct ath_softc *sc)
1147{
1148 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1149
1150 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1151 (unsigned long)sc);
1152 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1153 (unsigned long)sc);
1154}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301155
Sujith Manoharanea22df22014-08-23 13:29:07 +05301156void ath9k_deinit_channel_context(struct ath_softc *sc)
1157{
1158 cancel_work_sync(&sc->chanctx_work);
1159}
1160
Sujith Manoharan499afac2014-08-22 20:39:31 +05301161bool ath9k_is_chanctx_enabled(void)
1162{
1163 return (ath9k_use_chanctx == 1);
1164}
1165
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301166/********************/
1167/* Queue management */
1168/********************/
1169
1170void ath9k_chanctx_wake_queues(struct ath_softc *sc)
1171{
1172 struct ath_hw *ah = sc->sc_ah;
1173 int i;
1174
1175 if (sc->cur_chan == &sc->offchannel.chan) {
1176 ieee80211_wake_queue(sc->hw,
1177 sc->hw->offchannel_tx_hw_queue);
1178 } else {
1179 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1180 ieee80211_wake_queue(sc->hw,
1181 sc->cur_chan->hw_queue_base + i);
1182 }
1183
1184 if (ah->opmode == NL80211_IFTYPE_AP)
1185 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1186}
1187
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301188/*****************/
1189/* P2P Powersave */
1190/*****************/
1191
1192static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301193{
1194 struct ath_hw *ah = sc->sc_ah;
1195 s32 tsf, target_tsf;
1196
1197 if (!avp || !avp->noa.has_next_tsf)
1198 return;
1199
1200 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1201
1202 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1203
1204 target_tsf = avp->noa.next_tsf;
1205 if (!avp->noa.absent)
1206 target_tsf -= ATH_P2P_PS_STOP_TIME;
1207
1208 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1209 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1210
1211 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1212}
1213
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301214static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1215{
1216 struct ath_vif *avp = (void *)vif->drv_priv;
1217 u32 tsf;
1218
1219 if (!sc->p2p_ps_timer)
1220 return;
1221
1222 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1223 return;
1224
1225 sc->p2p_ps_vif = avp;
1226 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1227 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1228 ath9k_update_p2p_ps_timer(sc, avp);
1229}
1230
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301231static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1232{
1233 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1234 u8 switch_time, ctwin;
1235
1236 /*
1237 * Channel switch in multi-channel mode is deferred
1238 * by a quarter beacon interval when handling
1239 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1240 * interface is guaranteed to be discoverable
1241 * for that duration after a TBTT.
1242 */
1243 switch_time = cur_conf->beacon_interval / 4;
1244
1245 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1246 if (ctwin && (ctwin < switch_time))
1247 return ctwin;
1248
1249 if (switch_time < P2P_DEFAULT_CTWIN)
1250 return 0;
1251
1252 return P2P_DEFAULT_CTWIN;
1253}
1254
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301255void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1256 struct sk_buff *skb)
1257{
1258 static const u8 noa_ie_hdr[] = {
1259 WLAN_EID_VENDOR_SPECIFIC, /* type */
1260 0, /* length */
1261 0x50, 0x6f, 0x9a, /* WFA OUI */
1262 0x09, /* P2P subtype */
1263 0x0c, /* Notice of Absence */
1264 0x00, /* LSB of little-endian len */
1265 0x00, /* MSB of little-endian len */
1266 };
1267
1268 struct ieee80211_p2p_noa_attr *noa;
1269 int noa_len, noa_desc, i = 0;
1270 u8 *hdr;
1271
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301272 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301273 return;
1274
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301275 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301276 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1277
1278 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1279 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1280 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1281 hdr[7] = noa_len;
1282
1283 noa = (void *) skb_put(skb, noa_len);
1284 memset(noa, 0, noa_len);
1285
1286 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301287 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1288
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301289 if (avp->noa_duration) {
1290 if (avp->periodic_noa) {
1291 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1292 noa->desc[i].count = 255;
1293 noa->desc[i].interval = cpu_to_le32(interval);
1294 } else {
1295 noa->desc[i].count = 1;
1296 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301297
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301298 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1299 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301300 i++;
1301 }
1302
1303 if (avp->offchannel_duration) {
1304 noa->desc[i].count = 1;
1305 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1306 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1307 }
1308}
1309
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301310void ath9k_p2p_ps_timer(void *priv)
1311{
1312 struct ath_softc *sc = priv;
1313 struct ath_vif *avp = sc->p2p_ps_vif;
1314 struct ieee80211_vif *vif;
1315 struct ieee80211_sta *sta;
1316 struct ath_node *an;
1317 u32 tsf;
1318
1319 del_timer_sync(&sc->sched.timer);
1320 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1321 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1322
1323 if (!avp || avp->chanctx != sc->cur_chan)
1324 return;
1325
1326 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1327 if (!avp->noa.absent)
1328 tsf += ATH_P2P_PS_STOP_TIME;
1329
1330 if (!avp->noa.has_next_tsf ||
1331 avp->noa.next_tsf - tsf > BIT(31))
1332 ieee80211_update_p2p_noa(&avp->noa, tsf);
1333
1334 ath9k_update_p2p_ps_timer(sc, avp);
1335
1336 rcu_read_lock();
1337
1338 vif = avp->vif;
1339 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1340 if (!sta)
1341 goto out;
1342
1343 an = (void *) sta->drv_priv;
1344 if (an->sleeping == !!avp->noa.absent)
1345 goto out;
1346
1347 an->sleeping = avp->noa.absent;
1348 if (an->sleeping)
1349 ath_tx_aggr_sleep(sta, sc, an);
1350 else
1351 ath_tx_aggr_wakeup(sc, an);
1352
1353out:
1354 rcu_read_unlock();
1355}
1356
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301357void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1358 struct ieee80211_vif *vif)
1359{
1360 unsigned long flags;
1361
1362 spin_lock_bh(&sc->sc_pcu_lock);
1363 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1364 if (!(sc->ps_flags & PS_BEACON_SYNC))
1365 ath9k_update_p2p_ps(sc, vif);
1366 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1367 spin_unlock_bh(&sc->sc_pcu_lock);
1368}
1369
1370void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1371{
1372 if (sc->p2p_ps_vif)
1373 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1374}
1375
1376void ath9k_p2p_remove_vif(struct ath_softc *sc,
1377 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301378{
1379 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301380
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301381 spin_lock_bh(&sc->sc_pcu_lock);
1382 if (avp == sc->p2p_ps_vif) {
1383 sc->p2p_ps_vif = NULL;
1384 ath9k_update_p2p_ps_timer(sc, NULL);
1385 }
1386 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301387}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301388
1389int ath9k_init_p2p(struct ath_softc *sc)
1390{
1391 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1392 NULL, sc, AR_FIRST_NDP_TIMER);
1393 if (!sc->p2p_ps_timer)
1394 return -ENOMEM;
1395
1396 return 0;
1397}
1398
1399void ath9k_deinit_p2p(struct ath_softc *sc)
1400{
1401 if (sc->p2p_ps_timer)
1402 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1403}
1404
1405#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */