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 set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
186 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
190 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
196 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
197 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
205 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
207 if (sc->cur_chan == &sc->offchannel.chan)
210 switch (sc->sched.state) {
211 case ATH_CHANCTX_STATE_SWITCH:
213 case ATH_CHANCTX_STATE_IDLE:
214 if (!sc->cur_chan->switch_after_beacon)
217 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
226 void ath_chanctx_work(struct work_struct *work)
228 struct ath_softc *sc = container_of(work, struct ath_softc,
231 bool measure_time = false;
232 bool send_ps = false;
234 mutex_lock(&sc->mutex);
235 spin_lock_bh(&sc->chan_lock);
236 if (!sc->next_chan) {
237 spin_unlock_bh(&sc->chan_lock);
238 mutex_unlock(&sc->mutex);
242 if (ath_chanctx_defer_switch(sc)) {
243 spin_unlock_bh(&sc->chan_lock);
244 mutex_unlock(&sc->mutex);
248 if (sc->cur_chan != sc->next_chan) {
249 sc->cur_chan->stopped = true;
250 spin_unlock_bh(&sc->chan_lock);
252 if (sc->next_chan == &sc->offchannel.chan) {
253 getrawmonotonic(&ts);
256 __ath9k_flush(sc->hw, ~0, true);
258 if (ath_chanctx_send_ps_frame(sc, true))
259 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false);
262 spin_lock_bh(&sc->chan_lock);
264 if (sc->cur_chan != &sc->offchannel.chan) {
265 getrawmonotonic(&sc->cur_chan->tsf_ts);
266 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
269 sc->cur_chan = sc->next_chan;
270 sc->cur_chan->stopped = false;
271 sc->next_chan = NULL;
272 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
273 sc->sched.offchannel_duration = 0;
275 spin_unlock_bh(&sc->chan_lock);
277 if (sc->sc_ah->chip_fullsleep ||
278 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
279 sizeof(sc->cur_chandef))) {
282 sc->sched.channel_switch_time =
283 ath9k_hw_get_tsf_offset(&ts, NULL);
286 ath_chanctx_send_ps_frame(sc, false);
288 ath_offchannel_channel_change(sc);
289 mutex_unlock(&sc->mutex);
292 void ath_chanctx_init(struct ath_softc *sc)
294 struct ath_chanctx *ctx;
295 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
296 struct ieee80211_supported_band *sband;
297 struct ieee80211_channel *chan;
300 sband = &common->sbands[IEEE80211_BAND_2GHZ];
301 if (!sband->n_channels)
302 sband = &common->sbands[IEEE80211_BAND_5GHZ];
304 chan = &sband->channels[0];
305 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
306 ctx = &sc->chanctx[i];
307 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
308 INIT_LIST_HEAD(&ctx->vifs);
309 ctx->txpower = ATH_TXPOWER_MAX;
310 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
311 INIT_LIST_HEAD(&ctx->acq[j]);
313 ctx = &sc->offchannel.chan;
314 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
315 INIT_LIST_HEAD(&ctx->vifs);
316 ctx->txpower = ATH_TXPOWER_MAX;
317 for (j = 0; j < ARRAY_SIZE(ctx->acq); j++)
318 INIT_LIST_HEAD(&ctx->acq[j]);
319 sc->offchannel.chan.offchannel = true;
323 void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
324 struct cfg80211_chan_def *chandef)
327 spin_lock_bh(&sc->chan_lock);
330 ctx->chandef = *chandef;
332 if (sc->next_chan == &sc->offchannel.chan) {
333 sc->sched.offchannel_duration =
334 TU_TO_USEC(sc->offchannel.duration) +
335 sc->sched.channel_switch_time;
337 spin_unlock_bh(&sc->chan_lock);
338 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
341 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
342 struct cfg80211_chan_def *chandef)
346 spin_lock_bh(&sc->chan_lock);
348 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
349 cur_chan = sc->cur_chan == ctx;
350 spin_unlock_bh(&sc->chan_lock);
358 struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, bool active)
360 struct ath_chanctx *ctx;
362 ath_for_each_chanctx(sc, ctx) {
363 if (!ctx->assigned || list_empty(&ctx->vifs))
365 if (active && !ctx->active)
371 return &sc->chanctx[0];
374 void ath_chanctx_offchan_switch(struct ath_softc *sc,
375 struct ieee80211_channel *chan)
377 struct cfg80211_chan_def chandef;
379 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
381 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
384 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
385 enum ath_chanctx_event ev)
387 struct ath_hw *ah = sc->sc_ah;
388 struct ath_vif *avp = NULL;
390 bool noa_changed = false;
393 avp = (struct ath_vif *) vif->drv_priv;
395 spin_lock_bh(&sc->chan_lock);
398 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
399 if (avp->offchannel_duration)
400 avp->offchannel_duration = 0;
402 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
405 sc->sched.beacon_pending = true;
406 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
408 /* defer channel switch by a quarter beacon interval */
409 tsf_time = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
410 tsf_time = sc->sched.next_tbtt + tsf_time / 4;
411 sc->sched.switch_start_time = tsf_time;
413 if (sc->sched.offchannel_duration) {
415 avp->offchannel_start = tsf_time;
416 avp->offchannel_duration =
417 sc->sched.offchannel_duration;
423 case ATH_CHANCTX_EVENT_BEACON_SENT:
424 if (!sc->sched.beacon_pending)
427 sc->sched.beacon_pending = false;
428 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
431 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
432 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer,
433 sc->sched.switch_start_time,
436 case ATH_CHANCTX_EVENT_TSF_TIMER:
437 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
440 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
441 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
445 spin_unlock_bh(&sc->chan_lock);