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->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
96 ath9k_cmn_spectral_scan_trigger(common, &sc->spec_priv);
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;
359 unsigned long timeout;
361 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
362 tsf_time -= ath9k_hw_gettsf32(ah);
363 timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
364 mod_timer(&sc->sched.timer, jiffies + timeout);
366 ath_dbg(common, CHAN_CTX,
367 "Setup chanctx timer with timeout: %d (%d) ms\n",
368 tsf_time / 1000, jiffies_to_msecs(timeout));
371 static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
372 struct ath_chanctx *ctx,
376 * Clear the extend_absence flag if it had been
377 * set during the previous beacon transmission,
378 * since we need to revert to the normal NoA
381 if (ctx->active && sc->sched.extend_absence) {
382 avp->noa_duration = 0;
383 sc->sched.extend_absence = false;
386 /* If at least two consecutive beacons were missed on the STA
387 * chanctx, stay on the STA channel for one extra beacon period,
388 * to resync the timer properly.
390 if (ctx->active && sc->sched.beacon_miss >= 2) {
391 avp->noa_duration = 0;
392 sc->sched.extend_absence = true;
396 static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
397 struct ath_chanctx *ctx,
401 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
404 avp->offchannel_start = tsf_time;
405 avp->offchannel_duration = sc->sched.offchannel_duration;
407 ath_dbg(common, CHAN_CTX,
408 "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
409 avp->offchannel_duration,
410 avp->offchannel_start,
414 * When multiple contexts are active, the NoA
415 * has to be recalculated and advertised after
416 * an offchannel operation.
418 if (ctx->active && avp->noa_duration)
419 avp->noa_duration = 0;
422 static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
424 struct ath_beacon_config *cur_conf,
428 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
431 avp->noa_start = tsf_time;
433 if (sc->sched.extend_absence)
434 avp->noa_duration = (3 * beacon_int / 2) +
435 sc->sched.channel_switch_time;
438 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
439 sc->sched.channel_switch_time;
441 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
442 sc->sched.extend_absence)
443 avp->periodic_noa = false;
445 avp->periodic_noa = true;
447 ath_dbg(common, CHAN_CTX,
448 "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
455 static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
460 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
463 avp->noa_start = tsf_time;
464 avp->periodic_noa = false;
465 avp->oneshot_noa = true;
466 avp->noa_duration = duration + sc->sched.channel_switch_time;
468 ath_dbg(common, CHAN_CTX,
469 "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
476 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
477 enum ath_chanctx_event ev)
479 struct ath_hw *ah = sc->sc_ah;
480 struct ath_common *common = ath9k_hw_common(ah);
481 struct ath_beacon_config *cur_conf;
482 struct ath_vif *avp = NULL;
483 struct ath_chanctx *ctx;
488 avp = (struct ath_vif *) vif->drv_priv;
490 spin_lock_bh(&sc->chan_lock);
492 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s\n",
493 sc->cur_chan->chandef.center_freq1,
494 chanctx_event_string(ev),
495 chanctx_state_string(sc->sched.state));
498 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
499 if (avp->offchannel_duration)
500 avp->offchannel_duration = 0;
502 if (avp->oneshot_noa) {
503 avp->noa_duration = 0;
504 avp->oneshot_noa = false;
506 ath_dbg(common, CHAN_CTX,
507 "Clearing oneshot NoA\n");
510 if (avp->chanctx != sc->cur_chan) {
511 ath_dbg(common, CHAN_CTX,
512 "Contexts differ, not preparing beacon\n");
516 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
517 sc->sched.offchannel_pending = false;
518 sc->next_chan = &sc->offchannel.chan;
519 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
520 ath_dbg(common, CHAN_CTX,
521 "Setting offchannel_pending to false\n");
524 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
525 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
527 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
528 ath_dbg(common, CHAN_CTX,
529 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
532 /* if the timer missed its window, use the next interval */
533 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
534 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
535 ath_dbg(common, CHAN_CTX,
536 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
539 if (sc->sched.mgd_prepare_tx)
540 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
543 * When a context becomes inactive, for example,
544 * disassociation of a station context, the NoA
545 * attribute needs to be removed from subsequent
548 if (!ctx->active && avp->noa_duration &&
549 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
550 avp->noa_duration = 0;
551 avp->periodic_noa = false;
553 ath_dbg(common, CHAN_CTX,
554 "Clearing NoA schedule\n");
557 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
560 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
562 sc->sched.beacon_pending = true;
563 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
565 cur_conf = &sc->cur_chan->beacon;
566 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
568 /* defer channel switch by a quarter beacon interval */
569 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
570 sc->sched.switch_start_time = tsf_time;
571 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
574 * If an offchannel switch is scheduled to happen after
575 * a beacon transmission, update the NoA with one-shot
576 * values and increment the index.
578 if (sc->next_chan == &sc->offchannel.chan) {
579 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
583 ath_chanctx_handle_bmiss(sc, ctx, avp);
586 * If a mgd_prepare_tx() has been called by mac80211,
587 * a one-shot NoA needs to be sent. This can happen
588 * with one or more active channel contexts - in both
589 * cases, a new NoA schedule has to be advertised.
591 if (sc->sched.mgd_prepare_tx) {
592 ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
593 jiffies_to_usecs(HZ / 5));
597 /* Prevent wrap-around issues */
598 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
599 avp->noa_duration = 0;
602 * If multiple contexts are active, start periodic
603 * NoA and increment the index for the first
607 (!avp->noa_duration || sc->sched.force_noa_update))
608 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
609 tsf_time, beacon_int);
611 if (ctx->active && sc->sched.force_noa_update)
612 sc->sched.force_noa_update = false;
615 case ATH_CHANCTX_EVENT_BEACON_SENT:
616 if (!sc->sched.beacon_pending) {
617 ath_dbg(common, CHAN_CTX,
618 "No pending beacon\n");
622 sc->sched.beacon_pending = false;
624 if (sc->sched.mgd_prepare_tx) {
625 sc->sched.mgd_prepare_tx = false;
626 complete(&sc->go_beacon);
627 ath_dbg(common, CHAN_CTX,
628 "Beacon sent, complete go_beacon\n");
632 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
635 ath_dbg(common, CHAN_CTX,
636 "Move chanctx state to WAIT_FOR_TIMER\n");
638 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
639 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
641 case ATH_CHANCTX_EVENT_TSF_TIMER:
642 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
645 if (!sc->cur_chan->switch_after_beacon &&
646 sc->sched.beacon_pending)
647 sc->sched.beacon_miss++;
649 ath_dbg(common, CHAN_CTX,
650 "Move chanctx state to SWITCH\n");
652 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
653 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
655 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
656 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
657 sc->cur_chan == &sc->offchannel.chan)
660 sc->sched.beacon_pending = false;
661 sc->sched.beacon_miss = 0;
663 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
664 !sc->sched.beacon_adjust ||
665 !sc->cur_chan->tsf_val)
668 ath_chanctx_adjust_tbtt_delta(sc);
670 /* TSF time might have been updated by the incoming beacon,
671 * need update the channel switch timer to reflect the change.
673 tsf_time = sc->sched.switch_start_time;
674 tsf_time -= (u32) sc->cur_chan->tsf_val +
675 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
676 tsf_time += ath9k_hw_gettsf32(ah);
678 sc->sched.beacon_adjust = false;
679 ath_chanctx_setup_timer(sc, tsf_time);
681 case ATH_CHANCTX_EVENT_AUTHORIZED:
682 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
683 avp->chanctx != sc->cur_chan)
686 ath_dbg(common, CHAN_CTX,
687 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
689 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
691 case ATH_CHANCTX_EVENT_SWITCH:
692 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
693 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
694 sc->cur_chan->switch_after_beacon ||
695 sc->cur_chan == &sc->offchannel.chan)
698 /* If this is a station chanctx, stay active for a half
699 * beacon period (minus channel switch time)
701 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
702 cur_conf = &sc->cur_chan->beacon;
704 ath_dbg(common, CHAN_CTX,
705 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
707 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
708 sc->sched.wait_switch = false;
710 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
712 if (sc->sched.extend_absence) {
713 sc->sched.beacon_miss = 0;
717 tsf_time -= sc->sched.channel_switch_time;
718 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
719 sc->sched.switch_start_time = tsf_time;
721 ath_chanctx_setup_timer(sc, tsf_time);
722 sc->sched.beacon_pending = true;
723 sc->sched.beacon_adjust = true;
725 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
726 if (sc->cur_chan == &sc->offchannel.chan ||
727 sc->cur_chan->switch_after_beacon)
730 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
731 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
733 case ATH_CHANCTX_EVENT_UNASSIGN:
734 if (sc->cur_chan->assigned) {
735 if (sc->next_chan && !sc->next_chan->assigned &&
736 sc->next_chan != &sc->offchannel.chan)
737 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
741 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
742 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
747 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
749 case ATH_CHANCTX_EVENT_ASSIGN:
751 case ATH_CHANCTX_EVENT_CHANGE:
755 spin_unlock_bh(&sc->chan_lock);
758 void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
759 enum ath_chanctx_event ev)
761 if (sc->sched.beacon_pending)
762 ath_chanctx_event(sc, NULL, ev);
765 void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
766 enum ath_chanctx_event ev)
768 ath_chanctx_event(sc, NULL, ev);
771 static int ath_scan_channel_duration(struct ath_softc *sc,
772 struct ieee80211_channel *chan)
774 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
776 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
777 return (HZ / 9); /* ~110 ms */
779 return (HZ / 16); /* ~60 ms */
782 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
783 struct cfg80211_chan_def *chandef)
785 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
787 spin_lock_bh(&sc->chan_lock);
789 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
790 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
792 ctx->chandef = *chandef;
794 sc->sched.offchannel_pending = true;
795 sc->sched.wait_switch = true;
796 sc->sched.offchannel_duration =
797 jiffies_to_usecs(sc->offchannel.duration) +
798 sc->sched.channel_switch_time;
800 spin_unlock_bh(&sc->chan_lock);
801 ath_dbg(common, CHAN_CTX,
802 "Set offchannel_pending to true\n");
808 ctx->chandef = *chandef;
809 ath_dbg(common, CHAN_CTX,
810 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
813 if (sc->next_chan == &sc->offchannel.chan) {
814 sc->sched.offchannel_duration =
815 jiffies_to_usecs(sc->offchannel.duration) +
816 sc->sched.channel_switch_time;
819 ath_dbg(common, CHAN_CTX,
820 "Offchannel duration for chan %d MHz : %u\n",
821 chandef->center_freq1,
822 sc->sched.offchannel_duration);
825 spin_unlock_bh(&sc->chan_lock);
826 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
829 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
830 struct ieee80211_channel *chan)
832 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
833 struct cfg80211_chan_def chandef;
835 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
836 ath_dbg(common, CHAN_CTX,
837 "Channel definition created: %d MHz\n", chandef.center_freq1);
839 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
842 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
845 struct ath_chanctx *ctx;
847 ath_for_each_chanctx(sc, ctx) {
848 if (!ctx->assigned || list_empty(&ctx->vifs))
850 if (active && !ctx->active)
853 if (ctx->switch_after_beacon)
857 return &sc->chanctx[0];
861 ath_scan_next_channel(struct ath_softc *sc)
863 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
864 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
865 struct ieee80211_channel *chan;
867 if (sc->offchannel.scan_idx >= req->n_channels) {
868 ath_dbg(common, CHAN_CTX,
869 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
870 "scan_idx: %d, n_channels: %d\n",
871 sc->offchannel.scan_idx,
874 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
875 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
880 ath_dbg(common, CHAN_CTX,
881 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
882 sc->offchannel.scan_idx);
884 chan = req->channels[sc->offchannel.scan_idx++];
885 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
886 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
888 ath_chanctx_offchan_switch(sc, chan);
891 void ath_offchannel_next(struct ath_softc *sc)
893 struct ieee80211_vif *vif;
895 if (sc->offchannel.scan_req) {
896 vif = sc->offchannel.scan_vif;
897 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
898 ath_scan_next_channel(sc);
899 } else if (sc->offchannel.roc_vif) {
900 vif = sc->offchannel.roc_vif;
901 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
902 sc->offchannel.duration =
903 msecs_to_jiffies(sc->offchannel.roc_duration);
904 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
905 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
907 spin_lock_bh(&sc->chan_lock);
908 sc->sched.offchannel_pending = false;
909 sc->sched.wait_switch = false;
910 spin_unlock_bh(&sc->chan_lock);
912 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
914 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
920 void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
922 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
924 sc->offchannel.roc_vif = NULL;
925 sc->offchannel.roc_chan = NULL;
928 case ATH_ROC_COMPLETE_ABORT:
929 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
930 ieee80211_remain_on_channel_expired(sc->hw);
932 case ATH_ROC_COMPLETE_EXPIRE:
933 ath_dbg(common, CHAN_CTX, "RoC expired\n");
934 ieee80211_remain_on_channel_expired(sc->hw);
936 case ATH_ROC_COMPLETE_CANCEL:
937 ath_dbg(common, CHAN_CTX, "RoC canceled\n");
941 ath_offchannel_next(sc);
942 ath9k_ps_restore(sc);
945 void ath_scan_complete(struct ath_softc *sc, bool abort)
947 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
950 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
952 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
954 sc->offchannel.scan_req = NULL;
955 sc->offchannel.scan_vif = NULL;
956 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
957 ieee80211_scan_completed(sc->hw, abort);
958 clear_bit(ATH_OP_SCANNING, &common->op_flags);
959 spin_lock_bh(&sc->chan_lock);
960 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
961 sc->sched.force_noa_update = true;
962 spin_unlock_bh(&sc->chan_lock);
963 ath_offchannel_next(sc);
964 ath9k_ps_restore(sc);
967 static void ath_scan_send_probe(struct ath_softc *sc,
968 struct cfg80211_ssid *ssid)
970 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
971 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
972 struct ath_tx_control txctl = {};
974 struct ieee80211_tx_info *info;
975 int band = sc->offchannel.chan.chandef.chan->band;
977 skb = ieee80211_probereq_get(sc->hw, vif->addr,
978 ssid->ssid, ssid->ssid_len, req->ie_len);
982 info = IEEE80211_SKB_CB(skb);
984 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
987 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
989 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
991 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
994 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
995 txctl.force_channel = true;
996 if (ath_tx_start(sc->hw, skb, &txctl))
1002 ieee80211_free_txskb(sc->hw, skb);
1005 static void ath_scan_channel_start(struct ath_softc *sc)
1007 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1008 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1011 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1013 for (i = 0; i < req->n_ssids; i++)
1014 ath_scan_send_probe(sc, &req->ssids[i]);
1018 ath_dbg(common, CHAN_CTX,
1019 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1021 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1022 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1025 static void ath_chanctx_timer(unsigned long data)
1027 struct ath_softc *sc = (struct ath_softc *) data;
1028 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1030 ath_dbg(common, CHAN_CTX,
1031 "Channel context timer invoked\n");
1033 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1036 static void ath_offchannel_timer(unsigned long data)
1038 struct ath_softc *sc = (struct ath_softc *)data;
1039 struct ath_chanctx *ctx;
1040 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1042 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1043 __func__, offchannel_state_string(sc->offchannel.state));
1045 switch (sc->offchannel.state) {
1046 case ATH_OFFCHANNEL_PROBE_WAIT:
1047 if (!sc->offchannel.scan_req)
1050 /* get first active channel context */
1051 ctx = ath_chanctx_get_oper_chan(sc, true);
1053 ath_dbg(common, CHAN_CTX,
1054 "Switch to oper/active context, "
1055 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1057 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1058 ath_chanctx_switch(sc, ctx, NULL);
1059 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1063 case ATH_OFFCHANNEL_SUSPEND:
1064 if (!sc->offchannel.scan_req)
1067 ath_scan_next_channel(sc);
1069 case ATH_OFFCHANNEL_ROC_START:
1070 case ATH_OFFCHANNEL_ROC_WAIT:
1071 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1072 ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
1080 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1083 struct ieee80211_vif *vif = avp->vif;
1084 struct ieee80211_sta *sta = NULL;
1085 struct ieee80211_hdr_3addr *nullfunc;
1086 struct ath_tx_control txctl;
1087 struct sk_buff *skb;
1088 int band = sc->cur_chan->chandef.chan->band;
1090 switch (vif->type) {
1091 case NL80211_IFTYPE_STATION:
1095 skb = ieee80211_nullfunc_get(sc->hw, vif);
1099 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1101 nullfunc->frame_control |=
1102 cpu_to_le16(IEEE80211_FCTL_PM);
1105 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1106 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1107 dev_kfree_skb_any(skb);
1115 memset(&txctl, 0, sizeof(txctl));
1116 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1118 txctl.force_channel = true;
1119 if (ath_tx_start(sc->hw, skb, &txctl)) {
1120 ieee80211_free_txskb(sc->hw, skb);
1128 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1130 struct ath_vif *avp;
1134 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1135 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1143 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1145 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1147 if (sc->cur_chan == &sc->offchannel.chan)
1150 switch (sc->sched.state) {
1151 case ATH_CHANCTX_STATE_SWITCH:
1153 case ATH_CHANCTX_STATE_IDLE:
1154 if (!sc->cur_chan->switch_after_beacon)
1157 ath_dbg(common, CHAN_CTX,
1158 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1160 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1169 static void ath_offchannel_channel_change(struct ath_softc *sc)
1171 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1173 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1174 __func__, offchannel_state_string(sc->offchannel.state));
1176 switch (sc->offchannel.state) {
1177 case ATH_OFFCHANNEL_PROBE_SEND:
1178 if (!sc->offchannel.scan_req)
1181 if (sc->cur_chan->chandef.chan !=
1182 sc->offchannel.chan.chandef.chan)
1185 ath_scan_channel_start(sc);
1187 case ATH_OFFCHANNEL_IDLE:
1188 if (!sc->offchannel.scan_req)
1191 ath_scan_complete(sc, false);
1193 case ATH_OFFCHANNEL_ROC_START:
1194 if (sc->cur_chan != &sc->offchannel.chan)
1197 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1198 mod_timer(&sc->offchannel.timer,
1199 jiffies + sc->offchannel.duration);
1200 ieee80211_ready_on_channel(sc->hw);
1202 case ATH_OFFCHANNEL_ROC_DONE:
1209 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1211 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1212 struct ath_chanctx *old_ctx;
1214 bool measure_time = false;
1215 bool send_ps = false;
1216 bool queues_stopped = false;
1218 spin_lock_bh(&sc->chan_lock);
1219 if (!sc->next_chan) {
1220 spin_unlock_bh(&sc->chan_lock);
1224 if (!force && ath_chanctx_defer_switch(sc)) {
1225 spin_unlock_bh(&sc->chan_lock);
1229 ath_dbg(common, CHAN_CTX,
1230 "%s: current: %d MHz, next: %d MHz\n",
1232 sc->cur_chan->chandef.center_freq1,
1233 sc->next_chan->chandef.center_freq1);
1235 if (sc->cur_chan != sc->next_chan) {
1236 ath_dbg(common, CHAN_CTX,
1237 "Stopping current chanctx: %d\n",
1238 sc->cur_chan->chandef.center_freq1);
1239 sc->cur_chan->stopped = true;
1240 spin_unlock_bh(&sc->chan_lock);
1242 if (sc->next_chan == &sc->offchannel.chan) {
1243 getrawmonotonic(&ts);
1244 measure_time = true;
1247 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1248 queues_stopped = true;
1250 __ath9k_flush(sc->hw, ~0, true, false, false);
1252 if (ath_chanctx_send_ps_frame(sc, true))
1253 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1254 false, false, false);
1257 spin_lock_bh(&sc->chan_lock);
1259 if (sc->cur_chan != &sc->offchannel.chan) {
1260 getrawmonotonic(&sc->cur_chan->tsf_ts);
1261 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1264 old_ctx = sc->cur_chan;
1265 sc->cur_chan = sc->next_chan;
1266 sc->cur_chan->stopped = false;
1267 sc->next_chan = NULL;
1269 if (!sc->sched.offchannel_pending)
1270 sc->sched.offchannel_duration = 0;
1272 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1273 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1275 spin_unlock_bh(&sc->chan_lock);
1277 if (sc->sc_ah->chip_fullsleep ||
1278 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1279 sizeof(sc->cur_chandef))) {
1280 ath_dbg(common, CHAN_CTX,
1281 "%s: Set channel %d MHz\n",
1282 __func__, sc->cur_chan->chandef.center_freq1);
1283 ath_set_channel(sc);
1285 sc->sched.channel_switch_time =
1286 ath9k_hw_get_tsf_offset(&ts, NULL);
1288 * A reset will ensure that all queues are woken up,
1289 * so there is no need to awaken them again.
1295 ath9k_chanctx_wake_queues(sc, old_ctx);
1298 ath_chanctx_send_ps_frame(sc, false);
1300 ath_offchannel_channel_change(sc);
1301 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1304 static void ath_chanctx_work(struct work_struct *work)
1306 struct ath_softc *sc = container_of(work, struct ath_softc,
1308 mutex_lock(&sc->mutex);
1309 ath_chanctx_set_next(sc, false);
1310 mutex_unlock(&sc->mutex);
1313 void ath9k_offchannel_init(struct ath_softc *sc)
1315 struct ath_chanctx *ctx;
1316 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1317 struct ieee80211_supported_band *sband;
1318 struct ieee80211_channel *chan;
1321 sband = &common->sbands[IEEE80211_BAND_2GHZ];
1322 if (!sband->n_channels)
1323 sband = &common->sbands[IEEE80211_BAND_5GHZ];
1325 chan = &sband->channels[0];
1327 ctx = &sc->offchannel.chan;
1328 INIT_LIST_HEAD(&ctx->vifs);
1329 ctx->txpower = ATH_TXPOWER_MAX;
1330 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1332 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++)
1333 INIT_LIST_HEAD(&ctx->acq[i]);
1335 sc->offchannel.chan.offchannel = true;
1338 void ath9k_init_channel_context(struct ath_softc *sc)
1340 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1342 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1344 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1347 init_completion(&sc->go_beacon);
1350 void ath9k_deinit_channel_context(struct ath_softc *sc)
1352 cancel_work_sync(&sc->chanctx_work);
1355 bool ath9k_is_chanctx_enabled(void)
1357 return (ath9k_use_chanctx == 1);
1360 /********************/
1361 /* Queue management */
1362 /********************/
1364 void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1366 struct ath_hw *ah = sc->sc_ah;
1369 if (ctx == &sc->offchannel.chan) {
1370 ieee80211_stop_queue(sc->hw,
1371 sc->hw->offchannel_tx_hw_queue);
1373 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1374 ieee80211_stop_queue(sc->hw,
1375 ctx->hw_queue_base + i);
1378 if (ah->opmode == NL80211_IFTYPE_AP)
1379 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1383 void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1385 struct ath_hw *ah = sc->sc_ah;
1388 if (ctx == &sc->offchannel.chan) {
1389 ieee80211_wake_queue(sc->hw,
1390 sc->hw->offchannel_tx_hw_queue);
1392 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1393 ieee80211_wake_queue(sc->hw,
1394 ctx->hw_queue_base + i);
1397 if (ah->opmode == NL80211_IFTYPE_AP)
1398 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1405 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1407 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1408 struct ath_hw *ah = sc->sc_ah;
1409 u32 tsf, target_tsf;
1411 if (!avp || !avp->noa.has_next_tsf)
1414 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1416 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1418 target_tsf = avp->noa.next_tsf;
1419 if (!avp->noa.absent)
1420 target_tsf -= ATH_P2P_PS_STOP_TIME;
1422 target_tsf += ATH_P2P_PS_STOP_TIME;
1424 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1425 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1427 ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1428 __func__, avp->noa.absent, tsf, target_tsf,
1429 (target_tsf - tsf) / 1000);
1431 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
1434 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1436 struct ath_vif *avp = (void *)vif->drv_priv;
1439 if (!sc->p2p_ps_timer)
1442 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1445 sc->p2p_ps_vif = avp;
1446 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1447 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1448 ath9k_update_p2p_ps_timer(sc, avp);
1451 static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1453 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1454 u8 switch_time, ctwin;
1457 * Channel switch in multi-channel mode is deferred
1458 * by a quarter beacon interval when handling
1459 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1460 * interface is guaranteed to be discoverable
1461 * for that duration after a TBTT.
1463 switch_time = cur_conf->beacon_interval / 4;
1465 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1466 if (ctwin && (ctwin < switch_time))
1469 if (switch_time < P2P_DEFAULT_CTWIN)
1472 return P2P_DEFAULT_CTWIN;
1475 void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1476 struct sk_buff *skb)
1478 static const u8 noa_ie_hdr[] = {
1479 WLAN_EID_VENDOR_SPECIFIC, /* type */
1481 0x50, 0x6f, 0x9a, /* WFA OUI */
1482 0x09, /* P2P subtype */
1483 0x0c, /* Notice of Absence */
1484 0x00, /* LSB of little-endian len */
1485 0x00, /* MSB of little-endian len */
1488 struct ieee80211_p2p_noa_attr *noa;
1489 int noa_len, noa_desc, i = 0;
1492 if (!avp->offchannel_duration && !avp->noa_duration)
1495 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
1496 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1498 hdr = skb_put(skb, sizeof(noa_ie_hdr));
1499 memcpy(hdr, noa_ie_hdr, sizeof(noa_ie_hdr));
1500 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1503 noa = (void *) skb_put(skb, noa_len);
1504 memset(noa, 0, noa_len);
1506 noa->index = avp->noa_index;
1507 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1508 if (noa->oppps_ctwindow)
1509 noa->oppps_ctwindow |= BIT(7);
1511 if (avp->noa_duration) {
1512 if (avp->periodic_noa) {
1513 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1514 noa->desc[i].count = 255;
1515 noa->desc[i].interval = cpu_to_le32(interval);
1517 noa->desc[i].count = 1;
1520 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1521 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
1525 if (avp->offchannel_duration) {
1526 noa->desc[i].count = 1;
1527 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1528 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1532 void ath9k_p2p_ps_timer(void *priv)
1534 struct ath_softc *sc = priv;
1535 struct ath_vif *avp = sc->p2p_ps_vif;
1536 struct ieee80211_vif *vif;
1537 struct ieee80211_sta *sta;
1538 struct ath_node *an;
1541 del_timer_sync(&sc->sched.timer);
1542 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1543 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1545 if (!avp || avp->chanctx != sc->cur_chan)
1548 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1549 if (!avp->noa.absent)
1550 tsf += ATH_P2P_PS_STOP_TIME;
1552 tsf -= ATH_P2P_PS_STOP_TIME;
1554 if (!avp->noa.has_next_tsf ||
1555 avp->noa.next_tsf - tsf > BIT(31))
1556 ieee80211_update_p2p_noa(&avp->noa, tsf);
1558 ath9k_update_p2p_ps_timer(sc, avp);
1563 sta = ieee80211_find_sta(vif, avp->bssid);
1567 an = (void *) sta->drv_priv;
1568 if (an->sleeping == !!avp->noa.absent)
1571 an->sleeping = avp->noa.absent;
1573 ath_tx_aggr_sleep(sta, sc, an);
1575 ath_tx_aggr_wakeup(sc, an);
1581 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1582 struct ieee80211_vif *vif)
1584 unsigned long flags;
1586 spin_lock_bh(&sc->sc_pcu_lock);
1587 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1588 if (!(sc->ps_flags & PS_BEACON_SYNC))
1589 ath9k_update_p2p_ps(sc, vif);
1590 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1591 spin_unlock_bh(&sc->sc_pcu_lock);
1594 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1597 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1600 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1601 struct ieee80211_vif *vif)
1603 struct ath_vif *avp = (void *)vif->drv_priv;
1605 spin_lock_bh(&sc->sc_pcu_lock);
1606 if (avp == sc->p2p_ps_vif) {
1607 sc->p2p_ps_vif = NULL;
1608 ath9k_update_p2p_ps_timer(sc, NULL);
1610 spin_unlock_bh(&sc->sc_pcu_lock);
1613 int ath9k_init_p2p(struct ath_softc *sc)
1615 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1616 NULL, sc, AR_FIRST_NDP_TIMER);
1617 if (!sc->p2p_ps_timer)
1623 void ath9k_deinit_p2p(struct ath_softc *sc)
1625 if (sc->p2p_ps_timer)
1626 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1629 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */