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 static void ath9k_set_assoc_state(struct ath_softc *sc,
23 struct ieee80211_vif *vif);
25 u8 ath9k_parse_mpdudensity(u8 mpdudensity)
28 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
29 * 0 for no restriction
38 switch (mpdudensity) {
44 /* Our lower layer calculations limit our precision to
60 static bool ath9k_has_pending_frames(struct ath_softc *sc, struct ath_txq *txq)
64 spin_lock_bh(&txq->axq_lock);
66 if (txq->axq_depth || !list_empty(&txq->axq_acq))
69 spin_unlock_bh(&txq->axq_lock);
73 static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
78 spin_lock_irqsave(&sc->sc_pm_lock, flags);
79 ret = ath9k_hw_setpower(sc->sc_ah, mode);
80 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
85 void ath_ps_full_sleep(unsigned long data)
87 struct ath_softc *sc = (struct ath_softc *) data;
88 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
91 spin_lock(&common->cc_lock);
92 ath_hw_cycle_counters_update(common);
93 spin_unlock(&common->cc_lock);
95 ath9k_hw_setrxabort(sc->sc_ah, 1);
96 ath9k_hw_stopdmarecv(sc->sc_ah, &reset);
98 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
101 void ath9k_ps_wakeup(struct ath_softc *sc)
103 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
105 enum ath9k_power_mode power_mode;
107 spin_lock_irqsave(&sc->sc_pm_lock, flags);
108 if (++sc->ps_usecount != 1)
111 del_timer_sync(&sc->sleep_timer);
112 power_mode = sc->sc_ah->power_mode;
113 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
116 * While the hardware is asleep, the cycle counters contain no
117 * useful data. Better clear them now so that they don't mess up
118 * survey data results.
120 if (power_mode != ATH9K_PM_AWAKE) {
121 spin_lock(&common->cc_lock);
122 ath_hw_cycle_counters_update(common);
123 memset(&common->cc_survey, 0, sizeof(common->cc_survey));
124 memset(&common->cc_ani, 0, sizeof(common->cc_ani));
125 spin_unlock(&common->cc_lock);
129 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
132 void ath9k_ps_restore(struct ath_softc *sc)
134 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
135 enum ath9k_power_mode mode;
138 spin_lock_irqsave(&sc->sc_pm_lock, flags);
139 if (--sc->ps_usecount != 0)
143 mod_timer(&sc->sleep_timer, jiffies + HZ / 10);
147 if (sc->ps_enabled &&
148 !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
150 PS_WAIT_FOR_PSPOLL_DATA |
153 mode = ATH9K_PM_NETWORK_SLEEP;
154 if (ath9k_hw_btcoex_is_enabled(sc->sc_ah))
155 ath9k_btcoex_stop_gen_timer(sc);
160 spin_lock(&common->cc_lock);
161 ath_hw_cycle_counters_update(common);
162 spin_unlock(&common->cc_lock);
164 ath9k_hw_setpower(sc->sc_ah, mode);
167 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
170 static void __ath_cancel_work(struct ath_softc *sc)
172 cancel_work_sync(&sc->paprd_work);
173 cancel_delayed_work_sync(&sc->tx_complete_work);
174 cancel_delayed_work_sync(&sc->hw_pll_work);
176 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
177 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
178 cancel_work_sync(&sc->mci_work);
182 void ath_cancel_work(struct ath_softc *sc)
184 __ath_cancel_work(sc);
185 cancel_work_sync(&sc->hw_reset_work);
188 void ath_restart_work(struct ath_softc *sc)
190 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
192 if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
193 ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
194 msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
199 static bool ath_prepare_reset(struct ath_softc *sc)
201 struct ath_hw *ah = sc->sc_ah;
204 ieee80211_stop_queues(sc->hw);
206 ath9k_hw_disable_interrupts(ah);
208 if (!ath_drain_all_txq(sc))
211 if (!ath_stoprecv(sc))
217 static bool ath_complete_reset(struct ath_softc *sc, bool start)
219 struct ath_hw *ah = sc->sc_ah;
220 struct ath_common *common = ath9k_hw_common(ah);
224 if (ath_startrecv(sc) != 0) {
225 ath_err(common, "Unable to restart recv logic\n");
229 ath9k_cmn_update_txpow(ah, sc->curtxpow,
230 sc->cur_chan->txpower, &sc->curtxpow);
232 clear_bit(ATH_OP_HW_RESET, &common->op_flags);
233 ath9k_hw_set_interrupts(ah);
234 ath9k_hw_enable_interrupts(ah);
236 if (!sc->cur_chan->offchannel && start) {
237 if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
240 if (ah->opmode == NL80211_IFTYPE_STATION &&
241 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
242 spin_lock_irqsave(&sc->sc_pm_lock, flags);
243 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
244 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
246 ath9k_set_beacon(sc);
249 ath_restart_work(sc);
251 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
252 if (!ATH_TXQ_SETUP(sc, i))
255 spin_lock_bh(&sc->tx.txq[i].axq_lock);
256 ath_txq_schedule(sc, &sc->tx.txq[i]);
257 spin_unlock_bh(&sc->tx.txq[i].axq_lock);
262 ieee80211_wake_queues(sc->hw);
264 ath9k_p2p_ps_timer(sc);
269 int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
271 struct ath_hw *ah = sc->sc_ah;
272 struct ath_common *common = ath9k_hw_common(ah);
273 struct ath9k_hw_cal_data *caldata = NULL;
277 __ath_cancel_work(sc);
279 tasklet_disable(&sc->intr_tq);
280 spin_lock_bh(&sc->sc_pcu_lock);
282 if (!sc->cur_chan->offchannel) {
284 caldata = &sc->caldata;
292 if (!ath_prepare_reset(sc))
295 ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
296 hchan->channel, IS_CHAN_HT40(hchan), fastcc);
298 r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
301 "Unable to reset channel, reset status %d\n", r);
303 ath9k_hw_enable_interrupts(ah);
304 ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
309 if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
310 sc->cur_chan->offchannel)
311 ath9k_mci_set_txpower(sc, true, false);
313 if (!ath_complete_reset(sc, true))
317 spin_unlock_bh(&sc->sc_pcu_lock);
318 tasklet_enable(&sc->intr_tq);
323 static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
324 struct ieee80211_vif *vif)
327 an = (struct ath_node *)sta->drv_priv;
332 memset(&an->key_idx, 0, sizeof(an->key_idx));
334 ath_tx_node_init(sc, an);
337 static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
339 struct ath_node *an = (struct ath_node *)sta->drv_priv;
340 ath_tx_node_cleanup(sc, an);
343 void ath9k_tasklet(unsigned long data)
345 struct ath_softc *sc = (struct ath_softc *)data;
346 struct ath_hw *ah = sc->sc_ah;
347 struct ath_common *common = ath9k_hw_common(ah);
348 enum ath_reset_type type;
350 u32 status = sc->intrstatus;
354 spin_lock(&sc->sc_pcu_lock);
356 if (status & ATH9K_INT_FATAL) {
357 type = RESET_TYPE_FATAL_INT;
358 ath9k_queue_reset(sc, type);
361 * Increment the ref. counter here so that
362 * interrupts are enabled in the reset routine.
364 atomic_inc(&ah->intr_ref_cnt);
365 ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
369 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
370 (status & ATH9K_INT_BB_WATCHDOG)) {
371 spin_lock(&common->cc_lock);
372 ath_hw_cycle_counters_update(common);
373 ar9003_hw_bb_watchdog_dbg_info(ah);
374 spin_unlock(&common->cc_lock);
376 if (ar9003_hw_bb_watchdog_check(ah)) {
377 type = RESET_TYPE_BB_WATCHDOG;
378 ath9k_queue_reset(sc, type);
381 * Increment the ref. counter here so that
382 * interrupts are enabled in the reset routine.
384 atomic_inc(&ah->intr_ref_cnt);
385 ath_dbg(common, RESET,
386 "BB_WATCHDOG: Skipping interrupts\n");
391 if (status & ATH9K_INT_GTT) {
394 if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
395 type = RESET_TYPE_TX_GTT;
396 ath9k_queue_reset(sc, type);
397 atomic_inc(&ah->intr_ref_cnt);
398 ath_dbg(common, RESET,
399 "GTT: Skipping interrupts\n");
404 spin_lock_irqsave(&sc->sc_pm_lock, flags);
405 if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
407 * TSF sync does not look correct; remain awake to sync with
410 ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
411 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
413 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
415 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
416 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
419 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
421 if (status & rxmask) {
422 /* Check for high priority Rx first */
423 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
424 (status & ATH9K_INT_RXHP))
425 ath_rx_tasklet(sc, 0, true);
427 ath_rx_tasklet(sc, 0, false);
430 if (status & ATH9K_INT_TX) {
431 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
433 * For EDMA chips, TX completion is enabled for the
434 * beacon queue, so if a beacon has been transmitted
435 * successfully after a GTT interrupt, the GTT counter
436 * gets reset to zero here.
440 ath_tx_edma_tasklet(sc);
445 wake_up(&sc->tx_wait);
448 if (status & ATH9K_INT_GENTIMER)
449 ath_gen_timer_isr(sc->sc_ah);
451 ath9k_btcoex_handle_interrupt(sc, status);
453 /* re-enable hardware interrupt */
454 ath9k_hw_enable_interrupts(ah);
456 spin_unlock(&sc->sc_pcu_lock);
457 ath9k_ps_restore(sc);
460 irqreturn_t ath_isr(int irq, void *dev)
462 #define SCHED_INTR ( \
464 ATH9K_INT_BB_WATCHDOG | \
475 ATH9K_INT_GENTIMER | \
478 struct ath_softc *sc = dev;
479 struct ath_hw *ah = sc->sc_ah;
480 struct ath_common *common = ath9k_hw_common(ah);
481 enum ath9k_int status;
486 * The hardware is not ready/present, don't
487 * touch anything. Note this can happen early
488 * on if the IRQ is shared.
490 if (test_bit(ATH_OP_INVALID, &common->op_flags))
493 /* shared irq, not for us */
495 if (!ath9k_hw_intrpend(ah))
498 if (test_bit(ATH_OP_HW_RESET, &common->op_flags)) {
499 ath9k_hw_kill_interrupts(ah);
504 * Figure out the reason(s) for the interrupt. Note
505 * that the hal returns a pseudo-ISR that may include
506 * bits we haven't explicitly enabled so we mask the
507 * value to insure we only process bits we requested.
509 ath9k_hw_getisr(ah, &status, &sync_cause); /* NB: clears ISR too */
510 ath9k_debug_sync_cause(sc, sync_cause);
511 status &= ah->imask; /* discard unasked-for bits */
514 * If there are no status bits set, then this interrupt was not
515 * for me (should have been caught above).
520 /* Cache the status */
521 sc->intrstatus = status;
523 if (status & SCHED_INTR)
527 * If a FATAL or RXORN interrupt is received, we have to reset the
530 if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
531 !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
534 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
535 (status & ATH9K_INT_BB_WATCHDOG))
538 #ifdef CONFIG_ATH9K_WOW
539 if (status & ATH9K_INT_BMISS) {
540 if (atomic_read(&sc->wow_sleep_proc_intr) == 0) {
541 atomic_inc(&sc->wow_got_bmiss_intr);
542 atomic_dec(&sc->wow_sleep_proc_intr);
547 if (status & ATH9K_INT_SWBA)
548 tasklet_schedule(&sc->bcon_tasklet);
550 if (status & ATH9K_INT_TXURN)
551 ath9k_hw_updatetxtriglevel(ah, true);
553 if (status & ATH9K_INT_RXEOL) {
554 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
555 ath9k_hw_set_interrupts(ah);
558 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
559 if (status & ATH9K_INT_TIM_TIMER) {
560 if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
562 /* Clear RxAbort bit so that we can
564 ath9k_setpower(sc, ATH9K_PM_AWAKE);
565 spin_lock(&sc->sc_pm_lock);
566 ath9k_hw_setrxabort(sc->sc_ah, 0);
567 sc->ps_flags |= PS_WAIT_FOR_BEACON;
568 spin_unlock(&sc->sc_pm_lock);
573 ath_debug_stat_interrupt(sc, status);
576 /* turn off every interrupt */
577 ath9k_hw_disable_interrupts(ah);
578 tasklet_schedule(&sc->intr_tq);
586 int ath_reset(struct ath_softc *sc)
591 r = ath_reset_internal(sc, NULL);
592 ath9k_ps_restore(sc);
597 void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
599 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
600 #ifdef CONFIG_ATH9K_DEBUGFS
601 RESET_STAT_INC(sc, type);
603 set_bit(ATH_OP_HW_RESET, &common->op_flags);
604 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
607 void ath_reset_work(struct work_struct *work)
609 struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
614 /**********************/
615 /* mac80211 callbacks */
616 /**********************/
618 static int ath9k_start(struct ieee80211_hw *hw)
620 struct ath_softc *sc = hw->priv;
621 struct ath_hw *ah = sc->sc_ah;
622 struct ath_common *common = ath9k_hw_common(ah);
623 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
624 struct ath_chanctx *ctx = sc->cur_chan;
625 struct ath9k_channel *init_channel;
628 ath_dbg(common, CONFIG,
629 "Starting driver with initial channel: %d MHz\n",
630 curchan->center_freq);
633 mutex_lock(&sc->mutex);
635 memcpy(&ctx->chandef, &hw->conf.chandef, sizeof(ctx->chandef));
636 init_channel = ath9k_cmn_get_channel(hw, ah, &ctx->chandef);
638 /* Reset SERDES registers */
639 ath9k_hw_configpcipowersave(ah, false);
642 * The basic interface to setting the hardware in a good
643 * state is ``reset''. On return the hardware is known to
644 * be powered up and with interrupts disabled. This must
645 * be followed by initialization of the appropriate bits
646 * and then setup of the interrupt mask.
648 spin_lock_bh(&sc->sc_pcu_lock);
650 atomic_set(&ah->intr_ref_cnt, -1);
652 r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
655 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
656 r, curchan->center_freq);
657 ah->reset_power_on = false;
660 /* Setup our intr mask. */
661 ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
662 ATH9K_INT_RXORN | ATH9K_INT_FATAL |
665 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
666 ah->imask |= ATH9K_INT_RXHP |
669 ah->imask |= ATH9K_INT_RX;
671 if (ah->config.hw_hang_checks & HW_BB_WATCHDOG)
672 ah->imask |= ATH9K_INT_BB_WATCHDOG;
675 * Enable GTT interrupts only for AR9003/AR9004 chips
678 if (AR_SREV_9300_20_OR_LATER(ah))
679 ah->imask |= ATH9K_INT_GTT;
681 if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
682 ah->imask |= ATH9K_INT_CST;
686 clear_bit(ATH_OP_INVALID, &common->op_flags);
687 sc->sc_ah->is_monitoring = false;
689 if (!ath_complete_reset(sc, false))
690 ah->reset_power_on = false;
692 if (ah->led_pin >= 0) {
693 ath9k_hw_cfg_output(ah, ah->led_pin,
694 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
695 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
699 * Reset key cache to sane defaults (all entries cleared) instead of
700 * semi-random values after suspend/resume.
702 ath9k_cmn_init_crypto(sc->sc_ah);
704 ath9k_hw_reset_tsf(ah);
706 spin_unlock_bh(&sc->sc_pcu_lock);
708 mutex_unlock(&sc->mutex);
710 ath9k_ps_restore(sc);
715 static void ath9k_tx(struct ieee80211_hw *hw,
716 struct ieee80211_tx_control *control,
719 struct ath_softc *sc = hw->priv;
720 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
721 struct ath_tx_control txctl;
722 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
725 if (sc->ps_enabled) {
727 * mac80211 does not set PM field for normal data frames, so we
728 * need to update that based on the current PS mode.
730 if (ieee80211_is_data(hdr->frame_control) &&
731 !ieee80211_is_nullfunc(hdr->frame_control) &&
732 !ieee80211_has_pm(hdr->frame_control)) {
734 "Add PM=1 for a TX frame while in PS mode\n");
735 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
739 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
741 * We are using PS-Poll and mac80211 can request TX while in
742 * power save mode. Need to wake up hardware for the TX to be
743 * completed and if needed, also for RX of buffered frames.
746 spin_lock_irqsave(&sc->sc_pm_lock, flags);
747 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
748 ath9k_hw_setrxabort(sc->sc_ah, 0);
749 if (ieee80211_is_pspoll(hdr->frame_control)) {
751 "Sending PS-Poll to pick a buffered frame\n");
752 sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
754 ath_dbg(common, PS, "Wake up to complete TX\n");
755 sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
758 * The actual restore operation will happen only after
759 * the ps_flags bit is cleared. We are just dropping
760 * the ps_usecount here.
762 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
763 ath9k_ps_restore(sc);
767 * Cannot tx while the hardware is in full sleep, it first needs a full
768 * chip reset to recover from that
770 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
771 ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
775 memset(&txctl, 0, sizeof(struct ath_tx_control));
776 txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
777 txctl.sta = control->sta;
779 ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
781 if (ath_tx_start(hw, skb, &txctl) != 0) {
782 ath_dbg(common, XMIT, "TX failed\n");
783 TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
789 ieee80211_free_txskb(hw, skb);
792 static void ath9k_stop(struct ieee80211_hw *hw)
794 struct ath_softc *sc = hw->priv;
795 struct ath_hw *ah = sc->sc_ah;
796 struct ath_common *common = ath9k_hw_common(ah);
799 mutex_lock(&sc->mutex);
803 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
804 ath_dbg(common, ANY, "Device not present\n");
805 mutex_unlock(&sc->mutex);
809 /* Ensure HW is awake when we try to shut it down. */
812 spin_lock_bh(&sc->sc_pcu_lock);
814 /* prevent tasklets to enable interrupts once we disable them */
815 ah->imask &= ~ATH9K_INT_GLOBAL;
817 /* make sure h/w will not generate any interrupt
818 * before setting the invalid flag. */
819 ath9k_hw_disable_interrupts(ah);
821 spin_unlock_bh(&sc->sc_pcu_lock);
823 /* we can now sync irq and kill any running tasklets, since we already
824 * disabled interrupts and not holding a spin lock */
825 synchronize_irq(sc->irq);
826 tasklet_kill(&sc->intr_tq);
827 tasklet_kill(&sc->bcon_tasklet);
829 prev_idle = sc->ps_idle;
832 spin_lock_bh(&sc->sc_pcu_lock);
834 if (ah->led_pin >= 0) {
835 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
836 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
839 ath_prepare_reset(sc);
842 dev_kfree_skb_any(sc->rx.frag);
847 ah->curchan = ath9k_cmn_get_channel(hw, ah,
848 &sc->cur_chan->chandef);
850 ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
851 ath9k_hw_phy_disable(ah);
853 ath9k_hw_configpcipowersave(ah, true);
855 spin_unlock_bh(&sc->sc_pcu_lock);
857 ath9k_ps_restore(sc);
859 set_bit(ATH_OP_INVALID, &common->op_flags);
860 sc->ps_idle = prev_idle;
862 mutex_unlock(&sc->mutex);
864 ath_dbg(common, CONFIG, "Driver halt\n");
867 static bool ath9k_uses_beacons(int type)
870 case NL80211_IFTYPE_AP:
871 case NL80211_IFTYPE_ADHOC:
872 case NL80211_IFTYPE_MESH_POINT:
879 static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
881 struct ath9k_vif_iter_data *iter_data = data;
884 if (iter_data->has_hw_macaddr) {
885 for (i = 0; i < ETH_ALEN; i++)
886 iter_data->mask[i] &=
887 ~(iter_data->hw_macaddr[i] ^ mac[i]);
889 memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
890 iter_data->has_hw_macaddr = true;
894 case NL80211_IFTYPE_AP:
897 case NL80211_IFTYPE_STATION:
898 iter_data->nstations++;
900 case NL80211_IFTYPE_ADHOC:
901 iter_data->nadhocs++;
903 case NL80211_IFTYPE_MESH_POINT:
904 iter_data->nmeshes++;
906 case NL80211_IFTYPE_WDS:
914 static void ath9k_sta_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
916 struct ath_softc *sc = data;
917 struct ath_vif *avp = (void *)vif->drv_priv;
919 if (vif->type != NL80211_IFTYPE_STATION)
922 if (avp->primary_sta_vif)
923 ath9k_set_assoc_state(sc, vif);
926 /* Called with sc->mutex held. */
927 void ath9k_calculate_iter_data(struct ieee80211_hw *hw,
928 struct ieee80211_vif *vif,
929 struct ath9k_vif_iter_data *iter_data)
931 struct ath_softc *sc = hw->priv;
932 struct ath_hw *ah = sc->sc_ah;
933 struct ath_common *common = ath9k_hw_common(ah);
936 * Pick the MAC address of the first interface as the new hardware
937 * MAC address. The hardware will use it together with the BSSID mask
938 * when matching addresses.
940 memset(iter_data, 0, sizeof(*iter_data));
941 memset(&iter_data->mask, 0xff, ETH_ALEN);
944 ath9k_vif_iter(iter_data, vif->addr, vif);
946 /* Get list of all active MAC addresses */
947 ieee80211_iterate_active_interfaces_atomic(
948 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
949 ath9k_vif_iter, iter_data);
951 memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
954 /* Called with sc->mutex held. */
955 static void ath9k_calculate_summary_state(struct ieee80211_hw *hw,
956 struct ieee80211_vif *vif)
958 struct ath_softc *sc = hw->priv;
959 struct ath_hw *ah = sc->sc_ah;
960 struct ath_common *common = ath9k_hw_common(ah);
961 struct ath9k_vif_iter_data iter_data;
962 enum nl80211_iftype old_opmode = ah->opmode;
964 ath9k_calculate_iter_data(hw, vif, &iter_data);
966 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
967 ath_hw_setbssidmask(common);
969 if (iter_data.naps > 0) {
970 ath9k_hw_set_tsfadjust(ah, true);
971 ah->opmode = NL80211_IFTYPE_AP;
973 ath9k_hw_set_tsfadjust(ah, false);
975 if (iter_data.nmeshes)
976 ah->opmode = NL80211_IFTYPE_MESH_POINT;
977 else if (iter_data.nwds)
978 ah->opmode = NL80211_IFTYPE_AP;
979 else if (iter_data.nadhocs)
980 ah->opmode = NL80211_IFTYPE_ADHOC;
982 ah->opmode = NL80211_IFTYPE_STATION;
985 ath9k_hw_setopmode(ah);
987 if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
988 ah->imask |= ATH9K_INT_TSFOOR;
990 ah->imask &= ~ATH9K_INT_TSFOOR;
992 ath9k_hw_set_interrupts(ah);
995 * If we are changing the opmode to STATION,
996 * a beacon sync needs to be done.
998 if (ah->opmode == NL80211_IFTYPE_STATION &&
999 old_opmode == NL80211_IFTYPE_AP &&
1000 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
1001 ieee80211_iterate_active_interfaces_atomic(
1002 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1003 ath9k_sta_vif_iter, sc);
1007 static int ath9k_add_interface(struct ieee80211_hw *hw,
1008 struct ieee80211_vif *vif)
1010 struct ath_softc *sc = hw->priv;
1011 struct ath_hw *ah = sc->sc_ah;
1012 struct ath_common *common = ath9k_hw_common(ah);
1013 struct ath_vif *avp = (void *)vif->drv_priv;
1014 struct ath_node *an = &avp->mcast_node;
1016 mutex_lock(&sc->mutex);
1018 if (config_enabled(CONFIG_ATH9K_TX99)) {
1019 if (sc->nvifs >= 1) {
1020 mutex_unlock(&sc->mutex);
1026 ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
1029 ath9k_ps_wakeup(sc);
1030 ath9k_calculate_summary_state(hw, vif);
1031 ath9k_ps_restore(sc);
1033 if (ath9k_uses_beacons(vif->type))
1034 ath9k_beacon_assign_slot(sc, vif);
1041 an->no_ps_filter = true;
1042 ath_tx_node_init(sc, an);
1044 mutex_unlock(&sc->mutex);
1048 static int ath9k_change_interface(struct ieee80211_hw *hw,
1049 struct ieee80211_vif *vif,
1050 enum nl80211_iftype new_type,
1053 struct ath_softc *sc = hw->priv;
1054 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1056 mutex_lock(&sc->mutex);
1058 if (config_enabled(CONFIG_ATH9K_TX99)) {
1059 mutex_unlock(&sc->mutex);
1063 ath_dbg(common, CONFIG, "Change Interface\n");
1065 if (ath9k_uses_beacons(vif->type))
1066 ath9k_beacon_remove_slot(sc, vif);
1068 vif->type = new_type;
1071 ath9k_ps_wakeup(sc);
1072 ath9k_calculate_summary_state(hw, vif);
1073 ath9k_ps_restore(sc);
1075 if (ath9k_uses_beacons(vif->type))
1076 ath9k_beacon_assign_slot(sc, vif);
1078 mutex_unlock(&sc->mutex);
1083 ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1085 struct ath_hw *ah = sc->sc_ah;
1086 s32 tsf, target_tsf;
1088 if (!avp || !avp->noa.has_next_tsf)
1091 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1093 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1095 target_tsf = avp->noa.next_tsf;
1096 if (!avp->noa.absent)
1097 target_tsf -= ATH_P2P_PS_STOP_TIME;
1099 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1100 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1102 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1105 static void ath9k_remove_interface(struct ieee80211_hw *hw,
1106 struct ieee80211_vif *vif)
1108 struct ath_softc *sc = hw->priv;
1109 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1110 struct ath_vif *avp = (void *)vif->drv_priv;
1112 ath_dbg(common, CONFIG, "Detach Interface\n");
1114 mutex_lock(&sc->mutex);
1116 spin_lock_bh(&sc->sc_pcu_lock);
1117 if (avp == sc->p2p_ps_vif) {
1118 sc->p2p_ps_vif = NULL;
1119 ath9k_update_p2p_ps_timer(sc, NULL);
1121 spin_unlock_bh(&sc->sc_pcu_lock);
1124 sc->tx99_vif = NULL;
1126 if (ath9k_uses_beacons(vif->type))
1127 ath9k_beacon_remove_slot(sc, vif);
1129 ath9k_ps_wakeup(sc);
1130 ath9k_calculate_summary_state(hw, NULL);
1131 ath9k_ps_restore(sc);
1133 ath_tx_node_cleanup(sc, &avp->mcast_node);
1135 mutex_unlock(&sc->mutex);
1138 static void ath9k_enable_ps(struct ath_softc *sc)
1140 struct ath_hw *ah = sc->sc_ah;
1141 struct ath_common *common = ath9k_hw_common(ah);
1143 if (config_enabled(CONFIG_ATH9K_TX99))
1146 sc->ps_enabled = true;
1147 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1148 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1149 ah->imask |= ATH9K_INT_TIM_TIMER;
1150 ath9k_hw_set_interrupts(ah);
1152 ath9k_hw_setrxabort(ah, 1);
1154 ath_dbg(common, PS, "PowerSave enabled\n");
1157 static void ath9k_disable_ps(struct ath_softc *sc)
1159 struct ath_hw *ah = sc->sc_ah;
1160 struct ath_common *common = ath9k_hw_common(ah);
1162 if (config_enabled(CONFIG_ATH9K_TX99))
1165 sc->ps_enabled = false;
1166 ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
1167 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1168 ath9k_hw_setrxabort(ah, 0);
1169 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1171 PS_WAIT_FOR_PSPOLL_DATA |
1172 PS_WAIT_FOR_TX_ACK);
1173 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1174 ah->imask &= ~ATH9K_INT_TIM_TIMER;
1175 ath9k_hw_set_interrupts(ah);
1178 ath_dbg(common, PS, "PowerSave disabled\n");
1181 void ath9k_spectral_scan_trigger(struct ieee80211_hw *hw)
1183 struct ath_softc *sc = hw->priv;
1184 struct ath_hw *ah = sc->sc_ah;
1185 struct ath_common *common = ath9k_hw_common(ah);
1188 if (config_enabled(CONFIG_ATH9K_TX99))
1191 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1192 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1196 ath9k_ps_wakeup(sc);
1197 rxfilter = ath9k_hw_getrxfilter(ah);
1198 ath9k_hw_setrxfilter(ah, rxfilter |
1199 ATH9K_RX_FILTER_PHYRADAR |
1200 ATH9K_RX_FILTER_PHYERR);
1202 /* TODO: usually this should not be neccesary, but for some reason
1203 * (or in some mode?) the trigger must be called after the
1204 * configuration, otherwise the register will have its values reset
1205 * (on my ar9220 to value 0x01002310)
1207 ath9k_spectral_scan_config(hw, sc->spectral_mode);
1208 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
1209 ath9k_ps_restore(sc);
1212 int ath9k_spectral_scan_config(struct ieee80211_hw *hw,
1213 enum spectral_mode spectral_mode)
1215 struct ath_softc *sc = hw->priv;
1216 struct ath_hw *ah = sc->sc_ah;
1217 struct ath_common *common = ath9k_hw_common(ah);
1219 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1220 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1224 switch (spectral_mode) {
1225 case SPECTRAL_DISABLED:
1226 sc->spec_config.enabled = 0;
1228 case SPECTRAL_BACKGROUND:
1229 /* send endless samples.
1230 * TODO: is this really useful for "background"?
1232 sc->spec_config.endless = 1;
1233 sc->spec_config.enabled = 1;
1235 case SPECTRAL_CHANSCAN:
1236 case SPECTRAL_MANUAL:
1237 sc->spec_config.endless = 0;
1238 sc->spec_config.enabled = 1;
1244 ath9k_ps_wakeup(sc);
1245 ath9k_hw_ops(ah)->spectral_scan_config(ah, &sc->spec_config);
1246 ath9k_ps_restore(sc);
1248 sc->spectral_mode = spectral_mode;
1253 static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
1255 struct ath_softc *sc = hw->priv;
1256 struct ath_hw *ah = sc->sc_ah;
1257 struct ath_common *common = ath9k_hw_common(ah);
1258 struct ieee80211_conf *conf = &hw->conf;
1259 struct ath_chanctx *ctx = sc->cur_chan;
1260 bool reset_channel = false;
1262 ath9k_ps_wakeup(sc);
1263 mutex_lock(&sc->mutex);
1265 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1266 sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1268 ath_cancel_work(sc);
1269 ath9k_stop_btcoex(sc);
1271 ath9k_start_btcoex(sc);
1273 * The chip needs a reset to properly wake up from
1276 reset_channel = ah->chip_fullsleep;
1281 * We just prepare to enable PS. We have to wait until our AP has
1282 * ACK'd our null data frame to disable RX otherwise we'll ignore
1283 * those ACKs and end up retransmitting the same null data frames.
1284 * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1286 if (changed & IEEE80211_CONF_CHANGE_PS) {
1287 unsigned long flags;
1288 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1289 if (conf->flags & IEEE80211_CONF_PS)
1290 ath9k_enable_ps(sc);
1292 ath9k_disable_ps(sc);
1293 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1296 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1297 if (conf->flags & IEEE80211_CONF_MONITOR) {
1298 ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
1299 sc->sc_ah->is_monitoring = true;
1301 ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
1302 sc->sc_ah->is_monitoring = false;
1306 if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || reset_channel) {
1307 ctx->offchannel = !!(conf->flags & IEEE80211_CONF_OFFCHANNEL);
1308 if (ath_chanctx_set_channel(sc, ctx, &hw->conf.chandef) < 0) {
1309 ath_err(common, "Unable to set channel\n");
1310 mutex_unlock(&sc->mutex);
1311 ath9k_ps_restore(sc);
1316 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1317 ath_dbg(common, CONFIG, "Set power: %d\n", conf->power_level);
1318 sc->cur_chan->txpower = 2 * conf->power_level;
1319 ath9k_cmn_update_txpow(ah, sc->curtxpow,
1320 sc->cur_chan->txpower, &sc->curtxpow);
1323 mutex_unlock(&sc->mutex);
1324 ath9k_ps_restore(sc);
1329 #define SUPPORTED_FILTERS \
1330 (FIF_PROMISC_IN_BSS | \
1335 FIF_BCN_PRBRESP_PROMISC | \
1339 /* FIXME: sc->sc_full_reset ? */
1340 static void ath9k_configure_filter(struct ieee80211_hw *hw,
1341 unsigned int changed_flags,
1342 unsigned int *total_flags,
1345 struct ath_softc *sc = hw->priv;
1348 changed_flags &= SUPPORTED_FILTERS;
1349 *total_flags &= SUPPORTED_FILTERS;
1351 sc->rx.rxfilter = *total_flags;
1352 ath9k_ps_wakeup(sc);
1353 rfilt = ath_calcrxfilter(sc);
1354 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1355 ath9k_ps_restore(sc);
1357 ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
1361 static int ath9k_sta_add(struct ieee80211_hw *hw,
1362 struct ieee80211_vif *vif,
1363 struct ieee80211_sta *sta)
1365 struct ath_softc *sc = hw->priv;
1366 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1367 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1368 struct ieee80211_key_conf ps_key = { };
1371 ath_node_attach(sc, sta, vif);
1373 if (vif->type != NL80211_IFTYPE_AP &&
1374 vif->type != NL80211_IFTYPE_AP_VLAN)
1377 key = ath_key_config(common, vif, sta, &ps_key);
1380 an->key_idx[0] = key;
1386 static void ath9k_del_ps_key(struct ath_softc *sc,
1387 struct ieee80211_vif *vif,
1388 struct ieee80211_sta *sta)
1390 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1391 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1392 struct ieee80211_key_conf ps_key = { .hw_key_idx = an->ps_key };
1397 ath_key_delete(common, &ps_key);
1402 static int ath9k_sta_remove(struct ieee80211_hw *hw,
1403 struct ieee80211_vif *vif,
1404 struct ieee80211_sta *sta)
1406 struct ath_softc *sc = hw->priv;
1408 ath9k_del_ps_key(sc, vif, sta);
1409 ath_node_detach(sc, sta);
1414 static void ath9k_sta_set_tx_filter(struct ath_hw *ah,
1415 struct ath_node *an,
1420 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1421 if (!an->key_idx[i])
1423 ath9k_hw_set_tx_filter(ah, an->key_idx[i], set);
1427 static void ath9k_sta_notify(struct ieee80211_hw *hw,
1428 struct ieee80211_vif *vif,
1429 enum sta_notify_cmd cmd,
1430 struct ieee80211_sta *sta)
1432 struct ath_softc *sc = hw->priv;
1433 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1436 case STA_NOTIFY_SLEEP:
1437 an->sleeping = true;
1438 ath_tx_aggr_sleep(sta, sc, an);
1439 ath9k_sta_set_tx_filter(sc->sc_ah, an, true);
1441 case STA_NOTIFY_AWAKE:
1442 ath9k_sta_set_tx_filter(sc->sc_ah, an, false);
1443 an->sleeping = false;
1444 ath_tx_aggr_wakeup(sc, an);
1449 static int ath9k_conf_tx(struct ieee80211_hw *hw,
1450 struct ieee80211_vif *vif, u16 queue,
1451 const struct ieee80211_tx_queue_params *params)
1453 struct ath_softc *sc = hw->priv;
1454 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1455 struct ath_txq *txq;
1456 struct ath9k_tx_queue_info qi;
1459 if (queue >= IEEE80211_NUM_ACS)
1462 txq = sc->tx.txq_map[queue];
1464 ath9k_ps_wakeup(sc);
1465 mutex_lock(&sc->mutex);
1467 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1469 qi.tqi_aifs = params->aifs;
1470 qi.tqi_cwmin = params->cw_min;
1471 qi.tqi_cwmax = params->cw_max;
1472 qi.tqi_burstTime = params->txop * 32;
1474 ath_dbg(common, CONFIG,
1475 "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1476 queue, txq->axq_qnum, params->aifs, params->cw_min,
1477 params->cw_max, params->txop);
1479 ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
1480 ret = ath_txq_update(sc, txq->axq_qnum, &qi);
1482 ath_err(common, "TXQ Update failed\n");
1484 mutex_unlock(&sc->mutex);
1485 ath9k_ps_restore(sc);
1490 static int ath9k_set_key(struct ieee80211_hw *hw,
1491 enum set_key_cmd cmd,
1492 struct ieee80211_vif *vif,
1493 struct ieee80211_sta *sta,
1494 struct ieee80211_key_conf *key)
1496 struct ath_softc *sc = hw->priv;
1497 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1498 struct ath_node *an = NULL;
1501 if (ath9k_modparam_nohwcrypt)
1504 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1505 vif->type == NL80211_IFTYPE_MESH_POINT) &&
1506 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1507 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1508 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1510 * For now, disable hw crypto for the RSN IBSS group keys. This
1511 * could be optimized in the future to use a modified key cache
1512 * design to support per-STA RX GTK, but until that gets
1513 * implemented, use of software crypto for group addressed
1514 * frames is a acceptable to allow RSN IBSS to be used.
1519 mutex_lock(&sc->mutex);
1520 ath9k_ps_wakeup(sc);
1521 ath_dbg(common, CONFIG, "Set HW Key %d\n", cmd);
1523 an = (struct ath_node *)sta->drv_priv;
1528 ath9k_del_ps_key(sc, vif, sta);
1530 key->hw_key_idx = 0;
1531 ret = ath_key_config(common, vif, sta, key);
1533 key->hw_key_idx = ret;
1534 /* push IV and Michael MIC generation to stack */
1535 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1536 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1537 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1538 if (sc->sc_ah->sw_mgmt_crypto &&
1539 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1540 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1543 if (an && key->hw_key_idx) {
1544 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1547 an->key_idx[i] = key->hw_key_idx;
1550 WARN_ON(i == ARRAY_SIZE(an->key_idx));
1554 ath_key_delete(common, key);
1556 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1557 if (an->key_idx[i] != key->hw_key_idx)
1563 key->hw_key_idx = 0;
1569 ath9k_ps_restore(sc);
1570 mutex_unlock(&sc->mutex);
1575 static void ath9k_set_assoc_state(struct ath_softc *sc,
1576 struct ieee80211_vif *vif)
1578 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1579 struct ath_vif *avp = (void *)vif->drv_priv;
1580 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1581 unsigned long flags;
1583 set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1584 avp->primary_sta_vif = true;
1587 * Set the AID, BSSID and do beacon-sync only when
1588 * the HW opmode is STATION.
1590 * But the primary bit is set above in any case.
1592 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
1595 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1596 common->curaid = bss_conf->aid;
1597 ath9k_hw_write_associd(sc->sc_ah);
1599 common->last_rssi = ATH_RSSI_DUMMY_MARKER;
1600 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
1602 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1603 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
1604 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1606 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1607 ath9k_mci_update_wlan_channels(sc, false);
1609 ath_dbg(common, CONFIG,
1610 "Primary Station interface: %pM, BSSID: %pM\n",
1611 vif->addr, common->curbssid);
1614 static void ath9k_bss_assoc_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1616 struct ath_softc *sc = data;
1617 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1618 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1620 if (test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
1623 if (bss_conf->assoc)
1624 ath9k_set_assoc_state(sc, vif);
1627 void ath9k_p2p_ps_timer(void *priv)
1629 struct ath_softc *sc = priv;
1630 struct ath_vif *avp = sc->p2p_ps_vif;
1631 struct ieee80211_vif *vif;
1632 struct ieee80211_sta *sta;
1633 struct ath_node *an;
1639 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1640 if (!avp->noa.absent)
1641 tsf += ATH_P2P_PS_STOP_TIME;
1643 if (!avp->noa.has_next_tsf ||
1644 avp->noa.next_tsf - tsf > BIT(31))
1645 ieee80211_update_p2p_noa(&avp->noa, tsf);
1647 ath9k_update_p2p_ps_timer(sc, avp);
1652 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1656 an = (void *) sta->drv_priv;
1657 if (an->sleeping == !!avp->noa.absent)
1660 an->sleeping = avp->noa.absent;
1662 ath_tx_aggr_sleep(sta, sc, an);
1664 ath_tx_aggr_wakeup(sc, an);
1670 void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1672 struct ath_vif *avp = (void *)vif->drv_priv;
1675 if (!sc->p2p_ps_timer)
1678 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1681 sc->p2p_ps_vif = avp;
1682 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1683 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1684 ath9k_update_p2p_ps_timer(sc, avp);
1687 static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1688 struct ieee80211_vif *vif,
1689 struct ieee80211_bss_conf *bss_conf,
1693 (BSS_CHANGED_ASSOC | \
1694 BSS_CHANGED_IBSS | \
1695 BSS_CHANGED_BEACON_ENABLED)
1697 struct ath_softc *sc = hw->priv;
1698 struct ath_hw *ah = sc->sc_ah;
1699 struct ath_common *common = ath9k_hw_common(ah);
1700 struct ath_vif *avp = (void *)vif->drv_priv;
1701 unsigned long flags;
1704 ath9k_ps_wakeup(sc);
1705 mutex_lock(&sc->mutex);
1707 if (changed & BSS_CHANGED_ASSOC) {
1708 ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
1709 bss_conf->bssid, bss_conf->assoc);
1711 if (avp->primary_sta_vif && !bss_conf->assoc) {
1712 clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1713 avp->primary_sta_vif = false;
1715 if (ah->opmode == NL80211_IFTYPE_STATION)
1716 clear_bit(ATH_OP_BEACONS, &common->op_flags);
1719 ieee80211_iterate_active_interfaces_atomic(
1720 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1721 ath9k_bss_assoc_iter, sc);
1723 if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags) &&
1724 ah->opmode == NL80211_IFTYPE_STATION) {
1725 memset(common->curbssid, 0, ETH_ALEN);
1727 ath9k_hw_write_associd(sc->sc_ah);
1728 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1729 ath9k_mci_update_wlan_channels(sc, true);
1733 if (changed & BSS_CHANGED_IBSS) {
1734 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1735 common->curaid = bss_conf->aid;
1736 ath9k_hw_write_associd(sc->sc_ah);
1739 if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
1740 (changed & BSS_CHANGED_BEACON_INT))
1741 ath9k_beacon_config(sc, vif, changed);
1743 if (changed & BSS_CHANGED_ERP_SLOT) {
1744 if (bss_conf->use_short_slot)
1748 if (vif->type == NL80211_IFTYPE_AP) {
1750 * Defer update, so that connected stations can adjust
1751 * their settings at the same time.
1752 * See beacon.c for more details
1754 sc->beacon.slottime = slottime;
1755 sc->beacon.updateslot = UPDATE;
1757 ah->slottime = slottime;
1758 ath9k_hw_init_global_settings(ah);
1762 if (changed & BSS_CHANGED_P2P_PS) {
1763 spin_lock_bh(&sc->sc_pcu_lock);
1764 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1765 if (!(sc->ps_flags & PS_BEACON_SYNC))
1766 ath9k_update_p2p_ps(sc, vif);
1767 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1768 spin_unlock_bh(&sc->sc_pcu_lock);
1771 if (changed & CHECK_ANI)
1774 mutex_unlock(&sc->mutex);
1775 ath9k_ps_restore(sc);
1780 static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1782 struct ath_softc *sc = hw->priv;
1785 mutex_lock(&sc->mutex);
1786 ath9k_ps_wakeup(sc);
1787 tsf = ath9k_hw_gettsf64(sc->sc_ah);
1788 ath9k_ps_restore(sc);
1789 mutex_unlock(&sc->mutex);
1794 static void ath9k_set_tsf(struct ieee80211_hw *hw,
1795 struct ieee80211_vif *vif,
1798 struct ath_softc *sc = hw->priv;
1800 mutex_lock(&sc->mutex);
1801 ath9k_ps_wakeup(sc);
1802 ath9k_hw_settsf64(sc->sc_ah, tsf);
1803 ath9k_ps_restore(sc);
1804 mutex_unlock(&sc->mutex);
1807 static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1809 struct ath_softc *sc = hw->priv;
1811 mutex_lock(&sc->mutex);
1813 ath9k_ps_wakeup(sc);
1814 ath9k_hw_reset_tsf(sc->sc_ah);
1815 ath9k_ps_restore(sc);
1817 mutex_unlock(&sc->mutex);
1820 static int ath9k_ampdu_action(struct ieee80211_hw *hw,
1821 struct ieee80211_vif *vif,
1822 enum ieee80211_ampdu_mlme_action action,
1823 struct ieee80211_sta *sta,
1824 u16 tid, u16 *ssn, u8 buf_size)
1826 struct ath_softc *sc = hw->priv;
1830 mutex_lock(&sc->mutex);
1833 case IEEE80211_AMPDU_RX_START:
1835 case IEEE80211_AMPDU_RX_STOP:
1837 case IEEE80211_AMPDU_TX_START:
1838 ath9k_ps_wakeup(sc);
1839 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
1841 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1842 ath9k_ps_restore(sc);
1844 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1845 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1847 case IEEE80211_AMPDU_TX_STOP_CONT:
1848 ath9k_ps_wakeup(sc);
1849 ath_tx_aggr_stop(sc, sta, tid);
1851 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1852 ath9k_ps_restore(sc);
1854 case IEEE80211_AMPDU_TX_OPERATIONAL:
1855 ath9k_ps_wakeup(sc);
1856 ath_tx_aggr_resume(sc, sta, tid);
1857 ath9k_ps_restore(sc);
1860 ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
1863 mutex_unlock(&sc->mutex);
1868 static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
1869 struct survey_info *survey)
1871 struct ath_softc *sc = hw->priv;
1872 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1873 struct ieee80211_supported_band *sband;
1874 struct ieee80211_channel *chan;
1877 if (config_enabled(CONFIG_ATH9K_TX99))
1880 spin_lock_bh(&common->cc_lock);
1882 ath_update_survey_stats(sc);
1884 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
1885 if (sband && idx >= sband->n_channels) {
1886 idx -= sband->n_channels;
1891 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
1893 if (!sband || idx >= sband->n_channels) {
1894 spin_unlock_bh(&common->cc_lock);
1898 chan = &sband->channels[idx];
1899 pos = chan->hw_value;
1900 memcpy(survey, &sc->survey[pos], sizeof(*survey));
1901 survey->channel = chan;
1902 spin_unlock_bh(&common->cc_lock);
1907 static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
1909 struct ath_softc *sc = hw->priv;
1910 struct ath_hw *ah = sc->sc_ah;
1912 if (config_enabled(CONFIG_ATH9K_TX99))
1915 mutex_lock(&sc->mutex);
1916 ah->coverage_class = coverage_class;
1918 ath9k_ps_wakeup(sc);
1919 ath9k_hw_init_global_settings(ah);
1920 ath9k_ps_restore(sc);
1922 mutex_unlock(&sc->mutex);
1925 static bool ath9k_has_tx_pending(struct ath_softc *sc)
1929 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1930 if (!ATH_TXQ_SETUP(sc, i))
1933 if (!sc->tx.txq[i].axq_depth)
1936 npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i]);
1944 static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1945 u32 queues, bool drop)
1947 struct ath_softc *sc = hw->priv;
1948 struct ath_hw *ah = sc->sc_ah;
1949 struct ath_common *common = ath9k_hw_common(ah);
1950 int timeout = HZ / 5; /* 200 ms */
1953 mutex_lock(&sc->mutex);
1954 cancel_delayed_work_sync(&sc->tx_complete_work);
1956 if (ah->ah_flags & AH_UNPLUGGED) {
1957 ath_dbg(common, ANY, "Device has been unplugged!\n");
1958 mutex_unlock(&sc->mutex);
1962 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
1963 ath_dbg(common, ANY, "Device not present\n");
1964 mutex_unlock(&sc->mutex);
1968 if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc),
1973 ath9k_ps_wakeup(sc);
1974 spin_lock_bh(&sc->sc_pcu_lock);
1975 drain_txq = ath_drain_all_txq(sc);
1976 spin_unlock_bh(&sc->sc_pcu_lock);
1981 ath9k_ps_restore(sc);
1982 ieee80211_wake_queues(hw);
1985 ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
1986 mutex_unlock(&sc->mutex);
1989 static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
1991 struct ath_softc *sc = hw->priv;
1994 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1995 if (!ATH_TXQ_SETUP(sc, i))
1998 if (ath9k_has_pending_frames(sc, &sc->tx.txq[i]))
2004 static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
2006 struct ath_softc *sc = hw->priv;
2007 struct ath_hw *ah = sc->sc_ah;
2008 struct ieee80211_vif *vif;
2009 struct ath_vif *avp;
2011 struct ath_tx_status ts;
2012 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
2015 vif = sc->beacon.bslot[0];
2019 if (!vif->bss_conf.enable_beacon)
2022 avp = (void *)vif->drv_priv;
2024 if (!sc->beacon.tx_processed && !edma) {
2025 tasklet_disable(&sc->bcon_tasklet);
2028 if (!bf || !bf->bf_mpdu)
2031 status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
2032 if (status == -EINPROGRESS)
2035 sc->beacon.tx_processed = true;
2036 sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
2039 tasklet_enable(&sc->bcon_tasklet);
2042 return sc->beacon.tx_last;
2045 static int ath9k_get_stats(struct ieee80211_hw *hw,
2046 struct ieee80211_low_level_stats *stats)
2048 struct ath_softc *sc = hw->priv;
2049 struct ath_hw *ah = sc->sc_ah;
2050 struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
2052 stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
2053 stats->dot11RTSFailureCount = mib_stats->rts_bad;
2054 stats->dot11FCSErrorCount = mib_stats->fcs_bad;
2055 stats->dot11RTSSuccessCount = mib_stats->rts_good;
2059 static u32 fill_chainmask(u32 cap, u32 new)
2064 for (i = 0; cap && new; i++, cap >>= 1) {
2065 if (!(cap & BIT(0)))
2077 static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
2079 if (AR_SREV_9300_20_OR_LATER(ah))
2082 switch (val & 0x7) {
2088 return (ah->caps.rx_chainmask == 1);
2094 static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2096 struct ath_softc *sc = hw->priv;
2097 struct ath_hw *ah = sc->sc_ah;
2099 if (ah->caps.rx_chainmask != 1)
2102 if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
2105 sc->ant_rx = rx_ant;
2106 sc->ant_tx = tx_ant;
2108 if (ah->caps.rx_chainmask == 1)
2111 /* AR9100 runs into calibration issues if not all rx chains are enabled */
2112 if (AR_SREV_9100(ah))
2113 ah->rxchainmask = 0x7;
2115 ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
2117 ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
2118 ath9k_cmn_reload_chainmask(ah);
2123 static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2125 struct ath_softc *sc = hw->priv;
2127 *tx_ant = sc->ant_tx;
2128 *rx_ant = sc->ant_rx;
2132 static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2134 struct ath_softc *sc = hw->priv;
2135 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2136 set_bit(ATH_OP_SCANNING, &common->op_flags);
2139 static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2141 struct ath_softc *sc = hw->priv;
2142 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2143 clear_bit(ATH_OP_SCANNING, &common->op_flags);
2146 struct ieee80211_ops ath9k_ops = {
2148 .start = ath9k_start,
2150 .add_interface = ath9k_add_interface,
2151 .change_interface = ath9k_change_interface,
2152 .remove_interface = ath9k_remove_interface,
2153 .config = ath9k_config,
2154 .configure_filter = ath9k_configure_filter,
2155 .sta_add = ath9k_sta_add,
2156 .sta_remove = ath9k_sta_remove,
2157 .sta_notify = ath9k_sta_notify,
2158 .conf_tx = ath9k_conf_tx,
2159 .bss_info_changed = ath9k_bss_info_changed,
2160 .set_key = ath9k_set_key,
2161 .get_tsf = ath9k_get_tsf,
2162 .set_tsf = ath9k_set_tsf,
2163 .reset_tsf = ath9k_reset_tsf,
2164 .ampdu_action = ath9k_ampdu_action,
2165 .get_survey = ath9k_get_survey,
2166 .rfkill_poll = ath9k_rfkill_poll_state,
2167 .set_coverage_class = ath9k_set_coverage_class,
2168 .flush = ath9k_flush,
2169 .tx_frames_pending = ath9k_tx_frames_pending,
2170 .tx_last_beacon = ath9k_tx_last_beacon,
2171 .release_buffered_frames = ath9k_release_buffered_frames,
2172 .get_stats = ath9k_get_stats,
2173 .set_antenna = ath9k_set_antenna,
2174 .get_antenna = ath9k_get_antenna,
2176 #ifdef CONFIG_ATH9K_WOW
2177 .suspend = ath9k_suspend,
2178 .resume = ath9k_resume,
2179 .set_wakeup = ath9k_set_wakeup,
2182 #ifdef CONFIG_ATH9K_DEBUGFS
2183 .get_et_sset_count = ath9k_get_et_sset_count,
2184 .get_et_stats = ath9k_get_et_stats,
2185 .get_et_strings = ath9k_get_et_strings,
2188 #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
2189 .sta_add_debugfs = ath9k_sta_add_debugfs,
2191 .sw_scan_start = ath9k_sw_scan_start,
2192 .sw_scan_complete = ath9k_sw_scan_complete,