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(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 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",
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
);
102 void ath_chanctx_init(struct ath_softc
*sc
)
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
;
110 sband
= &common
->sbands
[IEEE80211_BAND_2GHZ
];
111 if (!sband
->n_channels
)
112 sband
= &common
->sbands
[IEEE80211_BAND_5GHZ
];
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
);
119 ctx
->txpower
= ATH_TXPOWER_MAX
;
120 ctx
->flush_timeout
= HZ
/ 5; /* 200ms */
121 for (j
= 0; j
< ARRAY_SIZE(ctx
->acq
); j
++)
122 INIT_LIST_HEAD(&ctx
->acq
[j
]);
126 void ath_chanctx_set_channel(struct ath_softc
*sc
, struct ath_chanctx
*ctx
,
127 struct cfg80211_chan_def
*chandef
)
129 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
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
);
139 ath_dbg(common
, CHAN_CTX
,
140 "Current context differs from the new context\n");
147 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
153 struct ath_chanctx
* ath_is_go_chanctx_present(struct ath_softc
*sc
)
155 struct ath_chanctx
*ctx
;
157 struct ieee80211_vif
*vif
;
159 spin_lock_bh(&sc
->chan_lock
);
161 ath_for_each_chanctx(sc
, ctx
) {
165 list_for_each_entry(avp
, &ctx
->vifs
, list
) {
168 if (ieee80211_vif_type_p2p(vif
) == NL80211_IFTYPE_P2P_GO
) {
169 spin_unlock_bh(&sc
->chan_lock
);
175 spin_unlock_bh(&sc
->chan_lock
);
179 /**********************************************************/
180 /* Functions to handle the channel context state machine. */
181 /**********************************************************/
183 static const char *offchannel_state_string(enum ath_offchannel_state state
)
186 case_rtn_string(ATH_OFFCHANNEL_IDLE
);
187 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND
);
188 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT
);
189 case_rtn_string(ATH_OFFCHANNEL_SUSPEND
);
190 case_rtn_string(ATH_OFFCHANNEL_ROC_START
);
191 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT
);
192 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE
);
198 static const char *chanctx_event_string(enum ath_chanctx_event ev
)
201 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE
);
202 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT
);
203 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER
);
204 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED
);
205 case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED
);
206 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH
);
207 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN
);
208 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN
);
209 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE
);
210 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
);
216 static const char *chanctx_state_string(enum ath_chanctx_state state
)
219 case_rtn_string(ATH_CHANCTX_STATE_IDLE
);
220 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON
);
221 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER
);
222 case_rtn_string(ATH_CHANCTX_STATE_SWITCH
);
223 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE
);
229 void ath_chanctx_check_active(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
231 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
232 struct ath_chanctx
*ictx
;
240 if (ctx
== &sc
->offchannel
.chan
) {
241 spin_lock_bh(&sc
->chan_lock
);
243 if (likely(sc
->sched
.channel_switch_time
))
245 usecs_to_jiffies(sc
->sched
.channel_switch_time
);
248 msecs_to_jiffies(10);
250 spin_unlock_bh(&sc
->chan_lock
);
253 * There is no need to iterate over the
254 * active/assigned channel contexts if
255 * the current context is offchannel.
262 list_for_each_entry(avp
, &ctx
->vifs
, list
) {
263 struct ieee80211_vif
*vif
= avp
->vif
;
266 case NL80211_IFTYPE_P2P_CLIENT
:
267 case NL80211_IFTYPE_STATION
:
276 ctx
->active
= active
;
278 ath_for_each_chanctx(sc
, ctx
) {
279 if (!ctx
->assigned
|| list_empty(&ctx
->vifs
))
284 spin_lock_bh(&sc
->chan_lock
);
287 ictx
->flush_timeout
= HZ
/ 5;
288 clear_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
);
289 spin_unlock_bh(&sc
->chan_lock
);
293 ictx
->flush_timeout
= usecs_to_jiffies(sc
->sched
.channel_switch_time
);
295 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
)) {
296 spin_unlock_bh(&sc
->chan_lock
);
300 spin_unlock_bh(&sc
->chan_lock
);
302 if (ath9k_is_chanctx_enabled()) {
303 ath_chanctx_event(sc
, NULL
,
304 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
);
308 static struct ath_chanctx
*
309 ath_chanctx_get_next(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
311 int idx
= ctx
- &sc
->chanctx
[0];
313 return &sc
->chanctx
[!idx
];
316 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc
*sc
)
318 struct ath_chanctx
*prev
, *cur
;
320 u32 cur_tsf
, prev_tsf
, beacon_int
;
323 beacon_int
= TU_TO_USEC(sc
->cur_chan
->beacon
.beacon_interval
);
326 prev
= ath_chanctx_get_next(sc
, cur
);
328 if (!prev
->switch_after_beacon
)
331 getrawmonotonic(&ts
);
332 cur_tsf
= (u32
) cur
->tsf_val
+
333 ath9k_hw_get_tsf_offset(&cur
->tsf_ts
, &ts
);
335 prev_tsf
= prev
->last_beacon
- (u32
) prev
->tsf_val
+ cur_tsf
;
336 prev_tsf
-= ath9k_hw_get_tsf_offset(&prev
->tsf_ts
, &ts
);
338 /* Adjust the TSF time of the AP chanctx to keep its beacons
339 * at half beacon interval offset relative to the STA chanctx.
341 offset
= cur_tsf
- prev_tsf
;
343 /* Ignore stale data or spurious timestamps */
344 if (offset
< 0 || offset
> 3 * beacon_int
)
347 offset
= beacon_int
/ 2 - (offset
% beacon_int
);
348 prev
->tsf_val
+= offset
;
351 /* Configure the TSF based hardware timer for a channel switch.
352 * Also set up backup software timer, in case the gen timer fails.
353 * This could be caused by a hardware reset.
355 static void ath_chanctx_setup_timer(struct ath_softc
*sc
, u32 tsf_time
)
357 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
358 struct ath_hw
*ah
= sc
->sc_ah
;
360 ath9k_hw_gen_timer_start(ah
, sc
->p2p_ps_timer
, tsf_time
, 1000000);
361 tsf_time
-= ath9k_hw_gettsf32(ah
);
362 tsf_time
= msecs_to_jiffies(tsf_time
/ 1000) + 1;
363 mod_timer(&sc
->sched
.timer
, jiffies
+ tsf_time
);
365 ath_dbg(common
, CHAN_CTX
,
366 "Setup chanctx timer with timeout: %d ms\n", jiffies_to_msecs(tsf_time
));
369 void ath_chanctx_event(struct ath_softc
*sc
, struct ieee80211_vif
*vif
,
370 enum ath_chanctx_event ev
)
372 struct ath_hw
*ah
= sc
->sc_ah
;
373 struct ath_common
*common
= ath9k_hw_common(ah
);
374 struct ath_beacon_config
*cur_conf
;
375 struct ath_vif
*avp
= NULL
;
376 struct ath_chanctx
*ctx
;
381 avp
= (struct ath_vif
*) vif
->drv_priv
;
383 spin_lock_bh(&sc
->chan_lock
);
385 ath_dbg(common
, CHAN_CTX
, "cur_chan: %d MHz, event: %s, state: %s\n",
386 sc
->cur_chan
->chandef
.center_freq1
,
387 chanctx_event_string(ev
),
388 chanctx_state_string(sc
->sched
.state
));
391 case ATH_CHANCTX_EVENT_BEACON_PREPARE
:
392 if (avp
->offchannel_duration
)
393 avp
->offchannel_duration
= 0;
395 if (avp
->chanctx
!= sc
->cur_chan
) {
396 ath_dbg(common
, CHAN_CTX
,
397 "Contexts differ, not preparing beacon\n");
401 if (sc
->sched
.offchannel_pending
&& !sc
->sched
.wait_switch
) {
402 sc
->sched
.offchannel_pending
= false;
403 sc
->next_chan
= &sc
->offchannel
.chan
;
404 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
405 ath_dbg(common
, CHAN_CTX
,
406 "Setting offchannel_pending to false\n");
409 ctx
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
410 if (ctx
->active
&& sc
->sched
.state
== ATH_CHANCTX_STATE_IDLE
) {
412 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
413 ath_dbg(common
, CHAN_CTX
,
414 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
417 /* if the timer missed its window, use the next interval */
418 if (sc
->sched
.state
== ATH_CHANCTX_STATE_WAIT_FOR_TIMER
) {
419 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
420 ath_dbg(common
, CHAN_CTX
,
421 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
424 if (sc
->sched
.mgd_prepare_tx
)
425 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
428 * When a context becomes inactive, for example,
429 * disassociation of a station context, the NoA
430 * attribute needs to be removed from subsequent
433 if (!ctx
->active
&& avp
->noa_duration
&&
434 sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
) {
435 avp
->noa_duration
= 0;
436 avp
->periodic_noa
= false;
438 ath_dbg(common
, CHAN_CTX
,
439 "Clearing NoA schedule\n");
442 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
)
445 ath_dbg(common
, CHAN_CTX
, "Preparing beacon for vif: %pM\n", vif
->addr
);
447 sc
->sched
.beacon_pending
= true;
448 sc
->sched
.next_tbtt
= REG_READ(ah
, AR_NEXT_TBTT_TIMER
);
450 cur_conf
= &sc
->cur_chan
->beacon
;
451 beacon_int
= TU_TO_USEC(cur_conf
->beacon_interval
);
453 /* defer channel switch by a quarter beacon interval */
454 tsf_time
= sc
->sched
.next_tbtt
+ beacon_int
/ 4;
455 sc
->sched
.switch_start_time
= tsf_time
;
456 sc
->cur_chan
->last_beacon
= sc
->sched
.next_tbtt
;
459 * If an offchannel switch is scheduled to happen after
460 * a beacon transmission, update the NoA with one-shot
461 * values and increment the index.
463 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
465 avp
->offchannel_start
= tsf_time
;
466 avp
->offchannel_duration
= sc
->sched
.offchannel_duration
;
468 ath_dbg(common
, CHAN_CTX
,
469 "offchannel noa_duration: %d, noa_start: %d, noa_index: %d\n",
470 avp
->offchannel_duration
,
471 avp
->offchannel_start
,
475 * When multiple contexts are active, the NoA
476 * has to be recalculated and advertised after
477 * an offchannel operation.
479 if (ctx
->active
&& avp
->noa_duration
)
480 avp
->noa_duration
= 0;
486 * Clear the extend_absence flag if it had been
487 * set during the previous beacon transmission,
488 * since we need to revert to the normal NoA
491 if (ctx
->active
&& sc
->sched
.extend_absence
) {
492 avp
->noa_duration
= 0;
493 sc
->sched
.extend_absence
= false;
496 /* If at least two consecutive beacons were missed on the STA
497 * chanctx, stay on the STA channel for one extra beacon period,
498 * to resync the timer properly.
500 if (ctx
->active
&& sc
->sched
.beacon_miss
>= 2) {
501 avp
->noa_duration
= 0;
502 sc
->sched
.extend_absence
= true;
505 /* Prevent wrap-around issues */
506 if (avp
->noa_duration
&& tsf_time
- avp
->noa_start
> BIT(30))
507 avp
->noa_duration
= 0;
510 * If multiple contexts are active, start periodic
511 * NoA and increment the index for the first
515 (!avp
->noa_duration
|| sc
->sched
.force_noa_update
)) {
517 avp
->noa_start
= tsf_time
;
519 if (sc
->sched
.extend_absence
)
520 avp
->noa_duration
= (3 * beacon_int
/ 2) +
521 sc
->sched
.channel_switch_time
;
524 TU_TO_USEC(cur_conf
->beacon_interval
) / 2 +
525 sc
->sched
.channel_switch_time
;
527 if (test_bit(ATH_OP_SCANNING
, &common
->op_flags
) ||
528 sc
->sched
.extend_absence
)
529 avp
->periodic_noa
= false;
531 avp
->periodic_noa
= true;
533 ath_dbg(common
, CHAN_CTX
,
534 "noa_duration: %d, noa_start: %d, noa_index: %d, periodic: %d\n",
541 if (ctx
->active
&& sc
->sched
.force_noa_update
)
542 sc
->sched
.force_noa_update
= false;
545 case ATH_CHANCTX_EVENT_BEACON_SENT
:
546 if (!sc
->sched
.beacon_pending
) {
547 ath_dbg(common
, CHAN_CTX
,
548 "No pending beacon\n");
552 sc
->sched
.beacon_pending
= false;
554 if (sc
->sched
.mgd_prepare_tx
) {
555 sc
->sched
.mgd_prepare_tx
= false;
556 complete(&sc
->go_beacon
);
557 ath_dbg(common
, CHAN_CTX
,
558 "Beacon sent, complete go_beacon\n");
562 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
)
565 ath_dbg(common
, CHAN_CTX
,
566 "Move chanctx state to WAIT_FOR_TIMER\n");
568 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
;
569 ath_chanctx_setup_timer(sc
, sc
->sched
.switch_start_time
);
571 case ATH_CHANCTX_EVENT_TSF_TIMER
:
572 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
)
575 if (!sc
->cur_chan
->switch_after_beacon
&&
576 sc
->sched
.beacon_pending
)
577 sc
->sched
.beacon_miss
++;
579 ath_dbg(common
, CHAN_CTX
,
580 "Move chanctx state to SWITCH\n");
582 sc
->sched
.state
= ATH_CHANCTX_STATE_SWITCH
;
583 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
585 case ATH_CHANCTX_EVENT_BEACON_RECEIVED
:
586 if (!test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) ||
587 sc
->cur_chan
== &sc
->offchannel
.chan
)
590 sc
->sched
.beacon_pending
= false;
591 sc
->sched
.beacon_miss
= 0;
593 if (sc
->sched
.state
== ATH_CHANCTX_STATE_FORCE_ACTIVE
||
594 !sc
->cur_chan
->tsf_val
)
597 ath_chanctx_adjust_tbtt_delta(sc
);
599 /* TSF time might have been updated by the incoming beacon,
600 * need update the channel switch timer to reflect the change.
602 tsf_time
= sc
->sched
.switch_start_time
;
603 tsf_time
-= (u32
) sc
->cur_chan
->tsf_val
+
604 ath9k_hw_get_tsf_offset(&sc
->cur_chan
->tsf_ts
, NULL
);
605 tsf_time
+= ath9k_hw_gettsf32(ah
);
608 ath_chanctx_setup_timer(sc
, tsf_time
);
610 case ATH_CHANCTX_EVENT_AUTHORIZED
:
611 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_FORCE_ACTIVE
||
612 avp
->chanctx
!= sc
->cur_chan
)
615 ath_dbg(common
, CHAN_CTX
,
616 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
618 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
620 case ATH_CHANCTX_EVENT_SWITCH
:
621 if (!test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) ||
622 sc
->sched
.state
== ATH_CHANCTX_STATE_FORCE_ACTIVE
||
623 sc
->cur_chan
->switch_after_beacon
||
624 sc
->cur_chan
== &sc
->offchannel
.chan
)
627 /* If this is a station chanctx, stay active for a half
628 * beacon period (minus channel switch time)
630 sc
->next_chan
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
631 cur_conf
= &sc
->cur_chan
->beacon
;
633 ath_dbg(common
, CHAN_CTX
,
634 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
636 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_TIMER
;
637 sc
->sched
.wait_switch
= false;
639 tsf_time
= TU_TO_USEC(cur_conf
->beacon_interval
) / 2;
641 if (sc
->sched
.extend_absence
) {
642 sc
->sched
.beacon_miss
= 0;
646 tsf_time
-= sc
->sched
.channel_switch_time
;
647 tsf_time
+= ath9k_hw_gettsf32(sc
->sc_ah
);
648 sc
->sched
.switch_start_time
= tsf_time
;
650 ath_chanctx_setup_timer(sc
, tsf_time
);
651 sc
->sched
.beacon_pending
= true;
653 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL
:
654 if (sc
->cur_chan
== &sc
->offchannel
.chan
||
655 sc
->cur_chan
->switch_after_beacon
)
658 sc
->next_chan
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
659 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
661 case ATH_CHANCTX_EVENT_UNASSIGN
:
662 if (sc
->cur_chan
->assigned
) {
663 if (sc
->next_chan
&& !sc
->next_chan
->assigned
&&
664 sc
->next_chan
!= &sc
->offchannel
.chan
)
665 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
669 ctx
= ath_chanctx_get_next(sc
, sc
->cur_chan
);
670 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
675 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
677 case ATH_CHANCTX_EVENT_ASSIGN
:
679 * When adding a new channel context, check if a scan
680 * is in progress and abort it since the addition of
681 * a new channel context is usually followed by VIF
682 * assignment, in which case we have to start multi-channel
685 if (test_bit(ATH_OP_SCANNING
, &common
->op_flags
)) {
686 ath_dbg(common
, CHAN_CTX
,
687 "Aborting HW scan to add new context\n");
689 spin_unlock_bh(&sc
->chan_lock
);
690 del_timer_sync(&sc
->offchannel
.timer
);
691 ath_scan_complete(sc
, true);
692 spin_lock_bh(&sc
->chan_lock
);
695 case ATH_CHANCTX_EVENT_CHANGE
:
699 spin_unlock_bh(&sc
->chan_lock
);
702 void ath_chanctx_beacon_sent_ev(struct ath_softc
*sc
,
703 enum ath_chanctx_event ev
)
705 if (sc
->sched
.beacon_pending
)
706 ath_chanctx_event(sc
, NULL
, ev
);
709 void ath_chanctx_beacon_recv_ev(struct ath_softc
*sc
,
710 enum ath_chanctx_event ev
)
712 ath_chanctx_event(sc
, NULL
, ev
);
715 static int ath_scan_channel_duration(struct ath_softc
*sc
,
716 struct ieee80211_channel
*chan
)
718 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
720 if (!req
->n_ssids
|| (chan
->flags
& IEEE80211_CHAN_NO_IR
))
721 return (HZ
/ 9); /* ~110 ms */
723 return (HZ
/ 16); /* ~60 ms */
726 static void ath_chanctx_switch(struct ath_softc
*sc
, struct ath_chanctx
*ctx
,
727 struct cfg80211_chan_def
*chandef
)
729 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
731 spin_lock_bh(&sc
->chan_lock
);
733 if (test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
) &&
734 (sc
->cur_chan
!= ctx
) && (ctx
== &sc
->offchannel
.chan
)) {
736 ctx
->chandef
= *chandef
;
738 sc
->sched
.offchannel_pending
= true;
739 sc
->sched
.wait_switch
= true;
740 sc
->sched
.offchannel_duration
=
741 jiffies_to_usecs(sc
->offchannel
.duration
) +
742 sc
->sched
.channel_switch_time
;
744 spin_unlock_bh(&sc
->chan_lock
);
745 ath_dbg(common
, CHAN_CTX
,
746 "Set offchannel_pending to true\n");
752 ctx
->chandef
= *chandef
;
753 ath_dbg(common
, CHAN_CTX
,
754 "Assigned next_chan to %d MHz\n", chandef
->center_freq1
);
757 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
758 sc
->sched
.offchannel_duration
=
759 jiffies_to_usecs(sc
->offchannel
.duration
) +
760 sc
->sched
.channel_switch_time
;
763 ath_dbg(common
, CHAN_CTX
,
764 "Offchannel duration for chan %d MHz : %u\n",
765 chandef
->center_freq1
,
766 sc
->sched
.offchannel_duration
);
769 spin_unlock_bh(&sc
->chan_lock
);
770 ieee80211_queue_work(sc
->hw
, &sc
->chanctx_work
);
773 static void ath_chanctx_offchan_switch(struct ath_softc
*sc
,
774 struct ieee80211_channel
*chan
)
776 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
777 struct cfg80211_chan_def chandef
;
779 cfg80211_chandef_create(&chandef
, chan
, NL80211_CHAN_NO_HT
);
780 ath_dbg(common
, CHAN_CTX
,
781 "Channel definition created: %d MHz\n", chandef
.center_freq1
);
783 ath_chanctx_switch(sc
, &sc
->offchannel
.chan
, &chandef
);
786 static struct ath_chanctx
*ath_chanctx_get_oper_chan(struct ath_softc
*sc
,
789 struct ath_chanctx
*ctx
;
791 ath_for_each_chanctx(sc
, ctx
) {
792 if (!ctx
->assigned
|| list_empty(&ctx
->vifs
))
794 if (active
&& !ctx
->active
)
797 if (ctx
->switch_after_beacon
)
801 return &sc
->chanctx
[0];
805 ath_scan_next_channel(struct ath_softc
*sc
)
807 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
808 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
809 struct ieee80211_channel
*chan
;
811 if (sc
->offchannel
.scan_idx
>= req
->n_channels
) {
812 ath_dbg(common
, CHAN_CTX
,
813 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
814 "scan_idx: %d, n_channels: %d\n",
815 sc
->offchannel
.scan_idx
,
818 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
819 ath_chanctx_switch(sc
, ath_chanctx_get_oper_chan(sc
, false),
824 ath_dbg(common
, CHAN_CTX
,
825 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
826 sc
->offchannel
.scan_idx
);
828 chan
= req
->channels
[sc
->offchannel
.scan_idx
++];
829 sc
->offchannel
.duration
= ath_scan_channel_duration(sc
, chan
);
830 sc
->offchannel
.state
= ATH_OFFCHANNEL_PROBE_SEND
;
832 ath_chanctx_offchan_switch(sc
, chan
);
835 void ath_offchannel_next(struct ath_softc
*sc
)
837 struct ieee80211_vif
*vif
;
839 if (sc
->offchannel
.scan_req
) {
840 vif
= sc
->offchannel
.scan_vif
;
841 sc
->offchannel
.chan
.txpower
= vif
->bss_conf
.txpower
;
842 ath_scan_next_channel(sc
);
843 } else if (sc
->offchannel
.roc_vif
) {
844 vif
= sc
->offchannel
.roc_vif
;
845 sc
->offchannel
.chan
.txpower
= vif
->bss_conf
.txpower
;
846 sc
->offchannel
.duration
=
847 msecs_to_jiffies(sc
->offchannel
.roc_duration
);
848 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_START
;
849 ath_chanctx_offchan_switch(sc
, sc
->offchannel
.roc_chan
);
851 ath_chanctx_switch(sc
, ath_chanctx_get_oper_chan(sc
, false),
853 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
859 void ath_roc_complete(struct ath_softc
*sc
, bool abort
)
861 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
864 ath_dbg(common
, CHAN_CTX
, "RoC aborted\n");
866 ath_dbg(common
, CHAN_CTX
, "RoC expired\n");
868 sc
->offchannel
.roc_vif
= NULL
;
869 sc
->offchannel
.roc_chan
= NULL
;
871 ieee80211_remain_on_channel_expired(sc
->hw
);
872 ath_offchannel_next(sc
);
873 ath9k_ps_restore(sc
);
876 void ath_scan_complete(struct ath_softc
*sc
, bool abort
)
878 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
881 ath_dbg(common
, CHAN_CTX
, "HW scan aborted\n");
883 ath_dbg(common
, CHAN_CTX
, "HW scan complete\n");
885 sc
->offchannel
.scan_req
= NULL
;
886 sc
->offchannel
.scan_vif
= NULL
;
887 sc
->offchannel
.state
= ATH_OFFCHANNEL_IDLE
;
888 ieee80211_scan_completed(sc
->hw
, abort
);
889 clear_bit(ATH_OP_SCANNING
, &common
->op_flags
);
890 spin_lock_bh(&sc
->chan_lock
);
891 if (test_bit(ATH_OP_MULTI_CHANNEL
, &common
->op_flags
))
892 sc
->sched
.force_noa_update
= true;
893 spin_unlock_bh(&sc
->chan_lock
);
894 ath_offchannel_next(sc
);
895 ath9k_ps_restore(sc
);
898 static void ath_scan_send_probe(struct ath_softc
*sc
,
899 struct cfg80211_ssid
*ssid
)
901 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
902 struct ieee80211_vif
*vif
= sc
->offchannel
.scan_vif
;
903 struct ath_tx_control txctl
= {};
905 struct ieee80211_tx_info
*info
;
906 int band
= sc
->offchannel
.chan
.chandef
.chan
->band
;
908 skb
= ieee80211_probereq_get(sc
->hw
, vif
,
909 ssid
->ssid
, ssid
->ssid_len
, req
->ie_len
);
913 info
= IEEE80211_SKB_CB(skb
);
915 info
->flags
|= IEEE80211_TX_CTL_NO_CCK_RATE
;
918 memcpy(skb_put(skb
, req
->ie_len
), req
->ie
, req
->ie_len
);
920 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
922 if (!ieee80211_tx_prepare_skb(sc
->hw
, vif
, skb
, band
, NULL
))
925 txctl
.txq
= sc
->tx
.txq_map
[IEEE80211_AC_VO
];
926 txctl
.force_channel
= true;
927 if (ath_tx_start(sc
->hw
, skb
, &txctl
))
933 ieee80211_free_txskb(sc
->hw
, skb
);
936 static void ath_scan_channel_start(struct ath_softc
*sc
)
938 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
939 struct cfg80211_scan_request
*req
= sc
->offchannel
.scan_req
;
942 if (!(sc
->cur_chan
->chandef
.chan
->flags
& IEEE80211_CHAN_NO_IR
) &&
944 for (i
= 0; i
< req
->n_ssids
; i
++)
945 ath_scan_send_probe(sc
, &req
->ssids
[i
]);
949 ath_dbg(common
, CHAN_CTX
,
950 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
952 sc
->offchannel
.state
= ATH_OFFCHANNEL_PROBE_WAIT
;
953 mod_timer(&sc
->offchannel
.timer
, jiffies
+ sc
->offchannel
.duration
);
956 static void ath_chanctx_timer(unsigned long data
)
958 struct ath_softc
*sc
= (struct ath_softc
*) data
;
959 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
961 ath_dbg(common
, CHAN_CTX
,
962 "Channel context timer invoked\n");
964 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_TSF_TIMER
);
967 static void ath_offchannel_timer(unsigned long data
)
969 struct ath_softc
*sc
= (struct ath_softc
*)data
;
970 struct ath_chanctx
*ctx
;
971 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
973 ath_dbg(common
, CHAN_CTX
, "%s: offchannel state: %s\n",
974 __func__
, offchannel_state_string(sc
->offchannel
.state
));
976 switch (sc
->offchannel
.state
) {
977 case ATH_OFFCHANNEL_PROBE_WAIT
:
978 if (!sc
->offchannel
.scan_req
)
981 /* get first active channel context */
982 ctx
= ath_chanctx_get_oper_chan(sc
, true);
984 ath_dbg(common
, CHAN_CTX
,
985 "Switch to oper/active context, "
986 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
988 sc
->offchannel
.state
= ATH_OFFCHANNEL_SUSPEND
;
989 ath_chanctx_switch(sc
, ctx
, NULL
);
990 mod_timer(&sc
->offchannel
.timer
, jiffies
+ HZ
/ 10);
994 case ATH_OFFCHANNEL_SUSPEND
:
995 if (!sc
->offchannel
.scan_req
)
998 ath_scan_next_channel(sc
);
1000 case ATH_OFFCHANNEL_ROC_START
:
1001 case ATH_OFFCHANNEL_ROC_WAIT
:
1002 ctx
= ath_chanctx_get_oper_chan(sc
, false);
1003 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_DONE
;
1004 ath_chanctx_switch(sc
, ctx
, NULL
);
1012 ath_chanctx_send_vif_ps_frame(struct ath_softc
*sc
, struct ath_vif
*avp
,
1015 struct ieee80211_vif
*vif
= avp
->vif
;
1016 struct ieee80211_sta
*sta
= NULL
;
1017 struct ieee80211_hdr_3addr
*nullfunc
;
1018 struct ath_tx_control txctl
;
1019 struct sk_buff
*skb
;
1020 int band
= sc
->cur_chan
->chandef
.chan
->band
;
1022 switch (vif
->type
) {
1023 case NL80211_IFTYPE_STATION
:
1027 skb
= ieee80211_nullfunc_get(sc
->hw
, vif
);
1031 nullfunc
= (struct ieee80211_hdr_3addr
*) skb
->data
;
1033 nullfunc
->frame_control
|=
1034 cpu_to_le16(IEEE80211_FCTL_PM
);
1036 skb_set_queue_mapping(skb
, IEEE80211_AC_VO
);
1037 if (!ieee80211_tx_prepare_skb(sc
->hw
, vif
, skb
, band
, &sta
)) {
1038 dev_kfree_skb_any(skb
);
1046 memset(&txctl
, 0, sizeof(txctl
));
1047 txctl
.txq
= sc
->tx
.txq_map
[IEEE80211_AC_VO
];
1049 txctl
.force_channel
= true;
1050 if (ath_tx_start(sc
->hw
, skb
, &txctl
)) {
1051 ieee80211_free_txskb(sc
->hw
, skb
);
1059 ath_chanctx_send_ps_frame(struct ath_softc
*sc
, bool powersave
)
1061 struct ath_vif
*avp
;
1065 list_for_each_entry(avp
, &sc
->cur_chan
->vifs
, list
) {
1066 if (ath_chanctx_send_vif_ps_frame(sc
, avp
, powersave
))
1074 static bool ath_chanctx_defer_switch(struct ath_softc
*sc
)
1076 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
1078 if (sc
->cur_chan
== &sc
->offchannel
.chan
)
1081 switch (sc
->sched
.state
) {
1082 case ATH_CHANCTX_STATE_SWITCH
:
1084 case ATH_CHANCTX_STATE_IDLE
:
1085 if (!sc
->cur_chan
->switch_after_beacon
)
1088 ath_dbg(common
, CHAN_CTX
,
1089 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1091 sc
->sched
.state
= ATH_CHANCTX_STATE_WAIT_FOR_BEACON
;
1100 static void ath_offchannel_channel_change(struct ath_softc
*sc
)
1102 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
1104 ath_dbg(common
, CHAN_CTX
, "%s: offchannel state: %s\n",
1105 __func__
, offchannel_state_string(sc
->offchannel
.state
));
1107 switch (sc
->offchannel
.state
) {
1108 case ATH_OFFCHANNEL_PROBE_SEND
:
1109 if (!sc
->offchannel
.scan_req
)
1112 if (sc
->cur_chan
->chandef
.chan
!=
1113 sc
->offchannel
.chan
.chandef
.chan
)
1116 ath_scan_channel_start(sc
);
1118 case ATH_OFFCHANNEL_IDLE
:
1119 if (!sc
->offchannel
.scan_req
)
1122 ath_scan_complete(sc
, false);
1124 case ATH_OFFCHANNEL_ROC_START
:
1125 if (sc
->cur_chan
!= &sc
->offchannel
.chan
)
1128 sc
->offchannel
.state
= ATH_OFFCHANNEL_ROC_WAIT
;
1129 mod_timer(&sc
->offchannel
.timer
,
1130 jiffies
+ sc
->offchannel
.duration
);
1131 ieee80211_ready_on_channel(sc
->hw
);
1133 case ATH_OFFCHANNEL_ROC_DONE
:
1134 ath_roc_complete(sc
, false);
1141 void ath_chanctx_set_next(struct ath_softc
*sc
, bool force
)
1143 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
1144 struct ath_chanctx
*old_ctx
;
1146 bool measure_time
= false;
1147 bool send_ps
= false;
1148 bool queues_stopped
= false;
1150 spin_lock_bh(&sc
->chan_lock
);
1151 if (!sc
->next_chan
) {
1152 spin_unlock_bh(&sc
->chan_lock
);
1156 if (!force
&& ath_chanctx_defer_switch(sc
)) {
1157 spin_unlock_bh(&sc
->chan_lock
);
1161 ath_dbg(common
, CHAN_CTX
,
1162 "%s: current: %d MHz, next: %d MHz\n",
1164 sc
->cur_chan
->chandef
.center_freq1
,
1165 sc
->next_chan
->chandef
.center_freq1
);
1167 if (sc
->cur_chan
!= sc
->next_chan
) {
1168 ath_dbg(common
, CHAN_CTX
,
1169 "Stopping current chanctx: %d\n",
1170 sc
->cur_chan
->chandef
.center_freq1
);
1171 sc
->cur_chan
->stopped
= true;
1172 spin_unlock_bh(&sc
->chan_lock
);
1174 if (sc
->next_chan
== &sc
->offchannel
.chan
) {
1175 getrawmonotonic(&ts
);
1176 measure_time
= true;
1179 ath9k_chanctx_stop_queues(sc
, sc
->cur_chan
);
1180 queues_stopped
= true;
1182 __ath9k_flush(sc
->hw
, ~0, true, false);
1184 if (ath_chanctx_send_ps_frame(sc
, true))
1185 __ath9k_flush(sc
->hw
, BIT(IEEE80211_AC_VO
),
1189 spin_lock_bh(&sc
->chan_lock
);
1191 if (sc
->cur_chan
!= &sc
->offchannel
.chan
) {
1192 getrawmonotonic(&sc
->cur_chan
->tsf_ts
);
1193 sc
->cur_chan
->tsf_val
= ath9k_hw_gettsf64(sc
->sc_ah
);
1196 old_ctx
= sc
->cur_chan
;
1197 sc
->cur_chan
= sc
->next_chan
;
1198 sc
->cur_chan
->stopped
= false;
1199 sc
->next_chan
= NULL
;
1201 if (!sc
->sched
.offchannel_pending
)
1202 sc
->sched
.offchannel_duration
= 0;
1204 if (sc
->sched
.state
!= ATH_CHANCTX_STATE_FORCE_ACTIVE
)
1205 sc
->sched
.state
= ATH_CHANCTX_STATE_IDLE
;
1207 spin_unlock_bh(&sc
->chan_lock
);
1209 if (sc
->sc_ah
->chip_fullsleep
||
1210 memcmp(&sc
->cur_chandef
, &sc
->cur_chan
->chandef
,
1211 sizeof(sc
->cur_chandef
))) {
1212 ath_dbg(common
, CHAN_CTX
,
1213 "%s: Set channel %d MHz\n",
1214 __func__
, sc
->cur_chan
->chandef
.center_freq1
);
1215 ath_set_channel(sc
);
1217 sc
->sched
.channel_switch_time
=
1218 ath9k_hw_get_tsf_offset(&ts
, NULL
);
1220 * A reset will ensure that all queues are woken up,
1221 * so there is no need to awaken them again.
1227 ath9k_chanctx_wake_queues(sc
, old_ctx
);
1230 ath_chanctx_send_ps_frame(sc
, false);
1232 ath_offchannel_channel_change(sc
);
1233 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_SWITCH
);
1236 static void ath_chanctx_work(struct work_struct
*work
)
1238 struct ath_softc
*sc
= container_of(work
, struct ath_softc
,
1240 mutex_lock(&sc
->mutex
);
1241 ath_chanctx_set_next(sc
, false);
1242 mutex_unlock(&sc
->mutex
);
1245 void ath9k_offchannel_init(struct ath_softc
*sc
)
1247 struct ath_chanctx
*ctx
;
1248 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
1249 struct ieee80211_supported_band
*sband
;
1250 struct ieee80211_channel
*chan
;
1253 sband
= &common
->sbands
[IEEE80211_BAND_2GHZ
];
1254 if (!sband
->n_channels
)
1255 sband
= &common
->sbands
[IEEE80211_BAND_5GHZ
];
1257 chan
= &sband
->channels
[0];
1259 ctx
= &sc
->offchannel
.chan
;
1260 INIT_LIST_HEAD(&ctx
->vifs
);
1261 ctx
->txpower
= ATH_TXPOWER_MAX
;
1262 cfg80211_chandef_create(&ctx
->chandef
, chan
, NL80211_CHAN_HT20
);
1264 for (i
= 0; i
< ARRAY_SIZE(ctx
->acq
); i
++)
1265 INIT_LIST_HEAD(&ctx
->acq
[i
]);
1267 sc
->offchannel
.chan
.offchannel
= true;
1270 void ath9k_init_channel_context(struct ath_softc
*sc
)
1272 INIT_WORK(&sc
->chanctx_work
, ath_chanctx_work
);
1274 setup_timer(&sc
->offchannel
.timer
, ath_offchannel_timer
,
1276 setup_timer(&sc
->sched
.timer
, ath_chanctx_timer
,
1279 init_completion(&sc
->go_beacon
);
1282 void ath9k_deinit_channel_context(struct ath_softc
*sc
)
1284 cancel_work_sync(&sc
->chanctx_work
);
1287 bool ath9k_is_chanctx_enabled(void)
1289 return (ath9k_use_chanctx
== 1);
1292 /********************/
1293 /* Queue management */
1294 /********************/
1296 void ath9k_chanctx_stop_queues(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
1298 struct ath_hw
*ah
= sc
->sc_ah
;
1301 if (ctx
== &sc
->offchannel
.chan
) {
1302 ieee80211_stop_queue(sc
->hw
,
1303 sc
->hw
->offchannel_tx_hw_queue
);
1305 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++)
1306 ieee80211_stop_queue(sc
->hw
,
1307 ctx
->hw_queue_base
+ i
);
1310 if (ah
->opmode
== NL80211_IFTYPE_AP
)
1311 ieee80211_stop_queue(sc
->hw
, sc
->hw
->queues
- 2);
1315 void ath9k_chanctx_wake_queues(struct ath_softc
*sc
, struct ath_chanctx
*ctx
)
1317 struct ath_hw
*ah
= sc
->sc_ah
;
1320 if (ctx
== &sc
->offchannel
.chan
) {
1321 ieee80211_wake_queue(sc
->hw
,
1322 sc
->hw
->offchannel_tx_hw_queue
);
1324 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++)
1325 ieee80211_wake_queue(sc
->hw
,
1326 ctx
->hw_queue_base
+ i
);
1329 if (ah
->opmode
== NL80211_IFTYPE_AP
)
1330 ieee80211_wake_queue(sc
->hw
, sc
->hw
->queues
- 2);
1337 static void ath9k_update_p2p_ps_timer(struct ath_softc
*sc
, struct ath_vif
*avp
)
1339 struct ath_hw
*ah
= sc
->sc_ah
;
1340 s32 tsf
, target_tsf
;
1342 if (!avp
|| !avp
->noa
.has_next_tsf
)
1345 ath9k_hw_gen_timer_stop(ah
, sc
->p2p_ps_timer
);
1347 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1349 target_tsf
= avp
->noa
.next_tsf
;
1350 if (!avp
->noa
.absent
)
1351 target_tsf
-= ATH_P2P_PS_STOP_TIME
;
1353 if (target_tsf
- tsf
< ATH_P2P_PS_STOP_TIME
)
1354 target_tsf
= tsf
+ ATH_P2P_PS_STOP_TIME
;
1356 ath9k_hw_gen_timer_start(ah
, sc
->p2p_ps_timer
, (u32
) target_tsf
, 1000000);
1359 static void ath9k_update_p2p_ps(struct ath_softc
*sc
, struct ieee80211_vif
*vif
)
1361 struct ath_vif
*avp
= (void *)vif
->drv_priv
;
1364 if (!sc
->p2p_ps_timer
)
1367 if (vif
->type
!= NL80211_IFTYPE_STATION
|| !vif
->p2p
)
1370 sc
->p2p_ps_vif
= avp
;
1371 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1372 ieee80211_parse_p2p_noa(&vif
->bss_conf
.p2p_noa_attr
, &avp
->noa
, tsf
);
1373 ath9k_update_p2p_ps_timer(sc
, avp
);
1376 static u8
ath9k_get_ctwin(struct ath_softc
*sc
, struct ath_vif
*avp
)
1378 struct ath_beacon_config
*cur_conf
= &sc
->cur_chan
->beacon
;
1379 u8 switch_time
, ctwin
;
1382 * Channel switch in multi-channel mode is deferred
1383 * by a quarter beacon interval when handling
1384 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1385 * interface is guaranteed to be discoverable
1386 * for that duration after a TBTT.
1388 switch_time
= cur_conf
->beacon_interval
/ 4;
1390 ctwin
= avp
->vif
->bss_conf
.p2p_noa_attr
.oppps_ctwindow
;
1391 if (ctwin
&& (ctwin
< switch_time
))
1394 if (switch_time
< P2P_DEFAULT_CTWIN
)
1397 return P2P_DEFAULT_CTWIN
;
1400 void ath9k_beacon_add_noa(struct ath_softc
*sc
, struct ath_vif
*avp
,
1401 struct sk_buff
*skb
)
1403 static const u8 noa_ie_hdr
[] = {
1404 WLAN_EID_VENDOR_SPECIFIC
, /* type */
1406 0x50, 0x6f, 0x9a, /* WFA OUI */
1407 0x09, /* P2P subtype */
1408 0x0c, /* Notice of Absence */
1409 0x00, /* LSB of little-endian len */
1410 0x00, /* MSB of little-endian len */
1413 struct ieee80211_p2p_noa_attr
*noa
;
1414 int noa_len
, noa_desc
, i
= 0;
1417 if (!avp
->offchannel_duration
&& !avp
->noa_duration
)
1420 noa_desc
= !!avp
->offchannel_duration
+ !!avp
->noa_duration
;
1421 noa_len
= 2 + sizeof(struct ieee80211_p2p_noa_desc
) * noa_desc
;
1423 hdr
= skb_put(skb
, sizeof(noa_ie_hdr
));
1424 memcpy(hdr
, noa_ie_hdr
, sizeof(noa_ie_hdr
));
1425 hdr
[1] = sizeof(noa_ie_hdr
) + noa_len
- 2;
1428 noa
= (void *) skb_put(skb
, noa_len
);
1429 memset(noa
, 0, noa_len
);
1431 noa
->index
= avp
->noa_index
;
1432 noa
->oppps_ctwindow
= ath9k_get_ctwin(sc
, avp
);
1434 if (avp
->noa_duration
) {
1435 if (avp
->periodic_noa
) {
1436 u32 interval
= TU_TO_USEC(sc
->cur_chan
->beacon
.beacon_interval
);
1437 noa
->desc
[i
].count
= 255;
1438 noa
->desc
[i
].interval
= cpu_to_le32(interval
);
1440 noa
->desc
[i
].count
= 1;
1443 noa
->desc
[i
].start_time
= cpu_to_le32(avp
->noa_start
);
1444 noa
->desc
[i
].duration
= cpu_to_le32(avp
->noa_duration
);
1448 if (avp
->offchannel_duration
) {
1449 noa
->desc
[i
].count
= 1;
1450 noa
->desc
[i
].start_time
= cpu_to_le32(avp
->offchannel_start
);
1451 noa
->desc
[i
].duration
= cpu_to_le32(avp
->offchannel_duration
);
1455 void ath9k_p2p_ps_timer(void *priv
)
1457 struct ath_softc
*sc
= priv
;
1458 struct ath_vif
*avp
= sc
->p2p_ps_vif
;
1459 struct ieee80211_vif
*vif
;
1460 struct ieee80211_sta
*sta
;
1461 struct ath_node
*an
;
1464 del_timer_sync(&sc
->sched
.timer
);
1465 ath9k_hw_gen_timer_stop(sc
->sc_ah
, sc
->p2p_ps_timer
);
1466 ath_chanctx_event(sc
, NULL
, ATH_CHANCTX_EVENT_TSF_TIMER
);
1468 if (!avp
|| avp
->chanctx
!= sc
->cur_chan
)
1471 tsf
= ath9k_hw_gettsf32(sc
->sc_ah
);
1472 if (!avp
->noa
.absent
)
1473 tsf
+= ATH_P2P_PS_STOP_TIME
;
1475 if (!avp
->noa
.has_next_tsf
||
1476 avp
->noa
.next_tsf
- tsf
> BIT(31))
1477 ieee80211_update_p2p_noa(&avp
->noa
, tsf
);
1479 ath9k_update_p2p_ps_timer(sc
, avp
);
1484 sta
= ieee80211_find_sta(vif
, avp
->bssid
);
1488 an
= (void *) sta
->drv_priv
;
1489 if (an
->sleeping
== !!avp
->noa
.absent
)
1492 an
->sleeping
= avp
->noa
.absent
;
1494 ath_tx_aggr_sleep(sta
, sc
, an
);
1496 ath_tx_aggr_wakeup(sc
, an
);
1502 void ath9k_p2p_bss_info_changed(struct ath_softc
*sc
,
1503 struct ieee80211_vif
*vif
)
1505 unsigned long flags
;
1507 spin_lock_bh(&sc
->sc_pcu_lock
);
1508 spin_lock_irqsave(&sc
->sc_pm_lock
, flags
);
1509 if (!(sc
->ps_flags
& PS_BEACON_SYNC
))
1510 ath9k_update_p2p_ps(sc
, vif
);
1511 spin_unlock_irqrestore(&sc
->sc_pm_lock
, flags
);
1512 spin_unlock_bh(&sc
->sc_pcu_lock
);
1515 void ath9k_p2p_beacon_sync(struct ath_softc
*sc
)
1518 ath9k_update_p2p_ps(sc
, sc
->p2p_ps_vif
->vif
);
1521 void ath9k_p2p_remove_vif(struct ath_softc
*sc
,
1522 struct ieee80211_vif
*vif
)
1524 struct ath_vif
*avp
= (void *)vif
->drv_priv
;
1526 spin_lock_bh(&sc
->sc_pcu_lock
);
1527 if (avp
== sc
->p2p_ps_vif
) {
1528 sc
->p2p_ps_vif
= NULL
;
1529 ath9k_update_p2p_ps_timer(sc
, NULL
);
1531 spin_unlock_bh(&sc
->sc_pcu_lock
);
1534 int ath9k_init_p2p(struct ath_softc
*sc
)
1536 sc
->p2p_ps_timer
= ath_gen_timer_alloc(sc
->sc_ah
, ath9k_p2p_ps_timer
,
1537 NULL
, sc
, AR_FIRST_NDP_TIMER
);
1538 if (!sc
->p2p_ps_timer
)
1544 void ath9k_deinit_p2p(struct ath_softc
*sc
)
1546 if (sc
->p2p_ps_timer
)
1547 ath_gen_timer_free(sc
->sc_ah
, sc
->p2p_ps_timer
);
1550 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */