blob: 4525fc0f657b02eac2bbdc58e0b340f723b892a6 [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
Sujith Manoharane20a8542014-08-23 13:29:09 +0530246void 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 Fietkaubff11762014-06-11 16:17:52 +0530339void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
340 struct cfg80211_chan_def *chandef)
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530341{
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530342 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530343
Felix Fietkaubff11762014-06-11 16:17:52 +0530344 spin_lock_bh(&sc->chan_lock);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530345
346 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
347 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
348 sc->sched.offchannel_pending = true;
349 spin_unlock_bh(&sc->chan_lock);
350 return;
351 }
352
Felix Fietkaubff11762014-06-11 16:17:52 +0530353 sc->next_chan = ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530354 if (chandef) {
Felix Fietkaubff11762014-06-11 16:17:52 +0530355 ctx->chandef = *chandef;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530356 ath_dbg(common, CHAN_CTX,
357 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
358 }
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530359
360 if (sc->next_chan == &sc->offchannel.chan) {
361 sc->sched.offchannel_duration =
362 TU_TO_USEC(sc->offchannel.duration) +
363 sc->sched.channel_switch_time;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530364
365 if (chandef) {
366 ath_dbg(common, CHAN_CTX,
367 "Offchannel duration for chan %d MHz : %u\n",
368 chandef->center_freq1,
369 sc->sched.offchannel_duration);
370 }
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530371 }
Felix Fietkaubff11762014-06-11 16:17:52 +0530372 spin_unlock_bh(&sc->chan_lock);
373 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
374}
375
376void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
377 struct cfg80211_chan_def *chandef)
378{
379 bool cur_chan;
380
381 spin_lock_bh(&sc->chan_lock);
382 if (chandef)
383 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
384 cur_chan = sc->cur_chan == ctx;
385 spin_unlock_bh(&sc->chan_lock);
386
387 if (!cur_chan)
388 return;
389
390 ath_set_channel(sc);
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530391}
Felix Fietkau78b21942014-06-11 16:17:55 +0530392
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +0530393struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
Felix Fietkau78b21942014-06-11 16:17:55 +0530394{
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +0530395 struct ath_chanctx *ctx;
Felix Fietkau78b21942014-06-11 16:17:55 +0530396
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +0530397 ath_for_each_chanctx(sc, ctx) {
398 if (!ctx->assigned || list_empty(&ctx->vifs))
399 continue;
400 if (active && !ctx->active)
401 continue;
402
Felix Fietkau8eab25102014-06-11 16:18:07 +0530403 if (ctx->switch_after_beacon)
404 return ctx;
Felix Fietkau78b21942014-06-11 16:17:55 +0530405 }
406
407 return &sc->chanctx[0];
408}
409
410void ath_chanctx_offchan_switch(struct ath_softc *sc,
411 struct ieee80211_channel *chan)
412{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530413 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau78b21942014-06-11 16:17:55 +0530414 struct cfg80211_chan_def chandef;
415
416 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530417 ath_dbg(common, CHAN_CTX,
418 "Channel definition created: %d MHz\n", chandef.center_freq1);
Felix Fietkau78b21942014-06-11 16:17:55 +0530419
420 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
421}
Felix Fietkau748299f2014-06-11 16:18:04 +0530422
Felix Fietkau58b57372014-06-11 16:18:08 +0530423static struct ath_chanctx *
424ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
425{
426 int idx = ctx - &sc->chanctx[0];
427
428 return &sc->chanctx[!idx];
429}
430
431static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
432{
433 struct ath_chanctx *prev, *cur;
434 struct timespec ts;
435 u32 cur_tsf, prev_tsf, beacon_int;
436 s32 offset;
437
438 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
439
440 cur = sc->cur_chan;
441 prev = ath_chanctx_get_next(sc, cur);
442
443 getrawmonotonic(&ts);
444 cur_tsf = (u32) cur->tsf_val +
445 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
446
447 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
448 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
449
450 /* Adjust the TSF time of the AP chanctx to keep its beacons
451 * at half beacon interval offset relative to the STA chanctx.
452 */
453 offset = cur_tsf - prev_tsf;
454
455 /* Ignore stale data or spurious timestamps */
456 if (offset < 0 || offset > 3 * beacon_int)
457 return;
458
459 offset = beacon_int / 2 - (offset % beacon_int);
460 prev->tsf_val += offset;
461}
462
Felix Fietkau42eda112014-06-11 16:18:14 +0530463/* Configure the TSF based hardware timer for a channel switch.
464 * Also set up backup software timer, in case the gen timer fails.
465 * This could be caused by a hardware reset.
466 */
467static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
468{
469 struct ath_hw *ah = sc->sc_ah;
470
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530471#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
Felix Fietkau42eda112014-06-11 16:18:14 +0530472 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530473#endif
Felix Fietkau42eda112014-06-11 16:18:14 +0530474 tsf_time -= ath9k_hw_gettsf32(ah);
475 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
476 mod_timer(&sc->sched.timer, tsf_time);
477}
478
Felix Fietkau748299f2014-06-11 16:18:04 +0530479void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
480 enum ath_chanctx_event ev)
481{
482 struct ath_hw *ah = sc->sc_ah;
Felix Fietkau58b57372014-06-11 16:18:08 +0530483 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau74148632014-06-11 16:18:11 +0530484 struct ath_beacon_config *cur_conf;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530485 struct ath_vif *avp = NULL;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530486 struct ath_chanctx *ctx;
Felix Fietkau748299f2014-06-11 16:18:04 +0530487 u32 tsf_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530488 u32 beacon_int;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530489 bool noa_changed = false;
490
491 if (vif)
492 avp = (struct ath_vif *) vif->drv_priv;
Felix Fietkau748299f2014-06-11 16:18:04 +0530493
494 spin_lock_bh(&sc->chan_lock);
495
496 switch (ev) {
497 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530498 if (avp->offchannel_duration)
499 avp->offchannel_duration = 0;
500
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530501 if (avp->chanctx != sc->cur_chan)
502 break;
503
504 if (sc->sched.offchannel_pending) {
505 sc->sched.offchannel_pending = false;
506 sc->next_chan = &sc->offchannel.chan;
507 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
508 }
509
510 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
511 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
512 sc->next_chan = ctx;
513 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
514 }
515
516 /* if the timer missed its window, use the next interval */
517 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
518 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
519
Felix Fietkau748299f2014-06-11 16:18:04 +0530520 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
521 break;
522
523 sc->sched.beacon_pending = true;
524 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530525
Felix Fietkau74148632014-06-11 16:18:11 +0530526 cur_conf = &sc->cur_chan->beacon;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530527 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
528
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530529 /* defer channel switch by a quarter beacon interval */
Felix Fietkauec70abe2014-06-11 16:18:12 +0530530 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530531 sc->sched.switch_start_time = tsf_time;
Felix Fietkau58b57372014-06-11 16:18:08 +0530532 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530533
Felix Fietkau74148632014-06-11 16:18:11 +0530534 /* Prevent wrap-around issues */
535 if (avp->periodic_noa_duration &&
536 tsf_time - avp->periodic_noa_start > BIT(30))
537 avp->periodic_noa_duration = 0;
538
539 if (ctx->active && !avp->periodic_noa_duration) {
540 avp->periodic_noa_start = tsf_time;
541 avp->periodic_noa_duration =
542 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
543 sc->sched.channel_switch_time;
544 noa_changed = true;
545 } else if (!ctx->active && avp->periodic_noa_duration) {
546 avp->periodic_noa_duration = 0;
547 noa_changed = true;
548 }
549
Felix Fietkauec70abe2014-06-11 16:18:12 +0530550 /* If at least two consecutive beacons were missed on the STA
551 * chanctx, stay on the STA channel for one extra beacon period,
552 * to resync the timer properly.
553 */
554 if (ctx->active && sc->sched.beacon_miss >= 2)
555 sc->sched.offchannel_duration = 3 * beacon_int / 2;
556
Felix Fietkau3ae07d32014-06-11 16:18:06 +0530557 if (sc->sched.offchannel_duration) {
558 noa_changed = true;
559 avp->offchannel_start = tsf_time;
560 avp->offchannel_duration =
561 sc->sched.offchannel_duration;
562 }
563
564 if (noa_changed)
565 avp->noa_index++;
Felix Fietkau748299f2014-06-11 16:18:04 +0530566 break;
567 case ATH_CHANCTX_EVENT_BEACON_SENT:
568 if (!sc->sched.beacon_pending)
569 break;
570
571 sc->sched.beacon_pending = false;
572 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
573 break;
574
Felix Fietkau748299f2014-06-11 16:18:04 +0530575 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkau42eda112014-06-11 16:18:14 +0530576 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
Felix Fietkau748299f2014-06-11 16:18:04 +0530577 break;
578 case ATH_CHANCTX_EVENT_TSF_TIMER:
579 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
580 break;
581
Felix Fietkauec70abe2014-06-11 16:18:12 +0530582 if (!sc->cur_chan->switch_after_beacon &&
583 sc->sched.beacon_pending)
584 sc->sched.beacon_miss++;
585
Felix Fietkau748299f2014-06-11 16:18:04 +0530586 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
587 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
588 break;
Felix Fietkau58b57372014-06-11 16:18:08 +0530589 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530590 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
591 sc->cur_chan == &sc->offchannel.chan)
Felix Fietkau58b57372014-06-11 16:18:08 +0530592 break;
593
594 ath_chanctx_adjust_tbtt_delta(sc);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530595 sc->sched.beacon_pending = false;
596 sc->sched.beacon_miss = 0;
Felix Fietkaua899b672014-06-11 16:18:13 +0530597
598 /* TSF time might have been updated by the incoming beacon,
599 * need update the channel switch timer to reflect the change.
600 */
601 tsf_time = sc->sched.switch_start_time;
602 tsf_time -= (u32) sc->cur_chan->tsf_val +
603 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
604 tsf_time += ath9k_hw_gettsf32(ah);
605
Felix Fietkau42eda112014-06-11 16:18:14 +0530606
607 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkau58b57372014-06-11 16:18:08 +0530608 break;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530609 case ATH_CHANCTX_EVENT_ASSOC:
610 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
611 avp->chanctx != sc->cur_chan)
612 break;
613
614 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
615 /* fall through */
616 case ATH_CHANCTX_EVENT_SWITCH:
617 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
618 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
619 sc->cur_chan->switch_after_beacon ||
620 sc->cur_chan == &sc->offchannel.chan)
621 break;
622
623 /* If this is a station chanctx, stay active for a half
624 * beacon period (minus channel switch time)
625 */
626 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
Felix Fietkau74148632014-06-11 16:18:11 +0530627 cur_conf = &sc->cur_chan->beacon;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530628
629 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530630
631 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
632 if (sc->sched.beacon_miss >= 2) {
633 sc->sched.beacon_miss = 0;
634 tsf_time *= 3;
635 }
636
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530637 tsf_time -= sc->sched.channel_switch_time;
Felix Fietkauec70abe2014-06-11 16:18:12 +0530638 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530639 sc->sched.switch_start_time = tsf_time;
640
Felix Fietkau42eda112014-06-11 16:18:14 +0530641 ath_chanctx_setup_timer(sc, tsf_time);
Felix Fietkauec70abe2014-06-11 16:18:12 +0530642 sc->sched.beacon_pending = true;
Felix Fietkau73fa2f22014-06-11 16:18:10 +0530643 break;
644 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
645 if (sc->cur_chan == &sc->offchannel.chan ||
646 sc->cur_chan->switch_after_beacon)
647 break;
648
649 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
650 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
651 break;
652 case ATH_CHANCTX_EVENT_UNASSIGN:
653 if (sc->cur_chan->assigned) {
654 if (sc->next_chan && !sc->next_chan->assigned &&
655 sc->next_chan != &sc->offchannel.chan)
656 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
657 break;
658 }
659
660 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
661 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
662 if (!ctx->assigned)
663 break;
664
665 sc->next_chan = ctx;
666 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
667 break;
Felix Fietkau748299f2014-06-11 16:18:04 +0530668 }
669
670 spin_unlock_bh(&sc->chan_lock);
671}
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530672
673static int ath_scan_channel_duration(struct ath_softc *sc,
674 struct ieee80211_channel *chan)
675{
676 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
677
678 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
679 return (HZ / 9); /* ~110 ms */
680
681 return (HZ / 16); /* ~60 ms */
682}
683
684static void
685ath_scan_next_channel(struct ath_softc *sc)
686{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530687 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530688 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
689 struct ieee80211_channel *chan;
690
691 if (sc->offchannel.scan_idx >= req->n_channels) {
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530692 ath_dbg(common, CHAN_CTX,
693 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
694 sc->offchannel.scan_idx,
695 req->n_channels);
696
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530697 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
698 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
699 NULL);
700 return;
701 }
702
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530703 ath_dbg(common, CHAN_CTX,
704 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
705 sc->offchannel.scan_idx);
706
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530707 chan = req->channels[sc->offchannel.scan_idx++];
708 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
709 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530710
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530711 ath_chanctx_offchan_switch(sc, chan);
712}
713
714void ath_offchannel_next(struct ath_softc *sc)
715{
716 struct ieee80211_vif *vif;
717
718 if (sc->offchannel.scan_req) {
719 vif = sc->offchannel.scan_vif;
720 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
721 ath_scan_next_channel(sc);
722 } else if (sc->offchannel.roc_vif) {
723 vif = sc->offchannel.roc_vif;
724 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
725 sc->offchannel.duration = sc->offchannel.roc_duration;
726 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
727 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
728 } else {
729 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
730 NULL);
731 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
732 if (sc->ps_idle)
733 ath_cancel_work(sc);
734 }
735}
736
737void ath_roc_complete(struct ath_softc *sc, bool abort)
738{
739 sc->offchannel.roc_vif = NULL;
740 sc->offchannel.roc_chan = NULL;
741 if (!abort)
742 ieee80211_remain_on_channel_expired(sc->hw);
743 ath_offchannel_next(sc);
744 ath9k_ps_restore(sc);
745}
746
747void ath_scan_complete(struct ath_softc *sc, bool abort)
748{
749 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
750
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530751 if (abort)
752 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
753 else
754 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
755
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530756 sc->offchannel.scan_req = NULL;
757 sc->offchannel.scan_vif = NULL;
758 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
759 ieee80211_scan_completed(sc->hw, abort);
760 clear_bit(ATH_OP_SCANNING, &common->op_flags);
761 ath_offchannel_next(sc);
762 ath9k_ps_restore(sc);
763}
764
765static void ath_scan_send_probe(struct ath_softc *sc,
766 struct cfg80211_ssid *ssid)
767{
768 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
769 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
770 struct ath_tx_control txctl = {};
771 struct sk_buff *skb;
772 struct ieee80211_tx_info *info;
773 int band = sc->offchannel.chan.chandef.chan->band;
774
775 skb = ieee80211_probereq_get(sc->hw, vif,
776 ssid->ssid, ssid->ssid_len, req->ie_len);
777 if (!skb)
778 return;
779
780 info = IEEE80211_SKB_CB(skb);
781 if (req->no_cck)
782 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
783
784 if (req->ie_len)
785 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
786
787 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
788
789 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
790 goto error;
791
792 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
793 txctl.force_channel = true;
794 if (ath_tx_start(sc->hw, skb, &txctl))
795 goto error;
796
797 return;
798
799error:
800 ieee80211_free_txskb(sc->hw, skb);
801}
802
803static void ath_scan_channel_start(struct ath_softc *sc)
804{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530805 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530806 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
807 int i;
808
809 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
810 req->n_ssids) {
811 for (i = 0; i < req->n_ssids; i++)
812 ath_scan_send_probe(sc, &req->ssids[i]);
813
814 }
815
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530816 ath_dbg(common, CHAN_CTX,
817 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
818
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530819 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
820 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
821}
822
823void ath_offchannel_channel_change(struct ath_softc *sc)
824{
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530825 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
826
827 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
828 __func__, offchannel_state_string(sc->offchannel.state));
829
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530830 switch (sc->offchannel.state) {
831 case ATH_OFFCHANNEL_PROBE_SEND:
832 if (!sc->offchannel.scan_req)
833 return;
834
835 if (sc->cur_chan->chandef.chan !=
836 sc->offchannel.chan.chandef.chan)
837 return;
838
839 ath_scan_channel_start(sc);
840 break;
841 case ATH_OFFCHANNEL_IDLE:
842 if (!sc->offchannel.scan_req)
843 return;
844
845 ath_scan_complete(sc, false);
846 break;
847 case ATH_OFFCHANNEL_ROC_START:
848 if (sc->cur_chan != &sc->offchannel.chan)
849 break;
850
851 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
852 mod_timer(&sc->offchannel.timer, jiffies +
853 msecs_to_jiffies(sc->offchannel.duration));
854 ieee80211_ready_on_channel(sc->hw);
855 break;
856 case ATH_OFFCHANNEL_ROC_DONE:
857 ath_roc_complete(sc, false);
858 break;
859 default:
860 break;
861 }
862}
863
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530864#ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
865
866static void ath_chanctx_timer(unsigned long data)
867{
868 struct ath_softc *sc = (struct ath_softc *) data;
869
870 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
871}
872
873static void ath_offchannel_timer(unsigned long data)
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530874{
875 struct ath_softc *sc = (struct ath_softc *)data;
876 struct ath_chanctx *ctx;
Sujith Manoharanbc81d432014-08-22 20:39:27 +0530877 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
878
879 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
880 __func__, offchannel_state_string(sc->offchannel.state));
Sujith Manoharandfcbb3e2014-08-22 20:39:26 +0530881
882 switch (sc->offchannel.state) {
883 case ATH_OFFCHANNEL_PROBE_WAIT:
884 if (!sc->offchannel.scan_req)
885 return;
886
887 /* get first active channel context */
888 ctx = ath_chanctx_get_oper_chan(sc, true);
889 if (ctx->active) {
890 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
891 ath_chanctx_switch(sc, ctx, NULL);
892 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
893 break;
894 }
895 /* fall through */
896 case ATH_OFFCHANNEL_SUSPEND:
897 if (!sc->offchannel.scan_req)
898 return;
899
900 ath_scan_next_channel(sc);
901 break;
902 case ATH_OFFCHANNEL_ROC_START:
903 case ATH_OFFCHANNEL_ROC_WAIT:
904 ctx = ath_chanctx_get_oper_chan(sc, false);
905 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
906 ath_chanctx_switch(sc, ctx, NULL);
907 break;
908 default:
909 break;
910 }
911}
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530912
Sujith Manoharan0e62f8b2014-08-23 13:29:08 +0530913static void ath_chanctx_work(struct work_struct *work)
914{
915 struct ath_softc *sc = container_of(work, struct ath_softc,
916 chanctx_work);
917 mutex_lock(&sc->mutex);
918 ath_chanctx_set_next(sc, false);
919 mutex_unlock(&sc->mutex);
920}
921
Sujith Manoharan705d0bf2014-08-23 13:29:06 +0530922void ath9k_init_channel_context(struct ath_softc *sc)
923{
924 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
925
926 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
927 (unsigned long)sc);
928 setup_timer(&sc->sched.timer, ath_chanctx_timer,
929 (unsigned long)sc);
930}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530931
Sujith Manoharanea22df22014-08-23 13:29:07 +0530932void ath9k_deinit_channel_context(struct ath_softc *sc)
933{
934 cancel_work_sync(&sc->chanctx_work);
935}
936
Sujith Manoharan499afac2014-08-22 20:39:31 +0530937bool ath9k_is_chanctx_enabled(void)
938{
939 return (ath9k_use_chanctx == 1);
940}
941
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530942/*****************/
943/* P2P Powersave */
944/*****************/
945
946static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530947{
948 struct ath_hw *ah = sc->sc_ah;
949 s32 tsf, target_tsf;
950
951 if (!avp || !avp->noa.has_next_tsf)
952 return;
953
954 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
955
956 tsf = ath9k_hw_gettsf32(sc->sc_ah);
957
958 target_tsf = avp->noa.next_tsf;
959 if (!avp->noa.absent)
960 target_tsf -= ATH_P2P_PS_STOP_TIME;
961
962 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
963 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
964
965 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
966}
967
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +0530968static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
969{
970 struct ath_vif *avp = (void *)vif->drv_priv;
971 u32 tsf;
972
973 if (!sc->p2p_ps_timer)
974 return;
975
976 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
977 return;
978
979 sc->p2p_ps_vif = avp;
980 tsf = ath9k_hw_gettsf32(sc->sc_ah);
981 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
982 ath9k_update_p2p_ps_timer(sc, avp);
983}
984
Sujith Manoharan2471adf2014-08-22 20:39:29 +0530985void ath9k_p2p_ps_timer(void *priv)
986{
987 struct ath_softc *sc = priv;
988 struct ath_vif *avp = sc->p2p_ps_vif;
989 struct ieee80211_vif *vif;
990 struct ieee80211_sta *sta;
991 struct ath_node *an;
992 u32 tsf;
993
994 del_timer_sync(&sc->sched.timer);
995 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
996 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
997
998 if (!avp || avp->chanctx != sc->cur_chan)
999 return;
1000
1001 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1002 if (!avp->noa.absent)
1003 tsf += ATH_P2P_PS_STOP_TIME;
1004
1005 if (!avp->noa.has_next_tsf ||
1006 avp->noa.next_tsf - tsf > BIT(31))
1007 ieee80211_update_p2p_noa(&avp->noa, tsf);
1008
1009 ath9k_update_p2p_ps_timer(sc, avp);
1010
1011 rcu_read_lock();
1012
1013 vif = avp->vif;
1014 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1015 if (!sta)
1016 goto out;
1017
1018 an = (void *) sta->drv_priv;
1019 if (an->sleeping == !!avp->noa.absent)
1020 goto out;
1021
1022 an->sleeping = avp->noa.absent;
1023 if (an->sleeping)
1024 ath_tx_aggr_sleep(sta, sc, an);
1025 else
1026 ath_tx_aggr_wakeup(sc, an);
1027
1028out:
1029 rcu_read_unlock();
1030}
1031
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301032void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1033 struct ieee80211_vif *vif)
1034{
1035 unsigned long flags;
1036
1037 spin_lock_bh(&sc->sc_pcu_lock);
1038 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1039 if (!(sc->ps_flags & PS_BEACON_SYNC))
1040 ath9k_update_p2p_ps(sc, vif);
1041 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1042 spin_unlock_bh(&sc->sc_pcu_lock);
1043}
1044
1045void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1046{
1047 if (sc->p2p_ps_vif)
1048 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1049}
1050
1051void ath9k_p2p_remove_vif(struct ath_softc *sc,
1052 struct ieee80211_vif *vif)
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301053{
1054 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301055
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301056 spin_lock_bh(&sc->sc_pcu_lock);
1057 if (avp == sc->p2p_ps_vif) {
1058 sc->p2p_ps_vif = NULL;
1059 ath9k_update_p2p_ps_timer(sc, NULL);
1060 }
1061 spin_unlock_bh(&sc->sc_pcu_lock);
Sujith Manoharan2471adf2014-08-22 20:39:29 +05301062}
Sujith Manoharanc7dd40c2014-08-22 20:39:30 +05301063
1064int ath9k_init_p2p(struct ath_softc *sc)
1065{
1066 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1067 NULL, sc, AR_FIRST_NDP_TIMER);
1068 if (!sc->p2p_ps_timer)
1069 return -ENOMEM;
1070
1071 return 0;
1072}
1073
1074void ath9k_deinit_p2p(struct ath_softc *sc)
1075{
1076 if (sc->p2p_ps_timer)
1077 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1078}
1079
1080#endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */