carl9170 firmware: fix CAB queue delivery
[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_CAB_QUEUE
430         if (unlikely(super->s.cab)) {
431                 fw.wlan.cab_queue_len[super->s.vif_id]++;
432                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
433                 return;
434         }
435 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
436
437 #ifdef CONFIG_CARL9170FW_DELAYED_TX
438         if (!queue_empty(&fw.wlan.tx_queue[super->s.queue])) {
439                 dma_put(&fw.wlan.tx_delay[super->s.queue], desc);
440                 return;
441         }
442 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
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
606         if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
607             desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
608             mac_err & AR9170_RX_ERROR_BAD) {
609                 /*
610                  * This frame is too damaged to do anything
611                  * useful with it.
612                  */
613
614                 return CARL9170_RX_FILTER_BAD;
615         }
616
617         rx_filter = 0;
618         if (mac_err & AR9170_RX_ERROR_WRONG_RA)
619                 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
620
621         if (mac_err & AR9170_RX_ERROR_DECRYPT)
622                 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
623
624         hdr = ar9170_get_rx_i3e(desc);
625         if (likely(ieee80211_is_data(hdr->frame_control))) {
626                 rx_filter |= CARL9170_RX_FILTER_DATA;
627         } else if (ieee80211_is_ctl(hdr->frame_control)) {
628                 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
629                 case IEEE80211_STYPE_BACK_REQ:
630 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
631                         handle_bar(desc, hdr, data_len, mac_err);
632 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
633                         /* fallthrough */
634                         rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
635                         break;
636                 case IEEE80211_STYPE_PSPOLL:
637                         rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
638                         break;
639                 default:
640                         rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
641                         break;
642                 }
643         } else {
644                 /* ieee80211_is_mgmt */
645                 rx_filter |= CARL9170_RX_FILTER_MGMT;
646         }
647
648 #undef AR9170_RX_ERROR_BAD
649
650         return rx_filter;
651 }
652
653 static void handle_rx(void)
654 {
655         struct dma_desc *desc;
656
657         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
658                 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
659                         dma_put(&fw.pta.up_queue, desc);
660                         up_trigger();
661                 } else {
662                         dma_reclaim(&fw.wlan.rx_queue, desc);
663                         _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
664                 }
665         }
666 }
667
668 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
669 void wlan_cab_flush_queue(const unsigned int vif)
670 {
671         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
672         struct dma_desc *desc;
673
674         /* move queued frames into the main tx queues */
675         for_each_desc(desc, cab_queue) {
676                 struct carl9170_tx_superframe *super = get_super(desc);
677                 if (!queue_empty(cab_queue)) {
678                         /*
679                          * Set MOREDATA flag for all,
680                          * but the last queued frame.
681                          * see: 802.11-2007 11.2.1.5 f)
682                          *
683                          * This is actually the reason to why
684                          * we need to prevent the reentry.
685                          */
686
687                         super->f.data.i3e.frame_control |=
688                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
689                 } else {
690                         super->f.data.i3e.frame_control &=
691                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
692                 }
693
694                 /* ready to roll! */
695                 _wlan_tx(desc);
696         }
697 }
698
699 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
700                                const unsigned int len)
701 {
702         struct ieee80211_mgmt *mgmt = addr;
703         uint8_t *pos, *end;
704
705         pos = mgmt->u.beacon.variable;
706         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
707         while (pos < end) {
708                 if (pos + 2 + pos[1] > end)
709                         return NULL;
710
711                 if (pos[0] == ie)
712                         return pos;
713
714                 pos += pos[1] + 2;
715         }
716
717         return NULL;
718 }
719
720 void wlan_cab_modify_dtim_beacon(const unsigned int vif,
721         const unsigned int addr, const unsigned int len)
722 {
723         uint8_t *_ie;
724         struct ieee80211_tim_ie *ie;
725
726         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
727         if (likely(_ie)) {
728                 ie = (struct ieee80211_tim_ie *) &_ie[2];
729
730                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
731                         /* schedule DTIM transfer */
732                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
733                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
734                         /* undo all chances to the beacon structure */
735                         ie->bitmap_ctrl &= ~0x1;
736                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
737                 }
738
739                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
740                 if (fw.wlan.cab_flush_trigger[vif]) {
741                         /* Set the almighty Multicast Traffic Indication Bit. */
742                         ie->bitmap_ctrl |= 0x1;
743                 }
744         }
745 }
746 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
747
748 static void handle_beacon_config(void)
749 {
750         uint32_t bcn_count;
751
752         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
753         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
754                          (uint8_t *) &bcn_count);
755
756         set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
757 }
758
759 static void handle_pretbtt(void)
760 {
761 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
762         fw.wlan.cab_flush_time = get_clock_counter();
763 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
764
765 #ifdef CONFIG_CARL9170FW_PSM
766         rf_psm();
767
768         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
769                          (uint8_t *) &fw.phy.psm.state);
770 #else
771         send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
772 #endif /* CONFIG_CARL9170FW_PSM */
773 }
774
775 static void handle_atim(void)
776 {
777         send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
778 }
779
780 #ifdef CONFIG_CARL9170FW_DEBUG
781 static void handle_qos(void)
782 {
783         /*
784          * What is the QoS Bit used for?
785          * Is it only an indicator for TXOP & Burst, or
786          * should we do something here?
787          */
788 }
789
790 static void handle_radar(void)
791 {
792         send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
793 }
794 #endif /* CONFIG_CARL9170FW_DEBUG */
795
796 static void wlan_janitor(void)
797 {
798 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
799         unsigned int i;
800
801         for (i = 0; i < CARL9170_INTF_NUM; i++) {
802                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
803                         /*
804                          * This is hardcoded into carl9170usb driver.
805                          *
806                          * The driver must set the PRETBTT event to beacon_interval -
807                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
808                          *
809                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
810                          * 11.2.1.6. Let's hope the current solution is adequate enough.
811                          */
812
813                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
814                                 wlan_cab_flush_queue(i);
815
816                                 /*
817                                  * This prevents the code from sending new BC/MC frames
818                                  * which were queued after the previous buffered traffic
819                                  * has been sent out... They will have to wait until the
820                                  * next DTIM beacon comes along.
821                                  */
822                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
823                         }
824                 }
825
826         }
827 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
828
829 #ifdef CONFIG_CARL9170FW_DELAYED_TX
830         if (fw.wlan.tx_trigger) {
831                 _wlan_trigger(fw.wlan.tx_trigger);
832                 fw.wlan.tx_trigger = 0;
833         }
834 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
835
836         wlan_send_buffered_tx_status();
837
838 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
839         wlan_send_buffered_ba();
840 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
841 }
842
843 void handle_wlan(void)
844 {
845         uint32_t intr;
846
847         intr = get(AR9170_MAC_REG_INT_CTRL);
848         /* ACK Interrupt */
849         set(AR9170_MAC_REG_INT_CTRL, intr);
850
851 #define HANDLER(intr, flag, func)                       \
852         do {                                            \
853                 if ((intr & flag) != 0) {               \
854                         func();                         \
855                 }                                       \
856         } while (0)
857
858         intr |= fw.wlan.soft_int;
859         fw.wlan.soft_int = 0;
860
861         HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
862
863         HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
864
865         HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
866
867         HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
868                 handle_tx_completion);
869
870 #ifdef CONFIG_CARL9170FW_DEBUG
871         HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
872
873         HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
874 #endif /* CONFIG_CARL9170FW_DEBUG */
875
876         HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
877
878         if (unlikely(intr))
879                 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
880
881         wlan_janitor();
882
883 #undef HANDLER
884 }
885
886 static void wlan_check_hang(void)
887 {
888         struct dma_desc *desc;
889         unsigned int i;
890
891         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
892                 if (queue_empty(&fw.wlan.tx_queue[i])) {
893                         /* Nothing to do here... move along */
894                         continue;
895                 }
896
897                 /* fetch the current DMA queue position */
898                 desc = get_wlan_txq_addr(i);
899
900                 /* Stuck frame detection */
901                 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
902                         fw.wlan.last_tx_desc_num[i]++;
903
904                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
905                                 /*
906                                  * schedule MAC reset (aka OFF/ON => dead)
907                                  *
908                                  * This will almost certainly kill
909                                  * the device for good, but it's the
910                                  * recommended thing to do...
911                                  */
912
913                                 fw.wlan.mac_reset++;
914                         }
915
916 #ifdef CONFIG_CARL9170FW_DEBUG
917                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
918                                 /*
919                                  * Sigh, the queue is almost certainly
920                                  * dead. Dump the queue content to the
921                                  * user, maybe we find out why it got
922                                  * so stuck.
923                                  */
924
925                                 wlan_dump_queue(i);
926                         }
927 #endif /* CONFIG_CARL9170FW_DEBUG */
928
929 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
930                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
931                                 /*
932                                  * Hrrm, bump the queue a bit.
933                                  * maybe this will get it going again.
934                                  */
935
936                                 wlan_dma_bump(i);
937                         }
938 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
939                 } else {
940                         /* Nothing stuck */
941                         fw.wlan.last_tx_desc[i] = desc;
942                         fw.wlan.last_tx_desc_num[i] = 0;
943                 }
944         }
945 }
946
947 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
948 /*
949  * NB: Resetting the MAC is a two-edged sword.
950  * On most occasions, it does what it is supposed to do.
951  * But there is a chance that this will make it
952  * even worse and the radio dies silently.
953  */
954 static void wlan_mac_reset(void)
955 {
956         uint32_t val;
957         uint32_t agg_wait_counter;
958         uint32_t agg_density;
959         uint32_t bcn_start_addr;
960         uint32_t rctl, rcth;
961         uint32_t cam_mode;
962         uint32_t ack_power;
963         uint32_t rts_cts_tpc;
964         uint32_t rts_cts_rate;
965         unsigned int i;
966
967 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
968         uint32_t rx_BB;
969 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
970
971         INFO("MAC RESET");
972
973         /* Save aggregation parameters */
974         agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
975         agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
976
977         bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
978
979         cam_mode = get(AR9170_MAC_REG_CAM_MODE);
980         rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
981         rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
982
983         ack_power = get(AR9170_MAC_REG_ACK_TPC);
984         rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
985         rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
986
987 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
988         /* 0x1c8960 write only */
989         rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
990 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
991
992         /* TX/RX must be stopped by now */
993         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
994
995         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
996
997         /*
998          * Manipulate CCA threshold to stop transmission
999          *
1000          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1001          */
1002
1003         /*
1004          * check Rx state in 0(idle) 9(disable)
1005          *
1006          * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1007          * while( (chState != 0) && (chState != 9)) {
1008          *      chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1009          * }
1010          */
1011
1012         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1013
1014         delay(2);
1015
1016         /* Restore aggregation parameters */
1017         set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1018         set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1019
1020         set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1021         set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1022         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1023         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1024
1025         set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1026         set(AR9170_MAC_REG_ACK_TPC, ack_power);
1027         set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1028
1029 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1030         set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1031 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1032
1033         /*
1034          * Manipulate CCA threshold to resume transmission
1035          *
1036          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1037          */
1038
1039         /* Reinitialize all WLAN TX DMA queues. */
1040         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1041                 struct dma_desc *iter;
1042
1043                 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1044
1045                 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1046                 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1047                         wlan_trigger(BIT(i));
1048
1049                 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]),
1050                      fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1051                      get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1052         }
1053
1054         fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1055                             AR9170_MAC_INT_RETRY_FAIL;
1056
1057         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1058         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1059 }
1060 #else
1061 static void wlan_mac_reset(void)
1062 {
1063         /* The driver takes care of reinitializing the device */
1064         BUG("MAC RESET");
1065 }
1066 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1067
1068 void __cold wlan_timer(void)
1069 {
1070         unsigned int cached_mac_reset;
1071
1072         cached_mac_reset = fw.wlan.mac_reset;
1073
1074         /* TX Queue Hang check */
1075         wlan_check_hang();
1076
1077         /* RX Overrun check */
1078         wlan_check_rx_overrun();
1079
1080         if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1081                 wlan_mac_reset();
1082                 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1083         } else {
1084                 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1085                         fw.wlan.mac_reset--;
1086         }
1087 }