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_init(struct ath_softc *sc)
106 struct ath_chanctx *ctx;
107 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
108 struct ieee80211_supported_band *sband;
109 struct ieee80211_channel *chan;
112 sband = &common->sbands[IEEE80211_BAND_2GHZ];
113 if (!sband->n_channels)
114 sband = &common->sbands[IEEE80211_BAND_5GHZ];
116 chan = &sband->channels[0];
117 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
118 ctx = &sc->chanctx[i];
119 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
120 INIT_LIST_HEAD(&ctx->vifs);
121 ctx->txpower = ATH_TXPOWER_MAX;
122 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
123 INIT_LIST_HEAD(&ctx->acq[j]);
127 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
128 struct cfg80211_chan_def *chandef)
130 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
133 spin_lock_bh(&sc->chan_lock);
135 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
136 cur_chan = sc->cur_chan == ctx;
137 spin_unlock_bh(&sc->chan_lock);
140 ath_dbg(common, CHAN_CTX,
141 "Current context differs from the new context\n");
148 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
150 /**********************************************************/
151 /* Functions to handle the channel context state machine. */
152 /**********************************************************/
154 static const char *offchannel_state_string(enum ath_offchannel_state state)
157 case_rtn_string(ATH_OFFCHANNEL_IDLE);
158 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
159 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
160 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
161 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
162 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
163 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
169 static const char *chanctx_event_string(enum ath_chanctx_event ev)
172 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
173 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
174 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
175 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
176 case_rtn_string(ATH_CHANCTX_EVENT_ASSOC);
177 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
178 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
179 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
180 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
181 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
187 static const char *chanctx_state_string(enum ath_chanctx_state state)
190 case_rtn_string(ATH_CHANCTX_STATE_IDLE);
191 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
192 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
193 case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
194 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
200 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
202 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
210 list_for_each_entry(avp, &ctx->vifs, list) {
211 struct ieee80211_vif *vif = avp->vif;
214 case NL80211_IFTYPE_P2P_CLIENT:
215 case NL80211_IFTYPE_STATION:
216 if (vif->bss_conf.assoc)
224 ctx->active = active;
226 ath_for_each_chanctx(sc, ctx) {
227 if (!ctx->assigned || list_empty(&ctx->vifs))
233 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
236 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
239 if (ath9k_is_chanctx_enabled()) {
240 ath_chanctx_event(sc, NULL,
241 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
245 static struct ath_chanctx *
246 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
248 int idx = ctx - &sc->chanctx[0];
250 return &sc->chanctx[!idx];
253 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
255 struct ath_chanctx *prev, *cur;
257 u32 cur_tsf, prev_tsf, beacon_int;
260 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
263 prev = ath_chanctx_get_next(sc, cur);
265 getrawmonotonic(&ts);
266 cur_tsf = (u32) cur->tsf_val +
267 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
269 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
270 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
272 /* Adjust the TSF time of the AP chanctx to keep its beacons
273 * at half beacon interval offset relative to the STA chanctx.
275 offset = cur_tsf - prev_tsf;
277 /* Ignore stale data or spurious timestamps */
278 if (offset < 0 || offset > 3 * beacon_int)
281 offset = beacon_int / 2 - (offset % beacon_int);
282 prev->tsf_val += offset;
285 /* Configure the TSF based hardware timer for a channel switch.
286 * Also set up backup software timer, in case the gen timer fails.
287 * This could be caused by a hardware reset.
289 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
291 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
292 struct ath_hw *ah = sc->sc_ah;
294 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
295 tsf_time -= ath9k_hw_gettsf32(ah);
296 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
297 mod_timer(&sc->sched.timer, jiffies + tsf_time);
299 ath_dbg(common, CHAN_CTX,
300 "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time));
303 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
304 enum ath_chanctx_event ev)
306 struct ath_hw *ah = sc->sc_ah;
307 struct ath_common *common = ath9k_hw_common(ah);
308 struct ath_beacon_config *cur_conf;
309 struct ath_vif *avp = NULL;
310 struct ath_chanctx *ctx;
313 bool noa_changed = false;
316 avp = (struct ath_vif *) vif->drv_priv;
318 spin_lock_bh(&sc->chan_lock);
320 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
321 sc->cur_chan->chandef.center_freq1,
322 chanctx_event_string(ev),
323 chanctx_state_string(sc->sched.state));
326 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
327 if (avp->offchannel_duration)
328 avp->offchannel_duration = 0;
330 if (avp->chanctx != sc->cur_chan) {
331 ath_dbg(common, CHAN_CTX,
332 "Contexts differ, not preparing beacon\n");
336 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
337 sc->sched.offchannel_pending = false;
338 sc->next_chan = &sc->offchannel.chan;
339 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
340 ath_dbg(common, CHAN_CTX,
341 "Setting offchannel_pending to false\n");
344 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
345 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
347 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
348 ath_dbg(common, CHAN_CTX,
349 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
352 /* if the timer missed its window, use the next interval */
353 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
354 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
355 ath_dbg(common, CHAN_CTX,
356 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
359 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
362 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
364 sc->sched.beacon_pending = true;
365 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
367 cur_conf = &sc->cur_chan->beacon;
368 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
370 /* defer channel switch by a quarter beacon interval */
371 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
372 sc->sched.switch_start_time = tsf_time;
373 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
375 /* Prevent wrap-around issues */
376 if (avp->periodic_noa_duration &&
377 tsf_time - avp->periodic_noa_start > BIT(30))
378 avp->periodic_noa_duration = 0;
381 avp->periodic_noa_start = tsf_time;
382 avp->periodic_noa_duration =
383 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
384 sc->sched.channel_switch_time;
386 } else if (!ctx->active) {
387 avp->periodic_noa_duration = 0;
391 /* If at least two consecutive beacons were missed on the STA
392 * chanctx, stay on the STA channel for one extra beacon period,
393 * to resync the timer properly.
395 if (ctx->active && sc->sched.beacon_miss >= 2)
396 sc->sched.offchannel_duration = 3 * beacon_int / 2;
398 if (sc->sched.offchannel_duration) {
400 avp->offchannel_start = tsf_time;
401 avp->offchannel_duration =
402 sc->sched.offchannel_duration;
408 ath_dbg(common, CHAN_CTX,
409 "periodic_noa_duration: %d, periodic_noa_start: %d, noa_index: %d\n",
410 avp->periodic_noa_duration,
411 avp->periodic_noa_start,
415 case ATH_CHANCTX_EVENT_BEACON_SENT:
416 if (!sc->sched.beacon_pending) {
417 ath_dbg(common, CHAN_CTX,
418 "No pending beacon\n");
422 sc->sched.beacon_pending = false;
423 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
426 ath_dbg(common, CHAN_CTX,
427 "Move chanctx state to WAIT_FOR_TIMER\n");
429 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
430 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
432 case ATH_CHANCTX_EVENT_TSF_TIMER:
433 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
436 if (!sc->cur_chan->switch_after_beacon &&
437 sc->sched.beacon_pending)
438 sc->sched.beacon_miss++;
440 ath_dbg(common, CHAN_CTX,
441 "Move chanctx state to SWITCH\n");
443 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
444 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
446 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
447 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
448 sc->cur_chan == &sc->offchannel.chan)
451 ath_chanctx_adjust_tbtt_delta(sc);
452 sc->sched.beacon_pending = false;
453 sc->sched.beacon_miss = 0;
455 /* TSF time might have been updated by the incoming beacon,
456 * need update the channel switch timer to reflect the change.
458 tsf_time = sc->sched.switch_start_time;
459 tsf_time -= (u32) sc->cur_chan->tsf_val +
460 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
461 tsf_time += ath9k_hw_gettsf32(ah);
464 ath_chanctx_setup_timer(sc, tsf_time);
466 case ATH_CHANCTX_EVENT_ASSOC:
467 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
468 avp->chanctx != sc->cur_chan)
471 ath_dbg(common, CHAN_CTX,
472 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
474 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
476 case ATH_CHANCTX_EVENT_SWITCH:
477 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
478 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
479 sc->cur_chan->switch_after_beacon ||
480 sc->cur_chan == &sc->offchannel.chan)
483 /* If this is a station chanctx, stay active for a half
484 * beacon period (minus channel switch time)
486 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
487 cur_conf = &sc->cur_chan->beacon;
489 ath_dbg(common, CHAN_CTX,
490 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
492 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
493 sc->sched.wait_switch = false;
495 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
496 if (sc->sched.beacon_miss >= 2) {
497 sc->sched.beacon_miss = 0;
501 tsf_time -= sc->sched.channel_switch_time;
502 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
503 sc->sched.switch_start_time = tsf_time;
505 ath_chanctx_setup_timer(sc, tsf_time);
506 sc->sched.beacon_pending = true;
508 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
509 if (sc->cur_chan == &sc->offchannel.chan ||
510 sc->cur_chan->switch_after_beacon)
513 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
514 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
516 case ATH_CHANCTX_EVENT_UNASSIGN:
517 if (sc->cur_chan->assigned) {
518 if (sc->next_chan && !sc->next_chan->assigned &&
519 sc->next_chan != &sc->offchannel.chan)
520 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
524 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
525 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
530 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
532 case ATH_CHANCTX_EVENT_ASSIGN:
534 * When adding a new channel context, check if a scan
535 * is in progress and abort it since the addition of
536 * a new channel context is usually followed by VIF
537 * assignment, in which case we have to start multi-channel
540 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
541 ath_dbg(common, CHAN_CTX,
542 "Aborting HW scan to add new context\n");
544 spin_unlock_bh(&sc->chan_lock);
545 del_timer_sync(&sc->offchannel.timer);
546 ath_scan_complete(sc, true);
547 spin_lock_bh(&sc->chan_lock);
550 case ATH_CHANCTX_EVENT_CHANGE:
554 spin_unlock_bh(&sc->chan_lock);
557 void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
558 enum ath_chanctx_event ev)
560 if (sc->sched.beacon_pending)
561 ath_chanctx_event(sc, NULL, ev);
564 void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, u32 ts,
565 enum ath_chanctx_event ev)
567 sc->sched.next_tbtt = ts;
568 ath_chanctx_event(sc, NULL, ev);
571 static int ath_scan_channel_duration(struct ath_softc *sc,
572 struct ieee80211_channel *chan)
574 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
576 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
577 return (HZ / 9); /* ~110 ms */
579 return (HZ / 16); /* ~60 ms */
582 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
583 struct cfg80211_chan_def *chandef)
585 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
587 spin_lock_bh(&sc->chan_lock);
589 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
590 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
592 ctx->chandef = *chandef;
594 sc->sched.offchannel_pending = true;
595 sc->sched.wait_switch = true;
596 sc->sched.offchannel_duration =
597 jiffies_to_usecs(sc->offchannel.duration) +
598 sc->sched.channel_switch_time;
600 spin_unlock_bh(&sc->chan_lock);
601 ath_dbg(common, CHAN_CTX,
602 "Set offchannel_pending to true\n");
608 ctx->chandef = *chandef;
609 ath_dbg(common, CHAN_CTX,
610 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
613 if (sc->next_chan == &sc->offchannel.chan) {
614 sc->sched.offchannel_duration =
615 jiffies_to_usecs(sc->offchannel.duration) +
616 sc->sched.channel_switch_time;
619 ath_dbg(common, CHAN_CTX,
620 "Offchannel duration for chan %d MHz : %u\n",
621 chandef->center_freq1,
622 sc->sched.offchannel_duration);
625 spin_unlock_bh(&sc->chan_lock);
626 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
629 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
630 struct ieee80211_channel *chan)
632 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
633 struct cfg80211_chan_def chandef;
635 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
636 ath_dbg(common, CHAN_CTX,
637 "Channel definition created: %d MHz\n", chandef.center_freq1);
639 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
642 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
645 struct ath_chanctx *ctx;
647 ath_for_each_chanctx(sc, ctx) {
648 if (!ctx->assigned || list_empty(&ctx->vifs))
650 if (active && !ctx->active)
653 if (ctx->switch_after_beacon)
657 return &sc->chanctx[0];
661 ath_scan_next_channel(struct ath_softc *sc)
663 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
664 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
665 struct ieee80211_channel *chan;
667 if (sc->offchannel.scan_idx >= req->n_channels) {
668 ath_dbg(common, CHAN_CTX,
669 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
670 "scan_idx: %d, n_channels: %d\n",
671 sc->offchannel.scan_idx,
674 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
675 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
680 ath_dbg(common, CHAN_CTX,
681 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
682 sc->offchannel.scan_idx);
684 chan = req->channels[sc->offchannel.scan_idx++];
685 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
686 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
688 ath_chanctx_offchan_switch(sc, chan);
691 void ath_offchannel_next(struct ath_softc *sc)
693 struct ieee80211_vif *vif;
695 if (sc->offchannel.scan_req) {
696 vif = sc->offchannel.scan_vif;
697 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
698 ath_scan_next_channel(sc);
699 } else if (sc->offchannel.roc_vif) {
700 vif = sc->offchannel.roc_vif;
701 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
702 sc->offchannel.duration =
703 msecs_to_jiffies(sc->offchannel.roc_duration);
704 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
705 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
707 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
709 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
715 void ath_roc_complete(struct ath_softc *sc, bool abort)
717 sc->offchannel.roc_vif = NULL;
718 sc->offchannel.roc_chan = NULL;
720 ieee80211_remain_on_channel_expired(sc->hw);
721 ath_offchannel_next(sc);
722 ath9k_ps_restore(sc);
725 void ath_scan_complete(struct ath_softc *sc, bool abort)
727 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
730 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
732 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
734 sc->offchannel.scan_req = NULL;
735 sc->offchannel.scan_vif = NULL;
736 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
737 ieee80211_scan_completed(sc->hw, abort);
738 clear_bit(ATH_OP_SCANNING, &common->op_flags);
739 ath_offchannel_next(sc);
740 ath9k_ps_restore(sc);
743 static void ath_scan_send_probe(struct ath_softc *sc,
744 struct cfg80211_ssid *ssid)
746 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
747 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
748 struct ath_tx_control txctl = {};
750 struct ieee80211_tx_info *info;
751 int band = sc->offchannel.chan.chandef.chan->band;
753 skb = ieee80211_probereq_get(sc->hw, vif,
754 ssid->ssid, ssid->ssid_len, req->ie_len);
758 info = IEEE80211_SKB_CB(skb);
760 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
763 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
765 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
767 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
770 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
771 txctl.force_channel = true;
772 if (ath_tx_start(sc->hw, skb, &txctl))
778 ieee80211_free_txskb(sc->hw, skb);
781 static void ath_scan_channel_start(struct ath_softc *sc)
783 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
784 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
787 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
789 for (i = 0; i < req->n_ssids; i++)
790 ath_scan_send_probe(sc, &req->ssids[i]);
794 ath_dbg(common, CHAN_CTX,
795 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
797 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
798 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
801 static void ath_chanctx_timer(unsigned long data)
803 struct ath_softc *sc = (struct ath_softc *) data;
804 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
806 ath_dbg(common, CHAN_CTX,
807 "Channel context timer invoked\n");
809 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
812 static void ath_offchannel_timer(unsigned long data)
814 struct ath_softc *sc = (struct ath_softc *)data;
815 struct ath_chanctx *ctx;
816 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
818 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
819 __func__, offchannel_state_string(sc->offchannel.state));
821 switch (sc->offchannel.state) {
822 case ATH_OFFCHANNEL_PROBE_WAIT:
823 if (!sc->offchannel.scan_req)
826 /* get first active channel context */
827 ctx = ath_chanctx_get_oper_chan(sc, true);
829 ath_dbg(common, CHAN_CTX,
830 "Switch to oper/active context, "
831 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
833 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
834 ath_chanctx_switch(sc, ctx, NULL);
835 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
839 case ATH_OFFCHANNEL_SUSPEND:
840 if (!sc->offchannel.scan_req)
843 ath_scan_next_channel(sc);
845 case ATH_OFFCHANNEL_ROC_START:
846 case ATH_OFFCHANNEL_ROC_WAIT:
847 ctx = ath_chanctx_get_oper_chan(sc, false);
848 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
849 ath_chanctx_switch(sc, ctx, NULL);
857 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
860 struct ieee80211_vif *vif = avp->vif;
861 struct ieee80211_sta *sta = NULL;
862 struct ieee80211_hdr_3addr *nullfunc;
863 struct ath_tx_control txctl;
865 int band = sc->cur_chan->chandef.chan->band;
868 case NL80211_IFTYPE_STATION:
869 if (!vif->bss_conf.assoc)
872 skb = ieee80211_nullfunc_get(sc->hw, vif);
876 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
878 nullfunc->frame_control |=
879 cpu_to_le16(IEEE80211_FCTL_PM);
881 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
882 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
883 dev_kfree_skb_any(skb);
891 memset(&txctl, 0, sizeof(txctl));
892 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
894 txctl.force_channel = true;
895 if (ath_tx_start(sc->hw, skb, &txctl)) {
896 ieee80211_free_txskb(sc->hw, skb);
904 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
910 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
911 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
919 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
921 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
923 if (sc->cur_chan == &sc->offchannel.chan)
926 switch (sc->sched.state) {
927 case ATH_CHANCTX_STATE_SWITCH:
929 case ATH_CHANCTX_STATE_IDLE:
930 if (!sc->cur_chan->switch_after_beacon)
933 ath_dbg(common, CHAN_CTX,
934 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
936 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
945 static void ath_offchannel_channel_change(struct ath_softc *sc)
947 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
949 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
950 __func__, offchannel_state_string(sc->offchannel.state));
952 switch (sc->offchannel.state) {
953 case ATH_OFFCHANNEL_PROBE_SEND:
954 if (!sc->offchannel.scan_req)
957 if (sc->cur_chan->chandef.chan !=
958 sc->offchannel.chan.chandef.chan)
961 ath_scan_channel_start(sc);
963 case ATH_OFFCHANNEL_IDLE:
964 if (!sc->offchannel.scan_req)
967 ath_scan_complete(sc, false);
969 case ATH_OFFCHANNEL_ROC_START:
970 if (sc->cur_chan != &sc->offchannel.chan)
973 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
974 mod_timer(&sc->offchannel.timer,
975 jiffies + sc->offchannel.duration);
976 ieee80211_ready_on_channel(sc->hw);
978 case ATH_OFFCHANNEL_ROC_DONE:
979 ath_roc_complete(sc, false);
986 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
988 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
990 bool measure_time = false;
991 bool send_ps = false;
993 spin_lock_bh(&sc->chan_lock);
994 if (!sc->next_chan) {
995 spin_unlock_bh(&sc->chan_lock);
999 if (!force && ath_chanctx_defer_switch(sc)) {
1000 spin_unlock_bh(&sc->chan_lock);
1004 ath_dbg(common, CHAN_CTX,
1005 "%s: current: %d MHz, next: %d MHz\n",
1007 sc->cur_chan->chandef.center_freq1,
1008 sc->next_chan->chandef.center_freq1);
1010 if (sc->cur_chan != sc->next_chan) {
1011 ath_dbg(common, CHAN_CTX,
1012 "Stopping current chanctx: %d\n",
1013 sc->cur_chan->chandef.center_freq1);
1014 sc->cur_chan->stopped = true;
1015 spin_unlock_bh(&sc->chan_lock);
1017 if (sc->next_chan == &sc->offchannel.chan) {
1018 getrawmonotonic(&ts);
1019 measure_time = true;
1021 __ath9k_flush(sc->hw, ~0, true);
1023 if (ath_chanctx_send_ps_frame(sc, true))
1024 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
1027 spin_lock_bh(&sc->chan_lock);
1029 if (sc->cur_chan != &sc->offchannel.chan) {
1030 getrawmonotonic(&sc->cur_chan->tsf_ts);
1031 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1034 sc->cur_chan = sc->next_chan;
1035 sc->cur_chan->stopped = false;
1036 sc->next_chan = NULL;
1037 sc->sched.offchannel_duration = 0;
1038 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1039 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1041 spin_unlock_bh(&sc->chan_lock);
1043 if (sc->sc_ah->chip_fullsleep ||
1044 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1045 sizeof(sc->cur_chandef))) {
1046 ath_dbg(common, CHAN_CTX,
1047 "%s: Set channel %d MHz\n",
1048 __func__, sc->cur_chan->chandef.center_freq1);
1049 ath_set_channel(sc);
1051 sc->sched.channel_switch_time =
1052 ath9k_hw_get_tsf_offset(&ts, NULL);
1055 ath_chanctx_send_ps_frame(sc, false);
1057 ath_offchannel_channel_change(sc);
1058 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1061 static void ath_chanctx_work(struct work_struct *work)
1063 struct ath_softc *sc = container_of(work, struct ath_softc,
1065 mutex_lock(&sc->mutex);
1066 ath_chanctx_set_next(sc, false);
1067 mutex_unlock(&sc->mutex);
1070 void ath9k_offchannel_init(struct ath_softc *sc)
1072 struct ath_chanctx *ctx;
1073 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1074 struct ieee80211_supported_band *sband;
1075 struct ieee80211_channel *chan;
1078 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1079 if (!sband->n_channels)
1080 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1082 chan = &sband->channels[0];
1084 ctx = &sc->offchannel.chan;
1085 INIT_LIST_HEAD(&ctx->vifs);
1086 ctx->txpower = ATH_TXPOWER_MAX;
1087 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1089 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1090 INIT_LIST_HEAD(&ctx->acq[i]);
1092 sc->offchannel.chan.offchannel = true;
1095 void ath9k_init_channel_context(struct ath_softc *sc)
1097 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1099 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1101 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1105 void ath9k_deinit_channel_context(struct ath_softc *sc)
1107 cancel_work_sync(&sc->chanctx_work);
1110 bool ath9k_is_chanctx_enabled(void)
1112 return (ath9k_use_chanctx == 1);
1115 /********************/
1116 /* Queue management */
1117 /********************/
1119 void ath9k_chanctx_wake_queues(struct ath_softc *sc)
1121 struct ath_hw *ah = sc->sc_ah;
1124 if (sc->cur_chan == &sc->offchannel.chan) {
1125 ieee80211_wake_queue(sc->hw,
1126 sc->hw->offchannel_tx_hw_queue);
1128 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1129 ieee80211_wake_queue(sc->hw,
1130 sc->cur_chan->hw_queue_base + i);
1133 if (ah->opmode == NL80211_IFTYPE_AP)
1134 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1141 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1143 struct ath_hw *ah = sc->sc_ah;
1144 s32 tsf, target_tsf;
1146 if (!avp || !avp->noa.has_next_tsf)
1149 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1151 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1153 target_tsf = avp->noa.next_tsf;
1154 if (!avp->noa.absent)
1155 target_tsf -= ATH_P2P_PS_STOP_TIME;
1157 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1158 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1160 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1163 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1165 struct ath_vif *avp = (void *)vif->drv_priv;
1168 if (!sc->p2p_ps_timer)
1171 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1174 sc->p2p_ps_vif = avp;
1175 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1176 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1177 ath9k_update_p2p_ps_timer(sc, avp);
1180 static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1182 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1183 u8 switch_time, ctwin;
1186 * Channel switch in multi-channel mode is deferred
1187 * by a quarter beacon interval when handling
1188 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1189 * interface is guaranteed to be discoverable
1190 * for that duration after a TBTT.
1192 switch_time = cur_conf->beacon_interval / 4;
1194 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1195 if (ctwin && (ctwin < switch_time))
1198 if (switch_time < P2P_DEFAULT_CTWIN)
1201 return P2P_DEFAULT_CTWIN;
1204 void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1205 struct sk_buff *skb)
1207 static const u8 noa_ie_hdr[] = {
1208 WLAN_EID_VENDOR_SPECIFIC, /* type */
1210 0x50, 0x6f, 0x9a, /* WFA OUI */
1211 0x09, /* P2P subtype */
1212 0x0c, /* Notice of Absence */
1213 0x00, /* LSB of little-endian len */
1214 0x00, /* MSB of little-endian len */
1217 struct ieee80211_p2p_noa_attr *noa;
1218 int noa_len, noa_desc, i = 0;
1221 if (!avp->offchannel_duration && !avp->periodic_noa_duration)
1224 noa_desc = !!avp->offchannel_duration + !!avp->periodic_noa_duration;
1225 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1227 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1228 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1229 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1232 noa = (void *) skb_put(skb, noa_len);
1233 memset(noa, 0, noa_len);
1235 noa->index = avp->noa_index;
1236 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1238 if (avp->periodic_noa_duration) {
1239 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1241 noa->desc[i].count = 255;
1242 noa->desc[i].start_time = cpu_to_le32(avp->periodic_noa_start);
1243 noa->desc[i].duration = cpu_to_le32(avp->periodic_noa_duration);
1244 noa->desc[i].interval = cpu_to_le32(interval);
1248 if (avp->offchannel_duration) {
1249 noa->desc[i].count = 1;
1250 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1251 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1255 void ath9k_p2p_ps_timer(void *priv)
1257 struct ath_softc *sc = priv;
1258 struct ath_vif *avp = sc->p2p_ps_vif;
1259 struct ieee80211_vif *vif;
1260 struct ieee80211_sta *sta;
1261 struct ath_node *an;
1264 del_timer_sync(&sc->sched.timer);
1265 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1266 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1268 if (!avp || avp->chanctx != sc->cur_chan)
1271 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1272 if (!avp->noa.absent)
1273 tsf += ATH_P2P_PS_STOP_TIME;
1275 if (!avp->noa.has_next_tsf ||
1276 avp->noa.next_tsf - tsf > BIT(31))
1277 ieee80211_update_p2p_noa(&avp->noa, tsf);
1279 ath9k_update_p2p_ps_timer(sc, avp);
1284 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1288 an = (void *) sta->drv_priv;
1289 if (an->sleeping == !!avp->noa.absent)
1292 an->sleeping = avp->noa.absent;
1294 ath_tx_aggr_sleep(sta, sc, an);
1296 ath_tx_aggr_wakeup(sc, an);
1302 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1303 struct ieee80211_vif *vif)
1305 unsigned long flags;
1307 spin_lock_bh(&sc->sc_pcu_lock);
1308 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1309 if (!(sc->ps_flags & PS_BEACON_SYNC))
1310 ath9k_update_p2p_ps(sc, vif);
1311 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1312 spin_unlock_bh(&sc->sc_pcu_lock);
1315 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1318 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1321 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1322 struct ieee80211_vif *vif)
1324 struct ath_vif *avp = (void *)vif->drv_priv;
1326 spin_lock_bh(&sc->sc_pcu_lock);
1327 if (avp == sc->p2p_ps_vif) {
1328 sc->p2p_ps_vif = NULL;
1329 ath9k_update_p2p_ps_timer(sc, NULL);
1331 spin_unlock_bh(&sc->sc_pcu_lock);
1334 int ath9k_init_p2p(struct ath_softc *sc)
1336 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1337 NULL, sc, AR_FIRST_NDP_TIMER);
1338 if (!sc->p2p_ps_timer)
1344 void ath9k_deinit_p2p(struct ath_softc *sc)
1346 if (sc->p2p_ps_timer)
1347 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1350 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */