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"
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 static 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;
174 * This field holds the number of tries of the rate in
175 * the rate index field (rix).
177 status->rix = super->s.rix;
178 status->tries = super->s.cnt;
179 status->success = (txs) ? 1 : 0;
182 static bool wlan_tx_consume_retry(struct carl9170_tx_superframe *super)
184 /* check if this was the last possible retry with this rate */
185 if (unlikely(super->s.cnt >= super->s.ri[super->s.rix].tries)) {
186 /* end of the road - indicate tx failure */
187 if (unlikely(super->s.rix == CARL9170_TX_MAX_RETRY_RATES))
190 /* check if there are alternative rates available */
191 if (!super->s.rr[super->s.rix].set)
194 /* try next retry rate */
195 super->f.hdr.phy.set = super->s.rr[super->s.rix].set;
197 /* finally - mark the old rate as USED */
200 /* update MAC flags */
201 super->f.hdr.mac.erp_prot = super->s.ri[super->s.rix].erp_prot;
202 super->f.hdr.mac.ampdu = super->s.ri[super->s.rix].ampdu;
204 /* reinitialize try counter */
207 /* just increase retry counter */
214 static inline u16 get_tid(struct ieee80211_hdr *hdr)
216 return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
219 /* This function will only work on uint32_t-aligned pointers! */
220 static inline bool compare_ether_address(const void *_d0, const void *_d1)
222 const uint32_t *d0 = _d0;
223 const uint32_t *d1 = _d1;
225 /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
226 return !((d0[0] ^ d1[0]) | (unsigned short)(d0[1] ^ d1[1]));
229 #ifdef CONFIG_CARL9170FW_TX_AMPDU
230 static void wlan_tx_ampdu(struct carl9170_tx_superframe *super)
232 unsigned int qidx = super->s.queue;
233 struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
235 if (!super->f.hdr.mac.ampdu) {
236 fw.wlan.ampdu_prev[qidx] = NULL;
239 ht_prev->f.hdr.mac.ba_end = 1;
241 fw.wlan.ampdu_prev[qidx] = super;
243 if (ht_prev && (get_tid(&super->f.data.i3e) != get_tid(&ht_prev->f.data.i3e) ||
244 !compare_ether_address(super->f.data.i3e.addr1, ht_prev->f.data.i3e.addr1)))
245 ht_prev->f.hdr.mac.ba_end = 1;
247 super->f.hdr.mac.ba_end = 0;
250 #endif /* CONFIG_CARL9170FW_TX_AMPDU */
253 static void __wlan_tx(struct dma_desc *desc)
255 struct carl9170_tx_superframe *super = get_super(desc);
257 if (unlikely(super->s.fill_in_tsf)) {
258 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
259 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
262 * Truth be told: this is a hack.
264 * The *real* TSF is definitely going to be higher/older.
265 * But this hardware emulation code is head and shoulders
266 * above anything a driver can possibly do.
268 * (even, if it's got an accurate atomic clock source).
274 #ifdef CONFIG_CARL9170FW_TX_AMPDU
275 wlan_tx_ampdu(super);
276 #endif /* CONFIG_CARL9170FW_TX_AMPDU */
278 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
279 wlan_tx_complete(super, true);
281 # ifdef CONFIG_CARL9170FW_LOOPBACK
282 dma_put(&fw.pta.up_queue, desc);
284 # elif CONFIG_CARL9170FW_DISCARD
285 dma_reclaim(&fw.pta.down_queue, desc);
288 #else /* CONFIG_CARL9170FW_LOOPBACK */
290 # if ((defined CONFIG_CARL9170FW_DEBUG) && (defined CONFIG_CARL9170FW_PSM))
291 BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
292 # endif /* CONFIG_CARL9170FW_DEBUG && CONFIG_CARL9170FW_PSM */
294 /* insert desc into the right queue */
295 dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
296 #endif /* CONFIG_CARL9170FW_LOOPBACK */
299 static void wlan_assign_seq(struct ieee80211_hdr *hdr, unsigned int vif)
301 hdr->seq_ctrl &= cpu_to_le16(~IEEE80211_SCTL_SEQ);
302 hdr->seq_ctrl |= cpu_to_le16(fw.wlan.sequence[vif]);
304 if (!(hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG)))
305 fw.wlan.sequence[vif] += 0x10;
308 /* prepares frame for the first transmission */
309 static void _wlan_tx(struct dma_desc *desc)
311 struct carl9170_tx_superframe *super = get_super(desc);
313 if (unlikely(super->s.assign_seq)) {
314 wlan_assign_seq(&super->f.data.i3e, super->s.vif_id);
317 if (unlikely(super->s.ampdu_commit_density)) {
318 set(AR9170_MAC_REG_AMPDU_DENSITY,
319 MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
320 get(AR9170_MAC_REG_AMPDU_DENSITY),
321 super->s.ampdu_density));
324 if (unlikely(super->s.ampdu_commit_factor)) {
325 set(AR9170_MAC_REG_AMPDU_FACTOR,
326 MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
327 get(AR9170_MAC_REG_AMPDU_FACTOR),
328 8 << super->s.ampdu_factor));
334 /* propagate transmission status back to the driver */
335 static bool wlan_tx_status(struct dma_queue *queue,
336 struct dma_desc *desc)
338 struct ar9170_tx_frame *frame = DESC_PAYLOAD(desc);
339 struct carl9170_tx_superframe *super = get_super(desc);
340 struct ieee80211_hdr *hdr = &super->f.data.i3e;
341 unsigned int qidx = super->s.queue;
342 bool txfail, success;
346 /* update hangcheck */
347 fw.wlan.last_tx_desc_num[qidx] = 0;
349 if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
350 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
352 /* reset retry indicator flags */
353 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
355 if (wlan_tx_consume_retry(super)) {
357 * retry for simple and aggregated 802.11 frames.
359 * Note: We must not mess up the original frame
363 if (!frame->hdr.mac.ampdu) {
365 * 802.11 - 7.1.3.1.5.
366 * set "Retry Field" for consecutive attempts
368 * Note: For AMPDU see:
369 * 802.11n 9.9.1.6 "Retransmit Procedures"
372 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_RETRY);
376 /* Normal TX Failure */
378 /* demise descriptor ownership back to the hardware */
382 * And this will get the queue going again.
383 * To understand why: you have to get the HW
384 * specs... But sadly I never saw them.
386 wlan_txunstuck(qidx);
388 /* abort cycle - this is necessary due to HW design */
391 /* (HT-) BlockACK failure */
394 * Unlink the failed attempt and put it into
395 * the retry queue. The caller routine must
396 * be aware of this so the frames don't get lost.
399 dma_unlink_head(queue);
400 #ifdef CONFIG_CARL9170FW_DELAYED_TX
401 dma_put(&fw.wlan.tx_retry, desc);
404 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
408 /* out of frame attempts - discard frame */
413 dma_unlink_head(queue);
416 * Issue the queue bump,
417 * We need to do this in case this was the frame's last
418 * possible retry attempt and it unfortunately: it failed.
421 wlan_txunstuck(qidx);
426 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
427 if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
428 fw.wlan.ba_desc = desc;
429 fw.wlan.ba_desc_available = 1;
432 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
434 wlan_tx_complete(super, success);
436 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
437 if (unlikely(super->s.cab))
438 fw.wlan.cab_queue_len[super->s.vif_id]--;
439 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
441 /* recycle freed descriptors */
442 dma_reclaim(&fw.pta.down_queue, desc);
447 static void handle_tx_completion(void)
449 struct dma_desc *desc;
452 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
453 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
454 if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
455 /* termination requested. */
460 #ifdef CONFIG_CARL9170FW_DELAYED_TX
461 for_each_desc(desc, &fw.wlan.tx_retry)
464 for_each_desc(desc, &fw.wlan.tx_delay[i])
466 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
467 wlan_trigger(BIT(i));
471 void __hot wlan_tx(struct dma_desc *desc)
473 struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
475 /* initialize rate control struct */
480 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
481 if (unlikely(super->s.cab)) {
482 fw.wlan.cab_queue_len[super->s.vif_id]++;
483 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
486 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
488 #ifdef CONFIG_CARL9170FW_DELAYED_TX
489 if (!queue_empty(&fw.wlan.tx_queue[super->s.queue])) {
490 dma_put(&fw.wlan.tx_delay[super->s.queue], desc);
493 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
496 wlan_trigger(BIT(super->s.queue));
499 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
500 static void wlan_send_buffered_ba(void)
502 struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
503 struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
504 struct carl9170_bar_ctx *ctx;
506 if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
509 /* there's no point to continue when the ba_desc is not available. */
510 if (!fw.wlan.ba_desc_available)
513 ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
514 fw.wlan.ba_head_idx++;
516 /* Format BlockAck */
517 fw.wlan.ba_desc->status = 0;
518 fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
519 fw.wlan.ba_desc_available = 0;
520 fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
523 baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
524 sizeof(struct carl9170_tx_superdesc) +
525 sizeof(struct ar9170_tx_hwdesc) +
526 sizeof(struct ieee80211_ba);
528 baf->s.ri[0].tries = 3;
530 baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
532 /* HW Duration / Backoff */
533 baf->f.hdr.mac.backoff = 1;
534 baf->f.hdr.mac.hw_duration = 1;
536 /* take the TX rate from the RX'd BAR */
537 baf->f.hdr.phy.set = ctx->phy;
538 baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
540 /* format outgoing BA */
541 ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
542 ba->duration = cpu_to_le16(0);
543 memcpy(ba->ta, ctx->ta, 6);
544 memcpy(ba->ra, ctx->ra, 6);
547 * Unfortunately, we cannot look into the hardware's scoreboard.
548 * Therefore we have to proceed as described in 802.11n 9.10.7.5
549 * and send a null BlockAck.
551 memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
555 * not entirely sure if this is 100% correct?!
557 ba->control = ctx->control | cpu_to_le16(1);
558 ba->start_seq_num = ctx->start_seq_num;
560 wlan_tx(fw.wlan.ba_desc);
563 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
565 struct carl9170_bar_ctx *tmp;
567 /* expire oldest entry, if we ran out of ba_ctx' */
568 if (fw.wlan.ba_head_idx + CONFIG_CARL9170FW_BACK_REQS_NUM < fw.wlan.ba_tail_idx)
569 fw.wlan.ba_head_idx++;
571 tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx % CONFIG_CARL9170FW_BACK_REQS_NUM];
572 fw.wlan.ba_tail_idx++;
577 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
578 unsigned int len, unsigned int mac_err)
580 struct ieee80211_bar *bar;
581 struct carl9170_bar_ctx *ctx;
583 if (unlikely(mac_err)) {
585 * This check does a number of things:
586 * 1. checks if the frame is in good nick
587 * 2. checks if the RA (MAC) matches
592 if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
594 * Sneaky, corrupted BARs... but not with us!
602 if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
603 !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
604 /* not implemented yet */
609 ctx = wlan_get_bar_cache_buffer();
611 /* Brilliant! The BAR provides all necessary MACs! */
612 memcpy(ctx->ra, bar->ta, 6);
613 memcpy(ctx->ta, bar->ra, 6);
617 * not entirely sure if this is 100% correct to force the
618 * imm ack bit or not...
620 ctx->control = bar->control | cpu_to_le16(1);
621 ctx->start_seq_num = bar->start_seq_num;
622 ctx->phy = ar9170_rx_to_phy(desc);
623 if (unlikely(!ctx->phy)) {
624 /* provide a backup, in case ar9170_rx_to_phy fails */
625 ctx->phy = cpu_to_le32(0x2cc301);
628 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
630 static void wlan_check_rx_overrun(void)
632 uint32_t overruns, total;
634 fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
635 fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
636 if (unlikely(overruns)) {
637 if (overruns == total) {
642 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
646 #ifdef CONFIG_CARL9170FW_WOL
647 static void wlan_rx_wol(struct ieee80211_hdr *hdr, unsigned int len)
649 const unsigned char *data, *end, *mac;
650 unsigned int found = 0;
654 * We can only scan the first AR9170_BLOCK_SIZE [=~320] bytes
655 * for MAGIC patterns!
660 * Currently, the MAGIC MAC Address is fixed to the EEPROM default.
661 * It's possible to make it fully configurable, e.g:
663 * mac = (const unsigned char *) AR9170_MAC_REG_MAC_ADDR_L;
664 * But this will clash with the driver's suspend path, because it
665 * needs to reset the registers.
667 mac = rom.sys.mac_address;
669 data = (u8 *)((unsigned long)hdr + ieee80211_hdrlen(hdr->frame_control));
670 end = (u8 *)((unsigned long)hdr + len);
673 * scan for standard WOL Magic frame
675 * "A physical WakeOnLAN (Magic Packet) will look like this:
676 * ---------------------------------------------------------------
677 * | Synchronization Stream | Target MAC | Password (optional) |
678 * | 6 octets | 96 octets | 0, 4 or 6 |
679 * ---------------------------------------------------------------
681 * The Synchronization Stream is defined as 6 bytes of FFh.
682 * The Target MAC block contains 16 duplications of the IEEEaddress
683 * of the target, with no breaks or interruptions.
685 * The Password field is optional, but if present, contains either
686 * 4 bytes or 6 bytes. The WakeOnLAN dissector was implemented to
687 * dissect the password, if present, according to the command-line
688 * format that ether-wake uses, therefore, if a 4-byte password is
689 * present, it will be dissected as an IPv4 address and if a 6-byte
690 * password is present, it will be dissected as an Ethernet address.
692 * <http://wiki.wireshark.org/WakeOnLAN>
697 if (*data == mac[found % 6])
703 /* previous check might reset found counter */
711 if (found == (6 + 16 * 6)) {
712 fw.suspend_mode = CARL9170_AWAKE_HOST;
721 #endif /* CONFIG_CARL9170FW_WOL */
723 static unsigned int wlan_rx_filter(struct dma_desc *desc)
725 struct ieee80211_hdr *hdr;
726 unsigned int data_len;
727 unsigned int rx_filter;
728 unsigned int mac_err;
730 data_len = ar9170_get_rx_mpdu_len(desc);
731 mac_err = ar9170_get_rx_macstatus_error(desc);
733 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
735 if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
736 desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
737 mac_err & AR9170_RX_ERROR_BAD) {
739 * This frame is too damaged to do anything
743 return CARL9170_RX_FILTER_BAD;
747 if (mac_err & AR9170_RX_ERROR_WRONG_RA)
748 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
750 if (mac_err & AR9170_RX_ERROR_DECRYPT)
751 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
753 hdr = ar9170_get_rx_i3e(desc);
754 if (likely(ieee80211_is_data(hdr->frame_control))) {
755 rx_filter |= CARL9170_RX_FILTER_DATA;
756 } else if (ieee80211_is_ctl(hdr->frame_control)) {
757 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
758 case IEEE80211_STYPE_BACK_REQ:
759 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
760 handle_bar(desc, hdr, data_len, mac_err);
761 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
763 rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
765 case IEEE80211_STYPE_PSPOLL:
766 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
769 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
773 /* ieee80211_is_mgmt */
774 rx_filter |= CARL9170_RX_FILTER_MGMT;
777 #ifdef CONFIG_CARL9170FW_WOL
778 if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
779 if (rx_filter & CARL9170_RX_FILTER_DATA)
780 wlan_rx_wol(hdr, min(data_len,
781 (unsigned int)AR9170_BLOCK_SIZE));
783 #endif /* CONFIG_CARL9170FW_WOL */
785 #undef AR9170_RX_ERROR_BAD
790 static void handle_rx(void)
792 struct dma_desc *desc;
794 for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
795 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
796 dma_put(&fw.pta.up_queue, desc);
799 dma_reclaim(&fw.wlan.rx_queue, desc);
800 _wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
805 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
806 void wlan_cab_flush_queue(const unsigned int vif)
808 struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
809 struct dma_desc *desc;
811 /* move queued frames into the main tx queues */
812 for_each_desc(desc, cab_queue) {
813 struct carl9170_tx_superframe *super = get_super(desc);
814 if (!queue_empty(cab_queue)) {
816 * Set MOREDATA flag for all,
817 * but the last queued frame.
818 * see: 802.11-2007 11.2.1.5 f)
820 * This is actually the reason to why
821 * we need to prevent the reentry.
824 super->f.data.i3e.frame_control |=
825 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
827 super->f.data.i3e.frame_control &=
828 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
833 wlan_trigger(BIT(super->s.queue));
837 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
838 const unsigned int len)
840 struct ieee80211_mgmt *mgmt = addr;
843 pos = mgmt->u.beacon.variable;
844 end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
846 if (pos + 2 + pos[1] > end)
858 void wlan_modify_beacon(const unsigned int vif,
859 const unsigned int addr, const unsigned int len)
862 struct ieee80211_tim_ie *ie;
864 _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
866 ie = (struct ieee80211_tim_ie *) &_ie[2];
868 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
869 /* schedule DTIM transfer */
870 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
871 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
872 /* undo all chances to the beacon structure */
873 ie->bitmap_ctrl &= ~0x1;
874 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
877 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
878 if (fw.wlan.cab_flush_trigger[vif]) {
879 /* Set the almighty Multicast Traffic Indication Bit. */
880 ie->bitmap_ctrl |= 0x1;
885 * Ideally, the sequence number should be assigned by the TX arbiter
886 * hardware. But AFAIK that's not possible, so we have to go for the
887 * next best thing and write it into the beacon fifo during the open
888 * beacon update window.
891 wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
893 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
895 static void handle_beacon_config(void)
899 bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
900 send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
901 (uint8_t *) &bcn_count);
904 static void handle_pretbtt(void)
906 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
907 fw.wlan.cab_flush_time = get_clock_counter();
908 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
910 #ifdef CONFIG_CARL9170FW_PSM
913 send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
914 (uint8_t *) &fw.phy.psm.state);
916 send_cmd_to_host(0, CARL9170_RSP_PRETBTT, 0x00, NULL);
917 #endif /* CONFIG_CARL9170FW_PSM */
920 static void handle_atim(void)
922 send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
925 #ifdef CONFIG_CARL9170FW_DEBUG
926 static void handle_qos(void)
929 * What is the QoS Bit used for?
930 * Is it only an indicator for TXOP & Burst, or
931 * should we do something here?
935 static void handle_radar(void)
937 send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
939 #endif /* CONFIG_CARL9170FW_DEBUG */
941 static void wlan_janitor(void)
943 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
946 for (i = 0; i < CARL9170_INTF_NUM; i++) {
947 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
949 * This is hardcoded into carl9170usb driver.
951 * The driver must set the PRETBTT event to beacon_interval -
952 * CARL9170_PRETBTT_KUS (usually 6) Kus.
954 * But still, we can only do so much about 802.11-2007 9.3.2.1 &
955 * 11.2.1.6. Let's hope the current solution is adequate enough.
958 if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
959 wlan_cab_flush_queue(i);
962 * This prevents the code from sending new BC/MC frames
963 * which were queued after the previous buffered traffic
964 * has been sent out... They will have to wait until the
965 * next DTIM beacon comes along.
967 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
972 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
974 #ifdef CONFIG_CARL9170FW_DELAYED_TX
975 if (fw.wlan.tx_trigger) {
976 _wlan_trigger(fw.wlan.tx_trigger);
977 fw.wlan.tx_trigger = 0;
979 #endif /* CONFIG_CARL9170FW_DELAYED_TX */
981 wlan_send_buffered_tx_status();
983 #ifdef CONFIG_CARL9170FW_HANDLE_BACK_REQ
984 wlan_send_buffered_ba();
985 #endif /* CONFIG_CARL9170FW_HANDLE_BACK_REQ */
988 void handle_wlan(void)
992 intr = get(AR9170_MAC_REG_INT_CTRL);
994 set(AR9170_MAC_REG_INT_CTRL, intr);
996 #define HANDLER(intr, flag, func) \
998 if ((intr & flag) != 0) { \
1003 intr |= fw.wlan.soft_int;
1004 fw.wlan.soft_int = 0;
1006 HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
1008 HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
1010 HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
1012 HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
1013 handle_tx_completion);
1015 #ifdef CONFIG_CARL9170FW_DEBUG
1016 HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
1018 HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
1019 #endif /* CONFIG_CARL9170FW_DEBUG */
1021 HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
1024 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
1031 static void wlan_check_hang(void)
1033 struct dma_desc *desc;
1036 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1037 if (queue_empty(&fw.wlan.tx_queue[i])) {
1038 /* Nothing to do here... move along */
1042 /* fetch the current DMA queue position */
1043 desc = get_wlan_txq_addr(i);
1045 /* Stuck frame detection */
1046 if (unlikely(desc == fw.wlan.last_tx_desc[i])) {
1047 fw.wlan.last_tx_desc_num[i]++;
1049 if (unlikely(fw.wlan.last_tx_desc_num[i] > 6)) {
1051 * schedule MAC reset (aka OFF/ON => dead)
1053 * This will almost certainly kill
1054 * the device for good, but it's the
1055 * recommended thing to do...
1058 fw.wlan.mac_reset++;
1061 #ifdef CONFIG_CARL9170FW_DEBUG
1062 if (unlikely(fw.wlan.last_tx_desc_num[i] > 5)) {
1064 * Sigh, the queue is almost certainly
1065 * dead. Dump the queue content to the
1066 * user, maybe we find out why it got
1072 #endif /* CONFIG_CARL9170FW_DEBUG */
1074 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
1075 if (unlikely(fw.wlan.last_tx_desc_num[i] > 3)) {
1077 * Hrrm, bump the queue a bit.
1078 * maybe this will get it going again.
1082 wlan_trigger(BIT(i));
1084 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
1087 fw.wlan.last_tx_desc[i] = desc;
1088 fw.wlan.last_tx_desc_num[i] = 0;
1093 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
1095 * NB: Resetting the MAC is a two-edged sword.
1096 * On most occasions, it does what it is supposed to do.
1097 * But there is a chance that this will make it
1098 * even worse and the radio dies silently.
1100 static void wlan_mac_reset(void)
1103 uint32_t agg_wait_counter;
1104 uint32_t agg_density;
1105 uint32_t bcn_start_addr;
1106 uint32_t rctl, rcth;
1109 uint32_t rts_cts_tpc;
1110 uint32_t rts_cts_rate;
1113 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1115 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1119 /* Save aggregation parameters */
1120 agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
1121 agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
1123 bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
1125 cam_mode = get(AR9170_MAC_REG_CAM_MODE);
1126 rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
1127 rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
1129 ack_power = get(AR9170_MAC_REG_ACK_TPC);
1130 rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
1131 rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
1133 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1134 /* 0x1c8960 write only */
1135 rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
1136 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1138 /* TX/RX must be stopped by now */
1139 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
1141 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
1144 * Manipulate CCA threshold to stop transmission
1146 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1150 * check Rx state in 0(idle) 9(disable)
1152 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1153 * while( (chState != 0) && (chState != 9)) {
1154 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1158 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1162 /* Restore aggregation parameters */
1163 set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1164 set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1166 set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1167 set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1168 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1169 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1171 set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1172 set(AR9170_MAC_REG_ACK_TPC, ack_power);
1173 set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1175 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1176 set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1177 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1180 * Manipulate CCA threshold to resume transmission
1182 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1185 /* Reinitialize all WLAN TX DMA queues. */
1186 for (i = 0; i < __AR9170_NUM_TX_QUEUES; i++) {
1187 struct dma_desc *iter;
1189 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1191 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1192 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1193 wlan_trigger(BIT(i));
1195 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]),
1196 fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1197 get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1200 fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1201 AR9170_MAC_INT_RETRY_FAIL;
1203 set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1204 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
1207 static void wlan_mac_reset(void)
1209 /* The driver takes care of reinitializing the device */
1212 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1214 void __cold wlan_timer(void)
1216 unsigned int cached_mac_reset;
1218 cached_mac_reset = fw.wlan.mac_reset;
1220 /* TX Queue Hang check */
1223 /* RX Overrun check */
1224 wlan_check_rx_overrun();
1226 if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1228 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1230 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1231 fw.wlan.mac_reset--;