ath9k: clean up block ack window handling
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wireless / ath / ath9k / main.c
CommitLineData
f078f209 1/*
cee075a2 2 * Copyright (c) 2008-2009 Atheros Communications Inc.
f078f209
LR
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
f078f209 17#include <linux/nl80211.h>
394cf0a1 18#include "ath9k.h"
af03abec 19#include "btcoex.h"
f078f209 20
ce111bad
LR
21static void ath_cache_conf_rate(struct ath_softc *sc,
22 struct ieee80211_conf *conf)
ff37e337 23{
030bb495
LR
24 switch (conf->channel->band) {
25 case IEEE80211_BAND_2GHZ:
26 if (conf_is_ht20(conf))
545750d3 27 sc->cur_rate_mode = ATH9K_MODE_11NG_HT20;
030bb495 28 else if (conf_is_ht40_minus(conf))
545750d3 29 sc->cur_rate_mode = ATH9K_MODE_11NG_HT40MINUS;
030bb495 30 else if (conf_is_ht40_plus(conf))
545750d3 31 sc->cur_rate_mode = ATH9K_MODE_11NG_HT40PLUS;
96742256 32 else
545750d3 33 sc->cur_rate_mode = ATH9K_MODE_11G;
030bb495
LR
34 break;
35 case IEEE80211_BAND_5GHZ:
36 if (conf_is_ht20(conf))
545750d3 37 sc->cur_rate_mode = ATH9K_MODE_11NA_HT20;
030bb495 38 else if (conf_is_ht40_minus(conf))
545750d3 39 sc->cur_rate_mode = ATH9K_MODE_11NA_HT40MINUS;
030bb495 40 else if (conf_is_ht40_plus(conf))
545750d3 41 sc->cur_rate_mode = ATH9K_MODE_11NA_HT40PLUS;
030bb495 42 else
545750d3 43 sc->cur_rate_mode = ATH9K_MODE_11A;
030bb495
LR
44 break;
45 default:
ce111bad 46 BUG_ON(1);
030bb495
LR
47 break;
48 }
ff37e337
S
49}
50
51static void ath_update_txpow(struct ath_softc *sc)
52{
cbe61d8a 53 struct ath_hw *ah = sc->sc_ah;
ff37e337 54
17d7904d
S
55 if (sc->curtxpow != sc->config.txpowlimit) {
56 ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
ff37e337 57 /* read back in case value is clamped */
9cc3271f 58 sc->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
ff37e337
S
59 }
60}
61
62static u8 parse_mpdudensity(u8 mpdudensity)
63{
64 /*
65 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
66 * 0 for no restriction
67 * 1 for 1/4 us
68 * 2 for 1/2 us
69 * 3 for 1 us
70 * 4 for 2 us
71 * 5 for 4 us
72 * 6 for 8 us
73 * 7 for 16 us
74 */
75 switch (mpdudensity) {
76 case 0:
77 return 0;
78 case 1:
79 case 2:
80 case 3:
81 /* Our lower layer calculations limit our precision to
82 1 microsecond */
83 return 1;
84 case 4:
85 return 2;
86 case 5:
87 return 4;
88 case 6:
89 return 8;
90 case 7:
91 return 16;
92 default:
93 return 0;
94 }
95}
96
82880a7c
VT
97static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
98 struct ieee80211_hw *hw)
99{
100 struct ieee80211_channel *curchan = hw->conf.channel;
101 struct ath9k_channel *channel;
102 u8 chan_idx;
103
104 chan_idx = curchan->hw_value;
105 channel = &sc->sc_ah->channels[chan_idx];
106 ath9k_update_ichannel(sc, hw, channel);
107 return channel;
108}
109
55624204 110bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
8c77a569
LR
111{
112 unsigned long flags;
113 bool ret;
114
9ecdef4b
LR
115 spin_lock_irqsave(&sc->sc_pm_lock, flags);
116 ret = ath9k_hw_setpower(sc->sc_ah, mode);
117 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
8c77a569
LR
118
119 return ret;
120}
121
a91d75ae
LR
122void ath9k_ps_wakeup(struct ath_softc *sc)
123{
124 unsigned long flags;
125
126 spin_lock_irqsave(&sc->sc_pm_lock, flags);
127 if (++sc->ps_usecount != 1)
128 goto unlock;
129
9ecdef4b 130 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
a91d75ae
LR
131
132 unlock:
133 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
134}
135
136void ath9k_ps_restore(struct ath_softc *sc)
137{
138 unsigned long flags;
139
140 spin_lock_irqsave(&sc->sc_pm_lock, flags);
141 if (--sc->ps_usecount != 0)
142 goto unlock;
143
1dbfd9d4
VN
144 if (sc->ps_idle)
145 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
146 else if (sc->ps_enabled &&
147 !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
1b04b930
S
148 PS_WAIT_FOR_CAB |
149 PS_WAIT_FOR_PSPOLL_DATA |
150 PS_WAIT_FOR_TX_ACK)))
9ecdef4b 151 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
a91d75ae
LR
152
153 unlock:
154 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
155}
156
5ee08656
FF
157static void ath_start_ani(struct ath_common *common)
158{
159 struct ath_hw *ah = common->ah;
160 unsigned long timestamp = jiffies_to_msecs(jiffies);
161 struct ath_softc *sc = (struct ath_softc *) common->priv;
162
163 if (!(sc->sc_flags & SC_OP_ANI_RUN))
164 return;
165
166 if (sc->sc_flags & SC_OP_OFFCHANNEL)
167 return;
168
169 common->ani.longcal_timer = timestamp;
170 common->ani.shortcal_timer = timestamp;
171 common->ani.checkani_timer = timestamp;
172
173 mod_timer(&common->ani.timer,
174 jiffies +
175 msecs_to_jiffies((u32)ah->config.ani_poll_interval));
176}
177
ff37e337
S
178/*
179 * Set/change channels. If the channel is really being changed, it's done
180 * by reseting the chip. To accomplish this we must first cleanup any pending
181 * DMA, then restart stuff.
182*/
0e2dedf9
JM
183int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
184 struct ath9k_channel *hchan)
ff37e337 185{
20bd2a09 186 struct ath_wiphy *aphy = hw->priv;
cbe61d8a 187 struct ath_hw *ah = sc->sc_ah;
c46917bb 188 struct ath_common *common = ath9k_hw_common(ah);
25c56eec 189 struct ieee80211_conf *conf = &common->hw->conf;
ff37e337 190 bool fastcc = true, stopped;
ae8d2858 191 struct ieee80211_channel *channel = hw->conf.channel;
20bd2a09 192 struct ath9k_hw_cal_data *caldata = NULL;
ae8d2858 193 int r;
ff37e337
S
194
195 if (sc->sc_flags & SC_OP_INVALID)
196 return -EIO;
197
5ee08656
FF
198 del_timer_sync(&common->ani.timer);
199 cancel_work_sync(&sc->paprd_work);
200 cancel_work_sync(&sc->hw_check_work);
201 cancel_delayed_work_sync(&sc->tx_complete_work);
202
3cbb5dd7
VN
203 ath9k_ps_wakeup(sc);
204
c0d7c7af
LR
205 /*
206 * This is only performed if the channel settings have
207 * actually changed.
208 *
209 * To switch channels clear any pending DMA operations;
210 * wait long enough for the RX fifo to drain, reset the
211 * hardware at the new frequency, and then re-enable
212 * the relevant bits of the h/w.
213 */
214 ath9k_hw_set_interrupts(ah, 0);
043a0405 215 ath_drain_all_txq(sc, false);
c0d7c7af 216 stopped = ath_stoprecv(sc);
ff37e337 217
c0d7c7af
LR
218 /* XXX: do not flush receive queue here. We don't want
219 * to flush data frames already in queue because of
220 * changing channel. */
ff37e337 221
5ee08656 222 if (!stopped || !(sc->sc_flags & SC_OP_OFFCHANNEL))
c0d7c7af
LR
223 fastcc = false;
224
20bd2a09
FF
225 if (!(sc->sc_flags & SC_OP_OFFCHANNEL))
226 caldata = &aphy->caldata;
227
c46917bb 228 ath_print(common, ATH_DBG_CONFIG,
1e51b2ff 229 "(%u MHz) -> (%u MHz), conf_is_ht40: %d fastcc: %d\n",
c46917bb 230 sc->sc_ah->curchan->channel,
1e51b2ff
LR
231 channel->center_freq, conf_is_ht40(conf),
232 fastcc);
ff37e337 233
c0d7c7af
LR
234 spin_lock_bh(&sc->sc_resetlock);
235
20bd2a09 236 r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
c0d7c7af 237 if (r) {
c46917bb 238 ath_print(common, ATH_DBG_FATAL,
f643e51d 239 "Unable to reset channel (%u MHz), "
c46917bb
LR
240 "reset status %d\n",
241 channel->center_freq, r);
c0d7c7af 242 spin_unlock_bh(&sc->sc_resetlock);
3989279c 243 goto ps_restore;
ff37e337 244 }
c0d7c7af
LR
245 spin_unlock_bh(&sc->sc_resetlock);
246
c0d7c7af 247 if (ath_startrecv(sc) != 0) {
c46917bb
LR
248 ath_print(common, ATH_DBG_FATAL,
249 "Unable to restart recv logic\n");
3989279c
GJ
250 r = -EIO;
251 goto ps_restore;
c0d7c7af
LR
252 }
253
254 ath_cache_conf_rate(sc, &hw->conf);
255 ath_update_txpow(sc);
3069168c 256 ath9k_hw_set_interrupts(ah, ah->imask);
3989279c 257
48a6a468
LR
258 if (!(sc->sc_flags & (SC_OP_OFFCHANNEL))) {
259 ath_beacon_config(sc, NULL);
5ee08656 260 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
48a6a468 261 ath_start_ani(common);
5ee08656
FF
262 }
263
3989279c 264 ps_restore:
3cbb5dd7 265 ath9k_ps_restore(sc);
3989279c 266 return r;
ff37e337
S
267}
268
9f42c2b6
FF
269static void ath_paprd_activate(struct ath_softc *sc)
270{
271 struct ath_hw *ah = sc->sc_ah;
20bd2a09 272 struct ath9k_hw_cal_data *caldata = ah->caldata;
9f42c2b6
FF
273 int chain;
274
20bd2a09 275 if (!caldata || !caldata->paprd_done)
9f42c2b6
FF
276 return;
277
278 ath9k_ps_wakeup(sc);
ddfef792 279 ar9003_paprd_enable(ah, false);
9f42c2b6
FF
280 for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
281 if (!(ah->caps.tx_chainmask & BIT(chain)))
282 continue;
283
20bd2a09 284 ar9003_paprd_populate_single_table(ah, caldata, chain);
9f42c2b6
FF
285 }
286
287 ar9003_paprd_enable(ah, true);
288 ath9k_ps_restore(sc);
289}
290
291void ath_paprd_calibrate(struct work_struct *work)
292{
293 struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
294 struct ieee80211_hw *hw = sc->hw;
295 struct ath_hw *ah = sc->sc_ah;
296 struct ieee80211_hdr *hdr;
297 struct sk_buff *skb = NULL;
298 struct ieee80211_tx_info *tx_info;
299 int band = hw->conf.channel->band;
300 struct ieee80211_supported_band *sband = &sc->sbands[band];
301 struct ath_tx_control txctl;
20bd2a09 302 struct ath9k_hw_cal_data *caldata = ah->caldata;
9f42c2b6
FF
303 int qnum, ftype;
304 int chain_ok = 0;
305 int chain;
306 int len = 1800;
307 int time_left;
308 int i;
309
20bd2a09
FF
310 if (!caldata)
311 return;
312
9f42c2b6
FF
313 skb = alloc_skb(len, GFP_KERNEL);
314 if (!skb)
315 return;
316
317 tx_info = IEEE80211_SKB_CB(skb);
318
319 skb_put(skb, len);
320 memset(skb->data, 0, len);
321 hdr = (struct ieee80211_hdr *)skb->data;
322 ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
323 hdr->frame_control = cpu_to_le16(ftype);
a3d3da14 324 hdr->duration_id = cpu_to_le16(10);
9f42c2b6
FF
325 memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
326 memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
327 memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
328
329 memset(&txctl, 0, sizeof(txctl));
330 qnum = sc->tx.hwq_map[WME_AC_BE];
331 txctl.txq = &sc->tx.txq[qnum];
332
47399f1a 333 ath9k_ps_wakeup(sc);
9f42c2b6
FF
334 ar9003_paprd_init_table(ah);
335 for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
336 if (!(ah->caps.tx_chainmask & BIT(chain)))
337 continue;
338
339 chain_ok = 0;
340 memset(tx_info, 0, sizeof(*tx_info));
341 tx_info->band = band;
342
343 for (i = 0; i < 4; i++) {
344 tx_info->control.rates[i].idx = sband->n_bitrates - 1;
345 tx_info->control.rates[i].count = 6;
346 }
347
348 init_completion(&sc->paprd_complete);
349 ar9003_paprd_setup_gain_table(ah, chain);
350 txctl.paprd = BIT(chain);
351 if (ath_tx_start(hw, skb, &txctl) != 0)
352 break;
353
354 time_left = wait_for_completion_timeout(&sc->paprd_complete,
ca369eb4 355 msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
9f42c2b6
FF
356 if (!time_left) {
357 ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
358 "Timeout waiting for paprd training on "
359 "TX chain %d\n",
360 chain);
ca369eb4 361 goto fail_paprd;
9f42c2b6
FF
362 }
363
364 if (!ar9003_paprd_is_done(ah))
365 break;
366
20bd2a09 367 if (ar9003_paprd_create_curve(ah, caldata, chain) != 0)
9f42c2b6
FF
368 break;
369
370 chain_ok = 1;
371 }
372 kfree_skb(skb);
373
374 if (chain_ok) {
20bd2a09 375 caldata->paprd_done = true;
9f42c2b6
FF
376 ath_paprd_activate(sc);
377 }
378
ca369eb4 379fail_paprd:
9f42c2b6
FF
380 ath9k_ps_restore(sc);
381}
382
ff37e337
S
383/*
384 * This routine performs the periodic noise floor calibration function
385 * that is used to adjust and optimize the chip performance. This
386 * takes environmental changes (location, temperature) into account.
387 * When the task is complete, it reschedules itself depending on the
388 * appropriate interval that was calculated.
389 */
55624204 390void ath_ani_calibrate(unsigned long data)
ff37e337 391{
20977d3e
S
392 struct ath_softc *sc = (struct ath_softc *)data;
393 struct ath_hw *ah = sc->sc_ah;
c46917bb 394 struct ath_common *common = ath9k_hw_common(ah);
ff37e337
S
395 bool longcal = false;
396 bool shortcal = false;
397 bool aniflag = false;
398 unsigned int timestamp = jiffies_to_msecs(jiffies);
6044474e
FF
399 u32 cal_interval, short_cal_interval, long_cal_interval;
400
401 if (ah->caldata && ah->caldata->nfcal_interference)
402 long_cal_interval = ATH_LONG_CALINTERVAL_INT;
403 else
404 long_cal_interval = ATH_LONG_CALINTERVAL;
ff37e337 405
20977d3e
S
406 short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
407 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
ff37e337 408
1ffc1c61
JM
409 /* Only calibrate if awake */
410 if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
411 goto set_timer;
412
413 ath9k_ps_wakeup(sc);
414
ff37e337 415 /* Long calibration runs independently of short calibration. */
6044474e 416 if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
ff37e337 417 longcal = true;
c46917bb 418 ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
3d536acf 419 common->ani.longcal_timer = timestamp;
ff37e337
S
420 }
421
17d7904d 422 /* Short calibration applies only while caldone is false */
3d536acf
LR
423 if (!common->ani.caldone) {
424 if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
ff37e337 425 shortcal = true;
c46917bb
LR
426 ath_print(common, ATH_DBG_ANI,
427 "shortcal @%lu\n", jiffies);
3d536acf
LR
428 common->ani.shortcal_timer = timestamp;
429 common->ani.resetcal_timer = timestamp;
ff37e337
S
430 }
431 } else {
3d536acf 432 if ((timestamp - common->ani.resetcal_timer) >=
ff37e337 433 ATH_RESTART_CALINTERVAL) {
3d536acf
LR
434 common->ani.caldone = ath9k_hw_reset_calvalid(ah);
435 if (common->ani.caldone)
436 common->ani.resetcal_timer = timestamp;
ff37e337
S
437 }
438 }
439
440 /* Verify whether we must check ANI */
e36b27af
LR
441 if ((timestamp - common->ani.checkani_timer) >=
442 ah->config.ani_poll_interval) {
ff37e337 443 aniflag = true;
3d536acf 444 common->ani.checkani_timer = timestamp;
ff37e337
S
445 }
446
447 /* Skip all processing if there's nothing to do. */
448 if (longcal || shortcal || aniflag) {
449 /* Call ANI routine if necessary */
450 if (aniflag)
22e66a4c 451 ath9k_hw_ani_monitor(ah, ah->curchan);
ff37e337
S
452
453 /* Perform calibration if necessary */
454 if (longcal || shortcal) {
3d536acf 455 common->ani.caldone =
43c27613
LR
456 ath9k_hw_calibrate(ah,
457 ah->curchan,
458 common->rx_chainmask,
459 longcal);
379f0440
S
460
461 if (longcal)
3d536acf 462 common->ani.noise_floor = ath9k_hw_getchan_noise(ah,
379f0440
S
463 ah->curchan);
464
c46917bb
LR
465 ath_print(common, ATH_DBG_ANI,
466 " calibrate chan %u/%x nf: %d\n",
467 ah->curchan->channel,
468 ah->curchan->channelFlags,
3d536acf 469 common->ani.noise_floor);
ff37e337
S
470 }
471 }
472
1ffc1c61
JM
473 ath9k_ps_restore(sc);
474
20977d3e 475set_timer:
ff37e337
S
476 /*
477 * Set timer interval based on previous results.
478 * The interval must be the shortest necessary to satisfy ANI,
479 * short calibration and long calibration.
480 */
aac9207e 481 cal_interval = ATH_LONG_CALINTERVAL;
2660b81a 482 if (sc->sc_ah->config.enable_ani)
e36b27af
LR
483 cal_interval = min(cal_interval,
484 (u32)ah->config.ani_poll_interval);
3d536acf 485 if (!common->ani.caldone)
20977d3e 486 cal_interval = min(cal_interval, (u32)short_cal_interval);
ff37e337 487
3d536acf 488 mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
20bd2a09
FF
489 if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->caldata) {
490 if (!ah->caldata->paprd_done)
9f42c2b6
FF
491 ieee80211_queue_work(sc->hw, &sc->paprd_work);
492 else
493 ath_paprd_activate(sc);
494 }
ff37e337
S
495}
496
497/*
498 * Update tx/rx chainmask. For legacy association,
499 * hard code chainmask to 1x1, for 11n association, use
c97c92d9
VT
500 * the chainmask configuration, for bt coexistence, use
501 * the chainmask configuration even in legacy mode.
ff37e337 502 */
0e2dedf9 503void ath_update_chainmask(struct ath_softc *sc, int is_ht)
ff37e337 504{
af03abec 505 struct ath_hw *ah = sc->sc_ah;
43c27613 506 struct ath_common *common = ath9k_hw_common(ah);
af03abec 507
5ee08656 508 if ((sc->sc_flags & SC_OP_OFFCHANNEL) || is_ht ||
766ec4a9 509 (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
43c27613
LR
510 common->tx_chainmask = ah->caps.tx_chainmask;
511 common->rx_chainmask = ah->caps.rx_chainmask;
ff37e337 512 } else {
43c27613
LR
513 common->tx_chainmask = 1;
514 common->rx_chainmask = 1;
ff37e337
S
515 }
516
43c27613 517 ath_print(common, ATH_DBG_CONFIG,
c46917bb 518 "tx chmask: %d, rx chmask: %d\n",
43c27613
LR
519 common->tx_chainmask,
520 common->rx_chainmask);
ff37e337
S
521}
522
523static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
524{
525 struct ath_node *an;
526
527 an = (struct ath_node *)sta->drv_priv;
528
87792efc 529 if (sc->sc_flags & SC_OP_TXAGGR) {
ff37e337 530 ath_tx_node_init(sc, an);
9e98ac65 531 an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
87792efc
S
532 sta->ht_cap.ampdu_factor);
533 an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
a59b5a5e 534 an->last_rssi = ATH_RSSI_DUMMY_MARKER;
87792efc 535 }
ff37e337
S
536}
537
538static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
539{
540 struct ath_node *an = (struct ath_node *)sta->drv_priv;
541
542 if (sc->sc_flags & SC_OP_TXAGGR)
543 ath_tx_node_cleanup(sc, an);
544}
545
347809fc
FF
546void ath_hw_check(struct work_struct *work)
547{
548 struct ath_softc *sc = container_of(work, struct ath_softc, hw_check_work);
549 int i;
550
551 ath9k_ps_wakeup(sc);
552
553 for (i = 0; i < 3; i++) {
554 if (ath9k_hw_check_alive(sc->sc_ah))
555 goto out;
556
557 msleep(1);
558 }
559 ath_reset(sc, false);
560
561out:
562 ath9k_ps_restore(sc);
563}
564
55624204 565void ath9k_tasklet(unsigned long data)
ff37e337
S
566{
567 struct ath_softc *sc = (struct ath_softc *)data;
af03abec 568 struct ath_hw *ah = sc->sc_ah;
c46917bb 569 struct ath_common *common = ath9k_hw_common(ah);
af03abec 570
17d7904d 571 u32 status = sc->intrstatus;
b5c80475 572 u32 rxmask;
ff37e337 573
153e080d
VT
574 ath9k_ps_wakeup(sc);
575
347809fc 576 if (status & ATH9K_INT_FATAL) {
ff37e337 577 ath_reset(sc, false);
153e080d 578 ath9k_ps_restore(sc);
ff37e337 579 return;
063d8be3 580 }
ff37e337 581
347809fc
FF
582 if (!ath9k_hw_check_alive(ah))
583 ieee80211_queue_work(sc->hw, &sc->hw_check_work);
584
b5c80475
FF
585 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
586 rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
587 ATH9K_INT_RXORN);
588 else
589 rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
590
591 if (status & rxmask) {
063d8be3 592 spin_lock_bh(&sc->rx.rxflushlock);
b5c80475
FF
593
594 /* Check for high priority Rx first */
595 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
596 (status & ATH9K_INT_RXHP))
597 ath_rx_tasklet(sc, 0, true);
598
599 ath_rx_tasklet(sc, 0, false);
063d8be3 600 spin_unlock_bh(&sc->rx.rxflushlock);
ff37e337
S
601 }
602
e5003249
VT
603 if (status & ATH9K_INT_TX) {
604 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
605 ath_tx_edma_tasklet(sc);
606 else
607 ath_tx_tasklet(sc);
608 }
063d8be3 609
96148326 610 if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
54ce846e
JM
611 /*
612 * TSF sync does not look correct; remain awake to sync with
613 * the next Beacon.
614 */
c46917bb
LR
615 ath_print(common, ATH_DBG_PS,
616 "TSFOOR - Sync with next Beacon\n");
1b04b930 617 sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
54ce846e
JM
618 }
619
766ec4a9 620 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
ebb8e1d7
VT
621 if (status & ATH9K_INT_GENTIMER)
622 ath_gen_timer_isr(sc->sc_ah);
623
ff37e337 624 /* re-enable hardware interrupt */
3069168c 625 ath9k_hw_set_interrupts(ah, ah->imask);
153e080d 626 ath9k_ps_restore(sc);
ff37e337
S
627}
628
6baff7f9 629irqreturn_t ath_isr(int irq, void *dev)
ff37e337 630{
063d8be3
S
631#define SCHED_INTR ( \
632 ATH9K_INT_FATAL | \
633 ATH9K_INT_RXORN | \
634 ATH9K_INT_RXEOL | \
635 ATH9K_INT_RX | \
b5c80475
FF
636 ATH9K_INT_RXLP | \
637 ATH9K_INT_RXHP | \
063d8be3
S
638 ATH9K_INT_TX | \
639 ATH9K_INT_BMISS | \
640 ATH9K_INT_CST | \
ebb8e1d7
VT
641 ATH9K_INT_TSFOOR | \
642 ATH9K_INT_GENTIMER)
063d8be3 643
ff37e337 644 struct ath_softc *sc = dev;
cbe61d8a 645 struct ath_hw *ah = sc->sc_ah;
ff37e337
S
646 enum ath9k_int status;
647 bool sched = false;
648
063d8be3
S
649 /*
650 * The hardware is not ready/present, don't
651 * touch anything. Note this can happen early
652 * on if the IRQ is shared.
653 */
654 if (sc->sc_flags & SC_OP_INVALID)
655 return IRQ_NONE;
ff37e337 656
063d8be3
S
657
658 /* shared irq, not for us */
659
153e080d 660 if (!ath9k_hw_intrpend(ah))
063d8be3 661 return IRQ_NONE;
063d8be3
S
662
663 /*
664 * Figure out the reason(s) for the interrupt. Note
665 * that the hal returns a pseudo-ISR that may include
666 * bits we haven't explicitly enabled so we mask the
667 * value to insure we only process bits we requested.
668 */
669 ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
3069168c 670 status &= ah->imask; /* discard unasked-for bits */
ff37e337 671
063d8be3
S
672 /*
673 * If there are no status bits set, then this interrupt was not
674 * for me (should have been caught above).
675 */
153e080d 676 if (!status)
063d8be3 677 return IRQ_NONE;
ff37e337 678
063d8be3
S
679 /* Cache the status */
680 sc->intrstatus = status;
681
682 if (status & SCHED_INTR)
683 sched = true;
684
685 /*
686 * If a FATAL or RXORN interrupt is received, we have to reset the
687 * chip immediately.
688 */
b5c80475
FF
689 if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
690 !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
063d8be3
S
691 goto chip_reset;
692
08578b8f
LR
693 if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
694 (status & ATH9K_INT_BB_WATCHDOG)) {
695 ar9003_hw_bb_watchdog_dbg_info(ah);
696 goto chip_reset;
697 }
698
063d8be3
S
699 if (status & ATH9K_INT_SWBA)
700 tasklet_schedule(&sc->bcon_tasklet);
701
702 if (status & ATH9K_INT_TXURN)
703 ath9k_hw_updatetxtriglevel(ah, true);
704
b5c80475
FF
705 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
706 if (status & ATH9K_INT_RXEOL) {
707 ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
708 ath9k_hw_set_interrupts(ah, ah->imask);
709 }
710 }
711
063d8be3 712 if (status & ATH9K_INT_MIB) {
ff37e337 713 /*
063d8be3
S
714 * Disable interrupts until we service the MIB
715 * interrupt; otherwise it will continue to
716 * fire.
ff37e337 717 */
063d8be3
S
718 ath9k_hw_set_interrupts(ah, 0);
719 /*
720 * Let the hal handle the event. We assume
721 * it will clear whatever condition caused
722 * the interrupt.
723 */
22e66a4c 724 ath9k_hw_procmibevent(ah);
3069168c 725 ath9k_hw_set_interrupts(ah, ah->imask);
063d8be3 726 }
ff37e337 727
153e080d
VT
728 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
729 if (status & ATH9K_INT_TIM_TIMER) {
063d8be3
S
730 /* Clear RxAbort bit so that we can
731 * receive frames */
9ecdef4b 732 ath9k_setpower(sc, ATH9K_PM_AWAKE);
153e080d 733 ath9k_hw_setrxabort(sc->sc_ah, 0);
1b04b930 734 sc->ps_flags |= PS_WAIT_FOR_BEACON;
ff37e337 735 }
063d8be3
S
736
737chip_reset:
ff37e337 738
817e11de
S
739 ath_debug_stat_interrupt(sc, status);
740
ff37e337
S
741 if (sched) {
742 /* turn off every interrupt except SWBA */
3069168c 743 ath9k_hw_set_interrupts(ah, (ah->imask & ATH9K_INT_SWBA));
ff37e337
S
744 tasklet_schedule(&sc->intr_tq);
745 }
746
747 return IRQ_HANDLED;
063d8be3
S
748
749#undef SCHED_INTR
ff37e337
S
750}
751
f078f209 752static u32 ath_get_extchanmode(struct ath_softc *sc,
99405f93 753 struct ieee80211_channel *chan,
094d05dc 754 enum nl80211_channel_type channel_type)
f078f209
LR
755{
756 u32 chanmode = 0;
f078f209
LR
757
758 switch (chan->band) {
759 case IEEE80211_BAND_2GHZ:
094d05dc
S
760 switch(channel_type) {
761 case NL80211_CHAN_NO_HT:
762 case NL80211_CHAN_HT20:
f078f209 763 chanmode = CHANNEL_G_HT20;
094d05dc
S
764 break;
765 case NL80211_CHAN_HT40PLUS:
f078f209 766 chanmode = CHANNEL_G_HT40PLUS;
094d05dc
S
767 break;
768 case NL80211_CHAN_HT40MINUS:
f078f209 769 chanmode = CHANNEL_G_HT40MINUS;
094d05dc
S
770 break;
771 }
f078f209
LR
772 break;
773 case IEEE80211_BAND_5GHZ:
094d05dc
S
774 switch(channel_type) {
775 case NL80211_CHAN_NO_HT:
776 case NL80211_CHAN_HT20:
f078f209 777 chanmode = CHANNEL_A_HT20;
094d05dc
S
778 break;
779 case NL80211_CHAN_HT40PLUS:
f078f209 780 chanmode = CHANNEL_A_HT40PLUS;
094d05dc
S
781 break;
782 case NL80211_CHAN_HT40MINUS:
f078f209 783 chanmode = CHANNEL_A_HT40MINUS;
094d05dc
S
784 break;
785 }
f078f209
LR
786 break;
787 default:
788 break;
789 }
790
791 return chanmode;
792}
793
8feceb67 794static void ath9k_bss_assoc_info(struct ath_softc *sc,
5640b08e 795 struct ieee80211_vif *vif,
8feceb67 796 struct ieee80211_bss_conf *bss_conf)
f078f209 797{
f2b2143e 798 struct ath_hw *ah = sc->sc_ah;
1510718d 799 struct ath_common *common = ath9k_hw_common(ah);
f078f209 800
8feceb67 801 if (bss_conf->assoc) {
c46917bb
LR
802 ath_print(common, ATH_DBG_CONFIG,
803 "Bss Info ASSOC %d, bssid: %pM\n",
804 bss_conf->aid, common->curbssid);
f078f209 805
8feceb67 806 /* New association, store aid */
1510718d 807 common->curaid = bss_conf->aid;
f2b2143e 808 ath9k_hw_write_associd(ah);
2664f201
SB
809
810 /*
811 * Request a re-configuration of Beacon related timers
812 * on the receipt of the first Beacon frame (i.e.,
813 * after time sync with the AP).
814 */
1b04b930 815 sc->ps_flags |= PS_BEACON_SYNC;
f078f209 816
8feceb67 817 /* Configure the beacon */
2c3db3d5 818 ath_beacon_config(sc, vif);
f078f209 819
8feceb67 820 /* Reset rssi stats */
22e66a4c 821 sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
f078f209 822
6c3118e2 823 sc->sc_flags |= SC_OP_ANI_RUN;
3d536acf 824 ath_start_ani(common);
8feceb67 825 } else {
c46917bb 826 ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
1510718d 827 common->curaid = 0;
f38faa31 828 /* Stop ANI */
6c3118e2 829 sc->sc_flags &= ~SC_OP_ANI_RUN;
3d536acf 830 del_timer_sync(&common->ani.timer);
f078f209 831 }
8feceb67 832}
f078f209 833
68a89116 834void ath_radio_enable(struct ath_softc *sc, struct ieee80211_hw *hw)
500c064d 835{
cbe61d8a 836 struct ath_hw *ah = sc->sc_ah;
c46917bb 837 struct ath_common *common = ath9k_hw_common(ah);
68a89116 838 struct ieee80211_channel *channel = hw->conf.channel;
ae8d2858 839 int r;
500c064d 840
3cbb5dd7 841 ath9k_ps_wakeup(sc);
93b1b37f 842 ath9k_hw_configpcipowersave(ah, 0, 0);
ae8d2858 843
159cd468
VT
844 if (!ah->curchan)
845 ah->curchan = ath_get_curchannel(sc, sc->hw);
846
d2f5b3a6 847 spin_lock_bh(&sc->sc_resetlock);
20bd2a09 848 r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
ae8d2858 849 if (r) {
c46917bb 850 ath_print(common, ATH_DBG_FATAL,
f643e51d 851 "Unable to reset channel (%u MHz), "
c46917bb
LR
852 "reset status %d\n",
853 channel->center_freq, r);
500c064d
VT
854 }
855 spin_unlock_bh(&sc->sc_resetlock);
856
857 ath_update_txpow(sc);
858 if (ath_startrecv(sc) != 0) {
c46917bb
LR
859 ath_print(common, ATH_DBG_FATAL,
860 "Unable to restart recv logic\n");
500c064d
VT
861 return;
862 }
863
864 if (sc->sc_flags & SC_OP_BEACONS)
2c3db3d5 865 ath_beacon_config(sc, NULL); /* restart beacons */
500c064d
VT
866
867 /* Re-Enable interrupts */
3069168c 868 ath9k_hw_set_interrupts(ah, ah->imask);
500c064d
VT
869
870 /* Enable LED */
08fc5c1b 871 ath9k_hw_cfg_output(ah, ah->led_pin,
500c064d 872 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
08fc5c1b 873 ath9k_hw_set_gpio(ah, ah->led_pin, 0);
500c064d 874
68a89116 875 ieee80211_wake_queues(hw);
3cbb5dd7 876 ath9k_ps_restore(sc);
500c064d
VT
877}
878
68a89116 879void ath_radio_disable(struct ath_softc *sc, struct ieee80211_hw *hw)
500c064d 880{
cbe61d8a 881 struct ath_hw *ah = sc->sc_ah;
68a89116 882 struct ieee80211_channel *channel = hw->conf.channel;
ae8d2858 883 int r;
500c064d 884
3cbb5dd7 885 ath9k_ps_wakeup(sc);
68a89116 886 ieee80211_stop_queues(hw);
500c064d 887
982723df
VN
888 /*
889 * Keep the LED on when the radio is disabled
890 * during idle unassociated state.
891 */
892 if (!sc->ps_idle) {
893 ath9k_hw_set_gpio(ah, ah->led_pin, 1);
894 ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
895 }
500c064d
VT
896
897 /* Disable interrupts */
898 ath9k_hw_set_interrupts(ah, 0);
899
043a0405 900 ath_drain_all_txq(sc, false); /* clear pending tx frames */
500c064d
VT
901 ath_stoprecv(sc); /* turn off frame recv */
902 ath_flushrecv(sc); /* flush recv queue */
903
159cd468 904 if (!ah->curchan)
68a89116 905 ah->curchan = ath_get_curchannel(sc, hw);
159cd468 906
500c064d 907 spin_lock_bh(&sc->sc_resetlock);
20bd2a09 908 r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
ae8d2858 909 if (r) {
c46917bb 910 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
f643e51d 911 "Unable to reset channel (%u MHz), "
c46917bb
LR
912 "reset status %d\n",
913 channel->center_freq, r);
500c064d
VT
914 }
915 spin_unlock_bh(&sc->sc_resetlock);
916
917 ath9k_hw_phy_disable(ah);
93b1b37f 918 ath9k_hw_configpcipowersave(ah, 1, 1);
3cbb5dd7 919 ath9k_ps_restore(sc);
9ecdef4b 920 ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
500c064d
VT
921}
922
ff37e337
S
923int ath_reset(struct ath_softc *sc, bool retry_tx)
924{
cbe61d8a 925 struct ath_hw *ah = sc->sc_ah;
c46917bb 926 struct ath_common *common = ath9k_hw_common(ah);
030bb495 927 struct ieee80211_hw *hw = sc->hw;
ae8d2858 928 int r;
ff37e337 929
2ab81d4a
S
930 /* Stop ANI */
931 del_timer_sync(&common->ani.timer);
932
cc9c378a
S
933 ieee80211_stop_queues(hw);
934
ff37e337 935 ath9k_hw_set_interrupts(ah, 0);
043a0405 936 ath_drain_all_txq(sc, retry_tx);
ff37e337
S
937 ath_stoprecv(sc);
938 ath_flushrecv(sc);
939
940 spin_lock_bh(&sc->sc_resetlock);
20bd2a09 941 r = ath9k_hw_reset(ah, sc->sc_ah->curchan, ah->caldata, false);
ae8d2858 942 if (r)
c46917bb
LR
943 ath_print(common, ATH_DBG_FATAL,
944 "Unable to reset hardware; reset status %d\n", r);
ff37e337
S
945 spin_unlock_bh(&sc->sc_resetlock);
946
947 if (ath_startrecv(sc) != 0)
c46917bb
LR
948 ath_print(common, ATH_DBG_FATAL,
949 "Unable to start recv logic\n");
ff37e337
S
950
951 /*
952 * We may be doing a reset in response to a request
953 * that changes the channel so update any state that
954 * might change as a result.
955 */
ce111bad 956 ath_cache_conf_rate(sc, &hw->conf);
ff37e337
S
957
958 ath_update_txpow(sc);
959
52b8ac92 960 if ((sc->sc_flags & SC_OP_BEACONS) || !(sc->sc_flags & (SC_OP_OFFCHANNEL)))
2c3db3d5 961 ath_beacon_config(sc, NULL); /* restart beacons */
ff37e337 962
3069168c 963 ath9k_hw_set_interrupts(ah, ah->imask);
ff37e337
S
964
965 if (retry_tx) {
966 int i;
967 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
968 if (ATH_TXQ_SETUP(sc, i)) {
b77f483f
S
969 spin_lock_bh(&sc->tx.txq[i].axq_lock);
970 ath_txq_schedule(sc, &sc->tx.txq[i]);
971 spin_unlock_bh(&sc->tx.txq[i].axq_lock);
ff37e337
S
972 }
973 }
974 }
975
cc9c378a
S
976 ieee80211_wake_queues(hw);
977
2ab81d4a
S
978 /* Start ANI */
979 ath_start_ani(common);
980
ae8d2858 981 return r;
ff37e337
S
982}
983
ebe297c3 984static int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
ff37e337
S
985{
986 int qnum;
987
988 switch (queue) {
989 case 0:
1d2231e2 990 qnum = sc->tx.hwq_map[WME_AC_VO];
ff37e337
S
991 break;
992 case 1:
1d2231e2 993 qnum = sc->tx.hwq_map[WME_AC_VI];
ff37e337
S
994 break;
995 case 2:
1d2231e2 996 qnum = sc->tx.hwq_map[WME_AC_BE];
ff37e337
S
997 break;
998 case 3:
1d2231e2 999 qnum = sc->tx.hwq_map[WME_AC_BK];
ff37e337
S
1000 break;
1001 default:
1d2231e2 1002 qnum = sc->tx.hwq_map[WME_AC_BE];
ff37e337
S
1003 break;
1004 }
1005
1006 return qnum;
1007}
1008
1009int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
1010{
1011 int qnum;
1012
1013 switch (queue) {
1d2231e2 1014 case WME_AC_VO:
ff37e337
S
1015 qnum = 0;
1016 break;
1d2231e2 1017 case WME_AC_VI:
ff37e337
S
1018 qnum = 1;
1019 break;
1d2231e2 1020 case WME_AC_BE:
ff37e337
S
1021 qnum = 2;
1022 break;
1d2231e2 1023 case WME_AC_BK:
ff37e337
S
1024 qnum = 3;
1025 break;
1026 default:
1027 qnum = -1;
1028 break;
1029 }
1030
1031 return qnum;
1032}
1033
5f8e077c
LR
1034/* XXX: Remove me once we don't depend on ath9k_channel for all
1035 * this redundant data */
0e2dedf9
JM
1036void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
1037 struct ath9k_channel *ichan)
5f8e077c 1038{
5f8e077c
LR
1039 struct ieee80211_channel *chan = hw->conf.channel;
1040 struct ieee80211_conf *conf = &hw->conf;
1041
1042 ichan->channel = chan->center_freq;
1043 ichan->chan = chan;
1044
1045 if (chan->band == IEEE80211_BAND_2GHZ) {
1046 ichan->chanmode = CHANNEL_G;
8813262e 1047 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
5f8e077c
LR
1048 } else {
1049 ichan->chanmode = CHANNEL_A;
1050 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
1051 }
1052
25c56eec 1053 if (conf_is_ht(conf))
5f8e077c
LR
1054 ichan->chanmode = ath_get_extchanmode(sc, chan,
1055 conf->channel_type);
5f8e077c
LR
1056}
1057
ff37e337
S
1058/**********************/
1059/* mac80211 callbacks */
1060/**********************/
1061
8feceb67 1062static int ath9k_start(struct ieee80211_hw *hw)
f078f209 1063{
bce048d7
JM
1064 struct ath_wiphy *aphy = hw->priv;
1065 struct ath_softc *sc = aphy->sc;
af03abec 1066 struct ath_hw *ah = sc->sc_ah;
c46917bb 1067 struct ath_common *common = ath9k_hw_common(ah);
8feceb67 1068 struct ieee80211_channel *curchan = hw->conf.channel;
ff37e337 1069 struct ath9k_channel *init_channel;
82880a7c 1070 int r;
f078f209 1071
c46917bb
LR
1072 ath_print(common, ATH_DBG_CONFIG,
1073 "Starting driver with initial channel: %d MHz\n",
1074 curchan->center_freq);
f078f209 1075
141b38b6
S
1076 mutex_lock(&sc->mutex);
1077
9580a222
JM
1078 if (ath9k_wiphy_started(sc)) {
1079 if (sc->chan_idx == curchan->hw_value) {
1080 /*
1081 * Already on the operational channel, the new wiphy
1082 * can be marked active.
1083 */
1084 aphy->state = ATH_WIPHY_ACTIVE;
1085 ieee80211_wake_queues(hw);
1086 } else {
1087 /*
1088 * Another wiphy is on another channel, start the new
1089 * wiphy in paused state.
1090 */
1091 aphy->state = ATH_WIPHY_PAUSED;
1092 ieee80211_stop_queues(hw);
1093 }
1094 mutex_unlock(&sc->mutex);
1095 return 0;
1096 }
1097 aphy->state = ATH_WIPHY_ACTIVE;
1098
8feceb67 1099 /* setup initial channel */
f078f209 1100
82880a7c 1101 sc->chan_idx = curchan->hw_value;
f078f209 1102
82880a7c 1103 init_channel = ath_get_curchannel(sc, hw);
ff37e337
S
1104
1105 /* Reset SERDES registers */
af03abec 1106 ath9k_hw_configpcipowersave(ah, 0, 0);
ff37e337
S
1107
1108 /*
1109 * The basic interface to setting the hardware in a good
1110 * state is ``reset''. On return the hardware is known to
1111 * be powered up and with interrupts disabled. This must
1112 * be followed by initialization of the appropriate bits
1113 * and then setup of the interrupt mask.
1114 */
1115 spin_lock_bh(&sc->sc_resetlock);
20bd2a09 1116 r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
ae8d2858 1117 if (r) {
c46917bb
LR
1118 ath_print(common, ATH_DBG_FATAL,
1119 "Unable to reset hardware; reset status %d "
1120 "(freq %u MHz)\n", r,
1121 curchan->center_freq);
ff37e337 1122 spin_unlock_bh(&sc->sc_resetlock);
141b38b6 1123 goto mutex_unlock;
ff37e337
S
1124 }
1125 spin_unlock_bh(&sc->sc_resetlock);
1126
1127 /*
1128 * This is needed only to setup initial state
1129 * but it's best done after a reset.
1130 */
1131 ath_update_txpow(sc);
8feceb67 1132
ff37e337
S
1133 /*
1134 * Setup the hardware after reset:
1135 * The receive engine is set going.
1136 * Frame transmit is handled entirely
1137 * in the frame output path; there's nothing to do
1138 * here except setup the interrupt mask.
1139 */
1140 if (ath_startrecv(sc) != 0) {
c46917bb
LR
1141 ath_print(common, ATH_DBG_FATAL,
1142 "Unable to start recv logic\n");
141b38b6
S
1143 r = -EIO;
1144 goto mutex_unlock;
f078f209 1145 }
8feceb67 1146
ff37e337 1147 /* Setup our intr mask. */
b5c80475
FF
1148 ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
1149 ATH9K_INT_RXORN | ATH9K_INT_FATAL |
1150 ATH9K_INT_GLOBAL;
1151
1152 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
08578b8f
LR
1153 ah->imask |= ATH9K_INT_RXHP |
1154 ATH9K_INT_RXLP |
1155 ATH9K_INT_BB_WATCHDOG;
b5c80475
FF
1156 else
1157 ah->imask |= ATH9K_INT_RX;
ff37e337 1158
364734fa 1159 ah->imask |= ATH9K_INT_GTT;
ff37e337 1160
af03abec 1161 if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
3069168c 1162 ah->imask |= ATH9K_INT_CST;
ff37e337 1163
ce111bad 1164 ath_cache_conf_rate(sc, &hw->conf);
ff37e337
S
1165
1166 sc->sc_flags &= ~SC_OP_INVALID;
1167
1168 /* Disable BMISS interrupt when we're not associated */
3069168c
PR
1169 ah->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1170 ath9k_hw_set_interrupts(ah, ah->imask);
ff37e337 1171
bce048d7 1172 ieee80211_wake_queues(hw);
ff37e337 1173
42935eca 1174 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
164ace38 1175
766ec4a9
LR
1176 if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
1177 !ah->btcoex_hw.enabled) {
5e197292
LR
1178 ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
1179 AR_STOMP_LOW_WLAN_WGHT);
af03abec 1180 ath9k_hw_btcoex_enable(ah);
f985ad12 1181
5bb12791
LR
1182 if (common->bus_ops->bt_coex_prep)
1183 common->bus_ops->bt_coex_prep(common);
766ec4a9 1184 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
75d7839f 1185 ath9k_btcoex_timer_resume(sc);
1773912b
VT
1186 }
1187
141b38b6
S
1188mutex_unlock:
1189 mutex_unlock(&sc->mutex);
1190
ae8d2858 1191 return r;
f078f209
LR
1192}
1193
8feceb67
VT
1194static int ath9k_tx(struct ieee80211_hw *hw,
1195 struct sk_buff *skb)
f078f209 1196{
528f0c6b 1197 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
bce048d7
JM
1198 struct ath_wiphy *aphy = hw->priv;
1199 struct ath_softc *sc = aphy->sc;
c46917bb 1200 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
528f0c6b 1201 struct ath_tx_control txctl;
1bc14880
BP
1202 int padpos, padsize;
1203 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
84642d6b 1204 int qnum;
528f0c6b 1205
8089cc47 1206 if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
c46917bb
LR
1207 ath_print(common, ATH_DBG_XMIT,
1208 "ath9k: %s: TX in unexpected wiphy state "
1209 "%d\n", wiphy_name(hw->wiphy), aphy->state);
ee166a0e
JM
1210 goto exit;
1211 }
1212
96148326 1213 if (sc->ps_enabled) {
dc8c4585
JM
1214 /*
1215 * mac80211 does not set PM field for normal data frames, so we
1216 * need to update that based on the current PS mode.
1217 */
1218 if (ieee80211_is_data(hdr->frame_control) &&
1219 !ieee80211_is_nullfunc(hdr->frame_control) &&
1220 !ieee80211_has_pm(hdr->frame_control)) {
c46917bb
LR
1221 ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
1222 "while in PS mode\n");
dc8c4585
JM
1223 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
1224 }
1225 }
1226
9a23f9ca
JM
1227 if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
1228 /*
1229 * We are using PS-Poll and mac80211 can request TX while in
1230 * power save mode. Need to wake up hardware for the TX to be
1231 * completed and if needed, also for RX of buffered frames.
1232 */
9a23f9ca 1233 ath9k_ps_wakeup(sc);
fdf76622
VT
1234 if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
1235 ath9k_hw_setrxabort(sc->sc_ah, 0);
9a23f9ca 1236 if (ieee80211_is_pspoll(hdr->frame_control)) {
c46917bb
LR
1237 ath_print(common, ATH_DBG_PS,
1238 "Sending PS-Poll to pick a buffered frame\n");
1b04b930 1239 sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
9a23f9ca 1240 } else {
c46917bb
LR
1241 ath_print(common, ATH_DBG_PS,
1242 "Wake up to complete TX\n");
1b04b930 1243 sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
9a23f9ca
JM
1244 }
1245 /*
1246 * The actual restore operation will happen only after
1247 * the sc_flags bit is cleared. We are just dropping
1248 * the ps_usecount here.
1249 */
1250 ath9k_ps_restore(sc);
1251 }
1252
528f0c6b 1253 memset(&txctl, 0, sizeof(struct ath_tx_control));
f078f209 1254
8feceb67
VT
1255 /*
1256 * As a temporary workaround, assign seq# here; this will likely need
1257 * to be cleaned up to work better with Beacon transmission and virtual
1258 * BSSes.
1259 */
1260 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
8feceb67 1261 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
b77f483f 1262 sc->tx.seq_no += 0x10;
8feceb67 1263 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
b77f483f 1264 hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
8feceb67 1265 }
f078f209 1266
8feceb67 1267 /* Add the padding after the header if this is not already done */
1bc14880
BP
1268 padpos = ath9k_cmn_padpos(hdr->frame_control);
1269 padsize = padpos & 3;
1270 if (padsize && skb->len>padpos) {
8feceb67
VT
1271 if (skb_headroom(skb) < padsize)
1272 return -1;
1273 skb_push(skb, padsize);
1bc14880 1274 memmove(skb->data, skb->data + padsize, padpos);
8feceb67
VT
1275 }
1276
84642d6b
FF
1277 qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
1278 txctl.txq = &sc->tx.txq[qnum];
528f0c6b 1279
c46917bb 1280 ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
8feceb67 1281
c52f33d0 1282 if (ath_tx_start(hw, skb, &txctl) != 0) {
c46917bb 1283 ath_print(common, ATH_DBG_XMIT, "TX failed\n");
528f0c6b 1284 goto exit;
8feceb67
VT
1285 }
1286
528f0c6b
S
1287 return 0;
1288exit:
1289 dev_kfree_skb_any(skb);
8feceb67 1290 return 0;
f078f209
LR
1291}
1292
8feceb67 1293static void ath9k_stop(struct ieee80211_hw *hw)
f078f209 1294{
bce048d7
JM
1295 struct ath_wiphy *aphy = hw->priv;
1296 struct ath_softc *sc = aphy->sc;
af03abec 1297 struct ath_hw *ah = sc->sc_ah;
c46917bb 1298 struct ath_common *common = ath9k_hw_common(ah);
447a42c2 1299 int i;
f078f209 1300
4c483817
S
1301 mutex_lock(&sc->mutex);
1302
9580a222
JM
1303 aphy->state = ATH_WIPHY_INACTIVE;
1304
9a75c2ff
VN
1305 if (led_blink)
1306 cancel_delayed_work_sync(&sc->ath_led_blink_work);
1307
c94dbff7 1308 cancel_delayed_work_sync(&sc->tx_complete_work);
9f42c2b6 1309 cancel_work_sync(&sc->paprd_work);
347809fc 1310 cancel_work_sync(&sc->hw_check_work);
c94dbff7 1311
447a42c2
RM
1312 for (i = 0; i < sc->num_sec_wiphy; i++) {
1313 if (sc->sec_wiphy[i])
1314 break;
1315 }
1316
1317 if (i == sc->num_sec_wiphy) {
c94dbff7
LR
1318 cancel_delayed_work_sync(&sc->wiphy_work);
1319 cancel_work_sync(&sc->chan_work);
1320 }
1321
9c84b797 1322 if (sc->sc_flags & SC_OP_INVALID) {
c46917bb 1323 ath_print(common, ATH_DBG_ANY, "Device not present\n");
4c483817 1324 mutex_unlock(&sc->mutex);
9c84b797
S
1325 return;
1326 }
8feceb67 1327
9580a222
JM
1328 if (ath9k_wiphy_started(sc)) {
1329 mutex_unlock(&sc->mutex);
1330 return; /* another wiphy still in use */
1331 }
1332
3867cf6a
S
1333 /* Ensure HW is awake when we try to shut it down. */
1334 ath9k_ps_wakeup(sc);
1335
766ec4a9 1336 if (ah->btcoex_hw.enabled) {
af03abec 1337 ath9k_hw_btcoex_disable(ah);
766ec4a9 1338 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
75d7839f 1339 ath9k_btcoex_timer_pause(sc);
1773912b
VT
1340 }
1341
ff37e337
S
1342 /* make sure h/w will not generate any interrupt
1343 * before setting the invalid flag. */
af03abec 1344 ath9k_hw_set_interrupts(ah, 0);
ff37e337
S
1345
1346 if (!(sc->sc_flags & SC_OP_INVALID)) {
043a0405 1347 ath_drain_all_txq(sc, false);
ff37e337 1348 ath_stoprecv(sc);
af03abec 1349 ath9k_hw_phy_disable(ah);
ff37e337 1350 } else
b77f483f 1351 sc->rx.rxlink = NULL;
ff37e337 1352
ff37e337 1353 /* disable HAL and put h/w to sleep */
af03abec
LR
1354 ath9k_hw_disable(ah);
1355 ath9k_hw_configpcipowersave(ah, 1, 1);
3867cf6a
S
1356 ath9k_ps_restore(sc);
1357
1358 /* Finally, put the chip in FULL SLEEP mode */
9ecdef4b 1359 ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
ff37e337
S
1360
1361 sc->sc_flags |= SC_OP_INVALID;
500c064d 1362
141b38b6
S
1363 mutex_unlock(&sc->mutex);
1364
c46917bb 1365 ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
f078f209
LR
1366}
1367
8feceb67 1368static int ath9k_add_interface(struct ieee80211_hw *hw,
1ed32e4f 1369 struct ieee80211_vif *vif)
f078f209 1370{
bce048d7
JM
1371 struct ath_wiphy *aphy = hw->priv;
1372 struct ath_softc *sc = aphy->sc;
3069168c
PR
1373 struct ath_hw *ah = sc->sc_ah;
1374 struct ath_common *common = ath9k_hw_common(ah);
1ed32e4f 1375 struct ath_vif *avp = (void *)vif->drv_priv;
d97809db 1376 enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
2c3db3d5 1377 int ret = 0;
8feceb67 1378
141b38b6
S
1379 mutex_lock(&sc->mutex);
1380
1ed32e4f 1381 switch (vif->type) {
05c914fe 1382 case NL80211_IFTYPE_STATION:
d97809db 1383 ic_opmode = NL80211_IFTYPE_STATION;
f078f209 1384 break;
05c914fe 1385 case NL80211_IFTYPE_ADHOC:
05c914fe 1386 case NL80211_IFTYPE_AP:
9cb5412b 1387 case NL80211_IFTYPE_MESH_POINT:
2c3db3d5
JM
1388 if (sc->nbcnvifs >= ATH_BCBUF) {
1389 ret = -ENOBUFS;
1390 goto out;
1391 }
1ed32e4f 1392 ic_opmode = vif->type;
f078f209
LR
1393 break;
1394 default:
c46917bb 1395 ath_print(common, ATH_DBG_FATAL,
1ed32e4f 1396 "Interface type %d not yet supported\n", vif->type);
2c3db3d5
JM
1397 ret = -EOPNOTSUPP;
1398 goto out;
f078f209
LR
1399 }
1400
c46917bb
LR
1401 ath_print(common, ATH_DBG_CONFIG,
1402 "Attach a VIF of type: %d\n", ic_opmode);
8feceb67 1403
17d7904d 1404 /* Set the VIF opmode */
5640b08e
S
1405 avp->av_opmode = ic_opmode;
1406 avp->av_bslot = -1;
1407
2c3db3d5 1408 sc->nvifs++;
8ca21f01 1409
364734fa 1410 ath9k_set_bssid_mask(hw, vif);
8ca21f01 1411
2c3db3d5
JM
1412 if (sc->nvifs > 1)
1413 goto out; /* skip global settings for secondary vif */
1414
b238e90e 1415 if (ic_opmode == NL80211_IFTYPE_AP) {
3069168c 1416 ath9k_hw_set_tsfadjust(ah, 1);
b238e90e
S
1417 sc->sc_flags |= SC_OP_TSF_RESET;
1418 }
5640b08e 1419
5640b08e 1420 /* Set the device opmode */
3069168c 1421 ah->opmode = ic_opmode;
5640b08e 1422
4e30ffa2
VN
1423 /*
1424 * Enable MIB interrupts when there are hardware phy counters.
1425 * Note we only do this (at the moment) for station mode.
1426 */
1ed32e4f
JB
1427 if ((vif->type == NL80211_IFTYPE_STATION) ||
1428 (vif->type == NL80211_IFTYPE_ADHOC) ||
1429 (vif->type == NL80211_IFTYPE_MESH_POINT)) {
3448f912
LR
1430 if (ah->config.enable_ani)
1431 ah->imask |= ATH9K_INT_MIB;
3069168c 1432 ah->imask |= ATH9K_INT_TSFOOR;
4af9cf4f
S
1433 }
1434
3069168c 1435 ath9k_hw_set_interrupts(ah, ah->imask);
4e30ffa2 1436
1ed32e4f
JB
1437 if (vif->type == NL80211_IFTYPE_AP ||
1438 vif->type == NL80211_IFTYPE_ADHOC ||
6c3118e2
VT
1439 vif->type == NL80211_IFTYPE_MONITOR) {
1440 sc->sc_flags |= SC_OP_ANI_RUN;
3d536acf 1441 ath_start_ani(common);
6c3118e2 1442 }
6f255425 1443
2c3db3d5 1444out:
141b38b6 1445 mutex_unlock(&sc->mutex);
2c3db3d5 1446 return ret;
f078f209
LR
1447}
1448
8feceb67 1449static void ath9k_remove_interface(struct ieee80211_hw *hw,
1ed32e4f 1450 struct ieee80211_vif *vif)
f078f209 1451{
bce048d7
JM
1452 struct ath_wiphy *aphy = hw->priv;
1453 struct ath_softc *sc = aphy->sc;
c46917bb 1454 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
1ed32e4f 1455 struct ath_vif *avp = (void *)vif->drv_priv;
2c3db3d5 1456 int i;
f078f209 1457
c46917bb 1458 ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
f078f209 1459
141b38b6
S
1460 mutex_lock(&sc->mutex);
1461
6f255425 1462 /* Stop ANI */
6c3118e2 1463 sc->sc_flags &= ~SC_OP_ANI_RUN;
3d536acf 1464 del_timer_sync(&common->ani.timer);
580f0b8a 1465
8feceb67 1466 /* Reclaim beacon resources */
9cb5412b
PE
1467 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
1468 (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
1469 (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
5f70a88f 1470 ath9k_ps_wakeup(sc);
b77f483f 1471 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
5f70a88f 1472 ath9k_ps_restore(sc);
580f0b8a 1473 }
f078f209 1474
74401773 1475 ath_beacon_return(sc, avp);
8feceb67 1476 sc->sc_flags &= ~SC_OP_BEACONS;
f078f209 1477
2c3db3d5 1478 for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
1ed32e4f 1479 if (sc->beacon.bslot[i] == vif) {
2c3db3d5
JM
1480 printk(KERN_DEBUG "%s: vif had allocated beacon "
1481 "slot\n", __func__);
1482 sc->beacon.bslot[i] = NULL;
c52f33d0 1483 sc->beacon.bslot_aphy[i] = NULL;
2c3db3d5
JM
1484 }
1485 }
1486
17d7904d 1487 sc->nvifs--;
141b38b6
S
1488
1489 mutex_unlock(&sc->mutex);
f078f209
LR
1490}
1491
3f7c5c10
SB
1492void ath9k_enable_ps(struct ath_softc *sc)
1493{
3069168c
PR
1494 struct ath_hw *ah = sc->sc_ah;
1495
3f7c5c10 1496 sc->ps_enabled = true;
3069168c
PR
1497 if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
1498 if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
1499 ah->imask |= ATH9K_INT_TIM_TIMER;
1500 ath9k_hw_set_interrupts(ah, ah->imask);
3f7c5c10 1501 }
fdf76622 1502 ath9k_hw_setrxabort(ah, 1);
3f7c5c10 1503 }
3f7c5c10
SB
1504}
1505
e8975581 1506static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
f078f209 1507{
bce048d7
JM
1508 struct ath_wiphy *aphy = hw->priv;
1509 struct ath_softc *sc = aphy->sc;
c46917bb 1510 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
e8975581 1511 struct ieee80211_conf *conf = &hw->conf;
8782b41d 1512 struct ath_hw *ah = sc->sc_ah;
194b7c13 1513 bool disable_radio;
f078f209 1514
aa33de09 1515 mutex_lock(&sc->mutex);
141b38b6 1516
194b7c13
LR
1517 /*
1518 * Leave this as the first check because we need to turn on the
1519 * radio if it was disabled before prior to processing the rest
1520 * of the changes. Likewise we must only disable the radio towards
1521 * the end.
1522 */
64839170 1523 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
194b7c13
LR
1524 bool enable_radio;
1525 bool all_wiphys_idle;
1526 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
64839170
LR
1527
1528 spin_lock_bh(&sc->wiphy_lock);
1529 all_wiphys_idle = ath9k_all_wiphys_idle(sc);
194b7c13
LR
1530 ath9k_set_wiphy_idle(aphy, idle);
1531
11446011 1532 enable_radio = (!idle && all_wiphys_idle);
194b7c13
LR
1533
1534 /*
1535 * After we unlock here its possible another wiphy
1536 * can be re-renabled so to account for that we will
1537 * only disable the radio toward the end of this routine
1538 * if by then all wiphys are still idle.
1539 */
64839170
LR
1540 spin_unlock_bh(&sc->wiphy_lock);
1541
194b7c13 1542 if (enable_radio) {
1dbfd9d4 1543 sc->ps_idle = false;
68a89116 1544 ath_radio_enable(sc, hw);
c46917bb
LR
1545 ath_print(common, ATH_DBG_CONFIG,
1546 "not-idle: enabling radio\n");
64839170
LR
1547 }
1548 }
1549
e7824a50
LR
1550 /*
1551 * We just prepare to enable PS. We have to wait until our AP has
1552 * ACK'd our null data frame to disable RX otherwise we'll ignore
1553 * those ACKs and end up retransmitting the same null data frames.
1554 * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
1555 */
3cbb5dd7 1556 if (changed & IEEE80211_CONF_CHANGE_PS) {
8ab2cd09
LR
1557 unsigned long flags;
1558 spin_lock_irqsave(&sc->sc_pm_lock, flags);
3cbb5dd7 1559 if (conf->flags & IEEE80211_CONF_PS) {
1b04b930 1560 sc->ps_flags |= PS_ENABLED;
e7824a50
LR
1561 /*
1562 * At this point we know hardware has received an ACK
1563 * of a previously sent null data frame.
1564 */
1b04b930
S
1565 if ((sc->ps_flags & PS_NULLFUNC_COMPLETED)) {
1566 sc->ps_flags &= ~PS_NULLFUNC_COMPLETED;
3f7c5c10 1567 ath9k_enable_ps(sc);
e7824a50 1568 }
3cbb5dd7 1569 } else {
96148326 1570 sc->ps_enabled = false;
1b04b930
S
1571 sc->ps_flags &= ~(PS_ENABLED |
1572 PS_NULLFUNC_COMPLETED);
8ab2cd09 1573 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
8782b41d
VN
1574 if (!(ah->caps.hw_caps &
1575 ATH9K_HW_CAP_AUTOSLEEP)) {
1576 ath9k_hw_setrxabort(sc->sc_ah, 0);
1b04b930
S
1577 sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
1578 PS_WAIT_FOR_CAB |
1579 PS_WAIT_FOR_PSPOLL_DATA |
1580 PS_WAIT_FOR_TX_ACK);
3069168c
PR
1581 if (ah->imask & ATH9K_INT_TIM_TIMER) {
1582 ah->imask &= ~ATH9K_INT_TIM_TIMER;
8782b41d 1583 ath9k_hw_set_interrupts(sc->sc_ah,
3069168c 1584 ah->imask);
8782b41d 1585 }
3cbb5dd7
VN
1586 }
1587 }
8ab2cd09 1588 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
3cbb5dd7
VN
1589 }
1590
199afd9d
S
1591 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1592 if (conf->flags & IEEE80211_CONF_MONITOR) {
1593 ath_print(common, ATH_DBG_CONFIG,
1594 "HW opmode set to Monitor mode\n");
1595 sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
1596 }
1597 }
1598
4797938c 1599 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
99405f93 1600 struct ieee80211_channel *curchan = hw->conf.channel;
5f8e077c 1601 int pos = curchan->hw_value;
ae5eb026 1602
0e2dedf9
JM
1603 aphy->chan_idx = pos;
1604 aphy->chan_is_ht = conf_is_ht(conf);
5ee08656
FF
1605 if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
1606 sc->sc_flags |= SC_OP_OFFCHANNEL;
1607 else
1608 sc->sc_flags &= ~SC_OP_OFFCHANNEL;
0e2dedf9 1609
8089cc47
JM
1610 if (aphy->state == ATH_WIPHY_SCAN ||
1611 aphy->state == ATH_WIPHY_ACTIVE)
1612 ath9k_wiphy_pause_all_forced(sc, aphy);
1613 else {
1614 /*
1615 * Do not change operational channel based on a paused
1616 * wiphy changes.
1617 */
1618 goto skip_chan_change;
1619 }
0e2dedf9 1620
c46917bb
LR
1621 ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1622 curchan->center_freq);
f078f209 1623
5f8e077c 1624 /* XXX: remove me eventualy */
0e2dedf9 1625 ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
e11602b7 1626
ecf70441 1627 ath_update_chainmask(sc, conf_is_ht(conf));
86060f0d 1628
0e2dedf9 1629 if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
c46917bb
LR
1630 ath_print(common, ATH_DBG_FATAL,
1631 "Unable to set channel\n");
aa33de09 1632 mutex_unlock(&sc->mutex);
e11602b7
S
1633 return -EINVAL;
1634 }
094d05dc 1635 }
f078f209 1636
8089cc47 1637skip_chan_change:
c9f6a656 1638 if (changed & IEEE80211_CONF_CHANGE_POWER) {
17d7904d 1639 sc->config.txpowlimit = 2 * conf->power_level;
c9f6a656
LR
1640 ath_update_txpow(sc);
1641 }
f078f209 1642
194b7c13
LR
1643 spin_lock_bh(&sc->wiphy_lock);
1644 disable_radio = ath9k_all_wiphys_idle(sc);
1645 spin_unlock_bh(&sc->wiphy_lock);
1646
64839170 1647 if (disable_radio) {
c46917bb 1648 ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
1dbfd9d4 1649 sc->ps_idle = true;
68a89116 1650 ath_radio_disable(sc, hw);
64839170
LR
1651 }
1652
aa33de09 1653 mutex_unlock(&sc->mutex);
141b38b6 1654
f078f209
LR
1655 return 0;
1656}
1657
8feceb67
VT
1658#define SUPPORTED_FILTERS \
1659 (FIF_PROMISC_IN_BSS | \
1660 FIF_ALLMULTI | \
1661 FIF_CONTROL | \
af6a3fc7 1662 FIF_PSPOLL | \
8feceb67
VT
1663 FIF_OTHER_BSS | \
1664 FIF_BCN_PRBRESP_PROMISC | \
1665 FIF_FCSFAIL)
c83be688 1666
8feceb67
VT
1667/* FIXME: sc->sc_full_reset ? */
1668static void ath9k_configure_filter(struct ieee80211_hw *hw,
1669 unsigned int changed_flags,
1670 unsigned int *total_flags,
3ac64bee 1671 u64 multicast)
8feceb67 1672{
bce048d7
JM
1673 struct ath_wiphy *aphy = hw->priv;
1674 struct ath_softc *sc = aphy->sc;
8feceb67 1675 u32 rfilt;
f078f209 1676
8feceb67
VT
1677 changed_flags &= SUPPORTED_FILTERS;
1678 *total_flags &= SUPPORTED_FILTERS;
f078f209 1679
b77f483f 1680 sc->rx.rxfilter = *total_flags;
aa68aeaa 1681 ath9k_ps_wakeup(sc);
8feceb67
VT
1682 rfilt = ath_calcrxfilter(sc);
1683 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
aa68aeaa 1684 ath9k_ps_restore(sc);
f078f209 1685
c46917bb
LR
1686 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
1687 "Set HW RX filter: 0x%x\n", rfilt);
8feceb67 1688}
f078f209 1689
4ca77860
JB
1690static int ath9k_sta_add(struct ieee80211_hw *hw,
1691 struct ieee80211_vif *vif,
1692 struct ieee80211_sta *sta)
8feceb67 1693{
bce048d7
JM
1694 struct ath_wiphy *aphy = hw->priv;
1695 struct ath_softc *sc = aphy->sc;
f078f209 1696
4ca77860
JB
1697 ath_node_attach(sc, sta);
1698
1699 return 0;
1700}
1701
1702static int ath9k_sta_remove(struct ieee80211_hw *hw,
1703 struct ieee80211_vif *vif,
1704 struct ieee80211_sta *sta)
1705{
1706 struct ath_wiphy *aphy = hw->priv;
1707 struct ath_softc *sc = aphy->sc;
1708
1709 ath_node_detach(sc, sta);
1710
1711 return 0;
f078f209
LR
1712}
1713
141b38b6 1714static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
8feceb67 1715 const struct ieee80211_tx_queue_params *params)
f078f209 1716{
bce048d7
JM
1717 struct ath_wiphy *aphy = hw->priv;
1718 struct ath_softc *sc = aphy->sc;
c46917bb 1719 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
8feceb67
VT
1720 struct ath9k_tx_queue_info qi;
1721 int ret = 0, qnum;
f078f209 1722
8feceb67
VT
1723 if (queue >= WME_NUM_AC)
1724 return 0;
f078f209 1725
141b38b6
S
1726 mutex_lock(&sc->mutex);
1727
1ffb0610
S
1728 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1729
8feceb67
VT
1730 qi.tqi_aifs = params->aifs;
1731 qi.tqi_cwmin = params->cw_min;
1732 qi.tqi_cwmax = params->cw_max;
1733 qi.tqi_burstTime = params->txop;
1734 qnum = ath_get_hal_qnum(queue, sc);
f078f209 1735
c46917bb
LR
1736 ath_print(common, ATH_DBG_CONFIG,
1737 "Configure tx [queue/halq] [%d/%d], "
1738 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1739 queue, qnum, params->aifs, params->cw_min,
1740 params->cw_max, params->txop);
f078f209 1741
8feceb67
VT
1742 ret = ath_txq_update(sc, qnum, &qi);
1743 if (ret)
c46917bb 1744 ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
f078f209 1745
94db2936 1746 if (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
1d2231e2 1747 if ((qnum == sc->tx.hwq_map[WME_AC_BE]) && !ret)
94db2936
VN
1748 ath_beaconq_config(sc);
1749
141b38b6
S
1750 mutex_unlock(&sc->mutex);
1751
8feceb67
VT
1752 return ret;
1753}
f078f209 1754
8feceb67
VT
1755static int ath9k_set_key(struct ieee80211_hw *hw,
1756 enum set_key_cmd cmd,
dc822b5d
JB
1757 struct ieee80211_vif *vif,
1758 struct ieee80211_sta *sta,
8feceb67
VT
1759 struct ieee80211_key_conf *key)
1760{
bce048d7
JM
1761 struct ath_wiphy *aphy = hw->priv;
1762 struct ath_softc *sc = aphy->sc;
c46917bb 1763 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
8feceb67 1764 int ret = 0;
f078f209 1765
b3bd89ce
JM
1766 if (modparam_nohwcrypt)
1767 return -ENOSPC;
1768
141b38b6 1769 mutex_lock(&sc->mutex);
3cbb5dd7 1770 ath9k_ps_wakeup(sc);
c46917bb 1771 ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
f078f209 1772
8feceb67
VT
1773 switch (cmd) {
1774 case SET_KEY:
040e539e 1775 ret = ath_key_config(common, vif, sta, key);
6ace2891
JM
1776 if (ret >= 0) {
1777 key->hw_key_idx = ret;
8feceb67
VT
1778 /* push IV and Michael MIC generation to stack */
1779 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
97359d12 1780 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
8feceb67 1781 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
97359d12
JB
1782 if (sc->sc_ah->sw_mgmt_crypto &&
1783 key->cipher == WLAN_CIPHER_SUITE_CCMP)
0ced0e17 1784 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
6ace2891 1785 ret = 0;
8feceb67
VT
1786 }
1787 break;
1788 case DISABLE_KEY:
040e539e 1789 ath_key_delete(common, key);
8feceb67
VT
1790 break;
1791 default:
1792 ret = -EINVAL;
1793 }
f078f209 1794
3cbb5dd7 1795 ath9k_ps_restore(sc);
141b38b6
S
1796 mutex_unlock(&sc->mutex);
1797
8feceb67
VT
1798 return ret;
1799}
f078f209 1800
8feceb67
VT
1801static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
1802 struct ieee80211_vif *vif,
1803 struct ieee80211_bss_conf *bss_conf,
1804 u32 changed)
1805{
bce048d7
JM
1806 struct ath_wiphy *aphy = hw->priv;
1807 struct ath_softc *sc = aphy->sc;
2d0ddec5 1808 struct ath_hw *ah = sc->sc_ah;
1510718d 1809 struct ath_common *common = ath9k_hw_common(ah);
2d0ddec5 1810 struct ath_vif *avp = (void *)vif->drv_priv;
0005baf4 1811 int slottime;
c6089ccc 1812 int error;
f078f209 1813
141b38b6
S
1814 mutex_lock(&sc->mutex);
1815
c6089ccc
S
1816 if (changed & BSS_CHANGED_BSSID) {
1817 /* Set BSSID */
1818 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1819 memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
1510718d 1820 common->curaid = 0;
f2b2143e 1821 ath9k_hw_write_associd(ah);
2d0ddec5 1822
c6089ccc
S
1823 /* Set aggregation protection mode parameters */
1824 sc->config.ath_aggr_prot = 0;
2d0ddec5 1825
c6089ccc
S
1826 /* Only legacy IBSS for now */
1827 if (vif->type == NL80211_IFTYPE_ADHOC)
1828 ath_update_chainmask(sc, 0);
2d0ddec5 1829
c6089ccc
S
1830 ath_print(common, ATH_DBG_CONFIG,
1831 "BSSID: %pM aid: 0x%x\n",
1832 common->curbssid, common->curaid);
2d0ddec5 1833
c6089ccc
S
1834 /* need to reconfigure the beacon */
1835 sc->sc_flags &= ~SC_OP_BEACONS ;
1836 }
2d0ddec5 1837
c6089ccc
S
1838 /* Enable transmission of beacons (AP, IBSS, MESH) */
1839 if ((changed & BSS_CHANGED_BEACON) ||
1840 ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon)) {
1841 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
1842 error = ath_beacon_alloc(aphy, vif);
1843 if (!error)
1844 ath_beacon_config(sc, vif);
0005baf4
FF
1845 }
1846
1847 if (changed & BSS_CHANGED_ERP_SLOT) {
1848 if (bss_conf->use_short_slot)
1849 slottime = 9;
1850 else
1851 slottime = 20;
1852 if (vif->type == NL80211_IFTYPE_AP) {
1853 /*
1854 * Defer update, so that connected stations can adjust
1855 * their settings at the same time.
1856 * See beacon.c for more details
1857 */
1858 sc->beacon.slottime = slottime;
1859 sc->beacon.updateslot = UPDATE;
1860 } else {
1861 ah->slottime = slottime;
1862 ath9k_hw_init_global_settings(ah);
1863 }
2d0ddec5
JB
1864 }
1865
c6089ccc
S
1866 /* Disable transmission of beacons */
1867 if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon)
1868 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
2d0ddec5 1869
c6089ccc
S
1870 if (changed & BSS_CHANGED_BEACON_INT) {
1871 sc->beacon_interval = bss_conf->beacon_int;
1872 /*
1873 * In case of AP mode, the HW TSF has to be reset
1874 * when the beacon interval changes.
1875 */
1876 if (vif->type == NL80211_IFTYPE_AP) {
1877 sc->sc_flags |= SC_OP_TSF_RESET;
1878 ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
2d0ddec5
JB
1879 error = ath_beacon_alloc(aphy, vif);
1880 if (!error)
1881 ath_beacon_config(sc, vif);
c6089ccc
S
1882 } else {
1883 ath_beacon_config(sc, vif);
2d0ddec5
JB
1884 }
1885 }
1886
8feceb67 1887 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
c46917bb
LR
1888 ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
1889 bss_conf->use_short_preamble);
8feceb67
VT
1890 if (bss_conf->use_short_preamble)
1891 sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
1892 else
1893 sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
1894 }
f078f209 1895
8feceb67 1896 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
c46917bb
LR
1897 ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
1898 bss_conf->use_cts_prot);
8feceb67
VT
1899 if (bss_conf->use_cts_prot &&
1900 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
1901 sc->sc_flags |= SC_OP_PROTECT_ENABLE;
1902 else
1903 sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
1904 }
f078f209 1905
8feceb67 1906 if (changed & BSS_CHANGED_ASSOC) {
c46917bb 1907 ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
8feceb67 1908 bss_conf->assoc);
5640b08e 1909 ath9k_bss_assoc_info(sc, vif, bss_conf);
8feceb67 1910 }
141b38b6
S
1911
1912 mutex_unlock(&sc->mutex);
8feceb67 1913}
f078f209 1914
8feceb67
VT
1915static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
1916{
1917 u64 tsf;
bce048d7
JM
1918 struct ath_wiphy *aphy = hw->priv;
1919 struct ath_softc *sc = aphy->sc;
f078f209 1920
141b38b6
S
1921 mutex_lock(&sc->mutex);
1922 tsf = ath9k_hw_gettsf64(sc->sc_ah);
1923 mutex_unlock(&sc->mutex);
f078f209 1924
8feceb67
VT
1925 return tsf;
1926}
f078f209 1927
3b5d665b
AF
1928static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1929{
bce048d7
JM
1930 struct ath_wiphy *aphy = hw->priv;
1931 struct ath_softc *sc = aphy->sc;
3b5d665b 1932
141b38b6
S
1933 mutex_lock(&sc->mutex);
1934 ath9k_hw_settsf64(sc->sc_ah, tsf);
1935 mutex_unlock(&sc->mutex);
3b5d665b
AF
1936}
1937
8feceb67
VT
1938static void ath9k_reset_tsf(struct ieee80211_hw *hw)
1939{
bce048d7
JM
1940 struct ath_wiphy *aphy = hw->priv;
1941 struct ath_softc *sc = aphy->sc;
c83be688 1942
141b38b6 1943 mutex_lock(&sc->mutex);
21526d57
LR
1944
1945 ath9k_ps_wakeup(sc);
141b38b6 1946 ath9k_hw_reset_tsf(sc->sc_ah);
21526d57
LR
1947 ath9k_ps_restore(sc);
1948
141b38b6 1949 mutex_unlock(&sc->mutex);
8feceb67 1950}
f078f209 1951
8feceb67 1952static int ath9k_ampdu_action(struct ieee80211_hw *hw,
c951ad35 1953 struct ieee80211_vif *vif,
141b38b6
S
1954 enum ieee80211_ampdu_mlme_action action,
1955 struct ieee80211_sta *sta,
1956 u16 tid, u16 *ssn)
8feceb67 1957{
bce048d7
JM
1958 struct ath_wiphy *aphy = hw->priv;
1959 struct ath_softc *sc = aphy->sc;
8feceb67 1960 int ret = 0;
f078f209 1961
85ad181e
JB
1962 local_bh_disable();
1963
8feceb67
VT
1964 switch (action) {
1965 case IEEE80211_AMPDU_RX_START:
dca3edb8
S
1966 if (!(sc->sc_flags & SC_OP_RXAGGR))
1967 ret = -ENOTSUPP;
8feceb67
VT
1968 break;
1969 case IEEE80211_AMPDU_RX_STOP:
8feceb67
VT
1970 break;
1971 case IEEE80211_AMPDU_TX_START:
8b685ba9 1972 ath9k_ps_wakeup(sc);
f83da965 1973 ath_tx_aggr_start(sc, sta, tid, ssn);
c951ad35 1974 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
8b685ba9 1975 ath9k_ps_restore(sc);
8feceb67
VT
1976 break;
1977 case IEEE80211_AMPDU_TX_STOP:
8b685ba9 1978 ath9k_ps_wakeup(sc);
f83da965 1979 ath_tx_aggr_stop(sc, sta, tid);
c951ad35 1980 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
8b685ba9 1981 ath9k_ps_restore(sc);
8feceb67 1982 break;
b1720231 1983 case IEEE80211_AMPDU_TX_OPERATIONAL:
8b685ba9 1984 ath9k_ps_wakeup(sc);
8469cdef 1985 ath_tx_aggr_resume(sc, sta, tid);
8b685ba9 1986 ath9k_ps_restore(sc);
8469cdef 1987 break;
8feceb67 1988 default:
c46917bb
LR
1989 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
1990 "Unknown AMPDU action\n");
8feceb67
VT
1991 }
1992
85ad181e
JB
1993 local_bh_enable();
1994
8feceb67 1995 return ret;
f078f209
LR
1996}
1997
62dad5b0
BP
1998static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
1999 struct survey_info *survey)
2000{
2001 struct ath_wiphy *aphy = hw->priv;
2002 struct ath_softc *sc = aphy->sc;
2003 struct ath_hw *ah = sc->sc_ah;
2004 struct ath_common *common = ath9k_hw_common(ah);
2005 struct ieee80211_conf *conf = &hw->conf;
2006
2007 if (idx != 0)
2008 return -ENOENT;
2009
2010 survey->channel = conf->channel;
2011 survey->filled = SURVEY_INFO_NOISE_DBM;
2012 survey->noise = common->ani.noise_floor;
2013
2014 return 0;
2015}
2016
0c98de65
S
2017static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
2018{
bce048d7
JM
2019 struct ath_wiphy *aphy = hw->priv;
2020 struct ath_softc *sc = aphy->sc;
0c98de65 2021
3d832611 2022 mutex_lock(&sc->mutex);
8089cc47 2023 if (ath9k_wiphy_scanning(sc)) {
8089cc47 2024 /*
30888338
LR
2025 * There is a race here in mac80211 but fixing it requires
2026 * we revisit how we handle the scan complete callback.
2027 * After mac80211 fixes we will not have configured hardware
2028 * to the home channel nor would we have configured the RX
2029 * filter yet.
8089cc47 2030 */
3d832611 2031 mutex_unlock(&sc->mutex);
8089cc47
JM
2032 return;
2033 }
2034
2035 aphy->state = ATH_WIPHY_SCAN;
2036 ath9k_wiphy_pause_all_forced(sc, aphy);
3d832611 2037 mutex_unlock(&sc->mutex);
0c98de65
S
2038}
2039
30888338
LR
2040/*
2041 * XXX: this requires a revisit after the driver
2042 * scan_complete gets moved to another place/removed in mac80211.
2043 */
0c98de65
S
2044static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
2045{
bce048d7
JM
2046 struct ath_wiphy *aphy = hw->priv;
2047 struct ath_softc *sc = aphy->sc;
0c98de65 2048
3d832611 2049 mutex_lock(&sc->mutex);
8089cc47 2050 aphy->state = ATH_WIPHY_ACTIVE;
3d832611 2051 mutex_unlock(&sc->mutex);
0c98de65
S
2052}
2053
e239d859
FF
2054static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
2055{
2056 struct ath_wiphy *aphy = hw->priv;
2057 struct ath_softc *sc = aphy->sc;
2058 struct ath_hw *ah = sc->sc_ah;
2059
2060 mutex_lock(&sc->mutex);
2061 ah->coverage_class = coverage_class;
2062 ath9k_hw_init_global_settings(ah);
2063 mutex_unlock(&sc->mutex);
2064}
2065
6baff7f9 2066struct ieee80211_ops ath9k_ops = {
8feceb67
VT
2067 .tx = ath9k_tx,
2068 .start = ath9k_start,
2069 .stop = ath9k_stop,
2070 .add_interface = ath9k_add_interface,
2071 .remove_interface = ath9k_remove_interface,
2072 .config = ath9k_config,
8feceb67 2073 .configure_filter = ath9k_configure_filter,
4ca77860
JB
2074 .sta_add = ath9k_sta_add,
2075 .sta_remove = ath9k_sta_remove,
8feceb67 2076 .conf_tx = ath9k_conf_tx,
8feceb67 2077 .bss_info_changed = ath9k_bss_info_changed,
8feceb67 2078 .set_key = ath9k_set_key,
8feceb67 2079 .get_tsf = ath9k_get_tsf,
3b5d665b 2080 .set_tsf = ath9k_set_tsf,
8feceb67 2081 .reset_tsf = ath9k_reset_tsf,
4233df6b 2082 .ampdu_action = ath9k_ampdu_action,
62dad5b0 2083 .get_survey = ath9k_get_survey,
0c98de65
S
2084 .sw_scan_start = ath9k_sw_scan_start,
2085 .sw_scan_complete = ath9k_sw_scan_complete,
3b319aae 2086 .rfkill_poll = ath9k_rfkill_poll_state,
e239d859 2087 .set_coverage_class = ath9k_set_coverage_class,
8feceb67 2088};