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);
38 wlan_trigger(BIT(queue));
41 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
42 static void wlan_txupdate(unsigned int queue)
44 set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head));
45 wlan_trigger(BIT(queue));
48 static void wlan_dma_bump(unsigned int qidx)
50 unsigned int offset = qidx;
51 uint32_t status, trigger;
53 status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
54 trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
65 if ((trigger == 0xa) && (status == 0x8)) {
73 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
75 #ifdef CONFIG_CARL9170FW_DEBUG
76 static void wlan_dump_queue(unsigned int qidx)
79 struct dma_desc *desc;
80 struct carl9170_tx_superframe *super;
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);
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);
98 desc = get_wlan_txq_addr(qidx);
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);
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));
111 #endif /* CONFIG_CARL9170FW_DEBUG */
113 static void wlan_send_buffered_tx_status(void)
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);
123 * rather than memcpy each individual request into a large buffer,
124 * we _splice_ them all together.
126 * The only downside is however that we have to be careful around
127 * the edges of the tx_status_cache.
130 * Each tx_status is about 2 bytes. However every command package
131 * must have a size which is a multiple of 4.
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]);
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;
144 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
146 struct carl9170_tx_status *tmp;
148 tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
149 fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
151 if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
152 wlan_send_buffered_tx_status();
154 fw.wlan.tx_status_pending++;
159 /* generate _aggregated_ tx_status for the host */
160 static void wlan_tx_complete(struct carl9170_tx_superframe *super,
163 struct carl9170_tx_status *status;
165 status = wlan_get_tx_status_buffer();
168 * The *unique* cookie and AC_ID is used by the driver for
171 status->cookie = super->s.cookie;
172 status->queue = super->s.queue;
175 * This field holds the number of tries of the rate in
176 * the rate index field (rix).
178 status->rix = super->s.rix;
179 status->tries = super->s.cnt;
180 status->success = (txs) ? 1 : 0;
183 static bool wlan_tx_consume_retry(struct carl9170_tx_superframe *super)
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))
191 /* check if there are alternative rates available */
192 if (!super->s.rr[super->s.rix].set)
195 /* try next retry rate */
196 super->f.hdr.phy.set = super->s.rr[super->s.rix].set;
198 /* finally - mark the old rate as USED */
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;
205 /* reinitialize try counter */
208 /* just increase retry counter */
216 static void __wlan_tx(struct dma_desc *desc)
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 */
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;
228 * Truth be told: this is a hack.
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.
234 * (even, if it's got an accurate atomic clock source).
240 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
241 wlan_tx_complete(super, true);
243 # ifdef CONFIG_CARL9170FW_LOOPBACK
244 dma_put(&fw.pta.up_queue, desc);
246 # elif CONFIG_CARL9170FW_DISCARD
247 dma_reclaim(&fw.pta.down_queue, desc);
250 #else /* CONFIG_CARL9170FW_LOOPBACK */
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 */
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 */
262 /* prepares frame for the first transmission */
263 static void _wlan_tx(struct dma_desc *desc)
265 struct carl9170_tx_superframe *super = get_super(desc);
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));
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));
284 /* propagate transmission status back to the driver */
285 static bool wlan_tx_status(struct dma_queue *queue,
286 struct dma_desc *desc)
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;
296 /* update hangcheck */
297 fw.wlan.last_tx_desc_num[qidx] = 0;
299 if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
300 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
302 /* reset retry indicator flags */
303 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
305 if (wlan_tx_consume_retry(super)) {
307 * retry for simple and aggregated 802.11 frames.
309 * Note: We must not mess up the original frame
313 if (!frame->hdr.mac.ampdu) {
315 * 802.11 - 7.1.3.1.5.
316 * set "Retry Field" for consecutive attempts
318 * Note: For AMPDU see:
319 * 802.11n 9.9.1.6 "Retransmit Procedures"
322 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
326 /* Normal TX Failure */
328 /* demise descriptor ownership back to the hardware */
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.
336 wlan_txunstuck(qidx);
338 /* abort cycle - this is necessary due to HW design */
341 /* (HT-) BlockACK failure */
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.
349 dma_unlink_head(queue);
350 #ifdef CONFIG_CARL9170FW_DELAYED_TX
351 dma_put(&fw.wlan.tx_retry, desc);
354 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
358 /* out of frame attempts - discard frame */
363 dma_unlink_head(queue);
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.
371 wlan_txunstuck(qidx);
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;
382 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
384 wlan_tx_complete(super, success);
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 */
391 /* recycle freed descriptors */
392 dma_reclaim(&fw.pta.down_queue, desc);
397 static void handle_tx_completion(void)
399 struct dma_desc *desc;
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. */
410 #ifdef CONFIG_CARL9170FW_DELAYED_TX
411 for_each_desc(desc, &fw.wlan.tx_retry)
414 for_each_desc(desc, &fw.wlan.tx_delay[i])
416 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
420 void __hot wlan_tx(struct dma_desc *desc)
422 struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
424 /* initialize rate control struct */
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);
434 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
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);
442 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
447 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
448 static void wlan_send_buffered_ba(void)
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;
454 if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
457 /* there's no point to continue when the ba_desc is not available. */
458 if (!fw.wlan.ba_desc_available)
461 ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
462 fw.wlan.ba_head_idx++;
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 =
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);
476 baf->s.ri[0].tries = 3;
478 baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
480 /* HW Duration / Backoff */
481 baf->f.hdr.mac.backoff = 1;
482 baf->f.hdr.mac.hw_duration = 1;
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 */
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);
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.
499 memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
503 * not entirely sure if this is 100% correct?!
505 ba->control = ctx->control | cpu_to_le16(1);
506 ba->start_seq_num = ctx->start_seq_num;
508 wlan_tx(fw.wlan.ba_desc);
511 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
513 struct carl9170_bar_ctx *tmp;
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++;
519 tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
520 fw.wlan.ba_tail_idx++;
525 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
526 unsigned int len, unsigned int mac_err)
528 struct ieee80211_bar *bar;
529 struct carl9170_bar_ctx *ctx;
531 if (unlikely(mac_err)) {
533 * This check does a number of things:
534 * 1. checks if the frame is in good nick
535 * 2. checks if the RA (MAC) matches
540 if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
542 * Sneaky, corrupted BARs... but not with us!
550 if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
551 !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
552 /* not implemented yet */
557 ctx = wlan_get_bar_cache_buffer();
559 /* Brilliant! The BAR provides all necessary MACs! */
560 memcpy(ctx->ra, bar->ta, 6);
561 memcpy(ctx->ta, bar->ra, 6);
565 * not entirely sure if this is 100% correct to force the
566 * imm ack bit or not...
568 ctx->control = bar->control | cpu_to_le16(1);
569 ctx->start_seq_num = bar->start_seq_num;
570 ctx->phy = ar9170_rx_to_phy(desc);
571 if (unlikely(!ctx->phy)) {
572 /* provide a backup, in case ar9170_rx_to_phy fails */
573 ctx->phy = cpu_to_le32(0x2cc301);
576 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
578 static void wlan_check_rx_overrun(void)
580 uint32_t overruns, total;
582 fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
583 fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
584 if (unlikely(overruns)) {
585 if (overruns == total) {
590 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
594 static unsigned int wlan_rx_filter(struct dma_desc *desc)
596 struct ieee80211_hdr *hdr;
597 unsigned int data_len;
598 unsigned int rx_filter;
599 unsigned int mac_err;
601 data_len = ar9170_get_rx_mpdu_len(desc);
602 mac_err = ar9170_get_rx_macstatus_error(desc);
604 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
606 if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
607 desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
608 mac_err & AR9170_RX_ERROR_BAD) {
610 * This frame is too damaged to do anything
614 return CARL9170_RX_FILTER_BAD;
618 if (mac_err & AR9170_RX_ERROR_WRONG_RA)
619 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
621 if (mac_err & AR9170_RX_ERROR_DECRYPT)
622 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
624 hdr = ar9170_get_rx_i3e(desc);
625 if (likely(ieee80211_is_data(hdr->frame_control))) {
626 rx_filter |= CARL9170_RX_FILTER_DATA;
627 } else if (ieee80211_is_ctl(hdr->frame_control)) {
628 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
629 case IEEE80211_STYPE_BACK_REQ:
630 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
631 handle_bar(desc, hdr, data_len, mac_err);
632 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
634 rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
636 case IEEE80211_STYPE_PSPOLL:
637 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
640 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
644 /* ieee80211_is_mgmt */
645 rx_filter |= CARL9170_RX_FILTER_MGMT;
648 #undef AR9170_RX_ERROR_BAD
653 static void handle_rx(void)
655 struct dma_desc *desc;
657 for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
658 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
659 dma_put(&fw.pta.up_queue, desc);
662 dma_reclaim(&fw.wlan.rx_queue, desc);
663 _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
668 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
669 void wlan_cab_flush_queue(const unsigned int vif)
671 struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
672 struct dma_desc *desc;
674 /* move queued frames into the main tx queues */
675 for_each_desc(desc, cab_queue) {
676 struct carl9170_tx_superframe *super = get_super(desc);
677 if (!queue_empty(cab_queue)) {
679 * Set MOREDATA flag for all,
680 * but the last queued frame.
681 * see: 802.11-2007 11.2.1.5 f)
683 * This is actually the reason to why
684 * we need to prevent the reentry.
687 super->f.data.i3e.frame_control |=
688 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
690 super->f.data.i3e.frame_control &=
691 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
699 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
700 const unsigned int len)
702 struct ieee80211_mgmt *mgmt = addr;
705 pos = mgmt->u.beacon.variable;
706 end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
708 if (pos + 2 + pos[1] > end)
720 void wlan_cab_modify_dtim_beacon(const unsigned int vif,
721 const unsigned int addr, const unsigned int len)
724 struct ieee80211_tim_ie *ie;
726 _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
728 ie = (struct ieee80211_tim_ie *) &_ie[2];
730 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
731 /* schedule DTIM transfer */
732 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
733 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
734 /* undo all chances to the beacon structure */
735 ie->bitmap_ctrl &= ~0x1;
736 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
739 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
740 if (fw.wlan.cab_flush_trigger[vif]) {
741 /* Set the almighty Multicast Traffic Indication Bit. */
742 ie->bitmap_ctrl |= 0x1;
746 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
748 static void handle_beacon_config(void)
752 bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
753 send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
754 (uint8_t *) &bcn_count);
756 set(AR9170_MAC_REG_BCN_CTRL, AR9170_BCN_CTRL_READY);
759 static void handle_pretbtt(void)
761 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
762 fw.wlan.cab_flush_time = get_clock_counter();
763 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
765 #ifdef CONFIG_CARL9170FW_PSM
768 send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
769 (uint8_t *) &fw.phy.psm.state);
771 send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
772 #endif /* CONFIG_CARL9170FW_PSM */
775 static void handle_atim(void)
777 send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
780 #ifdef CONFIG_CARL9170FW_DEBUG
781 static void handle_qos(void)
784 * What is the QoS Bit used for?
785 * Is it only an indicator for TXOP & Burst, or
786 * should we do something here?
790 static void handle_radar(void)
792 send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
794 #endif /* CONFIG_CARL9170FW_DEBUG */
796 static void wlan_janitor(void)
798 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
801 for (i = 0; i < CARL9170_INTF_NUM; i++) {
802 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
804 * This is hardcoded into carl9170usb driver.
806 * The driver must set the PRETBTT event to beacon_interval -
807 * CARL9170_PRETBTT_KUS (usually 6) Kus.
809 * But still, we can only do so much about 802.11-2007 9.3.2.1 &
810 * 11.2.1.6. Let's hope the current solution is adequate enough.
813 if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
814 wlan_cab_flush_queue(i);
817 * This prevents the code from sending new BC/MC frames
818 * which were queued after the previous buffered traffic
819 * has been sent out... They will have to wait until the
820 * next DTIM beacon comes along.
822 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
827 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
829 #ifdef CONFIG_CARL9170FW_DELAYED_TX
830 if (fw.wlan.tx_trigger) {
831 _wlan_trigger(fw.wlan.tx_trigger);
832 fw.wlan.tx_trigger = 0;
834 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
836 wlan_send_buffered_tx_status();
838 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
839 wlan_send_buffered_ba();
840 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
843 void handle_wlan(void)
847 intr = get(AR9170_MAC_REG_INT_CTRL);
849 set(AR9170_MAC_REG_INT_CTRL, intr);
851 #define HANDLER(intr, flag, func) \
853 if ((intr & flag) != 0) { \
858 intr |= fw.wlan.soft_int;
859 fw.wlan.soft_int = 0;
861 HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
863 HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
865 HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
867 HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
868 handle_tx_completion);
870 #ifdef CONFIG_CARL9170FW_DEBUG
871 HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
873 HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
874 #endif /* CONFIG_CARL9170FW_DEBUG */
876 HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
879 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
886 static void wlan_check_hang(void)
888 struct dma_desc *desc;
891 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
892 if (queue_empty(&fw.wlan.tx_queue[i])) {
893 /* Nothing to do here... move along */
897 /* fetch the current DMA queue position */
898 desc = get_wlan_txq_addr(i);
900 /* Stuck frame detection */
901 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
902 fw.wlan.last_tx_desc_num[i]++;
904 if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
906 * schedule MAC reset (aka OFF/ON => dead)
908 * This will almost certainly kill
909 * the device for good, but it's the
910 * recommended thing to do...
916 #ifdef CONFIG_CARL9170FW_DEBUG
917 if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
919 * Sigh, the queue is almost certainly
920 * dead. Dump the queue content to the
921 * user, maybe we find out why it got
927 #endif /* CONFIG_CARL9170FW_DEBUG */
929 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
930 if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
932 * Hrrm, bump the queue a bit.
933 * maybe this will get it going again.
938 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
941 fw.wlan.last_tx_desc[i] = desc;
942 fw.wlan.last_tx_desc_num[i] = 0;
947 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
949 * NB: Resetting the MAC is a two-edged sword.
950 * On most occasions, it does what it is supposed to do.
951 * But there is a chance that this will make it
952 * even worse and the radio dies silently.
954 static void wlan_mac_reset(void)
957 uint32_t agg_wait_counter;
958 uint32_t agg_density;
959 uint32_t bcn_start_addr;
963 uint32_t rts_cts_tpc;
964 uint32_t rts_cts_rate;
967 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
969 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
973 /* Save aggregation parameters */
974 agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
975 agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
977 bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
979 cam_mode = get(AR9170_MAC_REG_CAM_MODE);
980 rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
981 rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
983 ack_power = get(AR9170_MAC_REG_ACK_TPC);
984 rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
985 rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
987 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
988 /* 0x1c8960 write only */
989 rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
990 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
992 /* TX/RX must be stopped by now */
993 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
995 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
998 * Manipulate CCA threshold to stop transmission
1000 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1004 * check Rx state in 0(idle) 9(disable)
1006 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1007 * while( (chState != 0) && (chState != 9)) {
1008 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1012 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1016 /* Restore aggregation parameters */
1017 set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1018 set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1020 set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1021 set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1022 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1023 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1025 set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1026 set(AR9170_MAC_REG_ACK_TPC, ack_power);
1027 set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1029 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1030 set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1031 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1034 * Manipulate CCA threshold to resume transmission
1036 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1039 /* Reinitialize all WLAN TX DMA queues. */
1040 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1041 struct dma_desc *iter;
1043 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1045 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1046 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1047 wlan_trigger(BIT(i));
1049 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]),
1050 fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1051 get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1054 fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1055 AR9170_MAC_INT_RETRY_FAIL;
1057 set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1058 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1061 static void wlan_mac_reset(void)
1063 /* The driver takes care of reinitializing the device */
1066 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1068 void __cold wlan_timer(void)
1070 unsigned int cached_mac_reset;
1072 cached_mac_reset = fw.wlan.mac_reset;
1074 /* TX Queue Hang check */
1077 /* RX Overrun check */
1078 wlan_check_rx_overrun();
1080 if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1082 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1084 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1085 fw.wlan.mac_reset--;