carl9170 firmware: checkpatch/style fixes
[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         if (unlikely(super->s.ampdu_commit_density)) {
337                 set(AR9170_MAC_REG_AMPDU_DENSITY,
338                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
339                             get(AR9170_MAC_REG_AMPDU_DENSITY),
340                             super->s.ampdu_density));
341         }
342
343         if (unlikely(super->s.ampdu_commit_factor)) {
344                 set(AR9170_MAC_REG_AMPDU_FACTOR,
345                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
346                             get(AR9170_MAC_REG_AMPDU_FACTOR),
347                             8 << super->s.ampdu_factor));
348         }
349
350         __wlan_tx(desc);
351 }
352
353 /* propagate transmission status back to the driver */
354 static bool wlan_tx_status(struct dma_queue *queue,
355                            struct dma_desc *desc)
356 {
357         struct carl9170_tx_superframe *super = get_super(desc);
358         unsigned int qidx = super->s.queue;
359         bool txfail = false, success;
360
361         success = true;
362
363         /* update hangcheck */
364         fw.wlan.last_super_num[qidx] = 0;
365
366         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
367                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
368
369                 /* reset retry indicator flags */
370                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
371
372                 if (wlan_tx_consume_retry(super)) {
373                         /*
374                          * retry for simple and aggregated 802.11 frames.
375                          *
376                          * Note: We must not mess up the original frame
377                          * order.
378                          */
379
380                         if (!super->f.hdr.mac.ampdu) {
381                                 /*
382                                  * 802.11 - 7.1.3.1.5.
383                                  * set "Retry Field" for consecutive attempts
384                                  *
385                                  * Note: For AMPDU see:
386                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
387                                  */
388                                 super->f.data.i3e.frame_control |=
389                                         cpu_to_le16(IEEE80211_FCTL_RETRY);
390                         }
391
392                         if (txfail) {
393                                 /* Normal TX Failure */
394
395                                 /* demise descriptor ownership back to the hardware */
396                                 dma_rearm(desc);
397
398                                 /*
399                                  * And this will get the queue going again.
400                                  * To understand why: you have to get the HW
401                                  * specs... But sadly I never saw them.
402                                  */
403                                 wlan_txunstuck(qidx);
404
405                                 /* abort cycle - this is necessary due to HW design */
406                                 return false;
407                         } else {
408                                 /* (HT-) BlockACK failure */
409
410                                 /*
411                                  * Unlink the failed attempt and put it into
412                                  * the retry queue. The caller routine must
413                                  * be aware of this so the frames don't get lost.
414                                  */
415
416                                 dma_unlink_head(queue);
417                                 dma_put(&fw.wlan.tx_retry, desc);
418                                 return true;
419                         }
420                 } else {
421                         /* out of frame attempts - discard frame */
422                         success = false;
423                 }
424         }
425
426         dma_unlink_head(queue);
427         if (txfail) {
428                 /*
429                  * Issue the queue bump,
430                  * We need to do this in case this was the frame's last
431                  * possible retry attempt and it unfortunately: it failed.
432                  */
433
434                 wlan_txunstuck(qidx);
435         }
436
437         unhide_super(desc);
438
439         if (unlikely(super == fw.wlan.fw_desc_data)) {
440                 fw.wlan.fw_desc = desc;
441                 fw.wlan.fw_desc_available = 1;
442
443                 if (fw.wlan.fw_desc_callback)
444                         fw.wlan.fw_desc_callback(super, success);
445
446                 return true;
447         }
448
449 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
450         if (unlikely(super->s.cab))
451                 fw.wlan.cab_queue_len[super->s.vif_id]--;
452 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
453
454         wlan_tx_complete(super, success);
455
456         /* recycle freed descriptors */
457         dma_reclaim(&fw.pta.down_queue, desc);
458         down_trigger();
459         return true;
460 }
461
462 static void handle_tx_completion(void)
463 {
464         struct dma_desc *desc;
465         int i;
466
467         for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
468                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
469                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
470                                 /* termination requested. */
471                                 break;
472                         }
473                 }
474
475                 for_each_desc(desc, &fw.wlan.tx_retry)
476                         __wlan_tx(desc);
477
478                 wlan_tx_ampdu_end(i);
479                 if (!queue_empty(&fw.wlan.tx_queue[i]))
480                         wlan_trigger(BIT(i));
481         }
482 }
483
484 void __hot wlan_tx(struct dma_desc *desc)
485 {
486         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
487
488         /* initialize rate control struct */
489         super->s.rix = 0;
490         super->s.cnt = 1;
491         hide_super(desc);
492
493 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
494         if (unlikely(super->s.cab)) {
495                 fw.wlan.cab_queue_len[super->s.vif_id]++;
496                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
497                 return;
498         }
499 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
500
501         _wlan_tx(desc);
502         wlan_trigger(BIT(super->s.queue));
503 }
504
505 static void wlan_tx_fw(struct carl9170_tx_superdesc *super, fw_desc_callback_t cb)
506 {
507         if (!fw.wlan.fw_desc_available)
508                 return;
509
510         fw.wlan.fw_desc_available = 0;
511
512         /* Format BlockAck */
513         fw.wlan.fw_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
514         fw.wlan.fw_desc->status = AR9170_OWN_BITS_SW;
515
516         fw.wlan.fw_desc->totalLen = fw.wlan.fw_desc->dataSize = super->len;
517         fw.wlan.fw_desc_data = fw.wlan.fw_desc->dataAddr = super;
518         fw.wlan.fw_desc->nextAddr = fw.wlan.fw_desc->lastAddr =
519                 fw.wlan.fw_desc;
520         fw.wlan.fw_desc_callback = cb;
521         wlan_tx(fw.wlan.fw_desc);
522 }
523
524 static void wlan_send_buffered_ba(void)
525 {
526         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
527         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
528         struct carl9170_bar_ctx *ctx;
529
530         if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
531                 return;
532
533         /* there's no point to continue when the ba_desc is not available. */
534         if (!fw.wlan.fw_desc_available)
535                 return;
536
537         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx];
538         fw.wlan.ba_head_idx++;
539         fw.wlan.ba_head_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
540
541         baf->s.len = sizeof(struct carl9170_tx_superdesc) +
542                      sizeof(struct ar9170_tx_hwdesc) +
543                      sizeof(struct ieee80211_ba);
544         baf->s.ri[0].tries = 1;
545         baf->s.cookie = 0;
546         baf->s.queue = AR9170_TXQ_VO;
547         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
548
549         /* HW Duration / Backoff */
550         baf->f.hdr.mac.backoff = 1;
551         baf->f.hdr.mac.hw_duration = 1;
552
553         /* take the TX rate from the RX'd BAR */
554         baf->f.hdr.phy.set = ctx->phy;
555         baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
556
557         /* format outgoing BA */
558         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_NULLFUNC);
559         ba->duration = cpu_to_le16(0);
560         memcpy(ba->ta, ctx->ta, 6);
561         memcpy(ba->ra, ctx->ra, 6);
562
563         /*
564          * Unfortunately, we cannot look into the hardware's scoreboard.
565          * Therefore we have to proceed as described in 802.11n 9.10.7.5
566          * and send a null BlockAck.
567          */
568         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
569
570         /*
571          * NB:
572          * not entirely sure if this is 100% correct?!
573          */
574         ba->control = ctx->control | cpu_to_le16(1);
575         ba->start_seq_num = ctx->start_seq_num;
576         wlan_tx_fw(&baf->s, NULL);
577 }
578
579 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
580 {
581         struct carl9170_bar_ctx *tmp;
582
583         tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx];
584         fw.wlan.ba_tail_idx++;
585         fw.wlan.ba_tail_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
586
587         return tmp;
588 }
589
590 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
591                        unsigned int len, unsigned int mac_err)
592 {
593         struct ieee80211_bar *bar;
594         struct carl9170_bar_ctx *ctx;
595
596         if (unlikely(mac_err)) {
597                 /*
598                  * This check does a number of things:
599                  * 1. checks if the frame is in good nick
600                  * 2. checks if the RA (MAC) matches
601                  */
602                 return ;
603         }
604
605         if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
606                 /*
607                  * Sneaky, corrupted BARs... but not with us!
608                  */
609
610                 return ;
611         }
612
613         bar = (void *) hdr;
614
615         if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
616             !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
617                 /* not implemented yet */
618
619                 return ;
620         }
621
622         ctx = wlan_get_bar_cache_buffer();
623
624         /* Brilliant! The BAR provides all necessary MACs! */
625         memcpy(ctx->ra, bar->ta, 6);
626         memcpy(ctx->ta, bar->ra, 6);
627
628         /*
629          * NB:
630          * not entirely sure if this is 100% correct to force the
631          * imm ack bit or not...
632          */
633         ctx->control = bar->control | cpu_to_le16(1);
634         ctx->start_seq_num = bar->start_seq_num;
635         ctx->phy = ar9170_rx_to_phy(desc);
636         if (unlikely(!ctx->phy)) {
637                 /* provide a backup, in case ar9170_rx_to_phy fails */
638                 ctx->phy = cpu_to_le32(0x2cc301);
639         }
640 }
641
642 static void wlan_check_rx_overrun(void)
643 {
644         uint32_t overruns, total;
645
646         fw.tally.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
647         fw.tally.rx_overrun += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
648         if (unlikely(overruns)) {
649                 if (overruns == total) {
650                         DBG("RX Overrun");
651                         fw.wlan.mac_reset++;
652                 }
653
654                 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
655         }
656 }
657
658 #ifdef CONFIG_CARL9170FW_WOL
659 void wlan_prepare_wol(void)
660 {
661         /* set MAC filter */
662         memcpy((void *)AR9170_MAC_REG_MAC_ADDR_L, fw.wlan.wol.cmd.mac, 6);
663         memcpy((void *)AR9170_MAC_REG_BSSID_L, fw.wlan.wol.cmd.bssid, 6);
664         set(AR9170_MAC_REG_RX_CONTROL, AR9170_MAC_RX_CTRL_DEAGG);
665
666         /* set filter policy to: discard everything */
667         fw.wlan.rx_filter = CARL9170_RX_FILTER_EVERYTHING;
668
669         /* reenable rx dma */
670         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
671
672         /* initialize the last_beacon timer */
673         fw.wlan.wol.last_null = fw.wlan.wol.last_beacon = get_clock_counter();
674 }
675
676 #ifdef CONFIG_CARL9170FW_WOL_NL80211_TRIGGERS
677 static bool wlan_rx_wol_magic_packet(struct ieee80211_hdr *hdr, unsigned int len)
678 {
679         const unsigned char *data, *end, *mac;
680         unsigned int found = 0;
681
682         /*
683          * LIMITATION:
684          * We can only scan the first AR9170_BLOCK_SIZE [=~320] bytes
685          * for MAGIC patterns!
686          */
687
688         mac = (const unsigned char *) AR9170_MAC_REG_MAC_ADDR_L;
689
690         data = (u8 *)((unsigned long)hdr + ieee80211_hdrlen(hdr->frame_control));
691         end = (u8 *)((unsigned long)hdr + len);
692
693         /*
694          * scan for standard WOL Magic frame
695          *
696          * "A physical WakeOnLAN (Magic Packet) will look like this:
697          * ---------------------------------------------------------------
698          * | Synchronization Stream |  Target MAC |  Password (optional) |
699          * |    6 octets            |   96 octets |   0, 4 or 6          |
700          * ---------------------------------------------------------------
701          *
702          * The Synchronization Stream is defined as 6 bytes of FFh.
703          * The Target MAC block contains 16 duplications of the IEEEaddress
704          * of the target, with no breaks or interruptions.
705          *
706          * The Password field is optional, but if present, contains either
707          * 4 bytes or 6 bytes. The WakeOnLAN dissector was implemented to
708          * dissect the password, if present, according to the command-line
709          * format that ether-wake uses, therefore, if a 4-byte password is
710          * present, it will be dissected as an IPv4 address and if a 6-byte
711          * password is present, it will be dissected as an Ethernet address.
712          *
713          * <http://wiki.wireshark.org/WakeOnLAN>
714          */
715
716         while (data < end) {
717                 if (found >= 6) {
718                         if (*data == mac[found % 6])
719                                 found++;
720                         else
721                                 found = 0;
722                 }
723
724                 /* previous check might reset found counter */
725                 if (found < 6) {
726                         if (*data == 0xff)
727                                 found++;
728                         else
729                                 found = 0;
730                 }
731
732                 if (found == (6 + 16 * 6))
733                         return true;
734
735                 data++;
736         }
737
738         return false;
739 }
740
741 static void wlan_wol_connect_callback(void __unused *dummy, bool success)
742 {
743         if (success)
744                 fw.wlan.wol.lost_null = 0;
745         else
746                 fw.wlan.wol.lost_null++;
747 }
748
749 static void wlan_wol_connection_monitor(void)
750 {
751         struct carl9170_tx_null_superframe *nullf = &dma_mem.reserved.cmd.null;
752         struct ieee80211_hdr *null = (struct ieee80211_hdr *) &nullf->f.null;
753
754         if (!fw.wlan.fw_desc_available)
755                 return;
756
757         memset(nullf, 0, sizeof(*nullf));
758
759         nullf->s.len = sizeof(struct carl9170_tx_superdesc) +
760                      sizeof(struct ar9170_tx_hwdesc) +
761                      sizeof(struct ieee80211_hdr);
762         nullf->s.ri[0].tries = 3;
763         nullf->s.assign_seq = true;
764         nullf->s.queue = AR9170_TXQ_VO;
765         nullf->f.hdr.length = sizeof(struct ieee80211_hdr) + FCS_LEN;
766
767         nullf->f.hdr.mac.backoff = 1;
768         nullf->f.hdr.mac.hw_duration = 1;
769         nullf->f.hdr.mac.erp_prot = AR9170_TX_MAC_PROT_RTS;
770
771         nullf->f.hdr.phy.modulation = AR9170_TX_PHY_MOD_OFDM;
772         nullf->f.hdr.phy.bandwidth = AR9170_TX_PHY_BW_20MHZ;
773         nullf->f.hdr.phy.chains = AR9170_TX_PHY_TXCHAIN_2;
774         nullf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
775         nullf->f.hdr.phy.mcs = AR9170_TXRX_PHY_RATE_OFDM_6M;
776
777         /* format outgoing nullfunc */
778         null->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
779                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS);
780
781         memcpy(null->addr1, fw.wlan.wol.cmd.bssid, 6);
782         memcpy(null->addr2, fw.wlan.wol.cmd.mac, 6);
783         memcpy(null->addr3, fw.wlan.wol.cmd.bssid, 6);
784
785         wlan_tx_fw(&nullf->s, wlan_wol_connect_callback);
786 }
787
788 static bool wlan_rx_wol_disconnect(const unsigned int rx_filter,
789                                    struct ieee80211_hdr *hdr,
790                                    unsigned int __unused len)
791 {
792         const unsigned char *bssid;
793         bssid = (const unsigned char *) AR9170_MAC_REG_BSSID_L;
794
795         /* should catch both broadcast and unicast MLMEs */
796         if (!(rx_filter & CARL9170_RX_FILTER_OTHER_RA)) {
797                 if (ieee80211_is_deauth(hdr->frame_control) ||
798                     ieee80211_is_disassoc(hdr->frame_control))
799                         return true;
800         }
801
802         if (ieee80211_is_beacon(hdr->frame_control) &&
803             compare_ether_address(hdr->addr3, bssid)) {
804                 fw.wlan.wol.last_beacon = get_clock_counter();
805         }
806
807         return false;
808 }
809
810 #endif /* CARL9170FW_WOL_NL80211_TRIGGERS */
811
812 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
813
814 /*
815  * Note: CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID is not a real
816  * string. We have to be careful not to add a \0 at the end.
817  */
818 static const struct {
819         u8 ssid_ie;
820         u8 ssid_len;
821         u8 ssid[sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1];
822 } __packed probe_req = {
823         .ssid_ie = WLAN_EID_SSID,
824         .ssid_len = sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1,
825         .ssid = CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID,
826 };
827
828 static bool wlan_rx_wol_probe_ssid(struct ieee80211_hdr *hdr, unsigned int len)
829 {
830         const unsigned char *data, *end, *scan = (void *) &probe_req;
831
832         /*
833          * IEEE 802.11-2007 7.3.2.1 specifies that the SSID is no
834          * longer than 32 octets.
835          */
836         BUILD_BUG_ON((sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1) > 32);
837
838         if (ieee80211_is_probe_req(hdr->frame_control)) {
839                 unsigned int i;
840                 end = (u8 *)((unsigned long)hdr + len);
841
842                 /*
843                  * The position of the SSID information element inside
844                  * a probe request frame is more or less "fixed".
845                  */
846                 data = (u8 *)((struct ieee80211_mgmt *)hdr)->u.probe_req.variable;
847                 for (i = 0; i < (unsigned int)(probe_req.ssid_len + 1); i++) {
848                         if (scan[i] != data[i])
849                                 return false;
850                 }
851
852                 return true;
853         }
854
855         return false;
856 }
857 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
858
859 static void wlan_rx_wol(unsigned int rx_filter __unused, struct ieee80211_hdr *hdr __unused, unsigned int len __unused)
860 {
861 #ifdef CONFIG_CARL9170FW_WOL_NL80211_TRIGGERS
862         /* Disconnect is always enabled */
863         if (fw.wlan.wol.cmd.flags & CARL9170_WOL_DISCONNECT &&
864             rx_filter & CARL9170_RX_FILTER_MGMT)
865                 fw.wlan.wol.wake_up |= wlan_rx_wol_disconnect(rx_filter, hdr, len);
866
867         if (fw.wlan.wol.cmd.flags & CARL9170_WOL_MAGIC_PKT &&
868             rx_filter & CARL9170_RX_FILTER_DATA)
869                 fw.wlan.wol.wake_up |= wlan_rx_wol_magic_packet(hdr, len);
870 #endif /* CONFIG_CARL9170FW_WOL_NL80211_TRIGGERS */
871
872 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
873         if (rx_filter & CARL9170_RX_FILTER_MGMT)
874                 fw.wlan.wol.wake_up |= wlan_rx_wol_probe_ssid(hdr, len);
875 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
876 }
877
878 static void wlan_wol_janitor(void)
879 {
880         if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
881                 if (fw.wlan.wol.cmd.flags & CARL9170_WOL_DISCONNECT) {
882                         /*
883                          * connection lost after 10sec without receiving
884                          * a beacon
885                           */
886                         if (is_after_msecs(fw.wlan.wol.last_beacon, 10000))
887                                 fw.wlan.wol.wake_up |= true;
888
889                         if (fw.wlan.wol.cmd.null_interval &&
890                             is_after_msecs(fw.wlan.wol.last_null, fw.wlan.wol.cmd.null_interval))
891                                 wlan_wol_connection_monitor();
892
893                         if (fw.wlan.wol.lost_null >= 5)
894                                 fw.wlan.wol.wake_up |= true;
895                 }
896
897                 if (fw.wlan.wol.wake_up) {
898                         fw.suspend_mode = CARL9170_AWAKE_HOST;
899                         set(AR9170_USB_REG_WAKE_UP, AR9170_USB_WAKE_UP_WAKE);
900                 }
901         }
902 }
903 #endif /* CONFIG_CARL9170FW_WOL */
904
905 static unsigned int wlan_rx_filter(struct dma_desc *desc)
906 {
907         struct ieee80211_hdr *hdr;
908         unsigned int data_len;
909         unsigned int rx_filter;
910         unsigned int mac_err;
911
912         data_len = ar9170_get_rx_mpdu_len(desc);
913         mac_err = ar9170_get_rx_macstatus_error(desc);
914
915 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
916
917         if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
918             desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
919             mac_err & AR9170_RX_ERROR_BAD) {
920                 /*
921                  * This frame is too damaged to do anything
922                  * useful with it.
923                  */
924
925                 return CARL9170_RX_FILTER_BAD;
926         }
927
928         rx_filter = 0;
929         if (mac_err & AR9170_RX_ERROR_WRONG_RA)
930                 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
931
932         if (mac_err & AR9170_RX_ERROR_DECRYPT)
933                 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
934
935         hdr = ar9170_get_rx_i3e(desc);
936         if (likely(ieee80211_is_data(hdr->frame_control))) {
937                 rx_filter |= CARL9170_RX_FILTER_DATA;
938         } else if (ieee80211_is_ctl(hdr->frame_control)) {
939                 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
940                 case IEEE80211_STYPE_BACK_REQ:
941                         handle_bar(desc, hdr, data_len, mac_err);
942                         /* fallthrough */
943                         rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
944                         break;
945                 case IEEE80211_STYPE_PSPOLL:
946                         rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
947                         break;
948                 default:
949                         rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
950                         break;
951                 }
952         } else {
953                 /* ieee80211_is_mgmt */
954                 rx_filter |= CARL9170_RX_FILTER_MGMT;
955         }
956
957 #ifdef CONFIG_CARL9170FW_WOL
958         if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
959                 wlan_rx_wol(rx_filter, hdr, min(data_len,
960                                (unsigned int)AR9170_BLOCK_SIZE));
961         }
962 #endif /* CONFIG_CARL9170FW_WOL */
963
964 #undef AR9170_RX_ERROR_BAD
965
966         return rx_filter;
967 }
968
969 static void handle_rx(void)
970 {
971         struct dma_desc *desc;
972
973         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
974                 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
975                         dma_put(&fw.pta.up_queue, desc);
976                         up_trigger();
977                 } else {
978                         dma_reclaim(&fw.wlan.rx_queue, desc);
979                         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
980                 }
981         }
982 }
983
984 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
985 void wlan_cab_flush_queue(const unsigned int vif)
986 {
987         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
988         struct dma_desc *desc;
989
990         /* move queued frames into the main tx queues */
991         for_each_desc(desc, cab_queue) {
992                 struct carl9170_tx_superframe *super = get_super(desc);
993                 if (!queue_empty(cab_queue)) {
994                         /*
995                          * Set MOREDATA flag for all,
996                          * but the last queued frame.
997                          * see: 802.11-2007 11.2.1.5 f)
998                          *
999                          * This is actually the reason to why
1000                          * we need to prevent the reentry.
1001                          */
1002
1003                         super->f.data.i3e.frame_control |=
1004                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1005                 } else {
1006                         super->f.data.i3e.frame_control &=
1007                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1008                 }
1009
1010                 /* ready to roll! */
1011                 _wlan_tx(desc);
1012                 wlan_trigger(BIT(super->s.queue));
1013         }
1014 }
1015
1016 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
1017                                const unsigned int len)
1018 {
1019         struct ieee80211_mgmt *mgmt = addr;
1020         uint8_t *pos, *end;
1021
1022         pos = mgmt->u.beacon.variable;
1023         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
1024         while (pos < end) {
1025                 if (pos + 2 + pos[1] > end)
1026                         return NULL;
1027
1028                 if (pos[0] == ie)
1029                         return pos;
1030
1031                 pos += pos[1] + 2;
1032         }
1033
1034         return NULL;
1035 }
1036
1037 void wlan_modify_beacon(const unsigned int vif,
1038         const unsigned int addr, const unsigned int len)
1039 {
1040         uint8_t *_ie;
1041         struct ieee80211_tim_ie *ie;
1042
1043         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
1044         if (likely(_ie)) {
1045                 ie = (struct ieee80211_tim_ie *) &_ie[2];
1046
1047                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
1048                         /* schedule DTIM transfer */
1049                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
1050                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
1051                         /* undo all chances to the beacon structure */
1052                         ie->bitmap_ctrl &= ~0x1;
1053                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
1054                 }
1055
1056                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
1057                 if (fw.wlan.cab_flush_trigger[vif]) {
1058                         /* Set the almighty Multicast Traffic Indication Bit. */
1059                         ie->bitmap_ctrl |= 0x1;
1060                 }
1061         }
1062
1063         /*
1064          * Ideally, the sequence number should be assigned by the TX arbiter
1065          * hardware. But AFAIK that's not possible, so we have to go for the
1066          * next best thing and write it into the beacon fifo during the open
1067          * beacon update window.
1068          */
1069
1070         wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
1071 }
1072
1073 static void wlan_send_buffered_cab(void)
1074 {
1075         unsigned int i;
1076
1077         for (i = 0; i < CARL9170_INTF_NUM; i++) {
1078                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
1079                         /*
1080                          * This is hardcoded into carl9170usb driver.
1081                          *
1082                          * The driver must set the PRETBTT event to beacon_interval -
1083                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
1084                          *
1085                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
1086                          * 11.2.1.6. Let's hope the current solution is adequate enough.
1087                          */
1088
1089                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
1090                                 wlan_cab_flush_queue(i);
1091
1092                                 /*
1093                                  * This prevents the code from sending new BC/MC frames
1094                                  * which were queued after the previous buffered traffic
1095                                  * has been sent out... They will have to wait until the
1096                                  * next DTIM beacon comes along.
1097                                  */
1098                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
1099                         }
1100                 }
1101
1102         }
1103 }
1104 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1105
1106 static void handle_beacon_config(void)
1107 {
1108         uint32_t bcn_count;
1109
1110         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
1111         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
1112                          (uint8_t *) &bcn_count);
1113 }
1114
1115 static void handle_pretbtt(void)
1116 {
1117 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
1118         fw.wlan.cab_flush_time = get_clock_counter();
1119 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1120
1121 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1122         rf_psm();
1123
1124         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
1125                          (uint8_t *) &fw.phy.psm.state);
1126 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
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 = (struct dma_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 }