blob: 6abdf99ffae41539d82a4a9e790fdd5cd3cef631 [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) {
Felix Fietkau8d7e09d2014-06-11 16:18:01 +0530243 /* restore per chanctx TSF timer */
244 if (sc->cur_chan->tsf_val) {
245 u32 offset;
246
247 offset = ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts,
248 NULL);
249 ath9k_hw_settsf64(ah, sc->cur_chan->tsf_val + offset);
250 }
251
252
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100253 if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
Sujith Manoharan196fb862012-06-04 20:24:13 +0530254 goto work;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200255
Sujith Manoharan196fb862012-06-04 20:24:13 +0530256 if (ah->opmode == NL80211_IFTYPE_STATION &&
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100257 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
Sujith Manoharan196fb862012-06-04 20:24:13 +0530258 spin_lock_irqsave(&sc->sc_pm_lock, flags);
259 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
260 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Sujith Manoharana6768282013-05-06 10:09:03 +0530261 } else {
262 ath9k_set_beacon(sc);
Sujith Manoharan196fb862012-06-04 20:24:13 +0530263 }
264 work:
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530265 ath_restart_work(sc);
Felix Fietkau04535312014-06-11 16:17:51 +0530266 ath_txq_schedule_all(sc);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200267 }
268
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530269 sc->gtt_cnt = 0;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200270 ieee80211_wake_queues(sc->hw);
271
Felix Fietkaud463af42014-04-06 00:37:03 +0200272 ath9k_p2p_ps_timer(sc);
273
Felix Fietkau9adcf442011-09-03 01:40:26 +0200274 return true;
275}
276
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530277int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
Felix Fietkau9adcf442011-09-03 01:40:26 +0200278{
279 struct ath_hw *ah = sc->sc_ah;
280 struct ath_common *common = ath9k_hw_common(ah);
281 struct ath9k_hw_cal_data *caldata = NULL;
282 bool fastcc = true;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200283 int r;
284
285 __ath_cancel_work(sc);
286
Felix Fietkau4668cce2013-01-14 16:56:46 +0100287 tasklet_disable(&sc->intr_tq);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200288 spin_lock_bh(&sc->sc_pcu_lock);
289
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530290 if (!sc->cur_chan->offchannel) {
Felix Fietkau9adcf442011-09-03 01:40:26 +0200291 fastcc = false;
Felix Fietkaub01459e2014-06-11 16:17:59 +0530292 caldata = &sc->cur_chan->caldata;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200293 }
294
295 if (!hchan) {
296 fastcc = false;
Felix Fietkau9adcf442011-09-03 01:40:26 +0200297 hchan = ah->curchan;
298 }
299
John W. Linville9ebea382013-01-28 13:54:03 -0500300 if (!ath_prepare_reset(sc))
Felix Fietkau9adcf442011-09-03 01:40:26 +0200301 fastcc = false;
302
Felix Fietkaubff11762014-06-11 16:17:52 +0530303 if (hchan) {
304 spin_lock_bh(&sc->chan_lock);
305 sc->cur_chandef = sc->cur_chan->chandef;
306 spin_unlock_bh(&sc->chan_lock);
307 }
308
Joe Perchesd2182b62011-12-15 14:55:53 -0800309 ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
Sujith Manoharanfeced202012-01-30 14:21:42 +0530310 hchan->channel, IS_CHAN_HT40(hchan), fastcc);
Felix Fietkau9adcf442011-09-03 01:40:26 +0200311
312 r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
313 if (r) {
314 ath_err(common,
315 "Unable to reset channel, reset status %d\n", r);
Robert Shadef50b1cd2013-04-02 19:52:45 -0400316
317 ath9k_hw_enable_interrupts(ah);
318 ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
319
Felix Fietkau9adcf442011-09-03 01:40:26 +0200320 goto out;
321 }
322
Rajkumar Manoharane82cb032012-10-12 14:07:25 +0530323 if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530324 sc->cur_chan->offchannel)
Rajkumar Manoharane82cb032012-10-12 14:07:25 +0530325 ath9k_mci_set_txpower(sc, true, false);
326
Felix Fietkau9adcf442011-09-03 01:40:26 +0200327 if (!ath_complete_reset(sc, true))
328 r = -EIO;
329
330out:
331 spin_unlock_bh(&sc->sc_pcu_lock);
Felix Fietkau4668cce2013-01-14 16:56:46 +0100332 tasklet_enable(&sc->intr_tq);
333
Felix Fietkau9adcf442011-09-03 01:40:26 +0200334 return r;
335}
336
Ben Greear7e1e3862011-11-03 11:33:13 -0700337static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
338 struct ieee80211_vif *vif)
Sujithff37e332008-11-24 12:07:55 +0530339{
340 struct ath_node *an;
Sujithff37e332008-11-24 12:07:55 +0530341 an = (struct ath_node *)sta->drv_priv;
342
Sujith Manoharana145daf2012-11-28 15:08:54 +0530343 an->sc = sc;
Ben Greear7f010c92011-01-09 23:11:49 -0800344 an->sta = sta;
Ben Greear7e1e3862011-11-03 11:33:13 -0700345 an->vif = vif;
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +0530346 memset(&an->key_idx, 0, sizeof(an->key_idx));
Sujith Manoharan3d4e20f2012-03-14 14:40:58 +0530347
Sujith Manoharandd5ee592013-02-04 15:38:23 +0530348 ath_tx_node_init(sc, an);
Sujithff37e332008-11-24 12:07:55 +0530349}
350
351static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
352{
353 struct ath_node *an = (struct ath_node *)sta->drv_priv;
Sujith Manoharandd5ee592013-02-04 15:38:23 +0530354 ath_tx_node_cleanup(sc, an);
Sujithff37e332008-11-24 12:07:55 +0530355}
356
Sujith55624202010-01-08 10:36:02 +0530357void ath9k_tasklet(unsigned long data)
Sujithff37e332008-11-24 12:07:55 +0530358{
359 struct ath_softc *sc = (struct ath_softc *)data;
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700360 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700361 struct ath_common *common = ath9k_hw_common(ah);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530362 enum ath_reset_type type;
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530363 unsigned long flags;
Sujith17d79042009-02-09 13:27:03 +0530364 u32 status = sc->intrstatus;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400365 u32 rxmask;
Sujithff37e332008-11-24 12:07:55 +0530366
Felix Fietkaue3927002011-09-14 21:23:01 +0200367 ath9k_ps_wakeup(sc);
368 spin_lock(&sc->sc_pcu_lock);
369
Sujith Manoharan6549a862013-12-24 10:44:24 +0530370 if (status & ATH9K_INT_FATAL) {
371 type = RESET_TYPE_FATAL_INT;
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530372 ath9k_queue_reset(sc, type);
Sujith Manoharanc6cc47b2013-09-16 10:37:00 +0530373
374 /*
375 * Increment the ref. counter here so that
376 * interrupts are enabled in the reset routine.
377 */
378 atomic_inc(&ah->intr_ref_cnt);
Felix Fietkauaffad452014-02-22 14:52:49 +0100379 ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
Felix Fietkaue3927002011-09-14 21:23:01 +0200380 goto out;
Sujithff37e332008-11-24 12:07:55 +0530381 }
382
Sujith Manoharan6549a862013-12-24 10:44:24 +0530383 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
384 (status & ATH9K_INT_BB_WATCHDOG)) {
Sujith Manoharan0c759972013-12-24 10:44:26 +0530385 spin_lock(&common->cc_lock);
386 ath_hw_cycle_counters_update(common);
387 ar9003_hw_bb_watchdog_dbg_info(ah);
388 spin_unlock(&common->cc_lock);
389
Sujith Manoharan6549a862013-12-24 10:44:24 +0530390 if (ar9003_hw_bb_watchdog_check(ah)) {
391 type = RESET_TYPE_BB_WATCHDOG;
392 ath9k_queue_reset(sc, type);
393
394 /*
395 * Increment the ref. counter here so that
396 * interrupts are enabled in the reset routine.
397 */
398 atomic_inc(&ah->intr_ref_cnt);
Felix Fietkauaffad452014-02-22 14:52:49 +0100399 ath_dbg(common, RESET,
Sujith Manoharan6549a862013-12-24 10:44:24 +0530400 "BB_WATCHDOG: Skipping interrupts\n");
401 goto out;
402 }
403 }
404
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530405 if (status & ATH9K_INT_GTT) {
406 sc->gtt_cnt++;
407
408 if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
409 type = RESET_TYPE_TX_GTT;
410 ath9k_queue_reset(sc, type);
411 atomic_inc(&ah->intr_ref_cnt);
Felix Fietkauaffad452014-02-22 14:52:49 +0100412 ath_dbg(common, RESET,
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530413 "GTT: Skipping interrupts\n");
414 goto out;
415 }
416 }
417
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530418 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Rajkumar Manoharan4105f802011-05-06 18:27:47 +0530419 if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
420 /*
421 * TSF sync does not look correct; remain awake to sync with
422 * the next Beacon.
423 */
Joe Perchesd2182b62011-12-15 14:55:53 -0800424 ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
Rajkumar Manoharane8fe7332011-08-05 18:59:41 +0530425 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
Rajkumar Manoharan4105f802011-05-06 18:27:47 +0530426 }
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530427 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Rajkumar Manoharan4105f802011-05-06 18:27:47 +0530428
Felix Fietkaub5c804752010-04-15 17:38:48 -0400429 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
430 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
431 ATH9K_INT_RXORN);
432 else
433 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
434
435 if (status & rxmask) {
Felix Fietkaub5c804752010-04-15 17:38:48 -0400436 /* Check for high priority Rx first */
437 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
438 (status & ATH9K_INT_RXHP))
439 ath_rx_tasklet(sc, 0, true);
440
441 ath_rx_tasklet(sc, 0, false);
Sujith063d8be2009-03-30 15:28:49 +0530442 }
443
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400444 if (status & ATH9K_INT_TX) {
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530445 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
446 /*
447 * For EDMA chips, TX completion is enabled for the
448 * beacon queue, so if a beacon has been transmitted
449 * successfully after a GTT interrupt, the GTT counter
450 * gets reset to zero here.
451 */
Sujith Manoharan3b745c72014-01-20 13:25:28 +0530452 sc->gtt_cnt = 0;
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530453
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400454 ath_tx_edma_tasklet(sc);
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530455 } else {
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400456 ath_tx_tasklet(sc);
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530457 }
Felix Fietkau10e23182013-11-11 22:23:35 +0100458
459 wake_up(&sc->tx_wait);
Vasanthakumar Thiagarajane5003242010-04-15 17:39:36 -0400460 }
Sujith063d8be2009-03-30 15:28:49 +0530461
Felix Fietkauc67ce332013-12-14 18:03:38 +0100462 if (status & ATH9K_INT_GENTIMER)
463 ath_gen_timer_isr(sc->sc_ah);
464
Sujith Manoharan56ca0db2012-02-22 12:40:32 +0530465 ath9k_btcoex_handle_interrupt(sc, status);
Mohammed Shafi Shajakhan19686dd2011-11-30 10:41:28 +0530466
Sujithff37e332008-11-24 12:07:55 +0530467 /* re-enable hardware interrupt */
Felix Fietkau4df30712010-11-08 20:54:47 +0100468 ath9k_hw_enable_interrupts(ah);
Sujith Manoharanc6cc47b2013-09-16 10:37:00 +0530469out:
Senthil Balasubramanian52671e42010-12-23 21:06:57 +0530470 spin_unlock(&sc->sc_pcu_lock);
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400471 ath9k_ps_restore(sc);
Sujithff37e332008-11-24 12:07:55 +0530472}
473
Gabor Juhos6baff7f2009-01-14 20:17:06 +0100474irqreturn_t ath_isr(int irq, void *dev)
Sujithff37e332008-11-24 12:07:55 +0530475{
Sujith063d8be2009-03-30 15:28:49 +0530476#define SCHED_INTR ( \
477 ATH9K_INT_FATAL | \
Rajkumar Manoharana4d86d92011-05-20 17:52:10 +0530478 ATH9K_INT_BB_WATCHDOG | \
Sujith063d8be2009-03-30 15:28:49 +0530479 ATH9K_INT_RXORN | \
480 ATH9K_INT_RXEOL | \
481 ATH9K_INT_RX | \
Felix Fietkaub5c804752010-04-15 17:38:48 -0400482 ATH9K_INT_RXLP | \
483 ATH9K_INT_RXHP | \
Sujith063d8be2009-03-30 15:28:49 +0530484 ATH9K_INT_TX | \
485 ATH9K_INT_BMISS | \
486 ATH9K_INT_CST | \
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530487 ATH9K_INT_GTT | \
Vasanthakumar Thiagarajanebb8e1d2009-09-01 17:46:32 +0530488 ATH9K_INT_TSFOOR | \
Mohammed Shafi Shajakhan40dc5392011-11-30 10:41:18 +0530489 ATH9K_INT_GENTIMER | \
490 ATH9K_INT_MCI)
Sujith063d8be2009-03-30 15:28:49 +0530491
Sujithff37e332008-11-24 12:07:55 +0530492 struct ath_softc *sc = dev;
Sujithcbe61d82009-02-09 13:27:12 +0530493 struct ath_hw *ah = sc->sc_ah;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100494 struct ath_common *common = ath9k_hw_common(ah);
Sujithff37e332008-11-24 12:07:55 +0530495 enum ath9k_int status;
Sujith Manoharan78c8a952013-12-28 09:47:15 +0530496 u32 sync_cause = 0;
Sujithff37e332008-11-24 12:07:55 +0530497 bool sched = false;
498
Sujith063d8be2009-03-30 15:28:49 +0530499 /*
500 * The hardware is not ready/present, don't
501 * touch anything. Note this can happen early
502 * on if the IRQ is shared.
503 */
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100504 if (test_bit(ATH_OP_INVALID, &common->op_flags))
Sujith063d8be2009-03-30 15:28:49 +0530505 return IRQ_NONE;
Sujithff37e332008-11-24 12:07:55 +0530506
Sujith063d8be2009-03-30 15:28:49 +0530507 /* shared irq, not for us */
Sujithff37e332008-11-24 12:07:55 +0530508
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400509 if (!ath9k_hw_intrpend(ah))
Sujith063d8be2009-03-30 15:28:49 +0530510 return IRQ_NONE;
Sujithff37e332008-11-24 12:07:55 +0530511
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100512 if (test_bit(ATH_OP_HW_RESET, &common->op_flags)) {
Felix Fietkauf41a9b32012-08-08 16:25:03 +0200513 ath9k_hw_kill_interrupts(ah);
Sujith Manoharanb74713d2012-06-04 20:24:01 +0530514 return IRQ_HANDLED;
Felix Fietkauf41a9b32012-08-08 16:25:03 +0200515 }
Sujith Manoharanb74713d2012-06-04 20:24:01 +0530516
Sujith063d8be2009-03-30 15:28:49 +0530517 /*
518 * Figure out the reason(s) for the interrupt. Note
519 * that the hal returns a pseudo-ISR that may include
520 * bits we haven't explicitly enabled so we mask the
521 * value to insure we only process bits we requested.
522 */
Felix Fietkau6a4d05d2013-12-19 18:01:48 +0100523 ath9k_hw_getisr(ah, &status, &sync_cause); /* NB: clears ISR too */
524 ath9k_debug_sync_cause(sc, sync_cause);
Pavel Roskin30691682010-03-31 18:05:31 -0400525 status &= ah->imask; /* discard unasked-for bits */
Sujith063d8be2009-03-30 15:28:49 +0530526
527 /*
528 * If there are no status bits set, then this interrupt was not
529 * for me (should have been caught above).
530 */
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400531 if (!status)
Sujith063d8be2009-03-30 15:28:49 +0530532 return IRQ_NONE;
Sujith063d8be2009-03-30 15:28:49 +0530533
534 /* Cache the status */
535 sc->intrstatus = status;
536
537 if (status & SCHED_INTR)
538 sched = true;
539
540 /*
541 * If a FATAL or RXORN interrupt is received, we have to reset the
542 * chip immediately.
543 */
Felix Fietkaub5c804752010-04-15 17:38:48 -0400544 if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
545 !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
Sujith063d8be2009-03-30 15:28:49 +0530546 goto chip_reset;
547
Sujith Manoharana6bb8602013-12-24 10:44:22 +0530548 if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
Sujith Manoharan0c759972013-12-24 10:44:26 +0530549 (status & ATH9K_INT_BB_WATCHDOG))
Luis R. Rodriguez08578b82010-05-13 13:33:44 -0400550 goto chip_reset;
Sujith Manoharane60001e2013-10-28 12:22:04 +0530551
552#ifdef CONFIG_ATH9K_WOW
Rajkumar Manoharanca90ef42012-11-20 18:29:59 +0530553 if (status & ATH9K_INT_BMISS) {
554 if (atomic_read(&sc->wow_sleep_proc_intr) == 0) {
Rajkumar Manoharanca90ef42012-11-20 18:29:59 +0530555 atomic_inc(&sc->wow_got_bmiss_intr);
556 atomic_dec(&sc->wow_sleep_proc_intr);
557 }
558 }
559#endif
Sujith Manoharane60001e2013-10-28 12:22:04 +0530560
Sujith063d8be2009-03-30 15:28:49 +0530561 if (status & ATH9K_INT_SWBA)
562 tasklet_schedule(&sc->bcon_tasklet);
563
564 if (status & ATH9K_INT_TXURN)
565 ath9k_hw_updatetxtriglevel(ah, true);
566
Rajkumar Manoharan0682c9b2011-08-13 10:28:09 +0530567 if (status & ATH9K_INT_RXEOL) {
568 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
Felix Fietkau72d874c2011-10-08 20:06:19 +0200569 ath9k_hw_set_interrupts(ah);
Felix Fietkaub5c804752010-04-15 17:38:48 -0400570 }
571
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400572 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
573 if (status & ATH9K_INT_TIM_TIMER) {
Luis R. Rodriguezff9f0b62010-12-07 15:13:22 -0800574 if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
575 goto chip_reset;
Sujith063d8be2009-03-30 15:28:49 +0530576 /* Clear RxAbort bit so that we can
577 * receive frames */
Luis R. Rodriguez9ecdef42009-09-09 21:10:09 -0700578 ath9k_setpower(sc, ATH9K_PM_AWAKE);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530579 spin_lock(&sc->sc_pm_lock);
Vasanthakumar Thiagarajan153e0802009-05-15 02:47:16 -0400580 ath9k_hw_setrxabort(sc->sc_ah, 0);
Sujith1b04b932010-01-08 10:36:05 +0530581 sc->ps_flags |= PS_WAIT_FOR_BEACON;
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530582 spin_unlock(&sc->sc_pm_lock);
Sujith063d8be2009-03-30 15:28:49 +0530583 }
Sujith063d8be2009-03-30 15:28:49 +0530584
585chip_reset:
586
Sujith817e11d2008-12-07 21:42:44 +0530587 ath_debug_stat_interrupt(sc, status);
588
Sujithff37e332008-11-24 12:07:55 +0530589 if (sched) {
Felix Fietkau4df30712010-11-08 20:54:47 +0100590 /* turn off every interrupt */
591 ath9k_hw_disable_interrupts(ah);
Sujithff37e332008-11-24 12:07:55 +0530592 tasklet_schedule(&sc->intr_tq);
593 }
594
595 return IRQ_HANDLED;
Sujith063d8be2009-03-30 15:28:49 +0530596
597#undef SCHED_INTR
Sujithff37e332008-11-24 12:07:55 +0530598}
599
Sujith Manoharanef6b19e2013-10-24 12:04:39 +0530600int ath_reset(struct ath_softc *sc)
Sujithff37e332008-11-24 12:07:55 +0530601{
Felix Fietkauec303262013-10-05 14:09:30 +0200602 int r;
Sujithff37e332008-11-24 12:07:55 +0530603
Felix Fietkau783cd012011-01-21 18:52:38 +0100604 ath9k_ps_wakeup(sc);
Felix Fietkau13815592013-01-20 18:51:53 +0100605 r = ath_reset_internal(sc, NULL);
Felix Fietkau783cd012011-01-21 18:52:38 +0100606 ath9k_ps_restore(sc);
Sujith2ab81d42009-12-14 16:34:56 +0530607
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -0800608 return r;
Sujithff37e332008-11-24 12:07:55 +0530609}
610
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530611void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
612{
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100613 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530614#ifdef CONFIG_ATH9K_DEBUGFS
615 RESET_STAT_INC(sc, type);
616#endif
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100617 set_bit(ATH_OP_HW_RESET, &common->op_flags);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530618 ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
619}
620
Felix Fietkau236de512011-09-03 01:40:25 +0200621void ath_reset_work(struct work_struct *work)
622{
623 struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
624
Felix Fietkau13815592013-01-20 18:51:53 +0100625 ath_reset(sc);
Felix Fietkau236de512011-09-03 01:40:25 +0200626}
627
Sujithff37e332008-11-24 12:07:55 +0530628/**********************/
629/* mac80211 callbacks */
630/**********************/
631
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700632static int ath9k_start(struct ieee80211_hw *hw)
633{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100634 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700635 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700636 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau39305632014-06-11 16:17:57 +0530637 struct ieee80211_channel *curchan = sc->cur_chan->chandef.chan;
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530638 struct ath_chanctx *ctx = sc->cur_chan;
Sujithff37e332008-11-24 12:07:55 +0530639 struct ath9k_channel *init_channel;
Vasanthakumar Thiagarajan82880a72009-06-13 14:50:24 +0530640 int r;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641
Joe Perchesd2182b62011-12-15 14:55:53 -0800642 ath_dbg(common, CONFIG,
Joe Perches226afe62010-12-02 19:12:37 -0800643 "Starting driver with initial channel: %d MHz\n",
644 curchan->center_freq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700645
Felix Fietkauf62d8162011-03-25 17:43:41 +0100646 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +0530647 mutex_lock(&sc->mutex);
648
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530649 init_channel = ath9k_cmn_get_channel(hw, ah, &ctx->chandef);
Felix Fietkaubff11762014-06-11 16:17:52 +0530650 sc->cur_chandef = hw->conf.chandef;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700651
Sujithff37e332008-11-24 12:07:55 +0530652 /* Reset SERDES registers */
Stanislaw Gruszka84c87dc2011-08-05 13:10:32 +0200653 ath9k_hw_configpcipowersave(ah, false);
Sujithff37e332008-11-24 12:07:55 +0530654
655 /*
656 * The basic interface to setting the hardware in a good
657 * state is ``reset''. On return the hardware is known to
658 * be powered up and with interrupts disabled. This must
659 * be followed by initialization of the appropriate bits
660 * and then setup of the interrupt mask.
661 */
Luis R. Rodriguez4bdd1e92010-10-26 15:27:24 -0700662 spin_lock_bh(&sc->sc_pcu_lock);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100663
664 atomic_set(&ah->intr_ref_cnt, -1);
665
Felix Fietkau20bd2a02010-07-31 00:12:00 +0200666 r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
Luis R. Rodriguezae8d2852008-12-23 15:58:40 -0800667 if (r) {
Joe Perches38002762010-12-02 19:12:36 -0800668 ath_err(common,
669 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
670 r, curchan->center_freq);
Felix Fietkauceb26a62012-10-03 21:07:51 +0200671 ah->reset_power_on = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700672 }
Sujithff37e332008-11-24 12:07:55 +0530673
Sujithff37e332008-11-24 12:07:55 +0530674 /* Setup our intr mask. */
Felix Fietkaub5c804752010-04-15 17:38:48 -0400675 ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
676 ATH9K_INT_RXORN | ATH9K_INT_FATAL |
677 ATH9K_INT_GLOBAL;
678
679 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
Luis R. Rodriguez08578b82010-05-13 13:33:44 -0400680 ah->imask |= ATH9K_INT_RXHP |
Sujith Manoharana6bb8602013-12-24 10:44:22 +0530681 ATH9K_INT_RXLP;
Felix Fietkaub5c804752010-04-15 17:38:48 -0400682 else
683 ah->imask |= ATH9K_INT_RX;
Sujithff37e332008-11-24 12:07:55 +0530684
Sujith Manoharana6bb8602013-12-24 10:44:22 +0530685 if (ah->config.hw_hang_checks & HW_BB_WATCHDOG)
686 ah->imask |= ATH9K_INT_BB_WATCHDOG;
687
Sujith Manoharan071aa9a2014-01-13 13:55:11 +0530688 /*
689 * Enable GTT interrupts only for AR9003/AR9004 chips
690 * for now.
691 */
692 if (AR_SREV_9300_20_OR_LATER(ah))
693 ah->imask |= ATH9K_INT_GTT;
Sujithff37e332008-11-24 12:07:55 +0530694
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700695 if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
Pavel Roskin30691682010-03-31 18:05:31 -0400696 ah->imask |= ATH9K_INT_CST;
Sujithff37e332008-11-24 12:07:55 +0530697
Sujith Manoharane270e772012-06-04 16:27:19 +0530698 ath_mci_enable(sc);
Mohammed Shafi Shajakhan40dc5392011-11-30 10:41:18 +0530699
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100700 clear_bit(ATH_OP_INVALID, &common->op_flags);
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +0530701 sc->sc_ah->is_monitoring = false;
Sujithff37e332008-11-24 12:07:55 +0530702
Felix Fietkauceb26a62012-10-03 21:07:51 +0200703 if (!ath_complete_reset(sc, false))
704 ah->reset_power_on = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700705
Felix Fietkauc0c11742011-11-16 13:08:41 +0100706 if (ah->led_pin >= 0) {
707 ath9k_hw_cfg_output(ah, ah->led_pin,
708 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
709 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
710 }
711
712 /*
713 * Reset key cache to sane defaults (all entries cleared) instead of
714 * semi-random values after suspend/resume.
715 */
716 ath9k_cmn_init_crypto(sc->sc_ah);
717
Felix Fietkaua35051c2013-12-14 18:03:45 +0100718 ath9k_hw_reset_tsf(ah);
719
Felix Fietkau9adcf442011-09-03 01:40:26 +0200720 spin_unlock_bh(&sc->sc_pcu_lock);
Senthil Balasubramanian164ace32009-07-14 20:17:09 -0400721
Sujith141b38b2009-02-04 08:10:07 +0530722 mutex_unlock(&sc->mutex);
723
Felix Fietkauf62d8162011-03-25 17:43:41 +0100724 ath9k_ps_restore(sc);
725
Felix Fietkauceb26a62012-10-03 21:07:51 +0200726 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700727}
728
Thomas Huehn36323f82012-07-23 21:33:42 +0200729static void ath9k_tx(struct ieee80211_hw *hw,
730 struct ieee80211_tx_control *control,
731 struct sk_buff *skb)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700732{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100733 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700734 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith528f0c62008-10-29 10:14:26 +0530735 struct ath_tx_control txctl;
Benoit Papillault1bc14882009-11-24 15:49:18 +0100736 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530737 unsigned long flags;
Sujith528f0c62008-10-29 10:14:26 +0530738
Gabor Juhos96148322009-07-24 17:27:21 +0200739 if (sc->ps_enabled) {
Jouni Malinendc8c4582009-05-19 17:01:42 +0300740 /*
741 * mac80211 does not set PM field for normal data frames, so we
742 * need to update that based on the current PS mode.
743 */
744 if (ieee80211_is_data(hdr->frame_control) &&
745 !ieee80211_is_nullfunc(hdr->frame_control) &&
746 !ieee80211_has_pm(hdr->frame_control)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800747 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800748 "Add PM=1 for a TX frame while in PS mode\n");
Jouni Malinendc8c4582009-05-19 17:01:42 +0300749 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
750 }
751 }
752
Sujith Manoharanad128862012-04-24 10:23:20 +0530753 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300754 /*
755 * We are using PS-Poll and mac80211 can request TX while in
756 * power save mode. Need to wake up hardware for the TX to be
757 * completed and if needed, also for RX of buffered frames.
758 */
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300759 ath9k_ps_wakeup(sc);
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530760 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Vasanthakumar Thiagarajanfdf76622010-05-17 18:57:55 -0700761 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
762 ath9k_hw_setrxabort(sc->sc_ah, 0);
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300763 if (ieee80211_is_pspoll(hdr->frame_control)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800764 ath_dbg(common, PS,
Joe Perches226afe62010-12-02 19:12:37 -0800765 "Sending PS-Poll to pick a buffered frame\n");
Sujith1b04b932010-01-08 10:36:05 +0530766 sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300767 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -0800768 ath_dbg(common, PS, "Wake up to complete TX\n");
Sujith1b04b932010-01-08 10:36:05 +0530769 sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300770 }
771 /*
772 * The actual restore operation will happen only after
Sujith Manoharanad128862012-04-24 10:23:20 +0530773 * the ps_flags bit is cleared. We are just dropping
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300774 * the ps_usecount here.
775 */
Sujith Manoharan07c15a32012-06-04 20:24:07 +0530776 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Jouni Malinen9a23f9c2009-05-19 17:01:38 +0300777 ath9k_ps_restore(sc);
778 }
779
Sujith Manoharanad128862012-04-24 10:23:20 +0530780 /*
781 * Cannot tx while the hardware is in full sleep, it first needs a full
782 * chip reset to recover from that
783 */
784 if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
785 ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
786 goto exit;
787 }
788
Sujith528f0c62008-10-29 10:14:26 +0530789 memset(&txctl, 0, sizeof(struct ath_tx_control));
Felix Fietkau066dae92010-11-07 14:59:39 +0100790 txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
Thomas Huehn36323f82012-07-23 21:33:42 +0200791 txctl.sta = control->sta;
Sujith528f0c62008-10-29 10:14:26 +0530792
Joe Perchesd2182b62011-12-15 14:55:53 -0800793 ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700794
Jouni Malinenc52f33d2009-03-03 19:23:29 +0200795 if (ath_tx_start(hw, skb, &txctl) != 0) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800796 ath_dbg(common, XMIT, "TX failed\n");
Ben Greeara5a0bca2012-04-03 09:16:55 -0700797 TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
Sujith528f0c62008-10-29 10:14:26 +0530798 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700799 }
800
Johannes Berg7bb45682011-02-24 14:42:06 +0100801 return;
Sujith528f0c62008-10-29 10:14:26 +0530802exit:
Felix Fietkau249ee722012-10-03 21:07:52 +0200803 ieee80211_free_txskb(hw, skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700804}
805
806static void ath9k_stop(struct ieee80211_hw *hw)
807{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100808 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezaf03abe2009-09-09 02:33:11 -0700809 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -0700810 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100811 bool prev_idle;
Sujith9c84b792008-10-29 10:17:13 +0530812
Felix Fietkaubff11762014-06-11 16:17:52 +0530813 cancel_work_sync(&sc->chanctx_work);
Sujith4c483812009-08-18 10:51:52 +0530814 mutex_lock(&sc->mutex);
815
Felix Fietkau9adcf442011-09-03 01:40:26 +0200816 ath_cancel_work(sc);
Luis R. Rodriguezc94dbff2009-07-27 11:53:04 -0700817
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100818 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
Joe Perchesd2182b62011-12-15 14:55:53 -0800819 ath_dbg(common, ANY, "Device not present\n");
Sujith4c483812009-08-18 10:51:52 +0530820 mutex_unlock(&sc->mutex);
Sujith9c84b792008-10-29 10:17:13 +0530821 return;
822 }
823
Sujith3867cf62009-12-23 20:03:27 -0500824 /* Ensure HW is awake when we try to shut it down. */
825 ath9k_ps_wakeup(sc);
826
Luis R. Rodriguez6a6733f2010-10-26 15:27:25 -0700827 spin_lock_bh(&sc->sc_pcu_lock);
828
Stanislaw Gruszka203043f2011-01-25 14:08:40 +0100829 /* prevent tasklets to enable interrupts once we disable them */
830 ah->imask &= ~ATH9K_INT_GLOBAL;
831
Sujithff37e332008-11-24 12:07:55 +0530832 /* make sure h/w will not generate any interrupt
833 * before setting the invalid flag. */
Felix Fietkau4df30712010-11-08 20:54:47 +0100834 ath9k_hw_disable_interrupts(ah);
Sujithff37e332008-11-24 12:07:55 +0530835
Luis R. Rodriguez6a6733f2010-10-26 15:27:25 -0700836 spin_unlock_bh(&sc->sc_pcu_lock);
837
Stanislaw Gruszka203043f2011-01-25 14:08:40 +0100838 /* we can now sync irq and kill any running tasklets, since we already
839 * disabled interrupts and not holding a spin lock */
840 synchronize_irq(sc->irq);
841 tasklet_kill(&sc->intr_tq);
842 tasklet_kill(&sc->bcon_tasklet);
843
Felix Fietkauc0c11742011-11-16 13:08:41 +0100844 prev_idle = sc->ps_idle;
845 sc->ps_idle = true;
846
847 spin_lock_bh(&sc->sc_pcu_lock);
848
849 if (ah->led_pin >= 0) {
850 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
851 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
852 }
853
John W. Linville9ebea382013-01-28 13:54:03 -0500854 ath_prepare_reset(sc);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100855
856 if (sc->rx.frag) {
857 dev_kfree_skb_any(sc->rx.frag);
858 sc->rx.frag = NULL;
859 }
860
861 if (!ah->curchan)
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530862 ah->curchan = ath9k_cmn_get_channel(hw, ah,
863 &sc->cur_chan->chandef);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100864
865 ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
866 ath9k_hw_phy_disable(ah);
867
868 ath9k_hw_configpcipowersave(ah, true);
869
870 spin_unlock_bh(&sc->sc_pcu_lock);
871
Sujith3867cf62009-12-23 20:03:27 -0500872 ath9k_ps_restore(sc);
873
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100874 set_bit(ATH_OP_INVALID, &common->op_flags);
Felix Fietkauc0c11742011-11-16 13:08:41 +0100875 sc->ps_idle = prev_idle;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700876
Sujith141b38b2009-02-04 08:10:07 +0530877 mutex_unlock(&sc->mutex);
878
Joe Perchesd2182b62011-12-15 14:55:53 -0800879 ath_dbg(common, CONFIG, "Driver halt\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700880}
881
Felix Fietkauc648ecb2013-10-11 23:31:00 +0200882static bool ath9k_uses_beacons(int type)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700883{
Ben Greear48014162011-01-15 19:13:48 +0000884 switch (type) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200885 case NL80211_IFTYPE_AP:
Ben Greear48014162011-01-15 19:13:48 +0000886 case NL80211_IFTYPE_ADHOC:
Pat Erley9cb54122009-03-20 22:59:59 -0400887 case NL80211_IFTYPE_MESH_POINT:
Ben Greear48014162011-01-15 19:13:48 +0000888 return true;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700889 default:
Ben Greear48014162011-01-15 19:13:48 +0000890 return false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700891 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700892}
893
Ben Greear48014162011-01-15 19:13:48 +0000894static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
895{
896 struct ath9k_vif_iter_data *iter_data = data;
897 int i;
898
Felix Fietkauab11bb22013-04-16 12:51:57 +0200899 if (iter_data->has_hw_macaddr) {
Ben Greear48014162011-01-15 19:13:48 +0000900 for (i = 0; i < ETH_ALEN; i++)
901 iter_data->mask[i] &=
902 ~(iter_data->hw_macaddr[i] ^ mac[i]);
Felix Fietkauab11bb22013-04-16 12:51:57 +0200903 } else {
904 memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
905 iter_data->has_hw_macaddr = true;
906 }
Ben Greear48014162011-01-15 19:13:48 +0000907
908 switch (vif->type) {
909 case NL80211_IFTYPE_AP:
910 iter_data->naps++;
911 break;
912 case NL80211_IFTYPE_STATION:
913 iter_data->nstations++;
914 break;
915 case NL80211_IFTYPE_ADHOC:
916 iter_data->nadhocs++;
917 break;
918 case NL80211_IFTYPE_MESH_POINT:
919 iter_data->nmeshes++;
920 break;
921 case NL80211_IFTYPE_WDS:
922 iter_data->nwds++;
923 break;
924 default:
Ben Greear48014162011-01-15 19:13:48 +0000925 break;
926 }
927}
928
Sujith Manoharan6dcc3442012-07-17 17:16:36 +0530929static void ath9k_sta_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
930{
931 struct ath_softc *sc = data;
932 struct ath_vif *avp = (void *)vif->drv_priv;
933
934 if (vif->type != NL80211_IFTYPE_STATION)
935 return;
936
937 if (avp->primary_sta_vif)
938 ath9k_set_assoc_state(sc, vif);
939}
940
Ben Greear48014162011-01-15 19:13:48 +0000941/* Called with sc->mutex held. */
942void ath9k_calculate_iter_data(struct ieee80211_hw *hw,
943 struct ieee80211_vif *vif,
944 struct ath9k_vif_iter_data *iter_data)
945{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100946 struct ath_softc *sc = hw->priv;
Ben Greear48014162011-01-15 19:13:48 +0000947 struct ath_hw *ah = sc->sc_ah;
948 struct ath_common *common = ath9k_hw_common(ah);
Ben Greear48014162011-01-15 19:13:48 +0000949
950 /*
Mathy Vanhoef657eb172013-11-28 12:21:45 +0100951 * Pick the MAC address of the first interface as the new hardware
952 * MAC address. The hardware will use it together with the BSSID mask
953 * when matching addresses.
Ben Greear48014162011-01-15 19:13:48 +0000954 */
955 memset(iter_data, 0, sizeof(*iter_data));
Ben Greear48014162011-01-15 19:13:48 +0000956 memset(&iter_data->mask, 0xff, ETH_ALEN);
957
958 if (vif)
959 ath9k_vif_iter(iter_data, vif->addr, vif);
960
961 /* Get list of all active MAC addresses */
Johannes Berg8b2c9822012-11-06 20:23:30 +0100962 ieee80211_iterate_active_interfaces_atomic(
963 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
964 ath9k_vif_iter, iter_data);
Felix Fietkauab11bb22013-04-16 12:51:57 +0200965
966 memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
Ben Greear48014162011-01-15 19:13:48 +0000967}
968
969/* Called with sc->mutex held. */
970static void ath9k_calculate_summary_state(struct ieee80211_hw *hw,
971 struct ieee80211_vif *vif)
972{
Felix Fietkau9ac586152011-01-24 19:23:18 +0100973 struct ath_softc *sc = hw->priv;
Ben Greear48014162011-01-15 19:13:48 +0000974 struct ath_hw *ah = sc->sc_ah;
975 struct ath_common *common = ath9k_hw_common(ah);
976 struct ath9k_vif_iter_data iter_data;
Sujith Manoharan6dcc3442012-07-17 17:16:36 +0530977 enum nl80211_iftype old_opmode = ah->opmode;
Ben Greear48014162011-01-15 19:13:48 +0000978
979 ath9k_calculate_iter_data(hw, vif, &iter_data);
980
Ben Greear48014162011-01-15 19:13:48 +0000981 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
982 ath_hw_setbssidmask(common);
983
Ben Greear48014162011-01-15 19:13:48 +0000984 if (iter_data.naps > 0) {
Sujith Manoharan60ca9f82012-07-17 17:15:37 +0530985 ath9k_hw_set_tsfadjust(ah, true);
Ben Greear48014162011-01-15 19:13:48 +0000986 ah->opmode = NL80211_IFTYPE_AP;
987 } else {
Sujith Manoharan60ca9f82012-07-17 17:15:37 +0530988 ath9k_hw_set_tsfadjust(ah, false);
Ben Greear48014162011-01-15 19:13:48 +0000989
Javier Cardonafd5999c2011-05-03 16:57:19 -0700990 if (iter_data.nmeshes)
991 ah->opmode = NL80211_IFTYPE_MESH_POINT;
992 else if (iter_data.nwds)
Ben Greear48014162011-01-15 19:13:48 +0000993 ah->opmode = NL80211_IFTYPE_AP;
994 else if (iter_data.nadhocs)
995 ah->opmode = NL80211_IFTYPE_ADHOC;
996 else
997 ah->opmode = NL80211_IFTYPE_STATION;
998 }
999
Sujith Manoharandf35d292012-07-17 17:15:43 +05301000 ath9k_hw_setopmode(ah);
1001
Felix Fietkau198823f2012-06-15 15:25:25 +02001002 if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
Ben Greear48014162011-01-15 19:13:48 +00001003 ah->imask |= ATH9K_INT_TSFOOR;
Felix Fietkau198823f2012-06-15 15:25:25 +02001004 else
Ben Greear48014162011-01-15 19:13:48 +00001005 ah->imask &= ~ATH9K_INT_TSFOOR;
Ben Greear48014162011-01-15 19:13:48 +00001006
Felix Fietkau72d874c2011-10-08 20:06:19 +02001007 ath9k_hw_set_interrupts(ah);
Sujith Manoharan6dcc3442012-07-17 17:16:36 +05301008
1009 /*
1010 * If we are changing the opmode to STATION,
1011 * a beacon sync needs to be done.
1012 */
1013 if (ah->opmode == NL80211_IFTYPE_STATION &&
1014 old_opmode == NL80211_IFTYPE_AP &&
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001015 test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
Johannes Berg8b2c9822012-11-06 20:23:30 +01001016 ieee80211_iterate_active_interfaces_atomic(
1017 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1018 ath9k_sta_vif_iter, sc);
Sujith Manoharan6dcc3442012-07-17 17:16:36 +05301019 }
Ben Greear48014162011-01-15 19:13:48 +00001020}
1021
Ben Greear48014162011-01-15 19:13:48 +00001022static int ath9k_add_interface(struct ieee80211_hw *hw,
1023 struct ieee80211_vif *vif)
1024{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001025 struct ath_softc *sc = hw->priv;
Ben Greear48014162011-01-15 19:13:48 +00001026 struct ath_hw *ah = sc->sc_ah;
1027 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001028 struct ath_vif *avp = (void *)vif->drv_priv;
1029 struct ath_node *an = &avp->mcast_node;
Ben Greear48014162011-01-15 19:13:48 +00001030
1031 mutex_lock(&sc->mutex);
1032
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001033 if (config_enabled(CONFIG_ATH9K_TX99)) {
1034 if (sc->nvifs >= 1) {
1035 mutex_unlock(&sc->mutex);
1036 return -EOPNOTSUPP;
1037 }
1038 sc->tx99_vif = vif;
1039 }
1040
Joe Perchesd2182b62011-12-15 14:55:53 -08001041 ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
Ben Greear48014162011-01-15 19:13:48 +00001042 sc->nvifs++;
1043
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301044 ath9k_ps_wakeup(sc);
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301045 ath9k_calculate_summary_state(hw, vif);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301046 ath9k_ps_restore(sc);
1047
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301048 if (ath9k_uses_beacons(vif->type))
1049 ath9k_beacon_assign_slot(sc, vif);
1050
Felix Fietkaud463af42014-04-06 00:37:03 +02001051 avp->vif = vif;
Felix Fietkau39305632014-06-11 16:17:57 +05301052 if (!ath9k_use_chanctx)
1053 avp->chanctx = sc->cur_chan;
Felix Fietkau04535312014-06-11 16:17:51 +05301054
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001055 an->sc = sc;
1056 an->sta = NULL;
1057 an->vif = vif;
1058 an->no_ps_filter = true;
1059 ath_tx_node_init(sc, an);
1060
Ben Greear48014162011-01-15 19:13:48 +00001061 mutex_unlock(&sc->mutex);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301062 return 0;
Ben Greear48014162011-01-15 19:13:48 +00001063}
1064
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301065static int ath9k_change_interface(struct ieee80211_hw *hw,
1066 struct ieee80211_vif *vif,
1067 enum nl80211_iftype new_type,
1068 bool p2p)
1069{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001070 struct ath_softc *sc = hw->priv;
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301071 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkauc083ce92014-06-11 16:17:54 +05301072 struct ath_vif *avp = (void *)vif->drv_priv;
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301073
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301074 mutex_lock(&sc->mutex);
Ben Greear48014162011-01-15 19:13:48 +00001075
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001076 if (config_enabled(CONFIG_ATH9K_TX99)) {
1077 mutex_unlock(&sc->mutex);
1078 return -EOPNOTSUPP;
1079 }
1080
1081 ath_dbg(common, CONFIG, "Change Interface\n");
1082
Ben Greear48014162011-01-15 19:13:48 +00001083 if (ath9k_uses_beacons(vif->type))
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301084 ath9k_beacon_remove_slot(sc, vif);
Ben Greear48014162011-01-15 19:13:48 +00001085
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301086 vif->type = new_type;
1087 vif->p2p = p2p;
1088
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301089 ath9k_ps_wakeup(sc);
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301090 ath9k_calculate_summary_state(hw, vif);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301091 ath9k_ps_restore(sc);
1092
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301093 if (ath9k_uses_beacons(vif->type))
1094 ath9k_beacon_assign_slot(sc, vif);
Felix Fietkauc083ce92014-06-11 16:17:54 +05301095 ath_chanctx_check_active(sc, avp->chanctx);
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301096
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301097 mutex_unlock(&sc->mutex);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301098 return 0;
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05301099}
1100
Felix Fietkaud463af42014-04-06 00:37:03 +02001101static void
1102ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
1103{
1104 struct ath_hw *ah = sc->sc_ah;
1105 s32 tsf, target_tsf;
1106
1107 if (!avp || !avp->noa.has_next_tsf)
1108 return;
1109
1110 ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
1111
1112 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1113
1114 target_tsf = avp->noa.next_tsf;
1115 if (!avp->noa.absent)
1116 target_tsf -= ATH_P2P_PS_STOP_TIME;
1117
1118 if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
1119 target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
1120
1121 ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
1122}
1123
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001124static void ath9k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01001125 struct ieee80211_vif *vif)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001126{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001127 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001128 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001129 struct ath_vif *avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001130
Joe Perchesd2182b62011-12-15 14:55:53 -08001131 ath_dbg(common, CONFIG, "Detach Interface\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001132
Sujith141b38b2009-02-04 08:10:07 +05301133 mutex_lock(&sc->mutex);
1134
Felix Fietkaud463af42014-04-06 00:37:03 +02001135 spin_lock_bh(&sc->sc_pcu_lock);
1136 if (avp == sc->p2p_ps_vif) {
1137 sc->p2p_ps_vif = NULL;
1138 ath9k_update_p2p_ps_timer(sc, NULL);
1139 }
1140 spin_unlock_bh(&sc->sc_pcu_lock);
1141
Ben Greear48014162011-01-15 19:13:48 +00001142 sc->nvifs--;
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001143 sc->tx99_vif = NULL;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001144
Ben Greear48014162011-01-15 19:13:48 +00001145 if (ath9k_uses_beacons(vif->type))
Sujith Manoharan130ef6e2012-07-17 17:15:30 +05301146 ath9k_beacon_remove_slot(sc, vif);
Jouni Malinen2c3db3d2009-03-03 19:23:26 +02001147
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301148 ath9k_ps_wakeup(sc);
Ben Greear48014162011-01-15 19:13:48 +00001149 ath9k_calculate_summary_state(hw, NULL);
Mohammed Shafi Shajakhan327967c2012-09-04 19:33:36 +05301150 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301151
Felix Fietkauf89d1bc2013-08-06 14:18:13 +02001152 ath_tx_node_cleanup(sc, &avp->mcast_node);
1153
Sujith141b38b2009-02-04 08:10:07 +05301154 mutex_unlock(&sc->mutex);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001155}
1156
Senthil Balasubramanianfbab7392010-10-05 20:36:40 +05301157static void ath9k_enable_ps(struct ath_softc *sc)
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301158{
Pavel Roskin30691682010-03-31 18:05:31 -04001159 struct ath_hw *ah = sc->sc_ah;
Sujith Manoharanad128862012-04-24 10:23:20 +05301160 struct ath_common *common = ath9k_hw_common(ah);
Pavel Roskin30691682010-03-31 18:05:31 -04001161
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001162 if (config_enabled(CONFIG_ATH9K_TX99))
1163 return;
1164
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301165 sc->ps_enabled = true;
Pavel Roskin30691682010-03-31 18:05:31 -04001166 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1167 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1168 ah->imask |= ATH9K_INT_TIM_TIMER;
Felix Fietkau72d874c2011-10-08 20:06:19 +02001169 ath9k_hw_set_interrupts(ah);
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301170 }
Vasanthakumar Thiagarajanfdf76622010-05-17 18:57:55 -07001171 ath9k_hw_setrxabort(ah, 1);
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301172 }
Sujith Manoharanad128862012-04-24 10:23:20 +05301173 ath_dbg(common, PS, "PowerSave enabled\n");
Senthil Balasubramanian3f7c5c12010-02-03 22:51:13 +05301174}
1175
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301176static void ath9k_disable_ps(struct ath_softc *sc)
1177{
1178 struct ath_hw *ah = sc->sc_ah;
Sujith Manoharanad128862012-04-24 10:23:20 +05301179 struct ath_common *common = ath9k_hw_common(ah);
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301180
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001181 if (config_enabled(CONFIG_ATH9K_TX99))
1182 return;
1183
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301184 sc->ps_enabled = false;
1185 ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
1186 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1187 ath9k_hw_setrxabort(ah, 0);
1188 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1189 PS_WAIT_FOR_CAB |
1190 PS_WAIT_FOR_PSPOLL_DATA |
1191 PS_WAIT_FOR_TX_ACK);
1192 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1193 ah->imask &= ~ATH9K_INT_TIM_TIMER;
Felix Fietkau72d874c2011-10-08 20:06:19 +02001194 ath9k_hw_set_interrupts(ah);
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301195 }
1196 }
Sujith Manoharanad128862012-04-24 10:23:20 +05301197 ath_dbg(common, PS, "PowerSave disabled\n");
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301198}
1199
Simon Wunderliche93d0832013-01-08 14:48:58 +01001200void ath9k_spectral_scan_trigger(struct ieee80211_hw *hw)
1201{
1202 struct ath_softc *sc = hw->priv;
1203 struct ath_hw *ah = sc->sc_ah;
1204 struct ath_common *common = ath9k_hw_common(ah);
1205 u32 rxfilter;
1206
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001207 if (config_enabled(CONFIG_ATH9K_TX99))
1208 return;
1209
Simon Wunderliche93d0832013-01-08 14:48:58 +01001210 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1211 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1212 return;
1213 }
1214
1215 ath9k_ps_wakeup(sc);
1216 rxfilter = ath9k_hw_getrxfilter(ah);
1217 ath9k_hw_setrxfilter(ah, rxfilter |
1218 ATH9K_RX_FILTER_PHYRADAR |
1219 ATH9K_RX_FILTER_PHYERR);
1220
1221 /* TODO: usually this should not be neccesary, but for some reason
1222 * (or in some mode?) the trigger must be called after the
1223 * configuration, otherwise the register will have its values reset
1224 * (on my ar9220 to value 0x01002310)
1225 */
1226 ath9k_spectral_scan_config(hw, sc->spectral_mode);
1227 ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
1228 ath9k_ps_restore(sc);
1229}
1230
1231int ath9k_spectral_scan_config(struct ieee80211_hw *hw,
1232 enum spectral_mode spectral_mode)
1233{
1234 struct ath_softc *sc = hw->priv;
1235 struct ath_hw *ah = sc->sc_ah;
1236 struct ath_common *common = ath9k_hw_common(ah);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001237
1238 if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
1239 ath_err(common, "spectrum analyzer not implemented on this hardware\n");
1240 return -1;
1241 }
1242
Simon Wunderliche93d0832013-01-08 14:48:58 +01001243 switch (spectral_mode) {
1244 case SPECTRAL_DISABLED:
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001245 sc->spec_config.enabled = 0;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001246 break;
1247 case SPECTRAL_BACKGROUND:
1248 /* send endless samples.
1249 * TODO: is this really useful for "background"?
1250 */
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001251 sc->spec_config.endless = 1;
1252 sc->spec_config.enabled = 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001253 break;
1254 case SPECTRAL_CHANSCAN:
Simon Wunderliche93d0832013-01-08 14:48:58 +01001255 case SPECTRAL_MANUAL:
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001256 sc->spec_config.endless = 0;
1257 sc->spec_config.enabled = 1;
Simon Wunderliche93d0832013-01-08 14:48:58 +01001258 break;
1259 default:
1260 return -1;
1261 }
1262
1263 ath9k_ps_wakeup(sc);
Simon Wunderlich04ccd4a2013-01-23 17:38:04 +01001264 ath9k_hw_ops(ah)->spectral_scan_config(ah, &sc->spec_config);
Simon Wunderliche93d0832013-01-08 14:48:58 +01001265 ath9k_ps_restore(sc);
1266
1267 sc->spectral_mode = spectral_mode;
1268
1269 return 0;
1270}
1271
Johannes Berge8975582008-10-09 12:18:51 +02001272static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001273{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001274 struct ath_softc *sc = hw->priv;
Felix Fietkau34300982010-10-10 18:21:52 +02001275 struct ath_hw *ah = sc->sc_ah;
1276 struct ath_common *common = ath9k_hw_common(ah);
Johannes Berge8975582008-10-09 12:18:51 +02001277 struct ieee80211_conf *conf = &hw->conf;
Felix Fietkaufbbcd142014-06-11 16:17:49 +05301278 struct ath_chanctx *ctx = sc->cur_chan;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001279
Felix Fietkauc0c11742011-11-16 13:08:41 +01001280 ath9k_ps_wakeup(sc);
Sujithaa33de02008-12-18 11:40:16 +05301281 mutex_lock(&sc->mutex);
Sujith141b38b2009-02-04 08:10:07 +05301282
Felix Fietkaudaa1b6e2011-11-16 13:08:43 +01001283 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
Felix Fietkau7545daf2011-01-24 19:23:16 +01001284 sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
Rajkumar Manoharanb73f3e72012-07-01 19:53:53 +05301285 if (sc->ps_idle) {
Felix Fietkaudaa1b6e2011-11-16 13:08:43 +01001286 ath_cancel_work(sc);
Rajkumar Manoharanb73f3e72012-07-01 19:53:53 +05301287 ath9k_stop_btcoex(sc);
1288 } else {
1289 ath9k_start_btcoex(sc);
Felix Fietkau75600ab2012-04-12 20:36:31 +02001290 /*
1291 * The chip needs a reset to properly wake up from
1292 * full sleep
1293 */
Felix Fietkau39305632014-06-11 16:17:57 +05301294 ath_chanctx_set_channel(sc, ctx, &ctx->chandef);
Rajkumar Manoharanb73f3e72012-07-01 19:53:53 +05301295 }
Felix Fietkaudaa1b6e2011-11-16 13:08:43 +01001296 }
Luis R. Rodriguez64839172009-07-14 20:22:53 -04001297
Luis R. Rodrigueze7824a52009-11-24 02:53:25 -05001298 /*
1299 * We just prepare to enable PS. We have to wait until our AP has
1300 * ACK'd our null data frame to disable RX otherwise we'll ignore
1301 * those ACKs and end up retransmitting the same null data frames.
1302 * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1303 */
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301304 if (changed & IEEE80211_CONF_CHANGE_PS) {
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001305 unsigned long flags;
1306 spin_lock_irqsave(&sc->sc_pm_lock, flags);
Senthil Balasubramanianfbab7392010-10-05 20:36:40 +05301307 if (conf->flags & IEEE80211_CONF_PS)
1308 ath9k_enable_ps(sc);
Senthil Balasubramanian845d7082010-10-05 20:36:41 +05301309 else
1310 ath9k_disable_ps(sc);
Luis R. Rodriguez8ab2cd02010-09-16 15:12:26 -04001311 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301312 }
1313
Sujith199afd92010-01-08 10:36:13 +05301314 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1315 if (conf->flags & IEEE80211_CONF_MONITOR) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001316 ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301317 sc->sc_ah->is_monitoring = true;
1318 } else {
Joe Perchesd2182b62011-12-15 14:55:53 -08001319 ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
Rajkumar Manoharan5f841b42010-10-27 18:31:15 +05301320 sc->sc_ah->is_monitoring = false;
Sujith199afd92010-01-08 10:36:13 +05301321 }
1322 }
1323
Felix Fietkau39305632014-06-11 16:17:57 +05301324 if (!ath9k_use_chanctx && (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
Felix Fietkaufbbcd142014-06-11 16:17:49 +05301325 ctx->offchannel = !!(conf->flags & IEEE80211_CONF_OFFCHANNEL);
Felix Fietkaubff11762014-06-11 16:17:52 +05301326 ath_chanctx_set_channel(sc, ctx, &hw->conf.chandef);
Sujith094d05d2008-12-12 11:57:43 +05301327 }
Sujith86b89ee2008-08-07 10:54:57 +05301328
Luis R. Rodriguezc9f6a652010-01-19 14:04:19 -05001329 if (changed & IEEE80211_CONF_CHANGE_POWER) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001330 ath_dbg(common, CONFIG, "Set power: %d\n", conf->power_level);
Felix Fietkaubc7e1be2014-06-11 16:17:50 +05301331 sc->cur_chan->txpower = 2 * conf->power_level;
Rajkumar Manoharan5048e8c2011-01-31 23:47:44 +05301332 ath9k_cmn_update_txpow(ah, sc->curtxpow,
Felix Fietkaubc7e1be2014-06-11 16:17:50 +05301333 sc->cur_chan->txpower, &sc->curtxpow);
Luis R. Rodriguez64839172009-07-14 20:22:53 -04001334 }
1335
Sujithaa33de02008-12-18 11:40:16 +05301336 mutex_unlock(&sc->mutex);
Felix Fietkauc0c11742011-11-16 13:08:41 +01001337 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301338
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001339 return 0;
1340}
1341
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001342#define SUPPORTED_FILTERS \
1343 (FIF_PROMISC_IN_BSS | \
1344 FIF_ALLMULTI | \
1345 FIF_CONTROL | \
Luis R. Rodriguezaf6a3fc2009-08-08 21:55:16 -04001346 FIF_PSPOLL | \
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001347 FIF_OTHER_BSS | \
1348 FIF_BCN_PRBRESP_PROMISC | \
Jouni Malinen9c1d8e42010-10-13 17:29:31 +03001349 FIF_PROBE_REQ | \
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001350 FIF_FCSFAIL)
1351
Sujith7dcfdcd2008-08-11 14:03:13 +05301352/* FIXME: sc->sc_full_reset ? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001353static void ath9k_configure_filter(struct ieee80211_hw *hw,
1354 unsigned int changed_flags,
1355 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02001356 u64 multicast)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001357{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001358 struct ath_softc *sc = hw->priv;
Sujith7dcfdcd2008-08-11 14:03:13 +05301359 u32 rfilt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001360
1361 changed_flags &= SUPPORTED_FILTERS;
1362 *total_flags &= SUPPORTED_FILTERS;
1363
Sujithb77f4832008-12-07 21:44:03 +05301364 sc->rx.rxfilter = *total_flags;
Jouni Malinenaa68aea2009-05-19 17:01:41 +03001365 ath9k_ps_wakeup(sc);
Sujith7dcfdcd2008-08-11 14:03:13 +05301366 rfilt = ath_calcrxfilter(sc);
1367 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
Jouni Malinenaa68aea2009-05-19 17:01:41 +03001368 ath9k_ps_restore(sc);
Sujith7dcfdcd2008-08-11 14:03:13 +05301369
Joe Perchesd2182b62011-12-15 14:55:53 -08001370 ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
1371 rfilt);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001372}
1373
Johannes Berg4ca77862010-02-19 19:06:56 +01001374static int ath9k_sta_add(struct ieee80211_hw *hw,
1375 struct ieee80211_vif *vif,
1376 struct ieee80211_sta *sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001377{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001378 struct ath_softc *sc = hw->priv;
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001379 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1380 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1381 struct ieee80211_key_conf ps_key = { };
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001382 int key;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001383
Ben Greear7e1e3862011-11-03 11:33:13 -07001384 ath_node_attach(sc, sta, vif);
Felix Fietkauf59a59f2011-05-10 20:52:22 +02001385
1386 if (vif->type != NL80211_IFTYPE_AP &&
1387 vif->type != NL80211_IFTYPE_AP_VLAN)
1388 return 0;
1389
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001390 key = ath_key_config(common, vif, sta, &ps_key);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301391 if (key > 0) {
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001392 an->ps_key = key;
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301393 an->key_idx[0] = key;
1394 }
Johannes Berg4ca77862010-02-19 19:06:56 +01001395
1396 return 0;
1397}
1398
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001399static void ath9k_del_ps_key(struct ath_softc *sc,
1400 struct ieee80211_vif *vif,
1401 struct ieee80211_sta *sta)
1402{
1403 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1404 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1405 struct ieee80211_key_conf ps_key = { .hw_key_idx = an->ps_key };
1406
1407 if (!an->ps_key)
1408 return;
1409
1410 ath_key_delete(common, &ps_key);
Felix Fietkau4ef69d02013-04-27 11:47:01 +02001411 an->ps_key = 0;
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301412 an->key_idx[0] = 0;
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001413}
1414
Johannes Berg4ca77862010-02-19 19:06:56 +01001415static int ath9k_sta_remove(struct ieee80211_hw *hw,
1416 struct ieee80211_vif *vif,
1417 struct ieee80211_sta *sta)
1418{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001419 struct ath_softc *sc = hw->priv;
Johannes Berg4ca77862010-02-19 19:06:56 +01001420
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001421 ath9k_del_ps_key(sc, vif, sta);
Johannes Berg4ca77862010-02-19 19:06:56 +01001422 ath_node_detach(sc, sta);
1423
1424 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001425}
1426
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301427static void ath9k_sta_set_tx_filter(struct ath_hw *ah,
1428 struct ath_node *an,
1429 bool set)
1430{
1431 int i;
1432
1433 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1434 if (!an->key_idx[i])
1435 continue;
1436 ath9k_hw_set_tx_filter(ah, an->key_idx[i], set);
1437 }
1438}
1439
Felix Fietkau55195412011-04-17 23:28:09 +02001440static void ath9k_sta_notify(struct ieee80211_hw *hw,
1441 struct ieee80211_vif *vif,
1442 enum sta_notify_cmd cmd,
1443 struct ieee80211_sta *sta)
1444{
1445 struct ath_softc *sc = hw->priv;
1446 struct ath_node *an = (struct ath_node *) sta->drv_priv;
1447
1448 switch (cmd) {
1449 case STA_NOTIFY_SLEEP:
1450 an->sleeping = true;
Johannes Berg042ec452011-09-29 16:04:26 +02001451 ath_tx_aggr_sleep(sta, sc, an);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301452 ath9k_sta_set_tx_filter(sc->sc_ah, an, true);
Felix Fietkau55195412011-04-17 23:28:09 +02001453 break;
1454 case STA_NOTIFY_AWAKE:
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301455 ath9k_sta_set_tx_filter(sc->sc_ah, an, false);
Felix Fietkau55195412011-04-17 23:28:09 +02001456 an->sleeping = false;
1457 ath_tx_aggr_wakeup(sc, an);
1458 break;
1459 }
1460}
1461
Eliad Peller8a3a3c82011-10-02 10:15:52 +02001462static int ath9k_conf_tx(struct ieee80211_hw *hw,
1463 struct ieee80211_vif *vif, u16 queue,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001464 const struct ieee80211_tx_queue_params *params)
1465{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001466 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001467 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau066dae92010-11-07 14:59:39 +01001468 struct ath_txq *txq;
Sujithea9880f2008-08-07 10:53:10 +05301469 struct ath9k_tx_queue_info qi;
Felix Fietkau066dae92010-11-07 14:59:39 +01001470 int ret = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001471
Sujith Manoharanbea843c2012-11-21 18:13:10 +05301472 if (queue >= IEEE80211_NUM_ACS)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001473 return 0;
1474
Felix Fietkau066dae92010-11-07 14:59:39 +01001475 txq = sc->tx.txq_map[queue];
1476
Felix Fietkau96f372c2011-04-07 19:07:17 +02001477 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301478 mutex_lock(&sc->mutex);
1479
Sujith1ffb0612009-03-30 15:28:46 +05301480 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1481
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001482 qi.tqi_aifs = params->aifs;
1483 qi.tqi_cwmin = params->cw_min;
1484 qi.tqi_cwmax = params->cw_max;
Felix Fietkau531bd072012-07-15 19:53:34 +02001485 qi.tqi_burstTime = params->txop * 32;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001486
Joe Perchesd2182b62011-12-15 14:55:53 -08001487 ath_dbg(common, CONFIG,
Joe Perches226afe62010-12-02 19:12:37 -08001488 "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1489 queue, txq->axq_qnum, params->aifs, params->cw_min,
1490 params->cw_max, params->txop);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001491
Felix Fietkauaa5955c2012-07-15 19:53:36 +02001492 ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
Felix Fietkau066dae92010-11-07 14:59:39 +01001493 ret = ath_txq_update(sc, txq->axq_qnum, &qi);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001494 if (ret)
Joe Perches38002762010-12-02 19:12:36 -08001495 ath_err(common, "TXQ Update failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001496
Sujith141b38b2009-02-04 08:10:07 +05301497 mutex_unlock(&sc->mutex);
Felix Fietkau96f372c2011-04-07 19:07:17 +02001498 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301499
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001500 return ret;
1501}
1502
1503static int ath9k_set_key(struct ieee80211_hw *hw,
1504 enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01001505 struct ieee80211_vif *vif,
1506 struct ieee80211_sta *sta,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001507 struct ieee80211_key_conf *key)
1508{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001509 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezc46917b2009-09-13 02:42:02 -07001510 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301511 struct ath_node *an = NULL;
1512 int ret = 0, i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001513
John W. Linville3e6109c2011-01-05 09:39:17 -05001514 if (ath9k_modparam_nohwcrypt)
Jouni Malinenb3bd89c2009-02-24 13:42:01 +02001515 return -ENOSPC;
1516
Chun-Yeow Yeoh5bd5e9a2011-12-07 12:45:46 -08001517 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1518 vif->type == NL80211_IFTYPE_MESH_POINT) &&
Jouni Malinencfdc9a82011-03-23 14:52:19 +02001519 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1520 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1521 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1522 /*
1523 * For now, disable hw crypto for the RSN IBSS group keys. This
1524 * could be optimized in the future to use a modified key cache
1525 * design to support per-STA RX GTK, but until that gets
1526 * implemented, use of software crypto for group addressed
1527 * frames is a acceptable to allow RSN IBSS to be used.
1528 */
1529 return -EOPNOTSUPP;
1530 }
1531
Sujith141b38b2009-02-04 08:10:07 +05301532 mutex_lock(&sc->mutex);
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301533 ath9k_ps_wakeup(sc);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301534 ath_dbg(common, CONFIG, "Set HW Key %d\n", cmd);
1535 if (sta)
1536 an = (struct ath_node *)sta->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001537
1538 switch (cmd) {
1539 case SET_KEY:
Felix Fietkau93ae2dd2011-04-17 23:28:10 +02001540 if (sta)
1541 ath9k_del_ps_key(sc, vif, sta);
1542
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301543 key->hw_key_idx = 0;
Bruno Randolf040e5392010-09-08 16:05:04 +09001544 ret = ath_key_config(common, vif, sta, key);
Jouni Malinen6ace2892008-12-17 13:32:17 +02001545 if (ret >= 0) {
1546 key->hw_key_idx = ret;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001547 /* push IV and Michael MIC generation to stack */
1548 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Johannes Berg97359d12010-08-10 09:46:38 +02001549 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
Senthil Balasubramanian1b961752008-09-01 19:45:21 +05301550 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Johannes Berg97359d12010-08-10 09:46:38 +02001551 if (sc->sc_ah->sw_mgmt_crypto &&
1552 key->cipher == WLAN_CIPHER_SUITE_CCMP)
Johannes Berge548c492012-09-04 17:08:23 +02001553 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
Jouni Malinen6ace2892008-12-17 13:32:17 +02001554 ret = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001555 }
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301556 if (an && key->hw_key_idx) {
1557 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1558 if (an->key_idx[i])
1559 continue;
1560 an->key_idx[i] = key->hw_key_idx;
1561 break;
1562 }
1563 WARN_ON(i == ARRAY_SIZE(an->key_idx));
1564 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001565 break;
1566 case DISABLE_KEY:
Bruno Randolf040e5392010-09-08 16:05:04 +09001567 ath_key_delete(common, key);
Rajkumar Manoharan4bbf4412014-05-22 12:35:49 +05301568 if (an) {
1569 for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
1570 if (an->key_idx[i] != key->hw_key_idx)
1571 continue;
1572 an->key_idx[i] = 0;
1573 break;
1574 }
1575 }
1576 key->hw_key_idx = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001577 break;
1578 default:
1579 ret = -EINVAL;
1580 }
1581
Vivek Natarajan3cbb5dd2009-01-20 11:17:08 +05301582 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301583 mutex_unlock(&sc->mutex);
1584
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001585 return ret;
1586}
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301587
1588static void ath9k_set_assoc_state(struct ath_softc *sc,
1589 struct ieee80211_vif *vif)
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301590{
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301591 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301592 struct ath_vif *avp = (void *)vif->drv_priv;
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301593 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
Sujith Manoharan07c15a32012-06-04 20:24:07 +05301594 unsigned long flags;
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301595
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001596 set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301597 avp->primary_sta_vif = true;
1598
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301599 /*
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301600 * Set the AID, BSSID and do beacon-sync only when
1601 * the HW opmode is STATION.
1602 *
1603 * But the primary bit is set above in any case.
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301604 */
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301605 if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
1606 return;
1607
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301608 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1609 common->curaid = bss_conf->aid;
1610 ath9k_hw_write_associd(sc->sc_ah);
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301611
Oleksij Rempel32efb0c2014-02-04 10:27:39 +01001612 common->last_rssi = ATH_RSSI_DUMMY_MARKER;
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301613 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301614
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301615 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1616 sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
1617 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1618
Rajkumar Manoharan50072eb2012-10-12 14:07:22 +05301619 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1620 ath9k_mci_update_wlan_channels(sc, false);
1621
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301622 ath_dbg(common, CONFIG,
1623 "Primary Station interface: %pM, BSSID: %pM\n",
1624 vif->addr, common->curbssid);
1625}
1626
1627static void ath9k_bss_assoc_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1628{
1629 struct ath_softc *sc = data;
1630 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001631 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301632
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001633 if (test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301634 return;
1635
1636 if (bss_conf->assoc)
1637 ath9k_set_assoc_state(sc, vif);
Rajkumar Manoharan4f5ef75b2011-04-04 22:56:18 +05301638}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001639
Felix Fietkaud463af42014-04-06 00:37:03 +02001640void ath9k_p2p_ps_timer(void *priv)
1641{
1642 struct ath_softc *sc = priv;
1643 struct ath_vif *avp = sc->p2p_ps_vif;
1644 struct ieee80211_vif *vif;
1645 struct ieee80211_sta *sta;
1646 struct ath_node *an;
1647 u32 tsf;
1648
1649 if (!avp)
1650 return;
1651
1652 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1653 if (!avp->noa.absent)
1654 tsf += ATH_P2P_PS_STOP_TIME;
1655
1656 if (!avp->noa.has_next_tsf ||
1657 avp->noa.next_tsf - tsf > BIT(31))
1658 ieee80211_update_p2p_noa(&avp->noa, tsf);
1659
1660 ath9k_update_p2p_ps_timer(sc, avp);
1661
1662 rcu_read_lock();
1663
1664 vif = avp->vif;
1665 sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
1666 if (!sta)
1667 goto out;
1668
1669 an = (void *) sta->drv_priv;
1670 if (an->sleeping == !!avp->noa.absent)
1671 goto out;
1672
1673 an->sleeping = avp->noa.absent;
1674 if (an->sleeping)
1675 ath_tx_aggr_sleep(sta, sc, an);
1676 else
1677 ath_tx_aggr_wakeup(sc, an);
1678
1679out:
1680 rcu_read_unlock();
1681}
1682
1683void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
1684{
1685 struct ath_vif *avp = (void *)vif->drv_priv;
Felix Fietkaud463af42014-04-06 00:37:03 +02001686 u32 tsf;
1687
1688 if (!sc->p2p_ps_timer)
1689 return;
1690
1691 if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
1692 return;
1693
1694 sc->p2p_ps_vif = avp;
Rajkumar Manoharan60ccc102014-05-27 16:58:02 +05301695 tsf = ath9k_hw_gettsf32(sc->sc_ah);
1696 ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
1697 ath9k_update_p2p_ps_timer(sc, avp);
Felix Fietkaud463af42014-04-06 00:37:03 +02001698}
1699
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001700static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1701 struct ieee80211_vif *vif,
1702 struct ieee80211_bss_conf *bss_conf,
1703 u32 changed)
1704{
Sujith Manoharanda0d45f2012-07-17 17:16:29 +05301705#define CHECK_ANI \
1706 (BSS_CHANGED_ASSOC | \
1707 BSS_CHANGED_IBSS | \
1708 BSS_CHANGED_BEACON_ENABLED)
1709
Felix Fietkau9ac586152011-01-24 19:23:18 +01001710 struct ath_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001711 struct ath_hw *ah = sc->sc_ah;
Luis R. Rodriguez15107182009-09-10 09:22:37 -07001712 struct ath_common *common = ath9k_hw_common(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001713 struct ath_vif *avp = (void *)vif->drv_priv;
Rajkumar Manoharan60ccc102014-05-27 16:58:02 +05301714 unsigned long flags;
Felix Fietkau0005baf2010-01-15 02:33:40 +01001715 int slottime;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001716
Felix Fietkau96f372c2011-04-07 19:07:17 +02001717 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301718 mutex_lock(&sc->mutex);
1719
Rajkumar Manoharan9f619032012-03-10 05:06:49 +05301720 if (changed & BSS_CHANGED_ASSOC) {
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301721 ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
1722 bss_conf->bssid, bss_conf->assoc);
Sujithc6089cc2009-11-16 11:40:48 +05301723
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301724 if (avp->primary_sta_vif && !bss_conf->assoc) {
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001725 clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301726 avp->primary_sta_vif = false;
1727
1728 if (ah->opmode == NL80211_IFTYPE_STATION)
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001729 clear_bit(ATH_OP_BEACONS, &common->op_flags);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301730 }
1731
Johannes Berg8b2c9822012-11-06 20:23:30 +01001732 ieee80211_iterate_active_interfaces_atomic(
1733 sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1734 ath9k_bss_assoc_iter, sc);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301735
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001736 if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags) &&
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301737 ah->opmode == NL80211_IFTYPE_STATION) {
1738 memset(common->curbssid, 0, ETH_ALEN);
1739 common->curaid = 0;
1740 ath9k_hw_write_associd(sc->sc_ah);
Rajkumar Manoharan50072eb2012-10-12 14:07:22 +05301741 if (ath9k_hw_mci_is_enabled(sc->sc_ah))
1742 ath9k_mci_update_wlan_channels(sc, true);
Sujith Manoharan6c43c0902012-07-17 17:15:50 +05301743 }
Felix Fietkauc083ce92014-06-11 16:17:54 +05301744
1745 ath_chanctx_check_active(sc, avp->chanctx);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001746 }
1747
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301748 if (changed & BSS_CHANGED_IBSS) {
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301749 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1750 common->curaid = bss_conf->aid;
1751 ath9k_hw_write_associd(sc->sc_ah);
Rajkumar Manoharan2e5ef452011-05-20 17:52:12 +05301752 }
1753
Sujith Manoharanef4ad632012-07-17 17:15:56 +05301754 if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
Felix Fietkauc32e4e52013-12-19 18:01:49 +01001755 (changed & BSS_CHANGED_BEACON_INT))
1756 ath9k_beacon_config(sc, vif, changed);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02001757
Felix Fietkau0005baf2010-01-15 02:33:40 +01001758 if (changed & BSS_CHANGED_ERP_SLOT) {
1759 if (bss_conf->use_short_slot)
1760 slottime = 9;
1761 else
1762 slottime = 20;
1763 if (vif->type == NL80211_IFTYPE_AP) {
1764 /*
1765 * Defer update, so that connected stations can adjust
1766 * their settings at the same time.
1767 * See beacon.c for more details
1768 */
1769 sc->beacon.slottime = slottime;
1770 sc->beacon.updateslot = UPDATE;
1771 } else {
1772 ah->slottime = slottime;
1773 ath9k_hw_init_global_settings(ah);
1774 }
1775 }
1776
Felix Fietkaud463af42014-04-06 00:37:03 +02001777 if (changed & BSS_CHANGED_P2P_PS) {
1778 spin_lock_bh(&sc->sc_pcu_lock);
Rajkumar Manoharan60ccc102014-05-27 16:58:02 +05301779 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1780 if (!(sc->ps_flags & PS_BEACON_SYNC))
1781 ath9k_update_p2p_ps(sc, vif);
1782 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Felix Fietkaud463af42014-04-06 00:37:03 +02001783 spin_unlock_bh(&sc->sc_pcu_lock);
1784 }
1785
Sujith Manoharanda0d45f2012-07-17 17:16:29 +05301786 if (changed & CHECK_ANI)
1787 ath_check_ani(sc);
1788
Sujith141b38b2009-02-04 08:10:07 +05301789 mutex_unlock(&sc->mutex);
Felix Fietkau96f372c2011-04-07 19:07:17 +02001790 ath9k_ps_restore(sc);
Sujith Manoharanda0d45f2012-07-17 17:16:29 +05301791
1792#undef CHECK_ANI
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001793}
1794
Eliad Peller37a41b42011-09-21 14:06:11 +03001795static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001796{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001797 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001798 u64 tsf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001799
Sujith141b38b2009-02-04 08:10:07 +05301800 mutex_lock(&sc->mutex);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301801 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301802 tsf = ath9k_hw_gettsf64(sc->sc_ah);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301803 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301804 mutex_unlock(&sc->mutex);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001805
1806 return tsf;
1807}
1808
Eliad Peller37a41b42011-09-21 14:06:11 +03001809static void ath9k_set_tsf(struct ieee80211_hw *hw,
1810 struct ieee80211_vif *vif,
1811 u64 tsf)
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01001812{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001813 struct ath_softc *sc = hw->priv;
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01001814
Sujith141b38b2009-02-04 08:10:07 +05301815 mutex_lock(&sc->mutex);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301816 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301817 ath9k_hw_settsf64(sc->sc_ah, tsf);
Sujith Manoharan9abbfb22010-12-10 11:27:06 +05301818 ath9k_ps_restore(sc);
Sujith141b38b2009-02-04 08:10:07 +05301819 mutex_unlock(&sc->mutex);
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01001820}
1821
Eliad Peller37a41b42011-09-21 14:06:11 +03001822static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001823{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001824 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001825
Sujith141b38b2009-02-04 08:10:07 +05301826 mutex_lock(&sc->mutex);
Luis R. Rodriguez21526d52009-09-09 20:05:39 -07001827
1828 ath9k_ps_wakeup(sc);
Sujith141b38b2009-02-04 08:10:07 +05301829 ath9k_hw_reset_tsf(sc->sc_ah);
Luis R. Rodriguez21526d52009-09-09 20:05:39 -07001830 ath9k_ps_restore(sc);
1831
Sujith141b38b2009-02-04 08:10:07 +05301832 mutex_unlock(&sc->mutex);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001833}
1834
1835static int ath9k_ampdu_action(struct ieee80211_hw *hw,
Johannes Bergc951ad32009-11-16 12:00:38 +01001836 struct ieee80211_vif *vif,
Sujith141b38b2009-02-04 08:10:07 +05301837 enum ieee80211_ampdu_mlme_action action,
1838 struct ieee80211_sta *sta,
Johannes Berg0b01f032011-01-18 13:51:05 +01001839 u16 tid, u16 *ssn, u8 buf_size)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001840{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001841 struct ath_softc *sc = hw->priv;
Felix Fietkau16e23422013-05-17 12:58:24 +02001842 bool flush = false;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001843 int ret = 0;
1844
Sujith Manoharan7ca7c772013-05-08 05:03:32 +05301845 mutex_lock(&sc->mutex);
Johannes Berg85ad1812010-06-10 10:21:49 +02001846
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001847 switch (action) {
1848 case IEEE80211_AMPDU_RX_START:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001849 break;
1850 case IEEE80211_AMPDU_RX_STOP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001851 break;
1852 case IEEE80211_AMPDU_TX_START:
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001853 ath9k_ps_wakeup(sc);
Felix Fietkau231c3a12010-09-20 19:35:28 +02001854 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
1855 if (!ret)
1856 ieee80211_start_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 Berg18b559d2012-07-18 13:51:25 +02001859 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1860 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Felix Fietkau16e23422013-05-17 12:58:24 +02001861 flush = true;
1862 case IEEE80211_AMPDU_TX_STOP_CONT:
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001863 ath9k_ps_wakeup(sc);
Sujithf83da962009-07-23 15:32:37 +05301864 ath_tx_aggr_stop(sc, sta, tid);
Felix Fietkau08c96ab2013-05-18 21:28:15 +02001865 if (!flush)
Felix Fietkau16e23422013-05-17 12:58:24 +02001866 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001867 ath9k_ps_restore(sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001868 break;
Johannes Bergb1720232009-03-23 17:28:39 +01001869 case IEEE80211_AMPDU_TX_OPERATIONAL:
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001870 ath9k_ps_wakeup(sc);
Sujith8469cde2008-10-29 10:19:28 +05301871 ath_tx_aggr_resume(sc, sta, tid);
Luis R. Rodriguez8b685ba2009-12-23 20:03:29 -05001872 ath9k_ps_restore(sc);
Sujith8469cde2008-10-29 10:19:28 +05301873 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001874 default:
Joe Perches38002762010-12-02 19:12:36 -08001875 ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001876 }
1877
Sujith Manoharan7ca7c772013-05-08 05:03:32 +05301878 mutex_unlock(&sc->mutex);
Johannes Berg85ad1812010-06-10 10:21:49 +02001879
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001880 return ret;
1881}
1882
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001883static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
1884 struct survey_info *survey)
1885{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001886 struct ath_softc *sc = hw->priv;
Felix Fietkau34300982010-10-10 18:21:52 +02001887 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Felix Fietkau39162db2010-09-29 19:12:06 +02001888 struct ieee80211_supported_band *sband;
Felix Fietkau34300982010-10-10 18:21:52 +02001889 struct ieee80211_channel *chan;
Felix Fietkau34300982010-10-10 18:21:52 +02001890 int pos;
1891
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001892 if (config_enabled(CONFIG_ATH9K_TX99))
1893 return -EOPNOTSUPP;
1894
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +05301895 spin_lock_bh(&common->cc_lock);
Felix Fietkau34300982010-10-10 18:21:52 +02001896 if (idx == 0)
1897 ath_update_survey_stats(sc);
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001898
Felix Fietkau39162db2010-09-29 19:12:06 +02001899 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
1900 if (sband && idx >= sband->n_channels) {
1901 idx -= sband->n_channels;
1902 sband = NULL;
1903 }
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001904
Felix Fietkau39162db2010-09-29 19:12:06 +02001905 if (!sband)
1906 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
1907
Felix Fietkau34300982010-10-10 18:21:52 +02001908 if (!sband || idx >= sband->n_channels) {
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +05301909 spin_unlock_bh(&common->cc_lock);
Felix Fietkau34300982010-10-10 18:21:52 +02001910 return -ENOENT;
Felix Fietkau4f1a5a42010-09-29 17:15:28 +02001911 }
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001912
Felix Fietkau34300982010-10-10 18:21:52 +02001913 chan = &sband->channels[idx];
1914 pos = chan->hw_value;
1915 memcpy(survey, &sc->survey[pos], sizeof(*survey));
1916 survey->channel = chan;
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +05301917 spin_unlock_bh(&common->cc_lock);
Felix Fietkau34300982010-10-10 18:21:52 +02001918
Benoit Papillault62dad5b2010-04-28 00:08:24 +02001919 return 0;
1920}
1921
Felix Fietkaue239d852010-01-15 02:34:58 +01001922static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
1923{
Felix Fietkau9ac586152011-01-24 19:23:18 +01001924 struct ath_softc *sc = hw->priv;
Felix Fietkaue239d852010-01-15 02:34:58 +01001925 struct ath_hw *ah = sc->sc_ah;
1926
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -07001927 if (config_enabled(CONFIG_ATH9K_TX99))
1928 return;
1929
Felix Fietkaue239d852010-01-15 02:34:58 +01001930 mutex_lock(&sc->mutex);
1931 ah->coverage_class = coverage_class;
Mohammed Shafi Shajakhan8b2a38272011-08-24 21:38:07 +05301932
1933 ath9k_ps_wakeup(sc);
Felix Fietkaue239d852010-01-15 02:34:58 +01001934 ath9k_hw_init_global_settings(ah);
Mohammed Shafi Shajakhan8b2a38272011-08-24 21:38:07 +05301935 ath9k_ps_restore(sc);
1936
Felix Fietkaue239d852010-01-15 02:34:58 +01001937 mutex_unlock(&sc->mutex);
1938}
1939
Felix Fietkau10e23182013-11-11 22:23:35 +01001940static bool ath9k_has_tx_pending(struct ath_softc *sc)
1941{
Geert Uytterhoevenf7838072014-01-26 11:53:21 +01001942 int i, npend = 0;
Felix Fietkau10e23182013-11-11 22:23:35 +01001943
1944 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1945 if (!ATH_TXQ_SETUP(sc, i))
1946 continue;
1947
1948 if (!sc->tx.txq[i].axq_depth)
1949 continue;
1950
1951 npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i]);
1952 if (npend)
1953 break;
1954 }
1955
1956 return !!npend;
1957}
1958
Emmanuel Grumbach77be2c52014-03-27 11:30:29 +02001959static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1960 u32 queues, bool drop)
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001961{
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001962 struct ath_softc *sc = hw->priv;
Felix Fietkaubff11762014-06-11 16:17:52 +05301963
1964 mutex_lock(&sc->mutex);
1965 __ath9k_flush(hw, queues, drop);
1966 mutex_unlock(&sc->mutex);
1967}
1968
1969void __ath9k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1970{
1971 struct ath_softc *sc = hw->priv;
Mohammed Shafi Shajakhan99aa55b2011-05-06 20:43:11 +05301972 struct ath_hw *ah = sc->sc_ah;
1973 struct ath_common *common = ath9k_hw_common(ah);
Felix Fietkau10e23182013-11-11 22:23:35 +01001974 int timeout = HZ / 5; /* 200 ms */
Rajkumar Manoharan2f6fc352011-04-28 15:31:57 +05301975 bool drain_txq;
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001976
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001977 cancel_delayed_work_sync(&sc->tx_complete_work);
1978
Mohammed Shafi Shajakhan6a6b3f32011-09-09 10:41:08 +05301979 if (ah->ah_flags & AH_UNPLUGGED) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001980 ath_dbg(common, ANY, "Device has been unplugged!\n");
Mohammed Shafi Shajakhan6a6b3f32011-09-09 10:41:08 +05301981 return;
1982 }
1983
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01001984 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
Joe Perchesd2182b62011-12-15 14:55:53 -08001985 ath_dbg(common, ANY, "Device not present\n");
Mohammed Shafi Shajakhan99aa55b2011-05-06 20:43:11 +05301986 return;
1987 }
1988
Felix Fietkau10e23182013-11-11 22:23:35 +01001989 if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc),
1990 timeout) > 0)
1991 drop = false;
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08001992
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001993 if (drop) {
1994 ath9k_ps_wakeup(sc);
1995 spin_lock_bh(&sc->sc_pcu_lock);
Felix Fietkau13815592013-01-20 18:51:53 +01001996 drain_txq = ath_drain_all_txq(sc);
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001997 spin_unlock_bh(&sc->sc_pcu_lock);
Felix Fietkau9adcf442011-09-03 01:40:26 +02001998
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01001999 if (!drain_txq)
Felix Fietkau13815592013-01-20 18:51:53 +01002000 ath_reset(sc);
Felix Fietkau9adcf442011-09-03 01:40:26 +02002001
Felix Fietkau9df0d6a2011-11-16 13:08:42 +01002002 ath9k_ps_restore(sc);
2003 ieee80211_wake_queues(hw);
2004 }
Senthil Balasubramaniand78f4b32011-03-23 23:07:22 +05302005
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08002006 ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08002007}
2008
Vivek Natarajan15b91e82011-04-06 11:41:11 +05302009static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
2010{
2011 struct ath_softc *sc = hw->priv;
2012 int i;
2013
2014 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
2015 if (!ATH_TXQ_SETUP(sc, i))
2016 continue;
2017
2018 if (ath9k_has_pending_frames(sc, &sc->tx.txq[i]))
2019 return true;
2020 }
2021 return false;
2022}
2023
Mohammed Shafi Shajakhan5595f112011-05-19 18:08:57 +05302024static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
Felix Fietkauba4903f2011-05-17 21:09:54 +02002025{
2026 struct ath_softc *sc = hw->priv;
2027 struct ath_hw *ah = sc->sc_ah;
2028 struct ieee80211_vif *vif;
2029 struct ath_vif *avp;
2030 struct ath_buf *bf;
2031 struct ath_tx_status ts;
Felix Fietkau4286df62012-02-27 19:58:40 +01002032 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
Felix Fietkauba4903f2011-05-17 21:09:54 +02002033 int status;
2034
2035 vif = sc->beacon.bslot[0];
2036 if (!vif)
2037 return 0;
2038
Sujith Manoharanaa45fe92012-07-17 17:16:03 +05302039 if (!vif->bss_conf.enable_beacon)
Felix Fietkauba4903f2011-05-17 21:09:54 +02002040 return 0;
2041
Sujith Manoharanaa45fe92012-07-17 17:16:03 +05302042 avp = (void *)vif->drv_priv;
2043
Felix Fietkau4286df62012-02-27 19:58:40 +01002044 if (!sc->beacon.tx_processed && !edma) {
Felix Fietkauba4903f2011-05-17 21:09:54 +02002045 tasklet_disable(&sc->bcon_tasklet);
2046
2047 bf = avp->av_bcbuf;
2048 if (!bf || !bf->bf_mpdu)
2049 goto skip;
2050
2051 status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
2052 if (status == -EINPROGRESS)
2053 goto skip;
2054
2055 sc->beacon.tx_processed = true;
2056 sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
2057
2058skip:
2059 tasklet_enable(&sc->bcon_tasklet);
2060 }
2061
2062 return sc->beacon.tx_last;
2063}
2064
Mohammed Shafi Shajakhan52c94f42011-08-20 17:21:42 +05302065static int ath9k_get_stats(struct ieee80211_hw *hw,
2066 struct ieee80211_low_level_stats *stats)
2067{
2068 struct ath_softc *sc = hw->priv;
2069 struct ath_hw *ah = sc->sc_ah;
2070 struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
2071
2072 stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
2073 stats->dot11RTSFailureCount = mib_stats->rts_bad;
2074 stats->dot11FCSErrorCount = mib_stats->fcs_bad;
2075 stats->dot11RTSSuccessCount = mib_stats->rts_good;
2076 return 0;
2077}
2078
Felix Fietkau43c35282011-09-03 01:40:27 +02002079static u32 fill_chainmask(u32 cap, u32 new)
2080{
2081 u32 filled = 0;
2082 int i;
2083
2084 for (i = 0; cap && new; i++, cap >>= 1) {
2085 if (!(cap & BIT(0)))
2086 continue;
2087
2088 if (new & BIT(0))
2089 filled |= BIT(i);
2090
2091 new >>= 1;
2092 }
2093
2094 return filled;
2095}
2096
Felix Fietkau5d9c7e32012-07-15 19:53:30 +02002097static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
2098{
Felix Fietkaufea92cb2013-01-20 21:55:22 +01002099 if (AR_SREV_9300_20_OR_LATER(ah))
2100 return true;
2101
Felix Fietkau5d9c7e32012-07-15 19:53:30 +02002102 switch (val & 0x7) {
2103 case 0x1:
2104 case 0x3:
2105 case 0x7:
2106 return true;
2107 case 0x2:
2108 return (ah->caps.rx_chainmask == 1);
2109 default:
2110 return false;
2111 }
2112}
2113
Felix Fietkau43c35282011-09-03 01:40:27 +02002114static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2115{
2116 struct ath_softc *sc = hw->priv;
2117 struct ath_hw *ah = sc->sc_ah;
2118
Felix Fietkau5d9c7e32012-07-15 19:53:30 +02002119 if (ah->caps.rx_chainmask != 1)
2120 rx_ant |= tx_ant;
2121
2122 if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
Felix Fietkau43c35282011-09-03 01:40:27 +02002123 return -EINVAL;
2124
2125 sc->ant_rx = rx_ant;
2126 sc->ant_tx = tx_ant;
2127
2128 if (ah->caps.rx_chainmask == 1)
2129 return 0;
2130
2131 /* AR9100 runs into calibration issues if not all rx chains are enabled */
2132 if (AR_SREV_9100(ah))
2133 ah->rxchainmask = 0x7;
2134 else
2135 ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
2136
2137 ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
Oleksij Rempelb57ba3b2014-02-25 14:48:55 +01002138 ath9k_cmn_reload_chainmask(ah);
Felix Fietkau43c35282011-09-03 01:40:27 +02002139
2140 return 0;
2141}
2142
2143static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2144{
2145 struct ath_softc *sc = hw->priv;
2146
2147 *tx_ant = sc->ant_tx;
2148 *rx_ant = sc->ant_rx;
2149 return 0;
2150}
2151
Simon Wunderliche93d0832013-01-08 14:48:58 +01002152static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2153{
2154 struct ath_softc *sc = hw->priv;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01002155 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2156 set_bit(ATH_OP_SCANNING, &common->op_flags);
Simon Wunderliche93d0832013-01-08 14:48:58 +01002157}
2158
2159static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2160{
2161 struct ath_softc *sc = hw->priv;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +01002162 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2163 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Simon Wunderliche93d0832013-01-08 14:48:58 +01002164}
Mohammed Shafi Shajakhanb11e6402012-07-10 14:56:52 +05302165
Felix Fietkau78b21942014-06-11 16:17:55 +05302166static void
2167ath_scan_next_channel(struct ath_softc *sc)
2168{
2169 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
2170 struct ieee80211_channel *chan;
2171
2172 if (sc->offchannel.scan_idx >= req->n_channels) {
2173 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302174 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
2175 NULL);
Felix Fietkau78b21942014-06-11 16:17:55 +05302176 return;
2177 }
2178
2179 chan = req->channels[sc->offchannel.scan_idx++];
2180 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_SEND;
2181 ath_chanctx_offchan_switch(sc, chan);
2182}
2183
Felix Fietkau405393c2014-06-11 16:17:56 +05302184static void ath_offchannel_next(struct ath_softc *sc)
2185{
2186 struct ieee80211_vif *vif;
2187
2188 if (sc->offchannel.scan_req) {
2189 vif = sc->offchannel.scan_vif;
2190 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
2191 ath_scan_next_channel(sc);
2192 } else if (sc->offchannel.roc_vif) {
2193 vif = sc->offchannel.roc_vif;
2194 sc->offchannel.chan.txpower = vif->bss_conf.txpower;
2195 sc->offchannel.state = ATH_OFFCHANNEL_ROC_START;
2196 ath_chanctx_offchan_switch(sc, sc->offchannel.roc_chan);
2197 } else {
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302198 ath_chanctx_switch(sc, ath_chanctx_get_oper_chan(sc, false),
2199 NULL);
Felix Fietkau405393c2014-06-11 16:17:56 +05302200 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
2201 if (sc->ps_idle)
2202 ath_cancel_work(sc);
2203 }
2204}
2205
2206static void ath_roc_complete(struct ath_softc *sc, bool abort)
2207{
2208 sc->offchannel.roc_vif = NULL;
2209 sc->offchannel.roc_chan = NULL;
2210 if (!abort)
2211 ieee80211_remain_on_channel_expired(sc->hw);
2212 ath_offchannel_next(sc);
2213 ath9k_ps_restore(sc);
2214}
2215
Felix Fietkau78b21942014-06-11 16:17:55 +05302216static void ath_scan_complete(struct ath_softc *sc, bool abort)
2217{
2218 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2219
Felix Fietkau78b21942014-06-11 16:17:55 +05302220 sc->offchannel.scan_req = NULL;
2221 sc->offchannel.scan_vif = NULL;
2222 sc->offchannel.state = ATH_OFFCHANNEL_IDLE;
2223 ieee80211_scan_completed(sc->hw, abort);
2224 clear_bit(ATH_OP_SCANNING, &common->op_flags);
Felix Fietkau405393c2014-06-11 16:17:56 +05302225 ath_offchannel_next(sc);
Felix Fietkau78b21942014-06-11 16:17:55 +05302226 ath9k_ps_restore(sc);
Felix Fietkau78b21942014-06-11 16:17:55 +05302227}
2228
2229static void ath_scan_send_probe(struct ath_softc *sc,
2230 struct cfg80211_ssid *ssid)
2231{
2232 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
2233 struct ieee80211_vif *vif = sc->offchannel.scan_vif;
2234 struct ath_tx_control txctl = {};
2235 struct sk_buff *skb;
2236 struct ieee80211_tx_info *info;
2237 int band = sc->offchannel.chan.chandef.chan->band;
2238
2239 skb = ieee80211_probereq_get(sc->hw, vif,
2240 ssid->ssid, ssid->ssid_len, req->ie_len);
2241 if (!skb)
2242 return;
2243
2244 info = IEEE80211_SKB_CB(skb);
2245 if (req->no_cck)
2246 info->flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
2247
2248 if (req->ie_len)
2249 memcpy(skb_put(skb, req->ie_len), req->ie, req->ie_len);
2250
2251 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
2252
2253 if (!ieee80211_tx_prepare_skb(sc->hw, vif, skb, band, NULL))
2254 goto error;
2255
2256 txctl.txq = sc->tx.txq_map[IEEE80211_AC_VO];
2257 txctl.force_channel = true;
2258 if (ath_tx_start(sc->hw, skb, &txctl))
2259 goto error;
2260
2261 return;
2262
2263error:
2264 ieee80211_free_txskb(sc->hw, skb);
2265}
2266
2267static void ath_scan_channel_start(struct ath_softc *sc)
2268{
2269 struct cfg80211_scan_request *req = sc->offchannel.scan_req;
2270 int i, dwell;
2271
2272 if ((sc->cur_chan->chandef.chan->flags & IEEE80211_CHAN_NO_IR) ||
2273 !req->n_ssids) {
2274 dwell = HZ / 9; /* ~110 ms */
2275 } else {
2276 dwell = HZ / 16; /* ~60 ms */
2277
2278 for (i = 0; i < req->n_ssids; i++)
2279 ath_scan_send_probe(sc, &req->ssids[i]);
2280 }
2281
2282 sc->offchannel.state = ATH_OFFCHANNEL_PROBE_WAIT;
2283 mod_timer(&sc->offchannel.timer, jiffies + dwell);
2284}
2285
2286void ath_offchannel_channel_change(struct ath_softc *sc)
2287{
Felix Fietkau78b21942014-06-11 16:17:55 +05302288 switch (sc->offchannel.state) {
2289 case ATH_OFFCHANNEL_PROBE_SEND:
Felix Fietkau405393c2014-06-11 16:17:56 +05302290 if (!sc->offchannel.scan_req)
2291 return;
2292
Felix Fietkau78b21942014-06-11 16:17:55 +05302293 if (sc->cur_chan->chandef.chan !=
2294 sc->offchannel.chan.chandef.chan)
2295 return;
2296
2297 ath_scan_channel_start(sc);
2298 break;
2299 case ATH_OFFCHANNEL_IDLE:
Felix Fietkau405393c2014-06-11 16:17:56 +05302300 if (!sc->offchannel.scan_req)
2301 return;
2302
Felix Fietkau78b21942014-06-11 16:17:55 +05302303 ath_scan_complete(sc, false);
2304 break;
Felix Fietkau405393c2014-06-11 16:17:56 +05302305 case ATH_OFFCHANNEL_ROC_START:
2306 if (sc->cur_chan != &sc->offchannel.chan)
2307 break;
2308
2309 sc->offchannel.state = ATH_OFFCHANNEL_ROC_WAIT;
2310 mod_timer(&sc->offchannel.timer, jiffies +
2311 msecs_to_jiffies(sc->offchannel.roc_duration));
2312 ieee80211_ready_on_channel(sc->hw);
2313 break;
2314 case ATH_OFFCHANNEL_ROC_DONE:
2315 ath_roc_complete(sc, false);
2316 break;
Felix Fietkau78b21942014-06-11 16:17:55 +05302317 default:
2318 break;
2319 }
2320}
2321
2322void ath_offchannel_timer(unsigned long data)
2323{
2324 struct ath_softc *sc = (struct ath_softc *)data;
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302325 struct ath_chanctx *ctx;
Felix Fietkau78b21942014-06-11 16:17:55 +05302326
Felix Fietkau78b21942014-06-11 16:17:55 +05302327 switch (sc->offchannel.state) {
2328 case ATH_OFFCHANNEL_PROBE_WAIT:
Felix Fietkau405393c2014-06-11 16:17:56 +05302329 if (!sc->offchannel.scan_req)
2330 return;
2331
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302332 /* get first active channel context */
2333 ctx = ath_chanctx_get_oper_chan(sc, true);
Felix Fietkau78b21942014-06-11 16:17:55 +05302334 if (ctx->active) {
2335 sc->offchannel.state = ATH_OFFCHANNEL_SUSPEND;
2336 ath_chanctx_switch(sc, ctx, NULL);
2337 mod_timer(&sc->offchannel.timer, jiffies + HZ / 10);
2338 break;
2339 }
2340 /* fall through */
2341 case ATH_OFFCHANNEL_SUSPEND:
Felix Fietkau405393c2014-06-11 16:17:56 +05302342 if (!sc->offchannel.scan_req)
2343 return;
2344
Felix Fietkau78b21942014-06-11 16:17:55 +05302345 ath_scan_next_channel(sc);
2346 break;
Felix Fietkau405393c2014-06-11 16:17:56 +05302347 case ATH_OFFCHANNEL_ROC_START:
2348 case ATH_OFFCHANNEL_ROC_WAIT:
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302349 ctx = ath_chanctx_get_oper_chan(sc, false);
Felix Fietkau405393c2014-06-11 16:17:56 +05302350 sc->offchannel.state = ATH_OFFCHANNEL_ROC_DONE;
2351 ath_chanctx_switch(sc, ctx, NULL);
2352 break;
Felix Fietkau78b21942014-06-11 16:17:55 +05302353 default:
2354 break;
2355 }
2356}
2357
2358static int ath9k_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2359 struct cfg80211_scan_request *req)
2360{
2361 struct ath_softc *sc = hw->priv;
2362 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
2363 int ret = 0;
2364
2365 mutex_lock(&sc->mutex);
2366
2367 if (WARN_ON(sc->offchannel.scan_req)) {
2368 ret = -EBUSY;
2369 goto out;
2370 }
2371
2372 ath9k_ps_wakeup(sc);
2373 set_bit(ATH_OP_SCANNING, &common->op_flags);
2374 sc->offchannel.scan_vif = vif;
2375 sc->offchannel.scan_req = req;
2376 sc->offchannel.scan_idx = 0;
Felix Fietkau78b21942014-06-11 16:17:55 +05302377
Felix Fietkau405393c2014-06-11 16:17:56 +05302378 if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE)
2379 ath_offchannel_next(sc);
Felix Fietkau78b21942014-06-11 16:17:55 +05302380
2381out:
2382 mutex_unlock(&sc->mutex);
2383
2384 return ret;
2385}
2386
2387static void ath9k_cancel_hw_scan(struct ieee80211_hw *hw,
2388 struct ieee80211_vif *vif)
2389{
2390 struct ath_softc *sc = hw->priv;
2391
2392 mutex_lock(&sc->mutex);
2393 del_timer_sync(&sc->offchannel.timer);
2394 ath_scan_complete(sc, true);
2395 mutex_unlock(&sc->mutex);
2396}
2397
Felix Fietkau405393c2014-06-11 16:17:56 +05302398static int ath9k_remain_on_channel(struct ieee80211_hw *hw,
2399 struct ieee80211_vif *vif,
2400 struct ieee80211_channel *chan, int duration,
2401 enum ieee80211_roc_type type)
2402{
2403 struct ath_softc *sc = hw->priv;
2404 int ret = 0;
2405
2406 mutex_lock(&sc->mutex);
2407
2408 if (WARN_ON(sc->offchannel.roc_vif)) {
2409 ret = -EBUSY;
2410 goto out;
2411 }
2412
2413 ath9k_ps_wakeup(sc);
2414 sc->offchannel.roc_vif = vif;
2415 sc->offchannel.roc_chan = chan;
2416 sc->offchannel.roc_duration = duration;
2417
2418 if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE)
2419 ath_offchannel_next(sc);
2420
2421out:
2422 mutex_unlock(&sc->mutex);
2423
2424 return ret;
2425}
2426
2427static int ath9k_cancel_remain_on_channel(struct ieee80211_hw *hw)
2428{
2429 struct ath_softc *sc = hw->priv;
2430
2431 mutex_lock(&sc->mutex);
2432
2433 del_timer_sync(&sc->offchannel.timer);
2434
2435 if (sc->offchannel.roc_vif) {
2436 if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
2437 ath_roc_complete(sc, true);
2438 }
2439
2440 mutex_unlock(&sc->mutex);
2441
2442 return 0;
2443}
2444
Felix Fietkau39305632014-06-11 16:17:57 +05302445static int ath9k_add_chanctx(struct ieee80211_hw *hw,
2446 struct ieee80211_chanctx_conf *conf)
2447{
2448 struct ath_softc *sc = hw->priv;
2449 struct ath_chanctx *ctx, **ptr;
Felix Fietkau39305632014-06-11 16:17:57 +05302450
2451 mutex_lock(&sc->mutex);
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302452
2453 ath_for_each_chanctx(sc, ctx) {
2454 if (ctx->assigned)
2455 continue;
2456
2457 ptr = (void *) conf->drv_priv;
2458 *ptr = ctx;
2459 ctx->assigned = true;
2460 ath_chanctx_set_channel(sc, ctx, &conf->def);
Felix Fietkau39305632014-06-11 16:17:57 +05302461 mutex_unlock(&sc->mutex);
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302462 return 0;
Felix Fietkau39305632014-06-11 16:17:57 +05302463 }
Felix Fietkau39305632014-06-11 16:17:57 +05302464 mutex_unlock(&sc->mutex);
Rajkumar Manoharanc4dc0d02014-06-11 16:17:58 +05302465 return -ENOSPC;
Felix Fietkau39305632014-06-11 16:17:57 +05302466}
2467
2468
2469static void ath9k_remove_chanctx(struct ieee80211_hw *hw,
2470 struct ieee80211_chanctx_conf *conf)
2471{
2472 struct ath_softc *sc = hw->priv;
2473 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2474
2475 mutex_lock(&sc->mutex);
2476 ctx->assigned = false;
2477 mutex_unlock(&sc->mutex);
2478}
2479
2480static void ath9k_change_chanctx(struct ieee80211_hw *hw,
2481 struct ieee80211_chanctx_conf *conf,
2482 u32 changed)
2483{
2484 struct ath_softc *sc = hw->priv;
2485 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2486
2487 mutex_lock(&sc->mutex);
2488 ath_chanctx_set_channel(sc, ctx, &conf->def);
2489 mutex_unlock(&sc->mutex);
2490}
2491
2492static int ath9k_assign_vif_chanctx(struct ieee80211_hw *hw,
2493 struct ieee80211_vif *vif,
2494 struct ieee80211_chanctx_conf *conf)
2495{
2496 struct ath_softc *sc = hw->priv;
2497 struct ath_vif *avp = (void *)vif->drv_priv;
2498 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2499
2500 mutex_lock(&sc->mutex);
2501 avp->chanctx = ctx;
2502 list_add_tail(&avp->list, &ctx->vifs);
2503 ath_chanctx_check_active(sc, ctx);
2504 mutex_unlock(&sc->mutex);
2505
2506 return 0;
2507}
2508
2509static void ath9k_unassign_vif_chanctx(struct ieee80211_hw *hw,
2510 struct ieee80211_vif *vif,
2511 struct ieee80211_chanctx_conf *conf)
2512{
2513 struct ath_softc *sc = hw->priv;
2514 struct ath_vif *avp = (void *)vif->drv_priv;
2515 struct ath_chanctx *ctx = ath_chanctx_get(conf);
2516
2517 mutex_lock(&sc->mutex);
2518 avp->chanctx = NULL;
2519 list_del(&avp->list);
2520 ath_chanctx_check_active(sc, ctx);
2521 mutex_unlock(&sc->mutex);
2522}
2523
Felix Fietkau78b21942014-06-11 16:17:55 +05302524void ath9k_fill_chanctx_ops(void)
2525{
2526 if (!ath9k_use_chanctx)
2527 return;
2528
2529 ath9k_ops.hw_scan = ath9k_hw_scan;
2530 ath9k_ops.cancel_hw_scan = ath9k_cancel_hw_scan;
Felix Fietkau405393c2014-06-11 16:17:56 +05302531 ath9k_ops.remain_on_channel = ath9k_remain_on_channel;
2532 ath9k_ops.cancel_remain_on_channel = ath9k_cancel_remain_on_channel;
Felix Fietkau39305632014-06-11 16:17:57 +05302533 ath9k_ops.add_chanctx = ath9k_add_chanctx;
2534 ath9k_ops.remove_chanctx = ath9k_remove_chanctx;
2535 ath9k_ops.change_chanctx = ath9k_change_chanctx;
2536 ath9k_ops.assign_vif_chanctx = ath9k_assign_vif_chanctx;
2537 ath9k_ops.unassign_vif_chanctx = ath9k_unassign_vif_chanctx;
Felix Fietkau78b21942014-06-11 16:17:55 +05302538}
2539
Gabor Juhos6baff7f2009-01-14 20:17:06 +01002540struct ieee80211_ops ath9k_ops = {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002541 .tx = ath9k_tx,
2542 .start = ath9k_start,
2543 .stop = ath9k_stop,
2544 .add_interface = ath9k_add_interface,
Rajkumar Manoharan6b3b9912010-12-08 19:38:55 +05302545 .change_interface = ath9k_change_interface,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002546 .remove_interface = ath9k_remove_interface,
2547 .config = ath9k_config,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002548 .configure_filter = ath9k_configure_filter,
Johannes Berg4ca77862010-02-19 19:06:56 +01002549 .sta_add = ath9k_sta_add,
2550 .sta_remove = ath9k_sta_remove,
Felix Fietkau55195412011-04-17 23:28:09 +02002551 .sta_notify = ath9k_sta_notify,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002552 .conf_tx = ath9k_conf_tx,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002553 .bss_info_changed = ath9k_bss_info_changed,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002554 .set_key = ath9k_set_key,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002555 .get_tsf = ath9k_get_tsf,
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01002556 .set_tsf = ath9k_set_tsf,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002557 .reset_tsf = ath9k_reset_tsf,
Johannes Berg4233df62008-10-13 13:35:05 +02002558 .ampdu_action = ath9k_ampdu_action,
Benoit Papillault62dad5b2010-04-28 00:08:24 +02002559 .get_survey = ath9k_get_survey,
Johannes Berg3b319aa2009-06-13 14:50:26 +05302560 .rfkill_poll = ath9k_rfkill_poll_state,
Felix Fietkaue239d852010-01-15 02:34:58 +01002561 .set_coverage_class = ath9k_set_coverage_class,
Vasanthakumar Thiagarajan69081622011-02-19 01:13:42 -08002562 .flush = ath9k_flush,
Vivek Natarajan15b91e82011-04-06 11:41:11 +05302563 .tx_frames_pending = ath9k_tx_frames_pending,
Mohammed Shafi Shajakhan52c94f42011-08-20 17:21:42 +05302564 .tx_last_beacon = ath9k_tx_last_beacon,
Felix Fietkau86a22ac2013-06-07 18:12:01 +02002565 .release_buffered_frames = ath9k_release_buffered_frames,
Mohammed Shafi Shajakhan52c94f42011-08-20 17:21:42 +05302566 .get_stats = ath9k_get_stats,
Felix Fietkau43c35282011-09-03 01:40:27 +02002567 .set_antenna = ath9k_set_antenna,
2568 .get_antenna = ath9k_get_antenna,
Ben Greearb90bd9d2012-05-15 15:33:25 -07002569
Sujith Manoharane60001e2013-10-28 12:22:04 +05302570#ifdef CONFIG_ATH9K_WOW
Mohammed Shafi Shajakhanb11e6402012-07-10 14:56:52 +05302571 .suspend = ath9k_suspend,
2572 .resume = ath9k_resume,
2573 .set_wakeup = ath9k_set_wakeup,
2574#endif
2575
Ben Greearb90bd9d2012-05-15 15:33:25 -07002576#ifdef CONFIG_ATH9K_DEBUGFS
2577 .get_et_sset_count = ath9k_get_et_sset_count,
Sujith Manoharana145daf2012-11-28 15:08:54 +05302578 .get_et_stats = ath9k_get_et_stats,
2579 .get_et_strings = ath9k_get_et_strings,
2580#endif
2581
Sujith Manoharan1cdbaf02014-01-13 07:29:27 +05302582#if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
Sujith Manoharana145daf2012-11-28 15:08:54 +05302583 .sta_add_debugfs = ath9k_sta_add_debugfs,
Ben Greearb90bd9d2012-05-15 15:33:25 -07002584#endif
Simon Wunderliche93d0832013-01-08 14:48:58 +01002585 .sw_scan_start = ath9k_sw_scan_start,
2586 .sw_scan_complete = ath9k_sw_scan_complete,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002587};