61976b05d6b3311ca96d905907f55eedbc52406e
[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, 2010 Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "carl9170.h"
27 #include "shared/phy.h"
28 #include "hostif.h"
29 #include "timer.h"
30 #include "wl.h"
31 #include "printf.h"
32 #include "rf.h"
33 #include "linux/ieee80211.h"
34
35 static void wlan_txunstuck(unsigned int queue)
36 {
37         set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head) | 1);
38         wlan_trigger(BIT(queue));
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         wlan_trigger(BIT(queue));
46 }
47
48 static void wlan_dma_bump(unsigned int qidx)
49 {
50         unsigned int offset = qidx;
51         uint32_t status, trigger;
52
53         status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
54         trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
55
56         while (offset != 0) {
57                 status >>= 4;
58                 trigger >>= 4;
59                 offset--;
60         }
61
62         status &= 0xf;
63         trigger &= 0xf;
64
65         if ((trigger == 0xa) && (status == 0x8)) {
66                 DBG("UNSTUCK");
67                 wlan_txunstuck(qidx);
68         } else {
69                 DBG("UPDATE");
70                 wlan_txupdate(qidx);
71         }
72 }
73 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
74
75 #ifdef CONFIG_CARL9170FW_DEBUG
76 static void wlan_dump_queue(unsigned int qidx)
77 {
78
79         struct dma_desc *desc;
80         struct carl9170_tx_superframe *super;
81         int entries = 0;
82
83         __for_each_desc(desc, &fw.wlan.tx_queue[qidx]) {
84                 super = get_super(desc);
85                 DBG("%d: %p s:%x c:%x tl:%x ds:%x n:%p l:%p ", entries, desc,
86                     desc->status, desc->ctrl, desc->totalLen,
87                     desc->dataSize, desc->nextAddr, desc->lastAddr);
88
89                 DBG("c:%x tr:%d ri:%d l:%x m:%x p:%x fc:%x",
90                     super->s.cookie, super->s.cnt, super->s.rix,
91                     super->f.hdr.length, super->f.hdr.mac.set,
92                     (unsigned int) le32_to_cpu(super->f.hdr.phy.set),
93                     super->f.data.i3e.frame_control);
94
95                 entries++;
96         }
97
98         desc = get_wlan_txq_addr(qidx);
99
100         DBG("Queue: %d: te:%d td:%d h:%p c:%p t:%p",
101             qidx, entries, queue_len(&fw.wlan.tx_queue[qidx]),
102             fw.wlan.tx_queue[qidx].head,
103             desc, fw.wlan.tx_queue[qidx].terminator);
104
105         DBG("HW: t:%x s:%x ac:%x c:%x",
106             (unsigned int) get(AR9170_MAC_REG_DMA_TRIGGER),
107             (unsigned int) get(AR9170_MAC_REG_DMA_STATUS),
108             (unsigned int) get(AR9170_MAC_REG_AMPDU_COUNT),
109             (unsigned int) get(AR9170_MAC_REG_DMA_TXQX_ADDR_CURR));
110 }
111 #endif /* CONFIG_CARL9170FW_DEBUG */
112
113 static void wlan_send_buffered_tx_status(void)
114 {
115         unsigned int len;
116
117         while (fw.wlan.tx_status_pending) {
118                 len = min((unsigned int)fw.wlan.tx_status_pending,
119                           CARL9170_RSP_TX_STATUS_NUM);
120                 len = min(len, CARL9170_TX_STATUS_NUM - fw.wlan.tx_status_head_idx);
121
122                 /*
123                  * rather than memcpy each individual request into a large buffer,
124                  * we _splice_ them all together.
125                  *
126                  * The only downside is however that we have to be careful around
127                  * the edges of the tx_status_cache.
128                  *
129                  * Note:
130                  * Each tx_status is about 2 bytes. However every command package
131                  * must have a size which is a multiple of 4.
132                  */
133
134                 send_cmd_to_host((len * sizeof(struct carl9170_tx_status) + 3) & ~3,
135                                  CARL9170_RSP_TXCOMP, len, (void *)
136                                  &fw.wlan.tx_status_cache[fw.wlan.tx_status_head_idx]);
137
138                 fw.wlan.tx_status_pending -= len;
139                 fw.wlan.tx_status_head_idx += len;
140                 fw.wlan.tx_status_head_idx %= CARL9170_TX_STATUS_NUM;
141         }
142 }
143
144 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
145 {
146         struct carl9170_tx_status *tmp;
147
148         tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
149         fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
150
151         if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
152                 wlan_send_buffered_tx_status();
153
154         fw.wlan.tx_status_pending++;
155
156         return tmp;
157 }
158
159 /* generate _aggregated_ tx_status for the host */
160 static void wlan_tx_complete(struct carl9170_tx_superframe *super,
161                              bool txs)
162 {
163         struct carl9170_tx_status *status;
164
165         status = wlan_get_tx_status_buffer();
166
167         /*
168          * The *unique* cookie and AC_ID is used by the driver for
169          * frame lookup.
170          */
171         status->cookie = super->s.cookie;
172         status->queue = super->s.queue;
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 /* for all tries */
216 static void __wlan_tx(struct dma_desc *desc)
217 {
218         struct carl9170_tx_superframe *super = get_super(desc);
219 #ifdef CONFIG_CARL9170FW_NORMAL_TX_RX
220         unsigned int queue = super->s.queue;
221 #endif /* CONFIG_CARL9170FW_LOOPBACK */
222
223         if (unlikely(super->s.fill_in_tsf)) {
224                 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
225                 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
226
227                 /*
228                  * Truth be told: this is a hack.
229                  *
230                  * The *real* TSF is definitely going to be higher/older.
231                  * But this hardware emulation code is head and shoulders
232                  * above anything a driver can possibly do.
233                  *
234                  * (even, if it's got an accurate atomic clock source).
235                  */
236
237                 read_tsf(tsf);
238         }
239
240 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
241         wlan_tx_complete(super, true);
242         unhide_super(desc);
243 # ifdef CONFIG_CARL9170FW_LOOPBACK
244         dma_put(&fw.pta.up_queue, desc);
245         up_trigger();
246 # elif CONFIG_CARL9170FW_DISCARD
247         dma_reclaim(&fw.pta.down_queue, desc);
248         down_trigger();
249 # endif
250 #else /* CONFIG_CARL9170FW_LOOPBACK */
251
252 # if ((defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_PSM))
253         BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
254 # endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_PSM */
255
256         /* insert desc into the right queue */
257         dma_put(&fw.wlan.tx_queue[queue], desc);
258         wlan_trigger(BIT(queue));
259 #endif /* CONFIG_CARL9170FW_LOOPBACK */
260 }
261
262 /* prepares frame for the first transmission */
263 static void _wlan_tx(struct dma_desc *desc)
264 {
265         struct carl9170_tx_superframe *super = get_super(desc);
266
267         if (unlikely(super->s.ampdu_commit_density)) {
268                 set(AR9170_MAC_REG_AMPDU_DENSITY,
269                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
270                             get(AR9170_MAC_REG_AMPDU_DENSITY),
271                             super->s.ampdu_density));
272         }
273
274         if (unlikely(super->s.ampdu_commit_factor)) {
275                 set(AR9170_MAC_REG_AMPDU_FACTOR,
276                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
277                             get(AR9170_MAC_REG_AMPDU_FACTOR),
278                             8 << super->s.ampdu_factor));
279         }
280
281         __wlan_tx(desc);
282 }
283
284 /* propagate transmission status back to the driver */
285 static bool wlan_tx_status(struct dma_queue *queue,
286                            struct dma_desc *desc)
287 {
288         struct ar9170_tx_frame *frame = DESC_PAYLOAD(desc);
289         struct carl9170_tx_superframe *super = get_super(desc);
290         struct ieee80211_hdr *hdr = &super->f.data.i3e;
291         unsigned int qidx = super->s.queue;
292         bool txfail, success;
293
294         success = true;
295
296         /* update hangcheck */
297         fw.wlan.last_tx_desc_num[qidx] = 0;
298
299         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
300                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
301
302                 /* reset retry indicator flags */
303                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
304
305                 if (wlan_tx_consume_retry(super)) {
306                         /*
307                          * retry for simple and aggregated 802.11 frames.
308                          *
309                          * Note: We must not mess up the original frame
310                          * order.
311                          */
312
313                         if (!frame->hdr.mac.ampdu) {
314                                 /*
315                                  * 802.11 - 7.1.3.1.5.
316                                  * set "Retry Field" for consecutive attempts
317                                  *
318                                  * Note: For AMPDU see:
319                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
320                                  */
321
322                                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
323                         }
324
325                         if (txfail) {
326                                 /* Normal TX Failure */
327
328                                 /* demise descriptor ownership back to the hardware */
329                                 dma_rearm(desc);
330
331                                 /*
332                                  * And this will get the queue going again.
333                                  * To understand why: you have to get the HW
334                                  * specs... But sadly I never saw them.
335                                  */
336                                 wlan_txunstuck(qidx);
337
338                                 /* abort cycle - this is necessary due to HW design */
339                                 return false;
340                         } else {
341                                 /* (HT-) BlockACK failure */
342
343                                 /*
344                                  * Unlink the failed attempt and put it into
345                                  * the retry queue. The caller routine must
346                                  * be aware of this so the frames don't get lost.
347                                  */
348
349                                 dma_unlink_head(queue);
350 #ifdef CONFIG_CARL9170FW_DELAYED_TX
351                                 dma_put(&fw.wlan.tx_retry, desc);
352 #else
353                                 __wlan_tx(desc);
354 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
355                                 return true;
356                         }
357                 } else {
358                         /* out of frame attempts - discard frame */
359                         success = false;
360                 }
361         }
362
363         dma_unlink_head(queue);
364         if (txfail) {
365                 /*
366                  * Issue the queue bump,
367                  * We need to do this in case this was the frame's last
368                  * possible retry attempt and it unfortunately: it failed.
369                  */
370
371                 wlan_txunstuck(qidx);
372         }
373
374         unhide_super(desc);
375
376 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
377         if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
378                 fw.wlan.ba_desc = desc;
379                 fw.wlan.ba_desc_available = 1;
380                 return true;
381         }
382 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
383
384         wlan_tx_complete(super, success);
385
386 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
387         if (unlikely(super->s.cab))
388                 fw.wlan.cab_queue_len[super->s.vif_id]--;
389 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
390
391         /* recycle freed descriptors */
392         dma_reclaim(&fw.pta.down_queue, desc);
393         down_trigger();
394         return true;
395 }
396
397 static void handle_tx_completion(void)
398 {
399         struct dma_desc *desc;
400         unsigned int i;
401
402         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
403                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
404                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
405                                 /* termination requested. */
406                                 break;
407                         }
408                 }
409
410 #ifdef CONFIG_CARL9170FW_DELAYED_TX
411                 for_each_desc(desc, &fw.wlan.tx_retry)
412                         __wlan_tx(desc);
413
414                 for_each_desc(desc, &fw.wlan.tx_delay[i])
415                         _wlan_tx(desc);
416 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
417         }
418 }
419
420 void __hot wlan_tx(struct dma_desc *desc)
421 {
422         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
423
424         /* initialize rate control struct */
425         super->s.rix = 0;
426         super->s.cnt = 1;
427         hide_super(desc);
428
429 #ifdef CONFIG_CARL9170FW_DELAYED_TX
430         if (!queue_empty(&fw.wlan.tx_queue[super->s.queue])) {
431                 dma_put(&fw.wlan.tx_delay[super->s.queue], desc);
432                 return;
433         }
434 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
435
436 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
437         if (unlikely(super->s.cab)) {
438                 fw.wlan.cab_queue_len[super->s.vif_id]++;
439                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
440                 return;
441         }
442 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
443
444         _wlan_tx(desc);
445 }
446
447 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
448 static void wlan_send_buffered_ba(void)
449 {
450         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
451         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
452         struct carl9170_bar_ctx *ctx;
453
454         if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
455                 return;
456
457         /* there's no point to continue when the ba_desc is not available. */
458         if (!fw.wlan.ba_desc_available)
459                 return;
460
461         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
462         fw.wlan.ba_head_idx++;
463
464         /* Format BlockAck */
465         fw.wlan.ba_desc->status = 0;
466         fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
467         fw.wlan.ba_desc_available = 0;
468         fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
469                 fw.wlan.ba_desc;
470
471         baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
472                 sizeof(struct carl9170_tx_superdesc) +
473                 sizeof(struct ar9170_tx_hwdesc) +
474                 sizeof(struct ieee80211_ba);
475
476         baf->s.ri[0].tries = 3;
477         baf->s.queue = 0;
478         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
479
480         /* HW Duration / Backoff */
481         baf->f.hdr.mac.backoff = 1;
482         baf->f.hdr.mac.hw_duration = 1;
483
484         /* take the TX rate from the RX'd BAR */
485         baf->f.hdr.phy.set = ctx->phy;
486         baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
487
488         /* format outgoing BA */
489         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
490         ba->duration = cpu_to_le16(0);
491         memcpy(ba->ta, ctx->ta, 6);
492         memcpy(ba->ra, ctx->ra, 6);
493
494         /*
495          * Unfortunately, we cannot look into the hardware's scoreboard.
496          * Therefore we have to proceed as described in 802.11n 9.10.7.5
497          * and send a null BlockAck.
498          */
499         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
500
501         /*
502          * NB:
503          * not entirely sure if this is 100% correct?!
504          */
505         ba->control = ctx->control | cpu_to_le16(1);
506         ba->start_seq_num = ctx->start_seq_num;
507
508         wlan_tx(fw.wlan.ba_desc);
509 }
510
511 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
512 {
513         struct carl9170_bar_ctx *tmp;
514
515         /* expire oldest entry, if we ran out of ba_ctx' */
516         if (fw.wlan.ba_head_idx + CONFIG_CARL9170FW_BACK_REQS_NUM < fw.wlan.ba_tail_idx)
517                 fw.wlan.ba_head_idx++;
518
519         tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
520         fw.wlan.ba_tail_idx++;
521
522         return tmp;
523 }
524
525 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
526                        unsigned int len, unsigned int mac_err)
527 {
528         struct ieee80211_bar *bar;
529         struct carl9170_bar_ctx *ctx;
530
531         if (unlikely(mac_err)) {
532                 /*
533                  * This check does a number of things:
534                  * 1. checks if the frame is in good nick
535                  * 2. checks if the RA (MAC) matches
536                  */
537                 return ;
538         }
539
540         if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
541                 /*
542                  * Sneaky, corrupted BARs... but not with us!
543                  */
544
545                 return ;
546         }
547
548         bar = (void *) hdr;
549
550         if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
551             !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
552                 /* not implemented yet */
553
554                 return ;
555         }
556
557         ctx = wlan_get_bar_cache_buffer();
558
559         /* Brilliant! The BAR provides all necessary MACs! */
560         memcpy(ctx->ra, bar->ta, 6);
561         memcpy(ctx->ta, bar->ra, 6);
562
563         /*
564          * NB:
565          * not entirely sure if this is 100% correct to force the
566          * imm ack bit or not...
567          */
568         ctx->control = bar->control | cpu_to_le16(1);
569         ctx->start_seq_num = bar->start_seq_num;
570         ctx->phy = ar9170_rx_to_phy(desc);
571         if (unlikely(!ctx->phy)) {
572                 /* provide a backup, in case ar9170_rx_to_phy fails */
573                 ctx->phy = cpu_to_le32(0x2cc301);
574         }
575 }
576 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
577
578 static void wlan_check_rx_overrun(void)
579 {
580         uint32_t overruns, total;
581
582         fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
583         fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
584         if (unlikely(overruns)) {
585                 if (overruns == total) {
586                         DBG("RX Overrun");
587                         fw.wlan.mac_reset++;
588                 }
589
590                 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
591         }
592 }
593
594 static unsigned int wlan_rx_filter(struct dma_desc *desc)
595 {
596         struct ieee80211_hdr *hdr;
597         unsigned int data_len;
598         unsigned int rx_filter;
599         unsigned int mac_err;
600
601         data_len = ar9170_get_rx_mpdu_len(desc);
602         mac_err = ar9170_get_rx_macstatus_error(desc);
603
604 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP | \
605                              AR9170_RX_ERROR_FATAL)
606
607         if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
608             desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
609             mac_err & AR9170_RX_ERROR_BAD) {
610
611                 /*
612                  * This frame is too damaged to do anything
613                  * useful with it.
614                  */
615
616                 return CARL9170_RX_FILTER_BAD;
617         }
618
619         rx_filter = 0;
620         if (mac_err & AR9170_RX_ERROR_WRONG_RA)
621                 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
622
623         if (mac_err & AR9170_RX_ERROR_DECRYPT)
624                 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
625
626         hdr = ar9170_get_rx_i3e(desc);
627         if (likely(ieee80211_is_data(hdr->frame_control))) {
628                 rx_filter |= CARL9170_RX_FILTER_DATA;
629         } else if (ieee80211_is_ctl(hdr->frame_control)) {
630                 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
631                 case IEEE80211_STYPE_BACK_REQ:
632 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
633                         handle_bar(desc, hdr, data_len, mac_err);
634 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
635                         /* fallthrough */
636                         rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
637                         break;
638                 case IEEE80211_STYPE_PSPOLL:
639                         rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
640                         break;
641                 default:
642                         rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
643                         break;
644                 }
645         } else {
646                 /* ieee80211_is_mgmt */
647                 rx_filter |= CARL9170_RX_FILTER_MGMT;
648         }
649
650 #undef AR9170_RX_ERROR_BAD
651
652         return rx_filter;
653 }
654
655 static void handle_rx(void)
656 {
657         struct dma_desc *desc;
658
659         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
660                 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
661                         dma_put(&fw.pta.up_queue, desc);
662                         up_trigger();
663                 } else {
664                         dma_reclaim(&fw.wlan.rx_queue, desc);
665                         _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
666                 }
667         }
668 }
669
670 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
671 void wlan_cab_flush_queue(const unsigned int vif)
672 {
673         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
674         struct dma_desc *desc;
675
676         /* move queued frames into the main tx queues */
677         for_each_desc(desc, cab_queue) {
678                 struct carl9170_tx_superframe *super = get_super(desc);
679                 if (!queue_empty(cab_queue)) {
680                         /*
681                          * Set MOREDATA flag for all,
682                          * but the last queued frame.
683                          * see: 802.11-2007 11.2.1.5 f)
684                          *
685                          * This is actually the reason to why
686                          * we need to prevent the reentry.
687                          */
688
689                         super->f.data.i3e.frame_control |=
690                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
691                 } else {
692                         super->f.data.i3e.frame_control &=
693                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
694                 }
695
696                 /* ready to roll! */
697                 _wlan_tx(desc);
698         }
699 }
700
701 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
702                                const unsigned int len)
703 {
704         struct ieee80211_mgmt *mgmt = addr;
705         uint8_t *pos, *end;
706
707         pos = mgmt->u.beacon.variable;
708         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
709         while (pos < end) {
710                 if (pos + 2 + pos[1] > end)
711                         return NULL;
712
713                 if (pos[0] == ie)
714                         return pos;
715
716                 pos += pos[1] + 2;
717         }
718
719         return NULL;
720 }
721
722 void wlan_cab_modify_dtim_beacon(const unsigned int vif,
723         const unsigned int addr, const unsigned int len)
724 {
725         uint8_t *_ie;
726         struct ieee80211_tim_ie *ie;
727
728         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
729         if (likely(_ie)) {
730                 ie = (struct ieee80211_tim_ie *) &_ie[2];
731
732                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
733                         /* schedule DTIM transfer */
734                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
735                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
736                         /* undo all chances to the beacon structure */
737                         ie->bitmap_ctrl &= ~0x1;
738                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
739                 }
740
741                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
742                 if (fw.wlan.cab_flush_trigger[vif]) {
743                         /* Set the almighty Multicast Traffic Indication Bit. */
744                         ie->bitmap_ctrl |= 0x1;
745                 }
746         }
747 }
748 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
749
750 static void handle_beacon_config(void)
751 {
752         uint32_t bcn_count;
753
754         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
755         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
756                          (uint8_t *) &bcn_count);
757
758         set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
759 }
760
761 static void handle_pretbtt(void)
762 {
763 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
764         fw.wlan.cab_flush_time = get_clock_counter();
765 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
766
767 #ifdef CONFIG_CARL9170FW_PSM
768         rf_psm();
769
770         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
771                          (uint8_t *) &fw.phy.psm.state);
772 #else
773         send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
774 #endif /* CONFIG_CARL9170FW_PSM */
775 }
776
777 static void handle_atim(void)
778 {
779         send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
780 }
781
782 #ifdef CONFIG_CARL9170FW_DEBUG
783 static void handle_qos(void)
784 {
785         /*
786          * What is the QoS Bit used for?
787          * Is it only an indicator for TXOP & Burst, or
788          * should we do something here?
789          */
790 }
791
792 static void handle_radar(void)
793 {
794         send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
795 }
796 #endif /* CONFIG_CARL9170FW_DEBUG */
797
798 static void wlan_janitor(void)
799 {
800 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
801         unsigned int i;
802
803         for (i = 0; i < CARL9170_INTF_NUM; i++) {
804                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
805                         /*
806                          * This is hardcoded into carl9170usb driver.
807                          *
808                          * The driver must set the PRETBTT event to beacon_interval -
809                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
810                          *
811                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
812                          * 11.2.1.6. Let's hope the current solution is adequate enough.
813                          */
814
815                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
816                                 wlan_cab_flush_queue(i);
817
818                                 /*
819                                  * This prevents the code from sending new BC/MC frames
820                                  * which were queued after the previous buffered traffic
821                                  * has been sent out... They will have to wait until the
822                                  * next DTIM beacon comes along.
823                                  */
824                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
825                         }
826                 }
827
828         }
829 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
830
831 #ifdef CONFIG_CARL9170FW_DELAYED_TX
832         if (fw.wlan.tx_trigger) {
833                 _wlan_trigger(fw.wlan.tx_trigger);
834                 fw.wlan.tx_trigger = 0;
835         }
836 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
837
838         wlan_send_buffered_tx_status();
839
840 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
841         wlan_send_buffered_ba();
842 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
843 }
844
845 void handle_wlan(void)
846 {
847         uint32_t intr;
848
849         intr = get(AR9170_MAC_REG_INT_CTRL);
850         /* ACK Interrupt */
851         set(AR9170_MAC_REG_INT_CTRL, intr);
852
853 #define HANDLER(intr, flag, func)                       \
854         do {                                            \
855                 if ((intr & flag) != 0) {               \
856                         func();                         \
857                 }                                       \
858         } while (0)
859
860         intr |= fw.wlan.soft_int;
861         fw.wlan.soft_int = 0;
862
863         HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
864
865         HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
866
867         HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
868
869         HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
870                 handle_tx_completion);
871
872 #ifdef CONFIG_CARL9170FW_DEBUG
873         HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
874
875         HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
876 #endif /* CONFIG_CARL9170FW_DEBUG */
877
878         HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
879
880         if (unlikely(intr))
881                 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
882
883         wlan_janitor();
884
885 #undef HANDLER
886 }
887
888 static void wlan_check_hang(void)
889 {
890         struct dma_desc *desc;
891         unsigned int i;
892
893         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
894                 if (queue_empty(&fw.wlan.tx_queue[i])) {
895                         /* Nothing to do here... move along */
896                         continue;
897                 }
898
899                 /* fetch the current DMA queue position */
900                 desc = get_wlan_txq_addr(i);
901
902                 /* Stuck frame detection */
903                 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
904                         fw.wlan.last_tx_desc_num[i]++;
905
906                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
907                                 /*
908                                  * schedule MAC reset (aka OFF/ON => dead)
909                                  *
910                                  * This will almost certainly kill
911                                  * the device for good, but it's the
912                                  * recommended thing to do...
913                                  */
914
915                                 fw.wlan.mac_reset++;
916                         }
917
918 #ifdef CONFIG_CARL9170FW_DEBUG
919                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
920                                 /*
921                                  * Sigh, the queue is almost certainly
922                                  * dead. Dump the queue content to the
923                                  * user, maybe we find out why it got
924                                  * so stuck.
925                                  */
926
927                                 wlan_dump_queue(i);
928                         }
929 #endif /* CONFIG_CARL9170FW_DEBUG */
930
931 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
932                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
933                                 /*
934                                  * Hrrm, bump the queue a bit.
935                                  * maybe this will get it going again.
936                                  */
937
938                                 wlan_dma_bump(i);
939                         }
940 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
941                 } else {
942                         /* Nothing stuck */
943                         fw.wlan.last_tx_desc[i] = desc;
944                         fw.wlan.last_tx_desc_num[i] = 0;
945                 }
946         }
947 }
948
949 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
950 /*
951  * NB: Resetting the MAC is a two-edged sword.
952  * On most occasions, it does what it is supposed to do.
953  * But there is a chance that this will make it
954  * even worse and the radio dies silently.
955  */
956 static void wlan_mac_reset(void)
957 {
958         uint32_t val;
959         uint32_t agg_wait_counter;
960         uint32_t agg_density;
961         uint32_t bcn_start_addr;
962         uint32_t rctl, rcth;
963         uint32_t cam_mode;
964         uint32_t ack_power;
965         uint32_t rts_cts_tpc;
966         uint32_t rts_cts_rate;
967         unsigned int i;
968
969 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
970         uint32_t rx_BB;
971 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
972
973         INFO("MAC RESET");
974
975         /* Save aggregation parameters */
976         agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
977         agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
978
979         bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
980
981         cam_mode = get(AR9170_MAC_REG_CAM_MODE);
982         rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
983         rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
984
985         ack_power = get(AR9170_MAC_REG_ACK_TPC);
986         rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
987         rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
988
989 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
990         /* 0x1c8960 write only */
991         rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
992 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
993
994         /* TX/RX must be stopped by now */
995         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
996
997         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
998
999         /*
1000          * Manipulate CCA threshold to stop transmission
1001          *
1002          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1003          */
1004
1005         /*
1006          * check Rx state in 0(idle) 9(disable)
1007          *
1008          * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1009          * while( (chState != 0) && (chState != 9)) {
1010          *      chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1011          * }
1012          */
1013
1014         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1015
1016         delay(2);
1017
1018         /* Restore aggregation parameters */
1019         set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1020         set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1021
1022         set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1023         set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1024         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1025         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1026
1027         set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1028         set(AR9170_MAC_REG_ACK_TPC, ack_power);
1029         set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1030
1031 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1032         set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1033 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1034
1035         /*
1036          * Manipulate CCA threshold to resume transmission
1037          *
1038          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1039          */
1040
1041         /* Reinitialize all WLAN TX DMA queues. */
1042         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1043                 struct dma_desc *iter;
1044
1045                 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1046
1047                 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1048                 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1049                         wlan_trigger(BIT(i));
1050
1051                 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]),
1052                      fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1053                      get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1054         }
1055
1056         fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1057                             AR9170_MAC_INT_RETRY_FAIL;
1058
1059         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1060         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1061 }
1062 #else
1063 static void wlan_mac_reset(void)
1064 {
1065         /* The driver takes care of reinitializing the device */
1066         BUG("MAC RESET");
1067 }
1068 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1069
1070 void __cold wlan_timer(void)
1071 {
1072         unsigned int cached_mac_reset;
1073
1074         cached_mac_reset = fw.wlan.mac_reset;
1075
1076         /* TX Queue Hang check */
1077         wlan_check_hang();
1078
1079         /* RX Overrun check */
1080         wlan_check_rx_overrun();
1081
1082         if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1083                 wlan_mac_reset();
1084                 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1085         } else {
1086                 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1087                         fw.wlan.mac_reset--;
1088         }
1089 }