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