2 * carl9170 firmware - used by the ar9170 wireless device
4 * Interface to the WLAN part of the chip
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>
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.
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.
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.
27 #include "shared/phy.h"
33 #include "linux/ieee80211.h"
35 static void wlan_txunstuck(unsigned int queue)
37 set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head) | 1);
40 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
41 static void wlan_txupdate(unsigned int queue)
43 set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head));
46 static void wlan_dma_bump(unsigned int qidx)
48 unsigned int offset = qidx;
49 uint32_t status, trigger;
51 status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
52 trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
63 if ((trigger == 0xa) && (status == 0x8)) {
71 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
73 #ifdef CONFIG_CARL9170FW_DEBUG
74 static void wlan_dump_queue(unsigned int qidx)
77 struct dma_desc *desc;
78 struct carl9170_tx_superframe *super;
81 __for_each_desc(desc, &fw.wlan.tx_queue[qidx]) {
82 super = get_super(desc);
83 DBG("%d: %p s:%x c:%x tl:%x ds:%x n:%p l:%p ", entries, desc,
84 desc->status, desc->ctrl, desc->totalLen,
85 desc->dataSize, desc->nextAddr, desc->lastAddr);
87 DBG("c:%x tr:%d ri:%d l:%x m:%x p:%x fc:%x",
88 super->s.cookie, super->s.cnt, super->s.rix,
89 super->f.hdr.length, super->f.hdr.mac.set,
90 (unsigned int) le32_to_cpu(super->f.hdr.phy.set),
91 super->f.data.i3e.frame_control);
96 desc = get_wlan_txq_addr(qidx);
98 DBG("Queue: %d: te:%d td:%d h:%p c:%p t:%p",
99 qidx, entries, queue_len(&fw.wlan.tx_queue[qidx]),
100 fw.wlan.tx_queue[qidx].head,
101 desc, fw.wlan.tx_queue[qidx].terminator);
103 DBG("HW: t:%x s:%x ac:%x c:%x",
104 (unsigned int) get(AR9170_MAC_REG_DMA_TRIGGER),
105 (unsigned int) get(AR9170_MAC_REG_DMA_STATUS),
106 (unsigned int) get(AR9170_MAC_REG_AMPDU_COUNT),
107 (unsigned int) get(AR9170_MAC_REG_DMA_TXQX_ADDR_CURR));
109 #endif /* CONFIG_CARL9170FW_DEBUG */
111 static void wlan_send_buffered_tx_status(void)
115 while (fw.wlan.tx_status_pending) {
116 len = min((unsigned int)fw.wlan.tx_status_pending,
117 CARL9170_RSP_TX_STATUS_NUM);
118 len = min(len, CARL9170_TX_STATUS_NUM - fw.wlan.tx_status_head_idx);
121 * rather than memcpy each individual request into a large buffer,
122 * we _splice_ them all together.
124 * The only downside is however that we have to be careful around
125 * the edges of the tx_status_cache.
128 * Each tx_status is about 2 bytes. However every command package
129 * must have a size which is a multiple of 4.
132 send_cmd_to_host((len * sizeof(struct carl9170_tx_status) + 3) & ~3,
133 CARL9170_RSP_TXCOMP, len, (void *)
134 &fw.wlan.tx_status_cache[fw.wlan.tx_status_head_idx]);
136 fw.wlan.tx_status_pending -= len;
137 fw.wlan.tx_status_head_idx += len;
138 fw.wlan.tx_status_head_idx %= CARL9170_TX_STATUS_NUM;
142 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
144 struct carl9170_tx_status *tmp;
146 tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
147 fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
149 if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
150 wlan_send_buffered_tx_status();
152 fw.wlan.tx_status_pending++;
157 /* generate _aggregated_ tx_status for the host */
158 static void wlan_tx_complete(struct carl9170_tx_superframe *super,
161 struct carl9170_tx_status *status;
163 status = wlan_get_tx_status_buffer();
166 * The *unique* cookie and AC_ID is used by the driver for
169 status->cookie = super->s.cookie;
170 status->queue = super->s.queue;
173 * This field holds the number of tries of the rate in
174 * the rate index field (rix).
176 status->rix = super->s.rix;
177 status->tries = super->s.cnt;
178 status->success = (txs) ? 1 : 0;
181 static bool wlan_tx_consume_retry(struct carl9170_tx_superframe *super)
183 /* check if this was the last possible retry with this rate */
184 if (unlikely(super->s.cnt >= super->s.ri[super->s.rix].tries)) {
185 /* end of the road - indicate tx failure */
186 if (unlikely(super->s.rix == CARL9170_TX_MAX_RETRY_RATES))
189 /* check if there are alternative rates available */
190 if (!super->s.rr[super->s.rix].set)
193 /* try next retry rate */
194 super->f.hdr.phy.set = super->s.rr[super->s.rix].set;
196 /* finally - mark the old rate as USED */
199 /* update MAC flags */
200 super->f.hdr.mac.erp_prot = super->s.ri[super->s.rix].erp_prot;
201 super->f.hdr.mac.ampdu = super->s.ri[super->s.rix].ampdu;
203 /* reinitialize try counter */
206 /* just increase retry counter */
213 static inline u16 get_tid(struct ieee80211_hdr *hdr)
215 return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
218 /* This function will only work on uint32_t-aligned pointers! */
219 static inline bool compare_ether_address(const void *_d0, const void *_d1)
221 const uint32_t *d0 = _d0;
222 const uint32_t *d1 = _d1;
224 /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
225 return !((d0[0] ^ d1[0]) | (unsigned short)(d0[1] ^ d1[1]));
228 #ifdef CONFIG_CARL9170FW_TX_AMPDU
229 static void wlan_tx_ampdu(struct carl9170_tx_superframe *super)
231 unsigned int qidx = super->s.queue;
232 struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
234 if (!super->f.hdr.mac.ampdu) {
235 fw.wlan.ampdu_prev[qidx] = NULL;
238 ht_prev->f.hdr.mac.ba_end = 1;
240 fw.wlan.ampdu_prev[qidx] = super;
242 if (ht_prev && (get_tid(&super->f.data.i3e) != get_tid(&ht_prev->f.data.i3e) ||
243 !compare_ether_address(super->f.data.i3e.addr1, ht_prev->f.data.i3e.addr1)))
244 ht_prev->f.hdr.mac.ba_end = 1;
246 super->f.hdr.mac.ba_end = 0;
249 #endif /* CONFIG_CARL9170FW_TX_AMPDU */
252 static void __wlan_tx(struct dma_desc *desc)
254 struct carl9170_tx_superframe *super = get_super(desc);
256 if (unlikely(super->s.fill_in_tsf)) {
257 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
258 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
261 * Truth be told: this is a hack.
263 * The *real* TSF is definitely going to be higher/older.
264 * But this hardware emulation code is head and shoulders
265 * above anything a driver can possibly do.
267 * (even, if it's got an accurate atomic clock source).
273 #ifdef CONFIG_CARL9170FW_TX_AMPDU
274 wlan_tx_ampdu(super);
275 #endif /* CONFIG_CARL9170FW_TX_AMPDU */
277 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
278 wlan_tx_complete(super, true);
280 # ifdef CONFIG_CARL9170FW_LOOPBACK
281 dma_put(&fw.pta.up_queue, desc);
283 # elif CONFIG_CARL9170FW_DISCARD
284 dma_reclaim(&fw.pta.down_queue, desc);
287 #else /* CONFIG_CARL9170FW_LOOPBACK */
289 # if ((defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_PSM))
290 BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
291 # endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_PSM */
293 /* insert desc into the right queue */
294 dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
295 #endif /* CONFIG_CARL9170FW_LOOPBACK */
298 /* prepares frame for the first transmission */
299 static void _wlan_tx(struct dma_desc *desc)
301 struct carl9170_tx_superframe *super = get_super(desc);
303 if (unlikely(super->s.ampdu_commit_density)) {
304 set(AR9170_MAC_REG_AMPDU_DENSITY,
305 MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
306 get(AR9170_MAC_REG_AMPDU_DENSITY),
307 super->s.ampdu_density));
310 if (unlikely(super->s.ampdu_commit_factor)) {
311 set(AR9170_MAC_REG_AMPDU_FACTOR,
312 MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
313 get(AR9170_MAC_REG_AMPDU_FACTOR),
314 8 << super->s.ampdu_factor));
320 /* propagate transmission status back to the driver */
321 static bool wlan_tx_status(struct dma_queue *queue,
322 struct dma_desc *desc)
324 struct ar9170_tx_frame *frame = DESC_PAYLOAD(desc);
325 struct carl9170_tx_superframe *super = get_super(desc);
326 struct ieee80211_hdr *hdr = &super->f.data.i3e;
327 unsigned int qidx = super->s.queue;
328 bool txfail, success;
332 /* update hangcheck */
333 fw.wlan.last_tx_desc_num[qidx] = 0;
335 if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
336 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
338 /* reset retry indicator flags */
339 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
341 if (wlan_tx_consume_retry(super)) {
343 * retry for simple and aggregated 802.11 frames.
345 * Note: We must not mess up the original frame
349 if (!frame->hdr.mac.ampdu) {
351 * 802.11 - 7.1.3.1.5.
352 * set "Retry Field" for consecutive attempts
354 * Note: For AMPDU see:
355 * 802.11n 9.9.1.6 "Retransmit Procedures"
358 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
362 /* Normal TX Failure */
364 /* demise descriptor ownership back to the hardware */
368 * And this will get the queue going again.
369 * To understand why: you have to get the HW
370 * specs... But sadly I never saw them.
372 wlan_txunstuck(qidx);
374 /* abort cycle - this is necessary due to HW design */
377 /* (HT-) BlockACK failure */
380 * Unlink the failed attempt and put it into
381 * the retry queue. The caller routine must
382 * be aware of this so the frames don't get lost.
385 dma_unlink_head(queue);
386 #ifdef CONFIG_CARL9170FW_DELAYED_TX
387 dma_put(&fw.wlan.tx_retry, desc);
390 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
394 /* out of frame attempts - discard frame */
399 dma_unlink_head(queue);
402 * Issue the queue bump,
403 * We need to do this in case this was the frame's last
404 * possible retry attempt and it unfortunately: it failed.
407 wlan_txunstuck(qidx);
412 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
413 if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
414 fw.wlan.ba_desc = desc;
415 fw.wlan.ba_desc_available = 1;
418 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
420 wlan_tx_complete(super, success);
422 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
423 if (unlikely(super->s.cab))
424 fw.wlan.cab_queue_len[super->s.vif_id]--;
425 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
427 /* recycle freed descriptors */
428 dma_reclaim(&fw.pta.down_queue, desc);
433 static void handle_tx_completion(void)
435 struct dma_desc *desc;
438 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
439 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
440 if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
441 /* termination requested. */
446 #ifdef CONFIG_CARL9170FW_DELAYED_TX
447 for_each_desc(desc, &fw.wlan.tx_retry)
450 for_each_desc(desc, &fw.wlan.tx_delay[i])
452 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
453 wlan_trigger(BIT(i));
457 void __hot wlan_tx(struct dma_desc *desc)
459 struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
461 /* initialize rate control struct */
466 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
467 if (unlikely(super->s.cab)) {
468 fw.wlan.cab_queue_len[super->s.vif_id]++;
469 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
472 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
474 #ifdef CONFIG_CARL9170FW_DELAYED_TX
475 if (!queue_empty(&fw.wlan.tx_queue[super->s.queue])) {
476 dma_put(&fw.wlan.tx_delay[super->s.queue], desc);
479 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
482 wlan_trigger(BIT(super->s.queue));
485 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
486 static void wlan_send_buffered_ba(void)
488 struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
489 struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
490 struct carl9170_bar_ctx *ctx;
492 if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
495 /* there's no point to continue when the ba_desc is not available. */
496 if (!fw.wlan.ba_desc_available)
499 ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
500 fw.wlan.ba_head_idx++;
502 /* Format BlockAck */
503 fw.wlan.ba_desc->status = 0;
504 fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
505 fw.wlan.ba_desc_available = 0;
506 fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
509 baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
510 sizeof(struct carl9170_tx_superdesc) +
511 sizeof(struct ar9170_tx_hwdesc) +
512 sizeof(struct ieee80211_ba);
514 baf->s.ri[0].tries = 3;
516 baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
518 /* HW Duration / Backoff */
519 baf->f.hdr.mac.backoff = 1;
520 baf->f.hdr.mac.hw_duration = 1;
522 /* take the TX rate from the RX'd BAR */
523 baf->f.hdr.phy.set = ctx->phy;
524 baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
526 /* format outgoing BA */
527 ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
528 ba->duration = cpu_to_le16(0);
529 memcpy(ba->ta, ctx->ta, 6);
530 memcpy(ba->ra, ctx->ra, 6);
533 * Unfortunately, we cannot look into the hardware's scoreboard.
534 * Therefore we have to proceed as described in 802.11n 9.10.7.5
535 * and send a null BlockAck.
537 memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
541 * not entirely sure if this is 100% correct?!
543 ba->control = ctx->control | cpu_to_le16(1);
544 ba->start_seq_num = ctx->start_seq_num;
546 wlan_tx(fw.wlan.ba_desc);
549 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
551 struct carl9170_bar_ctx *tmp;
553 /* expire oldest entry, if we ran out of ba_ctx' */
554 if (fw.wlan.ba_head_idx + CONFIG_CARL9170FW_BACK_REQS_NUM < fw.wlan.ba_tail_idx)
555 fw.wlan.ba_head_idx++;
557 tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
558 fw.wlan.ba_tail_idx++;
563 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
564 unsigned int len, unsigned int mac_err)
566 struct ieee80211_bar *bar;
567 struct carl9170_bar_ctx *ctx;
569 if (unlikely(mac_err)) {
571 * This check does a number of things:
572 * 1. checks if the frame is in good nick
573 * 2. checks if the RA (MAC) matches
578 if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
580 * Sneaky, corrupted BARs... but not with us!
588 if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
589 !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
590 /* not implemented yet */
595 ctx = wlan_get_bar_cache_buffer();
597 /* Brilliant! The BAR provides all necessary MACs! */
598 memcpy(ctx->ra, bar->ta, 6);
599 memcpy(ctx->ta, bar->ra, 6);
603 * not entirely sure if this is 100% correct to force the
604 * imm ack bit or not...
606 ctx->control = bar->control | cpu_to_le16(1);
607 ctx->start_seq_num = bar->start_seq_num;
608 ctx->phy = ar9170_rx_to_phy(desc);
609 if (unlikely(!ctx->phy)) {
610 /* provide a backup, in case ar9170_rx_to_phy fails */
611 ctx->phy = cpu_to_le32(0x2cc301);
614 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
616 static void wlan_check_rx_overrun(void)
618 uint32_t overruns, total;
620 fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
621 fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
622 if (unlikely(overruns)) {
623 if (overruns == total) {
628 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
632 static unsigned int wlan_rx_filter(struct dma_desc *desc)
634 struct ieee80211_hdr *hdr;
635 unsigned int data_len;
636 unsigned int rx_filter;
637 unsigned int mac_err;
639 data_len = ar9170_get_rx_mpdu_len(desc);
640 mac_err = ar9170_get_rx_macstatus_error(desc);
642 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
644 if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
645 desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
646 mac_err & AR9170_RX_ERROR_BAD) {
648 * This frame is too damaged to do anything
652 return CARL9170_RX_FILTER_BAD;
656 if (mac_err & AR9170_RX_ERROR_WRONG_RA)
657 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
659 if (mac_err & AR9170_RX_ERROR_DECRYPT)
660 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
662 hdr = ar9170_get_rx_i3e(desc);
663 if (likely(ieee80211_is_data(hdr->frame_control))) {
664 rx_filter |= CARL9170_RX_FILTER_DATA;
665 } else if (ieee80211_is_ctl(hdr->frame_control)) {
666 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
667 case IEEE80211_STYPE_BACK_REQ:
668 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
669 handle_bar(desc, hdr, data_len, mac_err);
670 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
672 rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
674 case IEEE80211_STYPE_PSPOLL:
675 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
678 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
682 /* ieee80211_is_mgmt */
683 rx_filter |= CARL9170_RX_FILTER_MGMT;
686 #undef AR9170_RX_ERROR_BAD
691 static void handle_rx(void)
693 struct dma_desc *desc;
695 for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
696 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
697 dma_put(&fw.pta.up_queue, desc);
700 dma_reclaim(&fw.wlan.rx_queue, desc);
701 _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
706 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
707 void wlan_cab_flush_queue(const unsigned int vif)
709 struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
710 struct dma_desc *desc;
712 /* move queued frames into the main tx queues */
713 for_each_desc(desc, cab_queue) {
714 struct carl9170_tx_superframe *super = get_super(desc);
715 if (!queue_empty(cab_queue)) {
717 * Set MOREDATA flag for all,
718 * but the last queued frame.
719 * see: 802.11-2007 11.2.1.5 f)
721 * This is actually the reason to why
722 * we need to prevent the reentry.
725 super->f.data.i3e.frame_control |=
726 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
728 super->f.data.i3e.frame_control &=
729 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
734 wlan_trigger(BIT(super->s.queue));
738 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
739 const unsigned int len)
741 struct ieee80211_mgmt *mgmt = addr;
744 pos = mgmt->u.beacon.variable;
745 end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
747 if (pos + 2 + pos[1] > end)
759 void wlan_cab_modify_dtim_beacon(const unsigned int vif,
760 const unsigned int addr, const unsigned int len)
763 struct ieee80211_tim_ie *ie;
765 _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
767 ie = (struct ieee80211_tim_ie *) &_ie[2];
769 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
770 /* schedule DTIM transfer */
771 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
772 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
773 /* undo all chances to the beacon structure */
774 ie->bitmap_ctrl &= ~0x1;
775 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
778 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
779 if (fw.wlan.cab_flush_trigger[vif]) {
780 /* Set the almighty Multicast Traffic Indication Bit. */
781 ie->bitmap_ctrl |= 0x1;
785 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
787 static void handle_beacon_config(void)
791 bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
792 send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
793 (uint8_t *) &bcn_count);
795 set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
798 static void handle_pretbtt(void)
800 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
801 fw.wlan.cab_flush_time = get_clock_counter();
802 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
804 #ifdef CONFIG_CARL9170FW_PSM
807 send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
808 (uint8_t *) &fw.phy.psm.state);
810 send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
811 #endif /* CONFIG_CARL9170FW_PSM */
814 static void handle_atim(void)
816 send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
819 #ifdef CONFIG_CARL9170FW_DEBUG
820 static void handle_qos(void)
823 * What is the QoS Bit used for?
824 * Is it only an indicator for TXOP & Burst, or
825 * should we do something here?
829 static void handle_radar(void)
831 send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
833 #endif /* CONFIG_CARL9170FW_DEBUG */
835 static void wlan_janitor(void)
837 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
840 for (i = 0; i < CARL9170_INTF_NUM; i++) {
841 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
843 * This is hardcoded into carl9170usb driver.
845 * The driver must set the PRETBTT event to beacon_interval -
846 * CARL9170_PRETBTT_KUS (usually 6) Kus.
848 * But still, we can only do so much about 802.11-2007 9.3.2.1 &
849 * 11.2.1.6. Let's hope the current solution is adequate enough.
852 if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
853 wlan_cab_flush_queue(i);
856 * This prevents the code from sending new BC/MC frames
857 * which were queued after the previous buffered traffic
858 * has been sent out... They will have to wait until the
859 * next DTIM beacon comes along.
861 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
866 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
868 #ifdef CONFIG_CARL9170FW_DELAYED_TX
869 if (fw.wlan.tx_trigger) {
870 _wlan_trigger(fw.wlan.tx_trigger);
871 fw.wlan.tx_trigger = 0;
873 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
875 wlan_send_buffered_tx_status();
877 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
878 wlan_send_buffered_ba();
879 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
882 void handle_wlan(void)
886 intr = get(AR9170_MAC_REG_INT_CTRL);
888 set(AR9170_MAC_REG_INT_CTRL, intr);
890 #define HANDLER(intr, flag, func) \
892 if ((intr & flag) != 0) { \
897 intr |= fw.wlan.soft_int;
898 fw.wlan.soft_int = 0;
900 HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
902 HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
904 HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
906 HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
907 handle_tx_completion);
909 #ifdef CONFIG_CARL9170FW_DEBUG
910 HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
912 HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
913 #endif /* CONFIG_CARL9170FW_DEBUG */
915 HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
918 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
925 static void wlan_check_hang(void)
927 struct dma_desc *desc;
930 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
931 if (queue_empty(&fw.wlan.tx_queue[i])) {
932 /* Nothing to do here... move along */
936 /* fetch the current DMA queue position */
937 desc = get_wlan_txq_addr(i);
939 /* Stuck frame detection */
940 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
941 fw.wlan.last_tx_desc_num[i]++;
943 if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
945 * schedule MAC reset (aka OFF/ON => dead)
947 * This will almost certainly kill
948 * the device for good, but it's the
949 * recommended thing to do...
955 #ifdef CONFIG_CARL9170FW_DEBUG
956 if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
958 * Sigh, the queue is almost certainly
959 * dead. Dump the queue content to the
960 * user, maybe we find out why it got
966 #endif /* CONFIG_CARL9170FW_DEBUG */
968 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
969 if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
971 * Hrrm, bump the queue a bit.
972 * maybe this will get it going again.
976 wlan_trigger(BIT(i));
978 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
981 fw.wlan.last_tx_desc[i] = desc;
982 fw.wlan.last_tx_desc_num[i] = 0;
987 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
989 * NB: Resetting the MAC is a two-edged sword.
990 * On most occasions, it does what it is supposed to do.
991 * But there is a chance that this will make it
992 * even worse and the radio dies silently.
994 static void wlan_mac_reset(void)
997 uint32_t agg_wait_counter;
998 uint32_t agg_density;
999 uint32_t bcn_start_addr;
1000 uint32_t rctl, rcth;
1003 uint32_t rts_cts_tpc;
1004 uint32_t rts_cts_rate;
1007 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1009 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1013 /* Save aggregation parameters */
1014 agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
1015 agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
1017 bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
1019 cam_mode = get(AR9170_MAC_REG_CAM_MODE);
1020 rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
1021 rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
1023 ack_power = get(AR9170_MAC_REG_ACK_TPC);
1024 rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
1025 rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
1027 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1028 /* 0x1c8960 write only */
1029 rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
1030 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1032 /* TX/RX must be stopped by now */
1033 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
1035 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
1038 * Manipulate CCA threshold to stop transmission
1040 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1044 * check Rx state in 0(idle) 9(disable)
1046 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1047 * while( (chState != 0) && (chState != 9)) {
1048 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1052 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1056 /* Restore aggregation parameters */
1057 set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1058 set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1060 set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1061 set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1062 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1063 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1065 set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1066 set(AR9170_MAC_REG_ACK_TPC, ack_power);
1067 set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1069 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1070 set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1071 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1074 * Manipulate CCA threshold to resume transmission
1076 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1079 /* Reinitialize all WLAN TX DMA queues. */
1080 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1081 struct dma_desc *iter;
1083 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1085 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1086 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1087 wlan_trigger(BIT(i));
1089 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]),
1090 fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1091 get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1094 fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1095 AR9170_MAC_INT_RETRY_FAIL;
1097 set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1098 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1101 static void wlan_mac_reset(void)
1103 /* The driver takes care of reinitializing the device */
1106 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1108 void __cold wlan_timer(void)
1110 unsigned int cached_mac_reset;
1112 cached_mac_reset = fw.wlan.mac_reset;
1114 /* TX Queue Hang check */
1117 /* RX Overrun check */
1118 wlan_check_rx_overrun();
1120 if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1122 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1124 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1125 fw.wlan.mac_reset--;