2 * Copyright (c) 2008-2011 Atheros Communications 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.
17 #include <linux/nl80211.h>
18 #include <linux/delay.h>
22 u8 ath9k_parse_mpdudensity(u8 mpdudensity)
25 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
26 * 0 for no restriction
35 switch (mpdudensity) {
41 /* Our lower layer calculations limit our precision to
57 static bool ath9k_has_pending_frames(struct ath_softc *sc, struct ath_txq *txq,
62 spin_lock_bh(&txq->axq_lock);
72 if (txq->mac80211_qnum >= 0) {
75 acq = &sc->cur_chan->acq[txq->mac80211_qnum];
76 if (!list_empty(&acq->acq_new) || !list_empty(&acq->acq_old))
80 spin_unlock_bh(&txq->axq_lock);
84 static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
89 spin_lock_irqsave(&sc->sc_pm_lock, flags);
90 ret = ath9k_hw_setpower(sc->sc_ah, mode);
91 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
96 void ath_ps_full_sleep(unsigned long data)
98 struct ath_softc *sc = (struct ath_softc *) data;
99 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
102 spin_lock(&common->cc_lock);
103 ath_hw_cycle_counters_update(common);
104 spin_unlock(&common->cc_lock);
106 ath9k_hw_setrxabort(sc->sc_ah, 1);
107 ath9k_hw_stopdmarecv(sc->sc_ah, &reset);
109 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
112 void ath9k_ps_wakeup(struct ath_softc *sc)
114 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
116 enum ath9k_power_mode power_mode;
118 spin_lock_irqsave(&sc->sc_pm_lock, flags);
119 if (++sc->ps_usecount != 1)
122 del_timer_sync(&sc->sleep_timer);
123 power_mode = sc->sc_ah->power_mode;
124 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
127 * While the hardware is asleep, the cycle counters contain no
128 * useful data. Better clear them now so that they don't mess up
129 * survey data results.
131 if (power_mode != ATH9K_PM_AWAKE) {
132 spin_lock(&common->cc_lock);
133 ath_hw_cycle_counters_update(common);
134 memset(&common->cc_survey, 0, sizeof(common->cc_survey));
135 memset(&common->cc_ani, 0, sizeof(common->cc_ani));
136 spin_unlock(&common->cc_lock);
140 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
143 void ath9k_ps_restore(struct ath_softc *sc)
145 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
146 enum ath9k_power_mode mode;
149 spin_lock_irqsave(&sc->sc_pm_lock, flags);
150 if (--sc->ps_usecount != 0)
154 mod_timer(&sc->sleep_timer, jiffies + HZ / 10);
158 if (sc->ps_enabled &&
159 !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
161 PS_WAIT_FOR_PSPOLL_DATA |
164 mode = ATH9K_PM_NETWORK_SLEEP;
165 if (ath9k_hw_btcoex_is_enabled(sc->sc_ah))
166 ath9k_btcoex_stop_gen_timer(sc);
171 spin_lock(&common->cc_lock);
172 ath_hw_cycle_counters_update(common);
173 spin_unlock(&common->cc_lock);
175 ath9k_hw_setpower(sc->sc_ah, mode);
178 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
181 static void __ath_cancel_work(struct ath_softc *sc)
183 cancel_work_sync(&sc->paprd_work);
184 cancel_delayed_work_sync(&sc->hw_check_work);
185 cancel_delayed_work_sync(&sc->hw_pll_work);
187 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
188 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
189 cancel_work_sync(&sc->mci_work);
193 void ath_cancel_work(struct ath_softc *sc)
195 __ath_cancel_work(sc);
196 cancel_work_sync(&sc->hw_reset_work);
199 void ath_restart_work(struct ath_softc *sc)
201 ieee80211_queue_delayed_work(sc->hw, &sc->hw_check_work,
202 ATH_HW_CHECK_POLL_INT);
204 if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
205 ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
206 msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
211 static bool ath_prepare_reset(struct ath_softc *sc)
213 struct ath_hw *ah = sc->sc_ah;
216 ieee80211_stop_queues(sc->hw);
218 ath9k_hw_disable_interrupts(ah);
220 if (AR_SREV_9300_20_OR_LATER(ah)) {
221 ret &= ath_stoprecv(sc);
222 ret &= ath_drain_all_txq(sc);
224 ret &= ath_drain_all_txq(sc);
225 ret &= ath_stoprecv(sc);
231 static bool ath_complete_reset(struct ath_softc *sc, bool start)
233 struct ath_hw *ah = sc->sc_ah;
234 struct ath_common *common = ath9k_hw_common(ah);
237 ath9k_calculate_summary_state(sc, sc->cur_chan);
239 ath9k_cmn_update_txpow(ah, sc->cur_chan->cur_txpower,
240 sc->cur_chan->txpower,
241 &sc->cur_chan->cur_txpower);
242 clear_bit(ATH_OP_HW_RESET, &common->op_flags);
244 if (!sc->cur_chan->offchannel && start) {
245 /* restore per chanctx TSF timer */
246 if (sc->cur_chan->tsf_val) {
249 offset = ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts,
251 ath9k_hw_settsf64(ah, sc->cur_chan->tsf_val + offset);
255 if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
258 if (ah->opmode == NL80211_IFTYPE_STATION &&
259 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
260 spin_lock_irqsave(&sc->sc_pm_lock, flags);
261 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
262 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
264 ath9k_set_beacon(sc);
267 ath_restart_work(sc);
268 ath_txq_schedule_all(sc);
273 ath9k_hw_set_interrupts(ah);
274 ath9k_hw_enable_interrupts(ah);
275 ieee80211_wake_queues(sc->hw);
276 ath9k_p2p_ps_timer(sc);
281 static int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
283 struct ath_hw *ah = sc->sc_ah;
284 struct ath_common *common = ath9k_hw_common(ah);
285 struct ath9k_hw_cal_data *caldata = NULL;
289 __ath_cancel_work(sc);
291 disable_irq(sc->irq);
292 tasklet_disable(&sc->intr_tq);
293 tasklet_disable(&sc->bcon_tasklet);
294 spin_lock_bh(&sc->sc_pcu_lock);
296 if (!sc->cur_chan->offchannel) {
298 caldata = &sc->cur_chan->caldata;
308 hchan = ath9k_cmn_get_channel(sc->hw, ah, &sc->cur_chan->chandef);
311 if (!ath_prepare_reset(sc))
314 if (ath9k_is_chanctx_enabled())
317 spin_lock_bh(&sc->chan_lock);
318 sc->cur_chandef = sc->cur_chan->chandef;
319 spin_unlock_bh(&sc->chan_lock);
321 ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
322 hchan->channel, IS_CHAN_HT40(hchan), fastcc);
324 r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
327 "Unable to reset channel, reset status %d\n", r);
329 ath9k_hw_enable_interrupts(ah);
330 ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
335 if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
336 sc->cur_chan->offchannel)
337 ath9k_mci_set_txpower(sc, true, false);
339 if (!ath_complete_reset(sc, true))
344 spin_unlock_bh(&sc->sc_pcu_lock);
345 tasklet_enable(&sc->bcon_tasklet);
346 tasklet_enable(&sc->intr_tq);
351 static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
352 struct ieee80211_vif *vif)
355 an = (struct ath_node *)sta->drv_priv;
360 memset(&an->key_idx, 0, sizeof(an->key_idx));
362 ath_tx_node_init(sc, an);
364 ath_dynack_node_init(sc->sc_ah, an);
367 static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
369 struct ath_node *an = (struct ath_node *)sta->drv_priv;
370 ath_tx_node_cleanup(sc, an);
372 ath_dynack_node_deinit(sc->sc_ah, an);
375 void ath9k_tasklet(unsigned long data)
377 struct ath_softc *sc = (struct ath_softc *)data;
378 struct ath_hw *ah = sc->sc_ah;
379 struct ath_common *common = ath9k_hw_common(ah);
380 enum ath_reset_type type;
385 spin_lock_irqsave(&sc->intr_lock, flags);
386 status = sc->intrstatus;
388 spin_unlock_irqrestore(&sc->intr_lock, flags);
391 spin_lock(&sc->sc_pcu_lock);
393 if (status & ATH9K_INT_FATAL) {
394 type = RESET_TYPE_FATAL_INT;
395 ath9k_queue_reset(sc, type);
396 ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
400 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
401 (status & ATH9K_INT_BB_WATCHDOG)) {
402 spin_lock(&common->cc_lock);
403 ath_hw_cycle_counters_update(common);
404 ar9003_hw_bb_watchdog_dbg_info(ah);
405 spin_unlock(&common->cc_lock);
407 if (ar9003_hw_bb_watchdog_check(ah)) {
408 type = RESET_TYPE_BB_WATCHDOG;
409 ath9k_queue_reset(sc, type);
411 ath_dbg(common, RESET,
412 "BB_WATCHDOG: Skipping interrupts\n");
417 if (status & ATH9K_INT_GTT) {
420 if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
421 type = RESET_TYPE_TX_GTT;
422 ath9k_queue_reset(sc, type);
423 ath_dbg(common, RESET,
424 "GTT: Skipping interrupts\n");
429 spin_lock_irqsave(&sc->sc_pm_lock, flags);
430 if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
432 * TSF sync does not look correct; remain awake to sync with
435 ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
436 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
438 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
440 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
441 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
444 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
446 if (status & rxmask) {
447 /* Check for high priority Rx first */
448 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
449 (status & ATH9K_INT_RXHP))
450 ath_rx_tasklet(sc, 0, true);
452 ath_rx_tasklet(sc, 0, false);
455 if (status & ATH9K_INT_TX) {
456 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
458 * For EDMA chips, TX completion is enabled for the
459 * beacon queue, so if a beacon has been transmitted
460 * successfully after a GTT interrupt, the GTT counter
461 * gets reset to zero here.
465 ath_tx_edma_tasklet(sc);
470 wake_up(&sc->tx_wait);
473 if (status & ATH9K_INT_GENTIMER)
474 ath_gen_timer_isr(sc->sc_ah);
476 ath9k_btcoex_handle_interrupt(sc, status);
478 /* re-enable hardware interrupt */
479 ath9k_hw_resume_interrupts(ah);
481 spin_unlock(&sc->sc_pcu_lock);
482 ath9k_ps_restore(sc);
485 irqreturn_t ath_isr(int irq, void *dev)
487 #define SCHED_INTR ( \
489 ATH9K_INT_BB_WATCHDOG | \
500 ATH9K_INT_GENTIMER | \
503 struct ath_softc *sc = dev;
504 struct ath_hw *ah = sc->sc_ah;
505 struct ath_common *common = ath9k_hw_common(ah);
506 enum ath9k_int status;
511 * The hardware is not ready/present, don't
512 * touch anything. Note this can happen early
513 * on if the IRQ is shared.
515 if (!ah || test_bit(ATH_OP_INVALID, &common->op_flags))
518 /* shared irq, not for us */
519 if (!ath9k_hw_intrpend(ah))
523 * Figure out the reason(s) for the interrupt. Note
524 * that the hal returns a pseudo-ISR that may include
525 * bits we haven't explicitly enabled so we mask the
526 * value to insure we only process bits we requested.
528 ath9k_hw_getisr(ah, &status, &sync_cause); /* NB: clears ISR too */
529 ath9k_debug_sync_cause(sc, sync_cause);
530 status &= ah->imask; /* discard unasked-for bits */
532 if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
536 * If there are no status bits set, then this interrupt was not
537 * for me (should have been caught above).
542 /* Cache the status */
543 spin_lock(&sc->intr_lock);
544 sc->intrstatus |= status;
545 spin_unlock(&sc->intr_lock);
547 if (status & SCHED_INTR)
551 * If a FATAL interrupt is received, we have to reset the chip
554 if (status & ATH9K_INT_FATAL)
557 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
558 (status & ATH9K_INT_BB_WATCHDOG))
561 if (status & ATH9K_INT_SWBA)
562 tasklet_schedule(&sc->bcon_tasklet);
564 if (status & ATH9K_INT_TXURN)
565 ath9k_hw_updatetxtriglevel(ah, true);
567 if (status & ATH9K_INT_RXEOL) {
568 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
569 ath9k_hw_set_interrupts(ah);
572 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
573 if (status & ATH9K_INT_TIM_TIMER) {
574 if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
576 /* Clear RxAbort bit so that we can
578 ath9k_setpower(sc, ATH9K_PM_AWAKE);
579 spin_lock(&sc->sc_pm_lock);
580 ath9k_hw_setrxabort(sc->sc_ah, 0);
581 sc->ps_flags |= PS_WAIT_FOR_BEACON;
582 spin_unlock(&sc->sc_pm_lock);
587 ath_debug_stat_interrupt(sc, status);
590 /* turn off every interrupt */
591 ath9k_hw_kill_interrupts(ah);
592 tasklet_schedule(&sc->intr_tq);
601 * This function is called when a HW reset cannot be deferred
602 * and has to be immediate.
604 int ath_reset(struct ath_softc *sc, struct ath9k_channel *hchan)
606 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
609 ath9k_hw_kill_interrupts(sc->sc_ah);
610 set_bit(ATH_OP_HW_RESET, &common->op_flags);
613 r = ath_reset_internal(sc, hchan);
614 ath9k_ps_restore(sc);
620 * When a HW reset can be deferred, it is added to the
621 * hw_reset_work workqueue, but we set ATH_OP_HW_RESET before
624 void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
626 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
627 #ifdef CONFIG_ATH9K_DEBUGFS
628 RESET_STAT_INC(sc, type);
630 ath9k_hw_kill_interrupts(sc->sc_ah);
631 set_bit(ATH_OP_HW_RESET, &common->op_flags);
632 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
635 void ath_reset_work(struct work_struct *work)
637 struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
640 ath_reset_internal(sc, NULL);
641 ath9k_ps_restore(sc);
644 /**********************/
645 /* mac80211 callbacks */
646 /**********************/
648 static int ath9k_start(struct ieee80211_hw *hw)
650 struct ath_softc *sc = hw->priv;
651 struct ath_hw *ah = sc->sc_ah;
652 struct ath_common *common = ath9k_hw_common(ah);
653 struct ieee80211_channel *curchan = sc->cur_chan->chandef.chan;
654 struct ath_chanctx *ctx = sc->cur_chan;
655 struct ath9k_channel *init_channel;
658 ath_dbg(common, CONFIG,
659 "Starting driver with initial channel: %d MHz\n",
660 curchan->center_freq);
663 mutex_lock(&sc->mutex);
665 init_channel = ath9k_cmn_get_channel(hw, ah, &ctx->chandef);
666 sc->cur_chandef = hw->conf.chandef;
668 /* Reset SERDES registers */
669 ath9k_hw_configpcipowersave(ah, false);
672 * The basic interface to setting the hardware in a good
673 * state is ``reset''. On return the hardware is known to
674 * be powered up and with interrupts disabled. This must
675 * be followed by initialization of the appropriate bits
676 * and then setup of the interrupt mask.
678 spin_lock_bh(&sc->sc_pcu_lock);
680 atomic_set(&ah->intr_ref_cnt, -1);
682 r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
685 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
686 r, curchan->center_freq);
687 ah->reset_power_on = false;
690 /* Setup our intr mask. */
691 ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
692 ATH9K_INT_RXORN | ATH9K_INT_FATAL |
695 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
696 ah->imask |= ATH9K_INT_RXHP |
699 ah->imask |= ATH9K_INT_RX;
701 if (ah->config.hw_hang_checks & HW_BB_WATCHDOG)
702 ah->imask |= ATH9K_INT_BB_WATCHDOG;
705 * Enable GTT interrupts only for AR9003/AR9004 chips
708 if (AR_SREV_9300_20_OR_LATER(ah))
709 ah->imask |= ATH9K_INT_GTT;
711 if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
712 ah->imask |= ATH9K_INT_CST;
716 clear_bit(ATH_OP_INVALID, &common->op_flags);
717 sc->sc_ah->is_monitoring = false;
719 if (!ath_complete_reset(sc, false))
720 ah->reset_power_on = false;
722 if (ah->led_pin >= 0) {
723 ath9k_hw_set_gpio(ah, ah->led_pin,
724 (ah->config.led_active_high) ? 1 : 0);
725 ath9k_hw_gpio_request_out(ah, ah->led_pin, NULL,
726 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
730 * Reset key cache to sane defaults (all entries cleared) instead of
731 * semi-random values after suspend/resume.
733 ath9k_cmn_init_crypto(sc->sc_ah);
735 ath9k_hw_reset_tsf(ah);
737 spin_unlock_bh(&sc->sc_pcu_lock);
741 mutex_unlock(&sc->mutex);
743 ath9k_ps_restore(sc);
748 static void ath9k_tx(struct ieee80211_hw *hw,
749 struct ieee80211_tx_control *control,
752 struct ath_softc *sc = hw->priv;
753 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
754 struct ath_tx_control txctl;
755 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
758 if (sc->ps_enabled) {
760 * mac80211 does not set PM field for normal data frames, so we
761 * need to update that based on the current PS mode.
763 if (ieee80211_is_data(hdr->frame_control) &&
764 !ieee80211_is_nullfunc(hdr->frame_control) &&
765 !ieee80211_has_pm(hdr->frame_control)) {
767 "Add PM=1 for a TX frame while in PS mode\n");
768 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
772 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
774 * We are using PS-Poll and mac80211 can request TX while in
775 * power save mode. Need to wake up hardware for the TX to be
776 * completed and if needed, also for RX of buffered frames.
779 spin_lock_irqsave(&sc->sc_pm_lock, flags);
780 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
781 ath9k_hw_setrxabort(sc->sc_ah, 0);
782 if (ieee80211_is_pspoll(hdr->frame_control)) {
784 "Sending PS-Poll to pick a buffered frame\n");
785 sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
787 ath_dbg(common, PS, "Wake up to complete TX\n");
788 sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
791 * The actual restore operation will happen only after
792 * the ps_flags bit is cleared. We are just dropping
793 * the ps_usecount here.
795 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
796 ath9k_ps_restore(sc);
800 * Cannot tx while the hardware is in full sleep, it first needs a full
801 * chip reset to recover from that
803 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
804 ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
808 memset(&txctl, 0, sizeof(struct ath_tx_control));
809 txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
810 txctl.sta = control->sta;
812 ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
814 if (ath_tx_start(hw, skb, &txctl) != 0) {
815 ath_dbg(common, XMIT, "TX failed\n");
816 TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
822 ieee80211_free_txskb(hw, skb);
825 static bool ath9k_txq_list_has_key(struct list_head *txq_list, u32 keyix)
828 struct ieee80211_tx_info *txinfo;
829 struct ath_frame_info *fi;
831 list_for_each_entry(bf, txq_list, list) {
832 if (bf->bf_state.stale || !bf->bf_mpdu)
835 txinfo = IEEE80211_SKB_CB(bf->bf_mpdu);
836 fi = (struct ath_frame_info *)&txinfo->rate_driver_data[0];
837 if (fi->keyix == keyix)
844 static bool ath9k_txq_has_key(struct ath_softc *sc, u32 keyix)
846 struct ath_hw *ah = sc->sc_ah;
849 bool key_in_use = false;
851 for (i = 0; !key_in_use && i < ATH9K_NUM_TX_QUEUES; i++) {
852 if (!ATH_TXQ_SETUP(sc, i))
854 txq = &sc->tx.txq[i];
857 if (!ath9k_hw_numtxpending(ah, txq->axq_qnum))
860 ath_txq_lock(sc, txq);
861 key_in_use = ath9k_txq_list_has_key(&txq->axq_q, keyix);
862 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
863 int idx = txq->txq_tailidx;
865 while (!key_in_use &&
866 !list_empty(&txq->txq_fifo[idx])) {
867 key_in_use = ath9k_txq_list_has_key(
868 &txq->txq_fifo[idx], keyix);
869 INCR(idx, ATH_TXFIFO_DEPTH);
872 ath_txq_unlock(sc, txq);
878 static void ath9k_pending_key_del(struct ath_softc *sc, u8 keyix)
880 struct ath_hw *ah = sc->sc_ah;
881 struct ath_common *common = ath9k_hw_common(ah);
883 if (!test_bit(keyix, ah->pending_del_keymap) ||
884 ath9k_txq_has_key(sc, keyix))
887 /* No more TXQ frames point to this key cache entry, so delete it. */
888 clear_bit(keyix, ah->pending_del_keymap);
889 ath_key_delete(common, keyix);
892 static void ath9k_stop(struct ieee80211_hw *hw)
894 struct ath_softc *sc = hw->priv;
895 struct ath_hw *ah = sc->sc_ah;
896 struct ath_common *common = ath9k_hw_common(ah);
900 ath9k_deinit_channel_context(sc);
902 mutex_lock(&sc->mutex);
908 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
909 ath_dbg(common, ANY, "Device not present\n");
910 mutex_unlock(&sc->mutex);
914 /* Ensure HW is awake when we try to shut it down. */
917 spin_lock_bh(&sc->sc_pcu_lock);
919 /* prevent tasklets to enable interrupts once we disable them */
920 ah->imask &= ~ATH9K_INT_GLOBAL;
922 /* make sure h/w will not generate any interrupt
923 * before setting the invalid flag. */
924 ath9k_hw_disable_interrupts(ah);
926 spin_unlock_bh(&sc->sc_pcu_lock);
928 /* we can now sync irq and kill any running tasklets, since we already
929 * disabled interrupts and not holding a spin lock */
930 synchronize_irq(sc->irq);
931 tasklet_kill(&sc->intr_tq);
932 tasklet_kill(&sc->bcon_tasklet);
934 prev_idle = sc->ps_idle;
937 spin_lock_bh(&sc->sc_pcu_lock);
939 if (ah->led_pin >= 0) {
940 ath9k_hw_set_gpio(ah, ah->led_pin,
941 (ah->config.led_active_high) ? 0 : 1);
942 ath9k_hw_gpio_request_in(ah, ah->led_pin, NULL);
945 ath_prepare_reset(sc);
948 dev_kfree_skb_any(sc->rx.frag);
953 ah->curchan = ath9k_cmn_get_channel(hw, ah,
954 &sc->cur_chan->chandef);
956 ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
958 set_bit(ATH_OP_INVALID, &common->op_flags);
960 ath9k_hw_phy_disable(ah);
962 ath9k_hw_configpcipowersave(ah, true);
964 spin_unlock_bh(&sc->sc_pcu_lock);
966 for (i = 0; i < ATH_KEYMAX; i++)
967 ath9k_pending_key_del(sc, i);
969 /* Clear key cache entries explicitly to get rid of any potentially
972 ath9k_cmn_init_crypto(sc->sc_ah);
974 ath9k_ps_restore(sc);
976 sc->ps_idle = prev_idle;
978 mutex_unlock(&sc->mutex);
980 ath_dbg(common, CONFIG, "Driver halt\n");
983 static bool ath9k_uses_beacons(int type)
986 case NL80211_IFTYPE_AP:
987 case NL80211_IFTYPE_ADHOC:
988 case NL80211_IFTYPE_MESH_POINT:
995 static void ath9k_vif_iter_set_beacon(struct ath9k_vif_iter_data *iter_data,
996 struct ieee80211_vif *vif)
998 /* Use the first (configured) interface, but prefering AP interfaces. */
999 if (!iter_data->primary_beacon_vif) {
1000 iter_data->primary_beacon_vif = vif;
1002 if (iter_data->primary_beacon_vif->type != NL80211_IFTYPE_AP &&
1003 vif->type == NL80211_IFTYPE_AP)
1004 iter_data->primary_beacon_vif = vif;
1007 iter_data->beacons = true;
1008 iter_data->nbcnvifs += 1;
1011 static void ath9k_vif_iter(struct ath9k_vif_iter_data *iter_data,
1012 u8 *mac, struct ieee80211_vif *vif)
1014 struct ath_vif *avp = (struct ath_vif *)vif->drv_priv;
1017 if (iter_data->has_hw_macaddr) {
1018 for (i = 0; i < ETH_ALEN; i++)
1019 iter_data->mask[i] &=
1020 ~(iter_data->hw_macaddr[i] ^ mac[i]);
1022 memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
1023 iter_data->has_hw_macaddr = true;
1026 if (!vif->bss_conf.use_short_slot)
1027 iter_data->slottime = 20;
1029 switch (vif->type) {
1030 case NL80211_IFTYPE_AP:
1032 if (vif->bss_conf.enable_beacon)
1033 ath9k_vif_iter_set_beacon(iter_data, vif);
1035 case NL80211_IFTYPE_STATION:
1036 iter_data->nstations++;
1037 if (avp->assoc && !iter_data->primary_sta)
1038 iter_data->primary_sta = vif;
1040 case NL80211_IFTYPE_OCB:
1043 case NL80211_IFTYPE_ADHOC:
1044 iter_data->nadhocs++;
1045 if (vif->bss_conf.enable_beacon)
1046 ath9k_vif_iter_set_beacon(iter_data, vif);
1048 case NL80211_IFTYPE_MESH_POINT:
1049 iter_data->nmeshes++;
1050 if (vif->bss_conf.enable_beacon)
1051 ath9k_vif_iter_set_beacon(iter_data, vif);
1053 case NL80211_IFTYPE_WDS:
1061 static void ath9k_update_bssid_mask(struct ath_softc *sc,
1062 struct ath_chanctx *ctx,
1063 struct ath9k_vif_iter_data *iter_data)
1065 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1066 struct ath_vif *avp;
1069 if (!ath9k_is_chanctx_enabled())
1072 list_for_each_entry(avp, &ctx->vifs, list) {
1073 if (ctx->nvifs_assigned != 1)
1076 if (!iter_data->has_hw_macaddr)
1079 ether_addr_copy(common->curbssid, avp->bssid);
1081 /* perm_addr will be used as the p2p device address. */
1082 for (i = 0; i < ETH_ALEN; i++)
1083 iter_data->mask[i] &=
1084 ~(iter_data->hw_macaddr[i] ^
1085 sc->hw->wiphy->perm_addr[i]);
1089 /* Called with sc->mutex held. */
1090 void ath9k_calculate_iter_data(struct ath_softc *sc,
1091 struct ath_chanctx *ctx,
1092 struct ath9k_vif_iter_data *iter_data)
1094 struct ath_vif *avp;
1097 * The hardware will use primary station addr together with the
1098 * BSSID mask when matching addresses.
1100 memset(iter_data, 0, sizeof(*iter_data));
1101 eth_broadcast_addr(iter_data->mask);
1102 iter_data->slottime = 9;
1104 list_for_each_entry(avp, &ctx->vifs, list)
1105 ath9k_vif_iter(iter_data, avp->vif->addr, avp->vif);
1107 ath9k_update_bssid_mask(sc, ctx, iter_data);
1110 static void ath9k_set_assoc_state(struct ath_softc *sc,
1111 struct ieee80211_vif *vif, bool changed)
1113 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1114 struct ath_vif *avp = (struct ath_vif *)vif->drv_priv;
1115 unsigned long flags;
1117 set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1119 ether_addr_copy(common->curbssid, avp->bssid);
1120 common->curaid = avp->aid;
1121 ath9k_hw_write_associd(sc->sc_ah);
1124 common->last_rssi = ATH_RSSI_DUMMY_MARKER;
1125 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
1127 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1128 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
1129 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1132 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1133 ath9k_mci_update_wlan_channels(sc, false);
1135 ath_dbg(common, CONFIG,
1136 "Primary Station interface: %pM, BSSID: %pM\n",
1137 vif->addr, common->curbssid);
1140 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
1141 static void ath9k_set_offchannel_state(struct ath_softc *sc)
1143 struct ath_hw *ah = sc->sc_ah;
1144 struct ath_common *common = ath9k_hw_common(ah);
1145 struct ieee80211_vif *vif = NULL;
1147 ath9k_ps_wakeup(sc);
1149 if (sc->offchannel.state < ATH_OFFCHANNEL_ROC_START)
1150 vif = sc->offchannel.scan_vif;
1152 vif = sc->offchannel.roc_vif;
1157 eth_zero_addr(common->curbssid);
1158 eth_broadcast_addr(common->bssidmask);
1159 memcpy(common->macaddr, vif->addr, ETH_ALEN);
1161 ah->opmode = vif->type;
1162 ah->imask &= ~ATH9K_INT_SWBA;
1163 ah->imask &= ~ATH9K_INT_TSFOOR;
1166 ath_hw_setbssidmask(common);
1167 ath9k_hw_setopmode(ah);
1168 ath9k_hw_write_associd(sc->sc_ah);
1169 ath9k_hw_set_interrupts(ah);
1170 ath9k_hw_init_global_settings(ah);
1173 ath9k_ps_restore(sc);
1177 /* Called with sc->mutex held. */
1178 void ath9k_calculate_summary_state(struct ath_softc *sc,
1179 struct ath_chanctx *ctx)
1181 struct ath_hw *ah = sc->sc_ah;
1182 struct ath_common *common = ath9k_hw_common(ah);
1183 struct ath9k_vif_iter_data iter_data;
1185 ath_chanctx_check_active(sc, ctx);
1187 if (ctx != sc->cur_chan)
1190 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
1191 if (ctx == &sc->offchannel.chan)
1192 return ath9k_set_offchannel_state(sc);
1195 ath9k_ps_wakeup(sc);
1196 ath9k_calculate_iter_data(sc, ctx, &iter_data);
1198 if (iter_data.has_hw_macaddr)
1199 memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
1201 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
1202 ath_hw_setbssidmask(common);
1204 if (iter_data.naps > 0) {
1205 ath9k_hw_set_tsfadjust(ah, true);
1206 ah->opmode = NL80211_IFTYPE_AP;
1208 ath9k_hw_set_tsfadjust(ah, false);
1209 if (iter_data.beacons)
1210 ath9k_beacon_ensure_primary_slot(sc);
1212 if (iter_data.nmeshes)
1213 ah->opmode = NL80211_IFTYPE_MESH_POINT;
1214 else if (iter_data.nocbs)
1215 ah->opmode = NL80211_IFTYPE_OCB;
1216 else if (iter_data.nwds)
1217 ah->opmode = NL80211_IFTYPE_AP;
1218 else if (iter_data.nadhocs)
1219 ah->opmode = NL80211_IFTYPE_ADHOC;
1221 ah->opmode = NL80211_IFTYPE_STATION;
1224 ath9k_hw_setopmode(ah);
1226 ctx->switch_after_beacon = false;
1227 if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
1228 ah->imask |= ATH9K_INT_TSFOOR;
1230 ah->imask &= ~ATH9K_INT_TSFOOR;
1231 if (iter_data.naps == 1 && iter_data.beacons)
1232 ctx->switch_after_beacon = true;
1235 if (ah->opmode == NL80211_IFTYPE_STATION) {
1236 bool changed = (iter_data.primary_sta != ctx->primary_sta);
1238 if (iter_data.primary_sta) {
1239 iter_data.primary_beacon_vif = iter_data.primary_sta;
1240 iter_data.beacons = true;
1241 ath9k_set_assoc_state(sc, iter_data.primary_sta,
1243 ctx->primary_sta = iter_data.primary_sta;
1245 ctx->primary_sta = NULL;
1246 eth_zero_addr(common->curbssid);
1248 ath9k_hw_write_associd(sc->sc_ah);
1249 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1250 ath9k_mci_update_wlan_channels(sc, true);
1253 sc->nbcnvifs = iter_data.nbcnvifs;
1254 ath9k_beacon_config(sc, iter_data.primary_beacon_vif,
1256 ath9k_hw_set_interrupts(ah);
1258 if (ah->slottime != iter_data.slottime) {
1259 ah->slottime = iter_data.slottime;
1260 ath9k_hw_init_global_settings(ah);
1263 if (iter_data.primary_sta)
1264 set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1266 clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1268 ath_dbg(common, CONFIG,
1269 "macaddr: %pM, bssid: %pM, bssidmask: %pM\n",
1270 common->macaddr, common->curbssid, common->bssidmask);
1272 ath9k_ps_restore(sc);
1275 static void ath9k_tpc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1277 int *power = (int *)data;
1279 if (*power < vif->bss_conf.txpower)
1280 *power = vif->bss_conf.txpower;
1283 /* Called with sc->mutex held. */
1284 void ath9k_set_txpower(struct ath_softc *sc, struct ieee80211_vif *vif)
1287 struct ath_hw *ah = sc->sc_ah;
1288 struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
1290 ath9k_ps_wakeup(sc);
1291 if (ah->tpc_enabled) {
1292 power = (vif) ? vif->bss_conf.txpower : -1;
1293 ieee80211_iterate_active_interfaces_atomic(
1294 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1295 ath9k_tpc_vif_iter, &power);
1297 power = sc->hw->conf.power_level;
1299 power = sc->hw->conf.power_level;
1301 sc->cur_chan->txpower = 2 * power;
1302 ath9k_hw_set_txpowerlimit(ah, sc->cur_chan->txpower, false);
1303 sc->cur_chan->cur_txpower = reg->max_power_level;
1304 ath9k_ps_restore(sc);
1307 static void ath9k_assign_hw_queues(struct ieee80211_hw *hw,
1308 struct ieee80211_vif *vif)
1312 if (!ath9k_is_chanctx_enabled())
1315 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1316 vif->hw_queue[i] = i;
1318 if (vif->type == NL80211_IFTYPE_AP ||
1319 vif->type == NL80211_IFTYPE_MESH_POINT)
1320 vif->cab_queue = hw->queues - 2;
1322 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
1325 static int ath9k_add_interface(struct ieee80211_hw *hw,
1326 struct ieee80211_vif *vif)
1328 struct ath_softc *sc = hw->priv;
1329 struct ath_hw *ah = sc->sc_ah;
1330 struct ath_common *common = ath9k_hw_common(ah);
1331 struct ath_vif *avp = (void *)vif->drv_priv;
1332 struct ath_node *an = &avp->mcast_node;
1334 mutex_lock(&sc->mutex);
1335 if (IS_ENABLED(CONFIG_ATH9K_TX99)) {
1336 if (sc->cur_chan->nvifs >= 1) {
1337 mutex_unlock(&sc->mutex);
1343 ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
1344 sc->cur_chan->nvifs++;
1346 if (vif->type == NL80211_IFTYPE_STATION && ath9k_is_chanctx_enabled())
1347 vif->driver_flags |= IEEE80211_VIF_GET_NOA_UPDATE;
1349 if (ath9k_uses_beacons(vif->type))
1350 ath9k_beacon_assign_slot(sc, vif);
1353 if (!ath9k_is_chanctx_enabled()) {
1354 avp->chanctx = sc->cur_chan;
1355 list_add_tail(&avp->list, &avp->chanctx->vifs);
1358 ath9k_calculate_summary_state(sc, avp->chanctx);
1360 ath9k_assign_hw_queues(hw, vif);
1362 ath9k_set_txpower(sc, vif);
1367 an->no_ps_filter = true;
1368 ath_tx_node_init(sc, an);
1370 mutex_unlock(&sc->mutex);
1374 static int ath9k_change_interface(struct ieee80211_hw *hw,
1375 struct ieee80211_vif *vif,
1376 enum nl80211_iftype new_type,
1379 struct ath_softc *sc = hw->priv;
1380 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1381 struct ath_vif *avp = (void *)vif->drv_priv;
1383 mutex_lock(&sc->mutex);
1385 if (IS_ENABLED(CONFIG_ATH9K_TX99)) {
1386 mutex_unlock(&sc->mutex);
1390 ath_dbg(common, CONFIG, "Change Interface\n");
1392 if (ath9k_uses_beacons(vif->type))
1393 ath9k_beacon_remove_slot(sc, vif);
1395 vif->type = new_type;
1398 if (ath9k_uses_beacons(vif->type))
1399 ath9k_beacon_assign_slot(sc, vif);
1401 ath9k_assign_hw_queues(hw, vif);
1402 ath9k_calculate_summary_state(sc, avp->chanctx);
1404 ath9k_set_txpower(sc, vif);
1406 mutex_unlock(&sc->mutex);
1410 static void ath9k_remove_interface(struct ieee80211_hw *hw,
1411 struct ieee80211_vif *vif)
1413 struct ath_softc *sc = hw->priv;
1414 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1415 struct ath_vif *avp = (void *)vif->drv_priv;
1417 ath_dbg(common, CONFIG, "Detach Interface\n");
1419 mutex_lock(&sc->mutex);
1421 ath9k_p2p_remove_vif(sc, vif);
1423 sc->cur_chan->nvifs--;
1424 sc->tx99_vif = NULL;
1425 if (!ath9k_is_chanctx_enabled())
1426 list_del(&avp->list);
1428 if (ath9k_uses_beacons(vif->type))
1429 ath9k_beacon_remove_slot(sc, vif);
1431 ath_tx_node_cleanup(sc, &avp->mcast_node);
1433 ath9k_calculate_summary_state(sc, avp->chanctx);
1435 ath9k_set_txpower(sc, NULL);
1437 mutex_unlock(&sc->mutex);
1440 static void ath9k_enable_ps(struct ath_softc *sc)
1442 struct ath_hw *ah = sc->sc_ah;
1443 struct ath_common *common = ath9k_hw_common(ah);
1445 if (IS_ENABLED(CONFIG_ATH9K_TX99))
1448 sc->ps_enabled = true;
1449 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1450 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1451 ah->imask |= ATH9K_INT_TIM_TIMER;
1452 ath9k_hw_set_interrupts(ah);
1454 ath9k_hw_setrxabort(ah, 1);
1456 ath_dbg(common, PS, "PowerSave enabled\n");
1459 static void ath9k_disable_ps(struct ath_softc *sc)
1461 struct ath_hw *ah = sc->sc_ah;
1462 struct ath_common *common = ath9k_hw_common(ah);
1464 if (IS_ENABLED(CONFIG_ATH9K_TX99))
1467 sc->ps_enabled = false;
1468 ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
1469 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1470 ath9k_hw_setrxabort(ah, 0);
1471 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1473 PS_WAIT_FOR_PSPOLL_DATA |
1474 PS_WAIT_FOR_TX_ACK);
1475 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1476 ah->imask &= ~ATH9K_INT_TIM_TIMER;
1477 ath9k_hw_set_interrupts(ah);
1480 ath_dbg(common, PS, "PowerSave disabled\n");
1483 static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
1485 struct ath_softc *sc = hw->priv;
1486 struct ath_hw *ah = sc->sc_ah;
1487 struct ath_common *common = ath9k_hw_common(ah);
1488 struct ieee80211_conf *conf = &hw->conf;
1489 struct ath_chanctx *ctx = sc->cur_chan;
1491 ath9k_ps_wakeup(sc);
1492 mutex_lock(&sc->mutex);
1494 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1495 sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1497 ath_cancel_work(sc);
1498 ath9k_stop_btcoex(sc);
1500 ath9k_start_btcoex(sc);
1502 * The chip needs a reset to properly wake up from
1505 ath_chanctx_set_channel(sc, ctx, &ctx->chandef);
1510 * We just prepare to enable PS. We have to wait until our AP has
1511 * ACK'd our null data frame to disable RX otherwise we'll ignore
1512 * those ACKs and end up retransmitting the same null data frames.
1513 * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1515 if (changed & IEEE80211_CONF_CHANGE_PS) {
1516 unsigned long flags;
1517 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1518 if (conf->flags & IEEE80211_CONF_PS)
1519 ath9k_enable_ps(sc);
1521 ath9k_disable_ps(sc);
1522 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1525 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1526 if (conf->flags & IEEE80211_CONF_MONITOR) {
1527 ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
1528 sc->sc_ah->is_monitoring = true;
1530 ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
1531 sc->sc_ah->is_monitoring = false;
1535 if (!ath9k_is_chanctx_enabled() && (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
1536 ctx->offchannel = !!(conf->flags & IEEE80211_CONF_OFFCHANNEL);
1537 ath_chanctx_set_channel(sc, ctx, &hw->conf.chandef);
1540 if (changed & IEEE80211_CONF_CHANGE_POWER)
1541 ath9k_set_txpower(sc, NULL);
1543 mutex_unlock(&sc->mutex);
1544 ath9k_ps_restore(sc);
1549 #define SUPPORTED_FILTERS \
1554 FIF_BCN_PRBRESP_PROMISC | \
1558 /* FIXME: sc->sc_full_reset ? */
1559 static void ath9k_configure_filter(struct ieee80211_hw *hw,
1560 unsigned int changed_flags,
1561 unsigned int *total_flags,
1564 struct ath_softc *sc = hw->priv;
1565 struct ath_chanctx *ctx;
1568 changed_flags &= SUPPORTED_FILTERS;
1569 *total_flags &= SUPPORTED_FILTERS;
1571 spin_lock_bh(&sc->chan_lock);
1572 ath_for_each_chanctx(sc, ctx)
1573 ctx->rxfilter = *total_flags;
1574 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
1575 sc->offchannel.chan.rxfilter = *total_flags;
1577 spin_unlock_bh(&sc->chan_lock);
1579 ath9k_ps_wakeup(sc);
1580 rfilt = ath_calcrxfilter(sc);
1581 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1582 ath9k_ps_restore(sc);
1584 ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
1588 static int ath9k_sta_add(struct ieee80211_hw *hw,
1589 struct ieee80211_vif *vif,
1590 struct ieee80211_sta *sta)
1592 struct ath_softc *sc = hw->priv;
1593 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1594 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1595 struct ieee80211_key_conf ps_key = { };
1598 ath_node_attach(sc, sta, vif);
1600 if (vif->type != NL80211_IFTYPE_AP &&
1601 vif->type != NL80211_IFTYPE_AP_VLAN)
1604 key = ath_key_config(common, vif, sta, &ps_key);
1607 an->key_idx[0] = key;
1613 static void ath9k_del_ps_key(struct ath_softc *sc,
1614 struct ieee80211_vif *vif,
1615 struct ieee80211_sta *sta)
1617 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1618 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1623 ath_key_delete(common, an->ps_key);
1628 static int ath9k_sta_remove(struct ieee80211_hw *hw,
1629 struct ieee80211_vif *vif,
1630 struct ieee80211_sta *sta)
1632 struct ath_softc *sc = hw->priv;
1634 ath9k_del_ps_key(sc, vif, sta);
1635 ath_node_detach(sc, sta);
1640 static int ath9k_sta_state(struct ieee80211_hw *hw,
1641 struct ieee80211_vif *vif,
1642 struct ieee80211_sta *sta,
1643 enum ieee80211_sta_state old_state,
1644 enum ieee80211_sta_state new_state)
1646 struct ath_softc *sc = hw->priv;
1647 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1650 if (old_state == IEEE80211_STA_NOTEXIST &&
1651 new_state == IEEE80211_STA_NONE) {
1652 ret = ath9k_sta_add(hw, vif, sta);
1653 ath_dbg(common, CONFIG,
1654 "Add station: %pM\n", sta->addr);
1655 } else if (old_state == IEEE80211_STA_NONE &&
1656 new_state == IEEE80211_STA_NOTEXIST) {
1657 ret = ath9k_sta_remove(hw, vif, sta);
1658 ath_dbg(common, CONFIG,
1659 "Remove station: %pM\n", sta->addr);
1662 if (ath9k_is_chanctx_enabled()) {
1663 if (vif->type == NL80211_IFTYPE_STATION) {
1664 if (old_state == IEEE80211_STA_ASSOC &&
1665 new_state == IEEE80211_STA_AUTHORIZED)
1666 ath_chanctx_event(sc, vif,
1667 ATH_CHANCTX_EVENT_AUTHORIZED);
1674 static void ath9k_sta_set_tx_filter(struct ath_hw *ah,
1675 struct ath_node *an,
1680 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1681 if (!an->key_idx[i])
1683 ath9k_hw_set_tx_filter(ah, an->key_idx[i], set);
1687 static void ath9k_sta_notify(struct ieee80211_hw *hw,
1688 struct ieee80211_vif *vif,
1689 enum sta_notify_cmd cmd,
1690 struct ieee80211_sta *sta)
1692 struct ath_softc *sc = hw->priv;
1693 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1696 case STA_NOTIFY_SLEEP:
1697 an->sleeping = true;
1698 ath_tx_aggr_sleep(sta, sc, an);
1699 ath9k_sta_set_tx_filter(sc->sc_ah, an, true);
1701 case STA_NOTIFY_AWAKE:
1702 ath9k_sta_set_tx_filter(sc->sc_ah, an, false);
1703 an->sleeping = false;
1704 ath_tx_aggr_wakeup(sc, an);
1709 static int ath9k_conf_tx(struct ieee80211_hw *hw,
1710 struct ieee80211_vif *vif, u16 queue,
1711 const struct ieee80211_tx_queue_params *params)
1713 struct ath_softc *sc = hw->priv;
1714 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1715 struct ath_txq *txq;
1716 struct ath9k_tx_queue_info qi;
1719 if (queue >= IEEE80211_NUM_ACS)
1722 txq = sc->tx.txq_map[queue];
1724 ath9k_ps_wakeup(sc);
1725 mutex_lock(&sc->mutex);
1727 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1729 qi.tqi_aifs = params->aifs;
1730 qi.tqi_cwmin = params->cw_min;
1731 qi.tqi_cwmax = params->cw_max;
1732 qi.tqi_burstTime = params->txop * 32;
1734 ath_dbg(common, CONFIG,
1735 "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1736 queue, txq->axq_qnum, params->aifs, params->cw_min,
1737 params->cw_max, params->txop);
1739 ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
1740 ret = ath_txq_update(sc, txq->axq_qnum, &qi);
1742 ath_err(common, "TXQ Update failed\n");
1744 mutex_unlock(&sc->mutex);
1745 ath9k_ps_restore(sc);
1750 static int ath9k_set_key(struct ieee80211_hw *hw,
1751 enum set_key_cmd cmd,
1752 struct ieee80211_vif *vif,
1753 struct ieee80211_sta *sta,
1754 struct ieee80211_key_conf *key)
1756 struct ath_softc *sc = hw->priv;
1757 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1758 struct ath_node *an = NULL;
1761 if (ath9k_modparam_nohwcrypt)
1764 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1765 vif->type == NL80211_IFTYPE_MESH_POINT) &&
1766 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1767 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1768 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1770 * For now, disable hw crypto for the RSN IBSS group keys. This
1771 * could be optimized in the future to use a modified key cache
1772 * design to support per-STA RX GTK, but until that gets
1773 * implemented, use of software crypto for group addressed
1774 * frames is a acceptable to allow RSN IBSS to be used.
1779 mutex_lock(&sc->mutex);
1780 ath9k_ps_wakeup(sc);
1781 ath_dbg(common, CONFIG, "Set HW Key %d\n", cmd);
1783 an = (struct ath_node *)sta->drv_priv;
1785 /* Delete pending key cache entries if no more frames are pointing to
1788 for (i = 0; i < ATH_KEYMAX; i++)
1789 ath9k_pending_key_del(sc, i);
1794 ath9k_del_ps_key(sc, vif, sta);
1796 key->hw_key_idx = 0;
1797 ret = ath_key_config(common, vif, sta, key);
1799 key->hw_key_idx = ret;
1800 /* push IV and Michael MIC generation to stack */
1801 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1802 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1803 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1804 if (sc->sc_ah->sw_mgmt_crypto_tx &&
1805 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1806 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1809 if (an && key->hw_key_idx) {
1810 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1813 an->key_idx[i] = key->hw_key_idx;
1816 WARN_ON(i == ARRAY_SIZE(an->key_idx));
1820 if (ath9k_txq_has_key(sc, key->hw_key_idx)) {
1821 /* Delay key cache entry deletion until there are no
1822 * remaining TXQ frames pointing to this entry.
1824 set_bit(key->hw_key_idx, sc->sc_ah->pending_del_keymap);
1825 ath_hw_keysetmac(common, key->hw_key_idx, NULL);
1827 ath_key_delete(common, key->hw_key_idx);
1830 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1831 if (an->key_idx[i] != key->hw_key_idx)
1837 key->hw_key_idx = 0;
1843 ath9k_ps_restore(sc);
1844 mutex_unlock(&sc->mutex);
1849 static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1850 struct ieee80211_vif *vif,
1851 struct ieee80211_bss_conf *bss_conf,
1855 (BSS_CHANGED_ASSOC | \
1856 BSS_CHANGED_IBSS | \
1857 BSS_CHANGED_BEACON_ENABLED)
1859 struct ath_softc *sc = hw->priv;
1860 struct ath_hw *ah = sc->sc_ah;
1861 struct ath_common *common = ath9k_hw_common(ah);
1862 struct ath_vif *avp = (void *)vif->drv_priv;
1865 ath9k_ps_wakeup(sc);
1866 mutex_lock(&sc->mutex);
1868 if (changed & BSS_CHANGED_ASSOC) {
1869 ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
1870 bss_conf->bssid, bss_conf->assoc);
1872 memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
1873 avp->aid = bss_conf->aid;
1874 avp->assoc = bss_conf->assoc;
1876 ath9k_calculate_summary_state(sc, avp->chanctx);
1879 if ((changed & BSS_CHANGED_IBSS) ||
1880 (changed & BSS_CHANGED_OCB)) {
1881 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1882 common->curaid = bss_conf->aid;
1883 ath9k_hw_write_associd(sc->sc_ah);
1886 if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
1887 (changed & BSS_CHANGED_BEACON_INT) ||
1888 (changed & BSS_CHANGED_BEACON_INFO)) {
1889 ath9k_calculate_summary_state(sc, avp->chanctx);
1892 if ((avp->chanctx == sc->cur_chan) &&
1893 (changed & BSS_CHANGED_ERP_SLOT)) {
1894 if (bss_conf->use_short_slot)
1899 if (vif->type == NL80211_IFTYPE_AP) {
1901 * Defer update, so that connected stations can adjust
1902 * their settings at the same time.
1903 * See beacon.c for more details
1905 sc->beacon.slottime = slottime;
1906 sc->beacon.updateslot = UPDATE;
1908 ah->slottime = slottime;
1909 ath9k_hw_init_global_settings(ah);
1913 if (changed & BSS_CHANGED_P2P_PS)
1914 ath9k_p2p_bss_info_changed(sc, vif);
1916 if (changed & CHECK_ANI)
1919 if (changed & BSS_CHANGED_TXPOWER) {
1920 ath_dbg(common, CONFIG, "vif %pM power %d dbm power_type %d\n",
1921 vif->addr, bss_conf->txpower, bss_conf->txpower_type);
1922 ath9k_set_txpower(sc, vif);
1925 mutex_unlock(&sc->mutex);
1926 ath9k_ps_restore(sc);
1931 static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1933 struct ath_softc *sc = hw->priv;
1934 struct ath_vif *avp = (void *)vif->drv_priv;
1937 mutex_lock(&sc->mutex);
1938 ath9k_ps_wakeup(sc);
1939 /* Get current TSF either from HW or kernel time. */
1940 if (sc->cur_chan == avp->chanctx) {
1941 tsf = ath9k_hw_gettsf64(sc->sc_ah);
1943 tsf = sc->cur_chan->tsf_val +
1944 ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts, NULL);
1946 tsf += le64_to_cpu(avp->tsf_adjust);
1947 ath9k_ps_restore(sc);
1948 mutex_unlock(&sc->mutex);
1953 static void ath9k_set_tsf(struct ieee80211_hw *hw,
1954 struct ieee80211_vif *vif,
1957 struct ath_softc *sc = hw->priv;
1958 struct ath_vif *avp = (void *)vif->drv_priv;
1960 mutex_lock(&sc->mutex);
1961 ath9k_ps_wakeup(sc);
1962 tsf -= le64_to_cpu(avp->tsf_adjust);
1963 getrawmonotonic(&avp->chanctx->tsf_ts);
1964 if (sc->cur_chan == avp->chanctx)
1965 ath9k_hw_settsf64(sc->sc_ah, tsf);
1966 avp->chanctx->tsf_val = tsf;
1967 ath9k_ps_restore(sc);
1968 mutex_unlock(&sc->mutex);
1971 static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1973 struct ath_softc *sc = hw->priv;
1974 struct ath_vif *avp = (void *)vif->drv_priv;
1976 mutex_lock(&sc->mutex);
1978 ath9k_ps_wakeup(sc);
1979 getrawmonotonic(&avp->chanctx->tsf_ts);
1980 if (sc->cur_chan == avp->chanctx)
1981 ath9k_hw_reset_tsf(sc->sc_ah);
1982 avp->chanctx->tsf_val = 0;
1983 ath9k_ps_restore(sc);
1985 mutex_unlock(&sc->mutex);
1988 static int ath9k_ampdu_action(struct ieee80211_hw *hw,
1989 struct ieee80211_vif *vif,
1990 struct ieee80211_ampdu_params *params)
1992 struct ath_softc *sc = hw->priv;
1993 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1996 struct ieee80211_sta *sta = params->sta;
1997 struct ath_node *an = (struct ath_node *)sta->drv_priv;
1998 enum ieee80211_ampdu_mlme_action action = params->action;
1999 u16 tid = params->tid;
2000 u16 *ssn = ¶ms->ssn;
2001 struct ath_atx_tid *atid;
2003 mutex_lock(&sc->mutex);
2006 case IEEE80211_AMPDU_RX_START:
2008 case IEEE80211_AMPDU_RX_STOP:
2010 case IEEE80211_AMPDU_TX_START:
2011 if (ath9k_is_chanctx_enabled()) {
2012 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
2017 ath9k_ps_wakeup(sc);
2018 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
2020 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2021 ath9k_ps_restore(sc);
2023 case IEEE80211_AMPDU_TX_STOP_FLUSH:
2024 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
2026 case IEEE80211_AMPDU_TX_STOP_CONT:
2027 ath9k_ps_wakeup(sc);
2028 ath_tx_aggr_stop(sc, sta, tid);
2030 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2031 ath9k_ps_restore(sc);
2033 case IEEE80211_AMPDU_TX_OPERATIONAL:
2034 atid = ath_node_to_tid(an, tid);
2035 atid->baw_size = IEEE80211_MIN_AMPDU_BUF <<
2036 sta->ht_cap.ampdu_factor;
2039 ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
2042 mutex_unlock(&sc->mutex);
2047 static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
2048 struct survey_info *survey)
2050 struct ath_softc *sc = hw->priv;
2051 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2052 struct ieee80211_supported_band *sband;
2053 struct ieee80211_channel *chan;
2056 if (IS_ENABLED(CONFIG_ATH9K_TX99))
2059 spin_lock_bh(&common->cc_lock);
2061 ath_update_survey_stats(sc);
2063 sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
2064 if (sband && idx >= sband->n_channels) {
2065 idx -= sband->n_channels;
2070 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
2072 if (!sband || idx >= sband->n_channels) {
2073 spin_unlock_bh(&common->cc_lock);
2077 chan = &sband->channels[idx];
2078 pos = chan->hw_value;
2079 memcpy(survey, &sc->survey[pos], sizeof(*survey));
2080 survey->channel = chan;
2081 spin_unlock_bh(&common->cc_lock);
2086 static void ath9k_enable_dynack(struct ath_softc *sc)
2088 #ifdef CONFIG_ATH9K_DYNACK
2090 struct ath_hw *ah = sc->sc_ah;
2092 ath_dynack_reset(ah);
2094 ah->dynack.enabled = true;
2095 rfilt = ath_calcrxfilter(sc);
2096 ath9k_hw_setrxfilter(ah, rfilt);
2100 static void ath9k_set_coverage_class(struct ieee80211_hw *hw,
2103 struct ath_softc *sc = hw->priv;
2104 struct ath_hw *ah = sc->sc_ah;
2106 if (IS_ENABLED(CONFIG_ATH9K_TX99))
2109 mutex_lock(&sc->mutex);
2111 if (coverage_class >= 0) {
2112 ah->coverage_class = coverage_class;
2113 if (ah->dynack.enabled) {
2116 ah->dynack.enabled = false;
2117 rfilt = ath_calcrxfilter(sc);
2118 ath9k_hw_setrxfilter(ah, rfilt);
2120 ath9k_ps_wakeup(sc);
2121 ath9k_hw_init_global_settings(ah);
2122 ath9k_ps_restore(sc);
2123 } else if (!ah->dynack.enabled) {
2124 ath9k_enable_dynack(sc);
2127 mutex_unlock(&sc->mutex);
2130 static bool ath9k_has_tx_pending(struct ath_softc *sc,
2135 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2136 if (!ATH_TXQ_SETUP(sc, i))
2139 npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i],
2148 static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2149 u32 queues, bool drop)
2151 struct ath_softc *sc = hw->priv;
2152 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2154 if (ath9k_is_chanctx_enabled()) {
2155 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
2159 * If MCC is active, extend the flush timeout
2160 * and wait for the HW/SW queues to become
2161 * empty. This needs to be done outside the
2162 * sc->mutex lock to allow the channel scheduler
2163 * to switch channel contexts.
2165 * The vif queues have been stopped in mac80211,
2166 * so there won't be any incoming frames.
2168 __ath9k_flush(hw, queues, drop, true, true);
2172 mutex_lock(&sc->mutex);
2173 __ath9k_flush(hw, queues, drop, true, false);
2174 mutex_unlock(&sc->mutex);
2177 void __ath9k_flush(struct ieee80211_hw *hw, u32 queues, bool drop,
2178 bool sw_pending, bool timeout_override)
2180 struct ath_softc *sc = hw->priv;
2181 struct ath_hw *ah = sc->sc_ah;
2182 struct ath_common *common = ath9k_hw_common(ah);
2186 cancel_delayed_work_sync(&sc->hw_check_work);
2188 if (ah->ah_flags & AH_UNPLUGGED) {
2189 ath_dbg(common, ANY, "Device has been unplugged!\n");
2193 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
2194 ath_dbg(common, ANY, "Device not present\n");
2198 spin_lock_bh(&sc->chan_lock);
2199 if (timeout_override)
2202 timeout = sc->cur_chan->flush_timeout;
2203 spin_unlock_bh(&sc->chan_lock);
2205 ath_dbg(common, CHAN_CTX,
2206 "Flush timeout: %d\n", jiffies_to_msecs(timeout));
2208 if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc, sw_pending),
2213 ath9k_ps_wakeup(sc);
2214 spin_lock_bh(&sc->sc_pcu_lock);
2215 drain_txq = ath_drain_all_txq(sc);
2216 spin_unlock_bh(&sc->sc_pcu_lock);
2219 ath_reset(sc, NULL);
2221 ath9k_ps_restore(sc);
2224 ieee80211_queue_delayed_work(hw, &sc->hw_check_work,
2225 ATH_HW_CHECK_POLL_INT);
2228 static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
2230 struct ath_softc *sc = hw->priv;
2232 return ath9k_has_tx_pending(sc, true);
2235 static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
2237 struct ath_softc *sc = hw->priv;
2238 struct ath_hw *ah = sc->sc_ah;
2239 struct ieee80211_vif *vif;
2240 struct ath_vif *avp;
2242 struct ath_tx_status ts;
2243 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
2246 vif = sc->beacon.bslot[0];
2250 if (!vif->bss_conf.enable_beacon)
2253 avp = (void *)vif->drv_priv;
2255 if (!sc->beacon.tx_processed && !edma) {
2256 tasklet_disable(&sc->bcon_tasklet);
2259 if (!bf || !bf->bf_mpdu)
2262 status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
2263 if (status == -EINPROGRESS)
2266 sc->beacon.tx_processed = true;
2267 sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
2270 tasklet_enable(&sc->bcon_tasklet);
2273 return sc->beacon.tx_last;
2276 static int ath9k_get_stats(struct ieee80211_hw *hw,
2277 struct ieee80211_low_level_stats *stats)
2279 struct ath_softc *sc = hw->priv;
2280 struct ath_hw *ah = sc->sc_ah;
2281 struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
2283 stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
2284 stats->dot11RTSFailureCount = mib_stats->rts_bad;
2285 stats->dot11FCSErrorCount = mib_stats->fcs_bad;
2286 stats->dot11RTSSuccessCount = mib_stats->rts_good;
2290 static u32 fill_chainmask(u32 cap, u32 new)
2295 for (i = 0; cap && new; i++, cap >>= 1) {
2296 if (!(cap & BIT(0)))
2308 static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
2310 if (AR_SREV_9300_20_OR_LATER(ah))
2313 switch (val & 0x7) {
2319 return (ah->caps.rx_chainmask == 1);
2325 static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2327 struct ath_softc *sc = hw->priv;
2328 struct ath_hw *ah = sc->sc_ah;
2330 if (ah->caps.rx_chainmask != 1)
2333 if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
2336 sc->ant_rx = rx_ant;
2337 sc->ant_tx = tx_ant;
2339 if (ah->caps.rx_chainmask == 1)
2342 /* AR9100 runs into calibration issues if not all rx chains are enabled */
2343 if (AR_SREV_9100(ah))
2344 ah->rxchainmask = 0x7;
2346 ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
2348 ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
2349 ath9k_cmn_reload_chainmask(ah);
2354 static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2356 struct ath_softc *sc = hw->priv;
2358 *tx_ant = sc->ant_tx;
2359 *rx_ant = sc->ant_rx;
2363 static void ath9k_sw_scan_start(struct ieee80211_hw *hw,
2364 struct ieee80211_vif *vif,
2367 struct ath_softc *sc = hw->priv;
2368 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2369 set_bit(ATH_OP_SCANNING, &common->op_flags);
2372 static void ath9k_sw_scan_complete(struct ieee80211_hw *hw,
2373 struct ieee80211_vif *vif)
2375 struct ath_softc *sc = hw->priv;
2376 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2377 clear_bit(ATH_OP_SCANNING, &common->op_flags);
2380 #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
2382 static void ath9k_cancel_pending_offchannel(struct ath_softc *sc)
2384 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2386 if (sc->offchannel.roc_vif) {
2387 ath_dbg(common, CHAN_CTX,
2388 "%s: Aborting RoC\n", __func__);
2390 del_timer_sync(&sc->offchannel.timer);
2391 if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
2392 ath_roc_complete(sc, ATH_ROC_COMPLETE_ABORT);
2395 if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
2396 ath_dbg(common, CHAN_CTX,
2397 "%s: Aborting HW scan\n", __func__);
2399 del_timer_sync(&sc->offchannel.timer);
2400 ath_scan_complete(sc, true);
2404 static int ath9k_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2405 struct ieee80211_scan_request *hw_req)
2407 struct cfg80211_scan_request *req = &hw_req->req;
2408 struct ath_softc *sc = hw->priv;
2409 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2412 mutex_lock(&sc->mutex);
2414 if (WARN_ON(sc->offchannel.scan_req)) {
2419 ath9k_ps_wakeup(sc);
2420 set_bit(ATH_OP_SCANNING, &common->op_flags);
2421 sc->offchannel.scan_vif = vif;
2422 sc->offchannel.scan_req = req;
2423 sc->offchannel.scan_idx = 0;
2425 ath_dbg(common, CHAN_CTX, "HW scan request received on vif: %pM\n",
2428 if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE) {
2429 ath_dbg(common, CHAN_CTX, "Starting HW scan\n");
2430 ath_offchannel_next(sc);
2434 mutex_unlock(&sc->mutex);
2439 static void ath9k_cancel_hw_scan(struct ieee80211_hw *hw,
2440 struct ieee80211_vif *vif)
2442 struct ath_softc *sc = hw->priv;
2443 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2445 ath_dbg(common, CHAN_CTX, "Cancel HW scan on vif: %pM\n", vif->addr);
2447 mutex_lock(&sc->mutex);
2448 del_timer_sync(&sc->offchannel.timer);
2449 ath_scan_complete(sc, true);
2450 mutex_unlock(&sc->mutex);
2453 static int ath9k_remain_on_channel(struct ieee80211_hw *hw,
2454 struct ieee80211_vif *vif,
2455 struct ieee80211_channel *chan, int duration,
2456 enum ieee80211_roc_type type)
2458 struct ath_softc *sc = hw->priv;
2459 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2462 mutex_lock(&sc->mutex);
2464 if (WARN_ON(sc->offchannel.roc_vif)) {
2469 ath9k_ps_wakeup(sc);
2470 sc->offchannel.roc_vif = vif;
2471 sc->offchannel.roc_chan = chan;
2472 sc->offchannel.roc_duration = duration;
2474 ath_dbg(common, CHAN_CTX,
2475 "RoC request on vif: %pM, type: %d duration: %d\n",
2476 vif->addr, type, duration);
2478 if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE) {
2479 ath_dbg(common, CHAN_CTX, "Starting RoC period\n");
2480 ath_offchannel_next(sc);
2484 mutex_unlock(&sc->mutex);
2489 static int ath9k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2491 struct ath_softc *sc = hw->priv;
2492 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2494 mutex_lock(&sc->mutex);
2496 ath_dbg(common, CHAN_CTX, "Cancel RoC\n");
2497 del_timer_sync(&sc->offchannel.timer);
2499 if (sc->offchannel.roc_vif) {
2500 if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
2501 ath_roc_complete(sc, ATH_ROC_COMPLETE_CANCEL);
2504 mutex_unlock(&sc->mutex);
2509 static int ath9k_add_chanctx(struct ieee80211_hw *hw,
2510 struct ieee80211_chanctx_conf *conf)
2512 struct ath_softc *sc = hw->priv;
2513 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2514 struct ath_chanctx *ctx, **ptr;
2517 mutex_lock(&sc->mutex);
2519 ath_for_each_chanctx(sc, ctx) {
2523 ptr = (void *) conf->drv_priv;
2525 ctx->assigned = true;
2526 pos = ctx - &sc->chanctx[0];
2527 ctx->hw_queue_base = pos * IEEE80211_NUM_ACS;
2529 ath_dbg(common, CHAN_CTX,
2530 "Add channel context: %d MHz\n",
2531 conf->def.chan->center_freq);
2533 ath_chanctx_set_channel(sc, ctx, &conf->def);
2535 mutex_unlock(&sc->mutex);
2539 mutex_unlock(&sc->mutex);
2544 static void ath9k_remove_chanctx(struct ieee80211_hw *hw,
2545 struct ieee80211_chanctx_conf *conf)
2547 struct ath_softc *sc = hw->priv;
2548 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2549 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2551 mutex_lock(&sc->mutex);
2553 ath_dbg(common, CHAN_CTX,
2554 "Remove channel context: %d MHz\n",
2555 conf->def.chan->center_freq);
2557 ctx->assigned = false;
2558 ctx->hw_queue_base = 0;
2559 ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_UNASSIGN);
2561 mutex_unlock(&sc->mutex);
2564 static void ath9k_change_chanctx(struct ieee80211_hw *hw,
2565 struct ieee80211_chanctx_conf *conf,
2568 struct ath_softc *sc = hw->priv;
2569 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2570 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2572 mutex_lock(&sc->mutex);
2573 ath_dbg(common, CHAN_CTX,
2574 "Change channel context: %d MHz\n",
2575 conf->def.chan->center_freq);
2576 ath_chanctx_set_channel(sc, ctx, &conf->def);
2577 mutex_unlock(&sc->mutex);
2580 static int ath9k_assign_vif_chanctx(struct ieee80211_hw *hw,
2581 struct ieee80211_vif *vif,
2582 struct ieee80211_chanctx_conf *conf)
2584 struct ath_softc *sc = hw->priv;
2585 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2586 struct ath_vif *avp = (void *)vif->drv_priv;
2587 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2590 ath9k_cancel_pending_offchannel(sc);
2592 mutex_lock(&sc->mutex);
2594 ath_dbg(common, CHAN_CTX,
2595 "Assign VIF (addr: %pM, type: %d, p2p: %d) to channel context: %d MHz\n",
2596 vif->addr, vif->type, vif->p2p,
2597 conf->def.chan->center_freq);
2600 ctx->nvifs_assigned++;
2601 list_add_tail(&avp->list, &ctx->vifs);
2602 ath9k_calculate_summary_state(sc, ctx);
2603 for (i = 0; i < IEEE80211_NUM_ACS; i++)
2604 vif->hw_queue[i] = ctx->hw_queue_base + i;
2606 mutex_unlock(&sc->mutex);
2611 static void ath9k_unassign_vif_chanctx(struct ieee80211_hw *hw,
2612 struct ieee80211_vif *vif,
2613 struct ieee80211_chanctx_conf *conf)
2615 struct ath_softc *sc = hw->priv;
2616 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2617 struct ath_vif *avp = (void *)vif->drv_priv;
2618 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2621 ath9k_cancel_pending_offchannel(sc);
2623 mutex_lock(&sc->mutex);
2625 ath_dbg(common, CHAN_CTX,
2626 "Remove VIF (addr: %pM, type: %d, p2p: %d) from channel context: %d MHz\n",
2627 vif->addr, vif->type, vif->p2p,
2628 conf->def.chan->center_freq);
2630 avp->chanctx = NULL;
2631 ctx->nvifs_assigned--;
2632 list_del(&avp->list);
2633 ath9k_calculate_summary_state(sc, ctx);
2634 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
2635 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
2637 mutex_unlock(&sc->mutex);
2640 static void ath9k_mgd_prepare_tx(struct ieee80211_hw *hw,
2641 struct ieee80211_vif *vif)
2643 struct ath_softc *sc = hw->priv;
2644 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2645 struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
2646 struct ath_beacon_config *cur_conf;
2647 struct ath_chanctx *go_ctx;
2648 unsigned long timeout;
2649 bool changed = false;
2652 if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
2658 mutex_lock(&sc->mutex);
2660 spin_lock_bh(&sc->chan_lock);
2661 if (sc->next_chan || (sc->cur_chan != avp->chanctx))
2663 spin_unlock_bh(&sc->chan_lock);
2668 ath9k_cancel_pending_offchannel(sc);
2670 go_ctx = ath_is_go_chanctx_present(sc);
2674 * Wait till the GO interface gets a chance
2675 * to send out an NoA.
2677 spin_lock_bh(&sc->chan_lock);
2678 sc->sched.mgd_prepare_tx = true;
2679 cur_conf = &go_ctx->beacon;
2680 beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
2681 spin_unlock_bh(&sc->chan_lock);
2683 timeout = usecs_to_jiffies(beacon_int * 2);
2684 init_completion(&sc->go_beacon);
2686 mutex_unlock(&sc->mutex);
2688 if (wait_for_completion_timeout(&sc->go_beacon,
2690 ath_dbg(common, CHAN_CTX,
2691 "Failed to send new NoA\n");
2693 spin_lock_bh(&sc->chan_lock);
2694 sc->sched.mgd_prepare_tx = false;
2695 spin_unlock_bh(&sc->chan_lock);
2698 mutex_lock(&sc->mutex);
2701 ath_dbg(common, CHAN_CTX,
2702 "%s: Set chanctx state to FORCE_ACTIVE for vif: %pM\n",
2703 __func__, vif->addr);
2705 spin_lock_bh(&sc->chan_lock);
2706 sc->next_chan = avp->chanctx;
2707 sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
2708 spin_unlock_bh(&sc->chan_lock);
2710 ath_chanctx_set_next(sc, true);
2712 mutex_unlock(&sc->mutex);
2715 void ath9k_fill_chanctx_ops(void)
2717 if (!ath9k_is_chanctx_enabled())
2720 ath9k_ops.hw_scan = ath9k_hw_scan;
2721 ath9k_ops.cancel_hw_scan = ath9k_cancel_hw_scan;
2722 ath9k_ops.remain_on_channel = ath9k_remain_on_channel;
2723 ath9k_ops.cancel_remain_on_channel = ath9k_cancel_remain_on_channel;
2724 ath9k_ops.add_chanctx = ath9k_add_chanctx;
2725 ath9k_ops.remove_chanctx = ath9k_remove_chanctx;
2726 ath9k_ops.change_chanctx = ath9k_change_chanctx;
2727 ath9k_ops.assign_vif_chanctx = ath9k_assign_vif_chanctx;
2728 ath9k_ops.unassign_vif_chanctx = ath9k_unassign_vif_chanctx;
2729 ath9k_ops.mgd_prepare_tx = ath9k_mgd_prepare_tx;
2734 static int ath9k_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2737 struct ath_softc *sc = hw->priv;
2738 struct ath_vif *avp = (void *)vif->drv_priv;
2740 mutex_lock(&sc->mutex);
2742 *dbm = avp->chanctx->cur_txpower;
2744 *dbm = sc->cur_chan->cur_txpower;
2745 mutex_unlock(&sc->mutex);
2752 struct ieee80211_ops ath9k_ops = {
2754 .start = ath9k_start,
2756 .add_interface = ath9k_add_interface,
2757 .change_interface = ath9k_change_interface,
2758 .remove_interface = ath9k_remove_interface,
2759 .config = ath9k_config,
2760 .configure_filter = ath9k_configure_filter,
2761 .sta_state = ath9k_sta_state,
2762 .sta_notify = ath9k_sta_notify,
2763 .conf_tx = ath9k_conf_tx,
2764 .bss_info_changed = ath9k_bss_info_changed,
2765 .set_key = ath9k_set_key,
2766 .get_tsf = ath9k_get_tsf,
2767 .set_tsf = ath9k_set_tsf,
2768 .reset_tsf = ath9k_reset_tsf,
2769 .ampdu_action = ath9k_ampdu_action,
2770 .get_survey = ath9k_get_survey,
2771 .rfkill_poll = ath9k_rfkill_poll_state,
2772 .set_coverage_class = ath9k_set_coverage_class,
2773 .flush = ath9k_flush,
2774 .tx_frames_pending = ath9k_tx_frames_pending,
2775 .tx_last_beacon = ath9k_tx_last_beacon,
2776 .release_buffered_frames = ath9k_release_buffered_frames,
2777 .get_stats = ath9k_get_stats,
2778 .set_antenna = ath9k_set_antenna,
2779 .get_antenna = ath9k_get_antenna,
2781 #ifdef CONFIG_ATH9K_WOW
2782 .suspend = ath9k_suspend,
2783 .resume = ath9k_resume,
2784 .set_wakeup = ath9k_set_wakeup,
2787 #ifdef CONFIG_ATH9K_DEBUGFS
2788 .get_et_sset_count = ath9k_get_et_sset_count,
2789 .get_et_stats = ath9k_get_et_stats,
2790 .get_et_strings = ath9k_get_et_strings,
2793 #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
2794 .sta_add_debugfs = ath9k_sta_add_debugfs,
2796 .sw_scan_start = ath9k_sw_scan_start,
2797 .sw_scan_complete = ath9k_sw_scan_complete,
2798 .get_txpower = ath9k_get_txpower,
2799 .wake_tx_queue = ath9k_wake_tx_queue,