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