blob: 9be92d81ef6dd32268b9d822f5f648d36db8f496 [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{
130 bool cur_chan;
131
132 spin_lock_bh(&sc->chan_lock);
133 if (chandef)
134 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
135 cur_chan = sc->cur_chan == ctx;
136 spin_unlock_bh(&sc->chan_lock);
137
138 if (!cur_chan)
139 return;
140
141 ath_set_channel(sc);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530142}
Felix Fietkau78b21942014-06-11 16:17:55 +0530143
Sujith Manoharan27babf92014-08-23 13:29:16 +0530144#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
145
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +0530146/**********************************************************/
147/* Functions to handle the channel context state machine. */
148/**********************************************************/
149
Sujith Manoharan27babf92014-08-23 13:29:16 +0530150static const char *offchannel_state_string(enum ath_offchannel_state state)
151{
Sujith Manoharan27babf92014-08-23 13:29:16 +0530152 switch (state) {
153 case_rtn_string(ATH_OFFCHANNEL_IDLE);
154 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
155 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
156 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
157 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
158 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
159 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
160 default:
161 return "unknown";
162 }
163}
164
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530165static const char *chanctx_event_string(enum ath_chanctx_event ev)
166{
167 switch (ev) {
168 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
169 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
170 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
171 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
172 case_rtn_string(ATH_CHANCTX_EVENT_ASSOC);
173 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
174 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
175 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
176 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
177 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
178 default:
179 return "unknown";
180 }
181}
182
183static const char *chanctx_state_string(enum ath_chanctx_state state)
184{
185 switch (state) {
186 case_rtn_string(ATH_CHANCTX_STATE_IDLE);
187 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
188 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
189 case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
190 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
191 default:
192 return "unknown";
193 }
194}
195
Sujith Manoharana09798f2014-08-23 13:29:21 +0530196void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
197{
198 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
199 struct ath_vif *avp;
200 bool active = false;
201 u8 n_active = 0;
202
203 if (!ctx)
204 return;
205
206 list_for_each_entry(avp, &ctx->vifs, list) {
207 struct ieee80211_vif *vif = avp->vif;
208
209 switch (vif->type) {
210 case NL80211_IFTYPE_P2P_CLIENT:
211 case NL80211_IFTYPE_STATION:
212 if (vif->bss_conf.assoc)
213 active = true;
214 break;
215 default:
216 active = true;
217 break;
218 }
219 }
220 ctx->active = active;
221
222 ath_for_each_chanctx(sc, ctx) {
223 if (!ctx->assigned || list_empty(&ctx->vifs))
224 continue;
225 n_active++;
226 }
227
228 if (n_active <= 1) {
229 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
230 return;
231 }
232 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
233 return;
234
235 if (ath9k_is_chanctx_enabled()) {
236 ath_chanctx_event(sc, NULL,
237 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
238 }
239}
240
Felix Fietkau58b57372014-06-11 16:18:08 +0530241static struct ath_chanctx *
242ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
243{
244 int idx = ctx - &sc->chanctx[0];
245
246 return &sc->chanctx[!idx];
247}
248
249static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
250{
251 struct ath_chanctx *prev, *cur;
252 struct timespec ts;
253 u32 cur_tsf, prev_tsf, beacon_int;
254 s32 offset;
255
256 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
257
258 cur = sc->cur_chan;
259 prev = ath_chanctx_get_next(sc, cur);
260
261 getrawmonotonic(&ts);
262 cur_tsf = (u32) cur->tsf_val +
263 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
264
265 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
266 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
267
268 /* Adjust the TSF time of the AP chanctx to keep its beacons
269 * at half beacon interval offset relative to the STA chanctx.
270 */
271 offset = cur_tsf - prev_tsf;
272
273 /* Ignore stale data or spurious timestamps */
274 if (offset < 0 || offset > 3 * beacon_int)
275 return;
276
277 offset = beacon_int / 2 - (offset % beacon_int);
278 prev->tsf_val += offset;
279}
280
Felix Fietkau42eda112014-06-11 16:18:14 +0530281/* Configure the TSF based hardware timer for a channel switch.
282 * Also set up backup software timer, in case the gen timer fails.
283 * This could be caused by a hardware reset.
284 */
285static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
286{
287 struct ath_hw *ah = sc->sc_ah;
288
289 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
290 tsf_time -= ath9k_hw_gettsf32(ah);
291 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
292 mod_timer(&sc->sched.timer, tsf_time);
293}
294
Felix Fietkau748299f2014-06-11 16:18:04 +0530295void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
296 enum ath_chanctx_event ev)
297{
298 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530299 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530300 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530301 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530302 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530303 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530304 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530305 bool noa_changed = false;
306
Sujith Manoharan5a8cbec2014-08-24 21:16:11 +0530307 ath_dbg(common, CHAN_CTX, "event: %s, state: %s\n",
308 chanctx_event_string(ev),
309 chanctx_state_string(sc->sched.state));
310
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530311 if (vif)
312 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530313
314 spin_lock_bh(&sc->chan_lock);
315
316 switch (ev) {
317 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530318 if (avp->offchannel_duration)
319 avp->offchannel_duration = 0;
320
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530321 if (avp->chanctx != sc->cur_chan)
322 break;
323
324 if (sc->sched.offchannel_pending) {
325 sc->sched.offchannel_pending = false;
326 sc->next_chan = &sc->offchannel.chan;
327 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
328 }
329
330 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
331 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
332 sc->next_chan = ctx;
333 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
334 }
335
336 /* if the timer missed its window, use the next interval */
337 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
338 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
339
Felix Fietkau748299f2014-06-11 16:18:04 +0530340 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
341 break;
342
343 sc->sched.beacon_pending = true;
344 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530345
Felix Fietkau74148632014-06-11 16:18:11 +0530346 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530347 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
348
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530349 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530350 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530351 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530352 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530353
Felix Fietkau74148632014-06-11 16:18:11 +0530354 /* Prevent wrap-around issues */
355 if (avp->periodic_noa_duration &&
356 tsf_time - avp->periodic_noa_start > BIT(30))
357 avp->periodic_noa_duration = 0;
358
359 if (ctx->active && !avp->periodic_noa_duration) {
360 avp->periodic_noa_start = tsf_time;
361 avp->periodic_noa_duration =
362 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
363 sc->sched.channel_switch_time;
364 noa_changed = true;
365 } else if (!ctx->active && avp->periodic_noa_duration) {
366 avp->periodic_noa_duration = 0;
367 noa_changed = true;
368 }
369
Felix Fietkauec70abe2014-06-11 16:18:12 +0530370 /* If at least two consecutive beacons were missed on the STA
371 * chanctx, stay on the STA channel for one extra beacon period,
372 * to resync the timer properly.
373 */
374 if (ctx->active && sc->sched.beacon_miss >= 2)
375 sc->sched.offchannel_duration = 3 * beacon_int / 2;
376
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530377 if (sc->sched.offchannel_duration) {
378 noa_changed = true;
379 avp->offchannel_start = tsf_time;
380 avp->offchannel_duration =
381 sc->sched.offchannel_duration;
382 }
383
384 if (noa_changed)
385 avp->noa_index++;
Felix Fietkau748299f2014-06-11 16:18:04 +0530386 break;
387 case ATH_CHANCTX_EVENT_BEACON_SENT:
388 if (!sc->sched.beacon_pending)
389 break;
390
391 sc->sched.beacon_pending = false;
392 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
393 break;
394
Felix Fietkau748299f2014-06-11 16:18:04 +0530395 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530396 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530397 break;
398 case ATH_CHANCTX_EVENT_TSF_TIMER:
399 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
400 break;
401
Felix Fietkauec70abe2014-06-11 16:18:12 +0530402 if (!sc->cur_chan->switch_after_beacon &&
403 sc->sched.beacon_pending)
404 sc->sched.beacon_miss++;
405
Felix Fietkau748299f2014-06-11 16:18:04 +0530406 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
407 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
408 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530409 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530410 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
411 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530412 break;
413
414 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530415 sc->sched.beacon_pending = false;
416 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530417
418 /* TSF time might have been updated by the incoming beacon,
419 * need update the channel switch timer to reflect the change.
420 */
421 tsf_time = sc->sched.switch_start_time;
422 tsf_time -= (u32) sc->cur_chan->tsf_val +
423 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
424 tsf_time += ath9k_hw_gettsf32(ah);
425
Felix Fietkau42eda112014-06-11 16:18:14 +0530426
427 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530428 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530429 case ATH_CHANCTX_EVENT_ASSOC:
430 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
431 avp->chanctx != sc->cur_chan)
432 break;
433
434 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
435 /* fall through */
436 case ATH_CHANCTX_EVENT_SWITCH:
437 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
438 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
439 sc->cur_chan->switch_after_beacon ||
440 sc->cur_chan == &sc->offchannel.chan)
441 break;
442
443 /* If this is a station chanctx, stay active for a half
444 * beacon period (minus channel switch time)
445 */
446 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530447 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530448
449 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530450
451 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
452 if (sc->sched.beacon_miss >= 2) {
453 sc->sched.beacon_miss = 0;
454 tsf_time *= 3;
455 }
456
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530457 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530458 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530459 sc->sched.switch_start_time = tsf_time;
460
Felix Fietkau42eda112014-06-11 16:18:14 +0530461 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530462 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530463 break;
464 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
465 if (sc->cur_chan == &sc->offchannel.chan ||
466 sc->cur_chan->switch_after_beacon)
467 break;
468
469 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
470 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
471 break;
472 case ATH_CHANCTX_EVENT_UNASSIGN:
473 if (sc->cur_chan->assigned) {
474 if (sc->next_chan && !sc->next_chan->assigned &&
475 sc->next_chan != &sc->offchannel.chan)
476 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
477 break;
478 }
479
480 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
481 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
482 if (!ctx->assigned)
483 break;
484
485 sc->next_chan = ctx;
486 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
487 break;
Sujith Manoharan02da18b2014-08-24 21:16:10 +0530488 case ATH_CHANCTX_EVENT_ASSIGN:
489 break;
490 case ATH_CHANCTX_EVENT_CHANGE:
491 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530492 }
493
494 spin_unlock_bh(&sc->chan_lock);
495}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530496
Sujith Manoharan70b06da2014-08-23 13:29:18 +0530497void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
498 enum ath_chanctx_event ev)
499{
500 if (sc->sched.beacon_pending)
501 ath_chanctx_event(sc, NULL, ev);
502}
503
504void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
505 enum ath_chanctx_event ev)
506{
507 sc->sched.next_tbtt = ts;
508 ath_chanctx_event(sc, NULL, ev);
509}
510
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530511static int ath_scan_channel_duration(struct ath_softc *sc,
512 struct ieee80211_channel *chan)
513{
514 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
515
516 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
517 return (HZ / 9); /* ~110 ms */
518
519 return (HZ / 16); /* ~60 ms */
520}
521
Sujith Manoharan922c9432014-08-23 13:29:15 +0530522static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
523 struct cfg80211_chan_def *chandef)
524{
525 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
526
527 spin_lock_bh(&sc->chan_lock);
528
529 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
530 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
531 sc->sched.offchannel_pending = true;
532 spin_unlock_bh(&sc->chan_lock);
533 return;
534 }
535
536 sc->next_chan = ctx;
537 if (chandef) {
538 ctx->chandef = *chandef;
539 ath_dbg(common, CHAN_CTX,
540 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
541 }
542
543 if (sc->next_chan == &sc->offchannel.chan) {
544 sc->sched.offchannel_duration =
545 TU_TO_USEC(sc->offchannel.duration) +
546 sc->sched.channel_switch_time;
547
548 if (chandef) {
549 ath_dbg(common, CHAN_CTX,
550 "Offchannel duration for chan %d MHz : %u\n",
551 chandef->center_freq1,
552 sc->sched.offchannel_duration);
553 }
554 }
555 spin_unlock_bh(&sc->chan_lock);
556 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
557}
558
Sujith Manoharan344ae6a2014-08-23 13:29:13 +0530559static void ath_chanctx_offchan_switch(struct ath_softc *sc,
560 struct ieee80211_channel *chan)
561{
562 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
563 struct cfg80211_chan_def chandef;
564
565 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
566 ath_dbg(common, CHAN_CTX,
567 "Channel definition created: %d MHz\n", chandef.center_freq1);
568
569 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
570}
571
Sujith Manoharan98f411b2014-08-23 13:29:14 +0530572static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
573 bool active)
574{
575 struct ath_chanctx *ctx;
576
577 ath_for_each_chanctx(sc, ctx) {
578 if (!ctx->assigned || list_empty(&ctx->vifs))
579 continue;
580 if (active && !ctx->active)
581 continue;
582
583 if (ctx->switch_after_beacon)
584 return ctx;
585 }
586
587 return &sc->chanctx[0];
588}
589
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530590static void
591ath_scan_next_channel(struct ath_softc *sc)
592{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530593 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530594 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
595 struct ieee80211_channel *chan;
596
597 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530598 ath_dbg(common, CHAN_CTX,
599 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
600 sc->offchannel.scan_idx,
601 req->n_channels);
602
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530603 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
604 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
605 NULL);
606 return;
607 }
608
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530609 ath_dbg(common, CHAN_CTX,
610 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
611 sc->offchannel.scan_idx);
612
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530613 chan = req->channels[sc->offchannel.scan_idx++];
614 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
615 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530616
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530617 ath_chanctx_offchan_switch(sc, chan);
618}
619
620void ath_offchannel_next(struct ath_softc *sc)
621{
622 struct ieee80211_vif *vif;
623
624 if (sc->offchannel.scan_req) {
625 vif = sc->offchannel.scan_vif;
626 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
627 ath_scan_next_channel(sc);
628 } else if (sc->offchannel.roc_vif) {
629 vif = sc->offchannel.roc_vif;
630 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
631 sc->offchannel.duration = sc->offchannel.roc_duration;
632 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
633 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
634 } else {
635 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
636 NULL);
637 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
638 if (sc->ps_idle)
639 ath_cancel_work(sc);
640 }
641}
642
643void ath_roc_complete(struct ath_softc *sc, bool abort)
644{
645 sc->offchannel.roc_vif = NULL;
646 sc->offchannel.roc_chan = NULL;
647 if (!abort)
648 ieee80211_remain_on_channel_expired(sc->hw);
649 ath_offchannel_next(sc);
650 ath9k_ps_restore(sc);
651}
652
653void ath_scan_complete(struct ath_softc *sc, bool abort)
654{
655 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
656
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530657 if (abort)
658 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
659 else
660 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
661
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530662 sc->offchannel.scan_req = NULL;
663 sc->offchannel.scan_vif = NULL;
664 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
665 ieee80211_scan_completed(sc->hw, abort);
666 clear_bit(ATH_OP_SCANNING, &common->op_flags);
667 ath_offchannel_next(sc);
668 ath9k_ps_restore(sc);
669}
670
671static void ath_scan_send_probe(struct ath_softc *sc,
672 struct cfg80211_ssid *ssid)
673{
674 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
675 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
676 struct ath_tx_control txctl = {};
677 struct sk_buff *skb;
678 struct ieee80211_tx_info *info;
679 int band = sc->offchannel.chan.chandef.chan->band;
680
681 skb = ieee80211_probereq_get(sc->hw, vif,
682 ssid->ssid, ssid->ssid_len, req->ie_len);
683 if (!skb)
684 return;
685
686 info = IEEE80211_SKB_CB(skb);
687 if (req->no_cck)
688 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
689
690 if (req->ie_len)
691 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
692
693 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
694
695 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
696 goto error;
697
698 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
699 txctl.force_channel = true;
700 if (ath_tx_start(sc->hw, skb, &txctl))
701 goto error;
702
703 return;
704
705error:
706 ieee80211_free_txskb(sc->hw, skb);
707}
708
709static void ath_scan_channel_start(struct ath_softc *sc)
710{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530711 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530712 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
713 int i;
714
715 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
716 req->n_ssids) {
717 for (i = 0; i < req->n_ssids; i++)
718 ath_scan_send_probe(sc, &req->ssids[i]);
719
720 }
721
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530722 ath_dbg(common, CHAN_CTX,
723 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
724
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530725 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
726 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
727}
728
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530729static void ath_chanctx_timer(unsigned long data)
730{
731 struct ath_softc *sc = (struct ath_softc *) data;
732
733 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
734}
735
736static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530737{
738 struct ath_softc *sc = (struct ath_softc *)data;
739 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530740 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
741
742 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
743 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530744
745 switch (sc->offchannel.state) {
746 case ATH_OFFCHANNEL_PROBE_WAIT:
747 if (!sc->offchannel.scan_req)
748 return;
749
750 /* get first active channel context */
751 ctx = ath_chanctx_get_oper_chan(sc, true);
752 if (ctx->active) {
753 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
754 ath_chanctx_switch(sc, ctx, NULL);
755 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
756 break;
757 }
758 /* fall through */
759 case ATH_OFFCHANNEL_SUSPEND:
760 if (!sc->offchannel.scan_req)
761 return;
762
763 ath_scan_next_channel(sc);
764 break;
765 case ATH_OFFCHANNEL_ROC_START:
766 case ATH_OFFCHANNEL_ROC_WAIT:
767 ctx = ath_chanctx_get_oper_chan(sc, false);
768 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
769 ath_chanctx_switch(sc, ctx, NULL);
770 break;
771 default:
772 break;
773 }
774}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530775
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530776static bool
777ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
778 bool powersave)
779{
780 struct ieee80211_vif *vif = avp->vif;
781 struct ieee80211_sta *sta = NULL;
782 struct ieee80211_hdr_3addr *nullfunc;
783 struct ath_tx_control txctl;
784 struct sk_buff *skb;
785 int band = sc->cur_chan->chandef.chan->band;
786
787 switch (vif->type) {
788 case NL80211_IFTYPE_STATION:
789 if (!vif->bss_conf.assoc)
790 return false;
791
792 skb = ieee80211_nullfunc_get(sc->hw, vif);
793 if (!skb)
794 return false;
795
796 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
797 if (powersave)
798 nullfunc->frame_control |=
799 cpu_to_le16(IEEE80211_FCTL_PM);
800
801 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
802 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
803 dev_kfree_skb_any(skb);
804 return false;
805 }
806 break;
807 default:
808 return false;
809 }
810
811 memset(&txctl, 0, sizeof(txctl));
812 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
813 txctl.sta = sta;
814 txctl.force_channel = true;
815 if (ath_tx_start(sc->hw, skb, &txctl)) {
816 ieee80211_free_txskb(sc->hw, skb);
817 return false;
818 }
819
820 return true;
821}
822
823static bool
824ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
825{
826 struct ath_vif *avp;
827 bool sent = false;
828
829 rcu_read_lock();
830 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
831 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
832 sent = true;
833 }
834 rcu_read_unlock();
835
836 return sent;
837}
838
839static bool ath_chanctx_defer_switch(struct ath_softc *sc)
840{
841 if (sc->cur_chan == &sc->offchannel.chan)
842 return false;
843
844 switch (sc->sched.state) {
845 case ATH_CHANCTX_STATE_SWITCH:
846 return false;
847 case ATH_CHANCTX_STATE_IDLE:
848 if (!sc->cur_chan->switch_after_beacon)
849 return false;
850
851 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
852 break;
853 default:
854 break;
855 }
856
857 return true;
858}
859
Sujith Manoharan55254ee2014-08-23 13:29:11 +0530860static void ath_offchannel_channel_change(struct ath_softc *sc)
861{
862 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
863
864 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
865 __func__, offchannel_state_string(sc->offchannel.state));
866
867 switch (sc->offchannel.state) {
868 case ATH_OFFCHANNEL_PROBE_SEND:
869 if (!sc->offchannel.scan_req)
870 return;
871
872 if (sc->cur_chan->chandef.chan !=
873 sc->offchannel.chan.chandef.chan)
874 return;
875
876 ath_scan_channel_start(sc);
877 break;
878 case ATH_OFFCHANNEL_IDLE:
879 if (!sc->offchannel.scan_req)
880 return;
881
882 ath_scan_complete(sc, false);
883 break;
884 case ATH_OFFCHANNEL_ROC_START:
885 if (sc->cur_chan != &sc->offchannel.chan)
886 break;
887
888 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
889 mod_timer(&sc->offchannel.timer, jiffies +
890 msecs_to_jiffies(sc->offchannel.duration));
891 ieee80211_ready_on_channel(sc->hw);
892 break;
893 case ATH_OFFCHANNEL_ROC_DONE:
894 ath_roc_complete(sc, false);
895 break;
896 default:
897 break;
898 }
899}
900
Sujith Manoharan6d7cbd72014-08-23 13:29:10 +0530901void ath_chanctx_set_next(struct ath_softc *sc, bool force)
902{
903 struct timespec ts;
904 bool measure_time = false;
905 bool send_ps = false;
906
907 spin_lock_bh(&sc->chan_lock);
908 if (!sc->next_chan) {
909 spin_unlock_bh(&sc->chan_lock);
910 return;
911 }
912
913 if (!force && ath_chanctx_defer_switch(sc)) {
914 spin_unlock_bh(&sc->chan_lock);
915 return;
916 }
917
918 if (sc->cur_chan != sc->next_chan) {
919 sc->cur_chan->stopped = true;
920 spin_unlock_bh(&sc->chan_lock);
921
922 if (sc->next_chan == &sc->offchannel.chan) {
923 getrawmonotonic(&ts);
924 measure_time = true;
925 }
926 __ath9k_flush(sc->hw, ~0, true);
927
928 if (ath_chanctx_send_ps_frame(sc, true))
929 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
930
931 send_ps = true;
932 spin_lock_bh(&sc->chan_lock);
933
934 if (sc->cur_chan != &sc->offchannel.chan) {
935 getrawmonotonic(&sc->cur_chan->tsf_ts);
936 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
937 }
938 }
939 sc->cur_chan = sc->next_chan;
940 sc->cur_chan->stopped = false;
941 sc->next_chan = NULL;
942 sc->sched.offchannel_duration = 0;
943 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
944 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
945
946 spin_unlock_bh(&sc->chan_lock);
947
948 if (sc->sc_ah->chip_fullsleep ||
949 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
950 sizeof(sc->cur_chandef))) {
951 ath_set_channel(sc);
952 if (measure_time)
953 sc->sched.channel_switch_time =
954 ath9k_hw_get_tsf_offset(&ts, NULL);
955 }
956 if (send_ps)
957 ath_chanctx_send_ps_frame(sc, false);
958
959 ath_offchannel_channel_change(sc);
960 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
961}
962
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +0530963static void ath_chanctx_work(struct work_struct *work)
964{
965 struct ath_softc *sc = container_of(work, struct ath_softc,
966 chanctx_work);
967 mutex_lock(&sc->mutex);
968 ath_chanctx_set_next(sc, false);
969 mutex_unlock(&sc->mutex);
970}
971
Sujith Manoharane90e3022014-08-23 13:29:20 +0530972void ath9k_offchannel_init(struct ath_softc *sc)
973{
974 struct ath_chanctx *ctx;
975 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
976 struct ieee80211_supported_band *sband;
977 struct ieee80211_channel *chan;
978 int i;
979
980 sband = &common->sbands[IEEE80211_BAND_2GHZ];
981 if (!sband->n_channels)
982 sband = &common->sbands[IEEE80211_BAND_5GHZ];
983
984 chan = &sband->channels[0];
985
986 ctx = &sc->offchannel.chan;
987 INIT_LIST_HEAD(&ctx->vifs);
988 ctx->txpower = ATH_TXPOWER_MAX;
989 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
990
991 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
992 INIT_LIST_HEAD(&ctx->acq[i]);
993
994 sc->offchannel.chan.offchannel = true;
995}
996
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530997void ath9k_init_channel_context(struct ath_softc *sc)
998{
999 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1000
1001 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1002 (unsigned long)sc);
1003 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1004 (unsigned long)sc);
1005}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301006
Sujith Manoharanea22df22014-08-23 13:29:07 +05301007void ath9k_deinit_channel_context(struct ath_softc *sc)
1008{
1009 cancel_work_sync(&sc->chanctx_work);
1010}
1011
Sujith Manoharan499afac2014-08-22 20:39:31 +05301012bool ath9k_is_chanctx_enabled(void)
1013{
1014 return (ath9k_use_chanctx == 1);
1015}
1016
Sujith Manoharan0e08b5f2014-08-23 13:29:19 +05301017/********************/
1018/* Queue management */
1019/********************/
1020
1021void ath9k_chanctx_wake_queues(struct ath_softc *sc)
1022{
1023 struct ath_hw *ah = sc->sc_ah;
1024 int i;
1025
1026 if (sc->cur_chan == &sc->offchannel.chan) {
1027 ieee80211_wake_queue(sc->hw,
1028 sc->hw->offchannel_tx_hw_queue);
1029 } else {
1030 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1031 ieee80211_wake_queue(sc->hw,
1032 sc->cur_chan->hw_queue_base + i);
1033 }
1034
1035 if (ah->opmode == NL80211_IFTYPE_AP)
1036 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1037}
1038
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301039/*****************/
1040/* P2P Powersave */
1041/*****************/
1042
1043static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301044{
1045 struct ath_hw *ah = sc->sc_ah;
1046 s32 tsf, target_tsf;
1047
1048 if (!avp || !avp->noa.has_next_tsf)
1049 return;
1050
1051 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1052
1053 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1054
1055 target_tsf = avp->noa.next_tsf;
1056 if (!avp->noa.absent)
1057 target_tsf -= ATH_P2P_PS_STOP_TIME;
1058
1059 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1060 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1061
1062 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1063}
1064
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301065static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1066{
1067 struct ath_vif *avp = (void *)vif->drv_priv;
1068 u32 tsf;
1069
1070 if (!sc->p2p_ps_timer)
1071 return;
1072
1073 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1074 return;
1075
1076 sc->p2p_ps_vif = avp;
1077 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1078 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1079 ath9k_update_p2p_ps_timer(sc, avp);
1080}
1081
Sujith Manoharan11e39a42014-08-23 19:12:15 +05301082void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1083 struct sk_buff *skb)
1084{
1085 static const u8 noa_ie_hdr[] = {
1086 WLAN_EID_VENDOR_SPECIFIC, /* type */
1087 0, /* length */
1088 0x50, 0x6f, 0x9a, /* WFA OUI */
1089 0x09, /* P2P subtype */
1090 0x0c, /* Notice of Absence */
1091 0x00, /* LSB of little-endian len */
1092 0x00, /* MSB of little-endian len */
1093 };
1094
1095 struct ieee80211_p2p_noa_attr *noa;
1096 int noa_len, noa_desc, i = 0;
1097 u8 *hdr;
1098
1099 if (!avp->offchannel_duration && !avp->periodic_noa_duration)
1100 return;
1101
1102 noa_desc = !!avp->offchannel_duration + !!avp->periodic_noa_duration;
1103 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1104
1105 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1106 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1107 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1108 hdr[7] = noa_len;
1109
1110 noa = (void *) skb_put(skb, noa_len);
1111 memset(noa, 0, noa_len);
1112
1113 noa->index = avp->noa_index;
1114 if (avp->periodic_noa_duration) {
1115 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1116
1117 noa->desc[i].count = 255;
1118 noa->desc[i].start_time = cpu_to_le32(avp->periodic_noa_start);
1119 noa->desc[i].duration = cpu_to_le32(avp->periodic_noa_duration);
1120 noa->desc[i].interval = cpu_to_le32(interval);
1121 i++;
1122 }
1123
1124 if (avp->offchannel_duration) {
1125 noa->desc[i].count = 1;
1126 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1127 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1128 }
1129}
1130
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301131void ath9k_p2p_ps_timer(void *priv)
1132{
1133 struct ath_softc *sc = priv;
1134 struct ath_vif *avp = sc->p2p_ps_vif;
1135 struct ieee80211_vif *vif;
1136 struct ieee80211_sta *sta;
1137 struct ath_node *an;
1138 u32 tsf;
1139
1140 del_timer_sync(&sc->sched.timer);
1141 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1142 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1143
1144 if (!avp || avp->chanctx != sc->cur_chan)
1145 return;
1146
1147 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1148 if (!avp->noa.absent)
1149 tsf += ATH_P2P_PS_STOP_TIME;
1150
1151 if (!avp->noa.has_next_tsf ||
1152 avp->noa.next_tsf - tsf > BIT(31))
1153 ieee80211_update_p2p_noa(&avp->noa, tsf);
1154
1155 ath9k_update_p2p_ps_timer(sc, avp);
1156
1157 rcu_read_lock();
1158
1159 vif = avp->vif;
1160 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1161 if (!sta)
1162 goto out;
1163
1164 an = (void *) sta->drv_priv;
1165 if (an->sleeping == !!avp->noa.absent)
1166 goto out;
1167
1168 an->sleeping = avp->noa.absent;
1169 if (an->sleeping)
1170 ath_tx_aggr_sleep(sta, sc, an);
1171 else
1172 ath_tx_aggr_wakeup(sc, an);
1173
1174out:
1175 rcu_read_unlock();
1176}
1177
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301178void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1179 struct ieee80211_vif *vif)
1180{
1181 unsigned long flags;
1182
1183 spin_lock_bh(&sc->sc_pcu_lock);
1184 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1185 if (!(sc->ps_flags & PS_BEACON_SYNC))
1186 ath9k_update_p2p_ps(sc, vif);
1187 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1188 spin_unlock_bh(&sc->sc_pcu_lock);
1189}
1190
1191void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1192{
1193 if (sc->p2p_ps_vif)
1194 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1195}
1196
1197void ath9k_p2p_remove_vif(struct ath_softc *sc,
1198 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301199{
1200 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301201
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301202 spin_lock_bh(&sc->sc_pcu_lock);
1203 if (avp == sc->p2p_ps_vif) {
1204 sc->p2p_ps_vif = NULL;
1205 ath9k_update_p2p_ps_timer(sc, NULL);
1206 }
1207 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301208}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301209
1210int ath9k_init_p2p(struct ath_softc *sc)
1211{
1212 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1213 NULL, sc, AR_FIRST_NDP_TIMER);
1214 if (!sc->p2p_ps_timer)
1215 return -ENOMEM;
1216
1217 return 0;
1218}
1219
1220void ath9k_deinit_p2p(struct ath_softc *sc)
1221{
1222 if (sc->p2p_ps_timer)
1223 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1224}
1225
1226#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */