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 inline bool compare_ether_address(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]) | (unsigned short)(d0[1] ^ d1[1]));
230 /* This function will only work on uint32_t-aligned pointers! */
231 static bool same_hdr(const void *_d0, const void *_d1)
233 const uint32_t *d0 = _d0;
234 const uint32_t *d1 = _d1;
236 /* BUG_ON((unsigned long)d0 & 3 || (unsigned long)d1 & 3)) */
237 return !((d0[0] ^ d1[0]) | /* FC + DU */
238 (d0[1] ^ d1[1]) | /* addr1 */
239 (d0[2] ^ d1[2]) | (d0[3] ^ d1[3]) | /* addr2 + addr3 */
240 (d0[4] ^ d1[4])); /* addr3 */
243 static inline bool same_aggr(struct ieee80211_hdr *a, struct ieee80211_hdr *b)
245 return (get_tid(a) == get_tid(b)) || same_hdr(a, b);
248 static void wlan_tx_ampdu_end(unsigned int qidx)
250 struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
252 fw.wlan.ampdu_prev[qidx] = NULL;
254 ht_prev->f.hdr.mac.ba_end = 1;
257 static void wlan_tx_ampdu(struct carl9170_tx_superframe *super)
259 unsigned int qidx = super->s.queue;
260 struct carl9170_tx_superframe *ht_prev = fw.wlan.ampdu_prev[qidx];
262 if (!super->f.hdr.mac.ampdu) {
263 wlan_tx_ampdu_end(qidx);
265 fw.wlan.ampdu_prev[qidx] = super;
268 !same_aggr(&super->f.data.i3e, &ht_prev->f.data.i3e))
269 ht_prev->f.hdr.mac.ba_end = 1;
271 super->f.hdr.mac.ba_end = 0;
276 static void __wlan_tx(struct dma_desc *desc)
278 struct carl9170_tx_superframe *super = get_super(desc);
280 if (unlikely(super->s.fill_in_tsf)) {
281 struct ieee80211_mgmt *mgmt = (void *) &super->f.data.i3e;
282 uint32_t *tsf = (uint32_t *) &mgmt->u.probe_resp.timestamp;
285 * Truth be told: this is a hack.
287 * The *real* TSF is definitely going to be higher/older.
288 * But this hardware emulation code is head and shoulders
289 * above anything a driver can possibly do.
291 * (even, if it's got an accurate atomic clock source).
297 wlan_tx_ampdu(super);
299 #if (defined CONFIG_CARL9170FW_LOOPBACK) || (defined CONFIG_CARL9170FW_DISCARD)
300 wlan_tx_complete(super, true);
302 # ifdef CONFIG_CARL9170FW_LOOPBACK
303 dma_put(&fw.pta.up_queue, desc);
305 # elif CONFIG_CARL9170FW_DISCARD
306 dma_reclaim(&fw.pta.down_queue, desc);
309 #else /* CONFIG_CARL9170FW_LOOPBACK */
311 # ifdef CONFIG_CARL9170FW_DEBUG
312 BUG_ON(fw.phy.psm.state != CARL9170_PSM_WAKE);
313 # endif /* CONFIG_CARL9170FW_DEBUG */
315 /* insert desc into the right queue */
316 dma_put(&fw.wlan.tx_queue[super->s.queue], desc);
317 #endif /* CONFIG_CARL9170FW_LOOPBACK */
320 static void wlan_assign_seq(struct ieee80211_hdr *hdr, unsigned int vif)
322 hdr->seq_ctrl &= cpu_to_le16(~IEEE80211_SCTL_SEQ);
323 hdr->seq_ctrl |= cpu_to_le16(fw.wlan.sequence[vif]);
325 if (!(hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG)))
326 fw.wlan.sequence[vif] += 0x10;
329 /* prepares frame for the first transmission */
330 static void _wlan_tx(struct dma_desc *desc)
332 struct carl9170_tx_superframe *super = get_super(desc);
334 if (unlikely(super->s.assign_seq)) {
335 wlan_assign_seq(&super->f.data.i3e, super->s.vif_id);
338 if (unlikely(super->s.ampdu_commit_density)) {
339 set(AR9170_MAC_REG_AMPDU_DENSITY,
340 MOD_VAL(AR9170_MAC_AMPDU_DENSITY,
341 get(AR9170_MAC_REG_AMPDU_DENSITY),
342 super->s.ampdu_density));
345 if (unlikely(super->s.ampdu_commit_factor)) {
346 set(AR9170_MAC_REG_AMPDU_FACTOR,
347 MOD_VAL(AR9170_MAC_AMPDU_FACTOR,
348 get(AR9170_MAC_REG_AMPDU_FACTOR),
349 8 << super->s.ampdu_factor));
355 /* propagate transmission status back to the driver */
356 static bool wlan_tx_status(struct dma_queue *queue,
357 struct dma_desc *desc)
359 struct carl9170_tx_superframe *super = get_super(desc);
360 unsigned int qidx = super->s.queue;
361 bool txfail = false, success;
365 /* update hangcheck */
366 fw.wlan.last_super_num[qidx] = 0;
368 if (!!(desc->ctrl & AR9170_CTRL_FAIL)) {
369 txfail = !!(desc->ctrl & AR9170_CTRL_TXFAIL);
371 /* reset retry indicator flags */
372 desc->ctrl &= ~(AR9170_CTRL_TXFAIL | AR9170_CTRL_BAFAIL);
374 if (wlan_tx_consume_retry(super)) {
376 * retry for simple and aggregated 802.11 frames.
378 * Note: We must not mess up the original frame
382 if (!super->f.hdr.mac.ampdu) {
384 * 802.11 - 7.1.3.1.5.
385 * set "Retry Field" for consecutive attempts
387 * Note: For AMPDU see:
388 * 802.11n 9.9.1.6 "Retransmit Procedures"
390 super->f.data.i3e.frame_control |=
391 cpu_to_le16(IEEE80211_FCTL_RETRY);
395 /* Normal TX Failure */
397 /* demise descriptor ownership back to the hardware */
401 * And this will get the queue going again.
402 * To understand why: you have to get the HW
403 * specs... But sadly I never saw them.
405 wlan_txunstuck(qidx);
407 /* abort cycle - this is necessary due to HW design */
410 /* (HT-) BlockACK failure */
413 * Unlink the failed attempt and put it into
414 * the retry queue. The caller routine must
415 * be aware of this so the frames don't get lost.
418 dma_unlink_head(queue);
419 dma_put(&fw.wlan.tx_retry, desc);
423 /* out of frame attempts - discard frame */
428 dma_unlink_head(queue);
431 * Issue the queue bump,
432 * We need to do this in case this was the frame's last
433 * possible retry attempt and it unfortunately: it failed.
436 wlan_txunstuck(qidx);
441 if (unlikely(super == (void *) &dma_mem.reserved.ba)) {
442 fw.wlan.ba_desc = desc;
443 fw.wlan.ba_desc_available = 1;
447 wlan_tx_complete(super, success);
449 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
450 if (unlikely(super->s.cab))
451 fw.wlan.cab_queue_len[super->s.vif_id]--;
452 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
454 /* recycle freed descriptors */
455 dma_reclaim(&fw.pta.down_queue, desc);
460 static void handle_tx_completion(void)
462 struct dma_desc *desc;
465 for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
466 __while_desc_bits(desc, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW) {
467 if (!wlan_tx_status(&fw.wlan.tx_queue[i], desc)) {
468 /* termination requested. */
473 for_each_desc(desc, &fw.wlan.tx_retry)
476 wlan_tx_ampdu_end(i);
477 if (!queue_empty(&fw.wlan.tx_queue[i]))
478 wlan_trigger(BIT(i));
482 void __hot wlan_tx(struct dma_desc *desc)
484 struct carl9170_tx_superframe *super = DESC_PAYLOAD(desc);
486 /* initialize rate control struct */
491 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
492 if (unlikely(super->s.cab)) {
493 fw.wlan.cab_queue_len[super->s.vif_id]++;
494 dma_put(&fw.wlan.cab_queue[super->s.vif_id], desc);
497 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
500 wlan_trigger(BIT(super->s.queue));
503 static void wlan_send_buffered_ba(void)
505 struct carl9170_tx_ba_superframe *baf = &dma_mem.reserved.ba.ba;
506 struct ieee80211_ba *ba = (struct ieee80211_ba *) &baf->f.ba;
507 struct carl9170_bar_ctx *ctx;
509 if (likely(fw.wlan.ba_head_idx == fw.wlan.ba_tail_idx))
512 /* there's no point to continue when the ba_desc is not available. */
513 if (!fw.wlan.ba_desc_available)
516 fw.wlan.ba_desc_available = 0;
518 ctx = &fw.wlan.ba_cache[fw.wlan.ba_head_idx];
519 fw.wlan.ba_head_idx++;
520 fw.wlan.ba_head_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
522 /* Format BlockAck */
523 fw.wlan.ba_desc->status = AR9170_OWN_BITS_SW;
524 fw.wlan.ba_desc->ctrl = AR9170_CTRL_FS_BIT | AR9170_CTRL_LS_BIT;
525 fw.wlan.ba_desc->nextAddr = fw.wlan.ba_desc->lastAddr =
528 baf->s.len = fw.wlan.ba_desc->totalLen = fw.wlan.ba_desc->dataSize =
529 sizeof(struct carl9170_tx_superdesc) +
530 sizeof(struct ar9170_tx_hwdesc) +
531 sizeof(struct ieee80211_ba);
533 baf->s.ri[0].tries = 1;
534 baf->s.queue = AR9170_TXQ_VO;
535 baf->f.hdr.length = sizeof(struct ieee80211_ba) + FCS_LEN;
537 /* HW Duration / Backoff */
538 baf->f.hdr.mac.backoff = 1;
539 baf->f.hdr.mac.hw_duration = 1;
541 /* take the TX rate from the RX'd BAR */
542 baf->f.hdr.phy.set = ctx->phy;
543 baf->f.hdr.phy.tx_power = 29; /* 14.5 dBm */
545 /* format outgoing BA */
546 ba->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_BACK);
547 ba->duration = cpu_to_le16(0);
548 memcpy(ba->ta, ctx->ta, 6);
549 memcpy(ba->ra, ctx->ra, 6);
552 * Unfortunately, we cannot look into the hardware's scoreboard.
553 * Therefore we have to proceed as described in 802.11n 9.10.7.5
554 * and send a null BlockAck.
556 memset(ba->bitmap, 0x0, sizeof(ba->bitmap));
560 * not entirely sure if this is 100% correct?!
562 ba->control = ctx->control | cpu_to_le16(1);
563 ba->start_seq_num = ctx->start_seq_num;
565 wlan_tx(fw.wlan.ba_desc);
568 static struct carl9170_bar_ctx *wlan_get_bar_cache_buffer(void)
570 struct carl9170_bar_ctx *tmp;
572 tmp = &fw.wlan.ba_cache[fw.wlan.ba_tail_idx];
573 fw.wlan.ba_tail_idx++;
574 fw.wlan.ba_tail_idx %= CONFIG_CARL9170FW_BACK_REQS_NUM;
579 static void handle_bar(struct dma_desc *desc, struct ieee80211_hdr *hdr,
580 unsigned int len, unsigned int mac_err)
582 struct ieee80211_bar *bar;
583 struct carl9170_bar_ctx *ctx;
585 if (unlikely(mac_err)) {
587 * This check does a number of things:
588 * 1. checks if the frame is in good nick
589 * 2. checks if the RA (MAC) matches
594 if (unlikely(len < (sizeof(struct ieee80211_bar) + FCS_LEN))) {
596 * Sneaky, corrupted BARs... but not with us!
604 if ((bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_MULTI_TID)) ||
605 !(bar->control & cpu_to_le16(IEEE80211_BAR_CTRL_CBMTID_COMPRESSED_BA))) {
606 /* not implemented yet */
611 ctx = wlan_get_bar_cache_buffer();
613 /* Brilliant! The BAR provides all necessary MACs! */
614 memcpy(ctx->ra, bar->ta, 6);
615 memcpy(ctx->ta, bar->ra, 6);
619 * not entirely sure if this is 100% correct to force the
620 * imm ack bit or not...
622 ctx->control = bar->control | cpu_to_le16(1);
623 ctx->start_seq_num = bar->start_seq_num;
624 ctx->phy = ar9170_rx_to_phy(desc);
625 if (unlikely(!ctx->phy)) {
626 /* provide a backup, in case ar9170_rx_to_phy fails */
627 ctx->phy = cpu_to_le32(0x2cc301);
631 static void wlan_check_rx_overrun(void)
633 uint32_t overruns, total;
635 fw.wlan.rx_total += total = get(AR9170_MAC_REG_RX_TOTAL);
636 fw.wlan.rx_overruns += overruns = get(AR9170_MAC_REG_RX_OVERRUN);
637 if (unlikely(overruns)) {
638 if (overruns == total) {
643 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
647 #ifdef CONFIG_CARL9170FW_WOL
649 #ifdef CONFIG_CARL9170FW_WOL_MAGIC_PACKET
650 static bool wlan_rx_wol_magic_packet(struct ieee80211_hdr *hdr, unsigned int len)
652 const unsigned char *data, *end, *mac;
653 unsigned int found = 0;
657 * We can only scan the first AR9170_BLOCK_SIZE [=~320] bytes
658 * for MAGIC patterns!
663 * Currently, the MAGIC MAC Address is fixed to the EEPROM default.
664 * It's possible to make it fully configurable, e.g:
666 * mac = (const unsigned char *) AR9170_MAC_REG_MAC_ADDR_L;
667 * But this will clash with the driver's suspend path, because it
668 * needs to reset the registers.
670 mac = rom.sys.mac_address;
672 data = (u8 *)((unsigned long)hdr + ieee80211_hdrlen(hdr->frame_control));
673 end = (u8 *)((unsigned long)hdr + len);
676 * scan for standard WOL Magic frame
678 * "A physical WakeOnLAN (Magic Packet) will look like this:
679 * ---------------------------------------------------------------
680 * | Synchronization Stream | Target MAC | Password (optional) |
681 * | 6 octets | 96 octets | 0, 4 or 6 |
682 * ---------------------------------------------------------------
684 * The Synchronization Stream is defined as 6 bytes of FFh.
685 * The Target MAC block contains 16 duplications of the IEEEaddress
686 * of the target, with no breaks or interruptions.
688 * The Password field is optional, but if present, contains either
689 * 4 bytes or 6 bytes. The WakeOnLAN dissector was implemented to
690 * dissect the password, if present, according to the command-line
691 * format that ether-wake uses, therefore, if a 4-byte password is
692 * present, it will be dissected as an IPv4 address and if a 6-byte
693 * password is present, it will be dissected as an Ethernet address.
695 * <http://wiki.wireshark.org/WakeOnLAN>
700 if (*data == mac[found % 6])
706 /* previous check might reset found counter */
714 if (found == (6 + 16 * 6)) {
723 #endif /* CONFIG_CARL9170FW_WOL_MAGIC_PACKET */
725 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
728 * Note: CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID is not a real
729 * string. We have to be careful not to add a \0 at the end.
731 static const struct {
734 u8 ssid[sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1];
735 } __packed probe_req = {
736 .ssid_ie = WLAN_EID_SSID,
737 .ssid_len = sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1,
738 .ssid = CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID,
741 static bool wlan_rx_wol_probe_ssid(struct ieee80211_hdr *hdr, unsigned int len)
743 const unsigned char *data, *end, *scan = (void *) &probe_req;
746 * IEEE 802.11-2007 7.3.2.1 specifies that the SSID is no
747 * longer than 32 octets.
749 BUILD_BUG_ON((sizeof(CONFIG_CARL9170FW_WOL_PROBE_REQUEST_SSID) - 1) > 32);
751 if (ieee80211_is_probe_req(hdr->frame_control)) {
753 end = (u8 *)((unsigned long)hdr + len);
756 * The position of the SSID information element inside
757 * a probe request frame is more or less "fixed".
759 data = (u8 *)((struct ieee80211_mgmt *)hdr)->u.probe_req.variable;
760 for (i = 0; i < (unsigned int)(probe_req.ssid_len + 1); i++) {
761 if (scan[i] != data[i])
770 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
772 static void wlan_rx_wol(unsigned int rx_filter __unused, struct ieee80211_hdr *hdr __unused, unsigned int len __unused)
774 bool __unused wake_up = false;
776 #ifdef CONFIG_CARL9170FW_WOL_MAGIC_PACKET
777 if (rx_filter & CARL9170_RX_FILTER_DATA)
778 wake_up |= wlan_rx_wol_magic_packet(hdr, len);
779 #endif /* CONFIG_CARL9170FW_WOL_MAGIC_PACKET */
781 #ifdef CONFIG_CARL9170FW_WOL_PROBE_REQUEST
782 if (rx_filter & CARL9170_RX_FILTER_MGMT)
783 wake_up |= wlan_rx_wol_probe_ssid(hdr, len);
784 #endif /* CONFIG_CARL9170FW_WOL_PROBE_REQUEST */
787 fw.suspend_mode = CARL9170_AWAKE_HOST;
788 set(AR9170_USB_REG_WAKE_UP, AR9170_USB_WAKE_UP_WAKE);
791 #endif /* CONFIG_CARL9170FW_WOL */
793 static unsigned int wlan_rx_filter(struct dma_desc *desc)
795 struct ieee80211_hdr *hdr;
796 unsigned int data_len;
797 unsigned int rx_filter;
798 unsigned int mac_err;
800 data_len = ar9170_get_rx_mpdu_len(desc);
801 mac_err = ar9170_get_rx_macstatus_error(desc);
803 #define AR9170_RX_ERROR_BAD (AR9170_RX_ERROR_FCS | AR9170_RX_ERROR_PLCP)
805 if (unlikely(data_len < (4 + 6 + FCS_LEN) ||
806 desc->totalLen > CONFIG_CARL9170FW_RX_FRAME_LEN) ||
807 mac_err & AR9170_RX_ERROR_BAD) {
809 * This frame is too damaged to do anything
813 return CARL9170_RX_FILTER_BAD;
817 if (mac_err & AR9170_RX_ERROR_WRONG_RA)
818 rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
820 if (mac_err & AR9170_RX_ERROR_DECRYPT)
821 rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
823 hdr = ar9170_get_rx_i3e(desc);
824 if (likely(ieee80211_is_data(hdr->frame_control))) {
825 rx_filter |= CARL9170_RX_FILTER_DATA;
826 } else if (ieee80211_is_ctl(hdr->frame_control)) {
827 switch (le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_STYPE) {
828 case IEEE80211_STYPE_BACK_REQ:
829 handle_bar(desc, hdr, data_len, mac_err);
831 rx_filter |= CARL9170_RX_FILTER_CTL_BACKR;
833 case IEEE80211_STYPE_PSPOLL:
834 rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
837 rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
841 /* ieee80211_is_mgmt */
842 rx_filter |= CARL9170_RX_FILTER_MGMT;
845 #ifdef CONFIG_CARL9170FW_WOL
846 if (unlikely(fw.suspend_mode == CARL9170_HOST_SUSPENDED)) {
847 wlan_rx_wol(rx_filter, hdr, min(data_len,
848 (unsigned int)AR9170_BLOCK_SIZE));
850 #endif /* CONFIG_CARL9170FW_WOL */
852 #undef AR9170_RX_ERROR_BAD
857 static void handle_rx(void)
859 struct dma_desc *desc;
861 for_each_desc_not_bits(desc, &fw.wlan.rx_queue, AR9170_OWN_BITS_HW) {
862 if (!(wlan_rx_filter(desc) & fw.wlan.rx_filter)) {
863 dma_put(&fw.pta.up_queue, desc);
866 dma_reclaim(&fw.wlan.rx_queue, desc);
867 wlan_trigger(AR9170_DMA_TRIGGER_RXQ);
872 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
873 void wlan_cab_flush_queue(const unsigned int vif)
875 struct dma_queue *cab_queue = &fw.wlan.cab_queue[vif];
876 struct dma_desc *desc;
878 /* move queued frames into the main tx queues */
879 for_each_desc(desc, cab_queue) {
880 struct carl9170_tx_superframe *super = get_super(desc);
881 if (!queue_empty(cab_queue)) {
883 * Set MOREDATA flag for all,
884 * but the last queued frame.
885 * see: 802.11-2007 11.2.1.5 f)
887 * This is actually the reason to why
888 * we need to prevent the reentry.
891 super->f.data.i3e.frame_control |=
892 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
894 super->f.data.i3e.frame_control &=
895 cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
900 wlan_trigger(BIT(super->s.queue));
904 static uint8_t *beacon_find_ie(uint8_t ie, void *addr,
905 const unsigned int len)
907 struct ieee80211_mgmt *mgmt = addr;
910 pos = mgmt->u.beacon.variable;
911 end = (uint8_t *) ((unsigned long)mgmt + (len - FCS_LEN));
913 if (pos + 2 + pos[1] > end)
925 void wlan_modify_beacon(const unsigned int vif,
926 const unsigned int addr, const unsigned int len)
929 struct ieee80211_tim_ie *ie;
931 _ie = beacon_find_ie(WLAN_EID_TIM, (void *)addr, len);
933 ie = (struct ieee80211_tim_ie *) &_ie[2];
935 if (!queue_empty(&fw.wlan.cab_queue[vif]) && (ie->dtim_count == 0)) {
936 /* schedule DTIM transfer */
937 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_ARMED;
938 } else if ((fw.wlan.cab_queue_len[vif] == 0) && (fw.wlan.cab_flush_trigger[vif])) {
939 /* undo all chances to the beacon structure */
940 ie->bitmap_ctrl &= ~0x1;
941 fw.wlan.cab_flush_trigger[vif] = CARL9170_CAB_TRIGGER_EMPTY;
944 /* Triggered by CARL9170_CAB_TRIGGER_ARMED || CARL9170_CAB_TRIGGER_DEFER */
945 if (fw.wlan.cab_flush_trigger[vif]) {
946 /* Set the almighty Multicast Traffic Indication Bit. */
947 ie->bitmap_ctrl |= 0x1;
952 * Ideally, the sequence number should be assigned by the TX arbiter
953 * hardware. But AFAIK that's not possible, so we have to go for the
954 * next best thing and write it into the beacon fifo during the open
955 * beacon update window.
958 wlan_assign_seq((struct ieee80211_hdr *)addr, vif);
960 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
962 static void handle_beacon_config(void)
966 bcn_count = get(AR9170_MAC_REG_BCN_COUNT);
967 send_cmd_to_host(4, CARL9170_RSP_BEACON_CONFIG, 0x00,
968 (uint8_t *) &bcn_count);
971 static void handle_pretbtt(void)
973 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
974 fw.wlan.cab_flush_time = get_clock_counter();
975 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
979 send_cmd_to_host(4, CARL9170_RSP_PRETBTT, 0x00,
980 (uint8_t *) &fw.phy.psm.state);
983 static void handle_atim(void)
985 send_cmd_to_host(0, CARL9170_RSP_ATIM, 0x00, NULL);
988 #ifdef CONFIG_CARL9170FW_DEBUG
989 static void handle_qos(void)
992 * What is the QoS Bit used for?
993 * Is it only an indicator for TXOP & Burst, or
994 * should we do something here?
998 static void handle_radar(void)
1000 send_cmd_to_host(0, CARL9170_RSP_RADAR, 0x00, NULL);
1002 #endif /* CONFIG_CARL9170FW_DEBUG */
1004 static void wlan_janitor(void)
1006 #ifdef CONFIG_CARL9170FW_CAB_QUEUE
1009 for (i = 0; i < CARL9170_INTF_NUM; i++) {
1010 if (unlikely(fw.wlan.cab_flush_trigger[i] == CARL9170_CAB_TRIGGER_ARMED)) {
1012 * This is hardcoded into carl9170usb driver.
1014 * The driver must set the PRETBTT event to beacon_interval -
1015 * CARL9170_PRETBTT_KUS (usually 6) Kus.
1017 * But still, we can only do so much about 802.11-2007 9.3.2.1 &
1018 * 11.2.1.6. Let's hope the current solution is adequate enough.
1021 if (is_after_msecs(fw.wlan.cab_flush_time, (CARL9170_TBTT_DELTA))) {
1022 wlan_cab_flush_queue(i);
1025 * This prevents the code from sending new BC/MC frames
1026 * which were queued after the previous buffered traffic
1027 * has been sent out... They will have to wait until the
1028 * next DTIM beacon comes along.
1030 fw.wlan.cab_flush_trigger[i] = CARL9170_CAB_TRIGGER_DEFER;
1035 #endif /* CONFIG_CARL9170FW_CAB_QUEUE */
1037 wlan_send_buffered_tx_status();
1039 wlan_send_buffered_ba();
1042 void handle_wlan(void)
1046 intr = get(AR9170_MAC_REG_INT_CTRL);
1048 set(AR9170_MAC_REG_INT_CTRL, intr);
1050 #define HANDLER(intr, flag, func) \
1052 if ((intr & flag) != 0) { \
1057 intr |= fw.wlan.soft_int;
1058 fw.wlan.soft_int = 0;
1060 HANDLER(intr, AR9170_MAC_INT_PRETBTT, handle_pretbtt);
1062 HANDLER(intr, AR9170_MAC_INT_ATIM, handle_atim);
1064 HANDLER(intr, AR9170_MAC_INT_RXC, handle_rx);
1066 HANDLER(intr, (AR9170_MAC_INT_TXC | AR9170_MAC_INT_RETRY_FAIL),
1067 handle_tx_completion);
1069 #ifdef CONFIG_CARL9170FW_DEBUG
1070 HANDLER(intr, AR9170_MAC_INT_QOS, handle_qos);
1072 HANDLER(intr, AR9170_MAC_INT_RADAR, handle_radar);
1073 #endif /* CONFIG_CARL9170FW_DEBUG */
1075 HANDLER(intr, AR9170_MAC_INT_CFG_BCN, handle_beacon_config);
1078 DBG("Unhandled Interrupt %x\n", (unsigned int) intr);
1086 CARL9170FW_TX_MAC_BUMP = 4,
1087 CARL9170FW_TX_MAC_DEBUG = 6,
1088 CARL9170FW_TX_MAC_RESET = 7,
1091 static void wlan_check_hang(void)
1093 struct dma_desc *desc;
1096 for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
1097 if (queue_empty(&fw.wlan.tx_queue[i])) {
1098 /* Nothing to do here... move along */
1102 /* fetch the current DMA queue position */
1103 desc = get_wlan_txq_addr(i);
1105 /* Stuck frame detection */
1106 if (unlikely(DESC_PAYLOAD(desc) == fw.wlan.last_super[i])) {
1107 fw.wlan.last_super_num[i]++;
1109 if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_RESET)) {
1111 * schedule MAC reset (aka OFF/ON => dead)
1113 * This will almost certainly kill
1114 * the device for good, but it's the
1115 * recommended thing to do...
1118 fw.wlan.mac_reset++;
1121 #ifdef CONFIG_CARL9170FW_DEBUG
1122 if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_DEBUG)) {
1124 * Sigh, the queue is almost certainly
1125 * dead. Dump the queue content to the
1126 * user, maybe we find out why it got
1132 #endif /* CONFIG_CARL9170FW_DEBUG */
1134 #ifdef CONFIG_CARL9170FW_DMA_QUEUE_BUMP
1135 if (unlikely(fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_BUMP)) {
1137 * Hrrm, bump the queue a bit.
1138 * maybe this will get it going again.
1142 wlan_trigger(BIT(i));
1144 #endif /* CONFIG_CARL9170FW_DMA_QUEUE_BUMP */
1147 fw.wlan.last_super[i] = DESC_PAYLOAD(desc);
1148 fw.wlan.last_super_num[i] = 0;
1153 #ifdef CONFIG_CARL9170FW_FW_MAC_RESET
1155 * NB: Resetting the MAC is a two-edged sword.
1156 * On most occasions, it does what it is supposed to do.
1157 * But there is a chance that this will make it
1158 * even worse and the radio dies silently.
1160 static void wlan_mac_reset(void)
1163 uint32_t agg_wait_counter;
1164 uint32_t agg_density;
1165 uint32_t bcn_start_addr;
1166 uint32_t rctl, rcth;
1169 uint32_t rts_cts_tpc;
1170 uint32_t rts_cts_rate;
1173 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1175 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1177 #ifdef CONFIG_CARL9170FW_NOISY_MAC_RESET
1179 #endif /* CONFIG_CARL9170FW_NOISY_MAC_RESET */
1181 /* Save aggregation parameters */
1182 agg_wait_counter = get(AR9170_MAC_REG_AMPDU_FACTOR);
1183 agg_density = get(AR9170_MAC_REG_AMPDU_DENSITY);
1185 bcn_start_addr = get(AR9170_MAC_REG_BCN_ADDR);
1187 cam_mode = get(AR9170_MAC_REG_CAM_MODE);
1188 rctl = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L);
1189 rcth = get(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H);
1191 ack_power = get(AR9170_MAC_REG_ACK_TPC);
1192 rts_cts_tpc = get(AR9170_MAC_REG_RTS_CTS_TPC);
1193 rts_cts_rate = get(AR9170_MAC_REG_RTS_CTS_RATE);
1195 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1196 /* 0x1c8960 write only */
1197 rx_BB = get(AR9170_PHY_REG_SWITCH_CHAIN_0);
1198 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1200 /* TX/RX must be stopped by now */
1201 val = get(AR9170_MAC_REG_POWER_STATE_CTRL);
1203 val |= AR9170_MAC_POWER_STATE_CTRL_RESET;
1206 * Manipulate CCA threshold to stop transmission
1208 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x300);
1212 * check Rx state in 0(idle) 9(disable)
1214 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1215 * while( (chState != 0) && (chState != 9)) {
1216 * chState = (get(AR9170_MAC_REG_MISC_684) >> 16) & 0xf;
1220 set(AR9170_MAC_REG_POWER_STATE_CTRL, val);
1224 /* Restore aggregation parameters */
1225 set(AR9170_MAC_REG_AMPDU_FACTOR, agg_wait_counter);
1226 set(AR9170_MAC_REG_AMPDU_DENSITY, agg_density);
1228 set(AR9170_MAC_REG_BCN_ADDR, bcn_start_addr);
1229 set(AR9170_MAC_REG_CAM_MODE, cam_mode);
1230 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_L, rctl);
1231 set(AR9170_MAC_REG_CAM_ROLL_CALL_TBL_H, rcth);
1233 set(AR9170_MAC_REG_RTS_CTS_TPC, rts_cts_tpc);
1234 set(AR9170_MAC_REG_ACK_TPC, ack_power);
1235 set(AR9170_MAC_REG_RTS_CTS_RATE, rts_cts_rate);
1237 #ifdef CONFIG_CARL9170FW_RADIO_FUNCTIONS
1238 set(AR9170_PHY_REG_SWITCH_CHAIN_2, rx_BB);
1239 #endif /* CONFIG_CARL9170FW_RADIO_FUNCTIONS */
1242 * Manipulate CCA threshold to resume transmission
1244 * set(AR9170_PHY_REG_CCA_THRESHOLD, 0x0);
1247 val = AR9170_DMA_TRIGGER_RXQ;
1248 /* Reinitialize all WLAN TX DMA queues. */
1249 for (i = AR9170_TXQ_SPECIAL; i >= AR9170_TXQ0; i--) {
1250 struct dma_desc *iter;
1252 __for_each_desc_bits(iter, &fw.wlan.tx_queue[i], AR9170_OWN_BITS_SW);
1254 /* kill the stuck frame */
1255 if (!is_terminator(&fw.wlan.tx_queue[i], iter) &&
1256 fw.wlan.last_super_num[i] >= CARL9170FW_TX_MAC_RESET &&
1257 fw.wlan.last_super[i] == DESC_PAYLOAD(iter)) {
1258 struct carl9170_tx_superframe *super = get_super(iter);
1260 iter->status = AR9170_OWN_BITS_SW;
1262 * Mark the frame as failed.
1263 * The BAFAIL flag allows the frame to sail through
1264 * wlan_tx_status without much "unstuck" trouble.
1266 iter->ctrl &= ~(AR9170_CTRL_FAIL);
1267 iter->ctrl |= AR9170_CTRL_BAFAIL;
1269 super->s.cnt = CARL9170_TX_MAX_RATE_TRIES;
1270 super->s.rix = CARL9170_TX_MAX_RETRY_RATES;
1272 fw.wlan.last_super_num[i] = 0;
1273 fw.wlan.last_super[i] = NULL;
1274 iter = iter->lastAddr->nextAddr;
1277 set_wlan_txq_dma_addr(i, (uint32_t) iter);
1278 if (!is_terminator(&fw.wlan.tx_queue[i], iter))
1281 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]),
1282 fw.wlan.tx_queue[i].head, fw.wlan.tx_queue[i].terminator,
1283 get_wlan_txq_addr(i), iter, iter->ctrl, iter->status);
1286 fw.wlan.soft_int |= AR9170_MAC_INT_RXC | AR9170_MAC_INT_TXC |
1287 AR9170_MAC_INT_RETRY_FAIL;
1289 set(AR9170_MAC_REG_DMA_RXQ_ADDR, (uint32_t) fw.wlan.rx_queue.head);
1293 static void wlan_mac_reset(void)
1295 /* The driver takes care of reinitializing the device */
1298 #endif /* CONFIG_CARL9170FW_FW_MAC_RESET */
1300 void __cold wlan_timer(void)
1302 unsigned int cached_mac_reset;
1304 cached_mac_reset = fw.wlan.mac_reset;
1306 /* TX Queue Hang check */
1309 /* RX Overrun check */
1310 wlan_check_rx_overrun();
1312 if (unlikely(fw.wlan.mac_reset >= CARL9170_MAC_RESET_RESET)) {
1314 fw.wlan.mac_reset = CARL9170_MAC_RESET_OFF;
1316 if (fw.wlan.mac_reset && cached_mac_reset == fw.wlan.mac_reset)
1317 fw.wlan.mac_reset--;