xref: /linux/drivers/net/wireless/ath/ath9k/channel.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1fbbcd146SFelix Fietkau /*
2fbbcd146SFelix Fietkau  * Copyright (c) 2014 Qualcomm Atheros, Inc.
3fbbcd146SFelix Fietkau  *
4fbbcd146SFelix Fietkau  * Permission to use, copy, modify, and/or distribute this software for any
5fbbcd146SFelix Fietkau  * purpose with or without fee is hereby granted, provided that the above
6fbbcd146SFelix Fietkau  * copyright notice and this permission notice appear in all copies.
7fbbcd146SFelix Fietkau  *
8fbbcd146SFelix Fietkau  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9fbbcd146SFelix Fietkau  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10fbbcd146SFelix Fietkau  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11fbbcd146SFelix Fietkau  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12fbbcd146SFelix Fietkau  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13fbbcd146SFelix Fietkau  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14fbbcd146SFelix Fietkau  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15fbbcd146SFelix Fietkau  */
16fbbcd146SFelix Fietkau 
17fbbcd146SFelix Fietkau #include "ath9k.h"
18fbbcd146SFelix Fietkau 
19fbbcd146SFelix Fietkau /* Set/change channels.  If the channel is really being changed, it's done
20fbbcd146SFelix Fietkau  * by reseting the chip.  To accomplish this we must first cleanup any pending
21fbbcd146SFelix Fietkau  * DMA, then restart stuff.
22fbbcd146SFelix Fietkau  */
ath_set_channel(struct ath_softc * sc)23fbbcd146SFelix Fietkau static int ath_set_channel(struct ath_softc *sc)
24fbbcd146SFelix Fietkau {
25fbbcd146SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
26fbbcd146SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
27fbbcd146SFelix Fietkau 	struct ieee80211_hw *hw = sc->hw;
28fbbcd146SFelix Fietkau 	struct ath9k_channel *hchan;
29fbbcd146SFelix Fietkau 	struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef;
30fbbcd146SFelix Fietkau 	struct ieee80211_channel *chan = chandef->chan;
31fbbcd146SFelix Fietkau 	int pos = chan->hw_value;
32ba24d63dSVille Syrjälä 	unsigned long flags;
33fbbcd146SFelix Fietkau 	int old_pos = -1;
34fbbcd146SFelix Fietkau 	int r;
35fbbcd146SFelix Fietkau 
36fbbcd146SFelix Fietkau 	if (test_bit(ATH_OP_INVALID, &common->op_flags))
37fbbcd146SFelix Fietkau 		return -EIO;
38fbbcd146SFelix Fietkau 
39fbbcd146SFelix Fietkau 	if (ah->curchan)
40fbbcd146SFelix Fietkau 		old_pos = ah->curchan - &ah->channels[0];
41fbbcd146SFelix Fietkau 
42fbbcd146SFelix Fietkau 	ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
43fbbcd146SFelix Fietkau 		chan->center_freq, chandef->width);
44fbbcd146SFelix Fietkau 
45fbbcd146SFelix Fietkau 	/* update survey stats for the old channel before switching */
46ba24d63dSVille Syrjälä 	spin_lock_irqsave(&common->cc_lock, flags);
47fbbcd146SFelix Fietkau 	ath_update_survey_stats(sc);
48ba24d63dSVille Syrjälä 	spin_unlock_irqrestore(&common->cc_lock, flags);
49fbbcd146SFelix Fietkau 
50fbbcd146SFelix Fietkau 	ath9k_cmn_get_channel(hw, ah, chandef);
51fbbcd146SFelix Fietkau 
52fbbcd146SFelix Fietkau 	/* If the operating channel changes, change the survey in-use flags
53fbbcd146SFelix Fietkau 	 * along with it.
54fbbcd146SFelix Fietkau 	 * Reset the survey data for the new channel, unless we're switching
55fbbcd146SFelix Fietkau 	 * back to the operating channel from an off-channel operation.
56fbbcd146SFelix Fietkau 	 */
57fbbcd146SFelix Fietkau 	if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) {
58fbbcd146SFelix Fietkau 		if (sc->cur_survey)
59fbbcd146SFelix Fietkau 			sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
60fbbcd146SFelix Fietkau 
61fbbcd146SFelix Fietkau 		sc->cur_survey = &sc->survey[pos];
62fbbcd146SFelix Fietkau 
63fbbcd146SFelix Fietkau 		memset(sc->cur_survey, 0, sizeof(struct survey_info));
64fbbcd146SFelix Fietkau 		sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
65fbbcd146SFelix Fietkau 	} else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
66fbbcd146SFelix Fietkau 		memset(&sc->survey[pos], 0, sizeof(struct survey_info));
67fbbcd146SFelix Fietkau 	}
68fbbcd146SFelix Fietkau 
69fbbcd146SFelix Fietkau 	hchan = &sc->sc_ah->channels[pos];
705555c955SSujith Manoharan 	r = ath_reset(sc, hchan);
71fbbcd146SFelix Fietkau 	if (r)
72fbbcd146SFelix Fietkau 		return r;
73fbbcd146SFelix Fietkau 
74fbbcd146SFelix Fietkau 	/* The most recent snapshot of channel->noisefloor for the old
75fbbcd146SFelix Fietkau 	 * channel is only available after the hardware reset. Copy it to
76fbbcd146SFelix Fietkau 	 * the survey stats now.
77fbbcd146SFelix Fietkau 	 */
78fbbcd146SFelix Fietkau 	if (old_pos >= 0)
79fbbcd146SFelix Fietkau 		ath_update_survey_nf(sc, old_pos);
80fbbcd146SFelix Fietkau 
81fbbcd146SFelix Fietkau 	/* Enable radar pulse detection if on a DFS channel. Spectral
82fbbcd146SFelix Fietkau 	 * scanning and radar detection can not be used concurrently.
83fbbcd146SFelix Fietkau 	 */
84fbbcd146SFelix Fietkau 	if (hw->conf.radar_enabled) {
85fbbcd146SFelix Fietkau 		u32 rxfilter;
86fbbcd146SFelix Fietkau 
87fbbcd146SFelix Fietkau 		rxfilter = ath9k_hw_getrxfilter(ah);
88fbbcd146SFelix Fietkau 		rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
89fbbcd146SFelix Fietkau 				ATH9K_RX_FILTER_PHYERR;
90fbbcd146SFelix Fietkau 		ath9k_hw_setrxfilter(ah, rxfilter);
91fbbcd146SFelix Fietkau 		ath_dbg(common, DFS, "DFS enabled at freq %d\n",
92fbbcd146SFelix Fietkau 			chan->center_freq);
93fbbcd146SFelix Fietkau 	} else {
94fbbcd146SFelix Fietkau 		/* perform spectral scan if requested. */
95fbbcd146SFelix Fietkau 		if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
968391f601SOleksij Rempel 			sc->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
9767dc74f1SOleksij Rempel 			ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
98fbbcd146SFelix Fietkau 	}
99fbbcd146SFelix Fietkau 
100fbbcd146SFelix Fietkau 	return 0;
101fbbcd146SFelix Fietkau }
102fbbcd146SFelix Fietkau 
ath_chanctx_init(struct ath_softc * sc)103fbbcd146SFelix Fietkau void ath_chanctx_init(struct ath_softc *sc)
104fbbcd146SFelix Fietkau {
105fbbcd146SFelix Fietkau 	struct ath_chanctx *ctx;
106fbbcd146SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
107fbbcd146SFelix Fietkau 	struct ieee80211_supported_band *sband;
108fbbcd146SFelix Fietkau 	struct ieee80211_channel *chan;
1090453531eSFelix Fietkau 	int i, j;
110fbbcd146SFelix Fietkau 
11157fbcce3SJohannes Berg 	sband = &common->sbands[NL80211_BAND_2GHZ];
112fbbcd146SFelix Fietkau 	if (!sband->n_channels)
11357fbcce3SJohannes Berg 		sband = &common->sbands[NL80211_BAND_5GHZ];
114fbbcd146SFelix Fietkau 
115fbbcd146SFelix Fietkau 	chan = &sband->channels[0];
116fbbcd146SFelix Fietkau 	for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
117fbbcd146SFelix Fietkau 		ctx = &sc->chanctx[i];
118fbbcd146SFelix Fietkau 		cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
119fbbcd146SFelix Fietkau 		INIT_LIST_HEAD(&ctx->vifs);
120bc7e1be7SFelix Fietkau 		ctx->txpower = ATH_TXPOWER_MAX;
1212fae0d9fSSujith Manoharan 		ctx->flush_timeout = HZ / 5; /* 200ms */
12263fefa05SToke Høiland-Jørgensen 		for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) {
12363fefa05SToke Høiland-Jørgensen 			INIT_LIST_HEAD(&ctx->acq[j].acq_new);
12463fefa05SToke Høiland-Jørgensen 			INIT_LIST_HEAD(&ctx->acq[j].acq_old);
12563fefa05SToke Høiland-Jørgensen 			spin_lock_init(&ctx->acq[j].lock);
12663fefa05SToke Høiland-Jørgensen 		}
127fbbcd146SFelix Fietkau 	}
128fbbcd146SFelix Fietkau }
129fbbcd146SFelix Fietkau 
ath_chanctx_set_channel(struct ath_softc * sc,struct ath_chanctx * ctx,struct cfg80211_chan_def * chandef)130bff11766SFelix Fietkau void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
131bff11766SFelix Fietkau 			     struct cfg80211_chan_def *chandef)
132bff11766SFelix Fietkau {
1334c7e9aeeSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
134bff11766SFelix Fietkau 	bool cur_chan;
135bff11766SFelix Fietkau 
136bff11766SFelix Fietkau 	spin_lock_bh(&sc->chan_lock);
137bff11766SFelix Fietkau 	if (chandef)
138bff11766SFelix Fietkau 		memcpy(&ctx->chandef, chandef, sizeof(*chandef));
139bff11766SFelix Fietkau 	cur_chan = sc->cur_chan == ctx;
140bff11766SFelix Fietkau 	spin_unlock_bh(&sc->chan_lock);
141bff11766SFelix Fietkau 
1424c7e9aeeSSujith Manoharan 	if (!cur_chan) {
1434c7e9aeeSSujith Manoharan 		ath_dbg(common, CHAN_CTX,
1444c7e9aeeSSujith Manoharan 			"Current context differs from the new context\n");
145bff11766SFelix Fietkau 		return;
1464c7e9aeeSSujith Manoharan 	}
147bff11766SFelix Fietkau 
148bff11766SFelix Fietkau 	ath_set_channel(sc);
149fbbcd146SFelix Fietkau }
15078b21949SFelix Fietkau 
15127babf9fSSujith Manoharan #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
15227babf9fSSujith Manoharan 
15326103b8dSSujith Manoharan /*************/
15426103b8dSSujith Manoharan /* Utilities */
15526103b8dSSujith Manoharan /*************/
15626103b8dSSujith Manoharan 
ath_is_go_chanctx_present(struct ath_softc * sc)15726103b8dSSujith Manoharan struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
15826103b8dSSujith Manoharan {
15926103b8dSSujith Manoharan 	struct ath_chanctx *ctx;
16026103b8dSSujith Manoharan 	struct ath_vif *avp;
16126103b8dSSujith Manoharan 	struct ieee80211_vif *vif;
16226103b8dSSujith Manoharan 
16326103b8dSSujith Manoharan 	spin_lock_bh(&sc->chan_lock);
16426103b8dSSujith Manoharan 
16526103b8dSSujith Manoharan 	ath_for_each_chanctx(sc, ctx) {
16626103b8dSSujith Manoharan 		if (!ctx->active)
16726103b8dSSujith Manoharan 			continue;
16826103b8dSSujith Manoharan 
16926103b8dSSujith Manoharan 		list_for_each_entry(avp, &ctx->vifs, list) {
17026103b8dSSujith Manoharan 			vif = avp->vif;
17126103b8dSSujith Manoharan 
17226103b8dSSujith Manoharan 			if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
17326103b8dSSujith Manoharan 				spin_unlock_bh(&sc->chan_lock);
17426103b8dSSujith Manoharan 				return ctx;
17526103b8dSSujith Manoharan 			}
17626103b8dSSujith Manoharan 		}
17726103b8dSSujith Manoharan 	}
17826103b8dSSujith Manoharan 
17926103b8dSSujith Manoharan 	spin_unlock_bh(&sc->chan_lock);
18026103b8dSSujith Manoharan 	return NULL;
18126103b8dSSujith Manoharan }
18226103b8dSSujith Manoharan 
1830e08b5fbSSujith Manoharan /**********************************************************/
1840e08b5fbSSujith Manoharan /* Functions to handle the channel context state machine. */
1850e08b5fbSSujith Manoharan /**********************************************************/
1860e08b5fbSSujith Manoharan 
offchannel_state_string(enum ath_offchannel_state state)18727babf9fSSujith Manoharan static const char *offchannel_state_string(enum ath_offchannel_state state)
18827babf9fSSujith Manoharan {
18927babf9fSSujith Manoharan 	switch (state) {
19027babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_IDLE);
19127babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
19227babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
19327babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
19427babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_ROC_START);
19527babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
19627babf9fSSujith Manoharan 		case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
19727babf9fSSujith Manoharan 	default:
19827babf9fSSujith Manoharan 		return "unknown";
19927babf9fSSujith Manoharan 	}
20027babf9fSSujith Manoharan }
20127babf9fSSujith Manoharan 
chanctx_event_string(enum ath_chanctx_event ev)2025a8cbec7SSujith Manoharan static const char *chanctx_event_string(enum ath_chanctx_event ev)
2035a8cbec7SSujith Manoharan {
2045a8cbec7SSujith Manoharan 	switch (ev) {
2055a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
2065a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
2075a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
2085a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
209b8f9279bSSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
2105a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
2115a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
2125a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
2135a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
2145a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
2155a8cbec7SSujith Manoharan 	default:
2165a8cbec7SSujith Manoharan 		return "unknown";
2175a8cbec7SSujith Manoharan 	}
2185a8cbec7SSujith Manoharan }
2195a8cbec7SSujith Manoharan 
chanctx_state_string(enum ath_chanctx_state state)2205a8cbec7SSujith Manoharan static const char *chanctx_state_string(enum ath_chanctx_state state)
2215a8cbec7SSujith Manoharan {
2225a8cbec7SSujith Manoharan 	switch (state) {
2235a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_STATE_IDLE);
2245a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
2255a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
2265a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
2275a8cbec7SSujith Manoharan 		case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
2285a8cbec7SSujith Manoharan 	default:
2295a8cbec7SSujith Manoharan 		return "unknown";
2305a8cbec7SSujith Manoharan 	}
2315a8cbec7SSujith Manoharan }
2325a8cbec7SSujith Manoharan 
chanctx_event_delta(struct ath_softc * sc)233344cd850SGeert Uytterhoeven static u32 chanctx_event_delta(struct ath_softc *sc)
23402edab5bSJanusz Dziedzic {
23502edab5bSJanusz Dziedzic 	u64 ms;
236fe041debSArnd Bergmann 	struct timespec64 ts, *old;
23702edab5bSJanusz Dziedzic 
238fe041debSArnd Bergmann 	ktime_get_raw_ts64(&ts);
23902edab5bSJanusz Dziedzic 	old = &sc->last_event_time;
24002edab5bSJanusz Dziedzic 	ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
24102edab5bSJanusz Dziedzic 	ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
24202edab5bSJanusz Dziedzic 	sc->last_event_time = ts;
24302edab5bSJanusz Dziedzic 
24402edab5bSJanusz Dziedzic 	return (u32)ms;
24502edab5bSJanusz Dziedzic }
24602edab5bSJanusz Dziedzic 
ath_chanctx_check_active(struct ath_softc * sc,struct ath_chanctx * ctx)247a09798f4SSujith Manoharan void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
248a09798f4SSujith Manoharan {
249a09798f4SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
25067259d51SSujith Manoharan 	struct ath_chanctx *ictx;
251a09798f4SSujith Manoharan 	struct ath_vif *avp;
252a09798f4SSujith Manoharan 	bool active = false;
253a09798f4SSujith Manoharan 	u8 n_active = 0;
254a09798f4SSujith Manoharan 
255a09798f4SSujith Manoharan 	if (!ctx)
256a09798f4SSujith Manoharan 		return;
257a09798f4SSujith Manoharan 
258290c8a77SSujith Manoharan 	if (ctx == &sc->offchannel.chan) {
259290c8a77SSujith Manoharan 		spin_lock_bh(&sc->chan_lock);
260290c8a77SSujith Manoharan 
261290c8a77SSujith Manoharan 		if (likely(sc->sched.channel_switch_time))
262290c8a77SSujith Manoharan 			ctx->flush_timeout =
263290c8a77SSujith Manoharan 				usecs_to_jiffies(sc->sched.channel_switch_time);
264290c8a77SSujith Manoharan 		else
265290c8a77SSujith Manoharan 			ctx->flush_timeout =
266290c8a77SSujith Manoharan 				msecs_to_jiffies(10);
267290c8a77SSujith Manoharan 
268290c8a77SSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
269290c8a77SSujith Manoharan 
270290c8a77SSujith Manoharan 		/*
271290c8a77SSujith Manoharan 		 * There is no need to iterate over the
272290c8a77SSujith Manoharan 		 * active/assigned channel contexts if
273290c8a77SSujith Manoharan 		 * the current context is offchannel.
274290c8a77SSujith Manoharan 		 */
275290c8a77SSujith Manoharan 		return;
276290c8a77SSujith Manoharan 	}
277290c8a77SSujith Manoharan 
27867259d51SSujith Manoharan 	ictx = ctx;
27967259d51SSujith Manoharan 
280a09798f4SSujith Manoharan 	list_for_each_entry(avp, &ctx->vifs, list) {
281a09798f4SSujith Manoharan 		struct ieee80211_vif *vif = avp->vif;
282a09798f4SSujith Manoharan 
283a09798f4SSujith Manoharan 		switch (vif->type) {
284a09798f4SSujith Manoharan 		case NL80211_IFTYPE_P2P_CLIENT:
285a09798f4SSujith Manoharan 		case NL80211_IFTYPE_STATION:
286cb35582aSSujith Manoharan 			if (avp->assoc)
287a09798f4SSujith Manoharan 				active = true;
288a09798f4SSujith Manoharan 			break;
289a09798f4SSujith Manoharan 		default:
290a09798f4SSujith Manoharan 			active = true;
291a09798f4SSujith Manoharan 			break;
292a09798f4SSujith Manoharan 		}
293a09798f4SSujith Manoharan 	}
294a09798f4SSujith Manoharan 	ctx->active = active;
295a09798f4SSujith Manoharan 
296a09798f4SSujith Manoharan 	ath_for_each_chanctx(sc, ctx) {
297a09798f4SSujith Manoharan 		if (!ctx->assigned || list_empty(&ctx->vifs))
298a09798f4SSujith Manoharan 			continue;
299a09798f4SSujith Manoharan 		n_active++;
300a09798f4SSujith Manoharan 	}
301a09798f4SSujith Manoharan 
30267259d51SSujith Manoharan 	spin_lock_bh(&sc->chan_lock);
30367259d51SSujith Manoharan 
304a09798f4SSujith Manoharan 	if (n_active <= 1) {
30567259d51SSujith Manoharan 		ictx->flush_timeout = HZ / 5;
306a09798f4SSujith Manoharan 		clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
30767259d51SSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
308a09798f4SSujith Manoharan 		return;
309a09798f4SSujith Manoharan 	}
31067259d51SSujith Manoharan 
31167259d51SSujith Manoharan 	ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
31267259d51SSujith Manoharan 
31367259d51SSujith Manoharan 	if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
31467259d51SSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
315a09798f4SSujith Manoharan 		return;
31667259d51SSujith Manoharan 	}
31767259d51SSujith Manoharan 
31867259d51SSujith Manoharan 	spin_unlock_bh(&sc->chan_lock);
319a09798f4SSujith Manoharan 
320a09798f4SSujith Manoharan 	if (ath9k_is_chanctx_enabled()) {
321a09798f4SSujith Manoharan 		ath_chanctx_event(sc, NULL,
322a09798f4SSujith Manoharan 				  ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
323a09798f4SSujith Manoharan 	}
324a09798f4SSujith Manoharan }
325a09798f4SSujith Manoharan 
32658b57375SFelix Fietkau static struct ath_chanctx *
ath_chanctx_get_next(struct ath_softc * sc,struct ath_chanctx * ctx)32758b57375SFelix Fietkau ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
32858b57375SFelix Fietkau {
32958b57375SFelix Fietkau 	int idx = ctx - &sc->chanctx[0];
33058b57375SFelix Fietkau 
33158b57375SFelix Fietkau 	return &sc->chanctx[!idx];
33258b57375SFelix Fietkau }
33358b57375SFelix Fietkau 
ath_chanctx_adjust_tbtt_delta(struct ath_softc * sc)33458b57375SFelix Fietkau static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
33558b57375SFelix Fietkau {
33658b57375SFelix Fietkau 	struct ath_chanctx *prev, *cur;
337fe041debSArnd Bergmann 	struct timespec64 ts;
33858b57375SFelix Fietkau 	u32 cur_tsf, prev_tsf, beacon_int;
33958b57375SFelix Fietkau 	s32 offset;
34058b57375SFelix Fietkau 
34158b57375SFelix Fietkau 	beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
34258b57375SFelix Fietkau 
34358b57375SFelix Fietkau 	cur = sc->cur_chan;
34458b57375SFelix Fietkau 	prev = ath_chanctx_get_next(sc, cur);
34558b57375SFelix Fietkau 
3467f30eac9SSujith Manoharan 	if (!prev->switch_after_beacon)
3477f30eac9SSujith Manoharan 		return;
3487f30eac9SSujith Manoharan 
349fe041debSArnd Bergmann 	ktime_get_raw_ts64(&ts);
35058b57375SFelix Fietkau 	cur_tsf = (u32) cur->tsf_val +
35158b57375SFelix Fietkau 		  ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
35258b57375SFelix Fietkau 
35358b57375SFelix Fietkau 	prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
35458b57375SFelix Fietkau 	prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
35558b57375SFelix Fietkau 
35658b57375SFelix Fietkau 	/* Adjust the TSF time of the AP chanctx to keep its beacons
35758b57375SFelix Fietkau 	 * at half beacon interval offset relative to the STA chanctx.
35858b57375SFelix Fietkau 	 */
35958b57375SFelix Fietkau 	offset = cur_tsf - prev_tsf;
36058b57375SFelix Fietkau 
36158b57375SFelix Fietkau 	/* Ignore stale data or spurious timestamps */
36258b57375SFelix Fietkau 	if (offset < 0 || offset > 3 * beacon_int)
36358b57375SFelix Fietkau 		return;
36458b57375SFelix Fietkau 
36558b57375SFelix Fietkau 	offset = beacon_int / 2 - (offset % beacon_int);
36658b57375SFelix Fietkau 	prev->tsf_val += offset;
36758b57375SFelix Fietkau }
36858b57375SFelix Fietkau 
36942eda115SFelix Fietkau /* Configure the TSF based hardware timer for a channel switch.
37042eda115SFelix Fietkau  * Also set up backup software timer, in case the gen timer fails.
37142eda115SFelix Fietkau  * This could be caused by a hardware reset.
37242eda115SFelix Fietkau  */
ath_chanctx_setup_timer(struct ath_softc * sc,u32 tsf_time)37342eda115SFelix Fietkau static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
37442eda115SFelix Fietkau {
375878066e7SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
37642eda115SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
3772f985539SJanusz Dziedzic 	unsigned long timeout;
37842eda115SFelix Fietkau 
37942eda115SFelix Fietkau 	ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
38042eda115SFelix Fietkau 	tsf_time -= ath9k_hw_gettsf32(ah);
3812f985539SJanusz Dziedzic 	timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
3822f985539SJanusz Dziedzic 	mod_timer(&sc->sched.timer, jiffies + timeout);
383878066e7SSujith Manoharan 
384878066e7SSujith Manoharan 	ath_dbg(common, CHAN_CTX,
3852f985539SJanusz Dziedzic 		"Setup chanctx timer with timeout: %d (%d) ms\n",
3862f985539SJanusz Dziedzic 		tsf_time / 1000, jiffies_to_msecs(timeout));
38742eda115SFelix Fietkau }
38842eda115SFelix Fietkau 
ath_chanctx_handle_bmiss(struct ath_softc * sc,struct ath_chanctx * ctx,struct ath_vif * avp)389828fe01aSSujith Manoharan static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
390828fe01aSSujith Manoharan 				     struct ath_chanctx *ctx,
391828fe01aSSujith Manoharan 				     struct ath_vif *avp)
392828fe01aSSujith Manoharan {
393828fe01aSSujith Manoharan 	/*
394828fe01aSSujith Manoharan 	 * Clear the extend_absence flag if it had been
395828fe01aSSujith Manoharan 	 * set during the previous beacon transmission,
396828fe01aSSujith Manoharan 	 * since we need to revert to the normal NoA
397828fe01aSSujith Manoharan 	 * schedule.
398828fe01aSSujith Manoharan 	 */
399828fe01aSSujith Manoharan 	if (ctx->active && sc->sched.extend_absence) {
400828fe01aSSujith Manoharan 		avp->noa_duration = 0;
401828fe01aSSujith Manoharan 		sc->sched.extend_absence = false;
402828fe01aSSujith Manoharan 	}
403828fe01aSSujith Manoharan 
404828fe01aSSujith Manoharan 	/* If at least two consecutive beacons were missed on the STA
405828fe01aSSujith Manoharan 	 * chanctx, stay on the STA channel for one extra beacon period,
406828fe01aSSujith Manoharan 	 * to resync the timer properly.
407828fe01aSSujith Manoharan 	 */
408828fe01aSSujith Manoharan 	if (ctx->active && sc->sched.beacon_miss >= 2) {
409828fe01aSSujith Manoharan 		avp->noa_duration = 0;
410828fe01aSSujith Manoharan 		sc->sched.extend_absence = true;
411828fe01aSSujith Manoharan 	}
412828fe01aSSujith Manoharan }
413828fe01aSSujith Manoharan 
ath_chanctx_offchannel_noa(struct ath_softc * sc,struct ath_chanctx * ctx,struct ath_vif * avp,u32 tsf_time)414a23152a8SSujith Manoharan static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
415a23152a8SSujith Manoharan 				       struct ath_chanctx *ctx,
416a23152a8SSujith Manoharan 				       struct ath_vif *avp,
417a23152a8SSujith Manoharan 				       u32 tsf_time)
418a23152a8SSujith Manoharan {
419a23152a8SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
420a23152a8SSujith Manoharan 
421a23152a8SSujith Manoharan 	avp->noa_index++;
422a23152a8SSujith Manoharan 	avp->offchannel_start = tsf_time;
423a23152a8SSujith Manoharan 	avp->offchannel_duration = sc->sched.offchannel_duration;
424a23152a8SSujith Manoharan 
425a23152a8SSujith Manoharan 	ath_dbg(common, CHAN_CTX,
42658bb9ca8SJanusz Dziedzic 		"offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
427a23152a8SSujith Manoharan 		avp->offchannel_duration,
428a23152a8SSujith Manoharan 		avp->offchannel_start,
429a23152a8SSujith Manoharan 		avp->noa_index);
430a23152a8SSujith Manoharan 
431a23152a8SSujith Manoharan 	/*
432a23152a8SSujith Manoharan 	 * When multiple contexts are active, the NoA
433a23152a8SSujith Manoharan 	 * has to be recalculated and advertised after
434a23152a8SSujith Manoharan 	 * an offchannel operation.
435a23152a8SSujith Manoharan 	 */
436a23152a8SSujith Manoharan 	if (ctx->active && avp->noa_duration)
437a23152a8SSujith Manoharan 		avp->noa_duration = 0;
438a23152a8SSujith Manoharan }
439a23152a8SSujith Manoharan 
ath_chanctx_set_periodic_noa(struct ath_softc * sc,struct ath_vif * avp,struct ath_beacon_config * cur_conf,u32 tsf_time,u32 beacon_int)440347a9566SSujith Manoharan static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
441347a9566SSujith Manoharan 					 struct ath_vif *avp,
442347a9566SSujith Manoharan 					 struct ath_beacon_config *cur_conf,
443347a9566SSujith Manoharan 					 u32 tsf_time,
444347a9566SSujith Manoharan 					 u32 beacon_int)
445347a9566SSujith Manoharan {
446347a9566SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
447347a9566SSujith Manoharan 
448347a9566SSujith Manoharan 	avp->noa_index++;
449347a9566SSujith Manoharan 	avp->noa_start = tsf_time;
450347a9566SSujith Manoharan 
451347a9566SSujith Manoharan 	if (sc->sched.extend_absence)
452347a9566SSujith Manoharan 		avp->noa_duration = (3 * beacon_int / 2) +
453347a9566SSujith Manoharan 			sc->sched.channel_switch_time;
454347a9566SSujith Manoharan 	else
455347a9566SSujith Manoharan 		avp->noa_duration =
456347a9566SSujith Manoharan 			TU_TO_USEC(cur_conf->beacon_interval) / 2 +
457347a9566SSujith Manoharan 			sc->sched.channel_switch_time;
458347a9566SSujith Manoharan 
459347a9566SSujith Manoharan 	if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
460347a9566SSujith Manoharan 	    sc->sched.extend_absence)
461347a9566SSujith Manoharan 		avp->periodic_noa = false;
462347a9566SSujith Manoharan 	else
463347a9566SSujith Manoharan 		avp->periodic_noa = true;
464347a9566SSujith Manoharan 
465347a9566SSujith Manoharan 	ath_dbg(common, CHAN_CTX,
46658bb9ca8SJanusz Dziedzic 		"noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
467347a9566SSujith Manoharan 		avp->noa_duration,
468347a9566SSujith Manoharan 		avp->noa_start,
469347a9566SSujith Manoharan 		avp->noa_index,
470347a9566SSujith Manoharan 		avp->periodic_noa);
471347a9566SSujith Manoharan }
472347a9566SSujith Manoharan 
ath_chanctx_set_oneshot_noa(struct ath_softc * sc,struct ath_vif * avp,u32 tsf_time,u32 duration)4730a019a58SSujith Manoharan static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
4740a019a58SSujith Manoharan 					struct ath_vif *avp,
4750a019a58SSujith Manoharan 					u32 tsf_time,
4760a019a58SSujith Manoharan 					u32 duration)
4770a019a58SSujith Manoharan {
4780a019a58SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
4790a019a58SSujith Manoharan 
4800a019a58SSujith Manoharan 	avp->noa_index++;
4810a019a58SSujith Manoharan 	avp->noa_start = tsf_time;
4820a019a58SSujith Manoharan 	avp->periodic_noa = false;
4830a019a58SSujith Manoharan 	avp->oneshot_noa = true;
4840a019a58SSujith Manoharan 	avp->noa_duration = duration + sc->sched.channel_switch_time;
4850a019a58SSujith Manoharan 
4860a019a58SSujith Manoharan 	ath_dbg(common, CHAN_CTX,
48758bb9ca8SJanusz Dziedzic 		"oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
4880a019a58SSujith Manoharan 		avp->noa_duration,
4890a019a58SSujith Manoharan 		avp->noa_start,
4900a019a58SSujith Manoharan 		avp->noa_index,
4910a019a58SSujith Manoharan 		avp->periodic_noa);
4920a019a58SSujith Manoharan }
4930a019a58SSujith Manoharan 
ath_chanctx_event(struct ath_softc * sc,struct ieee80211_vif * vif,enum ath_chanctx_event ev)494748299f2SFelix Fietkau void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
495748299f2SFelix Fietkau 		       enum ath_chanctx_event ev)
496748299f2SFelix Fietkau {
497748299f2SFelix Fietkau 	struct ath_hw *ah = sc->sc_ah;
49858b57375SFelix Fietkau 	struct ath_common *common = ath9k_hw_common(ah);
4997414863eSFelix Fietkau 	struct ath_beacon_config *cur_conf;
5003ae07d39SFelix Fietkau 	struct ath_vif *avp = NULL;
50173fa2f26SFelix Fietkau 	struct ath_chanctx *ctx;
502748299f2SFelix Fietkau 	u32 tsf_time;
503ec70abe1SFelix Fietkau 	u32 beacon_int;
5043ae07d39SFelix Fietkau 
5053ae07d39SFelix Fietkau 	if (vif)
5063ae07d39SFelix Fietkau 		avp = (struct ath_vif *) vif->drv_priv;
507748299f2SFelix Fietkau 
508748299f2SFelix Fietkau 	spin_lock_bh(&sc->chan_lock);
509748299f2SFelix Fietkau 
51002edab5bSJanusz Dziedzic 	ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
511878066e7SSujith Manoharan 		sc->cur_chan->chandef.center_freq1,
512878066e7SSujith Manoharan 		chanctx_event_string(ev),
51302edab5bSJanusz Dziedzic 		chanctx_state_string(sc->sched.state),
51402edab5bSJanusz Dziedzic 		chanctx_event_delta(sc));
515878066e7SSujith Manoharan 
516748299f2SFelix Fietkau 	switch (ev) {
517748299f2SFelix Fietkau 	case ATH_CHANCTX_EVENT_BEACON_PREPARE:
5183ae07d39SFelix Fietkau 		if (avp->offchannel_duration)
5193ae07d39SFelix Fietkau 			avp->offchannel_duration = 0;
5203ae07d39SFelix Fietkau 
5210a019a58SSujith Manoharan 		if (avp->oneshot_noa) {
5220a019a58SSujith Manoharan 			avp->noa_duration = 0;
5230a019a58SSujith Manoharan 			avp->oneshot_noa = false;
5240a019a58SSujith Manoharan 
5250a019a58SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
5260a019a58SSujith Manoharan 				"Clearing oneshot NoA\n");
5270a019a58SSujith Manoharan 		}
5280a019a58SSujith Manoharan 
529878066e7SSujith Manoharan 		if (avp->chanctx != sc->cur_chan) {
530878066e7SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
531878066e7SSujith Manoharan 				"Contexts differ, not preparing beacon\n");
53273fa2f26SFelix Fietkau 			break;
533878066e7SSujith Manoharan 		}
53473fa2f26SFelix Fietkau 
535367b341eSSujith Manoharan 		if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
53673fa2f26SFelix Fietkau 			sc->sched.offchannel_pending = false;
53773fa2f26SFelix Fietkau 			sc->next_chan = &sc->offchannel.chan;
53873fa2f26SFelix Fietkau 			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
539878066e7SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
540878066e7SSujith Manoharan 				"Setting offchannel_pending to false\n");
54173fa2f26SFelix Fietkau 		}
54273fa2f26SFelix Fietkau 
54373fa2f26SFelix Fietkau 		ctx = ath_chanctx_get_next(sc, sc->cur_chan);
54473fa2f26SFelix Fietkau 		if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
54573fa2f26SFelix Fietkau 			sc->next_chan = ctx;
54673fa2f26SFelix Fietkau 			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
547878066e7SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
548878066e7SSujith Manoharan 				"Set next context, move chanctx state to WAIT_FOR_BEACON\n");
54973fa2f26SFelix Fietkau 		}
55073fa2f26SFelix Fietkau 
55173fa2f26SFelix Fietkau 		/* if the timer missed its window, use the next interval */
552878066e7SSujith Manoharan 		if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
55373fa2f26SFelix Fietkau 			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
554878066e7SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
555878066e7SSujith Manoharan 				"Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
556878066e7SSujith Manoharan 		}
55773fa2f26SFelix Fietkau 
558c6500ea2SSujith Manoharan 		if (sc->sched.mgd_prepare_tx)
559c6500ea2SSujith Manoharan 			sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
560c6500ea2SSujith Manoharan 
5618890d05fSSujith Manoharan 		/*
5628890d05fSSujith Manoharan 		 * When a context becomes inactive, for example,
5638890d05fSSujith Manoharan 		 * disassociation of a station context, the NoA
5648890d05fSSujith Manoharan 		 * attribute needs to be removed from subsequent
5658890d05fSSujith Manoharan 		 * beacons.
5668890d05fSSujith Manoharan 		 */
5678890d05fSSujith Manoharan 		if (!ctx->active && avp->noa_duration &&
5688890d05fSSujith Manoharan 		    sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
5698890d05fSSujith Manoharan 			avp->noa_duration = 0;
5708890d05fSSujith Manoharan 			avp->periodic_noa = false;
5718890d05fSSujith Manoharan 
5728890d05fSSujith Manoharan 			ath_dbg(common, CHAN_CTX,
5738890d05fSSujith Manoharan 				"Clearing NoA schedule\n");
5748890d05fSSujith Manoharan 		}
5758890d05fSSujith Manoharan 
576748299f2SFelix Fietkau 		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
577748299f2SFelix Fietkau 			break;
578748299f2SFelix Fietkau 
579878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
580878066e7SSujith Manoharan 
581748299f2SFelix Fietkau 		sc->sched.beacon_pending = true;
582748299f2SFelix Fietkau 		sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
5833ae07d39SFelix Fietkau 
5847414863eSFelix Fietkau 		cur_conf = &sc->cur_chan->beacon;
585ec70abe1SFelix Fietkau 		beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
586ec70abe1SFelix Fietkau 
5873ae07d39SFelix Fietkau 		/* defer channel switch by a quarter beacon interval */
588ec70abe1SFelix Fietkau 		tsf_time = sc->sched.next_tbtt + beacon_int / 4;
5893ae07d39SFelix Fietkau 		sc->sched.switch_start_time = tsf_time;
59058b57375SFelix Fietkau 		sc->cur_chan->last_beacon = sc->sched.next_tbtt;
5913ae07d39SFelix Fietkau 
592d0975eddSSujith Manoharan 		/*
593d0975eddSSujith Manoharan 		 * If an offchannel switch is scheduled to happen after
594d0975eddSSujith Manoharan 		 * a beacon transmission, update the NoA with one-shot
595d0975eddSSujith Manoharan 		 * values and increment the index.
596d0975eddSSujith Manoharan 		 */
597d0975eddSSujith Manoharan 		if (sc->next_chan == &sc->offchannel.chan) {
598a23152a8SSujith Manoharan 			ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
599d0975eddSSujith Manoharan 			break;
600d0975eddSSujith Manoharan 		}
601d0975eddSSujith Manoharan 
602828fe01aSSujith Manoharan 		ath_chanctx_handle_bmiss(sc, ctx, avp);
603a2b28601SSujith Manoharan 
6040a019a58SSujith Manoharan 		/*
6050a019a58SSujith Manoharan 		 * If a mgd_prepare_tx() has been called by mac80211,
6060a019a58SSujith Manoharan 		 * a one-shot NoA needs to be sent. This can happen
6070a019a58SSujith Manoharan 		 * with one or more active channel contexts - in both
6080a019a58SSujith Manoharan 		 * cases, a new NoA schedule has to be advertised.
6090a019a58SSujith Manoharan 		 */
6100a019a58SSujith Manoharan 		if (sc->sched.mgd_prepare_tx) {
6110a019a58SSujith Manoharan 			ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
6120a019a58SSujith Manoharan 						    jiffies_to_usecs(HZ / 5));
6130a019a58SSujith Manoharan 			break;
6140a019a58SSujith Manoharan 		}
6150a019a58SSujith Manoharan 
616d0975eddSSujith Manoharan 		/* Prevent wrap-around issues */
617d0975eddSSujith Manoharan 		if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
618d0975eddSSujith Manoharan 			avp->noa_duration = 0;
619d0975eddSSujith Manoharan 
620d0975eddSSujith Manoharan 		/*
621d0975eddSSujith Manoharan 		 * If multiple contexts are active, start periodic
622d0975eddSSujith Manoharan 		 * NoA and increment the index for the first
623d0975eddSSujith Manoharan 		 * announcement.
624d0975eddSSujith Manoharan 		 */
625d0975eddSSujith Manoharan 		if (ctx->active &&
626347a9566SSujith Manoharan 		    (!avp->noa_duration || sc->sched.force_noa_update))
627347a9566SSujith Manoharan 			ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
628347a9566SSujith Manoharan 						     tsf_time, beacon_int);
629d0975eddSSujith Manoharan 
630d0975eddSSujith Manoharan 		if (ctx->active && sc->sched.force_noa_update)
631d0975eddSSujith Manoharan 			sc->sched.force_noa_update = false;
632d0975eddSSujith Manoharan 
633748299f2SFelix Fietkau 		break;
634748299f2SFelix Fietkau 	case ATH_CHANCTX_EVENT_BEACON_SENT:
635878066e7SSujith Manoharan 		if (!sc->sched.beacon_pending) {
636878066e7SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
637878066e7SSujith Manoharan 				"No pending beacon\n");
638748299f2SFelix Fietkau 			break;
639878066e7SSujith Manoharan 		}
640748299f2SFelix Fietkau 
641748299f2SFelix Fietkau 		sc->sched.beacon_pending = false;
642c6500ea2SSujith Manoharan 
643c6500ea2SSujith Manoharan 		if (sc->sched.mgd_prepare_tx) {
644c6500ea2SSujith Manoharan 			sc->sched.mgd_prepare_tx = false;
645c6500ea2SSujith Manoharan 			complete(&sc->go_beacon);
646c6500ea2SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
647c6500ea2SSujith Manoharan 				"Beacon sent, complete go_beacon\n");
648c6500ea2SSujith Manoharan 			break;
649c6500ea2SSujith Manoharan 		}
650c6500ea2SSujith Manoharan 
651748299f2SFelix Fietkau 		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
652748299f2SFelix Fietkau 			break;
653748299f2SFelix Fietkau 
654878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
655878066e7SSujith Manoharan 			"Move chanctx state to WAIT_FOR_TIMER\n");
656878066e7SSujith Manoharan 
657748299f2SFelix Fietkau 		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
65842eda115SFelix Fietkau 		ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
659748299f2SFelix Fietkau 		break;
660748299f2SFelix Fietkau 	case ATH_CHANCTX_EVENT_TSF_TIMER:
661748299f2SFelix Fietkau 		if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
662748299f2SFelix Fietkau 			break;
663748299f2SFelix Fietkau 
664ec70abe1SFelix Fietkau 		if (!sc->cur_chan->switch_after_beacon &&
665ec70abe1SFelix Fietkau 		    sc->sched.beacon_pending)
666ec70abe1SFelix Fietkau 			sc->sched.beacon_miss++;
667ec70abe1SFelix Fietkau 
668878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
669878066e7SSujith Manoharan 			"Move chanctx state to SWITCH\n");
670878066e7SSujith Manoharan 
671748299f2SFelix Fietkau 		sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
672748299f2SFelix Fietkau 		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
673748299f2SFelix Fietkau 		break;
67458b57375SFelix Fietkau 	case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
67573fa2f26SFelix Fietkau 		if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
67673fa2f26SFelix Fietkau 		    sc->cur_chan == &sc->offchannel.chan)
67758b57375SFelix Fietkau 			break;
67858b57375SFelix Fietkau 
679ec70abe1SFelix Fietkau 		sc->sched.beacon_pending = false;
680ec70abe1SFelix Fietkau 		sc->sched.beacon_miss = 0;
681a899b678SFelix Fietkau 
682be247c1fSSujith Manoharan 		if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
683d9092c98SSujith Manoharan 		    !sc->sched.beacon_adjust ||
684be247c1fSSujith Manoharan 		    !sc->cur_chan->tsf_val)
685be247c1fSSujith Manoharan 			break;
686be247c1fSSujith Manoharan 
687be247c1fSSujith Manoharan 		ath_chanctx_adjust_tbtt_delta(sc);
688be247c1fSSujith Manoharan 
689a899b678SFelix Fietkau 		/* TSF time might have been updated by the incoming beacon,
690a899b678SFelix Fietkau 		 * need update the channel switch timer to reflect the change.
691a899b678SFelix Fietkau 		 */
692a899b678SFelix Fietkau 		tsf_time = sc->sched.switch_start_time;
693a899b678SFelix Fietkau 		tsf_time -= (u32) sc->cur_chan->tsf_val +
694a899b678SFelix Fietkau 			ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
695a899b678SFelix Fietkau 		tsf_time += ath9k_hw_gettsf32(ah);
696a899b678SFelix Fietkau 
697d9092c98SSujith Manoharan 		sc->sched.beacon_adjust = false;
69842eda115SFelix Fietkau 		ath_chanctx_setup_timer(sc, tsf_time);
69958b57375SFelix Fietkau 		break;
700b8f9279bSSujith Manoharan 	case ATH_CHANCTX_EVENT_AUTHORIZED:
70173fa2f26SFelix Fietkau 		if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
70273fa2f26SFelix Fietkau 		    avp->chanctx != sc->cur_chan)
70373fa2f26SFelix Fietkau 			break;
70473fa2f26SFelix Fietkau 
705878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
706878066e7SSujith Manoharan 			"Move chanctx state from FORCE_ACTIVE to IDLE\n");
707878066e7SSujith Manoharan 
70873fa2f26SFelix Fietkau 		sc->sched.state = ATH_CHANCTX_STATE_IDLE;
709221af813SGustavo A. R. Silva 		fallthrough;
71073fa2f26SFelix Fietkau 	case ATH_CHANCTX_EVENT_SWITCH:
71173fa2f26SFelix Fietkau 		if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
71273fa2f26SFelix Fietkau 		    sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
71373fa2f26SFelix Fietkau 		    sc->cur_chan->switch_after_beacon ||
71473fa2f26SFelix Fietkau 		    sc->cur_chan == &sc->offchannel.chan)
71573fa2f26SFelix Fietkau 			break;
71673fa2f26SFelix Fietkau 
71773fa2f26SFelix Fietkau 		/* If this is a station chanctx, stay active for a half
71873fa2f26SFelix Fietkau 		 * beacon period (minus channel switch time)
71973fa2f26SFelix Fietkau 		 */
72073fa2f26SFelix Fietkau 		sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
7217414863eSFelix Fietkau 		cur_conf = &sc->cur_chan->beacon;
72273fa2f26SFelix Fietkau 
723878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
724878066e7SSujith Manoharan 			"Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
725878066e7SSujith Manoharan 
72673fa2f26SFelix Fietkau 		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
727367b341eSSujith Manoharan 		sc->sched.wait_switch = false;
728ec70abe1SFelix Fietkau 
729ec70abe1SFelix Fietkau 		tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
730167bf96dSSujith Manoharan 
731167bf96dSSujith Manoharan 		if (sc->sched.extend_absence) {
732ec70abe1SFelix Fietkau 			sc->sched.beacon_miss = 0;
733ec70abe1SFelix Fietkau 			tsf_time *= 3;
734ec70abe1SFelix Fietkau 		}
735ec70abe1SFelix Fietkau 
73673fa2f26SFelix Fietkau 		tsf_time -= sc->sched.channel_switch_time;
737ec70abe1SFelix Fietkau 		tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
73873fa2f26SFelix Fietkau 		sc->sched.switch_start_time = tsf_time;
73973fa2f26SFelix Fietkau 
74042eda115SFelix Fietkau 		ath_chanctx_setup_timer(sc, tsf_time);
741ec70abe1SFelix Fietkau 		sc->sched.beacon_pending = true;
742d9092c98SSujith Manoharan 		sc->sched.beacon_adjust = true;
74373fa2f26SFelix Fietkau 		break;
74473fa2f26SFelix Fietkau 	case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
74573fa2f26SFelix Fietkau 		if (sc->cur_chan == &sc->offchannel.chan ||
74673fa2f26SFelix Fietkau 		    sc->cur_chan->switch_after_beacon)
74773fa2f26SFelix Fietkau 			break;
74873fa2f26SFelix Fietkau 
74973fa2f26SFelix Fietkau 		sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
75073fa2f26SFelix Fietkau 		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
75173fa2f26SFelix Fietkau 		break;
75273fa2f26SFelix Fietkau 	case ATH_CHANCTX_EVENT_UNASSIGN:
75373fa2f26SFelix Fietkau 		if (sc->cur_chan->assigned) {
75473fa2f26SFelix Fietkau 			if (sc->next_chan && !sc->next_chan->assigned &&
75573fa2f26SFelix Fietkau 			    sc->next_chan != &sc->offchannel.chan)
75673fa2f26SFelix Fietkau 				sc->sched.state = ATH_CHANCTX_STATE_IDLE;
75773fa2f26SFelix Fietkau 			break;
75873fa2f26SFelix Fietkau 		}
75973fa2f26SFelix Fietkau 
76073fa2f26SFelix Fietkau 		ctx = ath_chanctx_get_next(sc, sc->cur_chan);
76173fa2f26SFelix Fietkau 		sc->sched.state = ATH_CHANCTX_STATE_IDLE;
76273fa2f26SFelix Fietkau 		if (!ctx->assigned)
76373fa2f26SFelix Fietkau 			break;
76473fa2f26SFelix Fietkau 
76573fa2f26SFelix Fietkau 		sc->next_chan = ctx;
76673fa2f26SFelix Fietkau 		ieee80211_queue_work(sc->hw, &sc->chanctx_work);
76773fa2f26SFelix Fietkau 		break;
76802da18b7SSujith Manoharan 	case ATH_CHANCTX_EVENT_ASSIGN:
76902da18b7SSujith Manoharan 		break;
77002da18b7SSujith Manoharan 	case ATH_CHANCTX_EVENT_CHANGE:
77102da18b7SSujith Manoharan 		break;
772748299f2SFelix Fietkau 	}
773748299f2SFelix Fietkau 
774748299f2SFelix Fietkau 	spin_unlock_bh(&sc->chan_lock);
775748299f2SFelix Fietkau }
776dfcbb3e8SSujith Manoharan 
ath_chanctx_beacon_sent_ev(struct ath_softc * sc,enum ath_chanctx_event ev)77770b06dacSSujith Manoharan void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
77870b06dacSSujith Manoharan 				enum ath_chanctx_event ev)
77970b06dacSSujith Manoharan {
78070b06dacSSujith Manoharan 	if (sc->sched.beacon_pending)
78170b06dacSSujith Manoharan 		ath_chanctx_event(sc, NULL, ev);
78270b06dacSSujith Manoharan }
78370b06dacSSujith Manoharan 
ath_chanctx_beacon_recv_ev(struct ath_softc * sc,enum ath_chanctx_event ev)784a2b28601SSujith Manoharan void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
78570b06dacSSujith Manoharan 				enum ath_chanctx_event ev)
78670b06dacSSujith Manoharan {
78770b06dacSSujith Manoharan 	ath_chanctx_event(sc, NULL, ev);
78870b06dacSSujith Manoharan }
78970b06dacSSujith Manoharan 
ath_scan_channel_duration(struct ath_softc * sc,struct ieee80211_channel * chan)790dfcbb3e8SSujith Manoharan static int ath_scan_channel_duration(struct ath_softc *sc,
791dfcbb3e8SSujith Manoharan 				     struct ieee80211_channel *chan)
792dfcbb3e8SSujith Manoharan {
793dfcbb3e8SSujith Manoharan 	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
794dfcbb3e8SSujith Manoharan 
795dfcbb3e8SSujith Manoharan 	if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
796dfcbb3e8SSujith Manoharan 		return (HZ / 9); /* ~110 ms */
797dfcbb3e8SSujith Manoharan 
798dfcbb3e8SSujith Manoharan 	return (HZ / 16); /* ~60 ms */
799dfcbb3e8SSujith Manoharan }
800dfcbb3e8SSujith Manoharan 
ath_chanctx_switch(struct ath_softc * sc,struct ath_chanctx * ctx,struct cfg80211_chan_def * chandef)801922c943dSSujith Manoharan static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
802922c943dSSujith Manoharan 			       struct cfg80211_chan_def *chandef)
803922c943dSSujith Manoharan {
804922c943dSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
805922c943dSSujith Manoharan 
806922c943dSSujith Manoharan 	spin_lock_bh(&sc->chan_lock);
807922c943dSSujith Manoharan 
808922c943dSSujith Manoharan 	if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
809922c943dSSujith Manoharan 	    (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
810da0162f3SSujith Manoharan 		if (chandef)
811da0162f3SSujith Manoharan 			ctx->chandef = *chandef;
812cbc775dbSSujith Manoharan 
813cbc775dbSSujith Manoharan 		sc->sched.offchannel_pending = true;
814cbc775dbSSujith Manoharan 		sc->sched.wait_switch = true;
815cbc775dbSSujith Manoharan 		sc->sched.offchannel_duration =
816cbc775dbSSujith Manoharan 			jiffies_to_usecs(sc->offchannel.duration) +
817cbc775dbSSujith Manoharan 			sc->sched.channel_switch_time;
818cbc775dbSSujith Manoharan 
819922c943dSSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
820da0162f3SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
821da0162f3SSujith Manoharan 			"Set offchannel_pending to true\n");
822922c943dSSujith Manoharan 		return;
823922c943dSSujith Manoharan 	}
824922c943dSSujith Manoharan 
825922c943dSSujith Manoharan 	sc->next_chan = ctx;
826922c943dSSujith Manoharan 	if (chandef) {
827922c943dSSujith Manoharan 		ctx->chandef = *chandef;
828922c943dSSujith Manoharan 		ath_dbg(common, CHAN_CTX,
829922c943dSSujith Manoharan 			"Assigned next_chan to %d MHz\n", chandef->center_freq1);
830922c943dSSujith Manoharan 	}
831922c943dSSujith Manoharan 
832922c943dSSujith Manoharan 	if (sc->next_chan == &sc->offchannel.chan) {
833922c943dSSujith Manoharan 		sc->sched.offchannel_duration =
834bb628eb9SSujith Manoharan 			jiffies_to_usecs(sc->offchannel.duration) +
835922c943dSSujith Manoharan 			sc->sched.channel_switch_time;
836922c943dSSujith Manoharan 
837922c943dSSujith Manoharan 		if (chandef) {
838922c943dSSujith Manoharan 			ath_dbg(common, CHAN_CTX,
839922c943dSSujith Manoharan 				"Offchannel duration for chan %d MHz : %u\n",
840922c943dSSujith Manoharan 				chandef->center_freq1,
841922c943dSSujith Manoharan 				sc->sched.offchannel_duration);
842922c943dSSujith Manoharan 		}
843922c943dSSujith Manoharan 	}
844922c943dSSujith Manoharan 	spin_unlock_bh(&sc->chan_lock);
845922c943dSSujith Manoharan 	ieee80211_queue_work(sc->hw, &sc->chanctx_work);
846922c943dSSujith Manoharan }
847922c943dSSujith Manoharan 
ath_chanctx_offchan_switch(struct ath_softc * sc,struct ieee80211_channel * chan)848344ae6abSSujith Manoharan static void ath_chanctx_offchan_switch(struct ath_softc *sc,
849344ae6abSSujith Manoharan 				       struct ieee80211_channel *chan)
850344ae6abSSujith Manoharan {
851344ae6abSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
852344ae6abSSujith Manoharan 	struct cfg80211_chan_def chandef;
853344ae6abSSujith Manoharan 
854344ae6abSSujith Manoharan 	cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
855344ae6abSSujith Manoharan 	ath_dbg(common, CHAN_CTX,
856344ae6abSSujith Manoharan 		"Channel definition created: %d MHz\n", chandef.center_freq1);
857344ae6abSSujith Manoharan 
858344ae6abSSujith Manoharan 	ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
859344ae6abSSujith Manoharan }
860344ae6abSSujith Manoharan 
ath_chanctx_get_oper_chan(struct ath_softc * sc,bool active)86198f411b8SSujith Manoharan static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
86298f411b8SSujith Manoharan 						     bool active)
86398f411b8SSujith Manoharan {
86498f411b8SSujith Manoharan 	struct ath_chanctx *ctx;
86598f411b8SSujith Manoharan 
86698f411b8SSujith Manoharan 	ath_for_each_chanctx(sc, ctx) {
86798f411b8SSujith Manoharan 		if (!ctx->assigned || list_empty(&ctx->vifs))
86898f411b8SSujith Manoharan 			continue;
86998f411b8SSujith Manoharan 		if (active && !ctx->active)
87098f411b8SSujith Manoharan 			continue;
87198f411b8SSujith Manoharan 
87298f411b8SSujith Manoharan 		if (ctx->switch_after_beacon)
87398f411b8SSujith Manoharan 			return ctx;
87498f411b8SSujith Manoharan 	}
87598f411b8SSujith Manoharan 
87698f411b8SSujith Manoharan 	return &sc->chanctx[0];
87798f411b8SSujith Manoharan }
87898f411b8SSujith Manoharan 
879dfcbb3e8SSujith Manoharan static void
ath_scan_next_channel(struct ath_softc * sc)880dfcbb3e8SSujith Manoharan ath_scan_next_channel(struct ath_softc *sc)
881dfcbb3e8SSujith Manoharan {
882bc81d43aSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
883dfcbb3e8SSujith Manoharan 	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
884dfcbb3e8SSujith Manoharan 	struct ieee80211_channel *chan;
885dfcbb3e8SSujith Manoharan 
886dfcbb3e8SSujith Manoharan 	if (sc->offchannel.scan_idx >= req->n_channels) {
887bc81d43aSSujith Manoharan 		ath_dbg(common, CHAN_CTX,
888878066e7SSujith Manoharan 			"Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
889878066e7SSujith Manoharan 			"scan_idx: %d, n_channels: %d\n",
890bc81d43aSSujith Manoharan 			sc->offchannel.scan_idx,
891bc81d43aSSujith Manoharan 			req->n_channels);
892bc81d43aSSujith Manoharan 
893dfcbb3e8SSujith Manoharan 		sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
894dfcbb3e8SSujith Manoharan 		ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
895dfcbb3e8SSujith Manoharan 				   NULL);
896dfcbb3e8SSujith Manoharan 		return;
897dfcbb3e8SSujith Manoharan 	}
898dfcbb3e8SSujith Manoharan 
899bc81d43aSSujith Manoharan 	ath_dbg(common, CHAN_CTX,
900878066e7SSujith Manoharan 		"Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
901bc81d43aSSujith Manoharan 		sc->offchannel.scan_idx);
902bc81d43aSSujith Manoharan 
903dfcbb3e8SSujith Manoharan 	chan = req->channels[sc->offchannel.scan_idx++];
904dfcbb3e8SSujith Manoharan 	sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
905dfcbb3e8SSujith Manoharan 	sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
906bc81d43aSSujith Manoharan 
907dfcbb3e8SSujith Manoharan 	ath_chanctx_offchan_switch(sc, chan);
908dfcbb3e8SSujith Manoharan }
909dfcbb3e8SSujith Manoharan 
ath_offchannel_next(struct ath_softc * sc)910dfcbb3e8SSujith Manoharan void ath_offchannel_next(struct ath_softc *sc)
911dfcbb3e8SSujith Manoharan {
912dfcbb3e8SSujith Manoharan 	struct ieee80211_vif *vif;
913dfcbb3e8SSujith Manoharan 
914dfcbb3e8SSujith Manoharan 	if (sc->offchannel.scan_req) {
915dfcbb3e8SSujith Manoharan 		vif = sc->offchannel.scan_vif;
916dfcbb3e8SSujith Manoharan 		sc->offchannel.chan.txpower = vif->bss_conf.txpower;
917dfcbb3e8SSujith Manoharan 		ath_scan_next_channel(sc);
918dfcbb3e8SSujith Manoharan 	} else if (sc->offchannel.roc_vif) {
919dfcbb3e8SSujith Manoharan 		vif = sc->offchannel.roc_vif;
920dfcbb3e8SSujith Manoharan 		sc->offchannel.chan.txpower = vif->bss_conf.txpower;
921bb628eb9SSujith Manoharan 		sc->offchannel.duration =
922bb628eb9SSujith Manoharan 			msecs_to_jiffies(sc->offchannel.roc_duration);
923dfcbb3e8SSujith Manoharan 		sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
924dfcbb3e8SSujith Manoharan 		ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
925dfcbb3e8SSujith Manoharan 	} else {
926e21a1d8bSSujith Manoharan 		spin_lock_bh(&sc->chan_lock);
927e21a1d8bSSujith Manoharan 		sc->sched.offchannel_pending = false;
928e21a1d8bSSujith Manoharan 		sc->sched.wait_switch = false;
929e21a1d8bSSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
930e21a1d8bSSujith Manoharan 
931dfcbb3e8SSujith Manoharan 		ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
932dfcbb3e8SSujith Manoharan 				   NULL);
933dfcbb3e8SSujith Manoharan 		sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
934dfcbb3e8SSujith Manoharan 		if (sc->ps_idle)
935dfcbb3e8SSujith Manoharan 			ath_cancel_work(sc);
936dfcbb3e8SSujith Manoharan 	}
937dfcbb3e8SSujith Manoharan }
938dfcbb3e8SSujith Manoharan 
ath_roc_complete(struct ath_softc * sc,enum ath_roc_complete_reason reason)939d83520b7SJanusz.Dziedzic@tieto.com void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
940dfcbb3e8SSujith Manoharan {
9414b60af4aSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
9424b60af4aSSujith Manoharan 
943dfcbb3e8SSujith Manoharan 	sc->offchannel.roc_vif = NULL;
944dfcbb3e8SSujith Manoharan 	sc->offchannel.roc_chan = NULL;
945d83520b7SJanusz.Dziedzic@tieto.com 
946d83520b7SJanusz.Dziedzic@tieto.com 	switch (reason) {
947d83520b7SJanusz.Dziedzic@tieto.com 	case ATH_ROC_COMPLETE_ABORT:
948d83520b7SJanusz.Dziedzic@tieto.com 		ath_dbg(common, CHAN_CTX, "RoC aborted\n");
949dfcbb3e8SSujith Manoharan 		ieee80211_remain_on_channel_expired(sc->hw);
950d83520b7SJanusz.Dziedzic@tieto.com 		break;
951d83520b7SJanusz.Dziedzic@tieto.com 	case ATH_ROC_COMPLETE_EXPIRE:
952d83520b7SJanusz.Dziedzic@tieto.com 		ath_dbg(common, CHAN_CTX, "RoC expired\n");
953d83520b7SJanusz.Dziedzic@tieto.com 		ieee80211_remain_on_channel_expired(sc->hw);
954d83520b7SJanusz.Dziedzic@tieto.com 		break;
955d83520b7SJanusz.Dziedzic@tieto.com 	case ATH_ROC_COMPLETE_CANCEL:
956d83520b7SJanusz.Dziedzic@tieto.com 		ath_dbg(common, CHAN_CTX, "RoC canceled\n");
957d83520b7SJanusz.Dziedzic@tieto.com 		break;
958d83520b7SJanusz.Dziedzic@tieto.com 	}
959d83520b7SJanusz.Dziedzic@tieto.com 
960dfcbb3e8SSujith Manoharan 	ath_offchannel_next(sc);
961dfcbb3e8SSujith Manoharan 	ath9k_ps_restore(sc);
962dfcbb3e8SSujith Manoharan }
963dfcbb3e8SSujith Manoharan 
ath_scan_complete(struct ath_softc * sc,bool abort)964dfcbb3e8SSujith Manoharan void ath_scan_complete(struct ath_softc *sc, bool abort)
965dfcbb3e8SSujith Manoharan {
966dfcbb3e8SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
9677947d3e0SAvraham Stern 	struct cfg80211_scan_info info = {
9687947d3e0SAvraham Stern 		.aborted = abort,
9697947d3e0SAvraham Stern 	};
970dfcbb3e8SSujith Manoharan 
971bc81d43aSSujith Manoharan 	if (abort)
972bc81d43aSSujith Manoharan 		ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
973bc81d43aSSujith Manoharan 	else
974bc81d43aSSujith Manoharan 		ath_dbg(common, CHAN_CTX, "HW scan complete\n");
975bc81d43aSSujith Manoharan 
976dfcbb3e8SSujith Manoharan 	sc->offchannel.scan_req = NULL;
977dfcbb3e8SSujith Manoharan 	sc->offchannel.scan_vif = NULL;
978dfcbb3e8SSujith Manoharan 	sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
9797947d3e0SAvraham Stern 	ieee80211_scan_completed(sc->hw, &info);
980dfcbb3e8SSujith Manoharan 	clear_bit(ATH_OP_SCANNING, &common->op_flags);
981d0975eddSSujith Manoharan 	spin_lock_bh(&sc->chan_lock);
982d0975eddSSujith Manoharan 	if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
983d0975eddSSujith Manoharan 		sc->sched.force_noa_update = true;
984d0975eddSSujith Manoharan 	spin_unlock_bh(&sc->chan_lock);
985dfcbb3e8SSujith Manoharan 	ath_offchannel_next(sc);
986dfcbb3e8SSujith Manoharan 	ath9k_ps_restore(sc);
987dfcbb3e8SSujith Manoharan }
988dfcbb3e8SSujith Manoharan 
ath_scan_send_probe(struct ath_softc * sc,struct cfg80211_ssid * ssid)989dfcbb3e8SSujith Manoharan static void ath_scan_send_probe(struct ath_softc *sc,
990dfcbb3e8SSujith Manoharan 				struct cfg80211_ssid *ssid)
991dfcbb3e8SSujith Manoharan {
992dfcbb3e8SSujith Manoharan 	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
993dfcbb3e8SSujith Manoharan 	struct ieee80211_vif *vif = sc->offchannel.scan_vif;
994dfcbb3e8SSujith Manoharan 	struct ath_tx_control txctl = {};
995dfcbb3e8SSujith Manoharan 	struct sk_buff *skb;
996dfcbb3e8SSujith Manoharan 	struct ieee80211_tx_info *info;
997dfcbb3e8SSujith Manoharan 	int band = sc->offchannel.chan.chandef.chan->band;
998dfcbb3e8SSujith Manoharan 
999a344d677SJohannes Berg 	skb = ieee80211_probereq_get(sc->hw, vif->addr,
1000dfcbb3e8SSujith Manoharan 			ssid->ssid, ssid->ssid_len, req->ie_len);
1001dfcbb3e8SSujith Manoharan 	if (!skb)
1002dfcbb3e8SSujith Manoharan 		return;
1003dfcbb3e8SSujith Manoharan 
1004dfcbb3e8SSujith Manoharan 	info = IEEE80211_SKB_CB(skb);
1005dfcbb3e8SSujith Manoharan 	if (req->no_cck)
1006dfcbb3e8SSujith Manoharan 		info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1007dfcbb3e8SSujith Manoharan 
1008dfcbb3e8SSujith Manoharan 	if (req->ie_len)
100959ae1d12SJohannes Berg 		skb_put_data(skb, req->ie, req->ie_len);
1010dfcbb3e8SSujith Manoharan 
1011dfcbb3e8SSujith Manoharan 	skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1012dfcbb3e8SSujith Manoharan 
1013dfcbb3e8SSujith Manoharan 	if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1014dfcbb3e8SSujith Manoharan 		goto error;
1015dfcbb3e8SSujith Manoharan 
1016dfcbb3e8SSujith Manoharan 	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1017dfcbb3e8SSujith Manoharan 	if (ath_tx_start(sc->hw, skb, &txctl))
1018dfcbb3e8SSujith Manoharan 		goto error;
1019dfcbb3e8SSujith Manoharan 
1020dfcbb3e8SSujith Manoharan 	return;
1021dfcbb3e8SSujith Manoharan 
1022dfcbb3e8SSujith Manoharan error:
1023dfcbb3e8SSujith Manoharan 	ieee80211_free_txskb(sc->hw, skb);
1024dfcbb3e8SSujith Manoharan }
1025dfcbb3e8SSujith Manoharan 
ath_scan_channel_start(struct ath_softc * sc)1026dfcbb3e8SSujith Manoharan static void ath_scan_channel_start(struct ath_softc *sc)
1027dfcbb3e8SSujith Manoharan {
1028bc81d43aSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1029dfcbb3e8SSujith Manoharan 	struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1030dfcbb3e8SSujith Manoharan 	int i;
1031dfcbb3e8SSujith Manoharan 
1032dfcbb3e8SSujith Manoharan 	if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1033dfcbb3e8SSujith Manoharan 	    req->n_ssids) {
1034dfcbb3e8SSujith Manoharan 		for (i = 0; i < req->n_ssids; i++)
1035dfcbb3e8SSujith Manoharan 			ath_scan_send_probe(sc, &req->ssids[i]);
1036dfcbb3e8SSujith Manoharan 
1037dfcbb3e8SSujith Manoharan 	}
1038dfcbb3e8SSujith Manoharan 
1039bc81d43aSSujith Manoharan 	ath_dbg(common, CHAN_CTX,
1040878066e7SSujith Manoharan 		"Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1041bc81d43aSSujith Manoharan 
1042dfcbb3e8SSujith Manoharan 	sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1043dfcbb3e8SSujith Manoharan 	mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1044dfcbb3e8SSujith Manoharan }
1045dfcbb3e8SSujith Manoharan 
ath_chanctx_timer(struct timer_list * t)10467ac76764SKees Cook static void ath_chanctx_timer(struct timer_list *t)
1047705d0bf8SSujith Manoharan {
10487ac76764SKees Cook 	struct ath_softc *sc = from_timer(sc, t, sched.timer);
1049878066e7SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1050878066e7SSujith Manoharan 
1051878066e7SSujith Manoharan 	ath_dbg(common, CHAN_CTX,
1052878066e7SSujith Manoharan 		"Channel context timer invoked\n");
1053705d0bf8SSujith Manoharan 
1054705d0bf8SSujith Manoharan 	ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1055705d0bf8SSujith Manoharan }
1056705d0bf8SSujith Manoharan 
ath_offchannel_timer(struct timer_list * t)10577ac76764SKees Cook static void ath_offchannel_timer(struct timer_list *t)
1058dfcbb3e8SSujith Manoharan {
10597ac76764SKees Cook 	struct ath_softc *sc = from_timer(sc, t, offchannel.timer);
1060dfcbb3e8SSujith Manoharan 	struct ath_chanctx *ctx;
1061bc81d43aSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1062bc81d43aSSujith Manoharan 
1063878066e7SSujith Manoharan 	ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1064bc81d43aSSujith Manoharan 		__func__, offchannel_state_string(sc->offchannel.state));
1065dfcbb3e8SSujith Manoharan 
1066dfcbb3e8SSujith Manoharan 	switch (sc->offchannel.state) {
1067dfcbb3e8SSujith Manoharan 	case ATH_OFFCHANNEL_PROBE_WAIT:
1068dfcbb3e8SSujith Manoharan 		if (!sc->offchannel.scan_req)
1069dfcbb3e8SSujith Manoharan 			return;
1070dfcbb3e8SSujith Manoharan 
1071dfcbb3e8SSujith Manoharan 		/* get first active channel context */
1072dfcbb3e8SSujith Manoharan 		ctx = ath_chanctx_get_oper_chan(sc, true);
1073dfcbb3e8SSujith Manoharan 		if (ctx->active) {
1074878066e7SSujith Manoharan 			ath_dbg(common, CHAN_CTX,
1075878066e7SSujith Manoharan 				"Switch to oper/active context, "
1076878066e7SSujith Manoharan 				"move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1077878066e7SSujith Manoharan 
1078dfcbb3e8SSujith Manoharan 			sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1079dfcbb3e8SSujith Manoharan 			ath_chanctx_switch(sc, ctx, NULL);
1080dfcbb3e8SSujith Manoharan 			mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1081dfcbb3e8SSujith Manoharan 			break;
1082dfcbb3e8SSujith Manoharan 		}
1083221af813SGustavo A. R. Silva 		fallthrough;
1084dfcbb3e8SSujith Manoharan 	case ATH_OFFCHANNEL_SUSPEND:
1085dfcbb3e8SSujith Manoharan 		if (!sc->offchannel.scan_req)
1086dfcbb3e8SSujith Manoharan 			return;
1087dfcbb3e8SSujith Manoharan 
1088dfcbb3e8SSujith Manoharan 		ath_scan_next_channel(sc);
1089dfcbb3e8SSujith Manoharan 		break;
1090dfcbb3e8SSujith Manoharan 	case ATH_OFFCHANNEL_ROC_START:
1091dfcbb3e8SSujith Manoharan 	case ATH_OFFCHANNEL_ROC_WAIT:
1092dfcbb3e8SSujith Manoharan 		sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1093d83520b7SJanusz.Dziedzic@tieto.com 		ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
1094dfcbb3e8SSujith Manoharan 		break;
1095dfcbb3e8SSujith Manoharan 	default:
1096dfcbb3e8SSujith Manoharan 		break;
1097dfcbb3e8SSujith Manoharan 	}
1098dfcbb3e8SSujith Manoharan }
10992471adffSSujith Manoharan 
11006d7cbd77SSujith Manoharan static bool
ath_chanctx_send_vif_ps_frame(struct ath_softc * sc,struct ath_vif * avp,bool powersave)11016d7cbd77SSujith Manoharan ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
11026d7cbd77SSujith Manoharan 			      bool powersave)
11036d7cbd77SSujith Manoharan {
11046d7cbd77SSujith Manoharan 	struct ieee80211_vif *vif = avp->vif;
11056d7cbd77SSujith Manoharan 	struct ieee80211_sta *sta = NULL;
11066d7cbd77SSujith Manoharan 	struct ieee80211_hdr_3addr *nullfunc;
11076d7cbd77SSujith Manoharan 	struct ath_tx_control txctl;
11086d7cbd77SSujith Manoharan 	struct sk_buff *skb;
11096d7cbd77SSujith Manoharan 	int band = sc->cur_chan->chandef.chan->band;
11106d7cbd77SSujith Manoharan 
11116d7cbd77SSujith Manoharan 	switch (vif->type) {
11126d7cbd77SSujith Manoharan 	case NL80211_IFTYPE_STATION:
1113cb35582aSSujith Manoharan 		if (!avp->assoc)
11146d7cbd77SSujith Manoharan 			return false;
11156d7cbd77SSujith Manoharan 
1116*0ab26380SJohannes Berg 		skb = ieee80211_nullfunc_get(sc->hw, vif, -1, false);
11176d7cbd77SSujith Manoharan 		if (!skb)
11186d7cbd77SSujith Manoharan 			return false;
11196d7cbd77SSujith Manoharan 
11206d7cbd77SSujith Manoharan 		nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
11216d7cbd77SSujith Manoharan 		if (powersave)
11226d7cbd77SSujith Manoharan 			nullfunc->frame_control |=
11236d7cbd77SSujith Manoharan 				cpu_to_le16(IEEE80211_FCTL_PM);
11246d7cbd77SSujith Manoharan 
1125c1b7bea0SJanusz Dziedzic 		skb->priority = 7;
11266d7cbd77SSujith Manoharan 		skb_set_queue_mapping(skb, IEEE80211_AC_VO);
11276d7cbd77SSujith Manoharan 		if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
11286d7cbd77SSujith Manoharan 			dev_kfree_skb_any(skb);
11296d7cbd77SSujith Manoharan 			return false;
11306d7cbd77SSujith Manoharan 		}
11316d7cbd77SSujith Manoharan 		break;
11326d7cbd77SSujith Manoharan 	default:
11336d7cbd77SSujith Manoharan 		return false;
11346d7cbd77SSujith Manoharan 	}
11356d7cbd77SSujith Manoharan 
11366d7cbd77SSujith Manoharan 	memset(&txctl, 0, sizeof(txctl));
11376d7cbd77SSujith Manoharan 	txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
11386d7cbd77SSujith Manoharan 	txctl.sta = sta;
11396d7cbd77SSujith Manoharan 	if (ath_tx_start(sc->hw, skb, &txctl)) {
11406d7cbd77SSujith Manoharan 		ieee80211_free_txskb(sc->hw, skb);
11416d7cbd77SSujith Manoharan 		return false;
11426d7cbd77SSujith Manoharan 	}
11436d7cbd77SSujith Manoharan 
11446d7cbd77SSujith Manoharan 	return true;
11456d7cbd77SSujith Manoharan }
11466d7cbd77SSujith Manoharan 
11476d7cbd77SSujith Manoharan static bool
ath_chanctx_send_ps_frame(struct ath_softc * sc,bool powersave)11486d7cbd77SSujith Manoharan ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
11496d7cbd77SSujith Manoharan {
11506d7cbd77SSujith Manoharan 	struct ath_vif *avp;
11516d7cbd77SSujith Manoharan 	bool sent = false;
11526d7cbd77SSujith Manoharan 
11536d7cbd77SSujith Manoharan 	rcu_read_lock();
11546d7cbd77SSujith Manoharan 	list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
11556d7cbd77SSujith Manoharan 		if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
11566d7cbd77SSujith Manoharan 			sent = true;
11576d7cbd77SSujith Manoharan 	}
11586d7cbd77SSujith Manoharan 	rcu_read_unlock();
11596d7cbd77SSujith Manoharan 
11606d7cbd77SSujith Manoharan 	return sent;
11616d7cbd77SSujith Manoharan }
11626d7cbd77SSujith Manoharan 
ath_chanctx_defer_switch(struct ath_softc * sc)11636d7cbd77SSujith Manoharan static bool ath_chanctx_defer_switch(struct ath_softc *sc)
11646d7cbd77SSujith Manoharan {
1165878066e7SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1166878066e7SSujith Manoharan 
11676d7cbd77SSujith Manoharan 	if (sc->cur_chan == &sc->offchannel.chan)
11686d7cbd77SSujith Manoharan 		return false;
11696d7cbd77SSujith Manoharan 
11706d7cbd77SSujith Manoharan 	switch (sc->sched.state) {
11716d7cbd77SSujith Manoharan 	case ATH_CHANCTX_STATE_SWITCH:
11726d7cbd77SSujith Manoharan 		return false;
11736d7cbd77SSujith Manoharan 	case ATH_CHANCTX_STATE_IDLE:
11746d7cbd77SSujith Manoharan 		if (!sc->cur_chan->switch_after_beacon)
11756d7cbd77SSujith Manoharan 			return false;
11766d7cbd77SSujith Manoharan 
1177878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
1178878066e7SSujith Manoharan 			"Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1179878066e7SSujith Manoharan 
11806d7cbd77SSujith Manoharan 		sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
11816d7cbd77SSujith Manoharan 		break;
11826d7cbd77SSujith Manoharan 	default:
11836d7cbd77SSujith Manoharan 		break;
11846d7cbd77SSujith Manoharan 	}
11856d7cbd77SSujith Manoharan 
11866d7cbd77SSujith Manoharan 	return true;
11876d7cbd77SSujith Manoharan }
11886d7cbd77SSujith Manoharan 
ath_offchannel_channel_change(struct ath_softc * sc)118955254eeaSSujith Manoharan static void ath_offchannel_channel_change(struct ath_softc *sc)
119055254eeaSSujith Manoharan {
119155254eeaSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
119255254eeaSSujith Manoharan 
1193878066e7SSujith Manoharan 	ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
119455254eeaSSujith Manoharan 		__func__, offchannel_state_string(sc->offchannel.state));
119555254eeaSSujith Manoharan 
119655254eeaSSujith Manoharan 	switch (sc->offchannel.state) {
119755254eeaSSujith Manoharan 	case ATH_OFFCHANNEL_PROBE_SEND:
119855254eeaSSujith Manoharan 		if (!sc->offchannel.scan_req)
119955254eeaSSujith Manoharan 			return;
120055254eeaSSujith Manoharan 
120155254eeaSSujith Manoharan 		if (sc->cur_chan->chandef.chan !=
120255254eeaSSujith Manoharan 		    sc->offchannel.chan.chandef.chan)
120355254eeaSSujith Manoharan 			return;
120455254eeaSSujith Manoharan 
120555254eeaSSujith Manoharan 		ath_scan_channel_start(sc);
120655254eeaSSujith Manoharan 		break;
120755254eeaSSujith Manoharan 	case ATH_OFFCHANNEL_IDLE:
120855254eeaSSujith Manoharan 		if (!sc->offchannel.scan_req)
120955254eeaSSujith Manoharan 			return;
121055254eeaSSujith Manoharan 
121155254eeaSSujith Manoharan 		ath_scan_complete(sc, false);
121255254eeaSSujith Manoharan 		break;
121355254eeaSSujith Manoharan 	case ATH_OFFCHANNEL_ROC_START:
121455254eeaSSujith Manoharan 		if (sc->cur_chan != &sc->offchannel.chan)
121555254eeaSSujith Manoharan 			break;
121655254eeaSSujith Manoharan 
121755254eeaSSujith Manoharan 		sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1218bb628eb9SSujith Manoharan 		mod_timer(&sc->offchannel.timer,
1219bb628eb9SSujith Manoharan 			  jiffies + sc->offchannel.duration);
122055254eeaSSujith Manoharan 		ieee80211_ready_on_channel(sc->hw);
122155254eeaSSujith Manoharan 		break;
122255254eeaSSujith Manoharan 	case ATH_OFFCHANNEL_ROC_DONE:
122355254eeaSSujith Manoharan 		break;
122455254eeaSSujith Manoharan 	default:
122555254eeaSSujith Manoharan 		break;
122655254eeaSSujith Manoharan 	}
122755254eeaSSujith Manoharan }
122855254eeaSSujith Manoharan 
ath_chanctx_set_next(struct ath_softc * sc,bool force)12296d7cbd77SSujith Manoharan void ath_chanctx_set_next(struct ath_softc *sc, bool force)
12306d7cbd77SSujith Manoharan {
1231878066e7SSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1232e2cba8d7SSujith Manoharan 	struct ath_chanctx *old_ctx;
1233fe041debSArnd Bergmann 	struct timespec64 ts;
12346d7cbd77SSujith Manoharan 	bool measure_time = false;
12356d7cbd77SSujith Manoharan 	bool send_ps = false;
1236e2cba8d7SSujith Manoharan 	bool queues_stopped = false;
12376d7cbd77SSujith Manoharan 
12386d7cbd77SSujith Manoharan 	spin_lock_bh(&sc->chan_lock);
12396d7cbd77SSujith Manoharan 	if (!sc->next_chan) {
12406d7cbd77SSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
12416d7cbd77SSujith Manoharan 		return;
12426d7cbd77SSujith Manoharan 	}
12436d7cbd77SSujith Manoharan 
12446d7cbd77SSujith Manoharan 	if (!force && ath_chanctx_defer_switch(sc)) {
12456d7cbd77SSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
12466d7cbd77SSujith Manoharan 		return;
12476d7cbd77SSujith Manoharan 	}
12486d7cbd77SSujith Manoharan 
1249878066e7SSujith Manoharan 	ath_dbg(common, CHAN_CTX,
1250878066e7SSujith Manoharan 		"%s: current: %d MHz, next: %d MHz\n",
1251878066e7SSujith Manoharan 		__func__,
1252878066e7SSujith Manoharan 		sc->cur_chan->chandef.center_freq1,
1253878066e7SSujith Manoharan 		sc->next_chan->chandef.center_freq1);
1254878066e7SSujith Manoharan 
12556d7cbd77SSujith Manoharan 	if (sc->cur_chan != sc->next_chan) {
1256878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
1257878066e7SSujith Manoharan 			"Stopping current chanctx: %d\n",
1258878066e7SSujith Manoharan 			sc->cur_chan->chandef.center_freq1);
12596d7cbd77SSujith Manoharan 		sc->cur_chan->stopped = true;
12606d7cbd77SSujith Manoharan 		spin_unlock_bh(&sc->chan_lock);
12616d7cbd77SSujith Manoharan 
12626d7cbd77SSujith Manoharan 		if (sc->next_chan == &sc->offchannel.chan) {
1263fe041debSArnd Bergmann 			ktime_get_raw_ts64(&ts);
12646d7cbd77SSujith Manoharan 			measure_time = true;
12656d7cbd77SSujith Manoharan 		}
1266e2cba8d7SSujith Manoharan 
1267e2cba8d7SSujith Manoharan 		ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1268e2cba8d7SSujith Manoharan 		queues_stopped = true;
1269e2cba8d7SSujith Manoharan 
127025f3bc7dSSujith Manoharan 		__ath9k_flush(sc->hw, ~0, true, false, false);
12716d7cbd77SSujith Manoharan 
12726d7cbd77SSujith Manoharan 		if (ath_chanctx_send_ps_frame(sc, true))
1273e2d389b5SSujith Manoharan 			__ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
127425f3bc7dSSujith Manoharan 				      false, false, false);
12756d7cbd77SSujith Manoharan 
12766d7cbd77SSujith Manoharan 		send_ps = true;
12776d7cbd77SSujith Manoharan 		spin_lock_bh(&sc->chan_lock);
12786d7cbd77SSujith Manoharan 
12796d7cbd77SSujith Manoharan 		if (sc->cur_chan != &sc->offchannel.chan) {
1280fe041debSArnd Bergmann 			ktime_get_raw_ts64(&sc->cur_chan->tsf_ts);
12816d7cbd77SSujith Manoharan 			sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
12826d7cbd77SSujith Manoharan 		}
12836d7cbd77SSujith Manoharan 	}
1284e2cba8d7SSujith Manoharan 	old_ctx = sc->cur_chan;
12856d7cbd77SSujith Manoharan 	sc->cur_chan = sc->next_chan;
12866d7cbd77SSujith Manoharan 	sc->cur_chan->stopped = false;
12876d7cbd77SSujith Manoharan 	sc->next_chan = NULL;
1288124130d7SSujith Manoharan 
1289124130d7SSujith Manoharan 	if (!sc->sched.offchannel_pending)
12906d7cbd77SSujith Manoharan 		sc->sched.offchannel_duration = 0;
1291124130d7SSujith Manoharan 
12926d7cbd77SSujith Manoharan 	if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
12936d7cbd77SSujith Manoharan 		sc->sched.state = ATH_CHANCTX_STATE_IDLE;
12946d7cbd77SSujith Manoharan 
12956d7cbd77SSujith Manoharan 	spin_unlock_bh(&sc->chan_lock);
12966d7cbd77SSujith Manoharan 
12976d7cbd77SSujith Manoharan 	if (sc->sc_ah->chip_fullsleep ||
12986d7cbd77SSujith Manoharan 	    memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
12996d7cbd77SSujith Manoharan 		   sizeof(sc->cur_chandef))) {
1300878066e7SSujith Manoharan 		ath_dbg(common, CHAN_CTX,
1301878066e7SSujith Manoharan 			"%s: Set channel %d MHz\n",
1302878066e7SSujith Manoharan 			__func__, sc->cur_chan->chandef.center_freq1);
13036d7cbd77SSujith Manoharan 		ath_set_channel(sc);
13046d7cbd77SSujith Manoharan 		if (measure_time)
13056d7cbd77SSujith Manoharan 			sc->sched.channel_switch_time =
13066d7cbd77SSujith Manoharan 				ath9k_hw_get_tsf_offset(&ts, NULL);
1307e2cba8d7SSujith Manoharan 		/*
1308e2cba8d7SSujith Manoharan 		 * A reset will ensure that all queues are woken up,
1309e2cba8d7SSujith Manoharan 		 * so there is no need to awaken them again.
1310e2cba8d7SSujith Manoharan 		 */
1311e2cba8d7SSujith Manoharan 		goto out;
13126d7cbd77SSujith Manoharan 	}
1313e2cba8d7SSujith Manoharan 
1314e2cba8d7SSujith Manoharan 	if (queues_stopped)
1315e2cba8d7SSujith Manoharan 		ath9k_chanctx_wake_queues(sc, old_ctx);
1316e2cba8d7SSujith Manoharan out:
13176d7cbd77SSujith Manoharan 	if (send_ps)
13186d7cbd77SSujith Manoharan 		ath_chanctx_send_ps_frame(sc, false);
13196d7cbd77SSujith Manoharan 
13206d7cbd77SSujith Manoharan 	ath_offchannel_channel_change(sc);
13216d7cbd77SSujith Manoharan 	ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
13226d7cbd77SSujith Manoharan }
13236d7cbd77SSujith Manoharan 
ath_chanctx_work(struct work_struct * work)13240e62f8b7SSujith Manoharan static void ath_chanctx_work(struct work_struct *work)
13250e62f8b7SSujith Manoharan {
13260e62f8b7SSujith Manoharan 	struct ath_softc *sc = container_of(work, struct ath_softc,
13270e62f8b7SSujith Manoharan 					    chanctx_work);
13280e62f8b7SSujith Manoharan 	mutex_lock(&sc->mutex);
13290e62f8b7SSujith Manoharan 	ath_chanctx_set_next(sc, false);
13300e62f8b7SSujith Manoharan 	mutex_unlock(&sc->mutex);
13310e62f8b7SSujith Manoharan }
13320e62f8b7SSujith Manoharan 
ath9k_offchannel_init(struct ath_softc * sc)1333e90e302aSSujith Manoharan void ath9k_offchannel_init(struct ath_softc *sc)
1334e90e302aSSujith Manoharan {
1335e90e302aSSujith Manoharan 	struct ath_chanctx *ctx;
1336e90e302aSSujith Manoharan 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1337e90e302aSSujith Manoharan 	struct ieee80211_supported_band *sband;
1338e90e302aSSujith Manoharan 	struct ieee80211_channel *chan;
1339e90e302aSSujith Manoharan 	int i;
1340e90e302aSSujith Manoharan 
134157fbcce3SJohannes Berg 	sband = &common->sbands[NL80211_BAND_2GHZ];
1342e90e302aSSujith Manoharan 	if (!sband->n_channels)
134357fbcce3SJohannes Berg 		sband = &common->sbands[NL80211_BAND_5GHZ];
1344e90e302aSSujith Manoharan 
1345e90e302aSSujith Manoharan 	chan = &sband->channels[0];
1346e90e302aSSujith Manoharan 
1347e90e302aSSujith Manoharan 	ctx = &sc->offchannel.chan;
1348e90e302aSSujith Manoharan 	INIT_LIST_HEAD(&ctx->vifs);
1349e90e302aSSujith Manoharan 	ctx->txpower = ATH_TXPOWER_MAX;
1350e90e302aSSujith Manoharan 	cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1351e90e302aSSujith Manoharan 
135263fefa05SToke Høiland-Jørgensen 	for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
135363fefa05SToke Høiland-Jørgensen 		INIT_LIST_HEAD(&ctx->acq[i].acq_new);
135463fefa05SToke Høiland-Jørgensen 		INIT_LIST_HEAD(&ctx->acq[i].acq_old);
135563fefa05SToke Høiland-Jørgensen 		spin_lock_init(&ctx->acq[i].lock);
135663fefa05SToke Høiland-Jørgensen 	}
1357e90e302aSSujith Manoharan 
1358e90e302aSSujith Manoharan 	sc->offchannel.chan.offchannel = true;
1359e90e302aSSujith Manoharan }
1360e90e302aSSujith Manoharan 
ath9k_init_channel_context(struct ath_softc * sc)1361705d0bf8SSujith Manoharan void ath9k_init_channel_context(struct ath_softc *sc)
1362705d0bf8SSujith Manoharan {
1363705d0bf8SSujith Manoharan 	INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1364705d0bf8SSujith Manoharan 
13657ac76764SKees Cook 	timer_setup(&sc->offchannel.timer, ath_offchannel_timer, 0);
13667ac76764SKees Cook 	timer_setup(&sc->sched.timer, ath_chanctx_timer, 0);
1367c6500ea2SSujith Manoharan 
1368c6500ea2SSujith Manoharan 	init_completion(&sc->go_beacon);
1369705d0bf8SSujith Manoharan }
1370c7dd40c9SSujith Manoharan 
ath9k_deinit_channel_context(struct ath_softc * sc)1371ea22df29SSujith Manoharan void ath9k_deinit_channel_context(struct ath_softc *sc)
1372ea22df29SSujith Manoharan {
1373ea22df29SSujith Manoharan 	cancel_work_sync(&sc->chanctx_work);
1374ea22df29SSujith Manoharan }
1375ea22df29SSujith Manoharan 
ath9k_is_chanctx_enabled(void)1376499afaccSSujith Manoharan bool ath9k_is_chanctx_enabled(void)
1377499afaccSSujith Manoharan {
1378499afaccSSujith Manoharan 	return (ath9k_use_chanctx == 1);
1379499afaccSSujith Manoharan }
1380499afaccSSujith Manoharan 
13810e08b5fbSSujith Manoharan /********************/
13820e08b5fbSSujith Manoharan /* Queue management */
13830e08b5fbSSujith Manoharan /********************/
13840e08b5fbSSujith Manoharan 
ath9k_chanctx_stop_queues(struct ath_softc * sc,struct ath_chanctx * ctx)1385a064eaa1SSujith Manoharan void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1386a064eaa1SSujith Manoharan {
1387a064eaa1SSujith Manoharan 	struct ath_hw *ah = sc->sc_ah;
1388a064eaa1SSujith Manoharan 	int i;
1389a064eaa1SSujith Manoharan 
1390a064eaa1SSujith Manoharan 	if (ctx == &sc->offchannel.chan) {
1391a064eaa1SSujith Manoharan 		ieee80211_stop_queue(sc->hw,
1392a064eaa1SSujith Manoharan 				     sc->hw->offchannel_tx_hw_queue);
1393a064eaa1SSujith Manoharan 	} else {
1394a064eaa1SSujith Manoharan 		for (i = 0; i < IEEE80211_NUM_ACS; i++)
1395a064eaa1SSujith Manoharan 			ieee80211_stop_queue(sc->hw,
1396a064eaa1SSujith Manoharan 					     ctx->hw_queue_base + i);
1397a064eaa1SSujith Manoharan 	}
1398a064eaa1SSujith Manoharan 
1399a064eaa1SSujith Manoharan 	if (ah->opmode == NL80211_IFTYPE_AP)
1400a064eaa1SSujith Manoharan 		ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1401a064eaa1SSujith Manoharan }
1402a064eaa1SSujith Manoharan 
1403a064eaa1SSujith Manoharan 
ath9k_chanctx_wake_queues(struct ath_softc * sc,struct ath_chanctx * ctx)1404b3903153SSujith Manoharan void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
14050e08b5fbSSujith Manoharan {
14060e08b5fbSSujith Manoharan 	struct ath_hw *ah = sc->sc_ah;
14070e08b5fbSSujith Manoharan 	int i;
14080e08b5fbSSujith Manoharan 
1409b3903153SSujith Manoharan 	if (ctx == &sc->offchannel.chan) {
14100e08b5fbSSujith Manoharan 		ieee80211_wake_queue(sc->hw,
14110e08b5fbSSujith Manoharan 				     sc->hw->offchannel_tx_hw_queue);
14120e08b5fbSSujith Manoharan 	} else {
14130e08b5fbSSujith Manoharan 		for (i = 0; i < IEEE80211_NUM_ACS; i++)
14140e08b5fbSSujith Manoharan 			ieee80211_wake_queue(sc->hw,
1415b3903153SSujith Manoharan 					     ctx->hw_queue_base + i);
14160e08b5fbSSujith Manoharan 	}
14170e08b5fbSSujith Manoharan 
14180e08b5fbSSujith Manoharan 	if (ah->opmode == NL80211_IFTYPE_AP)
14190e08b5fbSSujith Manoharan 		ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
14200e08b5fbSSujith Manoharan }
14210e08b5fbSSujith Manoharan 
1422c7dd40c9SSujith Manoharan /*****************/
1423c7dd40c9SSujith Manoharan /* P2P Powersave */
1424c7dd40c9SSujith Manoharan /*****************/
1425c7dd40c9SSujith Manoharan 
ath9k_update_p2p_ps_timer(struct ath_softc * sc,struct ath_vif * avp)1426c7dd40c9SSujith Manoharan static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
14272471adffSSujith Manoharan {
142858bb9ca8SJanusz Dziedzic 	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
14292471adffSSujith Manoharan 	struct ath_hw *ah = sc->sc_ah;
1430631c45f4SJanusz Dziedzic 	u32 tsf, target_tsf;
14312471adffSSujith Manoharan 
14322471adffSSujith Manoharan 	if (!avp || !avp->noa.has_next_tsf)
14332471adffSSujith Manoharan 		return;
14342471adffSSujith Manoharan 
14352471adffSSujith Manoharan 	ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
14362471adffSSujith Manoharan 
14372471adffSSujith Manoharan 	tsf = ath9k_hw_gettsf32(sc->sc_ah);
14382471adffSSujith Manoharan 
14392471adffSSujith Manoharan 	target_tsf = avp->noa.next_tsf;
14402471adffSSujith Manoharan 	if (!avp->noa.absent)
14412471adffSSujith Manoharan 		target_tsf -= ATH_P2P_PS_STOP_TIME;
1442b77b59aeSJanusz Dziedzic 	else
1443b77b59aeSJanusz Dziedzic 		target_tsf += ATH_P2P_PS_STOP_TIME;
14442471adffSSujith Manoharan 
14452471adffSSujith Manoharan 	if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
14462471adffSSujith Manoharan 		target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
14472471adffSSujith Manoharan 
144858bb9ca8SJanusz Dziedzic 	ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
144958bb9ca8SJanusz Dziedzic 		__func__, avp->noa.absent, tsf, target_tsf,
145058bb9ca8SJanusz Dziedzic 		(target_tsf - tsf) / 1000);
145158bb9ca8SJanusz Dziedzic 
1452631c45f4SJanusz Dziedzic 	ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
14532471adffSSujith Manoharan }
14542471adffSSujith Manoharan 
ath9k_update_p2p_ps(struct ath_softc * sc,struct ieee80211_vif * vif)1455c7dd40c9SSujith Manoharan static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1456c7dd40c9SSujith Manoharan {
1457c7dd40c9SSujith Manoharan 	struct ath_vif *avp = (void *)vif->drv_priv;
1458c7dd40c9SSujith Manoharan 	u32 tsf;
1459c7dd40c9SSujith Manoharan 
1460c7dd40c9SSujith Manoharan 	if (!sc->p2p_ps_timer)
1461c7dd40c9SSujith Manoharan 		return;
1462c7dd40c9SSujith Manoharan 
1463b9a9693fSKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
1464c7dd40c9SSujith Manoharan 		return;
1465c7dd40c9SSujith Manoharan 
1466c7dd40c9SSujith Manoharan 	sc->p2p_ps_vif = avp;
1467b10b7fb3SJanusz Dziedzic 
1468b10b7fb3SJanusz Dziedzic 	if (sc->ps_flags & PS_BEACON_SYNC)
1469b10b7fb3SJanusz Dziedzic 		return;
1470b10b7fb3SJanusz Dziedzic 
1471c7dd40c9SSujith Manoharan 	tsf = ath9k_hw_gettsf32(sc->sc_ah);
1472c7dd40c9SSujith Manoharan 	ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1473c7dd40c9SSujith Manoharan 	ath9k_update_p2p_ps_timer(sc, avp);
1474c7dd40c9SSujith Manoharan }
1475c7dd40c9SSujith Manoharan 
ath9k_get_ctwin(struct ath_softc * sc,struct ath_vif * avp)1476fdcf1bd4SSujith Manoharan static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1477fdcf1bd4SSujith Manoharan {
1478fdcf1bd4SSujith Manoharan 	struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1479fdcf1bd4SSujith Manoharan 	u8 switch_time, ctwin;
1480fdcf1bd4SSujith Manoharan 
1481fdcf1bd4SSujith Manoharan 	/*
1482fdcf1bd4SSujith Manoharan 	 * Channel switch in multi-channel mode is deferred
1483fdcf1bd4SSujith Manoharan 	 * by a quarter beacon interval when handling
1484fdcf1bd4SSujith Manoharan 	 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1485fdcf1bd4SSujith Manoharan 	 * interface is guaranteed to be discoverable
1486fdcf1bd4SSujith Manoharan 	 * for that duration after a TBTT.
1487fdcf1bd4SSujith Manoharan 	 */
1488fdcf1bd4SSujith Manoharan 	switch_time = cur_conf->beacon_interval / 4;
1489fdcf1bd4SSujith Manoharan 
1490fdcf1bd4SSujith Manoharan 	ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1491fdcf1bd4SSujith Manoharan 	if (ctwin && (ctwin < switch_time))
1492fdcf1bd4SSujith Manoharan 		return ctwin;
1493fdcf1bd4SSujith Manoharan 
1494fdcf1bd4SSujith Manoharan 	if (switch_time < P2P_DEFAULT_CTWIN)
1495fdcf1bd4SSujith Manoharan 		return 0;
1496fdcf1bd4SSujith Manoharan 
1497fdcf1bd4SSujith Manoharan 	return P2P_DEFAULT_CTWIN;
1498fdcf1bd4SSujith Manoharan }
1499fdcf1bd4SSujith Manoharan 
ath9k_beacon_add_noa(struct ath_softc * sc,struct ath_vif * avp,struct sk_buff * skb)150011e39a4eSSujith Manoharan void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
150111e39a4eSSujith Manoharan 			  struct sk_buff *skb)
150211e39a4eSSujith Manoharan {
150311e39a4eSSujith Manoharan 	static const u8 noa_ie_hdr[] = {
150411e39a4eSSujith Manoharan 		WLAN_EID_VENDOR_SPECIFIC,	/* type */
150511e39a4eSSujith Manoharan 		0,				/* length */
150611e39a4eSSujith Manoharan 		0x50, 0x6f, 0x9a,		/* WFA OUI */
150711e39a4eSSujith Manoharan 		0x09,				/* P2P subtype */
150811e39a4eSSujith Manoharan 		0x0c,				/* Notice of Absence */
150911e39a4eSSujith Manoharan 		0x00,				/* LSB of little-endian len */
151011e39a4eSSujith Manoharan 		0x00,				/* MSB of little-endian len */
151111e39a4eSSujith Manoharan 	};
151211e39a4eSSujith Manoharan 
151311e39a4eSSujith Manoharan 	struct ieee80211_p2p_noa_attr *noa;
151411e39a4eSSujith Manoharan 	int noa_len, noa_desc, i = 0;
151511e39a4eSSujith Manoharan 	u8 *hdr;
151611e39a4eSSujith Manoharan 
1517d0975eddSSujith Manoharan 	if (!avp->offchannel_duration && !avp->noa_duration)
151811e39a4eSSujith Manoharan 		return;
151911e39a4eSSujith Manoharan 
1520d0975eddSSujith Manoharan 	noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
152111e39a4eSSujith Manoharan 	noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
152211e39a4eSSujith Manoharan 
152359ae1d12SJohannes Berg 	hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
152411e39a4eSSujith Manoharan 	hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
152511e39a4eSSujith Manoharan 	hdr[7] = noa_len;
152611e39a4eSSujith Manoharan 
1527b080db58SJohannes Berg 	noa = skb_put_zero(skb, noa_len);
152811e39a4eSSujith Manoharan 
152911e39a4eSSujith Manoharan 	noa->index = avp->noa_index;
1530fdcf1bd4SSujith Manoharan 	noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
15313edbf0baSJanusz Dziedzic 	if (noa->oppps_ctwindow)
15323edbf0baSJanusz Dziedzic 		noa->oppps_ctwindow |= BIT(7);
1533fdcf1bd4SSujith Manoharan 
1534d0975eddSSujith Manoharan 	if (avp->noa_duration) {
1535d0975eddSSujith Manoharan 		if (avp->periodic_noa) {
153611e39a4eSSujith Manoharan 			u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
153711e39a4eSSujith Manoharan 			noa->desc[i].count = 255;
153811e39a4eSSujith Manoharan 			noa->desc[i].interval = cpu_to_le32(interval);
1539d0975eddSSujith Manoharan 		} else {
1540d0975eddSSujith Manoharan 			noa->desc[i].count = 1;
1541d0975eddSSujith Manoharan 		}
1542d0975eddSSujith Manoharan 
1543d0975eddSSujith Manoharan 		noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1544d0975eddSSujith Manoharan 		noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
154511e39a4eSSujith Manoharan 		i++;
154611e39a4eSSujith Manoharan 	}
154711e39a4eSSujith Manoharan 
154811e39a4eSSujith Manoharan 	if (avp->offchannel_duration) {
154911e39a4eSSujith Manoharan 		noa->desc[i].count = 1;
155011e39a4eSSujith Manoharan 		noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
155111e39a4eSSujith Manoharan 		noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
155211e39a4eSSujith Manoharan 	}
155311e39a4eSSujith Manoharan }
155411e39a4eSSujith Manoharan 
ath9k_p2p_ps_timer(void * priv)15552471adffSSujith Manoharan void ath9k_p2p_ps_timer(void *priv)
15562471adffSSujith Manoharan {
15572471adffSSujith Manoharan 	struct ath_softc *sc = priv;
15582471adffSSujith Manoharan 	struct ath_vif *avp = sc->p2p_ps_vif;
15592471adffSSujith Manoharan 	struct ieee80211_vif *vif;
15602471adffSSujith Manoharan 	struct ieee80211_sta *sta;
15612471adffSSujith Manoharan 	struct ath_node *an;
15622471adffSSujith Manoharan 	u32 tsf;
15632471adffSSujith Manoharan 
15642471adffSSujith Manoharan 	del_timer_sync(&sc->sched.timer);
15652471adffSSujith Manoharan 	ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
15662471adffSSujith Manoharan 	ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
15672471adffSSujith Manoharan 
15682471adffSSujith Manoharan 	if (!avp || avp->chanctx != sc->cur_chan)
15692471adffSSujith Manoharan 		return;
15702471adffSSujith Manoharan 
15712471adffSSujith Manoharan 	tsf = ath9k_hw_gettsf32(sc->sc_ah);
15722471adffSSujith Manoharan 	if (!avp->noa.absent)
15732471adffSSujith Manoharan 		tsf += ATH_P2P_PS_STOP_TIME;
1574b77b59aeSJanusz Dziedzic 	else
1575b77b59aeSJanusz Dziedzic 		tsf -= ATH_P2P_PS_STOP_TIME;
15762471adffSSujith Manoharan 
15772471adffSSujith Manoharan 	if (!avp->noa.has_next_tsf ||
15782471adffSSujith Manoharan 	    avp->noa.next_tsf - tsf > BIT(31))
15792471adffSSujith Manoharan 		ieee80211_update_p2p_noa(&avp->noa, tsf);
15802471adffSSujith Manoharan 
15812471adffSSujith Manoharan 	ath9k_update_p2p_ps_timer(sc, avp);
15822471adffSSujith Manoharan 
15832471adffSSujith Manoharan 	rcu_read_lock();
15842471adffSSujith Manoharan 
15852471adffSSujith Manoharan 	vif = avp->vif;
1586cb35582aSSujith Manoharan 	sta = ieee80211_find_sta(vif, avp->bssid);
15872471adffSSujith Manoharan 	if (!sta)
15882471adffSSujith Manoharan 		goto out;
15892471adffSSujith Manoharan 
15902471adffSSujith Manoharan 	an = (void *) sta->drv_priv;
15912471adffSSujith Manoharan 	if (an->sleeping == !!avp->noa.absent)
15922471adffSSujith Manoharan 		goto out;
15932471adffSSujith Manoharan 
15942471adffSSujith Manoharan 	an->sleeping = avp->noa.absent;
15952471adffSSujith Manoharan 	if (an->sleeping)
15962471adffSSujith Manoharan 		ath_tx_aggr_sleep(sta, sc, an);
15972471adffSSujith Manoharan 	else
15982471adffSSujith Manoharan 		ath_tx_aggr_wakeup(sc, an);
15992471adffSSujith Manoharan 
16002471adffSSujith Manoharan out:
16012471adffSSujith Manoharan 	rcu_read_unlock();
16022471adffSSujith Manoharan }
16032471adffSSujith Manoharan 
ath9k_p2p_bss_info_changed(struct ath_softc * sc,struct ieee80211_vif * vif)1604c7dd40c9SSujith Manoharan void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1605c7dd40c9SSujith Manoharan 				struct ieee80211_vif *vif)
1606c7dd40c9SSujith Manoharan {
1607c7dd40c9SSujith Manoharan 	unsigned long flags;
1608c7dd40c9SSujith Manoharan 
1609c7dd40c9SSujith Manoharan 	spin_lock_bh(&sc->sc_pcu_lock);
1610c7dd40c9SSujith Manoharan 	spin_lock_irqsave(&sc->sc_pm_lock, flags);
1611c7dd40c9SSujith Manoharan 	ath9k_update_p2p_ps(sc, vif);
1612c7dd40c9SSujith Manoharan 	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1613c7dd40c9SSujith Manoharan 	spin_unlock_bh(&sc->sc_pcu_lock);
1614c7dd40c9SSujith Manoharan }
1615c7dd40c9SSujith Manoharan 
ath9k_p2p_beacon_sync(struct ath_softc * sc)1616c7dd40c9SSujith Manoharan void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1617c7dd40c9SSujith Manoharan {
1618c7dd40c9SSujith Manoharan 	if (sc->p2p_ps_vif)
1619c7dd40c9SSujith Manoharan 		ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1620c7dd40c9SSujith Manoharan }
1621c7dd40c9SSujith Manoharan 
ath9k_p2p_remove_vif(struct ath_softc * sc,struct ieee80211_vif * vif)1622c7dd40c9SSujith Manoharan void ath9k_p2p_remove_vif(struct ath_softc *sc,
1623c7dd40c9SSujith Manoharan 			  struct ieee80211_vif *vif)
16242471adffSSujith Manoharan {
16252471adffSSujith Manoharan 	struct ath_vif *avp = (void *)vif->drv_priv;
16262471adffSSujith Manoharan 
1627c7dd40c9SSujith Manoharan 	spin_lock_bh(&sc->sc_pcu_lock);
1628c7dd40c9SSujith Manoharan 	if (avp == sc->p2p_ps_vif) {
1629c7dd40c9SSujith Manoharan 		sc->p2p_ps_vif = NULL;
1630c7dd40c9SSujith Manoharan 		ath9k_update_p2p_ps_timer(sc, NULL);
16312471adffSSujith Manoharan 	}
1632c7dd40c9SSujith Manoharan 	spin_unlock_bh(&sc->sc_pcu_lock);
1633c7dd40c9SSujith Manoharan }
1634c7dd40c9SSujith Manoharan 
ath9k_init_p2p(struct ath_softc * sc)1635c7dd40c9SSujith Manoharan int ath9k_init_p2p(struct ath_softc *sc)
1636c7dd40c9SSujith Manoharan {
1637c7dd40c9SSujith Manoharan 	sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1638c7dd40c9SSujith Manoharan 					       NULL, sc, AR_FIRST_NDP_TIMER);
1639c7dd40c9SSujith Manoharan 	if (!sc->p2p_ps_timer)
1640c7dd40c9SSujith Manoharan 		return -ENOMEM;
1641c7dd40c9SSujith Manoharan 
1642c7dd40c9SSujith Manoharan 	return 0;
1643c7dd40c9SSujith Manoharan }
1644c7dd40c9SSujith Manoharan 
ath9k_deinit_p2p(struct ath_softc * sc)1645c7dd40c9SSujith Manoharan void ath9k_deinit_p2p(struct ath_softc *sc)
1646c7dd40c9SSujith Manoharan {
1647c7dd40c9SSujith Manoharan 	if (sc->p2p_ps_timer)
1648c7dd40c9SSujith Manoharan 		ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1649c7dd40c9SSujith Manoharan }
1650c7dd40c9SSujith Manoharan 
1651c7dd40c9SSujith Manoharan #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */
1652