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