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);
105 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
108 struct ieee80211_vif *vif = avp->vif;
109 struct ieee80211_sta *sta = NULL;
110 struct ieee80211_hdr_3addr *nullfunc;
111 struct ath_tx_control txctl;
113 int band = sc->cur_chan->chandef.chan->band;
116 case NL80211_IFTYPE_STATION:
117 if (!vif->bss_conf.assoc)
120 skb = ieee80211_nullfunc_get(sc->hw, vif);
124 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
126 nullfunc->frame_control |=
127 cpu_to_le16(IEEE80211_FCTL_PM);
129 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
130 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
131 dev_kfree_skb_any(skb);
139 memset(&txctl, 0, sizeof(txctl));
140 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
142 txctl.force_channel = true;
143 if (ath_tx_start(sc->hw, skb, &txctl)) {
144 ieee80211_free_txskb(sc->hw, skb);
151 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
153 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
161 list_for_each_entry(avp, &ctx->vifs, list) {
162 struct ieee80211_vif *vif = avp->vif;
165 case NL80211_IFTYPE_P2P_CLIENT:
166 case NL80211_IFTYPE_STATION:
167 if (vif->bss_conf.assoc)
175 ctx->active = active;
177 ath_for_each_chanctx(sc, ctx) {
178 if (!ctx->assigned || list_empty(&ctx->vifs))
184 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
187 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
189 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
193 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
199 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
200 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
208 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
210 if (sc->cur_chan == &sc->offchannel.chan)
213 switch (sc->sched.state) {
214 case ATH_CHANCTX_STATE_SWITCH:
216 case ATH_CHANCTX_STATE_IDLE:
217 if (!sc->cur_chan->switch_after_beacon)
220 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
229 static void ath_chanctx_set_next(struct ath_softc *sc, bool force)
232 bool measure_time = false;
233 bool send_ps = false;
235 spin_lock_bh(&sc->chan_lock);
236 if (!sc->next_chan) {
237 spin_unlock_bh(&sc->chan_lock);
241 if (!force && ath_chanctx_defer_switch(sc)) {
242 spin_unlock_bh(&sc->chan_lock);
246 if (sc->cur_chan != sc->next_chan) {
247 sc->cur_chan->stopped = true;
248 spin_unlock_bh(&sc->chan_lock);
250 if (sc->next_chan == &sc->offchannel.chan) {
251 getrawmonotonic(&ts);
254 __ath9k_flush(sc->hw, ~0, true);
256 if (ath_chanctx_send_ps_frame(sc, true))
257 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
260 spin_lock_bh(&sc->chan_lock);
262 if (sc->cur_chan != &sc->offchannel.chan) {
263 getrawmonotonic(&sc->cur_chan->tsf_ts);
264 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
267 sc->cur_chan = sc->next_chan;
268 sc->cur_chan->stopped = false;
269 sc->next_chan = NULL;
270 sc->sched.offchannel_duration = 0;
271 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
272 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
274 spin_unlock_bh(&sc->chan_lock);
276 if (sc->sc_ah->chip_fullsleep ||
277 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
278 sizeof(sc->cur_chandef))) {
281 sc->sched.channel_switch_time =
282 ath9k_hw_get_tsf_offset(&ts, NULL);
285 ath_chanctx_send_ps_frame(sc, false);
287 ath_offchannel_channel_change(sc);
288 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
291 void ath_chanctx_work(struct work_struct *work)
293 struct ath_softc *sc = container_of(work, struct ath_softc,
295 mutex_lock(&sc->mutex);
296 ath_chanctx_set_next(sc, false);
297 mutex_unlock(&sc->mutex);
300 void ath_chanctx_init(struct ath_softc *sc)
302 struct ath_chanctx *ctx;
303 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
304 struct ieee80211_supported_band *sband;
305 struct ieee80211_channel *chan;
308 sband = &common->sbands[IEEE80211_BAND_2GHZ];
309 if (!sband->n_channels)
310 sband = &common->sbands[IEEE80211_BAND_5GHZ];
312 chan = &sband->channels[0];
313 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
314 ctx = &sc->chanctx[i];
315 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
316 INIT_LIST_HEAD(&ctx->vifs);
317 ctx->txpower = ATH_TXPOWER_MAX;
318 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
319 INIT_LIST_HEAD(&ctx->acq[j]);
321 ctx = &sc->offchannel.chan;
322 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
323 INIT_LIST_HEAD(&ctx->vifs);
324 ctx->txpower = ATH_TXPOWER_MAX;
325 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
326 INIT_LIST_HEAD(&ctx->acq[j]);
327 sc->offchannel.chan.offchannel = true;
331 void ath9k_chanctx_force_active(struct ieee80211_hw *hw,
332 struct ieee80211_vif *vif)
334 struct ath_softc *sc = hw->priv;
335 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
336 struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
337 bool changed = false;
339 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
345 mutex_lock(&sc->mutex);
347 spin_lock_bh(&sc->chan_lock);
348 if (sc->next_chan || (sc->cur_chan != avp->chanctx)) {
349 sc->next_chan = avp->chanctx;
352 sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
353 spin_unlock_bh(&sc->chan_lock);
356 ath_chanctx_set_next(sc, true);
358 mutex_unlock(&sc->mutex);
361 void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
362 struct cfg80211_chan_def *chandef)
364 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
366 spin_lock_bh(&sc->chan_lock);
368 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
369 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
370 sc->sched.offchannel_pending = true;
371 spin_unlock_bh(&sc->chan_lock);
377 ctx->chandef = *chandef;
379 if (sc->next_chan == &sc->offchannel.chan) {
380 sc->sched.offchannel_duration =
381 TU_TO_USEC(sc->offchannel.duration) +
382 sc->sched.channel_switch_time;
384 spin_unlock_bh(&sc->chan_lock);
385 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
388 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
389 struct cfg80211_chan_def *chandef)
393 spin_lock_bh(&sc->chan_lock);
395 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
396 cur_chan = sc->cur_chan == ctx;
397 spin_unlock_bh(&sc->chan_lock);
405 struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
407 struct ath_chanctx *ctx;
409 ath_for_each_chanctx(sc, ctx) {
410 if (!ctx->assigned || list_empty(&ctx->vifs))
412 if (active && !ctx->active)
415 if (ctx->switch_after_beacon)
419 return &sc->chanctx[0];
422 void ath_chanctx_offchan_switch(struct ath_softc *sc,
423 struct ieee80211_channel *chan)
425 struct cfg80211_chan_def chandef;
427 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
429 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
432 static struct ath_chanctx *
433 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
435 int idx = ctx - &sc->chanctx[0];
437 return &sc->chanctx[!idx];
440 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
442 struct ath_chanctx *prev, *cur;
444 u32 cur_tsf, prev_tsf, beacon_int;
447 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
450 prev = ath_chanctx_get_next(sc, cur);
452 getrawmonotonic(&ts);
453 cur_tsf = (u32) cur->tsf_val +
454 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
456 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
457 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
459 /* Adjust the TSF time of the AP chanctx to keep its beacons
460 * at half beacon interval offset relative to the STA chanctx.
462 offset = cur_tsf - prev_tsf;
464 /* Ignore stale data or spurious timestamps */
465 if (offset < 0 || offset > 3 * beacon_int)
468 offset = beacon_int / 2 - (offset % beacon_int);
469 prev->tsf_val += offset;
472 void ath_chanctx_timer(unsigned long data)
474 struct ath_softc *sc = (struct ath_softc *) data;
476 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
479 /* Configure the TSF based hardware timer for a channel switch.
480 * Also set up backup software timer, in case the gen timer fails.
481 * This could be caused by a hardware reset.
483 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
485 struct ath_hw *ah = sc->sc_ah;
487 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
488 tsf_time -= ath9k_hw_gettsf32(ah);
489 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
490 mod_timer(&sc->sched.timer, tsf_time);
493 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
494 enum ath_chanctx_event ev)
496 struct ath_hw *ah = sc->sc_ah;
497 struct ath_common *common = ath9k_hw_common(ah);
498 struct ath_beacon_config *cur_conf;
499 struct ath_vif *avp = NULL;
500 struct ath_chanctx *ctx;
503 bool noa_changed = false;
506 avp = (struct ath_vif *) vif->drv_priv;
508 spin_lock_bh(&sc->chan_lock);
511 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
512 if (avp->offchannel_duration)
513 avp->offchannel_duration = 0;
515 if (avp->chanctx != sc->cur_chan)
518 if (sc->sched.offchannel_pending) {
519 sc->sched.offchannel_pending = false;
520 sc->next_chan = &sc->offchannel.chan;
521 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
524 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
525 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
527 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
530 /* if the timer missed its window, use the next interval */
531 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
532 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
534 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
537 sc->sched.beacon_pending = true;
538 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
540 cur_conf = &sc->cur_chan->beacon;
541 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
543 /* defer channel switch by a quarter beacon interval */
544 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
545 sc->sched.switch_start_time = tsf_time;
546 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
548 /* Prevent wrap-around issues */
549 if (avp->periodic_noa_duration &&
550 tsf_time - avp->periodic_noa_start > BIT(30))
551 avp->periodic_noa_duration = 0;
553 if (ctx->active && !avp->periodic_noa_duration) {
554 avp->periodic_noa_start = tsf_time;
555 avp->periodic_noa_duration =
556 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
557 sc->sched.channel_switch_time;
559 } else if (!ctx->active && avp->periodic_noa_duration) {
560 avp->periodic_noa_duration = 0;
564 /* If at least two consecutive beacons were missed on the STA
565 * chanctx, stay on the STA channel for one extra beacon period,
566 * to resync the timer properly.
568 if (ctx->active && sc->sched.beacon_miss >= 2)
569 sc->sched.offchannel_duration = 3 * beacon_int / 2;
571 if (sc->sched.offchannel_duration) {
573 avp->offchannel_start = tsf_time;
574 avp->offchannel_duration =
575 sc->sched.offchannel_duration;
581 case ATH_CHANCTX_EVENT_BEACON_SENT:
582 if (!sc->sched.beacon_pending)
585 sc->sched.beacon_pending = false;
586 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
589 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
590 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
592 case ATH_CHANCTX_EVENT_TSF_TIMER:
593 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
596 if (!sc->cur_chan->switch_after_beacon &&
597 sc->sched.beacon_pending)
598 sc->sched.beacon_miss++;
600 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
601 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
603 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
604 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
605 sc->cur_chan == &sc->offchannel.chan)
608 ath_chanctx_adjust_tbtt_delta(sc);
609 sc->sched.beacon_pending = false;
610 sc->sched.beacon_miss = 0;
612 /* TSF time might have been updated by the incoming beacon,
613 * need update the channel switch timer to reflect the change.
615 tsf_time = sc->sched.switch_start_time;
616 tsf_time -= (u32) sc->cur_chan->tsf_val +
617 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
618 tsf_time += ath9k_hw_gettsf32(ah);
621 ath_chanctx_setup_timer(sc, tsf_time);
623 case ATH_CHANCTX_EVENT_ASSOC:
624 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
625 avp->chanctx != sc->cur_chan)
628 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
630 case ATH_CHANCTX_EVENT_SWITCH:
631 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
632 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
633 sc->cur_chan->switch_after_beacon ||
634 sc->cur_chan == &sc->offchannel.chan)
637 /* If this is a station chanctx, stay active for a half
638 * beacon period (minus channel switch time)
640 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
641 cur_conf = &sc->cur_chan->beacon;
643 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
645 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
646 if (sc->sched.beacon_miss >= 2) {
647 sc->sched.beacon_miss = 0;
651 tsf_time -= sc->sched.channel_switch_time;
652 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
653 sc->sched.switch_start_time = tsf_time;
655 ath_chanctx_setup_timer(sc, tsf_time);
656 sc->sched.beacon_pending = true;
658 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
659 if (sc->cur_chan == &sc->offchannel.chan ||
660 sc->cur_chan->switch_after_beacon)
663 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
664 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
666 case ATH_CHANCTX_EVENT_UNASSIGN:
667 if (sc->cur_chan->assigned) {
668 if (sc->next_chan && !sc->next_chan->assigned &&
669 sc->next_chan != &sc->offchannel.chan)
670 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
674 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
675 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
680 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
684 spin_unlock_bh(&sc->chan_lock);