2 * Copyright (c) 2008-2011 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 ath_ant_div_comb_alt_check(u8 div_group, int alt_ratio,
32 int curr_main_set, int curr_alt_set,
33 int alt_rssi_avg, int main_rssi_avg)
38 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
42 if ((((curr_main_set == ATH_ANT_DIV_COMB_LNA2) &&
43 (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) &&
44 (alt_rssi_avg >= (main_rssi_avg - 5))) ||
45 ((curr_main_set == ATH_ANT_DIV_COMB_LNA1) &&
46 (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) &&
47 (alt_rssi_avg >= (main_rssi_avg - 2)))) &&
58 static inline bool ath9k_check_auto_sleep(struct ath_softc *sc)
60 return sc->ps_enabled &&
61 (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP);
65 * Setup and link descriptors.
67 * 11N: we can no longer afford to self link the last descriptor.
68 * MAC acknowledges BA status as long as it copies frames to host
69 * buffer (or rx fifo). This can incorrectly acknowledge packets
70 * to a sender if last desc is self-linked.
72 static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf)
74 struct ath_hw *ah = sc->sc_ah;
75 struct ath_common *common = ath9k_hw_common(ah);
82 ds->ds_link = 0; /* link to null */
83 ds->ds_data = bf->bf_buf_addr;
85 /* virtual addr of the beginning of the buffer. */
88 ds->ds_vdata = skb->data;
91 * setup rx descriptors. The rx_bufsize here tells the hardware
92 * how much data it can DMA to us and that we are prepared
95 ath9k_hw_setuprxdesc(ah, ds,
99 if (sc->rx.rxlink == NULL)
100 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
102 *sc->rx.rxlink = bf->bf_daddr;
104 sc->rx.rxlink = &ds->ds_link;
107 static void ath_setdefantenna(struct ath_softc *sc, u32 antenna)
109 /* XXX block beacon interrupts */
110 ath9k_hw_setantenna(sc->sc_ah, antenna);
111 sc->rx.defant = antenna;
112 sc->rx.rxotherant = 0;
115 static void ath_opmode_init(struct ath_softc *sc)
117 struct ath_hw *ah = sc->sc_ah;
118 struct ath_common *common = ath9k_hw_common(ah);
122 /* configure rx filter */
123 rfilt = ath_calcrxfilter(sc);
124 ath9k_hw_setrxfilter(ah, rfilt);
126 /* configure bssid mask */
127 ath_hw_setbssidmask(common);
129 /* configure operational mode */
130 ath9k_hw_setopmode(ah);
132 /* calculate and install multicast filter */
133 mfilt[0] = mfilt[1] = ~0;
134 ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
137 static bool ath_rx_edma_buf_link(struct ath_softc *sc,
138 enum ath9k_rx_qtype qtype)
140 struct ath_hw *ah = sc->sc_ah;
141 struct ath_rx_edma *rx_edma;
145 rx_edma = &sc->rx.rx_edma[qtype];
146 if (skb_queue_len(&rx_edma->rx_fifo) >= rx_edma->rx_fifo_hwsize)
149 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
150 list_del_init(&bf->list);
155 memset(skb->data, 0, ah->caps.rx_status_len);
156 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
157 ah->caps.rx_status_len, DMA_TO_DEVICE);
159 SKB_CB_ATHBUF(skb) = bf;
160 ath9k_hw_addrxbuf_edma(ah, bf->bf_buf_addr, qtype);
161 skb_queue_tail(&rx_edma->rx_fifo, skb);
166 static void ath_rx_addbuffer_edma(struct ath_softc *sc,
167 enum ath9k_rx_qtype qtype, int size)
169 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
172 if (list_empty(&sc->rx.rxbuf)) {
173 ath_dbg(common, ATH_DBG_QUEUE, "No free rx buf available\n");
177 while (!list_empty(&sc->rx.rxbuf)) {
180 if (!ath_rx_edma_buf_link(sc, qtype))
188 static void ath_rx_remove_buffer(struct ath_softc *sc,
189 enum ath9k_rx_qtype qtype)
192 struct ath_rx_edma *rx_edma;
195 rx_edma = &sc->rx.rx_edma[qtype];
197 while ((skb = skb_dequeue(&rx_edma->rx_fifo)) != NULL) {
198 bf = SKB_CB_ATHBUF(skb);
200 list_add_tail(&bf->list, &sc->rx.rxbuf);
204 static void ath_rx_edma_cleanup(struct ath_softc *sc)
208 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
209 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
211 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
213 dev_kfree_skb_any(bf->bf_mpdu);
216 INIT_LIST_HEAD(&sc->rx.rxbuf);
218 kfree(sc->rx.rx_bufptr);
219 sc->rx.rx_bufptr = NULL;
222 static void ath_rx_edma_init_queue(struct ath_rx_edma *rx_edma, int size)
224 skb_queue_head_init(&rx_edma->rx_fifo);
225 skb_queue_head_init(&rx_edma->rx_buffers);
226 rx_edma->rx_fifo_hwsize = size;
229 static int ath_rx_edma_init(struct ath_softc *sc, int nbufs)
231 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
232 struct ath_hw *ah = sc->sc_ah;
238 ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
239 ah->caps.rx_status_len);
241 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_LP],
242 ah->caps.rx_lp_qdepth);
243 ath_rx_edma_init_queue(&sc->rx.rx_edma[ATH9K_RX_QUEUE_HP],
244 ah->caps.rx_hp_qdepth);
246 size = sizeof(struct ath_buf) * nbufs;
247 bf = kzalloc(size, GFP_KERNEL);
251 INIT_LIST_HEAD(&sc->rx.rxbuf);
252 sc->rx.rx_bufptr = bf;
254 for (i = 0; i < nbufs; i++, bf++) {
255 skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL);
261 memset(skb->data, 0, common->rx_bufsize);
264 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
267 if (unlikely(dma_mapping_error(sc->dev,
269 dev_kfree_skb_any(skb);
273 "dma_mapping_error() on RX init\n");
278 list_add_tail(&bf->list, &sc->rx.rxbuf);
284 ath_rx_edma_cleanup(sc);
288 static void ath_edma_start_recv(struct ath_softc *sc)
290 spin_lock_bh(&sc->rx.rxbuflock);
292 ath9k_hw_rxena(sc->sc_ah);
294 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_HP,
295 sc->rx.rx_edma[ATH9K_RX_QUEUE_HP].rx_fifo_hwsize);
297 ath_rx_addbuffer_edma(sc, ATH9K_RX_QUEUE_LP,
298 sc->rx.rx_edma[ATH9K_RX_QUEUE_LP].rx_fifo_hwsize);
302 ath9k_hw_startpcureceive(sc->sc_ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
304 spin_unlock_bh(&sc->rx.rxbuflock);
307 static void ath_edma_stop_recv(struct ath_softc *sc)
309 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_HP);
310 ath_rx_remove_buffer(sc, ATH9K_RX_QUEUE_LP);
313 int ath_rx_init(struct ath_softc *sc, int nbufs)
315 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
320 spin_lock_init(&sc->sc_pcu_lock);
321 sc->sc_flags &= ~SC_OP_RXFLUSH;
322 spin_lock_init(&sc->rx.rxbuflock);
324 common->rx_bufsize = IEEE80211_MAX_MPDU_LEN / 2 +
325 sc->sc_ah->caps.rx_status_len;
327 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
328 return ath_rx_edma_init(sc, nbufs);
330 ath_dbg(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n",
331 common->cachelsz, common->rx_bufsize);
333 /* Initialize rx descriptors */
335 error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf,
339 "failed to allocate rx descriptors: %d\n",
344 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
345 skb = ath_rxbuf_alloc(common, common->rx_bufsize,
353 bf->bf_buf_addr = dma_map_single(sc->dev, skb->data,
356 if (unlikely(dma_mapping_error(sc->dev,
358 dev_kfree_skb_any(skb);
362 "dma_mapping_error() on RX init\n");
367 sc->rx.rxlink = NULL;
377 void ath_rx_cleanup(struct ath_softc *sc)
379 struct ath_hw *ah = sc->sc_ah;
380 struct ath_common *common = ath9k_hw_common(ah);
384 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
385 ath_rx_edma_cleanup(sc);
388 list_for_each_entry(bf, &sc->rx.rxbuf, list) {
391 dma_unmap_single(sc->dev, bf->bf_buf_addr,
400 if (sc->rx.rxdma.dd_desc_len != 0)
401 ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf);
406 * Calculate the receive filter according to the
407 * operating mode and state:
409 * o always accept unicast, broadcast, and multicast traffic
410 * o maintain current state of phy error reception (the hal
411 * may enable phy error frames for noise immunity work)
412 * o probe request frames are accepted only when operating in
413 * hostap, adhoc, or monitor modes
414 * o enable promiscuous mode according to the interface state
416 * - when operating in adhoc mode so the 802.11 layer creates
417 * node table entries for peers,
418 * - when operating in station mode for collecting rssi data when
419 * the station is otherwise quiet, or
420 * - when operating as a repeater so we see repeater-sta beacons
424 u32 ath_calcrxfilter(struct ath_softc *sc)
428 rfilt = ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
429 | ATH9K_RX_FILTER_MCAST;
431 if (sc->rx.rxfilter & FIF_PROBE_REQ)
432 rfilt |= ATH9K_RX_FILTER_PROBEREQ;
435 * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
436 * mode interface or when in monitor mode. AP mode does not need this
437 * since it receives all in-BSS frames anyway.
439 if (sc->sc_ah->is_monitoring)
440 rfilt |= ATH9K_RX_FILTER_PROM;
442 if (sc->rx.rxfilter & FIF_CONTROL)
443 rfilt |= ATH9K_RX_FILTER_CONTROL;
445 if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
447 !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC))
448 rfilt |= ATH9K_RX_FILTER_MYBEACON;
450 rfilt |= ATH9K_RX_FILTER_BEACON;
452 if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
453 (sc->rx.rxfilter & FIF_PSPOLL))
454 rfilt |= ATH9K_RX_FILTER_PSPOLL;
456 if (conf_is_ht(&sc->hw->conf))
457 rfilt |= ATH9K_RX_FILTER_COMP_BAR;
459 if (sc->nvifs > 1 || (sc->rx.rxfilter & FIF_OTHER_BSS)) {
460 /* The following may also be needed for other older chips */
461 if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160)
462 rfilt |= ATH9K_RX_FILTER_PROM;
463 rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL;
468 #undef RX_FILTER_PRESERVE
471 int ath_startrecv(struct ath_softc *sc)
473 struct ath_hw *ah = sc->sc_ah;
474 struct ath_buf *bf, *tbf;
476 if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
477 ath_edma_start_recv(sc);
481 spin_lock_bh(&sc->rx.rxbuflock);
482 if (list_empty(&sc->rx.rxbuf))
485 sc->rx.rxlink = NULL;
486 list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) {
487 ath_rx_buf_link(sc, bf);
490 /* We could have deleted elements so the list may be empty now */
491 if (list_empty(&sc->rx.rxbuf))
494 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
495 ath9k_hw_putrxbuf(ah, bf->bf_daddr);
500 ath9k_hw_startpcureceive(ah, (sc->sc_flags & SC_OP_OFFCHANNEL));
502 spin_unlock_bh(&sc->rx.rxbuflock);
507 bool ath_stoprecv(struct ath_softc *sc)
509 struct ath_hw *ah = sc->sc_ah;
510 bool stopped, reset = false;
512 spin_lock_bh(&sc->rx.rxbuflock);
513 ath9k_hw_abortpcurecv(ah);
514 ath9k_hw_setrxfilter(ah, 0);
515 stopped = ath9k_hw_stopdmarecv(ah, &reset);
517 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
518 ath_edma_stop_recv(sc);
520 sc->rx.rxlink = NULL;
521 spin_unlock_bh(&sc->rx.rxbuflock);
523 if (!(ah->ah_flags & AH_UNPLUGGED) &&
524 unlikely(!stopped)) {
525 ath_err(ath9k_hw_common(sc->sc_ah),
526 "Could not stop RX, we could be "
527 "confusing the DMA engine when we start RX up\n");
528 ATH_DBG_WARN_ON_ONCE(!stopped);
530 return stopped && !reset;
533 void ath_flushrecv(struct ath_softc *sc)
535 sc->sc_flags |= SC_OP_RXFLUSH;
536 if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
537 ath_rx_tasklet(sc, 1, true);
538 ath_rx_tasklet(sc, 1, false);
539 sc->sc_flags &= ~SC_OP_RXFLUSH;
542 static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb)
544 /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */
545 struct ieee80211_mgmt *mgmt;
546 u8 *pos, *end, id, elen;
547 struct ieee80211_tim_ie *tim;
549 mgmt = (struct ieee80211_mgmt *)skb->data;
550 pos = mgmt->u.beacon.variable;
551 end = skb->data + skb->len;
553 while (pos + 2 < end) {
556 if (pos + elen > end)
559 if (id == WLAN_EID_TIM) {
560 if (elen < sizeof(*tim))
562 tim = (struct ieee80211_tim_ie *) pos;
563 if (tim->dtim_count != 0)
565 return tim->bitmap_ctrl & 0x01;
574 static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb)
576 struct ieee80211_mgmt *mgmt;
577 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
579 if (skb->len < 24 + 8 + 2 + 2)
582 mgmt = (struct ieee80211_mgmt *)skb->data;
583 if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) {
584 /* TODO: This doesn't work well if you have stations
585 * associated to two different APs because curbssid
586 * is just the last AP that any of the stations associated
589 return; /* not from our current AP */
592 sc->ps_flags &= ~PS_WAIT_FOR_BEACON;
594 if (sc->ps_flags & PS_BEACON_SYNC) {
595 sc->ps_flags &= ~PS_BEACON_SYNC;
596 ath_dbg(common, ATH_DBG_PS,
597 "Reconfigure Beacon timers based on timestamp from the AP\n");
599 sc->ps_flags &= ~PS_TSFOOR_SYNC;
602 if (ath_beacon_dtim_pending_cab(skb)) {
604 * Remain awake waiting for buffered broadcast/multicast
605 * frames. If the last broadcast/multicast frame is not
606 * received properly, the next beacon frame will work as
607 * a backup trigger for returning into NETWORK SLEEP state,
608 * so we are waiting for it as well.
610 ath_dbg(common, ATH_DBG_PS,
611 "Received DTIM beacon indicating buffered broadcast/multicast frame(s)\n");
612 sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON;
616 if (sc->ps_flags & PS_WAIT_FOR_CAB) {
618 * This can happen if a broadcast frame is dropped or the AP
619 * fails to send a frame indicating that all CAB frames have
622 sc->ps_flags &= ~PS_WAIT_FOR_CAB;
623 ath_dbg(common, ATH_DBG_PS,
624 "PS wait for CAB frames timed out\n");
628 static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb)
630 struct ieee80211_hdr *hdr;
631 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
633 hdr = (struct ieee80211_hdr *)skb->data;
635 /* Process Beacon and CAB receive in PS state */
636 if (((sc->ps_flags & PS_WAIT_FOR_BEACON) || ath9k_check_auto_sleep(sc))
637 && ieee80211_is_beacon(hdr->frame_control))
638 ath_rx_ps_beacon(sc, skb);
639 else if ((sc->ps_flags & PS_WAIT_FOR_CAB) &&
640 (ieee80211_is_data(hdr->frame_control) ||
641 ieee80211_is_action(hdr->frame_control)) &&
642 is_multicast_ether_addr(hdr->addr1) &&
643 !ieee80211_has_moredata(hdr->frame_control)) {
645 * No more broadcast/multicast frames to be received at this
648 sc->ps_flags &= ~(PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON);
649 ath_dbg(common, ATH_DBG_PS,
650 "All PS CAB frames received, back to sleep\n");
651 } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) &&
652 !is_multicast_ether_addr(hdr->addr1) &&
653 !ieee80211_has_morefrags(hdr->frame_control)) {
654 sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA;
655 ath_dbg(common, ATH_DBG_PS,
656 "Going back to sleep after having received PS-Poll data (0x%lx)\n",
657 sc->ps_flags & (PS_WAIT_FOR_BEACON |
659 PS_WAIT_FOR_PSPOLL_DATA |
660 PS_WAIT_FOR_TX_ACK));
664 static bool ath_edma_get_buffers(struct ath_softc *sc,
665 enum ath9k_rx_qtype qtype)
667 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
668 struct ath_hw *ah = sc->sc_ah;
669 struct ath_common *common = ath9k_hw_common(ah);
674 skb = skb_peek(&rx_edma->rx_fifo);
678 bf = SKB_CB_ATHBUF(skb);
681 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
682 common->rx_bufsize, DMA_FROM_DEVICE);
684 ret = ath9k_hw_process_rxdesc_edma(ah, NULL, skb->data);
685 if (ret == -EINPROGRESS) {
686 /*let device gain the buffer again*/
687 dma_sync_single_for_device(sc->dev, bf->bf_buf_addr,
688 common->rx_bufsize, DMA_FROM_DEVICE);
692 __skb_unlink(skb, &rx_edma->rx_fifo);
693 if (ret == -EINVAL) {
694 /* corrupt descriptor, skip this one and the following one */
695 list_add_tail(&bf->list, &sc->rx.rxbuf);
696 ath_rx_edma_buf_link(sc, qtype);
697 skb = skb_peek(&rx_edma->rx_fifo);
701 bf = SKB_CB_ATHBUF(skb);
704 __skb_unlink(skb, &rx_edma->rx_fifo);
705 list_add_tail(&bf->list, &sc->rx.rxbuf);
706 ath_rx_edma_buf_link(sc, qtype);
709 skb_queue_tail(&rx_edma->rx_buffers, skb);
714 static struct ath_buf *ath_edma_get_next_rx_buf(struct ath_softc *sc,
715 struct ath_rx_status *rs,
716 enum ath9k_rx_qtype qtype)
718 struct ath_rx_edma *rx_edma = &sc->rx.rx_edma[qtype];
722 while (ath_edma_get_buffers(sc, qtype));
723 skb = __skb_dequeue(&rx_edma->rx_buffers);
727 bf = SKB_CB_ATHBUF(skb);
728 ath9k_hw_process_rxdesc_edma(sc->sc_ah, rs, skb->data);
732 static struct ath_buf *ath_get_next_rx_buf(struct ath_softc *sc,
733 struct ath_rx_status *rs)
735 struct ath_hw *ah = sc->sc_ah;
736 struct ath_common *common = ath9k_hw_common(ah);
741 if (list_empty(&sc->rx.rxbuf)) {
742 sc->rx.rxlink = NULL;
746 bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list);
750 * Must provide the virtual address of the current
751 * descriptor, the physical address, and the virtual
752 * address of the next descriptor in the h/w chain.
753 * This allows the HAL to look ahead to see if the
754 * hardware is done with a descriptor by checking the
755 * done bit in the following descriptor and the address
756 * of the current descriptor the DMA engine is working
757 * on. All this is necessary because of our use of
758 * a self-linked list to avoid rx overruns.
760 ret = ath9k_hw_rxprocdesc(ah, ds, rs, 0);
761 if (ret == -EINPROGRESS) {
762 struct ath_rx_status trs;
764 struct ath_desc *tds;
766 memset(&trs, 0, sizeof(trs));
767 if (list_is_last(&bf->list, &sc->rx.rxbuf)) {
768 sc->rx.rxlink = NULL;
772 tbf = list_entry(bf->list.next, struct ath_buf, list);
775 * On some hardware the descriptor status words could
776 * get corrupted, including the done bit. Because of
777 * this, check if the next descriptor's done bit is
780 * If the next descriptor's done bit is set, the current
781 * descriptor has been corrupted. Force s/w to discard
782 * this descriptor and continue...
786 ret = ath9k_hw_rxprocdesc(ah, tds, &trs, 0);
787 if (ret == -EINPROGRESS)
795 * Synchronize the DMA transfer with CPU before
796 * 1. accessing the frame
797 * 2. requeueing the same buffer to h/w
799 dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr,
806 /* Assumes you've already done the endian to CPU conversion */
807 static bool ath9k_rx_accept(struct ath_common *common,
808 struct ieee80211_hdr *hdr,
809 struct ieee80211_rx_status *rxs,
810 struct ath_rx_status *rx_stats,
813 #define is_mc_or_valid_tkip_keyix ((is_mc || \
814 (rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID && \
815 test_bit(rx_stats->rs_keyix, common->tkip_keymap))))
817 struct ath_hw *ah = common->ah;
819 u8 rx_status_len = ah->caps.rx_status_len;
821 fc = hdr->frame_control;
823 if (!rx_stats->rs_datalen)
826 * rs_status follows rs_datalen so if rs_datalen is too large
827 * we can take a hint that hardware corrupted it, so ignore
830 if (rx_stats->rs_datalen > (common->rx_bufsize - rx_status_len))
833 /* Only use error bits from the last fragment */
834 if (rx_stats->rs_more)
838 * The rx_stats->rs_status will not be set until the end of the
839 * chained descriptors so it can be ignored if rs_more is set. The
840 * rs_more will be false at the last element of the chained
843 if (rx_stats->rs_status != 0) {
844 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
845 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
846 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
849 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
850 *decrypt_error = true;
851 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
854 * The MIC error bit is only valid if the frame
855 * is not a control frame or fragment, and it was
856 * decrypted using a valid TKIP key.
858 is_mc = !!is_multicast_ether_addr(hdr->addr1);
860 if (!ieee80211_is_ctl(fc) &&
861 !ieee80211_has_morefrags(fc) &&
862 !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
863 is_mc_or_valid_tkip_keyix)
864 rxs->flag |= RX_FLAG_MMIC_ERROR;
866 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
869 * Reject error frames with the exception of
870 * decryption and MIC failures. For monitor mode,
871 * we also ignore the CRC error.
873 if (ah->is_monitoring) {
874 if (rx_stats->rs_status &
875 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
879 if (rx_stats->rs_status &
880 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
888 static int ath9k_process_rate(struct ath_common *common,
889 struct ieee80211_hw *hw,
890 struct ath_rx_status *rx_stats,
891 struct ieee80211_rx_status *rxs)
893 struct ieee80211_supported_band *sband;
894 enum ieee80211_band band;
897 band = hw->conf.channel->band;
898 sband = hw->wiphy->bands[band];
900 if (rx_stats->rs_rate & 0x80) {
902 rxs->flag |= RX_FLAG_HT;
903 if (rx_stats->rs_flags & ATH9K_RX_2040)
904 rxs->flag |= RX_FLAG_40MHZ;
905 if (rx_stats->rs_flags & ATH9K_RX_GI)
906 rxs->flag |= RX_FLAG_SHORT_GI;
907 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
911 for (i = 0; i < sband->n_bitrates; i++) {
912 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
916 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
917 rxs->flag |= RX_FLAG_SHORTPRE;
924 * No valid hardware bitrate found -- we should not get here
925 * because hardware has already validated this frame as OK.
927 ath_dbg(common, ATH_DBG_XMIT,
928 "unsupported hw bitrate detected 0x%02x using 1 Mbit\n",
934 static void ath9k_process_rssi(struct ath_common *common,
935 struct ieee80211_hw *hw,
936 struct ieee80211_hdr *hdr,
937 struct ath_rx_status *rx_stats)
939 struct ath_softc *sc = hw->priv;
940 struct ath_hw *ah = common->ah;
944 if ((ah->opmode != NL80211_IFTYPE_STATION) &&
945 (ah->opmode != NL80211_IFTYPE_ADHOC))
948 fc = hdr->frame_control;
949 if (!ieee80211_is_beacon(fc) ||
950 compare_ether_addr(hdr->addr3, common->curbssid)) {
951 /* TODO: This doesn't work well if you have stations
952 * associated to two different APs because curbssid
953 * is just the last AP that any of the stations associated
959 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && !rx_stats->rs_moreaggr)
960 ATH_RSSI_LPF(sc->last_rssi, rx_stats->rs_rssi);
962 last_rssi = sc->last_rssi;
963 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
964 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
965 ATH_RSSI_EP_MULTIPLIER);
966 if (rx_stats->rs_rssi < 0)
967 rx_stats->rs_rssi = 0;
969 /* Update Beacon RSSI, this is used by ANI. */
970 ah->stats.avgbrssi = rx_stats->rs_rssi;
974 * For Decrypt or Demic errors, we only mark packet status here and always push
975 * up the frame up to let mac80211 handle the actual error case, be it no
976 * decryption key or real decryption error. This let us keep statistics there.
978 static int ath9k_rx_skb_preprocess(struct ath_common *common,
979 struct ieee80211_hw *hw,
980 struct ieee80211_hdr *hdr,
981 struct ath_rx_status *rx_stats,
982 struct ieee80211_rx_status *rx_status,
985 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
988 * everything but the rate is checked here, the rate check is done
989 * separately to avoid doing two lookups for a rate for each frame.
991 if (!ath9k_rx_accept(common, hdr, rx_status, rx_stats, decrypt_error))
994 /* Only use status info from the last fragment */
995 if (rx_stats->rs_more)
998 ath9k_process_rssi(common, hw, hdr, rx_stats);
1000 if (ath9k_process_rate(common, hw, rx_stats, rx_status))
1003 rx_status->band = hw->conf.channel->band;
1004 rx_status->freq = hw->conf.channel->center_freq;
1005 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
1006 rx_status->antenna = rx_stats->rs_antenna;
1007 rx_status->flag |= RX_FLAG_MACTIME_MPDU;
1012 static void ath9k_rx_skb_postprocess(struct ath_common *common,
1013 struct sk_buff *skb,
1014 struct ath_rx_status *rx_stats,
1015 struct ieee80211_rx_status *rxs,
1018 struct ath_hw *ah = common->ah;
1019 struct ieee80211_hdr *hdr;
1020 int hdrlen, padpos, padsize;
1024 /* see if any padding is done by the hw and remove it */
1025 hdr = (struct ieee80211_hdr *) skb->data;
1026 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1027 fc = hdr->frame_control;
1028 padpos = ath9k_cmn_padpos(hdr->frame_control);
1030 /* The MAC header is padded to have 32-bit boundary if the
1031 * packet payload is non-zero. The general calculation for
1032 * padsize would take into account odd header lengths:
1033 * padsize = (4 - padpos % 4) % 4; However, since only
1034 * even-length headers are used, padding can only be 0 or 2
1035 * bytes and we can optimize this a bit. In addition, we must
1036 * not try to remove padding from short control frames that do
1037 * not have payload. */
1038 padsize = padpos & 3;
1039 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
1040 memmove(skb->data + padsize, skb->data, padpos);
1041 skb_pull(skb, padsize);
1044 keyix = rx_stats->rs_keyix;
1046 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
1047 ieee80211_has_protected(fc)) {
1048 rxs->flag |= RX_FLAG_DECRYPTED;
1049 } else if (ieee80211_has_protected(fc)
1050 && !decrypt_error && skb->len >= hdrlen + 4) {
1051 keyix = skb->data[hdrlen + 3] >> 6;
1053 if (test_bit(keyix, common->keymap))
1054 rxs->flag |= RX_FLAG_DECRYPTED;
1056 if (ah->sw_mgmt_crypto &&
1057 (rxs->flag & RX_FLAG_DECRYPTED) &&
1058 ieee80211_is_mgmt(fc))
1059 /* Use software decrypt for management frames. */
1060 rxs->flag &= ~RX_FLAG_DECRYPTED;
1063 static void ath_lnaconf_alt_good_scan(struct ath_ant_comb *antcomb,
1064 struct ath_hw_antcomb_conf ant_conf,
1067 antcomb->quick_scan_cnt = 0;
1069 if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA2)
1070 antcomb->rssi_lna2 = main_rssi_avg;
1071 else if (ant_conf.main_lna_conf == ATH_ANT_DIV_COMB_LNA1)
1072 antcomb->rssi_lna1 = main_rssi_avg;
1074 switch ((ant_conf.main_lna_conf << 4) | ant_conf.alt_lna_conf) {
1075 case (0x10): /* LNA2 A-B */
1076 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1077 antcomb->first_quick_scan_conf =
1078 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1079 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1081 case (0x20): /* LNA1 A-B */
1082 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1083 antcomb->first_quick_scan_conf =
1084 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1085 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1087 case (0x21): /* LNA1 LNA2 */
1088 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA2;
1089 antcomb->first_quick_scan_conf =
1090 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1091 antcomb->second_quick_scan_conf =
1092 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1094 case (0x12): /* LNA2 LNA1 */
1095 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1;
1096 antcomb->first_quick_scan_conf =
1097 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1098 antcomb->second_quick_scan_conf =
1099 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1101 case (0x13): /* LNA2 A+B */
1102 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1103 antcomb->first_quick_scan_conf =
1104 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1105 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA1;
1107 case (0x23): /* LNA1 A+B */
1108 antcomb->main_conf = ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1109 antcomb->first_quick_scan_conf =
1110 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1111 antcomb->second_quick_scan_conf = ATH_ANT_DIV_COMB_LNA2;
1118 static void ath_select_ant_div_from_quick_scan(struct ath_ant_comb *antcomb,
1119 struct ath_hw_antcomb_conf *div_ant_conf,
1120 int main_rssi_avg, int alt_rssi_avg,
1124 switch (antcomb->quick_scan_cnt) {
1126 /* set alt to main, and alt to first conf */
1127 div_ant_conf->main_lna_conf = antcomb->main_conf;
1128 div_ant_conf->alt_lna_conf = antcomb->first_quick_scan_conf;
1131 /* set alt to main, and alt to first conf */
1132 div_ant_conf->main_lna_conf = antcomb->main_conf;
1133 div_ant_conf->alt_lna_conf = antcomb->second_quick_scan_conf;
1134 antcomb->rssi_first = main_rssi_avg;
1135 antcomb->rssi_second = alt_rssi_avg;
1137 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1139 if (ath_is_alt_ant_ratio_better(alt_ratio,
1140 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1141 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1142 main_rssi_avg, alt_rssi_avg,
1143 antcomb->total_pkt_count))
1144 antcomb->first_ratio = true;
1146 antcomb->first_ratio = false;
1147 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1148 if (ath_is_alt_ant_ratio_better(alt_ratio,
1149 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1150 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1151 main_rssi_avg, alt_rssi_avg,
1152 antcomb->total_pkt_count))
1153 antcomb->first_ratio = true;
1155 antcomb->first_ratio = false;
1157 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1158 (alt_rssi_avg > main_rssi_avg +
1159 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1160 (alt_rssi_avg > main_rssi_avg)) &&
1161 (antcomb->total_pkt_count > 50))
1162 antcomb->first_ratio = true;
1164 antcomb->first_ratio = false;
1168 antcomb->alt_good = false;
1169 antcomb->scan_not_start = false;
1170 antcomb->scan = false;
1171 antcomb->rssi_first = main_rssi_avg;
1172 antcomb->rssi_third = alt_rssi_avg;
1174 if (antcomb->second_quick_scan_conf == ATH_ANT_DIV_COMB_LNA1)
1175 antcomb->rssi_lna1 = alt_rssi_avg;
1176 else if (antcomb->second_quick_scan_conf ==
1177 ATH_ANT_DIV_COMB_LNA2)
1178 antcomb->rssi_lna2 = alt_rssi_avg;
1179 else if (antcomb->second_quick_scan_conf ==
1180 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2) {
1181 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2)
1182 antcomb->rssi_lna2 = main_rssi_avg;
1183 else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1)
1184 antcomb->rssi_lna1 = main_rssi_avg;
1187 if (antcomb->rssi_lna2 > antcomb->rssi_lna1 +
1188 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)
1189 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1191 div_ant_conf->main_lna_conf = ATH_ANT_DIV_COMB_LNA1;
1193 if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) {
1194 if (ath_is_alt_ant_ratio_better(alt_ratio,
1195 ATH_ANT_DIV_COMB_LNA1_DELTA_HI,
1196 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1197 main_rssi_avg, alt_rssi_avg,
1198 antcomb->total_pkt_count))
1199 antcomb->second_ratio = true;
1201 antcomb->second_ratio = false;
1202 } else if (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2) {
1203 if (ath_is_alt_ant_ratio_better(alt_ratio,
1204 ATH_ANT_DIV_COMB_LNA1_DELTA_MID,
1205 ATH_ANT_DIV_COMB_LNA1_DELTA_LOW,
1206 main_rssi_avg, alt_rssi_avg,
1207 antcomb->total_pkt_count))
1208 antcomb->second_ratio = true;
1210 antcomb->second_ratio = false;
1212 if ((((alt_ratio >= ATH_ANT_DIV_COMB_ALT_ANT_RATIO2) &&
1213 (alt_rssi_avg > main_rssi_avg +
1214 ATH_ANT_DIV_COMB_LNA1_DELTA_HI)) ||
1215 (alt_rssi_avg > main_rssi_avg)) &&
1216 (antcomb->total_pkt_count > 50))
1217 antcomb->second_ratio = true;
1219 antcomb->second_ratio = false;
1222 /* set alt to the conf with maximun ratio */
1223 if (antcomb->first_ratio && antcomb->second_ratio) {
1224 if (antcomb->rssi_second > antcomb->rssi_third) {
1226 if ((antcomb->first_quick_scan_conf ==
1227 ATH_ANT_DIV_COMB_LNA1) ||
1228 (antcomb->first_quick_scan_conf ==
1229 ATH_ANT_DIV_COMB_LNA2))
1230 /* Set alt LNA1 or LNA2*/
1231 if (div_ant_conf->main_lna_conf ==
1232 ATH_ANT_DIV_COMB_LNA2)
1233 div_ant_conf->alt_lna_conf =
1234 ATH_ANT_DIV_COMB_LNA1;
1236 div_ant_conf->alt_lna_conf =
1237 ATH_ANT_DIV_COMB_LNA2;
1239 /* Set alt to A+B or A-B */
1240 div_ant_conf->alt_lna_conf =
1241 antcomb->first_quick_scan_conf;
1242 } else if ((antcomb->second_quick_scan_conf ==
1243 ATH_ANT_DIV_COMB_LNA1) ||
1244 (antcomb->second_quick_scan_conf ==
1245 ATH_ANT_DIV_COMB_LNA2)) {
1246 /* Set alt LNA1 or LNA2 */
1247 if (div_ant_conf->main_lna_conf ==
1248 ATH_ANT_DIV_COMB_LNA2)
1249 div_ant_conf->alt_lna_conf =
1250 ATH_ANT_DIV_COMB_LNA1;
1252 div_ant_conf->alt_lna_conf =
1253 ATH_ANT_DIV_COMB_LNA2;
1255 /* Set alt to A+B or A-B */
1256 div_ant_conf->alt_lna_conf =
1257 antcomb->second_quick_scan_conf;
1259 } else if (antcomb->first_ratio) {
1261 if ((antcomb->first_quick_scan_conf ==
1262 ATH_ANT_DIV_COMB_LNA1) ||
1263 (antcomb->first_quick_scan_conf ==
1264 ATH_ANT_DIV_COMB_LNA2))
1265 /* Set alt LNA1 or LNA2 */
1266 if (div_ant_conf->main_lna_conf ==
1267 ATH_ANT_DIV_COMB_LNA2)
1268 div_ant_conf->alt_lna_conf =
1269 ATH_ANT_DIV_COMB_LNA1;
1271 div_ant_conf->alt_lna_conf =
1272 ATH_ANT_DIV_COMB_LNA2;
1274 /* Set alt to A+B or A-B */
1275 div_ant_conf->alt_lna_conf =
1276 antcomb->first_quick_scan_conf;
1277 } else if (antcomb->second_ratio) {
1279 if ((antcomb->second_quick_scan_conf ==
1280 ATH_ANT_DIV_COMB_LNA1) ||
1281 (antcomb->second_quick_scan_conf ==
1282 ATH_ANT_DIV_COMB_LNA2))
1283 /* Set alt LNA1 or LNA2 */
1284 if (div_ant_conf->main_lna_conf ==
1285 ATH_ANT_DIV_COMB_LNA2)
1286 div_ant_conf->alt_lna_conf =
1287 ATH_ANT_DIV_COMB_LNA1;
1289 div_ant_conf->alt_lna_conf =
1290 ATH_ANT_DIV_COMB_LNA2;
1292 /* Set alt to A+B or A-B */
1293 div_ant_conf->alt_lna_conf =
1294 antcomb->second_quick_scan_conf;
1296 /* main is largest */
1297 if ((antcomb->main_conf == ATH_ANT_DIV_COMB_LNA1) ||
1298 (antcomb->main_conf == ATH_ANT_DIV_COMB_LNA2))
1299 /* Set alt LNA1 or LNA2 */
1300 if (div_ant_conf->main_lna_conf ==
1301 ATH_ANT_DIV_COMB_LNA2)
1302 div_ant_conf->alt_lna_conf =
1303 ATH_ANT_DIV_COMB_LNA1;
1305 div_ant_conf->alt_lna_conf =
1306 ATH_ANT_DIV_COMB_LNA2;
1308 /* Set alt to A+B or A-B */
1309 div_ant_conf->alt_lna_conf = antcomb->main_conf;
1317 static void ath_ant_div_conf_fast_divbias(struct ath_hw_antcomb_conf *ant_conf,
1318 struct ath_ant_comb *antcomb, int alt_ratio)
1320 if (ant_conf->div_group == 0) {
1321 /* Adjust the fast_div_bias based on main and alt lna conf */
1322 switch ((ant_conf->main_lna_conf << 4) |
1323 ant_conf->alt_lna_conf) {
1324 case (0x01): /* A-B LNA2 */
1325 ant_conf->fast_div_bias = 0x3b;
1327 case (0x02): /* A-B LNA1 */
1328 ant_conf->fast_div_bias = 0x3d;
1330 case (0x03): /* A-B A+B */
1331 ant_conf->fast_div_bias = 0x1;
1333 case (0x10): /* LNA2 A-B */
1334 ant_conf->fast_div_bias = 0x7;
1336 case (0x12): /* LNA2 LNA1 */
1337 ant_conf->fast_div_bias = 0x2;
1339 case (0x13): /* LNA2 A+B */
1340 ant_conf->fast_div_bias = 0x7;
1342 case (0x20): /* LNA1 A-B */
1343 ant_conf->fast_div_bias = 0x6;
1345 case (0x21): /* LNA1 LNA2 */
1346 ant_conf->fast_div_bias = 0x0;
1348 case (0x23): /* LNA1 A+B */
1349 ant_conf->fast_div_bias = 0x6;
1351 case (0x30): /* A+B A-B */
1352 ant_conf->fast_div_bias = 0x1;
1354 case (0x31): /* A+B LNA2 */
1355 ant_conf->fast_div_bias = 0x3b;
1357 case (0x32): /* A+B LNA1 */
1358 ant_conf->fast_div_bias = 0x3d;
1363 } else if (ant_conf->div_group == 2) {
1364 /* Adjust the fast_div_bias based on main and alt_lna_conf */
1365 switch ((ant_conf->main_lna_conf << 4) |
1366 ant_conf->alt_lna_conf) {
1367 case (0x01): /* A-B LNA2 */
1368 ant_conf->fast_div_bias = 0x1;
1369 ant_conf->main_gaintb = 0;
1370 ant_conf->alt_gaintb = 0;
1372 case (0x02): /* A-B LNA1 */
1373 ant_conf->fast_div_bias = 0x1;
1374 ant_conf->main_gaintb = 0;
1375 ant_conf->alt_gaintb = 0;
1377 case (0x03): /* A-B A+B */
1378 ant_conf->fast_div_bias = 0x1;
1379 ant_conf->main_gaintb = 0;
1380 ant_conf->alt_gaintb = 0;
1382 case (0x10): /* LNA2 A-B */
1383 if (!(antcomb->scan) &&
1384 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1385 ant_conf->fast_div_bias = 0x1;
1387 ant_conf->fast_div_bias = 0x2;
1388 ant_conf->main_gaintb = 0;
1389 ant_conf->alt_gaintb = 0;
1391 case (0x12): /* LNA2 LNA1 */
1392 ant_conf->fast_div_bias = 0x1;
1393 ant_conf->main_gaintb = 0;
1394 ant_conf->alt_gaintb = 0;
1396 case (0x13): /* LNA2 A+B */
1397 if (!(antcomb->scan) &&
1398 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1399 ant_conf->fast_div_bias = 0x1;
1401 ant_conf->fast_div_bias = 0x2;
1402 ant_conf->main_gaintb = 0;
1403 ant_conf->alt_gaintb = 0;
1405 case (0x20): /* LNA1 A-B */
1406 if (!(antcomb->scan) &&
1407 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1408 ant_conf->fast_div_bias = 0x1;
1410 ant_conf->fast_div_bias = 0x2;
1411 ant_conf->main_gaintb = 0;
1412 ant_conf->alt_gaintb = 0;
1414 case (0x21): /* LNA1 LNA2 */
1415 ant_conf->fast_div_bias = 0x1;
1416 ant_conf->main_gaintb = 0;
1417 ant_conf->alt_gaintb = 0;
1419 case (0x23): /* LNA1 A+B */
1420 if (!(antcomb->scan) &&
1421 (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO))
1422 ant_conf->fast_div_bias = 0x1;
1424 ant_conf->fast_div_bias = 0x2;
1425 ant_conf->main_gaintb = 0;
1426 ant_conf->alt_gaintb = 0;
1428 case (0x30): /* A+B A-B */
1429 ant_conf->fast_div_bias = 0x1;
1430 ant_conf->main_gaintb = 0;
1431 ant_conf->alt_gaintb = 0;
1433 case (0x31): /* A+B LNA2 */
1434 ant_conf->fast_div_bias = 0x1;
1435 ant_conf->main_gaintb = 0;
1436 ant_conf->alt_gaintb = 0;
1438 case (0x32): /* A+B LNA1 */
1439 ant_conf->fast_div_bias = 0x1;
1440 ant_conf->main_gaintb = 0;
1441 ant_conf->alt_gaintb = 0;
1451 /* Antenna diversity and combining */
1452 static void ath_ant_comb_scan(struct ath_softc *sc, struct ath_rx_status *rs)
1454 struct ath_hw_antcomb_conf div_ant_conf;
1455 struct ath_ant_comb *antcomb = &sc->ant_comb;
1456 int alt_ratio = 0, alt_rssi_avg = 0, main_rssi_avg = 0, curr_alt_set;
1458 int main_rssi = rs->rs_rssi_ctl0;
1459 int alt_rssi = rs->rs_rssi_ctl1;
1460 int rx_ant_conf, main_ant_conf;
1461 bool short_scan = false;
1463 rx_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_CURRENT_SHIFT) &
1465 main_ant_conf = (rs->rs_rssi_ctl2 >> ATH_ANT_RX_MAIN_SHIFT) &
1468 /* Record packet only when both main_rssi and alt_rssi is positive */
1469 if (main_rssi > 0 && alt_rssi > 0) {
1470 antcomb->total_pkt_count++;
1471 antcomb->main_total_rssi += main_rssi;
1472 antcomb->alt_total_rssi += alt_rssi;
1473 if (main_ant_conf == rx_ant_conf)
1474 antcomb->main_recv_cnt++;
1476 antcomb->alt_recv_cnt++;
1479 /* Short scan check */
1480 if (antcomb->scan && antcomb->alt_good) {
1481 if (time_after(jiffies, antcomb->scan_start_time +
1482 msecs_to_jiffies(ATH_ANT_DIV_COMB_SHORT_SCAN_INTR)))
1485 if (antcomb->total_pkt_count ==
1486 ATH_ANT_DIV_COMB_SHORT_SCAN_PKTCOUNT) {
1487 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1488 antcomb->total_pkt_count);
1489 if (alt_ratio < ATH_ANT_DIV_COMB_ALT_ANT_RATIO)
1494 if (((antcomb->total_pkt_count < ATH_ANT_DIV_COMB_MAX_PKTCOUNT) ||
1495 rs->rs_moreaggr) && !short_scan)
1498 if (antcomb->total_pkt_count) {
1499 alt_ratio = ((antcomb->alt_recv_cnt * 100) /
1500 antcomb->total_pkt_count);
1501 main_rssi_avg = (antcomb->main_total_rssi /
1502 antcomb->total_pkt_count);
1503 alt_rssi_avg = (antcomb->alt_total_rssi /
1504 antcomb->total_pkt_count);
1508 ath9k_hw_antdiv_comb_conf_get(sc->sc_ah, &div_ant_conf);
1509 curr_alt_set = div_ant_conf.alt_lna_conf;
1510 curr_main_set = div_ant_conf.main_lna_conf;
1514 if (antcomb->count == ATH_ANT_DIV_COMB_MAX_COUNT) {
1515 if (alt_ratio > ATH_ANT_DIV_COMB_ALT_ANT_RATIO) {
1516 ath_lnaconf_alt_good_scan(antcomb, div_ant_conf,
1518 antcomb->alt_good = true;
1520 antcomb->alt_good = false;
1524 antcomb->scan = true;
1525 antcomb->scan_not_start = true;
1528 if (!antcomb->scan) {
1529 if (ath_ant_div_comb_alt_check(div_ant_conf.div_group,
1530 alt_ratio, curr_main_set, curr_alt_set,
1531 alt_rssi_avg, main_rssi_avg)) {
1532 if (curr_alt_set == ATH_ANT_DIV_COMB_LNA2) {
1533 /* Switch main and alt LNA */
1534 div_ant_conf.main_lna_conf =
1535 ATH_ANT_DIV_COMB_LNA2;
1536 div_ant_conf.alt_lna_conf =
1537 ATH_ANT_DIV_COMB_LNA1;
1538 } else if (curr_alt_set == ATH_ANT_DIV_COMB_LNA1) {
1539 div_ant_conf.main_lna_conf =
1540 ATH_ANT_DIV_COMB_LNA1;
1541 div_ant_conf.alt_lna_conf =
1542 ATH_ANT_DIV_COMB_LNA2;
1546 } else if ((curr_alt_set != ATH_ANT_DIV_COMB_LNA1) &&
1547 (curr_alt_set != ATH_ANT_DIV_COMB_LNA2)) {
1548 /* Set alt to another LNA */
1549 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2)
1550 div_ant_conf.alt_lna_conf =
1551 ATH_ANT_DIV_COMB_LNA1;
1552 else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1)
1553 div_ant_conf.alt_lna_conf =
1554 ATH_ANT_DIV_COMB_LNA2;
1559 if ((alt_rssi_avg < (main_rssi_avg +
1560 div_ant_conf.lna1_lna2_delta)))
1564 if (!antcomb->scan_not_start) {
1565 switch (curr_alt_set) {
1566 case ATH_ANT_DIV_COMB_LNA2:
1567 antcomb->rssi_lna2 = alt_rssi_avg;
1568 antcomb->rssi_lna1 = main_rssi_avg;
1569 antcomb->scan = true;
1571 div_ant_conf.main_lna_conf =
1572 ATH_ANT_DIV_COMB_LNA1;
1573 div_ant_conf.alt_lna_conf =
1574 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1576 case ATH_ANT_DIV_COMB_LNA1:
1577 antcomb->rssi_lna1 = alt_rssi_avg;
1578 antcomb->rssi_lna2 = main_rssi_avg;
1579 antcomb->scan = true;
1581 div_ant_conf.main_lna_conf = ATH_ANT_DIV_COMB_LNA2;
1582 div_ant_conf.alt_lna_conf =
1583 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1585 case ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2:
1586 antcomb->rssi_add = alt_rssi_avg;
1587 antcomb->scan = true;
1589 div_ant_conf.alt_lna_conf =
1590 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1592 case ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2:
1593 antcomb->rssi_sub = alt_rssi_avg;
1594 antcomb->scan = false;
1595 if (antcomb->rssi_lna2 >
1596 (antcomb->rssi_lna1 +
1597 ATH_ANT_DIV_COMB_LNA1_LNA2_SWITCH_DELTA)) {
1598 /* use LNA2 as main LNA */
1599 if ((antcomb->rssi_add > antcomb->rssi_lna1) &&
1600 (antcomb->rssi_add > antcomb->rssi_sub)) {
1602 div_ant_conf.main_lna_conf =
1603 ATH_ANT_DIV_COMB_LNA2;
1604 div_ant_conf.alt_lna_conf =
1605 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1606 } else if (antcomb->rssi_sub >
1607 antcomb->rssi_lna1) {
1609 div_ant_conf.main_lna_conf =
1610 ATH_ANT_DIV_COMB_LNA2;
1611 div_ant_conf.alt_lna_conf =
1612 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1615 div_ant_conf.main_lna_conf =
1616 ATH_ANT_DIV_COMB_LNA2;
1617 div_ant_conf.alt_lna_conf =
1618 ATH_ANT_DIV_COMB_LNA1;
1621 /* use LNA1 as main LNA */
1622 if ((antcomb->rssi_add > antcomb->rssi_lna2) &&
1623 (antcomb->rssi_add > antcomb->rssi_sub)) {
1625 div_ant_conf.main_lna_conf =
1626 ATH_ANT_DIV_COMB_LNA1;
1627 div_ant_conf.alt_lna_conf =
1628 ATH_ANT_DIV_COMB_LNA1_PLUS_LNA2;
1629 } else if (antcomb->rssi_sub >
1630 antcomb->rssi_lna1) {
1632 div_ant_conf.main_lna_conf =
1633 ATH_ANT_DIV_COMB_LNA1;
1634 div_ant_conf.alt_lna_conf =
1635 ATH_ANT_DIV_COMB_LNA1_MINUS_LNA2;
1638 div_ant_conf.main_lna_conf =
1639 ATH_ANT_DIV_COMB_LNA1;
1640 div_ant_conf.alt_lna_conf =
1641 ATH_ANT_DIV_COMB_LNA2;
1649 if (!antcomb->alt_good) {
1650 antcomb->scan_not_start = false;
1651 /* Set alt to another LNA */
1652 if (curr_main_set == ATH_ANT_DIV_COMB_LNA2) {
1653 div_ant_conf.main_lna_conf =
1654 ATH_ANT_DIV_COMB_LNA2;
1655 div_ant_conf.alt_lna_conf =
1656 ATH_ANT_DIV_COMB_LNA1;
1657 } else if (curr_main_set == ATH_ANT_DIV_COMB_LNA1) {
1658 div_ant_conf.main_lna_conf =
1659 ATH_ANT_DIV_COMB_LNA1;
1660 div_ant_conf.alt_lna_conf =
1661 ATH_ANT_DIV_COMB_LNA2;
1667 ath_select_ant_div_from_quick_scan(antcomb, &div_ant_conf,
1668 main_rssi_avg, alt_rssi_avg,
1671 antcomb->quick_scan_cnt++;
1674 ath_ant_div_conf_fast_divbias(&div_ant_conf, antcomb, alt_ratio);
1675 ath9k_hw_antdiv_comb_conf_set(sc->sc_ah, &div_ant_conf);
1677 antcomb->scan_start_time = jiffies;
1678 antcomb->total_pkt_count = 0;
1679 antcomb->main_total_rssi = 0;
1680 antcomb->alt_total_rssi = 0;
1681 antcomb->main_recv_cnt = 0;
1682 antcomb->alt_recv_cnt = 0;
1685 int ath_rx_tasklet(struct ath_softc *sc, int flush, bool hp)
1688 struct sk_buff *skb = NULL, *requeue_skb, *hdr_skb;
1689 struct ieee80211_rx_status *rxs;
1690 struct ath_hw *ah = sc->sc_ah;
1691 struct ath_common *common = ath9k_hw_common(ah);
1693 * The hw can technically differ from common->hw when using ath9k
1694 * virtual wiphy so to account for that we iterate over the active
1695 * wiphys and find the appropriate wiphy and therefore hw.
1697 struct ieee80211_hw *hw = sc->hw;
1698 struct ieee80211_hdr *hdr;
1700 bool decrypt_error = false;
1701 struct ath_rx_status rs;
1702 enum ath9k_rx_qtype qtype;
1703 bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
1705 u8 rx_status_len = ah->caps.rx_status_len;
1708 unsigned long flags;
1711 dma_type = DMA_BIDIRECTIONAL;
1713 dma_type = DMA_FROM_DEVICE;
1715 qtype = hp ? ATH9K_RX_QUEUE_HP : ATH9K_RX_QUEUE_LP;
1716 spin_lock_bh(&sc->rx.rxbuflock);
1718 tsf = ath9k_hw_gettsf64(ah);
1719 tsf_lower = tsf & 0xffffffff;
1722 /* If handling rx interrupt and flush is in progress => exit */
1723 if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0))
1726 memset(&rs, 0, sizeof(rs));
1728 bf = ath_edma_get_next_rx_buf(sc, &rs, qtype);
1730 bf = ath_get_next_rx_buf(sc, &rs);
1740 * Take frame header from the first fragment and RX status from
1744 hdr_skb = sc->rx.frag;
1748 hdr = (struct ieee80211_hdr *) (hdr_skb->data + rx_status_len);
1749 rxs = IEEE80211_SKB_RXCB(hdr_skb);
1751 ath_debug_stat_rx(sc, &rs);
1754 * If we're asked to flush receive queue, directly
1755 * chain it back at the queue without processing it.
1758 goto requeue_drop_frag;
1760 retval = ath9k_rx_skb_preprocess(common, hw, hdr, &rs,
1761 rxs, &decrypt_error);
1763 goto requeue_drop_frag;
1765 rxs->mactime = (tsf & ~0xffffffffULL) | rs.rs_tstamp;
1766 if (rs.rs_tstamp > tsf_lower &&
1767 unlikely(rs.rs_tstamp - tsf_lower > 0x10000000))
1768 rxs->mactime -= 0x100000000ULL;
1770 if (rs.rs_tstamp < tsf_lower &&
1771 unlikely(tsf_lower - rs.rs_tstamp > 0x10000000))
1772 rxs->mactime += 0x100000000ULL;
1774 /* Ensure we always have an skb to requeue once we are done
1775 * processing the current buffer's skb */
1776 requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC);
1778 /* If there is no memory we ignore the current RX'd frame,
1779 * tell hardware it can give us a new frame using the old
1780 * skb and put it at the tail of the sc->rx.rxbuf list for
1783 goto requeue_drop_frag;
1785 /* Unmap the frame */
1786 dma_unmap_single(sc->dev, bf->bf_buf_addr,
1790 skb_put(skb, rs.rs_datalen + ah->caps.rx_status_len);
1791 if (ah->caps.rx_status_len)
1792 skb_pull(skb, ah->caps.rx_status_len);
1795 ath9k_rx_skb_postprocess(common, hdr_skb, &rs,
1796 rxs, decrypt_error);
1798 /* We will now give hardware our shiny new allocated skb */
1799 bf->bf_mpdu = requeue_skb;
1800 bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data,
1803 if (unlikely(dma_mapping_error(sc->dev,
1804 bf->bf_buf_addr))) {
1805 dev_kfree_skb_any(requeue_skb);
1807 bf->bf_buf_addr = 0;
1808 ath_err(common, "dma_mapping_error() on RX\n");
1809 ieee80211_rx(hw, skb);
1815 * rs_more indicates chained descriptors which can be
1816 * used to link buffers together for a sort of
1817 * scatter-gather operation.
1820 /* too many fragments - cannot handle frame */
1821 dev_kfree_skb_any(sc->rx.frag);
1822 dev_kfree_skb_any(skb);
1830 int space = skb->len - skb_tailroom(hdr_skb);
1834 if (pskb_expand_head(hdr_skb, 0, space, GFP_ATOMIC) < 0) {
1836 goto requeue_drop_frag;
1839 skb_copy_from_linear_data(skb, skb_put(hdr_skb, skb->len),
1841 dev_kfree_skb_any(skb);
1846 * change the default rx antenna if rx diversity chooses the
1847 * other antenna 3 times in a row.
1849 if (sc->rx.defant != rs.rs_antenna) {
1850 if (++sc->rx.rxotherant >= 3)
1851 ath_setdefantenna(sc, rs.rs_antenna);
1853 sc->rx.rxotherant = 0;
1856 spin_lock_irqsave(&sc->sc_pm_lock, flags);
1858 if ((sc->ps_flags & (PS_WAIT_FOR_BEACON |
1860 PS_WAIT_FOR_PSPOLL_DATA)) ||
1861 ath9k_check_auto_sleep(sc))
1863 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
1865 if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
1866 ath_ant_comb_scan(sc, &rs);
1868 ieee80211_rx(hw, skb);
1872 dev_kfree_skb_any(sc->rx.frag);
1877 list_add_tail(&bf->list, &sc->rx.rxbuf);
1878 ath_rx_edma_buf_link(sc, qtype);
1880 list_move_tail(&bf->list, &sc->rx.rxbuf);
1881 ath_rx_buf_link(sc, bf);
1886 spin_unlock_bh(&sc->rx.rxbuflock);