2 * Copyright (c) 2014 Qualcomm Atheros, Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* Set/change channels. If the channel is really being changed, it's done
20 * by reseting the chip. To accomplish this we must first cleanup any pending
21 * DMA, then restart stuff.
23 static int ath_set_channel(struct ath_softc
*sc
)
25 struct ath_hw
*ah
= sc
->sc_ah
;
26 struct ath_common
*common
= ath9k_hw_common(ah
);
27 struct ieee80211_hw
*hw
= sc
->hw
;
28 struct ath9k_channel
*hchan
;
29 struct cfg80211_chan_def
*chandef
= &sc
->cur_chan
->chandef
;
30 struct ieee80211_channel
*chan
= chandef
->chan
;
31 int pos
= chan
->hw_value
;
35 if (test_bit(ATH_OP_INVALID
, &common
->op_flags
))
39 old_pos
= ah
->curchan
- &ah
->channels
[0];
41 ath_dbg(common
, CONFIG
, "Set channel: %d MHz width: %d\n",
42 chan
->center_freq
, chandef
->width
);
44 /* update survey stats for the old channel before switching */
45 spin_lock_bh(&common
->cc_lock
);
46 ath_update_survey_stats(sc
);
47 spin_unlock_bh(&common
->cc_lock
);
49 ath9k_cmn_get_channel(hw
, ah
, chandef
);
51 /* If the operating channel changes, change the survey in-use flags
53 * Reset the survey data for the new channel, unless we're switching
54 * back to the operating channel from an off-channel operation.
56 if (!sc
->cur_chan
->offchannel
&& sc
->cur_survey
!= &sc
->survey
[pos
]) {
58 sc
->cur_survey
->filled
&= ~SURVEY_INFO_IN_USE
;
60 sc
->cur_survey
= &sc
->survey
[pos
];
62 memset(sc
->cur_survey
, 0, sizeof(struct survey_info
));
63 sc
->cur_survey
->filled
|= SURVEY_INFO_IN_USE
;
64 } else if (!(sc
->survey
[pos
].filled
& SURVEY_INFO_IN_USE
)) {
65 memset(&sc
->survey
[pos
], 0, sizeof(struct survey_info
));
68 hchan
= &sc
->sc_ah
->channels
[pos
];
69 r
= ath_reset_internal(sc
, hchan
);
73 /* The most recent snapshot of channel->noisefloor for the old
74 * channel is only available after the hardware reset. Copy it to
75 * the survey stats now.
78 ath_update_survey_nf(sc
, old_pos
);
80 /* Enable radar pulse detection if on a DFS channel. Spectral
81 * scanning and radar detection can not be used concurrently.
83 if (hw
->conf
.radar_enabled
) {
86 /* set HW specific DFS configuration */
87 ath9k_hw_set_radar_params(ah
);
88 rxfilter
= ath9k_hw_getrxfilter(ah
);
89 rxfilter
|= ATH9K_RX_FILTER_PHYRADAR
|
90 ATH9K_RX_FILTER_PHYERR
;
91 ath9k_hw_setrxfilter(ah
, rxfilter
);
92 ath_dbg(common
, DFS
, "DFS enabled at freq %d\n",
95 /* perform spectral scan if requested. */
96 if (test_bit(ATH_OP_SCANNING
, &common
->op_flags
) &&
97 sc
->spectral_mode
== SPECTRAL_CHANSCAN
)
98 ath9k_spectral_scan_trigger(hw
);
104 void ath_chanctx_check_active(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
106 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
114 list_for_each_entry(avp
, &ctx
->vifs
, list
) {
115 struct ieee80211_vif
*vif
= avp
->vif
;
118 case NL80211_IFTYPE_P2P_CLIENT
:
119 case NL80211_IFTYPE_STATION
:
120 if (vif
->bss_conf
.assoc
)
128 ctx
->active
= active
;
130 ath_for_each_chanctx(sc
, ctx
) {
131 if (!ctx
->assigned
|| list_empty(&ctx
->vifs
))
137 clear_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
);
140 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
))
142 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
);
145 void ath_chanctx_init(struct ath_softc
*sc
)
147 struct ath_chanctx
*ctx
;
148 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
149 struct ieee80211_supported_band
*sband
;
150 struct ieee80211_channel
*chan
;
153 sband
= &common
->sbands
[IEEE80211_BAND_2GHZ
];
154 if (!sband
->n_channels
)
155 sband
= &common
->sbands
[IEEE80211_BAND_5GHZ
];
157 chan
= &sband
->channels
[0];
158 for (i
= 0; i
< ATH9K_NUM_CHANCTX
; i
++) {
159 ctx
= &sc
->chanctx
[i
];
160 cfg80211_chandef_create(&ctx
->chandef
, chan
, NL80211_CHAN_HT20
);
161 INIT_LIST_HEAD(&ctx
->vifs
);
162 ctx
->txpower
= ATH_TXPOWER_MAX
;
163 for (j
= 0; j
< ARRAY_SIZE(ctx
->acq
); j
++)
164 INIT_LIST_HEAD(&ctx
->acq
[j
]);
166 ctx
= &sc
->offchannel
.chan
;
167 cfg80211_chandef_create(&ctx
->chandef
, chan
, NL80211_CHAN_HT20
);
168 INIT_LIST_HEAD(&ctx
->vifs
);
169 ctx
->txpower
= ATH_TXPOWER_MAX
;
170 for (j
= 0; j
< ARRAY_SIZE(ctx
->acq
); j
++)
171 INIT_LIST_HEAD(&ctx
->acq
[j
]);
172 sc
->offchannel
.chan
.offchannel
= true;
176 void ath_chanctx_switch(struct ath_softc
*sc
, struct ath_chanctx
*ctx
,
177 struct cfg80211_chan_def
*chandef
)
179 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
181 spin_lock_bh(&sc
->chan_lock
);
183 if (test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) &&
184 (sc
->cur_chan
!= ctx
) && (ctx
== &sc
->offchannel
.chan
)) {
185 sc
->sched
.offchannel_pending
= true;
186 spin_unlock_bh(&sc
->chan_lock
);
192 ctx
->chandef
= *chandef
;
193 ath_dbg(common
, CHAN_CTX
,
194 "Assigned next_chan to %d MHz\n", chandef
->center_freq1
);
197 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
198 sc
->sched
.offchannel_duration
=
199 TU_TO_USEC(sc
->offchannel
.duration
) +
200 sc
->sched
.channel_switch_time
;
203 ath_dbg(common
, CHAN_CTX
,
204 "Offchannel duration for chan %d MHz : %u\n",
205 chandef
->center_freq1
,
206 sc
->sched
.offchannel_duration
);
209 spin_unlock_bh(&sc
->chan_lock
);
210 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
213 void ath_chanctx_set_channel(struct ath_softc
*sc
, struct ath_chanctx
*ctx
,
214 struct cfg80211_chan_def
*chandef
)
218 spin_lock_bh(&sc
->chan_lock
);
220 memcpy(&ctx
->chandef
, chandef
, sizeof(*chandef
));
221 cur_chan
= sc
->cur_chan
== ctx
;
222 spin_unlock_bh(&sc
->chan_lock
);
230 struct ath_chanctx
*ath_chanctx_get_oper_chan(struct ath_softc
*sc
, bool active
)
232 struct ath_chanctx
*ctx
;
234 ath_for_each_chanctx(sc
, ctx
) {
235 if (!ctx
->assigned
|| list_empty(&ctx
->vifs
))
237 if (active
&& !ctx
->active
)
240 if (ctx
->switch_after_beacon
)
244 return &sc
->chanctx
[0];
247 void ath_chanctx_offchan_switch(struct ath_softc
*sc
,
248 struct ieee80211_channel
*chan
)
250 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
251 struct cfg80211_chan_def chandef
;
253 cfg80211_chandef_create(&chandef
, chan
, NL80211_CHAN_NO_HT
);
254 ath_dbg(common
, CHAN_CTX
,
255 "Channel definition created: %d MHz\n", chandef
.center_freq1
);
257 ath_chanctx_switch(sc
, &sc
->offchannel
.chan
, &chandef
);
260 static struct ath_chanctx
*
261 ath_chanctx_get_next(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
263 int idx
= ctx
- &sc
->chanctx
[0];
265 return &sc
->chanctx
[!idx
];
268 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc
*sc
)
270 struct ath_chanctx
*prev
, *cur
;
272 u32 cur_tsf
, prev_tsf
, beacon_int
;
275 beacon_int
= TU_TO_USEC(sc
->cur_chan
->beacon
.beacon_interval
);
278 prev
= ath_chanctx_get_next(sc
, cur
);
280 getrawmonotonic(&ts
);
281 cur_tsf
= (u32
) cur
->tsf_val
+
282 ath9k_hw_get_tsf_offset(&cur
->tsf_ts
, &ts
);
284 prev_tsf
= prev
->last_beacon
- (u32
) prev
->tsf_val
+ cur_tsf
;
285 prev_tsf
-= ath9k_hw_get_tsf_offset(&prev
->tsf_ts
, &ts
);
287 /* Adjust the TSF time of the AP chanctx to keep its beacons
288 * at half beacon interval offset relative to the STA chanctx.
290 offset
= cur_tsf
- prev_tsf
;
292 /* Ignore stale data or spurious timestamps */
293 if (offset
< 0 || offset
> 3 * beacon_int
)
296 offset
= beacon_int
/ 2 - (offset
% beacon_int
);
297 prev
->tsf_val
+= offset
;
300 /* Configure the TSF based hardware timer for a channel switch.
301 * Also set up backup software timer, in case the gen timer fails.
302 * This could be caused by a hardware reset.
304 static void ath_chanctx_setup_timer(struct ath_softc
*sc
, u32 tsf_time
)
306 struct ath_hw
*ah
= sc
->sc_ah
;
308 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
309 ath9k_hw_gen_timer_start(ah
, sc
->p2p_ps_timer
, tsf_time
, 1000000);
311 tsf_time
-= ath9k_hw_gettsf32(ah
);
312 tsf_time
= msecs_to_jiffies(tsf_time
/ 1000) + 1;
313 mod_timer(&sc
->sched
.timer
, tsf_time
);
316 void ath_chanctx_event(struct ath_softc
*sc
, struct ieee80211_vif
*vif
,
317 enum ath_chanctx_event ev
)
319 struct ath_hw
*ah
= sc
->sc_ah
;
320 struct ath_common
*common
= ath9k_hw_common(ah
);
321 struct ath_beacon_config
*cur_conf
;
322 struct ath_vif
*avp
= NULL
;
323 struct ath_chanctx
*ctx
;
326 bool noa_changed
= false;
329 avp
= (struct ath_vif
*) vif
->drv_priv
;
331 spin_lock_bh(&sc
->chan_lock
);
334 case ATH_CHANCTX_EVENT_BEACON_PREPARE
:
335 if (avp
->offchannel_duration
)
336 avp
->offchannel_duration
= 0;
338 if (avp
->chanctx
!= sc
->cur_chan
)
341 if (sc
->sched
.offchannel_pending
) {
342 sc
->sched
.offchannel_pending
= false;
343 sc
->next_chan
= &sc
->offchannel
.chan
;
344 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
347 ctx
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
348 if (ctx
->active
&& sc
->sched
.state
== ATH_CHANCTX_STATE_IDLE
) {
350 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
353 /* if the timer missed its window, use the next interval */
354 if (sc
->sched
.state
== ATH_CHANCTX_STATE_WAIT_FOR_TIMER
)
355 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
357 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
)
360 sc
->sched
.beacon_pending
= true;
361 sc
->sched
.next_tbtt
= REG_READ(ah
, AR_NEXT_TBTT_TIMER
);
363 cur_conf
= &sc
->cur_chan
->beacon
;
364 beacon_int
= TU_TO_USEC(cur_conf
->beacon_interval
);
366 /* defer channel switch by a quarter beacon interval */
367 tsf_time
= sc
->sched
.next_tbtt
+ beacon_int
/ 4;
368 sc
->sched
.switch_start_time
= tsf_time
;
369 sc
->cur_chan
->last_beacon
= sc
->sched
.next_tbtt
;
371 /* Prevent wrap-around issues */
372 if (avp
->periodic_noa_duration
&&
373 tsf_time
- avp
->periodic_noa_start
> BIT(30))
374 avp
->periodic_noa_duration
= 0;
376 if (ctx
->active
&& !avp
->periodic_noa_duration
) {
377 avp
->periodic_noa_start
= tsf_time
;
378 avp
->periodic_noa_duration
=
379 TU_TO_USEC(cur_conf
->beacon_interval
) / 2 -
380 sc
->sched
.channel_switch_time
;
382 } else if (!ctx
->active
&& avp
->periodic_noa_duration
) {
383 avp
->periodic_noa_duration
= 0;
387 /* If at least two consecutive beacons were missed on the STA
388 * chanctx, stay on the STA channel for one extra beacon period,
389 * to resync the timer properly.
391 if (ctx
->active
&& sc
->sched
.beacon_miss
>= 2)
392 sc
->sched
.offchannel_duration
= 3 * beacon_int
/ 2;
394 if (sc
->sched
.offchannel_duration
) {
396 avp
->offchannel_start
= tsf_time
;
397 avp
->offchannel_duration
=
398 sc
->sched
.offchannel_duration
;
404 case ATH_CHANCTX_EVENT_BEACON_SENT
:
405 if (!sc
->sched
.beacon_pending
)
408 sc
->sched
.beacon_pending
= false;
409 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
)
412 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
;
413 ath_chanctx_setup_timer(sc
, sc
->sched
.switch_start_time
);
415 case ATH_CHANCTX_EVENT_TSF_TIMER
:
416 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
)
419 if (!sc
->cur_chan
->switch_after_beacon
&&
420 sc
->sched
.beacon_pending
)
421 sc
->sched
.beacon_miss
++;
423 sc
->sched
.state
= ATH_CHANCTX_STATE_SWITCH
;
424 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
426 case ATH_CHANCTX_EVENT_BEACON_RECEIVED
:
427 if (!test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) ||
428 sc
->cur_chan
== &sc
->offchannel
.chan
)
431 ath_chanctx_adjust_tbtt_delta(sc
);
432 sc
->sched
.beacon_pending
= false;
433 sc
->sched
.beacon_miss
= 0;
435 /* TSF time might have been updated by the incoming beacon,
436 * need update the channel switch timer to reflect the change.
438 tsf_time
= sc
->sched
.switch_start_time
;
439 tsf_time
-= (u32
) sc
->cur_chan
->tsf_val
+
440 ath9k_hw_get_tsf_offset(&sc
->cur_chan
->tsf_ts
, NULL
);
441 tsf_time
+= ath9k_hw_gettsf32(ah
);
444 ath_chanctx_setup_timer(sc
, tsf_time
);
446 case ATH_CHANCTX_EVENT_ASSOC
:
447 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_FORCE_ACTIVE
||
448 avp
->chanctx
!= sc
->cur_chan
)
451 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
453 case ATH_CHANCTX_EVENT_SWITCH
:
454 if (!test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) ||
455 sc
->sched
.state
== ATH_CHANCTX_STATE_FORCE_ACTIVE
||
456 sc
->cur_chan
->switch_after_beacon
||
457 sc
->cur_chan
== &sc
->offchannel
.chan
)
460 /* If this is a station chanctx, stay active for a half
461 * beacon period (minus channel switch time)
463 sc
->next_chan
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
464 cur_conf
= &sc
->cur_chan
->beacon
;
466 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
;
468 tsf_time
= TU_TO_USEC(cur_conf
->beacon_interval
) / 2;
469 if (sc
->sched
.beacon_miss
>= 2) {
470 sc
->sched
.beacon_miss
= 0;
474 tsf_time
-= sc
->sched
.channel_switch_time
;
475 tsf_time
+= ath9k_hw_gettsf32(sc
->sc_ah
);
476 sc
->sched
.switch_start_time
= tsf_time
;
478 ath_chanctx_setup_timer(sc
, tsf_time
);
479 sc
->sched
.beacon_pending
= true;
481 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
:
482 if (sc
->cur_chan
== &sc
->offchannel
.chan
||
483 sc
->cur_chan
->switch_after_beacon
)
486 sc
->next_chan
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
487 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
489 case ATH_CHANCTX_EVENT_UNASSIGN
:
490 if (sc
->cur_chan
->assigned
) {
491 if (sc
->next_chan
&& !sc
->next_chan
->assigned
&&
492 sc
->next_chan
!= &sc
->offchannel
.chan
)
493 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
497 ctx
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
498 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
503 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
507 spin_unlock_bh(&sc
->chan_lock
);
510 static int ath_scan_channel_duration(struct ath_softc
*sc
,
511 struct ieee80211_channel
*chan
)
513 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
515 if (!req
->n_ssids
|| (chan
->flags
& IEEE80211_CHAN_NO_IR
))
516 return (HZ
/ 9); /* ~110 ms */
518 return (HZ
/ 16); /* ~60 ms */
522 ath_scan_next_channel(struct ath_softc
*sc
)
524 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
525 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
526 struct ieee80211_channel
*chan
;
528 if (sc
->offchannel
.scan_idx
>= req
->n_channels
) {
529 ath_dbg(common
, CHAN_CTX
,
530 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
531 sc
->offchannel
.scan_idx
,
534 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
535 ath_chanctx_switch(sc
, ath_chanctx_get_oper_chan(sc
, false),
540 ath_dbg(common
, CHAN_CTX
,
541 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
542 sc
->offchannel
.scan_idx
);
544 chan
= req
->channels
[sc
->offchannel
.scan_idx
++];
545 sc
->offchannel
.duration
= ath_scan_channel_duration(sc
, chan
);
546 sc
->offchannel
.state
= ATH_OFFCHANNEL_PROBE_SEND
;
548 ath_chanctx_offchan_switch(sc
, chan
);
551 void ath_offchannel_next(struct ath_softc
*sc
)
553 struct ieee80211_vif
*vif
;
555 if (sc
->offchannel
.scan_req
) {
556 vif
= sc
->offchannel
.scan_vif
;
557 sc
->offchannel
.chan
.txpower
= vif
->bss_conf
.txpower
;
558 ath_scan_next_channel(sc
);
559 } else if (sc
->offchannel
.roc_vif
) {
560 vif
= sc
->offchannel
.roc_vif
;
561 sc
->offchannel
.chan
.txpower
= vif
->bss_conf
.txpower
;
562 sc
->offchannel
.duration
= sc
->offchannel
.roc_duration
;
563 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_START
;
564 ath_chanctx_offchan_switch(sc
, sc
->offchannel
.roc_chan
);
566 ath_chanctx_switch(sc
, ath_chanctx_get_oper_chan(sc
, false),
568 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
574 void ath_roc_complete(struct ath_softc
*sc
, bool abort
)
576 sc
->offchannel
.roc_vif
= NULL
;
577 sc
->offchannel
.roc_chan
= NULL
;
579 ieee80211_remain_on_channel_expired(sc
->hw
);
580 ath_offchannel_next(sc
);
581 ath9k_ps_restore(sc
);
584 void ath_scan_complete(struct ath_softc
*sc
, bool abort
)
586 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
589 ath_dbg(common
, CHAN_CTX
, "HW scan aborted\n");
591 ath_dbg(common
, CHAN_CTX
, "HW scan complete\n");
593 sc
->offchannel
.scan_req
= NULL
;
594 sc
->offchannel
.scan_vif
= NULL
;
595 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
596 ieee80211_scan_completed(sc
->hw
, abort
);
597 clear_bit(ATH_OP_SCANNING
, &common
->op_flags
);
598 ath_offchannel_next(sc
);
599 ath9k_ps_restore(sc
);
602 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
604 static const char *offchannel_state_string(enum ath_offchannel_state state
)
606 #define case_rtn_string(val) case val: return #val
609 case_rtn_string(ATH_OFFCHANNEL_IDLE
);
610 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND
);
611 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT
);
612 case_rtn_string(ATH_OFFCHANNEL_SUSPEND
);
613 case_rtn_string(ATH_OFFCHANNEL_ROC_START
);
614 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT
);
615 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE
);
621 static void ath_scan_send_probe(struct ath_softc
*sc
,
622 struct cfg80211_ssid
*ssid
)
624 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
625 struct ieee80211_vif
*vif
= sc
->offchannel
.scan_vif
;
626 struct ath_tx_control txctl
= {};
628 struct ieee80211_tx_info
*info
;
629 int band
= sc
->offchannel
.chan
.chandef
.chan
->band
;
631 skb
= ieee80211_probereq_get(sc
->hw
, vif
,
632 ssid
->ssid
, ssid
->ssid_len
, req
->ie_len
);
636 info
= IEEE80211_SKB_CB(skb
);
638 info
->flags
|= IEEE80211_TX_CTL_NO_CCK_RATE
;
641 memcpy(skb_put(skb
, req
->ie_len
), req
->ie
, req
->ie_len
);
643 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
645 if (!ieee80211_tx_prepare_skb(sc
->hw
, vif
, skb
, band
, NULL
))
648 txctl
.txq
= sc
->tx
.txq_map
[IEEE80211_AC_VO
];
649 txctl
.force_channel
= true;
650 if (ath_tx_start(sc
->hw
, skb
, &txctl
))
656 ieee80211_free_txskb(sc
->hw
, skb
);
659 static void ath_scan_channel_start(struct ath_softc
*sc
)
661 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
662 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
665 if (!(sc
->cur_chan
->chandef
.chan
->flags
& IEEE80211_CHAN_NO_IR
) &&
667 for (i
= 0; i
< req
->n_ssids
; i
++)
668 ath_scan_send_probe(sc
, &req
->ssids
[i
]);
672 ath_dbg(common
, CHAN_CTX
,
673 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
675 sc
->offchannel
.state
= ATH_OFFCHANNEL_PROBE_WAIT
;
676 mod_timer(&sc
->offchannel
.timer
, jiffies
+ sc
->offchannel
.duration
);
679 static void ath_chanctx_timer(unsigned long data
)
681 struct ath_softc
*sc
= (struct ath_softc
*) data
;
683 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_TSF_TIMER
);
686 static void ath_offchannel_timer(unsigned long data
)
688 struct ath_softc
*sc
= (struct ath_softc
*)data
;
689 struct ath_chanctx
*ctx
;
690 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
692 ath_dbg(common
, CHAN_CTX
, "%s: state: %s\n",
693 __func__
, offchannel_state_string(sc
->offchannel
.state
));
695 switch (sc
->offchannel
.state
) {
696 case ATH_OFFCHANNEL_PROBE_WAIT
:
697 if (!sc
->offchannel
.scan_req
)
700 /* get first active channel context */
701 ctx
= ath_chanctx_get_oper_chan(sc
, true);
703 sc
->offchannel
.state
= ATH_OFFCHANNEL_SUSPEND
;
704 ath_chanctx_switch(sc
, ctx
, NULL
);
705 mod_timer(&sc
->offchannel
.timer
, jiffies
+ HZ
/ 10);
709 case ATH_OFFCHANNEL_SUSPEND
:
710 if (!sc
->offchannel
.scan_req
)
713 ath_scan_next_channel(sc
);
715 case ATH_OFFCHANNEL_ROC_START
:
716 case ATH_OFFCHANNEL_ROC_WAIT
:
717 ctx
= ath_chanctx_get_oper_chan(sc
, false);
718 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_DONE
;
719 ath_chanctx_switch(sc
, ctx
, NULL
);
727 ath_chanctx_send_vif_ps_frame(struct ath_softc
*sc
, struct ath_vif
*avp
,
730 struct ieee80211_vif
*vif
= avp
->vif
;
731 struct ieee80211_sta
*sta
= NULL
;
732 struct ieee80211_hdr_3addr
*nullfunc
;
733 struct ath_tx_control txctl
;
735 int band
= sc
->cur_chan
->chandef
.chan
->band
;
738 case NL80211_IFTYPE_STATION
:
739 if (!vif
->bss_conf
.assoc
)
742 skb
= ieee80211_nullfunc_get(sc
->hw
, vif
);
746 nullfunc
= (struct ieee80211_hdr_3addr
*) skb
->data
;
748 nullfunc
->frame_control
|=
749 cpu_to_le16(IEEE80211_FCTL_PM
);
751 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
752 if (!ieee80211_tx_prepare_skb(sc
->hw
, vif
, skb
, band
, &sta
)) {
753 dev_kfree_skb_any(skb
);
761 memset(&txctl
, 0, sizeof(txctl
));
762 txctl
.txq
= sc
->tx
.txq_map
[IEEE80211_AC_VO
];
764 txctl
.force_channel
= true;
765 if (ath_tx_start(sc
->hw
, skb
, &txctl
)) {
766 ieee80211_free_txskb(sc
->hw
, skb
);
774 ath_chanctx_send_ps_frame(struct ath_softc
*sc
, bool powersave
)
780 list_for_each_entry(avp
, &sc
->cur_chan
->vifs
, list
) {
781 if (ath_chanctx_send_vif_ps_frame(sc
, avp
, powersave
))
789 static bool ath_chanctx_defer_switch(struct ath_softc
*sc
)
791 if (sc
->cur_chan
== &sc
->offchannel
.chan
)
794 switch (sc
->sched
.state
) {
795 case ATH_CHANCTX_STATE_SWITCH
:
797 case ATH_CHANCTX_STATE_IDLE
:
798 if (!sc
->cur_chan
->switch_after_beacon
)
801 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
810 static void ath_offchannel_channel_change(struct ath_softc
*sc
)
812 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
814 ath_dbg(common
, CHAN_CTX
, "%s: state: %s\n",
815 __func__
, offchannel_state_string(sc
->offchannel
.state
));
817 switch (sc
->offchannel
.state
) {
818 case ATH_OFFCHANNEL_PROBE_SEND
:
819 if (!sc
->offchannel
.scan_req
)
822 if (sc
->cur_chan
->chandef
.chan
!=
823 sc
->offchannel
.chan
.chandef
.chan
)
826 ath_scan_channel_start(sc
);
828 case ATH_OFFCHANNEL_IDLE
:
829 if (!sc
->offchannel
.scan_req
)
832 ath_scan_complete(sc
, false);
834 case ATH_OFFCHANNEL_ROC_START
:
835 if (sc
->cur_chan
!= &sc
->offchannel
.chan
)
838 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_WAIT
;
839 mod_timer(&sc
->offchannel
.timer
, jiffies
+
840 msecs_to_jiffies(sc
->offchannel
.duration
));
841 ieee80211_ready_on_channel(sc
->hw
);
843 case ATH_OFFCHANNEL_ROC_DONE
:
844 ath_roc_complete(sc
, false);
851 void ath_chanctx_set_next(struct ath_softc
*sc
, bool force
)
854 bool measure_time
= false;
855 bool send_ps
= false;
857 spin_lock_bh(&sc
->chan_lock
);
858 if (!sc
->next_chan
) {
859 spin_unlock_bh(&sc
->chan_lock
);
863 if (!force
&& ath_chanctx_defer_switch(sc
)) {
864 spin_unlock_bh(&sc
->chan_lock
);
868 if (sc
->cur_chan
!= sc
->next_chan
) {
869 sc
->cur_chan
->stopped
= true;
870 spin_unlock_bh(&sc
->chan_lock
);
872 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
873 getrawmonotonic(&ts
);
876 __ath9k_flush(sc
->hw
, ~0, true);
878 if (ath_chanctx_send_ps_frame(sc
, true))
879 __ath9k_flush(sc
->hw
, BIT(IEEE80211_AC_VO
), false);
882 spin_lock_bh(&sc
->chan_lock
);
884 if (sc
->cur_chan
!= &sc
->offchannel
.chan
) {
885 getrawmonotonic(&sc
->cur_chan
->tsf_ts
);
886 sc
->cur_chan
->tsf_val
= ath9k_hw_gettsf64(sc
->sc_ah
);
889 sc
->cur_chan
= sc
->next_chan
;
890 sc
->cur_chan
->stopped
= false;
891 sc
->next_chan
= NULL
;
892 sc
->sched
.offchannel_duration
= 0;
893 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_FORCE_ACTIVE
)
894 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
896 spin_unlock_bh(&sc
->chan_lock
);
898 if (sc
->sc_ah
->chip_fullsleep
||
899 memcmp(&sc
->cur_chandef
, &sc
->cur_chan
->chandef
,
900 sizeof(sc
->cur_chandef
))) {
903 sc
->sched
.channel_switch_time
=
904 ath9k_hw_get_tsf_offset(&ts
, NULL
);
907 ath_chanctx_send_ps_frame(sc
, false);
909 ath_offchannel_channel_change(sc
);
910 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_SWITCH
);
913 static void ath_chanctx_work(struct work_struct
*work
)
915 struct ath_softc
*sc
= container_of(work
, struct ath_softc
,
917 mutex_lock(&sc
->mutex
);
918 ath_chanctx_set_next(sc
, false);
919 mutex_unlock(&sc
->mutex
);
922 void ath9k_init_channel_context(struct ath_softc
*sc
)
924 INIT_WORK(&sc
->chanctx_work
, ath_chanctx_work
);
926 setup_timer(&sc
->offchannel
.timer
, ath_offchannel_timer
,
928 setup_timer(&sc
->sched
.timer
, ath_chanctx_timer
,
932 void ath9k_deinit_channel_context(struct ath_softc
*sc
)
934 cancel_work_sync(&sc
->chanctx_work
);
937 bool ath9k_is_chanctx_enabled(void)
939 return (ath9k_use_chanctx
== 1);
946 static void ath9k_update_p2p_ps_timer(struct ath_softc
*sc
, struct ath_vif
*avp
)
948 struct ath_hw
*ah
= sc
->sc_ah
;
951 if (!avp
|| !avp
->noa
.has_next_tsf
)
954 ath9k_hw_gen_timer_stop(ah
, sc
->p2p_ps_timer
);
956 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
958 target_tsf
= avp
->noa
.next_tsf
;
959 if (!avp
->noa
.absent
)
960 target_tsf
-= ATH_P2P_PS_STOP_TIME
;
962 if (target_tsf
- tsf
< ATH_P2P_PS_STOP_TIME
)
963 target_tsf
= tsf
+ ATH_P2P_PS_STOP_TIME
;
965 ath9k_hw_gen_timer_start(ah
, sc
->p2p_ps_timer
, (u32
) target_tsf
, 1000000);
968 static void ath9k_update_p2p_ps(struct ath_softc
*sc
, struct ieee80211_vif
*vif
)
970 struct ath_vif
*avp
= (void *)vif
->drv_priv
;
973 if (!sc
->p2p_ps_timer
)
976 if (vif
->type
!= NL80211_IFTYPE_STATION
|| !vif
->p2p
)
979 sc
->p2p_ps_vif
= avp
;
980 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
981 ieee80211_parse_p2p_noa(&vif
->bss_conf
.p2p_noa_attr
, &avp
->noa
, tsf
);
982 ath9k_update_p2p_ps_timer(sc
, avp
);
985 void ath9k_p2p_ps_timer(void *priv
)
987 struct ath_softc
*sc
= priv
;
988 struct ath_vif
*avp
= sc
->p2p_ps_vif
;
989 struct ieee80211_vif
*vif
;
990 struct ieee80211_sta
*sta
;
994 del_timer_sync(&sc
->sched
.timer
);
995 ath9k_hw_gen_timer_stop(sc
->sc_ah
, sc
->p2p_ps_timer
);
996 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_TSF_TIMER
);
998 if (!avp
|| avp
->chanctx
!= sc
->cur_chan
)
1001 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1002 if (!avp
->noa
.absent
)
1003 tsf
+= ATH_P2P_PS_STOP_TIME
;
1005 if (!avp
->noa
.has_next_tsf
||
1006 avp
->noa
.next_tsf
- tsf
> BIT(31))
1007 ieee80211_update_p2p_noa(&avp
->noa
, tsf
);
1009 ath9k_update_p2p_ps_timer(sc
, avp
);
1014 sta
= ieee80211_find_sta(vif
, vif
->bss_conf
.bssid
);
1018 an
= (void *) sta
->drv_priv
;
1019 if (an
->sleeping
== !!avp
->noa
.absent
)
1022 an
->sleeping
= avp
->noa
.absent
;
1024 ath_tx_aggr_sleep(sta
, sc
, an
);
1026 ath_tx_aggr_wakeup(sc
, an
);
1032 void ath9k_p2p_bss_info_changed(struct ath_softc
*sc
,
1033 struct ieee80211_vif
*vif
)
1035 unsigned long flags
;
1037 spin_lock_bh(&sc
->sc_pcu_lock
);
1038 spin_lock_irqsave(&sc
->sc_pm_lock
, flags
);
1039 if (!(sc
->ps_flags
& PS_BEACON_SYNC
))
1040 ath9k_update_p2p_ps(sc
, vif
);
1041 spin_unlock_irqrestore(&sc
->sc_pm_lock
, flags
);
1042 spin_unlock_bh(&sc
->sc_pcu_lock
);
1045 void ath9k_p2p_beacon_sync(struct ath_softc
*sc
)
1048 ath9k_update_p2p_ps(sc
, sc
->p2p_ps_vif
->vif
);
1051 void ath9k_p2p_remove_vif(struct ath_softc
*sc
,
1052 struct ieee80211_vif
*vif
)
1054 struct ath_vif
*avp
= (void *)vif
->drv_priv
;
1056 spin_lock_bh(&sc
->sc_pcu_lock
);
1057 if (avp
== sc
->p2p_ps_vif
) {
1058 sc
->p2p_ps_vif
= NULL
;
1059 ath9k_update_p2p_ps_timer(sc
, NULL
);
1061 spin_unlock_bh(&sc
->sc_pcu_lock
);
1064 int ath9k_init_p2p(struct ath_softc
*sc
)
1066 sc
->p2p_ps_timer
= ath_gen_timer_alloc(sc
->sc_ah
, ath9k_p2p_ps_timer
,
1067 NULL
, sc
, AR_FIRST_NDP_TIMER
);
1068 if (!sc
->p2p_ps_timer
)
1074 void ath9k_deinit_p2p(struct ath_softc
*sc
)
1076 if (sc
->p2p_ps_timer
)
1077 ath_gen_timer_free(sc
->sc_ah
, sc
->p2p_ps_timer
);
1080 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */