blob: a1b3282bd29e5e0b95a67b46f4324ba97fb7fdf5 [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;
Sujith Manoharan1a7c5b72014-08-27 12:07:25 +0530297 mod_timer(&sc->sched.timer, jiffies + 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
314 if (vif)
315 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530316
317 spin_lock_bh(&sc->chan_lock);
318
Sujith Manoharan878066e2014-08-27 12:07:24 +0530319 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
320 sc->cur_chan->chandef.center_freq1,
321 chanctx_event_string(ev),
322 chanctx_state_string(sc->sched.state));
323
Felix Fietkau748299f2014-06-11 16:18:04 +0530324 switch (ev) {
325 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530326 if (avp->offchannel_duration)
327 avp->offchannel_duration = 0;
328
Sujith Manoharan878066e2014-08-27 12:07:24 +0530329 if (avp->chanctx != sc->cur_chan) {
330 ath_dbg(common, CHAN_CTX,
331 "Contexts differ, not preparing beacon\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530332 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530333 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530334
Sujith Manoharan367b3412014-09-05 09:50:57 +0530335 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530336 sc->sched.offchannel_pending = false;
337 sc->next_chan = &sc->offchannel.chan;
338 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530339 ath_dbg(common, CHAN_CTX,
340 "Setting offchannel_pending to false\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530341 }
342
343 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
344 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
345 sc->next_chan = ctx;
346 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530347 ath_dbg(common, CHAN_CTX,
348 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530349 }
350
351 /* if the timer missed its window, use the next interval */
Sujith Manoharan878066e2014-08-27 12:07:24 +0530352 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530353 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530354 ath_dbg(common, CHAN_CTX,
355 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
356 }
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530357
Felix Fietkau748299f2014-06-11 16:18:04 +0530358 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
359 break;
360
Sujith Manoharan878066e2014-08-27 12:07:24 +0530361 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
362
Felix Fietkau748299f2014-06-11 16:18:04 +0530363 sc->sched.beacon_pending = true;
364 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530365
Felix Fietkau74148632014-06-11 16:18:11 +0530366 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530367 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
368
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530369 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530370 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530371 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530372 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530373
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530374 /*
375 * If an offchannel switch is scheduled to happen after
376 * a beacon transmission, update the NoA with one-shot
377 * values and increment the index.
378 */
379 if (sc->next_chan == &sc->offchannel.chan) {
380 avp->noa_index++;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530381 avp->offchannel_start = tsf_time;
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530382 avp->offchannel_duration = sc->sched.offchannel_duration;
383
384 ath_dbg(common, CHAN_CTX,
385 "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n",
386 avp->offchannel_duration,
387 avp->offchannel_start,
388 avp->noa_index);
389
390 /*
391 * When multiple contexts are active, the NoA
392 * has to be recalculated and advertised after
393 * an offchannel operation.
394 */
395 if (ctx->active && avp->noa_duration)
396 avp->noa_duration = 0;
397
398 break;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530399 }
400
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530401
402 /*
403 * Clear the extend_absence flag if it had been
404 * set during the previous beacon transmission,
405 * since we need to revert to the normal NoA
406 * schedule.
407 */
408 if (ctx->active && sc->sched.extend_absence) {
409 avp->noa_duration = 0;
410 sc->sched.extend_absence = false;
411 }
412
413 /* If at least two consecutive beacons were missed on the STA
414 * chanctx, stay on the STA channel for one extra beacon period,
415 * to resync the timer properly.
416 */
417 if (ctx->active && sc->sched.beacon_miss >= 2) {
418 avp->noa_duration = 0;
419 sc->sched.extend_absence = true;
420 }
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530421 /* Prevent wrap-around issues */
422 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
423 avp->noa_duration = 0;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530424
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530425 /*
426 * If multiple contexts are active, start periodic
427 * NoA and increment the index for the first
428 * announcement.
429 */
430 if (ctx->active &&
431 (!avp->noa_duration || sc->sched.force_noa_update)) {
432 avp->noa_index++;
433 avp->noa_start = tsf_time;
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530434
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530435 if (sc->sched.extend_absence)
436 avp->noa_duration = (3 * beacon_int / 2) +
437 sc->sched.channel_switch_time;
438 else
439 avp->noa_duration =
440 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
441 sc->sched.channel_switch_time;
442
443 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
444 sc->sched.extend_absence)
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530445 avp->periodic_noa = false;
446 else
447 avp->periodic_noa = true;
448
449 ath_dbg(common, CHAN_CTX,
450 "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
451 avp->noa_duration,
452 avp->noa_start,
453 avp->noa_index,
454 avp->periodic_noa);
455 }
456
457 if (ctx->active && sc->sched.force_noa_update)
458 sc->sched.force_noa_update = false;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530459
Felix Fietkau748299f2014-06-11 16:18:04 +0530460 break;
461 case ATH_CHANCTX_EVENT_BEACON_SENT:
Sujith Manoharan878066e2014-08-27 12:07:24 +0530462 if (!sc->sched.beacon_pending) {
463 ath_dbg(common, CHAN_CTX,
464 "No pending beacon\n");
Felix Fietkau748299f2014-06-11 16:18:04 +0530465 break;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530466 }
Felix Fietkau748299f2014-06-11 16:18:04 +0530467
468 sc->sched.beacon_pending = false;
469 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
470 break;
471
Sujith Manoharan878066e2014-08-27 12:07:24 +0530472 ath_dbg(common, CHAN_CTX,
473 "Move chanctx state to WAIT_FOR_TIMER\n");
474
Felix Fietkau748299f2014-06-11 16:18:04 +0530475 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530476 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530477 break;
478 case ATH_CHANCTX_EVENT_TSF_TIMER:
479 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
480 break;
481
Felix Fietkauec70abe2014-06-11 16:18:12 +0530482 if (!sc->cur_chan->switch_after_beacon &&
483 sc->sched.beacon_pending)
484 sc->sched.beacon_miss++;
485
Sujith Manoharan878066e2014-08-27 12:07:24 +0530486 ath_dbg(common, CHAN_CTX,
487 "Move chanctx state to SWITCH\n");
488
Felix Fietkau748299f2014-06-11 16:18:04 +0530489 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
490 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
491 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530492 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530493 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
494 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530495 break;
496
497 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530498 sc->sched.beacon_pending = false;
499 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530500
501 /* TSF time might have been updated by the incoming beacon,
502 * need update the channel switch timer to reflect the change.
503 */
504 tsf_time = sc->sched.switch_start_time;
505 tsf_time -= (u32) sc->cur_chan->tsf_val +
506 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
507 tsf_time += ath9k_hw_gettsf32(ah);
508
Felix Fietkau42eda112014-06-11 16:18:14 +0530509
510 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530511 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530512 case ATH_CHANCTX_EVENT_ASSOC:
513 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
514 avp->chanctx != sc->cur_chan)
515 break;
516
Sujith Manoharan878066e2014-08-27 12:07:24 +0530517 ath_dbg(common, CHAN_CTX,
518 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
519
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530520 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
521 /* fall through */
522 case ATH_CHANCTX_EVENT_SWITCH:
523 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
524 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
525 sc->cur_chan->switch_after_beacon ||
526 sc->cur_chan == &sc->offchannel.chan)
527 break;
528
529 /* If this is a station chanctx, stay active for a half
530 * beacon period (minus channel switch time)
531 */
532 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530533 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530534
Sujith Manoharan878066e2014-08-27 12:07:24 +0530535 ath_dbg(common, CHAN_CTX,
536 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
537
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530538 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Sujith Manoharan367b3412014-09-05 09:50:57 +0530539 sc->sched.wait_switch = false;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530540
541 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
Sujith Manoharan167bf96d2014-09-10 19:16:00 +0530542
543 if (sc->sched.extend_absence) {
Felix Fietkauec70abe2014-06-11 16:18:12 +0530544 sc->sched.beacon_miss = 0;
545 tsf_time *= 3;
546 }
547
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530548 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530549 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530550 sc->sched.switch_start_time = tsf_time;
551
Felix Fietkau42eda112014-06-11 16:18:14 +0530552 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530553 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530554 break;
555 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
556 if (sc->cur_chan == &sc->offchannel.chan ||
557 sc->cur_chan->switch_after_beacon)
558 break;
559
560 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
561 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
562 break;
563 case ATH_CHANCTX_EVENT_UNASSIGN:
564 if (sc->cur_chan->assigned) {
565 if (sc->next_chan && !sc->next_chan->assigned &&
566 sc->next_chan != &sc->offchannel.chan)
567 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
568 break;
569 }
570
571 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
572 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
573 if (!ctx->assigned)
574 break;
575
576 sc->next_chan = ctx;
577 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
578 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530579 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530580 /*
581 * When adding a new channel context, check if a scan
582 * is in progress and abort it since the addition of
583 * a new channel context is usually followed by VIF
584 * assignment, in which case we have to start multi-channel
585 * operation.
586 */
587 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
588 ath_dbg(common, CHAN_CTX,
589 "Aborting HW scan to add new context\n");
590
591 spin_unlock_bh(&sc->chan_lock);
592 del_timer_sync(&sc->offchannel.timer);
593 ath_scan_complete(sc, true);
594 spin_lock_bh(&sc->chan_lock);
595 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530596 break;
597 case ATH_CHANCTX_EVENT_CHANGE:
598 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530599 }
600
601 spin_unlock_bh(&sc->chan_lock);
602}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530603
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530604void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
605 enum ath_chanctx_event ev)
606{
607 if (sc->sched.beacon_pending)
608 ath_chanctx_event(sc, NULL, ev);
609}
610
611void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
612 enum ath_chanctx_event ev)
613{
614 sc->sched.next_tbtt = ts;
615 ath_chanctx_event(sc, NULL, ev);
616}
617
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530618static int ath_scan_channel_duration(struct ath_softc *sc,
619 struct ieee80211_channel *chan)
620{
621 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
622
623 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
624 return (HZ / 9); /* ~110 ms */
625
626 return (HZ / 16); /* ~60 ms */
627}
628
Sujith Manoharan922c9432014-08-23 13:29:15 +0530629static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
630 struct cfg80211_chan_def *chandef)
631{
632 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
633
634 spin_lock_bh(&sc->chan_lock);
635
636 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
637 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530638 if (chandef)
639 ctx->chandef = *chandef;
Sujith Manoharancbc775d2014-09-10 19:15:56 +0530640
641 sc->sched.offchannel_pending = true;
642 sc->sched.wait_switch = true;
643 sc->sched.offchannel_duration =
644 jiffies_to_usecs(sc->offchannel.duration) +
645 sc->sched.channel_switch_time;
646
Sujith Manoharan922c9432014-08-23 13:29:15 +0530647 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharanda0162f2014-09-05 09:50:56 +0530648 ath_dbg(common, CHAN_CTX,
649 "Set offchannel_pending to true\n");
Sujith Manoharan922c9432014-08-23 13:29:15 +0530650 return;
651 }
652
653 sc->next_chan = ctx;
654 if (chandef) {
655 ctx->chandef = *chandef;
656 ath_dbg(common, CHAN_CTX,
657 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
658 }
659
660 if (sc->next_chan == &sc->offchannel.chan) {
661 sc->sched.offchannel_duration =
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530662 jiffies_to_usecs(sc->offchannel.duration) +
Sujith Manoharan922c9432014-08-23 13:29:15 +0530663 sc->sched.channel_switch_time;
664
665 if (chandef) {
666 ath_dbg(common, CHAN_CTX,
667 "Offchannel duration for chan %d MHz : %u\n",
668 chandef->center_freq1,
669 sc->sched.offchannel_duration);
670 }
671 }
672 spin_unlock_bh(&sc->chan_lock);
673 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
674}
675
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530676static void ath_chanctx_offchan_switch(struct ath_softc *sc,
677 struct ieee80211_channel *chan)
678{
679 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
680 struct cfg80211_chan_def chandef;
681
682 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
683 ath_dbg(common, CHAN_CTX,
684 "Channel definition created: %d MHz\n", chandef.center_freq1);
685
686 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
687}
688
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530689static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
690 bool active)
691{
692 struct ath_chanctx *ctx;
693
694 ath_for_each_chanctx(sc, ctx) {
695 if (!ctx->assigned || list_empty(&ctx->vifs))
696 continue;
697 if (active && !ctx->active)
698 continue;
699
700 if (ctx->switch_after_beacon)
701 return ctx;
702 }
703
704 return &sc->chanctx[0];
705}
706
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530707static void
708ath_scan_next_channel(struct ath_softc *sc)
709{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530710 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530711 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
712 struct ieee80211_channel *chan;
713
714 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530715 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530716 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
717 "scan_idx: %d, n_channels: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530718 sc->offchannel.scan_idx,
719 req->n_channels);
720
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530721 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
722 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
723 NULL);
724 return;
725 }
726
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530727 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530728 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530729 sc->offchannel.scan_idx);
730
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530731 chan = req->channels[sc->offchannel.scan_idx++];
732 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
733 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530734
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530735 ath_chanctx_offchan_switch(sc, chan);
736}
737
738void ath_offchannel_next(struct ath_softc *sc)
739{
740 struct ieee80211_vif *vif;
741
742 if (sc->offchannel.scan_req) {
743 vif = sc->offchannel.scan_vif;
744 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
745 ath_scan_next_channel(sc);
746 } else if (sc->offchannel.roc_vif) {
747 vif = sc->offchannel.roc_vif;
748 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +0530749 sc->offchannel.duration =
750 msecs_to_jiffies(sc->offchannel.roc_duration);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530751 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
752 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
753 } else {
754 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
755 NULL);
756 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
757 if (sc->ps_idle)
758 ath_cancel_work(sc);
759 }
760}
761
762void ath_roc_complete(struct ath_softc *sc, bool abort)
763{
764 sc->offchannel.roc_vif = NULL;
765 sc->offchannel.roc_chan = NULL;
766 if (!abort)
767 ieee80211_remain_on_channel_expired(sc->hw);
768 ath_offchannel_next(sc);
769 ath9k_ps_restore(sc);
770}
771
772void ath_scan_complete(struct ath_softc *sc, bool abort)
773{
774 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
775
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530776 if (abort)
777 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
778 else
779 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
780
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530781 sc->offchannel.scan_req = NULL;
782 sc->offchannel.scan_vif = NULL;
783 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
784 ieee80211_scan_completed(sc->hw, abort);
785 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Sujith Manoharand0975ed2014-09-10 19:15:57 +0530786 spin_lock_bh(&sc->chan_lock);
787 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
788 sc->sched.force_noa_update = true;
789 spin_unlock_bh(&sc->chan_lock);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530790 ath_offchannel_next(sc);
791 ath9k_ps_restore(sc);
792}
793
794static void ath_scan_send_probe(struct ath_softc *sc,
795 struct cfg80211_ssid *ssid)
796{
797 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
798 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
799 struct ath_tx_control txctl = {};
800 struct sk_buff *skb;
801 struct ieee80211_tx_info *info;
802 int band = sc->offchannel.chan.chandef.chan->band;
803
804 skb = ieee80211_probereq_get(sc->hw, vif,
805 ssid->ssid, ssid->ssid_len, req->ie_len);
806 if (!skb)
807 return;
808
809 info = IEEE80211_SKB_CB(skb);
810 if (req->no_cck)
811 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
812
813 if (req->ie_len)
814 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
815
816 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
817
818 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
819 goto error;
820
821 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
822 txctl.force_channel = true;
823 if (ath_tx_start(sc->hw, skb, &txctl))
824 goto error;
825
826 return;
827
828error:
829 ieee80211_free_txskb(sc->hw, skb);
830}
831
832static void ath_scan_channel_start(struct ath_softc *sc)
833{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530834 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530835 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
836 int i;
837
838 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
839 req->n_ssids) {
840 for (i = 0; i < req->n_ssids; i++)
841 ath_scan_send_probe(sc, &req->ssids[i]);
842
843 }
844
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530845 ath_dbg(common, CHAN_CTX,
Sujith Manoharan878066e2014-08-27 12:07:24 +0530846 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530847
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530848 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
849 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
850}
851
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530852static void ath_chanctx_timer(unsigned long data)
853{
854 struct ath_softc *sc = (struct ath_softc *) data;
Sujith Manoharan878066e2014-08-27 12:07:24 +0530855 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
856
857 ath_dbg(common, CHAN_CTX,
858 "Channel context timer invoked\n");
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530859
860 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
861}
862
863static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530864{
865 struct ath_softc *sc = (struct ath_softc *)data;
866 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530867 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
868
Sujith Manoharan878066e2014-08-27 12:07:24 +0530869 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530870 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530871
872 switch (sc->offchannel.state) {
873 case ATH_OFFCHANNEL_PROBE_WAIT:
874 if (!sc->offchannel.scan_req)
875 return;
876
877 /* get first active channel context */
878 ctx = ath_chanctx_get_oper_chan(sc, true);
879 if (ctx->active) {
Sujith Manoharan878066e2014-08-27 12:07:24 +0530880 ath_dbg(common, CHAN_CTX,
881 "Switch to oper/active context, "
882 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
883
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530884 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
885 ath_chanctx_switch(sc, ctx, NULL);
886 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
887 break;
888 }
889 /* fall through */
890 case ATH_OFFCHANNEL_SUSPEND:
891 if (!sc->offchannel.scan_req)
892 return;
893
894 ath_scan_next_channel(sc);
895 break;
896 case ATH_OFFCHANNEL_ROC_START:
897 case ATH_OFFCHANNEL_ROC_WAIT:
898 ctx = ath_chanctx_get_oper_chan(sc, false);
899 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
900 ath_chanctx_switch(sc, ctx, NULL);
901 break;
902 default:
903 break;
904 }
905}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530906
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530907static bool
908ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
909 bool powersave)
910{
911 struct ieee80211_vif *vif = avp->vif;
912 struct ieee80211_sta *sta = NULL;
913 struct ieee80211_hdr_3addr *nullfunc;
914 struct ath_tx_control txctl;
915 struct sk_buff *skb;
916 int band = sc->cur_chan->chandef.chan->band;
917
918 switch (vif->type) {
919 case NL80211_IFTYPE_STATION:
920 if (!vif->bss_conf.assoc)
921 return false;
922
923 skb = ieee80211_nullfunc_get(sc->hw, vif);
924 if (!skb)
925 return false;
926
927 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
928 if (powersave)
929 nullfunc->frame_control |=
930 cpu_to_le16(IEEE80211_FCTL_PM);
931
932 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
933 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
934 dev_kfree_skb_any(skb);
935 return false;
936 }
937 break;
938 default:
939 return false;
940 }
941
942 memset(&txctl, 0, sizeof(txctl));
943 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
944 txctl.sta = sta;
945 txctl.force_channel = true;
946 if (ath_tx_start(sc->hw, skb, &txctl)) {
947 ieee80211_free_txskb(sc->hw, skb);
948 return false;
949 }
950
951 return true;
952}
953
954static bool
955ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
956{
957 struct ath_vif *avp;
958 bool sent = false;
959
960 rcu_read_lock();
961 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
962 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
963 sent = true;
964 }
965 rcu_read_unlock();
966
967 return sent;
968}
969
970static bool ath_chanctx_defer_switch(struct ath_softc *sc)
971{
Sujith Manoharan878066e2014-08-27 12:07:24 +0530972 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
973
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530974 if (sc->cur_chan == &sc->offchannel.chan)
975 return false;
976
977 switch (sc->sched.state) {
978 case ATH_CHANCTX_STATE_SWITCH:
979 return false;
980 case ATH_CHANCTX_STATE_IDLE:
981 if (!sc->cur_chan->switch_after_beacon)
982 return false;
983
Sujith Manoharan878066e2014-08-27 12:07:24 +0530984 ath_dbg(common, CHAN_CTX,
985 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
986
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530987 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
988 break;
989 default:
990 break;
991 }
992
993 return true;
994}
995
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530996static void ath_offchannel_channel_change(struct ath_softc *sc)
997{
998 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
999
Sujith Manoharan878066e2014-08-27 12:07:24 +05301000 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301001 __func__, offchannel_state_string(sc->offchannel.state));
1002
1003 switch (sc->offchannel.state) {
1004 case ATH_OFFCHANNEL_PROBE_SEND:
1005 if (!sc->offchannel.scan_req)
1006 return;
1007
1008 if (sc->cur_chan->chandef.chan !=
1009 sc->offchannel.chan.chandef.chan)
1010 return;
1011
1012 ath_scan_channel_start(sc);
1013 break;
1014 case ATH_OFFCHANNEL_IDLE:
1015 if (!sc->offchannel.scan_req)
1016 return;
1017
1018 ath_scan_complete(sc, false);
1019 break;
1020 case ATH_OFFCHANNEL_ROC_START:
1021 if (sc->cur_chan != &sc->offchannel.chan)
1022 break;
1023
1024 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
Sujith Manoharanbb628eb2014-09-05 08:03:13 +05301025 mod_timer(&sc->offchannel.timer,
1026 jiffies + sc->offchannel.duration);
Sujith Manoharan55254ee2014-08-23 13:29:11 +05301027 ieee80211_ready_on_channel(sc->hw);
1028 break;
1029 case ATH_OFFCHANNEL_ROC_DONE:
1030 ath_roc_complete(sc, false);
1031 break;
1032 default:
1033 break;
1034 }
1035}
1036
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301037void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1038{
Sujith Manoharan878066e2014-08-27 12:07:24 +05301039 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301040 struct timespec ts;
1041 bool measure_time = false;
1042 bool send_ps = false;
1043
1044 spin_lock_bh(&sc->chan_lock);
1045 if (!sc->next_chan) {
1046 spin_unlock_bh(&sc->chan_lock);
1047 return;
1048 }
1049
1050 if (!force && ath_chanctx_defer_switch(sc)) {
1051 spin_unlock_bh(&sc->chan_lock);
1052 return;
1053 }
1054
Sujith Manoharan878066e2014-08-27 12:07:24 +05301055 ath_dbg(common, CHAN_CTX,
1056 "%s: current: %d MHz, next: %d MHz\n",
1057 __func__,
1058 sc->cur_chan->chandef.center_freq1,
1059 sc->next_chan->chandef.center_freq1);
1060
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301061 if (sc->cur_chan != sc->next_chan) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301062 ath_dbg(common, CHAN_CTX,
1063 "Stopping current chanctx: %d\n",
1064 sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301065 sc->cur_chan->stopped = true;
1066 spin_unlock_bh(&sc->chan_lock);
1067
1068 if (sc->next_chan == &sc->offchannel.chan) {
1069 getrawmonotonic(&ts);
1070 measure_time = true;
1071 }
1072 __ath9k_flush(sc->hw, ~0, true);
1073
1074 if (ath_chanctx_send_ps_frame(sc, true))
1075 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
1076
1077 send_ps = true;
1078 spin_lock_bh(&sc->chan_lock);
1079
1080 if (sc->cur_chan != &sc->offchannel.chan) {
1081 getrawmonotonic(&sc->cur_chan->tsf_ts);
1082 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1083 }
1084 }
1085 sc->cur_chan = sc->next_chan;
1086 sc->cur_chan->stopped = false;
1087 sc->next_chan = NULL;
Sujith Manoharan124130d2014-09-10 19:15:58 +05301088
1089 if (!sc->sched.offchannel_pending)
1090 sc->sched.offchannel_duration = 0;
1091
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301092 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1093 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1094
1095 spin_unlock_bh(&sc->chan_lock);
1096
1097 if (sc->sc_ah->chip_fullsleep ||
1098 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1099 sizeof(sc->cur_chandef))) {
Sujith Manoharan878066e2014-08-27 12:07:24 +05301100 ath_dbg(common, CHAN_CTX,
1101 "%s: Set channel %d MHz\n",
1102 __func__, sc->cur_chan->chandef.center_freq1);
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +05301103 ath_set_channel(sc);
1104 if (measure_time)
1105 sc->sched.channel_switch_time =
1106 ath9k_hw_get_tsf_offset(&ts, NULL);
1107 }
1108 if (send_ps)
1109 ath_chanctx_send_ps_frame(sc, false);
1110
1111 ath_offchannel_channel_change(sc);
1112 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1113}
1114
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +05301115static void ath_chanctx_work(struct work_struct *work)
1116{
1117 struct ath_softc *sc = container_of(work, struct ath_softc,
1118 chanctx_work);
1119 mutex_lock(&sc->mutex);
1120 ath_chanctx_set_next(sc, false);
1121 mutex_unlock(&sc->mutex);
1122}
1123
Sujith Manoharane90e3022014-08-23 13:29:20 +05301124void ath9k_offchannel_init(struct ath_softc *sc)
1125{
1126 struct ath_chanctx *ctx;
1127 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1128 struct ieee80211_supported_band *sband;
1129 struct ieee80211_channel *chan;
1130 int i;
1131
1132 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1133 if (!sband->n_channels)
1134 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1135
1136 chan = &sband->channels[0];
1137
1138 ctx = &sc->offchannel.chan;
1139 INIT_LIST_HEAD(&ctx->vifs);
1140 ctx->txpower = ATH_TXPOWER_MAX;
1141 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1142
1143 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1144 INIT_LIST_HEAD(&ctx->acq[i]);
1145
1146 sc->offchannel.chan.offchannel = true;
1147}
1148
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301149void ath9k_init_channel_context(struct ath_softc *sc)
1150{
1151 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1152
1153 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1154 (unsigned long)sc);
1155 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1156 (unsigned long)sc);
1157}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301158
Sujith Manoharanea22df22014-08-23 13:29:07 +05301159void ath9k_deinit_channel_context(struct ath_softc *sc)
1160{
1161 cancel_work_sync(&sc->chanctx_work);
1162}
1163
Sujith Manoharan499afac2014-08-22 20:39:31 +05301164bool ath9k_is_chanctx_enabled(void)
1165{
1166 return (ath9k_use_chanctx == 1);
1167}
1168
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301169/********************/
1170/* Queue management */
1171/********************/
1172
1173void ath9k_chanctx_wake_queues(struct ath_softc *sc)
1174{
1175 struct ath_hw *ah = sc->sc_ah;
1176 int i;
1177
1178 if (sc->cur_chan == &sc->offchannel.chan) {
1179 ieee80211_wake_queue(sc->hw,
1180 sc->hw->offchannel_tx_hw_queue);
1181 } else {
1182 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1183 ieee80211_wake_queue(sc->hw,
1184 sc->cur_chan->hw_queue_base + i);
1185 }
1186
1187 if (ah->opmode == NL80211_IFTYPE_AP)
1188 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1189}
1190
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301191/*****************/
1192/* P2P Powersave */
1193/*****************/
1194
1195static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301196{
1197 struct ath_hw *ah = sc->sc_ah;
1198 s32 tsf, target_tsf;
1199
1200 if (!avp || !avp->noa.has_next_tsf)
1201 return;
1202
1203 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1204
1205 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1206
1207 target_tsf = avp->noa.next_tsf;
1208 if (!avp->noa.absent)
1209 target_tsf -= ATH_P2P_PS_STOP_TIME;
1210
1211 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1212 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1213
1214 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1215}
1216
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301217static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1218{
1219 struct ath_vif *avp = (void *)vif->drv_priv;
1220 u32 tsf;
1221
1222 if (!sc->p2p_ps_timer)
1223 return;
1224
1225 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1226 return;
1227
1228 sc->p2p_ps_vif = avp;
1229 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1230 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1231 ath9k_update_p2p_ps_timer(sc, avp);
1232}
1233
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301234static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1235{
1236 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1237 u8 switch_time, ctwin;
1238
1239 /*
1240 * Channel switch in multi-channel mode is deferred
1241 * by a quarter beacon interval when handling
1242 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1243 * interface is guaranteed to be discoverable
1244 * for that duration after a TBTT.
1245 */
1246 switch_time = cur_conf->beacon_interval / 4;
1247
1248 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1249 if (ctwin && (ctwin < switch_time))
1250 return ctwin;
1251
1252 if (switch_time < P2P_DEFAULT_CTWIN)
1253 return 0;
1254
1255 return P2P_DEFAULT_CTWIN;
1256}
1257
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301258void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1259 struct sk_buff *skb)
1260{
1261 static const u8 noa_ie_hdr[] = {
1262 WLAN_EID_VENDOR_SPECIFIC, /* type */
1263 0, /* length */
1264 0x50, 0x6f, 0x9a, /* WFA OUI */
1265 0x09, /* P2P subtype */
1266 0x0c, /* Notice of Absence */
1267 0x00, /* LSB of little-endian len */
1268 0x00, /* MSB of little-endian len */
1269 };
1270
1271 struct ieee80211_p2p_noa_attr *noa;
1272 int noa_len, noa_desc, i = 0;
1273 u8 *hdr;
1274
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301275 if (!avp->offchannel_duration && !avp->noa_duration)
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301276 return;
1277
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301278 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301279 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1280
1281 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1282 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1283 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1284 hdr[7] = noa_len;
1285
1286 noa = (void *) skb_put(skb, noa_len);
1287 memset(noa, 0, noa_len);
1288
1289 noa->index = avp->noa_index;
Sujith Manoharanfdcf1bd2014-09-05 08:03:14 +05301290 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1291
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301292 if (avp->noa_duration) {
1293 if (avp->periodic_noa) {
1294 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1295 noa->desc[i].count = 255;
1296 noa->desc[i].interval = cpu_to_le32(interval);
1297 } else {
1298 noa->desc[i].count = 1;
1299 }
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301300
Sujith Manoharand0975ed2014-09-10 19:15:57 +05301301 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1302 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301303 i++;
1304 }
1305
1306 if (avp->offchannel_duration) {
1307 noa->desc[i].count = 1;
1308 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1309 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1310 }
1311}
1312
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301313void ath9k_p2p_ps_timer(void *priv)
1314{
1315 struct ath_softc *sc = priv;
1316 struct ath_vif *avp = sc->p2p_ps_vif;
1317 struct ieee80211_vif *vif;
1318 struct ieee80211_sta *sta;
1319 struct ath_node *an;
1320 u32 tsf;
1321
1322 del_timer_sync(&sc->sched.timer);
1323 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1324 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1325
1326 if (!avp || avp->chanctx != sc->cur_chan)
1327 return;
1328
1329 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1330 if (!avp->noa.absent)
1331 tsf += ATH_P2P_PS_STOP_TIME;
1332
1333 if (!avp->noa.has_next_tsf ||
1334 avp->noa.next_tsf - tsf > BIT(31))
1335 ieee80211_update_p2p_noa(&avp->noa, tsf);
1336
1337 ath9k_update_p2p_ps_timer(sc, avp);
1338
1339 rcu_read_lock();
1340
1341 vif = avp->vif;
1342 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1343 if (!sta)
1344 goto out;
1345
1346 an = (void *) sta->drv_priv;
1347 if (an->sleeping == !!avp->noa.absent)
1348 goto out;
1349
1350 an->sleeping = avp->noa.absent;
1351 if (an->sleeping)
1352 ath_tx_aggr_sleep(sta, sc, an);
1353 else
1354 ath_tx_aggr_wakeup(sc, an);
1355
1356out:
1357 rcu_read_unlock();
1358}
1359
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301360void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1361 struct ieee80211_vif *vif)
1362{
1363 unsigned long flags;
1364
1365 spin_lock_bh(&sc->sc_pcu_lock);
1366 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1367 if (!(sc->ps_flags & PS_BEACON_SYNC))
1368 ath9k_update_p2p_ps(sc, vif);
1369 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1370 spin_unlock_bh(&sc->sc_pcu_lock);
1371}
1372
1373void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1374{
1375 if (sc->p2p_ps_vif)
1376 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1377}
1378
1379void ath9k_p2p_remove_vif(struct ath_softc *sc,
1380 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301381{
1382 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301383
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301384 spin_lock_bh(&sc->sc_pcu_lock);
1385 if (avp == sc->p2p_ps_vif) {
1386 sc->p2p_ps_vif = NULL;
1387 ath9k_update_p2p_ps_timer(sc, NULL);
1388 }
1389 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301390}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301391
1392int ath9k_init_p2p(struct ath_softc *sc)
1393{
1394 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1395 NULL, sc, AR_FIRST_NDP_TIMER);
1396 if (!sc->p2p_ps_timer)
1397 return -ENOMEM;
1398
1399 return 0;
1400}
1401
1402void ath9k_deinit_p2p(struct ath_softc *sc)
1403{
1404 if (sc->p2p_ps_timer)
1405 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1406}
1407
1408#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */