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