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[NL80211_BAND_2GHZ];
111 if (!sband->n_channels)
112 sband = &common->sbands[NL80211_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].acq_new);
123 INIT_LIST_HEAD(&ctx->acq[j].acq_old);
124 spin_lock_init(&ctx->acq[j].lock);
129 void ath_chanctx_set_channel(struct ath_softc *sc, struct ath_chanctx *ctx,
130 struct cfg80211_chan_def *chandef)
132 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
135 spin_lock_bh(&sc->chan_lock);
137 memcpy(&ctx->chandef, chandef, sizeof(*chandef));
138 cur_chan = sc->cur_chan == ctx;
139 spin_unlock_bh(&sc->chan_lock);
142 ath_dbg(common, CHAN_CTX,
143 "Current context differs from the new context\n");
150 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
156 struct ath_chanctx* ath_is_go_chanctx_present(struct ath_softc *sc)
158 struct ath_chanctx *ctx;
160 struct ieee80211_vif *vif;
162 spin_lock_bh(&sc->chan_lock);
164 ath_for_each_chanctx(sc, ctx) {
168 list_for_each_entry(avp, &ctx->vifs, list) {
171 if (ieee80211_vif_type_p2p(vif) == NL80211_IFTYPE_P2P_GO) {
172 spin_unlock_bh(&sc->chan_lock);
178 spin_unlock_bh(&sc->chan_lock);
182 /**********************************************************/
183 /* Functions to handle the channel context state machine. */
184 /**********************************************************/
186 static const char *offchannel_state_string(enum ath_offchannel_state state)
189 case_rtn_string(ATH_OFFCHANNEL_IDLE);
190 case_rtn_string(ATH_OFFCHANNEL_PROBE_SEND);
191 case_rtn_string(ATH_OFFCHANNEL_PROBE_WAIT);
192 case_rtn_string(ATH_OFFCHANNEL_SUSPEND);
193 case_rtn_string(ATH_OFFCHANNEL_ROC_START);
194 case_rtn_string(ATH_OFFCHANNEL_ROC_WAIT);
195 case_rtn_string(ATH_OFFCHANNEL_ROC_DONE);
201 static const char *chanctx_event_string(enum ath_chanctx_event ev)
204 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_PREPARE);
205 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_SENT);
206 case_rtn_string(ATH_CHANCTX_EVENT_TSF_TIMER);
207 case_rtn_string(ATH_CHANCTX_EVENT_BEACON_RECEIVED);
208 case_rtn_string(ATH_CHANCTX_EVENT_AUTHORIZED);
209 case_rtn_string(ATH_CHANCTX_EVENT_SWITCH);
210 case_rtn_string(ATH_CHANCTX_EVENT_ASSIGN);
211 case_rtn_string(ATH_CHANCTX_EVENT_UNASSIGN);
212 case_rtn_string(ATH_CHANCTX_EVENT_CHANGE);
213 case_rtn_string(ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
219 static const char *chanctx_state_string(enum ath_chanctx_state state)
222 case_rtn_string(ATH_CHANCTX_STATE_IDLE);
223 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_BEACON);
224 case_rtn_string(ATH_CHANCTX_STATE_WAIT_FOR_TIMER);
225 case_rtn_string(ATH_CHANCTX_STATE_SWITCH);
226 case_rtn_string(ATH_CHANCTX_STATE_FORCE_ACTIVE);
232 static u32 chanctx_event_delta(struct ath_softc *sc)
235 struct timespec ts, *old;
237 getrawmonotonic(&ts);
238 old = &sc->last_event_time;
239 ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
240 ms -= old->tv_sec * 1000 + old->tv_nsec / 1000000;
241 sc->last_event_time = ts;
246 void ath_chanctx_check_active(struct ath_softc *sc, struct ath_chanctx *ctx)
248 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
249 struct ath_chanctx *ictx;
257 if (ctx == &sc->offchannel.chan) {
258 spin_lock_bh(&sc->chan_lock);
260 if (likely(sc->sched.channel_switch_time))
262 usecs_to_jiffies(sc->sched.channel_switch_time);
265 msecs_to_jiffies(10);
267 spin_unlock_bh(&sc->chan_lock);
270 * There is no need to iterate over the
271 * active/assigned channel contexts if
272 * the current context is offchannel.
279 list_for_each_entry(avp, &ctx->vifs, list) {
280 struct ieee80211_vif *vif = avp->vif;
283 case NL80211_IFTYPE_P2P_CLIENT:
284 case NL80211_IFTYPE_STATION:
293 ctx->active = active;
295 ath_for_each_chanctx(sc, ctx) {
296 if (!ctx->assigned || list_empty(&ctx->vifs))
301 spin_lock_bh(&sc->chan_lock);
304 ictx->flush_timeout = HZ / 5;
305 clear_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags);
306 spin_unlock_bh(&sc->chan_lock);
310 ictx->flush_timeout = usecs_to_jiffies(sc->sched.channel_switch_time);
312 if (test_and_set_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags)) {
313 spin_unlock_bh(&sc->chan_lock);
317 spin_unlock_bh(&sc->chan_lock);
319 if (ath9k_is_chanctx_enabled()) {
320 ath_chanctx_event(sc, NULL,
321 ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL);
325 static struct ath_chanctx *
326 ath_chanctx_get_next(struct ath_softc *sc, struct ath_chanctx *ctx)
328 int idx = ctx - &sc->chanctx[0];
330 return &sc->chanctx[!idx];
333 static void ath_chanctx_adjust_tbtt_delta(struct ath_softc *sc)
335 struct ath_chanctx *prev, *cur;
337 u32 cur_tsf, prev_tsf, beacon_int;
340 beacon_int = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
343 prev = ath_chanctx_get_next(sc, cur);
345 if (!prev->switch_after_beacon)
348 getrawmonotonic(&ts);
349 cur_tsf = (u32) cur->tsf_val +
350 ath9k_hw_get_tsf_offset(&cur->tsf_ts, &ts);
352 prev_tsf = prev->last_beacon - (u32) prev->tsf_val + cur_tsf;
353 prev_tsf -= ath9k_hw_get_tsf_offset(&prev->tsf_ts, &ts);
355 /* Adjust the TSF time of the AP chanctx to keep its beacons
356 * at half beacon interval offset relative to the STA chanctx.
358 offset = cur_tsf - prev_tsf;
360 /* Ignore stale data or spurious timestamps */
361 if (offset < 0 || offset > 3 * beacon_int)
364 offset = beacon_int / 2 - (offset % beacon_int);
365 prev->tsf_val += offset;
368 /* Configure the TSF based hardware timer for a channel switch.
369 * Also set up backup software timer, in case the gen timer fails.
370 * This could be caused by a hardware reset.
372 static void ath_chanctx_setup_timer(struct ath_softc *sc, u32 tsf_time)
374 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
375 struct ath_hw *ah = sc->sc_ah;
376 unsigned long timeout;
378 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, tsf_time, 1000000);
379 tsf_time -= ath9k_hw_gettsf32(ah);
380 timeout = msecs_to_jiffies(tsf_time / 1000) + 1;
381 mod_timer(&sc->sched.timer, jiffies + timeout);
383 ath_dbg(common, CHAN_CTX,
384 "Setup chanctx timer with timeout: %d (%d) ms\n",
385 tsf_time / 1000, jiffies_to_msecs(timeout));
388 static void ath_chanctx_handle_bmiss(struct ath_softc *sc,
389 struct ath_chanctx *ctx,
393 * Clear the extend_absence flag if it had been
394 * set during the previous beacon transmission,
395 * since we need to revert to the normal NoA
398 if (ctx->active && sc->sched.extend_absence) {
399 avp->noa_duration = 0;
400 sc->sched.extend_absence = false;
403 /* If at least two consecutive beacons were missed on the STA
404 * chanctx, stay on the STA channel for one extra beacon period,
405 * to resync the timer properly.
407 if (ctx->active && sc->sched.beacon_miss >= 2) {
408 avp->noa_duration = 0;
409 sc->sched.extend_absence = true;
413 static void ath_chanctx_offchannel_noa(struct ath_softc *sc,
414 struct ath_chanctx *ctx,
418 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
421 avp->offchannel_start = tsf_time;
422 avp->offchannel_duration = sc->sched.offchannel_duration;
424 ath_dbg(common, CHAN_CTX,
425 "offchannel noa_duration: %d, noa_start: %u, noa_index: %d\n",
426 avp->offchannel_duration,
427 avp->offchannel_start,
431 * When multiple contexts are active, the NoA
432 * has to be recalculated and advertised after
433 * an offchannel operation.
435 if (ctx->active && avp->noa_duration)
436 avp->noa_duration = 0;
439 static void ath_chanctx_set_periodic_noa(struct ath_softc *sc,
441 struct ath_beacon_config *cur_conf,
445 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
448 avp->noa_start = tsf_time;
450 if (sc->sched.extend_absence)
451 avp->noa_duration = (3 * beacon_int / 2) +
452 sc->sched.channel_switch_time;
455 TU_TO_USEC(cur_conf->beacon_interval) / 2 +
456 sc->sched.channel_switch_time;
458 if (test_bit(ATH_OP_SCANNING, &common->op_flags) ||
459 sc->sched.extend_absence)
460 avp->periodic_noa = false;
462 avp->periodic_noa = true;
464 ath_dbg(common, CHAN_CTX,
465 "noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
472 static void ath_chanctx_set_oneshot_noa(struct ath_softc *sc,
477 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
480 avp->noa_start = tsf_time;
481 avp->periodic_noa = false;
482 avp->oneshot_noa = true;
483 avp->noa_duration = duration + sc->sched.channel_switch_time;
485 ath_dbg(common, CHAN_CTX,
486 "oneshot noa_duration: %d, noa_start: %u, noa_index: %d, periodic: %d\n",
493 void ath_chanctx_event(struct ath_softc *sc, struct ieee80211_vif *vif,
494 enum ath_chanctx_event ev)
496 struct ath_hw *ah = sc->sc_ah;
497 struct ath_common *common = ath9k_hw_common(ah);
498 struct ath_beacon_config *cur_conf;
499 struct ath_vif *avp = NULL;
500 struct ath_chanctx *ctx;
505 avp = (struct ath_vif *) vif->drv_priv;
507 spin_lock_bh(&sc->chan_lock);
509 ath_dbg(common, CHAN_CTX, "cur_chan: %d MHz, event: %s, state: %s, delta: %u ms\n",
510 sc->cur_chan->chandef.center_freq1,
511 chanctx_event_string(ev),
512 chanctx_state_string(sc->sched.state),
513 chanctx_event_delta(sc));
516 case ATH_CHANCTX_EVENT_BEACON_PREPARE:
517 if (avp->offchannel_duration)
518 avp->offchannel_duration = 0;
520 if (avp->oneshot_noa) {
521 avp->noa_duration = 0;
522 avp->oneshot_noa = false;
524 ath_dbg(common, CHAN_CTX,
525 "Clearing oneshot NoA\n");
528 if (avp->chanctx != sc->cur_chan) {
529 ath_dbg(common, CHAN_CTX,
530 "Contexts differ, not preparing beacon\n");
534 if (sc->sched.offchannel_pending && !sc->sched.wait_switch) {
535 sc->sched.offchannel_pending = false;
536 sc->next_chan = &sc->offchannel.chan;
537 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
538 ath_dbg(common, CHAN_CTX,
539 "Setting offchannel_pending to false\n");
542 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
543 if (ctx->active && sc->sched.state == ATH_CHANCTX_STATE_IDLE) {
545 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
546 ath_dbg(common, CHAN_CTX,
547 "Set next context, move chanctx state to WAIT_FOR_BEACON\n");
550 /* if the timer missed its window, use the next interval */
551 if (sc->sched.state == ATH_CHANCTX_STATE_WAIT_FOR_TIMER) {
552 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
553 ath_dbg(common, CHAN_CTX,
554 "Move chanctx state from WAIT_FOR_TIMER to WAIT_FOR_BEACON\n");
557 if (sc->sched.mgd_prepare_tx)
558 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
561 * When a context becomes inactive, for example,
562 * disassociation of a station context, the NoA
563 * attribute needs to be removed from subsequent
566 if (!ctx->active && avp->noa_duration &&
567 sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON) {
568 avp->noa_duration = 0;
569 avp->periodic_noa = false;
571 ath_dbg(common, CHAN_CTX,
572 "Clearing NoA schedule\n");
575 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
578 ath_dbg(common, CHAN_CTX, "Preparing beacon for vif: %pM\n", vif->addr);
580 sc->sched.beacon_pending = true;
581 sc->sched.next_tbtt = REG_READ(ah, AR_NEXT_TBTT_TIMER);
583 cur_conf = &sc->cur_chan->beacon;
584 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
586 /* defer channel switch by a quarter beacon interval */
587 tsf_time = sc->sched.next_tbtt + beacon_int / 4;
588 sc->sched.switch_start_time = tsf_time;
589 sc->cur_chan->last_beacon = sc->sched.next_tbtt;
592 * If an offchannel switch is scheduled to happen after
593 * a beacon transmission, update the NoA with one-shot
594 * values and increment the index.
596 if (sc->next_chan == &sc->offchannel.chan) {
597 ath_chanctx_offchannel_noa(sc, ctx, avp, tsf_time);
601 ath_chanctx_handle_bmiss(sc, ctx, avp);
604 * If a mgd_prepare_tx() has been called by mac80211,
605 * a one-shot NoA needs to be sent. This can happen
606 * with one or more active channel contexts - in both
607 * cases, a new NoA schedule has to be advertised.
609 if (sc->sched.mgd_prepare_tx) {
610 ath_chanctx_set_oneshot_noa(sc, avp, tsf_time,
611 jiffies_to_usecs(HZ / 5));
615 /* Prevent wrap-around issues */
616 if (avp->noa_duration && tsf_time - avp->noa_start > BIT(30))
617 avp->noa_duration = 0;
620 * If multiple contexts are active, start periodic
621 * NoA and increment the index for the first
625 (!avp->noa_duration || sc->sched.force_noa_update))
626 ath_chanctx_set_periodic_noa(sc, avp, cur_conf,
627 tsf_time, beacon_int);
629 if (ctx->active && sc->sched.force_noa_update)
630 sc->sched.force_noa_update = false;
633 case ATH_CHANCTX_EVENT_BEACON_SENT:
634 if (!sc->sched.beacon_pending) {
635 ath_dbg(common, CHAN_CTX,
636 "No pending beacon\n");
640 sc->sched.beacon_pending = false;
642 if (sc->sched.mgd_prepare_tx) {
643 sc->sched.mgd_prepare_tx = false;
644 complete(&sc->go_beacon);
645 ath_dbg(common, CHAN_CTX,
646 "Beacon sent, complete go_beacon\n");
650 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_BEACON)
653 ath_dbg(common, CHAN_CTX,
654 "Move chanctx state to WAIT_FOR_TIMER\n");
656 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
657 ath_chanctx_setup_timer(sc, sc->sched.switch_start_time);
659 case ATH_CHANCTX_EVENT_TSF_TIMER:
660 if (sc->sched.state != ATH_CHANCTX_STATE_WAIT_FOR_TIMER)
663 if (!sc->cur_chan->switch_after_beacon &&
664 sc->sched.beacon_pending)
665 sc->sched.beacon_miss++;
667 ath_dbg(common, CHAN_CTX,
668 "Move chanctx state to SWITCH\n");
670 sc->sched.state = ATH_CHANCTX_STATE_SWITCH;
671 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
673 case ATH_CHANCTX_EVENT_BEACON_RECEIVED:
674 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
675 sc->cur_chan == &sc->offchannel.chan)
678 sc->sched.beacon_pending = false;
679 sc->sched.beacon_miss = 0;
681 if (sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
682 !sc->sched.beacon_adjust ||
683 !sc->cur_chan->tsf_val)
686 ath_chanctx_adjust_tbtt_delta(sc);
688 /* TSF time might have been updated by the incoming beacon,
689 * need update the channel switch timer to reflect the change.
691 tsf_time = sc->sched.switch_start_time;
692 tsf_time -= (u32) sc->cur_chan->tsf_val +
693 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
694 tsf_time += ath9k_hw_gettsf32(ah);
696 sc->sched.beacon_adjust = false;
697 ath_chanctx_setup_timer(sc, tsf_time);
699 case ATH_CHANCTX_EVENT_AUTHORIZED:
700 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE ||
701 avp->chanctx != sc->cur_chan)
704 ath_dbg(common, CHAN_CTX,
705 "Move chanctx state from FORCE_ACTIVE to IDLE\n");
707 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
709 case ATH_CHANCTX_EVENT_SWITCH:
710 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) ||
711 sc->sched.state == ATH_CHANCTX_STATE_FORCE_ACTIVE ||
712 sc->cur_chan->switch_after_beacon ||
713 sc->cur_chan == &sc->offchannel.chan)
716 /* If this is a station chanctx, stay active for a half
717 * beacon period (minus channel switch time)
719 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
720 cur_conf = &sc->cur_chan->beacon;
722 ath_dbg(common, CHAN_CTX,
723 "Move chanctx state to WAIT_FOR_TIMER (event SWITCH)\n");
725 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_TIMER;
726 sc->sched.wait_switch = false;
728 tsf_time = TU_TO_USEC(cur_conf->beacon_interval) / 2;
730 if (sc->sched.extend_absence) {
731 sc->sched.beacon_miss = 0;
735 tsf_time -= sc->sched.channel_switch_time;
736 tsf_time += ath9k_hw_gettsf32(sc->sc_ah);
737 sc->sched.switch_start_time = tsf_time;
739 ath_chanctx_setup_timer(sc, tsf_time);
740 sc->sched.beacon_pending = true;
741 sc->sched.beacon_adjust = true;
743 case ATH_CHANCTX_EVENT_ENABLE_MULTICHANNEL:
744 if (sc->cur_chan == &sc->offchannel.chan ||
745 sc->cur_chan->switch_after_beacon)
748 sc->next_chan = ath_chanctx_get_next(sc, sc->cur_chan);
749 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
751 case ATH_CHANCTX_EVENT_UNASSIGN:
752 if (sc->cur_chan->assigned) {
753 if (sc->next_chan && !sc->next_chan->assigned &&
754 sc->next_chan != &sc->offchannel.chan)
755 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
759 ctx = ath_chanctx_get_next(sc, sc->cur_chan);
760 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
765 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
767 case ATH_CHANCTX_EVENT_ASSIGN:
769 case ATH_CHANCTX_EVENT_CHANGE:
773 spin_unlock_bh(&sc->chan_lock);
776 void ath_chanctx_beacon_sent_ev(struct ath_softc *sc,
777 enum ath_chanctx_event ev)
779 if (sc->sched.beacon_pending)
780 ath_chanctx_event(sc, NULL, ev);
783 void ath_chanctx_beacon_recv_ev(struct ath_softc *sc,
784 enum ath_chanctx_event ev)
786 ath_chanctx_event(sc, NULL, ev);
789 static int ath_scan_channel_duration(struct ath_softc *sc,
790 struct ieee80211_channel *chan)
792 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
794 if (!req->n_ssids || (chan->flags & IEEE80211_CHAN_NO_IR))
795 return (HZ / 9); /* ~110 ms */
797 return (HZ / 16); /* ~60 ms */
800 static void ath_chanctx_switch(struct ath_softc *sc, struct ath_chanctx *ctx,
801 struct cfg80211_chan_def *chandef)
803 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
805 spin_lock_bh(&sc->chan_lock);
807 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags) &&
808 (sc->cur_chan != ctx) && (ctx == &sc->offchannel.chan)) {
810 ctx->chandef = *chandef;
812 sc->sched.offchannel_pending = true;
813 sc->sched.wait_switch = true;
814 sc->sched.offchannel_duration =
815 jiffies_to_usecs(sc->offchannel.duration) +
816 sc->sched.channel_switch_time;
818 spin_unlock_bh(&sc->chan_lock);
819 ath_dbg(common, CHAN_CTX,
820 "Set offchannel_pending to true\n");
826 ctx->chandef = *chandef;
827 ath_dbg(common, CHAN_CTX,
828 "Assigned next_chan to %d MHz\n", chandef->center_freq1);
831 if (sc->next_chan == &sc->offchannel.chan) {
832 sc->sched.offchannel_duration =
833 jiffies_to_usecs(sc->offchannel.duration) +
834 sc->sched.channel_switch_time;
837 ath_dbg(common, CHAN_CTX,
838 "Offchannel duration for chan %d MHz : %u\n",
839 chandef->center_freq1,
840 sc->sched.offchannel_duration);
843 spin_unlock_bh(&sc->chan_lock);
844 ieee80211_queue_work(sc->hw, &sc->chanctx_work);
847 static void ath_chanctx_offchan_switch(struct ath_softc *sc,
848 struct ieee80211_channel *chan)
850 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
851 struct cfg80211_chan_def chandef;
853 cfg80211_chandef_create(&chandef, chan, NL80211_CHAN_NO_HT);
854 ath_dbg(common, CHAN_CTX,
855 "Channel definition created: %d MHz\n", chandef.center_freq1);
857 ath_chanctx_switch(sc, &sc->offchannel.chan, &chandef);
860 static struct ath_chanctx *ath_chanctx_get_oper_chan(struct ath_softc *sc,
863 struct ath_chanctx *ctx;
865 ath_for_each_chanctx(sc, ctx) {
866 if (!ctx->assigned || list_empty(&ctx->vifs))
868 if (active && !ctx->active)
871 if (ctx->switch_after_beacon)
875 return &sc->chanctx[0];
879 ath_scan_next_channel(struct ath_softc *sc)
881 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
882 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
883 struct ieee80211_channel *chan;
885 if (sc->offchannel.scan_idx >= req->n_channels) {
886 ath_dbg(common, CHAN_CTX,
887 "Moving offchannel state to ATH_OFFCHANNEL_IDLE, "
888 "scan_idx: %d, n_channels: %d\n",
889 sc->offchannel.scan_idx,
892 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
893 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
898 ath_dbg(common, CHAN_CTX,
899 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_SEND, scan_idx: %d\n",
900 sc->offchannel.scan_idx);
902 chan = req->channels[sc->offchannel.scan_idx++];
903 sc->offchannel.duration = ath_scan_channel_duration(sc, chan);
904 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
906 ath_chanctx_offchan_switch(sc, chan);
909 void ath_offchannel_next(struct ath_softc *sc)
911 struct ieee80211_vif *vif;
913 if (sc->offchannel.scan_req) {
914 vif = sc->offchannel.scan_vif;
915 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
916 ath_scan_next_channel(sc);
917 } else if (sc->offchannel.roc_vif) {
918 vif = sc->offchannel.roc_vif;
919 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
920 sc->offchannel.duration =
921 msecs_to_jiffies(sc->offchannel.roc_duration);
922 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
923 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
925 spin_lock_bh(&sc->chan_lock);
926 sc->sched.offchannel_pending = false;
927 sc->sched.wait_switch = false;
928 spin_unlock_bh(&sc->chan_lock);
930 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
932 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
938 void ath_roc_complete(struct ath_softc *sc, enum ath_roc_complete_reason reason)
940 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
942 sc->offchannel.roc_vif = NULL;
943 sc->offchannel.roc_chan = NULL;
946 case ATH_ROC_COMPLETE_ABORT:
947 ath_dbg(common, CHAN_CTX, "RoC aborted\n");
948 ieee80211_remain_on_channel_expired(sc->hw);
950 case ATH_ROC_COMPLETE_EXPIRE:
951 ath_dbg(common, CHAN_CTX, "RoC expired\n");
952 ieee80211_remain_on_channel_expired(sc->hw);
954 case ATH_ROC_COMPLETE_CANCEL:
955 ath_dbg(common, CHAN_CTX, "RoC canceled\n");
959 ath_offchannel_next(sc);
960 ath9k_ps_restore(sc);
963 void ath_scan_complete(struct ath_softc *sc, bool abort)
965 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
966 struct cfg80211_scan_info info = {
971 ath_dbg(common, CHAN_CTX, "HW scan aborted\n");
973 ath_dbg(common, CHAN_CTX, "HW scan complete\n");
975 sc->offchannel.scan_req = NULL;
976 sc->offchannel.scan_vif = NULL;
977 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
978 ieee80211_scan_completed(sc->hw, &info);
979 clear_bit(ATH_OP_SCANNING, &common->op_flags);
980 spin_lock_bh(&sc->chan_lock);
981 if (test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
982 sc->sched.force_noa_update = true;
983 spin_unlock_bh(&sc->chan_lock);
984 ath_offchannel_next(sc);
985 ath9k_ps_restore(sc);
988 static void ath_scan_send_probe(struct ath_softc *sc,
989 struct cfg80211_ssid *ssid)
991 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
992 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
993 struct ath_tx_control txctl = {};
995 struct ieee80211_tx_info *info;
996 int band = sc->offchannel.chan.chandef.chan->band;
998 skb = ieee80211_probereq_get(sc->hw, vif->addr,
999 ssid->ssid, ssid->ssid_len, req->ie_len);
1003 info = IEEE80211_SKB_CB(skb);
1005 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
1008 skb_put_data(skb, req->ie, req->ie_len);
1010 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1012 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
1015 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1016 if (ath_tx_start(sc->hw, skb, &txctl))
1022 ieee80211_free_txskb(sc->hw, skb);
1025 static void ath_scan_channel_start(struct ath_softc *sc)
1027 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1028 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
1031 if (!(sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) &&
1033 for (i = 0; i < req->n_ssids; i++)
1034 ath_scan_send_probe(sc, &req->ssids[i]);
1038 ath_dbg(common, CHAN_CTX,
1039 "Moving offchannel state to ATH_OFFCHANNEL_PROBE_WAIT\n");
1041 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
1042 mod_timer(&sc->offchannel.timer, jiffies + sc->offchannel.duration);
1045 static void ath_chanctx_timer(unsigned long data)
1047 struct ath_softc *sc = (struct ath_softc *) data;
1048 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1050 ath_dbg(common, CHAN_CTX,
1051 "Channel context timer invoked\n");
1053 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1056 static void ath_offchannel_timer(unsigned long data)
1058 struct ath_softc *sc = (struct ath_softc *)data;
1059 struct ath_chanctx *ctx;
1060 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1062 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1063 __func__, offchannel_state_string(sc->offchannel.state));
1065 switch (sc->offchannel.state) {
1066 case ATH_OFFCHANNEL_PROBE_WAIT:
1067 if (!sc->offchannel.scan_req)
1070 /* get first active channel context */
1071 ctx = ath_chanctx_get_oper_chan(sc, true);
1073 ath_dbg(common, CHAN_CTX,
1074 "Switch to oper/active context, "
1075 "move offchannel state to ATH_OFFCHANNEL_SUSPEND\n");
1077 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
1078 ath_chanctx_switch(sc, ctx, NULL);
1079 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
1083 case ATH_OFFCHANNEL_SUSPEND:
1084 if (!sc->offchannel.scan_req)
1087 ath_scan_next_channel(sc);
1089 case ATH_OFFCHANNEL_ROC_START:
1090 case ATH_OFFCHANNEL_ROC_WAIT:
1091 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
1092 ath_roc_complete(sc, ATH_ROC_COMPLETE_EXPIRE);
1100 ath_chanctx_send_vif_ps_frame(struct ath_softc *sc, struct ath_vif *avp,
1103 struct ieee80211_vif *vif = avp->vif;
1104 struct ieee80211_sta *sta = NULL;
1105 struct ieee80211_hdr_3addr *nullfunc;
1106 struct ath_tx_control txctl;
1107 struct sk_buff *skb;
1108 int band = sc->cur_chan->chandef.chan->band;
1110 switch (vif->type) {
1111 case NL80211_IFTYPE_STATION:
1115 skb = ieee80211_nullfunc_get(sc->hw, vif, false);
1119 nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
1121 nullfunc->frame_control |=
1122 cpu_to_le16(IEEE80211_FCTL_PM);
1125 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
1126 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, &sta)) {
1127 dev_kfree_skb_any(skb);
1135 memset(&txctl, 0, sizeof(txctl));
1136 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
1138 if (ath_tx_start(sc->hw, skb, &txctl)) {
1139 ieee80211_free_txskb(sc->hw, skb);
1147 ath_chanctx_send_ps_frame(struct ath_softc *sc, bool powersave)
1149 struct ath_vif *avp;
1153 list_for_each_entry(avp, &sc->cur_chan->vifs, list) {
1154 if (ath_chanctx_send_vif_ps_frame(sc, avp, powersave))
1162 static bool ath_chanctx_defer_switch(struct ath_softc *sc)
1164 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1166 if (sc->cur_chan == &sc->offchannel.chan)
1169 switch (sc->sched.state) {
1170 case ATH_CHANCTX_STATE_SWITCH:
1172 case ATH_CHANCTX_STATE_IDLE:
1173 if (!sc->cur_chan->switch_after_beacon)
1176 ath_dbg(common, CHAN_CTX,
1177 "Defer switch, set chanctx state to WAIT_FOR_BEACON\n");
1179 sc->sched.state = ATH_CHANCTX_STATE_WAIT_FOR_BEACON;
1188 static void ath_offchannel_channel_change(struct ath_softc *sc)
1190 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1192 ath_dbg(common, CHAN_CTX, "%s: offchannel state: %s\n",
1193 __func__, offchannel_state_string(sc->offchannel.state));
1195 switch (sc->offchannel.state) {
1196 case ATH_OFFCHANNEL_PROBE_SEND:
1197 if (!sc->offchannel.scan_req)
1200 if (sc->cur_chan->chandef.chan !=
1201 sc->offchannel.chan.chandef.chan)
1204 ath_scan_channel_start(sc);
1206 case ATH_OFFCHANNEL_IDLE:
1207 if (!sc->offchannel.scan_req)
1210 ath_scan_complete(sc, false);
1212 case ATH_OFFCHANNEL_ROC_START:
1213 if (sc->cur_chan != &sc->offchannel.chan)
1216 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
1217 mod_timer(&sc->offchannel.timer,
1218 jiffies + sc->offchannel.duration);
1219 ieee80211_ready_on_channel(sc->hw);
1221 case ATH_OFFCHANNEL_ROC_DONE:
1228 void ath_chanctx_set_next(struct ath_softc *sc, bool force)
1230 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1231 struct ath_chanctx *old_ctx;
1233 bool measure_time = false;
1234 bool send_ps = false;
1235 bool queues_stopped = false;
1237 spin_lock_bh(&sc->chan_lock);
1238 if (!sc->next_chan) {
1239 spin_unlock_bh(&sc->chan_lock);
1243 if (!force && ath_chanctx_defer_switch(sc)) {
1244 spin_unlock_bh(&sc->chan_lock);
1248 ath_dbg(common, CHAN_CTX,
1249 "%s: current: %d MHz, next: %d MHz\n",
1251 sc->cur_chan->chandef.center_freq1,
1252 sc->next_chan->chandef.center_freq1);
1254 if (sc->cur_chan != sc->next_chan) {
1255 ath_dbg(common, CHAN_CTX,
1256 "Stopping current chanctx: %d\n",
1257 sc->cur_chan->chandef.center_freq1);
1258 sc->cur_chan->stopped = true;
1259 spin_unlock_bh(&sc->chan_lock);
1261 if (sc->next_chan == &sc->offchannel.chan) {
1262 getrawmonotonic(&ts);
1263 measure_time = true;
1266 ath9k_chanctx_stop_queues(sc, sc->cur_chan);
1267 queues_stopped = true;
1269 __ath9k_flush(sc->hw, ~0, true, false, false);
1271 if (ath_chanctx_send_ps_frame(sc, true))
1272 __ath9k_flush(sc->hw, BIT(IEEE80211_AC_VO),
1273 false, false, false);
1276 spin_lock_bh(&sc->chan_lock);
1278 if (sc->cur_chan != &sc->offchannel.chan) {
1279 getrawmonotonic(&sc->cur_chan->tsf_ts);
1280 sc->cur_chan->tsf_val = ath9k_hw_gettsf64(sc->sc_ah);
1283 old_ctx = sc->cur_chan;
1284 sc->cur_chan = sc->next_chan;
1285 sc->cur_chan->stopped = false;
1286 sc->next_chan = NULL;
1288 if (!sc->sched.offchannel_pending)
1289 sc->sched.offchannel_duration = 0;
1291 if (sc->sched.state != ATH_CHANCTX_STATE_FORCE_ACTIVE)
1292 sc->sched.state = ATH_CHANCTX_STATE_IDLE;
1294 spin_unlock_bh(&sc->chan_lock);
1296 if (sc->sc_ah->chip_fullsleep ||
1297 memcmp(&sc->cur_chandef, &sc->cur_chan->chandef,
1298 sizeof(sc->cur_chandef))) {
1299 ath_dbg(common, CHAN_CTX,
1300 "%s: Set channel %d MHz\n",
1301 __func__, sc->cur_chan->chandef.center_freq1);
1302 ath_set_channel(sc);
1304 sc->sched.channel_switch_time =
1305 ath9k_hw_get_tsf_offset(&ts, NULL);
1307 * A reset will ensure that all queues are woken up,
1308 * so there is no need to awaken them again.
1314 ath9k_chanctx_wake_queues(sc, old_ctx);
1317 ath_chanctx_send_ps_frame(sc, false);
1319 ath_offchannel_channel_change(sc);
1320 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_SWITCH);
1323 static void ath_chanctx_work(struct work_struct *work)
1325 struct ath_softc *sc = container_of(work, struct ath_softc,
1327 mutex_lock(&sc->mutex);
1328 ath_chanctx_set_next(sc, false);
1329 mutex_unlock(&sc->mutex);
1332 void ath9k_offchannel_init(struct ath_softc *sc)
1334 struct ath_chanctx *ctx;
1335 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1336 struct ieee80211_supported_band *sband;
1337 struct ieee80211_channel *chan;
1340 sband = &common->sbands[NL80211_BAND_2GHZ];
1341 if (!sband->n_channels)
1342 sband = &common->sbands[NL80211_BAND_5GHZ];
1344 chan = &sband->channels[0];
1346 ctx = &sc->offchannel.chan;
1347 INIT_LIST_HEAD(&ctx->vifs);
1348 ctx->txpower = ATH_TXPOWER_MAX;
1349 cfg80211_chandef_create(&ctx->chandef, chan, NL80211_CHAN_HT20);
1351 for (i = 0; i < ARRAY_SIZE(ctx->acq); i++) {
1352 INIT_LIST_HEAD(&ctx->acq[i].acq_new);
1353 INIT_LIST_HEAD(&ctx->acq[i].acq_old);
1354 spin_lock_init(&ctx->acq[i].lock);
1357 sc->offchannel.chan.offchannel = true;
1360 void ath9k_init_channel_context(struct ath_softc *sc)
1362 INIT_WORK(&sc->chanctx_work, ath_chanctx_work);
1364 setup_timer(&sc->offchannel.timer, ath_offchannel_timer,
1366 setup_timer(&sc->sched.timer, ath_chanctx_timer,
1369 init_completion(&sc->go_beacon);
1372 void ath9k_deinit_channel_context(struct ath_softc *sc)
1374 cancel_work_sync(&sc->chanctx_work);
1377 bool ath9k_is_chanctx_enabled(void)
1379 return (ath9k_use_chanctx == 1);
1382 /********************/
1383 /* Queue management */
1384 /********************/
1386 void ath9k_chanctx_stop_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1388 struct ath_hw *ah = sc->sc_ah;
1391 if (ctx == &sc->offchannel.chan) {
1392 ieee80211_stop_queue(sc->hw,
1393 sc->hw->offchannel_tx_hw_queue);
1395 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1396 ieee80211_stop_queue(sc->hw,
1397 ctx->hw_queue_base + i);
1400 if (ah->opmode == NL80211_IFTYPE_AP)
1401 ieee80211_stop_queue(sc->hw, sc->hw->queues - 2);
1405 void ath9k_chanctx_wake_queues(struct ath_softc *sc, struct ath_chanctx *ctx)
1407 struct ath_hw *ah = sc->sc_ah;
1410 if (ctx == &sc->offchannel.chan) {
1411 ieee80211_wake_queue(sc->hw,
1412 sc->hw->offchannel_tx_hw_queue);
1414 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1415 ieee80211_wake_queue(sc->hw,
1416 ctx->hw_queue_base + i);
1419 if (ah->opmode == NL80211_IFTYPE_AP)
1420 ieee80211_wake_queue(sc->hw, sc->hw->queues - 2);
1427 static void ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1429 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1430 struct ath_hw *ah = sc->sc_ah;
1431 u32 tsf, target_tsf;
1433 if (!avp || !avp->noa.has_next_tsf)
1436 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1438 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1440 target_tsf = avp->noa.next_tsf;
1441 if (!avp->noa.absent)
1442 target_tsf -= ATH_P2P_PS_STOP_TIME;
1444 target_tsf += ATH_P2P_PS_STOP_TIME;
1446 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1447 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1449 ath_dbg(common, CHAN_CTX, "%s absent %d tsf 0x%08X next_tsf 0x%08X (%dms)\n",
1450 __func__, avp->noa.absent, tsf, target_tsf,
1451 (target_tsf - tsf) / 1000);
1453 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, target_tsf, 1000000);
1456 static void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1458 struct ath_vif *avp = (void *)vif->drv_priv;
1461 if (!sc->p2p_ps_timer)
1464 if (vif->type != NL80211_IFTYPE_STATION)
1467 sc->p2p_ps_vif = avp;
1469 if (sc->ps_flags & PS_BEACON_SYNC)
1472 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1473 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1474 ath9k_update_p2p_ps_timer(sc, avp);
1477 static u8 ath9k_get_ctwin(struct ath_softc *sc, struct ath_vif *avp)
1479 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
1480 u8 switch_time, ctwin;
1483 * Channel switch in multi-channel mode is deferred
1484 * by a quarter beacon interval when handling
1485 * ATH_CHANCTX_EVENT_BEACON_PREPARE, so the P2P-GO
1486 * interface is guaranteed to be discoverable
1487 * for that duration after a TBTT.
1489 switch_time = cur_conf->beacon_interval / 4;
1491 ctwin = avp->vif->bss_conf.p2p_noa_attr.oppps_ctwindow;
1492 if (ctwin && (ctwin < switch_time))
1495 if (switch_time < P2P_DEFAULT_CTWIN)
1498 return P2P_DEFAULT_CTWIN;
1501 void ath9k_beacon_add_noa(struct ath_softc *sc, struct ath_vif *avp,
1502 struct sk_buff *skb)
1504 static const u8 noa_ie_hdr[] = {
1505 WLAN_EID_VENDOR_SPECIFIC, /* type */
1507 0x50, 0x6f, 0x9a, /* WFA OUI */
1508 0x09, /* P2P subtype */
1509 0x0c, /* Notice of Absence */
1510 0x00, /* LSB of little-endian len */
1511 0x00, /* MSB of little-endian len */
1514 struct ieee80211_p2p_noa_attr *noa;
1515 int noa_len, noa_desc, i = 0;
1518 if (!avp->offchannel_duration && !avp->noa_duration)
1521 noa_desc = !!avp->offchannel_duration + !!avp->noa_duration;
1522 noa_len = 2 + sizeof(struct ieee80211_p2p_noa_desc) * noa_desc;
1524 hdr = skb_put_data(skb, noa_ie_hdr, sizeof(noa_ie_hdr));
1525 hdr[1] = sizeof(noa_ie_hdr) + noa_len - 2;
1528 noa = skb_put_zero(skb, noa_len);
1530 noa->index = avp->noa_index;
1531 noa->oppps_ctwindow = ath9k_get_ctwin(sc, avp);
1532 if (noa->oppps_ctwindow)
1533 noa->oppps_ctwindow |= BIT(7);
1535 if (avp->noa_duration) {
1536 if (avp->periodic_noa) {
1537 u32 interval = TU_TO_USEC(sc->cur_chan->beacon.beacon_interval);
1538 noa->desc[i].count = 255;
1539 noa->desc[i].interval = cpu_to_le32(interval);
1541 noa->desc[i].count = 1;
1544 noa->desc[i].start_time = cpu_to_le32(avp->noa_start);
1545 noa->desc[i].duration = cpu_to_le32(avp->noa_duration);
1549 if (avp->offchannel_duration) {
1550 noa->desc[i].count = 1;
1551 noa->desc[i].start_time = cpu_to_le32(avp->offchannel_start);
1552 noa->desc[i].duration = cpu_to_le32(avp->offchannel_duration);
1556 void ath9k_p2p_ps_timer(void *priv)
1558 struct ath_softc *sc = priv;
1559 struct ath_vif *avp = sc->p2p_ps_vif;
1560 struct ieee80211_vif *vif;
1561 struct ieee80211_sta *sta;
1562 struct ath_node *an;
1565 del_timer_sync(&sc->sched.timer);
1566 ath9k_hw_gen_timer_stop(sc->sc_ah, sc->p2p_ps_timer);
1567 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_TSF_TIMER);
1569 if (!avp || avp->chanctx != sc->cur_chan)
1572 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1573 if (!avp->noa.absent)
1574 tsf += ATH_P2P_PS_STOP_TIME;
1576 tsf -= ATH_P2P_PS_STOP_TIME;
1578 if (!avp->noa.has_next_tsf ||
1579 avp->noa.next_tsf - tsf > BIT(31))
1580 ieee80211_update_p2p_noa(&avp->noa, tsf);
1582 ath9k_update_p2p_ps_timer(sc, avp);
1587 sta = ieee80211_find_sta(vif, avp->bssid);
1591 an = (void *) sta->drv_priv;
1592 if (an->sleeping == !!avp->noa.absent)
1595 an->sleeping = avp->noa.absent;
1597 ath_tx_aggr_sleep(sta, sc, an);
1599 ath_tx_aggr_wakeup(sc, an);
1605 void ath9k_p2p_bss_info_changed(struct ath_softc *sc,
1606 struct ieee80211_vif *vif)
1608 unsigned long flags;
1610 spin_lock_bh(&sc->sc_pcu_lock);
1611 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1612 ath9k_update_p2p_ps(sc, vif);
1613 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1614 spin_unlock_bh(&sc->sc_pcu_lock);
1617 void ath9k_p2p_beacon_sync(struct ath_softc *sc)
1620 ath9k_update_p2p_ps(sc, sc->p2p_ps_vif->vif);
1623 void ath9k_p2p_remove_vif(struct ath_softc *sc,
1624 struct ieee80211_vif *vif)
1626 struct ath_vif *avp = (void *)vif->drv_priv;
1628 spin_lock_bh(&sc->sc_pcu_lock);
1629 if (avp == sc->p2p_ps_vif) {
1630 sc->p2p_ps_vif = NULL;
1631 ath9k_update_p2p_ps_timer(sc, NULL);
1633 spin_unlock_bh(&sc->sc_pcu_lock);
1636 int ath9k_init_p2p(struct ath_softc *sc)
1638 sc->p2p_ps_timer = ath_gen_timer_alloc(sc->sc_ah, ath9k_p2p_ps_timer,
1639 NULL, sc, AR_FIRST_NDP_TIMER);
1640 if (!sc->p2p_ps_timer)
1646 void ath9k_deinit_p2p(struct ath_softc *sc)
1648 if (sc->p2p_ps_timer)
1649 ath_gen_timer_free(sc->sc_ah, sc->p2p_ps_timer);
1652 #endif /* CONFIG_ATH9K_CHANNEL_CONTEXT */