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 static const char *offchannel_state_string(enum ath_offchannel_state state)
21 #define case_rtn_string(val) case val: return #val
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);
36 /* 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.
40 static int ath_set_channel(struct ath_softc *sc)
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;
52 if (test_bit(ATH_OP_INVALID, &common->op_flags))
56 old_pos = ah->curchan - &ah->channels[0];
58 ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
59 chan->center_freq, chandef->width);
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);
66 ath9k_cmn_get_channel(hw, ah, chandef);
68 /* If the operating channel changes, change the survey in-use flags
70 * Reset the survey data for the new channel, unless we're switching
71 * back to the operating channel from an off-channel operation.
73 if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
75 sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
77 sc->cur_survey = &sc->survey[pos];
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));
85 hchan = &sc->sc_ah->channels[pos];
86 r = ath_reset_internal(sc, hchan);
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.
95 ath_update_survey_nf(sc, old_pos);
97 /* Enable radar pulse detection if on a DFS channel. Spectral
98 * scanning and radar detection can not be used concurrently.
100 if (hw->conf.radar_enabled) {
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",
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);
122 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
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;
130 int band = sc->cur_chan->chandef.chan->band;
133 case NL80211_IFTYPE_STATION:
134 if (!vif->bss_conf.assoc)
137 skb = ieee80211_nullfunc_get(sc->hw, vif);
141 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
143 nullfunc->frame_control |=
144 cpu_to_le16(IEEE80211_FCTL_PM);
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);
156 memset(&txctl, 0, sizeof(txctl));
157 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
159 txctl.force_channel = true;
160 if (ath_tx_start(sc->hw, skb, &txctl)) {
161 ieee80211_free_txskb(sc->hw, skb);
168 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
170 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
178 list_for_each_entry(avp, &ctx->vifs, list) {
179 struct ieee80211_vif *vif = avp->vif;
182 case NL80211_IFTYPE_P2P_CLIENT:
183 case NL80211_IFTYPE_STATION:
184 if (vif->bss_conf.assoc)
192 ctx->active = active;
194 ath_for_each_chanctx(sc, ctx) {
195 if (!ctx->assigned || list_empty(&ctx->vifs))
201 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
204 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
206 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
210 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
216 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
217 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
225 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
227 if (sc->cur_chan == &sc->offchannel.chan)
230 switch (sc->sched.state) {
231 case ATH_CHANCTX_STATE_SWITCH:
233 case ATH_CHANCTX_STATE_IDLE:
234 if (!sc->cur_chan->switch_after_beacon)
237 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
246 static void ath_chanctx_set_next(struct ath_softc *sc, bool force)
249 bool measure_time = false;
250 bool send_ps = false;
252 spin_lock_bh(&sc->chan_lock);
253 if (!sc->next_chan) {
254 spin_unlock_bh(&sc->chan_lock);
258 if (!force && ath_chanctx_defer_switch(sc)) {
259 spin_unlock_bh(&sc->chan_lock);
263 if (sc->cur_chan != sc->next_chan) {
264 sc->cur_chan->stopped = true;
265 spin_unlock_bh(&sc->chan_lock);
267 if (sc->next_chan == &sc->offchannel.chan) {
268 getrawmonotonic(&ts);
271 __ath9k_flush(sc->hw, ~0, true);
273 if (ath_chanctx_send_ps_frame(sc, true))
274 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
277 spin_lock_bh(&sc->chan_lock);
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);
284 sc->cur_chan = sc->next_chan;
285 sc->cur_chan->stopped = false;
286 sc->next_chan = NULL;
287 sc->sched.offchannel_duration = 0;
288 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
289 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
291 spin_unlock_bh(&sc->chan_lock);
293 if (sc->sc_ah->chip_fullsleep ||
294 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
295 sizeof(sc->cur_chandef))) {
298 sc->sched.channel_switch_time =
299 ath9k_hw_get_tsf_offset(&ts, NULL);
302 ath_chanctx_send_ps_frame(sc, false);
304 ath_offchannel_channel_change(sc);
305 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
308 void ath_chanctx_work(struct work_struct *work)
310 struct ath_softc *sc = container_of(work, struct ath_softc,
312 mutex_lock(&sc->mutex);
313 ath_chanctx_set_next(sc, false);
314 mutex_unlock(&sc->mutex);
317 void ath_chanctx_init(struct ath_softc *sc)
319 struct ath_chanctx *ctx;
320 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
321 struct ieee80211_supported_band *sband;
322 struct ieee80211_channel *chan;
325 sband = &common->sbands[IEEE80211_BAND_2GHZ];
326 if (!sband->n_channels)
327 sband = &common->sbands[IEEE80211_BAND_5GHZ];
329 chan = &sband->channels[0];
330 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
331 ctx = &sc->chanctx[i];
332 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
333 INIT_LIST_HEAD(&ctx->vifs);
334 ctx->txpower = ATH_TXPOWER_MAX;
335 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
336 INIT_LIST_HEAD(&ctx->acq[j]);
338 ctx = &sc->offchannel.chan;
339 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
340 INIT_LIST_HEAD(&ctx->vifs);
341 ctx->txpower = ATH_TXPOWER_MAX;
342 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
343 INIT_LIST_HEAD(&ctx->acq[j]);
344 sc->offchannel.chan.offchannel = true;
348 void ath9k_chanctx_force_active(struct ieee80211_hw *hw,
349 struct ieee80211_vif *vif)
351 struct ath_softc *sc = hw->priv;
352 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
353 struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
354 bool changed = false;
356 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
362 mutex_lock(&sc->mutex);
364 spin_lock_bh(&sc->chan_lock);
365 if (sc->next_chan || (sc->cur_chan != avp->chanctx)) {
366 sc->next_chan = avp->chanctx;
369 sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
370 spin_unlock_bh(&sc->chan_lock);
373 ath_chanctx_set_next(sc, true);
375 mutex_unlock(&sc->mutex);
378 void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
379 struct cfg80211_chan_def *chandef)
381 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
383 spin_lock_bh(&sc->chan_lock);
385 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
386 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
387 sc->sched.offchannel_pending = true;
388 spin_unlock_bh(&sc->chan_lock);
394 ctx->chandef = *chandef;
395 ath_dbg(common, CHAN_CTX,
396 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
399 if (sc->next_chan == &sc->offchannel.chan) {
400 sc->sched.offchannel_duration =
401 TU_TO_USEC(sc->offchannel.duration) +
402 sc->sched.channel_switch_time;
405 ath_dbg(common, CHAN_CTX,
406 "Offchannel duration for chan %d MHz : %u\n",
407 chandef->center_freq1,
408 sc->sched.offchannel_duration);
411 spin_unlock_bh(&sc->chan_lock);
412 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
415 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
416 struct cfg80211_chan_def *chandef)
420 spin_lock_bh(&sc->chan_lock);
422 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
423 cur_chan = sc->cur_chan == ctx;
424 spin_unlock_bh(&sc->chan_lock);
432 struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
434 struct ath_chanctx *ctx;
436 ath_for_each_chanctx(sc, ctx) {
437 if (!ctx->assigned || list_empty(&ctx->vifs))
439 if (active && !ctx->active)
442 if (ctx->switch_after_beacon)
446 return &sc->chanctx[0];
449 void ath_chanctx_offchan_switch(struct ath_softc *sc,
450 struct ieee80211_channel *chan)
452 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
453 struct cfg80211_chan_def chandef;
455 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
456 ath_dbg(common, CHAN_CTX,
457 "Channel definition created: %d MHz\n", chandef.center_freq1);
459 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
462 static struct ath_chanctx *
463 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
465 int idx = ctx - &sc->chanctx[0];
467 return &sc->chanctx[!idx];
470 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
472 struct ath_chanctx *prev, *cur;
474 u32 cur_tsf, prev_tsf, beacon_int;
477 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
480 prev = ath_chanctx_get_next(sc, cur);
482 getrawmonotonic(&ts);
483 cur_tsf = (u32) cur->tsf_val +
484 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
486 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
487 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
489 /* Adjust the TSF time of the AP chanctx to keep its beacons
490 * at half beacon interval offset relative to the STA chanctx.
492 offset = cur_tsf - prev_tsf;
494 /* Ignore stale data or spurious timestamps */
495 if (offset < 0 || offset > 3 * beacon_int)
498 offset = beacon_int / 2 - (offset % beacon_int);
499 prev->tsf_val += offset;
502 void ath_chanctx_timer(unsigned long data)
504 struct ath_softc *sc = (struct ath_softc *) data;
506 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
509 /* Configure the TSF based hardware timer for a channel switch.
510 * Also set up backup software timer, in case the gen timer fails.
511 * This could be caused by a hardware reset.
513 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
515 struct ath_hw *ah = sc->sc_ah;
517 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
518 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
520 tsf_time -= ath9k_hw_gettsf32(ah);
521 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
522 mod_timer(&sc->sched.timer, tsf_time);
525 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
526 enum ath_chanctx_event ev)
528 struct ath_hw *ah = sc->sc_ah;
529 struct ath_common *common = ath9k_hw_common(ah);
530 struct ath_beacon_config *cur_conf;
531 struct ath_vif *avp = NULL;
532 struct ath_chanctx *ctx;
535 bool noa_changed = false;
538 avp = (struct ath_vif *) vif->drv_priv;
540 spin_lock_bh(&sc->chan_lock);
543 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
544 if (avp->offchannel_duration)
545 avp->offchannel_duration = 0;
547 if (avp->chanctx != sc->cur_chan)
550 if (sc->sched.offchannel_pending) {
551 sc->sched.offchannel_pending = false;
552 sc->next_chan = &sc->offchannel.chan;
553 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
556 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
557 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
559 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
562 /* if the timer missed its window, use the next interval */
563 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
564 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
566 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
569 sc->sched.beacon_pending = true;
570 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
572 cur_conf = &sc->cur_chan->beacon;
573 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
575 /* defer channel switch by a quarter beacon interval */
576 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
577 sc->sched.switch_start_time = tsf_time;
578 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
580 /* Prevent wrap-around issues */
581 if (avp->periodic_noa_duration &&
582 tsf_time - avp->periodic_noa_start > BIT(30))
583 avp->periodic_noa_duration = 0;
585 if (ctx->active && !avp->periodic_noa_duration) {
586 avp->periodic_noa_start = tsf_time;
587 avp->periodic_noa_duration =
588 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
589 sc->sched.channel_switch_time;
591 } else if (!ctx->active && avp->periodic_noa_duration) {
592 avp->periodic_noa_duration = 0;
596 /* If at least two consecutive beacons were missed on the STA
597 * chanctx, stay on the STA channel for one extra beacon period,
598 * to resync the timer properly.
600 if (ctx->active && sc->sched.beacon_miss >= 2)
601 sc->sched.offchannel_duration = 3 * beacon_int / 2;
603 if (sc->sched.offchannel_duration) {
605 avp->offchannel_start = tsf_time;
606 avp->offchannel_duration =
607 sc->sched.offchannel_duration;
613 case ATH_CHANCTX_EVENT_BEACON_SENT:
614 if (!sc->sched.beacon_pending)
617 sc->sched.beacon_pending = false;
618 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
621 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
622 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
624 case ATH_CHANCTX_EVENT_TSF_TIMER:
625 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
628 if (!sc->cur_chan->switch_after_beacon &&
629 sc->sched.beacon_pending)
630 sc->sched.beacon_miss++;
632 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
633 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
635 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
636 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
637 sc->cur_chan == &sc->offchannel.chan)
640 ath_chanctx_adjust_tbtt_delta(sc);
641 sc->sched.beacon_pending = false;
642 sc->sched.beacon_miss = 0;
644 /* TSF time might have been updated by the incoming beacon,
645 * need update the channel switch timer to reflect the change.
647 tsf_time = sc->sched.switch_start_time;
648 tsf_time -= (u32) sc->cur_chan->tsf_val +
649 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
650 tsf_time += ath9k_hw_gettsf32(ah);
653 ath_chanctx_setup_timer(sc, tsf_time);
655 case ATH_CHANCTX_EVENT_ASSOC:
656 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
657 avp->chanctx != sc->cur_chan)
660 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
662 case ATH_CHANCTX_EVENT_SWITCH:
663 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
664 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
665 sc->cur_chan->switch_after_beacon ||
666 sc->cur_chan == &sc->offchannel.chan)
669 /* If this is a station chanctx, stay active for a half
670 * beacon period (minus channel switch time)
672 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
673 cur_conf = &sc->cur_chan->beacon;
675 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
677 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
678 if (sc->sched.beacon_miss >= 2) {
679 sc->sched.beacon_miss = 0;
683 tsf_time -= sc->sched.channel_switch_time;
684 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
685 sc->sched.switch_start_time = tsf_time;
687 ath_chanctx_setup_timer(sc, tsf_time);
688 sc->sched.beacon_pending = true;
690 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
691 if (sc->cur_chan == &sc->offchannel.chan ||
692 sc->cur_chan->switch_after_beacon)
695 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
696 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
698 case ATH_CHANCTX_EVENT_UNASSIGN:
699 if (sc->cur_chan->assigned) {
700 if (sc->next_chan && !sc->next_chan->assigned &&
701 sc->next_chan != &sc->offchannel.chan)
702 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
706 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
707 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
712 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
716 spin_unlock_bh(&sc->chan_lock);
719 static int ath_scan_channel_duration(struct ath_softc *sc,
720 struct ieee80211_channel *chan)
722 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
724 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
725 return (HZ / 9); /* ~110 ms */
727 return (HZ / 16); /* ~60 ms */
731 ath_scan_next_channel(struct ath_softc *sc)
733 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
734 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
735 struct ieee80211_channel *chan;
737 if (sc->offchannel.scan_idx >= req->n_channels) {
738 ath_dbg(common, CHAN_CTX,
739 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
740 sc->offchannel.scan_idx,
743 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
744 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
749 ath_dbg(common, CHAN_CTX,
750 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
751 sc->offchannel.scan_idx);
753 chan = req->channels[sc->offchannel.scan_idx++];
754 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
755 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
757 ath_chanctx_offchan_switch(sc, chan);
760 void ath_offchannel_next(struct ath_softc *sc)
762 struct ieee80211_vif *vif;
764 if (sc->offchannel.scan_req) {
765 vif = sc->offchannel.scan_vif;
766 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
767 ath_scan_next_channel(sc);
768 } else if (sc->offchannel.roc_vif) {
769 vif = sc->offchannel.roc_vif;
770 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
771 sc->offchannel.duration = sc->offchannel.roc_duration;
772 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
773 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
775 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
777 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
783 void ath_roc_complete(struct ath_softc *sc, bool abort)
785 sc->offchannel.roc_vif = NULL;
786 sc->offchannel.roc_chan = NULL;
788 ieee80211_remain_on_channel_expired(sc->hw);
789 ath_offchannel_next(sc);
790 ath9k_ps_restore(sc);
793 void ath_scan_complete(struct ath_softc *sc, bool abort)
795 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
798 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
800 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
802 sc->offchannel.scan_req = NULL;
803 sc->offchannel.scan_vif = NULL;
804 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
805 ieee80211_scan_completed(sc->hw, abort);
806 clear_bit(ATH_OP_SCANNING, &common->op_flags);
807 ath_offchannel_next(sc);
808 ath9k_ps_restore(sc);
811 static void ath_scan_send_probe(struct ath_softc *sc,
812 struct cfg80211_ssid *ssid)
814 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
815 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
816 struct ath_tx_control txctl = {};
818 struct ieee80211_tx_info *info;
819 int band = sc->offchannel.chan.chandef.chan->band;
821 skb = ieee80211_probereq_get(sc->hw, vif,
822 ssid->ssid, ssid->ssid_len, req->ie_len);
826 info = IEEE80211_SKB_CB(skb);
828 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
831 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
833 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
835 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
838 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
839 txctl.force_channel = true;
840 if (ath_tx_start(sc->hw, skb, &txctl))
846 ieee80211_free_txskb(sc->hw, skb);
849 static void ath_scan_channel_start(struct ath_softc *sc)
851 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
852 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
855 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
857 for (i = 0; i < req->n_ssids; i++)
858 ath_scan_send_probe(sc, &req->ssids[i]);
862 ath_dbg(common, CHAN_CTX,
863 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
865 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
866 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
869 void ath_offchannel_channel_change(struct ath_softc *sc)
871 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
873 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
874 __func__, offchannel_state_string(sc->offchannel.state));
876 switch (sc->offchannel.state) {
877 case ATH_OFFCHANNEL_PROBE_SEND:
878 if (!sc->offchannel.scan_req)
881 if (sc->cur_chan->chandef.chan !=
882 sc->offchannel.chan.chandef.chan)
885 ath_scan_channel_start(sc);
887 case ATH_OFFCHANNEL_IDLE:
888 if (!sc->offchannel.scan_req)
891 ath_scan_complete(sc, false);
893 case ATH_OFFCHANNEL_ROC_START:
894 if (sc->cur_chan != &sc->offchannel.chan)
897 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
898 mod_timer(&sc->offchannel.timer, jiffies +
899 msecs_to_jiffies(sc->offchannel.duration));
900 ieee80211_ready_on_channel(sc->hw);
902 case ATH_OFFCHANNEL_ROC_DONE:
903 ath_roc_complete(sc, false);
910 void ath_offchannel_timer(unsigned long data)
912 struct ath_softc *sc = (struct ath_softc *)data;
913 struct ath_chanctx *ctx;
914 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
916 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
917 __func__, offchannel_state_string(sc->offchannel.state));
919 switch (sc->offchannel.state) {
920 case ATH_OFFCHANNEL_PROBE_WAIT:
921 if (!sc->offchannel.scan_req)
924 /* get first active channel context */
925 ctx = ath_chanctx_get_oper_chan(sc, true);
927 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
928 ath_chanctx_switch(sc, ctx, NULL);
929 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
933 case ATH_OFFCHANNEL_SUSPEND:
934 if (!sc->offchannel.scan_req)
937 ath_scan_next_channel(sc);
939 case ATH_OFFCHANNEL_ROC_START:
940 case ATH_OFFCHANNEL_ROC_WAIT:
941 ctx = ath_chanctx_get_oper_chan(sc, false);
942 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
943 ath_chanctx_switch(sc, ctx, NULL);
950 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
952 bool ath9k_is_chanctx_enabled(void)
954 return (ath9k_use_chanctx == 1);
961 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
963 struct ath_hw *ah = sc->sc_ah;
966 if (!avp || !avp->noa.has_next_tsf)
969 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
971 tsf = ath9k_hw_gettsf32(sc->sc_ah);
973 target_tsf = avp->noa.next_tsf;
974 if (!avp->noa.absent)
975 target_tsf -= ATH_P2P_PS_STOP_TIME;
977 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
978 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
980 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
983 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
985 struct ath_vif *avp = (void *)vif->drv_priv;
988 if (!sc->p2p_ps_timer)
991 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
994 sc->p2p_ps_vif = avp;
995 tsf = ath9k_hw_gettsf32(sc->sc_ah);
996 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
997 ath9k_update_p2p_ps_timer(sc, avp);
1000 void ath9k_p2p_ps_timer(void *priv)
1002 struct ath_softc *sc = priv;
1003 struct ath_vif *avp = sc->p2p_ps_vif;
1004 struct ieee80211_vif *vif;
1005 struct ieee80211_sta *sta;
1006 struct ath_node *an;
1009 del_timer_sync(&sc->sched.timer);
1010 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1011 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1013 if (!avp || avp->chanctx != sc->cur_chan)
1016 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1017 if (!avp->noa.absent)
1018 tsf += ATH_P2P_PS_STOP_TIME;
1020 if (!avp->noa.has_next_tsf ||
1021 avp->noa.next_tsf - tsf > BIT(31))
1022 ieee80211_update_p2p_noa(&avp->noa, tsf);
1024 ath9k_update_p2p_ps_timer(sc, avp);
1029 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1033 an = (void *) sta->drv_priv;
1034 if (an->sleeping == !!avp->noa.absent)
1037 an->sleeping = avp->noa.absent;
1039 ath_tx_aggr_sleep(sta, sc, an);
1041 ath_tx_aggr_wakeup(sc, an);
1047 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1048 struct ieee80211_vif *vif)
1050 unsigned long flags;
1052 spin_lock_bh(&sc->sc_pcu_lock);
1053 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1054 if (!(sc->ps_flags & PS_BEACON_SYNC))
1055 ath9k_update_p2p_ps(sc, vif);
1056 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1057 spin_unlock_bh(&sc->sc_pcu_lock);
1060 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1063 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1066 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1067 struct ieee80211_vif *vif)
1069 struct ath_vif *avp = (void *)vif->drv_priv;
1071 spin_lock_bh(&sc->sc_pcu_lock);
1072 if (avp == sc->p2p_ps_vif) {
1073 sc->p2p_ps_vif = NULL;
1074 ath9k_update_p2p_ps_timer(sc, NULL);
1076 spin_unlock_bh(&sc->sc_pcu_lock);
1079 int ath9k_init_p2p(struct ath_softc *sc)
1081 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1082 NULL, sc, AR_FIRST_NDP_TIMER);
1083 if (!sc->p2p_ps_timer)
1089 void ath9k_deinit_p2p(struct ath_softc *sc)
1091 if (sc->p2p_ps_timer)
1092 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1095 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */