carl9170 firmware: outsource obsolete TX DMA bump code
[carl9170fw.git] / carlfw / src / wlan.c
1 /*
2  * carl9170 firmware - used by the ar9170 wireless device
3  *
4  * Interface to the WLAN part of the chip
5  *
6  * Copyright (c) 2000-2005 ZyDAS Technology Corporation
7  * Copyright (c) 2007-2009 Atheros Communications, Inc.
8  * Copyright    2009    Johannes Berg <johannes@sipsolutions.net>
9  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include "carl9170.h"
27 #include "shared/phy.h"
28 #include "hostif.h"
29 #include "timer.h"
30 #include "wl.h"
31 #include "printf.h"
32 #include "rf.h"
33 #include "linux/ieee80211.h"
34
35 static void wlan_txunstuck(unsigned int queue)
36 {
37         set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head) | 1);
38         wlan_trigger(BIT(queue));
39 }
40
41 #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         wlan_trigger(BIT(queue));
46 }
47
48 static void wlan_dma_bump(unsigned int qidx)
49 {
50         unsigned int offset = qidx;
51         uint32_t status, trigger;
52
53         status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
54         trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
55
56         while (offset != 0) {
57                 status >>= 4;
58                 trigger >>= 4;
59                 offset--;
60         }
61
62         status &= 0xf;
63         trigger &= 0xf;
64
65         if ((trigger == 0xa) && (status == 0x8)) {
66                 DBG("UNSTUCK");
67                 wlan_txunstuck(qidx);
68         } else {
69                 DBG("UPDATE");
70                 wlan_txupdate(qidx);
71         }
72 }
73 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
74
75 #ifdef CONFIG_CARL9170FW_DEBUG
76 static void wlan_dump_queue(unsigned int qidx)
77 {
78
79         struct dma_desc *desc;
80         struct carl9170_tx_superframe *super;
81         int entries = 0;
82
83         __for_each_desc(desc, &fw.wlan.tx_queue[qidx]) {
84                 super = get_super(desc);
85                 DBG("%d: %p s:%x c:%x tl:%x ds:%x n:%p l:%p ", entries, desc,
86                     desc->status, desc->ctrl, desc->totalLen,
87                     desc->dataSize, desc->nextAddr, desc->lastAddr);
88
89                 DBG("c:%x tr:%d ri:%d l:%x m:%x p:%x fc:%x",
90                     super->s.cookie, super->s.cnt, super->s.rix,
91                     super->f.hdr.length, super->f.hdr.mac.set,
92                     (unsigned int) le32_to_cpu(super->f.hdr.phy.set),
93                     super->f.data.i3e.frame_control);
94
95                 entries++;
96         }
97
98         desc = get_wlan_txq_addr(qidx);
99
100         DBG("Queue: %d: te:%d td:%d h:%p c:%p t:%p",
101             qidx, entries, queue_len(&fw.wlan.tx_queue[qidx]),
102             fw.wlan.tx_queue[qidx].head,
103             desc, fw.wlan.tx_queue[qidx].terminator);
104
105         DBG("HW: t:%x s:%x ac:%x c:%x",
106             (unsigned int) get(AR9170_MAC_REG_DMA_TRIGGER),
107             (unsigned int) get(AR9170_MAC_REG_DMA_STATUS),
108             (unsigned int) get(AR9170_MAC_REG_AMPDU_COUNT),
109             (unsigned int) get(AR9170_MAC_REG_DMA_TXQX_ADDR_CURR));
110 }
111 #endif /* CONFIG_CARL9170FW_DEBUG */
112
113 static void wlan_send_buffered_tx_status(void)
114 {
115         unsigned int len;
116
117         while (fw.wlan.tx_status_pending) {
118                 len = min((unsigned int)fw.wlan.tx_status_pending,
119                           CARL9170_RSP_TX_STATUS_NUM);
120                 len = min(len, CARL9170_TX_STATUS_NUM - fw.wlan.tx_status_head_idx);
121
122                 /*
123                  * rather than memcpy each individual request into a large buffer,
124                  * we _splice_ them all together.
125                  *
126                  * The only downside is however that we have to be careful around
127                  * the edges of the tx_status_cache.
128                  *
129                  * Note:
130                  * Each tx_status is about 2 bytes. However every command package
131                  * must have a size which is a multiple of 4.
132                  */
133
134                 send_cmd_to_host((len * sizeof(struct carl9170_tx_status) + 3) & ~3,
135                                  CARL9170_RSP_TXCOMP, len, (void *)
136                                  &fw.wlan.tx_status_cache[fw.wlan.tx_status_head_idx]);
137
138                 fw.wlan.tx_status_pending -= len;
139                 fw.wlan.tx_status_head_idx += len;
140                 fw.wlan.tx_status_head_idx %= CARL9170_TX_STATUS_NUM;
141         }
142 }
143
144 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
145 {
146         struct carl9170_tx_status *tmp;
147
148         tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
149         fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
150
151         if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
152                 wlan_send_buffered_tx_status();
153
154         fw.wlan.tx_status_pending++;
155
156         return tmp;
157 }
158
159 /* generate _aggregated_ tx_status for the host */
160 static void wlan_tx_complete(struct carl9170_tx_superframe *super,
161                              bool txs)
162 {
163         struct carl9170_tx_status *status;
164
165         status = wlan_get_tx_status_buffer();
166
167         /*
168          * The *unique* cookie and AC_ID is used by the driver for
169          * frame lookup.
170          */
171         status->cookie = super->s.cookie;
172         status->queue = super->s.queue;
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 /* for all tries */
216 static void __wlan_tx(struct dma_desc *desc)
217 {
218         struct carl9170_tx_superframe *super = get_super(desc);
219 #ifdef CONFIG_CARL9170FW_NORMAL_TX_RX
220         unsigned int queue = super->s.queue;
221 #endif /* CONFIG_CARL9170FW_LOOPBACK */
222
223         if (unlikely(super->s.fill_in_tsf)) {
224                 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
225                 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
226
227                 /*
228                  * Truth be told: this is a hack.
229                  *
230                  * The *real* TSF is definitely going to be higher/older.
231                  * But this hardware emulation code is head and shoulders
232                  * above anything a driver can possibly do.
233                  *
234                  * (even, if it's got an accurate atomic clock source).
235                  */
236
237                 read_tsf(tsf);
238         }
239
240 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
241         wlan_tx_complete(super, true);
242         unhide_super(desc);
243 # ifdef CONFIG_CARL9170FW_LOOPBACK
244         dma_put(&fw.pta.up_queue, desc);
245         up_trigger();
246 # elif CONFIG_CARL9170FW_DISCARD
247         dma_reclaim(&fw.pta.down_queue, desc);
248         down_trigger();
249 # endif
250 #else /* CONFIG_CARL9170FW_LOOPBACK */
251
252 # if ((defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_PSM))
253         BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
254 # endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_PSM */
255
256         /* insert desc into the right queue */
257         dma_put(&fw.wlan.tx_queue[queue], desc);
258         wlan_trigger(BIT(queue));
259 #endif /* CONFIG_CARL9170FW_LOOPBACK */
260 }
261
262 /* prepares frame for the first transmission */
263 static void _wlan_tx(struct dma_desc *desc)
264 {
265         struct carl9170_tx_superframe *super = get_super(desc);
266
267         if (unlikely(super->s.ampdu_commit_density)) {
268                 set(AR9170_MAC_REG_AMPDU_DENSITY,
269                     MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
270                             get(AR9170_MAC_REG_AMPDU_DENSITY),
271                             super->s.ampdu_density));
272         }
273
274         if (unlikely(super->s.ampdu_commit_factor)) {
275                 set(AR9170_MAC_REG_AMPDU_FACTOR,
276                     MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
277                             get(AR9170_MAC_REG_AMPDU_FACTOR),
278                             8 << super->s.ampdu_factor));
279         }
280
281         __wlan_tx(desc);
282 }
283
284 /* propagate transmission status back to the driver */
285 static bool wlan_tx_status(struct dma_queue *queue,
286                            struct dma_desc *desc)
287 {
288         struct ar9170_tx_frame *frame = DESC_PAYLOAD(desc);
289         struct carl9170_tx_superframe *super = get_super(desc);
290         struct ieee80211_hdr *hdr = &super->f.data.i3e;
291         unsigned int qidx = super->s.queue;
292         bool txfail, success;
293
294         success = true;
295
296         /* update hangcheck */
297         fw.wlan.last_tx_desc_num[qidx] = 0;
298
299         if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
300                 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
301
302                 /* reset retry indicator flags */
303                 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
304
305                 if (wlan_tx_consume_retry(super)) {
306                         /*
307                          * retry for simple and aggregated 802.11 frames.
308                          *
309                          * Note: We must not mess up the original frame
310                          * order.
311                          */
312
313                         if (!frame->hdr.mac.ampdu) {
314                                 /*
315                                  * 802.11 - 7.1.3.1.5.
316                                  * set "Retry Field" for consecutive attempts
317                                  *
318                                  * Note: For AMPDU see:
319                                  * 802.11n 9.9.1.6 "Retransmit Procedures"
320                                  */
321
322                                 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
323                         }
324
325                         if (txfail) {
326                                 /* Normal TX Failure */
327
328                                 /* demise descriptor ownership back to the hardware */
329                                 dma_rearm(desc);
330
331                                 /*
332                                  * And this will get the queue going again.
333                                  * To understand why: you have to get the HW
334                                  * specs... But sadly I never saw them.
335                                  */
336                                 wlan_txunstuck(qidx);
337
338                                 /* abort cycle - this is necessary due to HW design */
339                                 return false;
340                         } else {
341                                 /* (HT-) BlockACK failure */
342
343                                 /*
344                                  * Unlink the failed attempt and put it into
345                                  * the retry queue. The caller routine must
346                                  * be aware of this so the frames don't get lost.
347                                  */
348
349                                 dma_unlink_head(queue);
350 #ifdef CONFIG_CARL9170FW_DELAYED_TX
351                                 dma_put(&fw.wlan.tx_retry, desc);
352 #else
353                                 __wlan_tx(desc);
354 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
355                                 return true;
356                         }
357                 } else {
358                         /* out of frame attempts - discard frame */
359                         success = false;
360                 }
361         }
362
363         dma_unlink_head(queue);
364         if (txfail) {
365                 /*
366                  * Issue the queue bump,
367                  * We need to do this in case this was the frame's last
368                  * possible retry attempt and it unfortunately: it failed.
369                  */
370
371                 wlan_txunstuck(qidx);
372         }
373
374         unhide_super(desc);
375
376 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
377         if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
378                 fw.wlan.ba_desc = desc;
379                 fw.wlan.ba_desc_available = 1;
380                 return true;
381         }
382 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
383
384         wlan_tx_complete(super, success);
385
386 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
387         if (unlikely(super->s.cab))
388                 fw.wlan.cab_queue_len[super->s.vif_id]--;
389 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
390
391         /* recycle freed descriptors */
392         dma_reclaim(&fw.pta.down_queue, desc);
393         down_trigger();
394         return true;
395 }
396
397 static void handle_tx_completion(void)
398 {
399         struct dma_desc *desc;
400         unsigned int i;
401
402         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
403                 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
404                         if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
405                                 /* termination requested. */
406                                 break;
407                         }
408                 }
409
410 #ifdef CONFIG_CARL9170FW_DELAYED_TX
411                 for_each_desc(desc, &fw.wlan.tx_retry)
412                         __wlan_tx(desc);
413
414                 for_each_desc(desc, &fw.wlan.tx_delay[i])
415                         _wlan_tx(desc);
416 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
417         }
418 }
419
420 void __hot wlan_tx(struct dma_desc *desc)
421 {
422         struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
423
424         /* initialize rate control struct */
425         super->s.rix = 0;
426         super->s.cnt = 1;
427         hide_super(desc);
428
429 #ifdef CONFIG_CARL9170FW_DELAYED_TX
430         if (!queue_empty(&fw.wlan.tx_queue[super->s.queue])) {
431                 dma_put(&fw.wlan.tx_delay[super->s.queue], desc);
432                 return;
433         }
434 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
435
436 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
437         if (unlikely(super->s.cab)) {
438                 fw.wlan.cab_queue_len[super->s.vif_id]++;
439                 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
440                 return;
441         }
442 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
443
444         _wlan_tx(desc);
445 }
446
447 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
448 static void wlan_send_buffered_ba(void)
449 {
450         struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
451         struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
452         struct carl9170_bar_ctx *ctx;
453
454         if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
455                 return;
456
457         /* there's no point to continue when the ba_desc is not available. */
458         if (!fw.wlan.ba_desc_available)
459                 return;
460
461         ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
462         fw.wlan.ba_head_idx++;
463
464         /* Format BlockAck */
465         fw.wlan.ba_desc->status = 0;
466         fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
467         fw.wlan.ba_desc_available = 0;
468         fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
469                 fw.wlan.ba_desc;
470
471         baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
472                 sizeof(struct carl9170_tx_superdesc) +
473                 sizeof(struct ar9170_tx_hwdesc) +
474                 sizeof(struct ieee80211_ba);
475
476         baf->s.ri[0].tries = 3;
477         baf->s.queue = 0;
478         baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
479
480         /* HW Duration / Backoff */
481         baf->f.hdr.mac.backoff = 1;
482         baf->f.hdr.mac.hw_duration = 1;
483
484         /* take the TX rate from the RX'd BAR */
485         baf->f.hdr.phy.set = ctx->phy;
486         baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
487
488         /* format outgoing BA */
489         ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
490         ba->duration = cpu_to_le16(0);
491         memcpy(ba->ta, ctx->ta, 6);
492         memcpy(ba->ra, ctx->ra, 6);
493
494         /*
495          * Unfortunately, we cannot look into the hardware's scoreboard.
496          * Therefore we have to proceed as described in 802.11n 9.10.7.5
497          * and send a null BlockAck.
498          */
499         memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
500
501         /*
502          * NB:
503          * not entirely sure if this is 100% correct?!
504          */
505         ba->control = ctx->control | cpu_to_le16(1);
506         ba->start_seq_num = ctx->start_seq_num;
507
508         wlan_tx(fw.wlan.ba_desc);
509 }
510
511 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
512 {
513         struct carl9170_bar_ctx *tmp;
514
515         /* expire oldest entry, if we ran out of ba_ctx' */
516         if (fw.wlan.ba_head_idx + CONFIG_CARL9170FW_BACK_REQS_NUM < fw.wlan.ba_tail_idx)
517                 fw.wlan.ba_head_idx++;
518
519         tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
520         fw.wlan.ba_tail_idx++;
521
522         return tmp;
523 }
524
525 static void handle_bar(struct dma_desc *desc)
526 {
527         struct ieee80211_hdr *hdr;
528         struct ieee80211_bar *bar;
529         struct carl9170_bar_ctx *ctx;
530
531         hdr = ar9170_get_rx_i3e(desc);
532
533         /* check if this is a BAR for us */
534         if (likely(!ieee80211_is_back_req(hdr->frame_control)))
535                 return ;
536
537         if (unlikely(ar9170_get_rx_macstatus_error(desc))) {
538                 /*
539                  * This check does a number of things:
540                  * 1. checks if the frame is in good nick
541                  * 2. checks if the RA (MAC) matches
542                  */
543                 return ;
544         }
545
546         if (unlikely(ar9170_get_rx_mpdu_len(desc) <
547             sizeof(struct ieee80211_bar))) {
548                 /*
549                  * Sneaky, corrupted BARs... but not with us!
550                  */
551
552                 return ;
553         }
554
555         bar = (void *) hdr;
556
557         if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
558             !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
559                 /* not implemented yet */
560
561                 return ;
562         }
563
564         ctx = wlan_get_bar_cache_buffer();
565
566         /* Brilliant! The BAR provides all necessary MACs! */
567         memcpy(ctx->ra, bar->ta, 6);
568         memcpy(ctx->ta, bar->ra, 6);
569
570         /*
571          * NB:
572          * not entirely sure if this is 100% correct to force the
573          * imm ack bit or not...
574          */
575         ctx->control = bar->control | cpu_to_le16(1);
576         ctx->start_seq_num = bar->start_seq_num;
577         ctx->phy = ar9170_rx_to_phy(desc);
578         if (unlikely(!ctx->phy)) {
579                 /* provide a backup, in case ar9170_rx_to_phy fails */
580                 ctx->phy = cpu_to_le32(0x2cc301);
581         }
582 }
583 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
584
585 static void wlan_check_rx_overrun(void)
586 {
587         uint32_t overruns, total;
588
589         fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
590         fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
591         if (unlikely(overruns)) {
592                 if (overruns == total) {
593                         DBG("RX Overrun");
594                         fw.wlan.mac_reset++;
595                 }
596
597                 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
598         }
599 }
600
601 static void handle_rx(void)
602 {
603         struct dma_desc *desc;
604
605         for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
606                 if (unlikely(desc->totalLen < 26 ||
607                     desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN)) {
608                         /*
609                          * This frame is too damaged to do anything
610                          * useful with it.
611                          */
612                         dma_reclaim(&fw.wlan.rx_queue, desc);
613                         _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
614                 } else {
615 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
616                         handle_bar(desc);
617 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
618
619                         dma_put(&fw.pta.up_queue, desc);
620                         up_trigger();
621                 }
622         }
623 }
624
625 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
626 void wlan_cab_flush_queue(const unsigned int vif)
627 {
628         struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
629         struct dma_desc *desc;
630
631         /* move queued frames into the main tx queues */
632         for_each_desc(desc, cab_queue) {
633                 struct carl9170_tx_superframe *super = get_super(desc);
634                 if (!queue_empty(cab_queue)) {
635                         /*
636                          * Set MOREDATA flag for all,
637                          * but the last queued frame.
638                          * see: 802.11-2007 11.2.1.5 f)
639                          *
640                          * This is actually the reason to why
641                          * we need to prevent the reentry.
642                          */
643
644                         super->f.data.i3e.frame_control |=
645                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
646                 } else {
647                         super->f.data.i3e.frame_control &=
648                                 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
649                 }
650
651                 /* ready to roll! */
652                 _wlan_tx(desc);
653         }
654 }
655
656 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
657                                const unsigned int len)
658 {
659         struct ieee80211_mgmt *mgmt = addr;
660         uint8_t *pos, *end;
661
662         pos = mgmt->u.beacon.variable;
663         end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
664         while (pos < end) {
665                 if (pos + 2 + pos[1] > end)
666                         return NULL;
667
668                 if (pos[0] == ie)
669                         return pos;
670
671                 pos += pos[1] + 2;
672         }
673
674         return NULL;
675 }
676
677 void wlan_cab_modify_dtim_beacon(const unsigned int vif,
678         const unsigned int addr, const unsigned int len)
679 {
680         uint8_t *_ie;
681         struct ieee80211_tim_ie *ie;
682
683         _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
684         if (likely(_ie)) {
685                 ie = (struct ieee80211_tim_ie *) &_ie[2];
686
687                 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
688                         /* schedule DTIM transfer */
689                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
690                 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
691                         /* undo all chances to the beacon structure */
692                         ie->bitmap_ctrl &= ~0x1;
693                         fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
694                 }
695
696                 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
697                 if (fw.wlan.cab_flush_trigger[vif]) {
698                         /* Set the almighty Multicast Traffic Indication Bit. */
699                         ie->bitmap_ctrl |= 0x1;
700                 }
701         }
702 }
703 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
704
705 static void handle_beacon_config(void)
706 {
707         uint32_t bcn_count;
708
709         bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
710         send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
711                          (uint8_t *) &bcn_count);
712
713         set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
714 }
715
716 static void handle_pretbtt(void)
717 {
718 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
719         fw.wlan.cab_flush_time = get_clock_counter();
720 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
721
722 #ifdef CONFIG_CARL9170FW_PSM
723         rf_psm();
724
725         send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
726                          (uint8_t *) &fw.phy.psm.state);
727 #else
728         send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
729 #endif /* CONFIG_CARL9170FW_PSM */
730 }
731
732 static void handle_atim(void)
733 {
734         send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
735 }
736
737 #ifdef CONFIG_CARL9170FW_DEBUG
738 static void handle_qos(void)
739 {
740         /*
741          * What is the QoS Bit used for?
742          * Is it only an indicator for TXOP & Burst, or
743          * should we do something here?
744          */
745 }
746
747 static void handle_radar(void)
748 {
749         send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
750 }
751 #endif /* CONFIG_CARL9170FW_DEBUG */
752
753 static void wlan_janitor(void)
754 {
755 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
756         unsigned int i;
757
758         for (i = 0; i < CARL9170_INTF_NUM; i++) {
759                 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
760                         /*
761                          * This is hardcoded into carl9170usb driver.
762                          *
763                          * The driver must set the PRETBTT event to beacon_interval -
764                          * CARL9170_PRETBTT_KUS (usually 6) Kus.
765                          *
766                          * But still, we can only do so much about 802.11-2007 9.3.2.1 &
767                          * 11.2.1.6. Let's hope the current solution is adequate enough.
768                          */
769
770                         if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
771                                 wlan_cab_flush_queue(i);
772
773                                 /*
774                                  * This prevents the code from sending new BC/MC frames
775                                  * which were queued after the previous buffered traffic
776                                  * has been sent out... They will have to wait until the
777                                  * next DTIM beacon comes along.
778                                  */
779                                 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
780                         }
781                 }
782
783         }
784 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
785
786 #ifdef CONFIG_CARL9170FW_DELAYED_TX
787         if (fw.wlan.tx_trigger) {
788                 _wlan_trigger(fw.wlan.tx_trigger);
789                 fw.wlan.tx_trigger = 0;
790         }
791 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
792
793         wlan_send_buffered_tx_status();
794
795 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
796         wlan_send_buffered_ba();
797 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
798 }
799
800 void handle_wlan(void)
801 {
802         uint32_t intr;
803
804         intr = get(AR9170_MAC_REG_INT_CTRL);
805         /* ACK Interrupt */
806         set(AR9170_MAC_REG_INT_CTRL, intr);
807
808 #define HANDLER(intr, flag, func)                       \
809         do {                                            \
810                 if ((intr & flag) != 0) {               \
811                         func();                         \
812                 }                                       \
813         } while (0)
814
815         intr |= fw.wlan.soft_int;
816         fw.wlan.soft_int = 0;
817
818         HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
819
820         HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
821
822         HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
823
824         HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
825                 handle_tx_completion);
826
827 #ifdef CONFIG_CARL9170FW_DEBUG
828         HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
829
830         HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
831 #endif /* CONFIG_CARL9170FW_DEBUG */
832
833         HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
834
835         if (unlikely(intr))
836                 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
837
838         wlan_janitor();
839
840 #undef HANDLER
841 }
842
843 static void wlan_check_hang(void)
844 {
845         struct dma_desc *desc;
846         unsigned int i;
847
848         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
849                 if (queue_empty(&fw.wlan.tx_queue[i])) {
850                         /* Nothing to do here... move along */
851                         continue;
852                 }
853
854                 /* fetch the current DMA queue position */
855                 desc = get_wlan_txq_addr(i);
856
857                 /* Stuck frame detection */
858                 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
859                         fw.wlan.last_tx_desc_num[i]++;
860
861                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
862                                 /*
863                                  * schedule MAC reset (aka OFF/ON => dead)
864                                  *
865                                  * This will almost certainly kill
866                                  * the device for good, but it's the
867                                  * recommended thing to do...
868                                  */
869
870                                 fw.wlan.mac_reset++;
871                         }
872
873 #ifdef CONFIG_CARL9170FW_DEBUG
874                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
875                                 /*
876                                  * Sigh, the queue is almost certainly
877                                  * dead. Dump the queue content to the
878                                  * user, maybe we find out why it got
879                                  * so stuck.
880                                  */
881
882                                 wlan_dump_queue(i);
883                         }
884 #endif /* CONFIG_CARL9170FW_DEBUG */
885
886 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
887                         if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
888                                 /*
889                                  * Hrrm, bump the queue a bit.
890                                  * maybe this will get it going again.
891                                  */
892
893                                 wlan_dma_bump(i);
894                         }
895 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
896                 } else {
897                         /* Nothing stuck */
898                         fw.wlan.last_tx_desc[i] = desc;
899                         fw.wlan.last_tx_desc_num[i] = 0;
900                 }
901         }
902 }
903
904 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
905 /*
906  * NB: Resetting the MAC is a two-edged sword.
907  * On most occasions, it does what it is supposed to do.
908  * But there is a chance that this will make it
909  * even worse and the radio dies silently.
910  */
911 static void wlan_mac_reset(void)
912 {
913         uint32_t val;
914         uint32_t agg_wait_counter;
915         uint32_t agg_density;
916         uint32_t bcn_start_addr;
917         uint32_t rctl, rcth;
918         uint32_t cam_mode;
919         uint32_t ack_power;
920         uint32_t rts_cts_tpc;
921         uint32_t rts_cts_rate;
922         unsigned int i;
923
924 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
925         uint32_t rx_BB;
926 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
927
928         INFO("MAC RESET");
929
930         /* Save aggregation parameters */
931         agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
932         agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
933
934         bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
935
936         cam_mode = get(AR9170_MAC_REG_CAM_MODE);
937         rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
938         rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
939
940         ack_power = get(AR9170_MAC_REG_ACK_TPC);
941         rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
942         rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
943
944 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
945         /* 0x1c8960 write only */
946         rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
947 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
948
949         /* TX/RX must be stopped by now */
950         val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
951
952         val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
953
954         /*
955          * Manipulate CCA threshold to stop transmission
956          *
957          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
958          */
959
960         /*
961          * check Rx state in 0(idle) 9(disable)
962          *
963          * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
964          * while( (chState != 0) && (chState != 9)) {
965          *      chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
966          * }
967          */
968
969         set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
970
971         delay(2);
972
973         /* Restore aggregation parameters */
974         set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
975         set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
976
977         set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
978         set(AR9170_MAC_REG_CAM_MODE, cam_mode);
979         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
980         set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
981
982         set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
983         set(AR9170_MAC_REG_ACK_TPC, ack_power);
984         set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
985
986 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
987         set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
988 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
989
990         /*
991          * Manipulate CCA threshold to resume transmission
992          *
993          * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
994          */
995
996         /* Reinitialize all WLAN TX DMA queues. */
997         for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
998                 struct dma_desc *iter;
999
1000                 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1001
1002                 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1003                 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1004                         wlan_trigger(BIT(i));
1005
1006                 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]),
1007                      fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1008                      get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1009         }
1010
1011         fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1012                             AR9170_MAC_INT_RETRY_FAIL;
1013
1014         set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1015         wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1016 }
1017 #else
1018 static void wlan_mac_reset(void)
1019 {
1020         /* The driver takes care of reinitializing the device */
1021         BUG("MAC RESET");
1022 }
1023 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1024
1025 void __cold wlan_timer(void)
1026 {
1027         unsigned int cached_mac_reset;
1028
1029         cached_mac_reset = fw.wlan.mac_reset;
1030
1031         /* TX Queue Hang check */
1032         wlan_check_hang();
1033
1034         /* RX Overrun check */
1035         wlan_check_rx_overrun();
1036
1037         if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1038                 wlan_mac_reset();
1039                 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1040         } else {
1041                 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1042                         fw.wlan.mac_reset--;
1043         }
1044 }