2 * Copyright (c) 2014 Qualcomm Atheros, Inc.
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.
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.
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.
23 static int ath_set_channel(struct ath_softc *sc)
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;
35 if (test_bit(ATH_OP_INVALID, &common->op_flags))
39 old_pos = ah->curchan - &ah->channels[0];
41 ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
42 chan->center_freq, chandef->width);
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);
49 ath9k_cmn_get_channel(hw, ah, chandef);
51 /* If the operating channel changes, change the survey in-use flags
53 * Reset the survey data for the new channel, unless we're switching
54 * back to the operating channel from an off-channel operation.
56 if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
58 sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
60 sc->cur_survey = &sc->survey[pos];
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));
68 hchan = &sc->sc_ah->channels[pos];
69 r = ath_reset_internal(sc, hchan);
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.
78 ath_update_survey_nf(sc, old_pos);
80 /* Enable radar pulse detection if on a DFS channel. Spectral
81 * scanning and radar detection can not be used concurrently.
83 if (hw->conf.radar_enabled) {
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",
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);
104 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
106 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
114 list_for_each_entry(avp, &ctx->vifs, list) {
115 struct ieee80211_vif *vif = avp->vif;
118 case NL80211_IFTYPE_P2P_CLIENT:
119 case NL80211_IFTYPE_STATION:
120 if (vif->bss_conf.assoc)
128 ctx->active = active;
130 ath_for_each_chanctx(sc, ctx) {
131 if (!ctx->assigned || list_empty(&ctx->vifs))
137 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
140 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
143 if (ath9k_is_chanctx_enabled()) {
144 ath_chanctx_event(sc, NULL,
145 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
149 void ath_chanctx_init(struct ath_softc *sc)
151 struct ath_chanctx *ctx;
152 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
153 struct ieee80211_supported_band *sband;
154 struct ieee80211_channel *chan;
157 sband = &common->sbands[IEEE80211_BAND_2GHZ];
158 if (!sband->n_channels)
159 sband = &common->sbands[IEEE80211_BAND_5GHZ];
161 chan = &sband->channels[0];
162 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
163 ctx = &sc->chanctx[i];
164 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
165 INIT_LIST_HEAD(&ctx->vifs);
166 ctx->txpower = ATH_TXPOWER_MAX;
167 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
168 INIT_LIST_HEAD(&ctx->acq[j]);
170 ctx = &sc->offchannel.chan;
171 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
172 INIT_LIST_HEAD(&ctx->vifs);
173 ctx->txpower = ATH_TXPOWER_MAX;
174 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
175 INIT_LIST_HEAD(&ctx->acq[j]);
176 sc->offchannel.chan.offchannel = true;
180 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
181 struct cfg80211_chan_def *chandef)
185 spin_lock_bh(&sc->chan_lock);
187 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
188 cur_chan = sc->cur_chan == ctx;
189 spin_unlock_bh(&sc->chan_lock);
197 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
199 /**********************************************************/
200 /* Functions to handle the channel context state machine. */
201 /**********************************************************/
203 static const char *offchannel_state_string(enum ath_offchannel_state state)
205 #define case_rtn_string(val) case val: return #val
208 case_rtn_string(ATH_OFFCHANNEL_IDLE);
209 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
210 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
211 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
212 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
213 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
214 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
220 static struct ath_chanctx *
221 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
223 int idx = ctx - &sc->chanctx[0];
225 return &sc->chanctx[!idx];
228 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
230 struct ath_chanctx *prev, *cur;
232 u32 cur_tsf, prev_tsf, beacon_int;
235 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
238 prev = ath_chanctx_get_next(sc, cur);
240 getrawmonotonic(&ts);
241 cur_tsf = (u32) cur->tsf_val +
242 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
244 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
245 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
247 /* Adjust the TSF time of the AP chanctx to keep its beacons
248 * at half beacon interval offset relative to the STA chanctx.
250 offset = cur_tsf - prev_tsf;
252 /* Ignore stale data or spurious timestamps */
253 if (offset < 0 || offset > 3 * beacon_int)
256 offset = beacon_int / 2 - (offset % beacon_int);
257 prev->tsf_val += offset;
260 /* Configure the TSF based hardware timer for a channel switch.
261 * Also set up backup software timer, in case the gen timer fails.
262 * This could be caused by a hardware reset.
264 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
266 struct ath_hw *ah = sc->sc_ah;
268 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
269 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
271 tsf_time -= ath9k_hw_gettsf32(ah);
272 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
273 mod_timer(&sc->sched.timer, tsf_time);
276 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
277 enum ath_chanctx_event ev)
279 struct ath_hw *ah = sc->sc_ah;
280 struct ath_common *common = ath9k_hw_common(ah);
281 struct ath_beacon_config *cur_conf;
282 struct ath_vif *avp = NULL;
283 struct ath_chanctx *ctx;
286 bool noa_changed = false;
289 avp = (struct ath_vif *) vif->drv_priv;
291 spin_lock_bh(&sc->chan_lock);
294 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
295 if (avp->offchannel_duration)
296 avp->offchannel_duration = 0;
298 if (avp->chanctx != sc->cur_chan)
301 if (sc->sched.offchannel_pending) {
302 sc->sched.offchannel_pending = false;
303 sc->next_chan = &sc->offchannel.chan;
304 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
307 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
308 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
310 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
313 /* if the timer missed its window, use the next interval */
314 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
315 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
317 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
320 sc->sched.beacon_pending = true;
321 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
323 cur_conf = &sc->cur_chan->beacon;
324 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
326 /* defer channel switch by a quarter beacon interval */
327 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
328 sc->sched.switch_start_time = tsf_time;
329 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
331 /* Prevent wrap-around issues */
332 if (avp->periodic_noa_duration &&
333 tsf_time - avp->periodic_noa_start > BIT(30))
334 avp->periodic_noa_duration = 0;
336 if (ctx->active && !avp->periodic_noa_duration) {
337 avp->periodic_noa_start = tsf_time;
338 avp->periodic_noa_duration =
339 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
340 sc->sched.channel_switch_time;
342 } else if (!ctx->active && avp->periodic_noa_duration) {
343 avp->periodic_noa_duration = 0;
347 /* If at least two consecutive beacons were missed on the STA
348 * chanctx, stay on the STA channel for one extra beacon period,
349 * to resync the timer properly.
351 if (ctx->active && sc->sched.beacon_miss >= 2)
352 sc->sched.offchannel_duration = 3 * beacon_int / 2;
354 if (sc->sched.offchannel_duration) {
356 avp->offchannel_start = tsf_time;
357 avp->offchannel_duration =
358 sc->sched.offchannel_duration;
364 case ATH_CHANCTX_EVENT_BEACON_SENT:
365 if (!sc->sched.beacon_pending)
368 sc->sched.beacon_pending = false;
369 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
372 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
373 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
375 case ATH_CHANCTX_EVENT_TSF_TIMER:
376 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
379 if (!sc->cur_chan->switch_after_beacon &&
380 sc->sched.beacon_pending)
381 sc->sched.beacon_miss++;
383 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
384 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
386 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
387 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
388 sc->cur_chan == &sc->offchannel.chan)
391 ath_chanctx_adjust_tbtt_delta(sc);
392 sc->sched.beacon_pending = false;
393 sc->sched.beacon_miss = 0;
395 /* TSF time might have been updated by the incoming beacon,
396 * need update the channel switch timer to reflect the change.
398 tsf_time = sc->sched.switch_start_time;
399 tsf_time -= (u32) sc->cur_chan->tsf_val +
400 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
401 tsf_time += ath9k_hw_gettsf32(ah);
404 ath_chanctx_setup_timer(sc, tsf_time);
406 case ATH_CHANCTX_EVENT_ASSOC:
407 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
408 avp->chanctx != sc->cur_chan)
411 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
413 case ATH_CHANCTX_EVENT_SWITCH:
414 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
415 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
416 sc->cur_chan->switch_after_beacon ||
417 sc->cur_chan == &sc->offchannel.chan)
420 /* If this is a station chanctx, stay active for a half
421 * beacon period (minus channel switch time)
423 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
424 cur_conf = &sc->cur_chan->beacon;
426 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
428 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
429 if (sc->sched.beacon_miss >= 2) {
430 sc->sched.beacon_miss = 0;
434 tsf_time -= sc->sched.channel_switch_time;
435 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
436 sc->sched.switch_start_time = tsf_time;
438 ath_chanctx_setup_timer(sc, tsf_time);
439 sc->sched.beacon_pending = true;
441 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
442 if (sc->cur_chan == &sc->offchannel.chan ||
443 sc->cur_chan->switch_after_beacon)
446 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
447 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
449 case ATH_CHANCTX_EVENT_UNASSIGN:
450 if (sc->cur_chan->assigned) {
451 if (sc->next_chan && !sc->next_chan->assigned &&
452 sc->next_chan != &sc->offchannel.chan)
453 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
457 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
458 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
463 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
467 spin_unlock_bh(&sc->chan_lock);
470 void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
471 enum ath_chanctx_event ev)
473 if (sc->sched.beacon_pending)
474 ath_chanctx_event(sc, NULL, ev);
477 void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
478 enum ath_chanctx_event ev)
480 sc->sched.next_tbtt = ts;
481 ath_chanctx_event(sc, NULL, ev);
484 static int ath_scan_channel_duration(struct ath_softc *sc,
485 struct ieee80211_channel *chan)
487 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
489 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
490 return (HZ / 9); /* ~110 ms */
492 return (HZ / 16); /* ~60 ms */
495 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
496 struct cfg80211_chan_def *chandef)
498 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
500 spin_lock_bh(&sc->chan_lock);
502 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
503 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
504 sc->sched.offchannel_pending = true;
505 spin_unlock_bh(&sc->chan_lock);
511 ctx->chandef = *chandef;
512 ath_dbg(common, CHAN_CTX,
513 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
516 if (sc->next_chan == &sc->offchannel.chan) {
517 sc->sched.offchannel_duration =
518 TU_TO_USEC(sc->offchannel.duration) +
519 sc->sched.channel_switch_time;
522 ath_dbg(common, CHAN_CTX,
523 "Offchannel duration for chan %d MHz : %u\n",
524 chandef->center_freq1,
525 sc->sched.offchannel_duration);
528 spin_unlock_bh(&sc->chan_lock);
529 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
532 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
533 struct ieee80211_channel *chan)
535 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
536 struct cfg80211_chan_def chandef;
538 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
539 ath_dbg(common, CHAN_CTX,
540 "Channel definition created: %d MHz\n", chandef.center_freq1);
542 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
545 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
548 struct ath_chanctx *ctx;
550 ath_for_each_chanctx(sc, ctx) {
551 if (!ctx->assigned || list_empty(&ctx->vifs))
553 if (active && !ctx->active)
556 if (ctx->switch_after_beacon)
560 return &sc->chanctx[0];
564 ath_scan_next_channel(struct ath_softc *sc)
566 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
567 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
568 struct ieee80211_channel *chan;
570 if (sc->offchannel.scan_idx >= req->n_channels) {
571 ath_dbg(common, CHAN_CTX,
572 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
573 sc->offchannel.scan_idx,
576 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
577 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
582 ath_dbg(common, CHAN_CTX,
583 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
584 sc->offchannel.scan_idx);
586 chan = req->channels[sc->offchannel.scan_idx++];
587 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
588 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
590 ath_chanctx_offchan_switch(sc, chan);
593 void ath_offchannel_next(struct ath_softc *sc)
595 struct ieee80211_vif *vif;
597 if (sc->offchannel.scan_req) {
598 vif = sc->offchannel.scan_vif;
599 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
600 ath_scan_next_channel(sc);
601 } else if (sc->offchannel.roc_vif) {
602 vif = sc->offchannel.roc_vif;
603 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
604 sc->offchannel.duration = sc->offchannel.roc_duration;
605 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
606 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
608 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
610 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
616 void ath_roc_complete(struct ath_softc *sc, bool abort)
618 sc->offchannel.roc_vif = NULL;
619 sc->offchannel.roc_chan = NULL;
621 ieee80211_remain_on_channel_expired(sc->hw);
622 ath_offchannel_next(sc);
623 ath9k_ps_restore(sc);
626 void ath_scan_complete(struct ath_softc *sc, bool abort)
628 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
631 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
633 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
635 sc->offchannel.scan_req = NULL;
636 sc->offchannel.scan_vif = NULL;
637 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
638 ieee80211_scan_completed(sc->hw, abort);
639 clear_bit(ATH_OP_SCANNING, &common->op_flags);
640 ath_offchannel_next(sc);
641 ath9k_ps_restore(sc);
644 static void ath_scan_send_probe(struct ath_softc *sc,
645 struct cfg80211_ssid *ssid)
647 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
648 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
649 struct ath_tx_control txctl = {};
651 struct ieee80211_tx_info *info;
652 int band = sc->offchannel.chan.chandef.chan->band;
654 skb = ieee80211_probereq_get(sc->hw, vif,
655 ssid->ssid, ssid->ssid_len, req->ie_len);
659 info = IEEE80211_SKB_CB(skb);
661 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
664 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
666 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
668 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
671 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
672 txctl.force_channel = true;
673 if (ath_tx_start(sc->hw, skb, &txctl))
679 ieee80211_free_txskb(sc->hw, skb);
682 static void ath_scan_channel_start(struct ath_softc *sc)
684 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
685 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
688 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
690 for (i = 0; i < req->n_ssids; i++)
691 ath_scan_send_probe(sc, &req->ssids[i]);
695 ath_dbg(common, CHAN_CTX,
696 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
698 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
699 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
702 static void ath_chanctx_timer(unsigned long data)
704 struct ath_softc *sc = (struct ath_softc *) data;
706 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
709 static void ath_offchannel_timer(unsigned long data)
711 struct ath_softc *sc = (struct ath_softc *)data;
712 struct ath_chanctx *ctx;
713 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
715 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
716 __func__, offchannel_state_string(sc->offchannel.state));
718 switch (sc->offchannel.state) {
719 case ATH_OFFCHANNEL_PROBE_WAIT:
720 if (!sc->offchannel.scan_req)
723 /* get first active channel context */
724 ctx = ath_chanctx_get_oper_chan(sc, true);
726 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
727 ath_chanctx_switch(sc, ctx, NULL);
728 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
732 case ATH_OFFCHANNEL_SUSPEND:
733 if (!sc->offchannel.scan_req)
736 ath_scan_next_channel(sc);
738 case ATH_OFFCHANNEL_ROC_START:
739 case ATH_OFFCHANNEL_ROC_WAIT:
740 ctx = ath_chanctx_get_oper_chan(sc, false);
741 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
742 ath_chanctx_switch(sc, ctx, NULL);
750 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
753 struct ieee80211_vif *vif = avp->vif;
754 struct ieee80211_sta *sta = NULL;
755 struct ieee80211_hdr_3addr *nullfunc;
756 struct ath_tx_control txctl;
758 int band = sc->cur_chan->chandef.chan->band;
761 case NL80211_IFTYPE_STATION:
762 if (!vif->bss_conf.assoc)
765 skb = ieee80211_nullfunc_get(sc->hw, vif);
769 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
771 nullfunc->frame_control |=
772 cpu_to_le16(IEEE80211_FCTL_PM);
774 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
775 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
776 dev_kfree_skb_any(skb);
784 memset(&txctl, 0, sizeof(txctl));
785 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
787 txctl.force_channel = true;
788 if (ath_tx_start(sc->hw, skb, &txctl)) {
789 ieee80211_free_txskb(sc->hw, skb);
797 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
803 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
804 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
812 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
814 if (sc->cur_chan == &sc->offchannel.chan)
817 switch (sc->sched.state) {
818 case ATH_CHANCTX_STATE_SWITCH:
820 case ATH_CHANCTX_STATE_IDLE:
821 if (!sc->cur_chan->switch_after_beacon)
824 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
833 static void ath_offchannel_channel_change(struct ath_softc *sc)
835 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
837 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
838 __func__, offchannel_state_string(sc->offchannel.state));
840 switch (sc->offchannel.state) {
841 case ATH_OFFCHANNEL_PROBE_SEND:
842 if (!sc->offchannel.scan_req)
845 if (sc->cur_chan->chandef.chan !=
846 sc->offchannel.chan.chandef.chan)
849 ath_scan_channel_start(sc);
851 case ATH_OFFCHANNEL_IDLE:
852 if (!sc->offchannel.scan_req)
855 ath_scan_complete(sc, false);
857 case ATH_OFFCHANNEL_ROC_START:
858 if (sc->cur_chan != &sc->offchannel.chan)
861 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
862 mod_timer(&sc->offchannel.timer, jiffies +
863 msecs_to_jiffies(sc->offchannel.duration));
864 ieee80211_ready_on_channel(sc->hw);
866 case ATH_OFFCHANNEL_ROC_DONE:
867 ath_roc_complete(sc, false);
874 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
877 bool measure_time = false;
878 bool send_ps = false;
880 spin_lock_bh(&sc->chan_lock);
881 if (!sc->next_chan) {
882 spin_unlock_bh(&sc->chan_lock);
886 if (!force && ath_chanctx_defer_switch(sc)) {
887 spin_unlock_bh(&sc->chan_lock);
891 if (sc->cur_chan != sc->next_chan) {
892 sc->cur_chan->stopped = true;
893 spin_unlock_bh(&sc->chan_lock);
895 if (sc->next_chan == &sc->offchannel.chan) {
896 getrawmonotonic(&ts);
899 __ath9k_flush(sc->hw, ~0, true);
901 if (ath_chanctx_send_ps_frame(sc, true))
902 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
905 spin_lock_bh(&sc->chan_lock);
907 if (sc->cur_chan != &sc->offchannel.chan) {
908 getrawmonotonic(&sc->cur_chan->tsf_ts);
909 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
912 sc->cur_chan = sc->next_chan;
913 sc->cur_chan->stopped = false;
914 sc->next_chan = NULL;
915 sc->sched.offchannel_duration = 0;
916 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
917 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
919 spin_unlock_bh(&sc->chan_lock);
921 if (sc->sc_ah->chip_fullsleep ||
922 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
923 sizeof(sc->cur_chandef))) {
926 sc->sched.channel_switch_time =
927 ath9k_hw_get_tsf_offset(&ts, NULL);
930 ath_chanctx_send_ps_frame(sc, false);
932 ath_offchannel_channel_change(sc);
933 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
936 static void ath_chanctx_work(struct work_struct *work)
938 struct ath_softc *sc = container_of(work, struct ath_softc,
940 mutex_lock(&sc->mutex);
941 ath_chanctx_set_next(sc, false);
942 mutex_unlock(&sc->mutex);
945 void ath9k_init_channel_context(struct ath_softc *sc)
947 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
949 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
951 setup_timer(&sc->sched.timer, ath_chanctx_timer,
955 void ath9k_deinit_channel_context(struct ath_softc *sc)
957 cancel_work_sync(&sc->chanctx_work);
960 bool ath9k_is_chanctx_enabled(void)
962 return (ath9k_use_chanctx == 1);
965 /********************/
966 /* Queue management */
967 /********************/
969 void ath9k_chanctx_wake_queues(struct ath_softc *sc)
971 struct ath_hw *ah = sc->sc_ah;
974 if (sc->cur_chan == &sc->offchannel.chan) {
975 ieee80211_wake_queue(sc->hw,
976 sc->hw->offchannel_tx_hw_queue);
978 for (i = 0; i < IEEE80211_NUM_ACS; i++)
979 ieee80211_wake_queue(sc->hw,
980 sc->cur_chan->hw_queue_base + i);
983 if (ah->opmode == NL80211_IFTYPE_AP)
984 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
991 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
993 struct ath_hw *ah = sc->sc_ah;
996 if (!avp || !avp->noa.has_next_tsf)
999 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1001 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1003 target_tsf = avp->noa.next_tsf;
1004 if (!avp->noa.absent)
1005 target_tsf -= ATH_P2P_PS_STOP_TIME;
1007 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1008 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1010 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1013 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1015 struct ath_vif *avp = (void *)vif->drv_priv;
1018 if (!sc->p2p_ps_timer)
1021 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1024 sc->p2p_ps_vif = avp;
1025 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1026 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1027 ath9k_update_p2p_ps_timer(sc, avp);
1030 void ath9k_p2p_ps_timer(void *priv)
1032 struct ath_softc *sc = priv;
1033 struct ath_vif *avp = sc->p2p_ps_vif;
1034 struct ieee80211_vif *vif;
1035 struct ieee80211_sta *sta;
1036 struct ath_node *an;
1039 del_timer_sync(&sc->sched.timer);
1040 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1041 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1043 if (!avp || avp->chanctx != sc->cur_chan)
1046 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1047 if (!avp->noa.absent)
1048 tsf += ATH_P2P_PS_STOP_TIME;
1050 if (!avp->noa.has_next_tsf ||
1051 avp->noa.next_tsf - tsf > BIT(31))
1052 ieee80211_update_p2p_noa(&avp->noa, tsf);
1054 ath9k_update_p2p_ps_timer(sc, avp);
1059 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1063 an = (void *) sta->drv_priv;
1064 if (an->sleeping == !!avp->noa.absent)
1067 an->sleeping = avp->noa.absent;
1069 ath_tx_aggr_sleep(sta, sc, an);
1071 ath_tx_aggr_wakeup(sc, an);
1077 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1078 struct ieee80211_vif *vif)
1080 unsigned long flags;
1082 spin_lock_bh(&sc->sc_pcu_lock);
1083 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1084 if (!(sc->ps_flags & PS_BEACON_SYNC))
1085 ath9k_update_p2p_ps(sc, vif);
1086 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1087 spin_unlock_bh(&sc->sc_pcu_lock);
1090 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1093 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1096 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1097 struct ieee80211_vif *vif)
1099 struct ath_vif *avp = (void *)vif->drv_priv;
1101 spin_lock_bh(&sc->sc_pcu_lock);
1102 if (avp == sc->p2p_ps_vif) {
1103 sc->p2p_ps_vif = NULL;
1104 ath9k_update_p2p_ps_timer(sc, NULL);
1106 spin_unlock_bh(&sc->sc_pcu_lock);
1109 int ath9k_init_p2p(struct ath_softc *sc)
1111 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1112 NULL, sc, AR_FIRST_NDP_TIMER);
1113 if (!sc->p2p_ps_timer)
1119 void ath9k_deinit_p2p(struct ath_softc *sc)
1121 if (sc->p2p_ps_timer)
1122 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1125 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */