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_init(struct ath_softc
*sc
)
106 struct ath_chanctx
*ctx
;
107 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
108 struct ieee80211_supported_band
*sband
;
109 struct ieee80211_channel
*chan
;
112 sband
= &common
->sbands
[IEEE80211_BAND_2GHZ
];
113 if (!sband
->n_channels
)
114 sband
= &common
->sbands
[IEEE80211_BAND_5GHZ
];
116 chan
= &sband
->channels
[0];
117 for (i
= 0; i
< ATH9K_NUM_CHANCTX
; i
++) {
118 ctx
= &sc
->chanctx
[i
];
119 cfg80211_chandef_create(&ctx
->chandef
, chan
, NL80211_CHAN_HT20
);
120 INIT_LIST_HEAD(&ctx
->vifs
);
121 ctx
->txpower
= ATH_TXPOWER_MAX
;
122 for (j
= 0; j
< ARRAY_SIZE(ctx
->acq
); j
++)
123 INIT_LIST_HEAD(&ctx
->acq
[j
]);
127 void ath_chanctx_set_channel(struct ath_softc
*sc
, struct ath_chanctx
*ctx
,
128 struct cfg80211_chan_def
*chandef
)
132 spin_lock_bh(&sc
->chan_lock
);
134 memcpy(&ctx
->chandef
, chandef
, sizeof(*chandef
));
135 cur_chan
= sc
->cur_chan
== ctx
;
136 spin_unlock_bh(&sc
->chan_lock
);
144 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
146 /**********************************************************/
147 /* Functions to handle the channel context state machine. */
148 /**********************************************************/
150 static const char *offchannel_state_string(enum ath_offchannel_state state
)
152 #define case_rtn_string(val) case val: return #val
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
);
167 void ath_chanctx_check_active(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
169 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
177 list_for_each_entry(avp
, &ctx
->vifs
, list
) {
178 struct ieee80211_vif
*vif
= avp
->vif
;
181 case NL80211_IFTYPE_P2P_CLIENT
:
182 case NL80211_IFTYPE_STATION
:
183 if (vif
->bss_conf
.assoc
)
191 ctx
->active
= active
;
193 ath_for_each_chanctx(sc
, ctx
) {
194 if (!ctx
->assigned
|| list_empty(&ctx
->vifs
))
200 clear_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
);
203 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
))
206 if (ath9k_is_chanctx_enabled()) {
207 ath_chanctx_event(sc
, NULL
,
208 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
);
212 static struct ath_chanctx
*
213 ath_chanctx_get_next(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
215 int idx
= ctx
- &sc
->chanctx
[0];
217 return &sc
->chanctx
[!idx
];
220 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc
*sc
)
222 struct ath_chanctx
*prev
, *cur
;
224 u32 cur_tsf
, prev_tsf
, beacon_int
;
227 beacon_int
= TU_TO_USEC(sc
->cur_chan
->beacon
.beacon_interval
);
230 prev
= ath_chanctx_get_next(sc
, cur
);
232 getrawmonotonic(&ts
);
233 cur_tsf
= (u32
) cur
->tsf_val
+
234 ath9k_hw_get_tsf_offset(&cur
->tsf_ts
, &ts
);
236 prev_tsf
= prev
->last_beacon
- (u32
) prev
->tsf_val
+ cur_tsf
;
237 prev_tsf
-= ath9k_hw_get_tsf_offset(&prev
->tsf_ts
, &ts
);
239 /* Adjust the TSF time of the AP chanctx to keep its beacons
240 * at half beacon interval offset relative to the STA chanctx.
242 offset
= cur_tsf
- prev_tsf
;
244 /* Ignore stale data or spurious timestamps */
245 if (offset
< 0 || offset
> 3 * beacon_int
)
248 offset
= beacon_int
/ 2 - (offset
% beacon_int
);
249 prev
->tsf_val
+= offset
;
252 /* Configure the TSF based hardware timer for a channel switch.
253 * Also set up backup software timer, in case the gen timer fails.
254 * This could be caused by a hardware reset.
256 static void ath_chanctx_setup_timer(struct ath_softc
*sc
, u32 tsf_time
)
258 struct ath_hw
*ah
= sc
->sc_ah
;
260 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
261 ath9k_hw_gen_timer_start(ah
, sc
->p2p_ps_timer
, tsf_time
, 1000000);
263 tsf_time
-= ath9k_hw_gettsf32(ah
);
264 tsf_time
= msecs_to_jiffies(tsf_time
/ 1000) + 1;
265 mod_timer(&sc
->sched
.timer
, tsf_time
);
268 void ath_chanctx_event(struct ath_softc
*sc
, struct ieee80211_vif
*vif
,
269 enum ath_chanctx_event ev
)
271 struct ath_hw
*ah
= sc
->sc_ah
;
272 struct ath_common
*common
= ath9k_hw_common(ah
);
273 struct ath_beacon_config
*cur_conf
;
274 struct ath_vif
*avp
= NULL
;
275 struct ath_chanctx
*ctx
;
278 bool noa_changed
= false;
281 avp
= (struct ath_vif
*) vif
->drv_priv
;
283 spin_lock_bh(&sc
->chan_lock
);
286 case ATH_CHANCTX_EVENT_BEACON_PREPARE
:
287 if (avp
->offchannel_duration
)
288 avp
->offchannel_duration
= 0;
290 if (avp
->chanctx
!= sc
->cur_chan
)
293 if (sc
->sched
.offchannel_pending
) {
294 sc
->sched
.offchannel_pending
= false;
295 sc
->next_chan
= &sc
->offchannel
.chan
;
296 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
299 ctx
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
300 if (ctx
->active
&& sc
->sched
.state
== ATH_CHANCTX_STATE_IDLE
) {
302 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
305 /* if the timer missed its window, use the next interval */
306 if (sc
->sched
.state
== ATH_CHANCTX_STATE_WAIT_FOR_TIMER
)
307 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
309 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
)
312 sc
->sched
.beacon_pending
= true;
313 sc
->sched
.next_tbtt
= REG_READ(ah
, AR_NEXT_TBTT_TIMER
);
315 cur_conf
= &sc
->cur_chan
->beacon
;
316 beacon_int
= TU_TO_USEC(cur_conf
->beacon_interval
);
318 /* defer channel switch by a quarter beacon interval */
319 tsf_time
= sc
->sched
.next_tbtt
+ beacon_int
/ 4;
320 sc
->sched
.switch_start_time
= tsf_time
;
321 sc
->cur_chan
->last_beacon
= sc
->sched
.next_tbtt
;
323 /* Prevent wrap-around issues */
324 if (avp
->periodic_noa_duration
&&
325 tsf_time
- avp
->periodic_noa_start
> BIT(30))
326 avp
->periodic_noa_duration
= 0;
328 if (ctx
->active
&& !avp
->periodic_noa_duration
) {
329 avp
->periodic_noa_start
= tsf_time
;
330 avp
->periodic_noa_duration
=
331 TU_TO_USEC(cur_conf
->beacon_interval
) / 2 -
332 sc
->sched
.channel_switch_time
;
334 } else if (!ctx
->active
&& avp
->periodic_noa_duration
) {
335 avp
->periodic_noa_duration
= 0;
339 /* If at least two consecutive beacons were missed on the STA
340 * chanctx, stay on the STA channel for one extra beacon period,
341 * to resync the timer properly.
343 if (ctx
->active
&& sc
->sched
.beacon_miss
>= 2)
344 sc
->sched
.offchannel_duration
= 3 * beacon_int
/ 2;
346 if (sc
->sched
.offchannel_duration
) {
348 avp
->offchannel_start
= tsf_time
;
349 avp
->offchannel_duration
=
350 sc
->sched
.offchannel_duration
;
356 case ATH_CHANCTX_EVENT_BEACON_SENT
:
357 if (!sc
->sched
.beacon_pending
)
360 sc
->sched
.beacon_pending
= false;
361 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
)
364 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
;
365 ath_chanctx_setup_timer(sc
, sc
->sched
.switch_start_time
);
367 case ATH_CHANCTX_EVENT_TSF_TIMER
:
368 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
)
371 if (!sc
->cur_chan
->switch_after_beacon
&&
372 sc
->sched
.beacon_pending
)
373 sc
->sched
.beacon_miss
++;
375 sc
->sched
.state
= ATH_CHANCTX_STATE_SWITCH
;
376 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
378 case ATH_CHANCTX_EVENT_BEACON_RECEIVED
:
379 if (!test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) ||
380 sc
->cur_chan
== &sc
->offchannel
.chan
)
383 ath_chanctx_adjust_tbtt_delta(sc
);
384 sc
->sched
.beacon_pending
= false;
385 sc
->sched
.beacon_miss
= 0;
387 /* TSF time might have been updated by the incoming beacon,
388 * need update the channel switch timer to reflect the change.
390 tsf_time
= sc
->sched
.switch_start_time
;
391 tsf_time
-= (u32
) sc
->cur_chan
->tsf_val
+
392 ath9k_hw_get_tsf_offset(&sc
->cur_chan
->tsf_ts
, NULL
);
393 tsf_time
+= ath9k_hw_gettsf32(ah
);
396 ath_chanctx_setup_timer(sc
, tsf_time
);
398 case ATH_CHANCTX_EVENT_ASSOC
:
399 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_FORCE_ACTIVE
||
400 avp
->chanctx
!= sc
->cur_chan
)
403 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
405 case ATH_CHANCTX_EVENT_SWITCH
:
406 if (!test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) ||
407 sc
->sched
.state
== ATH_CHANCTX_STATE_FORCE_ACTIVE
||
408 sc
->cur_chan
->switch_after_beacon
||
409 sc
->cur_chan
== &sc
->offchannel
.chan
)
412 /* If this is a station chanctx, stay active for a half
413 * beacon period (minus channel switch time)
415 sc
->next_chan
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
416 cur_conf
= &sc
->cur_chan
->beacon
;
418 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
;
420 tsf_time
= TU_TO_USEC(cur_conf
->beacon_interval
) / 2;
421 if (sc
->sched
.beacon_miss
>= 2) {
422 sc
->sched
.beacon_miss
= 0;
426 tsf_time
-= sc
->sched
.channel_switch_time
;
427 tsf_time
+= ath9k_hw_gettsf32(sc
->sc_ah
);
428 sc
->sched
.switch_start_time
= tsf_time
;
430 ath_chanctx_setup_timer(sc
, tsf_time
);
431 sc
->sched
.beacon_pending
= true;
433 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
:
434 if (sc
->cur_chan
== &sc
->offchannel
.chan
||
435 sc
->cur_chan
->switch_after_beacon
)
438 sc
->next_chan
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
439 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
441 case ATH_CHANCTX_EVENT_UNASSIGN
:
442 if (sc
->cur_chan
->assigned
) {
443 if (sc
->next_chan
&& !sc
->next_chan
->assigned
&&
444 sc
->next_chan
!= &sc
->offchannel
.chan
)
445 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
449 ctx
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
450 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
455 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
459 spin_unlock_bh(&sc
->chan_lock
);
462 void ath_chanctx_beacon_sent_ev(struct ath_softc
*sc
,
463 enum ath_chanctx_event ev
)
465 if (sc
->sched
.beacon_pending
)
466 ath_chanctx_event(sc
, NULL
, ev
);
469 void ath_chanctx_beacon_recv_ev(struct ath_softc
*sc
, u32 ts
,
470 enum ath_chanctx_event ev
)
472 sc
->sched
.next_tbtt
= ts
;
473 ath_chanctx_event(sc
, NULL
, ev
);
476 static int ath_scan_channel_duration(struct ath_softc
*sc
,
477 struct ieee80211_channel
*chan
)
479 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
481 if (!req
->n_ssids
|| (chan
->flags
& IEEE80211_CHAN_NO_IR
))
482 return (HZ
/ 9); /* ~110 ms */
484 return (HZ
/ 16); /* ~60 ms */
487 static void ath_chanctx_switch(struct ath_softc
*sc
, struct ath_chanctx
*ctx
,
488 struct cfg80211_chan_def
*chandef
)
490 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
492 spin_lock_bh(&sc
->chan_lock
);
494 if (test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) &&
495 (sc
->cur_chan
!= ctx
) && (ctx
== &sc
->offchannel
.chan
)) {
496 sc
->sched
.offchannel_pending
= true;
497 spin_unlock_bh(&sc
->chan_lock
);
503 ctx
->chandef
= *chandef
;
504 ath_dbg(common
, CHAN_CTX
,
505 "Assigned next_chan to %d MHz\n", chandef
->center_freq1
);
508 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
509 sc
->sched
.offchannel_duration
=
510 TU_TO_USEC(sc
->offchannel
.duration
) +
511 sc
->sched
.channel_switch_time
;
514 ath_dbg(common
, CHAN_CTX
,
515 "Offchannel duration for chan %d MHz : %u\n",
516 chandef
->center_freq1
,
517 sc
->sched
.offchannel_duration
);
520 spin_unlock_bh(&sc
->chan_lock
);
521 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
524 static void ath_chanctx_offchan_switch(struct ath_softc
*sc
,
525 struct ieee80211_channel
*chan
)
527 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
528 struct cfg80211_chan_def chandef
;
530 cfg80211_chandef_create(&chandef
, chan
, NL80211_CHAN_NO_HT
);
531 ath_dbg(common
, CHAN_CTX
,
532 "Channel definition created: %d MHz\n", chandef
.center_freq1
);
534 ath_chanctx_switch(sc
, &sc
->offchannel
.chan
, &chandef
);
537 static struct ath_chanctx
*ath_chanctx_get_oper_chan(struct ath_softc
*sc
,
540 struct ath_chanctx
*ctx
;
542 ath_for_each_chanctx(sc
, ctx
) {
543 if (!ctx
->assigned
|| list_empty(&ctx
->vifs
))
545 if (active
&& !ctx
->active
)
548 if (ctx
->switch_after_beacon
)
552 return &sc
->chanctx
[0];
556 ath_scan_next_channel(struct ath_softc
*sc
)
558 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
559 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
560 struct ieee80211_channel
*chan
;
562 if (sc
->offchannel
.scan_idx
>= req
->n_channels
) {
563 ath_dbg(common
, CHAN_CTX
,
564 "Moving to ATH_OFFCHANNEL_IDLE state, scan_idx: %d, n_channels: %d\n",
565 sc
->offchannel
.scan_idx
,
568 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
569 ath_chanctx_switch(sc
, ath_chanctx_get_oper_chan(sc
, false),
574 ath_dbg(common
, CHAN_CTX
,
575 "Moving to ATH_OFFCHANNEL_PROBE_SEND state, scan_idx: %d\n",
576 sc
->offchannel
.scan_idx
);
578 chan
= req
->channels
[sc
->offchannel
.scan_idx
++];
579 sc
->offchannel
.duration
= ath_scan_channel_duration(sc
, chan
);
580 sc
->offchannel
.state
= ATH_OFFCHANNEL_PROBE_SEND
;
582 ath_chanctx_offchan_switch(sc
, chan
);
585 void ath_offchannel_next(struct ath_softc
*sc
)
587 struct ieee80211_vif
*vif
;
589 if (sc
->offchannel
.scan_req
) {
590 vif
= sc
->offchannel
.scan_vif
;
591 sc
->offchannel
.chan
.txpower
= vif
->bss_conf
.txpower
;
592 ath_scan_next_channel(sc
);
593 } else if (sc
->offchannel
.roc_vif
) {
594 vif
= sc
->offchannel
.roc_vif
;
595 sc
->offchannel
.chan
.txpower
= vif
->bss_conf
.txpower
;
596 sc
->offchannel
.duration
= sc
->offchannel
.roc_duration
;
597 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_START
;
598 ath_chanctx_offchan_switch(sc
, sc
->offchannel
.roc_chan
);
600 ath_chanctx_switch(sc
, ath_chanctx_get_oper_chan(sc
, false),
602 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
608 void ath_roc_complete(struct ath_softc
*sc
, bool abort
)
610 sc
->offchannel
.roc_vif
= NULL
;
611 sc
->offchannel
.roc_chan
= NULL
;
613 ieee80211_remain_on_channel_expired(sc
->hw
);
614 ath_offchannel_next(sc
);
615 ath9k_ps_restore(sc
);
618 void ath_scan_complete(struct ath_softc
*sc
, bool abort
)
620 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
623 ath_dbg(common
, CHAN_CTX
, "HW scan aborted\n");
625 ath_dbg(common
, CHAN_CTX
, "HW scan complete\n");
627 sc
->offchannel
.scan_req
= NULL
;
628 sc
->offchannel
.scan_vif
= NULL
;
629 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
630 ieee80211_scan_completed(sc
->hw
, abort
);
631 clear_bit(ATH_OP_SCANNING
, &common
->op_flags
);
632 ath_offchannel_next(sc
);
633 ath9k_ps_restore(sc
);
636 static void ath_scan_send_probe(struct ath_softc
*sc
,
637 struct cfg80211_ssid
*ssid
)
639 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
640 struct ieee80211_vif
*vif
= sc
->offchannel
.scan_vif
;
641 struct ath_tx_control txctl
= {};
643 struct ieee80211_tx_info
*info
;
644 int band
= sc
->offchannel
.chan
.chandef
.chan
->band
;
646 skb
= ieee80211_probereq_get(sc
->hw
, vif
,
647 ssid
->ssid
, ssid
->ssid_len
, req
->ie_len
);
651 info
= IEEE80211_SKB_CB(skb
);
653 info
->flags
|= IEEE80211_TX_CTL_NO_CCK_RATE
;
656 memcpy(skb_put(skb
, req
->ie_len
), req
->ie
, req
->ie_len
);
658 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
660 if (!ieee80211_tx_prepare_skb(sc
->hw
, vif
, skb
, band
, NULL
))
663 txctl
.txq
= sc
->tx
.txq_map
[IEEE80211_AC_VO
];
664 txctl
.force_channel
= true;
665 if (ath_tx_start(sc
->hw
, skb
, &txctl
))
671 ieee80211_free_txskb(sc
->hw
, skb
);
674 static void ath_scan_channel_start(struct ath_softc
*sc
)
676 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
677 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
680 if (!(sc
->cur_chan
->chandef
.chan
->flags
& IEEE80211_CHAN_NO_IR
) &&
682 for (i
= 0; i
< req
->n_ssids
; i
++)
683 ath_scan_send_probe(sc
, &req
->ssids
[i
]);
687 ath_dbg(common
, CHAN_CTX
,
688 "Moving to ATH_OFFCHANNEL_PROBE_WAIT state\n");
690 sc
->offchannel
.state
= ATH_OFFCHANNEL_PROBE_WAIT
;
691 mod_timer(&sc
->offchannel
.timer
, jiffies
+ sc
->offchannel
.duration
);
694 static void ath_chanctx_timer(unsigned long data
)
696 struct ath_softc
*sc
= (struct ath_softc
*) data
;
698 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_TSF_TIMER
);
701 static void ath_offchannel_timer(unsigned long data
)
703 struct ath_softc
*sc
= (struct ath_softc
*)data
;
704 struct ath_chanctx
*ctx
;
705 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
707 ath_dbg(common
, CHAN_CTX
, "%s: state: %s\n",
708 __func__
, offchannel_state_string(sc
->offchannel
.state
));
710 switch (sc
->offchannel
.state
) {
711 case ATH_OFFCHANNEL_PROBE_WAIT
:
712 if (!sc
->offchannel
.scan_req
)
715 /* get first active channel context */
716 ctx
= ath_chanctx_get_oper_chan(sc
, true);
718 sc
->offchannel
.state
= ATH_OFFCHANNEL_SUSPEND
;
719 ath_chanctx_switch(sc
, ctx
, NULL
);
720 mod_timer(&sc
->offchannel
.timer
, jiffies
+ HZ
/ 10);
724 case ATH_OFFCHANNEL_SUSPEND
:
725 if (!sc
->offchannel
.scan_req
)
728 ath_scan_next_channel(sc
);
730 case ATH_OFFCHANNEL_ROC_START
:
731 case ATH_OFFCHANNEL_ROC_WAIT
:
732 ctx
= ath_chanctx_get_oper_chan(sc
, false);
733 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_DONE
;
734 ath_chanctx_switch(sc
, ctx
, NULL
);
742 ath_chanctx_send_vif_ps_frame(struct ath_softc
*sc
, struct ath_vif
*avp
,
745 struct ieee80211_vif
*vif
= avp
->vif
;
746 struct ieee80211_sta
*sta
= NULL
;
747 struct ieee80211_hdr_3addr
*nullfunc
;
748 struct ath_tx_control txctl
;
750 int band
= sc
->cur_chan
->chandef
.chan
->band
;
753 case NL80211_IFTYPE_STATION
:
754 if (!vif
->bss_conf
.assoc
)
757 skb
= ieee80211_nullfunc_get(sc
->hw
, vif
);
761 nullfunc
= (struct ieee80211_hdr_3addr
*) skb
->data
;
763 nullfunc
->frame_control
|=
764 cpu_to_le16(IEEE80211_FCTL_PM
);
766 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
767 if (!ieee80211_tx_prepare_skb(sc
->hw
, vif
, skb
, band
, &sta
)) {
768 dev_kfree_skb_any(skb
);
776 memset(&txctl
, 0, sizeof(txctl
));
777 txctl
.txq
= sc
->tx
.txq_map
[IEEE80211_AC_VO
];
779 txctl
.force_channel
= true;
780 if (ath_tx_start(sc
->hw
, skb
, &txctl
)) {
781 ieee80211_free_txskb(sc
->hw
, skb
);
789 ath_chanctx_send_ps_frame(struct ath_softc
*sc
, bool powersave
)
795 list_for_each_entry(avp
, &sc
->cur_chan
->vifs
, list
) {
796 if (ath_chanctx_send_vif_ps_frame(sc
, avp
, powersave
))
804 static bool ath_chanctx_defer_switch(struct ath_softc
*sc
)
806 if (sc
->cur_chan
== &sc
->offchannel
.chan
)
809 switch (sc
->sched
.state
) {
810 case ATH_CHANCTX_STATE_SWITCH
:
812 case ATH_CHANCTX_STATE_IDLE
:
813 if (!sc
->cur_chan
->switch_after_beacon
)
816 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
825 static void ath_offchannel_channel_change(struct ath_softc
*sc
)
827 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
829 ath_dbg(common
, CHAN_CTX
, "%s: state: %s\n",
830 __func__
, offchannel_state_string(sc
->offchannel
.state
));
832 switch (sc
->offchannel
.state
) {
833 case ATH_OFFCHANNEL_PROBE_SEND
:
834 if (!sc
->offchannel
.scan_req
)
837 if (sc
->cur_chan
->chandef
.chan
!=
838 sc
->offchannel
.chan
.chandef
.chan
)
841 ath_scan_channel_start(sc
);
843 case ATH_OFFCHANNEL_IDLE
:
844 if (!sc
->offchannel
.scan_req
)
847 ath_scan_complete(sc
, false);
849 case ATH_OFFCHANNEL_ROC_START
:
850 if (sc
->cur_chan
!= &sc
->offchannel
.chan
)
853 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_WAIT
;
854 mod_timer(&sc
->offchannel
.timer
, jiffies
+
855 msecs_to_jiffies(sc
->offchannel
.duration
));
856 ieee80211_ready_on_channel(sc
->hw
);
858 case ATH_OFFCHANNEL_ROC_DONE
:
859 ath_roc_complete(sc
, false);
866 void ath_chanctx_set_next(struct ath_softc
*sc
, bool force
)
869 bool measure_time
= false;
870 bool send_ps
= false;
872 spin_lock_bh(&sc
->chan_lock
);
873 if (!sc
->next_chan
) {
874 spin_unlock_bh(&sc
->chan_lock
);
878 if (!force
&& ath_chanctx_defer_switch(sc
)) {
879 spin_unlock_bh(&sc
->chan_lock
);
883 if (sc
->cur_chan
!= sc
->next_chan
) {
884 sc
->cur_chan
->stopped
= true;
885 spin_unlock_bh(&sc
->chan_lock
);
887 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
888 getrawmonotonic(&ts
);
891 __ath9k_flush(sc
->hw
, ~0, true);
893 if (ath_chanctx_send_ps_frame(sc
, true))
894 __ath9k_flush(sc
->hw
, BIT(IEEE80211_AC_VO
), false);
897 spin_lock_bh(&sc
->chan_lock
);
899 if (sc
->cur_chan
!= &sc
->offchannel
.chan
) {
900 getrawmonotonic(&sc
->cur_chan
->tsf_ts
);
901 sc
->cur_chan
->tsf_val
= ath9k_hw_gettsf64(sc
->sc_ah
);
904 sc
->cur_chan
= sc
->next_chan
;
905 sc
->cur_chan
->stopped
= false;
906 sc
->next_chan
= NULL
;
907 sc
->sched
.offchannel_duration
= 0;
908 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_FORCE_ACTIVE
)
909 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
911 spin_unlock_bh(&sc
->chan_lock
);
913 if (sc
->sc_ah
->chip_fullsleep
||
914 memcmp(&sc
->cur_chandef
, &sc
->cur_chan
->chandef
,
915 sizeof(sc
->cur_chandef
))) {
918 sc
->sched
.channel_switch_time
=
919 ath9k_hw_get_tsf_offset(&ts
, NULL
);
922 ath_chanctx_send_ps_frame(sc
, false);
924 ath_offchannel_channel_change(sc
);
925 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_SWITCH
);
928 static void ath_chanctx_work(struct work_struct
*work
)
930 struct ath_softc
*sc
= container_of(work
, struct ath_softc
,
932 mutex_lock(&sc
->mutex
);
933 ath_chanctx_set_next(sc
, false);
934 mutex_unlock(&sc
->mutex
);
937 void ath9k_offchannel_init(struct ath_softc
*sc
)
939 struct ath_chanctx
*ctx
;
940 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
941 struct ieee80211_supported_band
*sband
;
942 struct ieee80211_channel
*chan
;
945 sband
= &common
->sbands
[IEEE80211_BAND_2GHZ
];
946 if (!sband
->n_channels
)
947 sband
= &common
->sbands
[IEEE80211_BAND_5GHZ
];
949 chan
= &sband
->channels
[0];
951 ctx
= &sc
->offchannel
.chan
;
952 INIT_LIST_HEAD(&ctx
->vifs
);
953 ctx
->txpower
= ATH_TXPOWER_MAX
;
954 cfg80211_chandef_create(&ctx
->chandef
, chan
, NL80211_CHAN_HT20
);
956 for (i
= 0; i
< ARRAY_SIZE(ctx
->acq
); i
++)
957 INIT_LIST_HEAD(&ctx
->acq
[i
]);
959 sc
->offchannel
.chan
.offchannel
= true;
962 void ath9k_init_channel_context(struct ath_softc
*sc
)
964 INIT_WORK(&sc
->chanctx_work
, ath_chanctx_work
);
966 setup_timer(&sc
->offchannel
.timer
, ath_offchannel_timer
,
968 setup_timer(&sc
->sched
.timer
, ath_chanctx_timer
,
972 void ath9k_deinit_channel_context(struct ath_softc
*sc
)
974 cancel_work_sync(&sc
->chanctx_work
);
977 bool ath9k_is_chanctx_enabled(void)
979 return (ath9k_use_chanctx
== 1);
982 /********************/
983 /* Queue management */
984 /********************/
986 void ath9k_chanctx_wake_queues(struct ath_softc
*sc
)
988 struct ath_hw
*ah
= sc
->sc_ah
;
991 if (sc
->cur_chan
== &sc
->offchannel
.chan
) {
992 ieee80211_wake_queue(sc
->hw
,
993 sc
->hw
->offchannel_tx_hw_queue
);
995 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++)
996 ieee80211_wake_queue(sc
->hw
,
997 sc
->cur_chan
->hw_queue_base
+ i
);
1000 if (ah
->opmode
== NL80211_IFTYPE_AP
)
1001 ieee80211_wake_queue(sc
->hw
, sc
->hw
->queues
- 2);
1008 static void ath9k_update_p2p_ps_timer(struct ath_softc
*sc
, struct ath_vif
*avp
)
1010 struct ath_hw
*ah
= sc
->sc_ah
;
1011 s32 tsf
, target_tsf
;
1013 if (!avp
|| !avp
->noa
.has_next_tsf
)
1016 ath9k_hw_gen_timer_stop(ah
, sc
->p2p_ps_timer
);
1018 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1020 target_tsf
= avp
->noa
.next_tsf
;
1021 if (!avp
->noa
.absent
)
1022 target_tsf
-= ATH_P2P_PS_STOP_TIME
;
1024 if (target_tsf
- tsf
< ATH_P2P_PS_STOP_TIME
)
1025 target_tsf
= tsf
+ ATH_P2P_PS_STOP_TIME
;
1027 ath9k_hw_gen_timer_start(ah
, sc
->p2p_ps_timer
, (u32
) target_tsf
, 1000000);
1030 static void ath9k_update_p2p_ps(struct ath_softc
*sc
, struct ieee80211_vif
*vif
)
1032 struct ath_vif
*avp
= (void *)vif
->drv_priv
;
1035 if (!sc
->p2p_ps_timer
)
1038 if (vif
->type
!= NL80211_IFTYPE_STATION
|| !vif
->p2p
)
1041 sc
->p2p_ps_vif
= avp
;
1042 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1043 ieee80211_parse_p2p_noa(&vif
->bss_conf
.p2p_noa_attr
, &avp
->noa
, tsf
);
1044 ath9k_update_p2p_ps_timer(sc
, avp
);
1047 void ath9k_p2p_ps_timer(void *priv
)
1049 struct ath_softc
*sc
= priv
;
1050 struct ath_vif
*avp
= sc
->p2p_ps_vif
;
1051 struct ieee80211_vif
*vif
;
1052 struct ieee80211_sta
*sta
;
1053 struct ath_node
*an
;
1056 del_timer_sync(&sc
->sched
.timer
);
1057 ath9k_hw_gen_timer_stop(sc
->sc_ah
, sc
->p2p_ps_timer
);
1058 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_TSF_TIMER
);
1060 if (!avp
|| avp
->chanctx
!= sc
->cur_chan
)
1063 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1064 if (!avp
->noa
.absent
)
1065 tsf
+= ATH_P2P_PS_STOP_TIME
;
1067 if (!avp
->noa
.has_next_tsf
||
1068 avp
->noa
.next_tsf
- tsf
> BIT(31))
1069 ieee80211_update_p2p_noa(&avp
->noa
, tsf
);
1071 ath9k_update_p2p_ps_timer(sc
, avp
);
1076 sta
= ieee80211_find_sta(vif
, vif
->bss_conf
.bssid
);
1080 an
= (void *) sta
->drv_priv
;
1081 if (an
->sleeping
== !!avp
->noa
.absent
)
1084 an
->sleeping
= avp
->noa
.absent
;
1086 ath_tx_aggr_sleep(sta
, sc
, an
);
1088 ath_tx_aggr_wakeup(sc
, an
);
1094 void ath9k_p2p_bss_info_changed(struct ath_softc
*sc
,
1095 struct ieee80211_vif
*vif
)
1097 unsigned long flags
;
1099 spin_lock_bh(&sc
->sc_pcu_lock
);
1100 spin_lock_irqsave(&sc
->sc_pm_lock
, flags
);
1101 if (!(sc
->ps_flags
& PS_BEACON_SYNC
))
1102 ath9k_update_p2p_ps(sc
, vif
);
1103 spin_unlock_irqrestore(&sc
->sc_pm_lock
, flags
);
1104 spin_unlock_bh(&sc
->sc_pcu_lock
);
1107 void ath9k_p2p_beacon_sync(struct ath_softc
*sc
)
1110 ath9k_update_p2p_ps(sc
, sc
->p2p_ps_vif
->vif
);
1113 void ath9k_p2p_remove_vif(struct ath_softc
*sc
,
1114 struct ieee80211_vif
*vif
)
1116 struct ath_vif
*avp
= (void *)vif
->drv_priv
;
1118 spin_lock_bh(&sc
->sc_pcu_lock
);
1119 if (avp
== sc
->p2p_ps_vif
) {
1120 sc
->p2p_ps_vif
= NULL
;
1121 ath9k_update_p2p_ps_timer(sc
, NULL
);
1123 spin_unlock_bh(&sc
->sc_pcu_lock
);
1126 int ath9k_init_p2p(struct ath_softc
*sc
)
1128 sc
->p2p_ps_timer
= ath_gen_timer_alloc(sc
->sc_ah
, ath9k_p2p_ps_timer
,
1129 NULL
, sc
, AR_FIRST_NDP_TIMER
);
1130 if (!sc
->p2p_ps_timer
)
1136 void ath9k_deinit_p2p(struct ath_softc
*sc
)
1138 if (sc
->p2p_ps_timer
)
1139 ath_gen_timer_free(sc
->sc_ah
, sc
->p2p_ps_timer
);
1142 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */