blob: f8f47571d98c94f267b95911ab4ab22941f8abb3 [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
86 /* set HW specific DFS configuration */
87 ath9k_hw_set_radar_params(ah);
88 rxfilter = ath9k_hw_getrxfilter(ah);
89 rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
90 ATH9K_RX_FILTER_PHYERR;
91 ath9k_hw_setrxfilter(ah, rxfilter);
92 ath_dbg(common, DFS, "DFS enabled at freq %d\n",
93 chan->center_freq);
94 } else {
95 /* perform spectral scan if requested. */
96 if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
97 sc->spectral_mode == SPECTRAL_CHANSCAN)
98 ath9k_spectral_scan_trigger(hw);
99 }
100
101 return 0;
102}
103
104void ath_chanctx_init(struct ath_softc *sc)
105{
106 struct ath_chanctx *ctx;
107 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
108 struct ieee80211_supported_band *sband;
109 struct ieee80211_channel *chan;
Felix Fietkau04535312014-06-11 16:17:51 +0530110 int i, j;
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530111
112 sband = &common->sbands[IEEE80211_BAND_2GHZ];
113 if (!sband->n_channels)
114 sband = &common->sbands[IEEE80211_BAND_5GHZ];
115
116 chan = &sband->channels[0];
117 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
118 ctx = &sc->chanctx[i];
119 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
120 INIT_LIST_HEAD(&ctx->vifs);
Felix Fietkaubc7e1be2014-06-11 16:17:50 +0530121 ctx->txpower = ATH_TXPOWER_MAX;
Felix Fietkau04535312014-06-11 16:17:51 +0530122 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
123 INIT_LIST_HEAD(&ctx->acq[j]);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530124 }
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530125}
126
Felix Fietkaubff11762014-06-11 16:17:52 +0530127void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
128 struct cfg80211_chan_def *chandef)
129{
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530130 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkaubff11762014-06-11 16:17:52 +0530131 bool cur_chan;
132
133 spin_lock_bh(&sc->chan_lock);
134 if (chandef)
135 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
136 cur_chan = sc->cur_chan == ctx;
137 spin_unlock_bh(&sc->chan_lock);
138
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530139 if (!cur_chan) {
140 ath_dbg(common, CHAN_CTX,
141 "Current context differs from the new context\n");
Felix Fietkaubff11762014-06-11 16:17:52 +0530142 return;
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530143 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530144
145 ath_set_channel(sc);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530146}
Felix Fietkau78b21942014-06-11 16:17:55 +0530147
Sujith Manoharan27babf92014-08-23 13:29:16 +0530148#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
149
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +0530150/**********************************************************/
151/* Functions to handle the channel context state machine. */
152/**********************************************************/
153
Sujith Manoharan27babf92014-08-23 13:29:16 +0530154static const char *offchannel_state_string(enum ath_offchannel_state state)
155{
Sujith Manoharan27babf92014-08-23 13:29:16 +0530156 switch (state) {
157 case_rtn_string(ATH_OFFCHANNEL_IDLE);
158 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
159 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
160 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
161 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
162 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
163 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
164 default:
165 return "unknown";
166 }
167}
168
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530169static const char *chanctx_event_string(enum ath_chanctx_event ev)
170{
171 switch (ev) {
172 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
173 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
174 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
175 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
176 case_rtn_string(ATH_CHANCTX_EVENT_ASSOC);
177 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
178 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
179 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
180 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
181 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
182 default:
183 return "unknown";
184 }
185}
186
187static const char *chanctx_state_string(enum ath_chanctx_state state)
188{
189 switch (state) {
190 case_rtn_string(ATH_CHANCTX_STATE_IDLE);
191 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
192 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
193 case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
194 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
195 default:
196 return "unknown";
197 }
198}
199
Sujith Manoharana09798f2014-08-23 13:29:21 +0530200void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
201{
202 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
203 struct ath_vif *avp;
204 bool active = false;
205 u8 n_active = 0;
206
207 if (!ctx)
208 return;
209
210 list_for_each_entry(avp, &ctx->vifs, list) {
211 struct ieee80211_vif *vif = avp->vif;
212
213 switch (vif->type) {
214 case NL80211_IFTYPE_P2P_CLIENT:
215 case NL80211_IFTYPE_STATION:
216 if (vif->bss_conf.assoc)
217 active = true;
218 break;
219 default:
220 active = true;
221 break;
222 }
223 }
224 ctx->active = active;
225
226 ath_for_each_chanctx(sc, ctx) {
227 if (!ctx->assigned || list_empty(&ctx->vifs))
228 continue;
229 n_active++;
230 }
231
232 if (n_active <= 1) {
233 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
234 return;
235 }
236 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
237 return;
238
239 if (ath9k_is_chanctx_enabled()) {
240 ath_chanctx_event(sc, NULL,
241 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
242 }
243}
244
Felix Fietkau58b57372014-06-11 16:18:08 +0530245static struct ath_chanctx *
246ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
247{
248 int idx = ctx - &sc->chanctx[0];
249
250 return &sc->chanctx[!idx];
251}
252
253static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
254{
255 struct ath_chanctx *prev, *cur;
256 struct timespec ts;
257 u32 cur_tsf, prev_tsf, beacon_int;
258 s32 offset;
259
260 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
261
262 cur = sc->cur_chan;
263 prev = ath_chanctx_get_next(sc, cur);
264
265 getrawmonotonic(&ts);
266 cur_tsf = (u32) cur->tsf_val +
267 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
268
269 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
270 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
271
272 /* Adjust the TSF time of the AP chanctx to keep its beacons
273 * at half beacon interval offset relative to the STA chanctx.
274 */
275 offset = cur_tsf - prev_tsf;
276
277 /* Ignore stale data or spurious timestamps */
278 if (offset < 0 || offset > 3 * beacon_int)
279 return;
280
281 offset = beacon_int / 2 - (offset % beacon_int);
282 prev->tsf_val += offset;
283}
284
Felix Fietkau42eda112014-06-11 16:18:14 +0530285/* Configure the TSF based hardware timer for a channel switch.
286 * Also set up backup software timer, in case the gen timer fails.
287 * This could be caused by a hardware reset.
288 */
289static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
290{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530291 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau42eda112014-06-11 16:18:14 +0530292 struct ath_hw *ah = sc->sc_ah;
293
294 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
295 tsf_time -= ath9k_hw_gettsf32(ah);
296 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
297 mod_timer(&sc->sched.timer, tsf_time);
Sujith Manoharan878066e2014-08-27 12:07:24 +0530298
299 ath_dbg(common, CHAN_CTX,
300 "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time));
Felix Fietkau42eda112014-06-11 16:18:14 +0530301}
302
Felix Fietkau748299f2014-06-11 16:18:04 +0530303void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
304 enum ath_chanctx_event ev)
305{
306 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530307 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530308 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530309 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530310 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530311 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530312 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530313 bool noa_changed = false;
314
315 if (vif)
316 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530317
318 spin_lock_bh(&sc->chan_lock);
319
Sujith Manoharan878066e2014-08-27 12:07:24 +0530320 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
321 sc->cur_chan->chandef.center_freq1,
322 chanctx_event_string(ev),
323 chanctx_state_string(sc->sched.state));
324
Felix Fietkau748299f2014-06-11 16:18:04 +0530325 switch (ev) {
326 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530327 if (avp->offchannel_duration)
328 avp->offchannel_duration = 0;
329
Sujith Manoharan878066e2014-08-27 12:07:24 +0530330 if (avp->chanctx != sc->cur_chan) {
331 ath_dbg(common, CHAN_CTX,
332 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530333 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530334 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530335
336 if (sc->sched.offchannel_pending) {
337 sc->sched.offchannel_pending = false;
338 sc->next_chan = &sc->offchannel.chan;
339 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530340 ath_dbg(common, CHAN_CTX,
341 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530342 }
343
344 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
345 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
346 sc->next_chan = ctx;
347 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530348 ath_dbg(common, CHAN_CTX,
349 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530350 }
351
352 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530353 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530354 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530355 ath_dbg(common, CHAN_CTX,
356 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
357 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530358
Felix Fietkau748299f2014-06-11 16:18:04 +0530359 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
360 break;
361
Sujith Manoharan878066e2014-08-27 12:07:24 +0530362 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
363
Felix Fietkau748299f2014-06-11 16:18:04 +0530364 sc->sched.beacon_pending = true;
365 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530366
Felix Fietkau74148632014-06-11 16:18:11 +0530367 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530368 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
369
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530370 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530371 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530372 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530373 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530374
Felix Fietkau74148632014-06-11 16:18:11 +0530375 /* Prevent wrap-around issues */
376 if (avp->periodic_noa_duration &&
377 tsf_time - avp->periodic_noa_start > BIT(30))
378 avp->periodic_noa_duration = 0;
379
380 if (ctx->active && !avp->periodic_noa_duration) {
381 avp->periodic_noa_start = tsf_time;
382 avp->periodic_noa_duration =
383 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
384 sc->sched.channel_switch_time;
385 noa_changed = true;
386 } else if (!ctx->active && avp->periodic_noa_duration) {
387 avp->periodic_noa_duration = 0;
388 noa_changed = true;
389 }
390
Felix Fietkauec70abe2014-06-11 16:18:12 +0530391 /* If at least two consecutive beacons were missed on the STA
392 * chanctx, stay on the STA channel for one extra beacon period,
393 * to resync the timer properly.
394 */
395 if (ctx->active && sc->sched.beacon_miss >= 2)
396 sc->sched.offchannel_duration = 3 * beacon_int / 2;
397
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530398 if (sc->sched.offchannel_duration) {
399 noa_changed = true;
400 avp->offchannel_start = tsf_time;
401 avp->offchannel_duration =
402 sc->sched.offchannel_duration;
403 }
404
405 if (noa_changed)
406 avp->noa_index++;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530407
408 ath_dbg(common, CHAN_CTX,
409 "periodic_noa_duration: %d, periodic_noa_start: %d, noa_index: %d\n",
410 avp->periodic_noa_duration,
411 avp->periodic_noa_start,
412 avp->noa_index);
413
Felix Fietkau748299f2014-06-11 16:18:04 +0530414 break;
415 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530416 if (!sc->sched.beacon_pending) {
417 ath_dbg(common, CHAN_CTX,
418 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530419 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530420 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530421
422 sc->sched.beacon_pending = false;
423 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
424 break;
425
Sujith Manoharan878066e2014-08-27 12:07:24 +0530426 ath_dbg(common, CHAN_CTX,
427 "Move chanctx state to WAIT_FOR_TIMER\n");
428
Felix Fietkau748299f2014-06-11 16:18:04 +0530429 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530430 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530431 break;
432 case ATH_CHANCTX_EVENT_TSF_TIMER:
433 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
434 break;
435
Felix Fietkauec70abe2014-06-11 16:18:12 +0530436 if (!sc->cur_chan->switch_after_beacon &&
437 sc->sched.beacon_pending)
438 sc->sched.beacon_miss++;
439
Sujith Manoharan878066e2014-08-27 12:07:24 +0530440 ath_dbg(common, CHAN_CTX,
441 "Move chanctx state to SWITCH\n");
442
Felix Fietkau748299f2014-06-11 16:18:04 +0530443 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
444 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
445 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530446 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530447 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
448 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530449 break;
450
451 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530452 sc->sched.beacon_pending = false;
453 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530454
455 /* TSF time might have been updated by the incoming beacon,
456 * need update the channel switch timer to reflect the change.
457 */
458 tsf_time = sc->sched.switch_start_time;
459 tsf_time -= (u32) sc->cur_chan->tsf_val +
460 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
461 tsf_time += ath9k_hw_gettsf32(ah);
462
Felix Fietkau42eda112014-06-11 16:18:14 +0530463
464 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530465 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530466 case ATH_CHANCTX_EVENT_ASSOC:
467 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
468 avp->chanctx != sc->cur_chan)
469 break;
470
Sujith Manoharan878066e2014-08-27 12:07:24 +0530471 ath_dbg(common, CHAN_CTX,
472 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
473
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530474 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
475 /* fall through */
476 case ATH_CHANCTX_EVENT_SWITCH:
477 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
478 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
479 sc->cur_chan->switch_after_beacon ||
480 sc->cur_chan == &sc->offchannel.chan)
481 break;
482
483 /* If this is a station chanctx, stay active for a half
484 * beacon period (minus channel switch time)
485 */
486 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530487 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530488
Sujith Manoharan878066e2014-08-27 12:07:24 +0530489 ath_dbg(common, CHAN_CTX,
490 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
491
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530492 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530493
494 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
495 if (sc->sched.beacon_miss >= 2) {
496 sc->sched.beacon_miss = 0;
497 tsf_time *= 3;
498 }
499
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530500 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530501 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530502 sc->sched.switch_start_time = tsf_time;
503
Felix Fietkau42eda112014-06-11 16:18:14 +0530504 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530505 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530506 break;
507 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
508 if (sc->cur_chan == &sc->offchannel.chan ||
509 sc->cur_chan->switch_after_beacon)
510 break;
511
512 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
513 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
514 break;
515 case ATH_CHANCTX_EVENT_UNASSIGN:
516 if (sc->cur_chan->assigned) {
517 if (sc->next_chan && !sc->next_chan->assigned &&
518 sc->next_chan != &sc->offchannel.chan)
519 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
520 break;
521 }
522
523 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
524 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
525 if (!ctx->assigned)
526 break;
527
528 sc->next_chan = ctx;
529 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
530 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530531 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530532 /*
533 * When adding a new channel context, check if a scan
534 * is in progress and abort it since the addition of
535 * a new channel context is usually followed by VIF
536 * assignment, in which case we have to start multi-channel
537 * operation.
538 */
539 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
540 ath_dbg(common, CHAN_CTX,
541 "Aborting HW scan to add new context\n");
542
543 spin_unlock_bh(&sc->chan_lock);
544 del_timer_sync(&sc->offchannel.timer);
545 ath_scan_complete(sc, true);
546 spin_lock_bh(&sc->chan_lock);
547 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530548 break;
549 case ATH_CHANCTX_EVENT_CHANGE:
550 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530551 }
552
553 spin_unlock_bh(&sc->chan_lock);
554}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530555
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530556void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
557 enum ath_chanctx_event ev)
558{
559 if (sc->sched.beacon_pending)
560 ath_chanctx_event(sc, NULL, ev);
561}
562
563void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
564 enum ath_chanctx_event ev)
565{
566 sc->sched.next_tbtt = ts;
567 ath_chanctx_event(sc, NULL, ev);
568}
569
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530570static int ath_scan_channel_duration(struct ath_softc *sc,
571 struct ieee80211_channel *chan)
572{
573 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
574
575 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
576 return (HZ / 9); /* ~110 ms */
577
578 return (HZ / 16); /* ~60 ms */
579}
580
Sujith Manoharan922c9432014-08-23 13:29:15 +0530581static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
582 struct cfg80211_chan_def *chandef)
583{
584 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
585
586 spin_lock_bh(&sc->chan_lock);
587
588 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
589 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
590 sc->sched.offchannel_pending = true;
591 spin_unlock_bh(&sc->chan_lock);
592 return;
593 }
594
595 sc->next_chan = ctx;
596 if (chandef) {
597 ctx->chandef = *chandef;
598 ath_dbg(common, CHAN_CTX,
599 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
600 }
601
602 if (sc->next_chan == &sc->offchannel.chan) {
603 sc->sched.offchannel_duration =
604 TU_TO_USEC(sc->offchannel.duration) +
605 sc->sched.channel_switch_time;
606
607 if (chandef) {
608 ath_dbg(common, CHAN_CTX,
609 "Offchannel duration for chan %d MHz : %u\n",
610 chandef->center_freq1,
611 sc->sched.offchannel_duration);
612 }
613 }
614 spin_unlock_bh(&sc->chan_lock);
615 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
616}
617
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530618static void ath_chanctx_offchan_switch(struct ath_softc *sc,
619 struct ieee80211_channel *chan)
620{
621 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
622 struct cfg80211_chan_def chandef;
623
624 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
625 ath_dbg(common, CHAN_CTX,
626 "Channel definition created: %d MHz\n", chandef.center_freq1);
627
628 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
629}
630
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530631static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
632 bool active)
633{
634 struct ath_chanctx *ctx;
635
636 ath_for_each_chanctx(sc, ctx) {
637 if (!ctx->assigned || list_empty(&ctx->vifs))
638 continue;
639 if (active && !ctx->active)
640 continue;
641
642 if (ctx->switch_after_beacon)
643 return ctx;
644 }
645
646 return &sc->chanctx[0];
647}
648
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530649static void
650ath_scan_next_channel(struct ath_softc *sc)
651{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530652 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530653 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
654 struct ieee80211_channel *chan;
655
656 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530657 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530658 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
659 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530660 sc->offchannel.scan_idx,
661 req->n_channels);
662
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530663 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
664 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
665 NULL);
666 return;
667 }
668
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530669 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530670 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530671 sc->offchannel.scan_idx);
672
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530673 chan = req->channels[sc->offchannel.scan_idx++];
674 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
675 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530676
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530677 ath_chanctx_offchan_switch(sc, chan);
678}
679
680void ath_offchannel_next(struct ath_softc *sc)
681{
682 struct ieee80211_vif *vif;
683
684 if (sc->offchannel.scan_req) {
685 vif = sc->offchannel.scan_vif;
686 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
687 ath_scan_next_channel(sc);
688 } else if (sc->offchannel.roc_vif) {
689 vif = sc->offchannel.roc_vif;
690 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
691 sc->offchannel.duration = sc->offchannel.roc_duration;
692 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
693 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
694 } else {
695 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
696 NULL);
697 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
698 if (sc->ps_idle)
699 ath_cancel_work(sc);
700 }
701}
702
703void ath_roc_complete(struct ath_softc *sc, bool abort)
704{
705 sc->offchannel.roc_vif = NULL;
706 sc->offchannel.roc_chan = NULL;
707 if (!abort)
708 ieee80211_remain_on_channel_expired(sc->hw);
709 ath_offchannel_next(sc);
710 ath9k_ps_restore(sc);
711}
712
713void ath_scan_complete(struct ath_softc *sc, bool abort)
714{
715 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
716
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530717 if (abort)
718 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
719 else
720 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
721
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530722 sc->offchannel.scan_req = NULL;
723 sc->offchannel.scan_vif = NULL;
724 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
725 ieee80211_scan_completed(sc->hw, abort);
726 clear_bit(ATH_OP_SCANNING, &common->op_flags);
727 ath_offchannel_next(sc);
728 ath9k_ps_restore(sc);
729}
730
731static void ath_scan_send_probe(struct ath_softc *sc,
732 struct cfg80211_ssid *ssid)
733{
734 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
735 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
736 struct ath_tx_control txctl = {};
737 struct sk_buff *skb;
738 struct ieee80211_tx_info *info;
739 int band = sc->offchannel.chan.chandef.chan->band;
740
741 skb = ieee80211_probereq_get(sc->hw, vif,
742 ssid->ssid, ssid->ssid_len, req->ie_len);
743 if (!skb)
744 return;
745
746 info = IEEE80211_SKB_CB(skb);
747 if (req->no_cck)
748 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
749
750 if (req->ie_len)
751 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
752
753 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
754
755 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
756 goto error;
757
758 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
759 txctl.force_channel = true;
760 if (ath_tx_start(sc->hw, skb, &txctl))
761 goto error;
762
763 return;
764
765error:
766 ieee80211_free_txskb(sc->hw, skb);
767}
768
769static void ath_scan_channel_start(struct ath_softc *sc)
770{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530771 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530772 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
773 int i;
774
775 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
776 req->n_ssids) {
777 for (i = 0; i < req->n_ssids; i++)
778 ath_scan_send_probe(sc, &req->ssids[i]);
779
780 }
781
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530782 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530783 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530784
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530785 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
786 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
787}
788
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530789static void ath_chanctx_timer(unsigned long data)
790{
791 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530792 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
793
794 ath_dbg(common, CHAN_CTX,
795 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530796
797 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
798}
799
800static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530801{
802 struct ath_softc *sc = (struct ath_softc *)data;
803 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530804 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
805
Sujith Manoharan878066e2014-08-27 12:07:24 +0530806 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530807 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530808
809 switch (sc->offchannel.state) {
810 case ATH_OFFCHANNEL_PROBE_WAIT:
811 if (!sc->offchannel.scan_req)
812 return;
813
814 /* get first active channel context */
815 ctx = ath_chanctx_get_oper_chan(sc, true);
816 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +0530817 ath_dbg(common, CHAN_CTX,
818 "Switch to oper/active context, "
819 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
820
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530821 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
822 ath_chanctx_switch(sc, ctx, NULL);
823 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
824 break;
825 }
826 /* fall through */
827 case ATH_OFFCHANNEL_SUSPEND:
828 if (!sc->offchannel.scan_req)
829 return;
830
831 ath_scan_next_channel(sc);
832 break;
833 case ATH_OFFCHANNEL_ROC_START:
834 case ATH_OFFCHANNEL_ROC_WAIT:
835 ctx = ath_chanctx_get_oper_chan(sc, false);
836 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
837 ath_chanctx_switch(sc, ctx, NULL);
838 break;
839 default:
840 break;
841 }
842}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530843
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530844static bool
845ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
846 bool powersave)
847{
848 struct ieee80211_vif *vif = avp->vif;
849 struct ieee80211_sta *sta = NULL;
850 struct ieee80211_hdr_3addr *nullfunc;
851 struct ath_tx_control txctl;
852 struct sk_buff *skb;
853 int band = sc->cur_chan->chandef.chan->band;
854
855 switch (vif->type) {
856 case NL80211_IFTYPE_STATION:
857 if (!vif->bss_conf.assoc)
858 return false;
859
860 skb = ieee80211_nullfunc_get(sc->hw, vif);
861 if (!skb)
862 return false;
863
864 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
865 if (powersave)
866 nullfunc->frame_control |=
867 cpu_to_le16(IEEE80211_FCTL_PM);
868
869 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
870 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
871 dev_kfree_skb_any(skb);
872 return false;
873 }
874 break;
875 default:
876 return false;
877 }
878
879 memset(&txctl, 0, sizeof(txctl));
880 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
881 txctl.sta = sta;
882 txctl.force_channel = true;
883 if (ath_tx_start(sc->hw, skb, &txctl)) {
884 ieee80211_free_txskb(sc->hw, skb);
885 return false;
886 }
887
888 return true;
889}
890
891static bool
892ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
893{
894 struct ath_vif *avp;
895 bool sent = false;
896
897 rcu_read_lock();
898 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
899 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
900 sent = true;
901 }
902 rcu_read_unlock();
903
904 return sent;
905}
906
907static bool ath_chanctx_defer_switch(struct ath_softc *sc)
908{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530909 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
910
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530911 if (sc->cur_chan == &sc->offchannel.chan)
912 return false;
913
914 switch (sc->sched.state) {
915 case ATH_CHANCTX_STATE_SWITCH:
916 return false;
917 case ATH_CHANCTX_STATE_IDLE:
918 if (!sc->cur_chan->switch_after_beacon)
919 return false;
920
Sujith Manoharan878066e2014-08-27 12:07:24 +0530921 ath_dbg(common, CHAN_CTX,
922 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
923
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530924 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
925 break;
926 default:
927 break;
928 }
929
930 return true;
931}
932
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530933static void ath_offchannel_channel_change(struct ath_softc *sc)
934{
935 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
936
Sujith Manoharan878066e2014-08-27 12:07:24 +0530937 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530938 __func__, offchannel_state_string(sc->offchannel.state));
939
940 switch (sc->offchannel.state) {
941 case ATH_OFFCHANNEL_PROBE_SEND:
942 if (!sc->offchannel.scan_req)
943 return;
944
945 if (sc->cur_chan->chandef.chan !=
946 sc->offchannel.chan.chandef.chan)
947 return;
948
949 ath_scan_channel_start(sc);
950 break;
951 case ATH_OFFCHANNEL_IDLE:
952 if (!sc->offchannel.scan_req)
953 return;
954
955 ath_scan_complete(sc, false);
956 break;
957 case ATH_OFFCHANNEL_ROC_START:
958 if (sc->cur_chan != &sc->offchannel.chan)
959 break;
960
961 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
962 mod_timer(&sc->offchannel.timer, jiffies +
963 msecs_to_jiffies(sc->offchannel.duration));
964 ieee80211_ready_on_channel(sc->hw);
965 break;
966 case ATH_OFFCHANNEL_ROC_DONE:
967 ath_roc_complete(sc, false);
968 break;
969 default:
970 break;
971 }
972}
973
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530974void ath_chanctx_set_next(struct ath_softc *sc, bool force)
975{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530976 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530977 struct timespec ts;
978 bool measure_time = false;
979 bool send_ps = false;
980
981 spin_lock_bh(&sc->chan_lock);
982 if (!sc->next_chan) {
983 spin_unlock_bh(&sc->chan_lock);
984 return;
985 }
986
987 if (!force && ath_chanctx_defer_switch(sc)) {
988 spin_unlock_bh(&sc->chan_lock);
989 return;
990 }
991
Sujith Manoharan878066e2014-08-27 12:07:24 +0530992 ath_dbg(common, CHAN_CTX,
993 "%s: current: %d MHz, next: %d MHz\n",
994 __func__,
995 sc->cur_chan->chandef.center_freq1,
996 sc->next_chan->chandef.center_freq1);
997
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530998 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +0530999 ath_dbg(common, CHAN_CTX,
1000 "Stopping current chanctx: %d\n",
1001 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301002 sc->cur_chan->stopped = true;
1003 spin_unlock_bh(&sc->chan_lock);
1004
1005 if (sc->next_chan == &sc->offchannel.chan) {
1006 getrawmonotonic(&ts);
1007 measure_time = true;
1008 }
1009 __ath9k_flush(sc->hw, ~0, true);
1010
1011 if (ath_chanctx_send_ps_frame(sc, true))
1012 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
1013
1014 send_ps = true;
1015 spin_lock_bh(&sc->chan_lock);
1016
1017 if (sc->cur_chan != &sc->offchannel.chan) {
1018 getrawmonotonic(&sc->cur_chan->tsf_ts);
1019 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1020 }
1021 }
1022 sc->cur_chan = sc->next_chan;
1023 sc->cur_chan->stopped = false;
1024 sc->next_chan = NULL;
1025 sc->sched.offchannel_duration = 0;
1026 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1027 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1028
1029 spin_unlock_bh(&sc->chan_lock);
1030
1031 if (sc->sc_ah->chip_fullsleep ||
1032 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1033 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301034 ath_dbg(common, CHAN_CTX,
1035 "%s: Set channel %d MHz\n",
1036 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301037 ath_set_channel(sc);
1038 if (measure_time)
1039 sc->sched.channel_switch_time =
1040 ath9k_hw_get_tsf_offset(&ts, NULL);
1041 }
1042 if (send_ps)
1043 ath_chanctx_send_ps_frame(sc, false);
1044
1045 ath_offchannel_channel_change(sc);
1046 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1047}
1048
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301049static void ath_chanctx_work(struct work_struct *work)
1050{
1051 struct ath_softc *sc = container_of(work, struct ath_softc,
1052 chanctx_work);
1053 mutex_lock(&sc->mutex);
1054 ath_chanctx_set_next(sc, false);
1055 mutex_unlock(&sc->mutex);
1056}
1057
Sujith Manoharane90e3022014-08-23 13:29:20 +05301058void ath9k_offchannel_init(struct ath_softc *sc)
1059{
1060 struct ath_chanctx *ctx;
1061 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1062 struct ieee80211_supported_band *sband;
1063 struct ieee80211_channel *chan;
1064 int i;
1065
1066 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1067 if (!sband->n_channels)
1068 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1069
1070 chan = &sband->channels[0];
1071
1072 ctx = &sc->offchannel.chan;
1073 INIT_LIST_HEAD(&ctx->vifs);
1074 ctx->txpower = ATH_TXPOWER_MAX;
1075 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1076
1077 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1078 INIT_LIST_HEAD(&ctx->acq[i]);
1079
1080 sc->offchannel.chan.offchannel = true;
1081}
1082
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301083void ath9k_init_channel_context(struct ath_softc *sc)
1084{
1085 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1086
1087 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1088 (unsigned long)sc);
1089 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1090 (unsigned long)sc);
1091}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301092
Sujith Manoharanea22df22014-08-23 13:29:07 +05301093void ath9k_deinit_channel_context(struct ath_softc *sc)
1094{
1095 cancel_work_sync(&sc->chanctx_work);
1096}
1097
Sujith Manoharan499afac2014-08-22 20:39:31 +05301098bool ath9k_is_chanctx_enabled(void)
1099{
1100 return (ath9k_use_chanctx == 1);
1101}
1102
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301103/********************/
1104/* Queue management */
1105/********************/
1106
1107void ath9k_chanctx_wake_queues(struct ath_softc *sc)
1108{
1109 struct ath_hw *ah = sc->sc_ah;
1110 int i;
1111
1112 if (sc->cur_chan == &sc->offchannel.chan) {
1113 ieee80211_wake_queue(sc->hw,
1114 sc->hw->offchannel_tx_hw_queue);
1115 } else {
1116 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1117 ieee80211_wake_queue(sc->hw,
1118 sc->cur_chan->hw_queue_base + i);
1119 }
1120
1121 if (ah->opmode == NL80211_IFTYPE_AP)
1122 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1123}
1124
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301125/*****************/
1126/* P2P Powersave */
1127/*****************/
1128
1129static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301130{
1131 struct ath_hw *ah = sc->sc_ah;
1132 s32 tsf, target_tsf;
1133
1134 if (!avp || !avp->noa.has_next_tsf)
1135 return;
1136
1137 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1138
1139 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1140
1141 target_tsf = avp->noa.next_tsf;
1142 if (!avp->noa.absent)
1143 target_tsf -= ATH_P2P_PS_STOP_TIME;
1144
1145 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1146 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1147
1148 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1149}
1150
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301151static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1152{
1153 struct ath_vif *avp = (void *)vif->drv_priv;
1154 u32 tsf;
1155
1156 if (!sc->p2p_ps_timer)
1157 return;
1158
1159 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1160 return;
1161
1162 sc->p2p_ps_vif = avp;
1163 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1164 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1165 ath9k_update_p2p_ps_timer(sc, avp);
1166}
1167
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301168void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1169 struct sk_buff *skb)
1170{
1171 static const u8 noa_ie_hdr[] = {
1172 WLAN_EID_VENDOR_SPECIFIC, /* type */
1173 0, /* length */
1174 0x50, 0x6f, 0x9a, /* WFA OUI */
1175 0x09, /* P2P subtype */
1176 0x0c, /* Notice of Absence */
1177 0x00, /* LSB of little-endian len */
1178 0x00, /* MSB of little-endian len */
1179 };
1180
1181 struct ieee80211_p2p_noa_attr *noa;
1182 int noa_len, noa_desc, i = 0;
1183 u8 *hdr;
1184
1185 if (!avp->offchannel_duration && !avp->periodic_noa_duration)
1186 return;
1187
1188 noa_desc = !!avp->offchannel_duration + !!avp->periodic_noa_duration;
1189 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1190
1191 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1192 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1193 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1194 hdr[7] = noa_len;
1195
1196 noa = (void *) skb_put(skb, noa_len);
1197 memset(noa, 0, noa_len);
1198
1199 noa->index = avp->noa_index;
1200 if (avp->periodic_noa_duration) {
1201 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1202
1203 noa->desc[i].count = 255;
1204 noa->desc[i].start_time = cpu_to_le32(avp->periodic_noa_start);
1205 noa->desc[i].duration = cpu_to_le32(avp->periodic_noa_duration);
1206 noa->desc[i].interval = cpu_to_le32(interval);
1207 i++;
1208 }
1209
1210 if (avp->offchannel_duration) {
1211 noa->desc[i].count = 1;
1212 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1213 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1214 }
1215}
1216
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301217void ath9k_p2p_ps_timer(void *priv)
1218{
1219 struct ath_softc *sc = priv;
1220 struct ath_vif *avp = sc->p2p_ps_vif;
1221 struct ieee80211_vif *vif;
1222 struct ieee80211_sta *sta;
1223 struct ath_node *an;
1224 u32 tsf;
1225
1226 del_timer_sync(&sc->sched.timer);
1227 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1228 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1229
1230 if (!avp || avp->chanctx != sc->cur_chan)
1231 return;
1232
1233 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1234 if (!avp->noa.absent)
1235 tsf += ATH_P2P_PS_STOP_TIME;
1236
1237 if (!avp->noa.has_next_tsf ||
1238 avp->noa.next_tsf - tsf > BIT(31))
1239 ieee80211_update_p2p_noa(&avp->noa, tsf);
1240
1241 ath9k_update_p2p_ps_timer(sc, avp);
1242
1243 rcu_read_lock();
1244
1245 vif = avp->vif;
1246 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1247 if (!sta)
1248 goto out;
1249
1250 an = (void *) sta->drv_priv;
1251 if (an->sleeping == !!avp->noa.absent)
1252 goto out;
1253
1254 an->sleeping = avp->noa.absent;
1255 if (an->sleeping)
1256 ath_tx_aggr_sleep(sta, sc, an);
1257 else
1258 ath_tx_aggr_wakeup(sc, an);
1259
1260out:
1261 rcu_read_unlock();
1262}
1263
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301264void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1265 struct ieee80211_vif *vif)
1266{
1267 unsigned long flags;
1268
1269 spin_lock_bh(&sc->sc_pcu_lock);
1270 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1271 if (!(sc->ps_flags & PS_BEACON_SYNC))
1272 ath9k_update_p2p_ps(sc, vif);
1273 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1274 spin_unlock_bh(&sc->sc_pcu_lock);
1275}
1276
1277void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1278{
1279 if (sc->p2p_ps_vif)
1280 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1281}
1282
1283void ath9k_p2p_remove_vif(struct ath_softc *sc,
1284 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301285{
1286 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301287
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301288 spin_lock_bh(&sc->sc_pcu_lock);
1289 if (avp == sc->p2p_ps_vif) {
1290 sc->p2p_ps_vif = NULL;
1291 ath9k_update_p2p_ps_timer(sc, NULL);
1292 }
1293 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301294}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301295
1296int ath9k_init_p2p(struct ath_softc *sc)
1297{
1298 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1299 NULL, sc, AR_FIRST_NDP_TIMER);
1300 if (!sc->p2p_ps_timer)
1301 return -ENOMEM;
1302
1303 return 0;
1304}
1305
1306void ath9k_deinit_p2p(struct ath_softc *sc)
1307{
1308 if (sc->p2p_ps_timer)
1309 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1310}
1311
1312#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */