blob: 0b84917dbfe30eb4815461daa47cac418662d9b3 [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
Sujith Manoharanbc81d432014-08-22 20:39:27 +053019static const char *offchannel_state_string(enum ath_offchannel_state state)
20{
21#define case_rtn_string(val) case val: return #val
22
23 switch (state) {
24 case_rtn_string(ATH_OFFCHANNEL_IDLE);
25 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
26 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
27 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
28 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
29 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
30 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
31 default:
32 return "unknown";
33 }
34}
35
Felix Fietkaufbbcd142014-06-11 16:17:49 +053036/* Set/change channels. If the channel is really being changed, it's done
37 * by reseting the chip. To accomplish this we must first cleanup any pending
38 * DMA, then restart stuff.
39 */
40static int ath_set_channel(struct ath_softc *sc)
41{
42 struct ath_hw *ah = sc->sc_ah;
43 struct ath_common *common = ath9k_hw_common(ah);
44 struct ieee80211_hw *hw = sc->hw;
45 struct ath9k_channel *hchan;
46 struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
47 struct ieee80211_channel *chan = chandef->chan;
48 int pos = chan->hw_value;
49 int old_pos = -1;
50 int r;
51
52 if (test_bit(ATH_OP_INVALID, &common->op_flags))
53 return -EIO;
54
55 if (ah->curchan)
56 old_pos = ah->curchan - &ah->channels[0];
57
58 ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
59 chan->center_freq, chandef->width);
60
61 /* update survey stats for the old channel before switching */
62 spin_lock_bh(&common->cc_lock);
63 ath_update_survey_stats(sc);
64 spin_unlock_bh(&common->cc_lock);
65
66 ath9k_cmn_get_channel(hw, ah, chandef);
67
68 /* If the operating channel changes, change the survey in-use flags
69 * along with it.
70 * Reset the survey data for the new channel, unless we're switching
71 * back to the operating channel from an off-channel operation.
72 */
73 if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
74 if (sc->cur_survey)
75 sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
76
77 sc->cur_survey = &sc->survey[pos];
78
79 memset(sc->cur_survey, 0, sizeof(struct survey_info));
80 sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
81 } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
82 memset(&sc->survey[pos], 0, sizeof(struct survey_info));
83 }
84
85 hchan = &sc->sc_ah->channels[pos];
86 r = ath_reset_internal(sc, hchan);
87 if (r)
88 return r;
89
90 /* The most recent snapshot of channel->noisefloor for the old
91 * channel is only available after the hardware reset. Copy it to
92 * the survey stats now.
93 */
94 if (old_pos >= 0)
95 ath_update_survey_nf(sc, old_pos);
96
97 /* Enable radar pulse detection if on a DFS channel. Spectral
98 * scanning and radar detection can not be used concurrently.
99 */
100 if (hw->conf.radar_enabled) {
101 u32 rxfilter;
102
103 /* set HW specific DFS configuration */
104 ath9k_hw_set_radar_params(ah);
105 rxfilter = ath9k_hw_getrxfilter(ah);
106 rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
107 ATH9K_RX_FILTER_PHYERR;
108 ath9k_hw_setrxfilter(ah, rxfilter);
109 ath_dbg(common, DFS, "DFS enabled at freq %d\n",
110 chan->center_freq);
111 } else {
112 /* perform spectral scan if requested. */
113 if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
114 sc->spectral_mode == SPECTRAL_CHANSCAN)
115 ath9k_spectral_scan_trigger(hw);
116 }
117
118 return 0;
119}
120
Felix Fietkauc083ce92014-06-11 16:17:54 +0530121static bool
122ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
123 bool powersave)
124{
125 struct ieee80211_vif *vif = avp->vif;
126 struct ieee80211_sta *sta = NULL;
127 struct ieee80211_hdr_3addr *nullfunc;
128 struct ath_tx_control txctl;
129 struct sk_buff *skb;
130 int band = sc->cur_chan->chandef.chan->band;
131
132 switch (vif->type) {
133 case NL80211_IFTYPE_STATION:
134 if (!vif->bss_conf.assoc)
135 return false;
136
137 skb = ieee80211_nullfunc_get(sc->hw, vif);
138 if (!skb)
139 return false;
140
141 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
142 if (powersave)
143 nullfunc->frame_control |=
144 cpu_to_le16(IEEE80211_FCTL_PM);
145
146 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
147 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
148 dev_kfree_skb_any(skb);
149 return false;
150 }
151 break;
152 default:
153 return false;
154 }
155
156 memset(&txctl, 0, sizeof(txctl));
157 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
158 txctl.sta = sta;
159 txctl.force_channel = true;
160 if (ath_tx_start(sc->hw, skb, &txctl)) {
161 ieee80211_free_txskb(sc->hw, skb);
162 return false;
163 }
164
165 return true;
166}
167
168void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
169{
Felix Fietkau26f16c22014-06-11 16:18:00 +0530170 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkauc083ce92014-06-11 16:17:54 +0530171 struct ath_vif *avp;
172 bool active = false;
Felix Fietkau26f16c22014-06-11 16:18:00 +0530173 u8 n_active = 0;
Felix Fietkauc083ce92014-06-11 16:17:54 +0530174
175 if (!ctx)
176 return;
177
178 list_for_each_entry(avp, &ctx->vifs, list) {
179 struct ieee80211_vif *vif = avp->vif;
180
181 switch (vif->type) {
182 case NL80211_IFTYPE_P2P_CLIENT:
183 case NL80211_IFTYPE_STATION:
184 if (vif->bss_conf.assoc)
185 active = true;
186 break;
187 default:
188 active = true;
189 break;
190 }
191 }
192 ctx->active = active;
Felix Fietkau26f16c22014-06-11 16:18:00 +0530193
194 ath_for_each_chanctx(sc, ctx) {
195 if (!ctx->assigned || list_empty(&ctx->vifs))
196 continue;
197 n_active++;
198 }
199
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530200 if (n_active <= 1) {
Felix Fietkau26f16c22014-06-11 16:18:00 +0530201 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530202 return;
203 }
204 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
205 return;
206 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
Felix Fietkauc083ce92014-06-11 16:17:54 +0530207}
208
209static bool
210ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
211{
212 struct ath_vif *avp;
213 bool sent = false;
214
215 rcu_read_lock();
216 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
217 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
218 sent = true;
219 }
220 rcu_read_unlock();
221
222 return sent;
223}
224
Felix Fietkau748299f2014-06-11 16:18:04 +0530225static bool ath_chanctx_defer_switch(struct ath_softc *sc)
226{
227 if (sc->cur_chan == &sc->offchannel.chan)
228 return false;
229
230 switch (sc->sched.state) {
231 case ATH_CHANCTX_STATE_SWITCH:
232 return false;
233 case ATH_CHANCTX_STATE_IDLE:
234 if (!sc->cur_chan->switch_after_beacon)
235 return false;
236
237 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
238 break;
239 default:
240 break;
241 }
242
243 return true;
244}
245
Felix Fietkau6036c282014-06-11 16:18:09 +0530246static void ath_chanctx_set_next(struct ath_softc *sc, bool force)
Felix Fietkaubff11762014-06-11 16:17:52 +0530247{
Felix Fietkau748299f2014-06-11 16:18:04 +0530248 struct timespec ts;
249 bool measure_time = false;
Felix Fietkauc083ce92014-06-11 16:17:54 +0530250 bool send_ps = false;
Felix Fietkaubff11762014-06-11 16:17:52 +0530251
Felix Fietkaubff11762014-06-11 16:17:52 +0530252 spin_lock_bh(&sc->chan_lock);
253 if (!sc->next_chan) {
254 spin_unlock_bh(&sc->chan_lock);
Felix Fietkaubff11762014-06-11 16:17:52 +0530255 return;
256 }
257
Felix Fietkau6036c282014-06-11 16:18:09 +0530258 if (!force && ath_chanctx_defer_switch(sc)) {
Felix Fietkau748299f2014-06-11 16:18:04 +0530259 spin_unlock_bh(&sc->chan_lock);
Felix Fietkau748299f2014-06-11 16:18:04 +0530260 return;
261 }
262
Felix Fietkaubff11762014-06-11 16:17:52 +0530263 if (sc->cur_chan != sc->next_chan) {
264 sc->cur_chan->stopped = true;
265 spin_unlock_bh(&sc->chan_lock);
266
Felix Fietkau748299f2014-06-11 16:18:04 +0530267 if (sc->next_chan == &sc->offchannel.chan) {
268 getrawmonotonic(&ts);
269 measure_time = true;
270 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530271 __ath9k_flush(sc->hw, ~0, true);
272
Felix Fietkauc083ce92014-06-11 16:17:54 +0530273 if (ath_chanctx_send_ps_frame(sc, true))
274 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
275
276 send_ps = true;
Felix Fietkaubff11762014-06-11 16:17:52 +0530277 spin_lock_bh(&sc->chan_lock);
Felix Fietkau8d7e09d2014-06-11 16:18:01 +0530278
279 if (sc->cur_chan != &sc->offchannel.chan) {
280 getrawmonotonic(&sc->cur_chan->tsf_ts);
281 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
282 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530283 }
284 sc->cur_chan = sc->next_chan;
285 sc->cur_chan->stopped = false;
286 sc->next_chan = NULL;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530287 sc->sched.offchannel_duration = 0;
Felix Fietkau6036c282014-06-11 16:18:09 +0530288 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
289 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530290
Felix Fietkaubff11762014-06-11 16:17:52 +0530291 spin_unlock_bh(&sc->chan_lock);
292
293 if (sc->sc_ah->chip_fullsleep ||
294 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
Felix Fietkau748299f2014-06-11 16:18:04 +0530295 sizeof(sc->cur_chandef))) {
Felix Fietkaubff11762014-06-11 16:17:52 +0530296 ath_set_channel(sc);
Felix Fietkau748299f2014-06-11 16:18:04 +0530297 if (measure_time)
298 sc->sched.channel_switch_time =
299 ath9k_hw_get_tsf_offset(&ts, NULL);
300 }
Felix Fietkauc083ce92014-06-11 16:17:54 +0530301 if (send_ps)
302 ath_chanctx_send_ps_frame(sc, false);
303
Felix Fietkau78b21942014-06-11 16:17:55 +0530304 ath_offchannel_channel_change(sc);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530305 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
Felix Fietkau6036c282014-06-11 16:18:09 +0530306}
307
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530308void ath_chanctx_init(struct ath_softc *sc)
309{
310 struct ath_chanctx *ctx;
311 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
312 struct ieee80211_supported_band *sband;
313 struct ieee80211_channel *chan;
Felix Fietkau04535312014-06-11 16:17:51 +0530314 int i, j;
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530315
316 sband = &common->sbands[IEEE80211_BAND_2GHZ];
317 if (!sband->n_channels)
318 sband = &common->sbands[IEEE80211_BAND_5GHZ];
319
320 chan = &sband->channels[0];
321 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
322 ctx = &sc->chanctx[i];
323 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
324 INIT_LIST_HEAD(&ctx->vifs);
Felix Fietkaubc7e1be2014-06-11 16:17:50 +0530325 ctx->txpower = ATH_TXPOWER_MAX;
Felix Fietkau04535312014-06-11 16:17:51 +0530326 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
327 INIT_LIST_HEAD(&ctx->acq[j]);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530328 }
Felix Fietkau78b21942014-06-11 16:17:55 +0530329 ctx = &sc->offchannel.chan;
330 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
331 INIT_LIST_HEAD(&ctx->vifs);
332 ctx->txpower = ATH_TXPOWER_MAX;
333 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
334 INIT_LIST_HEAD(&ctx->acq[j]);
335 sc->offchannel.chan.offchannel = true;
336
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530337}
338
Felix Fietkau6036c282014-06-11 16:18:09 +0530339void ath9k_chanctx_force_active(struct ieee80211_hw *hw,
340 struct ieee80211_vif *vif)
341{
342 struct ath_softc *sc = hw->priv;
343 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
344 struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
345 bool changed = false;
346
347 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
348 return;
349
350 if (!avp->chanctx)
351 return;
352
353 mutex_lock(&sc->mutex);
354
355 spin_lock_bh(&sc->chan_lock);
356 if (sc->next_chan || (sc->cur_chan != avp->chanctx)) {
357 sc->next_chan = avp->chanctx;
358 changed = true;
359 }
360 sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
361 spin_unlock_bh(&sc->chan_lock);
362
363 if (changed)
364 ath_chanctx_set_next(sc, true);
365
366 mutex_unlock(&sc->mutex);
367}
368
Felix Fietkaubff11762014-06-11 16:17:52 +0530369void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
370 struct cfg80211_chan_def *chandef)
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530371{
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530372 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530373
Felix Fietkaubff11762014-06-11 16:17:52 +0530374 spin_lock_bh(&sc->chan_lock);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530375
376 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
377 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
378 sc->sched.offchannel_pending = true;
379 spin_unlock_bh(&sc->chan_lock);
380 return;
381 }
382
Felix Fietkaubff11762014-06-11 16:17:52 +0530383 sc->next_chan = ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530384 if (chandef) {
Felix Fietkaubff11762014-06-11 16:17:52 +0530385 ctx->chandef = *chandef;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530386 ath_dbg(common, CHAN_CTX,
387 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
388 }
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530389
390 if (sc->next_chan == &sc->offchannel.chan) {
391 sc->sched.offchannel_duration =
392 TU_TO_USEC(sc->offchannel.duration) +
393 sc->sched.channel_switch_time;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530394
395 if (chandef) {
396 ath_dbg(common, CHAN_CTX,
397 "Offchannel duration for chan %d MHz : %u\n",
398 chandef->center_freq1,
399 sc->sched.offchannel_duration);
400 }
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530401 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530402 spin_unlock_bh(&sc->chan_lock);
403 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
404}
405
406void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
407 struct cfg80211_chan_def *chandef)
408{
409 bool cur_chan;
410
411 spin_lock_bh(&sc->chan_lock);
412 if (chandef)
413 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
414 cur_chan = sc->cur_chan == ctx;
415 spin_unlock_bh(&sc->chan_lock);
416
417 if (!cur_chan)
418 return;
419
420 ath_set_channel(sc);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530421}
Felix Fietkau78b21942014-06-11 16:17:55 +0530422
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +0530423struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
Felix Fietkau78b21942014-06-11 16:17:55 +0530424{
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +0530425 struct ath_chanctx *ctx;
Felix Fietkau78b21942014-06-11 16:17:55 +0530426
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +0530427 ath_for_each_chanctx(sc, ctx) {
428 if (!ctx->assigned || list_empty(&ctx->vifs))
429 continue;
430 if (active && !ctx->active)
431 continue;
432
Felix Fietkau8eab25102014-06-11 16:18:07 +0530433 if (ctx->switch_after_beacon)
434 return ctx;
Felix Fietkau78b21942014-06-11 16:17:55 +0530435 }
436
437 return &sc->chanctx[0];
438}
439
440void ath_chanctx_offchan_switch(struct ath_softc *sc,
441 struct ieee80211_channel *chan)
442{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530443 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau78b21942014-06-11 16:17:55 +0530444 struct cfg80211_chan_def chandef;
445
446 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530447 ath_dbg(common, CHAN_CTX,
448 "Channel definition created: %d MHz\n", chandef.center_freq1);
Felix Fietkau78b21942014-06-11 16:17:55 +0530449
450 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
451}
Felix Fietkau748299f2014-06-11 16:18:04 +0530452
Felix Fietkau58b57372014-06-11 16:18:08 +0530453static struct ath_chanctx *
454ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
455{
456 int idx = ctx - &sc->chanctx[0];
457
458 return &sc->chanctx[!idx];
459}
460
461static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
462{
463 struct ath_chanctx *prev, *cur;
464 struct timespec ts;
465 u32 cur_tsf, prev_tsf, beacon_int;
466 s32 offset;
467
468 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
469
470 cur = sc->cur_chan;
471 prev = ath_chanctx_get_next(sc, cur);
472
473 getrawmonotonic(&ts);
474 cur_tsf = (u32) cur->tsf_val +
475 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
476
477 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
478 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
479
480 /* Adjust the TSF time of the AP chanctx to keep its beacons
481 * at half beacon interval offset relative to the STA chanctx.
482 */
483 offset = cur_tsf - prev_tsf;
484
485 /* Ignore stale data or spurious timestamps */
486 if (offset < 0 || offset > 3 * beacon_int)
487 return;
488
489 offset = beacon_int / 2 - (offset % beacon_int);
490 prev->tsf_val += offset;
491}
492
Felix Fietkau42eda112014-06-11 16:18:14 +0530493/* Configure the TSF based hardware timer for a channel switch.
494 * Also set up backup software timer, in case the gen timer fails.
495 * This could be caused by a hardware reset.
496 */
497static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
498{
499 struct ath_hw *ah = sc->sc_ah;
500
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530501#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
Felix Fietkau42eda112014-06-11 16:18:14 +0530502 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530503#endif
Felix Fietkau42eda112014-06-11 16:18:14 +0530504 tsf_time -= ath9k_hw_gettsf32(ah);
505 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
506 mod_timer(&sc->sched.timer, tsf_time);
507}
508
Felix Fietkau748299f2014-06-11 16:18:04 +0530509void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
510 enum ath_chanctx_event ev)
511{
512 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530513 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530514 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530515 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530516 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530517 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530518 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530519 bool noa_changed = false;
520
521 if (vif)
522 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530523
524 spin_lock_bh(&sc->chan_lock);
525
526 switch (ev) {
527 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530528 if (avp->offchannel_duration)
529 avp->offchannel_duration = 0;
530
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530531 if (avp->chanctx != sc->cur_chan)
532 break;
533
534 if (sc->sched.offchannel_pending) {
535 sc->sched.offchannel_pending = false;
536 sc->next_chan = &sc->offchannel.chan;
537 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
538 }
539
540 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
541 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
542 sc->next_chan = ctx;
543 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
544 }
545
546 /* if the timer missed its window, use the next interval */
547 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
548 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
549
Felix Fietkau748299f2014-06-11 16:18:04 +0530550 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
551 break;
552
553 sc->sched.beacon_pending = true;
554 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530555
Felix Fietkau74148632014-06-11 16:18:11 +0530556 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530557 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
558
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530559 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530560 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530561 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530562 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530563
Felix Fietkau74148632014-06-11 16:18:11 +0530564 /* Prevent wrap-around issues */
565 if (avp->periodic_noa_duration &&
566 tsf_time - avp->periodic_noa_start > BIT(30))
567 avp->periodic_noa_duration = 0;
568
569 if (ctx->active && !avp->periodic_noa_duration) {
570 avp->periodic_noa_start = tsf_time;
571 avp->periodic_noa_duration =
572 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
573 sc->sched.channel_switch_time;
574 noa_changed = true;
575 } else if (!ctx->active && avp->periodic_noa_duration) {
576 avp->periodic_noa_duration = 0;
577 noa_changed = true;
578 }
579
Felix Fietkauec70abe2014-06-11 16:18:12 +0530580 /* If at least two consecutive beacons were missed on the STA
581 * chanctx, stay on the STA channel for one extra beacon period,
582 * to resync the timer properly.
583 */
584 if (ctx->active && sc->sched.beacon_miss >= 2)
585 sc->sched.offchannel_duration = 3 * beacon_int / 2;
586
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530587 if (sc->sched.offchannel_duration) {
588 noa_changed = true;
589 avp->offchannel_start = tsf_time;
590 avp->offchannel_duration =
591 sc->sched.offchannel_duration;
592 }
593
594 if (noa_changed)
595 avp->noa_index++;
Felix Fietkau748299f2014-06-11 16:18:04 +0530596 break;
597 case ATH_CHANCTX_EVENT_BEACON_SENT:
598 if (!sc->sched.beacon_pending)
599 break;
600
601 sc->sched.beacon_pending = false;
602 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
603 break;
604
Felix Fietkau748299f2014-06-11 16:18:04 +0530605 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530606 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530607 break;
608 case ATH_CHANCTX_EVENT_TSF_TIMER:
609 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
610 break;
611
Felix Fietkauec70abe2014-06-11 16:18:12 +0530612 if (!sc->cur_chan->switch_after_beacon &&
613 sc->sched.beacon_pending)
614 sc->sched.beacon_miss++;
615
Felix Fietkau748299f2014-06-11 16:18:04 +0530616 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
617 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
618 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530619 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530620 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
621 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530622 break;
623
624 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530625 sc->sched.beacon_pending = false;
626 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530627
628 /* TSF time might have been updated by the incoming beacon,
629 * need update the channel switch timer to reflect the change.
630 */
631 tsf_time = sc->sched.switch_start_time;
632 tsf_time -= (u32) sc->cur_chan->tsf_val +
633 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
634 tsf_time += ath9k_hw_gettsf32(ah);
635
Felix Fietkau42eda112014-06-11 16:18:14 +0530636
637 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530638 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530639 case ATH_CHANCTX_EVENT_ASSOC:
640 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
641 avp->chanctx != sc->cur_chan)
642 break;
643
644 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
645 /* fall through */
646 case ATH_CHANCTX_EVENT_SWITCH:
647 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
648 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
649 sc->cur_chan->switch_after_beacon ||
650 sc->cur_chan == &sc->offchannel.chan)
651 break;
652
653 /* If this is a station chanctx, stay active for a half
654 * beacon period (minus channel switch time)
655 */
656 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530657 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530658
659 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530660
661 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
662 if (sc->sched.beacon_miss >= 2) {
663 sc->sched.beacon_miss = 0;
664 tsf_time *= 3;
665 }
666
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530667 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530668 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530669 sc->sched.switch_start_time = tsf_time;
670
Felix Fietkau42eda112014-06-11 16:18:14 +0530671 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530672 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530673 break;
674 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
675 if (sc->cur_chan == &sc->offchannel.chan ||
676 sc->cur_chan->switch_after_beacon)
677 break;
678
679 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
680 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
681 break;
682 case ATH_CHANCTX_EVENT_UNASSIGN:
683 if (sc->cur_chan->assigned) {
684 if (sc->next_chan && !sc->next_chan->assigned &&
685 sc->next_chan != &sc->offchannel.chan)
686 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
687 break;
688 }
689
690 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
691 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
692 if (!ctx->assigned)
693 break;
694
695 sc->next_chan = ctx;
696 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
697 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530698 }
699
700 spin_unlock_bh(&sc->chan_lock);
701}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530702
703static int ath_scan_channel_duration(struct ath_softc *sc,
704 struct ieee80211_channel *chan)
705{
706 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
707
708 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
709 return (HZ / 9); /* ~110 ms */
710
711 return (HZ / 16); /* ~60 ms */
712}
713
714static void
715ath_scan_next_channel(struct ath_softc *sc)
716{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530717 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530718 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
719 struct ieee80211_channel *chan;
720
721 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530722 ath_dbg(common, CHAN_CTX,
723 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
724 sc->offchannel.scan_idx,
725 req->n_channels);
726
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530727 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
728 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
729 NULL);
730 return;
731 }
732
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530733 ath_dbg(common, CHAN_CTX,
734 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
735 sc->offchannel.scan_idx);
736
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530737 chan = req->channels[sc->offchannel.scan_idx++];
738 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
739 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530740
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530741 ath_chanctx_offchan_switch(sc, chan);
742}
743
744void ath_offchannel_next(struct ath_softc *sc)
745{
746 struct ieee80211_vif *vif;
747
748 if (sc->offchannel.scan_req) {
749 vif = sc->offchannel.scan_vif;
750 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
751 ath_scan_next_channel(sc);
752 } else if (sc->offchannel.roc_vif) {
753 vif = sc->offchannel.roc_vif;
754 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
755 sc->offchannel.duration = sc->offchannel.roc_duration;
756 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
757 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
758 } else {
759 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
760 NULL);
761 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
762 if (sc->ps_idle)
763 ath_cancel_work(sc);
764 }
765}
766
767void ath_roc_complete(struct ath_softc *sc, bool abort)
768{
769 sc->offchannel.roc_vif = NULL;
770 sc->offchannel.roc_chan = NULL;
771 if (!abort)
772 ieee80211_remain_on_channel_expired(sc->hw);
773 ath_offchannel_next(sc);
774 ath9k_ps_restore(sc);
775}
776
777void ath_scan_complete(struct ath_softc *sc, bool abort)
778{
779 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
780
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530781 if (abort)
782 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
783 else
784 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
785
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530786 sc->offchannel.scan_req = NULL;
787 sc->offchannel.scan_vif = NULL;
788 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
789 ieee80211_scan_completed(sc->hw, abort);
790 clear_bit(ATH_OP_SCANNING, &common->op_flags);
791 ath_offchannel_next(sc);
792 ath9k_ps_restore(sc);
793}
794
795static void ath_scan_send_probe(struct ath_softc *sc,
796 struct cfg80211_ssid *ssid)
797{
798 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
799 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
800 struct ath_tx_control txctl = {};
801 struct sk_buff *skb;
802 struct ieee80211_tx_info *info;
803 int band = sc->offchannel.chan.chandef.chan->band;
804
805 skb = ieee80211_probereq_get(sc->hw, vif,
806 ssid->ssid, ssid->ssid_len, req->ie_len);
807 if (!skb)
808 return;
809
810 info = IEEE80211_SKB_CB(skb);
811 if (req->no_cck)
812 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
813
814 if (req->ie_len)
815 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
816
817 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
818
819 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
820 goto error;
821
822 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
823 txctl.force_channel = true;
824 if (ath_tx_start(sc->hw, skb, &txctl))
825 goto error;
826
827 return;
828
829error:
830 ieee80211_free_txskb(sc->hw, skb);
831}
832
833static void ath_scan_channel_start(struct ath_softc *sc)
834{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530835 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530836 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
837 int i;
838
839 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
840 req->n_ssids) {
841 for (i = 0; i < req->n_ssids; i++)
842 ath_scan_send_probe(sc, &req->ssids[i]);
843
844 }
845
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530846 ath_dbg(common, CHAN_CTX,
847 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
848
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530849 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
850 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
851}
852
853void ath_offchannel_channel_change(struct ath_softc *sc)
854{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530855 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
856
857 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
858 __func__, offchannel_state_string(sc->offchannel.state));
859
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530860 switch (sc->offchannel.state) {
861 case ATH_OFFCHANNEL_PROBE_SEND:
862 if (!sc->offchannel.scan_req)
863 return;
864
865 if (sc->cur_chan->chandef.chan !=
866 sc->offchannel.chan.chandef.chan)
867 return;
868
869 ath_scan_channel_start(sc);
870 break;
871 case ATH_OFFCHANNEL_IDLE:
872 if (!sc->offchannel.scan_req)
873 return;
874
875 ath_scan_complete(sc, false);
876 break;
877 case ATH_OFFCHANNEL_ROC_START:
878 if (sc->cur_chan != &sc->offchannel.chan)
879 break;
880
881 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
882 mod_timer(&sc->offchannel.timer, jiffies +
883 msecs_to_jiffies(sc->offchannel.duration));
884 ieee80211_ready_on_channel(sc->hw);
885 break;
886 case ATH_OFFCHANNEL_ROC_DONE:
887 ath_roc_complete(sc, false);
888 break;
889 default:
890 break;
891 }
892}
893
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530894#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
895
896static void ath_chanctx_timer(unsigned long data)
897{
898 struct ath_softc *sc = (struct ath_softc *) data;
899
900 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
901}
902
903static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530904{
905 struct ath_softc *sc = (struct ath_softc *)data;
906 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530907 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
908
909 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
910 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530911
912 switch (sc->offchannel.state) {
913 case ATH_OFFCHANNEL_PROBE_WAIT:
914 if (!sc->offchannel.scan_req)
915 return;
916
917 /* get first active channel context */
918 ctx = ath_chanctx_get_oper_chan(sc, true);
919 if (ctx->active) {
920 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
921 ath_chanctx_switch(sc, ctx, NULL);
922 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
923 break;
924 }
925 /* fall through */
926 case ATH_OFFCHANNEL_SUSPEND:
927 if (!sc->offchannel.scan_req)
928 return;
929
930 ath_scan_next_channel(sc);
931 break;
932 case ATH_OFFCHANNEL_ROC_START:
933 case ATH_OFFCHANNEL_ROC_WAIT:
934 ctx = ath_chanctx_get_oper_chan(sc, false);
935 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
936 ath_chanctx_switch(sc, ctx, NULL);
937 break;
938 default:
939 break;
940 }
941}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530942
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +0530943static void ath_chanctx_work(struct work_struct *work)
944{
945 struct ath_softc *sc = container_of(work, struct ath_softc,
946 chanctx_work);
947 mutex_lock(&sc->mutex);
948 ath_chanctx_set_next(sc, false);
949 mutex_unlock(&sc->mutex);
950}
951
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530952void ath9k_init_channel_context(struct ath_softc *sc)
953{
954 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
955
956 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
957 (unsigned long)sc);
958 setup_timer(&sc->sched.timer, ath_chanctx_timer,
959 (unsigned long)sc);
960}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530961
Sujith Manoharanea22df22014-08-23 13:29:07 +0530962void ath9k_deinit_channel_context(struct ath_softc *sc)
963{
964 cancel_work_sync(&sc->chanctx_work);
965}
966
Sujith Manoharan499afac2014-08-22 20:39:31 +0530967bool ath9k_is_chanctx_enabled(void)
968{
969 return (ath9k_use_chanctx == 1);
970}
971
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530972/*****************/
973/* P2P Powersave */
974/*****************/
975
976static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530977{
978 struct ath_hw *ah = sc->sc_ah;
979 s32 tsf, target_tsf;
980
981 if (!avp || !avp->noa.has_next_tsf)
982 return;
983
984 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
985
986 tsf = ath9k_hw_gettsf32(sc->sc_ah);
987
988 target_tsf = avp->noa.next_tsf;
989 if (!avp->noa.absent)
990 target_tsf -= ATH_P2P_PS_STOP_TIME;
991
992 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
993 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
994
995 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
996}
997
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530998static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
999{
1000 struct ath_vif *avp = (void *)vif->drv_priv;
1001 u32 tsf;
1002
1003 if (!sc->p2p_ps_timer)
1004 return;
1005
1006 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1007 return;
1008
1009 sc->p2p_ps_vif = avp;
1010 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1011 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1012 ath9k_update_p2p_ps_timer(sc, avp);
1013}
1014
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301015void ath9k_p2p_ps_timer(void *priv)
1016{
1017 struct ath_softc *sc = priv;
1018 struct ath_vif *avp = sc->p2p_ps_vif;
1019 struct ieee80211_vif *vif;
1020 struct ieee80211_sta *sta;
1021 struct ath_node *an;
1022 u32 tsf;
1023
1024 del_timer_sync(&sc->sched.timer);
1025 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1026 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1027
1028 if (!avp || avp->chanctx != sc->cur_chan)
1029 return;
1030
1031 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1032 if (!avp->noa.absent)
1033 tsf += ATH_P2P_PS_STOP_TIME;
1034
1035 if (!avp->noa.has_next_tsf ||
1036 avp->noa.next_tsf - tsf > BIT(31))
1037 ieee80211_update_p2p_noa(&avp->noa, tsf);
1038
1039 ath9k_update_p2p_ps_timer(sc, avp);
1040
1041 rcu_read_lock();
1042
1043 vif = avp->vif;
1044 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1045 if (!sta)
1046 goto out;
1047
1048 an = (void *) sta->drv_priv;
1049 if (an->sleeping == !!avp->noa.absent)
1050 goto out;
1051
1052 an->sleeping = avp->noa.absent;
1053 if (an->sleeping)
1054 ath_tx_aggr_sleep(sta, sc, an);
1055 else
1056 ath_tx_aggr_wakeup(sc, an);
1057
1058out:
1059 rcu_read_unlock();
1060}
1061
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301062void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1063 struct ieee80211_vif *vif)
1064{
1065 unsigned long flags;
1066
1067 spin_lock_bh(&sc->sc_pcu_lock);
1068 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1069 if (!(sc->ps_flags & PS_BEACON_SYNC))
1070 ath9k_update_p2p_ps(sc, vif);
1071 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1072 spin_unlock_bh(&sc->sc_pcu_lock);
1073}
1074
1075void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1076{
1077 if (sc->p2p_ps_vif)
1078 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1079}
1080
1081void ath9k_p2p_remove_vif(struct ath_softc *sc,
1082 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301083{
1084 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301085
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301086 spin_lock_bh(&sc->sc_pcu_lock);
1087 if (avp == sc->p2p_ps_vif) {
1088 sc->p2p_ps_vif = NULL;
1089 ath9k_update_p2p_ps_timer(sc, NULL);
1090 }
1091 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301092}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301093
1094int ath9k_init_p2p(struct ath_softc *sc)
1095{
1096 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1097 NULL, sc, AR_FIRST_NDP_TIMER);
1098 if (!sc->p2p_ps_timer)
1099 return -ENOMEM;
1100
1101 return 0;
1102}
1103
1104void ath9k_deinit_p2p(struct ath_softc *sc)
1105{
1106 if (sc->p2p_ps_timer)
1107 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1108}
1109
1110#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */