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