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