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