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))
142 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
145 void ath_chanctx_init(struct ath_softc *sc)
147 struct ath_chanctx *ctx;
148 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
149 struct ieee80211_supported_band *sband;
150 struct ieee80211_channel *chan;
153 sband = &common->sbands[IEEE80211_BAND_2GHZ];
154 if (!sband->n_channels)
155 sband = &common->sbands[IEEE80211_BAND_5GHZ];
157 chan = &sband->channels[0];
158 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
159 ctx = &sc->chanctx[i];
160 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
161 INIT_LIST_HEAD(&ctx->vifs);
162 ctx->txpower = ATH_TXPOWER_MAX;
163 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
164 INIT_LIST_HEAD(&ctx->acq[j]);
166 ctx = &sc->offchannel.chan;
167 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
168 INIT_LIST_HEAD(&ctx->vifs);
169 ctx->txpower = ATH_TXPOWER_MAX;
170 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
171 INIT_LIST_HEAD(&ctx->acq[j]);
172 sc->offchannel.chan.offchannel = true;
176 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
177 struct cfg80211_chan_def *chandef)
181 spin_lock_bh(&sc->chan_lock);
183 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
184 cur_chan = sc->cur_chan == ctx;
185 spin_unlock_bh(&sc->chan_lock);
193 static struct ath_chanctx *
194 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
196 int idx = ctx - &sc->chanctx[0];
198 return &sc->chanctx[!idx];
201 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
203 struct ath_chanctx *prev, *cur;
205 u32 cur_tsf, prev_tsf, beacon_int;
208 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
211 prev = ath_chanctx_get_next(sc, cur);
213 getrawmonotonic(&ts);
214 cur_tsf = (u32) cur->tsf_val +
215 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
217 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
218 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
220 /* Adjust the TSF time of the AP chanctx to keep its beacons
221 * at half beacon interval offset relative to the STA chanctx.
223 offset = cur_tsf - prev_tsf;
225 /* Ignore stale data or spurious timestamps */
226 if (offset < 0 || offset > 3 * beacon_int)
229 offset = beacon_int / 2 - (offset % beacon_int);
230 prev->tsf_val += offset;
233 /* Configure the TSF based hardware timer for a channel switch.
234 * Also set up backup software timer, in case the gen timer fails.
235 * This could be caused by a hardware reset.
237 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
239 struct ath_hw *ah = sc->sc_ah;
241 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
242 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
244 tsf_time -= ath9k_hw_gettsf32(ah);
245 tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1;
246 mod_timer(&sc->sched.timer, tsf_time);
249 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
250 enum ath_chanctx_event ev)
252 struct ath_hw *ah = sc->sc_ah;
253 struct ath_common *common = ath9k_hw_common(ah);
254 struct ath_beacon_config *cur_conf;
255 struct ath_vif *avp = NULL;
256 struct ath_chanctx *ctx;
259 bool noa_changed = false;
262 avp = (struct ath_vif *) vif->drv_priv;
264 spin_lock_bh(&sc->chan_lock);
267 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
268 if (avp->offchannel_duration)
269 avp->offchannel_duration = 0;
271 if (avp->chanctx != sc->cur_chan)
274 if (sc->sched.offchannel_pending) {
275 sc->sched.offchannel_pending = false;
276 sc->next_chan = &sc->offchannel.chan;
277 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
280 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
281 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
283 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
286 /* if the timer missed its window, use the next interval */
287 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
288 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
290 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
293 sc->sched.beacon_pending = true;
294 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
296 cur_conf = &sc->cur_chan->beacon;
297 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
299 /* defer channel switch by a quarter beacon interval */
300 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
301 sc->sched.switch_start_time = tsf_time;
302 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
304 /* Prevent wrap-around issues */
305 if (avp->periodic_noa_duration &&
306 tsf_time - avp->periodic_noa_start > BIT(30))
307 avp->periodic_noa_duration = 0;
309 if (ctx->active && !avp->periodic_noa_duration) {
310 avp->periodic_noa_start = tsf_time;
311 avp->periodic_noa_duration =
312 TU_TO_USEC(cur_conf->beacon_interval) / 2 -
313 sc->sched.channel_switch_time;
315 } else if (!ctx->active && avp->periodic_noa_duration) {
316 avp->periodic_noa_duration = 0;
320 /* If at least two consecutive beacons were missed on the STA
321 * chanctx, stay on the STA channel for one extra beacon period,
322 * to resync the timer properly.
324 if (ctx->active && sc->sched.beacon_miss >= 2)
325 sc->sched.offchannel_duration = 3 * beacon_int / 2;
327 if (sc->sched.offchannel_duration) {
329 avp->offchannel_start = tsf_time;
330 avp->offchannel_duration =
331 sc->sched.offchannel_duration;
337 case ATH_CHANCTX_EVENT_BEACON_SENT:
338 if (!sc->sched.beacon_pending)
341 sc->sched.beacon_pending = false;
342 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
345 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
346 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
348 case ATH_CHANCTX_EVENT_TSF_TIMER:
349 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
352 if (!sc->cur_chan->switch_after_beacon &&
353 sc->sched.beacon_pending)
354 sc->sched.beacon_miss++;
356 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
357 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
359 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
360 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
361 sc->cur_chan == &sc->offchannel.chan)
364 ath_chanctx_adjust_tbtt_delta(sc);
365 sc->sched.beacon_pending = false;
366 sc->sched.beacon_miss = 0;
368 /* TSF time might have been updated by the incoming beacon,
369 * need update the channel switch timer to reflect the change.
371 tsf_time = sc->sched.switch_start_time;
372 tsf_time -= (u32) sc->cur_chan->tsf_val +
373 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
374 tsf_time += ath9k_hw_gettsf32(ah);
377 ath_chanctx_setup_timer(sc, tsf_time);
379 case ATH_CHANCTX_EVENT_ASSOC:
380 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
381 avp->chanctx != sc->cur_chan)
384 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
386 case ATH_CHANCTX_EVENT_SWITCH:
387 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
388 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
389 sc->cur_chan->switch_after_beacon ||
390 sc->cur_chan == &sc->offchannel.chan)
393 /* If this is a station chanctx, stay active for a half
394 * beacon period (minus channel switch time)
396 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
397 cur_conf = &sc->cur_chan->beacon;
399 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
401 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
402 if (sc->sched.beacon_miss >= 2) {
403 sc->sched.beacon_miss = 0;
407 tsf_time -= sc->sched.channel_switch_time;
408 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
409 sc->sched.switch_start_time = tsf_time;
411 ath_chanctx_setup_timer(sc, tsf_time);
412 sc->sched.beacon_pending = true;
414 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
415 if (sc->cur_chan == &sc->offchannel.chan ||
416 sc->cur_chan->switch_after_beacon)
419 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
420 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
422 case ATH_CHANCTX_EVENT_UNASSIGN:
423 if (sc->cur_chan->assigned) {
424 if (sc->next_chan && !sc->next_chan->assigned &&
425 sc->next_chan != &sc->offchannel.chan)
426 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
430 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
431 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
436 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
440 spin_unlock_bh(&sc->chan_lock);
443 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
445 static const char *offchannel_state_string(enum ath_offchannel_state state)
447 #define case_rtn_string(val) case val: return #val
450 case_rtn_string(ATH_OFFCHANNEL_IDLE);
451 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
452 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
453 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
454 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
455 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
456 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
462 static int ath_scan_channel_duration(struct ath_softc *sc,
463 struct ieee80211_channel *chan)
465 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
467 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
468 return (HZ / 9); /* ~110 ms */
470 return (HZ / 16); /* ~60 ms */
473 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
474 struct cfg80211_chan_def *chandef)
476 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
478 spin_lock_bh(&sc->chan_lock);
480 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
481 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
482 sc->sched.offchannel_pending = true;
483 spin_unlock_bh(&sc->chan_lock);
489 ctx->chandef = *chandef;
490 ath_dbg(common, CHAN_CTX,
491 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
494 if (sc->next_chan == &sc->offchannel.chan) {
495 sc->sched.offchannel_duration =
496 TU_TO_USEC(sc->offchannel.duration) +
497 sc->sched.channel_switch_time;
500 ath_dbg(common, CHAN_CTX,
501 "Offchannel duration for chan %d MHz : %u\n",
502 chandef->center_freq1,
503 sc->sched.offchannel_duration);
506 spin_unlock_bh(&sc->chan_lock);
507 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
510 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
511 struct ieee80211_channel *chan)
513 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
514 struct cfg80211_chan_def chandef;
516 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
517 ath_dbg(common, CHAN_CTX,
518 "Channel definition created: %d MHz\n", chandef.center_freq1);
520 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
523 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
526 struct ath_chanctx *ctx;
528 ath_for_each_chanctx(sc, ctx) {
529 if (!ctx->assigned || list_empty(&ctx->vifs))
531 if (active && !ctx->active)
534 if (ctx->switch_after_beacon)
538 return &sc->chanctx[0];
542 ath_scan_next_channel(struct ath_softc *sc)
544 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
545 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
546 struct ieee80211_channel *chan;
548 if (sc->offchannel.scan_idx >= req->n_channels) {
549 ath_dbg(common, CHAN_CTX,
550 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
551 sc->offchannel.scan_idx,
554 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
555 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
560 ath_dbg(common, CHAN_CTX,
561 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
562 sc->offchannel.scan_idx);
564 chan = req->channels[sc->offchannel.scan_idx++];
565 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
566 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
568 ath_chanctx_offchan_switch(sc, chan);
571 void ath_offchannel_next(struct ath_softc *sc)
573 struct ieee80211_vif *vif;
575 if (sc->offchannel.scan_req) {
576 vif = sc->offchannel.scan_vif;
577 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
578 ath_scan_next_channel(sc);
579 } else if (sc->offchannel.roc_vif) {
580 vif = sc->offchannel.roc_vif;
581 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
582 sc->offchannel.duration = sc->offchannel.roc_duration;
583 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
584 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
586 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
588 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
594 void ath_roc_complete(struct ath_softc *sc, bool abort)
596 sc->offchannel.roc_vif = NULL;
597 sc->offchannel.roc_chan = NULL;
599 ieee80211_remain_on_channel_expired(sc->hw);
600 ath_offchannel_next(sc);
601 ath9k_ps_restore(sc);
604 void ath_scan_complete(struct ath_softc *sc, bool abort)
606 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
609 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
611 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
613 sc->offchannel.scan_req = NULL;
614 sc->offchannel.scan_vif = NULL;
615 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
616 ieee80211_scan_completed(sc->hw, abort);
617 clear_bit(ATH_OP_SCANNING, &common->op_flags);
618 ath_offchannel_next(sc);
619 ath9k_ps_restore(sc);
622 static void ath_scan_send_probe(struct ath_softc *sc,
623 struct cfg80211_ssid *ssid)
625 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
626 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
627 struct ath_tx_control txctl = {};
629 struct ieee80211_tx_info *info;
630 int band = sc->offchannel.chan.chandef.chan->band;
632 skb = ieee80211_probereq_get(sc->hw, vif,
633 ssid->ssid, ssid->ssid_len, req->ie_len);
637 info = IEEE80211_SKB_CB(skb);
639 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
642 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
644 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
646 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
649 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
650 txctl.force_channel = true;
651 if (ath_tx_start(sc->hw, skb, &txctl))
657 ieee80211_free_txskb(sc->hw, skb);
660 static void ath_scan_channel_start(struct ath_softc *sc)
662 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
663 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
666 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
668 for (i = 0; i < req->n_ssids; i++)
669 ath_scan_send_probe(sc, &req->ssids[i]);
673 ath_dbg(common, CHAN_CTX,
674 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
676 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
677 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
680 static void ath_chanctx_timer(unsigned long data)
682 struct ath_softc *sc = (struct ath_softc *) data;
684 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
687 static void ath_offchannel_timer(unsigned long data)
689 struct ath_softc *sc = (struct ath_softc *)data;
690 struct ath_chanctx *ctx;
691 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
693 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
694 __func__, offchannel_state_string(sc->offchannel.state));
696 switch (sc->offchannel.state) {
697 case ATH_OFFCHANNEL_PROBE_WAIT:
698 if (!sc->offchannel.scan_req)
701 /* get first active channel context */
702 ctx = ath_chanctx_get_oper_chan(sc, true);
704 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
705 ath_chanctx_switch(sc, ctx, NULL);
706 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
710 case ATH_OFFCHANNEL_SUSPEND:
711 if (!sc->offchannel.scan_req)
714 ath_scan_next_channel(sc);
716 case ATH_OFFCHANNEL_ROC_START:
717 case ATH_OFFCHANNEL_ROC_WAIT:
718 ctx = ath_chanctx_get_oper_chan(sc, false);
719 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
720 ath_chanctx_switch(sc, ctx, NULL);
728 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
731 struct ieee80211_vif *vif = avp->vif;
732 struct ieee80211_sta *sta = NULL;
733 struct ieee80211_hdr_3addr *nullfunc;
734 struct ath_tx_control txctl;
736 int band = sc->cur_chan->chandef.chan->band;
739 case NL80211_IFTYPE_STATION:
740 if (!vif->bss_conf.assoc)
743 skb = ieee80211_nullfunc_get(sc->hw, vif);
747 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
749 nullfunc->frame_control |=
750 cpu_to_le16(IEEE80211_FCTL_PM);
752 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
753 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
754 dev_kfree_skb_any(skb);
762 memset(&txctl, 0, sizeof(txctl));
763 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
765 txctl.force_channel = true;
766 if (ath_tx_start(sc->hw, skb, &txctl)) {
767 ieee80211_free_txskb(sc->hw, skb);
775 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
781 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
782 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
790 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
792 if (sc->cur_chan == &sc->offchannel.chan)
795 switch (sc->sched.state) {
796 case ATH_CHANCTX_STATE_SWITCH:
798 case ATH_CHANCTX_STATE_IDLE:
799 if (!sc->cur_chan->switch_after_beacon)
802 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
811 static void ath_offchannel_channel_change(struct ath_softc *sc)
813 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
815 ath_dbg(common, CHAN_CTX, "%s: state: %s\n",
816 __func__, offchannel_state_string(sc->offchannel.state));
818 switch (sc->offchannel.state) {
819 case ATH_OFFCHANNEL_PROBE_SEND:
820 if (!sc->offchannel.scan_req)
823 if (sc->cur_chan->chandef.chan !=
824 sc->offchannel.chan.chandef.chan)
827 ath_scan_channel_start(sc);
829 case ATH_OFFCHANNEL_IDLE:
830 if (!sc->offchannel.scan_req)
833 ath_scan_complete(sc, false);
835 case ATH_OFFCHANNEL_ROC_START:
836 if (sc->cur_chan != &sc->offchannel.chan)
839 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
840 mod_timer(&sc->offchannel.timer, jiffies +
841 msecs_to_jiffies(sc->offchannel.duration));
842 ieee80211_ready_on_channel(sc->hw);
844 case ATH_OFFCHANNEL_ROC_DONE:
845 ath_roc_complete(sc, false);
852 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
855 bool measure_time = false;
856 bool send_ps = false;
858 spin_lock_bh(&sc->chan_lock);
859 if (!sc->next_chan) {
860 spin_unlock_bh(&sc->chan_lock);
864 if (!force && ath_chanctx_defer_switch(sc)) {
865 spin_unlock_bh(&sc->chan_lock);
869 if (sc->cur_chan != sc->next_chan) {
870 sc->cur_chan->stopped = true;
871 spin_unlock_bh(&sc->chan_lock);
873 if (sc->next_chan == &sc->offchannel.chan) {
874 getrawmonotonic(&ts);
877 __ath9k_flush(sc->hw, ~0, true);
879 if (ath_chanctx_send_ps_frame(sc, true))
880 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
883 spin_lock_bh(&sc->chan_lock);
885 if (sc->cur_chan != &sc->offchannel.chan) {
886 getrawmonotonic(&sc->cur_chan->tsf_ts);
887 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
890 sc->cur_chan = sc->next_chan;
891 sc->cur_chan->stopped = false;
892 sc->next_chan = NULL;
893 sc->sched.offchannel_duration = 0;
894 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
895 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
897 spin_unlock_bh(&sc->chan_lock);
899 if (sc->sc_ah->chip_fullsleep ||
900 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
901 sizeof(sc->cur_chandef))) {
904 sc->sched.channel_switch_time =
905 ath9k_hw_get_tsf_offset(&ts, NULL);
908 ath_chanctx_send_ps_frame(sc, false);
910 ath_offchannel_channel_change(sc);
911 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
914 static void ath_chanctx_work(struct work_struct *work)
916 struct ath_softc *sc = container_of(work, struct ath_softc,
918 mutex_lock(&sc->mutex);
919 ath_chanctx_set_next(sc, false);
920 mutex_unlock(&sc->mutex);
923 void ath9k_init_channel_context(struct ath_softc *sc)
925 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
927 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
929 setup_timer(&sc->sched.timer, ath_chanctx_timer,
933 void ath9k_deinit_channel_context(struct ath_softc *sc)
935 cancel_work_sync(&sc->chanctx_work);
938 bool ath9k_is_chanctx_enabled(void)
940 return (ath9k_use_chanctx == 1);
947 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
949 struct ath_hw *ah = sc->sc_ah;
952 if (!avp || !avp->noa.has_next_tsf)
955 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
957 tsf = ath9k_hw_gettsf32(sc->sc_ah);
959 target_tsf = avp->noa.next_tsf;
960 if (!avp->noa.absent)
961 target_tsf -= ATH_P2P_PS_STOP_TIME;
963 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
964 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
966 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
969 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
971 struct ath_vif *avp = (void *)vif->drv_priv;
974 if (!sc->p2p_ps_timer)
977 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
980 sc->p2p_ps_vif = avp;
981 tsf = ath9k_hw_gettsf32(sc->sc_ah);
982 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
983 ath9k_update_p2p_ps_timer(sc, avp);
986 void ath9k_p2p_ps_timer(void *priv)
988 struct ath_softc *sc = priv;
989 struct ath_vif *avp = sc->p2p_ps_vif;
990 struct ieee80211_vif *vif;
991 struct ieee80211_sta *sta;
995 del_timer_sync(&sc->sched.timer);
996 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
997 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
999 if (!avp || avp->chanctx != sc->cur_chan)
1002 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1003 if (!avp->noa.absent)
1004 tsf += ATH_P2P_PS_STOP_TIME;
1006 if (!avp->noa.has_next_tsf ||
1007 avp->noa.next_tsf - tsf > BIT(31))
1008 ieee80211_update_p2p_noa(&avp->noa, tsf);
1010 ath9k_update_p2p_ps_timer(sc, avp);
1015 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1019 an = (void *) sta->drv_priv;
1020 if (an->sleeping == !!avp->noa.absent)
1023 an->sleeping = avp->noa.absent;
1025 ath_tx_aggr_sleep(sta, sc, an);
1027 ath_tx_aggr_wakeup(sc, an);
1033 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1034 struct ieee80211_vif *vif)
1036 unsigned long flags;
1038 spin_lock_bh(&sc->sc_pcu_lock);
1039 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1040 if (!(sc->ps_flags & PS_BEACON_SYNC))
1041 ath9k_update_p2p_ps(sc, vif);
1042 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1043 spin_unlock_bh(&sc->sc_pcu_lock);
1046 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1049 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1052 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1053 struct ieee80211_vif *vif)
1055 struct ath_vif *avp = (void *)vif->drv_priv;
1057 spin_lock_bh(&sc->sc_pcu_lock);
1058 if (avp == sc->p2p_ps_vif) {
1059 sc->p2p_ps_vif = NULL;
1060 ath9k_update_p2p_ps_timer(sc, NULL);
1062 spin_unlock_bh(&sc->sc_pcu_lock);
1065 int ath9k_init_p2p(struct ath_softc *sc)
1067 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1068 NULL, sc, AR_FIRST_NDP_TIMER);
1069 if (!sc->p2p_ps_timer)
1075 void ath9k_deinit_p2p(struct ath_softc *sc)
1077 if (sc->p2p_ps_timer)
1078 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1081 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */