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