2 * Copyright (c) 2008-2009 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "ar9003_mac.h"
20 #define SKB_CB_ATHBUF(__skb) (*((struct ath_buf **)__skb->cb))
22 static inline bool ath_is_alt_ant_ratio_better(int alt_ratio, int maxdelta,
23 int mindelta, int main_rssi_avg,
24 int alt_rssi_avg, int pkt_count)
26 return (((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
27 (alt_rssi_avg > main_rssi_avg + maxdelta)) ||
28 (alt_rssi_avg > main_rssi_avg + mindelta)) && (pkt_count > 50);
31 static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
33 return sc->ps_enabled &&
34 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
38 * Setup and link descriptors.
40 * 11N: we can no longer afford to self link the last descriptor.
41 * MAC acknowledges BA status as long as it copies frames to host
42 * buffer (or rx fifo). This can incorrectly acknowledge packets
43 * to a sender if last desc is self-linked.
45 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
47 struct ath_hw *ah = sc->sc_ah;
48 struct ath_common *common = ath9k_hw_common(ah);
55 ds->ds_link = 0; /* link to null */
56 ds->ds_data = bf->bf_buf_addr;
58 /* virtual addr of the beginning of the buffer. */
61 ds->ds_vdata = skb->data;
64 * setup rx descriptors. The rx_bufsize here tells the hardware
65 * how much data it can DMA to us and that we are prepared
68 ath9k_hw_setuprxdesc(ah, ds,
72 if (sc->rx.rxlink == NULL)
73 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
75 *sc->rx.rxlink = bf->bf_daddr;
77 sc->rx.rxlink = &ds->ds_link;
81 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
83 /* XXX block beacon interrupts */
84 ath9k_hw_setantenna(sc->sc_ah, antenna);
85 sc->rx.defant = antenna;
86 sc->rx.rxotherant = 0;
89 static void ath_opmode_init(struct ath_softc *sc)
91 struct ath_hw *ah = sc->sc_ah;
92 struct ath_common *common = ath9k_hw_common(ah);
96 /* configure rx filter */
97 rfilt = ath_calcrxfilter(sc);
98 ath9k_hw_setrxfilter(ah, rfilt);
100 /* configure bssid mask */
101 ath_hw_setbssidmask(common);
103 /* configure operational mode */
104 ath9k_hw_setopmode(ah);
106 /* calculate and install multicast filter */
107 mfilt[0] = mfilt[1] = ~0;
108 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
111 static bool ath_rx_edma_buf_link(struct ath_softc *sc,
112 enum ath9k_rx_qtype qtype)
114 struct ath_hw *ah = sc->sc_ah;
115 struct ath_rx_edma *rx_edma;
119 rx_edma = &sc->rx.rx_edma[qtype];
120 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
123 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
124 list_del_init(&bf->list);
129 memset(skb->data, 0, ah->caps.rx_status_len);
130 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
131 ah->caps.rx_status_len, DMA_TO_DEVICE);
133 SKB_CB_ATHBUF(skb) = bf;
134 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
135 skb_queue_tail(&rx_edma->rx_fifo, skb);
140 static void ath_rx_addbuffer_edma(struct ath_softc *sc,
141 enum ath9k_rx_qtype qtype, int size)
143 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
146 if (list_empty(&sc->rx.rxbuf)) {
147 ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n");
151 while (!list_empty(&sc->rx.rxbuf)) {
154 if (!ath_rx_edma_buf_link(sc, qtype))
162 static void ath_rx_remove_buffer(struct ath_softc *sc,
163 enum ath9k_rx_qtype qtype)
166 struct ath_rx_edma *rx_edma;
169 rx_edma = &sc->rx.rx_edma[qtype];
171 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
172 bf = SKB_CB_ATHBUF(skb);
174 list_add_tail(&bf->list, &sc->rx.rxbuf);
178 static void ath_rx_edma_cleanup(struct ath_softc *sc)
182 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
183 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
185 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
187 dev_kfree_skb_any(bf->bf_mpdu);
190 INIT_LIST_HEAD(&sc->rx.rxbuf);
192 kfree(sc->rx.rx_bufptr);
193 sc->rx.rx_bufptr = NULL;
196 static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
198 skb_queue_head_init(&rx_edma->rx_fifo);
199 skb_queue_head_init(&rx_edma->rx_buffers);
200 rx_edma->rx_fifo_hwsize = size;
203 static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
205 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
206 struct ath_hw *ah = sc->sc_ah;
213 common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN +
214 ah->caps.rx_status_len,
215 min(common->cachelsz, (u16)64));
217 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
218 ah->caps.rx_status_len);
220 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
221 ah->caps.rx_lp_qdepth);
222 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
223 ah->caps.rx_hp_qdepth);
225 size = sizeof(struct ath_buf) * nbufs;
226 bf = kzalloc(size, GFP_KERNEL);
230 INIT_LIST_HEAD(&sc->rx.rxbuf);
231 sc->rx.rx_bufptr = bf;
233 for (i = 0; i < nbufs; i++, bf++) {
234 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
240 memset(skb->data, 0, common->rx_bufsize);
243 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
246 if (unlikely(dma_mapping_error(sc->dev,
248 dev_kfree_skb_any(skb);
252 "dma_mapping_error() on RX init\n");
257 list_add_tail(&bf->list, &sc->rx.rxbuf);
263 ath_rx_edma_cleanup(sc);
267 static void ath_edma_start_recv(struct ath_softc *sc)
269 spin_lock_bh(&sc->rx.rxbuflock);
271 ath9k_hw_rxena(sc->sc_ah);
273 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
274 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
276 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
277 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
281 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
283 spin_unlock_bh(&sc->rx.rxbuflock);
286 static void ath_edma_stop_recv(struct ath_softc *sc)
288 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
289 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
292 int ath_rx_init(struct ath_softc *sc, int nbufs)
294 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
299 spin_lock_init(&sc->sc_pcu_lock);
300 sc->sc_flags &= ~SC_OP_RXFLUSH;
301 spin_lock_init(&sc->rx.rxbuflock);
303 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
304 return ath_rx_edma_init(sc, nbufs);
306 common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN,
307 min(common->cachelsz, (u16)64));
309 ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
310 common->cachelsz, common->rx_bufsize);
312 /* Initialize rx descriptors */
314 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
318 "failed to allocate rx descriptors: %d\n",
323 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
324 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
332 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
335 if (unlikely(dma_mapping_error(sc->dev,
337 dev_kfree_skb_any(skb);
341 "dma_mapping_error() on RX init\n");
346 sc->rx.rxlink = NULL;
356 void ath_rx_cleanup(struct ath_softc *sc)
358 struct ath_hw *ah = sc->sc_ah;
359 struct ath_common *common = ath9k_hw_common(ah);
363 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
364 ath_rx_edma_cleanup(sc);
367 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
370 dma_unmap_single(sc->dev, bf->bf_buf_addr,
379 if (sc->rx.rxdma.dd_desc_len != 0)
380 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
385 * Calculate the receive filter according to the
386 * operating mode and state:
388 * o always accept unicast, broadcast, and multicast traffic
389 * o maintain current state of phy error reception (the hal
390 * may enable phy error frames for noise immunity work)
391 * o probe request frames are accepted only when operating in
392 * hostap, adhoc, or monitor modes
393 * o enable promiscuous mode according to the interface state
395 * - when operating in adhoc mode so the 802.11 layer creates
396 * node table entries for peers,
397 * - when operating in station mode for collecting rssi data when
398 * the station is otherwise quiet, or
399 * - when operating as a repeater so we see repeater-sta beacons
403 u32 ath_calcrxfilter(struct ath_softc *sc)
405 #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
409 rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE)
410 | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
411 | ATH9K_RX_FILTER_MCAST;
413 if (sc->rx.rxfilter & FIF_PROBE_REQ)
414 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
417 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
418 * mode interface or when in monitor mode. AP mode does not need this
419 * since it receives all in-BSS frames anyway.
421 if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) &&
422 (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) ||
423 (sc->sc_ah->is_monitoring))
424 rfilt |= ATH9K_RX_FILTER_PROM;
426 if (sc->rx.rxfilter & FIF_CONTROL)
427 rfilt |= ATH9K_RX_FILTER_CONTROL;
429 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
431 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
432 rfilt |= ATH9K_RX_FILTER_MYBEACON;
434 rfilt |= ATH9K_RX_FILTER_BEACON;
436 if ((AR_SREV_9280_20_OR_LATER(sc->sc_ah) ||
437 AR_SREV_9285_12_OR_LATER(sc->sc_ah)) &&
438 (sc->sc_ah->opmode == NL80211_IFTYPE_AP) &&
439 (sc->rx.rxfilter & FIF_PSPOLL))
440 rfilt |= ATH9K_RX_FILTER_PSPOLL;
442 if (conf_is_ht(&sc->hw->conf))
443 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
445 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
446 /* The following may also be needed for other older chips */
447 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
448 rfilt |= ATH9K_RX_FILTER_PROM;
449 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
454 #undef RX_FILTER_PRESERVE
457 int ath_startrecv(struct ath_softc *sc)
459 struct ath_hw *ah = sc->sc_ah;
460 struct ath_buf *bf, *tbf;
462 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
463 ath_edma_start_recv(sc);
467 spin_lock_bh(&sc->rx.rxbuflock);
468 if (list_empty(&sc->rx.rxbuf))
471 sc->rx.rxlink = NULL;
472 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
473 ath_rx_buf_link(sc, bf);
476 /* We could have deleted elements so the list may be empty now */
477 if (list_empty(&sc->rx.rxbuf))
480 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
481 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
486 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
488 spin_unlock_bh(&sc->rx.rxbuflock);
493 bool ath_stoprecv(struct ath_softc *sc)
495 struct ath_hw *ah = sc->sc_ah;
498 spin_lock_bh(&sc->rx.rxbuflock);
499 ath9k_hw_abortpcurecv(ah);
500 ath9k_hw_setrxfilter(ah, 0);
501 stopped = ath9k_hw_stopdmarecv(ah);
503 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
504 ath_edma_stop_recv(sc);
506 sc->rx.rxlink = NULL;
507 spin_unlock_bh(&sc->rx.rxbuflock);
509 if (!(ah->ah_flags & AH_UNPLUGGED) &&
510 unlikely(!stopped)) {
511 ath_err(ath9k_hw_common(sc->sc_ah),
512 "Could not stop RX, we could be "
513 "confusing the DMA engine when we start RX up\n");
514 ATH_DBG_WARN_ON_ONCE(!stopped);
519 void ath_flushrecv(struct ath_softc *sc)
521 sc->sc_flags |= SC_OP_RXFLUSH;
522 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
523 ath_rx_tasklet(sc, 1, true);
524 ath_rx_tasklet(sc, 1, false);
525 sc->sc_flags &= ~SC_OP_RXFLUSH;
528 static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
530 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
531 struct ieee80211_mgmt *mgmt;
532 u8 *pos, *end, id, elen;
533 struct ieee80211_tim_ie *tim;
535 mgmt = (struct ieee80211_mgmt *)skb->data;
536 pos = mgmt->u.beacon.variable;
537 end = skb->data + skb->len;
539 while (pos + 2 < end) {
542 if (pos + elen > end)
545 if (id == WLAN_EID_TIM) {
546 if (elen < sizeof(*tim))
548 tim = (struct ieee80211_tim_ie *) pos;
549 if (tim->dtim_count != 0)
551 return tim->bitmap_ctrl & 0x01;
560 static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
562 struct ieee80211_mgmt *mgmt;
563 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
565 if (skb->len < 24 + 8 + 2 + 2)
568 mgmt = (struct ieee80211_mgmt *)skb->data;
569 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
570 /* TODO: This doesn't work well if you have stations
571 * associated to two different APs because curbssid
572 * is just the last AP that any of the stations associated
575 return; /* not from our current AP */
578 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
580 if (sc->ps_flags & PS_BEACON_SYNC) {
581 sc->ps_flags &= ~PS_BEACON_SYNC;
582 ath_dbg(common, ATH_DBG_PS,
583 "Reconfigure Beacon timers based on timestamp from the AP\n");
584 ath_beacon_config(sc, NULL);
587 if (ath_beacon_dtim_pending_cab(skb)) {
589 * Remain awake waiting for buffered broadcast/multicast
590 * frames. If the last broadcast/multicast frame is not
591 * received properly, the next beacon frame will work as
592 * a backup trigger for returning into NETWORK SLEEP state,
593 * so we are waiting for it as well.
595 ath_dbg(common, ATH_DBG_PS,
596 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
597 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
601 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
603 * This can happen if a broadcast frame is dropped or the AP
604 * fails to send a frame indicating that all CAB frames have
607 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
608 ath_dbg(common, ATH_DBG_PS,
609 "PS wait for CAB frames timed out\n");
613 static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
615 struct ieee80211_hdr *hdr;
616 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
618 hdr = (struct ieee80211_hdr *)skb->data;
620 /* Process Beacon and CAB receive in PS state */
621 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
622 && ieee80211_is_beacon(hdr->frame_control))
623 ath_rx_ps_beacon(sc, skb);
624 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
625 (ieee80211_is_data(hdr->frame_control) ||
626 ieee80211_is_action(hdr->frame_control)) &&
627 is_multicast_ether_addr(hdr->addr1) &&
628 !ieee80211_has_moredata(hdr->frame_control)) {
630 * No more broadcast/multicast frames to be received at this
633 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
634 ath_dbg(common, ATH_DBG_PS,
635 "All PS CAB frames received, back to sleep\n");
636 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
637 !is_multicast_ether_addr(hdr->addr1) &&
638 !ieee80211_has_morefrags(hdr->frame_control)) {
639 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
640 ath_dbg(common, ATH_DBG_PS,
641 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
642 sc->ps_flags & (PS_WAIT_FOR_BEACON |
644 PS_WAIT_FOR_PSPOLL_DATA |
645 PS_WAIT_FOR_TX_ACK));
649 static bool ath_edma_get_buffers(struct ath_softc *sc,
650 enum ath9k_rx_qtype qtype)
652 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
653 struct ath_hw *ah = sc->sc_ah;
654 struct ath_common *common = ath9k_hw_common(ah);
659 skb = skb_peek(&rx_edma->rx_fifo);
663 bf = SKB_CB_ATHBUF(skb);
666 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
667 common->rx_bufsize, DMA_FROM_DEVICE);
669 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
670 if (ret == -EINPROGRESS) {
671 /*let device gain the buffer again*/
672 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
673 common->rx_bufsize, DMA_FROM_DEVICE);
677 __skb_unlink(skb, &rx_edma->rx_fifo);
678 if (ret == -EINVAL) {
679 /* corrupt descriptor, skip this one and the following one */
680 list_add_tail(&bf->list, &sc->rx.rxbuf);
681 ath_rx_edma_buf_link(sc, qtype);
682 skb = skb_peek(&rx_edma->rx_fifo);
686 bf = SKB_CB_ATHBUF(skb);
689 __skb_unlink(skb, &rx_edma->rx_fifo);
690 list_add_tail(&bf->list, &sc->rx.rxbuf);
691 ath_rx_edma_buf_link(sc, qtype);
694 skb_queue_tail(&rx_edma->rx_buffers, skb);
699 static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
700 struct ath_rx_status *rs,
701 enum ath9k_rx_qtype qtype)
703 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
707 while (ath_edma_get_buffers(sc, qtype));
708 skb = __skb_dequeue(&rx_edma->rx_buffers);
712 bf = SKB_CB_ATHBUF(skb);
713 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
717 static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
718 struct ath_rx_status *rs)
720 struct ath_hw *ah = sc->sc_ah;
721 struct ath_common *common = ath9k_hw_common(ah);
726 if (list_empty(&sc->rx.rxbuf)) {
727 sc->rx.rxlink = NULL;
731 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
735 * Must provide the virtual address of the current
736 * descriptor, the physical address, and the virtual
737 * address of the next descriptor in the h/w chain.
738 * This allows the HAL to look ahead to see if the
739 * hardware is done with a descriptor by checking the
740 * done bit in the following descriptor and the address
741 * of the current descriptor the DMA engine is working
742 * on. All this is necessary because of our use of
743 * a self-linked list to avoid rx overruns.
745 ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
746 if (ret == -EINPROGRESS) {
747 struct ath_rx_status trs;
749 struct ath_desc *tds;
751 memset(&trs, 0, sizeof(trs));
752 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
753 sc->rx.rxlink = NULL;
757 tbf = list_entry(bf->list.next, struct ath_buf, list);
760 * On some hardware the descriptor status words could
761 * get corrupted, including the done bit. Because of
762 * this, check if the next descriptor's done bit is
765 * If the next descriptor's done bit is set, the current
766 * descriptor has been corrupted. Force s/w to discard
767 * this descriptor and continue...
771 ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
772 if (ret == -EINPROGRESS)
780 * Synchronize the DMA transfer with CPU before
781 * 1. accessing the frame
782 * 2. requeueing the same buffer to h/w
784 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
791 /* Assumes you've already done the endian to CPU conversion */
792 static bool ath9k_rx_accept(struct ath_common *common,
793 struct ieee80211_hdr *hdr,
794 struct ieee80211_rx_status *rxs,
795 struct ath_rx_status *rx_stats,
798 #define is_mc_or_valid_tkip_keyix ((is_mc || \
799 (rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \
800 test_bit(rx_stats->rs_keyix, common->tkip_keymap))))
802 struct ath_hw *ah = common->ah;
804 u8 rx_status_len = ah->caps.rx_status_len;
806 fc = hdr->frame_control;
808 if (!rx_stats->rs_datalen)
811 * rs_status follows rs_datalen so if rs_datalen is too large
812 * we can take a hint that hardware corrupted it, so ignore
815 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
819 * rs_more indicates chained descriptors which can be used
820 * to link buffers together for a sort of scatter-gather
822 * reject the frame, we don't support scatter-gather yet and
823 * the frame is probably corrupt anyway
825 if (rx_stats->rs_more)
829 * The rx_stats->rs_status will not be set until the end of the
830 * chained descriptors so it can be ignored if rs_more is set. The
831 * rs_more will be false at the last element of the chained
834 if (rx_stats->rs_status != 0) {
835 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
836 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
837 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
840 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
841 *decrypt_error = true;
842 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
845 * The MIC error bit is only valid if the frame
846 * is not a control frame or fragment, and it was
847 * decrypted using a valid TKIP key.
849 is_mc = !!is_multicast_ether_addr(hdr->addr1);
851 if (!ieee80211_is_ctl(fc) &&
852 !ieee80211_has_morefrags(fc) &&
853 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
854 is_mc_or_valid_tkip_keyix)
855 rxs->flag |= RX_FLAG_MMIC_ERROR;
857 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
860 * Reject error frames with the exception of
861 * decryption and MIC failures. For monitor mode,
862 * we also ignore the CRC error.
864 if (ah->is_monitoring) {
865 if (rx_stats->rs_status &
866 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
870 if (rx_stats->rs_status &
871 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
879 static int ath9k_process_rate(struct ath_common *common,
880 struct ieee80211_hw *hw,
881 struct ath_rx_status *rx_stats,
882 struct ieee80211_rx_status *rxs)
884 struct ieee80211_supported_band *sband;
885 enum ieee80211_band band;
888 band = hw->conf.channel->band;
889 sband = hw->wiphy->bands[band];
891 if (rx_stats->rs_rate & 0x80) {
893 rxs->flag |= RX_FLAG_HT;
894 if (rx_stats->rs_flags & ATH9K_RX_2040)
895 rxs->flag |= RX_FLAG_40MHZ;
896 if (rx_stats->rs_flags & ATH9K_RX_GI)
897 rxs->flag |= RX_FLAG_SHORT_GI;
898 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
902 for (i = 0; i < sband->n_bitrates; i++) {
903 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
907 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
908 rxs->flag |= RX_FLAG_SHORTPRE;
915 * No valid hardware bitrate found -- we should not get here
916 * because hardware has already validated this frame as OK.
918 ath_dbg(common, ATH_DBG_XMIT,
919 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
925 static void ath9k_process_rssi(struct ath_common *common,
926 struct ieee80211_hw *hw,
927 struct ieee80211_hdr *hdr,
928 struct ath_rx_status *rx_stats)
930 struct ath_wiphy *aphy = hw->priv;
931 struct ath_hw *ah = common->ah;
935 if (ah->opmode != NL80211_IFTYPE_STATION)
938 fc = hdr->frame_control;
939 if (!ieee80211_is_beacon(fc) ||
940 compare_ether_addr(hdr->addr3, common->curbssid)) {
941 /* TODO: This doesn't work well if you have stations
942 * associated to two different APs because curbssid
943 * is just the last AP that any of the stations associated
949 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
950 ATH_RSSI_LPF(aphy->last_rssi, rx_stats->rs_rssi);
952 last_rssi = aphy->last_rssi;
953 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
954 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
955 ATH_RSSI_EP_MULTIPLIER);
956 if (rx_stats->rs_rssi < 0)
957 rx_stats->rs_rssi = 0;
959 /* Update Beacon RSSI, this is used by ANI. */
960 ah->stats.avgbrssi = rx_stats->rs_rssi;
964 * For Decrypt or Demic errors, we only mark packet status here and always push
965 * up the frame up to let mac80211 handle the actual error case, be it no
966 * decryption key or real decryption error. This let us keep statistics there.
968 static int ath9k_rx_skb_preprocess(struct ath_common *common,
969 struct ieee80211_hw *hw,
970 struct ieee80211_hdr *hdr,
971 struct ath_rx_status *rx_stats,
972 struct ieee80211_rx_status *rx_status,
975 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
978 * everything but the rate is checked here, the rate check is done
979 * separately to avoid doing two lookups for a rate for each frame.
981 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
984 ath9k_process_rssi(common, hw, hdr, rx_stats);
986 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
989 rx_status->band = hw->conf.channel->band;
990 rx_status->freq = hw->conf.channel->center_freq;
991 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
992 rx_status->antenna = rx_stats->rs_antenna;
993 rx_status->flag |= RX_FLAG_TSFT;
998 static void ath9k_rx_skb_postprocess(struct ath_common *common,
1000 struct ath_rx_status *rx_stats,
1001 struct ieee80211_rx_status *rxs,
1004 struct ath_hw *ah = common->ah;
1005 struct ieee80211_hdr *hdr;
1006 int hdrlen, padpos, padsize;
1010 /* see if any padding is done by the hw and remove it */
1011 hdr = (struct ieee80211_hdr *) skb->data;
1012 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1013 fc = hdr->frame_control;
1014 padpos = ath9k_cmn_padpos(hdr->frame_control);
1016 /* The MAC header is padded to have 32-bit boundary if the
1017 * packet payload is non-zero. The general calculation for
1018 * padsize would take into account odd header lengths:
1019 * padsize = (4 - padpos % 4) % 4; However, since only
1020 * even-length headers are used, padding can only be 0 or 2
1021 * bytes and we can optimize this a bit. In addition, we must
1022 * not try to remove padding from short control frames that do
1023 * not have payload. */
1024 padsize = padpos & 3;
1025 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1026 memmove(skb->data + padsize, skb->data, padpos);
1027 skb_pull(skb, padsize);
1030 keyix = rx_stats->rs_keyix;
1032 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1033 ieee80211_has_protected(fc)) {
1034 rxs->flag |= RX_FLAG_DECRYPTED;
1035 } else if (ieee80211_has_protected(fc)
1036 && !decrypt_error && skb->len >= hdrlen + 4) {
1037 keyix = skb->data[hdrlen + 3] >> 6;
1039 if (test_bit(keyix, common->keymap))
1040 rxs->flag |= RX_FLAG_DECRYPTED;
1042 if (ah->sw_mgmt_crypto &&
1043 (rxs->flag & RX_FLAG_DECRYPTED) &&
1044 ieee80211_is_mgmt(fc))
1045 /* Use software decrypt for management frames. */
1046 rxs->flag &= ~RX_FLAG_DECRYPTED;
1049 static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1050 struct ath_hw_antcomb_conf ant_conf,
1053 antcomb->quick_scan_cnt = 0;
1055 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1056 antcomb->rssi_lna2 = main_rssi_avg;
1057 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1058 antcomb->rssi_lna1 = main_rssi_avg;
1060 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1061 case (0x10): /* LNA2 A-B */
1062 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1063 antcomb->first_quick_scan_conf =
1064 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1065 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1067 case (0x20): /* LNA1 A-B */
1068 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1069 antcomb->first_quick_scan_conf =
1070 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1071 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1073 case (0x21): /* LNA1 LNA2 */
1074 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1075 antcomb->first_quick_scan_conf =
1076 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1077 antcomb->second_quick_scan_conf =
1078 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1080 case (0x12): /* LNA2 LNA1 */
1081 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1082 antcomb->first_quick_scan_conf =
1083 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1084 antcomb->second_quick_scan_conf =
1085 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1087 case (0x13): /* LNA2 A+B */
1088 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1089 antcomb->first_quick_scan_conf =
1090 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1091 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1093 case (0x23): /* LNA1 A+B */
1094 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1095 antcomb->first_quick_scan_conf =
1096 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1097 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1104 static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1105 struct ath_hw_antcomb_conf *div_ant_conf,
1106 int main_rssi_avg, int alt_rssi_avg,
1110 switch (antcomb->quick_scan_cnt) {
1112 /* set alt to main, and alt to first conf */
1113 div_ant_conf->main_lna_conf = antcomb->main_conf;
1114 div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1117 /* set alt to main, and alt to first conf */
1118 div_ant_conf->main_lna_conf = antcomb->main_conf;
1119 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1120 antcomb->rssi_first = main_rssi_avg;
1121 antcomb->rssi_second = alt_rssi_avg;
1123 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1125 if (ath_is_alt_ant_ratio_better(alt_ratio,
1126 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1127 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1128 main_rssi_avg, alt_rssi_avg,
1129 antcomb->total_pkt_count))
1130 antcomb->first_ratio = true;
1132 antcomb->first_ratio = false;
1133 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1134 if (ath_is_alt_ant_ratio_better(alt_ratio,
1135 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1136 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1137 main_rssi_avg, alt_rssi_avg,
1138 antcomb->total_pkt_count))
1139 antcomb->first_ratio = true;
1141 antcomb->first_ratio = false;
1143 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1144 (alt_rssi_avg > main_rssi_avg +
1145 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1146 (alt_rssi_avg > main_rssi_avg)) &&
1147 (antcomb->total_pkt_count > 50))
1148 antcomb->first_ratio = true;
1150 antcomb->first_ratio = false;
1154 antcomb->alt_good = false;
1155 antcomb->scan_not_start = false;
1156 antcomb->scan = false;
1157 antcomb->rssi_first = main_rssi_avg;
1158 antcomb->rssi_third = alt_rssi_avg;
1160 if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1161 antcomb->rssi_lna1 = alt_rssi_avg;
1162 else if (antcomb->second_quick_scan_conf ==
1163 ATH_ANT_DIV_COMB_LNA2)
1164 antcomb->rssi_lna2 = alt_rssi_avg;
1165 else if (antcomb->second_quick_scan_conf ==
1166 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1167 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1168 antcomb->rssi_lna2 = main_rssi_avg;
1169 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1170 antcomb->rssi_lna1 = main_rssi_avg;
1173 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1174 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1175 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1177 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1179 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1180 if (ath_is_alt_ant_ratio_better(alt_ratio,
1181 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1182 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1183 main_rssi_avg, alt_rssi_avg,
1184 antcomb->total_pkt_count))
1185 antcomb->second_ratio = true;
1187 antcomb->second_ratio = false;
1188 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1189 if (ath_is_alt_ant_ratio_better(alt_ratio,
1190 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1191 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1192 main_rssi_avg, alt_rssi_avg,
1193 antcomb->total_pkt_count))
1194 antcomb->second_ratio = true;
1196 antcomb->second_ratio = false;
1198 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1199 (alt_rssi_avg > main_rssi_avg +
1200 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1201 (alt_rssi_avg > main_rssi_avg)) &&
1202 (antcomb->total_pkt_count > 50))
1203 antcomb->second_ratio = true;
1205 antcomb->second_ratio = false;
1208 /* set alt to the conf with maximun ratio */
1209 if (antcomb->first_ratio && antcomb->second_ratio) {
1210 if (antcomb->rssi_second > antcomb->rssi_third) {
1212 if ((antcomb->first_quick_scan_conf ==
1213 ATH_ANT_DIV_COMB_LNA1) ||
1214 (antcomb->first_quick_scan_conf ==
1215 ATH_ANT_DIV_COMB_LNA2))
1216 /* Set alt LNA1 or LNA2*/
1217 if (div_ant_conf->main_lna_conf ==
1218 ATH_ANT_DIV_COMB_LNA2)
1219 div_ant_conf->alt_lna_conf =
1220 ATH_ANT_DIV_COMB_LNA1;
1222 div_ant_conf->alt_lna_conf =
1223 ATH_ANT_DIV_COMB_LNA2;
1225 /* Set alt to A+B or A-B */
1226 div_ant_conf->alt_lna_conf =
1227 antcomb->first_quick_scan_conf;
1228 } else if ((antcomb->second_quick_scan_conf ==
1229 ATH_ANT_DIV_COMB_LNA1) ||
1230 (antcomb->second_quick_scan_conf ==
1231 ATH_ANT_DIV_COMB_LNA2)) {
1232 /* Set alt LNA1 or LNA2 */
1233 if (div_ant_conf->main_lna_conf ==
1234 ATH_ANT_DIV_COMB_LNA2)
1235 div_ant_conf->alt_lna_conf =
1236 ATH_ANT_DIV_COMB_LNA1;
1238 div_ant_conf->alt_lna_conf =
1239 ATH_ANT_DIV_COMB_LNA2;
1241 /* Set alt to A+B or A-B */
1242 div_ant_conf->alt_lna_conf =
1243 antcomb->second_quick_scan_conf;
1245 } else if (antcomb->first_ratio) {
1247 if ((antcomb->first_quick_scan_conf ==
1248 ATH_ANT_DIV_COMB_LNA1) ||
1249 (antcomb->first_quick_scan_conf ==
1250 ATH_ANT_DIV_COMB_LNA2))
1251 /* Set alt LNA1 or LNA2 */
1252 if (div_ant_conf->main_lna_conf ==
1253 ATH_ANT_DIV_COMB_LNA2)
1254 div_ant_conf->alt_lna_conf =
1255 ATH_ANT_DIV_COMB_LNA1;
1257 div_ant_conf->alt_lna_conf =
1258 ATH_ANT_DIV_COMB_LNA2;
1260 /* Set alt to A+B or A-B */
1261 div_ant_conf->alt_lna_conf =
1262 antcomb->first_quick_scan_conf;
1263 } else if (antcomb->second_ratio) {
1265 if ((antcomb->second_quick_scan_conf ==
1266 ATH_ANT_DIV_COMB_LNA1) ||
1267 (antcomb->second_quick_scan_conf ==
1268 ATH_ANT_DIV_COMB_LNA2))
1269 /* Set alt LNA1 or LNA2 */
1270 if (div_ant_conf->main_lna_conf ==
1271 ATH_ANT_DIV_COMB_LNA2)
1272 div_ant_conf->alt_lna_conf =
1273 ATH_ANT_DIV_COMB_LNA1;
1275 div_ant_conf->alt_lna_conf =
1276 ATH_ANT_DIV_COMB_LNA2;
1278 /* Set alt to A+B or A-B */
1279 div_ant_conf->alt_lna_conf =
1280 antcomb->second_quick_scan_conf;
1282 /* main is largest */
1283 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1284 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1285 /* Set alt LNA1 or LNA2 */
1286 if (div_ant_conf->main_lna_conf ==
1287 ATH_ANT_DIV_COMB_LNA2)
1288 div_ant_conf->alt_lna_conf =
1289 ATH_ANT_DIV_COMB_LNA1;
1291 div_ant_conf->alt_lna_conf =
1292 ATH_ANT_DIV_COMB_LNA2;
1294 /* Set alt to A+B or A-B */
1295 div_ant_conf->alt_lna_conf = antcomb->main_conf;
1303 static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf)
1305 /* Adjust the fast_div_bias based on main and alt lna conf */
1306 switch ((ant_conf->main_lna_conf << 4) | ant_conf->alt_lna_conf) {
1307 case (0x01): /* A-B LNA2 */
1308 ant_conf->fast_div_bias = 0x3b;
1310 case (0x02): /* A-B LNA1 */
1311 ant_conf->fast_div_bias = 0x3d;
1313 case (0x03): /* A-B A+B */
1314 ant_conf->fast_div_bias = 0x1;
1316 case (0x10): /* LNA2 A-B */
1317 ant_conf->fast_div_bias = 0x7;
1319 case (0x12): /* LNA2 LNA1 */
1320 ant_conf->fast_div_bias = 0x2;
1322 case (0x13): /* LNA2 A+B */
1323 ant_conf->fast_div_bias = 0x7;
1325 case (0x20): /* LNA1 A-B */
1326 ant_conf->fast_div_bias = 0x6;
1328 case (0x21): /* LNA1 LNA2 */
1329 ant_conf->fast_div_bias = 0x0;
1331 case (0x23): /* LNA1 A+B */
1332 ant_conf->fast_div_bias = 0x6;
1334 case (0x30): /* A+B A-B */
1335 ant_conf->fast_div_bias = 0x1;
1337 case (0x31): /* A+B LNA2 */
1338 ant_conf->fast_div_bias = 0x3b;
1340 case (0x32): /* A+B LNA1 */
1341 ant_conf->fast_div_bias = 0x3d;
1348 /* Antenna diversity and combining */
1349 static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1351 struct ath_hw_antcomb_conf div_ant_conf;
1352 struct ath_ant_comb *antcomb = &sc->ant_comb;
1353 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
1354 int curr_main_set, curr_bias;
1355 int main_rssi = rs->rs_rssi_ctl0;
1356 int alt_rssi = rs->rs_rssi_ctl1;
1357 int rx_ant_conf, main_ant_conf;
1358 bool short_scan = false;
1360 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1362 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1365 /* Record packet only when alt_rssi is positive */
1367 antcomb->total_pkt_count++;
1368 antcomb->main_total_rssi += main_rssi;
1369 antcomb->alt_total_rssi += alt_rssi;
1370 if (main_ant_conf == rx_ant_conf)
1371 antcomb->main_recv_cnt++;
1373 antcomb->alt_recv_cnt++;
1376 /* Short scan check */
1377 if (antcomb->scan && antcomb->alt_good) {
1378 if (time_after(jiffies, antcomb->scan_start_time +
1379 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1382 if (antcomb->total_pkt_count ==
1383 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1384 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1385 antcomb->total_pkt_count);
1386 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1391 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1392 rs->rs_moreaggr) && !short_scan)
1395 if (antcomb->total_pkt_count) {
1396 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1397 antcomb->total_pkt_count);
1398 main_rssi_avg = (antcomb->main_total_rssi /
1399 antcomb->total_pkt_count);
1400 alt_rssi_avg = (antcomb->alt_total_rssi /
1401 antcomb->total_pkt_count);
1405 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1406 curr_alt_set = div_ant_conf.alt_lna_conf;
1407 curr_main_set = div_ant_conf.main_lna_conf;
1408 curr_bias = div_ant_conf.fast_div_bias;
1412 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1413 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1414 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1416 antcomb->alt_good = true;
1418 antcomb->alt_good = false;
1422 antcomb->scan = true;
1423 antcomb->scan_not_start = true;
1426 if (!antcomb->scan) {
1427 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1428 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1429 /* Switch main and alt LNA */
1430 div_ant_conf.main_lna_conf =
1431 ATH_ANT_DIV_COMB_LNA2;
1432 div_ant_conf.alt_lna_conf =
1433 ATH_ANT_DIV_COMB_LNA1;
1434 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1435 div_ant_conf.main_lna_conf =
1436 ATH_ANT_DIV_COMB_LNA1;
1437 div_ant_conf.alt_lna_conf =
1438 ATH_ANT_DIV_COMB_LNA2;
1442 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1443 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1444 /* Set alt to another LNA */
1445 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1446 div_ant_conf.alt_lna_conf =
1447 ATH_ANT_DIV_COMB_LNA1;
1448 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1449 div_ant_conf.alt_lna_conf =
1450 ATH_ANT_DIV_COMB_LNA2;
1455 if ((alt_rssi_avg < (main_rssi_avg +
1456 ATH_ANT_DIV_COMB_LNA1_LNA2_DELTA)))
1460 if (!antcomb->scan_not_start) {
1461 switch (curr_alt_set) {
1462 case ATH_ANT_DIV_COMB_LNA2:
1463 antcomb->rssi_lna2 = alt_rssi_avg;
1464 antcomb->rssi_lna1 = main_rssi_avg;
1465 antcomb->scan = true;
1467 div_ant_conf.main_lna_conf =
1468 ATH_ANT_DIV_COMB_LNA1;
1469 div_ant_conf.alt_lna_conf =
1470 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1472 case ATH_ANT_DIV_COMB_LNA1:
1473 antcomb->rssi_lna1 = alt_rssi_avg;
1474 antcomb->rssi_lna2 = main_rssi_avg;
1475 antcomb->scan = true;
1477 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1478 div_ant_conf.alt_lna_conf =
1479 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1481 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1482 antcomb->rssi_add = alt_rssi_avg;
1483 antcomb->scan = true;
1485 div_ant_conf.alt_lna_conf =
1486 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1488 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1489 antcomb->rssi_sub = alt_rssi_avg;
1490 antcomb->scan = false;
1491 if (antcomb->rssi_lna2 >
1492 (antcomb->rssi_lna1 +
1493 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1494 /* use LNA2 as main LNA */
1495 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1496 (antcomb->rssi_add > antcomb->rssi_sub)) {
1498 div_ant_conf.main_lna_conf =
1499 ATH_ANT_DIV_COMB_LNA2;
1500 div_ant_conf.alt_lna_conf =
1501 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1502 } else if (antcomb->rssi_sub >
1503 antcomb->rssi_lna1) {
1505 div_ant_conf.main_lna_conf =
1506 ATH_ANT_DIV_COMB_LNA2;
1507 div_ant_conf.alt_lna_conf =
1508 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1511 div_ant_conf.main_lna_conf =
1512 ATH_ANT_DIV_COMB_LNA2;
1513 div_ant_conf.alt_lna_conf =
1514 ATH_ANT_DIV_COMB_LNA1;
1517 /* use LNA1 as main LNA */
1518 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1519 (antcomb->rssi_add > antcomb->rssi_sub)) {
1521 div_ant_conf.main_lna_conf =
1522 ATH_ANT_DIV_COMB_LNA1;
1523 div_ant_conf.alt_lna_conf =
1524 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1525 } else if (antcomb->rssi_sub >
1526 antcomb->rssi_lna1) {
1528 div_ant_conf.main_lna_conf =
1529 ATH_ANT_DIV_COMB_LNA1;
1530 div_ant_conf.alt_lna_conf =
1531 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1534 div_ant_conf.main_lna_conf =
1535 ATH_ANT_DIV_COMB_LNA1;
1536 div_ant_conf.alt_lna_conf =
1537 ATH_ANT_DIV_COMB_LNA2;
1545 if (!antcomb->alt_good) {
1546 antcomb->scan_not_start = false;
1547 /* Set alt to another LNA */
1548 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1549 div_ant_conf.main_lna_conf =
1550 ATH_ANT_DIV_COMB_LNA2;
1551 div_ant_conf.alt_lna_conf =
1552 ATH_ANT_DIV_COMB_LNA1;
1553 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1554 div_ant_conf.main_lna_conf =
1555 ATH_ANT_DIV_COMB_LNA1;
1556 div_ant_conf.alt_lna_conf =
1557 ATH_ANT_DIV_COMB_LNA2;
1563 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1564 main_rssi_avg, alt_rssi_avg,
1567 antcomb->quick_scan_cnt++;
1570 ath_ant_div_conf_fast_divbias(&div_ant_conf);
1572 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1574 antcomb->scan_start_time = jiffies;
1575 antcomb->total_pkt_count = 0;
1576 antcomb->main_total_rssi = 0;
1577 antcomb->alt_total_rssi = 0;
1578 antcomb->main_recv_cnt = 0;
1579 antcomb->alt_recv_cnt = 0;
1582 int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1585 struct sk_buff *skb = NULL, *requeue_skb;
1586 struct ieee80211_rx_status *rxs;
1587 struct ath_hw *ah = sc->sc_ah;
1588 struct ath_common *common = ath9k_hw_common(ah);
1590 * The hw can technically differ from common->hw when using ath9k
1591 * virtual wiphy so to account for that we iterate over the active
1592 * wiphys and find the appropriate wiphy and therefore hw.
1594 struct ieee80211_hw *hw = sc->hw;
1595 struct ieee80211_hdr *hdr;
1597 bool decrypt_error = false;
1598 struct ath_rx_status rs;
1599 enum ath9k_rx_qtype qtype;
1600 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1602 u8 rx_status_len = ah->caps.rx_status_len;
1605 unsigned long flags;
1608 dma_type = DMA_BIDIRECTIONAL;
1610 dma_type = DMA_FROM_DEVICE;
1612 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1613 spin_lock_bh(&sc->rx.rxbuflock);
1615 tsf = ath9k_hw_gettsf64(ah);
1616 tsf_lower = tsf & 0xffffffff;
1619 /* If handling rx interrupt and flush is in progress => exit */
1620 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
1623 memset(&rs, 0, sizeof(rs));
1625 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1627 bf = ath_get_next_rx_buf(sc, &rs);
1636 hdr = (struct ieee80211_hdr *) (skb->data + rx_status_len);
1637 rxs = IEEE80211_SKB_RXCB(skb);
1639 ath_debug_stat_rx(sc, &rs);
1642 * If we're asked to flush receive queue, directly
1643 * chain it back at the queue without processing it.
1648 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1649 rxs, &decrypt_error);
1653 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1654 if (rs.rs_tstamp > tsf_lower &&
1655 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1656 rxs->mactime -= 0x100000000ULL;
1658 if (rs.rs_tstamp < tsf_lower &&
1659 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1660 rxs->mactime += 0x100000000ULL;
1662 /* Ensure we always have an skb to requeue once we are done
1663 * processing the current buffer's skb */
1664 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1666 /* If there is no memory we ignore the current RX'd frame,
1667 * tell hardware it can give us a new frame using the old
1668 * skb and put it at the tail of the sc->rx.rxbuf list for
1673 /* Unmap the frame */
1674 dma_unmap_single(sc->dev, bf->bf_buf_addr,
1678 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1679 if (ah->caps.rx_status_len)
1680 skb_pull(skb, ah->caps.rx_status_len);
1682 ath9k_rx_skb_postprocess(common, skb, &rs,
1683 rxs, decrypt_error);
1685 /* We will now give hardware our shiny new allocated skb */
1686 bf->bf_mpdu = requeue_skb;
1687 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1690 if (unlikely(dma_mapping_error(sc->dev,
1691 bf->bf_buf_addr))) {
1692 dev_kfree_skb_any(requeue_skb);
1694 bf->bf_buf_addr = 0;
1695 ath_err(common, "dma_mapping_error() on RX\n");
1696 ieee80211_rx(hw, skb);
1701 * change the default rx antenna if rx diversity chooses the
1702 * other antenna 3 times in a row.
1704 if (sc->rx.defant != rs.rs_antenna) {
1705 if (++sc->rx.rxotherant >= 3)
1706 ath_setdefantenna(sc, rs.rs_antenna);
1708 sc->rx.rxotherant = 0;
1711 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1713 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
1715 PS_WAIT_FOR_PSPOLL_DATA)) ||
1716 unlikely(ath9k_check_auto_sleep(sc)))
1718 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1720 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1721 ath_ant_comb_scan(sc, &rs);
1723 ieee80211_rx(hw, skb);
1727 list_add_tail(&bf->list, &sc->rx.rxbuf);
1728 ath_rx_edma_buf_link(sc, qtype);
1730 list_move_tail(&bf->list, &sc->rx.rxbuf);
1731 ath_rx_buf_link(sc, bf);
1735 spin_unlock_bh(&sc->rx.rxbuflock);