carl9170 firmware: initial WoWLAN support
[carl9170fw.git] / carlfw / src / wlan.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Interface to the WLAN part of the chip
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009-2011  Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "carl9170.h"
27 #include "shared/phy.h"
28 #include "hostif.h"
29 #include "timer.h"
30 #include "wl.h"
31 #include "printf.h"
32 #include "rf.h"
33 #include "linux/ieee80211.h"
34
35 static void wlan_txunstuck(unsigned int queue)
36 {
37         set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head) | 1);
38 }
39
40 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
41 static void wlan_txupdate(unsigned int queue)
42 {
43         set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head));
44 }
45
46 static void wlan_dma_bump(unsigned int qidx)
47 {
48         unsigned int offset = qidx;
49         uint32_t status, trigger;
50
51         status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
52         trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
53
54         while (offset != 0) {
55                 status >>= 4;
56                 trigger >>= 4;
57                 offset--;
58         }
59
60         status &= 0xf;
61         trigger &= 0xf;
62
63         if ((trigger == 0xa) && (status == 0x8)) {
64                 DBG("UNSTUCK");
65                 wlan_txunstuck(qidx);
66         } else {
67                 DBG("UPDATE");
68                 wlan_txupdate(qidx);
69         }
70 }
71 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
72
73 #ifdef CONFIG_CARL9170FW_DEBUG
74 static void wlan_dump_queue(unsigned int qidx)
75 {
76
77         struct dma_desc *desc;
78         struct carl9170_tx_superframe *super;
79         int entries = 0;
80
81         __for_each_desc(desc, &fw.wlan.tx_queue[qidx]) {
82                 super = get_super(desc);
83                 DBG("%d: %p s:%x c:%x tl:%x ds:%x n:%p l:%p ", entries, desc,
84                     desc->status, desc->ctrl, desc->totalLen,
85                     desc->dataSize, desc->nextAddr, desc->lastAddr);
86
87                 DBG("c:%x tr:%d ri:%d l:%x m:%x p:%x fc:%x",
88                     super->s.cookie, super->s.cnt, super->s.rix,
89                     super->f.hdr.length, super->f.hdr.mac.set,
90                     (unsigned int) le32_to_cpu(super->f.hdr.phy.set),
91                     super->f.data.i3e.frame_control);
92
93                 entries++;
94         }
95
96         desc = get_wlan_txq_addr(qidx);
97
98         DBG("Queue: %d: te:%d td:%d h:%p c:%p t:%p",
99             qidx, entries, queue_len(&fw.wlan.tx_queue[qidx]),
100             fw.wlan.tx_queue[qidx].head,
101             desc, fw.wlan.tx_queue[qidx].terminator);
102
103         DBG("HW: t:%x s:%x ac:%x c:%x",
104             (unsigned int) get(AR9170_MAC_REG_DMA_TRIGGER),
105             (unsigned int) get(AR9170_MAC_REG_DMA_STATUS),
106             (unsigned int) get(AR9170_MAC_REG_AMPDU_COUNT),
107             (unsigned int) get(AR9170_MAC_REG_DMA_TXQX_ADDR_CURR));
108 }
109 #endif /* CONFIG_CARL9170FW_DEBUG */
110
111 static void wlan_send_buffered_tx_status(void)
112 {
113         unsigned int len;
114
115         while (fw.wlan.tx_status_pending) {
116                 len = min((unsigned int)fw.wlan.tx_status_pending,
117                           CARL9170_RSP_TX_STATUS_NUM);
118                 len = min(len, CARL9170_TX_STATUS_NUM - fw.wlan.tx_status_head_idx);
119
120                 /*
121                  * rather than memcpy each individual request into a large buffer,
122                  * we _splice_ them all together.
123                  *
124                  * The only downside is however that we have to be careful around
125                  * the edges of the tx_status_cache.
126                  *
127                  * Note:
128                  * Each tx_status is about 2 bytes. However every command package
129                  * must have a size which is a multiple of 4.
130                  */
131
132                 send_cmd_to_host((len * sizeof(struct carl9170_tx_status) + 3) & ~3,
133                                  CARL9170_RSP_TXCOMP, len, (void *)
134                                  &fw.wlan.tx_status_cache[fw.wlan.tx_status_head_idx]);
135
136                 fw.wlan.tx_status_pending -= len;
137                 fw.wlan.tx_status_head_idx += len;
138                 fw.wlan.tx_status_head_idx %= CARL9170_TX_STATUS_NUM;
139         }
140 }
141
142 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
143 {
144         struct carl9170_tx_status *tmp;
145
146         tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
147         fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
148
149         if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
150                 wlan_send_buffered_tx_status();
151
152         fw.wlan.tx_status_pending++;
153
154         return tmp;
155 }
156
157 /* generate _aggregated_ tx_status for the host */
158 void wlan_tx_complete(struct carl9170_tx_superframe *super,
159                       bool txs)
160 {
161         struct carl9170_tx_status *status;
162
163         status = wlan_get_tx_status_buffer();
164
165         /*
166          * The *unique* cookie and AC_ID is used by the driver for
167          * frame lookup.
168          */
169         status->cookie = super->s.cookie;
170         status->queue = super->s.queue;
171         super->s.cookie = 0;
172
173         /*
174          * This field holds the number of tries of the rate in
175          * the rate index field (rix).
176          */
177         status->rix = super->s.rix;
178         status->tries = super->s.cnt;
179         status->success = (txs) ? 1 : 0;
180 }
181
182 static bool wlan_tx_consume_retry(struct carl9170_tx_superframe *super)
183 {
184         /* check if this was the last possible retry with this rate */
185         if (unlikely(super->s.cnt >= super->s.ri[super->s.rix].tries)) {
186                 /* end of the road - indicate tx failure */
187                 if (unlikely(super->s.rix == CARL9170_TX_MAX_RETRY_RATES))
188                         return false;
189
190                 /* check if there are alternative rates available */
191                 if (!super->s.rr[super->s.rix].set)
192                         return false;
193
194                 /* try next retry rate */
195                 super->f.hdr.phy.set = super->s.rr[super->s.rix].set;
196
197                 /* finally - mark the old rate as USED */
198                 super->s.rix++;
199
200                 /* update MAC flags */
201                 super->f.hdr.mac.erp_prot = super->s.ri[super->s.rix].erp_prot;
202                 super->f.hdr.mac.ampdu = super->s.ri[super->s.rix].ampdu;
203
204                 /* reinitialize try counter */
205                 super->s.cnt = 1;
206         } else {
207                 /* just increase retry counter */
208                 super->s.cnt++;
209         }
210
211         return true;
212 }
213
214 static inline u16 get_tid(struct ieee80211_hdr *hdr)
215 {
216         return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
217 }
218
219 /* This function will only work on uint32_t-aligned pointers! */
220 static inline bool compare_ether_address(const void *_d0, const void *_d1)
221 {
222         const uint32_t *d0 = _d0;
223         const uint32_t *d1 = _d1;
224
225         /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
226         return !((d0[0] ^ d1[0]) | (unsigned short)(d0[1] ^ d1[1]));
227 }
228
229 /* This function will only work on uint32_t-aligned pointers! */
230 static bool same_hdr(const void *_d0, const void *_d1)
231 {
232         const uint32_t *d0 = _d0;
233         const uint32_t *d1 = _d1;
234
235         /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
236         return !((d0[0] ^ d1[0]) |                      /* FC + DU */
237                  (d0[1] ^ d1[1]) |                      /* addr1 */
238                  (d0[2] ^ d1[2]) | (d0[3] ^ d1[3]) |    /* addr2 + addr3 */
239                  (d0[4] ^ d1[4]));                      /* addr3 */
240 }
241
242 static inline bool same_aggr(struct ieee80211_hdr *a, struct ieee80211_hdr *b)
243 {
244         return (get_tid(a) == get_tid(b)) || same_hdr(a, b);
245 }
246
247 static void wlan_tx_ampdu_end(unsigned int qidx)
248 {
249         struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
250
251         fw.wlan.ampdu_prev[qidx] = NULL;
252         if (ht_prev)
253                 ht_prev->f.hdr.mac.ba_end = 1;
254 }
255
256 static void wlan_tx_ampdu(struct carl9170_tx_superframe *super)
257 {
258         unsigned int qidx = super->s.queue;
259         struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
260
261         if (!super->f.hdr.mac.ampdu) {
262                 wlan_tx_ampdu_end(qidx);
263         } else {
264                 fw.wlan.ampdu_prev[qidx] = super;
265
266                 if (ht_prev &&
267                     !same_aggr(&super->f.data.i3e, &ht_prev->f.data.i3e))
268                         ht_prev->f.hdr.mac.ba_end = 1;
269                 else
270                         super->f.hdr.mac.ba_end = 0;
271         }
272 }
273
274 /* for all tries */
275 static void __wlan_tx(struct dma_desc *desc)
276 {
277         struct carl9170_tx_superframe *super = get_super(desc);
278
279         if (unlikely(super->s.fill_in_tsf)) {
280                 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
281                 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
282
283                 /*
284                  * Truth be told: this is a hack.
285                  *
286                  * The *real* TSF is definitely going to be higher/older.
287                  * But this hardware emulation code is head and shoulders
288                  * above anything a driver can possibly do.
289                  *
290                  * (even, if it's got an accurate atomic clock source).
291                  */
292
293                 read_tsf(tsf);
294         }
295
296         wlan_tx_ampdu(super);
297
298 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
299         wlan_tx_complete(super, true);
300         unhide_super(desc);
301 # ifdef CONFIG_CARL9170FW_LOOPBACK
302         dma_put(&fw.pta.up_queue, desc);
303         up_trigger();
304 # elif CONFIG_CARL9170FW_DISCARD
305         dma_reclaim(&fw.pta.down_queue, desc);
306         down_trigger();
307 # endif
308 #else /* CONFIG_CARL9170FW_LOOPBACK */
309
310 # ifdef CONFIG_CARL9170FW_DEBUG
311         BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
312 # endif /* CONFIG_CARL9170FW_DEBUG */
313
314         /* insert desc into the right queue */
315         dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
316 #endif /* CONFIG_CARL9170FW_LOOPBACK */
317 }
318
319 static void wlan_assign_seq(struct ieee80211_hdr *hdr, unsigned int vif)
320 {
321         hdr->seq_ctrl &= cpu_to_le16(~IEEE80211_SCTL_SEQ);
322         hdr->seq_ctrl |= cpu_to_le16(fw.wlan.sequence[vif]);
323
324         if (!(hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG)))
325                 fw.wlan.sequence[vif] += 0x10;
326 }
327
328 /* prepares frame for the first transmission */
329 static void _wlan_tx(struct dma_desc *desc)
330 {
331         struct carl9170_tx_superframe *super = get_super(desc);
332
333         if (unlikely(super->s.assign_seq)) {
334                 wlan_assign_seq(&super->f.data.i3e, super->s.vif_id);
335         }
336
337         if (unlikely(super->s.ampdu_commit_density)) {
338                 set(AR9170_MAC_REG_AMPDU_DENSITY,
339                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
340                             get(AR9170_MAC_REG_AMPDU_DENSITY),
341                             super->s.ampdu_density));
342         }
343
344         if (unlikely(super->s.ampdu_commit_factor)) {
345                 set(AR9170_MAC_REG_AMPDU_FACTOR,
346                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
347                             get(AR9170_MAC_REG_AMPDU_FACTOR),
348                             8 << super->s.ampdu_factor));
349         }
350
351         __wlan_tx(desc);
352 }
353
354 /* propagate transmission status back to the driver */
355 static bool wlan_tx_status(struct dma_queue *queue,
356                            struct dma_desc *desc)
357 {
358         struct carl9170_tx_superframe *super = get_super(desc);
359         unsigned int qidx = super->s.queue;
360         bool txfail = false, success;
361
362         success = true;
363
364         /* update hangcheck */
365         fw.wlan.last_super_num[qidx] = 0;
366
367         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
368                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
369
370                 /* reset retry indicator flags */
371                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
372
373                 if (wlan_tx_consume_retry(super)) {
374                         /*
375                          * retry for simple and aggregated 802.11 frames.
376                          *
377                          * Note: We must not mess up the original frame
378                          * order.
379                          */
380
381                         if (!super->f.hdr.mac.ampdu) {
382                                 /*
383                                  * 802.11 - 7.1.3.1.5.
384                                  * set "Retry Field" for consecutive attempts
385                                  *
386                                  * Note: For AMPDU see:
387                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
388                                  */
389                                 super->f.data.i3e.frame_control |=
390                                         cpu_to_le16(IEEE80211_FCTL_RETRY);
391                         }
392
393                         if (txfail) {
394                                 /* Normal TX Failure */
395
396                                 /* demise descriptor ownership back to the hardware */
397                                 dma_rearm(desc);
398
399                                 /*
400                                  * And this will get the queue going again.
401                                  * To understand why: you have to get the HW
402                                  * specs... But sadly I never saw them.
403                                  */
404                                 wlan_txunstuck(qidx);
405
406                                 /* abort cycle - this is necessary due to HW design */
407                                 return false;
408                         } else {
409                                 /* (HT-) BlockACK failure */
410
411                                 /*
412                                  * Unlink the failed attempt and put it into
413                                  * the retry queue. The caller routine must
414                                  * be aware of this so the frames don't get lost.
415                                  */
416
417                                 dma_unlink_head(queue);
418                                 dma_put(&fw.wlan.tx_retry, desc);
419                                 return true;
420                         }
421                 } else {
422                         /* out of frame attempts - discard frame */
423                         success = false;
424                 }
425         }
426
427         dma_unlink_head(queue);
428         if (txfail) {
429                 /*
430                  * Issue the queue bump,
431                  * We need to do this in case this was the frame's last
432                  * possible retry attempt and it unfortunately: it failed.
433                  */
434
435                 wlan_txunstuck(qidx);
436         }
437
438         unhide_super(desc);
439
440         if (unlikely(super == fw.wlan.fw_desc_data)) {
441                 fw.wlan.fw_desc = desc;
442                 fw.wlan.fw_desc_available = 1;
443
444                 if (fw.wlan.fw_desc_callback)
445                         fw.wlan.fw_desc_callback(super, success);
446
447                 return true;
448         }
449
450 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
451         if (unlikely(super->s.cab))
452                 fw.wlan.cab_queue_len[super->s.vif_id]--;
453 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
454
455         wlan_tx_complete(super, success);
456
457         /* recycle freed descriptors */
458         dma_reclaim(&fw.pta.down_queue, desc);
459         down_trigger();
460         return true;
461 }
462
463 static void handle_tx_completion(void)
464 {
465         struct dma_desc *desc;
466         int i;
467
468         for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
469                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
470                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
471                                 /* termination requested. */
472                                 break;
473                         }
474                 }
475
476                 for_each_desc(desc, &fw.wlan.tx_retry)
477                         __wlan_tx(desc);
478
479                 wlan_tx_ampdu_end(i);
480                 if (!queue_empty(&fw.wlan.tx_queue[i]))
481                         wlan_trigger(BIT(i));
482         }
483 }
484
485 void __hot wlan_tx(struct dma_desc *desc)
486 {
487         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
488
489         /* initialize rate control struct */
490         super->s.rix = 0;
491         super->s.cnt = 1;
492         hide_super(desc);
493
494 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
495         if (unlikely(super->s.cab)) {
496                 fw.wlan.cab_queue_len[super->s.vif_id]++;
497                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
498                 return;
499         }
500 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
501
502         _wlan_tx(desc);
503         wlan_trigger(BIT(super->s.queue));
504 }
505
506 static void wlan_tx_fw(struct carl9170_tx_superdesc *super, fw_desc_callback_t cb)
507 {
508         if (!fw.wlan.fw_desc_available)
509                 return;
510
511         fw.wlan.fw_desc_available = 0;
512
513         /* Format BlockAck */
514         fw.wlan.fw_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
515         fw.wlan.fw_desc->status = AR9170_OWN_BITS_SW;
516
517         fw.wlan.fw_desc->totalLen = fw.wlan.fw_desc->dataSize = super->len;
518         fw.wlan.fw_desc_data = fw.wlan.fw_desc->dataAddr = super;
519         fw.wlan.fw_desc->nextAddr = fw.wlan.fw_desc->lastAddr =
520                 fw.wlan.fw_desc;
521         fw.wlan.fw_desc_callback = cb;
522         wlan_tx(fw.wlan.fw_desc);
523 }
524
525 static void wlan_send_buffered_ba(void)
526 {
527         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
528         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
529         struct carl9170_bar_ctx *ctx;
530
531         if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
532                 return;
533
534         /* there's no point to continue when the ba_desc is not available. */
535         if (!fw.wlan.fw_desc_available)
536                 return;
537
538         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx];
539         fw.wlan.ba_head_idx++;
540         fw.wlan.ba_head_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
541
542         baf->s.len = sizeof(struct carl9170_tx_superdesc) +
543                      sizeof(struct ar9170_tx_hwdesc) +
544                      sizeof(struct ieee80211_ba);
545         baf->s.ri[0].tries = 1;
546         baf->s.cookie = 0;
547         baf->s.queue = AR9170_TXQ_VO;
548         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
549
550         /* HW Duration / Backoff */
551         baf->f.hdr.mac.backoff = 1;
552         baf->f.hdr.mac.hw_duration = 1;
553
554         /* take the TX rate from the RX'd BAR */
555         baf->f.hdr.phy.set = ctx->phy;
556         baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
557
558         /* format outgoing BA */
559         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_NULLFUNC);
560         ba->duration = cpu_to_le16(0);
561         memcpy(ba->ta, ctx->ta, 6);
562         memcpy(ba->ra, ctx->ra, 6);
563
564         /*
565          * Unfortunately, we cannot look into the hardware's scoreboard.
566          * Therefore we have to proceed as described in 802.11n 9.10.7.5
567          * and send a null BlockAck.
568          */
569         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
570
571         /*
572          * NB:
573          * not entirely sure if this is 100% correct?!
574          */
575         ba->control = ctx->control | cpu_to_le16(1);
576         ba->start_seq_num = ctx->start_seq_num;
577         wlan_tx_fw(&baf->s, NULL);
578 }
579
580 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
581 {
582         struct carl9170_bar_ctx *tmp;
583
584         tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx];
585         fw.wlan.ba_tail_idx++;
586         fw.wlan.ba_tail_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
587
588         return tmp;
589 }
590
591 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
592                        unsigned int len, unsigned int mac_err)
593 {
594         struct ieee80211_bar *bar;
595         struct carl9170_bar_ctx *ctx;
596
597         if (unlikely(mac_err)) {
598                 /*
599                  * This check does a number of things:
600                  * 1. checks if the frame is in good nick
601                  * 2. checks if the RA (MAC) matches
602                  */
603                 return ;
604         }
605
606         if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
607                 /*
608                  * Sneaky, corrupted BARs... but not with us!
609                  */
610
611                 return ;
612         }
613
614         bar = (void *) hdr;
615
616         if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
617             !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
618                 /* not implemented yet */
619
620                 return ;
621         }
622
623         ctx = wlan_get_bar_cache_buffer();
624
625         /* Brilliant! The BAR provides all necessary MACs! */
626         memcpy(ctx->ra, bar->ta, 6);
627         memcpy(ctx->ta, bar->ra, 6);
628
629         /*
630          * NB:
631          * not entirely sure if this is 100% correct to force the
632          * imm ack bit or not...
633          */
634         ctx->control = bar->control | cpu_to_le16(1);
635         ctx->start_seq_num = bar->start_seq_num;
636         ctx->phy = ar9170_rx_to_phy(desc);
637         if (unlikely(!ctx->phy)) {
638                 /* provide a backup, in case ar9170_rx_to_phy fails */
639                 ctx->phy = cpu_to_le32(0x2cc301);
640         }
641 }
642
643 static void wlan_check_rx_overrun(void)
644 {
645         uint32_t overruns, total;
646
647         fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
648         fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
649         if (unlikely(overruns)) {
650                 if (overruns == total) {
651                         DBG("RX Overrun");
652                         fw.wlan.mac_reset++;
653                 }
654
655                 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
656         }
657 }
658
659 #ifdef CONFIG_CARL9170FW_WOL
660 void wlan_prepare_wol(void)
661 {
662         /* set MAC filter */
663         memcpy((void *)AR9170_MAC_REG_MAC_ADDR_L, fw.wlan.wol.cmd.mac, 6);
664         memcpy((void *)AR9170_MAC_REG_BSSID_L, fw.wlan.wol.cmd.bssid, 6);
665         set(AR9170_MAC_REG_RX_CONTROL, AR9170_MAC_RX_CTRL_DEAGG);
666
667         /* set filter policy to: discard everything */
668         fw.wlan.rx_filter = CARL9170_RX_FILTER_EVERYTHING;
669
670         /* reenable rx dma */
671         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
672
673         /* initialize the last_beacon timer */
674         fw.wlan.wol.last_null = fw.wlan.wol.last_beacon = get_clock_counter();
675 }
676
677 #ifdef CONFIG_CARL9170FW_WOL_NL80211_TRIGGERS
678 static bool wlan_rx_wol_magic_packet(struct ieee80211_hdr *hdr, unsigned int len)
679 {
680         const unsigned char *data, *end, *mac;
681         unsigned int found = 0;
682
683         /*
684          * LIMITATION:
685          * We can only scan the first AR9170_BLOCK_SIZE [=~320] bytes
686          * for MAGIC patterns!
687          */
688
689         mac = (const unsigned char *) AR9170_MAC_REG_MAC_ADDR_L;
690
691         data = (u8 *)((unsigned long)hdr + ieee80211_hdrlen(hdr->frame_control));
692         end = (u8 *)((unsigned long)hdr + len);
693
694         /*
695          * scan for standard WOL Magic frame
696          *
697          * "A physical WakeOnLAN (Magic Packet) will look like this:
698          * ---------------------------------------------------------------
699          * | Synchronization Stream |  Target MAC |  Password (optional) |
700          * |    6 octets            |   96 octets |   0, 4 or 6          |
701          * ---------------------------------------------------------------
702          *
703          * The Synchronization Stream is defined as 6 bytes of FFh.
704          * The Target MAC block contains 16 duplications of the IEEEaddress
705          * of the target, with no breaks or interruptions.
706          *
707          * The Password field is optional, but if present, contains either
708          * 4 bytes or 6 bytes. The WakeOnLAN dissector was implemented to
709          * dissect the password, if present, according to the command-line
710          * format that ether-wake uses, therefore, if a 4-byte password is
711          * present, it will be dissected as an IPv4 address and if a 6-byte
712          * password is present, it will be dissected as an Ethernet address.
713          *
714          * <http://wiki.wireshark.org/WakeOnLAN>
715          */
716
717         while (data < end) {
718                 if (found >= 6) {
719                         if (*data == mac[found % 6])
720                                 found++;
721                         else
722                                 found = 0;
723                 }
724
725                 /* previous check might reset found counter */
726                 if (found < 6) {
727                         if (*data == 0xff)
728                                 found++;
729                         else
730                                 found = 0;
731                 }
732
733                 if (found == (6 + 16 * 6)) {
734                         return true;
735                 }
736
737                 data++;
738         }
739
740         return false;
741 }
742
743 static void wlan_wol_connect_callback(void __unused *dummy, bool success)
744 {
745         if (success)
746                 fw.wlan.wol.lost_null = 0;
747         else
748                 fw.wlan.wol.lost_null++;
749 }
750
751 static void wlan_wol_connection_monitor(void)
752 {
753         struct carl9170_tx_null_superframe *nullf = &dma_mem.reserved.cmd.null;
754         struct ieee80211_hdr *null = (struct ieee80211_hdr *) &nullf->f.null;
755
756         if (!fw.wlan.fw_desc_available)
757                 return;
758
759         memset(nullf, 0, sizeof(nullf));
760
761         nullf->s.len = sizeof(struct carl9170_tx_superdesc) +
762                      sizeof(struct ar9170_tx_hwdesc) +
763                      sizeof(struct ieee80211_hdr);
764         nullf->s.ri[0].tries = 3;
765         nullf->s.assign_seq = true;
766         nullf->s.queue = AR9170_TXQ_VO;
767         nullf->f.hdr.length = sizeof(struct ieee80211_hdr) + FCS_LEN;
768
769         nullf->f.hdr.mac.backoff = 1;
770         nullf->f.hdr.mac.hw_duration = 1;
771         nullf->f.hdr.mac.erp_prot = AR9170_TX_MAC_PROT_RTS;
772
773         nullf->f.hdr.phy.modulation = AR9170_TX_PHY_MOD_OFDM;
774         nullf->f.hdr.phy.bandwidth = AR9170_TX_PHY_BW_20MHZ;
775         nullf->f.hdr.phy.chains = AR9170_TX_PHY_TXCHAIN_2;
776         nullf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
777         nullf->f.hdr.phy.mcs = AR9170_TXRX_PHY_RATE_OFDM_6M;
778
779         /* format outgoing nullfunc */
780         null->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
781                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS);
782
783         memcpy(null->addr1, fw.wlan.wol.cmd.bssid, 6);
784         memcpy(null->addr2, fw.wlan.wol.cmd.mac, 6);
785         memcpy(null->addr3, fw.wlan.wol.cmd.bssid, 6);
786
787         wlan_tx_fw(&nullf->s, wlan_wol_connect_callback);
788 }
789
790 static bool wlan_rx_wol_disconnect(const unsigned int rx_filter,
791                                    struct ieee80211_hdr *hdr,
792                                    unsigned int __unused len)
793 {
794         const unsigned char *bssid;
795         bssid = (const unsigned char *) AR9170_MAC_REG_BSSID_L;
796
797         /* should catch both broadcast and unicast MLMEs */
798         if (!(rx_filter & CARL9170_RX_FILTER_OTHER_RA)) {
799                 if (ieee80211_is_deauth(hdr->frame_control) ||
800                     ieee80211_is_disassoc(hdr->frame_control))
801                         return true;
802         }
803
804         if (ieee80211_is_beacon(hdr->frame_control) &&
805             compare_ether_address(hdr->addr3, bssid)) {
806                 fw.wlan.wol.last_beacon = get_clock_counter();
807         }
808
809         return false;
810 }
811
812 #endif /* CARL9170FW_WOL_NL80211_TRIGGERS */
813
814 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
815
816 /*
817  * Note: CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID is not a real
818  * string. We have to be careful not to add a \0 at the end.
819  */
820 static const struct {
821         u8 ssid_ie;
822         u8 ssid_len;
823         u8 ssid[sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1];
824 } __packed probe_req = {
825         .ssid_ie = WLAN_EID_SSID,
826         .ssid_len = sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1,
827         .ssid = CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID,
828 };
829
830 static bool wlan_rx_wol_probe_ssid(struct ieee80211_hdr *hdr, unsigned int len)
831 {
832         const unsigned char *data, *end, *scan = (void *) &probe_req;
833
834         /*
835          * IEEE 802.11-2007 7.3.2.1 specifies that the SSID is no
836          * longer than 32 octets.
837          */
838         BUILD_BUG_ON((sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1) > 32);
839
840         if (ieee80211_is_probe_req(hdr->frame_control)) {
841                 unsigned int i;
842                 end = (u8 *)((unsigned long)hdr + len);
843
844                 /*
845                  * The position of the SSID information element inside
846                  * a probe request frame is more or less "fixed".
847                  */
848                 data = (u8 *)((struct ieee80211_mgmt *)hdr)->u.probe_req.variable;
849                 for (i = 0; i < (unsigned int)(probe_req.ssid_len + 1); i++) {
850                         if (scan[i] != data[i])
851                                 return false;
852                 }
853
854                 return true;
855         }
856
857         return false;
858 }
859 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
860
861 static void wlan_rx_wol(unsigned int rx_filter __unused, struct ieee80211_hdr *hdr __unused, unsigned int len __unused)
862 {
863 #ifdef CONFIG_CARL9170FW_WOL_NL80211_TRIGGERS
864         /* Disconnect is always enabled */
865         if (fw.wlan.wol.cmd.flags & CARL9170_WOL_DISCONNECT &&
866             rx_filter & CARL9170_RX_FILTER_MGMT)
867                 fw.wlan.wol.wake_up |= wlan_rx_wol_disconnect(rx_filter, hdr, len);
868
869         if (fw.wlan.wol.cmd.flags & CARL9170_WOL_MAGIC_PKT &&
870             rx_filter & CARL9170_RX_FILTER_DATA)
871                 fw.wlan.wol.wake_up |= wlan_rx_wol_magic_packet(hdr, len);
872 #endif /* CONFIG_CARL9170FW_WOL_NL80211_TRIGGERS */
873
874 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
875         if (rx_filter & CARL9170_RX_FILTER_MGMT)
876                 fw.wlan.wol.wake_up |= wlan_rx_wol_probe_ssid(hdr, len);
877 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
878 }
879
880 static void wlan_wol_janitor(void)
881 {
882         if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
883                 if (fw.wlan.wol.cmd.flags & CARL9170_WOL_DISCONNECT) {
884                         /*
885                          * connection lost after 10sec without receiving
886                          * a beacon
887                           */
888                         if (is_after_msecs(fw.wlan.wol.last_beacon, 10000))
889                                 fw.wlan.wol.wake_up |= true;
890
891                         if (fw.wlan.wol.cmd.null_interval &&
892                             is_after_msecs(fw.wlan.wol.last_null, fw.wlan.wol.cmd.null_interval))
893                                 wlan_wol_connection_monitor();
894
895                         if (fw.wlan.wol.lost_null >= 5)
896                                 fw.wlan.wol.wake_up |= true;
897                 }
898
899                 if (fw.wlan.wol.wake_up) {
900                         fw.suspend_mode = CARL9170_AWAKE_HOST;
901                         set(AR9170_USB_REG_WAKE_UP, AR9170_USB_WAKE_UP_WAKE);
902                 }
903         }
904 }
905 #endif /* CONFIG_CARL9170FW_WOL */
906
907 static unsigned int wlan_rx_filter(struct dma_desc *desc)
908 {
909         struct ieee80211_hdr *hdr;
910         unsigned int data_len;
911         unsigned int rx_filter;
912         unsigned int mac_err;
913
914         data_len = ar9170_get_rx_mpdu_len(desc);
915         mac_err = ar9170_get_rx_macstatus_error(desc);
916
917 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
918
919         if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
920             desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
921             mac_err & AR9170_RX_ERROR_BAD) {
922                 /*
923                  * This frame is too damaged to do anything
924                  * useful with it.
925                  */
926
927                 return CARL9170_RX_FILTER_BAD;
928         }
929
930         rx_filter = 0;
931         if (mac_err & AR9170_RX_ERROR_WRONG_RA)
932                 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
933
934         if (mac_err & AR9170_RX_ERROR_DECRYPT)
935                 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
936
937         hdr = ar9170_get_rx_i3e(desc);
938         if (likely(ieee80211_is_data(hdr->frame_control))) {
939                 rx_filter |= CARL9170_RX_FILTER_DATA;
940         } else if (ieee80211_is_ctl(hdr->frame_control)) {
941                 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
942                 case IEEE80211_STYPE_BACK_REQ:
943                         handle_bar(desc, hdr, data_len, mac_err);
944                         /* fallthrough */
945                         rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
946                         break;
947                 case IEEE80211_STYPE_PSPOLL:
948                         rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
949                         break;
950                 default:
951                         rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
952                         break;
953                 }
954         } else {
955                 /* ieee80211_is_mgmt */
956                 rx_filter |= CARL9170_RX_FILTER_MGMT;
957         }
958
959 #ifdef CONFIG_CARL9170FW_WOL
960         if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
961                 wlan_rx_wol(rx_filter, hdr, min(data_len,
962                                (unsigned int)AR9170_BLOCK_SIZE));
963         }
964 #endif /* CONFIG_CARL9170FW_WOL */
965
966 #undef AR9170_RX_ERROR_BAD
967
968         return rx_filter;
969 }
970
971 static void handle_rx(void)
972 {
973         struct dma_desc *desc;
974
975         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
976                 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
977                         dma_put(&fw.pta.up_queue, desc);
978                         up_trigger();
979                 } else {
980                         dma_reclaim(&fw.wlan.rx_queue, desc);
981                         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
982                 }
983         }
984 }
985
986 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
987 void wlan_cab_flush_queue(const unsigned int vif)
988 {
989         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
990         struct dma_desc *desc;
991
992         /* move queued frames into the main tx queues */
993         for_each_desc(desc, cab_queue) {
994                 struct carl9170_tx_superframe *super = get_super(desc);
995                 if (!queue_empty(cab_queue)) {
996                         /*
997                          * Set MOREDATA flag for all,
998                          * but the last queued frame.
999                          * see: 802.11-2007 11.2.1.5 f)
1000                          *
1001                          * This is actually the reason to why
1002                          * we need to prevent the reentry.
1003                          */
1004
1005                         super->f.data.i3e.frame_control |=
1006                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1007                 } else {
1008                         super->f.data.i3e.frame_control &=
1009                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1010                 }
1011
1012                 /* ready to roll! */
1013                 _wlan_tx(desc);
1014                 wlan_trigger(BIT(super->s.queue));
1015         }
1016 }
1017
1018 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
1019                                const unsigned int len)
1020 {
1021         struct ieee80211_mgmt *mgmt = addr;
1022         uint8_t *pos, *end;
1023
1024         pos = mgmt->u.beacon.variable;
1025         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
1026         while (pos < end) {
1027                 if (pos + 2 + pos[1] > end)
1028                         return NULL;
1029
1030                 if (pos[0] == ie)
1031                         return pos;
1032
1033                 pos += pos[1] + 2;
1034         }
1035
1036         return NULL;
1037 }
1038
1039 void wlan_modify_beacon(const unsigned int vif,
1040         const unsigned int addr, const unsigned int len)
1041 {
1042         uint8_t *_ie;
1043         struct ieee80211_tim_ie *ie;
1044
1045         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
1046         if (likely(_ie)) {
1047                 ie = (struct ieee80211_tim_ie *) &_ie[2];
1048
1049                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
1050                         /* schedule DTIM transfer */
1051                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
1052                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
1053                         /* undo all chances to the beacon structure */
1054                         ie->bitmap_ctrl &= ~0x1;
1055                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
1056                 }
1057
1058                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
1059                 if (fw.wlan.cab_flush_trigger[vif]) {
1060                         /* Set the almighty Multicast Traffic Indication Bit. */
1061                         ie->bitmap_ctrl |= 0x1;
1062                 }
1063         }
1064
1065         /*
1066          * Ideally, the sequence number should be assigned by the TX arbiter
1067          * hardware. But AFAIK that's not possible, so we have to go for the
1068          * next best thing and write it into the beacon fifo during the open
1069          * beacon update window.
1070          */
1071
1072         wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
1073 }
1074
1075 static void wlan_send_buffered_cab(void)
1076 {
1077         unsigned int i;
1078
1079         for (i = 0; i < CARL9170_INTF_NUM; i++) {
1080                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
1081                         /*
1082                          * This is hardcoded into carl9170usb driver.
1083                          *
1084                          * The driver must set the PRETBTT event to beacon_interval -
1085                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
1086                          *
1087                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
1088                          * 11.2.1.6. Let's hope the current solution is adequate enough.
1089                          */
1090
1091                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
1092                                 wlan_cab_flush_queue(i);
1093
1094                                 /*
1095                                  * This prevents the code from sending new BC/MC frames
1096                                  * which were queued after the previous buffered traffic
1097                                  * has been sent out... They will have to wait until the
1098                                  * next DTIM beacon comes along.
1099                                  */
1100                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
1101                         }
1102                 }
1103
1104         }
1105 }
1106 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1107
1108 static void handle_beacon_config(void)
1109 {
1110         uint32_t bcn_count;
1111
1112         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
1113         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
1114                          (uint8_t *) &bcn_count);
1115 }
1116
1117 static void handle_pretbtt(void)
1118 {
1119 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
1120         fw.wlan.cab_flush_time = get_clock_counter();
1121 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1122
1123         rf_psm();
1124
1125         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
1126                          (uint8_t *) &fw.phy.psm.state);
1127 }
1128
1129 static void handle_atim(void)
1130 {
1131         send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
1132 }
1133
1134 #ifdef CONFIG_CARL9170FW_DEBUG
1135 static void handle_qos(void)
1136 {
1137         /*
1138          * What is the QoS Bit used for?
1139          * Is it only an indicator for TXOP & Burst, or
1140          * should we do something here?
1141          */
1142 }
1143
1144 static void handle_radar(void)
1145 {
1146         send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
1147 }
1148 #endif /* CONFIG_CARL9170FW_DEBUG */
1149
1150 static void wlan_janitor(void)
1151 {
1152 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
1153         wlan_send_buffered_cab();
1154 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1155
1156         wlan_send_buffered_tx_status();
1157
1158         wlan_send_buffered_ba();
1159
1160 #ifdef CONFIG_CARL9170FW_WOL
1161         wlan_wol_janitor();
1162 #endif /* CONFIG_CARL9170FW_WOL */
1163 }
1164
1165 void handle_wlan(void)
1166 {
1167         uint32_t intr;
1168
1169         intr = get(AR9170_MAC_REG_INT_CTRL);
1170         /* ACK Interrupt */
1171         set(AR9170_MAC_REG_INT_CTRL, intr);
1172
1173 #define HANDLER(intr, flag, func)                       \
1174         do {                                            \
1175                 if ((intr & flag) != 0) {               \
1176                         func();                         \
1177                 }                                       \
1178         } while (0)
1179
1180         intr |= fw.wlan.soft_int;
1181         fw.wlan.soft_int = 0;
1182
1183         HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
1184
1185         HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
1186
1187         HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
1188
1189         HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
1190                 handle_tx_completion);
1191
1192 #ifdef CONFIG_CARL9170FW_DEBUG
1193         HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
1194
1195         HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
1196 #endif /* CONFIG_CARL9170FW_DEBUG */
1197
1198         HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
1199
1200         if (unlikely(intr))
1201                 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
1202
1203         wlan_janitor();
1204
1205 #undef HANDLER
1206 }
1207
1208 enum {
1209         CARL9170FW_TX_MAC_BUMP = 4,
1210         CARL9170FW_TX_MAC_DEBUG = 6,
1211         CARL9170FW_TX_MAC_RESET = 7,
1212 };
1213
1214 static void wlan_check_hang(void)
1215 {
1216         struct dma_desc *desc;
1217         int i;
1218
1219         for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
1220                 if (queue_empty(&fw.wlan.tx_queue[i])) {
1221                         /* Nothing to do here... move along */
1222                         continue;
1223                 }
1224
1225                 /* fetch the current DMA queue position */
1226                 desc = get_wlan_txq_addr(i);
1227
1228                 /* Stuck frame detection */
1229                 if (unlikely(DESC_PAYLOAD(desc) == fw.wlan.last_super[i])) {
1230                         fw.wlan.last_super_num[i]++;
1231
1232                         if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_RESET)) {
1233                                 /*
1234                                  * schedule MAC reset (aka OFF/ON => dead)
1235                                  *
1236                                  * This will almost certainly kill
1237                                  * the device for good, but it's the
1238                                  * recommended thing to do...
1239                                  */
1240
1241                                 fw.wlan.mac_reset++;
1242                         }
1243
1244 #ifdef CONFIG_CARL9170FW_DEBUG
1245                         if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_DEBUG)) {
1246                                 /*
1247                                  * Sigh, the queue is almost certainly
1248                                  * dead. Dump the queue content to the
1249                                  * user, maybe we find out why it got
1250                                  * so stuck.
1251                                  */
1252
1253                                 wlan_dump_queue(i);
1254                         }
1255 #endif /* CONFIG_CARL9170FW_DEBUG */
1256
1257 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
1258                         if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_BUMP)) {
1259                                 /*
1260                                  * Hrrm, bump the queue a bit.
1261                                  * maybe this will get it going again.
1262                                  */
1263
1264                                 wlan_dma_bump(i);
1265                                 wlan_trigger(BIT(i));
1266                         }
1267 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
1268                 } else {
1269                         /* Nothing stuck */
1270                         fw.wlan.last_super[i] = DESC_PAYLOAD(desc);
1271                         fw.wlan.last_super_num[i] = 0;
1272                 }
1273         }
1274 }
1275
1276 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
1277 /*
1278  * NB: Resetting the MAC is a two-edged sword.
1279  * On most occasions, it does what it is supposed to do.
1280  * But there is a chance that this will make it
1281  * even worse and the radio dies silently.
1282  */
1283 static void wlan_mac_reset(void)
1284 {
1285         uint32_t val;
1286         uint32_t agg_wait_counter;
1287         uint32_t agg_density;
1288         uint32_t bcn_start_addr;
1289         uint32_t rctl, rcth;
1290         uint32_t cam_mode;
1291         uint32_t ack_power;
1292         uint32_t rts_cts_tpc;
1293         uint32_t rts_cts_rate;
1294         int i;
1295
1296 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1297         uint32_t rx_BB;
1298 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1299
1300 #ifdef CONFIG_CARL9170FW_NOISY_MAC_RESET
1301         INFO("MAC RESET");
1302 #endif /* CONFIG_CARL9170FW_NOISY_MAC_RESET */
1303
1304         /* Save aggregation parameters */
1305         agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
1306         agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
1307
1308         bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
1309
1310         cam_mode = get(AR9170_MAC_REG_CAM_MODE);
1311         rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
1312         rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
1313
1314         ack_power = get(AR9170_MAC_REG_ACK_TPC);
1315         rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
1316         rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
1317
1318 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1319         /* 0x1c8960 write only */
1320         rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
1321 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1322
1323         /* TX/RX must be stopped by now */
1324         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
1325
1326         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
1327
1328         /*
1329          * Manipulate CCA threshold to stop transmission
1330          *
1331          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1332          */
1333
1334         /*
1335          * check Rx state in 0(idle) 9(disable)
1336          *
1337          * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1338          * while( (chState != 0) && (chState != 9)) {
1339          *      chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1340          * }
1341          */
1342
1343         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1344
1345         delay(2);
1346
1347         /* Restore aggregation parameters */
1348         set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1349         set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1350
1351         set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1352         set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1353         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1354         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1355
1356         set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1357         set(AR9170_MAC_REG_ACK_TPC, ack_power);
1358         set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1359
1360 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1361         set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1362 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1363
1364         /*
1365          * Manipulate CCA threshold to resume transmission
1366          *
1367          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1368          */
1369
1370         val = AR9170_DMA_TRIGGER_RXQ;
1371         /* Reinitialize all WLAN TX DMA queues. */
1372         for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
1373                 struct dma_desc *iter;
1374
1375                 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1376
1377                 /* kill the stuck frame */
1378                 if (!is_terminator(&fw.wlan.tx_queue[i], iter) &&
1379                     fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_RESET &&
1380                     fw.wlan.last_super[i] == DESC_PAYLOAD(iter)) {
1381                         struct carl9170_tx_superframe *super = get_super(iter);
1382
1383                         iter->status = AR9170_OWN_BITS_SW;
1384                         /*
1385                          * Mark the frame as failed.
1386                          * The BAFAIL flag allows the frame to sail through
1387                          * wlan_tx_status without much "unstuck" trouble.
1388                          */
1389                         iter->ctrl &= ~(AR9170_CTRL_FAIL);
1390                         iter->ctrl |= AR9170_CTRL_BAFAIL;
1391
1392                         super->s.cnt = CARL9170_TX_MAX_RATE_TRIES;
1393                         super->s.rix = CARL9170_TX_MAX_RETRY_RATES;
1394
1395                         fw.wlan.last_super_num[i] = 0;
1396                         fw.wlan.last_super[i] = NULL;
1397                         iter = iter->lastAddr->nextAddr;
1398                 }
1399
1400                 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1401                 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1402                         val |= BIT(i);
1403
1404                 DBG("Q:%d l:%d h:%p t:%p cu:%p it:%p ct:%x st:%x\n", i, queue_len(&fw.wlan.tx_queue[i]),
1405                      fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1406                      get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1407         }
1408
1409         fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1410                             AR9170_MAC_INT_RETRY_FAIL;
1411
1412         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1413         wlan_trigger(val);
1414 }
1415 #else
1416 static void wlan_mac_reset(void)
1417 {
1418         /* The driver takes care of reinitializing the device */
1419         BUG("MAC RESET");
1420 }
1421 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1422
1423 void __cold wlan_timer(void)
1424 {
1425         unsigned int cached_mac_reset;
1426
1427         cached_mac_reset = fw.wlan.mac_reset;
1428
1429         /* TX Queue Hang check */
1430         wlan_check_hang();
1431
1432         /* RX Overrun check */
1433         wlan_check_rx_overrun();
1434
1435         if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1436                 wlan_mac_reset();
1437                 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1438         } else {
1439                 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1440                         fw.wlan.mac_reset--;
1441         }
1442 }