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-2011 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"
36 static void wlan_txunstuck(unsigned int queue)
38 set_wlan_txq_dma_addr(queue, ((uint32_t) fw.wlan.tx_queue[queue].head) | 1);
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));
47 static void wlan_dma_bump(unsigned int qidx)
49 unsigned int offset = qidx;
50 uint32_t status, trigger;
52 status = get(AR9170_MAC_REG_DMA_STATUS) >> 12;
53 trigger = get(AR9170_MAC_REG_DMA_TRIGGER) >> 12;
64 if ((trigger == 0xa) && (status == 0x8)) {
72 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
74 #ifdef CONFIG_CARL9170FW_DEBUG
75 static void wlan_dump_queue(unsigned int qidx)
78 struct dma_desc *desc;
79 struct carl9170_tx_superframe *super;
82 __for_each_desc(desc, &fw.wlan.tx_queue[qidx]) {
83 super = get_super(desc);
84 DBG("%d: %p s:%x c:%x tl:%x ds:%x n:%p l:%p ", entries, desc,
85 desc->status, desc->ctrl, desc->totalLen,
86 desc->dataSize, desc->nextAddr, desc->lastAddr);
88 DBG("c:%x tr:%d ri:%d l:%x m:%x p:%x fc:%x",
89 super->s.cookie, super->s.cnt, super->s.rix,
90 super->f.hdr.length, super->f.hdr.mac.set,
91 (unsigned int) le32_to_cpu(super->f.hdr.phy.set),
92 super->f.data.i3e.frame_control);
97 desc = get_wlan_txq_addr(qidx);
99 DBG("Queue: %d: te:%d td:%d h:%p c:%p t:%p",
100 qidx, entries, queue_len(&fw.wlan.tx_queue[qidx]),
101 fw.wlan.tx_queue[qidx].head,
102 desc, fw.wlan.tx_queue[qidx].terminator);
104 DBG("HW: t:%x s:%x ac:%x c:%x",
105 (unsigned int) get(AR9170_MAC_REG_DMA_TRIGGER),
106 (unsigned int) get(AR9170_MAC_REG_DMA_STATUS),
107 (unsigned int) get(AR9170_MAC_REG_AMPDU_COUNT),
108 (unsigned int) get(AR9170_MAC_REG_DMA_TXQX_ADDR_CURR));
110 #endif /* CONFIG_CARL9170FW_DEBUG */
112 static void wlan_send_buffered_tx_status(void)
116 while (fw.wlan.tx_status_pending) {
117 len = min((unsigned int)fw.wlan.tx_status_pending,
118 CARL9170_RSP_TX_STATUS_NUM);
119 len = min(len, CARL9170_TX_STATUS_NUM - fw.wlan.tx_status_head_idx);
122 * rather than memcpy each individual request into a large buffer,
123 * we _splice_ them all together.
125 * The only downside is however that we have to be careful around
126 * the edges of the tx_status_cache.
129 * Each tx_status is about 2 bytes. However every command package
130 * must have a size which is a multiple of 4.
133 send_cmd_to_host((len * sizeof(struct carl9170_tx_status) + 3) & ~3,
134 CARL9170_RSP_TXCOMP, len, (void *)
135 &fw.wlan.tx_status_cache[fw.wlan.tx_status_head_idx]);
137 fw.wlan.tx_status_pending -= len;
138 fw.wlan.tx_status_head_idx += len;
139 fw.wlan.tx_status_head_idx %= CARL9170_TX_STATUS_NUM;
143 static struct carl9170_tx_status *wlan_get_tx_status_buffer(void)
145 struct carl9170_tx_status *tmp;
147 tmp = &fw.wlan.tx_status_cache[fw.wlan.tx_status_tail_idx++];
148 fw.wlan.tx_status_tail_idx %= CARL9170_TX_STATUS_NUM;
150 if (fw.wlan.tx_status_pending == CARL9170_TX_STATUS_NUM)
151 wlan_send_buffered_tx_status();
153 fw.wlan.tx_status_pending++;
158 /* generate _aggregated_ tx_status for the host */
159 void wlan_tx_complete(struct carl9170_tx_superframe *super,
162 struct carl9170_tx_status *status;
164 status = wlan_get_tx_status_buffer();
167 * The *unique* cookie and AC_ID is used by the driver for
170 status->cookie = super->s.cookie;
171 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 */
215 static inline u16 get_tid(struct ieee80211_hdr *hdr)
217 return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
220 /* This function will only work on uint32_t-aligned pointers! */
221 static bool same_hdr(const void *_d0, const void *_d1)
223 const uint32_t *d0 = _d0;
224 const uint32_t *d1 = _d1;
226 /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
227 return !((d0[0] ^ d1[0]) | /* FC + DU */
228 (d0[1] ^ d1[1]) | /* addr1 */
229 (d0[2] ^ d1[2]) | (d0[3] ^ d1[3]) | /* addr2 + addr3 */
230 (d0[4] ^ d1[4])); /* addr3 */
233 static inline bool same_aggr(struct ieee80211_hdr *a, struct ieee80211_hdr *b)
235 return (get_tid(a) == get_tid(b)) || same_hdr(a, b);
238 static void wlan_tx_ampdu_reset(unsigned int qidx)
240 fw.wlan.ampdu_prev[qidx] = NULL;
243 static void wlan_tx_ampdu_end(unsigned int qidx)
245 struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
248 ht_prev->f.hdr.mac.ba_end = 1;
250 wlan_tx_ampdu_reset(qidx);
253 static void wlan_tx_ampdu(struct carl9170_tx_superframe *super)
255 unsigned int qidx = super->s.queue;
256 struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
258 if (super->f.hdr.mac.ampdu) {
260 !same_aggr(&super->f.data.i3e, &ht_prev->f.data.i3e))
261 ht_prev->f.hdr.mac.ba_end = 1;
263 super->f.hdr.mac.ba_end = 0;
265 fw.wlan.ampdu_prev[qidx] = super;
267 wlan_tx_ampdu_end(qidx);
272 static void __wlan_tx(struct dma_desc *desc)
274 struct carl9170_tx_superframe *super = get_super(desc);
276 if (unlikely(super->s.fill_in_tsf)) {
277 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
278 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
281 * Truth be told: this is a hack.
283 * The *real* TSF is definitely going to be higher/older.
284 * But this hardware emulation code is head and shoulders
285 * above anything a driver can possibly do.
287 * (even, if it's got an accurate atomic clock source).
293 wlan_tx_ampdu(super);
295 #ifdef CONFIG_CARL9170FW_DEBUG
296 BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
297 #endif /* CONFIG_CARL9170FW_DEBUG */
299 /* insert desc into the right queue */
300 dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
303 static void wlan_assign_seq(struct ieee80211_hdr *hdr, unsigned int vif)
305 hdr->seq_ctrl &= cpu_to_le16(~IEEE80211_SCTL_SEQ);
306 hdr->seq_ctrl |= cpu_to_le16(fw.wlan.sequence[vif]);
308 if (ieee80211_is_first_frag(hdr->seq_ctrl))
309 fw.wlan.sequence[vif] += 0x10;
312 /* prepares frame for the first transmission */
313 static void _wlan_tx(struct dma_desc *desc)
315 struct carl9170_tx_superframe *super = get_super(desc);
317 if (unlikely(super->s.assign_seq))
318 wlan_assign_seq(&super->f.data.i3e, super->s.vif_id);
320 if (unlikely(super->s.ampdu_commit_density)) {
321 set(AR9170_MAC_REG_AMPDU_DENSITY,
322 MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
323 get(AR9170_MAC_REG_AMPDU_DENSITY),
324 super->s.ampdu_density));
327 if (unlikely(super->s.ampdu_commit_factor)) {
328 set(AR9170_MAC_REG_AMPDU_FACTOR,
329 MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
330 get(AR9170_MAC_REG_AMPDU_FACTOR),
331 8 << super->s.ampdu_factor));
335 /* propagate transmission status back to the driver */
336 static bool wlan_tx_status(struct dma_queue *queue,
337 struct dma_desc *desc)
339 struct carl9170_tx_superframe *super = get_super(desc);
340 unsigned int qidx = super->s.queue;
341 bool txfail = false, success;
345 /* update hangcheck */
346 fw.wlan.last_super_num[qidx] = 0;
350 * There could be a corner case when the TXFAIL is set
351 * even though the frame was properly ACKed by the peer:
352 * a BlockAckReq with the immediate policy will cause
353 * the receiving peer to produce a BlockACK unfortunately
354 * the MAC in this chip seems to be expecting a legacy
355 * ACK and marks the BAR as failed!
358 if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
359 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
361 /* reset retry indicator flags */
362 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
365 * Note: wlan_tx_consume_retry will override the old
366 * phy [CCK,OFDM, HT, BW20/40, MCS...] and mac vectors
367 * [AMPDU,RTS/CTS,...] therefore be careful when they
370 if (wlan_tx_consume_retry(super)) {
372 * retry for simple and aggregated 802.11 frames.
374 * Note: We must not mess up the original frame
378 if (!super->f.hdr.mac.ampdu) {
380 * 802.11 - 7.1.3.1.5.
381 * set "Retry Field" for consecutive attempts
383 * Note: For AMPDU see:
384 * 802.11n 9.9.1.6 "Retransmit Procedures"
386 super->f.data.i3e.frame_control |=
387 cpu_to_le16(IEEE80211_FCTL_RETRY);
391 /* Normal TX Failure */
393 /* demise descriptor ownership back to the hardware */
397 * And this will get the queue going again.
398 * To understand why: you have to get the HW
399 * specs... But sadly I never saw them.
401 wlan_txunstuck(qidx);
403 /* abort cycle - this is necessary due to HW design */
406 /* (HT-) BlockACK failure */
409 * Unlink the failed attempt and put it into
410 * the retry queue. The caller routine must
411 * be aware of this so the frames don't get lost.
414 #ifndef CONFIG_CARL9170FW_DEBUG
415 dma_unlink_head(queue);
416 #else /* CONFIG_CARL9170FW_DEBUG */
417 BUG_ON(dma_unlink_head(queue) != desc);
418 #endif /* CONFIG_CARL9170FW_DEBUG */
419 dma_put(&fw.wlan.tx_retry, desc);
423 /* out of frame attempts - discard frame */
428 #ifndef CONFIG_CARL9170FW_DEBUG
429 dma_unlink_head(queue);
430 #else /* CONFIG_CARL9170FW_DEBUG */
431 BUG_ON(dma_unlink_head(queue) != desc);
432 #endif /* CONFIG_CARL9170FW_DEBUG */
435 * Issue the queue bump,
436 * We need to do this in case this was the frame's last
437 * possible retry attempt and it unfortunately: it failed.
440 wlan_txunstuck(qidx);
445 if (unlikely(super == fw.wlan.fw_desc_data)) {
446 fw.wlan.fw_desc = desc;
447 fw.wlan.fw_desc_available = 1;
449 if (fw.wlan.fw_desc_callback)
450 fw.wlan.fw_desc_callback(super, success);
455 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
456 if (unlikely(super->s.cab))
457 fw.wlan.cab_queue_len[super->s.vif_id]--;
458 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
460 if (unlikely(ieee80211_is_back_req(super->f.data.i3e.frame_control))) {
462 * As explained above, the hardware seems to be
463 * incapable of matching BA to BARs. This is a
464 * problem especially with mac80211, because it
465 * does resent failed BARs which of course cause
466 * some mayhem in the receiver buffer at the HT
467 * peer on the other end.
472 wlan_tx_complete(super, success);
474 /* recycle freed descriptors */
475 dma_reclaim(&fw.pta.down_queue, desc);
480 static void handle_tx_completion(void)
482 struct dma_desc *desc;
485 for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
486 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
487 if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
488 /* termination requested. */
493 wlan_tx_ampdu_reset(i);
495 for_each_desc(desc, &fw.wlan.tx_retry)
498 wlan_tx_ampdu_end(i);
499 if (!queue_empty(&fw.wlan.tx_queue[i]))
500 wlan_trigger(BIT(i));
504 void __hot wlan_tx(struct dma_desc *desc)
506 struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
508 /* initialize rate control struct */
513 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
514 if (unlikely(super->s.cab)) {
515 fw.wlan.cab_queue_len[super->s.vif_id]++;
516 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
519 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
523 wlan_trigger(BIT(super->s.queue));
526 void wlan_tx_fw(struct carl9170_tx_superdesc *super, fw_desc_callback_t cb)
528 if (!fw.wlan.fw_desc_available)
531 fw.wlan.fw_desc_available = 0;
533 /* Format BlockAck */
534 fw.wlan.fw_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
535 fw.wlan.fw_desc->status = AR9170_OWN_BITS_SW;
537 fw.wlan.fw_desc->totalLen = fw.wlan.fw_desc->dataSize = super->len;
538 fw.wlan.fw_desc_data = fw.wlan.fw_desc->dataAddr = super;
539 fw.wlan.fw_desc->nextAddr = fw.wlan.fw_desc->lastAddr =
541 fw.wlan.fw_desc_callback = cb;
542 wlan_tx(fw.wlan.fw_desc);
545 static void wlan_send_buffered_ba(void)
547 struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
548 struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
549 struct carl9170_bar_ctx *ctx;
551 if (likely(!fw.wlan.queued_ba))
554 /* there's no point to continue when the ba_desc is not available. */
555 if (!fw.wlan.fw_desc_available)
558 ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx];
559 fw.wlan.ba_head_idx++;
560 fw.wlan.ba_head_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
563 baf->s.len = sizeof(struct carl9170_tx_superdesc) +
564 sizeof(struct ar9170_tx_hwdesc) +
565 sizeof(struct ieee80211_ba);
566 baf->s.ri[0].tries = 1;
568 baf->s.queue = AR9170_TXQ_VO;
569 baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
571 baf->f.hdr.mac.no_ack = 1;
573 baf->f.hdr.phy.modulation = 1; /* OFDM */
574 baf->f.hdr.phy.tx_power = 34; /* 17 dBm */
575 baf->f.hdr.phy.chains = 1;
576 baf->f.hdr.phy.mcs = AR9170_TXRX_PHY_RATE_OFDM_6M;
578 /* format outgoing BA */
579 ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
580 ba->duration = cpu_to_le16(0);
581 memcpy(ba->ta, ctx->ta, 6);
582 memcpy(ba->ra, ctx->ra, 6);
585 * Unfortunately, we cannot look into the hardware's scoreboard.
586 * Therefore we have to proceed as described in 802.11n 9.10.7.5
587 * and send a null BlockAck.
589 memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
591 ba->control = ctx->control;
592 ba->start_seq_num = ctx->start_seq_num;
593 wlan_tx_fw(&baf->s, NULL);
596 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
598 struct carl9170_bar_ctx *tmp;
600 tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx];
601 fw.wlan.ba_tail_idx++;
602 fw.wlan.ba_tail_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
603 if (fw.wlan.queued_ba < CONFIG_CARL9170FW_BACK_REQS_NUM)
609 static void handle_bar(struct dma_desc *desc __unused, struct ieee80211_hdr *hdr,
610 unsigned int len, unsigned int mac_err)
612 struct ieee80211_bar *bar;
613 struct carl9170_bar_ctx *ctx;
615 if (unlikely(mac_err)) {
617 * This check does a number of things:
618 * 1. checks if the frame is in good nick
619 * 2. checks if the RA (MAC) matches
624 if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
626 * Sneaky, corrupted BARs... but not with us!
634 if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
635 !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
636 /* not implemented yet */
641 ctx = wlan_get_bar_cache_buffer();
643 /* Brilliant! The BAR provides all necessary MACs! */
644 memcpy(ctx->ra, bar->ta, 6);
645 memcpy(ctx->ta, bar->ra, 6);
646 ctx->control = bar->control;
647 ctx->start_seq_num = bar->start_seq_num;
650 static void wlan_check_rx_overrun(void)
652 uint32_t overruns, total;
654 fw.tally.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
655 fw.tally.rx_overrun += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
656 if (unlikely(overruns)) {
657 if (overruns == total) {
662 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
666 static unsigned int wlan_rx_filter(struct dma_desc *desc)
668 struct ieee80211_hdr *hdr;
669 unsigned int data_len;
670 unsigned int rx_filter;
671 unsigned int mac_err;
673 data_len = ar9170_get_rx_mpdu_len(desc);
674 mac_err = ar9170_get_rx_macstatus_error(desc);
676 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
678 if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
679 desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
680 mac_err & AR9170_RX_ERROR_BAD) {
682 * This frame is too damaged to do anything
686 return CARL9170_RX_FILTER_BAD;
690 if (mac_err & AR9170_RX_ERROR_WRONG_RA)
691 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
693 if (mac_err & AR9170_RX_ERROR_DECRYPT)
694 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
696 hdr = ar9170_get_rx_i3e(desc);
697 if (likely(ieee80211_is_data(hdr->frame_control))) {
698 rx_filter |= CARL9170_RX_FILTER_DATA;
699 } else if (ieee80211_is_ctl(hdr->frame_control)) {
700 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
701 case IEEE80211_STYPE_BACK_REQ:
702 handle_bar(desc, hdr, data_len, mac_err);
704 rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
706 case IEEE80211_STYPE_PSPOLL:
707 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
710 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
714 /* ieee80211_is_mgmt */
715 rx_filter |= CARL9170_RX_FILTER_MGMT;
718 if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
719 wol_rx(rx_filter, hdr, min(data_len,
720 (unsigned int)AR9170_BLOCK_SIZE));
723 #undef AR9170_RX_ERROR_BAD
728 static void handle_rx(void)
730 struct dma_desc *desc;
732 for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
733 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
734 dma_put(&fw.pta.up_queue, desc);
737 dma_reclaim(&fw.wlan.rx_queue, desc);
738 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
743 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
744 void wlan_cab_flush_queue(const unsigned int vif)
746 struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
747 struct dma_desc *desc;
749 /* move queued frames into the main tx queues */
750 for_each_desc(desc, cab_queue) {
751 struct carl9170_tx_superframe *super = get_super(desc);
752 if (!queue_empty(cab_queue)) {
754 * Set MOREDATA flag for all,
755 * but the last queued frame.
756 * see: 802.11-2007 11.2.1.5 f)
758 * This is actually the reason to why
759 * we need to prevent the reentry.
762 super->f.data.i3e.frame_control |=
763 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
765 super->f.data.i3e.frame_control &=
766 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
772 wlan_trigger(BIT(super->s.queue));
776 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
777 const unsigned int len)
779 struct ieee80211_mgmt *mgmt = addr;
782 pos = mgmt->u.beacon.variable;
783 end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
785 if (pos + 2 + pos[1] > end)
797 void wlan_modify_beacon(const unsigned int vif,
798 const unsigned int addr, const unsigned int len)
801 struct ieee80211_tim_ie *ie;
803 _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
805 ie = (struct ieee80211_tim_ie *) &_ie[2];
807 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
808 /* schedule DTIM transfer */
809 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
810 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
811 /* undo all chances to the beacon structure */
812 ie->bitmap_ctrl &= ~0x1;
813 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
816 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
817 if (fw.wlan.cab_flush_trigger[vif]) {
818 /* Set the almighty Multicast Traffic Indication Bit. */
819 ie->bitmap_ctrl |= 0x1;
824 * Ideally, the sequence number should be assigned by the TX arbiter
825 * hardware. But AFAIK that's not possible, so we have to go for the
826 * next best thing and write it into the beacon fifo during the open
827 * beacon update window.
830 wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
833 static void wlan_send_buffered_cab(void)
837 for (i = 0; i < CARL9170_INTF_NUM; i++) {
838 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
840 * This is hardcoded into carl9170usb driver.
842 * The driver must set the PRETBTT event to beacon_interval -
843 * CARL9170_PRETBTT_KUS (usually 6) Kus.
845 * But still, we can only do so much about 802.11-2007 9.3.2.1 &
846 * 11.2.1.6. Let's hope the current solution is adequate enough.
849 if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
850 wlan_cab_flush_queue(i);
853 * This prevents the code from sending new BC/MC frames
854 * which were queued after the previous buffered traffic
855 * has been sent out... They will have to wait until the
856 * next DTIM beacon comes along.
858 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
864 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
866 static void handle_beacon_config(void)
870 bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
871 send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
872 (uint8_t *) &bcn_count);
875 static void handle_pretbtt(void)
877 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
878 fw.wlan.cab_flush_time = get_clock_counter();
879 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
881 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
884 send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
885 (uint8_t *) &fw.phy.psm.state);
886 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
889 static void handle_atim(void)
891 send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
894 #ifdef CONFIG_CARL9170FW_DEBUG
895 static void handle_qos(void)
898 * What is the QoS Bit used for?
899 * Is it only an indicator for TXOP & Burst, or
900 * should we do something here?
904 static void handle_radar(void)
906 send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
908 #endif /* CONFIG_CARL9170FW_DEBUG */
910 static void wlan_janitor(void)
912 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
913 wlan_send_buffered_cab();
914 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
916 wlan_send_buffered_tx_status();
918 wlan_send_buffered_ba();
923 void handle_wlan(void)
927 intr = get(AR9170_MAC_REG_INT_CTRL);
929 set(AR9170_MAC_REG_INT_CTRL, intr);
931 #define HANDLER(intr, flag, func) \
933 if ((intr & flag) != 0) { \
938 intr |= fw.wlan.soft_int;
939 fw.wlan.soft_int = 0;
941 HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
943 HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
945 HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
947 HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
948 handle_tx_completion);
950 #ifdef CONFIG_CARL9170FW_DEBUG
951 HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
953 HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
954 #endif /* CONFIG_CARL9170FW_DEBUG */
956 HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
959 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
967 CARL9170FW_TX_MAC_BUMP = 4,
968 CARL9170FW_TX_MAC_DEBUG = 6,
969 CARL9170FW_TX_MAC_RESET = 7,
972 static void wlan_check_hang(void)
974 struct dma_desc *desc;
977 for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
978 if (queue_empty(&fw.wlan.tx_queue[i])) {
979 /* Nothing to do here... move along */
983 /* fetch the current DMA queue position */
984 desc = (struct dma_desc *)get_wlan_txq_addr(i);
986 /* Stuck frame detection */
987 if (unlikely(DESC_PAYLOAD(desc) == fw.wlan.last_super[i])) {
988 fw.wlan.last_super_num[i]++;
990 if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_RESET)) {
992 * schedule MAC reset (aka OFF/ON => dead)
994 * This will almost certainly kill
995 * the device for good, but it's the
996 * recommended thing to do...
1002 #ifdef CONFIG_CARL9170FW_DEBUG
1003 if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_DEBUG)) {
1005 * Sigh, the queue is almost certainly
1006 * dead. Dump the queue content to the
1007 * user, maybe we find out why it got
1013 #endif /* CONFIG_CARL9170FW_DEBUG */
1015 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
1016 if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_BUMP)) {
1018 * Hrrm, bump the queue a bit.
1019 * maybe this will get it going again.
1023 wlan_trigger(BIT(i));
1025 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
1028 fw.wlan.last_super[i] = DESC_PAYLOAD(desc);
1029 fw.wlan.last_super_num[i] = 0;
1034 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
1036 * NB: Resetting the MAC is a two-edged sword.
1037 * On most occasions, it does what it is supposed to do.
1038 * But there is a chance that this will make it
1039 * even worse and the radio dies silently.
1041 static void wlan_mac_reset(void)
1044 uint32_t agg_wait_counter;
1045 uint32_t agg_density;
1046 uint32_t bcn_start_addr;
1047 uint32_t rctl, rcth;
1050 uint32_t rts_cts_tpc;
1051 uint32_t rts_cts_rate;
1054 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1056 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1058 #ifdef CONFIG_CARL9170FW_NOISY_MAC_RESET
1060 #endif /* CONFIG_CARL9170FW_NOISY_MAC_RESET */
1062 /* Save aggregation parameters */
1063 agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
1064 agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
1066 bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
1068 cam_mode = get(AR9170_MAC_REG_CAM_MODE);
1069 rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
1070 rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
1072 ack_power = get(AR9170_MAC_REG_ACK_TPC);
1073 rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
1074 rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
1076 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1077 /* 0x1c8960 write only */
1078 rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
1079 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1081 /* TX/RX must be stopped by now */
1082 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
1084 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
1087 * Manipulate CCA threshold to stop transmission
1089 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1093 * check Rx state in 0(idle) 9(disable)
1095 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1096 * while( (chState != 0) && (chState != 9)) {
1097 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1101 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1105 /* Restore aggregation parameters */
1106 set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1107 set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1109 set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1110 set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1111 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1112 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1114 set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1115 set(AR9170_MAC_REG_ACK_TPC, ack_power);
1116 set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1118 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1119 set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1120 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1123 * Manipulate CCA threshold to resume transmission
1125 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1128 val = AR9170_DMA_TRIGGER_RXQ;
1129 /* Reinitialize all WLAN TX DMA queues. */
1130 for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
1131 struct dma_desc *iter;
1133 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1135 /* kill the stuck frame */
1136 if (!is_terminator(&fw.wlan.tx_queue[i], iter) &&
1137 fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_RESET &&
1138 fw.wlan.last_super[i] == DESC_PAYLOAD(iter)) {
1139 struct carl9170_tx_superframe *super = get_super(iter);
1141 iter->status = AR9170_OWN_BITS_SW;
1143 * Mark the frame as failed.
1144 * The BAFAIL flag allows the frame to sail through
1145 * wlan_tx_status without much "unstuck" trouble.
1147 iter->ctrl &= ~(AR9170_CTRL_FAIL);
1148 iter->ctrl |= AR9170_CTRL_BAFAIL;
1150 super->s.cnt = CARL9170_TX_MAX_RATE_TRIES;
1151 super->s.rix = CARL9170_TX_MAX_RETRY_RATES;
1153 fw.wlan.last_super_num[i] = 0;
1154 fw.wlan.last_super[i] = NULL;
1155 iter = iter->lastAddr->nextAddr;
1158 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1159 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1162 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]),
1163 fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1164 get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1167 fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1168 AR9170_MAC_INT_RETRY_FAIL;
1170 set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1174 static void wlan_mac_reset(void)
1176 /* The driver takes care of reinitializing the device */
1179 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1181 void __cold wlan_timer(void)
1183 unsigned int cached_mac_reset;
1185 cached_mac_reset = fw.wlan.mac_reset;
1187 /* TX Queue Hang check */
1190 /* RX Overrun check */
1191 wlan_check_rx_overrun();
1193 if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1195 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1197 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1198 fw.wlan.mac_reset--;