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