carl9170 firmware: allow fallback to non-aggregation xmit
[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 static void wlan_txupdate(unsigned int queue)
42 {
43         set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head));
44         wlan_trigger(BIT(queue));
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
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 /* for all tries */
214 static void __wlan_tx(struct dma_desc *desc)
215 {
216         struct carl9170_tx_superframe *super = get_super(desc);
217 #ifdef CONFIG_CARL9170FW_NORMAL_TX_RX
218         unsigned int queue = super->s.queue;
219 #endif /* CONFIG_CARL9170FW_LOOPBACK */
220
221         if (unlikely(super->s.fill_in_tsf)) {
222                 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
223                 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
224
225                 /*
226                  * Truth be told: this is a hack.
227                  *
228                  * The *real* TSF is definitely going to be higher/older.
229                  * But this hardware emulation code is head and shoulders
230                  * above anything a driver can possibly do.
231                  *
232                  * (even, if it's got an accurate atomic clock source).
233                  */
234
235                 read_tsf(tsf);
236         }
237
238 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
239         wlan_tx_complete(super, true);
240         unhide_super(desc);
241 # ifdef CONFIG_CARL9170FW_LOOPBACK
242         dma_put(&fw.pta.up_queue, desc);
243         up_trigger();
244 # elif CONFIG_CARL9170FW_DISCARD
245         dma_reclaim(&fw.pta.down_queue, desc);
246         down_trigger();
247 # endif
248 #else /* CONFIG_CARL9170FW_LOOPBACK */
249
250 # if ((defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_PSM))
251         BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
252 # endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_PSM */
253
254         /* insert desc into the right queue */
255         dma_put(&fw.wlan.tx_queue[queue], desc);
256         wlan_trigger(BIT(queue));
257 #endif /* CONFIG_CARL9170FW_LOOPBACK */
258 }
259
260 /* prepares frame for the first transmission */
261 static void _wlan_tx(struct dma_desc *desc)
262 {
263         struct carl9170_tx_superframe *super = get_super(desc);
264
265         if (unlikely(super->s.ampdu_commit_density)) {
266                 set(AR9170_MAC_REG_AMPDU_DENSITY,
267                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
268                             get(AR9170_MAC_REG_AMPDU_DENSITY),
269                             super->s.ampdu_density));
270         }
271
272         if (unlikely(super->s.ampdu_commit_factor)) {
273                 set(AR9170_MAC_REG_AMPDU_FACTOR,
274                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
275                             get(AR9170_MAC_REG_AMPDU_FACTOR),
276                             8 << super->s.ampdu_factor));
277         }
278
279         __wlan_tx(desc);
280 }
281
282 /* propagate transmission status back to the driver */
283 static bool wlan_tx_status(struct dma_queue *queue,
284                            struct dma_desc *desc)
285 {
286         struct ar9170_tx_frame *frame = DESC_PAYLOAD(desc);
287         struct carl9170_tx_superframe *super = get_super(desc);
288         struct ieee80211_hdr *hdr = &super->f.data.i3e;
289         unsigned int qidx = super->s.queue;
290         bool txfail, success;
291
292         success = true;
293
294         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
295                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
296
297                 /* reset retry indicator flags */
298                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
299
300                 if (wlan_tx_consume_retry(super)) {
301                         /*
302                          * retry for simple and aggregated 802.11 frames.
303                          *
304                          * Note: We must not mess up the original frame
305                          * order.
306                          */
307
308                         if (!frame->hdr.mac.ampdu) {
309                                 /*
310                                  * 802.11 - 7.1.3.1.5.
311                                  * set "Retry Field" for consecutive attempts
312                                  *
313                                  * Note: For AMPDU see:
314                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
315                                  */
316
317                                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
318                         }
319
320                         if (txfail) {
321                                 /* Normal TX Failure */
322
323                                 /* demise descriptor ownership back to the hardware */
324                                 dma_rearm(desc);
325
326                                 /*
327                                  * And this will get the queue going again.
328                                  * To understand why: you have to get the HW
329                                  * specs... But sadly I never saw them.
330                                  */
331                                 wlan_txunstuck(qidx);
332
333                                 /* abort cycle - this is necessary due to HW design */
334                                 return false;
335                         } else {
336                                 /* (HT-) BlockACK failure */
337
338                                 /*
339                                  * Unlink the failed attempt and put it into
340                                  * the retry queue. The caller routine must
341                                  * be aware of this so the frames don't get lost.
342                                  */
343
344                                 dma_unlink_head(queue);
345 #ifdef CONFIG_CARL9170FW_DELAYED_TX
346                                 dma_put(&fw.wlan.tx_retry, desc);
347 #else
348                                 __wlan_tx(desc);
349 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
350                                 return true;
351                         }
352                 } else {
353                         /* out of frame attempts - discard frame */
354                         success = false;
355                 }
356         }
357
358         dma_unlink_head(queue);
359         if (txfail) {
360                 /*
361                  * Issue the queue bump,
362                  * We need to do this in case this was the frame's last
363                  * possible retry attempt and it unfortunately: it failed.
364                  */
365
366                 wlan_txunstuck(qidx);
367         }
368
369         unhide_super(desc);
370
371         /* update hangcheck */
372         fw.wlan.last_tx_desc_num[qidx] = 0;
373
374 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
375         if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
376                 fw.wlan.ba_desc = desc;
377                 fw.wlan.ba_desc_available = 1;
378                 return true;
379         }
380 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
381
382         wlan_tx_complete(super, success);
383
384 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
385         if (unlikely(super->s.cab))
386                 fw.wlan.cab_queue_len--;
387 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
388
389         /* recycle freed descriptors */
390         dma_reclaim(&fw.pta.down_queue, desc);
391         down_trigger();
392         return true;
393 }
394
395 static void handle_tx_completion(void)
396 {
397         struct dma_desc *desc;
398         unsigned int i;
399
400         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
401                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
402                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
403                                 /* termination requested. */
404                                 break;
405                         }
406                 }
407
408 #ifdef CONFIG_CARL9170FW_DELAYED_TX
409                 for_each_desc(desc, &fw.wlan.tx_retry)
410                         __wlan_tx(desc);
411
412                 for_each_desc(desc, &fw.wlan.tx_delay[i])
413                         _wlan_tx(desc);
414 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
415         }
416 }
417
418 void __hot wlan_tx(struct dma_desc *desc)
419 {
420         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
421
422         /* initialize rate control struct */
423         super->s.rix = 0;
424         super->s.cnt = 1;
425         hide_super(desc);
426
427 #ifdef CONFIG_CARL9170FW_DELAYED_TX
428         if (!queue_empty(&fw.wlan.tx_queue[super->s.queue])) {
429                 dma_put(&fw.wlan.tx_delay[super->s.queue], desc);
430                 return;
431         }
432 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
433
434 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
435         if (unlikely(super->s.cab)) {
436                 fw.wlan.cab_queue_len++;
437                 dma_put(&fw.wlan.cab_queue, desc);
438                 return;
439         }
440 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
441
442         _wlan_tx(desc);
443 }
444
445 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
446 static void wlan_send_buffered_ba(void)
447 {
448         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
449         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
450         struct carl9170_bar_ctx *ctx;
451
452         if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
453                 return;
454
455         /* there's no point to continue when the ba_desc is not available. */
456         if (!fw.wlan.ba_desc_available)
457                 return;
458
459         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
460         fw.wlan.ba_head_idx++;
461
462         /* Format BlockAck */
463         fw.wlan.ba_desc->status = 0;
464         fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
465         fw.wlan.ba_desc_available = 0;
466         fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
467                 fw.wlan.ba_desc;
468
469         baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
470                 sizeof(struct carl9170_tx_superdesc) +
471                 sizeof(struct ar9170_tx_hwdesc) +
472                 sizeof(struct ieee80211_ba);
473
474         baf->s.ri[0].tries = 3;
475         baf->s.queue = 0;
476         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
477
478         /* HW Duration / Backoff */
479         baf->f.hdr.mac.backoff = 1;
480         baf->f.hdr.mac.hw_duration = 1;
481
482         /* take the TX rate from the RX'd BAR */
483         baf->f.hdr.phy.set = ctx->phy;
484         baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
485
486         /* format outgoing BA */
487         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
488         ba->duration = cpu_to_le16(0);
489         memcpy(ba->ta, ctx->ta, 6);
490         memcpy(ba->ra, ctx->ra, 6);
491
492         /*
493          * Unfortunately, we cannot look into the hardware's scoreboard.
494          * Therefore we have to proceed as described in 802.11n 9.10.7.5
495          * and send a null BlockAck.
496          */
497         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
498
499         /*
500          * NB:
501          * not entirely sure if this is 100% correct?!
502          */
503         ba->control = ctx->control | cpu_to_le16(1);
504         ba->start_seq_num = ctx->start_seq_num;
505
506         wlan_tx(fw.wlan.ba_desc);
507 }
508
509 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
510 {
511         struct carl9170_bar_ctx *tmp;
512
513         /* expire oldest entry, if we ran out of ba_ctx' */
514         if (fw.wlan.ba_head_idx + CONFIG_CARL9170FW_BACK_REQS_NUM < fw.wlan.ba_tail_idx)
515                 fw.wlan.ba_head_idx++;
516
517         tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
518         fw.wlan.ba_tail_idx++;
519
520         return tmp;
521 }
522
523 static void handle_bar(struct dma_desc *desc)
524 {
525         struct ieee80211_hdr *hdr;
526         struct ieee80211_bar *bar;
527         struct carl9170_bar_ctx *ctx;
528
529         hdr = ar9170_get_rx_i3e(desc);
530
531         /* check if this is a BAR for us */
532         if (likely(!ieee80211_is_back_req(hdr->frame_control)))
533                 return ;
534
535         if (unlikely(ar9170_get_rx_macstatus_error(desc))) {
536                 /*
537                  * This check does a number of things:
538                  * 1. checks if the frame is in good nick
539                  * 2. checks if the RA (MAC) matches
540                  */
541                 return ;
542         }
543
544         if (unlikely(ar9170_get_rx_mpdu_len(desc) <
545             sizeof(struct ieee80211_bar))) {
546                 /*
547                  * Sneaky, corrupted BARs... but not with us!
548                  */
549
550                 return ;
551         }
552
553         bar = (void *) hdr;
554
555         if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
556             !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
557                 /* not implemented yet */
558
559                 return ;
560         }
561
562         ctx = wlan_get_bar_cache_buffer();
563
564         /* Brilliant! The BAR provides all necessary MACs! */
565         memcpy(ctx->ra, bar->ta, 6);
566         memcpy(ctx->ta, bar->ra, 6);
567
568         /*
569          * NB:
570          * not entirely sure if this is 100% correct to force the
571          * imm ack bit or not...
572          */
573         ctx->control = bar->control | cpu_to_le16(1);
574         ctx->start_seq_num = bar->start_seq_num;
575         ctx->phy = ar9170_rx_to_phy(desc);
576         if (unlikely(!ctx->phy)) {
577                 /* provide a backup, in case ar9170_rx_to_phy fails */
578                 ctx->phy = cpu_to_le32(0x2cc301);
579         }
580 }
581 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
582
583 static void wlan_check_rx_overrun(void)
584 {
585         uint32_t overruns, total;
586
587         fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
588         fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
589         if (unlikely(overruns)) {
590                 if (overruns == total) {
591                         DBG("RX Overrun");
592                         fw.wlan.mac_reset++;
593                 }
594         }
595 }
596
597 static void handle_rx(void)
598 {
599         struct dma_desc *desc;
600
601         wlan_check_rx_overrun();
602
603         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
604                 if (unlikely(desc->totalLen < 26 ||
605                     desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN)) {
606                         /*
607                          * This frame is too damaged to do anything
608                          * useful with it.
609                          */
610                         dma_reclaim(&fw.wlan.rx_queue, desc);
611                         _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
612                 } else {
613 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
614                         handle_bar(desc);
615 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
616
617                         dma_put(&fw.pta.up_queue, desc);
618                         up_trigger();
619                 }
620         }
621 }
622
623 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
624 static uint8_t *beacon_find_ie(uint8_t ie)
625 {
626         struct ieee80211_mgmt *mgmt = getp(AR9170_MAC_REG_BCN_ADDR);
627         uint8_t *pos, *end;
628         unsigned int len;
629
630         len = get(AR9170_MAC_REG_BCN_LENGTH);
631
632         if (len < FCS_LEN + sizeof(mgmt))
633                 return NULL;
634
635         pos = mgmt->u.beacon.variable;
636         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
637         while (pos < end) {
638                 if (pos + 2 + pos[1] > end)
639                         return NULL;
640
641                 if (pos[0] == ie)
642                         return pos;
643
644                 pos += pos[1] + 2;
645         }
646
647         return NULL;
648 }
649
650 static void wlan_cab_flush_queue(void)
651 {
652         struct dma_desc *desc;
653         uint8_t *_ie;
654         struct ieee80211_tim_ie *ie;
655
656         /*
657          * This prevents the code from sending new BC/MC frames
658          * which were queued after the previous buffered traffic
659          * has been sent out... They will have to wait until the
660          * next DTIM beacon comes along.
661          */
662         if (unlikely(fw.wlan.cab_flush_trigger == CARL9170_CAB_TRIGGER_DEFER))
663                 return ;
664
665         _ie = beacon_find_ie(WLAN_EID_TIM);
666         if (unlikely(!_ie))
667                 return ;
668
669         ie = (struct ieee80211_tim_ie *) &_ie[2];
670
671         /* Ideally, check here for == AR9170_CAB_TRIGGER_ARMED */
672         if (fw.wlan.cab_flush_trigger) {
673                 /* move queued frames into the main tx queues */
674                 for_each_desc(desc, &fw.wlan.cab_queue) {
675                         struct carl9170_tx_superframe *super = get_super(desc);
676
677                         if (!queue_empty(&fw.wlan.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         /* Transfer finished - waiting for tx status */
700         fw.wlan.cab_flush_trigger = CARL9170_CAB_TRIGGER_DEFER;
701 }
702
703 static void wlan_cab_modify_dtim_beacon(void)
704 {
705         uint8_t *_ie;
706         struct ieee80211_tim_ie *ie;
707
708         _ie = beacon_find_ie(WLAN_EID_TIM);
709         if (likely(_ie)) {
710                 ie = (struct ieee80211_tim_ie *) &_ie[2];
711
712                 if (!queue_empty(&fw.wlan.cab_queue) && (ie->dtim_count == 0)) {
713                         /* schedule DTIM transfer */
714                         fw.wlan.cab_flush_trigger = CARL9170_CAB_TRIGGER_ARMED;
715                 } else if ((fw.wlan.cab_queue_len == 0) && (fw.wlan.cab_flush_trigger)) {
716                         /* undo all chances to the beacon structure */
717                         ie->bitmap_ctrl &= ~0x1;
718                         fw.wlan.cab_flush_trigger = CARL9170_CAB_TRIGGER_EMPTY;
719                 }
720
721                 if (fw.wlan.cab_flush_trigger) {
722                         /* Set the almighty Multicast Traffic Indication Bit. */
723                         ie->bitmap_ctrl |= 0x1;
724                 }
725         }
726 }
727 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
728
729 static void handle_beacon_config(void)
730 {
731         uint32_t bcn_count;
732
733 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
734         /*
735          * The application has now updated the relevant beacon data.
736          * Now it should be the perfect time to apply the DTIM
737          * multicast information.
738          */
739
740         wlan_cab_modify_dtim_beacon();
741 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
742
743         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
744         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
745                          (uint8_t *) &bcn_count);
746 }
747
748 static void handle_pretbtt(void)
749 {
750 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
751         fw.wlan.cab_flush_time = get_clock_counter();
752 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
753
754 #ifdef CONFIG_CARL9170FW_PSM
755         rf_psm();
756
757         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
758                          (uint8_t *) &fw.phy.psm.state);
759 #else
760         send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
761 #endif /* CONFIG_CARL9170FW_PSM */
762
763 }
764
765 static void handle_atim(void)
766 {
767         send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
768 }
769
770 #ifdef CONFIG_CARL9170FW_DEBUG
771 static void handle_qos(void)
772 {
773         /*
774          * What is the QoS Bit used for?
775          * Is it only an indicator for TXOP & Burst, or
776          * should we do something here?
777          */
778 }
779
780 static void handle_radar(void)
781 {
782         send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
783 }
784 #endif /* CONFIG_CARL9170FW_DEBUG */
785
786 static void wlan_janitor(void)
787 {
788 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
789         if (unlikely(fw.wlan.cab_flush_trigger)) {
790                 /*
791                  * This is hardcoded into carl9170usb driver.
792                  *
793                  * The driver must set the PRETBTT event to beacon_interval -
794                  * CARL9170_PRETBTT_KUS (usually 6) Kus.
795                  *
796                  * But still, we can only do so much about 802.11-2007 9.3.2.1 &
797                  * 11.2.1.6. Let's hope the current solution is adequate enough.
798                  */
799
800                 if (is_after_msecs(fw.wlan.cab_flush_time,
801                     (CARL9170_TBTT_DELTA))) {
802                         wlan_cab_flush_queue();
803                 }
804         }
805 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
806
807 #ifdef CONFIG_CARL9170FW_DELAYED_TX
808         if (fw.wlan.tx_trigger) {
809                 _wlan_trigger(fw.wlan.tx_trigger);
810                 fw.wlan.tx_trigger = 0;
811         }
812 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
813
814         wlan_send_buffered_tx_status();
815
816 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
817         wlan_send_buffered_ba();
818 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
819 }
820
821 void handle_wlan(void)
822 {
823         uint32_t intr;
824
825         intr = get(AR9170_MAC_REG_INT_CTRL);
826         /* ACK Interrupt */
827         set(AR9170_MAC_REG_INT_CTRL, intr);
828
829 #define HANDLER(intr, flag, func)                       \
830         do {                                            \
831                 if ((intr & flag) != 0) {               \
832                         func();                         \
833                 }                                       \
834         } while (0)
835
836         HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
837
838         HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
839
840         HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
841
842         HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
843                 handle_tx_completion);
844
845 #ifdef CONFIG_CARL9170FW_DEBUG
846         HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
847
848         HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
849 #endif /* CONFIG_CARL9170FW_DEBUG */
850
851         HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
852
853         if (unlikely(intr))
854                 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
855
856         wlan_janitor();
857
858 #undef HANDLER
859 }
860
861 static void wlan_check_hang(void)
862 {
863         struct dma_desc *desc;
864         unsigned int i;
865
866         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
867                 if (queue_empty(&fw.wlan.tx_queue[i])) {
868                         /* Nothing to do here... move along */
869                         continue;
870                 }
871
872                 /* fetch the current DMA queue position */
873                 desc = get_wlan_txq_addr(i);
874
875                 /* Stuck frame detection */
876                 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
877                         fw.wlan.last_tx_desc_num[i]++;
878
879                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
880                                 /*
881                                  * schedule MAC reset (aka OFF/ON => dead)
882                                  *
883                                  * This will almost certainly kill
884                                  * the device for good, but it's the
885                                  * recommended thing to do...
886                                  */
887
888                                 fw.wlan.mac_reset++;
889                         }
890
891 #ifdef CONFIG_CARL9170FW_DEBUG
892                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
893                                 /*
894                                  * Sigh, the queue is almost certainly
895                                  * dead. Dump the queue content to the
896                                  * user, maybe we find out why it got
897                                  * so stuck.
898                                  */
899
900                                 wlan_dump_queue(i);
901                         }
902 #endif /* CONFIG_CARL9170FW_DEBUG */
903
904                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
905                                 /*
906                                  * Hrrm, bump the queue a bit.
907                                  * maybe this will get it going again.
908                                  */
909
910                                 wlan_dma_bump(i);
911                         }
912                 } else {
913                         /* Nothing stuck */
914                         fw.wlan.last_tx_desc[i] = desc;
915                         fw.wlan.last_tx_desc_num[i] = 0;
916                 }
917         }
918 }
919
920 /*
921  * NB: Resetting the MAC is a two-edged sword.
922  * On most occasions, it does what it is supposed to do.
923  * But there is a chance that this will make it
924  * even worse and the radio dies silently.
925  */
926 static void wlan_mac_reset(void)
927 {
928         uint32_t val;
929         uint32_t agg_wait_counter;
930         uint32_t agg_density;
931         uint32_t bcn_start_addr;
932         uint32_t rctl, rcth;
933         uint32_t cam_mode;
934         uint32_t ack_power;
935         uint32_t rts_cts_tpc;
936         unsigned int i;
937
938 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
939         uint32_t rx_BB;
940 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
941
942         ERR("MAC RESET");
943
944         /* Save aggregation parameters */
945         agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
946         agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
947
948         bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
949
950         cam_mode = get(AR9170_MAC_REG_CAM_MODE);
951         rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
952         rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
953
954         ack_power = get(AR9170_MAC_REG_ACK_TPC);
955         rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
956
957 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
958         /* 0x1c8960 write only */
959         rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
960 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
961
962         /* TX/RX must be stopped by now */
963         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
964
965         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
966
967         /*
968          * Manipulate CCA threshold to stop transmission
969          *
970          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
971          */
972
973         /*
974          * check Rx state in 0(idle) 9(disable)
975          *
976          * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
977          * while( (chState != 0) && (chState != 9)) {
978          *      chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
979          * }
980          */
981
982         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
983
984         delay(2);
985
986         /* Restore aggregation parameters */
987         set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
988         set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
989
990         set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
991         set(AR9170_MAC_REG_CAM_MODE, cam_mode);
992         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
993         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
994
995         set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
996         set(AR9170_MAC_REG_ACK_TPC, ack_power);
997
998 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
999         set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1000 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1001
1002         /*
1003          * Manipulate CCA threshold to resume transmission
1004          *
1005          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1006          */
1007
1008         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1009                 DBG("Q:%d l:%d h:%p t:%p\n", i, queue_len(&fw.wlan.tx_queue[i]),
1010                      fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator);
1011
1012                 set_wlan_txq_dma_addr(i, (uint32_t) fw.wlan.tx_queue[i].head);
1013
1014                 if (!queue_empty(&fw.wlan.tx_queue[i]))
1015                         wlan_trigger(BIT(i));
1016         }
1017
1018         handle_rx();
1019         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1020         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1021 }
1022
1023 void __cold wlan_timer(void)
1024 {
1025         unsigned int cached_mac_reset;
1026
1027         cached_mac_reset = fw.wlan.mac_reset;
1028
1029         /* TX Queue Hang check */
1030         wlan_check_hang();
1031
1032         /* RX Overrun check */
1033         wlan_check_rx_overrun();
1034
1035         if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1036                 wlan_mac_reset();
1037                 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1038         } else {
1039                 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1040                         fw.wlan.mac_reset--;
1041         }
1042 }