blob: e0cb2af5176743eddb89927d2fa1099815833fdb [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
Sujith Manoharan5b681382011-05-17 13:36:18 +05302 * Copyright (c) 2008-2011 Atheros Communications Inc.
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07003 *
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
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070017#include <linux/nl80211.h>
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -080018#include <linux/delay.h>
Sujith394cf0a2009-02-09 13:26:54 +053019#include "ath9k.h"
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -070020#include "btcoex.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070021
Sujith Manoharan6dcc3442012-07-17 17:16:36 +053022static void ath9k_set_assoc_state(struct ath_softc *sc,
23 struct ieee80211_vif *vif);
24
Sven Eckelmann313eb872012-06-25 07:15:22 +020025u8 ath9k_parse_mpdudensity(u8 mpdudensity)
Sujithff37e332008-11-24 12:07:55 +053026{
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
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -080060static 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
Felix Fietkau04535312014-06-11 16:17:51 +053066 if (txq->axq_depth)
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -080067 pending = true;
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -080068
Felix Fietkau04535312014-06-11 16:17:51 +053069 if (txq->mac80211_qnum >= 0) {
70 struct list_head *list;
71
72 list = &sc->cur_chan->acq[txq->mac80211_qnum];
73 if (!list_empty(list))
74 pending = true;
75 }
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -080076 spin_unlock_bh(&txq->axq_lock);
77 return pending;
78}
79
Mohammed Shafi Shajakhan6d79cb42011-05-19 17:40:46 +053080static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
Luis R. Rodriguez8c77a562009-09-09 21:02:34 -070081{
82 unsigned long flags;
83 bool ret;
84
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -070085 spin_lock_irqsave(&sc->sc_pm_lock, flags);
86 ret = ath9k_hw_setpower(sc->sc_ah, mode);
87 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Luis R. Rodriguez8c77a562009-09-09 21:02:34 -070088
89 return ret;
90}
91
Felix Fietkaubf3dac52013-11-11 22:23:33 +010092void ath_ps_full_sleep(unsigned long data)
93{
94 struct ath_softc *sc = (struct ath_softc *) data;
95 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
96 bool reset;
97
98 spin_lock(&common->cc_lock);
99 ath_hw_cycle_counters_update(common);
100 spin_unlock(&common->cc_lock);
101
102 ath9k_hw_setrxabort(sc->sc_ah, 1);
103 ath9k_hw_stopdmarecv(sc->sc_ah, &reset);
104
105 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
106}
107
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700108void ath9k_ps_wakeup(struct ath_softc *sc)
109{
Felix Fietkau898c9142010-10-12 14:02:53 +0200110 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700111 unsigned long flags;
Felix Fietkaufbb078f2010-11-03 01:36:51 +0100112 enum ath9k_power_mode power_mode;
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700113
114 spin_lock_irqsave(&sc->sc_pm_lock, flags);
115 if (++sc->ps_usecount != 1)
116 goto unlock;
117
Felix Fietkaubf3dac52013-11-11 22:23:33 +0100118 del_timer_sync(&sc->sleep_timer);
Felix Fietkaufbb078f2010-11-03 01:36:51 +0100119 power_mode = sc->sc_ah->power_mode;
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -0700120 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700121
Felix Fietkau898c9142010-10-12 14:02:53 +0200122 /*
123 * While the hardware is asleep, the cycle counters contain no
124 * useful data. Better clear them now so that they don't mess up
125 * survey data results.
126 */
Felix Fietkaufbb078f2010-11-03 01:36:51 +0100127 if (power_mode != ATH9K_PM_AWAKE) {
128 spin_lock(&common->cc_lock);
129 ath_hw_cycle_counters_update(common);
130 memset(&common->cc_survey, 0, sizeof(common->cc_survey));
Rajkumar Manoharanc9ae6ab2012-06-04 16:28:41 +0530131 memset(&common->cc_ani, 0, sizeof(common->cc_ani));
Felix Fietkaufbb078f2010-11-03 01:36:51 +0100132 spin_unlock(&common->cc_lock);
133 }
Felix Fietkau898c9142010-10-12 14:02:53 +0200134
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700135 unlock:
136 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
137}
138
139void ath9k_ps_restore(struct ath_softc *sc)
140{
Felix Fietkau898c9142010-10-12 14:02:53 +0200141 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkauc6c539f2011-09-14 21:24:24 +0200142 enum ath9k_power_mode mode;
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700143 unsigned long flags;
144
145 spin_lock_irqsave(&sc->sc_pm_lock, flags);
146 if (--sc->ps_usecount != 0)
147 goto unlock;
148
Sujith Manoharanad128862012-04-24 10:23:20 +0530149 if (sc->ps_idle) {
Felix Fietkaubf3dac52013-11-11 22:23:33 +0100150 mod_timer(&sc->sleep_timer, jiffies + HZ / 10);
151 goto unlock;
152 }
153
154 if (sc->ps_enabled &&
Sujith Manoharanad128862012-04-24 10:23:20 +0530155 !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
156 PS_WAIT_FOR_CAB |
157 PS_WAIT_FOR_PSPOLL_DATA |
Rajkumar Manoharan424749c2012-10-10 23:03:02 +0530158 PS_WAIT_FOR_TX_ACK |
159 PS_WAIT_FOR_ANI))) {
Felix Fietkauc6c539f2011-09-14 21:24:24 +0200160 mode = ATH9K_PM_NETWORK_SLEEP;
Rajkumar Manoharan08d4df42012-07-01 19:53:54 +0530161 if (ath9k_hw_btcoex_is_enabled(sc->sc_ah))
162 ath9k_btcoex_stop_gen_timer(sc);
Sujith Manoharanad128862012-04-24 10:23:20 +0530163 } else {
Felix Fietkauc6c539f2011-09-14 21:24:24 +0200164 goto unlock;
Sujith Manoharanad128862012-04-24 10:23:20 +0530165 }
Felix Fietkauc6c539f2011-09-14 21:24:24 +0200166
167 spin_lock(&common->cc_lock);
168 ath_hw_cycle_counters_update(common);
169 spin_unlock(&common->cc_lock);
170
Felix Fietkau1a8f0d392011-09-22 08:04:32 -0600171 ath9k_hw_setpower(sc->sc_ah, mode);
Luis R. Rodrigueza91d75ae2009-09-09 20:29:18 -0700172
173 unlock:
174 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
175}
176
Felix Fietkau9adcf442011-09-03 01:40:26 +0200177static void __ath_cancel_work(struct ath_softc *sc)
178{
179 cancel_work_sync(&sc->paprd_work);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200180 cancel_delayed_work_sync(&sc->tx_complete_work);
181 cancel_delayed_work_sync(&sc->hw_pll_work);
Sujith Manoharanfad29cd2012-06-25 13:54:22 +0530182
Sujith Manoharanbf525922012-06-27 14:15:59 +0530183#ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
Sujith Manoharanfad29cd2012-06-25 13:54:22 +0530184 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
185 cancel_work_sync(&sc->mci_work);
Sujith Manoharanbf525922012-06-27 14:15:59 +0530186#endif
Felix Fietkau9adcf442011-09-03 01:40:26 +0200187}
188
Sujith Manoharane60001e2013-10-28 12:22:04 +0530189void ath_cancel_work(struct ath_softc *sc)
Felix Fietkau9adcf442011-09-03 01:40:26 +0200190{
191 __ath_cancel_work(sc);
192 cancel_work_sync(&sc->hw_reset_work);
193}
194
Sujith Manoharane60001e2013-10-28 12:22:04 +0530195void ath_restart_work(struct ath_softc *sc)
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530196{
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530197 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
198
Sujith Manoharan19c36162013-08-20 10:05:59 +0530199 if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530200 ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
201 msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
202
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530203 ath_start_ani(sc);
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530204}
205
John W. Linville9ebea382013-01-28 13:54:03 -0500206static bool ath_prepare_reset(struct ath_softc *sc)
Felix Fietkau9adcf442011-09-03 01:40:26 +0200207{
208 struct ath_hw *ah = sc->sc_ah;
Felix Fietkauceea2a52012-05-24 14:32:19 +0200209 bool ret = true;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200210
211 ieee80211_stop_queues(sc->hw);
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530212 ath_stop_ani(sc);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200213 ath9k_hw_disable_interrupts(ah);
214
Felix Fietkau13815592013-01-20 18:51:53 +0100215 if (!ath_drain_all_txq(sc))
Felix Fietkau9adcf442011-09-03 01:40:26 +0200216 ret = false;
217
Felix Fietkau0a62acb2013-01-20 18:51:52 +0100218 if (!ath_stoprecv(sc))
Felix Fietkauceea2a52012-05-24 14:32:19 +0200219 ret = false;
220
Felix Fietkau9adcf442011-09-03 01:40:26 +0200221 return ret;
222}
223
224static bool ath_complete_reset(struct ath_softc *sc, bool start)
225{
226 struct ath_hw *ah = sc->sc_ah;
227 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharan196fb862012-06-04 20:24:13 +0530228 unsigned long flags;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200229
230 if (ath_startrecv(sc) != 0) {
231 ath_err(common, "Unable to restart recv logic\n");
232 return false;
233 }
234
235 ath9k_cmn_update_txpow(ah, sc->curtxpow,
Felix Fietkaubc7e1be2014-06-11 16:17:50 +0530236 sc->cur_chan->txpower, &sc->curtxpow);
Sujith Manoharanb74713d2012-06-04 20:24:01 +0530237
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100238 clear_bit(ATH_OP_HW_RESET, &common->op_flags);
Felix Fietkau72d874c2011-10-08 20:06:19 +0200239 ath9k_hw_set_interrupts(ah);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200240 ath9k_hw_enable_interrupts(ah);
241
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530242 if (!sc->cur_chan->offchannel && start) {
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100243 if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
Sujith Manoharan196fb862012-06-04 20:24:13 +0530244 goto work;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200245
Sujith Manoharan196fb862012-06-04 20:24:13 +0530246 if (ah->opmode == NL80211_IFTYPE_STATION &&
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100247 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
Sujith Manoharan196fb862012-06-04 20:24:13 +0530248 spin_lock_irqsave(&sc->sc_pm_lock, flags);
249 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
250 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Sujith Manoharana6768282013-05-06 10:09:03 +0530251 } else {
252 ath9k_set_beacon(sc);
Sujith Manoharan196fb862012-06-04 20:24:13 +0530253 }
254 work:
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530255 ath_restart_work(sc);
Felix Fietkau04535312014-06-11 16:17:51 +0530256 ath_txq_schedule_all(sc);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200257 }
258
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530259 sc->gtt_cnt = 0;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200260 ieee80211_wake_queues(sc->hw);
261
Felix Fietkaud463af42014-04-06 00:37:03 +0200262 ath9k_p2p_ps_timer(sc);
263
Felix Fietkau9adcf442011-09-03 01:40:26 +0200264 return true;
265}
266
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530267int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
Felix Fietkau9adcf442011-09-03 01:40:26 +0200268{
269 struct ath_hw *ah = sc->sc_ah;
270 struct ath_common *common = ath9k_hw_common(ah);
271 struct ath9k_hw_cal_data *caldata = NULL;
272 bool fastcc = true;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200273 int r;
274
275 __ath_cancel_work(sc);
276
Felix Fietkau4668cce2013-01-14 16:56:46 +0100277 tasklet_disable(&sc->intr_tq);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200278 spin_lock_bh(&sc->sc_pcu_lock);
279
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530280 if (!sc->cur_chan->offchannel) {
Felix Fietkau9adcf442011-09-03 01:40:26 +0200281 fastcc = false;
282 caldata = &sc->caldata;
283 }
284
285 if (!hchan) {
286 fastcc = false;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200287 hchan = ah->curchan;
288 }
289
John W. Linville9ebea382013-01-28 13:54:03 -0500290 if (!ath_prepare_reset(sc))
Felix Fietkau9adcf442011-09-03 01:40:26 +0200291 fastcc = false;
292
Felix Fietkaubff11762014-06-11 16:17:52 +0530293 if (hchan) {
294 spin_lock_bh(&sc->chan_lock);
295 sc->cur_chandef = sc->cur_chan->chandef;
296 spin_unlock_bh(&sc->chan_lock);
297 }
298
Joe Perchesd2182b62011-12-15 14:55:53 -0800299 ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
Sujith Manoharanfeced202012-01-30 14:21:42 +0530300 hchan->channel, IS_CHAN_HT40(hchan), fastcc);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200301
302 r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
303 if (r) {
304 ath_err(common,
305 "Unable to reset channel, reset status %d\n", r);
Robert Shadef50b1cd2013-04-02 19:52:45 -0400306
307 ath9k_hw_enable_interrupts(ah);
308 ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
309
Felix Fietkau9adcf442011-09-03 01:40:26 +0200310 goto out;
311 }
312
Rajkumar Manoharane82cb032012-10-12 14:07:25 +0530313 if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530314 sc->cur_chan->offchannel)
Rajkumar Manoharane82cb032012-10-12 14:07:25 +0530315 ath9k_mci_set_txpower(sc, true, false);
316
Felix Fietkau9adcf442011-09-03 01:40:26 +0200317 if (!ath_complete_reset(sc, true))
318 r = -EIO;
319
320out:
321 spin_unlock_bh(&sc->sc_pcu_lock);
Felix Fietkau4668cce2013-01-14 16:56:46 +0100322 tasklet_enable(&sc->intr_tq);
323
Felix Fietkau9adcf442011-09-03 01:40:26 +0200324 return r;
325}
326
Ben Greear7e1e3862011-11-03 11:33:13 -0700327static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
328 struct ieee80211_vif *vif)
Sujithff37e332008-11-24 12:07:55 +0530329{
330 struct ath_node *an;
Sujithff37e332008-11-24 12:07:55 +0530331 an = (struct ath_node *)sta->drv_priv;
332
Sujith Manoharana145daf2012-11-28 15:08:54 +0530333 an->sc = sc;
Ben Greear7f010c92011-01-09 23:11:49 -0800334 an->sta = sta;
Ben Greear7e1e3862011-11-03 11:33:13 -0700335 an->vif = vif;
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +0530336 memset(&an->key_idx, 0, sizeof(an->key_idx));
Sujith Manoharan3d4e20f2012-03-14 14:40:58 +0530337
Sujith Manoharandd5ee592013-02-04 15:38:23 +0530338 ath_tx_node_init(sc, an);
Sujithff37e332008-11-24 12:07:55 +0530339}
340
341static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
342{
343 struct ath_node *an = (struct ath_node *)sta->drv_priv;
Sujith Manoharandd5ee592013-02-04 15:38:23 +0530344 ath_tx_node_cleanup(sc, an);
Sujithff37e332008-11-24 12:07:55 +0530345}
346
Sujith55624202010-01-08 10:36:02 +0530347void ath9k_tasklet(unsigned long data)
Sujithff37e332008-11-24 12:07:55 +0530348{
349 struct ath_softc *sc = (struct ath_softc *)data;
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700350 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700351 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530352 enum ath_reset_type type;
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530353 unsigned long flags;
Sujith17d79042009-02-09 13:27:03 +0530354 u32 status = sc->intrstatus;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400355 u32 rxmask;
Sujithff37e332008-11-24 12:07:55 +0530356
Felix Fietkaue3927002011-09-14 21:23:01 +0200357 ath9k_ps_wakeup(sc);
358 spin_lock(&sc->sc_pcu_lock);
359
Sujith Manoharan6549a862013-12-24 10:44:24 +0530360 if (status & ATH9K_INT_FATAL) {
361 type = RESET_TYPE_FATAL_INT;
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530362 ath9k_queue_reset(sc, type);
Sujith Manoharanc6cc47b2013-09-16 10:37:00 +0530363
364 /*
365 * Increment the ref. counter here so that
366 * interrupts are enabled in the reset routine.
367 */
368 atomic_inc(&ah->intr_ref_cnt);
Felix Fietkauaffad452014-02-22 14:52:49 +0100369 ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
Felix Fietkaue3927002011-09-14 21:23:01 +0200370 goto out;
Sujithff37e332008-11-24 12:07:55 +0530371 }
372
Sujith Manoharan6549a862013-12-24 10:44:24 +0530373 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
374 (status & ATH9K_INT_BB_WATCHDOG)) {
Sujith Manoharan0c759972013-12-24 10:44:26 +0530375 spin_lock(&common->cc_lock);
376 ath_hw_cycle_counters_update(common);
377 ar9003_hw_bb_watchdog_dbg_info(ah);
378 spin_unlock(&common->cc_lock);
379
Sujith Manoharan6549a862013-12-24 10:44:24 +0530380 if (ar9003_hw_bb_watchdog_check(ah)) {
381 type = RESET_TYPE_BB_WATCHDOG;
382 ath9k_queue_reset(sc, type);
383
384 /*
385 * Increment the ref. counter here so that
386 * interrupts are enabled in the reset routine.
387 */
388 atomic_inc(&ah->intr_ref_cnt);
Felix Fietkauaffad452014-02-22 14:52:49 +0100389 ath_dbg(common, RESET,
Sujith Manoharan6549a862013-12-24 10:44:24 +0530390 "BB_WATCHDOG: Skipping interrupts\n");
391 goto out;
392 }
393 }
394
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530395 if (status & ATH9K_INT_GTT) {
396 sc->gtt_cnt++;
397
398 if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
399 type = RESET_TYPE_TX_GTT;
400 ath9k_queue_reset(sc, type);
401 atomic_inc(&ah->intr_ref_cnt);
Felix Fietkauaffad452014-02-22 14:52:49 +0100402 ath_dbg(common, RESET,
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530403 "GTT: Skipping interrupts\n");
404 goto out;
405 }
406 }
407
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530408 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Rajkumar Manoharan4105f802011-05-06 18:27:47 +0530409 if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
410 /*
411 * TSF sync does not look correct; remain awake to sync with
412 * the next Beacon.
413 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800414 ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
Rajkumar Manoharane8fe7332011-08-05 18:59:41 +0530415 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
Rajkumar Manoharan4105f802011-05-06 18:27:47 +0530416 }
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530417 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Rajkumar Manoharan4105f802011-05-06 18:27:47 +0530418
Felix Fietkaub5c804752010-04-15 17:38:48 -0400419 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
420 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
421 ATH9K_INT_RXORN);
422 else
423 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
424
425 if (status & rxmask) {
Felix Fietkaub5c804752010-04-15 17:38:48 -0400426 /* Check for high priority Rx first */
427 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
428 (status & ATH9K_INT_RXHP))
429 ath_rx_tasklet(sc, 0, true);
430
431 ath_rx_tasklet(sc, 0, false);
Sujith063d8be2009-03-30 15:28:49 +0530432 }
433
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400434 if (status & ATH9K_INT_TX) {
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530435 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
436 /*
437 * For EDMA chips, TX completion is enabled for the
438 * beacon queue, so if a beacon has been transmitted
439 * successfully after a GTT interrupt, the GTT counter
440 * gets reset to zero here.
441 */
Sujith Manoharan3b745c72014-01-20 13:25:28 +0530442 sc->gtt_cnt = 0;
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530443
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400444 ath_tx_edma_tasklet(sc);
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530445 } else {
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400446 ath_tx_tasklet(sc);
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530447 }
Felix Fietkau10e23182013-11-11 22:23:35 +0100448
449 wake_up(&sc->tx_wait);
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400450 }
Sujith063d8be2009-03-30 15:28:49 +0530451
Felix Fietkauc67ce332013-12-14 18:03:38 +0100452 if (status & ATH9K_INT_GENTIMER)
453 ath_gen_timer_isr(sc->sc_ah);
454
Sujith Manoharan56ca0db2012-02-22 12:40:32 +0530455 ath9k_btcoex_handle_interrupt(sc, status);
Mohammed Shafi Shajakhan19686dd2011-11-30 10:41:28 +0530456
Sujithff37e332008-11-24 12:07:55 +0530457 /* re-enable hardware interrupt */
Felix Fietkau4df30712010-11-08 20:54:47 +0100458 ath9k_hw_enable_interrupts(ah);
Sujith Manoharanc6cc47b2013-09-16 10:37:00 +0530459out:
Senthil Balasubramanian52671e42010-12-23 21:06:57 +0530460 spin_unlock(&sc->sc_pcu_lock);
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400461 ath9k_ps_restore(sc);
Sujithff37e332008-11-24 12:07:55 +0530462}
463
Gabor Juhos6baff7f2009-01-14 20:17:06 +0100464irqreturn_t ath_isr(int irq, void *dev)
Sujithff37e332008-11-24 12:07:55 +0530465{
Sujith063d8be2009-03-30 15:28:49 +0530466#define SCHED_INTR ( \
467 ATH9K_INT_FATAL | \
Rajkumar Manoharana4d86d92011-05-20 17:52:10 +0530468 ATH9K_INT_BB_WATCHDOG | \
Sujith063d8be2009-03-30 15:28:49 +0530469 ATH9K_INT_RXORN | \
470 ATH9K_INT_RXEOL | \
471 ATH9K_INT_RX | \
Felix Fietkaub5c804752010-04-15 17:38:48 -0400472 ATH9K_INT_RXLP | \
473 ATH9K_INT_RXHP | \
Sujith063d8be2009-03-30 15:28:49 +0530474 ATH9K_INT_TX | \
475 ATH9K_INT_BMISS | \
476 ATH9K_INT_CST | \
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530477 ATH9K_INT_GTT | \
Vasanthakumar Thiagarajanebb8e1d2009-09-01 17:46:32 +0530478 ATH9K_INT_TSFOOR | \
Mohammed Shafi Shajakhan40dc5392011-11-30 10:41:18 +0530479 ATH9K_INT_GENTIMER | \
480 ATH9K_INT_MCI)
Sujith063d8be2009-03-30 15:28:49 +0530481
Sujithff37e332008-11-24 12:07:55 +0530482 struct ath_softc *sc = dev;
Sujithcbe61d82009-02-09 13:27:12 +0530483 struct ath_hw *ah = sc->sc_ah;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100484 struct ath_common *common = ath9k_hw_common(ah);
Sujithff37e332008-11-24 12:07:55 +0530485 enum ath9k_int status;
Sujith Manoharan78c8a952013-12-28 09:47:15 +0530486 u32 sync_cause = 0;
Sujithff37e332008-11-24 12:07:55 +0530487 bool sched = false;
488
Sujith063d8be2009-03-30 15:28:49 +0530489 /*
490 * The hardware is not ready/present, don't
491 * touch anything. Note this can happen early
492 * on if the IRQ is shared.
493 */
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100494 if (test_bit(ATH_OP_INVALID, &common->op_flags))
Sujith063d8be2009-03-30 15:28:49 +0530495 return IRQ_NONE;
Sujithff37e332008-11-24 12:07:55 +0530496
Sujith063d8be2009-03-30 15:28:49 +0530497 /* shared irq, not for us */
Sujithff37e332008-11-24 12:07:55 +0530498
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400499 if (!ath9k_hw_intrpend(ah))
Sujith063d8be2009-03-30 15:28:49 +0530500 return IRQ_NONE;
Sujithff37e332008-11-24 12:07:55 +0530501
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100502 if (test_bit(ATH_OP_HW_RESET, &common->op_flags)) {
Felix Fietkauf41a9b32012-08-08 16:25:03 +0200503 ath9k_hw_kill_interrupts(ah);
Sujith Manoharanb74713d2012-06-04 20:24:01 +0530504 return IRQ_HANDLED;
Felix Fietkauf41a9b32012-08-08 16:25:03 +0200505 }
Sujith Manoharanb74713d2012-06-04 20:24:01 +0530506
Sujith063d8be2009-03-30 15:28:49 +0530507 /*
508 * Figure out the reason(s) for the interrupt. Note
509 * that the hal returns a pseudo-ISR that may include
510 * bits we haven't explicitly enabled so we mask the
511 * value to insure we only process bits we requested.
512 */
Felix Fietkau6a4d05d2013-12-19 18:01:48 +0100513 ath9k_hw_getisr(ah, &status, &sync_cause); /* NB: clears ISR too */
514 ath9k_debug_sync_cause(sc, sync_cause);
Pavel Roskin30691682010-03-31 18:05:31 -0400515 status &= ah->imask; /* discard unasked-for bits */
Sujith063d8be2009-03-30 15:28:49 +0530516
517 /*
518 * If there are no status bits set, then this interrupt was not
519 * for me (should have been caught above).
520 */
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400521 if (!status)
Sujith063d8be2009-03-30 15:28:49 +0530522 return IRQ_NONE;
Sujith063d8be2009-03-30 15:28:49 +0530523
524 /* Cache the status */
525 sc->intrstatus = status;
526
527 if (status & SCHED_INTR)
528 sched = true;
529
530 /*
531 * If a FATAL or RXORN interrupt is received, we have to reset the
532 * chip immediately.
533 */
Felix Fietkaub5c804752010-04-15 17:38:48 -0400534 if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
535 !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
Sujith063d8be2009-03-30 15:28:49 +0530536 goto chip_reset;
537
Sujith Manoharana6bb8602013-12-24 10:44:22 +0530538 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
Sujith Manoharan0c759972013-12-24 10:44:26 +0530539 (status & ATH9K_INT_BB_WATCHDOG))
Luis R. Rodriguez08578b82010-05-13 13:33:44 -0400540 goto chip_reset;
Sujith Manoharane60001e2013-10-28 12:22:04 +0530541
542#ifdef CONFIG_ATH9K_WOW
Rajkumar Manoharanca90ef42012-11-20 18:29:59 +0530543 if (status & ATH9K_INT_BMISS) {
544 if (atomic_read(&sc->wow_sleep_proc_intr) == 0) {
Rajkumar Manoharanca90ef42012-11-20 18:29:59 +0530545 atomic_inc(&sc->wow_got_bmiss_intr);
546 atomic_dec(&sc->wow_sleep_proc_intr);
547 }
548 }
549#endif
Sujith Manoharane60001e2013-10-28 12:22:04 +0530550
Sujith063d8be2009-03-30 15:28:49 +0530551 if (status & ATH9K_INT_SWBA)
552 tasklet_schedule(&sc->bcon_tasklet);
553
554 if (status & ATH9K_INT_TXURN)
555 ath9k_hw_updatetxtriglevel(ah, true);
556
Rajkumar Manoharan0682c9b2011-08-13 10:28:09 +0530557 if (status & ATH9K_INT_RXEOL) {
558 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +0200559 ath9k_hw_set_interrupts(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400560 }
561
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400562 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
563 if (status & ATH9K_INT_TIM_TIMER) {
Luis R. Rodriguezff9f0b62010-12-07 15:13:22 -0800564 if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
565 goto chip_reset;
Sujith063d8be2009-03-30 15:28:49 +0530566 /* Clear RxAbort bit so that we can
567 * receive frames */
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -0700568 ath9k_setpower(sc, ATH9K_PM_AWAKE);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530569 spin_lock(&sc->sc_pm_lock);
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400570 ath9k_hw_setrxabort(sc->sc_ah, 0);
Sujith1b04b932010-01-08 10:36:05 +0530571 sc->ps_flags |= PS_WAIT_FOR_BEACON;
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530572 spin_unlock(&sc->sc_pm_lock);
Sujith063d8be2009-03-30 15:28:49 +0530573 }
Sujith063d8be2009-03-30 15:28:49 +0530574
575chip_reset:
576
Sujith817e11d2008-12-07 21:42:44 +0530577 ath_debug_stat_interrupt(sc, status);
578
Sujithff37e332008-11-24 12:07:55 +0530579 if (sched) {
Felix Fietkau4df30712010-11-08 20:54:47 +0100580 /* turn off every interrupt */
581 ath9k_hw_disable_interrupts(ah);
Sujithff37e332008-11-24 12:07:55 +0530582 tasklet_schedule(&sc->intr_tq);
583 }
584
585 return IRQ_HANDLED;
Sujith063d8be2009-03-30 15:28:49 +0530586
587#undef SCHED_INTR
Sujithff37e332008-11-24 12:07:55 +0530588}
589
Sujith Manoharanef6b19e2013-10-24 12:04:39 +0530590int ath_reset(struct ath_softc *sc)
Sujithff37e332008-11-24 12:07:55 +0530591{
Felix Fietkauec303262013-10-05 14:09:30 +0200592 int r;
Sujithff37e332008-11-24 12:07:55 +0530593
Felix Fietkau783cd012011-01-21 18:52:38 +0100594 ath9k_ps_wakeup(sc);
Felix Fietkau13815592013-01-20 18:51:53 +0100595 r = ath_reset_internal(sc, NULL);
Felix Fietkau783cd012011-01-21 18:52:38 +0100596 ath9k_ps_restore(sc);
Sujith2ab81d42009-12-14 16:34:56 +0530597
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -0800598 return r;
Sujithff37e332008-11-24 12:07:55 +0530599}
600
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530601void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
602{
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100603 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530604#ifdef CONFIG_ATH9K_DEBUGFS
605 RESET_STAT_INC(sc, type);
606#endif
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100607 set_bit(ATH_OP_HW_RESET, &common->op_flags);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530608 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
609}
610
Felix Fietkau236de512011-09-03 01:40:25 +0200611void ath_reset_work(struct work_struct *work)
612{
613 struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
614
Felix Fietkau13815592013-01-20 18:51:53 +0100615 ath_reset(sc);
Felix Fietkau236de512011-09-03 01:40:25 +0200616}
617
Sujithff37e332008-11-24 12:07:55 +0530618/**********************/
619/* mac80211 callbacks */
620/**********************/
621
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700622static int ath9k_start(struct ieee80211_hw *hw)
623{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100624 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700625 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700626 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau39305632014-06-11 16:17:57 +0530627 struct ieee80211_channel *curchan = sc->cur_chan->chandef.chan;
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530628 struct ath_chanctx *ctx = sc->cur_chan;
Sujithff37e332008-11-24 12:07:55 +0530629 struct ath9k_channel *init_channel;
Vasanthakumar Thiagarajan82880a72009-06-13 14:50:24 +0530630 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700631
Joe Perchesd2182b62011-12-15 14:55:53 -0800632 ath_dbg(common, CONFIG,
Joe Perches226afe62010-12-02 19:12:37 -0800633 "Starting driver with initial channel: %d MHz\n",
634 curchan->center_freq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700635
Felix Fietkauf62d8162011-03-25 17:43:41 +0100636 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +0530637 mutex_lock(&sc->mutex);
638
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530639 init_channel = ath9k_cmn_get_channel(hw, ah, &ctx->chandef);
Felix Fietkaubff11762014-06-11 16:17:52 +0530640 sc->cur_chandef = hw->conf.chandef;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641
Sujithff37e332008-11-24 12:07:55 +0530642 /* Reset SERDES registers */
Stanislaw Gruszka84c87dc2011-08-05 13:10:32 +0200643 ath9k_hw_configpcipowersave(ah, false);
Sujithff37e332008-11-24 12:07:55 +0530644
645 /*
646 * The basic interface to setting the hardware in a good
647 * state is ``reset''. On return the hardware is known to
648 * be powered up and with interrupts disabled. This must
649 * be followed by initialization of the appropriate bits
650 * and then setup of the interrupt mask.
651 */
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700652 spin_lock_bh(&sc->sc_pcu_lock);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100653
654 atomic_set(&ah->intr_ref_cnt, -1);
655
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200656 r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -0800657 if (r) {
Joe Perches38002762010-12-02 19:12:36 -0800658 ath_err(common,
659 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
660 r, curchan->center_freq);
Felix Fietkauceb26a62012-10-03 21:07:51 +0200661 ah->reset_power_on = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700662 }
Sujithff37e332008-11-24 12:07:55 +0530663
Sujithff37e332008-11-24 12:07:55 +0530664 /* Setup our intr mask. */
Felix Fietkaub5c804752010-04-15 17:38:48 -0400665 ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
666 ATH9K_INT_RXORN | ATH9K_INT_FATAL |
667 ATH9K_INT_GLOBAL;
668
669 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
Luis R. Rodriguez08578b82010-05-13 13:33:44 -0400670 ah->imask |= ATH9K_INT_RXHP |
Sujith Manoharana6bb8602013-12-24 10:44:22 +0530671 ATH9K_INT_RXLP;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400672 else
673 ah->imask |= ATH9K_INT_RX;
Sujithff37e332008-11-24 12:07:55 +0530674
Sujith Manoharana6bb8602013-12-24 10:44:22 +0530675 if (ah->config.hw_hang_checks & HW_BB_WATCHDOG)
676 ah->imask |= ATH9K_INT_BB_WATCHDOG;
677
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530678 /*
679 * Enable GTT interrupts only for AR9003/AR9004 chips
680 * for now.
681 */
682 if (AR_SREV_9300_20_OR_LATER(ah))
683 ah->imask |= ATH9K_INT_GTT;
Sujithff37e332008-11-24 12:07:55 +0530684
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700685 if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
Pavel Roskin30691682010-03-31 18:05:31 -0400686 ah->imask |= ATH9K_INT_CST;
Sujithff37e332008-11-24 12:07:55 +0530687
Sujith Manoharane270e772012-06-04 16:27:19 +0530688 ath_mci_enable(sc);
Mohammed Shafi Shajakhan40dc5392011-11-30 10:41:18 +0530689
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100690 clear_bit(ATH_OP_INVALID, &common->op_flags);
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +0530691 sc->sc_ah->is_monitoring = false;
Sujithff37e332008-11-24 12:07:55 +0530692
Felix Fietkauceb26a62012-10-03 21:07:51 +0200693 if (!ath_complete_reset(sc, false))
694 ah->reset_power_on = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700695
Felix Fietkauc0c11742011-11-16 13:08:41 +0100696 if (ah->led_pin >= 0) {
697 ath9k_hw_cfg_output(ah, ah->led_pin,
698 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
699 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
700 }
701
702 /*
703 * Reset key cache to sane defaults (all entries cleared) instead of
704 * semi-random values after suspend/resume.
705 */
706 ath9k_cmn_init_crypto(sc->sc_ah);
707
Felix Fietkaua35051c2013-12-14 18:03:45 +0100708 ath9k_hw_reset_tsf(ah);
709
Felix Fietkau9adcf442011-09-03 01:40:26 +0200710 spin_unlock_bh(&sc->sc_pcu_lock);
Senthil Balasubramanian164ace32009-07-14 20:17:09 -0400711
Sujith141b38b2009-02-04 08:10:07 +0530712 mutex_unlock(&sc->mutex);
713
Felix Fietkauf62d8162011-03-25 17:43:41 +0100714 ath9k_ps_restore(sc);
715
Felix Fietkauceb26a62012-10-03 21:07:51 +0200716 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700717}
718
Thomas Huehn36323f82012-07-23 21:33:42 +0200719static void ath9k_tx(struct ieee80211_hw *hw,
720 struct ieee80211_tx_control *control,
721 struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700722{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100723 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700724 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith528f0c62008-10-29 10:14:26 +0530725 struct ath_tx_control txctl;
Benoit Papillault1bc14882009-11-24 15:49:18 +0100726 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530727 unsigned long flags;
Sujith528f0c62008-10-29 10:14:26 +0530728
Gabor Juhos96148322009-07-24 17:27:21 +0200729 if (sc->ps_enabled) {
Jouni Malinendc8c4582009-05-19 17:01:42 +0300730 /*
731 * mac80211 does not set PM field for normal data frames, so we
732 * need to update that based on the current PS mode.
733 */
734 if (ieee80211_is_data(hdr->frame_control) &&
735 !ieee80211_is_nullfunc(hdr->frame_control) &&
736 !ieee80211_has_pm(hdr->frame_control)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800737 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800738 "Add PM=1 for a TX frame while in PS mode\n");
Jouni Malinendc8c4582009-05-19 17:01:42 +0300739 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
740 }
741 }
742
Sujith Manoharanad128862012-04-24 10:23:20 +0530743 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300744 /*
745 * We are using PS-Poll and mac80211 can request TX while in
746 * power save mode. Need to wake up hardware for the TX to be
747 * completed and if needed, also for RX of buffered frames.
748 */
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300749 ath9k_ps_wakeup(sc);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530750 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Vasanthakumar Thiagarajanfdf76622010-05-17 18:57:55 -0700751 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
752 ath9k_hw_setrxabort(sc->sc_ah, 0);
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300753 if (ieee80211_is_pspoll(hdr->frame_control)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800754 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800755 "Sending PS-Poll to pick a buffered frame\n");
Sujith1b04b932010-01-08 10:36:05 +0530756 sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300757 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -0800758 ath_dbg(common, PS, "Wake up to complete TX\n");
Sujith1b04b932010-01-08 10:36:05 +0530759 sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300760 }
761 /*
762 * The actual restore operation will happen only after
Sujith Manoharanad128862012-04-24 10:23:20 +0530763 * the ps_flags bit is cleared. We are just dropping
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300764 * the ps_usecount here.
765 */
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530766 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300767 ath9k_ps_restore(sc);
768 }
769
Sujith Manoharanad128862012-04-24 10:23:20 +0530770 /*
771 * Cannot tx while the hardware is in full sleep, it first needs a full
772 * chip reset to recover from that
773 */
774 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
775 ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
776 goto exit;
777 }
778
Sujith528f0c62008-10-29 10:14:26 +0530779 memset(&txctl, 0, sizeof(struct ath_tx_control));
Felix Fietkau066dae92010-11-07 14:59:39 +0100780 txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
Thomas Huehn36323f82012-07-23 21:33:42 +0200781 txctl.sta = control->sta;
Sujith528f0c62008-10-29 10:14:26 +0530782
Joe Perchesd2182b62011-12-15 14:55:53 -0800783 ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700784
Jouni Malinenc52f33d2009-03-03 19:23:29 +0200785 if (ath_tx_start(hw, skb, &txctl) != 0) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800786 ath_dbg(common, XMIT, "TX failed\n");
Ben Greeara5a0bca2012-04-03 09:16:55 -0700787 TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
Sujith528f0c62008-10-29 10:14:26 +0530788 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700789 }
790
Johannes Berg7bb45682011-02-24 14:42:06 +0100791 return;
Sujith528f0c62008-10-29 10:14:26 +0530792exit:
Felix Fietkau249ee722012-10-03 21:07:52 +0200793 ieee80211_free_txskb(hw, skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700794}
795
796static void ath9k_stop(struct ieee80211_hw *hw)
797{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100798 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700799 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700800 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100801 bool prev_idle;
Sujith9c84b792008-10-29 10:17:13 +0530802
Felix Fietkaubff11762014-06-11 16:17:52 +0530803 cancel_work_sync(&sc->chanctx_work);
Sujith4c483812009-08-18 10:51:52 +0530804 mutex_lock(&sc->mutex);
805
Felix Fietkau9adcf442011-09-03 01:40:26 +0200806 ath_cancel_work(sc);
Luis R. Rodriguezc94dbff2009-07-27 11:53:04 -0700807
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100808 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800809 ath_dbg(common, ANY, "Device not present\n");
Sujith4c483812009-08-18 10:51:52 +0530810 mutex_unlock(&sc->mutex);
Sujith9c84b792008-10-29 10:17:13 +0530811 return;
812 }
813
Sujith3867cf62009-12-23 20:03:27 -0500814 /* Ensure HW is awake when we try to shut it down. */
815 ath9k_ps_wakeup(sc);
816
Luis R. Rodriguez6a6733f2010-10-26 15:27:25 -0700817 spin_lock_bh(&sc->sc_pcu_lock);
818
Stanislaw Gruszka203043f2011-01-25 14:08:40 +0100819 /* prevent tasklets to enable interrupts once we disable them */
820 ah->imask &= ~ATH9K_INT_GLOBAL;
821
Sujithff37e332008-11-24 12:07:55 +0530822 /* make sure h/w will not generate any interrupt
823 * before setting the invalid flag. */
Felix Fietkau4df30712010-11-08 20:54:47 +0100824 ath9k_hw_disable_interrupts(ah);
Sujithff37e332008-11-24 12:07:55 +0530825
Luis R. Rodriguez6a6733f2010-10-26 15:27:25 -0700826 spin_unlock_bh(&sc->sc_pcu_lock);
827
Stanislaw Gruszka203043f2011-01-25 14:08:40 +0100828 /* we can now sync irq and kill any running tasklets, since we already
829 * disabled interrupts and not holding a spin lock */
830 synchronize_irq(sc->irq);
831 tasklet_kill(&sc->intr_tq);
832 tasklet_kill(&sc->bcon_tasklet);
833
Felix Fietkauc0c11742011-11-16 13:08:41 +0100834 prev_idle = sc->ps_idle;
835 sc->ps_idle = true;
836
837 spin_lock_bh(&sc->sc_pcu_lock);
838
839 if (ah->led_pin >= 0) {
840 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
841 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
842 }
843
John W. Linville9ebea382013-01-28 13:54:03 -0500844 ath_prepare_reset(sc);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100845
846 if (sc->rx.frag) {
847 dev_kfree_skb_any(sc->rx.frag);
848 sc->rx.frag = NULL;
849 }
850
851 if (!ah->curchan)
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530852 ah->curchan = ath9k_cmn_get_channel(hw, ah,
853 &sc->cur_chan->chandef);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100854
855 ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
856 ath9k_hw_phy_disable(ah);
857
858 ath9k_hw_configpcipowersave(ah, true);
859
860 spin_unlock_bh(&sc->sc_pcu_lock);
861
Sujith3867cf62009-12-23 20:03:27 -0500862 ath9k_ps_restore(sc);
863
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100864 set_bit(ATH_OP_INVALID, &common->op_flags);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100865 sc->ps_idle = prev_idle;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700866
Sujith141b38b2009-02-04 08:10:07 +0530867 mutex_unlock(&sc->mutex);
868
Joe Perchesd2182b62011-12-15 14:55:53 -0800869 ath_dbg(common, CONFIG, "Driver halt\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700870}
871
Felix Fietkauc648ecb2013-10-11 23:31:00 +0200872static bool ath9k_uses_beacons(int type)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700873{
Ben Greear48014162011-01-15 19:13:48 +0000874 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200875 case NL80211_IFTYPE_AP:
Ben Greear48014162011-01-15 19:13:48 +0000876 case NL80211_IFTYPE_ADHOC:
Pat Erley9cb54122009-03-20 22:59:59 -0400877 case NL80211_IFTYPE_MESH_POINT:
Ben Greear48014162011-01-15 19:13:48 +0000878 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700879 default:
Ben Greear48014162011-01-15 19:13:48 +0000880 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700881 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700882}
883
Ben Greear48014162011-01-15 19:13:48 +0000884static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
885{
886 struct ath9k_vif_iter_data *iter_data = data;
887 int i;
888
Felix Fietkauab11bb22013-04-16 12:51:57 +0200889 if (iter_data->has_hw_macaddr) {
Ben Greear48014162011-01-15 19:13:48 +0000890 for (i = 0; i < ETH_ALEN; i++)
891 iter_data->mask[i] &=
892 ~(iter_data->hw_macaddr[i] ^ mac[i]);
Felix Fietkauab11bb22013-04-16 12:51:57 +0200893 } else {
894 memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
895 iter_data->has_hw_macaddr = true;
896 }
Ben Greear48014162011-01-15 19:13:48 +0000897
898 switch (vif->type) {
899 case NL80211_IFTYPE_AP:
900 iter_data->naps++;
901 break;
902 case NL80211_IFTYPE_STATION:
903 iter_data->nstations++;
904 break;
905 case NL80211_IFTYPE_ADHOC:
906 iter_data->nadhocs++;
907 break;
908 case NL80211_IFTYPE_MESH_POINT:
909 iter_data->nmeshes++;
910 break;
911 case NL80211_IFTYPE_WDS:
912 iter_data->nwds++;
913 break;
914 default:
Ben Greear48014162011-01-15 19:13:48 +0000915 break;
916 }
917}
918
Sujith Manoharan6dcc3442012-07-17 17:16:36 +0530919static void ath9k_sta_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
920{
921 struct ath_softc *sc = data;
922 struct ath_vif *avp = (void *)vif->drv_priv;
923
924 if (vif->type != NL80211_IFTYPE_STATION)
925 return;
926
927 if (avp->primary_sta_vif)
928 ath9k_set_assoc_state(sc, vif);
929}
930
Ben Greear48014162011-01-15 19:13:48 +0000931/* Called with sc->mutex held. */
932void ath9k_calculate_iter_data(struct ieee80211_hw *hw,
933 struct ieee80211_vif *vif,
934 struct ath9k_vif_iter_data *iter_data)
935{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100936 struct ath_softc *sc = hw->priv;
Ben Greear48014162011-01-15 19:13:48 +0000937 struct ath_hw *ah = sc->sc_ah;
938 struct ath_common *common = ath9k_hw_common(ah);
Ben Greear48014162011-01-15 19:13:48 +0000939
940 /*
Mathy Vanhoef657eb172013-11-28 12:21:45 +0100941 * Pick the MAC address of the first interface as the new hardware
942 * MAC address. The hardware will use it together with the BSSID mask
943 * when matching addresses.
Ben Greear48014162011-01-15 19:13:48 +0000944 */
945 memset(iter_data, 0, sizeof(*iter_data));
Ben Greear48014162011-01-15 19:13:48 +0000946 memset(&iter_data->mask, 0xff, ETH_ALEN);
947
948 if (vif)
949 ath9k_vif_iter(iter_data, vif->addr, vif);
950
951 /* Get list of all active MAC addresses */
Johannes Berg8b2c9822012-11-06 20:23:30 +0100952 ieee80211_iterate_active_interfaces_atomic(
953 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
954 ath9k_vif_iter, iter_data);
Felix Fietkauab11bb22013-04-16 12:51:57 +0200955
956 memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
Ben Greear48014162011-01-15 19:13:48 +0000957}
958
959/* Called with sc->mutex held. */
960static void ath9k_calculate_summary_state(struct ieee80211_hw *hw,
961 struct ieee80211_vif *vif)
962{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100963 struct ath_softc *sc = hw->priv;
Ben Greear48014162011-01-15 19:13:48 +0000964 struct ath_hw *ah = sc->sc_ah;
965 struct ath_common *common = ath9k_hw_common(ah);
966 struct ath9k_vif_iter_data iter_data;
Sujith Manoharan6dcc3442012-07-17 17:16:36 +0530967 enum nl80211_iftype old_opmode = ah->opmode;
Ben Greear48014162011-01-15 19:13:48 +0000968
969 ath9k_calculate_iter_data(hw, vif, &iter_data);
970
Ben Greear48014162011-01-15 19:13:48 +0000971 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
972 ath_hw_setbssidmask(common);
973
Ben Greear48014162011-01-15 19:13:48 +0000974 if (iter_data.naps > 0) {
Sujith Manoharan60ca9f82012-07-17 17:15:37 +0530975 ath9k_hw_set_tsfadjust(ah, true);
Ben Greear48014162011-01-15 19:13:48 +0000976 ah->opmode = NL80211_IFTYPE_AP;
977 } else {
Sujith Manoharan60ca9f82012-07-17 17:15:37 +0530978 ath9k_hw_set_tsfadjust(ah, false);
Ben Greear48014162011-01-15 19:13:48 +0000979
Javier Cardonafd5999c2011-05-03 16:57:19 -0700980 if (iter_data.nmeshes)
981 ah->opmode = NL80211_IFTYPE_MESH_POINT;
982 else if (iter_data.nwds)
Ben Greear48014162011-01-15 19:13:48 +0000983 ah->opmode = NL80211_IFTYPE_AP;
984 else if (iter_data.nadhocs)
985 ah->opmode = NL80211_IFTYPE_ADHOC;
986 else
987 ah->opmode = NL80211_IFTYPE_STATION;
988 }
989
Sujith Manoharandf35d292012-07-17 17:15:43 +0530990 ath9k_hw_setopmode(ah);
991
Felix Fietkau198823f2012-06-15 15:25:25 +0200992 if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
Ben Greear48014162011-01-15 19:13:48 +0000993 ah->imask |= ATH9K_INT_TSFOOR;
Felix Fietkau198823f2012-06-15 15:25:25 +0200994 else
Ben Greear48014162011-01-15 19:13:48 +0000995 ah->imask &= ~ATH9K_INT_TSFOOR;
Ben Greear48014162011-01-15 19:13:48 +0000996
Felix Fietkau72d874c2011-10-08 20:06:19 +0200997 ath9k_hw_set_interrupts(ah);
Sujith Manoharan6dcc3442012-07-17 17:16:36 +0530998
999 /*
1000 * If we are changing the opmode to STATION,
1001 * a beacon sync needs to be done.
1002 */
1003 if (ah->opmode == NL80211_IFTYPE_STATION &&
1004 old_opmode == NL80211_IFTYPE_AP &&
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001005 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
Johannes Berg8b2c9822012-11-06 20:23:30 +01001006 ieee80211_iterate_active_interfaces_atomic(
1007 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1008 ath9k_sta_vif_iter, sc);
Sujith Manoharan6dcc3442012-07-17 17:16:36 +05301009 }
Ben Greear48014162011-01-15 19:13:48 +00001010}
1011
Ben Greear48014162011-01-15 19:13:48 +00001012static int ath9k_add_interface(struct ieee80211_hw *hw,
1013 struct ieee80211_vif *vif)
1014{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001015 struct ath_softc *sc = hw->priv;
Ben Greear48014162011-01-15 19:13:48 +00001016 struct ath_hw *ah = sc->sc_ah;
1017 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001018 struct ath_vif *avp = (void *)vif->drv_priv;
1019 struct ath_node *an = &avp->mcast_node;
Ben Greear48014162011-01-15 19:13:48 +00001020
1021 mutex_lock(&sc->mutex);
1022
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001023 if (config_enabled(CONFIG_ATH9K_TX99)) {
1024 if (sc->nvifs >= 1) {
1025 mutex_unlock(&sc->mutex);
1026 return -EOPNOTSUPP;
1027 }
1028 sc->tx99_vif = vif;
1029 }
1030
Joe Perchesd2182b62011-12-15 14:55:53 -08001031 ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
Ben Greear48014162011-01-15 19:13:48 +00001032 sc->nvifs++;
1033
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301034 ath9k_ps_wakeup(sc);
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301035 ath9k_calculate_summary_state(hw, vif);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301036 ath9k_ps_restore(sc);
1037
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301038 if (ath9k_uses_beacons(vif->type))
1039 ath9k_beacon_assign_slot(sc, vif);
1040
Felix Fietkaud463af42014-04-06 00:37:03 +02001041 avp->vif = vif;
Felix Fietkau39305632014-06-11 16:17:57 +05301042 if (!ath9k_use_chanctx)
1043 avp->chanctx = sc->cur_chan;
Felix Fietkau04535312014-06-11 16:17:51 +05301044
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001045 an->sc = sc;
1046 an->sta = NULL;
1047 an->vif = vif;
1048 an->no_ps_filter = true;
1049 ath_tx_node_init(sc, an);
1050
Ben Greear48014162011-01-15 19:13:48 +00001051 mutex_unlock(&sc->mutex);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301052 return 0;
Ben Greear48014162011-01-15 19:13:48 +00001053}
1054
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301055static int ath9k_change_interface(struct ieee80211_hw *hw,
1056 struct ieee80211_vif *vif,
1057 enum nl80211_iftype new_type,
1058 bool p2p)
1059{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001060 struct ath_softc *sc = hw->priv;
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301061 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkauc083ce92014-06-11 16:17:54 +05301062 struct ath_vif *avp = (void *)vif->drv_priv;
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301063
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301064 mutex_lock(&sc->mutex);
Ben Greear48014162011-01-15 19:13:48 +00001065
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001066 if (config_enabled(CONFIG_ATH9K_TX99)) {
1067 mutex_unlock(&sc->mutex);
1068 return -EOPNOTSUPP;
1069 }
1070
1071 ath_dbg(common, CONFIG, "Change Interface\n");
1072
Ben Greear48014162011-01-15 19:13:48 +00001073 if (ath9k_uses_beacons(vif->type))
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301074 ath9k_beacon_remove_slot(sc, vif);
Ben Greear48014162011-01-15 19:13:48 +00001075
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301076 vif->type = new_type;
1077 vif->p2p = p2p;
1078
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301079 ath9k_ps_wakeup(sc);
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301080 ath9k_calculate_summary_state(hw, vif);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301081 ath9k_ps_restore(sc);
1082
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301083 if (ath9k_uses_beacons(vif->type))
1084 ath9k_beacon_assign_slot(sc, vif);
Felix Fietkauc083ce92014-06-11 16:17:54 +05301085 ath_chanctx_check_active(sc, avp->chanctx);
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301086
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301087 mutex_unlock(&sc->mutex);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301088 return 0;
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301089}
1090
Felix Fietkaud463af42014-04-06 00:37:03 +02001091static void
1092ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1093{
1094 struct ath_hw *ah = sc->sc_ah;
1095 s32 tsf, target_tsf;
1096
1097 if (!avp || !avp->noa.has_next_tsf)
1098 return;
1099
1100 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1101
1102 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1103
1104 target_tsf = avp->noa.next_tsf;
1105 if (!avp->noa.absent)
1106 target_tsf -= ATH_P2P_PS_STOP_TIME;
1107
1108 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1109 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1110
1111 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1112}
1113
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001114static void ath9k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001115 struct ieee80211_vif *vif)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001116{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001117 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001118 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001119 struct ath_vif *avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001120
Joe Perchesd2182b62011-12-15 14:55:53 -08001121 ath_dbg(common, CONFIG, "Detach Interface\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001122
Sujith141b38b2009-02-04 08:10:07 +05301123 mutex_lock(&sc->mutex);
1124
Felix Fietkaud463af42014-04-06 00:37:03 +02001125 spin_lock_bh(&sc->sc_pcu_lock);
1126 if (avp == sc->p2p_ps_vif) {
1127 sc->p2p_ps_vif = NULL;
1128 ath9k_update_p2p_ps_timer(sc, NULL);
1129 }
1130 spin_unlock_bh(&sc->sc_pcu_lock);
1131
Ben Greear48014162011-01-15 19:13:48 +00001132 sc->nvifs--;
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001133 sc->tx99_vif = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001134
Ben Greear48014162011-01-15 19:13:48 +00001135 if (ath9k_uses_beacons(vif->type))
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301136 ath9k_beacon_remove_slot(sc, vif);
Jouni Malinen2c3db3d2009-03-03 19:23:26 +02001137
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301138 ath9k_ps_wakeup(sc);
Ben Greear48014162011-01-15 19:13:48 +00001139 ath9k_calculate_summary_state(hw, NULL);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301140 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301141
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001142 ath_tx_node_cleanup(sc, &avp->mcast_node);
1143
Sujith141b38b2009-02-04 08:10:07 +05301144 mutex_unlock(&sc->mutex);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001145}
1146
Senthil Balasubramanianfbab7392010-10-05 20:36:40 +05301147static void ath9k_enable_ps(struct ath_softc *sc)
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301148{
Pavel Roskin30691682010-03-31 18:05:31 -04001149 struct ath_hw *ah = sc->sc_ah;
Sujith Manoharanad128862012-04-24 10:23:20 +05301150 struct ath_common *common = ath9k_hw_common(ah);
Pavel Roskin30691682010-03-31 18:05:31 -04001151
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001152 if (config_enabled(CONFIG_ATH9K_TX99))
1153 return;
1154
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301155 sc->ps_enabled = true;
Pavel Roskin30691682010-03-31 18:05:31 -04001156 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1157 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1158 ah->imask |= ATH9K_INT_TIM_TIMER;
Felix Fietkau72d874c2011-10-08 20:06:19 +02001159 ath9k_hw_set_interrupts(ah);
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301160 }
Vasanthakumar Thiagarajanfdf76622010-05-17 18:57:55 -07001161 ath9k_hw_setrxabort(ah, 1);
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301162 }
Sujith Manoharanad128862012-04-24 10:23:20 +05301163 ath_dbg(common, PS, "PowerSave enabled\n");
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301164}
1165
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301166static void ath9k_disable_ps(struct ath_softc *sc)
1167{
1168 struct ath_hw *ah = sc->sc_ah;
Sujith Manoharanad128862012-04-24 10:23:20 +05301169 struct ath_common *common = ath9k_hw_common(ah);
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301170
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001171 if (config_enabled(CONFIG_ATH9K_TX99))
1172 return;
1173
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301174 sc->ps_enabled = false;
1175 ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
1176 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1177 ath9k_hw_setrxabort(ah, 0);
1178 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1179 PS_WAIT_FOR_CAB |
1180 PS_WAIT_FOR_PSPOLL_DATA |
1181 PS_WAIT_FOR_TX_ACK);
1182 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1183 ah->imask &= ~ATH9K_INT_TIM_TIMER;
Felix Fietkau72d874c2011-10-08 20:06:19 +02001184 ath9k_hw_set_interrupts(ah);
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301185 }
1186 }
Sujith Manoharanad128862012-04-24 10:23:20 +05301187 ath_dbg(common, PS, "PowerSave disabled\n");
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301188}
1189
Simon Wunderliche93d0832013-01-08 14:48:58 +01001190void ath9k_spectral_scan_trigger(struct ieee80211_hw *hw)
1191{
1192 struct ath_softc *sc = hw->priv;
1193 struct ath_hw *ah = sc->sc_ah;
1194 struct ath_common *common = ath9k_hw_common(ah);
1195 u32 rxfilter;
1196
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001197 if (config_enabled(CONFIG_ATH9K_TX99))
1198 return;
1199
Simon Wunderliche93d0832013-01-08 14:48:58 +01001200 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1201 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1202 return;
1203 }
1204
1205 ath9k_ps_wakeup(sc);
1206 rxfilter = ath9k_hw_getrxfilter(ah);
1207 ath9k_hw_setrxfilter(ah, rxfilter |
1208 ATH9K_RX_FILTER_PHYRADAR |
1209 ATH9K_RX_FILTER_PHYERR);
1210
1211 /* TODO: usually this should not be neccesary, but for some reason
1212 * (or in some mode?) the trigger must be called after the
1213 * configuration, otherwise the register will have its values reset
1214 * (on my ar9220 to value 0x01002310)
1215 */
1216 ath9k_spectral_scan_config(hw, sc->spectral_mode);
1217 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
1218 ath9k_ps_restore(sc);
1219}
1220
1221int ath9k_spectral_scan_config(struct ieee80211_hw *hw,
1222 enum spectral_mode spectral_mode)
1223{
1224 struct ath_softc *sc = hw->priv;
1225 struct ath_hw *ah = sc->sc_ah;
1226 struct ath_common *common = ath9k_hw_common(ah);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001227
1228 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1229 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1230 return -1;
1231 }
1232
Simon Wunderliche93d0832013-01-08 14:48:58 +01001233 switch (spectral_mode) {
1234 case SPECTRAL_DISABLED:
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001235 sc->spec_config.enabled = 0;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001236 break;
1237 case SPECTRAL_BACKGROUND:
1238 /* send endless samples.
1239 * TODO: is this really useful for "background"?
1240 */
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001241 sc->spec_config.endless = 1;
1242 sc->spec_config.enabled = 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001243 break;
1244 case SPECTRAL_CHANSCAN:
Simon Wunderliche93d0832013-01-08 14:48:58 +01001245 case SPECTRAL_MANUAL:
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001246 sc->spec_config.endless = 0;
1247 sc->spec_config.enabled = 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001248 break;
1249 default:
1250 return -1;
1251 }
1252
1253 ath9k_ps_wakeup(sc);
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001254 ath9k_hw_ops(ah)->spectral_scan_config(ah, &sc->spec_config);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001255 ath9k_ps_restore(sc);
1256
1257 sc->spectral_mode = spectral_mode;
1258
1259 return 0;
1260}
1261
Johannes Berge8975582008-10-09 12:18:51 +02001262static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001263{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001264 struct ath_softc *sc = hw->priv;
Felix Fietkau34300982010-10-10 18:21:52 +02001265 struct ath_hw *ah = sc->sc_ah;
1266 struct ath_common *common = ath9k_hw_common(ah);
Johannes Berge8975582008-10-09 12:18:51 +02001267 struct ieee80211_conf *conf = &hw->conf;
Felix Fietkaufbbcd142014-06-11 16:17:49 +05301268 struct ath_chanctx *ctx = sc->cur_chan;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001269
Felix Fietkauc0c11742011-11-16 13:08:41 +01001270 ath9k_ps_wakeup(sc);
Sujithaa33de02008-12-18 11:40:16 +05301271 mutex_lock(&sc->mutex);
Sujith141b38b2009-02-04 08:10:07 +05301272
Felix Fietkaudaa1b6e2011-11-16 13:08:43 +01001273 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
Felix Fietkau7545daf2011-01-24 19:23:16 +01001274 sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
Rajkumar Manoharanb73f3e72012-07-01 19:53:53 +05301275 if (sc->ps_idle) {
Felix Fietkaudaa1b6e2011-11-16 13:08:43 +01001276 ath_cancel_work(sc);
Rajkumar Manoharanb73f3e72012-07-01 19:53:53 +05301277 ath9k_stop_btcoex(sc);
1278 } else {
1279 ath9k_start_btcoex(sc);
Felix Fietkau75600ab2012-04-12 20:36:31 +02001280 /*
1281 * The chip needs a reset to properly wake up from
1282 * full sleep
1283 */
Felix Fietkau39305632014-06-11 16:17:57 +05301284 ath_chanctx_set_channel(sc, ctx, &ctx->chandef);
Rajkumar Manoharanb73f3e72012-07-01 19:53:53 +05301285 }
Felix Fietkaudaa1b6e2011-11-16 13:08:43 +01001286 }
Luis R. Rodriguez64839172009-07-14 20:22:53 -04001287
Luis R. Rodrigueze7824a52009-11-24 02:53:25 -05001288 /*
1289 * We just prepare to enable PS. We have to wait until our AP has
1290 * ACK'd our null data frame to disable RX otherwise we'll ignore
1291 * those ACKs and end up retransmitting the same null data frames.
1292 * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1293 */
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301294 if (changed & IEEE80211_CONF_CHANGE_PS) {
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001295 unsigned long flags;
1296 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Senthil Balasubramanianfbab7392010-10-05 20:36:40 +05301297 if (conf->flags & IEEE80211_CONF_PS)
1298 ath9k_enable_ps(sc);
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301299 else
1300 ath9k_disable_ps(sc);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001301 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301302 }
1303
Sujith199afd92010-01-08 10:36:13 +05301304 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1305 if (conf->flags & IEEE80211_CONF_MONITOR) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001306 ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301307 sc->sc_ah->is_monitoring = true;
1308 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -08001309 ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301310 sc->sc_ah->is_monitoring = false;
Sujith199afd92010-01-08 10:36:13 +05301311 }
1312 }
1313
Felix Fietkau39305632014-06-11 16:17:57 +05301314 if (!ath9k_use_chanctx && (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
Felix Fietkaufbbcd142014-06-11 16:17:49 +05301315 ctx->offchannel = !!(conf->flags & IEEE80211_CONF_OFFCHANNEL);
Felix Fietkaubff11762014-06-11 16:17:52 +05301316 ath_chanctx_set_channel(sc, ctx, &hw->conf.chandef);
Sujith094d05d2008-12-12 11:57:43 +05301317 }
Sujith86b89ee2008-08-07 10:54:57 +05301318
Luis R. Rodriguezc9f6a652010-01-19 14:04:19 -05001319 if (changed & IEEE80211_CONF_CHANGE_POWER) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001320 ath_dbg(common, CONFIG, "Set power: %d\n", conf->power_level);
Felix Fietkaubc7e1be2014-06-11 16:17:50 +05301321 sc->cur_chan->txpower = 2 * conf->power_level;
Rajkumar Manoharan5048e8c2011-01-31 23:47:44 +05301322 ath9k_cmn_update_txpow(ah, sc->curtxpow,
Felix Fietkaubc7e1be2014-06-11 16:17:50 +05301323 sc->cur_chan->txpower, &sc->curtxpow);
Luis R. Rodriguez64839172009-07-14 20:22:53 -04001324 }
1325
Sujithaa33de02008-12-18 11:40:16 +05301326 mutex_unlock(&sc->mutex);
Felix Fietkauc0c11742011-11-16 13:08:41 +01001327 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301328
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001329 return 0;
1330}
1331
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001332#define SUPPORTED_FILTERS \
1333 (FIF_PROMISC_IN_BSS | \
1334 FIF_ALLMULTI | \
1335 FIF_CONTROL | \
Luis R. Rodriguezaf6a3fc2009-08-08 21:55:16 -04001336 FIF_PSPOLL | \
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001337 FIF_OTHER_BSS | \
1338 FIF_BCN_PRBRESP_PROMISC | \
Jouni Malinen9c1d8e42010-10-13 17:29:31 +03001339 FIF_PROBE_REQ | \
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001340 FIF_FCSFAIL)
1341
Sujith7dcfdcd2008-08-11 14:03:13 +05301342/* FIXME: sc->sc_full_reset ? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001343static void ath9k_configure_filter(struct ieee80211_hw *hw,
1344 unsigned int changed_flags,
1345 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001346 u64 multicast)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001347{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001348 struct ath_softc *sc = hw->priv;
Sujith7dcfdcd2008-08-11 14:03:13 +05301349 u32 rfilt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001350
1351 changed_flags &= SUPPORTED_FILTERS;
1352 *total_flags &= SUPPORTED_FILTERS;
1353
Sujithb77f4832008-12-07 21:44:03 +05301354 sc->rx.rxfilter = *total_flags;
Jouni Malinenaa68aea2009-05-19 17:01:41 +03001355 ath9k_ps_wakeup(sc);
Sujith7dcfdcd2008-08-11 14:03:13 +05301356 rfilt = ath_calcrxfilter(sc);
1357 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
Jouni Malinenaa68aea2009-05-19 17:01:41 +03001358 ath9k_ps_restore(sc);
Sujith7dcfdcd2008-08-11 14:03:13 +05301359
Joe Perchesd2182b62011-12-15 14:55:53 -08001360 ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
1361 rfilt);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001362}
1363
Johannes Berg4ca77862010-02-19 19:06:56 +01001364static int ath9k_sta_add(struct ieee80211_hw *hw,
1365 struct ieee80211_vif *vif,
1366 struct ieee80211_sta *sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001367{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001368 struct ath_softc *sc = hw->priv;
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001369 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1370 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1371 struct ieee80211_key_conf ps_key = { };
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001372 int key;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001373
Ben Greear7e1e3862011-11-03 11:33:13 -07001374 ath_node_attach(sc, sta, vif);
Felix Fietkauf59a59f2011-05-10 20:52:22 +02001375
1376 if (vif->type != NL80211_IFTYPE_AP &&
1377 vif->type != NL80211_IFTYPE_AP_VLAN)
1378 return 0;
1379
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001380 key = ath_key_config(common, vif, sta, &ps_key);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301381 if (key > 0) {
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001382 an->ps_key = key;
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301383 an->key_idx[0] = key;
1384 }
Johannes Berg4ca77862010-02-19 19:06:56 +01001385
1386 return 0;
1387}
1388
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001389static void ath9k_del_ps_key(struct ath_softc *sc,
1390 struct ieee80211_vif *vif,
1391 struct ieee80211_sta *sta)
1392{
1393 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1394 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1395 struct ieee80211_key_conf ps_key = { .hw_key_idx = an->ps_key };
1396
1397 if (!an->ps_key)
1398 return;
1399
1400 ath_key_delete(common, &ps_key);
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001401 an->ps_key = 0;
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301402 an->key_idx[0] = 0;
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001403}
1404
Johannes Berg4ca77862010-02-19 19:06:56 +01001405static int ath9k_sta_remove(struct ieee80211_hw *hw,
1406 struct ieee80211_vif *vif,
1407 struct ieee80211_sta *sta)
1408{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001409 struct ath_softc *sc = hw->priv;
Johannes Berg4ca77862010-02-19 19:06:56 +01001410
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001411 ath9k_del_ps_key(sc, vif, sta);
Johannes Berg4ca77862010-02-19 19:06:56 +01001412 ath_node_detach(sc, sta);
1413
1414 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001415}
1416
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301417static void ath9k_sta_set_tx_filter(struct ath_hw *ah,
1418 struct ath_node *an,
1419 bool set)
1420{
1421 int i;
1422
1423 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1424 if (!an->key_idx[i])
1425 continue;
1426 ath9k_hw_set_tx_filter(ah, an->key_idx[i], set);
1427 }
1428}
1429
Felix Fietkau55195412011-04-17 23:28:09 +02001430static void ath9k_sta_notify(struct ieee80211_hw *hw,
1431 struct ieee80211_vif *vif,
1432 enum sta_notify_cmd cmd,
1433 struct ieee80211_sta *sta)
1434{
1435 struct ath_softc *sc = hw->priv;
1436 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1437
1438 switch (cmd) {
1439 case STA_NOTIFY_SLEEP:
1440 an->sleeping = true;
Johannes Berg042ec452011-09-29 16:04:26 +02001441 ath_tx_aggr_sleep(sta, sc, an);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301442 ath9k_sta_set_tx_filter(sc->sc_ah, an, true);
Felix Fietkau55195412011-04-17 23:28:09 +02001443 break;
1444 case STA_NOTIFY_AWAKE:
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301445 ath9k_sta_set_tx_filter(sc->sc_ah, an, false);
Felix Fietkau55195412011-04-17 23:28:09 +02001446 an->sleeping = false;
1447 ath_tx_aggr_wakeup(sc, an);
1448 break;
1449 }
1450}
1451
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001452static int ath9k_conf_tx(struct ieee80211_hw *hw,
1453 struct ieee80211_vif *vif, u16 queue,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001454 const struct ieee80211_tx_queue_params *params)
1455{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001456 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001457 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau066dae92010-11-07 14:59:39 +01001458 struct ath_txq *txq;
Sujithea9880f2008-08-07 10:53:10 +05301459 struct ath9k_tx_queue_info qi;
Felix Fietkau066dae92010-11-07 14:59:39 +01001460 int ret = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001461
Sujith Manoharanbea843c2012-11-21 18:13:10 +05301462 if (queue >= IEEE80211_NUM_ACS)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001463 return 0;
1464
Felix Fietkau066dae92010-11-07 14:59:39 +01001465 txq = sc->tx.txq_map[queue];
1466
Felix Fietkau96f372c2011-04-07 19:07:17 +02001467 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301468 mutex_lock(&sc->mutex);
1469
Sujith1ffb0612009-03-30 15:28:46 +05301470 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1471
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001472 qi.tqi_aifs = params->aifs;
1473 qi.tqi_cwmin = params->cw_min;
1474 qi.tqi_cwmax = params->cw_max;
Felix Fietkau531bd072012-07-15 19:53:34 +02001475 qi.tqi_burstTime = params->txop * 32;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001476
Joe Perchesd2182b62011-12-15 14:55:53 -08001477 ath_dbg(common, CONFIG,
Joe Perches226afe62010-12-02 19:12:37 -08001478 "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1479 queue, txq->axq_qnum, params->aifs, params->cw_min,
1480 params->cw_max, params->txop);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001481
Felix Fietkauaa5955c2012-07-15 19:53:36 +02001482 ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
Felix Fietkau066dae92010-11-07 14:59:39 +01001483 ret = ath_txq_update(sc, txq->axq_qnum, &qi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001484 if (ret)
Joe Perches38002762010-12-02 19:12:36 -08001485 ath_err(common, "TXQ Update failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001486
Sujith141b38b2009-02-04 08:10:07 +05301487 mutex_unlock(&sc->mutex);
Felix Fietkau96f372c2011-04-07 19:07:17 +02001488 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301489
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001490 return ret;
1491}
1492
1493static int ath9k_set_key(struct ieee80211_hw *hw,
1494 enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01001495 struct ieee80211_vif *vif,
1496 struct ieee80211_sta *sta,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001497 struct ieee80211_key_conf *key)
1498{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001499 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001500 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301501 struct ath_node *an = NULL;
1502 int ret = 0, i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001503
John W. Linville3e6109c2011-01-05 09:39:17 -05001504 if (ath9k_modparam_nohwcrypt)
Jouni Malinenb3bd89c2009-02-24 13:42:01 +02001505 return -ENOSPC;
1506
Chun-Yeow Yeoh5bd5e9a2011-12-07 12:45:46 -08001507 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1508 vif->type == NL80211_IFTYPE_MESH_POINT) &&
Jouni Malinencfdc9a82011-03-23 14:52:19 +02001509 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1510 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1511 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1512 /*
1513 * For now, disable hw crypto for the RSN IBSS group keys. This
1514 * could be optimized in the future to use a modified key cache
1515 * design to support per-STA RX GTK, but until that gets
1516 * implemented, use of software crypto for group addressed
1517 * frames is a acceptable to allow RSN IBSS to be used.
1518 */
1519 return -EOPNOTSUPP;
1520 }
1521
Sujith141b38b2009-02-04 08:10:07 +05301522 mutex_lock(&sc->mutex);
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301523 ath9k_ps_wakeup(sc);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301524 ath_dbg(common, CONFIG, "Set HW Key %d\n", cmd);
1525 if (sta)
1526 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001527
1528 switch (cmd) {
1529 case SET_KEY:
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001530 if (sta)
1531 ath9k_del_ps_key(sc, vif, sta);
1532
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301533 key->hw_key_idx = 0;
Bruno Randolf040e5392010-09-08 16:05:04 +09001534 ret = ath_key_config(common, vif, sta, key);
Jouni Malinen6ace2892008-12-17 13:32:17 +02001535 if (ret >= 0) {
1536 key->hw_key_idx = ret;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001537 /* push IV and Michael MIC generation to stack */
1538 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Johannes Berg97359d12010-08-10 09:46:38 +02001539 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
Senthil Balasubramanian1b961752008-09-01 19:45:21 +05301540 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Johannes Berg97359d12010-08-10 09:46:38 +02001541 if (sc->sc_ah->sw_mgmt_crypto &&
1542 key->cipher == WLAN_CIPHER_SUITE_CCMP)
Johannes Berge548c492012-09-04 17:08:23 +02001543 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Jouni Malinen6ace2892008-12-17 13:32:17 +02001544 ret = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001545 }
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301546 if (an && key->hw_key_idx) {
1547 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1548 if (an->key_idx[i])
1549 continue;
1550 an->key_idx[i] = key->hw_key_idx;
1551 break;
1552 }
1553 WARN_ON(i == ARRAY_SIZE(an->key_idx));
1554 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001555 break;
1556 case DISABLE_KEY:
Bruno Randolf040e5392010-09-08 16:05:04 +09001557 ath_key_delete(common, key);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301558 if (an) {
1559 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1560 if (an->key_idx[i] != key->hw_key_idx)
1561 continue;
1562 an->key_idx[i] = 0;
1563 break;
1564 }
1565 }
1566 key->hw_key_idx = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001567 break;
1568 default:
1569 ret = -EINVAL;
1570 }
1571
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301572 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301573 mutex_unlock(&sc->mutex);
1574
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001575 return ret;
1576}
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301577
1578static void ath9k_set_assoc_state(struct ath_softc *sc,
1579 struct ieee80211_vif *vif)
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301580{
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301581 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301582 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301583 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
Sujith Manoharan07c15a32012-06-04 20:24:07 +05301584 unsigned long flags;
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301585
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001586 set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301587 avp->primary_sta_vif = true;
1588
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301589 /*
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301590 * Set the AID, BSSID and do beacon-sync only when
1591 * the HW opmode is STATION.
1592 *
1593 * But the primary bit is set above in any case.
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301594 */
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301595 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
1596 return;
1597
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301598 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1599 common->curaid = bss_conf->aid;
1600 ath9k_hw_write_associd(sc->sc_ah);
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301601
Oleksij Rempel32efb0c2014-02-04 10:27:39 +01001602 common->last_rssi = ATH_RSSI_DUMMY_MARKER;
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301603 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301604
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301605 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1606 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
1607 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1608
Rajkumar Manoharan50072eb2012-10-12 14:07:22 +05301609 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1610 ath9k_mci_update_wlan_channels(sc, false);
1611
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301612 ath_dbg(common, CONFIG,
1613 "Primary Station interface: %pM, BSSID: %pM\n",
1614 vif->addr, common->curbssid);
1615}
1616
1617static void ath9k_bss_assoc_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1618{
1619 struct ath_softc *sc = data;
1620 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001621 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301622
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001623 if (test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301624 return;
1625
1626 if (bss_conf->assoc)
1627 ath9k_set_assoc_state(sc, vif);
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301628}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001629
Felix Fietkaud463af42014-04-06 00:37:03 +02001630void ath9k_p2p_ps_timer(void *priv)
1631{
1632 struct ath_softc *sc = priv;
1633 struct ath_vif *avp = sc->p2p_ps_vif;
1634 struct ieee80211_vif *vif;
1635 struct ieee80211_sta *sta;
1636 struct ath_node *an;
1637 u32 tsf;
1638
1639 if (!avp)
1640 return;
1641
1642 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1643 if (!avp->noa.absent)
1644 tsf += ATH_P2P_PS_STOP_TIME;
1645
1646 if (!avp->noa.has_next_tsf ||
1647 avp->noa.next_tsf - tsf > BIT(31))
1648 ieee80211_update_p2p_noa(&avp->noa, tsf);
1649
1650 ath9k_update_p2p_ps_timer(sc, avp);
1651
1652 rcu_read_lock();
1653
1654 vif = avp->vif;
1655 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1656 if (!sta)
1657 goto out;
1658
1659 an = (void *) sta->drv_priv;
1660 if (an->sleeping == !!avp->noa.absent)
1661 goto out;
1662
1663 an->sleeping = avp->noa.absent;
1664 if (an->sleeping)
1665 ath_tx_aggr_sleep(sta, sc, an);
1666 else
1667 ath_tx_aggr_wakeup(sc, an);
1668
1669out:
1670 rcu_read_unlock();
1671}
1672
1673void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1674{
1675 struct ath_vif *avp = (void *)vif->drv_priv;
Felix Fietkaud463af42014-04-06 00:37:03 +02001676 u32 tsf;
1677
1678 if (!sc->p2p_ps_timer)
1679 return;
1680
1681 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1682 return;
1683
1684 sc->p2p_ps_vif = avp;
Rajkumar Manoharan60ccc102014-05-27 16:58:02 +05301685 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1686 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1687 ath9k_update_p2p_ps_timer(sc, avp);
Felix Fietkaud463af42014-04-06 00:37:03 +02001688}
1689
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001690static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1691 struct ieee80211_vif *vif,
1692 struct ieee80211_bss_conf *bss_conf,
1693 u32 changed)
1694{
Sujith Manoharanda0d45f2012-07-17 17:16:29 +05301695#define CHECK_ANI \
1696 (BSS_CHANGED_ASSOC | \
1697 BSS_CHANGED_IBSS | \
1698 BSS_CHANGED_BEACON_ENABLED)
1699
Felix Fietkau9ac586152011-01-24 19:23:18 +01001700 struct ath_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001701 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -07001702 struct ath_common *common = ath9k_hw_common(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001703 struct ath_vif *avp = (void *)vif->drv_priv;
Rajkumar Manoharan60ccc102014-05-27 16:58:02 +05301704 unsigned long flags;
Felix Fietkau0005baf2010-01-15 02:33:40 +01001705 int slottime;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001706
Felix Fietkau96f372c2011-04-07 19:07:17 +02001707 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301708 mutex_lock(&sc->mutex);
1709
Rajkumar Manoharan9f619032012-03-10 05:06:49 +05301710 if (changed & BSS_CHANGED_ASSOC) {
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301711 ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
1712 bss_conf->bssid, bss_conf->assoc);
Sujithc6089cc2009-11-16 11:40:48 +05301713
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301714 if (avp->primary_sta_vif && !bss_conf->assoc) {
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001715 clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301716 avp->primary_sta_vif = false;
1717
1718 if (ah->opmode == NL80211_IFTYPE_STATION)
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001719 clear_bit(ATH_OP_BEACONS, &common->op_flags);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301720 }
1721
Johannes Berg8b2c9822012-11-06 20:23:30 +01001722 ieee80211_iterate_active_interfaces_atomic(
1723 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1724 ath9k_bss_assoc_iter, sc);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301725
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001726 if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags) &&
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301727 ah->opmode == NL80211_IFTYPE_STATION) {
1728 memset(common->curbssid, 0, ETH_ALEN);
1729 common->curaid = 0;
1730 ath9k_hw_write_associd(sc->sc_ah);
Rajkumar Manoharan50072eb2012-10-12 14:07:22 +05301731 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1732 ath9k_mci_update_wlan_channels(sc, true);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301733 }
Felix Fietkauc083ce92014-06-11 16:17:54 +05301734
1735 ath_chanctx_check_active(sc, avp->chanctx);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001736 }
1737
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301738 if (changed & BSS_CHANGED_IBSS) {
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301739 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1740 common->curaid = bss_conf->aid;
1741 ath9k_hw_write_associd(sc->sc_ah);
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301742 }
1743
Sujith Manoharanef4ad632012-07-17 17:15:56 +05301744 if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
Felix Fietkauc32e4e52013-12-19 18:01:49 +01001745 (changed & BSS_CHANGED_BEACON_INT))
1746 ath9k_beacon_config(sc, vif, changed);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001747
Felix Fietkau0005baf2010-01-15 02:33:40 +01001748 if (changed & BSS_CHANGED_ERP_SLOT) {
1749 if (bss_conf->use_short_slot)
1750 slottime = 9;
1751 else
1752 slottime = 20;
1753 if (vif->type == NL80211_IFTYPE_AP) {
1754 /*
1755 * Defer update, so that connected stations can adjust
1756 * their settings at the same time.
1757 * See beacon.c for more details
1758 */
1759 sc->beacon.slottime = slottime;
1760 sc->beacon.updateslot = UPDATE;
1761 } else {
1762 ah->slottime = slottime;
1763 ath9k_hw_init_global_settings(ah);
1764 }
1765 }
1766
Felix Fietkaud463af42014-04-06 00:37:03 +02001767 if (changed & BSS_CHANGED_P2P_PS) {
1768 spin_lock_bh(&sc->sc_pcu_lock);
Rajkumar Manoharan60ccc102014-05-27 16:58:02 +05301769 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1770 if (!(sc->ps_flags & PS_BEACON_SYNC))
1771 ath9k_update_p2p_ps(sc, vif);
1772 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Felix Fietkaud463af42014-04-06 00:37:03 +02001773 spin_unlock_bh(&sc->sc_pcu_lock);
1774 }
1775
Sujith Manoharanda0d45f2012-07-17 17:16:29 +05301776 if (changed & CHECK_ANI)
1777 ath_check_ani(sc);
1778
Sujith141b38b2009-02-04 08:10:07 +05301779 mutex_unlock(&sc->mutex);
Felix Fietkau96f372c2011-04-07 19:07:17 +02001780 ath9k_ps_restore(sc);
Sujith Manoharanda0d45f2012-07-17 17:16:29 +05301781
1782#undef CHECK_ANI
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001783}
1784
Eliad Peller37a41b42011-09-21 14:06:11 +03001785static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001786{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001787 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001788 u64 tsf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001789
Sujith141b38b2009-02-04 08:10:07 +05301790 mutex_lock(&sc->mutex);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301791 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301792 tsf = ath9k_hw_gettsf64(sc->sc_ah);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301793 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301794 mutex_unlock(&sc->mutex);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001795
1796 return tsf;
1797}
1798
Eliad Peller37a41b42011-09-21 14:06:11 +03001799static void ath9k_set_tsf(struct ieee80211_hw *hw,
1800 struct ieee80211_vif *vif,
1801 u64 tsf)
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01001802{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001803 struct ath_softc *sc = hw->priv;
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01001804
Sujith141b38b2009-02-04 08:10:07 +05301805 mutex_lock(&sc->mutex);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301806 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301807 ath9k_hw_settsf64(sc->sc_ah, tsf);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301808 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301809 mutex_unlock(&sc->mutex);
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01001810}
1811
Eliad Peller37a41b42011-09-21 14:06:11 +03001812static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001813{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001814 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001815
Sujith141b38b2009-02-04 08:10:07 +05301816 mutex_lock(&sc->mutex);
Luis R. Rodriguez21526d52009-09-09 20:05:39 -07001817
1818 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301819 ath9k_hw_reset_tsf(sc->sc_ah);
Luis R. Rodriguez21526d52009-09-09 20:05:39 -07001820 ath9k_ps_restore(sc);
1821
Sujith141b38b2009-02-04 08:10:07 +05301822 mutex_unlock(&sc->mutex);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001823}
1824
1825static int ath9k_ampdu_action(struct ieee80211_hw *hw,
Johannes Bergc951ad32009-11-16 12:00:38 +01001826 struct ieee80211_vif *vif,
Sujith141b38b2009-02-04 08:10:07 +05301827 enum ieee80211_ampdu_mlme_action action,
1828 struct ieee80211_sta *sta,
Johannes Berg0b01f032011-01-18 13:51:05 +01001829 u16 tid, u16 *ssn, u8 buf_size)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001830{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001831 struct ath_softc *sc = hw->priv;
Felix Fietkau16e23422013-05-17 12:58:24 +02001832 bool flush = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001833 int ret = 0;
1834
Sujith Manoharan7ca7c772013-05-08 05:03:32 +05301835 mutex_lock(&sc->mutex);
Johannes Berg85ad1812010-06-10 10:21:49 +02001836
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001837 switch (action) {
1838 case IEEE80211_AMPDU_RX_START:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001839 break;
1840 case IEEE80211_AMPDU_RX_STOP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001841 break;
1842 case IEEE80211_AMPDU_TX_START:
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001843 ath9k_ps_wakeup(sc);
Felix Fietkau231c3a12010-09-20 19:35:28 +02001844 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
1845 if (!ret)
1846 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001847 ath9k_ps_restore(sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001848 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02001849 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1850 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Felix Fietkau16e23422013-05-17 12:58:24 +02001851 flush = true;
1852 case IEEE80211_AMPDU_TX_STOP_CONT:
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001853 ath9k_ps_wakeup(sc);
Sujithf83da962009-07-23 15:32:37 +05301854 ath_tx_aggr_stop(sc, sta, tid);
Felix Fietkau08c96ab2013-05-18 21:28:15 +02001855 if (!flush)
Felix Fietkau16e23422013-05-17 12:58:24 +02001856 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001857 ath9k_ps_restore(sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001858 break;
Johannes Bergb1720232009-03-23 17:28:39 +01001859 case IEEE80211_AMPDU_TX_OPERATIONAL:
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001860 ath9k_ps_wakeup(sc);
Sujith8469cde2008-10-29 10:19:28 +05301861 ath_tx_aggr_resume(sc, sta, tid);
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001862 ath9k_ps_restore(sc);
Sujith8469cde2008-10-29 10:19:28 +05301863 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001864 default:
Joe Perches38002762010-12-02 19:12:36 -08001865 ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001866 }
1867
Sujith Manoharan7ca7c772013-05-08 05:03:32 +05301868 mutex_unlock(&sc->mutex);
Johannes Berg85ad1812010-06-10 10:21:49 +02001869
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001870 return ret;
1871}
1872
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001873static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
1874 struct survey_info *survey)
1875{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001876 struct ath_softc *sc = hw->priv;
Felix Fietkau34300982010-10-10 18:21:52 +02001877 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau39162db2010-09-29 19:12:06 +02001878 struct ieee80211_supported_band *sband;
Felix Fietkau34300982010-10-10 18:21:52 +02001879 struct ieee80211_channel *chan;
Felix Fietkau34300982010-10-10 18:21:52 +02001880 int pos;
1881
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001882 if (config_enabled(CONFIG_ATH9K_TX99))
1883 return -EOPNOTSUPP;
1884
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +05301885 spin_lock_bh(&common->cc_lock);
Felix Fietkau34300982010-10-10 18:21:52 +02001886 if (idx == 0)
1887 ath_update_survey_stats(sc);
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001888
Felix Fietkau39162db2010-09-29 19:12:06 +02001889 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
1890 if (sband && idx >= sband->n_channels) {
1891 idx -= sband->n_channels;
1892 sband = NULL;
1893 }
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001894
Felix Fietkau39162db2010-09-29 19:12:06 +02001895 if (!sband)
1896 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
1897
Felix Fietkau34300982010-10-10 18:21:52 +02001898 if (!sband || idx >= sband->n_channels) {
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +05301899 spin_unlock_bh(&common->cc_lock);
Felix Fietkau34300982010-10-10 18:21:52 +02001900 return -ENOENT;
Felix Fietkau4f1a5a42010-09-29 17:15:28 +02001901 }
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001902
Felix Fietkau34300982010-10-10 18:21:52 +02001903 chan = &sband->channels[idx];
1904 pos = chan->hw_value;
1905 memcpy(survey, &sc->survey[pos], sizeof(*survey));
1906 survey->channel = chan;
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +05301907 spin_unlock_bh(&common->cc_lock);
Felix Fietkau34300982010-10-10 18:21:52 +02001908
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001909 return 0;
1910}
1911
Felix Fietkaue239d852010-01-15 02:34:58 +01001912static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
1913{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001914 struct ath_softc *sc = hw->priv;
Felix Fietkaue239d852010-01-15 02:34:58 +01001915 struct ath_hw *ah = sc->sc_ah;
1916
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001917 if (config_enabled(CONFIG_ATH9K_TX99))
1918 return;
1919
Felix Fietkaue239d852010-01-15 02:34:58 +01001920 mutex_lock(&sc->mutex);
1921 ah->coverage_class = coverage_class;
Mohammed Shafi Shajakhan8b2a38272011-08-24 21:38:07 +05301922
1923 ath9k_ps_wakeup(sc);
Felix Fietkaue239d852010-01-15 02:34:58 +01001924 ath9k_hw_init_global_settings(ah);
Mohammed Shafi Shajakhan8b2a38272011-08-24 21:38:07 +05301925 ath9k_ps_restore(sc);
1926
Felix Fietkaue239d852010-01-15 02:34:58 +01001927 mutex_unlock(&sc->mutex);
1928}
1929
Felix Fietkau10e23182013-11-11 22:23:35 +01001930static bool ath9k_has_tx_pending(struct ath_softc *sc)
1931{
Geert Uytterhoevenf7838072014-01-26 11:53:21 +01001932 int i, npend = 0;
Felix Fietkau10e23182013-11-11 22:23:35 +01001933
1934 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1935 if (!ATH_TXQ_SETUP(sc, i))
1936 continue;
1937
1938 if (!sc->tx.txq[i].axq_depth)
1939 continue;
1940
1941 npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i]);
1942 if (npend)
1943 break;
1944 }
1945
1946 return !!npend;
1947}
1948
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02001949static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1950 u32 queues, bool drop)
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001951{
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001952 struct ath_softc *sc = hw->priv;
Felix Fietkaubff11762014-06-11 16:17:52 +05301953
1954 mutex_lock(&sc->mutex);
1955 __ath9k_flush(hw, queues, drop);
1956 mutex_unlock(&sc->mutex);
1957}
1958
1959void __ath9k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1960{
1961 struct ath_softc *sc = hw->priv;
Mohammed Shafi Shajakhan99aa55b2011-05-06 20:43:11 +05301962 struct ath_hw *ah = sc->sc_ah;
1963 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau10e23182013-11-11 22:23:35 +01001964 int timeout = HZ / 5; /* 200 ms */
Rajkumar Manoharan2f6fc352011-04-28 15:31:57 +05301965 bool drain_txq;
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001966
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001967 cancel_delayed_work_sync(&sc->tx_complete_work);
1968
Mohammed Shafi Shajakhan6a6b3f32011-09-09 10:41:08 +05301969 if (ah->ah_flags & AH_UNPLUGGED) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001970 ath_dbg(common, ANY, "Device has been unplugged!\n");
Mohammed Shafi Shajakhan6a6b3f32011-09-09 10:41:08 +05301971 return;
1972 }
1973
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001974 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001975 ath_dbg(common, ANY, "Device not present\n");
Mohammed Shafi Shajakhan99aa55b2011-05-06 20:43:11 +05301976 return;
1977 }
1978
Felix Fietkau10e23182013-11-11 22:23:35 +01001979 if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc),
1980 timeout) > 0)
1981 drop = false;
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001982
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001983 if (drop) {
1984 ath9k_ps_wakeup(sc);
1985 spin_lock_bh(&sc->sc_pcu_lock);
Felix Fietkau13815592013-01-20 18:51:53 +01001986 drain_txq = ath_drain_all_txq(sc);
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001987 spin_unlock_bh(&sc->sc_pcu_lock);
Felix Fietkau9adcf442011-09-03 01:40:26 +02001988
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001989 if (!drain_txq)
Felix Fietkau13815592013-01-20 18:51:53 +01001990 ath_reset(sc);
Felix Fietkau9adcf442011-09-03 01:40:26 +02001991
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001992 ath9k_ps_restore(sc);
1993 ieee80211_wake_queues(hw);
1994 }
Senthil Balasubramaniand78f4b32011-03-23 23:07:22 +05301995
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001996 ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001997}
1998
Vivek Natarajan15b91e82011-04-06 11:41:11 +05301999static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
2000{
2001 struct ath_softc *sc = hw->priv;
2002 int i;
2003
2004 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2005 if (!ATH_TXQ_SETUP(sc, i))
2006 continue;
2007
2008 if (ath9k_has_pending_frames(sc, &sc->tx.txq[i]))
2009 return true;
2010 }
2011 return false;
2012}
2013
Mohammed Shafi Shajakhan5595f112011-05-19 18:08:57 +05302014static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
Felix Fietkauba4903f2011-05-17 21:09:54 +02002015{
2016 struct ath_softc *sc = hw->priv;
2017 struct ath_hw *ah = sc->sc_ah;
2018 struct ieee80211_vif *vif;
2019 struct ath_vif *avp;
2020 struct ath_buf *bf;
2021 struct ath_tx_status ts;
Felix Fietkau4286df62012-02-27 19:58:40 +01002022 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
Felix Fietkauba4903f2011-05-17 21:09:54 +02002023 int status;
2024
2025 vif = sc->beacon.bslot[0];
2026 if (!vif)
2027 return 0;
2028
Sujith Manoharanaa45fe92012-07-17 17:16:03 +05302029 if (!vif->bss_conf.enable_beacon)
Felix Fietkauba4903f2011-05-17 21:09:54 +02002030 return 0;
2031
Sujith Manoharanaa45fe92012-07-17 17:16:03 +05302032 avp = (void *)vif->drv_priv;
2033
Felix Fietkau4286df62012-02-27 19:58:40 +01002034 if (!sc->beacon.tx_processed && !edma) {
Felix Fietkauba4903f2011-05-17 21:09:54 +02002035 tasklet_disable(&sc->bcon_tasklet);
2036
2037 bf = avp->av_bcbuf;
2038 if (!bf || !bf->bf_mpdu)
2039 goto skip;
2040
2041 status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
2042 if (status == -EINPROGRESS)
2043 goto skip;
2044
2045 sc->beacon.tx_processed = true;
2046 sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
2047
2048skip:
2049 tasklet_enable(&sc->bcon_tasklet);
2050 }
2051
2052 return sc->beacon.tx_last;
2053}
2054
Mohammed Shafi Shajakhan52c94f42011-08-20 17:21:42 +05302055static int ath9k_get_stats(struct ieee80211_hw *hw,
2056 struct ieee80211_low_level_stats *stats)
2057{
2058 struct ath_softc *sc = hw->priv;
2059 struct ath_hw *ah = sc->sc_ah;
2060 struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
2061
2062 stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
2063 stats->dot11RTSFailureCount = mib_stats->rts_bad;
2064 stats->dot11FCSErrorCount = mib_stats->fcs_bad;
2065 stats->dot11RTSSuccessCount = mib_stats->rts_good;
2066 return 0;
2067}
2068
Felix Fietkau43c35282011-09-03 01:40:27 +02002069static u32 fill_chainmask(u32 cap, u32 new)
2070{
2071 u32 filled = 0;
2072 int i;
2073
2074 for (i = 0; cap && new; i++, cap >>= 1) {
2075 if (!(cap & BIT(0)))
2076 continue;
2077
2078 if (new & BIT(0))
2079 filled |= BIT(i);
2080
2081 new >>= 1;
2082 }
2083
2084 return filled;
2085}
2086
Felix Fietkau5d9c7e32012-07-15 19:53:30 +02002087static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
2088{
Felix Fietkaufea92cb2013-01-20 21:55:22 +01002089 if (AR_SREV_9300_20_OR_LATER(ah))
2090 return true;
2091
Felix Fietkau5d9c7e32012-07-15 19:53:30 +02002092 switch (val & 0x7) {
2093 case 0x1:
2094 case 0x3:
2095 case 0x7:
2096 return true;
2097 case 0x2:
2098 return (ah->caps.rx_chainmask == 1);
2099 default:
2100 return false;
2101 }
2102}
2103
Felix Fietkau43c35282011-09-03 01:40:27 +02002104static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2105{
2106 struct ath_softc *sc = hw->priv;
2107 struct ath_hw *ah = sc->sc_ah;
2108
Felix Fietkau5d9c7e32012-07-15 19:53:30 +02002109 if (ah->caps.rx_chainmask != 1)
2110 rx_ant |= tx_ant;
2111
2112 if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
Felix Fietkau43c35282011-09-03 01:40:27 +02002113 return -EINVAL;
2114
2115 sc->ant_rx = rx_ant;
2116 sc->ant_tx = tx_ant;
2117
2118 if (ah->caps.rx_chainmask == 1)
2119 return 0;
2120
2121 /* AR9100 runs into calibration issues if not all rx chains are enabled */
2122 if (AR_SREV_9100(ah))
2123 ah->rxchainmask = 0x7;
2124 else
2125 ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
2126
2127 ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
Oleksij Rempelb57ba3b2014-02-25 14:48:55 +01002128 ath9k_cmn_reload_chainmask(ah);
Felix Fietkau43c35282011-09-03 01:40:27 +02002129
2130 return 0;
2131}
2132
2133static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2134{
2135 struct ath_softc *sc = hw->priv;
2136
2137 *tx_ant = sc->ant_tx;
2138 *rx_ant = sc->ant_rx;
2139 return 0;
2140}
2141
Simon Wunderliche93d0832013-01-08 14:48:58 +01002142static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2143{
2144 struct ath_softc *sc = hw->priv;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01002145 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2146 set_bit(ATH_OP_SCANNING, &common->op_flags);
Simon Wunderliche93d0832013-01-08 14:48:58 +01002147}
2148
2149static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2150{
2151 struct ath_softc *sc = hw->priv;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01002152 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2153 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Simon Wunderliche93d0832013-01-08 14:48:58 +01002154}
Mohammed Shafi Shajakhanb11e6402012-07-10 14:56:52 +05302155
Felix Fietkau78b21942014-06-11 16:17:55 +05302156static void
2157ath_scan_next_channel(struct ath_softc *sc)
2158{
2159 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
2160 struct ieee80211_channel *chan;
2161
2162 if (sc->offchannel.scan_idx >= req->n_channels) {
2163 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
2164 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc), NULL);
2165 return;
2166 }
2167
2168 chan = req->channels[sc->offchannel.scan_idx++];
2169 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
2170 ath_chanctx_offchan_switch(sc, chan);
2171}
2172
Felix Fietkau405393c2014-06-11 16:17:56 +05302173static void ath_offchannel_next(struct ath_softc *sc)
2174{
2175 struct ieee80211_vif *vif;
2176
2177 if (sc->offchannel.scan_req) {
2178 vif = sc->offchannel.scan_vif;
2179 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
2180 ath_scan_next_channel(sc);
2181 } else if (sc->offchannel.roc_vif) {
2182 vif = sc->offchannel.roc_vif;
2183 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
2184 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
2185 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
2186 } else {
2187 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc), NULL);
2188 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
2189 if (sc->ps_idle)
2190 ath_cancel_work(sc);
2191 }
2192}
2193
2194static void ath_roc_complete(struct ath_softc *sc, bool abort)
2195{
2196 sc->offchannel.roc_vif = NULL;
2197 sc->offchannel.roc_chan = NULL;
2198 if (!abort)
2199 ieee80211_remain_on_channel_expired(sc->hw);
2200 ath_offchannel_next(sc);
2201 ath9k_ps_restore(sc);
2202}
2203
Felix Fietkau78b21942014-06-11 16:17:55 +05302204static void ath_scan_complete(struct ath_softc *sc, bool abort)
2205{
2206 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2207
Felix Fietkau78b21942014-06-11 16:17:55 +05302208 sc->offchannel.scan_req = NULL;
2209 sc->offchannel.scan_vif = NULL;
2210 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
2211 ieee80211_scan_completed(sc->hw, abort);
2212 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Felix Fietkau405393c2014-06-11 16:17:56 +05302213 ath_offchannel_next(sc);
Felix Fietkau78b21942014-06-11 16:17:55 +05302214 ath9k_ps_restore(sc);
Felix Fietkau78b21942014-06-11 16:17:55 +05302215}
2216
2217static void ath_scan_send_probe(struct ath_softc *sc,
2218 struct cfg80211_ssid *ssid)
2219{
2220 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
2221 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
2222 struct ath_tx_control txctl = {};
2223 struct sk_buff *skb;
2224 struct ieee80211_tx_info *info;
2225 int band = sc->offchannel.chan.chandef.chan->band;
2226
2227 skb = ieee80211_probereq_get(sc->hw, vif,
2228 ssid->ssid, ssid->ssid_len, req->ie_len);
2229 if (!skb)
2230 return;
2231
2232 info = IEEE80211_SKB_CB(skb);
2233 if (req->no_cck)
2234 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
2235
2236 if (req->ie_len)
2237 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
2238
2239 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
2240
2241 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
2242 goto error;
2243
2244 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
2245 txctl.force_channel = true;
2246 if (ath_tx_start(sc->hw, skb, &txctl))
2247 goto error;
2248
2249 return;
2250
2251error:
2252 ieee80211_free_txskb(sc->hw, skb);
2253}
2254
2255static void ath_scan_channel_start(struct ath_softc *sc)
2256{
2257 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
2258 int i, dwell;
2259
2260 if ((sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) ||
2261 !req->n_ssids) {
2262 dwell = HZ / 9; /* ~110 ms */
2263 } else {
2264 dwell = HZ / 16; /* ~60 ms */
2265
2266 for (i = 0; i < req->n_ssids; i++)
2267 ath_scan_send_probe(sc, &req->ssids[i]);
2268 }
2269
2270 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
2271 mod_timer(&sc->offchannel.timer, jiffies + dwell);
2272}
2273
2274void ath_offchannel_channel_change(struct ath_softc *sc)
2275{
Felix Fietkau78b21942014-06-11 16:17:55 +05302276 switch (sc->offchannel.state) {
2277 case ATH_OFFCHANNEL_PROBE_SEND:
Felix Fietkau405393c2014-06-11 16:17:56 +05302278 if (!sc->offchannel.scan_req)
2279 return;
2280
Felix Fietkau78b21942014-06-11 16:17:55 +05302281 if (sc->cur_chan->chandef.chan !=
2282 sc->offchannel.chan.chandef.chan)
2283 return;
2284
2285 ath_scan_channel_start(sc);
2286 break;
2287 case ATH_OFFCHANNEL_IDLE:
Felix Fietkau405393c2014-06-11 16:17:56 +05302288 if (!sc->offchannel.scan_req)
2289 return;
2290
Felix Fietkau78b21942014-06-11 16:17:55 +05302291 ath_scan_complete(sc, false);
2292 break;
Felix Fietkau405393c2014-06-11 16:17:56 +05302293 case ATH_OFFCHANNEL_ROC_START:
2294 if (sc->cur_chan != &sc->offchannel.chan)
2295 break;
2296
2297 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
2298 mod_timer(&sc->offchannel.timer, jiffies +
2299 msecs_to_jiffies(sc->offchannel.roc_duration));
2300 ieee80211_ready_on_channel(sc->hw);
2301 break;
2302 case ATH_OFFCHANNEL_ROC_DONE:
2303 ath_roc_complete(sc, false);
2304 break;
Felix Fietkau78b21942014-06-11 16:17:55 +05302305 default:
2306 break;
2307 }
2308}
2309
2310void ath_offchannel_timer(unsigned long data)
2311{
2312 struct ath_softc *sc = (struct ath_softc *)data;
2313 struct ath_chanctx *ctx = ath_chanctx_get_oper_chan(sc);
2314
Felix Fietkau78b21942014-06-11 16:17:55 +05302315 switch (sc->offchannel.state) {
2316 case ATH_OFFCHANNEL_PROBE_WAIT:
Felix Fietkau405393c2014-06-11 16:17:56 +05302317 if (!sc->offchannel.scan_req)
2318 return;
2319
Felix Fietkau78b21942014-06-11 16:17:55 +05302320 if (ctx->active) {
2321 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
2322 ath_chanctx_switch(sc, ctx, NULL);
2323 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
2324 break;
2325 }
2326 /* fall through */
2327 case ATH_OFFCHANNEL_SUSPEND:
Felix Fietkau405393c2014-06-11 16:17:56 +05302328 if (!sc->offchannel.scan_req)
2329 return;
2330
Felix Fietkau78b21942014-06-11 16:17:55 +05302331 ath_scan_next_channel(sc);
2332 break;
Felix Fietkau405393c2014-06-11 16:17:56 +05302333 case ATH_OFFCHANNEL_ROC_START:
2334 case ATH_OFFCHANNEL_ROC_WAIT:
2335 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
2336 ath_chanctx_switch(sc, ctx, NULL);
2337 break;
Felix Fietkau78b21942014-06-11 16:17:55 +05302338 default:
2339 break;
2340 }
2341}
2342
2343static int ath9k_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2344 struct cfg80211_scan_request *req)
2345{
2346 struct ath_softc *sc = hw->priv;
2347 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2348 int ret = 0;
2349
2350 mutex_lock(&sc->mutex);
2351
2352 if (WARN_ON(sc->offchannel.scan_req)) {
2353 ret = -EBUSY;
2354 goto out;
2355 }
2356
2357 ath9k_ps_wakeup(sc);
2358 set_bit(ATH_OP_SCANNING, &common->op_flags);
2359 sc->offchannel.scan_vif = vif;
2360 sc->offchannel.scan_req = req;
2361 sc->offchannel.scan_idx = 0;
Felix Fietkau78b21942014-06-11 16:17:55 +05302362
Felix Fietkau405393c2014-06-11 16:17:56 +05302363 if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE)
2364 ath_offchannel_next(sc);
Felix Fietkau78b21942014-06-11 16:17:55 +05302365
2366out:
2367 mutex_unlock(&sc->mutex);
2368
2369 return ret;
2370}
2371
2372static void ath9k_cancel_hw_scan(struct ieee80211_hw *hw,
2373 struct ieee80211_vif *vif)
2374{
2375 struct ath_softc *sc = hw->priv;
2376
2377 mutex_lock(&sc->mutex);
2378 del_timer_sync(&sc->offchannel.timer);
2379 ath_scan_complete(sc, true);
2380 mutex_unlock(&sc->mutex);
2381}
2382
Felix Fietkau405393c2014-06-11 16:17:56 +05302383static int ath9k_remain_on_channel(struct ieee80211_hw *hw,
2384 struct ieee80211_vif *vif,
2385 struct ieee80211_channel *chan, int duration,
2386 enum ieee80211_roc_type type)
2387{
2388 struct ath_softc *sc = hw->priv;
2389 int ret = 0;
2390
2391 mutex_lock(&sc->mutex);
2392
2393 if (WARN_ON(sc->offchannel.roc_vif)) {
2394 ret = -EBUSY;
2395 goto out;
2396 }
2397
2398 ath9k_ps_wakeup(sc);
2399 sc->offchannel.roc_vif = vif;
2400 sc->offchannel.roc_chan = chan;
2401 sc->offchannel.roc_duration = duration;
2402
2403 if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE)
2404 ath_offchannel_next(sc);
2405
2406out:
2407 mutex_unlock(&sc->mutex);
2408
2409 return ret;
2410}
2411
2412static int ath9k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2413{
2414 struct ath_softc *sc = hw->priv;
2415
2416 mutex_lock(&sc->mutex);
2417
2418 del_timer_sync(&sc->offchannel.timer);
2419
2420 if (sc->offchannel.roc_vif) {
2421 if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
2422 ath_roc_complete(sc, true);
2423 }
2424
2425 mutex_unlock(&sc->mutex);
2426
2427 return 0;
2428}
2429
Felix Fietkau39305632014-06-11 16:17:57 +05302430static int ath9k_add_chanctx(struct ieee80211_hw *hw,
2431 struct ieee80211_chanctx_conf *conf)
2432{
2433 struct ath_softc *sc = hw->priv;
2434 struct ath_chanctx *ctx, **ptr;
2435 int i;
2436
2437 mutex_lock(&sc->mutex);
2438 for (i = 0; i < ATH9K_NUM_CHANCTX; i++) {
2439 if (!sc->chanctx[i].assigned)
2440 break;
2441 }
2442 if (i == ATH9K_NUM_CHANCTX) {
2443 mutex_unlock(&sc->mutex);
2444 return -ENOSPC;
2445 }
2446
2447 ctx = &sc->chanctx[i];
2448 ptr = (void *) conf->drv_priv;
2449 *ptr = ctx;
2450 ctx->assigned = true;
2451 ath_chanctx_set_channel(sc, ctx, &conf->def);
2452 mutex_unlock(&sc->mutex);
2453
2454 return 0;
2455}
2456
2457
2458static void ath9k_remove_chanctx(struct ieee80211_hw *hw,
2459 struct ieee80211_chanctx_conf *conf)
2460{
2461 struct ath_softc *sc = hw->priv;
2462 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2463
2464 mutex_lock(&sc->mutex);
2465 ctx->assigned = false;
2466 mutex_unlock(&sc->mutex);
2467}
2468
2469static void ath9k_change_chanctx(struct ieee80211_hw *hw,
2470 struct ieee80211_chanctx_conf *conf,
2471 u32 changed)
2472{
2473 struct ath_softc *sc = hw->priv;
2474 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2475
2476 mutex_lock(&sc->mutex);
2477 ath_chanctx_set_channel(sc, ctx, &conf->def);
2478 mutex_unlock(&sc->mutex);
2479}
2480
2481static int ath9k_assign_vif_chanctx(struct ieee80211_hw *hw,
2482 struct ieee80211_vif *vif,
2483 struct ieee80211_chanctx_conf *conf)
2484{
2485 struct ath_softc *sc = hw->priv;
2486 struct ath_vif *avp = (void *)vif->drv_priv;
2487 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2488
2489 mutex_lock(&sc->mutex);
2490 avp->chanctx = ctx;
2491 list_add_tail(&avp->list, &ctx->vifs);
2492 ath_chanctx_check_active(sc, ctx);
2493 mutex_unlock(&sc->mutex);
2494
2495 return 0;
2496}
2497
2498static void ath9k_unassign_vif_chanctx(struct ieee80211_hw *hw,
2499 struct ieee80211_vif *vif,
2500 struct ieee80211_chanctx_conf *conf)
2501{
2502 struct ath_softc *sc = hw->priv;
2503 struct ath_vif *avp = (void *)vif->drv_priv;
2504 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2505
2506 mutex_lock(&sc->mutex);
2507 avp->chanctx = NULL;
2508 list_del(&avp->list);
2509 ath_chanctx_check_active(sc, ctx);
2510 mutex_unlock(&sc->mutex);
2511}
2512
Felix Fietkau78b21942014-06-11 16:17:55 +05302513void ath9k_fill_chanctx_ops(void)
2514{
2515 if (!ath9k_use_chanctx)
2516 return;
2517
2518 ath9k_ops.hw_scan = ath9k_hw_scan;
2519 ath9k_ops.cancel_hw_scan = ath9k_cancel_hw_scan;
Felix Fietkau405393c2014-06-11 16:17:56 +05302520 ath9k_ops.remain_on_channel = ath9k_remain_on_channel;
2521 ath9k_ops.cancel_remain_on_channel = ath9k_cancel_remain_on_channel;
Felix Fietkau39305632014-06-11 16:17:57 +05302522 ath9k_ops.add_chanctx = ath9k_add_chanctx;
2523 ath9k_ops.remove_chanctx = ath9k_remove_chanctx;
2524 ath9k_ops.change_chanctx = ath9k_change_chanctx;
2525 ath9k_ops.assign_vif_chanctx = ath9k_assign_vif_chanctx;
2526 ath9k_ops.unassign_vif_chanctx = ath9k_unassign_vif_chanctx;
Felix Fietkau78b21942014-06-11 16:17:55 +05302527}
2528
Gabor Juhos6baff7f2009-01-14 20:17:06 +01002529struct ieee80211_ops ath9k_ops = {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002530 .tx = ath9k_tx,
2531 .start = ath9k_start,
2532 .stop = ath9k_stop,
2533 .add_interface = ath9k_add_interface,
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05302534 .change_interface = ath9k_change_interface,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002535 .remove_interface = ath9k_remove_interface,
2536 .config = ath9k_config,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002537 .configure_filter = ath9k_configure_filter,
Johannes Berg4ca77862010-02-19 19:06:56 +01002538 .sta_add = ath9k_sta_add,
2539 .sta_remove = ath9k_sta_remove,
Felix Fietkau55195412011-04-17 23:28:09 +02002540 .sta_notify = ath9k_sta_notify,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002541 .conf_tx = ath9k_conf_tx,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002542 .bss_info_changed = ath9k_bss_info_changed,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002543 .set_key = ath9k_set_key,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002544 .get_tsf = ath9k_get_tsf,
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01002545 .set_tsf = ath9k_set_tsf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002546 .reset_tsf = ath9k_reset_tsf,
Johannes Berg4233df62008-10-13 13:35:05 +02002547 .ampdu_action = ath9k_ampdu_action,
Benoit Papillault62dad5b2010-04-28 00:08:24 +02002548 .get_survey = ath9k_get_survey,
Johannes Berg3b319aa2009-06-13 14:50:26 +05302549 .rfkill_poll = ath9k_rfkill_poll_state,
Felix Fietkaue239d852010-01-15 02:34:58 +01002550 .set_coverage_class = ath9k_set_coverage_class,
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08002551 .flush = ath9k_flush,
Vivek Natarajan15b91e82011-04-06 11:41:11 +05302552 .tx_frames_pending = ath9k_tx_frames_pending,
Mohammed Shafi Shajakhan52c94f42011-08-20 17:21:42 +05302553 .tx_last_beacon = ath9k_tx_last_beacon,
Felix Fietkau86a22ac2013-06-07 18:12:01 +02002554 .release_buffered_frames = ath9k_release_buffered_frames,
Mohammed Shafi Shajakhan52c94f42011-08-20 17:21:42 +05302555 .get_stats = ath9k_get_stats,
Felix Fietkau43c35282011-09-03 01:40:27 +02002556 .set_antenna = ath9k_set_antenna,
2557 .get_antenna = ath9k_get_antenna,
Ben Greearb90bd9d2012-05-15 15:33:25 -07002558
Sujith Manoharane60001e2013-10-28 12:22:04 +05302559#ifdef CONFIG_ATH9K_WOW
Mohammed Shafi Shajakhanb11e6402012-07-10 14:56:52 +05302560 .suspend = ath9k_suspend,
2561 .resume = ath9k_resume,
2562 .set_wakeup = ath9k_set_wakeup,
2563#endif
2564
Ben Greearb90bd9d2012-05-15 15:33:25 -07002565#ifdef CONFIG_ATH9K_DEBUGFS
2566 .get_et_sset_count = ath9k_get_et_sset_count,
Sujith Manoharana145daf2012-11-28 15:08:54 +05302567 .get_et_stats = ath9k_get_et_stats,
2568 .get_et_strings = ath9k_get_et_strings,
2569#endif
2570
Sujith Manoharan1cdbaf02014-01-13 07:29:27 +05302571#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
Sujith Manoharana145daf2012-11-28 15:08:54 +05302572 .sta_add_debugfs = ath9k_sta_add_debugfs,
Ben Greearb90bd9d2012-05-15 15:33:25 -07002573#endif
Simon Wunderliche93d0832013-01-08 14:48:58 +01002574 .sw_scan_start = ath9k_sw_scan_start,
2575 .sw_scan_complete = ath9k_sw_scan_complete,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002576};