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