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