1203c4805SLuis R. Rodriguez /* 2203c4805SLuis R. Rodriguez * Copyright (c) 2008-2009 Atheros Communications Inc. 3203c4805SLuis R. Rodriguez * 4203c4805SLuis R. Rodriguez * Permission to use, copy, modify, and/or distribute this software for any 5203c4805SLuis R. Rodriguez * purpose with or without fee is hereby granted, provided that the above 6203c4805SLuis R. Rodriguez * copyright notice and this permission notice appear in all copies. 7203c4805SLuis R. Rodriguez * 8203c4805SLuis R. Rodriguez * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9203c4805SLuis R. Rodriguez * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10203c4805SLuis R. Rodriguez * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11203c4805SLuis R. Rodriguez * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12203c4805SLuis R. Rodriguez * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13203c4805SLuis R. Rodriguez * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14203c4805SLuis R. Rodriguez * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15203c4805SLuis R. Rodriguez */ 16203c4805SLuis R. Rodriguez 17203c4805SLuis R. Rodriguez #include "ath9k.h" 18203c4805SLuis R. Rodriguez 19203c4805SLuis R. Rodriguez static struct ieee80211_hw * ath_get_virt_hw(struct ath_softc *sc, 20203c4805SLuis R. Rodriguez struct ieee80211_hdr *hdr) 21203c4805SLuis R. Rodriguez { 22203c4805SLuis R. Rodriguez struct ieee80211_hw *hw = sc->pri_wiphy->hw; 23203c4805SLuis R. Rodriguez int i; 24203c4805SLuis R. Rodriguez 25203c4805SLuis R. Rodriguez spin_lock_bh(&sc->wiphy_lock); 26203c4805SLuis R. Rodriguez for (i = 0; i < sc->num_sec_wiphy; i++) { 27203c4805SLuis R. Rodriguez struct ath_wiphy *aphy = sc->sec_wiphy[i]; 28203c4805SLuis R. Rodriguez if (aphy == NULL) 29203c4805SLuis R. Rodriguez continue; 30203c4805SLuis R. Rodriguez if (compare_ether_addr(hdr->addr1, aphy->hw->wiphy->perm_addr) 31203c4805SLuis R. Rodriguez == 0) { 32203c4805SLuis R. Rodriguez hw = aphy->hw; 33203c4805SLuis R. Rodriguez break; 34203c4805SLuis R. Rodriguez } 35203c4805SLuis R. Rodriguez } 36203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->wiphy_lock); 37203c4805SLuis R. Rodriguez return hw; 38203c4805SLuis R. Rodriguez } 39203c4805SLuis R. Rodriguez 40203c4805SLuis R. Rodriguez /* 41203c4805SLuis R. Rodriguez * Setup and link descriptors. 42203c4805SLuis R. Rodriguez * 43203c4805SLuis R. Rodriguez * 11N: we can no longer afford to self link the last descriptor. 44203c4805SLuis R. Rodriguez * MAC acknowledges BA status as long as it copies frames to host 45203c4805SLuis R. Rodriguez * buffer (or rx fifo). This can incorrectly acknowledge packets 46203c4805SLuis R. Rodriguez * to a sender if last desc is self-linked. 47203c4805SLuis R. Rodriguez */ 48203c4805SLuis R. Rodriguez static void ath_rx_buf_link(struct ath_softc *sc, struct ath_buf *bf) 49203c4805SLuis R. Rodriguez { 50203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 51cc861f74SLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 52203c4805SLuis R. Rodriguez struct ath_desc *ds; 53203c4805SLuis R. Rodriguez struct sk_buff *skb; 54203c4805SLuis R. Rodriguez 55203c4805SLuis R. Rodriguez ATH_RXBUF_RESET(bf); 56203c4805SLuis R. Rodriguez 57203c4805SLuis R. Rodriguez ds = bf->bf_desc; 58203c4805SLuis R. Rodriguez ds->ds_link = 0; /* link to null */ 59203c4805SLuis R. Rodriguez ds->ds_data = bf->bf_buf_addr; 60203c4805SLuis R. Rodriguez 61203c4805SLuis R. Rodriguez /* virtual addr of the beginning of the buffer. */ 62203c4805SLuis R. Rodriguez skb = bf->bf_mpdu; 639680e8a3SLuis R. Rodriguez BUG_ON(skb == NULL); 64203c4805SLuis R. Rodriguez ds->ds_vdata = skb->data; 65203c4805SLuis R. Rodriguez 66cc861f74SLuis R. Rodriguez /* 67cc861f74SLuis R. Rodriguez * setup rx descriptors. The rx_bufsize here tells the hardware 68203c4805SLuis R. Rodriguez * how much data it can DMA to us and that we are prepared 69cc861f74SLuis R. Rodriguez * to process 70cc861f74SLuis R. Rodriguez */ 71203c4805SLuis R. Rodriguez ath9k_hw_setuprxdesc(ah, ds, 72cc861f74SLuis R. Rodriguez common->rx_bufsize, 73203c4805SLuis R. Rodriguez 0); 74203c4805SLuis R. Rodriguez 75203c4805SLuis R. Rodriguez if (sc->rx.rxlink == NULL) 76203c4805SLuis R. Rodriguez ath9k_hw_putrxbuf(ah, bf->bf_daddr); 77203c4805SLuis R. Rodriguez else 78203c4805SLuis R. Rodriguez *sc->rx.rxlink = bf->bf_daddr; 79203c4805SLuis R. Rodriguez 80203c4805SLuis R. Rodriguez sc->rx.rxlink = &ds->ds_link; 81203c4805SLuis R. Rodriguez ath9k_hw_rxena(ah); 82203c4805SLuis R. Rodriguez } 83203c4805SLuis R. Rodriguez 84203c4805SLuis R. Rodriguez static void ath_setdefantenna(struct ath_softc *sc, u32 antenna) 85203c4805SLuis R. Rodriguez { 86203c4805SLuis R. Rodriguez /* XXX block beacon interrupts */ 87203c4805SLuis R. Rodriguez ath9k_hw_setantenna(sc->sc_ah, antenna); 88203c4805SLuis R. Rodriguez sc->rx.defant = antenna; 89203c4805SLuis R. Rodriguez sc->rx.rxotherant = 0; 90203c4805SLuis R. Rodriguez } 91203c4805SLuis R. Rodriguez 92203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc) 93203c4805SLuis R. Rodriguez { 94203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 951510718dSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 961510718dSLuis R. Rodriguez 97203c4805SLuis R. Rodriguez u32 rfilt, mfilt[2]; 98203c4805SLuis R. Rodriguez 99203c4805SLuis R. Rodriguez /* configure rx filter */ 100203c4805SLuis R. Rodriguez rfilt = ath_calcrxfilter(sc); 101203c4805SLuis R. Rodriguez ath9k_hw_setrxfilter(ah, rfilt); 102203c4805SLuis R. Rodriguez 103203c4805SLuis R. Rodriguez /* configure bssid mask */ 104203c4805SLuis R. Rodriguez if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) 10513b81559SLuis R. Rodriguez ath_hw_setbssidmask(common); 106203c4805SLuis R. Rodriguez 107203c4805SLuis R. Rodriguez /* configure operational mode */ 108203c4805SLuis R. Rodriguez ath9k_hw_setopmode(ah); 109203c4805SLuis R. Rodriguez 110203c4805SLuis R. Rodriguez /* Handle any link-level address change. */ 1111510718dSLuis R. Rodriguez ath9k_hw_setmac(ah, common->macaddr); 112203c4805SLuis R. Rodriguez 113203c4805SLuis R. Rodriguez /* calculate and install multicast filter */ 114203c4805SLuis R. Rodriguez mfilt[0] = mfilt[1] = ~0; 115203c4805SLuis R. Rodriguez ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]); 116203c4805SLuis R. Rodriguez } 117203c4805SLuis R. Rodriguez 118203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs) 119203c4805SLuis R. Rodriguez { 12027c51f1aSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah); 121203c4805SLuis R. Rodriguez struct sk_buff *skb; 122203c4805SLuis R. Rodriguez struct ath_buf *bf; 123203c4805SLuis R. Rodriguez int error = 0; 124203c4805SLuis R. Rodriguez 125203c4805SLuis R. Rodriguez spin_lock_init(&sc->rx.rxflushlock); 126203c4805SLuis R. Rodriguez sc->sc_flags &= ~SC_OP_RXFLUSH; 127203c4805SLuis R. Rodriguez spin_lock_init(&sc->rx.rxbuflock); 128203c4805SLuis R. Rodriguez 129cc861f74SLuis R. Rodriguez common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN, 13027c51f1aSLuis R. Rodriguez min(common->cachelsz, (u16)64)); 131203c4805SLuis R. Rodriguez 132c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n", 133cc861f74SLuis R. Rodriguez common->cachelsz, common->rx_bufsize); 134203c4805SLuis R. Rodriguez 135203c4805SLuis R. Rodriguez /* Initialize rx descriptors */ 136203c4805SLuis R. Rodriguez 137203c4805SLuis R. Rodriguez error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf, 138203c4805SLuis R. Rodriguez "rx", nbufs, 1); 139203c4805SLuis R. Rodriguez if (error != 0) { 140c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_FATAL, 141203c4805SLuis R. Rodriguez "failed to allocate rx descriptors: %d\n", error); 142203c4805SLuis R. Rodriguez goto err; 143203c4805SLuis R. Rodriguez } 144203c4805SLuis R. Rodriguez 145203c4805SLuis R. Rodriguez list_for_each_entry(bf, &sc->rx.rxbuf, list) { 146cc861f74SLuis R. Rodriguez skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL); 147203c4805SLuis R. Rodriguez if (skb == NULL) { 148203c4805SLuis R. Rodriguez error = -ENOMEM; 149203c4805SLuis R. Rodriguez goto err; 150203c4805SLuis R. Rodriguez } 151203c4805SLuis R. Rodriguez 152203c4805SLuis R. Rodriguez bf->bf_mpdu = skb; 153203c4805SLuis R. Rodriguez bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, 154cc861f74SLuis R. Rodriguez common->rx_bufsize, 155203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 156203c4805SLuis R. Rodriguez if (unlikely(dma_mapping_error(sc->dev, 157203c4805SLuis R. Rodriguez bf->bf_buf_addr))) { 158203c4805SLuis R. Rodriguez dev_kfree_skb_any(skb); 159203c4805SLuis R. Rodriguez bf->bf_mpdu = NULL; 160c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_FATAL, 161203c4805SLuis R. Rodriguez "dma_mapping_error() on RX init\n"); 162203c4805SLuis R. Rodriguez error = -ENOMEM; 163203c4805SLuis R. Rodriguez goto err; 164203c4805SLuis R. Rodriguez } 165203c4805SLuis R. Rodriguez bf->bf_dmacontext = bf->bf_buf_addr; 166203c4805SLuis R. Rodriguez } 167203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 168203c4805SLuis R. Rodriguez 169203c4805SLuis R. Rodriguez err: 170203c4805SLuis R. Rodriguez if (error) 171203c4805SLuis R. Rodriguez ath_rx_cleanup(sc); 172203c4805SLuis R. Rodriguez 173203c4805SLuis R. Rodriguez return error; 174203c4805SLuis R. Rodriguez } 175203c4805SLuis R. Rodriguez 176203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc) 177203c4805SLuis R. Rodriguez { 178cc861f74SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 179cc861f74SLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 180203c4805SLuis R. Rodriguez struct sk_buff *skb; 181203c4805SLuis R. Rodriguez struct ath_buf *bf; 182203c4805SLuis R. Rodriguez 183203c4805SLuis R. Rodriguez list_for_each_entry(bf, &sc->rx.rxbuf, list) { 184203c4805SLuis R. Rodriguez skb = bf->bf_mpdu; 185203c4805SLuis R. Rodriguez if (skb) { 186203c4805SLuis R. Rodriguez dma_unmap_single(sc->dev, bf->bf_buf_addr, 187cc861f74SLuis R. Rodriguez common->rx_bufsize, DMA_FROM_DEVICE); 188203c4805SLuis R. Rodriguez dev_kfree_skb(skb); 189203c4805SLuis R. Rodriguez } 190203c4805SLuis R. Rodriguez } 191203c4805SLuis R. Rodriguez 192203c4805SLuis R. Rodriguez if (sc->rx.rxdma.dd_desc_len != 0) 193203c4805SLuis R. Rodriguez ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf); 194203c4805SLuis R. Rodriguez } 195203c4805SLuis R. Rodriguez 196203c4805SLuis R. Rodriguez /* 197203c4805SLuis R. Rodriguez * Calculate the receive filter according to the 198203c4805SLuis R. Rodriguez * operating mode and state: 199203c4805SLuis R. Rodriguez * 200203c4805SLuis R. Rodriguez * o always accept unicast, broadcast, and multicast traffic 201203c4805SLuis R. Rodriguez * o maintain current state of phy error reception (the hal 202203c4805SLuis R. Rodriguez * may enable phy error frames for noise immunity work) 203203c4805SLuis R. Rodriguez * o probe request frames are accepted only when operating in 204203c4805SLuis R. Rodriguez * hostap, adhoc, or monitor modes 205203c4805SLuis R. Rodriguez * o enable promiscuous mode according to the interface state 206203c4805SLuis R. Rodriguez * o accept beacons: 207203c4805SLuis R. Rodriguez * - when operating in adhoc mode so the 802.11 layer creates 208203c4805SLuis R. Rodriguez * node table entries for peers, 209203c4805SLuis R. Rodriguez * - when operating in station mode for collecting rssi data when 210203c4805SLuis R. Rodriguez * the station is otherwise quiet, or 211203c4805SLuis R. Rodriguez * - when operating as a repeater so we see repeater-sta beacons 212203c4805SLuis R. Rodriguez * - when scanning 213203c4805SLuis R. Rodriguez */ 214203c4805SLuis R. Rodriguez 215203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc) 216203c4805SLuis R. Rodriguez { 217203c4805SLuis R. Rodriguez #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR) 218203c4805SLuis R. Rodriguez 219203c4805SLuis R. Rodriguez u32 rfilt; 220203c4805SLuis R. Rodriguez 221203c4805SLuis R. Rodriguez rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE) 222203c4805SLuis R. Rodriguez | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST 223203c4805SLuis R. Rodriguez | ATH9K_RX_FILTER_MCAST; 224203c4805SLuis R. Rodriguez 225203c4805SLuis R. Rodriguez /* If not a STA, enable processing of Probe Requests */ 226203c4805SLuis R. Rodriguez if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION) 227203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PROBEREQ; 228203c4805SLuis R. Rodriguez 229203c4805SLuis R. Rodriguez /* 230203c4805SLuis R. Rodriguez * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station 231203c4805SLuis R. Rodriguez * mode interface or when in monitor mode. AP mode does not need this 232203c4805SLuis R. Rodriguez * since it receives all in-BSS frames anyway. 233203c4805SLuis R. Rodriguez */ 234203c4805SLuis R. Rodriguez if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) && 235203c4805SLuis R. Rodriguez (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) || 236203c4805SLuis R. Rodriguez (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR)) 237203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PROM; 238203c4805SLuis R. Rodriguez 239203c4805SLuis R. Rodriguez if (sc->rx.rxfilter & FIF_CONTROL) 240203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_CONTROL; 241203c4805SLuis R. Rodriguez 242203c4805SLuis R. Rodriguez if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) && 243203c4805SLuis R. Rodriguez !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)) 244203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_MYBEACON; 245203c4805SLuis R. Rodriguez else 246203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_BEACON; 247203c4805SLuis R. Rodriguez 24866afad01SSenthil Balasubramanian if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) || 24966afad01SSenthil Balasubramanian AR_SREV_9285_10_OR_LATER(sc->sc_ah)) && 25066afad01SSenthil Balasubramanian (sc->sc_ah->opmode == NL80211_IFTYPE_AP) && 25166afad01SSenthil Balasubramanian (sc->rx.rxfilter & FIF_PSPOLL)) 252203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PSPOLL; 253203c4805SLuis R. Rodriguez 2547ea310beSSujith if (conf_is_ht(&sc->hw->conf)) 2557ea310beSSujith rfilt |= ATH9K_RX_FILTER_COMP_BAR; 2567ea310beSSujith 2575eb6ba83SJavier Cardona if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) { 258203c4805SLuis R. Rodriguez /* TODO: only needed if more than one BSSID is in use in 259203c4805SLuis R. Rodriguez * station/adhoc mode */ 2605eb6ba83SJavier Cardona /* The following may also be needed for other older chips */ 2615eb6ba83SJavier Cardona if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160) 2625eb6ba83SJavier Cardona rfilt |= ATH9K_RX_FILTER_PROM; 263203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL; 264203c4805SLuis R. Rodriguez } 265203c4805SLuis R. Rodriguez 266203c4805SLuis R. Rodriguez return rfilt; 267203c4805SLuis R. Rodriguez 268203c4805SLuis R. Rodriguez #undef RX_FILTER_PRESERVE 269203c4805SLuis R. Rodriguez } 270203c4805SLuis R. Rodriguez 271203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc) 272203c4805SLuis R. Rodriguez { 273203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 274203c4805SLuis R. Rodriguez struct ath_buf *bf, *tbf; 275203c4805SLuis R. Rodriguez 276203c4805SLuis R. Rodriguez spin_lock_bh(&sc->rx.rxbuflock); 277203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) 278203c4805SLuis R. Rodriguez goto start_recv; 279203c4805SLuis R. Rodriguez 280203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 281203c4805SLuis R. Rodriguez list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) { 282203c4805SLuis R. Rodriguez ath_rx_buf_link(sc, bf); 283203c4805SLuis R. Rodriguez } 284203c4805SLuis R. Rodriguez 285203c4805SLuis R. Rodriguez /* We could have deleted elements so the list may be empty now */ 286203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) 287203c4805SLuis R. Rodriguez goto start_recv; 288203c4805SLuis R. Rodriguez 289203c4805SLuis R. Rodriguez bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); 290203c4805SLuis R. Rodriguez ath9k_hw_putrxbuf(ah, bf->bf_daddr); 291203c4805SLuis R. Rodriguez ath9k_hw_rxena(ah); 292203c4805SLuis R. Rodriguez 293203c4805SLuis R. Rodriguez start_recv: 294203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->rx.rxbuflock); 295203c4805SLuis R. Rodriguez ath_opmode_init(sc); 296203c4805SLuis R. Rodriguez ath9k_hw_startpcureceive(ah); 297203c4805SLuis R. Rodriguez 298203c4805SLuis R. Rodriguez return 0; 299203c4805SLuis R. Rodriguez } 300203c4805SLuis R. Rodriguez 301203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc) 302203c4805SLuis R. Rodriguez { 303203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 304203c4805SLuis R. Rodriguez bool stopped; 305203c4805SLuis R. Rodriguez 306203c4805SLuis R. Rodriguez ath9k_hw_stoppcurecv(ah); 307203c4805SLuis R. Rodriguez ath9k_hw_setrxfilter(ah, 0); 308203c4805SLuis R. Rodriguez stopped = ath9k_hw_stopdmarecv(ah); 309203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 310203c4805SLuis R. Rodriguez 311203c4805SLuis R. Rodriguez return stopped; 312203c4805SLuis R. Rodriguez } 313203c4805SLuis R. Rodriguez 314203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc) 315203c4805SLuis R. Rodriguez { 316203c4805SLuis R. Rodriguez spin_lock_bh(&sc->rx.rxflushlock); 317203c4805SLuis R. Rodriguez sc->sc_flags |= SC_OP_RXFLUSH; 318203c4805SLuis R. Rodriguez ath_rx_tasklet(sc, 1); 319203c4805SLuis R. Rodriguez sc->sc_flags &= ~SC_OP_RXFLUSH; 320203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->rx.rxflushlock); 321203c4805SLuis R. Rodriguez } 322203c4805SLuis R. Rodriguez 323cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb) 324cc65965cSJouni Malinen { 325cc65965cSJouni Malinen /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */ 326cc65965cSJouni Malinen struct ieee80211_mgmt *mgmt; 327cc65965cSJouni Malinen u8 *pos, *end, id, elen; 328cc65965cSJouni Malinen struct ieee80211_tim_ie *tim; 329cc65965cSJouni Malinen 330cc65965cSJouni Malinen mgmt = (struct ieee80211_mgmt *)skb->data; 331cc65965cSJouni Malinen pos = mgmt->u.beacon.variable; 332cc65965cSJouni Malinen end = skb->data + skb->len; 333cc65965cSJouni Malinen 334cc65965cSJouni Malinen while (pos + 2 < end) { 335cc65965cSJouni Malinen id = *pos++; 336cc65965cSJouni Malinen elen = *pos++; 337cc65965cSJouni Malinen if (pos + elen > end) 338cc65965cSJouni Malinen break; 339cc65965cSJouni Malinen 340cc65965cSJouni Malinen if (id == WLAN_EID_TIM) { 341cc65965cSJouni Malinen if (elen < sizeof(*tim)) 342cc65965cSJouni Malinen break; 343cc65965cSJouni Malinen tim = (struct ieee80211_tim_ie *) pos; 344cc65965cSJouni Malinen if (tim->dtim_count != 0) 345cc65965cSJouni Malinen break; 346cc65965cSJouni Malinen return tim->bitmap_ctrl & 0x01; 347cc65965cSJouni Malinen } 348cc65965cSJouni Malinen 349cc65965cSJouni Malinen pos += elen; 350cc65965cSJouni Malinen } 351cc65965cSJouni Malinen 352cc65965cSJouni Malinen return false; 353cc65965cSJouni Malinen } 354cc65965cSJouni Malinen 355cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb) 356cc65965cSJouni Malinen { 357cc65965cSJouni Malinen struct ieee80211_mgmt *mgmt; 3581510718dSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah); 359cc65965cSJouni Malinen 360cc65965cSJouni Malinen if (skb->len < 24 + 8 + 2 + 2) 361cc65965cSJouni Malinen return; 362cc65965cSJouni Malinen 363cc65965cSJouni Malinen mgmt = (struct ieee80211_mgmt *)skb->data; 3641510718dSLuis R. Rodriguez if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) 365cc65965cSJouni Malinen return; /* not from our current AP */ 366cc65965cSJouni Malinen 3671b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_BEACON; 368293dc5dfSGabor Juhos 3691b04b930SSujith if (sc->ps_flags & PS_BEACON_SYNC) { 3701b04b930SSujith sc->ps_flags &= ~PS_BEACON_SYNC; 371c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 372c46917bbSLuis R. Rodriguez "Reconfigure Beacon timers based on " 373ccdfeab6SJouni Malinen "timestamp from the AP\n"); 374ccdfeab6SJouni Malinen ath_beacon_config(sc, NULL); 375ccdfeab6SJouni Malinen } 376ccdfeab6SJouni Malinen 377cc65965cSJouni Malinen if (ath_beacon_dtim_pending_cab(skb)) { 378cc65965cSJouni Malinen /* 379cc65965cSJouni Malinen * Remain awake waiting for buffered broadcast/multicast 38058f5fffdSGabor Juhos * frames. If the last broadcast/multicast frame is not 38158f5fffdSGabor Juhos * received properly, the next beacon frame will work as 38258f5fffdSGabor Juhos * a backup trigger for returning into NETWORK SLEEP state, 38358f5fffdSGabor Juhos * so we are waiting for it as well. 384cc65965cSJouni Malinen */ 385c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating " 386cc65965cSJouni Malinen "buffered broadcast/multicast frame(s)\n"); 3871b04b930SSujith sc->ps_flags |= PS_WAIT_FOR_CAB | PS_WAIT_FOR_BEACON; 388cc65965cSJouni Malinen return; 389cc65965cSJouni Malinen } 390cc65965cSJouni Malinen 3911b04b930SSujith if (sc->ps_flags & PS_WAIT_FOR_CAB) { 392cc65965cSJouni Malinen /* 393cc65965cSJouni Malinen * This can happen if a broadcast frame is dropped or the AP 394cc65965cSJouni Malinen * fails to send a frame indicating that all CAB frames have 395cc65965cSJouni Malinen * been delivered. 396cc65965cSJouni Malinen */ 3971b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_CAB; 398c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 399c46917bbSLuis R. Rodriguez "PS wait for CAB frames timed out\n"); 400cc65965cSJouni Malinen } 401cc65965cSJouni Malinen } 402cc65965cSJouni Malinen 403cc65965cSJouni Malinen static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb) 404cc65965cSJouni Malinen { 405cc65965cSJouni Malinen struct ieee80211_hdr *hdr; 406c46917bbSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah); 407cc65965cSJouni Malinen 408cc65965cSJouni Malinen hdr = (struct ieee80211_hdr *)skb->data; 409cc65965cSJouni Malinen 410cc65965cSJouni Malinen /* Process Beacon and CAB receive in PS state */ 4111b04b930SSujith if ((sc->ps_flags & PS_WAIT_FOR_BEACON) && 4129a23f9caSJouni Malinen ieee80211_is_beacon(hdr->frame_control)) 413cc65965cSJouni Malinen ath_rx_ps_beacon(sc, skb); 4141b04b930SSujith else if ((sc->ps_flags & PS_WAIT_FOR_CAB) && 415cc65965cSJouni Malinen (ieee80211_is_data(hdr->frame_control) || 416cc65965cSJouni Malinen ieee80211_is_action(hdr->frame_control)) && 417cc65965cSJouni Malinen is_multicast_ether_addr(hdr->addr1) && 418cc65965cSJouni Malinen !ieee80211_has_moredata(hdr->frame_control)) { 419cc65965cSJouni Malinen /* 420cc65965cSJouni Malinen * No more broadcast/multicast frames to be received at this 421cc65965cSJouni Malinen * point. 422cc65965cSJouni Malinen */ 4231b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_CAB; 424c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 425c46917bbSLuis R. Rodriguez "All PS CAB frames received, back to sleep\n"); 4261b04b930SSujith } else if ((sc->ps_flags & PS_WAIT_FOR_PSPOLL_DATA) && 4279a23f9caSJouni Malinen !is_multicast_ether_addr(hdr->addr1) && 4289a23f9caSJouni Malinen !ieee80211_has_morefrags(hdr->frame_control)) { 4291b04b930SSujith sc->ps_flags &= ~PS_WAIT_FOR_PSPOLL_DATA; 430c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 431c46917bbSLuis R. Rodriguez "Going back to sleep after having received " 432c46917bbSLuis R. Rodriguez "PS-Poll data (0x%x)\n", 4331b04b930SSujith sc->ps_flags & (PS_WAIT_FOR_BEACON | 4341b04b930SSujith PS_WAIT_FOR_CAB | 4351b04b930SSujith PS_WAIT_FOR_PSPOLL_DATA | 4361b04b930SSujith PS_WAIT_FOR_TX_ACK)); 437cc65965cSJouni Malinen } 438cc65965cSJouni Malinen } 439cc65965cSJouni Malinen 440b4afffc0SLuis R. Rodriguez static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw, 441b4afffc0SLuis R. Rodriguez struct ath_softc *sc, struct sk_buff *skb, 4425ca42627SLuis R. Rodriguez struct ieee80211_rx_status *rxs) 4439d64a3cfSJouni Malinen { 4449d64a3cfSJouni Malinen struct ieee80211_hdr *hdr; 4459d64a3cfSJouni Malinen 4469d64a3cfSJouni Malinen hdr = (struct ieee80211_hdr *)skb->data; 4479d64a3cfSJouni Malinen 4489d64a3cfSJouni Malinen /* Send the frame to mac80211 */ 4499d64a3cfSJouni Malinen if (is_multicast_ether_addr(hdr->addr1)) { 4509d64a3cfSJouni Malinen int i; 4519d64a3cfSJouni Malinen /* 4529d64a3cfSJouni Malinen * Deliver broadcast/multicast frames to all suitable 4539d64a3cfSJouni Malinen * virtual wiphys. 4549d64a3cfSJouni Malinen */ 4559d64a3cfSJouni Malinen /* TODO: filter based on channel configuration */ 4569d64a3cfSJouni Malinen for (i = 0; i < sc->num_sec_wiphy; i++) { 4579d64a3cfSJouni Malinen struct ath_wiphy *aphy = sc->sec_wiphy[i]; 4589d64a3cfSJouni Malinen struct sk_buff *nskb; 4599d64a3cfSJouni Malinen if (aphy == NULL) 4609d64a3cfSJouni Malinen continue; 4619d64a3cfSJouni Malinen nskb = skb_copy(skb, GFP_ATOMIC); 4625ca42627SLuis R. Rodriguez if (!nskb) 4635ca42627SLuis R. Rodriguez continue; 464f1d58c25SJohannes Berg ieee80211_rx(aphy->hw, nskb); 4659d64a3cfSJouni Malinen } 466f1d58c25SJohannes Berg ieee80211_rx(sc->hw, skb); 4675ca42627SLuis R. Rodriguez } else 4689d64a3cfSJouni Malinen /* Deliver unicast frames based on receiver address */ 469b4afffc0SLuis R. Rodriguez ieee80211_rx(hw, skb); 4709d64a3cfSJouni Malinen } 4719d64a3cfSJouni Malinen 472203c4805SLuis R. Rodriguez int ath_rx_tasklet(struct ath_softc *sc, int flush) 473203c4805SLuis R. Rodriguez { 474203c4805SLuis R. Rodriguez #define PA2DESC(_sc, _pa) \ 475203c4805SLuis R. Rodriguez ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \ 476203c4805SLuis R. Rodriguez ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr))) 477203c4805SLuis R. Rodriguez 478203c4805SLuis R. Rodriguez struct ath_buf *bf; 479203c4805SLuis R. Rodriguez struct ath_desc *ds; 48026ab2645SLuis R. Rodriguez struct ath_rx_status *rx_stats; 481203c4805SLuis R. Rodriguez struct sk_buff *skb = NULL, *requeue_skb; 4825ca42627SLuis R. Rodriguez struct ieee80211_rx_status *rxs; 483203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 48427c51f1aSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 485b4afffc0SLuis R. Rodriguez /* 486b4afffc0SLuis R. Rodriguez * The hw can techncically differ from common->hw when using ath9k 487b4afffc0SLuis R. Rodriguez * virtual wiphy so to account for that we iterate over the active 488b4afffc0SLuis R. Rodriguez * wiphys and find the appropriate wiphy and therefore hw. 489b4afffc0SLuis R. Rodriguez */ 490b4afffc0SLuis R. Rodriguez struct ieee80211_hw *hw = NULL; 491203c4805SLuis R. Rodriguez struct ieee80211_hdr *hdr; 492c9b14170SLuis R. Rodriguez int retval; 493203c4805SLuis R. Rodriguez bool decrypt_error = false; 494203c4805SLuis R. Rodriguez 495203c4805SLuis R. Rodriguez spin_lock_bh(&sc->rx.rxbuflock); 496203c4805SLuis R. Rodriguez 497203c4805SLuis R. Rodriguez do { 498203c4805SLuis R. Rodriguez /* If handling rx interrupt and flush is in progress => exit */ 499203c4805SLuis R. Rodriguez if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0)) 500203c4805SLuis R. Rodriguez break; 501203c4805SLuis R. Rodriguez 502203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) { 503203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 504203c4805SLuis R. Rodriguez break; 505203c4805SLuis R. Rodriguez } 506203c4805SLuis R. Rodriguez 507203c4805SLuis R. Rodriguez bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); 508203c4805SLuis R. Rodriguez ds = bf->bf_desc; 509203c4805SLuis R. Rodriguez 510203c4805SLuis R. Rodriguez /* 511203c4805SLuis R. Rodriguez * Must provide the virtual address of the current 512203c4805SLuis R. Rodriguez * descriptor, the physical address, and the virtual 513203c4805SLuis R. Rodriguez * address of the next descriptor in the h/w chain. 514203c4805SLuis R. Rodriguez * This allows the HAL to look ahead to see if the 515203c4805SLuis R. Rodriguez * hardware is done with a descriptor by checking the 516203c4805SLuis R. Rodriguez * done bit in the following descriptor and the address 517203c4805SLuis R. Rodriguez * of the current descriptor the DMA engine is working 518203c4805SLuis R. Rodriguez * on. All this is necessary because of our use of 519203c4805SLuis R. Rodriguez * a self-linked list to avoid rx overruns. 520203c4805SLuis R. Rodriguez */ 521203c4805SLuis R. Rodriguez retval = ath9k_hw_rxprocdesc(ah, ds, 522203c4805SLuis R. Rodriguez bf->bf_daddr, 523203c4805SLuis R. Rodriguez PA2DESC(sc, ds->ds_link), 524203c4805SLuis R. Rodriguez 0); 525203c4805SLuis R. Rodriguez if (retval == -EINPROGRESS) { 526203c4805SLuis R. Rodriguez struct ath_buf *tbf; 527203c4805SLuis R. Rodriguez struct ath_desc *tds; 528203c4805SLuis R. Rodriguez 529203c4805SLuis R. Rodriguez if (list_is_last(&bf->list, &sc->rx.rxbuf)) { 530203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 531203c4805SLuis R. Rodriguez break; 532203c4805SLuis R. Rodriguez } 533203c4805SLuis R. Rodriguez 534203c4805SLuis R. Rodriguez tbf = list_entry(bf->list.next, struct ath_buf, list); 535203c4805SLuis R. Rodriguez 536203c4805SLuis R. Rodriguez /* 537203c4805SLuis R. Rodriguez * On some hardware the descriptor status words could 538203c4805SLuis R. Rodriguez * get corrupted, including the done bit. Because of 539203c4805SLuis R. Rodriguez * this, check if the next descriptor's done bit is 540203c4805SLuis R. Rodriguez * set or not. 541203c4805SLuis R. Rodriguez * 542203c4805SLuis R. Rodriguez * If the next descriptor's done bit is set, the current 543203c4805SLuis R. Rodriguez * descriptor has been corrupted. Force s/w to discard 544203c4805SLuis R. Rodriguez * this descriptor and continue... 545203c4805SLuis R. Rodriguez */ 546203c4805SLuis R. Rodriguez 547203c4805SLuis R. Rodriguez tds = tbf->bf_desc; 548203c4805SLuis R. Rodriguez retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr, 549203c4805SLuis R. Rodriguez PA2DESC(sc, tds->ds_link), 0); 550203c4805SLuis R. Rodriguez if (retval == -EINPROGRESS) { 551203c4805SLuis R. Rodriguez break; 552203c4805SLuis R. Rodriguez } 553203c4805SLuis R. Rodriguez } 554203c4805SLuis R. Rodriguez 555203c4805SLuis R. Rodriguez skb = bf->bf_mpdu; 556203c4805SLuis R. Rodriguez if (!skb) 557203c4805SLuis R. Rodriguez continue; 558203c4805SLuis R. Rodriguez 559203c4805SLuis R. Rodriguez /* 560203c4805SLuis R. Rodriguez * Synchronize the DMA transfer with CPU before 561203c4805SLuis R. Rodriguez * 1. accessing the frame 562203c4805SLuis R. Rodriguez * 2. requeueing the same buffer to h/w 563203c4805SLuis R. Rodriguez */ 564203c4805SLuis R. Rodriguez dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, 565cc861f74SLuis R. Rodriguez common->rx_bufsize, 566203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 567203c4805SLuis R. Rodriguez 568b4afffc0SLuis R. Rodriguez hdr = (struct ieee80211_hdr *) skb->data; 5695ca42627SLuis R. Rodriguez rxs = IEEE80211_SKB_RXCB(skb); 5705ca42627SLuis R. Rodriguez 571b4afffc0SLuis R. Rodriguez hw = ath_get_virt_hw(sc, hdr); 57226ab2645SLuis R. Rodriguez rx_stats = &ds->ds_rxstat; 573b4afffc0SLuis R. Rodriguez 574*1395d3f0SSujith ath_debug_stat_rx(sc, bf); 575*1395d3f0SSujith 576203c4805SLuis R. Rodriguez /* 577203c4805SLuis R. Rodriguez * If we're asked to flush receive queue, directly 578203c4805SLuis R. Rodriguez * chain it back at the queue without processing it. 579203c4805SLuis R. Rodriguez */ 580203c4805SLuis R. Rodriguez if (flush) 581203c4805SLuis R. Rodriguez goto requeue; 582203c4805SLuis R. Rodriguez 583db86f07eSLuis R. Rodriguez retval = ath9k_cmn_rx_skb_preprocess(common, hw, skb, rx_stats, 5841e875e9fSLuis R. Rodriguez rxs, &decrypt_error); 5851e875e9fSLuis R. Rodriguez if (retval) 586203c4805SLuis R. Rodriguez goto requeue; 587203c4805SLuis R. Rodriguez 588203c4805SLuis R. Rodriguez /* Ensure we always have an skb to requeue once we are done 589203c4805SLuis R. Rodriguez * processing the current buffer's skb */ 590cc861f74SLuis R. Rodriguez requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC); 591203c4805SLuis R. Rodriguez 592203c4805SLuis R. Rodriguez /* If there is no memory we ignore the current RX'd frame, 593203c4805SLuis R. Rodriguez * tell hardware it can give us a new frame using the old 594203c4805SLuis R. Rodriguez * skb and put it at the tail of the sc->rx.rxbuf list for 595203c4805SLuis R. Rodriguez * processing. */ 596203c4805SLuis R. Rodriguez if (!requeue_skb) 597203c4805SLuis R. Rodriguez goto requeue; 598203c4805SLuis R. Rodriguez 599203c4805SLuis R. Rodriguez /* Unmap the frame */ 600203c4805SLuis R. Rodriguez dma_unmap_single(sc->dev, bf->bf_buf_addr, 601cc861f74SLuis R. Rodriguez common->rx_bufsize, 602203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 603203c4805SLuis R. Rodriguez 60426ab2645SLuis R. Rodriguez skb_put(skb, rx_stats->rs_datalen); 605203c4805SLuis R. Rodriguez 606db86f07eSLuis R. Rodriguez ath9k_cmn_rx_skb_postprocess(common, skb, rx_stats, 607c9b14170SLuis R. Rodriguez rxs, decrypt_error); 608203c4805SLuis R. Rodriguez 609203c4805SLuis R. Rodriguez /* We will now give hardware our shiny new allocated skb */ 610203c4805SLuis R. Rodriguez bf->bf_mpdu = requeue_skb; 611203c4805SLuis R. Rodriguez bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data, 612cc861f74SLuis R. Rodriguez common->rx_bufsize, 613203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 614203c4805SLuis R. Rodriguez if (unlikely(dma_mapping_error(sc->dev, 615203c4805SLuis R. Rodriguez bf->bf_buf_addr))) { 616203c4805SLuis R. Rodriguez dev_kfree_skb_any(requeue_skb); 617203c4805SLuis R. Rodriguez bf->bf_mpdu = NULL; 618c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_FATAL, 619203c4805SLuis R. Rodriguez "dma_mapping_error() on RX\n"); 6205ca42627SLuis R. Rodriguez ath_rx_send_to_mac80211(hw, sc, skb, rxs); 621203c4805SLuis R. Rodriguez break; 622203c4805SLuis R. Rodriguez } 623203c4805SLuis R. Rodriguez bf->bf_dmacontext = bf->bf_buf_addr; 624203c4805SLuis R. Rodriguez 625203c4805SLuis R. Rodriguez /* 626203c4805SLuis R. Rodriguez * change the default rx antenna if rx diversity chooses the 627203c4805SLuis R. Rodriguez * other antenna 3 times in a row. 628203c4805SLuis R. Rodriguez */ 629203c4805SLuis R. Rodriguez if (sc->rx.defant != ds->ds_rxstat.rs_antenna) { 630203c4805SLuis R. Rodriguez if (++sc->rx.rxotherant >= 3) 63126ab2645SLuis R. Rodriguez ath_setdefantenna(sc, rx_stats->rs_antenna); 632203c4805SLuis R. Rodriguez } else { 633203c4805SLuis R. Rodriguez sc->rx.rxotherant = 0; 634203c4805SLuis R. Rodriguez } 635203c4805SLuis R. Rodriguez 6361b04b930SSujith if (unlikely(sc->ps_flags & (PS_WAIT_FOR_BEACON | 6371b04b930SSujith PS_WAIT_FOR_CAB | 6381b04b930SSujith PS_WAIT_FOR_PSPOLL_DATA))) 639cc65965cSJouni Malinen ath_rx_ps(sc, skb); 640cc65965cSJouni Malinen 6415ca42627SLuis R. Rodriguez ath_rx_send_to_mac80211(hw, sc, skb, rxs); 642cc65965cSJouni Malinen 643203c4805SLuis R. Rodriguez requeue: 644203c4805SLuis R. Rodriguez list_move_tail(&bf->list, &sc->rx.rxbuf); 645203c4805SLuis R. Rodriguez ath_rx_buf_link(sc, bf); 646203c4805SLuis R. Rodriguez } while (1); 647203c4805SLuis R. Rodriguez 648203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->rx.rxbuflock); 649203c4805SLuis R. Rodriguez 650203c4805SLuis R. Rodriguez return 0; 651203c4805SLuis R. Rodriguez #undef PA2DESC 652203c4805SLuis R. Rodriguez } 653