carl9170 firmware: integrate TX_AMPDU & DELAYED_TX
[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 static 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
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 # if ((defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_PSM))
311         BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
312 # endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_PSM */
313
314         /* insert desc into the right queue */
315         dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
316 #endif /* CONFIG_CARL9170FW_LOOPBACK */
317 }
318
319 static void wlan_assign_seq(struct ieee80211_hdr *hdr, unsigned int vif)
320 {
321         hdr->seq_ctrl &= cpu_to_le16(~IEEE80211_SCTL_SEQ);
322         hdr->seq_ctrl |= cpu_to_le16(fw.wlan.sequence[vif]);
323
324         if (!(hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG)))
325                 fw.wlan.sequence[vif] += 0x10;
326 }
327
328 /* prepares frame for the first transmission */
329 static void _wlan_tx(struct dma_desc *desc)
330 {
331         struct carl9170_tx_superframe *super = get_super(desc);
332
333         if (unlikely(super->s.assign_seq)) {
334                 wlan_assign_seq(&super->f.data.i3e, super->s.vif_id);
335         }
336
337         if (unlikely(super->s.ampdu_commit_density)) {
338                 set(AR9170_MAC_REG_AMPDU_DENSITY,
339                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
340                             get(AR9170_MAC_REG_AMPDU_DENSITY),
341                             super->s.ampdu_density));
342         }
343
344         if (unlikely(super->s.ampdu_commit_factor)) {
345                 set(AR9170_MAC_REG_AMPDU_FACTOR,
346                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
347                             get(AR9170_MAC_REG_AMPDU_FACTOR),
348                             8 << super->s.ampdu_factor));
349         }
350
351         __wlan_tx(desc);
352 }
353
354 /* propagate transmission status back to the driver */
355 static bool wlan_tx_status(struct dma_queue *queue,
356                            struct dma_desc *desc)
357 {
358         struct ar9170_tx_frame *frame = DESC_PAYLOAD(desc);
359         struct carl9170_tx_superframe *super = get_super(desc);
360         struct ieee80211_hdr *hdr = &super->f.data.i3e;
361         unsigned int qidx = super->s.queue;
362         bool txfail, success;
363
364         success = true;
365
366         /* update hangcheck */
367         fw.wlan.last_tx_desc_num[qidx] = 0;
368
369         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
370                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
371
372                 /* reset retry indicator flags */
373                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
374
375                 if (wlan_tx_consume_retry(super)) {
376                         /*
377                          * retry for simple and aggregated 802.11 frames.
378                          *
379                          * Note: We must not mess up the original frame
380                          * order.
381                          */
382
383                         if (!frame->hdr.mac.ampdu) {
384                                 /*
385                                  * 802.11 - 7.1.3.1.5.
386                                  * set "Retry Field" for consecutive attempts
387                                  *
388                                  * Note: For AMPDU see:
389                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
390                                  */
391
392                                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
393                         }
394
395                         if (txfail) {
396                                 /* Normal TX Failure */
397
398                                 /* demise descriptor ownership back to the hardware */
399                                 dma_rearm(desc);
400
401                                 /*
402                                  * And this will get the queue going again.
403                                  * To understand why: you have to get the HW
404                                  * specs... But sadly I never saw them.
405                                  */
406                                 wlan_txunstuck(qidx);
407
408                                 /* abort cycle - this is necessary due to HW design */
409                                 return false;
410                         } else {
411                                 /* (HT-) BlockACK failure */
412
413                                 /*
414                                  * Unlink the failed attempt and put it into
415                                  * the retry queue. The caller routine must
416                                  * be aware of this so the frames don't get lost.
417                                  */
418
419                                 dma_unlink_head(queue);
420                                 dma_put(&fw.wlan.tx_retry, desc);
421                                 return true;
422                         }
423                 } else {
424                         /* out of frame attempts - discard frame */
425                         success = false;
426                 }
427         }
428
429         dma_unlink_head(queue);
430         if (txfail) {
431                 /*
432                  * Issue the queue bump,
433                  * We need to do this in case this was the frame's last
434                  * possible retry attempt and it unfortunately: it failed.
435                  */
436
437                 wlan_txunstuck(qidx);
438         }
439
440         unhide_super(desc);
441
442 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
443         if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
444                 fw.wlan.ba_desc = desc;
445                 fw.wlan.ba_desc_available = 1;
446                 return true;
447         }
448 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
449
450         wlan_tx_complete(super, success);
451
452 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
453         if (unlikely(super->s.cab))
454                 fw.wlan.cab_queue_len[super->s.vif_id]--;
455 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
456
457         /* recycle freed descriptors */
458         dma_reclaim(&fw.pta.down_queue, desc);
459         down_trigger();
460         return true;
461 }
462
463 static void handle_tx_completion(void)
464 {
465         struct dma_desc *desc;
466         unsigned int map = 0;
467         int i;
468
469         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
470                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
471                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
472                                 /* termination requested. */
473                                 break;
474                         }
475                 }
476
477                 for_each_desc(desc, &fw.wlan.tx_retry)
478                         __wlan_tx(desc);
479
480                 wlan_tx_ampdu_end(i);
481                 if (!queue_empty(&fw.wlan.tx_queue[i]))
482                         map |= BIT(i);
483
484         }
485         wlan_trigger(map);
486 }
487
488 void __hot wlan_tx(struct dma_desc *desc)
489 {
490         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
491
492         /* initialize rate control struct */
493         super->s.rix = 0;
494         super->s.cnt = 1;
495         hide_super(desc);
496
497 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
498         if (unlikely(super->s.cab)) {
499                 fw.wlan.cab_queue_len[super->s.vif_id]++;
500                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
501                 return;
502         }
503 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
504
505         _wlan_tx(desc);
506         wlan_trigger(BIT(super->s.queue));
507 }
508
509 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
510 static void wlan_send_buffered_ba(void)
511 {
512         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
513         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
514         struct carl9170_bar_ctx *ctx;
515
516         if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
517                 return;
518
519         /* there's no point to continue when the ba_desc is not available. */
520         if (!fw.wlan.ba_desc_available)
521                 return;
522
523         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
524         fw.wlan.ba_head_idx++;
525
526         /* Format BlockAck */
527         fw.wlan.ba_desc->status = 0;
528         fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
529         fw.wlan.ba_desc_available = 0;
530         fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
531                 fw.wlan.ba_desc;
532
533         baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
534                 sizeof(struct carl9170_tx_superdesc) +
535                 sizeof(struct ar9170_tx_hwdesc) +
536                 sizeof(struct ieee80211_ba);
537
538         baf->s.ri[0].tries = 3;
539         baf->s.queue = 0;
540         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
541
542         /* HW Duration / Backoff */
543         baf->f.hdr.mac.backoff = 1;
544         baf->f.hdr.mac.hw_duration = 1;
545
546         /* take the TX rate from the RX'd BAR */
547         baf->f.hdr.phy.set = ctx->phy;
548         baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
549
550         /* format outgoing BA */
551         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
552         ba->duration = cpu_to_le16(0);
553         memcpy(ba->ta, ctx->ta, 6);
554         memcpy(ba->ra, ctx->ra, 6);
555
556         /*
557          * Unfortunately, we cannot look into the hardware's scoreboard.
558          * Therefore we have to proceed as described in 802.11n 9.10.7.5
559          * and send a null BlockAck.
560          */
561         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
562
563         /*
564          * NB:
565          * not entirely sure if this is 100% correct?!
566          */
567         ba->control = ctx->control | cpu_to_le16(1);
568         ba->start_seq_num = ctx->start_seq_num;
569
570         wlan_tx(fw.wlan.ba_desc);
571 }
572
573 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
574 {
575         struct carl9170_bar_ctx *tmp;
576
577         /* expire oldest entry, if we ran out of ba_ctx' */
578         if (fw.wlan.ba_head_idx + CONFIG_CARL9170FW_BACK_REQS_NUM < fw.wlan.ba_tail_idx)
579                 fw.wlan.ba_head_idx++;
580
581         tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
582         fw.wlan.ba_tail_idx++;
583
584         return tmp;
585 }
586
587 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
588                        unsigned int len, unsigned int mac_err)
589 {
590         struct ieee80211_bar *bar;
591         struct carl9170_bar_ctx *ctx;
592
593         if (unlikely(mac_err)) {
594                 /*
595                  * This check does a number of things:
596                  * 1. checks if the frame is in good nick
597                  * 2. checks if the RA (MAC) matches
598                  */
599                 return ;
600         }
601
602         if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
603                 /*
604                  * Sneaky, corrupted BARs... but not with us!
605                  */
606
607                 return ;
608         }
609
610         bar = (void *) hdr;
611
612         if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
613             !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
614                 /* not implemented yet */
615
616                 return ;
617         }
618
619         ctx = wlan_get_bar_cache_buffer();
620
621         /* Brilliant! The BAR provides all necessary MACs! */
622         memcpy(ctx->ra, bar->ta, 6);
623         memcpy(ctx->ta, bar->ra, 6);
624
625         /*
626          * NB:
627          * not entirely sure if this is 100% correct to force the
628          * imm ack bit or not...
629          */
630         ctx->control = bar->control | cpu_to_le16(1);
631         ctx->start_seq_num = bar->start_seq_num;
632         ctx->phy = ar9170_rx_to_phy(desc);
633         if (unlikely(!ctx->phy)) {
634                 /* provide a backup, in case ar9170_rx_to_phy fails */
635                 ctx->phy = cpu_to_le32(0x2cc301);
636         }
637 }
638 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
639
640 static void wlan_check_rx_overrun(void)
641 {
642         uint32_t overruns, total;
643
644         fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
645         fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
646         if (unlikely(overruns)) {
647                 if (overruns == total) {
648                         DBG("RX Overrun");
649                         fw.wlan.mac_reset++;
650                 }
651
652                 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
653         }
654 }
655
656 #ifdef CONFIG_CARL9170FW_WOL
657
658 #ifdef CONFIG_CARL9170FW_WOL_MAGIC_PACKET
659 static bool wlan_rx_wol_magic_packet(struct ieee80211_hdr *hdr, unsigned int len)
660 {
661         const unsigned char *data, *end, *mac;
662         unsigned int found = 0;
663
664         /*
665          * LIMITATION:
666          * We can only scan the first AR9170_BLOCK_SIZE [=~320] bytes
667          * for MAGIC patterns!
668          */
669
670         /*
671          * TODO:
672          * Currently, the MAGIC MAC Address is fixed to the EEPROM default.
673          * It's possible to make it fully configurable, e.g:
674          *
675          * mac = (const unsigned char *) AR9170_MAC_REG_MAC_ADDR_L;
676          * But this will clash with the driver's suspend path, because it
677          * needs to reset the registers.
678          */
679         mac = rom.sys.mac_address;
680
681         data = (u8 *)((unsigned long)hdr + ieee80211_hdrlen(hdr->frame_control));
682         end = (u8 *)((unsigned long)hdr + len);
683
684         /*
685          * scan for standard WOL Magic frame
686          *
687          * "A physical WakeOnLAN (Magic Packet) will look like this:
688          * ---------------------------------------------------------------
689          * | Synchronization Stream |  Target MAC |  Password (optional) |
690          * |    6 octets            |   96 octets |   0, 4 or 6          |
691          * ---------------------------------------------------------------
692          *
693          * The Synchronization Stream is defined as 6 bytes of FFh.
694          * The Target MAC block contains 16 duplications of the IEEEaddress
695          * of the target, with no breaks or interruptions.
696          *
697          * The Password field is optional, but if present, contains either
698          * 4 bytes or 6 bytes. The WakeOnLAN dissector was implemented to
699          * dissect the password, if present, according to the command-line
700          * format that ether-wake uses, therefore, if a 4-byte password is
701          * present, it will be dissected as an IPv4 address and if a 6-byte
702          * password is present, it will be dissected as an Ethernet address.
703          *
704          * <http://wiki.wireshark.org/WakeOnLAN>
705          */
706
707         while (data < end) {
708                 if (found >= 6) {
709                         if (*data == mac[found % 6])
710                                 found++;
711                         else
712                                 found = 0;
713                 }
714
715                 /* previous check might reset found counter */
716                 if (found < 6) {
717                         if (*data == 0xff)
718                                 found++;
719                         else
720                                 found = 0;
721                 }
722
723                 if (found == (6 + 16 * 6)) {
724                         return true;
725                 }
726
727                 data++;
728         }
729
730         return false;
731 }
732 #endif /* CONFIG_CARL9170FW_WOL_MAGIC_PACKET */
733
734 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
735
736 /*
737  * Note: CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID is not a real
738  * string. We have to be careful not to add a \0 at the end.
739  */
740 static const struct {
741         u8 ssid_ie;
742         u8 ssid_len;
743         u8 ssid[sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1];
744 } __packed probe_req = {
745         .ssid_ie = WLAN_EID_SSID,
746         .ssid_len = sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1,
747         .ssid = CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID,
748 };
749
750 static bool wlan_rx_wol_probe_ssid(struct ieee80211_hdr *hdr, unsigned int len)
751 {
752         const unsigned char *data, *end, *scan = (void *) &probe_req;
753
754         /*
755          * IEEE 802.11-2007 7.3.2.1 specifies that the SSID is no
756          * longer than 32 octets.
757          */
758         BUILD_BUG_ON((sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1) > 32);
759
760         if (ieee80211_is_probe_req(hdr->frame_control)) {
761                 unsigned int i;
762                 end = (u8 *)((unsigned long)hdr + len);
763
764                 /*
765                  * The position of the SSID information element inside
766                  * a probe request frame is more or less "fixed".
767                  */
768                 data = (u8 *)((struct ieee80211_mgmt *)hdr)->u.probe_req.variable;
769                 for (i = 0; i < (unsigned int)(probe_req.ssid_len + 1); i++) {
770                         if (scan[i] != data[i])
771                                 return false;
772                 }
773
774                 return true;
775         }
776
777         return false;
778 }
779 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
780
781 static void wlan_rx_wol(unsigned int rx_filter __unused, struct ieee80211_hdr *hdr __unused, unsigned int len __unused)
782 {
783         bool __unused wake_up = false;
784
785 #ifdef CONFIG_CARL9170FW_WOL_MAGIC_PACKET
786         if (rx_filter & CARL9170_RX_FILTER_DATA)
787                 wake_up |= wlan_rx_wol_magic_packet(hdr, len);
788 #endif /* CONFIG_CARL9170FW_WOL_MAGIC_PACKET */
789
790 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
791         if (rx_filter & CARL9170_RX_FILTER_MGMT)
792                 wake_up |= wlan_rx_wol_probe_ssid(hdr, len);
793 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
794
795         if (wake_up) {
796                 fw.suspend_mode = CARL9170_AWAKE_HOST;
797                 set(AR9170_USB_REG_WAKE_UP, AR9170_USB_WAKE_UP_WAKE);
798         }
799 }
800 #endif /* CONFIG_CARL9170FW_WOL */
801
802 static unsigned int wlan_rx_filter(struct dma_desc *desc)
803 {
804         struct ieee80211_hdr *hdr;
805         unsigned int data_len;
806         unsigned int rx_filter;
807         unsigned int mac_err;
808
809         data_len = ar9170_get_rx_mpdu_len(desc);
810         mac_err = ar9170_get_rx_macstatus_error(desc);
811
812 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
813
814         if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
815             desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
816             mac_err & AR9170_RX_ERROR_BAD) {
817                 /*
818                  * This frame is too damaged to do anything
819                  * useful with it.
820                  */
821
822                 return CARL9170_RX_FILTER_BAD;
823         }
824
825         rx_filter = 0;
826         if (mac_err & AR9170_RX_ERROR_WRONG_RA)
827                 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
828
829         if (mac_err & AR9170_RX_ERROR_DECRYPT)
830                 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
831
832         hdr = ar9170_get_rx_i3e(desc);
833         if (likely(ieee80211_is_data(hdr->frame_control))) {
834                 rx_filter |= CARL9170_RX_FILTER_DATA;
835         } else if (ieee80211_is_ctl(hdr->frame_control)) {
836                 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
837                 case IEEE80211_STYPE_BACK_REQ:
838 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
839                         handle_bar(desc, hdr, data_len, mac_err);
840 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
841                         /* fallthrough */
842                         rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
843                         break;
844                 case IEEE80211_STYPE_PSPOLL:
845                         rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
846                         break;
847                 default:
848                         rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
849                         break;
850                 }
851         } else {
852                 /* ieee80211_is_mgmt */
853                 rx_filter |= CARL9170_RX_FILTER_MGMT;
854         }
855
856 #ifdef CONFIG_CARL9170FW_WOL
857         if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
858                 wlan_rx_wol(rx_filter, hdr, min(data_len,
859                             (unsigned int)AR9170_BLOCK_SIZE));
860         }
861 #endif /* CONFIG_CARL9170FW_WOL */
862
863 #undef AR9170_RX_ERROR_BAD
864
865         return rx_filter;
866 }
867
868 static void handle_rx(void)
869 {
870         struct dma_desc *desc;
871
872         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
873                 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
874                         dma_put(&fw.pta.up_queue, desc);
875                         up_trigger();
876                 } else {
877                         dma_reclaim(&fw.wlan.rx_queue, desc);
878                         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
879                 }
880         }
881 }
882
883 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
884 void wlan_cab_flush_queue(const unsigned int vif)
885 {
886         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
887         struct dma_desc *desc;
888
889         /* move queued frames into the main tx queues */
890         for_each_desc(desc, cab_queue) {
891                 struct carl9170_tx_superframe *super = get_super(desc);
892                 if (!queue_empty(cab_queue)) {
893                         /*
894                          * Set MOREDATA flag for all,
895                          * but the last queued frame.
896                          * see: 802.11-2007 11.2.1.5 f)
897                          *
898                          * This is actually the reason to why
899                          * we need to prevent the reentry.
900                          */
901
902                         super->f.data.i3e.frame_control |=
903                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
904                 } else {
905                         super->f.data.i3e.frame_control &=
906                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
907                 }
908
909                 /* ready to roll! */
910                 _wlan_tx(desc);
911                 wlan_trigger(BIT(super->s.queue));
912         }
913 }
914
915 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
916                                const unsigned int len)
917 {
918         struct ieee80211_mgmt *mgmt = addr;
919         uint8_t *pos, *end;
920
921         pos = mgmt->u.beacon.variable;
922         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
923         while (pos < end) {
924                 if (pos + 2 + pos[1] > end)
925                         return NULL;
926
927                 if (pos[0] == ie)
928                         return pos;
929
930                 pos += pos[1] + 2;
931         }
932
933         return NULL;
934 }
935
936 void wlan_modify_beacon(const unsigned int vif,
937         const unsigned int addr, const unsigned int len)
938 {
939         uint8_t *_ie;
940         struct ieee80211_tim_ie *ie;
941
942         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
943         if (likely(_ie)) {
944                 ie = (struct ieee80211_tim_ie *) &_ie[2];
945
946                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
947                         /* schedule DTIM transfer */
948                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
949                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
950                         /* undo all chances to the beacon structure */
951                         ie->bitmap_ctrl &= ~0x1;
952                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
953                 }
954
955                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
956                 if (fw.wlan.cab_flush_trigger[vif]) {
957                         /* Set the almighty Multicast Traffic Indication Bit. */
958                         ie->bitmap_ctrl |= 0x1;
959                 }
960         }
961
962         /*
963          * Ideally, the sequence number should be assigned by the TX arbiter
964          * hardware. But AFAIK that's not possible, so we have to go for the
965          * next best thing and write it into the beacon fifo during the open
966          * beacon update window.
967          */
968
969         wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
970 }
971 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
972
973 static void handle_beacon_config(void)
974 {
975         uint32_t bcn_count;
976
977         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
978         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
979                          (uint8_t *) &bcn_count);
980 }
981
982 static void handle_pretbtt(void)
983 {
984 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
985         fw.wlan.cab_flush_time = get_clock_counter();
986 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
987
988 #ifdef CONFIG_CARL9170FW_PSM
989         rf_psm();
990
991         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
992                          (uint8_t *) &fw.phy.psm.state);
993 #else
994         send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
995 #endif /* CONFIG_CARL9170FW_PSM */
996 }
997
998 static void handle_atim(void)
999 {
1000         send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
1001 }
1002
1003 #ifdef CONFIG_CARL9170FW_DEBUG
1004 static void handle_qos(void)
1005 {
1006         /*
1007          * What is the QoS Bit used for?
1008          * Is it only an indicator for TXOP & Burst, or
1009          * should we do something here?
1010          */
1011 }
1012
1013 static void handle_radar(void)
1014 {
1015         send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
1016 }
1017 #endif /* CONFIG_CARL9170FW_DEBUG */
1018
1019 static void wlan_janitor(void)
1020 {
1021 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
1022         unsigned int i;
1023
1024         for (i = 0; i < CARL9170_INTF_NUM; i++) {
1025                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
1026                         /*
1027                          * This is hardcoded into carl9170usb driver.
1028                          *
1029                          * The driver must set the PRETBTT event to beacon_interval -
1030                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
1031                          *
1032                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
1033                          * 11.2.1.6. Let's hope the current solution is adequate enough.
1034                          */
1035
1036                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
1037                                 wlan_cab_flush_queue(i);
1038
1039                                 /*
1040                                  * This prevents the code from sending new BC/MC frames
1041                                  * which were queued after the previous buffered traffic
1042                                  * has been sent out... They will have to wait until the
1043                                  * next DTIM beacon comes along.
1044                                  */
1045                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
1046                         }
1047                 }
1048
1049         }
1050 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1051
1052         wlan_send_buffered_tx_status();
1053
1054 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
1055         wlan_send_buffered_ba();
1056 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
1057 }
1058
1059 void handle_wlan(void)
1060 {
1061         uint32_t intr;
1062
1063         intr = get(AR9170_MAC_REG_INT_CTRL);
1064         /* ACK Interrupt */
1065         set(AR9170_MAC_REG_INT_CTRL, intr);
1066
1067 #define HANDLER(intr, flag, func)                       \
1068         do {                                            \
1069                 if ((intr & flag) != 0) {               \
1070                         func();                         \
1071                 }                                       \
1072         } while (0)
1073
1074         intr |= fw.wlan.soft_int;
1075         fw.wlan.soft_int = 0;
1076
1077         HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
1078
1079         HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
1080
1081         HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
1082
1083         HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
1084                 handle_tx_completion);
1085
1086 #ifdef CONFIG_CARL9170FW_DEBUG
1087         HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
1088
1089         HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
1090 #endif /* CONFIG_CARL9170FW_DEBUG */
1091
1092         HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
1093
1094         if (unlikely(intr))
1095                 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
1096
1097         wlan_janitor();
1098
1099 #undef HANDLER
1100 }
1101
1102 static void wlan_check_hang(void)
1103 {
1104         struct dma_desc *desc;
1105         unsigned int i;
1106
1107         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1108                 if (queue_empty(&fw.wlan.tx_queue[i])) {
1109                         /* Nothing to do here... move along */
1110                         continue;
1111                 }
1112
1113                 /* fetch the current DMA queue position */
1114                 desc = get_wlan_txq_addr(i);
1115
1116                 /* Stuck frame detection */
1117                 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
1118                         fw.wlan.last_tx_desc_num[i]++;
1119
1120                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
1121                                 /*
1122                                  * schedule MAC reset (aka OFF/ON => dead)
1123                                  *
1124                                  * This will almost certainly kill
1125                                  * the device for good, but it's the
1126                                  * recommended thing to do...
1127                                  */
1128
1129                                 fw.wlan.mac_reset++;
1130                         }
1131
1132 #ifdef CONFIG_CARL9170FW_DEBUG
1133                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
1134                                 /*
1135                                  * Sigh, the queue is almost certainly
1136                                  * dead. Dump the queue content to the
1137                                  * user, maybe we find out why it got
1138                                  * so stuck.
1139                                  */
1140
1141                                 wlan_dump_queue(i);
1142                         }
1143 #endif /* CONFIG_CARL9170FW_DEBUG */
1144
1145 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
1146                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
1147                                 /*
1148                                  * Hrrm, bump the queue a bit.
1149                                  * maybe this will get it going again.
1150                                  */
1151
1152                                 wlan_dma_bump(i);
1153                                 wlan_trigger(BIT(i));
1154                         }
1155 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
1156                 } else {
1157                         /* Nothing stuck */
1158                         fw.wlan.last_tx_desc[i] = desc;
1159                         fw.wlan.last_tx_desc_num[i] = 0;
1160                 }
1161         }
1162 }
1163
1164 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
1165 /*
1166  * NB: Resetting the MAC is a two-edged sword.
1167  * On most occasions, it does what it is supposed to do.
1168  * But there is a chance that this will make it
1169  * even worse and the radio dies silently.
1170  */
1171 static void wlan_mac_reset(void)
1172 {
1173         uint32_t val;
1174         uint32_t agg_wait_counter;
1175         uint32_t agg_density;
1176         uint32_t bcn_start_addr;
1177         uint32_t rctl, rcth;
1178         uint32_t cam_mode;
1179         uint32_t ack_power;
1180         uint32_t rts_cts_tpc;
1181         uint32_t rts_cts_rate;
1182         unsigned int i;
1183
1184 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1185         uint32_t rx_BB;
1186 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1187
1188         INFO("MAC RESET");
1189
1190         /* Save aggregation parameters */
1191         agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
1192         agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
1193
1194         bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
1195
1196         cam_mode = get(AR9170_MAC_REG_CAM_MODE);
1197         rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
1198         rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
1199
1200         ack_power = get(AR9170_MAC_REG_ACK_TPC);
1201         rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
1202         rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
1203
1204 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1205         /* 0x1c8960 write only */
1206         rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
1207 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1208
1209         /* TX/RX must be stopped by now */
1210         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
1211
1212         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
1213
1214         /*
1215          * Manipulate CCA threshold to stop transmission
1216          *
1217          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1218          */
1219
1220         /*
1221          * check Rx state in 0(idle) 9(disable)
1222          *
1223          * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1224          * while( (chState != 0) && (chState != 9)) {
1225          *      chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1226          * }
1227          */
1228
1229         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1230
1231         delay(2);
1232
1233         /* Restore aggregation parameters */
1234         set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1235         set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1236
1237         set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1238         set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1239         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1240         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1241
1242         set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1243         set(AR9170_MAC_REG_ACK_TPC, ack_power);
1244         set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1245
1246 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1247         set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1248 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1249
1250         /*
1251          * Manipulate CCA threshold to resume transmission
1252          *
1253          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1254          */
1255
1256         val = AR9170_DMA_TRIGGER_RXQ;
1257         /* Reinitialize all WLAN TX DMA queues. */
1258         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1259                 struct dma_desc *iter;
1260
1261                 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1262
1263                 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1264                 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1265                         val |= BIT(i);
1266
1267                 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]),
1268                      fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1269                      get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1270         }
1271
1272         fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1273                             AR9170_MAC_INT_RETRY_FAIL;
1274
1275         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1276         wlan_trigger(val);
1277 }
1278 #else
1279 static void wlan_mac_reset(void)
1280 {
1281         /* The driver takes care of reinitializing the device */
1282         BUG("MAC RESET");
1283 }
1284 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1285
1286 void __cold wlan_timer(void)
1287 {
1288         unsigned int cached_mac_reset;
1289
1290         cached_mac_reset = fw.wlan.mac_reset;
1291
1292         /* TX Queue Hang check */
1293         wlan_check_hang();
1294
1295         /* RX Overrun check */
1296         wlan_check_rx_overrun();
1297
1298         if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1299                 wlan_mac_reset();
1300                 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1301         } else {
1302                 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1303                         fw.wlan.mac_reset--;
1304         }
1305 }