]>
Commit | Line | Data |
---|---|---|
fbbcd146 FF |
1 | /* |
2 | * Copyright (c) 2014 Qualcomm Atheros, Inc. | |
3 | * | |
4 | * Permission to use, copy, modify, and/or distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice appear in all copies. | |
7 | * | |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
15 | */ | |
16 | ||
17 | #include "ath9k.h" | |
18 | ||
19 | /* Set/change channels. If the channel is really being changed, it's done | |
20 | * by reseting the chip. To accomplish this we must first cleanup any pending | |
21 | * DMA, then restart stuff. | |
22 | */ | |
23 | static int ath_set_channel(struct ath_softc *sc) | |
24 | { | |
25 | struct ath_hw *ah = sc->sc_ah; | |
26 | struct ath_common *common = ath9k_hw_common(ah); | |
27 | struct ieee80211_hw *hw = sc->hw; | |
28 | struct ath9k_channel *hchan; | |
29 | struct cfg80211_chan_def *chandef = &sc->cur_chan->chandef; | |
30 | struct ieee80211_channel *chan = chandef->chan; | |
31 | int pos = chan->hw_value; | |
32 | int old_pos = -1; | |
33 | int r; | |
34 | ||
35 | if (test_bit(ATH_OP_INVALID, &common->op_flags)) | |
36 | return -EIO; | |
37 | ||
38 | if (ah->curchan) | |
39 | old_pos = ah->curchan - &ah->channels[0]; | |
40 | ||
41 | ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n", | |
42 | chan->center_freq, chandef->width); | |
43 | ||
44 | /* update survey stats for the old channel before switching */ | |
45 | spin_lock_bh(&common->cc_lock); | |
46 | ath_update_survey_stats(sc); | |
47 | spin_unlock_bh(&common->cc_lock); | |
48 | ||
49 | ath9k_cmn_get_channel(hw, ah, chandef); | |
50 | ||
51 | /* If the operating channel changes, change the survey in-use flags | |
52 | * along with it. | |
53 | * Reset the survey data for the new channel, unless we're switching | |
54 | * back to the operating channel from an off-channel operation. | |
55 | */ | |
56 | if (!sc->cur_chan->offchannel && sc->cur_survey != &sc->survey[pos]) { | |
57 | if (sc->cur_survey) | |
58 | sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE; | |
59 | ||
60 | sc->cur_survey = &sc->survey[pos]; | |
61 | ||
62 | memset(sc->cur_survey, 0, sizeof(struct survey_info)); | |
63 | sc->cur_survey->filled |= SURVEY_INFO_IN_USE; | |
64 | } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) { | |
65 | memset(&sc->survey[pos], 0, sizeof(struct survey_info)); | |
66 | } | |
67 | ||
68 | hchan = &sc->sc_ah->channels[pos]; | |
69 | r = ath_reset_internal(sc, hchan); | |
70 | if (r) | |
71 | return r; | |
72 | ||
73 | /* The most recent snapshot of channel->noisefloor for the old | |
74 | * channel is only available after the hardware reset. Copy it to | |
75 | * the survey stats now. | |
76 | */ | |
77 | if (old_pos >= 0) | |
78 | ath_update_survey_nf(sc, old_pos); | |
79 | ||
80 | /* Enable radar pulse detection if on a DFS channel. Spectral | |
81 | * scanning and radar detection can not be used concurrently. | |
82 | */ | |
83 | if (hw->conf.radar_enabled) { | |
84 | u32 rxfilter; | |
85 | ||
fbbcd146 FF |
86 | rxfilter = ath9k_hw_getrxfilter(ah); |
87 | rxfilter |= ATH9K_RX_FILTER_PHYRADAR | | |
88 | ATH9K_RX_FILTER_PHYERR; | |
89 | ath9k_hw_setrxfilter(ah, rxfilter); | |
90 | ath_dbg(common, DFS, "DFS enabled at freq %d\n", | |
91 | chan->center_freq); | |
92 | } else { | |
93 | /* perform spectral scan if requested. */ | |
94 | if (test_bit(ATH_OP_SCANNING, &common->op_flags) && | |
95 | sc->spectral_mode == SPECTRAL_CHANSCAN) | |
96 | ath9k_spectral_scan_trigger(hw); | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | void ath_chanctx_init(struct ath_softc *sc) | |
103 | { | |
104 | struct ath_chanctx *ctx; | |
105 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
106 | struct ieee80211_supported_band *sband; | |
107 | struct ieee80211_channel *chan; | |
0453531e | 108 | int i, j; |
fbbcd146 FF |
109 | |
110 | sband = &common->sbands[IEEE80211_BAND_2GHZ]; | |
111 | if (!sband->n_channels) | |
112 | sband = &common->sbands[IEEE80211_BAND_5GHZ]; | |
113 | ||
114 | chan = &sband->channels[0]; | |
115 | for (i = 0; i < ATH9K_NUM_CHANCTX; i++) { | |
116 | ctx = &sc->chanctx[i]; | |
117 | cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20); | |
118 | INIT_LIST_HEAD(&ctx->vifs); | |
bc7e1be7 | 119 | ctx->txpower = ATH_TXPOWER_MAX; |
0453531e FF |
120 | for (j = 0; j < ARRAY_SIZE(ctx->acq); j++) |
121 | INIT_LIST_HEAD(&ctx->acq[j]); | |
fbbcd146 | 122 | } |
fbbcd146 FF |
123 | } |
124 | ||
bff11766 FF |
125 | void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx, |
126 | struct cfg80211_chan_def *chandef) | |
127 | { | |
4c7e9aee | 128 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
bff11766 FF |
129 | bool cur_chan; |
130 | ||
131 | spin_lock_bh(&sc->chan_lock); | |
132 | if (chandef) | |
133 | memcpy(&ctx->chandef, chandef, sizeof(*chandef)); | |
134 | cur_chan = sc->cur_chan == ctx; | |
135 | spin_unlock_bh(&sc->chan_lock); | |
136 | ||
4c7e9aee SM |
137 | if (!cur_chan) { |
138 | ath_dbg(common, CHAN_CTX, | |
139 | "Current context differs from the new context\n"); | |
bff11766 | 140 | return; |
4c7e9aee | 141 | } |
bff11766 FF |
142 | |
143 | ath_set_channel(sc); | |
fbbcd146 | 144 | } |
78b21949 | 145 | |
27babf9f SM |
146 | #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT |
147 | ||
0e08b5fb SM |
148 | /**********************************************************/ |
149 | /* Functions to handle the channel context state machine. */ | |
150 | /**********************************************************/ | |
151 | ||
27babf9f SM |
152 | static const char *offchannel_state_string(enum ath_offchannel_state state) |
153 | { | |
27babf9f SM |
154 | switch (state) { |
155 | case_rtn_string(ATH_OFFCHANNEL_IDLE); | |
156 | case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND); | |
157 | case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT); | |
158 | case_rtn_string(ATH_OFFCHANNEL_SUSPEND); | |
159 | case_rtn_string(ATH_OFFCHANNEL_ROC_START); | |
160 | case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT); | |
161 | case_rtn_string(ATH_OFFCHANNEL_ROC_DONE); | |
162 | default: | |
163 | return "unknown"; | |
164 | } | |
165 | } | |
166 | ||
5a8cbec7 SM |
167 | static const char *chanctx_event_string(enum ath_chanctx_event ev) |
168 | { | |
169 | switch (ev) { | |
170 | case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE); | |
171 | case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT); | |
172 | case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER); | |
173 | case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED); | |
174 | case_rtn_string(ATH_CHANCTX_EVENT_ASSOC); | |
175 | case_rtn_string(ATH_CHANCTX_EVENT_SWITCH); | |
176 | case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN); | |
177 | case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN); | |
178 | case_rtn_string(ATH_CHANCTX_EVENT_CHANGE); | |
179 | case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL); | |
180 | default: | |
181 | return "unknown"; | |
182 | } | |
183 | } | |
184 | ||
185 | static const char *chanctx_state_string(enum ath_chanctx_state state) | |
186 | { | |
187 | switch (state) { | |
188 | case_rtn_string(ATH_CHANCTX_STATE_IDLE); | |
189 | case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON); | |
190 | case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER); | |
191 | case_rtn_string(ATH_CHANCTX_STATE_SWITCH); | |
192 | case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE); | |
193 | default: | |
194 | return "unknown"; | |
195 | } | |
196 | } | |
197 | ||
a09798f4 SM |
198 | void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx) |
199 | { | |
200 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
201 | struct ath_vif *avp; | |
202 | bool active = false; | |
203 | u8 n_active = 0; | |
204 | ||
205 | if (!ctx) | |
206 | return; | |
207 | ||
208 | list_for_each_entry(avp, &ctx->vifs, list) { | |
209 | struct ieee80211_vif *vif = avp->vif; | |
210 | ||
211 | switch (vif->type) { | |
212 | case NL80211_IFTYPE_P2P_CLIENT: | |
213 | case NL80211_IFTYPE_STATION: | |
cb35582a | 214 | if (avp->assoc) |
a09798f4 SM |
215 | active = true; |
216 | break; | |
217 | default: | |
218 | active = true; | |
219 | break; | |
220 | } | |
221 | } | |
222 | ctx->active = active; | |
223 | ||
224 | ath_for_each_chanctx(sc, ctx) { | |
225 | if (!ctx->assigned || list_empty(&ctx->vifs)) | |
226 | continue; | |
227 | n_active++; | |
228 | } | |
229 | ||
230 | if (n_active <= 1) { | |
231 | clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags); | |
232 | return; | |
233 | } | |
234 | if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) | |
235 | return; | |
236 | ||
237 | if (ath9k_is_chanctx_enabled()) { | |
238 | ath_chanctx_event(sc, NULL, | |
239 | ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL); | |
240 | } | |
241 | } | |
242 | ||
58b57375 FF |
243 | static struct ath_chanctx * |
244 | ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx) | |
245 | { | |
246 | int idx = ctx - &sc->chanctx[0]; | |
247 | ||
248 | return &sc->chanctx[!idx]; | |
249 | } | |
250 | ||
251 | static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc) | |
252 | { | |
253 | struct ath_chanctx *prev, *cur; | |
254 | struct timespec ts; | |
255 | u32 cur_tsf, prev_tsf, beacon_int; | |
256 | s32 offset; | |
257 | ||
258 | beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval); | |
259 | ||
260 | cur = sc->cur_chan; | |
261 | prev = ath_chanctx_get_next(sc, cur); | |
262 | ||
7f30eac9 SM |
263 | if (!prev->switch_after_beacon) |
264 | return; | |
265 | ||
58b57375 FF |
266 | getrawmonotonic(&ts); |
267 | cur_tsf = (u32) cur->tsf_val + | |
268 | ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts); | |
269 | ||
270 | prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf; | |
271 | prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts); | |
272 | ||
273 | /* Adjust the TSF time of the AP chanctx to keep its beacons | |
274 | * at half beacon interval offset relative to the STA chanctx. | |
275 | */ | |
276 | offset = cur_tsf - prev_tsf; | |
277 | ||
278 | /* Ignore stale data or spurious timestamps */ | |
279 | if (offset < 0 || offset > 3 * beacon_int) | |
280 | return; | |
281 | ||
282 | offset = beacon_int / 2 - (offset % beacon_int); | |
283 | prev->tsf_val += offset; | |
284 | } | |
285 | ||
42eda115 FF |
286 | /* Configure the TSF based hardware timer for a channel switch. |
287 | * Also set up backup software timer, in case the gen timer fails. | |
288 | * This could be caused by a hardware reset. | |
289 | */ | |
290 | static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time) | |
291 | { | |
878066e7 | 292 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
42eda115 FF |
293 | struct ath_hw *ah = sc->sc_ah; |
294 | ||
295 | ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000); | |
296 | tsf_time -= ath9k_hw_gettsf32(ah); | |
297 | tsf_time = msecs_to_jiffies(tsf_time / 1000) + 1; | |
1a7c5b7e | 298 | mod_timer(&sc->sched.timer, jiffies + tsf_time); |
878066e7 SM |
299 | |
300 | ath_dbg(common, CHAN_CTX, | |
301 | "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time)); | |
42eda115 FF |
302 | } |
303 | ||
748299f2 FF |
304 | void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif, |
305 | enum ath_chanctx_event ev) | |
306 | { | |
307 | struct ath_hw *ah = sc->sc_ah; | |
58b57375 | 308 | struct ath_common *common = ath9k_hw_common(ah); |
7414863e | 309 | struct ath_beacon_config *cur_conf; |
3ae07d39 | 310 | struct ath_vif *avp = NULL; |
73fa2f26 | 311 | struct ath_chanctx *ctx; |
748299f2 | 312 | u32 tsf_time; |
ec70abe1 | 313 | u32 beacon_int; |
3ae07d39 FF |
314 | |
315 | if (vif) | |
316 | avp = (struct ath_vif *) vif->drv_priv; | |
748299f2 FF |
317 | |
318 | spin_lock_bh(&sc->chan_lock); | |
319 | ||
878066e7 SM |
320 | ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n", |
321 | sc->cur_chan->chandef.center_freq1, | |
322 | chanctx_event_string(ev), | |
323 | chanctx_state_string(sc->sched.state)); | |
324 | ||
748299f2 FF |
325 | switch (ev) { |
326 | case ATH_CHANCTX_EVENT_BEACON_PREPARE: | |
3ae07d39 FF |
327 | if (avp->offchannel_duration) |
328 | avp->offchannel_duration = 0; | |
329 | ||
878066e7 SM |
330 | if (avp->chanctx != sc->cur_chan) { |
331 | ath_dbg(common, CHAN_CTX, | |
332 | "Contexts differ, not preparing beacon\n"); | |
73fa2f26 | 333 | break; |
878066e7 | 334 | } |
73fa2f26 | 335 | |
367b341e | 336 | if (sc->sched.offchannel_pending && !sc->sched.wait_switch) { |
73fa2f26 FF |
337 | sc->sched.offchannel_pending = false; |
338 | sc->next_chan = &sc->offchannel.chan; | |
339 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; | |
878066e7 SM |
340 | ath_dbg(common, CHAN_CTX, |
341 | "Setting offchannel_pending to false\n"); | |
73fa2f26 FF |
342 | } |
343 | ||
344 | ctx = ath_chanctx_get_next(sc, sc->cur_chan); | |
345 | if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) { | |
346 | sc->next_chan = ctx; | |
347 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; | |
878066e7 SM |
348 | ath_dbg(common, CHAN_CTX, |
349 | "Set next context, move chanctx state to WAIT_FOR_BEACON\n"); | |
73fa2f26 FF |
350 | } |
351 | ||
352 | /* if the timer missed its window, use the next interval */ | |
878066e7 | 353 | if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) { |
73fa2f26 | 354 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; |
878066e7 SM |
355 | ath_dbg(common, CHAN_CTX, |
356 | "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n"); | |
357 | } | |
73fa2f26 | 358 | |
748299f2 FF |
359 | if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) |
360 | break; | |
361 | ||
878066e7 SM |
362 | ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr); |
363 | ||
748299f2 FF |
364 | sc->sched.beacon_pending = true; |
365 | sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER); | |
3ae07d39 | 366 | |
7414863e | 367 | cur_conf = &sc->cur_chan->beacon; |
ec70abe1 FF |
368 | beacon_int = TU_TO_USEC(cur_conf->beacon_interval); |
369 | ||
3ae07d39 | 370 | /* defer channel switch by a quarter beacon interval */ |
ec70abe1 | 371 | tsf_time = sc->sched.next_tbtt + beacon_int / 4; |
3ae07d39 | 372 | sc->sched.switch_start_time = tsf_time; |
58b57375 | 373 | sc->cur_chan->last_beacon = sc->sched.next_tbtt; |
3ae07d39 | 374 | |
d0975edd SM |
375 | /* |
376 | * If an offchannel switch is scheduled to happen after | |
377 | * a beacon transmission, update the NoA with one-shot | |
378 | * values and increment the index. | |
379 | */ | |
380 | if (sc->next_chan == &sc->offchannel.chan) { | |
381 | avp->noa_index++; | |
3ae07d39 | 382 | avp->offchannel_start = tsf_time; |
d0975edd SM |
383 | avp->offchannel_duration = sc->sched.offchannel_duration; |
384 | ||
385 | ath_dbg(common, CHAN_CTX, | |
386 | "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n", | |
387 | avp->offchannel_duration, | |
388 | avp->offchannel_start, | |
389 | avp->noa_index); | |
390 | ||
391 | /* | |
392 | * When multiple contexts are active, the NoA | |
393 | * has to be recalculated and advertised after | |
394 | * an offchannel operation. | |
395 | */ | |
396 | if (ctx->active && avp->noa_duration) | |
397 | avp->noa_duration = 0; | |
398 | ||
399 | break; | |
3ae07d39 FF |
400 | } |
401 | ||
167bf96d SM |
402 | /* |
403 | * Clear the extend_absence flag if it had been | |
404 | * set during the previous beacon transmission, | |
405 | * since we need to revert to the normal NoA | |
406 | * schedule. | |
407 | */ | |
408 | if (ctx->active && sc->sched.extend_absence) { | |
409 | avp->noa_duration = 0; | |
410 | sc->sched.extend_absence = false; | |
411 | } | |
412 | ||
413 | /* If at least two consecutive beacons were missed on the STA | |
414 | * chanctx, stay on the STA channel for one extra beacon period, | |
415 | * to resync the timer properly. | |
416 | */ | |
417 | if (ctx->active && sc->sched.beacon_miss >= 2) { | |
418 | avp->noa_duration = 0; | |
419 | sc->sched.extend_absence = true; | |
420 | } | |
a2b28601 | 421 | |
d0975edd SM |
422 | /* Prevent wrap-around issues */ |
423 | if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30)) | |
424 | avp->noa_duration = 0; | |
425 | ||
426 | /* | |
427 | * If multiple contexts are active, start periodic | |
428 | * NoA and increment the index for the first | |
429 | * announcement. | |
430 | */ | |
431 | if (ctx->active && | |
432 | (!avp->noa_duration || sc->sched.force_noa_update)) { | |
3ae07d39 | 433 | avp->noa_index++; |
d0975edd | 434 | avp->noa_start = tsf_time; |
878066e7 | 435 | |
167bf96d SM |
436 | if (sc->sched.extend_absence) |
437 | avp->noa_duration = (3 * beacon_int / 2) + | |
438 | sc->sched.channel_switch_time; | |
439 | else | |
440 | avp->noa_duration = | |
441 | TU_TO_USEC(cur_conf->beacon_interval) / 2 + | |
442 | sc->sched.channel_switch_time; | |
443 | ||
444 | if (test_bit(ATH_OP_SCANNING, &common->op_flags) || | |
445 | sc->sched.extend_absence) | |
d0975edd SM |
446 | avp->periodic_noa = false; |
447 | else | |
448 | avp->periodic_noa = true; | |
449 | ||
450 | ath_dbg(common, CHAN_CTX, | |
451 | "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n", | |
452 | avp->noa_duration, | |
453 | avp->noa_start, | |
454 | avp->noa_index, | |
455 | avp->periodic_noa); | |
456 | } | |
457 | ||
458 | if (ctx->active && sc->sched.force_noa_update) | |
459 | sc->sched.force_noa_update = false; | |
878066e7 | 460 | |
748299f2 FF |
461 | break; |
462 | case ATH_CHANCTX_EVENT_BEACON_SENT: | |
878066e7 SM |
463 | if (!sc->sched.beacon_pending) { |
464 | ath_dbg(common, CHAN_CTX, | |
465 | "No pending beacon\n"); | |
748299f2 | 466 | break; |
878066e7 | 467 | } |
748299f2 FF |
468 | |
469 | sc->sched.beacon_pending = false; | |
470 | if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) | |
471 | break; | |
472 | ||
878066e7 SM |
473 | ath_dbg(common, CHAN_CTX, |
474 | "Move chanctx state to WAIT_FOR_TIMER\n"); | |
475 | ||
748299f2 | 476 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER; |
42eda115 | 477 | ath_chanctx_setup_timer(sc, sc->sched.switch_start_time); |
748299f2 FF |
478 | break; |
479 | case ATH_CHANCTX_EVENT_TSF_TIMER: | |
480 | if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER) | |
481 | break; | |
482 | ||
ec70abe1 FF |
483 | if (!sc->cur_chan->switch_after_beacon && |
484 | sc->sched.beacon_pending) | |
485 | sc->sched.beacon_miss++; | |
486 | ||
878066e7 SM |
487 | ath_dbg(common, CHAN_CTX, |
488 | "Move chanctx state to SWITCH\n"); | |
489 | ||
748299f2 FF |
490 | sc->sched.state = ATH_CHANCTX_STATE_SWITCH; |
491 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); | |
492 | break; | |
58b57375 | 493 | case ATH_CHANCTX_EVENT_BEACON_RECEIVED: |
73fa2f26 FF |
494 | if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) || |
495 | sc->cur_chan == &sc->offchannel.chan) | |
58b57375 FF |
496 | break; |
497 | ||
498 | ath_chanctx_adjust_tbtt_delta(sc); | |
ec70abe1 FF |
499 | sc->sched.beacon_pending = false; |
500 | sc->sched.beacon_miss = 0; | |
a899b678 FF |
501 | |
502 | /* TSF time might have been updated by the incoming beacon, | |
503 | * need update the channel switch timer to reflect the change. | |
504 | */ | |
505 | tsf_time = sc->sched.switch_start_time; | |
506 | tsf_time -= (u32) sc->cur_chan->tsf_val + | |
507 | ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL); | |
508 | tsf_time += ath9k_hw_gettsf32(ah); | |
509 | ||
42eda115 FF |
510 | |
511 | ath_chanctx_setup_timer(sc, tsf_time); | |
58b57375 | 512 | break; |
73fa2f26 FF |
513 | case ATH_CHANCTX_EVENT_ASSOC: |
514 | if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE || | |
515 | avp->chanctx != sc->cur_chan) | |
516 | break; | |
517 | ||
878066e7 SM |
518 | ath_dbg(common, CHAN_CTX, |
519 | "Move chanctx state from FORCE_ACTIVE to IDLE\n"); | |
520 | ||
73fa2f26 FF |
521 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; |
522 | /* fall through */ | |
523 | case ATH_CHANCTX_EVENT_SWITCH: | |
524 | if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) || | |
525 | sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE || | |
526 | sc->cur_chan->switch_after_beacon || | |
527 | sc->cur_chan == &sc->offchannel.chan) | |
528 | break; | |
529 | ||
530 | /* If this is a station chanctx, stay active for a half | |
531 | * beacon period (minus channel switch time) | |
532 | */ | |
533 | sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan); | |
7414863e | 534 | cur_conf = &sc->cur_chan->beacon; |
73fa2f26 | 535 | |
878066e7 SM |
536 | ath_dbg(common, CHAN_CTX, |
537 | "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n"); | |
538 | ||
73fa2f26 | 539 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER; |
367b341e | 540 | sc->sched.wait_switch = false; |
ec70abe1 FF |
541 | |
542 | tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2; | |
167bf96d SM |
543 | |
544 | if (sc->sched.extend_absence) { | |
ec70abe1 FF |
545 | sc->sched.beacon_miss = 0; |
546 | tsf_time *= 3; | |
547 | } | |
548 | ||
73fa2f26 | 549 | tsf_time -= sc->sched.channel_switch_time; |
ec70abe1 | 550 | tsf_time += ath9k_hw_gettsf32(sc->sc_ah); |
73fa2f26 FF |
551 | sc->sched.switch_start_time = tsf_time; |
552 | ||
42eda115 | 553 | ath_chanctx_setup_timer(sc, tsf_time); |
ec70abe1 | 554 | sc->sched.beacon_pending = true; |
73fa2f26 FF |
555 | break; |
556 | case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL: | |
557 | if (sc->cur_chan == &sc->offchannel.chan || | |
558 | sc->cur_chan->switch_after_beacon) | |
559 | break; | |
560 | ||
561 | sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan); | |
562 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); | |
563 | break; | |
564 | case ATH_CHANCTX_EVENT_UNASSIGN: | |
565 | if (sc->cur_chan->assigned) { | |
566 | if (sc->next_chan && !sc->next_chan->assigned && | |
567 | sc->next_chan != &sc->offchannel.chan) | |
568 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; | |
569 | break; | |
570 | } | |
571 | ||
572 | ctx = ath_chanctx_get_next(sc, sc->cur_chan); | |
573 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; | |
574 | if (!ctx->assigned) | |
575 | break; | |
576 | ||
577 | sc->next_chan = ctx; | |
578 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); | |
579 | break; | |
02da18b7 | 580 | case ATH_CHANCTX_EVENT_ASSIGN: |
4c7e9aee SM |
581 | /* |
582 | * When adding a new channel context, check if a scan | |
583 | * is in progress and abort it since the addition of | |
584 | * a new channel context is usually followed by VIF | |
585 | * assignment, in which case we have to start multi-channel | |
586 | * operation. | |
587 | */ | |
588 | if (test_bit(ATH_OP_SCANNING, &common->op_flags)) { | |
589 | ath_dbg(common, CHAN_CTX, | |
590 | "Aborting HW scan to add new context\n"); | |
591 | ||
592 | spin_unlock_bh(&sc->chan_lock); | |
593 | del_timer_sync(&sc->offchannel.timer); | |
594 | ath_scan_complete(sc, true); | |
595 | spin_lock_bh(&sc->chan_lock); | |
596 | } | |
02da18b7 SM |
597 | break; |
598 | case ATH_CHANCTX_EVENT_CHANGE: | |
599 | break; | |
748299f2 FF |
600 | } |
601 | ||
602 | spin_unlock_bh(&sc->chan_lock); | |
603 | } | |
dfcbb3e8 | 604 | |
70b06dac SM |
605 | void ath_chanctx_beacon_sent_ev(struct ath_softc *sc, |
606 | enum ath_chanctx_event ev) | |
607 | { | |
608 | if (sc->sched.beacon_pending) | |
609 | ath_chanctx_event(sc, NULL, ev); | |
610 | } | |
611 | ||
a2b28601 | 612 | void ath_chanctx_beacon_recv_ev(struct ath_softc *sc, |
70b06dac SM |
613 | enum ath_chanctx_event ev) |
614 | { | |
70b06dac SM |
615 | ath_chanctx_event(sc, NULL, ev); |
616 | } | |
617 | ||
dfcbb3e8 SM |
618 | static int ath_scan_channel_duration(struct ath_softc *sc, |
619 | struct ieee80211_channel *chan) | |
620 | { | |
621 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; | |
622 | ||
623 | if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR)) | |
624 | return (HZ / 9); /* ~110 ms */ | |
625 | ||
626 | return (HZ / 16); /* ~60 ms */ | |
627 | } | |
628 | ||
922c943d SM |
629 | static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx, |
630 | struct cfg80211_chan_def *chandef) | |
631 | { | |
632 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
633 | ||
634 | spin_lock_bh(&sc->chan_lock); | |
635 | ||
636 | if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) && | |
637 | (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) { | |
da0162f3 SM |
638 | if (chandef) |
639 | ctx->chandef = *chandef; | |
cbc775db SM |
640 | |
641 | sc->sched.offchannel_pending = true; | |
642 | sc->sched.wait_switch = true; | |
643 | sc->sched.offchannel_duration = | |
644 | jiffies_to_usecs(sc->offchannel.duration) + | |
645 | sc->sched.channel_switch_time; | |
646 | ||
922c943d | 647 | spin_unlock_bh(&sc->chan_lock); |
da0162f3 SM |
648 | ath_dbg(common, CHAN_CTX, |
649 | "Set offchannel_pending to true\n"); | |
922c943d SM |
650 | return; |
651 | } | |
652 | ||
653 | sc->next_chan = ctx; | |
654 | if (chandef) { | |
655 | ctx->chandef = *chandef; | |
656 | ath_dbg(common, CHAN_CTX, | |
657 | "Assigned next_chan to %d MHz\n", chandef->center_freq1); | |
658 | } | |
659 | ||
660 | if (sc->next_chan == &sc->offchannel.chan) { | |
661 | sc->sched.offchannel_duration = | |
bb628eb9 | 662 | jiffies_to_usecs(sc->offchannel.duration) + |
922c943d SM |
663 | sc->sched.channel_switch_time; |
664 | ||
665 | if (chandef) { | |
666 | ath_dbg(common, CHAN_CTX, | |
667 | "Offchannel duration for chan %d MHz : %u\n", | |
668 | chandef->center_freq1, | |
669 | sc->sched.offchannel_duration); | |
670 | } | |
671 | } | |
672 | spin_unlock_bh(&sc->chan_lock); | |
673 | ieee80211_queue_work(sc->hw, &sc->chanctx_work); | |
674 | } | |
675 | ||
344ae6ab SM |
676 | static void ath_chanctx_offchan_switch(struct ath_softc *sc, |
677 | struct ieee80211_channel *chan) | |
678 | { | |
679 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
680 | struct cfg80211_chan_def chandef; | |
681 | ||
682 | cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT); | |
683 | ath_dbg(common, CHAN_CTX, | |
684 | "Channel definition created: %d MHz\n", chandef.center_freq1); | |
685 | ||
686 | ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef); | |
687 | } | |
688 | ||
98f411b8 SM |
689 | static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc, |
690 | bool active) | |
691 | { | |
692 | struct ath_chanctx *ctx; | |
693 | ||
694 | ath_for_each_chanctx(sc, ctx) { | |
695 | if (!ctx->assigned || list_empty(&ctx->vifs)) | |
696 | continue; | |
697 | if (active && !ctx->active) | |
698 | continue; | |
699 | ||
700 | if (ctx->switch_after_beacon) | |
701 | return ctx; | |
702 | } | |
703 | ||
704 | return &sc->chanctx[0]; | |
705 | } | |
706 | ||
dfcbb3e8 SM |
707 | static void |
708 | ath_scan_next_channel(struct ath_softc *sc) | |
709 | { | |
bc81d43a | 710 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
dfcbb3e8 SM |
711 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; |
712 | struct ieee80211_channel *chan; | |
713 | ||
714 | if (sc->offchannel.scan_idx >= req->n_channels) { | |
bc81d43a | 715 | ath_dbg(common, CHAN_CTX, |
878066e7 SM |
716 | "Moving offchannel state to ATH_OFFCHANNEL_IDLE, " |
717 | "scan_idx: %d, n_channels: %d\n", | |
bc81d43a SM |
718 | sc->offchannel.scan_idx, |
719 | req->n_channels); | |
720 | ||
dfcbb3e8 SM |
721 | sc->offchannel.state = ATH_OFFCHANNEL_IDLE; |
722 | ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false), | |
723 | NULL); | |
724 | return; | |
725 | } | |
726 | ||
bc81d43a | 727 | ath_dbg(common, CHAN_CTX, |
878066e7 | 728 | "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n", |
bc81d43a SM |
729 | sc->offchannel.scan_idx); |
730 | ||
dfcbb3e8 SM |
731 | chan = req->channels[sc->offchannel.scan_idx++]; |
732 | sc->offchannel.duration = ath_scan_channel_duration(sc, chan); | |
733 | sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND; | |
bc81d43a | 734 | |
dfcbb3e8 SM |
735 | ath_chanctx_offchan_switch(sc, chan); |
736 | } | |
737 | ||
738 | void ath_offchannel_next(struct ath_softc *sc) | |
739 | { | |
740 | struct ieee80211_vif *vif; | |
741 | ||
742 | if (sc->offchannel.scan_req) { | |
743 | vif = sc->offchannel.scan_vif; | |
744 | sc->offchannel.chan.txpower = vif->bss_conf.txpower; | |
745 | ath_scan_next_channel(sc); | |
746 | } else if (sc->offchannel.roc_vif) { | |
747 | vif = sc->offchannel.roc_vif; | |
748 | sc->offchannel.chan.txpower = vif->bss_conf.txpower; | |
bb628eb9 SM |
749 | sc->offchannel.duration = |
750 | msecs_to_jiffies(sc->offchannel.roc_duration); | |
dfcbb3e8 SM |
751 | sc->offchannel.state = ATH_OFFCHANNEL_ROC_START; |
752 | ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan); | |
753 | } else { | |
754 | ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false), | |
755 | NULL); | |
756 | sc->offchannel.state = ATH_OFFCHANNEL_IDLE; | |
757 | if (sc->ps_idle) | |
758 | ath_cancel_work(sc); | |
759 | } | |
760 | } | |
761 | ||
762 | void ath_roc_complete(struct ath_softc *sc, bool abort) | |
763 | { | |
4b60af4a SM |
764 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
765 | ||
766 | if (abort) | |
767 | ath_dbg(common, CHAN_CTX, "RoC aborted\n"); | |
768 | else | |
769 | ath_dbg(common, CHAN_CTX, "RoC expired\n"); | |
770 | ||
dfcbb3e8 SM |
771 | sc->offchannel.roc_vif = NULL; |
772 | sc->offchannel.roc_chan = NULL; | |
773 | if (!abort) | |
774 | ieee80211_remain_on_channel_expired(sc->hw); | |
775 | ath_offchannel_next(sc); | |
776 | ath9k_ps_restore(sc); | |
777 | } | |
778 | ||
779 | void ath_scan_complete(struct ath_softc *sc, bool abort) | |
780 | { | |
781 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
782 | ||
bc81d43a SM |
783 | if (abort) |
784 | ath_dbg(common, CHAN_CTX, "HW scan aborted\n"); | |
785 | else | |
786 | ath_dbg(common, CHAN_CTX, "HW scan complete\n"); | |
787 | ||
dfcbb3e8 SM |
788 | sc->offchannel.scan_req = NULL; |
789 | sc->offchannel.scan_vif = NULL; | |
790 | sc->offchannel.state = ATH_OFFCHANNEL_IDLE; | |
791 | ieee80211_scan_completed(sc->hw, abort); | |
792 | clear_bit(ATH_OP_SCANNING, &common->op_flags); | |
d0975edd SM |
793 | spin_lock_bh(&sc->chan_lock); |
794 | if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) | |
795 | sc->sched.force_noa_update = true; | |
796 | spin_unlock_bh(&sc->chan_lock); | |
dfcbb3e8 SM |
797 | ath_offchannel_next(sc); |
798 | ath9k_ps_restore(sc); | |
799 | } | |
800 | ||
801 | static void ath_scan_send_probe(struct ath_softc *sc, | |
802 | struct cfg80211_ssid *ssid) | |
803 | { | |
804 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; | |
805 | struct ieee80211_vif *vif = sc->offchannel.scan_vif; | |
806 | struct ath_tx_control txctl = {}; | |
807 | struct sk_buff *skb; | |
808 | struct ieee80211_tx_info *info; | |
809 | int band = sc->offchannel.chan.chandef.chan->band; | |
810 | ||
811 | skb = ieee80211_probereq_get(sc->hw, vif, | |
812 | ssid->ssid, ssid->ssid_len, req->ie_len); | |
813 | if (!skb) | |
814 | return; | |
815 | ||
816 | info = IEEE80211_SKB_CB(skb); | |
817 | if (req->no_cck) | |
818 | info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE; | |
819 | ||
820 | if (req->ie_len) | |
821 | memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len); | |
822 | ||
823 | skb_set_queue_mapping(skb, IEEE80211_AC_VO); | |
824 | ||
825 | if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL)) | |
826 | goto error; | |
827 | ||
828 | txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO]; | |
829 | txctl.force_channel = true; | |
830 | if (ath_tx_start(sc->hw, skb, &txctl)) | |
831 | goto error; | |
832 | ||
833 | return; | |
834 | ||
835 | error: | |
836 | ieee80211_free_txskb(sc->hw, skb); | |
837 | } | |
838 | ||
839 | static void ath_scan_channel_start(struct ath_softc *sc) | |
840 | { | |
bc81d43a | 841 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
dfcbb3e8 SM |
842 | struct cfg80211_scan_request *req = sc->offchannel.scan_req; |
843 | int i; | |
844 | ||
845 | if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) && | |
846 | req->n_ssids) { | |
847 | for (i = 0; i < req->n_ssids; i++) | |
848 | ath_scan_send_probe(sc, &req->ssids[i]); | |
849 | ||
850 | } | |
851 | ||
bc81d43a | 852 | ath_dbg(common, CHAN_CTX, |
878066e7 | 853 | "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n"); |
bc81d43a | 854 | |
dfcbb3e8 SM |
855 | sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT; |
856 | mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration); | |
857 | } | |
858 | ||
705d0bf8 SM |
859 | static void ath_chanctx_timer(unsigned long data) |
860 | { | |
861 | struct ath_softc *sc = (struct ath_softc *) data; | |
878066e7 SM |
862 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
863 | ||
864 | ath_dbg(common, CHAN_CTX, | |
865 | "Channel context timer invoked\n"); | |
705d0bf8 SM |
866 | |
867 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER); | |
868 | } | |
869 | ||
870 | static void ath_offchannel_timer(unsigned long data) | |
dfcbb3e8 SM |
871 | { |
872 | struct ath_softc *sc = (struct ath_softc *)data; | |
873 | struct ath_chanctx *ctx; | |
bc81d43a SM |
874 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
875 | ||
878066e7 | 876 | ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n", |
bc81d43a | 877 | __func__, offchannel_state_string(sc->offchannel.state)); |
dfcbb3e8 SM |
878 | |
879 | switch (sc->offchannel.state) { | |
880 | case ATH_OFFCHANNEL_PROBE_WAIT: | |
881 | if (!sc->offchannel.scan_req) | |
882 | return; | |
883 | ||
884 | /* get first active channel context */ | |
885 | ctx = ath_chanctx_get_oper_chan(sc, true); | |
886 | if (ctx->active) { | |
878066e7 SM |
887 | ath_dbg(common, CHAN_CTX, |
888 | "Switch to oper/active context, " | |
889 | "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n"); | |
890 | ||
dfcbb3e8 SM |
891 | sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND; |
892 | ath_chanctx_switch(sc, ctx, NULL); | |
893 | mod_timer(&sc->offchannel.timer, jiffies + HZ / 10); | |
894 | break; | |
895 | } | |
896 | /* fall through */ | |
897 | case ATH_OFFCHANNEL_SUSPEND: | |
898 | if (!sc->offchannel.scan_req) | |
899 | return; | |
900 | ||
901 | ath_scan_next_channel(sc); | |
902 | break; | |
903 | case ATH_OFFCHANNEL_ROC_START: | |
904 | case ATH_OFFCHANNEL_ROC_WAIT: | |
905 | ctx = ath_chanctx_get_oper_chan(sc, false); | |
906 | sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE; | |
907 | ath_chanctx_switch(sc, ctx, NULL); | |
908 | break; | |
909 | default: | |
910 | break; | |
911 | } | |
912 | } | |
2471adff | 913 | |
6d7cbd77 SM |
914 | static bool |
915 | ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp, | |
916 | bool powersave) | |
917 | { | |
918 | struct ieee80211_vif *vif = avp->vif; | |
919 | struct ieee80211_sta *sta = NULL; | |
920 | struct ieee80211_hdr_3addr *nullfunc; | |
921 | struct ath_tx_control txctl; | |
922 | struct sk_buff *skb; | |
923 | int band = sc->cur_chan->chandef.chan->band; | |
924 | ||
925 | switch (vif->type) { | |
926 | case NL80211_IFTYPE_STATION: | |
cb35582a | 927 | if (!avp->assoc) |
6d7cbd77 SM |
928 | return false; |
929 | ||
930 | skb = ieee80211_nullfunc_get(sc->hw, vif); | |
931 | if (!skb) | |
932 | return false; | |
933 | ||
934 | nullfunc = (struct ieee80211_hdr_3addr *) skb->data; | |
935 | if (powersave) | |
936 | nullfunc->frame_control |= | |
937 | cpu_to_le16(IEEE80211_FCTL_PM); | |
938 | ||
939 | skb_set_queue_mapping(skb, IEEE80211_AC_VO); | |
940 | if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) { | |
941 | dev_kfree_skb_any(skb); | |
942 | return false; | |
943 | } | |
944 | break; | |
945 | default: | |
946 | return false; | |
947 | } | |
948 | ||
949 | memset(&txctl, 0, sizeof(txctl)); | |
950 | txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO]; | |
951 | txctl.sta = sta; | |
952 | txctl.force_channel = true; | |
953 | if (ath_tx_start(sc->hw, skb, &txctl)) { | |
954 | ieee80211_free_txskb(sc->hw, skb); | |
955 | return false; | |
956 | } | |
957 | ||
958 | return true; | |
959 | } | |
960 | ||
961 | static bool | |
962 | ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave) | |
963 | { | |
964 | struct ath_vif *avp; | |
965 | bool sent = false; | |
966 | ||
967 | rcu_read_lock(); | |
968 | list_for_each_entry(avp, &sc->cur_chan->vifs, list) { | |
969 | if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave)) | |
970 | sent = true; | |
971 | } | |
972 | rcu_read_unlock(); | |
973 | ||
974 | return sent; | |
975 | } | |
976 | ||
977 | static bool ath_chanctx_defer_switch(struct ath_softc *sc) | |
978 | { | |
878066e7 SM |
979 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
980 | ||
6d7cbd77 SM |
981 | if (sc->cur_chan == &sc->offchannel.chan) |
982 | return false; | |
983 | ||
984 | switch (sc->sched.state) { | |
985 | case ATH_CHANCTX_STATE_SWITCH: | |
986 | return false; | |
987 | case ATH_CHANCTX_STATE_IDLE: | |
988 | if (!sc->cur_chan->switch_after_beacon) | |
989 | return false; | |
990 | ||
878066e7 SM |
991 | ath_dbg(common, CHAN_CTX, |
992 | "Defer switch, set chanctx state to WAIT_FOR_BEACON\n"); | |
993 | ||
6d7cbd77 SM |
994 | sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON; |
995 | break; | |
996 | default: | |
997 | break; | |
998 | } | |
999 | ||
1000 | return true; | |
1001 | } | |
1002 | ||
55254eea SM |
1003 | static void ath_offchannel_channel_change(struct ath_softc *sc) |
1004 | { | |
1005 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
1006 | ||
878066e7 | 1007 | ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n", |
55254eea SM |
1008 | __func__, offchannel_state_string(sc->offchannel.state)); |
1009 | ||
1010 | switch (sc->offchannel.state) { | |
1011 | case ATH_OFFCHANNEL_PROBE_SEND: | |
1012 | if (!sc->offchannel.scan_req) | |
1013 | return; | |
1014 | ||
1015 | if (sc->cur_chan->chandef.chan != | |
1016 | sc->offchannel.chan.chandef.chan) | |
1017 | return; | |
1018 | ||
1019 | ath_scan_channel_start(sc); | |
1020 | break; | |
1021 | case ATH_OFFCHANNEL_IDLE: | |
1022 | if (!sc->offchannel.scan_req) | |
1023 | return; | |
1024 | ||
1025 | ath_scan_complete(sc, false); | |
1026 | break; | |
1027 | case ATH_OFFCHANNEL_ROC_START: | |
1028 | if (sc->cur_chan != &sc->offchannel.chan) | |
1029 | break; | |
1030 | ||
1031 | sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT; | |
bb628eb9 SM |
1032 | mod_timer(&sc->offchannel.timer, |
1033 | jiffies + sc->offchannel.duration); | |
55254eea SM |
1034 | ieee80211_ready_on_channel(sc->hw); |
1035 | break; | |
1036 | case ATH_OFFCHANNEL_ROC_DONE: | |
1037 | ath_roc_complete(sc, false); | |
1038 | break; | |
1039 | default: | |
1040 | break; | |
1041 | } | |
1042 | } | |
1043 | ||
6d7cbd77 SM |
1044 | void ath_chanctx_set_next(struct ath_softc *sc, bool force) |
1045 | { | |
878066e7 | 1046 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); |
6d7cbd77 SM |
1047 | struct timespec ts; |
1048 | bool measure_time = false; | |
1049 | bool send_ps = false; | |
1050 | ||
1051 | spin_lock_bh(&sc->chan_lock); | |
1052 | if (!sc->next_chan) { | |
1053 | spin_unlock_bh(&sc->chan_lock); | |
1054 | return; | |
1055 | } | |
1056 | ||
1057 | if (!force && ath_chanctx_defer_switch(sc)) { | |
1058 | spin_unlock_bh(&sc->chan_lock); | |
1059 | return; | |
1060 | } | |
1061 | ||
878066e7 SM |
1062 | ath_dbg(common, CHAN_CTX, |
1063 | "%s: current: %d MHz, next: %d MHz\n", | |
1064 | __func__, | |
1065 | sc->cur_chan->chandef.center_freq1, | |
1066 | sc->next_chan->chandef.center_freq1); | |
1067 | ||
6d7cbd77 | 1068 | if (sc->cur_chan != sc->next_chan) { |
878066e7 SM |
1069 | ath_dbg(common, CHAN_CTX, |
1070 | "Stopping current chanctx: %d\n", | |
1071 | sc->cur_chan->chandef.center_freq1); | |
6d7cbd77 SM |
1072 | sc->cur_chan->stopped = true; |
1073 | spin_unlock_bh(&sc->chan_lock); | |
1074 | ||
1075 | if (sc->next_chan == &sc->offchannel.chan) { | |
1076 | getrawmonotonic(&ts); | |
1077 | measure_time = true; | |
1078 | } | |
1079 | __ath9k_flush(sc->hw, ~0, true); | |
1080 | ||
1081 | if (ath_chanctx_send_ps_frame(sc, true)) | |
1082 | __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO), false); | |
1083 | ||
1084 | send_ps = true; | |
1085 | spin_lock_bh(&sc->chan_lock); | |
1086 | ||
1087 | if (sc->cur_chan != &sc->offchannel.chan) { | |
1088 | getrawmonotonic(&sc->cur_chan->tsf_ts); | |
1089 | sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah); | |
1090 | } | |
1091 | } | |
1092 | sc->cur_chan = sc->next_chan; | |
1093 | sc->cur_chan->stopped = false; | |
1094 | sc->next_chan = NULL; | |
124130d7 SM |
1095 | |
1096 | if (!sc->sched.offchannel_pending) | |
1097 | sc->sched.offchannel_duration = 0; | |
1098 | ||
6d7cbd77 SM |
1099 | if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE) |
1100 | sc->sched.state = ATH_CHANCTX_STATE_IDLE; | |
1101 | ||
1102 | spin_unlock_bh(&sc->chan_lock); | |
1103 | ||
1104 | if (sc->sc_ah->chip_fullsleep || | |
1105 | memcmp(&sc->cur_chandef, &sc->cur_chan->chandef, | |
1106 | sizeof(sc->cur_chandef))) { | |
878066e7 SM |
1107 | ath_dbg(common, CHAN_CTX, |
1108 | "%s: Set channel %d MHz\n", | |
1109 | __func__, sc->cur_chan->chandef.center_freq1); | |
6d7cbd77 SM |
1110 | ath_set_channel(sc); |
1111 | if (measure_time) | |
1112 | sc->sched.channel_switch_time = | |
1113 | ath9k_hw_get_tsf_offset(&ts, NULL); | |
1114 | } | |
1115 | if (send_ps) | |
1116 | ath_chanctx_send_ps_frame(sc, false); | |
1117 | ||
1118 | ath_offchannel_channel_change(sc); | |
1119 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH); | |
1120 | } | |
1121 | ||
0e62f8b7 SM |
1122 | static void ath_chanctx_work(struct work_struct *work) |
1123 | { | |
1124 | struct ath_softc *sc = container_of(work, struct ath_softc, | |
1125 | chanctx_work); | |
1126 | mutex_lock(&sc->mutex); | |
1127 | ath_chanctx_set_next(sc, false); | |
1128 | mutex_unlock(&sc->mutex); | |
1129 | } | |
1130 | ||
e90e302a SM |
1131 | void ath9k_offchannel_init(struct ath_softc *sc) |
1132 | { | |
1133 | struct ath_chanctx *ctx; | |
1134 | struct ath_common *common = ath9k_hw_common(sc->sc_ah); | |
1135 | struct ieee80211_supported_band *sband; | |
1136 | struct ieee80211_channel *chan; | |
1137 | int i; | |
1138 | ||
1139 | sband = &common->sbands[IEEE80211_BAND_2GHZ]; | |
1140 | if (!sband->n_channels) | |
1141 | sband = &common->sbands[IEEE80211_BAND_5GHZ]; | |
1142 | ||
1143 | chan = &sband->channels[0]; | |
1144 | ||
1145 | ctx = &sc->offchannel.chan; | |
1146 | INIT_LIST_HEAD(&ctx->vifs); | |
1147 | ctx->txpower = ATH_TXPOWER_MAX; | |
1148 | cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20); | |
1149 | ||
1150 | for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) | |
1151 | INIT_LIST_HEAD(&ctx->acq[i]); | |
1152 | ||
1153 | sc->offchannel.chan.offchannel = true; | |
1154 | } | |
1155 | ||
705d0bf8 SM |
1156 | void ath9k_init_channel_context(struct ath_softc *sc) |
1157 | { | |
1158 | INIT_WORK(&sc->chanctx_work, ath_chanctx_work); | |
1159 | ||
1160 | setup_timer(&sc->offchannel.timer, ath_offchannel_timer, | |
1161 | (unsigned long)sc); | |
1162 | setup_timer(&sc->sched.timer, ath_chanctx_timer, | |
1163 | (unsigned long)sc); | |
1164 | } | |
c7dd40c9 | 1165 | |
ea22df29 SM |
1166 | void ath9k_deinit_channel_context(struct ath_softc *sc) |
1167 | { | |
1168 | cancel_work_sync(&sc->chanctx_work); | |
1169 | } | |
1170 | ||
499afacc SM |
1171 | bool ath9k_is_chanctx_enabled(void) |
1172 | { | |
1173 | return (ath9k_use_chanctx == 1); | |
1174 | } | |
1175 | ||
0e08b5fb SM |
1176 | /********************/ |
1177 | /* Queue management */ | |
1178 | /********************/ | |
1179 | ||
a064eaa1 SM |
1180 | void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx) |
1181 | { | |
1182 | struct ath_hw *ah = sc->sc_ah; | |
1183 | int i; | |
1184 | ||
1185 | if (ctx == &sc->offchannel.chan) { | |
1186 | ieee80211_stop_queue(sc->hw, | |
1187 | sc->hw->offchannel_tx_hw_queue); | |
1188 | } else { | |
1189 | for (i = 0; i < IEEE80211_NUM_ACS; i++) | |
1190 | ieee80211_stop_queue(sc->hw, | |
1191 | ctx->hw_queue_base + i); | |
1192 | } | |
1193 | ||
1194 | if (ah->opmode == NL80211_IFTYPE_AP) | |
1195 | ieee80211_stop_queue(sc->hw, sc->hw->queues - 2); | |
1196 | } | |
1197 | ||
1198 | ||
b3903153 | 1199 | void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx) |
0e08b5fb SM |
1200 | { |
1201 | struct ath_hw *ah = sc->sc_ah; | |
1202 | int i; | |
1203 | ||
b3903153 | 1204 | if (ctx == &sc->offchannel.chan) { |
0e08b5fb SM |
1205 | ieee80211_wake_queue(sc->hw, |
1206 | sc->hw->offchannel_tx_hw_queue); | |
1207 | } else { | |
1208 | for (i = 0; i < IEEE80211_NUM_ACS; i++) | |
1209 | ieee80211_wake_queue(sc->hw, | |
b3903153 | 1210 | ctx->hw_queue_base + i); |
0e08b5fb SM |
1211 | } |
1212 | ||
1213 | if (ah->opmode == NL80211_IFTYPE_AP) | |
1214 | ieee80211_wake_queue(sc->hw, sc->hw->queues - 2); | |
1215 | } | |
1216 | ||
c7dd40c9 SM |
1217 | /*****************/ |
1218 | /* P2P Powersave */ | |
1219 | /*****************/ | |
1220 | ||
1221 | static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp) | |
2471adff SM |
1222 | { |
1223 | struct ath_hw *ah = sc->sc_ah; | |
1224 | s32 tsf, target_tsf; | |
1225 | ||
1226 | if (!avp || !avp->noa.has_next_tsf) | |
1227 | return; | |
1228 | ||
1229 | ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer); | |
1230 | ||
1231 | tsf = ath9k_hw_gettsf32(sc->sc_ah); | |
1232 | ||
1233 | target_tsf = avp->noa.next_tsf; | |
1234 | if (!avp->noa.absent) | |
1235 | target_tsf -= ATH_P2P_PS_STOP_TIME; | |
1236 | ||
1237 | if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME) | |
1238 | target_tsf = tsf + ATH_P2P_PS_STOP_TIME; | |
1239 | ||
1240 | ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000); | |
1241 | } | |
1242 | ||
c7dd40c9 SM |
1243 | static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif) |
1244 | { | |
1245 | struct ath_vif *avp = (void *)vif->drv_priv; | |
1246 | u32 tsf; | |
1247 | ||
1248 | if (!sc->p2p_ps_timer) | |
1249 | return; | |
1250 | ||
1251 | if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p) | |
1252 | return; | |
1253 | ||
1254 | sc->p2p_ps_vif = avp; | |
1255 | tsf = ath9k_hw_gettsf32(sc->sc_ah); | |
1256 | ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf); | |
1257 | ath9k_update_p2p_ps_timer(sc, avp); | |
1258 | } | |
1259 | ||
fdcf1bd4 SM |
1260 | static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp) |
1261 | { | |
1262 | struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon; | |
1263 | u8 switch_time, ctwin; | |
1264 | ||
1265 | /* | |
1266 | * Channel switch in multi-channel mode is deferred | |
1267 | * by a quarter beacon interval when handling | |
1268 | * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO | |
1269 | * interface is guaranteed to be discoverable | |
1270 | * for that duration after a TBTT. | |
1271 | */ | |
1272 | switch_time = cur_conf->beacon_interval / 4; | |
1273 | ||
1274 | ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow; | |
1275 | if (ctwin && (ctwin < switch_time)) | |
1276 | return ctwin; | |
1277 | ||
1278 | if (switch_time < P2P_DEFAULT_CTWIN) | |
1279 | return 0; | |
1280 | ||
1281 | return P2P_DEFAULT_CTWIN; | |
1282 | } | |
1283 | ||
11e39a4e SM |
1284 | void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp, |
1285 | struct sk_buff *skb) | |
1286 | { | |
1287 | static const u8 noa_ie_hdr[] = { | |
1288 | WLAN_EID_VENDOR_SPECIFIC, /* type */ | |
1289 | 0, /* length */ | |
1290 | 0x50, 0x6f, 0x9a, /* WFA OUI */ | |
1291 | 0x09, /* P2P subtype */ | |
1292 | 0x0c, /* Notice of Absence */ | |
1293 | 0x00, /* LSB of little-endian len */ | |
1294 | 0x00, /* MSB of little-endian len */ | |
1295 | }; | |
1296 | ||
1297 | struct ieee80211_p2p_noa_attr *noa; | |
1298 | int noa_len, noa_desc, i = 0; | |
1299 | u8 *hdr; | |
1300 | ||
d0975edd | 1301 | if (!avp->offchannel_duration && !avp->noa_duration) |
11e39a4e SM |
1302 | return; |
1303 | ||
d0975edd | 1304 | noa_desc = !!avp->offchannel_duration + !!avp->noa_duration; |
11e39a4e SM |
1305 | noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc; |
1306 | ||
1307 | hdr = skb_put(skb, sizeof(noa_ie_hdr)); | |
1308 | memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr)); | |
1309 | hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2; | |
1310 | hdr[7] = noa_len; | |
1311 | ||
1312 | noa = (void *) skb_put(skb, noa_len); | |
1313 | memset(noa, 0, noa_len); | |
1314 | ||
1315 | noa->index = avp->noa_index; | |
fdcf1bd4 SM |
1316 | noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp); |
1317 | ||
d0975edd SM |
1318 | if (avp->noa_duration) { |
1319 | if (avp->periodic_noa) { | |
1320 | u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval); | |
1321 | noa->desc[i].count = 255; | |
1322 | noa->desc[i].interval = cpu_to_le32(interval); | |
1323 | } else { | |
1324 | noa->desc[i].count = 1; | |
1325 | } | |
11e39a4e | 1326 | |
d0975edd SM |
1327 | noa->desc[i].start_time = cpu_to_le32(avp->noa_start); |
1328 | noa->desc[i].duration = cpu_to_le32(avp->noa_duration); | |
11e39a4e SM |
1329 | i++; |
1330 | } | |
1331 | ||
1332 | if (avp->offchannel_duration) { | |
1333 | noa->desc[i].count = 1; | |
1334 | noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start); | |
1335 | noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration); | |
1336 | } | |
1337 | } | |
1338 | ||
2471adff SM |
1339 | void ath9k_p2p_ps_timer(void *priv) |
1340 | { | |
1341 | struct ath_softc *sc = priv; | |
1342 | struct ath_vif *avp = sc->p2p_ps_vif; | |
1343 | struct ieee80211_vif *vif; | |
1344 | struct ieee80211_sta *sta; | |
1345 | struct ath_node *an; | |
1346 | u32 tsf; | |
1347 | ||
1348 | del_timer_sync(&sc->sched.timer); | |
1349 | ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer); | |
1350 | ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER); | |
1351 | ||
1352 | if (!avp || avp->chanctx != sc->cur_chan) | |
1353 | return; | |
1354 | ||
1355 | tsf = ath9k_hw_gettsf32(sc->sc_ah); | |
1356 | if (!avp->noa.absent) | |
1357 | tsf += ATH_P2P_PS_STOP_TIME; | |
1358 | ||
1359 | if (!avp->noa.has_next_tsf || | |
1360 | avp->noa.next_tsf - tsf > BIT(31)) | |
1361 | ieee80211_update_p2p_noa(&avp->noa, tsf); | |
1362 | ||
1363 | ath9k_update_p2p_ps_timer(sc, avp); | |
1364 | ||
1365 | rcu_read_lock(); | |
1366 | ||
1367 | vif = avp->vif; | |
cb35582a | 1368 | sta = ieee80211_find_sta(vif, avp->bssid); |
2471adff SM |
1369 | if (!sta) |
1370 | goto out; | |
1371 | ||
1372 | an = (void *) sta->drv_priv; | |
1373 | if (an->sleeping == !!avp->noa.absent) | |
1374 | goto out; | |
1375 | ||
1376 | an->sleeping = avp->noa.absent; | |
1377 | if (an->sleeping) | |
1378 | ath_tx_aggr_sleep(sta, sc, an); | |
1379 | else | |
1380 | ath_tx_aggr_wakeup(sc, an); | |
1381 | ||
1382 | out: | |
1383 | rcu_read_unlock(); | |
1384 | } | |
1385 | ||
c7dd40c9 SM |
1386 | void ath9k_p2p_bss_info_changed(struct ath_softc *sc, |
1387 | struct ieee80211_vif *vif) | |
1388 | { | |
1389 | unsigned long flags; | |
1390 | ||
1391 | spin_lock_bh(&sc->sc_pcu_lock); | |
1392 | spin_lock_irqsave(&sc->sc_pm_lock, flags); | |
1393 | if (!(sc->ps_flags & PS_BEACON_SYNC)) | |
1394 | ath9k_update_p2p_ps(sc, vif); | |
1395 | spin_unlock_irqrestore(&sc->sc_pm_lock, flags); | |
1396 | spin_unlock_bh(&sc->sc_pcu_lock); | |
1397 | } | |
1398 | ||
1399 | void ath9k_p2p_beacon_sync(struct ath_softc *sc) | |
1400 | { | |
1401 | if (sc->p2p_ps_vif) | |
1402 | ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif); | |
1403 | } | |
1404 | ||
1405 | void ath9k_p2p_remove_vif(struct ath_softc *sc, | |
1406 | struct ieee80211_vif *vif) | |
2471adff SM |
1407 | { |
1408 | struct ath_vif *avp = (void *)vif->drv_priv; | |
2471adff | 1409 | |
c7dd40c9 SM |
1410 | spin_lock_bh(&sc->sc_pcu_lock); |
1411 | if (avp == sc->p2p_ps_vif) { | |
1412 | sc->p2p_ps_vif = NULL; | |
1413 | ath9k_update_p2p_ps_timer(sc, NULL); | |
1414 | } | |
1415 | spin_unlock_bh(&sc->sc_pcu_lock); | |
1416 | } | |
1417 | ||
1418 | int ath9k_init_p2p(struct ath_softc *sc) | |
1419 | { | |
1420 | sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer, | |
1421 | NULL, sc, AR_FIRST_NDP_TIMER); | |
2471adff | 1422 | if (!sc->p2p_ps_timer) |
c7dd40c9 | 1423 | return -ENOMEM; |
2471adff | 1424 | |
c7dd40c9 SM |
1425 | return 0; |
1426 | } | |
2471adff | 1427 | |
c7dd40c9 SM |
1428 | void ath9k_deinit_p2p(struct ath_softc *sc) |
1429 | { | |
1430 | if (sc->p2p_ps_timer) | |
1431 | ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer); | |
2471adff | 1432 | } |
c7dd40c9 SM |
1433 | |
1434 | #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */ |