Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / main.c
1 /*
2  * Copyright (c) 2008-2011 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 <linux/delay.h>
19 #include "ath9k.h"
20 #include "btcoex.h"
21
22 static void ath9k_set_assoc_state(struct ath_softc *sc,
23                                   struct ieee80211_vif *vif);
24
25 u8 ath9k_parse_mpdudensity(u8 mpdudensity)
26 {
27         /*
28          * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
29          *   0 for no restriction
30          *   1 for 1/4 us
31          *   2 for 1/2 us
32          *   3 for 1 us
33          *   4 for 2 us
34          *   5 for 4 us
35          *   6 for 8 us
36          *   7 for 16 us
37          */
38         switch (mpdudensity) {
39         case 0:
40                 return 0;
41         case 1:
42         case 2:
43         case 3:
44                 /* Our lower layer calculations limit our precision to
45                    1 microsecond */
46                 return 1;
47         case 4:
48                 return 2;
49         case 5:
50                 return 4;
51         case 6:
52                 return 8;
53         case 7:
54                 return 16;
55         default:
56                 return 0;
57         }
58 }
59
60 static bool ath9k_has_pending_frames(struct ath_softc *sc, struct ath_txq *txq)
61 {
62         bool pending = false;
63
64         spin_lock_bh(&txq->axq_lock);
65
66         if (txq->axq_depth || !list_empty(&txq->axq_acq))
67                 pending = true;
68
69         spin_unlock_bh(&txq->axq_lock);
70         return pending;
71 }
72
73 static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
74 {
75         unsigned long flags;
76         bool ret;
77
78         spin_lock_irqsave(&sc->sc_pm_lock, flags);
79         ret = ath9k_hw_setpower(sc->sc_ah, mode);
80         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
81
82         return ret;
83 }
84
85 void ath9k_ps_wakeup(struct ath_softc *sc)
86 {
87         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
88         unsigned long flags;
89         enum ath9k_power_mode power_mode;
90
91         spin_lock_irqsave(&sc->sc_pm_lock, flags);
92         if (++sc->ps_usecount != 1)
93                 goto unlock;
94
95         power_mode = sc->sc_ah->power_mode;
96         ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
97
98         /*
99          * While the hardware is asleep, the cycle counters contain no
100          * useful data. Better clear them now so that they don't mess up
101          * survey data results.
102          */
103         if (power_mode != ATH9K_PM_AWAKE) {
104                 spin_lock(&common->cc_lock);
105                 ath_hw_cycle_counters_update(common);
106                 memset(&common->cc_survey, 0, sizeof(common->cc_survey));
107                 memset(&common->cc_ani, 0, sizeof(common->cc_ani));
108                 spin_unlock(&common->cc_lock);
109         }
110
111  unlock:
112         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
113 }
114
115 void ath9k_ps_restore(struct ath_softc *sc)
116 {
117         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
118         enum ath9k_power_mode mode;
119         unsigned long flags;
120         bool reset;
121
122         spin_lock_irqsave(&sc->sc_pm_lock, flags);
123         if (--sc->ps_usecount != 0)
124                 goto unlock;
125
126         if (sc->ps_idle) {
127                 ath9k_hw_setrxabort(sc->sc_ah, 1);
128                 ath9k_hw_stopdmarecv(sc->sc_ah, &reset);
129                 mode = ATH9K_PM_FULL_SLEEP;
130         } else if (sc->ps_enabled &&
131                    !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
132                                      PS_WAIT_FOR_CAB |
133                                      PS_WAIT_FOR_PSPOLL_DATA |
134                                      PS_WAIT_FOR_TX_ACK |
135                                      PS_WAIT_FOR_ANI))) {
136                 mode = ATH9K_PM_NETWORK_SLEEP;
137                 if (ath9k_hw_btcoex_is_enabled(sc->sc_ah))
138                         ath9k_btcoex_stop_gen_timer(sc);
139         } else {
140                 goto unlock;
141         }
142
143         spin_lock(&common->cc_lock);
144         ath_hw_cycle_counters_update(common);
145         spin_unlock(&common->cc_lock);
146
147         ath9k_hw_setpower(sc->sc_ah, mode);
148
149  unlock:
150         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
151 }
152
153 static void __ath_cancel_work(struct ath_softc *sc)
154 {
155         cancel_work_sync(&sc->paprd_work);
156         cancel_work_sync(&sc->hw_check_work);
157         cancel_delayed_work_sync(&sc->tx_complete_work);
158         cancel_delayed_work_sync(&sc->hw_pll_work);
159
160 #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
161         if (ath9k_hw_mci_is_enabled(sc->sc_ah))
162                 cancel_work_sync(&sc->mci_work);
163 #endif
164 }
165
166 static void ath_cancel_work(struct ath_softc *sc)
167 {
168         __ath_cancel_work(sc);
169         cancel_work_sync(&sc->hw_reset_work);
170 }
171
172 static void ath_restart_work(struct ath_softc *sc)
173 {
174         ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
175
176         if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
177                 ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
178                                      msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
179
180         ath_start_rx_poll(sc, 3);
181         ath_start_ani(sc);
182 }
183
184 static bool ath_prepare_reset(struct ath_softc *sc)
185 {
186         struct ath_hw *ah = sc->sc_ah;
187         bool ret = true;
188
189         ieee80211_stop_queues(sc->hw);
190
191         sc->hw_busy_count = 0;
192         ath_stop_ani(sc);
193         del_timer_sync(&sc->rx_poll_timer);
194
195         ath9k_debug_samp_bb_mac(sc);
196         ath9k_hw_disable_interrupts(ah);
197
198         if (!ath_drain_all_txq(sc))
199                 ret = false;
200
201         if (!ath_stoprecv(sc))
202                 ret = false;
203
204         return ret;
205 }
206
207 static bool ath_complete_reset(struct ath_softc *sc, bool start)
208 {
209         struct ath_hw *ah = sc->sc_ah;
210         struct ath_common *common = ath9k_hw_common(ah);
211         unsigned long flags;
212         int i;
213
214         if (ath_startrecv(sc) != 0) {
215                 ath_err(common, "Unable to restart recv logic\n");
216                 return false;
217         }
218
219         ath9k_cmn_update_txpow(ah, sc->curtxpow,
220                                sc->config.txpowlimit, &sc->curtxpow);
221
222         clear_bit(SC_OP_HW_RESET, &sc->sc_flags);
223         ath9k_hw_set_interrupts(ah);
224         ath9k_hw_enable_interrupts(ah);
225
226         if (!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) && start) {
227                 if (!test_bit(SC_OP_BEACONS, &sc->sc_flags))
228                         goto work;
229
230                 if (ah->opmode == NL80211_IFTYPE_STATION &&
231                     test_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags)) {
232                         spin_lock_irqsave(&sc->sc_pm_lock, flags);
233                         sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
234                         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
235                 } else {
236                         ath9k_set_beacon(sc);
237                 }
238         work:
239                 ath_restart_work(sc);
240
241                 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
242                         if (!ATH_TXQ_SETUP(sc, i))
243                                 continue;
244
245                         spin_lock_bh(&sc->tx.txq[i].axq_lock);
246                         ath_txq_schedule(sc, &sc->tx.txq[i]);
247                         spin_unlock_bh(&sc->tx.txq[i].axq_lock);
248                 }
249         }
250
251         if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx != 3)
252                 ath_ant_comb_update(sc);
253
254         ieee80211_wake_queues(sc->hw);
255
256         return true;
257 }
258
259 static int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
260 {
261         struct ath_hw *ah = sc->sc_ah;
262         struct ath_common *common = ath9k_hw_common(ah);
263         struct ath9k_hw_cal_data *caldata = NULL;
264         bool fastcc = true;
265         int r;
266
267         __ath_cancel_work(sc);
268
269         tasklet_disable(&sc->intr_tq);
270         spin_lock_bh(&sc->sc_pcu_lock);
271
272         if (!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
273                 fastcc = false;
274                 caldata = &sc->caldata;
275         }
276
277         if (!hchan) {
278                 fastcc = false;
279                 hchan = ah->curchan;
280         }
281
282         if (!ath_prepare_reset(sc))
283                 fastcc = false;
284
285         ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
286                 hchan->channel, IS_CHAN_HT40(hchan), fastcc);
287
288         r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
289         if (r) {
290                 ath_err(common,
291                         "Unable to reset channel, reset status %d\n", r);
292
293                 ath9k_hw_enable_interrupts(ah);
294                 ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
295
296                 goto out;
297         }
298
299         if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
300             (sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
301                 ath9k_mci_set_txpower(sc, true, false);
302
303         if (!ath_complete_reset(sc, true))
304                 r = -EIO;
305
306 out:
307         spin_unlock_bh(&sc->sc_pcu_lock);
308         tasklet_enable(&sc->intr_tq);
309
310         return r;
311 }
312
313
314 /*
315  * Set/change channels.  If the channel is really being changed, it's done
316  * by reseting the chip.  To accomplish this we must first cleanup any pending
317  * DMA, then restart stuff.
318 */
319 static int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
320                     struct ath9k_channel *hchan)
321 {
322         int r;
323
324         if (test_bit(SC_OP_INVALID, &sc->sc_flags))
325                 return -EIO;
326
327         r = ath_reset_internal(sc, hchan);
328
329         return r;
330 }
331
332 static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
333                             struct ieee80211_vif *vif)
334 {
335         struct ath_node *an;
336         an = (struct ath_node *)sta->drv_priv;
337
338         an->sc = sc;
339         an->sta = sta;
340         an->vif = vif;
341
342         ath_tx_node_init(sc, an);
343
344         if (sta->ht_cap.ht_supported) {
345                 an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
346                                      sta->ht_cap.ampdu_factor);
347                 an->mpdudensity = ath9k_parse_mpdudensity(sta->ht_cap.ampdu_density);
348         }
349 }
350
351 static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
352 {
353         struct ath_node *an = (struct ath_node *)sta->drv_priv;
354         ath_tx_node_cleanup(sc, an);
355 }
356
357 void ath9k_tasklet(unsigned long data)
358 {
359         struct ath_softc *sc = (struct ath_softc *)data;
360         struct ath_hw *ah = sc->sc_ah;
361         struct ath_common *common = ath9k_hw_common(ah);
362         enum ath_reset_type type;
363         unsigned long flags;
364         u32 status = sc->intrstatus;
365         u32 rxmask;
366
367         ath9k_ps_wakeup(sc);
368         spin_lock(&sc->sc_pcu_lock);
369
370         if ((status & ATH9K_INT_FATAL) ||
371             (status & ATH9K_INT_BB_WATCHDOG)) {
372
373                 if (status & ATH9K_INT_FATAL)
374                         type = RESET_TYPE_FATAL_INT;
375                 else
376                         type = RESET_TYPE_BB_WATCHDOG;
377
378                 ath9k_queue_reset(sc, type);
379                 goto out;
380         }
381
382         spin_lock_irqsave(&sc->sc_pm_lock, flags);
383         if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
384                 /*
385                  * TSF sync does not look correct; remain awake to sync with
386                  * the next Beacon.
387                  */
388                 ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
389                 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
390         }
391         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
392
393         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
394                 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
395                           ATH9K_INT_RXORN);
396         else
397                 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
398
399         if (status & rxmask) {
400                 /* Check for high priority Rx first */
401                 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
402                     (status & ATH9K_INT_RXHP))
403                         ath_rx_tasklet(sc, 0, true);
404
405                 ath_rx_tasklet(sc, 0, false);
406         }
407
408         if (status & ATH9K_INT_TX) {
409                 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
410                         ath_tx_edma_tasklet(sc);
411                 else
412                         ath_tx_tasklet(sc);
413         }
414
415         ath9k_btcoex_handle_interrupt(sc, status);
416
417 out:
418         /* re-enable hardware interrupt */
419         ath9k_hw_enable_interrupts(ah);
420
421         spin_unlock(&sc->sc_pcu_lock);
422         ath9k_ps_restore(sc);
423 }
424
425 irqreturn_t ath_isr(int irq, void *dev)
426 {
427 #define SCHED_INTR (                            \
428                 ATH9K_INT_FATAL |               \
429                 ATH9K_INT_BB_WATCHDOG |         \
430                 ATH9K_INT_RXORN |               \
431                 ATH9K_INT_RXEOL |               \
432                 ATH9K_INT_RX |                  \
433                 ATH9K_INT_RXLP |                \
434                 ATH9K_INT_RXHP |                \
435                 ATH9K_INT_TX |                  \
436                 ATH9K_INT_BMISS |               \
437                 ATH9K_INT_CST |                 \
438                 ATH9K_INT_TSFOOR |              \
439                 ATH9K_INT_GENTIMER |            \
440                 ATH9K_INT_MCI)
441
442         struct ath_softc *sc = dev;
443         struct ath_hw *ah = sc->sc_ah;
444         struct ath_common *common = ath9k_hw_common(ah);
445         enum ath9k_int status;
446         bool sched = false;
447
448         /*
449          * The hardware is not ready/present, don't
450          * touch anything. Note this can happen early
451          * on if the IRQ is shared.
452          */
453         if (test_bit(SC_OP_INVALID, &sc->sc_flags))
454                 return IRQ_NONE;
455
456         /* shared irq, not for us */
457
458         if (!ath9k_hw_intrpend(ah))
459                 return IRQ_NONE;
460
461         if (test_bit(SC_OP_HW_RESET, &sc->sc_flags)) {
462                 ath9k_hw_kill_interrupts(ah);
463                 return IRQ_HANDLED;
464         }
465
466         /*
467          * Figure out the reason(s) for the interrupt.  Note
468          * that the hal returns a pseudo-ISR that may include
469          * bits we haven't explicitly enabled so we mask the
470          * value to insure we only process bits we requested.
471          */
472         ath9k_hw_getisr(ah, &status);   /* NB: clears ISR too */
473         status &= ah->imask;    /* discard unasked-for bits */
474
475         /*
476          * If there are no status bits set, then this interrupt was not
477          * for me (should have been caught above).
478          */
479         if (!status)
480                 return IRQ_NONE;
481
482         /* Cache the status */
483         sc->intrstatus = status;
484
485         if (status & SCHED_INTR)
486                 sched = true;
487
488         /*
489          * If a FATAL or RXORN interrupt is received, we have to reset the
490          * chip immediately.
491          */
492         if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
493             !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
494                 goto chip_reset;
495
496         if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
497             (status & ATH9K_INT_BB_WATCHDOG)) {
498
499                 spin_lock(&common->cc_lock);
500                 ath_hw_cycle_counters_update(common);
501                 ar9003_hw_bb_watchdog_dbg_info(ah);
502                 spin_unlock(&common->cc_lock);
503
504                 goto chip_reset;
505         }
506 #ifdef CONFIG_PM_SLEEP
507         if (status & ATH9K_INT_BMISS) {
508                 if (atomic_read(&sc->wow_sleep_proc_intr) == 0) {
509                         ath_dbg(common, ANY, "during WoW we got a BMISS\n");
510                         atomic_inc(&sc->wow_got_bmiss_intr);
511                         atomic_dec(&sc->wow_sleep_proc_intr);
512                 }
513         }
514 #endif
515         if (status & ATH9K_INT_SWBA)
516                 tasklet_schedule(&sc->bcon_tasklet);
517
518         if (status & ATH9K_INT_TXURN)
519                 ath9k_hw_updatetxtriglevel(ah, true);
520
521         if (status & ATH9K_INT_RXEOL) {
522                 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
523                 ath9k_hw_set_interrupts(ah);
524         }
525
526         if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
527                 if (status & ATH9K_INT_TIM_TIMER) {
528                         if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
529                                 goto chip_reset;
530                         /* Clear RxAbort bit so that we can
531                          * receive frames */
532                         ath9k_setpower(sc, ATH9K_PM_AWAKE);
533                         spin_lock(&sc->sc_pm_lock);
534                         ath9k_hw_setrxabort(sc->sc_ah, 0);
535                         sc->ps_flags |= PS_WAIT_FOR_BEACON;
536                         spin_unlock(&sc->sc_pm_lock);
537                 }
538
539 chip_reset:
540
541         ath_debug_stat_interrupt(sc, status);
542
543         if (sched) {
544                 /* turn off every interrupt */
545                 ath9k_hw_disable_interrupts(ah);
546                 tasklet_schedule(&sc->intr_tq);
547         }
548
549         return IRQ_HANDLED;
550
551 #undef SCHED_INTR
552 }
553
554 static int ath_reset(struct ath_softc *sc)
555 {
556         int r;
557
558         ath9k_ps_wakeup(sc);
559         r = ath_reset_internal(sc, NULL);
560         ath9k_ps_restore(sc);
561
562         return r;
563 }
564
565 void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
566 {
567 #ifdef CONFIG_ATH9K_DEBUGFS
568         RESET_STAT_INC(sc, type);
569 #endif
570         set_bit(SC_OP_HW_RESET, &sc->sc_flags);
571         ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
572 }
573
574 void ath_reset_work(struct work_struct *work)
575 {
576         struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
577
578         ath_reset(sc);
579 }
580
581 /**********************/
582 /* mac80211 callbacks */
583 /**********************/
584
585 static int ath9k_start(struct ieee80211_hw *hw)
586 {
587         struct ath_softc *sc = hw->priv;
588         struct ath_hw *ah = sc->sc_ah;
589         struct ath_common *common = ath9k_hw_common(ah);
590         struct ieee80211_channel *curchan = hw->conf.chandef.chan;
591         struct ath9k_channel *init_channel;
592         int r;
593
594         ath_dbg(common, CONFIG,
595                 "Starting driver with initial channel: %d MHz\n",
596                 curchan->center_freq);
597
598         ath9k_ps_wakeup(sc);
599         mutex_lock(&sc->mutex);
600
601         init_channel = ath9k_cmn_get_curchannel(hw, ah);
602
603         /* Reset SERDES registers */
604         ath9k_hw_configpcipowersave(ah, false);
605
606         /*
607          * The basic interface to setting the hardware in a good
608          * state is ``reset''.  On return the hardware is known to
609          * be powered up and with interrupts disabled.  This must
610          * be followed by initialization of the appropriate bits
611          * and then setup of the interrupt mask.
612          */
613         spin_lock_bh(&sc->sc_pcu_lock);
614
615         atomic_set(&ah->intr_ref_cnt, -1);
616
617         r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
618         if (r) {
619                 ath_err(common,
620                         "Unable to reset hardware; reset status %d (freq %u MHz)\n",
621                         r, curchan->center_freq);
622                 ah->reset_power_on = false;
623         }
624
625         /* Setup our intr mask. */
626         ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
627                     ATH9K_INT_RXORN | ATH9K_INT_FATAL |
628                     ATH9K_INT_GLOBAL;
629
630         if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
631                 ah->imask |= ATH9K_INT_RXHP |
632                              ATH9K_INT_RXLP |
633                              ATH9K_INT_BB_WATCHDOG;
634         else
635                 ah->imask |= ATH9K_INT_RX;
636
637         ah->imask |= ATH9K_INT_GTT;
638
639         if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
640                 ah->imask |= ATH9K_INT_CST;
641
642         ath_mci_enable(sc);
643
644         clear_bit(SC_OP_INVALID, &sc->sc_flags);
645         sc->sc_ah->is_monitoring = false;
646
647         if (!ath_complete_reset(sc, false))
648                 ah->reset_power_on = false;
649
650         if (ah->led_pin >= 0) {
651                 ath9k_hw_cfg_output(ah, ah->led_pin,
652                                     AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
653                 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
654         }
655
656         /*
657          * Reset key cache to sane defaults (all entries cleared) instead of
658          * semi-random values after suspend/resume.
659          */
660         ath9k_cmn_init_crypto(sc->sc_ah);
661
662         spin_unlock_bh(&sc->sc_pcu_lock);
663
664         mutex_unlock(&sc->mutex);
665
666         ath9k_ps_restore(sc);
667
668         return 0;
669 }
670
671 static void ath9k_tx(struct ieee80211_hw *hw,
672                      struct ieee80211_tx_control *control,
673                      struct sk_buff *skb)
674 {
675         struct ath_softc *sc = hw->priv;
676         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
677         struct ath_tx_control txctl;
678         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
679         unsigned long flags;
680
681         if (sc->ps_enabled) {
682                 /*
683                  * mac80211 does not set PM field for normal data frames, so we
684                  * need to update that based on the current PS mode.
685                  */
686                 if (ieee80211_is_data(hdr->frame_control) &&
687                     !ieee80211_is_nullfunc(hdr->frame_control) &&
688                     !ieee80211_has_pm(hdr->frame_control)) {
689                         ath_dbg(common, PS,
690                                 "Add PM=1 for a TX frame while in PS mode\n");
691                         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
692                 }
693         }
694
695         if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
696                 /*
697                  * We are using PS-Poll and mac80211 can request TX while in
698                  * power save mode. Need to wake up hardware for the TX to be
699                  * completed and if needed, also for RX of buffered frames.
700                  */
701                 ath9k_ps_wakeup(sc);
702                 spin_lock_irqsave(&sc->sc_pm_lock, flags);
703                 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
704                         ath9k_hw_setrxabort(sc->sc_ah, 0);
705                 if (ieee80211_is_pspoll(hdr->frame_control)) {
706                         ath_dbg(common, PS,
707                                 "Sending PS-Poll to pick a buffered frame\n");
708                         sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
709                 } else {
710                         ath_dbg(common, PS, "Wake up to complete TX\n");
711                         sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
712                 }
713                 /*
714                  * The actual restore operation will happen only after
715                  * the ps_flags bit is cleared. We are just dropping
716                  * the ps_usecount here.
717                  */
718                 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
719                 ath9k_ps_restore(sc);
720         }
721
722         /*
723          * Cannot tx while the hardware is in full sleep, it first needs a full
724          * chip reset to recover from that
725          */
726         if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
727                 ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
728                 goto exit;
729         }
730
731         memset(&txctl, 0, sizeof(struct ath_tx_control));
732         txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
733         txctl.sta = control->sta;
734
735         ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
736
737         if (ath_tx_start(hw, skb, &txctl) != 0) {
738                 ath_dbg(common, XMIT, "TX failed\n");
739                 TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
740                 goto exit;
741         }
742
743         return;
744 exit:
745         ieee80211_free_txskb(hw, skb);
746 }
747
748 static void ath9k_stop(struct ieee80211_hw *hw)
749 {
750         struct ath_softc *sc = hw->priv;
751         struct ath_hw *ah = sc->sc_ah;
752         struct ath_common *common = ath9k_hw_common(ah);
753         bool prev_idle;
754
755         mutex_lock(&sc->mutex);
756
757         ath_cancel_work(sc);
758         del_timer_sync(&sc->rx_poll_timer);
759
760         if (test_bit(SC_OP_INVALID, &sc->sc_flags)) {
761                 ath_dbg(common, ANY, "Device not present\n");
762                 mutex_unlock(&sc->mutex);
763                 return;
764         }
765
766         /* Ensure HW is awake when we try to shut it down. */
767         ath9k_ps_wakeup(sc);
768
769         spin_lock_bh(&sc->sc_pcu_lock);
770
771         /* prevent tasklets to enable interrupts once we disable them */
772         ah->imask &= ~ATH9K_INT_GLOBAL;
773
774         /* make sure h/w will not generate any interrupt
775          * before setting the invalid flag. */
776         ath9k_hw_disable_interrupts(ah);
777
778         spin_unlock_bh(&sc->sc_pcu_lock);
779
780         /* we can now sync irq and kill any running tasklets, since we already
781          * disabled interrupts and not holding a spin lock */
782         synchronize_irq(sc->irq);
783         tasklet_kill(&sc->intr_tq);
784         tasklet_kill(&sc->bcon_tasklet);
785
786         prev_idle = sc->ps_idle;
787         sc->ps_idle = true;
788
789         spin_lock_bh(&sc->sc_pcu_lock);
790
791         if (ah->led_pin >= 0) {
792                 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
793                 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
794         }
795
796         ath_prepare_reset(sc);
797
798         if (sc->rx.frag) {
799                 dev_kfree_skb_any(sc->rx.frag);
800                 sc->rx.frag = NULL;
801         }
802
803         if (!ah->curchan)
804                 ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
805
806         ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
807         ath9k_hw_phy_disable(ah);
808
809         ath9k_hw_configpcipowersave(ah, true);
810
811         spin_unlock_bh(&sc->sc_pcu_lock);
812
813         ath9k_ps_restore(sc);
814
815         set_bit(SC_OP_INVALID, &sc->sc_flags);
816         sc->ps_idle = prev_idle;
817
818         mutex_unlock(&sc->mutex);
819
820         ath_dbg(common, CONFIG, "Driver halt\n");
821 }
822
823 bool ath9k_uses_beacons(int type)
824 {
825         switch (type) {
826         case NL80211_IFTYPE_AP:
827         case NL80211_IFTYPE_ADHOC:
828         case NL80211_IFTYPE_MESH_POINT:
829                 return true;
830         default:
831                 return false;
832         }
833 }
834
835 static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
836 {
837         struct ath9k_vif_iter_data *iter_data = data;
838         int i;
839
840         if (iter_data->has_hw_macaddr) {
841                 for (i = 0; i < ETH_ALEN; i++)
842                         iter_data->mask[i] &=
843                                 ~(iter_data->hw_macaddr[i] ^ mac[i]);
844         } else {
845                 memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
846                 iter_data->has_hw_macaddr = true;
847         }
848
849         switch (vif->type) {
850         case NL80211_IFTYPE_AP:
851                 iter_data->naps++;
852                 break;
853         case NL80211_IFTYPE_STATION:
854                 iter_data->nstations++;
855                 break;
856         case NL80211_IFTYPE_ADHOC:
857                 iter_data->nadhocs++;
858                 break;
859         case NL80211_IFTYPE_MESH_POINT:
860                 iter_data->nmeshes++;
861                 break;
862         case NL80211_IFTYPE_WDS:
863                 iter_data->nwds++;
864                 break;
865         default:
866                 break;
867         }
868 }
869
870 static void ath9k_sta_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
871 {
872         struct ath_softc *sc = data;
873         struct ath_vif *avp = (void *)vif->drv_priv;
874
875         if (vif->type != NL80211_IFTYPE_STATION)
876                 return;
877
878         if (avp->primary_sta_vif)
879                 ath9k_set_assoc_state(sc, vif);
880 }
881
882 /* Called with sc->mutex held. */
883 void ath9k_calculate_iter_data(struct ieee80211_hw *hw,
884                                struct ieee80211_vif *vif,
885                                struct ath9k_vif_iter_data *iter_data)
886 {
887         struct ath_softc *sc = hw->priv;
888         struct ath_hw *ah = sc->sc_ah;
889         struct ath_common *common = ath9k_hw_common(ah);
890
891         /*
892          * Pick the MAC address of the first interface as the new hardware
893          * MAC address. The hardware will use it together with the BSSID mask
894          * when matching addresses.
895          */
896         memset(iter_data, 0, sizeof(*iter_data));
897         memset(&iter_data->mask, 0xff, ETH_ALEN);
898
899         if (vif)
900                 ath9k_vif_iter(iter_data, vif->addr, vif);
901
902         /* Get list of all active MAC addresses */
903         ieee80211_iterate_active_interfaces_atomic(
904                 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
905                 ath9k_vif_iter, iter_data);
906
907         memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
908 }
909
910 /* Called with sc->mutex held. */
911 static void ath9k_calculate_summary_state(struct ieee80211_hw *hw,
912                                           struct ieee80211_vif *vif)
913 {
914         struct ath_softc *sc = hw->priv;
915         struct ath_hw *ah = sc->sc_ah;
916         struct ath_common *common = ath9k_hw_common(ah);
917         struct ath9k_vif_iter_data iter_data;
918         enum nl80211_iftype old_opmode = ah->opmode;
919
920         ath9k_calculate_iter_data(hw, vif, &iter_data);
921
922         memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
923         ath_hw_setbssidmask(common);
924
925         if (iter_data.naps > 0) {
926                 ath9k_hw_set_tsfadjust(ah, true);
927                 ah->opmode = NL80211_IFTYPE_AP;
928         } else {
929                 ath9k_hw_set_tsfadjust(ah, false);
930
931                 if (iter_data.nmeshes)
932                         ah->opmode = NL80211_IFTYPE_MESH_POINT;
933                 else if (iter_data.nwds)
934                         ah->opmode = NL80211_IFTYPE_AP;
935                 else if (iter_data.nadhocs)
936                         ah->opmode = NL80211_IFTYPE_ADHOC;
937                 else
938                         ah->opmode = NL80211_IFTYPE_STATION;
939         }
940
941         ath9k_hw_setopmode(ah);
942
943         if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
944                 ah->imask |= ATH9K_INT_TSFOOR;
945         else
946                 ah->imask &= ~ATH9K_INT_TSFOOR;
947
948         ath9k_hw_set_interrupts(ah);
949
950         /*
951          * If we are changing the opmode to STATION,
952          * a beacon sync needs to be done.
953          */
954         if (ah->opmode == NL80211_IFTYPE_STATION &&
955             old_opmode == NL80211_IFTYPE_AP &&
956             test_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags)) {
957                 ieee80211_iterate_active_interfaces_atomic(
958                         sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
959                         ath9k_sta_vif_iter, sc);
960         }
961 }
962
963 static int ath9k_add_interface(struct ieee80211_hw *hw,
964                                struct ieee80211_vif *vif)
965 {
966         struct ath_softc *sc = hw->priv;
967         struct ath_hw *ah = sc->sc_ah;
968         struct ath_common *common = ath9k_hw_common(ah);
969
970         mutex_lock(&sc->mutex);
971
972         ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
973         sc->nvifs++;
974
975         ath9k_ps_wakeup(sc);
976         ath9k_calculate_summary_state(hw, vif);
977         ath9k_ps_restore(sc);
978
979         if (ath9k_uses_beacons(vif->type))
980                 ath9k_beacon_assign_slot(sc, vif);
981
982         mutex_unlock(&sc->mutex);
983         return 0;
984 }
985
986 static int ath9k_change_interface(struct ieee80211_hw *hw,
987                                   struct ieee80211_vif *vif,
988                                   enum nl80211_iftype new_type,
989                                   bool p2p)
990 {
991         struct ath_softc *sc = hw->priv;
992         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
993
994         ath_dbg(common, CONFIG, "Change Interface\n");
995         mutex_lock(&sc->mutex);
996
997         if (ath9k_uses_beacons(vif->type))
998                 ath9k_beacon_remove_slot(sc, vif);
999
1000         vif->type = new_type;
1001         vif->p2p = p2p;
1002
1003         ath9k_ps_wakeup(sc);
1004         ath9k_calculate_summary_state(hw, vif);
1005         ath9k_ps_restore(sc);
1006
1007         if (ath9k_uses_beacons(vif->type))
1008                 ath9k_beacon_assign_slot(sc, vif);
1009
1010         mutex_unlock(&sc->mutex);
1011         return 0;
1012 }
1013
1014 static void ath9k_remove_interface(struct ieee80211_hw *hw,
1015                                    struct ieee80211_vif *vif)
1016 {
1017         struct ath_softc *sc = hw->priv;
1018         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1019
1020         ath_dbg(common, CONFIG, "Detach Interface\n");
1021
1022         mutex_lock(&sc->mutex);
1023
1024         sc->nvifs--;
1025
1026         if (ath9k_uses_beacons(vif->type))
1027                 ath9k_beacon_remove_slot(sc, vif);
1028
1029         ath9k_ps_wakeup(sc);
1030         ath9k_calculate_summary_state(hw, NULL);
1031         ath9k_ps_restore(sc);
1032
1033         mutex_unlock(&sc->mutex);
1034 }
1035
1036 static void ath9k_enable_ps(struct ath_softc *sc)
1037 {
1038         struct ath_hw *ah = sc->sc_ah;
1039         struct ath_common *common = ath9k_hw_common(ah);
1040
1041         sc->ps_enabled = true;
1042         if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1043                 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1044                         ah->imask |= ATH9K_INT_TIM_TIMER;
1045                         ath9k_hw_set_interrupts(ah);
1046                 }
1047                 ath9k_hw_setrxabort(ah, 1);
1048         }
1049         ath_dbg(common, PS, "PowerSave enabled\n");
1050 }
1051
1052 static void ath9k_disable_ps(struct ath_softc *sc)
1053 {
1054         struct ath_hw *ah = sc->sc_ah;
1055         struct ath_common *common = ath9k_hw_common(ah);
1056
1057         sc->ps_enabled = false;
1058         ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
1059         if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1060                 ath9k_hw_setrxabort(ah, 0);
1061                 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1062                                   PS_WAIT_FOR_CAB |
1063                                   PS_WAIT_FOR_PSPOLL_DATA |
1064                                   PS_WAIT_FOR_TX_ACK);
1065                 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1066                         ah->imask &= ~ATH9K_INT_TIM_TIMER;
1067                         ath9k_hw_set_interrupts(ah);
1068                 }
1069         }
1070         ath_dbg(common, PS, "PowerSave disabled\n");
1071 }
1072
1073 void ath9k_spectral_scan_trigger(struct ieee80211_hw *hw)
1074 {
1075         struct ath_softc *sc = hw->priv;
1076         struct ath_hw *ah = sc->sc_ah;
1077         struct ath_common *common = ath9k_hw_common(ah);
1078         u32 rxfilter;
1079
1080         if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1081                 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1082                 return;
1083         }
1084
1085         ath9k_ps_wakeup(sc);
1086         rxfilter = ath9k_hw_getrxfilter(ah);
1087         ath9k_hw_setrxfilter(ah, rxfilter |
1088                                  ATH9K_RX_FILTER_PHYRADAR |
1089                                  ATH9K_RX_FILTER_PHYERR);
1090
1091         /* TODO: usually this should not be neccesary, but for some reason
1092          * (or in some mode?) the trigger must be called after the
1093          * configuration, otherwise the register will have its values reset
1094          * (on my ar9220 to value 0x01002310)
1095          */
1096         ath9k_spectral_scan_config(hw, sc->spectral_mode);
1097         ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
1098         ath9k_ps_restore(sc);
1099 }
1100
1101 int ath9k_spectral_scan_config(struct ieee80211_hw *hw,
1102                                enum spectral_mode spectral_mode)
1103 {
1104         struct ath_softc *sc = hw->priv;
1105         struct ath_hw *ah = sc->sc_ah;
1106         struct ath_common *common = ath9k_hw_common(ah);
1107
1108         if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1109                 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1110                 return -1;
1111         }
1112
1113         switch (spectral_mode) {
1114         case SPECTRAL_DISABLED:
1115                 sc->spec_config.enabled = 0;
1116                 break;
1117         case SPECTRAL_BACKGROUND:
1118                 /* send endless samples.
1119                  * TODO: is this really useful for "background"?
1120                  */
1121                 sc->spec_config.endless = 1;
1122                 sc->spec_config.enabled = 1;
1123                 break;
1124         case SPECTRAL_CHANSCAN:
1125         case SPECTRAL_MANUAL:
1126                 sc->spec_config.endless = 0;
1127                 sc->spec_config.enabled = 1;
1128                 break;
1129         default:
1130                 return -1;
1131         }
1132
1133         ath9k_ps_wakeup(sc);
1134         ath9k_hw_ops(ah)->spectral_scan_config(ah, &sc->spec_config);
1135         ath9k_ps_restore(sc);
1136
1137         sc->spectral_mode = spectral_mode;
1138
1139         return 0;
1140 }
1141
1142 static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
1143 {
1144         struct ath_softc *sc = hw->priv;
1145         struct ath_hw *ah = sc->sc_ah;
1146         struct ath_common *common = ath9k_hw_common(ah);
1147         struct ieee80211_conf *conf = &hw->conf;
1148         bool reset_channel = false;
1149
1150         ath9k_ps_wakeup(sc);
1151         mutex_lock(&sc->mutex);
1152
1153         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1154                 sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1155                 if (sc->ps_idle) {
1156                         ath_cancel_work(sc);
1157                         ath9k_stop_btcoex(sc);
1158                 } else {
1159                         ath9k_start_btcoex(sc);
1160                         /*
1161                          * The chip needs a reset to properly wake up from
1162                          * full sleep
1163                          */
1164                         reset_channel = ah->chip_fullsleep;
1165                 }
1166         }
1167
1168         /*
1169          * We just prepare to enable PS. We have to wait until our AP has
1170          * ACK'd our null data frame to disable RX otherwise we'll ignore
1171          * those ACKs and end up retransmitting the same null data frames.
1172          * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1173          */
1174         if (changed & IEEE80211_CONF_CHANGE_PS) {
1175                 unsigned long flags;
1176                 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1177                 if (conf->flags & IEEE80211_CONF_PS)
1178                         ath9k_enable_ps(sc);
1179                 else
1180                         ath9k_disable_ps(sc);
1181                 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1182         }
1183
1184         if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1185                 if (conf->flags & IEEE80211_CONF_MONITOR) {
1186                         ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
1187                         sc->sc_ah->is_monitoring = true;
1188                 } else {
1189                         ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
1190                         sc->sc_ah->is_monitoring = false;
1191                 }
1192         }
1193
1194         if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || reset_channel) {
1195                 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
1196                 enum nl80211_channel_type channel_type =
1197                         cfg80211_get_chandef_type(&conf->chandef);
1198                 int pos = curchan->hw_value;
1199                 int old_pos = -1;
1200                 unsigned long flags;
1201
1202                 if (ah->curchan)
1203                         old_pos = ah->curchan - &ah->channels[0];
1204
1205                 ath_dbg(common, CONFIG, "Set channel: %d MHz type: %d\n",
1206                         curchan->center_freq, channel_type);
1207
1208                 /* update survey stats for the old channel before switching */
1209                 spin_lock_irqsave(&common->cc_lock, flags);
1210                 ath_update_survey_stats(sc);
1211                 spin_unlock_irqrestore(&common->cc_lock, flags);
1212
1213                 ath9k_cmn_update_ichannel(&sc->sc_ah->channels[pos],
1214                                           curchan, channel_type);
1215
1216                 /*
1217                  * If the operating channel changes, change the survey in-use flags
1218                  * along with it.
1219                  * Reset the survey data for the new channel, unless we're switching
1220                  * back to the operating channel from an off-channel operation.
1221                  */
1222                 if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) &&
1223                     sc->cur_survey != &sc->survey[pos]) {
1224
1225                         if (sc->cur_survey)
1226                                 sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
1227
1228                         sc->cur_survey = &sc->survey[pos];
1229
1230                         memset(sc->cur_survey, 0, sizeof(struct survey_info));
1231                         sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
1232                 } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
1233                         memset(&sc->survey[pos], 0, sizeof(struct survey_info));
1234                 }
1235
1236                 if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
1237                         ath_err(common, "Unable to set channel\n");
1238                         mutex_unlock(&sc->mutex);
1239                         ath9k_ps_restore(sc);
1240                         return -EINVAL;
1241                 }
1242
1243                 /*
1244                  * The most recent snapshot of channel->noisefloor for the old
1245                  * channel is only available after the hardware reset. Copy it to
1246                  * the survey stats now.
1247                  */
1248                 if (old_pos >= 0)
1249                         ath_update_survey_nf(sc, old_pos);
1250
1251                 /*
1252                  * Enable radar pulse detection if on a DFS channel. Spectral
1253                  * scanning and radar detection can not be used concurrently.
1254                  */
1255                 if (hw->conf.radar_enabled) {
1256                         u32 rxfilter;
1257
1258                         /* set HW specific DFS configuration */
1259                         ath9k_hw_set_radar_params(ah);
1260                         rxfilter = ath9k_hw_getrxfilter(ah);
1261                         rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
1262                                     ATH9K_RX_FILTER_PHYERR;
1263                         ath9k_hw_setrxfilter(ah, rxfilter);
1264                         ath_dbg(common, DFS, "DFS enabled at freq %d\n",
1265                                 curchan->center_freq);
1266                 } else {
1267                         /* perform spectral scan if requested. */
1268                         if (sc->scanning &&
1269                             sc->spectral_mode == SPECTRAL_CHANSCAN)
1270                                 ath9k_spectral_scan_trigger(hw);
1271                 }
1272         }
1273
1274         if (changed & IEEE80211_CONF_CHANGE_POWER) {
1275                 ath_dbg(common, CONFIG, "Set power: %d\n", conf->power_level);
1276                 sc->config.txpowlimit = 2 * conf->power_level;
1277                 ath9k_cmn_update_txpow(ah, sc->curtxpow,
1278                                        sc->config.txpowlimit, &sc->curtxpow);
1279         }
1280
1281         mutex_unlock(&sc->mutex);
1282         ath9k_ps_restore(sc);
1283
1284         return 0;
1285 }
1286
1287 #define SUPPORTED_FILTERS                       \
1288         (FIF_PROMISC_IN_BSS |                   \
1289         FIF_ALLMULTI |                          \
1290         FIF_CONTROL |                           \
1291         FIF_PSPOLL |                            \
1292         FIF_OTHER_BSS |                         \
1293         FIF_BCN_PRBRESP_PROMISC |               \
1294         FIF_PROBE_REQ |                         \
1295         FIF_FCSFAIL)
1296
1297 /* FIXME: sc->sc_full_reset ? */
1298 static void ath9k_configure_filter(struct ieee80211_hw *hw,
1299                                    unsigned int changed_flags,
1300                                    unsigned int *total_flags,
1301                                    u64 multicast)
1302 {
1303         struct ath_softc *sc = hw->priv;
1304         u32 rfilt;
1305
1306         changed_flags &= SUPPORTED_FILTERS;
1307         *total_flags &= SUPPORTED_FILTERS;
1308
1309         sc->rx.rxfilter = *total_flags;
1310         ath9k_ps_wakeup(sc);
1311         rfilt = ath_calcrxfilter(sc);
1312         ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
1313         ath9k_ps_restore(sc);
1314
1315         ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
1316                 rfilt);
1317 }
1318
1319 static int ath9k_sta_add(struct ieee80211_hw *hw,
1320                          struct ieee80211_vif *vif,
1321                          struct ieee80211_sta *sta)
1322 {
1323         struct ath_softc *sc = hw->priv;
1324         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1325         struct ath_node *an = (struct ath_node *) sta->drv_priv;
1326         struct ieee80211_key_conf ps_key = { };
1327         int key;
1328
1329         ath_node_attach(sc, sta, vif);
1330
1331         if (vif->type != NL80211_IFTYPE_AP &&
1332             vif->type != NL80211_IFTYPE_AP_VLAN)
1333                 return 0;
1334
1335         key = ath_key_config(common, vif, sta, &ps_key);
1336         if (key > 0)
1337                 an->ps_key = key;
1338
1339         return 0;
1340 }
1341
1342 static void ath9k_del_ps_key(struct ath_softc *sc,
1343                              struct ieee80211_vif *vif,
1344                              struct ieee80211_sta *sta)
1345 {
1346         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1347         struct ath_node *an = (struct ath_node *) sta->drv_priv;
1348         struct ieee80211_key_conf ps_key = { .hw_key_idx = an->ps_key };
1349
1350         if (!an->ps_key)
1351             return;
1352
1353         ath_key_delete(common, &ps_key);
1354         an->ps_key = 0;
1355 }
1356
1357 static int ath9k_sta_remove(struct ieee80211_hw *hw,
1358                             struct ieee80211_vif *vif,
1359                             struct ieee80211_sta *sta)
1360 {
1361         struct ath_softc *sc = hw->priv;
1362
1363         ath9k_del_ps_key(sc, vif, sta);
1364         ath_node_detach(sc, sta);
1365
1366         return 0;
1367 }
1368
1369 static void ath9k_sta_notify(struct ieee80211_hw *hw,
1370                          struct ieee80211_vif *vif,
1371                          enum sta_notify_cmd cmd,
1372                          struct ieee80211_sta *sta)
1373 {
1374         struct ath_softc *sc = hw->priv;
1375         struct ath_node *an = (struct ath_node *) sta->drv_priv;
1376
1377         if (!sta->ht_cap.ht_supported)
1378                 return;
1379
1380         switch (cmd) {
1381         case STA_NOTIFY_SLEEP:
1382                 an->sleeping = true;
1383                 ath_tx_aggr_sleep(sta, sc, an);
1384                 break;
1385         case STA_NOTIFY_AWAKE:
1386                 an->sleeping = false;
1387                 ath_tx_aggr_wakeup(sc, an);
1388                 break;
1389         }
1390 }
1391
1392 static int ath9k_conf_tx(struct ieee80211_hw *hw,
1393                          struct ieee80211_vif *vif, u16 queue,
1394                          const struct ieee80211_tx_queue_params *params)
1395 {
1396         struct ath_softc *sc = hw->priv;
1397         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1398         struct ath_txq *txq;
1399         struct ath9k_tx_queue_info qi;
1400         int ret = 0;
1401
1402         if (queue >= IEEE80211_NUM_ACS)
1403                 return 0;
1404
1405         txq = sc->tx.txq_map[queue];
1406
1407         ath9k_ps_wakeup(sc);
1408         mutex_lock(&sc->mutex);
1409
1410         memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1411
1412         qi.tqi_aifs = params->aifs;
1413         qi.tqi_cwmin = params->cw_min;
1414         qi.tqi_cwmax = params->cw_max;
1415         qi.tqi_burstTime = params->txop * 32;
1416
1417         ath_dbg(common, CONFIG,
1418                 "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1419                 queue, txq->axq_qnum, params->aifs, params->cw_min,
1420                 params->cw_max, params->txop);
1421
1422         ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
1423         ret = ath_txq_update(sc, txq->axq_qnum, &qi);
1424         if (ret)
1425                 ath_err(common, "TXQ Update failed\n");
1426
1427         mutex_unlock(&sc->mutex);
1428         ath9k_ps_restore(sc);
1429
1430         return ret;
1431 }
1432
1433 static int ath9k_set_key(struct ieee80211_hw *hw,
1434                          enum set_key_cmd cmd,
1435                          struct ieee80211_vif *vif,
1436                          struct ieee80211_sta *sta,
1437                          struct ieee80211_key_conf *key)
1438 {
1439         struct ath_softc *sc = hw->priv;
1440         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1441         int ret = 0;
1442
1443         if (ath9k_modparam_nohwcrypt)
1444                 return -ENOSPC;
1445
1446         if ((vif->type == NL80211_IFTYPE_ADHOC ||
1447              vif->type == NL80211_IFTYPE_MESH_POINT) &&
1448             (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1449              key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1450             !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1451                 /*
1452                  * For now, disable hw crypto for the RSN IBSS group keys. This
1453                  * could be optimized in the future to use a modified key cache
1454                  * design to support per-STA RX GTK, but until that gets
1455                  * implemented, use of software crypto for group addressed
1456                  * frames is a acceptable to allow RSN IBSS to be used.
1457                  */
1458                 return -EOPNOTSUPP;
1459         }
1460
1461         mutex_lock(&sc->mutex);
1462         ath9k_ps_wakeup(sc);
1463         ath_dbg(common, CONFIG, "Set HW Key\n");
1464
1465         switch (cmd) {
1466         case SET_KEY:
1467                 if (sta)
1468                         ath9k_del_ps_key(sc, vif, sta);
1469
1470                 ret = ath_key_config(common, vif, sta, key);
1471                 if (ret >= 0) {
1472                         key->hw_key_idx = ret;
1473                         /* push IV and Michael MIC generation to stack */
1474                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1475                         if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1476                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1477                         if (sc->sc_ah->sw_mgmt_crypto &&
1478                             key->cipher == WLAN_CIPHER_SUITE_CCMP)
1479                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1480                         ret = 0;
1481                 }
1482                 break;
1483         case DISABLE_KEY:
1484                 ath_key_delete(common, key);
1485                 break;
1486         default:
1487                 ret = -EINVAL;
1488         }
1489
1490         ath9k_ps_restore(sc);
1491         mutex_unlock(&sc->mutex);
1492
1493         return ret;
1494 }
1495
1496 static void ath9k_set_assoc_state(struct ath_softc *sc,
1497                                   struct ieee80211_vif *vif)
1498 {
1499         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1500         struct ath_vif *avp = (void *)vif->drv_priv;
1501         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1502         unsigned long flags;
1503
1504         set_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags);
1505         avp->primary_sta_vif = true;
1506
1507         /*
1508          * Set the AID, BSSID and do beacon-sync only when
1509          * the HW opmode is STATION.
1510          *
1511          * But the primary bit is set above in any case.
1512          */
1513         if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
1514                 return;
1515
1516         memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1517         common->curaid = bss_conf->aid;
1518         ath9k_hw_write_associd(sc->sc_ah);
1519
1520         sc->last_rssi = ATH_RSSI_DUMMY_MARKER;
1521         sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
1522
1523         spin_lock_irqsave(&sc->sc_pm_lock, flags);
1524         sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
1525         spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1526
1527         if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1528                 ath9k_mci_update_wlan_channels(sc, false);
1529
1530         ath_dbg(common, CONFIG,
1531                 "Primary Station interface: %pM, BSSID: %pM\n",
1532                 vif->addr, common->curbssid);
1533 }
1534
1535 static void ath9k_bss_assoc_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1536 {
1537         struct ath_softc *sc = data;
1538         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1539
1540         if (test_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags))
1541                 return;
1542
1543         if (bss_conf->assoc)
1544                 ath9k_set_assoc_state(sc, vif);
1545 }
1546
1547 static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1548                                    struct ieee80211_vif *vif,
1549                                    struct ieee80211_bss_conf *bss_conf,
1550                                    u32 changed)
1551 {
1552 #define CHECK_ANI                               \
1553         (BSS_CHANGED_ASSOC |                    \
1554          BSS_CHANGED_IBSS |                     \
1555          BSS_CHANGED_BEACON_ENABLED)
1556
1557         struct ath_softc *sc = hw->priv;
1558         struct ath_hw *ah = sc->sc_ah;
1559         struct ath_common *common = ath9k_hw_common(ah);
1560         struct ath_vif *avp = (void *)vif->drv_priv;
1561         int slottime;
1562
1563         ath9k_ps_wakeup(sc);
1564         mutex_lock(&sc->mutex);
1565
1566         if (changed & BSS_CHANGED_ASSOC) {
1567                 ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
1568                         bss_conf->bssid, bss_conf->assoc);
1569
1570                 if (avp->primary_sta_vif && !bss_conf->assoc) {
1571                         clear_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags);
1572                         avp->primary_sta_vif = false;
1573
1574                         if (ah->opmode == NL80211_IFTYPE_STATION)
1575                                 clear_bit(SC_OP_BEACONS, &sc->sc_flags);
1576                 }
1577
1578                 ieee80211_iterate_active_interfaces_atomic(
1579                         sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1580                         ath9k_bss_assoc_iter, sc);
1581
1582                 if (!test_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags) &&
1583                     ah->opmode == NL80211_IFTYPE_STATION) {
1584                         memset(common->curbssid, 0, ETH_ALEN);
1585                         common->curaid = 0;
1586                         ath9k_hw_write_associd(sc->sc_ah);
1587                         if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1588                                 ath9k_mci_update_wlan_channels(sc, true);
1589                 }
1590         }
1591
1592         if (changed & BSS_CHANGED_IBSS) {
1593                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1594                 common->curaid = bss_conf->aid;
1595                 ath9k_hw_write_associd(sc->sc_ah);
1596         }
1597
1598         if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
1599             (changed & BSS_CHANGED_BEACON_INT)) {
1600                 if (ah->opmode == NL80211_IFTYPE_AP &&
1601                     bss_conf->enable_beacon)
1602                         ath9k_set_tsfadjust(sc, vif);
1603                 if (ath9k_allow_beacon_config(sc, vif))
1604                         ath9k_beacon_config(sc, vif, changed);
1605         }
1606
1607         if (changed & BSS_CHANGED_ERP_SLOT) {
1608                 if (bss_conf->use_short_slot)
1609                         slottime = 9;
1610                 else
1611                         slottime = 20;
1612                 if (vif->type == NL80211_IFTYPE_AP) {
1613                         /*
1614                          * Defer update, so that connected stations can adjust
1615                          * their settings at the same time.
1616                          * See beacon.c for more details
1617                          */
1618                         sc->beacon.slottime = slottime;
1619                         sc->beacon.updateslot = UPDATE;
1620                 } else {
1621                         ah->slottime = slottime;
1622                         ath9k_hw_init_global_settings(ah);
1623                 }
1624         }
1625
1626         if (changed & CHECK_ANI)
1627                 ath_check_ani(sc);
1628
1629         mutex_unlock(&sc->mutex);
1630         ath9k_ps_restore(sc);
1631
1632 #undef CHECK_ANI
1633 }
1634
1635 static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1636 {
1637         struct ath_softc *sc = hw->priv;
1638         u64 tsf;
1639
1640         mutex_lock(&sc->mutex);
1641         ath9k_ps_wakeup(sc);
1642         tsf = ath9k_hw_gettsf64(sc->sc_ah);
1643         ath9k_ps_restore(sc);
1644         mutex_unlock(&sc->mutex);
1645
1646         return tsf;
1647 }
1648
1649 static void ath9k_set_tsf(struct ieee80211_hw *hw,
1650                           struct ieee80211_vif *vif,
1651                           u64 tsf)
1652 {
1653         struct ath_softc *sc = hw->priv;
1654
1655         mutex_lock(&sc->mutex);
1656         ath9k_ps_wakeup(sc);
1657         ath9k_hw_settsf64(sc->sc_ah, tsf);
1658         ath9k_ps_restore(sc);
1659         mutex_unlock(&sc->mutex);
1660 }
1661
1662 static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1663 {
1664         struct ath_softc *sc = hw->priv;
1665
1666         mutex_lock(&sc->mutex);
1667
1668         ath9k_ps_wakeup(sc);
1669         ath9k_hw_reset_tsf(sc->sc_ah);
1670         ath9k_ps_restore(sc);
1671
1672         mutex_unlock(&sc->mutex);
1673 }
1674
1675 static int ath9k_ampdu_action(struct ieee80211_hw *hw,
1676                               struct ieee80211_vif *vif,
1677                               enum ieee80211_ampdu_mlme_action action,
1678                               struct ieee80211_sta *sta,
1679                               u16 tid, u16 *ssn, u8 buf_size)
1680 {
1681         struct ath_softc *sc = hw->priv;
1682         bool flush = false;
1683         int ret = 0;
1684
1685         local_bh_disable();
1686
1687         switch (action) {
1688         case IEEE80211_AMPDU_RX_START:
1689                 break;
1690         case IEEE80211_AMPDU_RX_STOP:
1691                 break;
1692         case IEEE80211_AMPDU_TX_START:
1693                 ath9k_ps_wakeup(sc);
1694                 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
1695                 if (!ret)
1696                         ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1697                 ath9k_ps_restore(sc);
1698                 break;
1699         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1700         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1701                 flush = true;
1702         case IEEE80211_AMPDU_TX_STOP_CONT:
1703                 ath9k_ps_wakeup(sc);
1704                 ath_tx_aggr_stop(sc, sta, tid);
1705                 if (!flush)
1706                         ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1707                 ath9k_ps_restore(sc);
1708                 break;
1709         case IEEE80211_AMPDU_TX_OPERATIONAL:
1710                 ath9k_ps_wakeup(sc);
1711                 ath_tx_aggr_resume(sc, sta, tid);
1712                 ath9k_ps_restore(sc);
1713                 break;
1714         default:
1715                 ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
1716         }
1717
1718         local_bh_enable();
1719
1720         return ret;
1721 }
1722
1723 static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
1724                              struct survey_info *survey)
1725 {
1726         struct ath_softc *sc = hw->priv;
1727         struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1728         struct ieee80211_supported_band *sband;
1729         struct ieee80211_channel *chan;
1730         unsigned long flags;
1731         int pos;
1732
1733         spin_lock_irqsave(&common->cc_lock, flags);
1734         if (idx == 0)
1735                 ath_update_survey_stats(sc);
1736
1737         sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
1738         if (sband && idx >= sband->n_channels) {
1739                 idx -= sband->n_channels;
1740                 sband = NULL;
1741         }
1742
1743         if (!sband)
1744                 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
1745
1746         if (!sband || idx >= sband->n_channels) {
1747                 spin_unlock_irqrestore(&common->cc_lock, flags);
1748                 return -ENOENT;
1749         }
1750
1751         chan = &sband->channels[idx];
1752         pos = chan->hw_value;
1753         memcpy(survey, &sc->survey[pos], sizeof(*survey));
1754         survey->channel = chan;
1755         spin_unlock_irqrestore(&common->cc_lock, flags);
1756
1757         return 0;
1758 }
1759
1760 static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
1761 {
1762         struct ath_softc *sc = hw->priv;
1763         struct ath_hw *ah = sc->sc_ah;
1764
1765         mutex_lock(&sc->mutex);
1766         ah->coverage_class = coverage_class;
1767
1768         ath9k_ps_wakeup(sc);
1769         ath9k_hw_init_global_settings(ah);
1770         ath9k_ps_restore(sc);
1771
1772         mutex_unlock(&sc->mutex);
1773 }
1774
1775 static void ath9k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1776 {
1777         struct ath_softc *sc = hw->priv;
1778         struct ath_hw *ah = sc->sc_ah;
1779         struct ath_common *common = ath9k_hw_common(ah);
1780         int timeout = 200; /* ms */
1781         int i, j;
1782         bool drain_txq;
1783
1784         mutex_lock(&sc->mutex);
1785         cancel_delayed_work_sync(&sc->tx_complete_work);
1786
1787         if (ah->ah_flags & AH_UNPLUGGED) {
1788                 ath_dbg(common, ANY, "Device has been unplugged!\n");
1789                 mutex_unlock(&sc->mutex);
1790                 return;
1791         }
1792
1793         if (test_bit(SC_OP_INVALID, &sc->sc_flags)) {
1794                 ath_dbg(common, ANY, "Device not present\n");
1795                 mutex_unlock(&sc->mutex);
1796                 return;
1797         }
1798
1799         for (j = 0; j < timeout; j++) {
1800                 bool npend = false;
1801
1802                 if (j)
1803                         usleep_range(1000, 2000);
1804
1805                 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1806                         if (!ATH_TXQ_SETUP(sc, i))
1807                                 continue;
1808
1809                         npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i]);
1810
1811                         if (npend)
1812                                 break;
1813                 }
1814
1815                 if (!npend)
1816                     break;
1817         }
1818
1819         if (drop) {
1820                 ath9k_ps_wakeup(sc);
1821                 spin_lock_bh(&sc->sc_pcu_lock);
1822                 drain_txq = ath_drain_all_txq(sc);
1823                 spin_unlock_bh(&sc->sc_pcu_lock);
1824
1825                 if (!drain_txq)
1826                         ath_reset(sc);
1827
1828                 ath9k_ps_restore(sc);
1829                 ieee80211_wake_queues(hw);
1830         }
1831
1832         ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
1833         mutex_unlock(&sc->mutex);
1834 }
1835
1836 static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
1837 {
1838         struct ath_softc *sc = hw->priv;
1839         int i;
1840
1841         for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1842                 if (!ATH_TXQ_SETUP(sc, i))
1843                         continue;
1844
1845                 if (ath9k_has_pending_frames(sc, &sc->tx.txq[i]))
1846                         return true;
1847         }
1848         return false;
1849 }
1850
1851 static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
1852 {
1853         struct ath_softc *sc = hw->priv;
1854         struct ath_hw *ah = sc->sc_ah;
1855         struct ieee80211_vif *vif;
1856         struct ath_vif *avp;
1857         struct ath_buf *bf;
1858         struct ath_tx_status ts;
1859         bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1860         int status;
1861
1862         vif = sc->beacon.bslot[0];
1863         if (!vif)
1864                 return 0;
1865
1866         if (!vif->bss_conf.enable_beacon)
1867                 return 0;
1868
1869         avp = (void *)vif->drv_priv;
1870
1871         if (!sc->beacon.tx_processed && !edma) {
1872                 tasklet_disable(&sc->bcon_tasklet);
1873
1874                 bf = avp->av_bcbuf;
1875                 if (!bf || !bf->bf_mpdu)
1876                         goto skip;
1877
1878                 status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
1879                 if (status == -EINPROGRESS)
1880                         goto skip;
1881
1882                 sc->beacon.tx_processed = true;
1883                 sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
1884
1885 skip:
1886                 tasklet_enable(&sc->bcon_tasklet);
1887         }
1888
1889         return sc->beacon.tx_last;
1890 }
1891
1892 static int ath9k_get_stats(struct ieee80211_hw *hw,
1893                            struct ieee80211_low_level_stats *stats)
1894 {
1895         struct ath_softc *sc = hw->priv;
1896         struct ath_hw *ah = sc->sc_ah;
1897         struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1898
1899         stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1900         stats->dot11RTSFailureCount = mib_stats->rts_bad;
1901         stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1902         stats->dot11RTSSuccessCount = mib_stats->rts_good;
1903         return 0;
1904 }
1905
1906 static u32 fill_chainmask(u32 cap, u32 new)
1907 {
1908         u32 filled = 0;
1909         int i;
1910
1911         for (i = 0; cap && new; i++, cap >>= 1) {
1912                 if (!(cap & BIT(0)))
1913                         continue;
1914
1915                 if (new & BIT(0))
1916                         filled |= BIT(i);
1917
1918                 new >>= 1;
1919         }
1920
1921         return filled;
1922 }
1923
1924 static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
1925 {
1926         if (AR_SREV_9300_20_OR_LATER(ah))
1927                 return true;
1928
1929         switch (val & 0x7) {
1930         case 0x1:
1931         case 0x3:
1932         case 0x7:
1933                 return true;
1934         case 0x2:
1935                 return (ah->caps.rx_chainmask == 1);
1936         default:
1937                 return false;
1938         }
1939 }
1940
1941 static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
1942 {
1943         struct ath_softc *sc = hw->priv;
1944         struct ath_hw *ah = sc->sc_ah;
1945
1946         if (ah->caps.rx_chainmask != 1)
1947                 rx_ant |= tx_ant;
1948
1949         if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
1950                 return -EINVAL;
1951
1952         sc->ant_rx = rx_ant;
1953         sc->ant_tx = tx_ant;
1954
1955         if (ah->caps.rx_chainmask == 1)
1956                 return 0;
1957
1958         /* AR9100 runs into calibration issues if not all rx chains are enabled */
1959         if (AR_SREV_9100(ah))
1960                 ah->rxchainmask = 0x7;
1961         else
1962                 ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
1963
1964         ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
1965         ath9k_reload_chainmask_settings(sc);
1966
1967         return 0;
1968 }
1969
1970 static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
1971 {
1972         struct ath_softc *sc = hw->priv;
1973
1974         *tx_ant = sc->ant_tx;
1975         *rx_ant = sc->ant_rx;
1976         return 0;
1977 }
1978
1979 #ifdef CONFIG_PM_SLEEP
1980
1981 static void ath9k_wow_map_triggers(struct ath_softc *sc,
1982                                    struct cfg80211_wowlan *wowlan,
1983                                    u32 *wow_triggers)
1984 {
1985         if (wowlan->disconnect)
1986                 *wow_triggers |= AH_WOW_LINK_CHANGE |
1987                                  AH_WOW_BEACON_MISS;
1988         if (wowlan->magic_pkt)
1989                 *wow_triggers |= AH_WOW_MAGIC_PATTERN_EN;
1990
1991         if (wowlan->n_patterns)
1992                 *wow_triggers |= AH_WOW_USER_PATTERN_EN;
1993
1994         sc->wow_enabled = *wow_triggers;
1995
1996 }
1997
1998 static void ath9k_wow_add_disassoc_deauth_pattern(struct ath_softc *sc)
1999 {
2000         struct ath_hw *ah = sc->sc_ah;
2001         struct ath_common *common = ath9k_hw_common(ah);
2002         struct ath9k_hw_capabilities *pcaps = &ah->caps;
2003         int pattern_count = 0;
2004         int i, byte_cnt;
2005         u8 dis_deauth_pattern[MAX_PATTERN_SIZE];
2006         u8 dis_deauth_mask[MAX_PATTERN_SIZE];
2007
2008         memset(dis_deauth_pattern, 0, MAX_PATTERN_SIZE);
2009         memset(dis_deauth_mask, 0, MAX_PATTERN_SIZE);
2010
2011         /*
2012          * Create Dissassociate / Deauthenticate packet filter
2013          *
2014          *     2 bytes        2 byte    6 bytes   6 bytes  6 bytes
2015          *  +--------------+----------+---------+--------+--------+----
2016          *  + Frame Control+ Duration +   DA    +  SA    +  BSSID +
2017          *  +--------------+----------+---------+--------+--------+----
2018          *
2019          * The above is the management frame format for disassociate/
2020          * deauthenticate pattern, from this we need to match the first byte
2021          * of 'Frame Control' and DA, SA, and BSSID fields
2022          * (skipping 2nd byte of FC and Duration feild.
2023          *
2024          * Disassociate pattern
2025          * --------------------
2026          * Frame control = 00 00 1010
2027          * DA, SA, BSSID = x:x:x:x:x:x
2028          * Pattern will be A0000000 | x:x:x:x:x:x | x:x:x:x:x:x
2029          *                          | x:x:x:x:x:x  -- 22 bytes
2030          *
2031          * Deauthenticate pattern
2032          * ----------------------
2033          * Frame control = 00 00 1100
2034          * DA, SA, BSSID = x:x:x:x:x:x
2035          * Pattern will be C0000000 | x:x:x:x:x:x | x:x:x:x:x:x
2036          *                          | x:x:x:x:x:x  -- 22 bytes
2037          */
2038
2039         /* Create Disassociate Pattern first */
2040
2041         byte_cnt = 0;
2042
2043         /* Fill out the mask with all FF's */
2044
2045         for (i = 0; i < MAX_PATTERN_MASK_SIZE; i++)
2046                 dis_deauth_mask[i] = 0xff;
2047
2048         /* copy the first byte of frame control field */
2049         dis_deauth_pattern[byte_cnt] = 0xa0;
2050         byte_cnt++;
2051
2052         /* skip 2nd byte of frame control and Duration field */
2053         byte_cnt += 3;
2054
2055         /*
2056          * need not match the destination mac address, it can be a broadcast
2057          * mac address or an unicast to this station
2058          */
2059         byte_cnt += 6;
2060
2061         /* copy the source mac address */
2062         memcpy((dis_deauth_pattern + byte_cnt), common->curbssid, ETH_ALEN);
2063
2064         byte_cnt += 6;
2065
2066         /* copy the bssid, its same as the source mac address */
2067
2068         memcpy((dis_deauth_pattern + byte_cnt), common->curbssid, ETH_ALEN);
2069
2070         /* Create Disassociate pattern mask */
2071
2072         if (pcaps->hw_caps & ATH9K_HW_WOW_PATTERN_MATCH_EXACT) {
2073
2074                 if (pcaps->hw_caps & ATH9K_HW_WOW_PATTERN_MATCH_DWORD) {
2075                         /*
2076                          * for AR9280, because of hardware limitation, the
2077                          * first 4 bytes have to be matched for all patterns.
2078                          * the mask for disassociation and de-auth pattern
2079                          * matching need to enable the first 4 bytes.
2080                          * also the duration field needs to be filled.
2081                          */
2082                         dis_deauth_mask[0] = 0xf0;
2083
2084                         /*
2085                          * fill in duration field
2086                          FIXME: what is the exact value ?
2087                          */
2088                         dis_deauth_pattern[2] = 0xff;
2089                         dis_deauth_pattern[3] = 0xff;
2090                 } else {
2091                         dis_deauth_mask[0] = 0xfe;
2092                 }
2093
2094                 dis_deauth_mask[1] = 0x03;
2095                 dis_deauth_mask[2] = 0xc0;
2096         } else {
2097                 dis_deauth_mask[0] = 0xef;
2098                 dis_deauth_mask[1] = 0x3f;
2099                 dis_deauth_mask[2] = 0x00;
2100                 dis_deauth_mask[3] = 0xfc;
2101         }
2102
2103         ath_dbg(common, WOW, "Adding disassoc/deauth patterns for WoW\n");
2104
2105         ath9k_hw_wow_apply_pattern(ah, dis_deauth_pattern, dis_deauth_mask,
2106                                    pattern_count, byte_cnt);
2107
2108         pattern_count++;
2109         /*
2110          * for de-authenticate pattern, only the first byte of the frame
2111          * control field gets changed from 0xA0 to 0xC0
2112          */
2113         dis_deauth_pattern[0] = 0xC0;
2114
2115         ath9k_hw_wow_apply_pattern(ah, dis_deauth_pattern, dis_deauth_mask,
2116                                    pattern_count, byte_cnt);
2117
2118 }
2119
2120 static void ath9k_wow_add_pattern(struct ath_softc *sc,
2121                                   struct cfg80211_wowlan *wowlan)
2122 {
2123         struct ath_hw *ah = sc->sc_ah;
2124         struct ath9k_wow_pattern *wow_pattern = NULL;
2125         struct cfg80211_wowlan_trig_pkt_pattern *patterns = wowlan->patterns;
2126         int mask_len;
2127         s8 i = 0;
2128
2129         if (!wowlan->n_patterns)
2130                 return;
2131
2132         /*
2133          * Add the new user configured patterns
2134          */
2135         for (i = 0; i < wowlan->n_patterns; i++) {
2136
2137                 wow_pattern = kzalloc(sizeof(*wow_pattern), GFP_KERNEL);
2138
2139                 if (!wow_pattern)
2140                         return;
2141
2142                 /*
2143                  * TODO: convert the generic user space pattern to
2144                  * appropriate chip specific/802.11 pattern.
2145                  */
2146
2147                 mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
2148                 memset(wow_pattern->pattern_bytes, 0, MAX_PATTERN_SIZE);
2149                 memset(wow_pattern->mask_bytes, 0, MAX_PATTERN_SIZE);
2150                 memcpy(wow_pattern->pattern_bytes, patterns[i].pattern,
2151                        patterns[i].pattern_len);
2152                 memcpy(wow_pattern->mask_bytes, patterns[i].mask, mask_len);
2153                 wow_pattern->pattern_len = patterns[i].pattern_len;
2154
2155                 /*
2156                  * just need to take care of deauth and disssoc pattern,
2157                  * make sure we don't overwrite them.
2158                  */
2159
2160                 ath9k_hw_wow_apply_pattern(ah, wow_pattern->pattern_bytes,
2161                                            wow_pattern->mask_bytes,
2162                                            i + 2,
2163                                            wow_pattern->pattern_len);
2164                 kfree(wow_pattern);
2165
2166         }
2167
2168 }
2169
2170 static int ath9k_suspend(struct ieee80211_hw *hw,
2171                          struct cfg80211_wowlan *wowlan)
2172 {
2173         struct ath_softc *sc = hw->priv;
2174         struct ath_hw *ah = sc->sc_ah;
2175         struct ath_common *common = ath9k_hw_common(ah);
2176         u32 wow_triggers_enabled = 0;
2177         int ret = 0;
2178
2179         mutex_lock(&sc->mutex);
2180
2181         ath_cancel_work(sc);
2182         ath_stop_ani(sc);
2183         del_timer_sync(&sc->rx_poll_timer);
2184
2185         if (test_bit(SC_OP_INVALID, &sc->sc_flags)) {
2186                 ath_dbg(common, ANY, "Device not present\n");
2187                 ret = -EINVAL;
2188                 goto fail_wow;
2189         }
2190
2191         if (WARN_ON(!wowlan)) {
2192                 ath_dbg(common, WOW, "None of the WoW triggers enabled\n");
2193                 ret = -EINVAL;
2194                 goto fail_wow;
2195         }
2196
2197         if (!device_can_wakeup(sc->dev)) {
2198                 ath_dbg(common, WOW, "device_can_wakeup failed, WoW is not enabled\n");
2199                 ret = 1;
2200                 goto fail_wow;
2201         }
2202
2203         /*
2204          * none of the sta vifs are associated
2205          * and we are not currently handling multivif
2206          * cases, for instance we have to seperately
2207          * configure 'keep alive frame' for each
2208          * STA.
2209          */
2210
2211         if (!test_bit(SC_OP_PRIM_STA_VIF, &sc->sc_flags)) {
2212                 ath_dbg(common, WOW, "None of the STA vifs are associated\n");
2213                 ret = 1;
2214                 goto fail_wow;
2215         }
2216
2217         if (sc->nvifs > 1) {
2218                 ath_dbg(common, WOW, "WoW for multivif is not yet supported\n");
2219                 ret = 1;
2220                 goto fail_wow;
2221         }
2222
2223         ath9k_wow_map_triggers(sc, wowlan, &wow_triggers_enabled);
2224
2225         ath_dbg(common, WOW, "WoW triggers enabled 0x%x\n",
2226                 wow_triggers_enabled);
2227
2228         ath9k_ps_wakeup(sc);
2229
2230         ath9k_stop_btcoex(sc);
2231
2232         /*
2233          * Enable wake up on recieving disassoc/deauth
2234          * frame by default.
2235          */
2236         ath9k_wow_add_disassoc_deauth_pattern(sc);
2237
2238         if (wow_triggers_enabled & AH_WOW_USER_PATTERN_EN)
2239                 ath9k_wow_add_pattern(sc, wowlan);
2240
2241         spin_lock_bh(&sc->sc_pcu_lock);
2242         /*
2243          * To avoid false wake, we enable beacon miss interrupt only
2244          * when we go to sleep. We save the current interrupt mask
2245          * so we can restore it after the system wakes up
2246          */
2247         sc->wow_intr_before_sleep = ah->imask;
2248         ah->imask &= ~ATH9K_INT_GLOBAL;
2249         ath9k_hw_disable_interrupts(ah);
2250         ah->imask = ATH9K_INT_BMISS | ATH9K_INT_GLOBAL;
2251         ath9k_hw_set_interrupts(ah);
2252         ath9k_hw_enable_interrupts(ah);
2253
2254         spin_unlock_bh(&sc->sc_pcu_lock);
2255
2256         /*
2257          * we can now sync irq and kill any running tasklets, since we already
2258          * disabled interrupts and not holding a spin lock
2259          */
2260         synchronize_irq(sc->irq);
2261         tasklet_kill(&sc->intr_tq);
2262
2263         ath9k_hw_wow_enable(ah, wow_triggers_enabled);
2264
2265         ath9k_ps_restore(sc);
2266         ath_dbg(common, ANY, "WoW enabled in ath9k\n");
2267         atomic_inc(&sc->wow_sleep_proc_intr);
2268
2269 fail_wow:
2270         mutex_unlock(&sc->mutex);
2271         return ret;
2272 }
2273
2274 static int ath9k_resume(struct ieee80211_hw *hw)
2275 {
2276         struct ath_softc *sc = hw->priv;
2277         struct ath_hw *ah = sc->sc_ah;
2278         struct ath_common *common = ath9k_hw_common(ah);
2279         u32 wow_status;
2280
2281         mutex_lock(&sc->mutex);
2282
2283         ath9k_ps_wakeup(sc);
2284
2285         spin_lock_bh(&sc->sc_pcu_lock);
2286
2287         ath9k_hw_disable_interrupts(ah);
2288         ah->imask = sc->wow_intr_before_sleep;
2289         ath9k_hw_set_interrupts(ah);
2290         ath9k_hw_enable_interrupts(ah);
2291
2292         spin_unlock_bh(&sc->sc_pcu_lock);
2293
2294         wow_status = ath9k_hw_wow_wakeup(ah);
2295
2296         if (atomic_read(&sc->wow_got_bmiss_intr) == 0) {
2297                 /*
2298                  * some devices may not pick beacon miss
2299                  * as the reason they woke up so we add
2300                  * that here for that shortcoming.
2301                  */
2302                 wow_status |= AH_WOW_BEACON_MISS;
2303                 atomic_dec(&sc->wow_got_bmiss_intr);
2304                 ath_dbg(common, ANY, "Beacon miss interrupt picked up during WoW sleep\n");
2305         }
2306
2307         atomic_dec(&sc->wow_sleep_proc_intr);
2308
2309         if (wow_status) {
2310                 ath_dbg(common, ANY, "Waking up due to WoW triggers %s with WoW status = %x\n",
2311                         ath9k_hw_wow_event_to_string(wow_status), wow_status);
2312         }
2313
2314         ath_restart_work(sc);
2315         ath9k_start_btcoex(sc);
2316
2317         ath9k_ps_restore(sc);
2318         mutex_unlock(&sc->mutex);
2319
2320         return 0;
2321 }
2322
2323 static void ath9k_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2324 {
2325         struct ath_softc *sc = hw->priv;
2326
2327         mutex_lock(&sc->mutex);
2328         device_init_wakeup(sc->dev, 1);
2329         device_set_wakeup_enable(sc->dev, enabled);
2330         mutex_unlock(&sc->mutex);
2331 }
2332
2333 #endif
2334 static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2335 {
2336         struct ath_softc *sc = hw->priv;
2337
2338         sc->scanning = 1;
2339 }
2340
2341 static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2342 {
2343         struct ath_softc *sc = hw->priv;
2344
2345         sc->scanning = 0;
2346 }
2347
2348 struct ieee80211_ops ath9k_ops = {
2349         .tx                 = ath9k_tx,
2350         .start              = ath9k_start,
2351         .stop               = ath9k_stop,
2352         .add_interface      = ath9k_add_interface,
2353         .change_interface   = ath9k_change_interface,
2354         .remove_interface   = ath9k_remove_interface,
2355         .config             = ath9k_config,
2356         .configure_filter   = ath9k_configure_filter,
2357         .sta_add            = ath9k_sta_add,
2358         .sta_remove         = ath9k_sta_remove,
2359         .sta_notify         = ath9k_sta_notify,
2360         .conf_tx            = ath9k_conf_tx,
2361         .bss_info_changed   = ath9k_bss_info_changed,
2362         .set_key            = ath9k_set_key,
2363         .get_tsf            = ath9k_get_tsf,
2364         .set_tsf            = ath9k_set_tsf,
2365         .reset_tsf          = ath9k_reset_tsf,
2366         .ampdu_action       = ath9k_ampdu_action,
2367         .get_survey         = ath9k_get_survey,
2368         .rfkill_poll        = ath9k_rfkill_poll_state,
2369         .set_coverage_class = ath9k_set_coverage_class,
2370         .flush              = ath9k_flush,
2371         .tx_frames_pending  = ath9k_tx_frames_pending,
2372         .tx_last_beacon     = ath9k_tx_last_beacon,
2373         .get_stats          = ath9k_get_stats,
2374         .set_antenna        = ath9k_set_antenna,
2375         .get_antenna        = ath9k_get_antenna,
2376
2377 #ifdef CONFIG_PM_SLEEP
2378         .suspend            = ath9k_suspend,
2379         .resume             = ath9k_resume,
2380         .set_wakeup         = ath9k_set_wakeup,
2381 #endif
2382
2383 #ifdef CONFIG_ATH9K_DEBUGFS
2384         .get_et_sset_count  = ath9k_get_et_sset_count,
2385         .get_et_stats       = ath9k_get_et_stats,
2386         .get_et_strings     = ath9k_get_et_strings,
2387 #endif
2388
2389 #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_DEBUGFS)
2390         .sta_add_debugfs    = ath9k_sta_add_debugfs,
2391         .sta_remove_debugfs = ath9k_sta_remove_debugfs,
2392 #endif
2393         .sw_scan_start      = ath9k_sw_scan_start,
2394         .sw_scan_complete   = ath9k_sw_scan_complete,
2395 };