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