2 * Copyright (c) 2008-2009 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/pm_qos_params.h>
22 static void ath_update_txpow(struct ath_softc *sc)
24 struct ath_hw *ah = sc->sc_ah;
26 if (sc->curtxpow != sc->config.txpowlimit) {
27 ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
28 /* read back in case value is clamped */
29 sc->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
33 static u8 parse_mpdudensity(u8 mpdudensity)
36 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
37 * 0 for no restriction
46 switch (mpdudensity) {
52 /* Our lower layer calculations limit our precision to
68 static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
69 struct ieee80211_hw *hw)
71 struct ieee80211_channel *curchan = hw->conf.channel;
72 struct ath9k_channel *channel;
75 chan_idx = curchan->hw_value;
76 channel = &sc->sc_ah->channels[chan_idx];
77 ath9k_update_ichannel(sc, hw, channel);
81 bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
86 spin_lock_irqsave(&sc->sc_pm_lock, flags);
87 ret = ath9k_hw_setpower(sc->sc_ah, mode);
88 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
93 void ath9k_ps_wakeup(struct ath_softc *sc)
95 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
98 spin_lock_irqsave(&sc->sc_pm_lock, flags);
99 if (++sc->ps_usecount != 1)
102 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
105 * While the hardware is asleep, the cycle counters contain no
106 * useful data. Better clear them now so that they don't mess up
107 * survey data results.
109 spin_lock(&common->cc_lock);
110 ath_hw_cycle_counters_update(common);
111 memset(&common->cc_survey, 0, sizeof(common->cc_survey));
112 spin_unlock(&common->cc_lock);
115 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
118 void ath9k_ps_restore(struct ath_softc *sc)
120 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
123 spin_lock_irqsave(&sc->sc_pm_lock, flags);
124 if (--sc->ps_usecount != 0)
127 spin_lock(&common->cc_lock);
128 ath_hw_cycle_counters_update(common);
129 spin_unlock(&common->cc_lock);
132 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
133 else if (sc->ps_enabled &&
134 !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
136 PS_WAIT_FOR_PSPOLL_DATA |
137 PS_WAIT_FOR_TX_ACK)))
138 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
141 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
144 static void ath_start_ani(struct ath_common *common)
146 struct ath_hw *ah = common->ah;
147 unsigned long timestamp = jiffies_to_msecs(jiffies);
148 struct ath_softc *sc = (struct ath_softc *) common->priv;
150 if (!(sc->sc_flags & SC_OP_ANI_RUN))
153 if (sc->sc_flags & SC_OP_OFFCHANNEL)
156 common->ani.longcal_timer = timestamp;
157 common->ani.shortcal_timer = timestamp;
158 common->ani.checkani_timer = timestamp;
160 mod_timer(&common->ani.timer,
162 msecs_to_jiffies((u32)ah->config.ani_poll_interval));
165 static void ath_update_survey_nf(struct ath_softc *sc, int channel)
167 struct ath_hw *ah = sc->sc_ah;
168 struct ath9k_channel *chan = &ah->channels[channel];
169 struct survey_info *survey = &sc->survey[channel];
171 if (chan->noisefloor) {
172 survey->filled |= SURVEY_INFO_NOISE_DBM;
173 survey->noise = chan->noisefloor;
177 static void ath_update_survey_stats(struct ath_softc *sc)
179 struct ath_hw *ah = sc->sc_ah;
180 struct ath_common *common = ath9k_hw_common(ah);
181 int pos = ah->curchan - &ah->channels[0];
182 struct survey_info *survey = &sc->survey[pos];
183 struct ath_cycle_counters *cc = &common->cc_survey;
184 unsigned int div = common->clockrate * 1000;
189 if (ah->power_mode == ATH9K_PM_AWAKE)
190 ath_hw_cycle_counters_update(common);
192 if (cc->cycles > 0) {
193 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
194 SURVEY_INFO_CHANNEL_TIME_BUSY |
195 SURVEY_INFO_CHANNEL_TIME_RX |
196 SURVEY_INFO_CHANNEL_TIME_TX;
197 survey->channel_time += cc->cycles / div;
198 survey->channel_time_busy += cc->rx_busy / div;
199 survey->channel_time_rx += cc->rx_frame / div;
200 survey->channel_time_tx += cc->tx_frame / div;
202 memset(cc, 0, sizeof(*cc));
204 ath_update_survey_nf(sc, pos);
208 * Set/change channels. If the channel is really being changed, it's done
209 * by reseting the chip. To accomplish this we must first cleanup any pending
210 * DMA, then restart stuff.
212 int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
213 struct ath9k_channel *hchan)
215 struct ath_wiphy *aphy = hw->priv;
216 struct ath_hw *ah = sc->sc_ah;
217 struct ath_common *common = ath9k_hw_common(ah);
218 struct ieee80211_conf *conf = &common->hw->conf;
219 bool fastcc = true, stopped;
220 struct ieee80211_channel *channel = hw->conf.channel;
221 struct ath9k_hw_cal_data *caldata = NULL;
224 if (sc->sc_flags & SC_OP_INVALID)
227 del_timer_sync(&common->ani.timer);
228 cancel_work_sync(&sc->paprd_work);
229 cancel_work_sync(&sc->hw_check_work);
230 cancel_delayed_work_sync(&sc->tx_complete_work);
235 * This is only performed if the channel settings have
238 * To switch channels clear any pending DMA operations;
239 * wait long enough for the RX fifo to drain, reset the
240 * hardware at the new frequency, and then re-enable
241 * the relevant bits of the h/w.
243 ath9k_hw_set_interrupts(ah, 0);
244 ath_drain_all_txq(sc, false);
246 spin_lock_bh(&sc->rx.pcu_lock);
248 stopped = ath_stoprecv(sc);
250 /* XXX: do not flush receive queue here. We don't want
251 * to flush data frames already in queue because of
252 * changing channel. */
254 if (!stopped || !(sc->sc_flags & SC_OP_OFFCHANNEL))
257 if (!(sc->sc_flags & SC_OP_OFFCHANNEL))
258 caldata = &aphy->caldata;
260 ath_print(common, ATH_DBG_CONFIG,
261 "(%u MHz) -> (%u MHz), conf_is_ht40: %d fastcc: %d\n",
262 sc->sc_ah->curchan->channel,
263 channel->center_freq, conf_is_ht40(conf),
266 spin_lock_bh(&sc->sc_resetlock);
268 r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
270 ath_print(common, ATH_DBG_FATAL,
271 "Unable to reset channel (%u MHz), "
273 channel->center_freq, r);
274 spin_unlock_bh(&sc->sc_resetlock);
275 spin_unlock_bh(&sc->rx.pcu_lock);
278 spin_unlock_bh(&sc->sc_resetlock);
280 if (ath_startrecv(sc) != 0) {
281 ath_print(common, ATH_DBG_FATAL,
282 "Unable to restart recv logic\n");
284 spin_unlock_bh(&sc->rx.pcu_lock);
288 spin_unlock_bh(&sc->rx.pcu_lock);
290 ath_update_txpow(sc);
291 ath9k_hw_set_interrupts(ah, ah->imask);
293 if (!(sc->sc_flags & (SC_OP_OFFCHANNEL))) {
294 ath_beacon_config(sc, NULL);
295 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
296 ath_start_ani(common);
300 ath9k_ps_restore(sc);
304 static void ath_paprd_activate(struct ath_softc *sc)
306 struct ath_hw *ah = sc->sc_ah;
307 struct ath9k_hw_cal_data *caldata = ah->caldata;
308 struct ath_common *common = ath9k_hw_common(ah);
311 if (!caldata || !caldata->paprd_done)
315 ar9003_paprd_enable(ah, false);
316 for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
317 if (!(common->tx_chainmask & BIT(chain)))
320 ar9003_paprd_populate_single_table(ah, caldata, chain);
323 ar9003_paprd_enable(ah, true);
324 ath9k_ps_restore(sc);
327 void ath_paprd_calibrate(struct work_struct *work)
329 struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
330 struct ieee80211_hw *hw = sc->hw;
331 struct ath_hw *ah = sc->sc_ah;
332 struct ieee80211_hdr *hdr;
333 struct sk_buff *skb = NULL;
334 struct ieee80211_tx_info *tx_info;
335 int band = hw->conf.channel->band;
336 struct ieee80211_supported_band *sband = &sc->sbands[band];
337 struct ath_tx_control txctl;
338 struct ath9k_hw_cal_data *caldata = ah->caldata;
339 struct ath_common *common = ath9k_hw_common(ah);
350 skb = alloc_skb(len, GFP_KERNEL);
354 tx_info = IEEE80211_SKB_CB(skb);
357 memset(skb->data, 0, len);
358 hdr = (struct ieee80211_hdr *)skb->data;
359 ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
360 hdr->frame_control = cpu_to_le16(ftype);
361 hdr->duration_id = cpu_to_le16(10);
362 memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
363 memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
364 memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
366 memset(&txctl, 0, sizeof(txctl));
367 qnum = sc->tx.hwq_map[WME_AC_BE];
368 txctl.txq = &sc->tx.txq[qnum];
371 ar9003_paprd_init_table(ah);
372 for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
373 if (!(common->tx_chainmask & BIT(chain)))
377 memset(tx_info, 0, sizeof(*tx_info));
378 tx_info->band = band;
380 for (i = 0; i < 4; i++) {
381 tx_info->control.rates[i].idx = sband->n_bitrates - 1;
382 tx_info->control.rates[i].count = 6;
385 init_completion(&sc->paprd_complete);
386 ar9003_paprd_setup_gain_table(ah, chain);
387 txctl.paprd = BIT(chain);
388 if (ath_tx_start(hw, skb, &txctl) != 0)
391 time_left = wait_for_completion_timeout(&sc->paprd_complete,
392 msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
394 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
395 "Timeout waiting for paprd training on "
401 if (!ar9003_paprd_is_done(ah))
404 if (ar9003_paprd_create_curve(ah, caldata, chain) != 0)
412 caldata->paprd_done = true;
413 ath_paprd_activate(sc);
417 ath9k_ps_restore(sc);
421 * This routine performs the periodic noise floor calibration function
422 * that is used to adjust and optimize the chip performance. This
423 * takes environmental changes (location, temperature) into account.
424 * When the task is complete, it reschedules itself depending on the
425 * appropriate interval that was calculated.
427 void ath_ani_calibrate(unsigned long data)
429 struct ath_softc *sc = (struct ath_softc *)data;
430 struct ath_hw *ah = sc->sc_ah;
431 struct ath_common *common = ath9k_hw_common(ah);
432 bool longcal = false;
433 bool shortcal = false;
434 bool aniflag = false;
435 unsigned int timestamp = jiffies_to_msecs(jiffies);
436 u32 cal_interval, short_cal_interval, long_cal_interval;
439 if (ah->caldata && ah->caldata->nfcal_interference)
440 long_cal_interval = ATH_LONG_CALINTERVAL_INT;
442 long_cal_interval = ATH_LONG_CALINTERVAL;
444 short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
445 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
447 /* Only calibrate if awake */
448 if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
453 /* Long calibration runs independently of short calibration. */
454 if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
456 ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
457 common->ani.longcal_timer = timestamp;
460 /* Short calibration applies only while caldone is false */
461 if (!common->ani.caldone) {
462 if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
464 ath_print(common, ATH_DBG_ANI,
465 "shortcal @%lu\n", jiffies);
466 common->ani.shortcal_timer = timestamp;
467 common->ani.resetcal_timer = timestamp;
470 if ((timestamp - common->ani.resetcal_timer) >=
471 ATH_RESTART_CALINTERVAL) {
472 common->ani.caldone = ath9k_hw_reset_calvalid(ah);
473 if (common->ani.caldone)
474 common->ani.resetcal_timer = timestamp;
478 /* Verify whether we must check ANI */
479 if ((timestamp - common->ani.checkani_timer) >=
480 ah->config.ani_poll_interval) {
482 common->ani.checkani_timer = timestamp;
485 /* Skip all processing if there's nothing to do. */
486 if (longcal || shortcal || aniflag) {
487 /* Call ANI routine if necessary */
489 spin_lock_irqsave(&common->cc_lock, flags);
490 ath9k_hw_ani_monitor(ah, ah->curchan);
491 ath_update_survey_stats(sc);
492 spin_unlock_irqrestore(&common->cc_lock, flags);
495 /* Perform calibration if necessary */
496 if (longcal || shortcal) {
497 common->ani.caldone =
498 ath9k_hw_calibrate(ah,
500 common->rx_chainmask,
505 ath9k_ps_restore(sc);
509 * Set timer interval based on previous results.
510 * The interval must be the shortest necessary to satisfy ANI,
511 * short calibration and long calibration.
513 cal_interval = ATH_LONG_CALINTERVAL;
514 if (sc->sc_ah->config.enable_ani)
515 cal_interval = min(cal_interval,
516 (u32)ah->config.ani_poll_interval);
517 if (!common->ani.caldone)
518 cal_interval = min(cal_interval, (u32)short_cal_interval);
520 mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
521 if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->caldata) {
522 if (!ah->caldata->paprd_done)
523 ieee80211_queue_work(sc->hw, &sc->paprd_work);
525 ath_paprd_activate(sc);
530 * Update tx/rx chainmask. For legacy association,
531 * hard code chainmask to 1x1, for 11n association, use
532 * the chainmask configuration, for bt coexistence, use
533 * the chainmask configuration even in legacy mode.
535 void ath_update_chainmask(struct ath_softc *sc, int is_ht)
537 struct ath_hw *ah = sc->sc_ah;
538 struct ath_common *common = ath9k_hw_common(ah);
540 if ((sc->sc_flags & SC_OP_OFFCHANNEL) || is_ht ||
541 (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
542 common->tx_chainmask = ah->caps.tx_chainmask;
543 common->rx_chainmask = ah->caps.rx_chainmask;
545 common->tx_chainmask = 1;
546 common->rx_chainmask = 1;
549 ath_print(common, ATH_DBG_CONFIG,
550 "tx chmask: %d, rx chmask: %d\n",
551 common->tx_chainmask,
552 common->rx_chainmask);
555 static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
559 an = (struct ath_node *)sta->drv_priv;
561 if (sc->sc_flags & SC_OP_TXAGGR) {
562 ath_tx_node_init(sc, an);
563 an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
564 sta->ht_cap.ampdu_factor);
565 an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
566 an->last_rssi = ATH_RSSI_DUMMY_MARKER;
570 static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
572 struct ath_node *an = (struct ath_node *)sta->drv_priv;
574 if (sc->sc_flags & SC_OP_TXAGGR)
575 ath_tx_node_cleanup(sc, an);
578 void ath_hw_check(struct work_struct *work)
580 struct ath_softc *sc = container_of(work, struct ath_softc, hw_check_work);
585 for (i = 0; i < 3; i++) {
586 if (ath9k_hw_check_alive(sc->sc_ah))
594 ath9k_ps_restore(sc);
597 void ath9k_tasklet(unsigned long data)
599 struct ath_softc *sc = (struct ath_softc *)data;
600 struct ath_hw *ah = sc->sc_ah;
601 struct ath_common *common = ath9k_hw_common(ah);
603 u32 status = sc->intrstatus;
608 if (status & ATH9K_INT_FATAL) {
610 ath9k_ps_restore(sc);
614 if (!ath9k_hw_check_alive(ah))
615 ieee80211_queue_work(sc->hw, &sc->hw_check_work);
617 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
618 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
621 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
623 if (status & rxmask) {
624 spin_lock_bh(&sc->rx.pcu_lock);
626 /* Check for high priority Rx first */
627 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
628 (status & ATH9K_INT_RXHP))
629 ath_rx_tasklet(sc, 0, true);
631 ath_rx_tasklet(sc, 0, false);
632 spin_unlock_bh(&sc->rx.pcu_lock);
635 if (status & ATH9K_INT_TX) {
636 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
637 ath_tx_edma_tasklet(sc);
642 if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
644 * TSF sync does not look correct; remain awake to sync with
647 ath_print(common, ATH_DBG_PS,
648 "TSFOOR - Sync with next Beacon\n");
649 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
652 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
653 if (status & ATH9K_INT_GENTIMER)
654 ath_gen_timer_isr(sc->sc_ah);
656 /* re-enable hardware interrupt */
657 ath9k_hw_set_interrupts(ah, ah->imask);
658 ath9k_ps_restore(sc);
661 irqreturn_t ath_isr(int irq, void *dev)
663 #define SCHED_INTR ( \
676 struct ath_softc *sc = dev;
677 struct ath_hw *ah = sc->sc_ah;
678 struct ath_common *common = ath9k_hw_common(ah);
679 enum ath9k_int status;
683 * The hardware is not ready/present, don't
684 * touch anything. Note this can happen early
685 * on if the IRQ is shared.
687 if (sc->sc_flags & SC_OP_INVALID)
691 /* shared irq, not for us */
693 if (!ath9k_hw_intrpend(ah))
697 * Figure out the reason(s) for the interrupt. Note
698 * that the hal returns a pseudo-ISR that may include
699 * bits we haven't explicitly enabled so we mask the
700 * value to insure we only process bits we requested.
702 ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
703 status &= ah->imask; /* discard unasked-for bits */
706 * If there are no status bits set, then this interrupt was not
707 * for me (should have been caught above).
712 /* Cache the status */
713 sc->intrstatus = status;
715 if (status & SCHED_INTR)
719 * If a FATAL or RXORN interrupt is received, we have to reset the
722 if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
723 !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
726 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
727 (status & ATH9K_INT_BB_WATCHDOG)) {
729 spin_lock(&common->cc_lock);
730 ath_hw_cycle_counters_update(common);
731 ar9003_hw_bb_watchdog_dbg_info(ah);
732 spin_unlock(&common->cc_lock);
737 if (status & ATH9K_INT_SWBA)
738 tasklet_schedule(&sc->bcon_tasklet);
740 if (status & ATH9K_INT_TXURN)
741 ath9k_hw_updatetxtriglevel(ah, true);
743 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
744 if (status & ATH9K_INT_RXEOL) {
745 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
746 ath9k_hw_set_interrupts(ah, ah->imask);
750 if (status & ATH9K_INT_MIB) {
752 * Disable interrupts until we service the MIB
753 * interrupt; otherwise it will continue to
756 ath9k_hw_set_interrupts(ah, 0);
758 * Let the hal handle the event. We assume
759 * it will clear whatever condition caused
762 spin_lock(&common->cc_lock);
763 ath9k_hw_proc_mib_event(ah);
764 spin_unlock(&common->cc_lock);
765 ath9k_hw_set_interrupts(ah, ah->imask);
768 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
769 if (status & ATH9K_INT_TIM_TIMER) {
770 /* Clear RxAbort bit so that we can
772 ath9k_setpower(sc, ATH9K_PM_AWAKE);
773 ath9k_hw_setrxabort(sc->sc_ah, 0);
774 sc->ps_flags |= PS_WAIT_FOR_BEACON;
779 ath_debug_stat_interrupt(sc, status);
782 /* turn off every interrupt except SWBA */
783 ath9k_hw_set_interrupts(ah, (ah->imask & ATH9K_INT_SWBA));
784 tasklet_schedule(&sc->intr_tq);
792 static u32 ath_get_extchanmode(struct ath_softc *sc,
793 struct ieee80211_channel *chan,
794 enum nl80211_channel_type channel_type)
798 switch (chan->band) {
799 case IEEE80211_BAND_2GHZ:
800 switch(channel_type) {
801 case NL80211_CHAN_NO_HT:
802 case NL80211_CHAN_HT20:
803 chanmode = CHANNEL_G_HT20;
805 case NL80211_CHAN_HT40PLUS:
806 chanmode = CHANNEL_G_HT40PLUS;
808 case NL80211_CHAN_HT40MINUS:
809 chanmode = CHANNEL_G_HT40MINUS;
813 case IEEE80211_BAND_5GHZ:
814 switch(channel_type) {
815 case NL80211_CHAN_NO_HT:
816 case NL80211_CHAN_HT20:
817 chanmode = CHANNEL_A_HT20;
819 case NL80211_CHAN_HT40PLUS:
820 chanmode = CHANNEL_A_HT40PLUS;
822 case NL80211_CHAN_HT40MINUS:
823 chanmode = CHANNEL_A_HT40MINUS;
834 static void ath9k_bss_assoc_info(struct ath_softc *sc,
835 struct ieee80211_vif *vif,
836 struct ieee80211_bss_conf *bss_conf)
838 struct ath_hw *ah = sc->sc_ah;
839 struct ath_common *common = ath9k_hw_common(ah);
841 if (bss_conf->assoc) {
842 ath_print(common, ATH_DBG_CONFIG,
843 "Bss Info ASSOC %d, bssid: %pM\n",
844 bss_conf->aid, common->curbssid);
846 /* New association, store aid */
847 common->curaid = bss_conf->aid;
848 ath9k_hw_write_associd(ah);
851 * Request a re-configuration of Beacon related timers
852 * on the receipt of the first Beacon frame (i.e.,
853 * after time sync with the AP).
855 sc->ps_flags |= PS_BEACON_SYNC;
857 /* Configure the beacon */
858 ath_beacon_config(sc, vif);
860 /* Reset rssi stats */
861 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
863 sc->sc_flags |= SC_OP_ANI_RUN;
864 ath_start_ani(common);
866 ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
869 sc->sc_flags &= ~SC_OP_ANI_RUN;
870 del_timer_sync(&common->ani.timer);
874 void ath_radio_enable(struct ath_softc *sc, struct ieee80211_hw *hw)
876 struct ath_hw *ah = sc->sc_ah;
877 struct ath_common *common = ath9k_hw_common(ah);
878 struct ieee80211_channel *channel = hw->conf.channel;
882 ath9k_hw_configpcipowersave(ah, 0, 0);
885 ah->curchan = ath_get_curchannel(sc, sc->hw);
887 spin_lock_bh(&sc->rx.pcu_lock);
888 spin_lock_bh(&sc->sc_resetlock);
889 r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
891 ath_print(common, ATH_DBG_FATAL,
892 "Unable to reset channel (%u MHz), "
894 channel->center_freq, r);
896 spin_unlock_bh(&sc->sc_resetlock);
898 ath_update_txpow(sc);
899 if (ath_startrecv(sc) != 0) {
900 ath_print(common, ATH_DBG_FATAL,
901 "Unable to restart recv logic\n");
902 spin_unlock_bh(&sc->rx.pcu_lock);
905 spin_unlock_bh(&sc->rx.pcu_lock);
907 if (sc->sc_flags & SC_OP_BEACONS)
908 ath_beacon_config(sc, NULL); /* restart beacons */
910 /* Re-Enable interrupts */
911 ath9k_hw_set_interrupts(ah, ah->imask);
914 ath9k_hw_cfg_output(ah, ah->led_pin,
915 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
916 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
918 ieee80211_wake_queues(hw);
919 ath9k_ps_restore(sc);
922 void ath_radio_disable(struct ath_softc *sc, struct ieee80211_hw *hw)
924 struct ath_hw *ah = sc->sc_ah;
925 struct ieee80211_channel *channel = hw->conf.channel;
929 ieee80211_stop_queues(hw);
932 * Keep the LED on when the radio is disabled
933 * during idle unassociated state.
936 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
937 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
940 /* Disable interrupts */
941 ath9k_hw_set_interrupts(ah, 0);
943 ath_drain_all_txq(sc, false); /* clear pending tx frames */
945 spin_lock_bh(&sc->rx.pcu_lock);
947 ath_stoprecv(sc); /* turn off frame recv */
948 ath_flushrecv(sc); /* flush recv queue */
951 ah->curchan = ath_get_curchannel(sc, hw);
953 spin_lock_bh(&sc->sc_resetlock);
954 r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
956 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
957 "Unable to reset channel (%u MHz), "
959 channel->center_freq, r);
961 spin_unlock_bh(&sc->sc_resetlock);
963 ath9k_hw_phy_disable(ah);
965 spin_unlock_bh(&sc->rx.pcu_lock);
967 ath9k_hw_configpcipowersave(ah, 1, 1);
968 ath9k_ps_restore(sc);
969 ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
972 int ath_reset(struct ath_softc *sc, bool retry_tx)
974 struct ath_hw *ah = sc->sc_ah;
975 struct ath_common *common = ath9k_hw_common(ah);
976 struct ieee80211_hw *hw = sc->hw;
980 del_timer_sync(&common->ani.timer);
982 ieee80211_stop_queues(hw);
984 ath9k_hw_set_interrupts(ah, 0);
985 ath_drain_all_txq(sc, retry_tx);
987 spin_lock_bh(&sc->rx.pcu_lock);
992 spin_lock_bh(&sc->sc_resetlock);
993 r = ath9k_hw_reset(ah, sc->sc_ah->curchan, ah->caldata, false);
995 ath_print(common, ATH_DBG_FATAL,
996 "Unable to reset hardware; reset status %d\n", r);
997 spin_unlock_bh(&sc->sc_resetlock);
999 if (ath_startrecv(sc) != 0)
1000 ath_print(common, ATH_DBG_FATAL,
1001 "Unable to start recv logic\n");
1003 spin_unlock_bh(&sc->rx.pcu_lock);
1006 * We may be doing a reset in response to a request
1007 * that changes the channel so update any state that
1008 * might change as a result.
1010 ath_update_txpow(sc);
1012 if ((sc->sc_flags & SC_OP_BEACONS) || !(sc->sc_flags & (SC_OP_OFFCHANNEL)))
1013 ath_beacon_config(sc, NULL); /* restart beacons */
1015 ath9k_hw_set_interrupts(ah, ah->imask);
1019 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1020 if (ATH_TXQ_SETUP(sc, i)) {
1021 spin_lock_bh(&sc->tx.txq[i].axq_lock);
1022 ath_txq_schedule(sc, &sc->tx.txq[i]);
1023 spin_unlock_bh(&sc->tx.txq[i].axq_lock);
1028 ieee80211_wake_queues(hw);
1031 ath_start_ani(common);
1036 static int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
1042 qnum = sc->tx.hwq_map[WME_AC_VO];
1045 qnum = sc->tx.hwq_map[WME_AC_VI];
1048 qnum = sc->tx.hwq_map[WME_AC_BE];
1051 qnum = sc->tx.hwq_map[WME_AC_BK];
1054 qnum = sc->tx.hwq_map[WME_AC_BE];
1061 int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
1086 /* XXX: Remove me once we don't depend on ath9k_channel for all
1087 * this redundant data */
1088 void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
1089 struct ath9k_channel *ichan)
1091 struct ieee80211_channel *chan = hw->conf.channel;
1092 struct ieee80211_conf *conf = &hw->conf;
1094 ichan->channel = chan->center_freq;
1097 if (chan->band == IEEE80211_BAND_2GHZ) {
1098 ichan->chanmode = CHANNEL_G;
1099 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
1101 ichan->chanmode = CHANNEL_A;
1102 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
1105 if (conf_is_ht(conf))
1106 ichan->chanmode = ath_get_extchanmode(sc, chan,
1107 conf->channel_type);
1110 /**********************/
1111 /* mac80211 callbacks */
1112 /**********************/
1114 static int ath9k_start(struct ieee80211_hw *hw)
1116 struct ath_wiphy *aphy = hw->priv;
1117 struct ath_softc *sc = aphy->sc;
1118 struct ath_hw *ah = sc->sc_ah;
1119 struct ath_common *common = ath9k_hw_common(ah);
1120 struct ieee80211_channel *curchan = hw->conf.channel;
1121 struct ath9k_channel *init_channel;
1124 ath_print(common, ATH_DBG_CONFIG,
1125 "Starting driver with initial channel: %d MHz\n",
1126 curchan->center_freq);
1128 mutex_lock(&sc->mutex);
1130 if (ath9k_wiphy_started(sc)) {
1131 if (sc->chan_idx == curchan->hw_value) {
1133 * Already on the operational channel, the new wiphy
1134 * can be marked active.
1136 aphy->state = ATH_WIPHY_ACTIVE;
1137 ieee80211_wake_queues(hw);
1140 * Another wiphy is on another channel, start the new
1141 * wiphy in paused state.
1143 aphy->state = ATH_WIPHY_PAUSED;
1144 ieee80211_stop_queues(hw);
1146 mutex_unlock(&sc->mutex);
1149 aphy->state = ATH_WIPHY_ACTIVE;
1151 /* setup initial channel */
1153 sc->chan_idx = curchan->hw_value;
1155 init_channel = ath_get_curchannel(sc, hw);
1157 /* Reset SERDES registers */
1158 ath9k_hw_configpcipowersave(ah, 0, 0);
1161 * The basic interface to setting the hardware in a good
1162 * state is ``reset''. On return the hardware is known to
1163 * be powered up and with interrupts disabled. This must
1164 * be followed by initialization of the appropriate bits
1165 * and then setup of the interrupt mask.
1167 spin_lock_bh(&sc->rx.pcu_lock);
1168 spin_lock_bh(&sc->sc_resetlock);
1169 r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
1171 ath_print(common, ATH_DBG_FATAL,
1172 "Unable to reset hardware; reset status %d "
1173 "(freq %u MHz)\n", r,
1174 curchan->center_freq);
1175 spin_unlock_bh(&sc->sc_resetlock);
1176 spin_unlock_bh(&sc->rx.pcu_lock);
1179 spin_unlock_bh(&sc->sc_resetlock);
1182 * This is needed only to setup initial state
1183 * but it's best done after a reset.
1185 ath_update_txpow(sc);
1188 * Setup the hardware after reset:
1189 * The receive engine is set going.
1190 * Frame transmit is handled entirely
1191 * in the frame output path; there's nothing to do
1192 * here except setup the interrupt mask.
1194 if (ath_startrecv(sc) != 0) {
1195 ath_print(common, ATH_DBG_FATAL,
1196 "Unable to start recv logic\n");
1198 spin_unlock_bh(&sc->rx.pcu_lock);
1201 spin_unlock_bh(&sc->rx.pcu_lock);
1203 /* Setup our intr mask. */
1204 ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
1205 ATH9K_INT_RXORN | ATH9K_INT_FATAL |
1208 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
1209 ah->imask |= ATH9K_INT_RXHP |
1211 ATH9K_INT_BB_WATCHDOG;
1213 ah->imask |= ATH9K_INT_RX;
1215 ah->imask |= ATH9K_INT_GTT;
1217 if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
1218 ah->imask |= ATH9K_INT_CST;
1220 sc->sc_flags &= ~SC_OP_INVALID;
1221 sc->sc_ah->is_monitoring = false;
1223 /* Disable BMISS interrupt when we're not associated */
1224 ah->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1225 ath9k_hw_set_interrupts(ah, ah->imask);
1227 ieee80211_wake_queues(hw);
1229 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
1231 if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
1232 !ah->btcoex_hw.enabled) {
1233 ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
1234 AR_STOMP_LOW_WLAN_WGHT);
1235 ath9k_hw_btcoex_enable(ah);
1237 if (common->bus_ops->bt_coex_prep)
1238 common->bus_ops->bt_coex_prep(common);
1239 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
1240 ath9k_btcoex_timer_resume(sc);
1243 pm_qos_update_request(&ath9k_pm_qos_req, 55);
1246 mutex_unlock(&sc->mutex);
1251 static int ath9k_tx(struct ieee80211_hw *hw,
1252 struct sk_buff *skb)
1254 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1255 struct ath_wiphy *aphy = hw->priv;
1256 struct ath_softc *sc = aphy->sc;
1257 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1258 struct ath_tx_control txctl;
1259 int padpos, padsize;
1260 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1263 if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
1264 ath_print(common, ATH_DBG_XMIT,
1265 "ath9k: %s: TX in unexpected wiphy state "
1266 "%d\n", wiphy_name(hw->wiphy), aphy->state);
1270 if (sc->ps_enabled) {
1272 * mac80211 does not set PM field for normal data frames, so we
1273 * need to update that based on the current PS mode.
1275 if (ieee80211_is_data(hdr->frame_control) &&
1276 !ieee80211_is_nullfunc(hdr->frame_control) &&
1277 !ieee80211_has_pm(hdr->frame_control)) {
1278 ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
1279 "while in PS mode\n");
1280 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1284 if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
1286 * We are using PS-Poll and mac80211 can request TX while in
1287 * power save mode. Need to wake up hardware for the TX to be
1288 * completed and if needed, also for RX of buffered frames.
1290 ath9k_ps_wakeup(sc);
1291 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
1292 ath9k_hw_setrxabort(sc->sc_ah, 0);
1293 if (ieee80211_is_pspoll(hdr->frame_control)) {
1294 ath_print(common, ATH_DBG_PS,
1295 "Sending PS-Poll to pick a buffered frame\n");
1296 sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
1298 ath_print(common, ATH_DBG_PS,
1299 "Wake up to complete TX\n");
1300 sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
1303 * The actual restore operation will happen only after
1304 * the sc_flags bit is cleared. We are just dropping
1305 * the ps_usecount here.
1307 ath9k_ps_restore(sc);
1310 memset(&txctl, 0, sizeof(struct ath_tx_control));
1313 * As a temporary workaround, assign seq# here; this will likely need
1314 * to be cleaned up to work better with Beacon transmission and virtual
1317 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1318 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1319 sc->tx.seq_no += 0x10;
1320 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1321 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
1324 /* Add the padding after the header if this is not already done */
1325 padpos = ath9k_cmn_padpos(hdr->frame_control);
1326 padsize = padpos & 3;
1327 if (padsize && skb->len>padpos) {
1328 if (skb_headroom(skb) < padsize)
1330 skb_push(skb, padsize);
1331 memmove(skb->data, skb->data + padsize, padpos);
1334 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
1335 txctl.txq = &sc->tx.txq[qnum];
1337 ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
1339 if (ath_tx_start(hw, skb, &txctl) != 0) {
1340 ath_print(common, ATH_DBG_XMIT, "TX failed\n");
1346 dev_kfree_skb_any(skb);
1350 static void ath9k_stop(struct ieee80211_hw *hw)
1352 struct ath_wiphy *aphy = hw->priv;
1353 struct ath_softc *sc = aphy->sc;
1354 struct ath_hw *ah = sc->sc_ah;
1355 struct ath_common *common = ath9k_hw_common(ah);
1358 mutex_lock(&sc->mutex);
1360 aphy->state = ATH_WIPHY_INACTIVE;
1363 cancel_delayed_work_sync(&sc->ath_led_blink_work);
1365 cancel_delayed_work_sync(&sc->tx_complete_work);
1366 cancel_work_sync(&sc->paprd_work);
1367 cancel_work_sync(&sc->hw_check_work);
1369 for (i = 0; i < sc->num_sec_wiphy; i++) {
1370 if (sc->sec_wiphy[i])
1374 if (i == sc->num_sec_wiphy) {
1375 cancel_delayed_work_sync(&sc->wiphy_work);
1376 cancel_work_sync(&sc->chan_work);
1379 if (sc->sc_flags & SC_OP_INVALID) {
1380 ath_print(common, ATH_DBG_ANY, "Device not present\n");
1381 mutex_unlock(&sc->mutex);
1385 if (ath9k_wiphy_started(sc)) {
1386 mutex_unlock(&sc->mutex);
1387 return; /* another wiphy still in use */
1390 /* Ensure HW is awake when we try to shut it down. */
1391 ath9k_ps_wakeup(sc);
1393 if (ah->btcoex_hw.enabled) {
1394 ath9k_hw_btcoex_disable(ah);
1395 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
1396 ath9k_btcoex_timer_pause(sc);
1399 /* make sure h/w will not generate any interrupt
1400 * before setting the invalid flag. */
1401 ath9k_hw_set_interrupts(ah, 0);
1403 spin_lock_bh(&sc->rx.pcu_lock);
1404 if (!(sc->sc_flags & SC_OP_INVALID)) {
1405 ath_drain_all_txq(sc, false);
1407 ath9k_hw_phy_disable(ah);
1409 sc->rx.rxlink = NULL;
1410 spin_unlock_bh(&sc->rx.pcu_lock);
1412 /* disable HAL and put h/w to sleep */
1413 ath9k_hw_disable(ah);
1414 ath9k_hw_configpcipowersave(ah, 1, 1);
1415 ath9k_ps_restore(sc);
1417 /* Finally, put the chip in FULL SLEEP mode */
1418 ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
1420 sc->sc_flags |= SC_OP_INVALID;
1422 pm_qos_update_request(&ath9k_pm_qos_req, PM_QOS_DEFAULT_VALUE);
1424 mutex_unlock(&sc->mutex);
1426 ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
1429 static int ath9k_add_interface(struct ieee80211_hw *hw,
1430 struct ieee80211_vif *vif)
1432 struct ath_wiphy *aphy = hw->priv;
1433 struct ath_softc *sc = aphy->sc;
1434 struct ath_hw *ah = sc->sc_ah;
1435 struct ath_common *common = ath9k_hw_common(ah);
1436 struct ath_vif *avp = (void *)vif->drv_priv;
1437 enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
1440 mutex_lock(&sc->mutex);
1442 switch (vif->type) {
1443 case NL80211_IFTYPE_STATION:
1444 ic_opmode = NL80211_IFTYPE_STATION;
1446 case NL80211_IFTYPE_WDS:
1447 ic_opmode = NL80211_IFTYPE_WDS;
1449 case NL80211_IFTYPE_ADHOC:
1450 case NL80211_IFTYPE_AP:
1451 case NL80211_IFTYPE_MESH_POINT:
1452 if (sc->nbcnvifs >= ATH_BCBUF) {
1456 ic_opmode = vif->type;
1459 ath_print(common, ATH_DBG_FATAL,
1460 "Interface type %d not yet supported\n", vif->type);
1465 ath_print(common, ATH_DBG_CONFIG,
1466 "Attach a VIF of type: %d\n", ic_opmode);
1468 /* Set the VIF opmode */
1469 avp->av_opmode = ic_opmode;
1474 ath9k_set_bssid_mask(hw, vif);
1477 goto out; /* skip global settings for secondary vif */
1479 if (ic_opmode == NL80211_IFTYPE_AP) {
1480 ath9k_hw_set_tsfadjust(ah, 1);
1481 sc->sc_flags |= SC_OP_TSF_RESET;
1484 /* Set the device opmode */
1485 ah->opmode = ic_opmode;
1488 * Enable MIB interrupts when there are hardware phy counters.
1489 * Note we only do this (at the moment) for station mode.
1491 if ((vif->type == NL80211_IFTYPE_STATION) ||
1492 (vif->type == NL80211_IFTYPE_ADHOC) ||
1493 (vif->type == NL80211_IFTYPE_MESH_POINT)) {
1494 if (ah->config.enable_ani)
1495 ah->imask |= ATH9K_INT_MIB;
1496 ah->imask |= ATH9K_INT_TSFOOR;
1499 ath9k_hw_set_interrupts(ah, ah->imask);
1501 if (vif->type == NL80211_IFTYPE_AP ||
1502 vif->type == NL80211_IFTYPE_ADHOC) {
1503 sc->sc_flags |= SC_OP_ANI_RUN;
1504 ath_start_ani(common);
1508 mutex_unlock(&sc->mutex);
1512 static void ath9k_remove_interface(struct ieee80211_hw *hw,
1513 struct ieee80211_vif *vif)
1515 struct ath_wiphy *aphy = hw->priv;
1516 struct ath_softc *sc = aphy->sc;
1517 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1518 struct ath_vif *avp = (void *)vif->drv_priv;
1521 ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
1523 mutex_lock(&sc->mutex);
1526 sc->sc_flags &= ~SC_OP_ANI_RUN;
1527 del_timer_sync(&common->ani.timer);
1529 /* Reclaim beacon resources */
1530 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
1531 (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
1532 (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
1533 ath9k_ps_wakeup(sc);
1534 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1535 ath9k_ps_restore(sc);
1538 ath_beacon_return(sc, avp);
1539 sc->sc_flags &= ~SC_OP_BEACONS;
1541 for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
1542 if (sc->beacon.bslot[i] == vif) {
1543 printk(KERN_DEBUG "%s: vif had allocated beacon "
1544 "slot\n", __func__);
1545 sc->beacon.bslot[i] = NULL;
1546 sc->beacon.bslot_aphy[i] = NULL;
1552 mutex_unlock(&sc->mutex);
1555 static void ath9k_enable_ps(struct ath_softc *sc)
1557 struct ath_hw *ah = sc->sc_ah;
1559 sc->ps_enabled = true;
1560 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1561 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1562 ah->imask |= ATH9K_INT_TIM_TIMER;
1563 ath9k_hw_set_interrupts(ah, ah->imask);
1565 ath9k_hw_setrxabort(ah, 1);
1569 static void ath9k_disable_ps(struct ath_softc *sc)
1571 struct ath_hw *ah = sc->sc_ah;
1573 sc->ps_enabled = false;
1574 ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
1575 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1576 ath9k_hw_setrxabort(ah, 0);
1577 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1579 PS_WAIT_FOR_PSPOLL_DATA |
1580 PS_WAIT_FOR_TX_ACK);
1581 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1582 ah->imask &= ~ATH9K_INT_TIM_TIMER;
1583 ath9k_hw_set_interrupts(ah, ah->imask);
1589 static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
1591 struct ath_wiphy *aphy = hw->priv;
1592 struct ath_softc *sc = aphy->sc;
1593 struct ath_hw *ah = sc->sc_ah;
1594 struct ath_common *common = ath9k_hw_common(ah);
1595 struct ieee80211_conf *conf = &hw->conf;
1598 mutex_lock(&sc->mutex);
1601 * Leave this as the first check because we need to turn on the
1602 * radio if it was disabled before prior to processing the rest
1603 * of the changes. Likewise we must only disable the radio towards
1606 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1608 bool all_wiphys_idle;
1609 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1611 spin_lock_bh(&sc->wiphy_lock);
1612 all_wiphys_idle = ath9k_all_wiphys_idle(sc);
1613 ath9k_set_wiphy_idle(aphy, idle);
1615 enable_radio = (!idle && all_wiphys_idle);
1618 * After we unlock here its possible another wiphy
1619 * can be re-renabled so to account for that we will
1620 * only disable the radio toward the end of this routine
1621 * if by then all wiphys are still idle.
1623 spin_unlock_bh(&sc->wiphy_lock);
1626 sc->ps_idle = false;
1627 ath_radio_enable(sc, hw);
1628 ath_print(common, ATH_DBG_CONFIG,
1629 "not-idle: enabling radio\n");
1634 * We just prepare to enable PS. We have to wait until our AP has
1635 * ACK'd our null data frame to disable RX otherwise we'll ignore
1636 * those ACKs and end up retransmitting the same null data frames.
1637 * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1639 if (changed & IEEE80211_CONF_CHANGE_PS) {
1640 unsigned long flags;
1641 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1642 if (conf->flags & IEEE80211_CONF_PS)
1643 ath9k_enable_ps(sc);
1645 ath9k_disable_ps(sc);
1646 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1649 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1650 if (conf->flags & IEEE80211_CONF_MONITOR) {
1651 ath_print(common, ATH_DBG_CONFIG,
1652 "Monitor mode is enabled\n");
1653 sc->sc_ah->is_monitoring = true;
1655 ath_print(common, ATH_DBG_CONFIG,
1656 "Monitor mode is disabled\n");
1657 sc->sc_ah->is_monitoring = false;
1661 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1662 struct ieee80211_channel *curchan = hw->conf.channel;
1663 int pos = curchan->hw_value;
1665 unsigned long flags;
1668 old_pos = ah->curchan - &ah->channels[0];
1670 aphy->chan_idx = pos;
1671 aphy->chan_is_ht = conf_is_ht(conf);
1672 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
1673 sc->sc_flags |= SC_OP_OFFCHANNEL;
1675 sc->sc_flags &= ~SC_OP_OFFCHANNEL;
1677 if (aphy->state == ATH_WIPHY_SCAN ||
1678 aphy->state == ATH_WIPHY_ACTIVE)
1679 ath9k_wiphy_pause_all_forced(sc, aphy);
1682 * Do not change operational channel based on a paused
1685 goto skip_chan_change;
1688 ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1689 curchan->center_freq);
1691 /* XXX: remove me eventualy */
1692 ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
1694 ath_update_chainmask(sc, conf_is_ht(conf));
1696 /* update survey stats for the old channel before switching */
1697 spin_lock_irqsave(&common->cc_lock, flags);
1698 ath_update_survey_stats(sc);
1699 spin_unlock_irqrestore(&common->cc_lock, flags);
1702 * If the operating channel changes, change the survey in-use flags
1704 * Reset the survey data for the new channel, unless we're switching
1705 * back to the operating channel from an off-channel operation.
1707 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) &&
1708 sc->cur_survey != &sc->survey[pos]) {
1711 sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
1713 sc->cur_survey = &sc->survey[pos];
1715 memset(sc->cur_survey, 0, sizeof(struct survey_info));
1716 sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
1717 } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
1718 memset(&sc->survey[pos], 0, sizeof(struct survey_info));
1721 if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
1722 ath_print(common, ATH_DBG_FATAL,
1723 "Unable to set channel\n");
1724 mutex_unlock(&sc->mutex);
1729 * The most recent snapshot of channel->noisefloor for the old
1730 * channel is only available after the hardware reset. Copy it to
1731 * the survey stats now.
1734 ath_update_survey_nf(sc, old_pos);
1738 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1739 sc->config.txpowlimit = 2 * conf->power_level;
1740 ath_update_txpow(sc);
1743 spin_lock_bh(&sc->wiphy_lock);
1744 disable_radio = ath9k_all_wiphys_idle(sc);
1745 spin_unlock_bh(&sc->wiphy_lock);
1747 if (disable_radio) {
1748 ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
1750 ath_radio_disable(sc, hw);
1753 mutex_unlock(&sc->mutex);
1758 #define SUPPORTED_FILTERS \
1759 (FIF_PROMISC_IN_BSS | \
1764 FIF_BCN_PRBRESP_PROMISC | \
1768 /* FIXME: sc->sc_full_reset ? */
1769 static void ath9k_configure_filter(struct ieee80211_hw *hw,
1770 unsigned int changed_flags,
1771 unsigned int *total_flags,
1774 struct ath_wiphy *aphy = hw->priv;
1775 struct ath_softc *sc = aphy->sc;
1778 changed_flags &= SUPPORTED_FILTERS;
1779 *total_flags &= SUPPORTED_FILTERS;
1781 sc->rx.rxfilter = *total_flags;
1782 ath9k_ps_wakeup(sc);
1783 rfilt = ath_calcrxfilter(sc);
1784 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1785 ath9k_ps_restore(sc);
1787 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
1788 "Set HW RX filter: 0x%x\n", rfilt);
1791 static int ath9k_sta_add(struct ieee80211_hw *hw,
1792 struct ieee80211_vif *vif,
1793 struct ieee80211_sta *sta)
1795 struct ath_wiphy *aphy = hw->priv;
1796 struct ath_softc *sc = aphy->sc;
1798 ath_node_attach(sc, sta);
1803 static int ath9k_sta_remove(struct ieee80211_hw *hw,
1804 struct ieee80211_vif *vif,
1805 struct ieee80211_sta *sta)
1807 struct ath_wiphy *aphy = hw->priv;
1808 struct ath_softc *sc = aphy->sc;
1810 ath_node_detach(sc, sta);
1815 static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
1816 const struct ieee80211_tx_queue_params *params)
1818 struct ath_wiphy *aphy = hw->priv;
1819 struct ath_softc *sc = aphy->sc;
1820 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1821 struct ath9k_tx_queue_info qi;
1824 if (queue >= WME_NUM_AC)
1827 mutex_lock(&sc->mutex);
1829 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1831 qi.tqi_aifs = params->aifs;
1832 qi.tqi_cwmin = params->cw_min;
1833 qi.tqi_cwmax = params->cw_max;
1834 qi.tqi_burstTime = params->txop;
1835 qnum = ath_get_hal_qnum(queue, sc);
1837 ath_print(common, ATH_DBG_CONFIG,
1838 "Configure tx [queue/halq] [%d/%d], "
1839 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1840 queue, qnum, params->aifs, params->cw_min,
1841 params->cw_max, params->txop);
1843 ret = ath_txq_update(sc, qnum, &qi);
1845 ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
1847 if (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
1848 if ((qnum == sc->tx.hwq_map[WME_AC_BE]) && !ret)
1849 ath_beaconq_config(sc);
1851 mutex_unlock(&sc->mutex);
1856 static int ath9k_set_key(struct ieee80211_hw *hw,
1857 enum set_key_cmd cmd,
1858 struct ieee80211_vif *vif,
1859 struct ieee80211_sta *sta,
1860 struct ieee80211_key_conf *key)
1862 struct ath_wiphy *aphy = hw->priv;
1863 struct ath_softc *sc = aphy->sc;
1864 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1867 if (modparam_nohwcrypt)
1870 mutex_lock(&sc->mutex);
1871 ath9k_ps_wakeup(sc);
1872 ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
1876 ret = ath_key_config(common, vif, sta, key);
1878 key->hw_key_idx = ret;
1879 /* push IV and Michael MIC generation to stack */
1880 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1881 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1882 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1883 if (sc->sc_ah->sw_mgmt_crypto &&
1884 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1885 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1890 ath_key_delete(common, key);
1896 ath9k_ps_restore(sc);
1897 mutex_unlock(&sc->mutex);
1902 static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1903 struct ieee80211_vif *vif,
1904 struct ieee80211_bss_conf *bss_conf,
1907 struct ath_wiphy *aphy = hw->priv;
1908 struct ath_softc *sc = aphy->sc;
1909 struct ath_hw *ah = sc->sc_ah;
1910 struct ath_common *common = ath9k_hw_common(ah);
1911 struct ath_vif *avp = (void *)vif->drv_priv;
1915 mutex_lock(&sc->mutex);
1917 if (changed & BSS_CHANGED_BSSID) {
1919 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1920 memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
1922 ath9k_hw_write_associd(ah);
1924 /* Set aggregation protection mode parameters */
1925 sc->config.ath_aggr_prot = 0;
1927 /* Only legacy IBSS for now */
1928 if (vif->type == NL80211_IFTYPE_ADHOC)
1929 ath_update_chainmask(sc, 0);
1931 ath_print(common, ATH_DBG_CONFIG,
1932 "BSSID: %pM aid: 0x%x\n",
1933 common->curbssid, common->curaid);
1935 /* need to reconfigure the beacon */
1936 sc->sc_flags &= ~SC_OP_BEACONS ;
1939 /* Enable transmission of beacons (AP, IBSS, MESH) */
1940 if ((changed & BSS_CHANGED_BEACON) ||
1941 ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon)) {
1942 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1943 error = ath_beacon_alloc(aphy, vif);
1945 ath_beacon_config(sc, vif);
1948 if (changed & BSS_CHANGED_ERP_SLOT) {
1949 if (bss_conf->use_short_slot)
1953 if (vif->type == NL80211_IFTYPE_AP) {
1955 * Defer update, so that connected stations can adjust
1956 * their settings at the same time.
1957 * See beacon.c for more details
1959 sc->beacon.slottime = slottime;
1960 sc->beacon.updateslot = UPDATE;
1962 ah->slottime = slottime;
1963 ath9k_hw_init_global_settings(ah);
1967 /* Disable transmission of beacons */
1968 if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon)
1969 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1971 if (changed & BSS_CHANGED_BEACON_INT) {
1972 sc->beacon_interval = bss_conf->beacon_int;
1974 * In case of AP mode, the HW TSF has to be reset
1975 * when the beacon interval changes.
1977 if (vif->type == NL80211_IFTYPE_AP) {
1978 sc->sc_flags |= SC_OP_TSF_RESET;
1979 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1980 error = ath_beacon_alloc(aphy, vif);
1982 ath_beacon_config(sc, vif);
1984 ath_beacon_config(sc, vif);
1988 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1989 ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1990 bss_conf->use_short_preamble);
1991 if (bss_conf->use_short_preamble)
1992 sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
1994 sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
1997 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1998 ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1999 bss_conf->use_cts_prot);
2000 if (bss_conf->use_cts_prot &&
2001 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
2002 sc->sc_flags |= SC_OP_PROTECT_ENABLE;
2004 sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
2007 if (changed & BSS_CHANGED_ASSOC) {
2008 ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
2010 ath9k_bss_assoc_info(sc, vif, bss_conf);
2013 mutex_unlock(&sc->mutex);
2016 static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
2019 struct ath_wiphy *aphy = hw->priv;
2020 struct ath_softc *sc = aphy->sc;
2022 mutex_lock(&sc->mutex);
2023 tsf = ath9k_hw_gettsf64(sc->sc_ah);
2024 mutex_unlock(&sc->mutex);
2029 static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
2031 struct ath_wiphy *aphy = hw->priv;
2032 struct ath_softc *sc = aphy->sc;
2034 mutex_lock(&sc->mutex);
2035 ath9k_hw_settsf64(sc->sc_ah, tsf);
2036 mutex_unlock(&sc->mutex);
2039 static void ath9k_reset_tsf(struct ieee80211_hw *hw)
2041 struct ath_wiphy *aphy = hw->priv;
2042 struct ath_softc *sc = aphy->sc;
2044 mutex_lock(&sc->mutex);
2046 ath9k_ps_wakeup(sc);
2047 ath9k_hw_reset_tsf(sc->sc_ah);
2048 ath9k_ps_restore(sc);
2050 mutex_unlock(&sc->mutex);
2053 static int ath9k_ampdu_action(struct ieee80211_hw *hw,
2054 struct ieee80211_vif *vif,
2055 enum ieee80211_ampdu_mlme_action action,
2056 struct ieee80211_sta *sta,
2059 struct ath_wiphy *aphy = hw->priv;
2060 struct ath_softc *sc = aphy->sc;
2066 case IEEE80211_AMPDU_RX_START:
2067 if (!(sc->sc_flags & SC_OP_RXAGGR))
2070 case IEEE80211_AMPDU_RX_STOP:
2072 case IEEE80211_AMPDU_TX_START:
2073 ath9k_ps_wakeup(sc);
2074 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
2076 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2077 ath9k_ps_restore(sc);
2079 case IEEE80211_AMPDU_TX_STOP:
2080 ath9k_ps_wakeup(sc);
2081 ath_tx_aggr_stop(sc, sta, tid);
2082 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2083 ath9k_ps_restore(sc);
2085 case IEEE80211_AMPDU_TX_OPERATIONAL:
2086 ath9k_ps_wakeup(sc);
2087 ath_tx_aggr_resume(sc, sta, tid);
2088 ath9k_ps_restore(sc);
2091 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
2092 "Unknown AMPDU action\n");
2100 static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
2101 struct survey_info *survey)
2103 struct ath_wiphy *aphy = hw->priv;
2104 struct ath_softc *sc = aphy->sc;
2105 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2106 struct ieee80211_supported_band *sband;
2107 struct ieee80211_channel *chan;
2108 unsigned long flags;
2111 spin_lock_irqsave(&common->cc_lock, flags);
2113 ath_update_survey_stats(sc);
2115 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
2116 if (sband && idx >= sband->n_channels) {
2117 idx -= sband->n_channels;
2122 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
2124 if (!sband || idx >= sband->n_channels) {
2125 spin_unlock_irqrestore(&common->cc_lock, flags);
2129 chan = &sband->channels[idx];
2130 pos = chan->hw_value;
2131 memcpy(survey, &sc->survey[pos], sizeof(*survey));
2132 survey->channel = chan;
2133 spin_unlock_irqrestore(&common->cc_lock, flags);
2138 static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2140 struct ath_wiphy *aphy = hw->priv;
2141 struct ath_softc *sc = aphy->sc;
2143 mutex_lock(&sc->mutex);
2144 if (ath9k_wiphy_scanning(sc)) {
2146 * There is a race here in mac80211 but fixing it requires
2147 * we revisit how we handle the scan complete callback.
2148 * After mac80211 fixes we will not have configured hardware
2149 * to the home channel nor would we have configured the RX
2152 mutex_unlock(&sc->mutex);
2156 aphy->state = ATH_WIPHY_SCAN;
2157 ath9k_wiphy_pause_all_forced(sc, aphy);
2158 mutex_unlock(&sc->mutex);
2162 * XXX: this requires a revisit after the driver
2163 * scan_complete gets moved to another place/removed in mac80211.
2165 static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2167 struct ath_wiphy *aphy = hw->priv;
2168 struct ath_softc *sc = aphy->sc;
2170 mutex_lock(&sc->mutex);
2171 aphy->state = ATH_WIPHY_ACTIVE;
2172 mutex_unlock(&sc->mutex);
2175 static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
2177 struct ath_wiphy *aphy = hw->priv;
2178 struct ath_softc *sc = aphy->sc;
2179 struct ath_hw *ah = sc->sc_ah;
2181 mutex_lock(&sc->mutex);
2182 ah->coverage_class = coverage_class;
2183 ath9k_hw_init_global_settings(ah);
2184 mutex_unlock(&sc->mutex);
2187 struct ieee80211_ops ath9k_ops = {
2189 .start = ath9k_start,
2191 .add_interface = ath9k_add_interface,
2192 .remove_interface = ath9k_remove_interface,
2193 .config = ath9k_config,
2194 .configure_filter = ath9k_configure_filter,
2195 .sta_add = ath9k_sta_add,
2196 .sta_remove = ath9k_sta_remove,
2197 .conf_tx = ath9k_conf_tx,
2198 .bss_info_changed = ath9k_bss_info_changed,
2199 .set_key = ath9k_set_key,
2200 .get_tsf = ath9k_get_tsf,
2201 .set_tsf = ath9k_set_tsf,
2202 .reset_tsf = ath9k_reset_tsf,
2203 .ampdu_action = ath9k_ampdu_action,
2204 .get_survey = ath9k_get_survey,
2205 .sw_scan_start = ath9k_sw_scan_start,
2206 .sw_scan_complete = ath9k_sw_scan_complete,
2207 .rfkill_poll = ath9k_rfkill_poll_state,
2208 .set_coverage_class = ath9k_set_coverage_class,