blob: b369c485978e03e6a1ffdb67b304cd88cfe09802 [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{
291 struct ath_hw *ah = sc->sc_ah;
292
293 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
294 tsf_time -= ath9k_hw_gettsf32(ah);
295 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
296 mod_timer(&sc->sched.timer, tsf_time);
297}
298
Felix Fietkau748299f2014-06-11 16:18:04 +0530299void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
300 enum ath_chanctx_event ev)
301{
302 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530303 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530304 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530305 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530306 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530307 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530308 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530309 bool noa_changed = false;
310
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530311 ath_dbg(common, CHAN_CTX, "event: %s, state: %s\n",
312 chanctx_event_string(ev),
313 chanctx_state_string(sc->sched.state));
314
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530315 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
320 switch (ev) {
321 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530322 if (avp->offchannel_duration)
323 avp->offchannel_duration = 0;
324
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530325 if (avp->chanctx != sc->cur_chan)
326 break;
327
328 if (sc->sched.offchannel_pending) {
329 sc->sched.offchannel_pending = false;
330 sc->next_chan = &sc->offchannel.chan;
331 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
332 }
333
334 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
335 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
336 sc->next_chan = ctx;
337 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
338 }
339
340 /* if the timer missed its window, use the next interval */
341 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
342 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
343
Felix Fietkau748299f2014-06-11 16:18:04 +0530344 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
345 break;
346
347 sc->sched.beacon_pending = true;
348 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530349
Felix Fietkau74148632014-06-11 16:18:11 +0530350 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530351 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
352
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530353 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530354 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530355 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530356 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530357
Felix Fietkau74148632014-06-11 16:18:11 +0530358 /* Prevent wrap-around issues */
359 if (avp->periodic_noa_duration &&
360 tsf_time - avp->periodic_noa_start > BIT(30))
361 avp->periodic_noa_duration = 0;
362
363 if (ctx->active && !avp->periodic_noa_duration) {
364 avp->periodic_noa_start = tsf_time;
365 avp->periodic_noa_duration =
366 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
367 sc->sched.channel_switch_time;
368 noa_changed = true;
369 } else if (!ctx->active && avp->periodic_noa_duration) {
370 avp->periodic_noa_duration = 0;
371 noa_changed = true;
372 }
373
Felix Fietkauec70abe2014-06-11 16:18:12 +0530374 /* If at least two consecutive beacons were missed on the STA
375 * chanctx, stay on the STA channel for one extra beacon period,
376 * to resync the timer properly.
377 */
378 if (ctx->active && sc->sched.beacon_miss >= 2)
379 sc->sched.offchannel_duration = 3 * beacon_int / 2;
380
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530381 if (sc->sched.offchannel_duration) {
382 noa_changed = true;
383 avp->offchannel_start = tsf_time;
384 avp->offchannel_duration =
385 sc->sched.offchannel_duration;
386 }
387
388 if (noa_changed)
389 avp->noa_index++;
Felix Fietkau748299f2014-06-11 16:18:04 +0530390 break;
391 case ATH_CHANCTX_EVENT_BEACON_SENT:
392 if (!sc->sched.beacon_pending)
393 break;
394
395 sc->sched.beacon_pending = false;
396 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
397 break;
398
Felix Fietkau748299f2014-06-11 16:18:04 +0530399 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530400 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530401 break;
402 case ATH_CHANCTX_EVENT_TSF_TIMER:
403 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
404 break;
405
Felix Fietkauec70abe2014-06-11 16:18:12 +0530406 if (!sc->cur_chan->switch_after_beacon &&
407 sc->sched.beacon_pending)
408 sc->sched.beacon_miss++;
409
Felix Fietkau748299f2014-06-11 16:18:04 +0530410 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
411 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
412 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530413 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530414 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
415 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530416 break;
417
418 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530419 sc->sched.beacon_pending = false;
420 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530421
422 /* TSF time might have been updated by the incoming beacon,
423 * need update the channel switch timer to reflect the change.
424 */
425 tsf_time = sc->sched.switch_start_time;
426 tsf_time -= (u32) sc->cur_chan->tsf_val +
427 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
428 tsf_time += ath9k_hw_gettsf32(ah);
429
Felix Fietkau42eda112014-06-11 16:18:14 +0530430
431 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530432 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530433 case ATH_CHANCTX_EVENT_ASSOC:
434 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
435 avp->chanctx != sc->cur_chan)
436 break;
437
438 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
439 /* fall through */
440 case ATH_CHANCTX_EVENT_SWITCH:
441 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
442 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
443 sc->cur_chan->switch_after_beacon ||
444 sc->cur_chan == &sc->offchannel.chan)
445 break;
446
447 /* If this is a station chanctx, stay active for a half
448 * beacon period (minus channel switch time)
449 */
450 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530451 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530452
453 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530454
455 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
456 if (sc->sched.beacon_miss >= 2) {
457 sc->sched.beacon_miss = 0;
458 tsf_time *= 3;
459 }
460
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530461 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530462 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530463 sc->sched.switch_start_time = tsf_time;
464
Felix Fietkau42eda112014-06-11 16:18:14 +0530465 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530466 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530467 break;
468 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
469 if (sc->cur_chan == &sc->offchannel.chan ||
470 sc->cur_chan->switch_after_beacon)
471 break;
472
473 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
474 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
475 break;
476 case ATH_CHANCTX_EVENT_UNASSIGN:
477 if (sc->cur_chan->assigned) {
478 if (sc->next_chan && !sc->next_chan->assigned &&
479 sc->next_chan != &sc->offchannel.chan)
480 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
481 break;
482 }
483
484 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
485 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
486 if (!ctx->assigned)
487 break;
488
489 sc->next_chan = ctx;
490 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
491 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530492 case ATH_CHANCTX_EVENT_ASSIGN:
Sujith Manoharan4c7e9ae2014-08-24 21:16:13 +0530493 /*
494 * When adding a new channel context, check if a scan
495 * is in progress and abort it since the addition of
496 * a new channel context is usually followed by VIF
497 * assignment, in which case we have to start multi-channel
498 * operation.
499 */
500 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
501 ath_dbg(common, CHAN_CTX,
502 "Aborting HW scan to add new context\n");
503
504 spin_unlock_bh(&sc->chan_lock);
505 del_timer_sync(&sc->offchannel.timer);
506 ath_scan_complete(sc, true);
507 spin_lock_bh(&sc->chan_lock);
508 }
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530509 break;
510 case ATH_CHANCTX_EVENT_CHANGE:
511 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530512 }
513
514 spin_unlock_bh(&sc->chan_lock);
515}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530516
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530517void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
518 enum ath_chanctx_event ev)
519{
520 if (sc->sched.beacon_pending)
521 ath_chanctx_event(sc, NULL, ev);
522}
523
524void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
525 enum ath_chanctx_event ev)
526{
527 sc->sched.next_tbtt = ts;
528 ath_chanctx_event(sc, NULL, ev);
529}
530
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530531static int ath_scan_channel_duration(struct ath_softc *sc,
532 struct ieee80211_channel *chan)
533{
534 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
535
536 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
537 return (HZ / 9); /* ~110 ms */
538
539 return (HZ / 16); /* ~60 ms */
540}
541
Sujith Manoharan922c9432014-08-23 13:29:15 +0530542static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
543 struct cfg80211_chan_def *chandef)
544{
545 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
546
547 spin_lock_bh(&sc->chan_lock);
548
549 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
550 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
551 sc->sched.offchannel_pending = true;
552 spin_unlock_bh(&sc->chan_lock);
553 return;
554 }
555
556 sc->next_chan = ctx;
557 if (chandef) {
558 ctx->chandef = *chandef;
559 ath_dbg(common, CHAN_CTX,
560 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
561 }
562
563 if (sc->next_chan == &sc->offchannel.chan) {
564 sc->sched.offchannel_duration =
565 TU_TO_USEC(sc->offchannel.duration) +
566 sc->sched.channel_switch_time;
567
568 if (chandef) {
569 ath_dbg(common, CHAN_CTX,
570 "Offchannel duration for chan %d MHz : %u\n",
571 chandef->center_freq1,
572 sc->sched.offchannel_duration);
573 }
574 }
575 spin_unlock_bh(&sc->chan_lock);
576 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
577}
578
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530579static void ath_chanctx_offchan_switch(struct ath_softc *sc,
580 struct ieee80211_channel *chan)
581{
582 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
583 struct cfg80211_chan_def chandef;
584
585 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
586 ath_dbg(common, CHAN_CTX,
587 "Channel definition created: %d MHz\n", chandef.center_freq1);
588
589 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
590}
591
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530592static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
593 bool active)
594{
595 struct ath_chanctx *ctx;
596
597 ath_for_each_chanctx(sc, ctx) {
598 if (!ctx->assigned || list_empty(&ctx->vifs))
599 continue;
600 if (active && !ctx->active)
601 continue;
602
603 if (ctx->switch_after_beacon)
604 return ctx;
605 }
606
607 return &sc->chanctx[0];
608}
609
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530610static void
611ath_scan_next_channel(struct ath_softc *sc)
612{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530613 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530614 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
615 struct ieee80211_channel *chan;
616
617 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530618 ath_dbg(common, CHAN_CTX,
619 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
620 sc->offchannel.scan_idx,
621 req->n_channels);
622
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530623 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
624 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
625 NULL);
626 return;
627 }
628
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530629 ath_dbg(common, CHAN_CTX,
630 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
631 sc->offchannel.scan_idx);
632
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530633 chan = req->channels[sc->offchannel.scan_idx++];
634 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
635 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530636
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530637 ath_chanctx_offchan_switch(sc, chan);
638}
639
640void ath_offchannel_next(struct ath_softc *sc)
641{
642 struct ieee80211_vif *vif;
643
644 if (sc->offchannel.scan_req) {
645 vif = sc->offchannel.scan_vif;
646 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
647 ath_scan_next_channel(sc);
648 } else if (sc->offchannel.roc_vif) {
649 vif = sc->offchannel.roc_vif;
650 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
651 sc->offchannel.duration = sc->offchannel.roc_duration;
652 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
653 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
654 } else {
655 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
656 NULL);
657 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
658 if (sc->ps_idle)
659 ath_cancel_work(sc);
660 }
661}
662
663void ath_roc_complete(struct ath_softc *sc, bool abort)
664{
665 sc->offchannel.roc_vif = NULL;
666 sc->offchannel.roc_chan = NULL;
667 if (!abort)
668 ieee80211_remain_on_channel_expired(sc->hw);
669 ath_offchannel_next(sc);
670 ath9k_ps_restore(sc);
671}
672
673void ath_scan_complete(struct ath_softc *sc, bool abort)
674{
675 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
676
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530677 if (abort)
678 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
679 else
680 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
681
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530682 sc->offchannel.scan_req = NULL;
683 sc->offchannel.scan_vif = NULL;
684 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
685 ieee80211_scan_completed(sc->hw, abort);
686 clear_bit(ATH_OP_SCANNING, &common->op_flags);
687 ath_offchannel_next(sc);
688 ath9k_ps_restore(sc);
689}
690
691static void ath_scan_send_probe(struct ath_softc *sc,
692 struct cfg80211_ssid *ssid)
693{
694 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
695 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
696 struct ath_tx_control txctl = {};
697 struct sk_buff *skb;
698 struct ieee80211_tx_info *info;
699 int band = sc->offchannel.chan.chandef.chan->band;
700
701 skb = ieee80211_probereq_get(sc->hw, vif,
702 ssid->ssid, ssid->ssid_len, req->ie_len);
703 if (!skb)
704 return;
705
706 info = IEEE80211_SKB_CB(skb);
707 if (req->no_cck)
708 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
709
710 if (req->ie_len)
711 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
712
713 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
714
715 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
716 goto error;
717
718 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
719 txctl.force_channel = true;
720 if (ath_tx_start(sc->hw, skb, &txctl))
721 goto error;
722
723 return;
724
725error:
726 ieee80211_free_txskb(sc->hw, skb);
727}
728
729static void ath_scan_channel_start(struct ath_softc *sc)
730{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530731 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530732 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
733 int i;
734
735 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
736 req->n_ssids) {
737 for (i = 0; i < req->n_ssids; i++)
738 ath_scan_send_probe(sc, &req->ssids[i]);
739
740 }
741
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530742 ath_dbg(common, CHAN_CTX,
743 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
744
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530745 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
746 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
747}
748
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530749static void ath_chanctx_timer(unsigned long data)
750{
751 struct ath_softc *sc = (struct ath_softc *) data;
752
753 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
754}
755
756static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530757{
758 struct ath_softc *sc = (struct ath_softc *)data;
759 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530760 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
761
762 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
763 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530764
765 switch (sc->offchannel.state) {
766 case ATH_OFFCHANNEL_PROBE_WAIT:
767 if (!sc->offchannel.scan_req)
768 return;
769
770 /* get first active channel context */
771 ctx = ath_chanctx_get_oper_chan(sc, true);
772 if (ctx->active) {
773 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
774 ath_chanctx_switch(sc, ctx, NULL);
775 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
776 break;
777 }
778 /* fall through */
779 case ATH_OFFCHANNEL_SUSPEND:
780 if (!sc->offchannel.scan_req)
781 return;
782
783 ath_scan_next_channel(sc);
784 break;
785 case ATH_OFFCHANNEL_ROC_START:
786 case ATH_OFFCHANNEL_ROC_WAIT:
787 ctx = ath_chanctx_get_oper_chan(sc, false);
788 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
789 ath_chanctx_switch(sc, ctx, NULL);
790 break;
791 default:
792 break;
793 }
794}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530795
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530796static bool
797ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
798 bool powersave)
799{
800 struct ieee80211_vif *vif = avp->vif;
801 struct ieee80211_sta *sta = NULL;
802 struct ieee80211_hdr_3addr *nullfunc;
803 struct ath_tx_control txctl;
804 struct sk_buff *skb;
805 int band = sc->cur_chan->chandef.chan->band;
806
807 switch (vif->type) {
808 case NL80211_IFTYPE_STATION:
809 if (!vif->bss_conf.assoc)
810 return false;
811
812 skb = ieee80211_nullfunc_get(sc->hw, vif);
813 if (!skb)
814 return false;
815
816 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
817 if (powersave)
818 nullfunc->frame_control |=
819 cpu_to_le16(IEEE80211_FCTL_PM);
820
821 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
822 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
823 dev_kfree_skb_any(skb);
824 return false;
825 }
826 break;
827 default:
828 return false;
829 }
830
831 memset(&txctl, 0, sizeof(txctl));
832 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
833 txctl.sta = sta;
834 txctl.force_channel = true;
835 if (ath_tx_start(sc->hw, skb, &txctl)) {
836 ieee80211_free_txskb(sc->hw, skb);
837 return false;
838 }
839
840 return true;
841}
842
843static bool
844ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
845{
846 struct ath_vif *avp;
847 bool sent = false;
848
849 rcu_read_lock();
850 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
851 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
852 sent = true;
853 }
854 rcu_read_unlock();
855
856 return sent;
857}
858
859static bool ath_chanctx_defer_switch(struct ath_softc *sc)
860{
861 if (sc->cur_chan == &sc->offchannel.chan)
862 return false;
863
864 switch (sc->sched.state) {
865 case ATH_CHANCTX_STATE_SWITCH:
866 return false;
867 case ATH_CHANCTX_STATE_IDLE:
868 if (!sc->cur_chan->switch_after_beacon)
869 return false;
870
871 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
872 break;
873 default:
874 break;
875 }
876
877 return true;
878}
879
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530880static void ath_offchannel_channel_change(struct ath_softc *sc)
881{
882 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
883
884 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
885 __func__, offchannel_state_string(sc->offchannel.state));
886
887 switch (sc->offchannel.state) {
888 case ATH_OFFCHANNEL_PROBE_SEND:
889 if (!sc->offchannel.scan_req)
890 return;
891
892 if (sc->cur_chan->chandef.chan !=
893 sc->offchannel.chan.chandef.chan)
894 return;
895
896 ath_scan_channel_start(sc);
897 break;
898 case ATH_OFFCHANNEL_IDLE:
899 if (!sc->offchannel.scan_req)
900 return;
901
902 ath_scan_complete(sc, false);
903 break;
904 case ATH_OFFCHANNEL_ROC_START:
905 if (sc->cur_chan != &sc->offchannel.chan)
906 break;
907
908 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
909 mod_timer(&sc->offchannel.timer, jiffies +
910 msecs_to_jiffies(sc->offchannel.duration));
911 ieee80211_ready_on_channel(sc->hw);
912 break;
913 case ATH_OFFCHANNEL_ROC_DONE:
914 ath_roc_complete(sc, false);
915 break;
916 default:
917 break;
918 }
919}
920
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530921void ath_chanctx_set_next(struct ath_softc *sc, bool force)
922{
923 struct timespec ts;
924 bool measure_time = false;
925 bool send_ps = false;
926
927 spin_lock_bh(&sc->chan_lock);
928 if (!sc->next_chan) {
929 spin_unlock_bh(&sc->chan_lock);
930 return;
931 }
932
933 if (!force && ath_chanctx_defer_switch(sc)) {
934 spin_unlock_bh(&sc->chan_lock);
935 return;
936 }
937
938 if (sc->cur_chan != sc->next_chan) {
939 sc->cur_chan->stopped = true;
940 spin_unlock_bh(&sc->chan_lock);
941
942 if (sc->next_chan == &sc->offchannel.chan) {
943 getrawmonotonic(&ts);
944 measure_time = true;
945 }
946 __ath9k_flush(sc->hw, ~0, true);
947
948 if (ath_chanctx_send_ps_frame(sc, true))
949 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
950
951 send_ps = true;
952 spin_lock_bh(&sc->chan_lock);
953
954 if (sc->cur_chan != &sc->offchannel.chan) {
955 getrawmonotonic(&sc->cur_chan->tsf_ts);
956 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
957 }
958 }
959 sc->cur_chan = sc->next_chan;
960 sc->cur_chan->stopped = false;
961 sc->next_chan = NULL;
962 sc->sched.offchannel_duration = 0;
963 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
964 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
965
966 spin_unlock_bh(&sc->chan_lock);
967
968 if (sc->sc_ah->chip_fullsleep ||
969 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
970 sizeof(sc->cur_chandef))) {
971 ath_set_channel(sc);
972 if (measure_time)
973 sc->sched.channel_switch_time =
974 ath9k_hw_get_tsf_offset(&ts, NULL);
975 }
976 if (send_ps)
977 ath_chanctx_send_ps_frame(sc, false);
978
979 ath_offchannel_channel_change(sc);
980 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
981}
982
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +0530983static void ath_chanctx_work(struct work_struct *work)
984{
985 struct ath_softc *sc = container_of(work, struct ath_softc,
986 chanctx_work);
987 mutex_lock(&sc->mutex);
988 ath_chanctx_set_next(sc, false);
989 mutex_unlock(&sc->mutex);
990}
991
Sujith Manoharane90e3022014-08-23 13:29:20 +0530992void ath9k_offchannel_init(struct ath_softc *sc)
993{
994 struct ath_chanctx *ctx;
995 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
996 struct ieee80211_supported_band *sband;
997 struct ieee80211_channel *chan;
998 int i;
999
1000 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1001 if (!sband->n_channels)
1002 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1003
1004 chan = &sband->channels[0];
1005
1006 ctx = &sc->offchannel.chan;
1007 INIT_LIST_HEAD(&ctx->vifs);
1008 ctx->txpower = ATH_TXPOWER_MAX;
1009 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1010
1011 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1012 INIT_LIST_HEAD(&ctx->acq[i]);
1013
1014 sc->offchannel.chan.offchannel = true;
1015}
1016
Sujith Manoharan705d0bf2014-08-23 13:29:06 +05301017void ath9k_init_channel_context(struct ath_softc *sc)
1018{
1019 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1020
1021 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1022 (unsigned long)sc);
1023 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1024 (unsigned long)sc);
1025}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301026
Sujith Manoharanea22df22014-08-23 13:29:07 +05301027void ath9k_deinit_channel_context(struct ath_softc *sc)
1028{
1029 cancel_work_sync(&sc->chanctx_work);
1030}
1031
Sujith Manoharan499afac2014-08-22 20:39:31 +05301032bool ath9k_is_chanctx_enabled(void)
1033{
1034 return (ath9k_use_chanctx == 1);
1035}
1036
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301037/********************/
1038/* Queue management */
1039/********************/
1040
1041void ath9k_chanctx_wake_queues(struct ath_softc *sc)
1042{
1043 struct ath_hw *ah = sc->sc_ah;
1044 int i;
1045
1046 if (sc->cur_chan == &sc->offchannel.chan) {
1047 ieee80211_wake_queue(sc->hw,
1048 sc->hw->offchannel_tx_hw_queue);
1049 } else {
1050 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1051 ieee80211_wake_queue(sc->hw,
1052 sc->cur_chan->hw_queue_base + i);
1053 }
1054
1055 if (ah->opmode == NL80211_IFTYPE_AP)
1056 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1057}
1058
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301059/*****************/
1060/* P2P Powersave */
1061/*****************/
1062
1063static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301064{
1065 struct ath_hw *ah = sc->sc_ah;
1066 s32 tsf, target_tsf;
1067
1068 if (!avp || !avp->noa.has_next_tsf)
1069 return;
1070
1071 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1072
1073 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1074
1075 target_tsf = avp->noa.next_tsf;
1076 if (!avp->noa.absent)
1077 target_tsf -= ATH_P2P_PS_STOP_TIME;
1078
1079 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1080 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1081
1082 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1083}
1084
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301085static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1086{
1087 struct ath_vif *avp = (void *)vif->drv_priv;
1088 u32 tsf;
1089
1090 if (!sc->p2p_ps_timer)
1091 return;
1092
1093 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1094 return;
1095
1096 sc->p2p_ps_vif = avp;
1097 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1098 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1099 ath9k_update_p2p_ps_timer(sc, avp);
1100}
1101
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301102void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1103 struct sk_buff *skb)
1104{
1105 static const u8 noa_ie_hdr[] = {
1106 WLAN_EID_VENDOR_SPECIFIC, /* type */
1107 0, /* length */
1108 0x50, 0x6f, 0x9a, /* WFA OUI */
1109 0x09, /* P2P subtype */
1110 0x0c, /* Notice of Absence */
1111 0x00, /* LSB of little-endian len */
1112 0x00, /* MSB of little-endian len */
1113 };
1114
1115 struct ieee80211_p2p_noa_attr *noa;
1116 int noa_len, noa_desc, i = 0;
1117 u8 *hdr;
1118
1119 if (!avp->offchannel_duration && !avp->periodic_noa_duration)
1120 return;
1121
1122 noa_desc = !!avp->offchannel_duration + !!avp->periodic_noa_duration;
1123 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1124
1125 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1126 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1127 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1128 hdr[7] = noa_len;
1129
1130 noa = (void *) skb_put(skb, noa_len);
1131 memset(noa, 0, noa_len);
1132
1133 noa->index = avp->noa_index;
1134 if (avp->periodic_noa_duration) {
1135 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1136
1137 noa->desc[i].count = 255;
1138 noa->desc[i].start_time = cpu_to_le32(avp->periodic_noa_start);
1139 noa->desc[i].duration = cpu_to_le32(avp->periodic_noa_duration);
1140 noa->desc[i].interval = cpu_to_le32(interval);
1141 i++;
1142 }
1143
1144 if (avp->offchannel_duration) {
1145 noa->desc[i].count = 1;
1146 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1147 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1148 }
1149}
1150
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301151void ath9k_p2p_ps_timer(void *priv)
1152{
1153 struct ath_softc *sc = priv;
1154 struct ath_vif *avp = sc->p2p_ps_vif;
1155 struct ieee80211_vif *vif;
1156 struct ieee80211_sta *sta;
1157 struct ath_node *an;
1158 u32 tsf;
1159
1160 del_timer_sync(&sc->sched.timer);
1161 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1162 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1163
1164 if (!avp || avp->chanctx != sc->cur_chan)
1165 return;
1166
1167 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1168 if (!avp->noa.absent)
1169 tsf += ATH_P2P_PS_STOP_TIME;
1170
1171 if (!avp->noa.has_next_tsf ||
1172 avp->noa.next_tsf - tsf > BIT(31))
1173 ieee80211_update_p2p_noa(&avp->noa, tsf);
1174
1175 ath9k_update_p2p_ps_timer(sc, avp);
1176
1177 rcu_read_lock();
1178
1179 vif = avp->vif;
1180 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1181 if (!sta)
1182 goto out;
1183
1184 an = (void *) sta->drv_priv;
1185 if (an->sleeping == !!avp->noa.absent)
1186 goto out;
1187
1188 an->sleeping = avp->noa.absent;
1189 if (an->sleeping)
1190 ath_tx_aggr_sleep(sta, sc, an);
1191 else
1192 ath_tx_aggr_wakeup(sc, an);
1193
1194out:
1195 rcu_read_unlock();
1196}
1197
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301198void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1199 struct ieee80211_vif *vif)
1200{
1201 unsigned long flags;
1202
1203 spin_lock_bh(&sc->sc_pcu_lock);
1204 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1205 if (!(sc->ps_flags & PS_BEACON_SYNC))
1206 ath9k_update_p2p_ps(sc, vif);
1207 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1208 spin_unlock_bh(&sc->sc_pcu_lock);
1209}
1210
1211void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1212{
1213 if (sc->p2p_ps_vif)
1214 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1215}
1216
1217void ath9k_p2p_remove_vif(struct ath_softc *sc,
1218 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301219{
1220 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301221
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301222 spin_lock_bh(&sc->sc_pcu_lock);
1223 if (avp == sc->p2p_ps_vif) {
1224 sc->p2p_ps_vif = NULL;
1225 ath9k_update_p2p_ps_timer(sc, NULL);
1226 }
1227 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301228}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301229
1230int ath9k_init_p2p(struct ath_softc *sc)
1231{
1232 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1233 NULL, sc, AR_FIRST_NDP_TIMER);
1234 if (!sc->p2p_ps_timer)
1235 return -ENOMEM;
1236
1237 return 0;
1238}
1239
1240void ath9k_deinit_p2p(struct ath_softc *sc)
1241{
1242 if (sc->p2p_ps_timer)
1243 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1244}
1245
1246#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */