ath9k_hw: remove code duplication in phy error counter handling
[linux-flexiantxendom0-natty.git] / drivers / net / wireless / ath / ath9k / main.c
1 /*
2  * Copyright (c) 2008-2009 Atheros Communications Inc.
3  *
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.
7  *
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.
15  */
16
17 #include <linux/nl80211.h>
18 #include "ath9k.h"
19 #include "btcoex.h"
20
21 static void ath_cache_conf_rate(struct ath_softc *sc,
22                                 struct ieee80211_conf *conf)
23 {
24         switch (conf->channel->band) {
25         case IEEE80211_BAND_2GHZ:
26                 if (conf_is_ht20(conf))
27                         sc->cur_rate_mode = ATH9K_MODE_11NG_HT20;
28                 else if (conf_is_ht40_minus(conf))
29                         sc->cur_rate_mode = ATH9K_MODE_11NG_HT40MINUS;
30                 else if (conf_is_ht40_plus(conf))
31                         sc->cur_rate_mode = ATH9K_MODE_11NG_HT40PLUS;
32                 else
33                         sc->cur_rate_mode = ATH9K_MODE_11G;
34                 break;
35         case IEEE80211_BAND_5GHZ:
36                 if (conf_is_ht20(conf))
37                         sc->cur_rate_mode = ATH9K_MODE_11NA_HT20;
38                 else if (conf_is_ht40_minus(conf))
39                         sc->cur_rate_mode = ATH9K_MODE_11NA_HT40MINUS;
40                 else if (conf_is_ht40_plus(conf))
41                         sc->cur_rate_mode = ATH9K_MODE_11NA_HT40PLUS;
42                 else
43                         sc->cur_rate_mode = ATH9K_MODE_11A;
44                 break;
45         default:
46                 BUG_ON(1);
47                 break;
48         }
49 }
50
51 static void ath_update_txpow(struct ath_softc *sc)
52 {
53         struct ath_hw *ah = sc->sc_ah;
54
55         if (sc->curtxpow != sc->config.txpowlimit) {
56                 ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
57                 /* read back in case value is clamped */
58                 sc->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
59         }
60 }
61
62 static u8 parse_mpdudensity(u8 mpdudensity)
63 {
64         /*
65          * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
66          *   0 for no restriction
67          *   1 for 1/4 us
68          *   2 for 1/2 us
69          *   3 for 1 us
70          *   4 for 2 us
71          *   5 for 4 us
72          *   6 for 8 us
73          *   7 for 16 us
74          */
75         switch (mpdudensity) {
76         case 0:
77                 return 0;
78         case 1:
79         case 2:
80         case 3:
81                 /* Our lower layer calculations limit our precision to
82                    1 microsecond */
83                 return 1;
84         case 4:
85                 return 2;
86         case 5:
87                 return 4;
88         case 6:
89                 return 8;
90         case 7:
91                 return 16;
92         default:
93                 return 0;
94         }
95 }
96
97 static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
98                                                 struct ieee80211_hw *hw)
99 {
100         struct ieee80211_channel *curchan = hw->conf.channel;
101         struct ath9k_channel *channel;
102         u8 chan_idx;
103
104         chan_idx = curchan->hw_value;
105         channel = &sc->sc_ah->channels[chan_idx];
106         ath9k_update_ichannel(sc, hw, channel);
107         return channel;
108 }
109
110 bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
111 {
112         unsigned long flags;
113         bool ret;
114
115         spin_lock_irqsave(&sc->sc_pm_lock, flags);
116         ret = ath9k_hw_setpower(sc->sc_ah, mode);
117         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
118
119         return ret;
120 }
121
122 void ath9k_ps_wakeup(struct ath_softc *sc)
123 {
124         unsigned long flags;
125
126         spin_lock_irqsave(&sc->sc_pm_lock, flags);
127         if (++sc->ps_usecount != 1)
128                 goto unlock;
129
130         ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
131
132  unlock:
133         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
134 }
135
136 void ath9k_ps_restore(struct ath_softc *sc)
137 {
138         unsigned long flags;
139
140         spin_lock_irqsave(&sc->sc_pm_lock, flags);
141         if (--sc->ps_usecount != 0)
142                 goto unlock;
143
144         if (sc->ps_idle)
145                 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
146         else if (sc->ps_enabled &&
147                  !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
148                               PS_WAIT_FOR_CAB |
149                               PS_WAIT_FOR_PSPOLL_DATA |
150                               PS_WAIT_FOR_TX_ACK)))
151                 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
152
153  unlock:
154         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
155 }
156
157 static void ath_start_ani(struct ath_common *common)
158 {
159         struct ath_hw *ah = common->ah;
160         unsigned long timestamp = jiffies_to_msecs(jiffies);
161         struct ath_softc *sc = (struct ath_softc *) common->priv;
162
163         if (!(sc->sc_flags & SC_OP_ANI_RUN))
164                 return;
165
166         if (sc->sc_flags & SC_OP_OFFCHANNEL)
167                 return;
168
169         common->ani.longcal_timer = timestamp;
170         common->ani.shortcal_timer = timestamp;
171         common->ani.checkani_timer = timestamp;
172
173         mod_timer(&common->ani.timer,
174                   jiffies +
175                         msecs_to_jiffies((u32)ah->config.ani_poll_interval));
176 }
177
178 /*
179  * Set/change channels.  If the channel is really being changed, it's done
180  * by reseting the chip.  To accomplish this we must first cleanup any pending
181  * DMA, then restart stuff.
182 */
183 int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
184                     struct ath9k_channel *hchan)
185 {
186         struct ath_wiphy *aphy = hw->priv;
187         struct ath_hw *ah = sc->sc_ah;
188         struct ath_common *common = ath9k_hw_common(ah);
189         struct ieee80211_conf *conf = &common->hw->conf;
190         bool fastcc = true, stopped;
191         struct ieee80211_channel *channel = hw->conf.channel;
192         struct ath9k_hw_cal_data *caldata = NULL;
193         int r;
194
195         if (sc->sc_flags & SC_OP_INVALID)
196                 return -EIO;
197
198         del_timer_sync(&common->ani.timer);
199         cancel_work_sync(&sc->paprd_work);
200         cancel_work_sync(&sc->hw_check_work);
201         cancel_delayed_work_sync(&sc->tx_complete_work);
202
203         ath9k_ps_wakeup(sc);
204
205         /*
206          * This is only performed if the channel settings have
207          * actually changed.
208          *
209          * To switch channels clear any pending DMA operations;
210          * wait long enough for the RX fifo to drain, reset the
211          * hardware at the new frequency, and then re-enable
212          * the relevant bits of the h/w.
213          */
214         ath9k_hw_set_interrupts(ah, 0);
215         ath_drain_all_txq(sc, false);
216         stopped = ath_stoprecv(sc);
217
218         /* XXX: do not flush receive queue here. We don't want
219          * to flush data frames already in queue because of
220          * changing channel. */
221
222         if (!stopped || !(sc->sc_flags & SC_OP_OFFCHANNEL))
223                 fastcc = false;
224
225         if (!(sc->sc_flags & SC_OP_OFFCHANNEL))
226                 caldata = &aphy->caldata;
227
228         ath_print(common, ATH_DBG_CONFIG,
229                   "(%u MHz) -> (%u MHz), conf_is_ht40: %d fastcc: %d\n",
230                   sc->sc_ah->curchan->channel,
231                   channel->center_freq, conf_is_ht40(conf),
232                   fastcc);
233
234         spin_lock_bh(&sc->sc_resetlock);
235
236         r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
237         if (r) {
238                 ath_print(common, ATH_DBG_FATAL,
239                           "Unable to reset channel (%u MHz), "
240                           "reset status %d\n",
241                           channel->center_freq, r);
242                 spin_unlock_bh(&sc->sc_resetlock);
243                 goto ps_restore;
244         }
245         spin_unlock_bh(&sc->sc_resetlock);
246
247         if (ath_startrecv(sc) != 0) {
248                 ath_print(common, ATH_DBG_FATAL,
249                           "Unable to restart recv logic\n");
250                 r = -EIO;
251                 goto ps_restore;
252         }
253
254         ath_cache_conf_rate(sc, &hw->conf);
255         ath_update_txpow(sc);
256         ath9k_hw_set_interrupts(ah, ah->imask);
257
258         if (!(sc->sc_flags & (SC_OP_OFFCHANNEL))) {
259                 ath_beacon_config(sc, NULL);
260                 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
261                 ath_start_ani(common);
262         }
263
264  ps_restore:
265         ath9k_ps_restore(sc);
266         return r;
267 }
268
269 static void ath_paprd_activate(struct ath_softc *sc)
270 {
271         struct ath_hw *ah = sc->sc_ah;
272         struct ath9k_hw_cal_data *caldata = ah->caldata;
273         struct ath_common *common = ath9k_hw_common(ah);
274         int chain;
275
276         if (!caldata || !caldata->paprd_done)
277                 return;
278
279         ath9k_ps_wakeup(sc);
280         ar9003_paprd_enable(ah, false);
281         for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
282                 if (!(common->tx_chainmask & BIT(chain)))
283                         continue;
284
285                 ar9003_paprd_populate_single_table(ah, caldata, chain);
286         }
287
288         ar9003_paprd_enable(ah, true);
289         ath9k_ps_restore(sc);
290 }
291
292 void ath_paprd_calibrate(struct work_struct *work)
293 {
294         struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
295         struct ieee80211_hw *hw = sc->hw;
296         struct ath_hw *ah = sc->sc_ah;
297         struct ieee80211_hdr *hdr;
298         struct sk_buff *skb = NULL;
299         struct ieee80211_tx_info *tx_info;
300         int band = hw->conf.channel->band;
301         struct ieee80211_supported_band *sband = &sc->sbands[band];
302         struct ath_tx_control txctl;
303         struct ath9k_hw_cal_data *caldata = ah->caldata;
304         struct ath_common *common = ath9k_hw_common(ah);
305         int qnum, ftype;
306         int chain_ok = 0;
307         int chain;
308         int len = 1800;
309         int time_left;
310         int i;
311
312         if (!caldata)
313                 return;
314
315         skb = alloc_skb(len, GFP_KERNEL);
316         if (!skb)
317                 return;
318
319         tx_info = IEEE80211_SKB_CB(skb);
320
321         skb_put(skb, len);
322         memset(skb->data, 0, len);
323         hdr = (struct ieee80211_hdr *)skb->data;
324         ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
325         hdr->frame_control = cpu_to_le16(ftype);
326         hdr->duration_id = cpu_to_le16(10);
327         memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
328         memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
329         memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
330
331         memset(&txctl, 0, sizeof(txctl));
332         qnum = sc->tx.hwq_map[WME_AC_BE];
333         txctl.txq = &sc->tx.txq[qnum];
334
335         ath9k_ps_wakeup(sc);
336         ar9003_paprd_init_table(ah);
337         for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
338                 if (!(common->tx_chainmask & BIT(chain)))
339                         continue;
340
341                 chain_ok = 0;
342                 memset(tx_info, 0, sizeof(*tx_info));
343                 tx_info->band = band;
344
345                 for (i = 0; i < 4; i++) {
346                         tx_info->control.rates[i].idx = sband->n_bitrates - 1;
347                         tx_info->control.rates[i].count = 6;
348                 }
349
350                 init_completion(&sc->paprd_complete);
351                 ar9003_paprd_setup_gain_table(ah, chain);
352                 txctl.paprd = BIT(chain);
353                 if (ath_tx_start(hw, skb, &txctl) != 0)
354                         break;
355
356                 time_left = wait_for_completion_timeout(&sc->paprd_complete,
357                                 msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
358                 if (!time_left) {
359                         ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
360                                   "Timeout waiting for paprd training on "
361                                   "TX chain %d\n",
362                                   chain);
363                         goto fail_paprd;
364                 }
365
366                 if (!ar9003_paprd_is_done(ah))
367                         break;
368
369                 if (ar9003_paprd_create_curve(ah, caldata, chain) != 0)
370                         break;
371
372                 chain_ok = 1;
373         }
374         kfree_skb(skb);
375
376         if (chain_ok) {
377                 caldata->paprd_done = true;
378                 ath_paprd_activate(sc);
379         }
380
381 fail_paprd:
382         ath9k_ps_restore(sc);
383 }
384
385 /*
386  *  This routine performs the periodic noise floor calibration function
387  *  that is used to adjust and optimize the chip performance.  This
388  *  takes environmental changes (location, temperature) into account.
389  *  When the task is complete, it reschedules itself depending on the
390  *  appropriate interval that was calculated.
391  */
392 void ath_ani_calibrate(unsigned long data)
393 {
394         struct ath_softc *sc = (struct ath_softc *)data;
395         struct ath_hw *ah = sc->sc_ah;
396         struct ath_common *common = ath9k_hw_common(ah);
397         bool longcal = false;
398         bool shortcal = false;
399         bool aniflag = false;
400         unsigned int timestamp = jiffies_to_msecs(jiffies);
401         u32 cal_interval, short_cal_interval, long_cal_interval;
402
403         if (ah->caldata && ah->caldata->nfcal_interference)
404                 long_cal_interval = ATH_LONG_CALINTERVAL_INT;
405         else
406                 long_cal_interval = ATH_LONG_CALINTERVAL;
407
408         short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
409                 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
410
411         /* Only calibrate if awake */
412         if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
413                 goto set_timer;
414
415         ath9k_ps_wakeup(sc);
416
417         /* Long calibration runs independently of short calibration. */
418         if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
419                 longcal = true;
420                 ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
421                 common->ani.longcal_timer = timestamp;
422         }
423
424         /* Short calibration applies only while caldone is false */
425         if (!common->ani.caldone) {
426                 if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
427                         shortcal = true;
428                         ath_print(common, ATH_DBG_ANI,
429                                   "shortcal @%lu\n", jiffies);
430                         common->ani.shortcal_timer = timestamp;
431                         common->ani.resetcal_timer = timestamp;
432                 }
433         } else {
434                 if ((timestamp - common->ani.resetcal_timer) >=
435                     ATH_RESTART_CALINTERVAL) {
436                         common->ani.caldone = ath9k_hw_reset_calvalid(ah);
437                         if (common->ani.caldone)
438                                 common->ani.resetcal_timer = timestamp;
439                 }
440         }
441
442         /* Verify whether we must check ANI */
443         if ((timestamp - common->ani.checkani_timer) >=
444              ah->config.ani_poll_interval) {
445                 aniflag = true;
446                 common->ani.checkani_timer = timestamp;
447         }
448
449         /* Skip all processing if there's nothing to do. */
450         if (longcal || shortcal || aniflag) {
451                 /* Call ANI routine if necessary */
452                 if (aniflag)
453                         ath9k_hw_ani_monitor(ah, ah->curchan);
454
455                 /* Perform calibration if necessary */
456                 if (longcal || shortcal) {
457                         common->ani.caldone =
458                                 ath9k_hw_calibrate(ah,
459                                                    ah->curchan,
460                                                    common->rx_chainmask,
461                                                    longcal);
462                 }
463         }
464
465         ath9k_ps_restore(sc);
466
467 set_timer:
468         /*
469         * Set timer interval based on previous results.
470         * The interval must be the shortest necessary to satisfy ANI,
471         * short calibration and long calibration.
472         */
473         cal_interval = ATH_LONG_CALINTERVAL;
474         if (sc->sc_ah->config.enable_ani)
475                 cal_interval = min(cal_interval,
476                                    (u32)ah->config.ani_poll_interval);
477         if (!common->ani.caldone)
478                 cal_interval = min(cal_interval, (u32)short_cal_interval);
479
480         mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
481         if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->caldata) {
482                 if (!ah->caldata->paprd_done)
483                         ieee80211_queue_work(sc->hw, &sc->paprd_work);
484                 else
485                         ath_paprd_activate(sc);
486         }
487 }
488
489 /*
490  * Update tx/rx chainmask. For legacy association,
491  * hard code chainmask to 1x1, for 11n association, use
492  * the chainmask configuration, for bt coexistence, use
493  * the chainmask configuration even in legacy mode.
494  */
495 void ath_update_chainmask(struct ath_softc *sc, int is_ht)
496 {
497         struct ath_hw *ah = sc->sc_ah;
498         struct ath_common *common = ath9k_hw_common(ah);
499
500         if ((sc->sc_flags & SC_OP_OFFCHANNEL) || is_ht ||
501             (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
502                 common->tx_chainmask = ah->caps.tx_chainmask;
503                 common->rx_chainmask = ah->caps.rx_chainmask;
504         } else {
505                 common->tx_chainmask = 1;
506                 common->rx_chainmask = 1;
507         }
508
509         ath_print(common, ATH_DBG_CONFIG,
510                   "tx chmask: %d, rx chmask: %d\n",
511                   common->tx_chainmask,
512                   common->rx_chainmask);
513 }
514
515 static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
516 {
517         struct ath_node *an;
518
519         an = (struct ath_node *)sta->drv_priv;
520
521         if (sc->sc_flags & SC_OP_TXAGGR) {
522                 ath_tx_node_init(sc, an);
523                 an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
524                                      sta->ht_cap.ampdu_factor);
525                 an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
526                 an->last_rssi = ATH_RSSI_DUMMY_MARKER;
527         }
528 }
529
530 static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
531 {
532         struct ath_node *an = (struct ath_node *)sta->drv_priv;
533
534         if (sc->sc_flags & SC_OP_TXAGGR)
535                 ath_tx_node_cleanup(sc, an);
536 }
537
538 void ath_hw_check(struct work_struct *work)
539 {
540         struct ath_softc *sc = container_of(work, struct ath_softc, hw_check_work);
541         int i;
542
543         ath9k_ps_wakeup(sc);
544
545         for (i = 0; i < 3; i++) {
546                 if (ath9k_hw_check_alive(sc->sc_ah))
547                         goto out;
548
549                 msleep(1);
550         }
551         ath_reset(sc, false);
552
553 out:
554         ath9k_ps_restore(sc);
555 }
556
557 void ath9k_tasklet(unsigned long data)
558 {
559         struct ath_softc *sc = (struct ath_softc *)data;
560         struct ath_hw *ah = sc->sc_ah;
561         struct ath_common *common = ath9k_hw_common(ah);
562
563         u32 status = sc->intrstatus;
564         u32 rxmask;
565
566         ath9k_ps_wakeup(sc);
567
568         if (status & ATH9K_INT_FATAL) {
569                 ath_reset(sc, false);
570                 ath9k_ps_restore(sc);
571                 return;
572         }
573
574         if (!ath9k_hw_check_alive(ah))
575                 ieee80211_queue_work(sc->hw, &sc->hw_check_work);
576
577         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
578                 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
579                           ATH9K_INT_RXORN);
580         else
581                 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
582
583         if (status & rxmask) {
584                 spin_lock_bh(&sc->rx.rxflushlock);
585
586                 /* Check for high priority Rx first */
587                 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
588                     (status & ATH9K_INT_RXHP))
589                         ath_rx_tasklet(sc, 0, true);
590
591                 ath_rx_tasklet(sc, 0, false);
592                 spin_unlock_bh(&sc->rx.rxflushlock);
593         }
594
595         if (status & ATH9K_INT_TX) {
596                 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
597                         ath_tx_edma_tasklet(sc);
598                 else
599                         ath_tx_tasklet(sc);
600         }
601
602         if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
603                 /*
604                  * TSF sync does not look correct; remain awake to sync with
605                  * the next Beacon.
606                  */
607                 ath_print(common, ATH_DBG_PS,
608                           "TSFOOR - Sync with next Beacon\n");
609                 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
610         }
611
612         if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
613                 if (status & ATH9K_INT_GENTIMER)
614                         ath_gen_timer_isr(sc->sc_ah);
615
616         /* re-enable hardware interrupt */
617         ath9k_hw_set_interrupts(ah, ah->imask);
618         ath9k_ps_restore(sc);
619 }
620
621 irqreturn_t ath_isr(int irq, void *dev)
622 {
623 #define SCHED_INTR (                            \
624                 ATH9K_INT_FATAL |               \
625                 ATH9K_INT_RXORN |               \
626                 ATH9K_INT_RXEOL |               \
627                 ATH9K_INT_RX |                  \
628                 ATH9K_INT_RXLP |                \
629                 ATH9K_INT_RXHP |                \
630                 ATH9K_INT_TX |                  \
631                 ATH9K_INT_BMISS |               \
632                 ATH9K_INT_CST |                 \
633                 ATH9K_INT_TSFOOR |              \
634                 ATH9K_INT_GENTIMER)
635
636         struct ath_softc *sc = dev;
637         struct ath_hw *ah = sc->sc_ah;
638         enum ath9k_int status;
639         bool sched = false;
640
641         /*
642          * The hardware is not ready/present, don't
643          * touch anything. Note this can happen early
644          * on if the IRQ is shared.
645          */
646         if (sc->sc_flags & SC_OP_INVALID)
647                 return IRQ_NONE;
648
649
650         /* shared irq, not for us */
651
652         if (!ath9k_hw_intrpend(ah))
653                 return IRQ_NONE;
654
655         /*
656          * Figure out the reason(s) for the interrupt.  Note
657          * that the hal returns a pseudo-ISR that may include
658          * bits we haven't explicitly enabled so we mask the
659          * value to insure we only process bits we requested.
660          */
661         ath9k_hw_getisr(ah, &status);   /* NB: clears ISR too */
662         status &= ah->imask;    /* discard unasked-for bits */
663
664         /*
665          * If there are no status bits set, then this interrupt was not
666          * for me (should have been caught above).
667          */
668         if (!status)
669                 return IRQ_NONE;
670
671         /* Cache the status */
672         sc->intrstatus = status;
673
674         if (status & SCHED_INTR)
675                 sched = true;
676
677         /*
678          * If a FATAL or RXORN interrupt is received, we have to reset the
679          * chip immediately.
680          */
681         if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
682             !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
683                 goto chip_reset;
684
685         if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
686             (status & ATH9K_INT_BB_WATCHDOG)) {
687                 ar9003_hw_bb_watchdog_dbg_info(ah);
688                 goto chip_reset;
689         }
690
691         if (status & ATH9K_INT_SWBA)
692                 tasklet_schedule(&sc->bcon_tasklet);
693
694         if (status & ATH9K_INT_TXURN)
695                 ath9k_hw_updatetxtriglevel(ah, true);
696
697         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
698                 if (status & ATH9K_INT_RXEOL) {
699                         ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
700                         ath9k_hw_set_interrupts(ah, ah->imask);
701                 }
702         }
703
704         if (status & ATH9K_INT_MIB) {
705                 /*
706                  * Disable interrupts until we service the MIB
707                  * interrupt; otherwise it will continue to
708                  * fire.
709                  */
710                 ath9k_hw_set_interrupts(ah, 0);
711                 /*
712                  * Let the hal handle the event. We assume
713                  * it will clear whatever condition caused
714                  * the interrupt.
715                  */
716                 ath9k_hw_proc_mib_event(ah);
717                 ath9k_hw_set_interrupts(ah, ah->imask);
718         }
719
720         if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
721                 if (status & ATH9K_INT_TIM_TIMER) {
722                         /* Clear RxAbort bit so that we can
723                          * receive frames */
724                         ath9k_setpower(sc, ATH9K_PM_AWAKE);
725                         ath9k_hw_setrxabort(sc->sc_ah, 0);
726                         sc->ps_flags |= PS_WAIT_FOR_BEACON;
727                 }
728
729 chip_reset:
730
731         ath_debug_stat_interrupt(sc, status);
732
733         if (sched) {
734                 /* turn off every interrupt except SWBA */
735                 ath9k_hw_set_interrupts(ah, (ah->imask & ATH9K_INT_SWBA));
736                 tasklet_schedule(&sc->intr_tq);
737         }
738
739         return IRQ_HANDLED;
740
741 #undef SCHED_INTR
742 }
743
744 static u32 ath_get_extchanmode(struct ath_softc *sc,
745                                struct ieee80211_channel *chan,
746                                enum nl80211_channel_type channel_type)
747 {
748         u32 chanmode = 0;
749
750         switch (chan->band) {
751         case IEEE80211_BAND_2GHZ:
752                 switch(channel_type) {
753                 case NL80211_CHAN_NO_HT:
754                 case NL80211_CHAN_HT20:
755                         chanmode = CHANNEL_G_HT20;
756                         break;
757                 case NL80211_CHAN_HT40PLUS:
758                         chanmode = CHANNEL_G_HT40PLUS;
759                         break;
760                 case NL80211_CHAN_HT40MINUS:
761                         chanmode = CHANNEL_G_HT40MINUS;
762                         break;
763                 }
764                 break;
765         case IEEE80211_BAND_5GHZ:
766                 switch(channel_type) {
767                 case NL80211_CHAN_NO_HT:
768                 case NL80211_CHAN_HT20:
769                         chanmode = CHANNEL_A_HT20;
770                         break;
771                 case NL80211_CHAN_HT40PLUS:
772                         chanmode = CHANNEL_A_HT40PLUS;
773                         break;
774                 case NL80211_CHAN_HT40MINUS:
775                         chanmode = CHANNEL_A_HT40MINUS;
776                         break;
777                 }
778                 break;
779         default:
780                 break;
781         }
782
783         return chanmode;
784 }
785
786 static void ath9k_bss_assoc_info(struct ath_softc *sc,
787                                  struct ieee80211_vif *vif,
788                                  struct ieee80211_bss_conf *bss_conf)
789 {
790         struct ath_hw *ah = sc->sc_ah;
791         struct ath_common *common = ath9k_hw_common(ah);
792
793         if (bss_conf->assoc) {
794                 ath_print(common, ATH_DBG_CONFIG,
795                           "Bss Info ASSOC %d, bssid: %pM\n",
796                            bss_conf->aid, common->curbssid);
797
798                 /* New association, store aid */
799                 common->curaid = bss_conf->aid;
800                 ath9k_hw_write_associd(ah);
801
802                 /*
803                  * Request a re-configuration of Beacon related timers
804                  * on the receipt of the first Beacon frame (i.e.,
805                  * after time sync with the AP).
806                  */
807                 sc->ps_flags |= PS_BEACON_SYNC;
808
809                 /* Configure the beacon */
810                 ath_beacon_config(sc, vif);
811
812                 /* Reset rssi stats */
813                 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
814
815                 sc->sc_flags |= SC_OP_ANI_RUN;
816                 ath_start_ani(common);
817         } else {
818                 ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
819                 common->curaid = 0;
820                 /* Stop ANI */
821                 sc->sc_flags &= ~SC_OP_ANI_RUN;
822                 del_timer_sync(&common->ani.timer);
823         }
824 }
825
826 void ath_radio_enable(struct ath_softc *sc, struct ieee80211_hw *hw)
827 {
828         struct ath_hw *ah = sc->sc_ah;
829         struct ath_common *common = ath9k_hw_common(ah);
830         struct ieee80211_channel *channel = hw->conf.channel;
831         int r;
832
833         ath9k_ps_wakeup(sc);
834         ath9k_hw_configpcipowersave(ah, 0, 0);
835
836         if (!ah->curchan)
837                 ah->curchan = ath_get_curchannel(sc, sc->hw);
838
839         spin_lock_bh(&sc->sc_resetlock);
840         r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
841         if (r) {
842                 ath_print(common, ATH_DBG_FATAL,
843                           "Unable to reset channel (%u MHz), "
844                           "reset status %d\n",
845                           channel->center_freq, r);
846         }
847         spin_unlock_bh(&sc->sc_resetlock);
848
849         ath_update_txpow(sc);
850         if (ath_startrecv(sc) != 0) {
851                 ath_print(common, ATH_DBG_FATAL,
852                           "Unable to restart recv logic\n");
853                 return;
854         }
855
856         if (sc->sc_flags & SC_OP_BEACONS)
857                 ath_beacon_config(sc, NULL);    /* restart beacons */
858
859         /* Re-Enable  interrupts */
860         ath9k_hw_set_interrupts(ah, ah->imask);
861
862         /* Enable LED */
863         ath9k_hw_cfg_output(ah, ah->led_pin,
864                             AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
865         ath9k_hw_set_gpio(ah, ah->led_pin, 0);
866
867         ieee80211_wake_queues(hw);
868         ath9k_ps_restore(sc);
869 }
870
871 void ath_radio_disable(struct ath_softc *sc, struct ieee80211_hw *hw)
872 {
873         struct ath_hw *ah = sc->sc_ah;
874         struct ieee80211_channel *channel = hw->conf.channel;
875         int r;
876
877         ath9k_ps_wakeup(sc);
878         ieee80211_stop_queues(hw);
879
880         /*
881          * Keep the LED on when the radio is disabled
882          * during idle unassociated state.
883          */
884         if (!sc->ps_idle) {
885                 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
886                 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
887         }
888
889         /* Disable interrupts */
890         ath9k_hw_set_interrupts(ah, 0);
891
892         ath_drain_all_txq(sc, false);   /* clear pending tx frames */
893         ath_stoprecv(sc);               /* turn off frame recv */
894         ath_flushrecv(sc);              /* flush recv queue */
895
896         if (!ah->curchan)
897                 ah->curchan = ath_get_curchannel(sc, hw);
898
899         spin_lock_bh(&sc->sc_resetlock);
900         r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
901         if (r) {
902                 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
903                           "Unable to reset channel (%u MHz), "
904                           "reset status %d\n",
905                           channel->center_freq, r);
906         }
907         spin_unlock_bh(&sc->sc_resetlock);
908
909         ath9k_hw_phy_disable(ah);
910         ath9k_hw_configpcipowersave(ah, 1, 1);
911         ath9k_ps_restore(sc);
912         ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
913 }
914
915 int ath_reset(struct ath_softc *sc, bool retry_tx)
916 {
917         struct ath_hw *ah = sc->sc_ah;
918         struct ath_common *common = ath9k_hw_common(ah);
919         struct ieee80211_hw *hw = sc->hw;
920         int r;
921
922         /* Stop ANI */
923         del_timer_sync(&common->ani.timer);
924
925         ieee80211_stop_queues(hw);
926
927         ath9k_hw_set_interrupts(ah, 0);
928         ath_drain_all_txq(sc, retry_tx);
929         ath_stoprecv(sc);
930         ath_flushrecv(sc);
931
932         spin_lock_bh(&sc->sc_resetlock);
933         r = ath9k_hw_reset(ah, sc->sc_ah->curchan, ah->caldata, false);
934         if (r)
935                 ath_print(common, ATH_DBG_FATAL,
936                           "Unable to reset hardware; reset status %d\n", r);
937         spin_unlock_bh(&sc->sc_resetlock);
938
939         if (ath_startrecv(sc) != 0)
940                 ath_print(common, ATH_DBG_FATAL,
941                           "Unable to start recv logic\n");
942
943         /*
944          * We may be doing a reset in response to a request
945          * that changes the channel so update any state that
946          * might change as a result.
947          */
948         ath_cache_conf_rate(sc, &hw->conf);
949
950         ath_update_txpow(sc);
951
952         if ((sc->sc_flags & SC_OP_BEACONS) || !(sc->sc_flags & (SC_OP_OFFCHANNEL)))
953                 ath_beacon_config(sc, NULL);    /* restart beacons */
954
955         ath9k_hw_set_interrupts(ah, ah->imask);
956
957         if (retry_tx) {
958                 int i;
959                 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
960                         if (ATH_TXQ_SETUP(sc, i)) {
961                                 spin_lock_bh(&sc->tx.txq[i].axq_lock);
962                                 ath_txq_schedule(sc, &sc->tx.txq[i]);
963                                 spin_unlock_bh(&sc->tx.txq[i].axq_lock);
964                         }
965                 }
966         }
967
968         ieee80211_wake_queues(hw);
969
970         /* Start ANI */
971         ath_start_ani(common);
972
973         return r;
974 }
975
976 static int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
977 {
978         int qnum;
979
980         switch (queue) {
981         case 0:
982                 qnum = sc->tx.hwq_map[WME_AC_VO];
983                 break;
984         case 1:
985                 qnum = sc->tx.hwq_map[WME_AC_VI];
986                 break;
987         case 2:
988                 qnum = sc->tx.hwq_map[WME_AC_BE];
989                 break;
990         case 3:
991                 qnum = sc->tx.hwq_map[WME_AC_BK];
992                 break;
993         default:
994                 qnum = sc->tx.hwq_map[WME_AC_BE];
995                 break;
996         }
997
998         return qnum;
999 }
1000
1001 int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
1002 {
1003         int qnum;
1004
1005         switch (queue) {
1006         case WME_AC_VO:
1007                 qnum = 0;
1008                 break;
1009         case WME_AC_VI:
1010                 qnum = 1;
1011                 break;
1012         case WME_AC_BE:
1013                 qnum = 2;
1014                 break;
1015         case WME_AC_BK:
1016                 qnum = 3;
1017                 break;
1018         default:
1019                 qnum = -1;
1020                 break;
1021         }
1022
1023         return qnum;
1024 }
1025
1026 /* XXX: Remove me once we don't depend on ath9k_channel for all
1027  * this redundant data */
1028 void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
1029                            struct ath9k_channel *ichan)
1030 {
1031         struct ieee80211_channel *chan = hw->conf.channel;
1032         struct ieee80211_conf *conf = &hw->conf;
1033
1034         ichan->channel = chan->center_freq;
1035         ichan->chan = chan;
1036
1037         if (chan->band == IEEE80211_BAND_2GHZ) {
1038                 ichan->chanmode = CHANNEL_G;
1039                 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
1040         } else {
1041                 ichan->chanmode = CHANNEL_A;
1042                 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
1043         }
1044
1045         if (conf_is_ht(conf))
1046                 ichan->chanmode = ath_get_extchanmode(sc, chan,
1047                                             conf->channel_type);
1048 }
1049
1050 /**********************/
1051 /* mac80211 callbacks */
1052 /**********************/
1053
1054 static int ath9k_start(struct ieee80211_hw *hw)
1055 {
1056         struct ath_wiphy *aphy = hw->priv;
1057         struct ath_softc *sc = aphy->sc;
1058         struct ath_hw *ah = sc->sc_ah;
1059         struct ath_common *common = ath9k_hw_common(ah);
1060         struct ieee80211_channel *curchan = hw->conf.channel;
1061         struct ath9k_channel *init_channel;
1062         int r;
1063
1064         ath_print(common, ATH_DBG_CONFIG,
1065                   "Starting driver with initial channel: %d MHz\n",
1066                   curchan->center_freq);
1067
1068         mutex_lock(&sc->mutex);
1069
1070         if (ath9k_wiphy_started(sc)) {
1071                 if (sc->chan_idx == curchan->hw_value) {
1072                         /*
1073                          * Already on the operational channel, the new wiphy
1074                          * can be marked active.
1075                          */
1076                         aphy->state = ATH_WIPHY_ACTIVE;
1077                         ieee80211_wake_queues(hw);
1078                 } else {
1079                         /*
1080                          * Another wiphy is on another channel, start the new
1081                          * wiphy in paused state.
1082                          */
1083                         aphy->state = ATH_WIPHY_PAUSED;
1084                         ieee80211_stop_queues(hw);
1085                 }
1086                 mutex_unlock(&sc->mutex);
1087                 return 0;
1088         }
1089         aphy->state = ATH_WIPHY_ACTIVE;
1090
1091         /* setup initial channel */
1092
1093         sc->chan_idx = curchan->hw_value;
1094
1095         init_channel = ath_get_curchannel(sc, hw);
1096
1097         /* Reset SERDES registers */
1098         ath9k_hw_configpcipowersave(ah, 0, 0);
1099
1100         /*
1101          * The basic interface to setting the hardware in a good
1102          * state is ``reset''.  On return the hardware is known to
1103          * be powered up and with interrupts disabled.  This must
1104          * be followed by initialization of the appropriate bits
1105          * and then setup of the interrupt mask.
1106          */
1107         spin_lock_bh(&sc->sc_resetlock);
1108         r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
1109         if (r) {
1110                 ath_print(common, ATH_DBG_FATAL,
1111                           "Unable to reset hardware; reset status %d "
1112                           "(freq %u MHz)\n", r,
1113                           curchan->center_freq);
1114                 spin_unlock_bh(&sc->sc_resetlock);
1115                 goto mutex_unlock;
1116         }
1117         spin_unlock_bh(&sc->sc_resetlock);
1118
1119         /*
1120          * This is needed only to setup initial state
1121          * but it's best done after a reset.
1122          */
1123         ath_update_txpow(sc);
1124
1125         /*
1126          * Setup the hardware after reset:
1127          * The receive engine is set going.
1128          * Frame transmit is handled entirely
1129          * in the frame output path; there's nothing to do
1130          * here except setup the interrupt mask.
1131          */
1132         if (ath_startrecv(sc) != 0) {
1133                 ath_print(common, ATH_DBG_FATAL,
1134                           "Unable to start recv logic\n");
1135                 r = -EIO;
1136                 goto mutex_unlock;
1137         }
1138
1139         /* Setup our intr mask. */
1140         ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
1141                     ATH9K_INT_RXORN | ATH9K_INT_FATAL |
1142                     ATH9K_INT_GLOBAL;
1143
1144         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
1145                 ah->imask |= ATH9K_INT_RXHP |
1146                              ATH9K_INT_RXLP |
1147                              ATH9K_INT_BB_WATCHDOG;
1148         else
1149                 ah->imask |= ATH9K_INT_RX;
1150
1151         ah->imask |= ATH9K_INT_GTT;
1152
1153         if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
1154                 ah->imask |= ATH9K_INT_CST;
1155
1156         ath_cache_conf_rate(sc, &hw->conf);
1157
1158         sc->sc_flags &= ~SC_OP_INVALID;
1159
1160         /* Disable BMISS interrupt when we're not associated */
1161         ah->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1162         ath9k_hw_set_interrupts(ah, ah->imask);
1163
1164         ieee80211_wake_queues(hw);
1165
1166         ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
1167
1168         if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
1169             !ah->btcoex_hw.enabled) {
1170                 ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
1171                                            AR_STOMP_LOW_WLAN_WGHT);
1172                 ath9k_hw_btcoex_enable(ah);
1173
1174                 if (common->bus_ops->bt_coex_prep)
1175                         common->bus_ops->bt_coex_prep(common);
1176                 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
1177                         ath9k_btcoex_timer_resume(sc);
1178         }
1179
1180 mutex_unlock:
1181         mutex_unlock(&sc->mutex);
1182
1183         return r;
1184 }
1185
1186 static int ath9k_tx(struct ieee80211_hw *hw,
1187                     struct sk_buff *skb)
1188 {
1189         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1190         struct ath_wiphy *aphy = hw->priv;
1191         struct ath_softc *sc = aphy->sc;
1192         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1193         struct ath_tx_control txctl;
1194         int padpos, padsize;
1195         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1196         int qnum;
1197
1198         if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
1199                 ath_print(common, ATH_DBG_XMIT,
1200                           "ath9k: %s: TX in unexpected wiphy state "
1201                           "%d\n", wiphy_name(hw->wiphy), aphy->state);
1202                 goto exit;
1203         }
1204
1205         if (sc->ps_enabled) {
1206                 /*
1207                  * mac80211 does not set PM field for normal data frames, so we
1208                  * need to update that based on the current PS mode.
1209                  */
1210                 if (ieee80211_is_data(hdr->frame_control) &&
1211                     !ieee80211_is_nullfunc(hdr->frame_control) &&
1212                     !ieee80211_has_pm(hdr->frame_control)) {
1213                         ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
1214                                   "while in PS mode\n");
1215                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1216                 }
1217         }
1218
1219         if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
1220                 /*
1221                  * We are using PS-Poll and mac80211 can request TX while in
1222                  * power save mode. Need to wake up hardware for the TX to be
1223                  * completed and if needed, also for RX of buffered frames.
1224                  */
1225                 ath9k_ps_wakeup(sc);
1226                 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
1227                         ath9k_hw_setrxabort(sc->sc_ah, 0);
1228                 if (ieee80211_is_pspoll(hdr->frame_control)) {
1229                         ath_print(common, ATH_DBG_PS,
1230                                   "Sending PS-Poll to pick a buffered frame\n");
1231                         sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
1232                 } else {
1233                         ath_print(common, ATH_DBG_PS,
1234                                   "Wake up to complete TX\n");
1235                         sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
1236                 }
1237                 /*
1238                  * The actual restore operation will happen only after
1239                  * the sc_flags bit is cleared. We are just dropping
1240                  * the ps_usecount here.
1241                  */
1242                 ath9k_ps_restore(sc);
1243         }
1244
1245         memset(&txctl, 0, sizeof(struct ath_tx_control));
1246
1247         /*
1248          * As a temporary workaround, assign seq# here; this will likely need
1249          * to be cleaned up to work better with Beacon transmission and virtual
1250          * BSSes.
1251          */
1252         if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1253                 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1254                         sc->tx.seq_no += 0x10;
1255                 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1256                 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
1257         }
1258
1259         /* Add the padding after the header if this is not already done */
1260         padpos = ath9k_cmn_padpos(hdr->frame_control);
1261         padsize = padpos & 3;
1262         if (padsize && skb->len>padpos) {
1263                 if (skb_headroom(skb) < padsize)
1264                         return -1;
1265                 skb_push(skb, padsize);
1266                 memmove(skb->data, skb->data + padsize, padpos);
1267         }
1268
1269         qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
1270         txctl.txq = &sc->tx.txq[qnum];
1271
1272         ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
1273
1274         if (ath_tx_start(hw, skb, &txctl) != 0) {
1275                 ath_print(common, ATH_DBG_XMIT, "TX failed\n");
1276                 goto exit;
1277         }
1278
1279         return 0;
1280 exit:
1281         dev_kfree_skb_any(skb);
1282         return 0;
1283 }
1284
1285 static void ath9k_stop(struct ieee80211_hw *hw)
1286 {
1287         struct ath_wiphy *aphy = hw->priv;
1288         struct ath_softc *sc = aphy->sc;
1289         struct ath_hw *ah = sc->sc_ah;
1290         struct ath_common *common = ath9k_hw_common(ah);
1291         int i;
1292
1293         mutex_lock(&sc->mutex);
1294
1295         aphy->state = ATH_WIPHY_INACTIVE;
1296
1297         if (led_blink)
1298                 cancel_delayed_work_sync(&sc->ath_led_blink_work);
1299
1300         cancel_delayed_work_sync(&sc->tx_complete_work);
1301         cancel_work_sync(&sc->paprd_work);
1302         cancel_work_sync(&sc->hw_check_work);
1303
1304         for (i = 0; i < sc->num_sec_wiphy; i++) {
1305                 if (sc->sec_wiphy[i])
1306                         break;
1307         }
1308
1309         if (i == sc->num_sec_wiphy) {
1310                 cancel_delayed_work_sync(&sc->wiphy_work);
1311                 cancel_work_sync(&sc->chan_work);
1312         }
1313
1314         if (sc->sc_flags & SC_OP_INVALID) {
1315                 ath_print(common, ATH_DBG_ANY, "Device not present\n");
1316                 mutex_unlock(&sc->mutex);
1317                 return;
1318         }
1319
1320         if (ath9k_wiphy_started(sc)) {
1321                 mutex_unlock(&sc->mutex);
1322                 return; /* another wiphy still in use */
1323         }
1324
1325         /* Ensure HW is awake when we try to shut it down. */
1326         ath9k_ps_wakeup(sc);
1327
1328         if (ah->btcoex_hw.enabled) {
1329                 ath9k_hw_btcoex_disable(ah);
1330                 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
1331                         ath9k_btcoex_timer_pause(sc);
1332         }
1333
1334         /* make sure h/w will not generate any interrupt
1335          * before setting the invalid flag. */
1336         ath9k_hw_set_interrupts(ah, 0);
1337
1338         if (!(sc->sc_flags & SC_OP_INVALID)) {
1339                 ath_drain_all_txq(sc, false);
1340                 ath_stoprecv(sc);
1341                 ath9k_hw_phy_disable(ah);
1342         } else
1343                 sc->rx.rxlink = NULL;
1344
1345         /* disable HAL and put h/w to sleep */
1346         ath9k_hw_disable(ah);
1347         ath9k_hw_configpcipowersave(ah, 1, 1);
1348         ath9k_ps_restore(sc);
1349
1350         /* Finally, put the chip in FULL SLEEP mode */
1351         ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
1352
1353         sc->sc_flags |= SC_OP_INVALID;
1354
1355         mutex_unlock(&sc->mutex);
1356
1357         ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
1358 }
1359
1360 static int ath9k_add_interface(struct ieee80211_hw *hw,
1361                                struct ieee80211_vif *vif)
1362 {
1363         struct ath_wiphy *aphy = hw->priv;
1364         struct ath_softc *sc = aphy->sc;
1365         struct ath_hw *ah = sc->sc_ah;
1366         struct ath_common *common = ath9k_hw_common(ah);
1367         struct ath_vif *avp = (void *)vif->drv_priv;
1368         enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
1369         int ret = 0;
1370
1371         mutex_lock(&sc->mutex);
1372
1373         switch (vif->type) {
1374         case NL80211_IFTYPE_STATION:
1375                 ic_opmode = NL80211_IFTYPE_STATION;
1376                 break;
1377         case NL80211_IFTYPE_WDS:
1378                 ic_opmode = NL80211_IFTYPE_WDS;
1379                 break;
1380         case NL80211_IFTYPE_ADHOC:
1381         case NL80211_IFTYPE_AP:
1382         case NL80211_IFTYPE_MESH_POINT:
1383                 if (sc->nbcnvifs >= ATH_BCBUF) {
1384                         ret = -ENOBUFS;
1385                         goto out;
1386                 }
1387                 ic_opmode = vif->type;
1388                 break;
1389         default:
1390                 ath_print(common, ATH_DBG_FATAL,
1391                         "Interface type %d not yet supported\n", vif->type);
1392                 ret = -EOPNOTSUPP;
1393                 goto out;
1394         }
1395
1396         ath_print(common, ATH_DBG_CONFIG,
1397                   "Attach a VIF of type: %d\n", ic_opmode);
1398
1399         /* Set the VIF opmode */
1400         avp->av_opmode = ic_opmode;
1401         avp->av_bslot = -1;
1402
1403         sc->nvifs++;
1404
1405         ath9k_set_bssid_mask(hw, vif);
1406
1407         if (sc->nvifs > 1)
1408                 goto out; /* skip global settings for secondary vif */
1409
1410         if (ic_opmode == NL80211_IFTYPE_AP) {
1411                 ath9k_hw_set_tsfadjust(ah, 1);
1412                 sc->sc_flags |= SC_OP_TSF_RESET;
1413         }
1414
1415         /* Set the device opmode */
1416         ah->opmode = ic_opmode;
1417
1418         /*
1419          * Enable MIB interrupts when there are hardware phy counters.
1420          * Note we only do this (at the moment) for station mode.
1421          */
1422         if ((vif->type == NL80211_IFTYPE_STATION) ||
1423             (vif->type == NL80211_IFTYPE_ADHOC) ||
1424             (vif->type == NL80211_IFTYPE_MESH_POINT)) {
1425                 if (ah->config.enable_ani)
1426                         ah->imask |= ATH9K_INT_MIB;
1427                 ah->imask |= ATH9K_INT_TSFOOR;
1428         }
1429
1430         ath9k_hw_set_interrupts(ah, ah->imask);
1431
1432         if (vif->type == NL80211_IFTYPE_AP    ||
1433             vif->type == NL80211_IFTYPE_ADHOC ||
1434             vif->type == NL80211_IFTYPE_MONITOR) {
1435                 sc->sc_flags |= SC_OP_ANI_RUN;
1436                 ath_start_ani(common);
1437         }
1438
1439 out:
1440         mutex_unlock(&sc->mutex);
1441         return ret;
1442 }
1443
1444 static void ath9k_remove_interface(struct ieee80211_hw *hw,
1445                                    struct ieee80211_vif *vif)
1446 {
1447         struct ath_wiphy *aphy = hw->priv;
1448         struct ath_softc *sc = aphy->sc;
1449         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1450         struct ath_vif *avp = (void *)vif->drv_priv;
1451         int i;
1452
1453         ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
1454
1455         mutex_lock(&sc->mutex);
1456
1457         /* Stop ANI */
1458         sc->sc_flags &= ~SC_OP_ANI_RUN;
1459         del_timer_sync(&common->ani.timer);
1460
1461         /* Reclaim beacon resources */
1462         if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
1463             (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
1464             (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
1465                 ath9k_ps_wakeup(sc);
1466                 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1467                 ath9k_ps_restore(sc);
1468         }
1469
1470         ath_beacon_return(sc, avp);
1471         sc->sc_flags &= ~SC_OP_BEACONS;
1472
1473         for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
1474                 if (sc->beacon.bslot[i] == vif) {
1475                         printk(KERN_DEBUG "%s: vif had allocated beacon "
1476                                "slot\n", __func__);
1477                         sc->beacon.bslot[i] = NULL;
1478                         sc->beacon.bslot_aphy[i] = NULL;
1479                 }
1480         }
1481
1482         sc->nvifs--;
1483
1484         mutex_unlock(&sc->mutex);
1485 }
1486
1487 void ath9k_enable_ps(struct ath_softc *sc)
1488 {
1489         struct ath_hw *ah = sc->sc_ah;
1490
1491         sc->ps_enabled = true;
1492         if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1493                 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1494                         ah->imask |= ATH9K_INT_TIM_TIMER;
1495                         ath9k_hw_set_interrupts(ah, ah->imask);
1496                 }
1497                 ath9k_hw_setrxabort(ah, 1);
1498         }
1499 }
1500
1501 static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
1502 {
1503         struct ath_wiphy *aphy = hw->priv;
1504         struct ath_softc *sc = aphy->sc;
1505         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1506         struct ieee80211_conf *conf = &hw->conf;
1507         struct ath_hw *ah = sc->sc_ah;
1508         bool disable_radio;
1509
1510         mutex_lock(&sc->mutex);
1511
1512         /*
1513          * Leave this as the first check because we need to turn on the
1514          * radio if it was disabled before prior to processing the rest
1515          * of the changes. Likewise we must only disable the radio towards
1516          * the end.
1517          */
1518         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1519                 bool enable_radio;
1520                 bool all_wiphys_idle;
1521                 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1522
1523                 spin_lock_bh(&sc->wiphy_lock);
1524                 all_wiphys_idle =  ath9k_all_wiphys_idle(sc);
1525                 ath9k_set_wiphy_idle(aphy, idle);
1526
1527                 enable_radio = (!idle && all_wiphys_idle);
1528
1529                 /*
1530                  * After we unlock here its possible another wiphy
1531                  * can be re-renabled so to account for that we will
1532                  * only disable the radio toward the end of this routine
1533                  * if by then all wiphys are still idle.
1534                  */
1535                 spin_unlock_bh(&sc->wiphy_lock);
1536
1537                 if (enable_radio) {
1538                         sc->ps_idle = false;
1539                         ath_radio_enable(sc, hw);
1540                         ath_print(common, ATH_DBG_CONFIG,
1541                                   "not-idle: enabling radio\n");
1542                 }
1543         }
1544
1545         /*
1546          * We just prepare to enable PS. We have to wait until our AP has
1547          * ACK'd our null data frame to disable RX otherwise we'll ignore
1548          * those ACKs and end up retransmitting the same null data frames.
1549          * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1550          */
1551         if (changed & IEEE80211_CONF_CHANGE_PS) {
1552                 unsigned long flags;
1553                 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1554                 if (conf->flags & IEEE80211_CONF_PS) {
1555                         sc->ps_flags |= PS_ENABLED;
1556                         /*
1557                          * At this point we know hardware has received an ACK
1558                          * of a previously sent null data frame.
1559                          */
1560                         if ((sc->ps_flags & PS_NULLFUNC_COMPLETED)) {
1561                                 sc->ps_flags &= ~PS_NULLFUNC_COMPLETED;
1562                                 ath9k_enable_ps(sc);
1563                         }
1564                 } else {
1565                         sc->ps_enabled = false;
1566                         sc->ps_flags &= ~(PS_ENABLED |
1567                                           PS_NULLFUNC_COMPLETED);
1568                         ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
1569                         if (!(ah->caps.hw_caps &
1570                               ATH9K_HW_CAP_AUTOSLEEP)) {
1571                                 ath9k_hw_setrxabort(sc->sc_ah, 0);
1572                                 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1573                                                   PS_WAIT_FOR_CAB |
1574                                                   PS_WAIT_FOR_PSPOLL_DATA |
1575                                                   PS_WAIT_FOR_TX_ACK);
1576                                 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1577                                         ah->imask &= ~ATH9K_INT_TIM_TIMER;
1578                                         ath9k_hw_set_interrupts(sc->sc_ah,
1579                                                         ah->imask);
1580                                 }
1581                         }
1582                 }
1583                 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1584         }
1585
1586         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1587                 if (conf->flags & IEEE80211_CONF_MONITOR) {
1588                         ath_print(common, ATH_DBG_CONFIG,
1589                                   "HW opmode set to Monitor mode\n");
1590                         sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
1591                 }
1592         }
1593
1594         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1595                 struct ieee80211_channel *curchan = hw->conf.channel;
1596                 int pos = curchan->hw_value;
1597
1598                 aphy->chan_idx = pos;
1599                 aphy->chan_is_ht = conf_is_ht(conf);
1600                 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
1601                         sc->sc_flags |= SC_OP_OFFCHANNEL;
1602                 else
1603                         sc->sc_flags &= ~SC_OP_OFFCHANNEL;
1604
1605                 if (aphy->state == ATH_WIPHY_SCAN ||
1606                     aphy->state == ATH_WIPHY_ACTIVE)
1607                         ath9k_wiphy_pause_all_forced(sc, aphy);
1608                 else {
1609                         /*
1610                          * Do not change operational channel based on a paused
1611                          * wiphy changes.
1612                          */
1613                         goto skip_chan_change;
1614                 }
1615
1616                 ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1617                           curchan->center_freq);
1618
1619                 /* XXX: remove me eventualy */
1620                 ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
1621
1622                 ath_update_chainmask(sc, conf_is_ht(conf));
1623
1624                 if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
1625                         ath_print(common, ATH_DBG_FATAL,
1626                                   "Unable to set channel\n");
1627                         mutex_unlock(&sc->mutex);
1628                         return -EINVAL;
1629                 }
1630         }
1631
1632 skip_chan_change:
1633         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1634                 sc->config.txpowlimit = 2 * conf->power_level;
1635                 ath_update_txpow(sc);
1636         }
1637
1638         spin_lock_bh(&sc->wiphy_lock);
1639         disable_radio = ath9k_all_wiphys_idle(sc);
1640         spin_unlock_bh(&sc->wiphy_lock);
1641
1642         if (disable_radio) {
1643                 ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
1644                 sc->ps_idle = true;
1645                 ath_radio_disable(sc, hw);
1646         }
1647
1648         mutex_unlock(&sc->mutex);
1649
1650         return 0;
1651 }
1652
1653 #define SUPPORTED_FILTERS                       \
1654         (FIF_PROMISC_IN_BSS |                   \
1655         FIF_ALLMULTI |                          \
1656         FIF_CONTROL |                           \
1657         FIF_PSPOLL |                            \
1658         FIF_OTHER_BSS |                         \
1659         FIF_BCN_PRBRESP_PROMISC |               \
1660         FIF_FCSFAIL)
1661
1662 /* FIXME: sc->sc_full_reset ? */
1663 static void ath9k_configure_filter(struct ieee80211_hw *hw,
1664                                    unsigned int changed_flags,
1665                                    unsigned int *total_flags,
1666                                    u64 multicast)
1667 {
1668         struct ath_wiphy *aphy = hw->priv;
1669         struct ath_softc *sc = aphy->sc;
1670         u32 rfilt;
1671
1672         changed_flags &= SUPPORTED_FILTERS;
1673         *total_flags &= SUPPORTED_FILTERS;
1674
1675         sc->rx.rxfilter = *total_flags;
1676         ath9k_ps_wakeup(sc);
1677         rfilt = ath_calcrxfilter(sc);
1678         ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1679         ath9k_ps_restore(sc);
1680
1681         ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
1682                   "Set HW RX filter: 0x%x\n", rfilt);
1683 }
1684
1685 static int ath9k_sta_add(struct ieee80211_hw *hw,
1686                          struct ieee80211_vif *vif,
1687                          struct ieee80211_sta *sta)
1688 {
1689         struct ath_wiphy *aphy = hw->priv;
1690         struct ath_softc *sc = aphy->sc;
1691
1692         ath_node_attach(sc, sta);
1693
1694         return 0;
1695 }
1696
1697 static int ath9k_sta_remove(struct ieee80211_hw *hw,
1698                             struct ieee80211_vif *vif,
1699                             struct ieee80211_sta *sta)
1700 {
1701         struct ath_wiphy *aphy = hw->priv;
1702         struct ath_softc *sc = aphy->sc;
1703
1704         ath_node_detach(sc, sta);
1705
1706         return 0;
1707 }
1708
1709 static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
1710                          const struct ieee80211_tx_queue_params *params)
1711 {
1712         struct ath_wiphy *aphy = hw->priv;
1713         struct ath_softc *sc = aphy->sc;
1714         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1715         struct ath9k_tx_queue_info qi;
1716         int ret = 0, qnum;
1717
1718         if (queue >= WME_NUM_AC)
1719                 return 0;
1720
1721         mutex_lock(&sc->mutex);
1722
1723         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1724
1725         qi.tqi_aifs = params->aifs;
1726         qi.tqi_cwmin = params->cw_min;
1727         qi.tqi_cwmax = params->cw_max;
1728         qi.tqi_burstTime = params->txop;
1729         qnum = ath_get_hal_qnum(queue, sc);
1730
1731         ath_print(common, ATH_DBG_CONFIG,
1732                   "Configure tx [queue/halq] [%d/%d],  "
1733                   "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1734                   queue, qnum, params->aifs, params->cw_min,
1735                   params->cw_max, params->txop);
1736
1737         ret = ath_txq_update(sc, qnum, &qi);
1738         if (ret)
1739                 ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
1740
1741         if (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
1742                 if ((qnum == sc->tx.hwq_map[WME_AC_BE]) && !ret)
1743                         ath_beaconq_config(sc);
1744
1745         mutex_unlock(&sc->mutex);
1746
1747         return ret;
1748 }
1749
1750 static int ath9k_set_key(struct ieee80211_hw *hw,
1751                          enum set_key_cmd cmd,
1752                          struct ieee80211_vif *vif,
1753                          struct ieee80211_sta *sta,
1754                          struct ieee80211_key_conf *key)
1755 {
1756         struct ath_wiphy *aphy = hw->priv;
1757         struct ath_softc *sc = aphy->sc;
1758         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1759         int ret = 0;
1760
1761         if (modparam_nohwcrypt)
1762                 return -ENOSPC;
1763
1764         mutex_lock(&sc->mutex);
1765         ath9k_ps_wakeup(sc);
1766         ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
1767
1768         switch (cmd) {
1769         case SET_KEY:
1770                 ret = ath_key_config(common, vif, sta, key);
1771                 if (ret >= 0) {
1772                         key->hw_key_idx = ret;
1773                         /* push IV and Michael MIC generation to stack */
1774                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1775                         if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1776                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1777                         if (sc->sc_ah->sw_mgmt_crypto &&
1778                             key->cipher == WLAN_CIPHER_SUITE_CCMP)
1779                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1780                         ret = 0;
1781                 }
1782                 break;
1783         case DISABLE_KEY:
1784                 ath_key_delete(common, key);
1785                 break;
1786         default:
1787                 ret = -EINVAL;
1788         }
1789
1790         ath9k_ps_restore(sc);
1791         mutex_unlock(&sc->mutex);
1792
1793         return ret;
1794 }
1795
1796 static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1797                                    struct ieee80211_vif *vif,
1798                                    struct ieee80211_bss_conf *bss_conf,
1799                                    u32 changed)
1800 {
1801         struct ath_wiphy *aphy = hw->priv;
1802         struct ath_softc *sc = aphy->sc;
1803         struct ath_hw *ah = sc->sc_ah;
1804         struct ath_common *common = ath9k_hw_common(ah);
1805         struct ath_vif *avp = (void *)vif->drv_priv;
1806         int slottime;
1807         int error;
1808
1809         mutex_lock(&sc->mutex);
1810
1811         if (changed & BSS_CHANGED_BSSID) {
1812                 /* Set BSSID */
1813                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1814                 memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
1815                 common->curaid = 0;
1816                 ath9k_hw_write_associd(ah);
1817
1818                 /* Set aggregation protection mode parameters */
1819                 sc->config.ath_aggr_prot = 0;
1820
1821                 /* Only legacy IBSS for now */
1822                 if (vif->type == NL80211_IFTYPE_ADHOC)
1823                         ath_update_chainmask(sc, 0);
1824
1825                 ath_print(common, ATH_DBG_CONFIG,
1826                           "BSSID: %pM aid: 0x%x\n",
1827                           common->curbssid, common->curaid);
1828
1829                 /* need to reconfigure the beacon */
1830                 sc->sc_flags &= ~SC_OP_BEACONS ;
1831         }
1832
1833         /* Enable transmission of beacons (AP, IBSS, MESH) */
1834         if ((changed & BSS_CHANGED_BEACON) ||
1835             ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon)) {
1836                 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1837                 error = ath_beacon_alloc(aphy, vif);
1838                 if (!error)
1839                         ath_beacon_config(sc, vif);
1840         }
1841
1842         if (changed & BSS_CHANGED_ERP_SLOT) {
1843                 if (bss_conf->use_short_slot)
1844                         slottime = 9;
1845                 else
1846                         slottime = 20;
1847                 if (vif->type == NL80211_IFTYPE_AP) {
1848                         /*
1849                          * Defer update, so that connected stations can adjust
1850                          * their settings at the same time.
1851                          * See beacon.c for more details
1852                          */
1853                         sc->beacon.slottime = slottime;
1854                         sc->beacon.updateslot = UPDATE;
1855                 } else {
1856                         ah->slottime = slottime;
1857                         ath9k_hw_init_global_settings(ah);
1858                 }
1859         }
1860
1861         /* Disable transmission of beacons */
1862         if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon)
1863                 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1864
1865         if (changed & BSS_CHANGED_BEACON_INT) {
1866                 sc->beacon_interval = bss_conf->beacon_int;
1867                 /*
1868                  * In case of AP mode, the HW TSF has to be reset
1869                  * when the beacon interval changes.
1870                  */
1871                 if (vif->type == NL80211_IFTYPE_AP) {
1872                         sc->sc_flags |= SC_OP_TSF_RESET;
1873                         ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1874                         error = ath_beacon_alloc(aphy, vif);
1875                         if (!error)
1876                                 ath_beacon_config(sc, vif);
1877                 } else {
1878                         ath_beacon_config(sc, vif);
1879                 }
1880         }
1881
1882         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1883                 ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1884                           bss_conf->use_short_preamble);
1885                 if (bss_conf->use_short_preamble)
1886                         sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
1887                 else
1888                         sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
1889         }
1890
1891         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1892                 ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1893                           bss_conf->use_cts_prot);
1894                 if (bss_conf->use_cts_prot &&
1895                     hw->conf.channel->band != IEEE80211_BAND_5GHZ)
1896                         sc->sc_flags |= SC_OP_PROTECT_ENABLE;
1897                 else
1898                         sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
1899         }
1900
1901         if (changed & BSS_CHANGED_ASSOC) {
1902                 ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
1903                         bss_conf->assoc);
1904                 ath9k_bss_assoc_info(sc, vif, bss_conf);
1905         }
1906
1907         mutex_unlock(&sc->mutex);
1908 }
1909
1910 static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
1911 {
1912         u64 tsf;
1913         struct ath_wiphy *aphy = hw->priv;
1914         struct ath_softc *sc = aphy->sc;
1915
1916         mutex_lock(&sc->mutex);
1917         tsf = ath9k_hw_gettsf64(sc->sc_ah);
1918         mutex_unlock(&sc->mutex);
1919
1920         return tsf;
1921 }
1922
1923 static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1924 {
1925         struct ath_wiphy *aphy = hw->priv;
1926         struct ath_softc *sc = aphy->sc;
1927
1928         mutex_lock(&sc->mutex);
1929         ath9k_hw_settsf64(sc->sc_ah, tsf);
1930         mutex_unlock(&sc->mutex);
1931 }
1932
1933 static void ath9k_reset_tsf(struct ieee80211_hw *hw)
1934 {
1935         struct ath_wiphy *aphy = hw->priv;
1936         struct ath_softc *sc = aphy->sc;
1937
1938         mutex_lock(&sc->mutex);
1939
1940         ath9k_ps_wakeup(sc);
1941         ath9k_hw_reset_tsf(sc->sc_ah);
1942         ath9k_ps_restore(sc);
1943
1944         mutex_unlock(&sc->mutex);
1945 }
1946
1947 static int ath9k_ampdu_action(struct ieee80211_hw *hw,
1948                               struct ieee80211_vif *vif,
1949                               enum ieee80211_ampdu_mlme_action action,
1950                               struct ieee80211_sta *sta,
1951                               u16 tid, u16 *ssn)
1952 {
1953         struct ath_wiphy *aphy = hw->priv;
1954         struct ath_softc *sc = aphy->sc;
1955         int ret = 0;
1956
1957         local_bh_disable();
1958
1959         switch (action) {
1960         case IEEE80211_AMPDU_RX_START:
1961                 if (!(sc->sc_flags & SC_OP_RXAGGR))
1962                         ret = -ENOTSUPP;
1963                 break;
1964         case IEEE80211_AMPDU_RX_STOP:
1965                 break;
1966         case IEEE80211_AMPDU_TX_START:
1967                 ath9k_ps_wakeup(sc);
1968                 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
1969                 if (!ret)
1970                         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1971                 ath9k_ps_restore(sc);
1972                 break;
1973         case IEEE80211_AMPDU_TX_STOP:
1974                 ath9k_ps_wakeup(sc);
1975                 ath_tx_aggr_stop(sc, sta, tid);
1976                 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1977                 ath9k_ps_restore(sc);
1978                 break;
1979         case IEEE80211_AMPDU_TX_OPERATIONAL:
1980                 ath9k_ps_wakeup(sc);
1981                 ath_tx_aggr_resume(sc, sta, tid);
1982                 ath9k_ps_restore(sc);
1983                 break;
1984         default:
1985                 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
1986                           "Unknown AMPDU action\n");
1987         }
1988
1989         local_bh_enable();
1990
1991         return ret;
1992 }
1993
1994 static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
1995                              struct survey_info *survey)
1996 {
1997         struct ath_wiphy *aphy = hw->priv;
1998         struct ath_softc *sc = aphy->sc;
1999         struct ath_hw *ah = sc->sc_ah;
2000         struct ieee80211_supported_band *sband;
2001         struct ath9k_channel *chan;
2002
2003         sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
2004         if (sband && idx >= sband->n_channels) {
2005                 idx -= sband->n_channels;
2006                 sband = NULL;
2007         }
2008
2009         if (!sband)
2010                 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
2011
2012         if (!sband || idx >= sband->n_channels)
2013             return -ENOENT;
2014
2015         survey->channel = &sband->channels[idx];
2016         chan = &ah->channels[survey->channel->hw_value];
2017         survey->filled = 0;
2018
2019         if (chan == ah->curchan)
2020                 survey->filled |= SURVEY_INFO_IN_USE;
2021
2022         if (chan->noisefloor) {
2023                 survey->filled |= SURVEY_INFO_NOISE_DBM;
2024                 survey->noise = chan->noisefloor;
2025         }
2026
2027         return 0;
2028 }
2029
2030 static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2031 {
2032         struct ath_wiphy *aphy = hw->priv;
2033         struct ath_softc *sc = aphy->sc;
2034
2035         mutex_lock(&sc->mutex);
2036         if (ath9k_wiphy_scanning(sc)) {
2037                 /*
2038                  * There is a race here in mac80211 but fixing it requires
2039                  * we revisit how we handle the scan complete callback.
2040                  * After mac80211 fixes we will not have configured hardware
2041                  * to the home channel nor would we have configured the RX
2042                  * filter yet.
2043                  */
2044                 mutex_unlock(&sc->mutex);
2045                 return;
2046         }
2047
2048         aphy->state = ATH_WIPHY_SCAN;
2049         ath9k_wiphy_pause_all_forced(sc, aphy);
2050         mutex_unlock(&sc->mutex);
2051 }
2052
2053 /*
2054  * XXX: this requires a revisit after the driver
2055  * scan_complete gets moved to another place/removed in mac80211.
2056  */
2057 static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2058 {
2059         struct ath_wiphy *aphy = hw->priv;
2060         struct ath_softc *sc = aphy->sc;
2061
2062         mutex_lock(&sc->mutex);
2063         aphy->state = ATH_WIPHY_ACTIVE;
2064         mutex_unlock(&sc->mutex);
2065 }
2066
2067 static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
2068 {
2069         struct ath_wiphy *aphy = hw->priv;
2070         struct ath_softc *sc = aphy->sc;
2071         struct ath_hw *ah = sc->sc_ah;
2072
2073         mutex_lock(&sc->mutex);
2074         ah->coverage_class = coverage_class;
2075         ath9k_hw_init_global_settings(ah);
2076         mutex_unlock(&sc->mutex);
2077 }
2078
2079 struct ieee80211_ops ath9k_ops = {
2080         .tx                 = ath9k_tx,
2081         .start              = ath9k_start,
2082         .stop               = ath9k_stop,
2083         .add_interface      = ath9k_add_interface,
2084         .remove_interface   = ath9k_remove_interface,
2085         .config             = ath9k_config,
2086         .configure_filter   = ath9k_configure_filter,
2087         .sta_add            = ath9k_sta_add,
2088         .sta_remove         = ath9k_sta_remove,
2089         .conf_tx            = ath9k_conf_tx,
2090         .bss_info_changed   = ath9k_bss_info_changed,
2091         .set_key            = ath9k_set_key,
2092         .get_tsf            = ath9k_get_tsf,
2093         .set_tsf            = ath9k_set_tsf,
2094         .reset_tsf          = ath9k_reset_tsf,
2095         .ampdu_action       = ath9k_ampdu_action,
2096         .get_survey         = ath9k_get_survey,
2097         .sw_scan_start      = ath9k_sw_scan_start,
2098         .sw_scan_complete   = ath9k_sw_scan_complete,
2099         .rfkill_poll        = ath9k_rfkill_poll_state,
2100         .set_coverage_class = ath9k_set_coverage_class,
2101 };