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 92207e9685SLuis R. Rodriguez /* Assumes you've already done the endian to CPU conversion */ 93207e9685SLuis R. Rodriguez static bool ath9k_rx_accept(struct ath_common *common, 94207e9685SLuis R. Rodriguez struct sk_buff *skb, 95207e9685SLuis R. Rodriguez struct ieee80211_rx_status *rxs, 96207e9685SLuis R. Rodriguez struct ath_rx_status *rx_stats, 97207e9685SLuis R. Rodriguez bool *decrypt_error) 98207e9685SLuis R. Rodriguez { 99207e9685SLuis R. Rodriguez struct ath_hw *ah = common->ah; 100207e9685SLuis R. Rodriguez struct ieee80211_hdr *hdr; 101207e9685SLuis R. Rodriguez __le16 fc; 102207e9685SLuis R. Rodriguez 103207e9685SLuis R. Rodriguez hdr = (struct ieee80211_hdr *) skb->data; 104207e9685SLuis R. Rodriguez fc = hdr->frame_control; 105207e9685SLuis R. Rodriguez 1060a45da76SLuis R. Rodriguez if (!rx_stats->rs_datalen) 1070a45da76SLuis R. Rodriguez return false; 1082c74aa4dSLuis R. Rodriguez /* 1092c74aa4dSLuis R. Rodriguez * rs_status follows rs_datalen so if rs_datalen is too large 1102c74aa4dSLuis R. Rodriguez * we can take a hint that hardware corrupted it, so ignore 1112c74aa4dSLuis R. Rodriguez * those frames. 1122c74aa4dSLuis R. Rodriguez */ 1132c74aa4dSLuis R. Rodriguez if (rx_stats->rs_datalen > common->rx_bufsize) 1142c74aa4dSLuis R. Rodriguez return false; 1150a45da76SLuis R. Rodriguez 116207e9685SLuis R. Rodriguez if (rx_stats->rs_more) { 117207e9685SLuis R. Rodriguez /* 118207e9685SLuis R. Rodriguez * Frame spans multiple descriptors; this cannot happen yet 119207e9685SLuis R. Rodriguez * as we don't support jumbograms. If not in monitor mode, 120207e9685SLuis R. Rodriguez * discard the frame. Enable this if you want to see 121207e9685SLuis R. Rodriguez * error frames in Monitor mode. 122207e9685SLuis R. Rodriguez */ 123207e9685SLuis R. Rodriguez if (ah->opmode != NL80211_IFTYPE_MONITOR) 124207e9685SLuis R. Rodriguez return false; 125207e9685SLuis R. Rodriguez } else if (rx_stats->rs_status != 0) { 126207e9685SLuis R. Rodriguez if (rx_stats->rs_status & ATH9K_RXERR_CRC) 127207e9685SLuis R. Rodriguez rxs->flag |= RX_FLAG_FAILED_FCS_CRC; 128207e9685SLuis R. Rodriguez if (rx_stats->rs_status & ATH9K_RXERR_PHY) 129207e9685SLuis R. Rodriguez return false; 130207e9685SLuis R. Rodriguez 131207e9685SLuis R. Rodriguez if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) { 132207e9685SLuis R. Rodriguez *decrypt_error = true; 133207e9685SLuis R. Rodriguez } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) { 134207e9685SLuis R. Rodriguez if (ieee80211_is_ctl(fc)) 135207e9685SLuis R. Rodriguez /* 136207e9685SLuis R. Rodriguez * Sometimes, we get invalid 137207e9685SLuis R. Rodriguez * MIC failures on valid control frames. 138207e9685SLuis R. Rodriguez * Remove these mic errors. 139207e9685SLuis R. Rodriguez */ 140207e9685SLuis R. Rodriguez rx_stats->rs_status &= ~ATH9K_RXERR_MIC; 141207e9685SLuis R. Rodriguez else 142207e9685SLuis R. Rodriguez rxs->flag |= RX_FLAG_MMIC_ERROR; 143207e9685SLuis R. Rodriguez } 144207e9685SLuis R. Rodriguez /* 145207e9685SLuis R. Rodriguez * Reject error frames with the exception of 146207e9685SLuis R. Rodriguez * decryption and MIC failures. For monitor mode, 147207e9685SLuis R. Rodriguez * we also ignore the CRC error. 148207e9685SLuis R. Rodriguez */ 149207e9685SLuis R. Rodriguez if (ah->opmode == NL80211_IFTYPE_MONITOR) { 150207e9685SLuis R. Rodriguez if (rx_stats->rs_status & 151207e9685SLuis R. Rodriguez ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC | 152207e9685SLuis R. Rodriguez ATH9K_RXERR_CRC)) 153207e9685SLuis R. Rodriguez return false; 154207e9685SLuis R. Rodriguez } else { 155207e9685SLuis R. Rodriguez if (rx_stats->rs_status & 156207e9685SLuis R. Rodriguez ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) { 157207e9685SLuis R. Rodriguez return false; 158207e9685SLuis R. Rodriguez } 159207e9685SLuis R. Rodriguez } 160207e9685SLuis R. Rodriguez } 161207e9685SLuis R. Rodriguez return true; 162207e9685SLuis R. Rodriguez } 163207e9685SLuis R. Rodriguez 1649878841eSLuis R. Rodriguez static u8 ath9k_process_rate(struct ath_common *common, 1659878841eSLuis R. Rodriguez struct ieee80211_hw *hw, 1669878841eSLuis R. Rodriguez struct ath_rx_status *rx_stats, 1679878841eSLuis R. Rodriguez struct ieee80211_rx_status *rxs, 1689878841eSLuis R. Rodriguez struct sk_buff *skb) 1699878841eSLuis R. Rodriguez { 1709878841eSLuis R. Rodriguez struct ieee80211_supported_band *sband; 1719878841eSLuis R. Rodriguez enum ieee80211_band band; 1729878841eSLuis R. Rodriguez unsigned int i = 0; 1739878841eSLuis R. Rodriguez 1749878841eSLuis R. Rodriguez band = hw->conf.channel->band; 1759878841eSLuis R. Rodriguez sband = hw->wiphy->bands[band]; 1769878841eSLuis R. Rodriguez 1779878841eSLuis R. Rodriguez if (rx_stats->rs_rate & 0x80) { 1789878841eSLuis R. Rodriguez /* HT rate */ 1799878841eSLuis R. Rodriguez rxs->flag |= RX_FLAG_HT; 1809878841eSLuis R. Rodriguez if (rx_stats->rs_flags & ATH9K_RX_2040) 1819878841eSLuis R. Rodriguez rxs->flag |= RX_FLAG_40MHZ; 1829878841eSLuis R. Rodriguez if (rx_stats->rs_flags & ATH9K_RX_GI) 1839878841eSLuis R. Rodriguez rxs->flag |= RX_FLAG_SHORT_GI; 1849878841eSLuis R. Rodriguez return rx_stats->rs_rate & 0x7f; 1859878841eSLuis R. Rodriguez } 1869878841eSLuis R. Rodriguez 1879878841eSLuis R. Rodriguez for (i = 0; i < sband->n_bitrates; i++) { 1889878841eSLuis R. Rodriguez if (sband->bitrates[i].hw_value == rx_stats->rs_rate) 1899878841eSLuis R. Rodriguez return i; 1909878841eSLuis R. Rodriguez if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) { 1919878841eSLuis R. Rodriguez rxs->flag |= RX_FLAG_SHORTPRE; 1929878841eSLuis R. Rodriguez return i; 1939878841eSLuis R. Rodriguez } 1949878841eSLuis R. Rodriguez } 1959878841eSLuis R. Rodriguez 1969878841eSLuis R. Rodriguez /* No valid hardware bitrate found -- we should not get here */ 1979878841eSLuis R. Rodriguez ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected " 1989878841eSLuis R. Rodriguez "0x%02x using 1 Mbit\n", rx_stats->rs_rate); 1999878841eSLuis R. Rodriguez if ((common->debug_mask & ATH_DBG_XMIT)) 2009878841eSLuis R. Rodriguez print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len); 2019878841eSLuis R. Rodriguez 2029878841eSLuis R. Rodriguez return 0; 2039878841eSLuis R. Rodriguez } 2049878841eSLuis R. Rodriguez 205203c4805SLuis R. Rodriguez /* 20621b22738SLuis R. Rodriguez * Theory for reporting quality: 20721b22738SLuis R. Rodriguez * 20821b22738SLuis R. Rodriguez * At a hardware RSSI of 45 you will be able to use MCS 7 reliably. 20921b22738SLuis R. Rodriguez * At a hardware RSSI of 45 you will be able to use MCS 15 reliably. 21021b22738SLuis R. Rodriguez * At a hardware RSSI of 35 you should be able use 54 Mbps reliably. 21121b22738SLuis R. Rodriguez * 21221b22738SLuis R. Rodriguez * MCS 7 is the highets MCS index usable by a 1-stream device. 21321b22738SLuis R. Rodriguez * MCS 15 is the highest MCS index usable by a 2-stream device. 21421b22738SLuis R. Rodriguez * 21521b22738SLuis R. Rodriguez * All ath9k devices are either 1-stream or 2-stream. 21621b22738SLuis R. Rodriguez * 21721b22738SLuis R. Rodriguez * How many bars you see is derived from the qual reporting. 21821b22738SLuis R. Rodriguez * 21921b22738SLuis R. Rodriguez * A more elaborate scheme can be used here but it requires tables 22021b22738SLuis R. Rodriguez * of SNR/throughput for each possible mode used. For the MCS table 22121b22738SLuis R. Rodriguez * you can refer to the wireless wiki: 22221b22738SLuis R. Rodriguez * 22321b22738SLuis R. Rodriguez * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n 22421b22738SLuis R. Rodriguez * 22521b22738SLuis R. Rodriguez */ 22621b22738SLuis R. Rodriguez static int ath9k_compute_qual(struct ieee80211_hw *hw, 22721b22738SLuis R. Rodriguez struct ath_rx_status *rx_stats) 22821b22738SLuis R. Rodriguez { 22921b22738SLuis R. Rodriguez int qual; 23021b22738SLuis R. Rodriguez 23121b22738SLuis R. Rodriguez if (conf_is_ht(&hw->conf)) 23221b22738SLuis R. Rodriguez qual = rx_stats->rs_rssi * 100 / 45; 23321b22738SLuis R. Rodriguez else 23421b22738SLuis R. Rodriguez qual = rx_stats->rs_rssi * 100 / 35; 23521b22738SLuis R. Rodriguez 23621b22738SLuis R. Rodriguez /* 23721b22738SLuis R. Rodriguez * rssi can be more than 45 though, anything above that 23821b22738SLuis R. Rodriguez * should be considered at 100% 23921b22738SLuis R. Rodriguez */ 24021b22738SLuis R. Rodriguez if (qual > 100) 24121b22738SLuis R. Rodriguez qual = 100; 24221b22738SLuis R. Rodriguez 24321b22738SLuis R. Rodriguez return qual; 24421b22738SLuis R. Rodriguez } 24521b22738SLuis R. Rodriguez 246dbfc22dfSLuis R. Rodriguez static void ath9k_process_rssi(struct ath_common *common, 247712c13a8SLuis R. Rodriguez struct ieee80211_hw *hw, 248dbfc22dfSLuis R. Rodriguez struct sk_buff *skb, 249dbfc22dfSLuis R. Rodriguez struct ath_rx_status *rx_stats) 250203c4805SLuis R. Rodriguez { 251712c13a8SLuis R. Rodriguez struct ath_hw *ah = common->ah; 252a59b5a5eSSenthil Balasubramanian struct ieee80211_sta *sta; 253dbfc22dfSLuis R. Rodriguez struct ieee80211_hdr *hdr; 254a59b5a5eSSenthil Balasubramanian struct ath_node *an; 255a59b5a5eSSenthil Balasubramanian int last_rssi = ATH_RSSI_DUMMY_MARKER; 256dbfc22dfSLuis R. Rodriguez __le16 fc; 257a59b5a5eSSenthil Balasubramanian 258203c4805SLuis R. Rodriguez hdr = (struct ieee80211_hdr *)skb->data; 259203c4805SLuis R. Rodriguez fc = hdr->frame_control; 260203c4805SLuis R. Rodriguez 261a59b5a5eSSenthil Balasubramanian rcu_read_lock(); 2625ed176e1SJohannes Berg /* XXX: use ieee80211_find_sta! */ 263cee71d6cSLuis R. Rodriguez sta = ieee80211_find_sta_by_hw(hw, hdr->addr2); 264a59b5a5eSSenthil Balasubramanian if (sta) { 265a59b5a5eSSenthil Balasubramanian an = (struct ath_node *) sta->drv_priv; 26626ab2645SLuis R. Rodriguez if (rx_stats->rs_rssi != ATH9K_RSSI_BAD && 26726ab2645SLuis R. Rodriguez !rx_stats->rs_moreaggr) 26826ab2645SLuis R. Rodriguez ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi); 269a59b5a5eSSenthil Balasubramanian last_rssi = an->last_rssi; 270a59b5a5eSSenthil Balasubramanian } 271a59b5a5eSSenthil Balasubramanian rcu_read_unlock(); 272a59b5a5eSSenthil Balasubramanian 273a59b5a5eSSenthil Balasubramanian if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER)) 27426ab2645SLuis R. Rodriguez rx_stats->rs_rssi = ATH_EP_RND(last_rssi, 275a59b5a5eSSenthil Balasubramanian ATH_RSSI_EP_MULTIPLIER); 27626ab2645SLuis R. Rodriguez if (rx_stats->rs_rssi < 0) 27726ab2645SLuis R. Rodriguez rx_stats->rs_rssi = 0; 27826ab2645SLuis R. Rodriguez else if (rx_stats->rs_rssi > 127) 27926ab2645SLuis R. Rodriguez rx_stats->rs_rssi = 127; 280a59b5a5eSSenthil Balasubramanian 2815e32b1edSSujith /* Update Beacon RSSI, this is used by ANI. */ 2825e32b1edSSujith if (ieee80211_is_beacon(fc)) 283712c13a8SLuis R. Rodriguez ah->stats.avgbrssi = rx_stats->rs_rssi; 284dbfc22dfSLuis R. Rodriguez } 285dbfc22dfSLuis R. Rodriguez 286dbfc22dfSLuis R. Rodriguez /* 287dbfc22dfSLuis R. Rodriguez * For Decrypt or Demic errors, we only mark packet status here and always push 288dbfc22dfSLuis R. Rodriguez * up the frame up to let mac80211 handle the actual error case, be it no 289dbfc22dfSLuis R. Rodriguez * decryption key or real decryption error. This let us keep statistics there. 290dbfc22dfSLuis R. Rodriguez */ 2911e875e9fSLuis R. Rodriguez static int ath9k_rx_skb_preprocess(struct ath_common *common, 292dbfc22dfSLuis R. Rodriguez struct ieee80211_hw *hw, 2931e875e9fSLuis R. Rodriguez struct sk_buff *skb, 2941e875e9fSLuis R. Rodriguez struct ath_rx_status *rx_stats, 295dbfc22dfSLuis R. Rodriguez struct ieee80211_rx_status *rx_status, 296dbfc22dfSLuis R. Rodriguez bool *decrypt_error) 297dbfc22dfSLuis R. Rodriguez { 298dbfc22dfSLuis R. Rodriguez struct ath_hw *ah = common->ah; 299dbfc22dfSLuis R. Rodriguez 300dbfc22dfSLuis R. Rodriguez if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error)) 3011e875e9fSLuis R. Rodriguez return -EINVAL; 302dbfc22dfSLuis R. Rodriguez 303dbfc22dfSLuis R. Rodriguez ath9k_process_rssi(common, hw, skb, rx_stats); 3045e32b1edSSujith 3059878841eSLuis R. Rodriguez rx_status->rate_idx = ath9k_process_rate(common, hw, 3069878841eSLuis R. Rodriguez rx_stats, rx_status, skb); 307712c13a8SLuis R. Rodriguez rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp); 308203c4805SLuis R. Rodriguez rx_status->band = hw->conf.channel->band; 309203c4805SLuis R. Rodriguez rx_status->freq = hw->conf.channel->center_freq; 3103d536acfSLuis R. Rodriguez rx_status->noise = common->ani.noise_floor; 31126ab2645SLuis R. Rodriguez rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi; 31226ab2645SLuis R. Rodriguez rx_status->antenna = rx_stats->rs_antenna; 31321b22738SLuis R. Rodriguez rx_status->qual = ath9k_compute_qual(hw, rx_stats); 314203c4805SLuis R. Rodriguez rx_status->flag |= RX_FLAG_TSFT; 315203c4805SLuis R. Rodriguez 316203c4805SLuis R. Rodriguez return 0; 317203c4805SLuis R. Rodriguez } 318203c4805SLuis R. Rodriguez 319*c9b14170SLuis R. Rodriguez static void ath9k_rx_skb_postprocess(struct ath_common *common, 320*c9b14170SLuis R. Rodriguez struct sk_buff *skb, 321*c9b14170SLuis R. Rodriguez struct ath_rx_status *rx_stats, 322*c9b14170SLuis R. Rodriguez struct ieee80211_rx_status *rxs, 323*c9b14170SLuis R. Rodriguez bool decrypt_error) 324*c9b14170SLuis R. Rodriguez { 325*c9b14170SLuis R. Rodriguez struct ath_hw *ah = common->ah; 326*c9b14170SLuis R. Rodriguez struct ieee80211_hdr *hdr; 327*c9b14170SLuis R. Rodriguez int hdrlen, padsize; 328*c9b14170SLuis R. Rodriguez u8 keyix; 329*c9b14170SLuis R. Rodriguez __le16 fc; 330*c9b14170SLuis R. Rodriguez 331*c9b14170SLuis R. Rodriguez /* see if any padding is done by the hw and remove it */ 332*c9b14170SLuis R. Rodriguez hdr = (struct ieee80211_hdr *) skb->data; 333*c9b14170SLuis R. Rodriguez hdrlen = ieee80211_get_hdrlen_from_skb(skb); 334*c9b14170SLuis R. Rodriguez fc = hdr->frame_control; 335*c9b14170SLuis R. Rodriguez 336*c9b14170SLuis R. Rodriguez /* The MAC header is padded to have 32-bit boundary if the 337*c9b14170SLuis R. Rodriguez * packet payload is non-zero. The general calculation for 338*c9b14170SLuis R. Rodriguez * padsize would take into account odd header lengths: 339*c9b14170SLuis R. Rodriguez * padsize = (4 - hdrlen % 4) % 4; However, since only 340*c9b14170SLuis R. Rodriguez * even-length headers are used, padding can only be 0 or 2 341*c9b14170SLuis R. Rodriguez * bytes and we can optimize this a bit. In addition, we must 342*c9b14170SLuis R. Rodriguez * not try to remove padding from short control frames that do 343*c9b14170SLuis R. Rodriguez * not have payload. */ 344*c9b14170SLuis R. Rodriguez padsize = hdrlen & 3; 345*c9b14170SLuis R. Rodriguez if (padsize && hdrlen >= 24) { 346*c9b14170SLuis R. Rodriguez memmove(skb->data + padsize, skb->data, hdrlen); 347*c9b14170SLuis R. Rodriguez skb_pull(skb, padsize); 348*c9b14170SLuis R. Rodriguez } 349*c9b14170SLuis R. Rodriguez 350*c9b14170SLuis R. Rodriguez keyix = rx_stats->rs_keyix; 351*c9b14170SLuis R. Rodriguez 352*c9b14170SLuis R. Rodriguez if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) { 353*c9b14170SLuis R. Rodriguez rxs->flag |= RX_FLAG_DECRYPTED; 354*c9b14170SLuis R. Rodriguez } else if (ieee80211_has_protected(fc) 355*c9b14170SLuis R. Rodriguez && !decrypt_error && skb->len >= hdrlen + 4) { 356*c9b14170SLuis R. Rodriguez keyix = skb->data[hdrlen + 3] >> 6; 357*c9b14170SLuis R. Rodriguez 358*c9b14170SLuis R. Rodriguez if (test_bit(keyix, common->keymap)) 359*c9b14170SLuis R. Rodriguez rxs->flag |= RX_FLAG_DECRYPTED; 360*c9b14170SLuis R. Rodriguez } 361*c9b14170SLuis R. Rodriguez if (ah->sw_mgmt_crypto && 362*c9b14170SLuis R. Rodriguez (rxs->flag & RX_FLAG_DECRYPTED) && 363*c9b14170SLuis R. Rodriguez ieee80211_is_mgmt(fc)) 364*c9b14170SLuis R. Rodriguez /* Use software decrypt for management frames. */ 365*c9b14170SLuis R. Rodriguez rxs->flag &= ~RX_FLAG_DECRYPTED; 366*c9b14170SLuis R. Rodriguez } 367*c9b14170SLuis R. Rodriguez 368203c4805SLuis R. Rodriguez static void ath_opmode_init(struct ath_softc *sc) 369203c4805SLuis R. Rodriguez { 370203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 3711510718dSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 3721510718dSLuis R. Rodriguez 373203c4805SLuis R. Rodriguez u32 rfilt, mfilt[2]; 374203c4805SLuis R. Rodriguez 375203c4805SLuis R. Rodriguez /* configure rx filter */ 376203c4805SLuis R. Rodriguez rfilt = ath_calcrxfilter(sc); 377203c4805SLuis R. Rodriguez ath9k_hw_setrxfilter(ah, rfilt); 378203c4805SLuis R. Rodriguez 379203c4805SLuis R. Rodriguez /* configure bssid mask */ 380203c4805SLuis R. Rodriguez if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) 38113b81559SLuis R. Rodriguez ath_hw_setbssidmask(common); 382203c4805SLuis R. Rodriguez 383203c4805SLuis R. Rodriguez /* configure operational mode */ 384203c4805SLuis R. Rodriguez ath9k_hw_setopmode(ah); 385203c4805SLuis R. Rodriguez 386203c4805SLuis R. Rodriguez /* Handle any link-level address change. */ 3871510718dSLuis R. Rodriguez ath9k_hw_setmac(ah, common->macaddr); 388203c4805SLuis R. Rodriguez 389203c4805SLuis R. Rodriguez /* calculate and install multicast filter */ 390203c4805SLuis R. Rodriguez mfilt[0] = mfilt[1] = ~0; 391203c4805SLuis R. Rodriguez ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]); 392203c4805SLuis R. Rodriguez } 393203c4805SLuis R. Rodriguez 394203c4805SLuis R. Rodriguez int ath_rx_init(struct ath_softc *sc, int nbufs) 395203c4805SLuis R. Rodriguez { 39627c51f1aSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah); 397203c4805SLuis R. Rodriguez struct sk_buff *skb; 398203c4805SLuis R. Rodriguez struct ath_buf *bf; 399203c4805SLuis R. Rodriguez int error = 0; 400203c4805SLuis R. Rodriguez 401203c4805SLuis R. Rodriguez spin_lock_init(&sc->rx.rxflushlock); 402203c4805SLuis R. Rodriguez sc->sc_flags &= ~SC_OP_RXFLUSH; 403203c4805SLuis R. Rodriguez spin_lock_init(&sc->rx.rxbuflock); 404203c4805SLuis R. Rodriguez 405cc861f74SLuis R. Rodriguez common->rx_bufsize = roundup(IEEE80211_MAX_MPDU_LEN, 40627c51f1aSLuis R. Rodriguez min(common->cachelsz, (u16)64)); 407203c4805SLuis R. Rodriguez 408c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_CONFIG, "cachelsz %u rxbufsize %u\n", 409cc861f74SLuis R. Rodriguez common->cachelsz, common->rx_bufsize); 410203c4805SLuis R. Rodriguez 411203c4805SLuis R. Rodriguez /* Initialize rx descriptors */ 412203c4805SLuis R. Rodriguez 413203c4805SLuis R. Rodriguez error = ath_descdma_setup(sc, &sc->rx.rxdma, &sc->rx.rxbuf, 414203c4805SLuis R. Rodriguez "rx", nbufs, 1); 415203c4805SLuis R. Rodriguez if (error != 0) { 416c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_FATAL, 417203c4805SLuis R. Rodriguez "failed to allocate rx descriptors: %d\n", error); 418203c4805SLuis R. Rodriguez goto err; 419203c4805SLuis R. Rodriguez } 420203c4805SLuis R. Rodriguez 421203c4805SLuis R. Rodriguez list_for_each_entry(bf, &sc->rx.rxbuf, list) { 422cc861f74SLuis R. Rodriguez skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_KERNEL); 423203c4805SLuis R. Rodriguez if (skb == NULL) { 424203c4805SLuis R. Rodriguez error = -ENOMEM; 425203c4805SLuis R. Rodriguez goto err; 426203c4805SLuis R. Rodriguez } 427203c4805SLuis R. Rodriguez 428203c4805SLuis R. Rodriguez bf->bf_mpdu = skb; 429203c4805SLuis R. Rodriguez bf->bf_buf_addr = dma_map_single(sc->dev, skb->data, 430cc861f74SLuis R. Rodriguez common->rx_bufsize, 431203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 432203c4805SLuis R. Rodriguez if (unlikely(dma_mapping_error(sc->dev, 433203c4805SLuis R. Rodriguez bf->bf_buf_addr))) { 434203c4805SLuis R. Rodriguez dev_kfree_skb_any(skb); 435203c4805SLuis R. Rodriguez bf->bf_mpdu = NULL; 436c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_FATAL, 437203c4805SLuis R. Rodriguez "dma_mapping_error() on RX init\n"); 438203c4805SLuis R. Rodriguez error = -ENOMEM; 439203c4805SLuis R. Rodriguez goto err; 440203c4805SLuis R. Rodriguez } 441203c4805SLuis R. Rodriguez bf->bf_dmacontext = bf->bf_buf_addr; 442203c4805SLuis R. Rodriguez } 443203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 444203c4805SLuis R. Rodriguez 445203c4805SLuis R. Rodriguez err: 446203c4805SLuis R. Rodriguez if (error) 447203c4805SLuis R. Rodriguez ath_rx_cleanup(sc); 448203c4805SLuis R. Rodriguez 449203c4805SLuis R. Rodriguez return error; 450203c4805SLuis R. Rodriguez } 451203c4805SLuis R. Rodriguez 452203c4805SLuis R. Rodriguez void ath_rx_cleanup(struct ath_softc *sc) 453203c4805SLuis R. Rodriguez { 454cc861f74SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 455cc861f74SLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 456203c4805SLuis R. Rodriguez struct sk_buff *skb; 457203c4805SLuis R. Rodriguez struct ath_buf *bf; 458203c4805SLuis R. Rodriguez 459203c4805SLuis R. Rodriguez list_for_each_entry(bf, &sc->rx.rxbuf, list) { 460203c4805SLuis R. Rodriguez skb = bf->bf_mpdu; 461203c4805SLuis R. Rodriguez if (skb) { 462203c4805SLuis R. Rodriguez dma_unmap_single(sc->dev, bf->bf_buf_addr, 463cc861f74SLuis R. Rodriguez common->rx_bufsize, DMA_FROM_DEVICE); 464203c4805SLuis R. Rodriguez dev_kfree_skb(skb); 465203c4805SLuis R. Rodriguez } 466203c4805SLuis R. Rodriguez } 467203c4805SLuis R. Rodriguez 468203c4805SLuis R. Rodriguez if (sc->rx.rxdma.dd_desc_len != 0) 469203c4805SLuis R. Rodriguez ath_descdma_cleanup(sc, &sc->rx.rxdma, &sc->rx.rxbuf); 470203c4805SLuis R. Rodriguez } 471203c4805SLuis R. Rodriguez 472203c4805SLuis R. Rodriguez /* 473203c4805SLuis R. Rodriguez * Calculate the receive filter according to the 474203c4805SLuis R. Rodriguez * operating mode and state: 475203c4805SLuis R. Rodriguez * 476203c4805SLuis R. Rodriguez * o always accept unicast, broadcast, and multicast traffic 477203c4805SLuis R. Rodriguez * o maintain current state of phy error reception (the hal 478203c4805SLuis R. Rodriguez * may enable phy error frames for noise immunity work) 479203c4805SLuis R. Rodriguez * o probe request frames are accepted only when operating in 480203c4805SLuis R. Rodriguez * hostap, adhoc, or monitor modes 481203c4805SLuis R. Rodriguez * o enable promiscuous mode according to the interface state 482203c4805SLuis R. Rodriguez * o accept beacons: 483203c4805SLuis R. Rodriguez * - when operating in adhoc mode so the 802.11 layer creates 484203c4805SLuis R. Rodriguez * node table entries for peers, 485203c4805SLuis R. Rodriguez * - when operating in station mode for collecting rssi data when 486203c4805SLuis R. Rodriguez * the station is otherwise quiet, or 487203c4805SLuis R. Rodriguez * - when operating as a repeater so we see repeater-sta beacons 488203c4805SLuis R. Rodriguez * - when scanning 489203c4805SLuis R. Rodriguez */ 490203c4805SLuis R. Rodriguez 491203c4805SLuis R. Rodriguez u32 ath_calcrxfilter(struct ath_softc *sc) 492203c4805SLuis R. Rodriguez { 493203c4805SLuis R. Rodriguez #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR) 494203c4805SLuis R. Rodriguez 495203c4805SLuis R. Rodriguez u32 rfilt; 496203c4805SLuis R. Rodriguez 497203c4805SLuis R. Rodriguez rfilt = (ath9k_hw_getrxfilter(sc->sc_ah) & RX_FILTER_PRESERVE) 498203c4805SLuis R. Rodriguez | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST 499203c4805SLuis R. Rodriguez | ATH9K_RX_FILTER_MCAST; 500203c4805SLuis R. Rodriguez 501203c4805SLuis R. Rodriguez /* If not a STA, enable processing of Probe Requests */ 502203c4805SLuis R. Rodriguez if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION) 503203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PROBEREQ; 504203c4805SLuis R. Rodriguez 505203c4805SLuis R. Rodriguez /* 506203c4805SLuis R. Rodriguez * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station 507203c4805SLuis R. Rodriguez * mode interface or when in monitor mode. AP mode does not need this 508203c4805SLuis R. Rodriguez * since it receives all in-BSS frames anyway. 509203c4805SLuis R. Rodriguez */ 510203c4805SLuis R. Rodriguez if (((sc->sc_ah->opmode != NL80211_IFTYPE_AP) && 511203c4805SLuis R. Rodriguez (sc->rx.rxfilter & FIF_PROMISC_IN_BSS)) || 512203c4805SLuis R. Rodriguez (sc->sc_ah->opmode == NL80211_IFTYPE_MONITOR)) 513203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PROM; 514203c4805SLuis R. Rodriguez 515203c4805SLuis R. Rodriguez if (sc->rx.rxfilter & FIF_CONTROL) 516203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_CONTROL; 517203c4805SLuis R. Rodriguez 518203c4805SLuis R. Rodriguez if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) && 519203c4805SLuis R. Rodriguez !(sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)) 520203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_MYBEACON; 521203c4805SLuis R. Rodriguez else 522203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_BEACON; 523203c4805SLuis R. Rodriguez 52466afad01SSenthil Balasubramanian if ((AR_SREV_9280_10_OR_LATER(sc->sc_ah) || 52566afad01SSenthil Balasubramanian AR_SREV_9285_10_OR_LATER(sc->sc_ah)) && 52666afad01SSenthil Balasubramanian (sc->sc_ah->opmode == NL80211_IFTYPE_AP) && 52766afad01SSenthil Balasubramanian (sc->rx.rxfilter & FIF_PSPOLL)) 528203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_PSPOLL; 529203c4805SLuis R. Rodriguez 5307ea310beSSujith if (conf_is_ht(&sc->hw->conf)) 5317ea310beSSujith rfilt |= ATH9K_RX_FILTER_COMP_BAR; 5327ea310beSSujith 5335eb6ba83SJavier Cardona if (sc->sec_wiphy || (sc->rx.rxfilter & FIF_OTHER_BSS)) { 534203c4805SLuis R. Rodriguez /* TODO: only needed if more than one BSSID is in use in 535203c4805SLuis R. Rodriguez * station/adhoc mode */ 5365eb6ba83SJavier Cardona /* The following may also be needed for other older chips */ 5375eb6ba83SJavier Cardona if (sc->sc_ah->hw_version.macVersion == AR_SREV_VERSION_9160) 5385eb6ba83SJavier Cardona rfilt |= ATH9K_RX_FILTER_PROM; 539203c4805SLuis R. Rodriguez rfilt |= ATH9K_RX_FILTER_MCAST_BCAST_ALL; 540203c4805SLuis R. Rodriguez } 541203c4805SLuis R. Rodriguez 542203c4805SLuis R. Rodriguez return rfilt; 543203c4805SLuis R. Rodriguez 544203c4805SLuis R. Rodriguez #undef RX_FILTER_PRESERVE 545203c4805SLuis R. Rodriguez } 546203c4805SLuis R. Rodriguez 547203c4805SLuis R. Rodriguez int ath_startrecv(struct ath_softc *sc) 548203c4805SLuis R. Rodriguez { 549203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 550203c4805SLuis R. Rodriguez struct ath_buf *bf, *tbf; 551203c4805SLuis R. Rodriguez 552203c4805SLuis R. Rodriguez spin_lock_bh(&sc->rx.rxbuflock); 553203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) 554203c4805SLuis R. Rodriguez goto start_recv; 555203c4805SLuis R. Rodriguez 556203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 557203c4805SLuis R. Rodriguez list_for_each_entry_safe(bf, tbf, &sc->rx.rxbuf, list) { 558203c4805SLuis R. Rodriguez ath_rx_buf_link(sc, bf); 559203c4805SLuis R. Rodriguez } 560203c4805SLuis R. Rodriguez 561203c4805SLuis R. Rodriguez /* We could have deleted elements so the list may be empty now */ 562203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) 563203c4805SLuis R. Rodriguez goto start_recv; 564203c4805SLuis R. Rodriguez 565203c4805SLuis R. Rodriguez bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); 566203c4805SLuis R. Rodriguez ath9k_hw_putrxbuf(ah, bf->bf_daddr); 567203c4805SLuis R. Rodriguez ath9k_hw_rxena(ah); 568203c4805SLuis R. Rodriguez 569203c4805SLuis R. Rodriguez start_recv: 570203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->rx.rxbuflock); 571203c4805SLuis R. Rodriguez ath_opmode_init(sc); 572203c4805SLuis R. Rodriguez ath9k_hw_startpcureceive(ah); 573203c4805SLuis R. Rodriguez 574203c4805SLuis R. Rodriguez return 0; 575203c4805SLuis R. Rodriguez } 576203c4805SLuis R. Rodriguez 577203c4805SLuis R. Rodriguez bool ath_stoprecv(struct ath_softc *sc) 578203c4805SLuis R. Rodriguez { 579203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 580203c4805SLuis R. Rodriguez bool stopped; 581203c4805SLuis R. Rodriguez 582203c4805SLuis R. Rodriguez ath9k_hw_stoppcurecv(ah); 583203c4805SLuis R. Rodriguez ath9k_hw_setrxfilter(ah, 0); 584203c4805SLuis R. Rodriguez stopped = ath9k_hw_stopdmarecv(ah); 585203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 586203c4805SLuis R. Rodriguez 587203c4805SLuis R. Rodriguez return stopped; 588203c4805SLuis R. Rodriguez } 589203c4805SLuis R. Rodriguez 590203c4805SLuis R. Rodriguez void ath_flushrecv(struct ath_softc *sc) 591203c4805SLuis R. Rodriguez { 592203c4805SLuis R. Rodriguez spin_lock_bh(&sc->rx.rxflushlock); 593203c4805SLuis R. Rodriguez sc->sc_flags |= SC_OP_RXFLUSH; 594203c4805SLuis R. Rodriguez ath_rx_tasklet(sc, 1); 595203c4805SLuis R. Rodriguez sc->sc_flags &= ~SC_OP_RXFLUSH; 596203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->rx.rxflushlock); 597203c4805SLuis R. Rodriguez } 598203c4805SLuis R. Rodriguez 599cc65965cSJouni Malinen static bool ath_beacon_dtim_pending_cab(struct sk_buff *skb) 600cc65965cSJouni Malinen { 601cc65965cSJouni Malinen /* Check whether the Beacon frame has DTIM indicating buffered bc/mc */ 602cc65965cSJouni Malinen struct ieee80211_mgmt *mgmt; 603cc65965cSJouni Malinen u8 *pos, *end, id, elen; 604cc65965cSJouni Malinen struct ieee80211_tim_ie *tim; 605cc65965cSJouni Malinen 606cc65965cSJouni Malinen mgmt = (struct ieee80211_mgmt *)skb->data; 607cc65965cSJouni Malinen pos = mgmt->u.beacon.variable; 608cc65965cSJouni Malinen end = skb->data + skb->len; 609cc65965cSJouni Malinen 610cc65965cSJouni Malinen while (pos + 2 < end) { 611cc65965cSJouni Malinen id = *pos++; 612cc65965cSJouni Malinen elen = *pos++; 613cc65965cSJouni Malinen if (pos + elen > end) 614cc65965cSJouni Malinen break; 615cc65965cSJouni Malinen 616cc65965cSJouni Malinen if (id == WLAN_EID_TIM) { 617cc65965cSJouni Malinen if (elen < sizeof(*tim)) 618cc65965cSJouni Malinen break; 619cc65965cSJouni Malinen tim = (struct ieee80211_tim_ie *) pos; 620cc65965cSJouni Malinen if (tim->dtim_count != 0) 621cc65965cSJouni Malinen break; 622cc65965cSJouni Malinen return tim->bitmap_ctrl & 0x01; 623cc65965cSJouni Malinen } 624cc65965cSJouni Malinen 625cc65965cSJouni Malinen pos += elen; 626cc65965cSJouni Malinen } 627cc65965cSJouni Malinen 628cc65965cSJouni Malinen return false; 629cc65965cSJouni Malinen } 630cc65965cSJouni Malinen 631cc65965cSJouni Malinen static void ath_rx_ps_beacon(struct ath_softc *sc, struct sk_buff *skb) 632cc65965cSJouni Malinen { 633cc65965cSJouni Malinen struct ieee80211_mgmt *mgmt; 6341510718dSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah); 635cc65965cSJouni Malinen 636cc65965cSJouni Malinen if (skb->len < 24 + 8 + 2 + 2) 637cc65965cSJouni Malinen return; 638cc65965cSJouni Malinen 639cc65965cSJouni Malinen mgmt = (struct ieee80211_mgmt *)skb->data; 6401510718dSLuis R. Rodriguez if (memcmp(common->curbssid, mgmt->bssid, ETH_ALEN) != 0) 641cc65965cSJouni Malinen return; /* not from our current AP */ 642cc65965cSJouni Malinen 643293dc5dfSGabor Juhos sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON; 644293dc5dfSGabor Juhos 645ccdfeab6SJouni Malinen if (sc->sc_flags & SC_OP_BEACON_SYNC) { 646ccdfeab6SJouni Malinen sc->sc_flags &= ~SC_OP_BEACON_SYNC; 647c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 648c46917bbSLuis R. Rodriguez "Reconfigure Beacon timers based on " 649ccdfeab6SJouni Malinen "timestamp from the AP\n"); 650ccdfeab6SJouni Malinen ath_beacon_config(sc, NULL); 651ccdfeab6SJouni Malinen } 652ccdfeab6SJouni Malinen 653cc65965cSJouni Malinen if (ath_beacon_dtim_pending_cab(skb)) { 654cc65965cSJouni Malinen /* 655cc65965cSJouni Malinen * Remain awake waiting for buffered broadcast/multicast 65658f5fffdSGabor Juhos * frames. If the last broadcast/multicast frame is not 65758f5fffdSGabor Juhos * received properly, the next beacon frame will work as 65858f5fffdSGabor Juhos * a backup trigger for returning into NETWORK SLEEP state, 65958f5fffdSGabor Juhos * so we are waiting for it as well. 660cc65965cSJouni Malinen */ 661c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, "Received DTIM beacon indicating " 662cc65965cSJouni Malinen "buffered broadcast/multicast frame(s)\n"); 66358f5fffdSGabor Juhos sc->sc_flags |= SC_OP_WAIT_FOR_CAB | SC_OP_WAIT_FOR_BEACON; 664cc65965cSJouni Malinen return; 665cc65965cSJouni Malinen } 666cc65965cSJouni Malinen 667cc65965cSJouni Malinen if (sc->sc_flags & SC_OP_WAIT_FOR_CAB) { 668cc65965cSJouni Malinen /* 669cc65965cSJouni Malinen * This can happen if a broadcast frame is dropped or the AP 670cc65965cSJouni Malinen * fails to send a frame indicating that all CAB frames have 671cc65965cSJouni Malinen * been delivered. 672cc65965cSJouni Malinen */ 673293dc5dfSGabor Juhos sc->sc_flags &= ~SC_OP_WAIT_FOR_CAB; 674c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 675c46917bbSLuis R. Rodriguez "PS wait for CAB frames timed out\n"); 676cc65965cSJouni Malinen } 677cc65965cSJouni Malinen } 678cc65965cSJouni Malinen 679cc65965cSJouni Malinen static void ath_rx_ps(struct ath_softc *sc, struct sk_buff *skb) 680cc65965cSJouni Malinen { 681cc65965cSJouni Malinen struct ieee80211_hdr *hdr; 682c46917bbSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(sc->sc_ah); 683cc65965cSJouni Malinen 684cc65965cSJouni Malinen hdr = (struct ieee80211_hdr *)skb->data; 685cc65965cSJouni Malinen 686cc65965cSJouni Malinen /* Process Beacon and CAB receive in PS state */ 6879a23f9caSJouni Malinen if ((sc->sc_flags & SC_OP_WAIT_FOR_BEACON) && 6889a23f9caSJouni Malinen ieee80211_is_beacon(hdr->frame_control)) 689cc65965cSJouni Malinen ath_rx_ps_beacon(sc, skb); 690cc65965cSJouni Malinen else if ((sc->sc_flags & SC_OP_WAIT_FOR_CAB) && 691cc65965cSJouni Malinen (ieee80211_is_data(hdr->frame_control) || 692cc65965cSJouni Malinen ieee80211_is_action(hdr->frame_control)) && 693cc65965cSJouni Malinen is_multicast_ether_addr(hdr->addr1) && 694cc65965cSJouni Malinen !ieee80211_has_moredata(hdr->frame_control)) { 695cc65965cSJouni Malinen /* 696cc65965cSJouni Malinen * No more broadcast/multicast frames to be received at this 697cc65965cSJouni Malinen * point. 698cc65965cSJouni Malinen */ 699293dc5dfSGabor Juhos sc->sc_flags &= ~SC_OP_WAIT_FOR_CAB; 700c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 701c46917bbSLuis R. Rodriguez "All PS CAB frames received, back to sleep\n"); 7029a23f9caSJouni Malinen } else if ((sc->sc_flags & SC_OP_WAIT_FOR_PSPOLL_DATA) && 7039a23f9caSJouni Malinen !is_multicast_ether_addr(hdr->addr1) && 7049a23f9caSJouni Malinen !ieee80211_has_morefrags(hdr->frame_control)) { 7059a23f9caSJouni Malinen sc->sc_flags &= ~SC_OP_WAIT_FOR_PSPOLL_DATA; 706c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_PS, 707c46917bbSLuis R. Rodriguez "Going back to sleep after having received " 708c46917bbSLuis R. Rodriguez "PS-Poll data (0x%x)\n", 7099a23f9caSJouni Malinen sc->sc_flags & (SC_OP_WAIT_FOR_BEACON | 7109a23f9caSJouni Malinen SC_OP_WAIT_FOR_CAB | 7119a23f9caSJouni Malinen SC_OP_WAIT_FOR_PSPOLL_DATA | 7129a23f9caSJouni Malinen SC_OP_WAIT_FOR_TX_ACK)); 713cc65965cSJouni Malinen } 714cc65965cSJouni Malinen } 715cc65965cSJouni Malinen 716b4afffc0SLuis R. Rodriguez static void ath_rx_send_to_mac80211(struct ieee80211_hw *hw, 717b4afffc0SLuis R. Rodriguez struct ath_softc *sc, struct sk_buff *skb, 7185ca42627SLuis R. Rodriguez struct ieee80211_rx_status *rxs) 7199d64a3cfSJouni Malinen { 7209d64a3cfSJouni Malinen struct ieee80211_hdr *hdr; 7219d64a3cfSJouni Malinen 7229d64a3cfSJouni Malinen hdr = (struct ieee80211_hdr *)skb->data; 7239d64a3cfSJouni Malinen 7249d64a3cfSJouni Malinen /* Send the frame to mac80211 */ 7259d64a3cfSJouni Malinen if (is_multicast_ether_addr(hdr->addr1)) { 7269d64a3cfSJouni Malinen int i; 7279d64a3cfSJouni Malinen /* 7289d64a3cfSJouni Malinen * Deliver broadcast/multicast frames to all suitable 7299d64a3cfSJouni Malinen * virtual wiphys. 7309d64a3cfSJouni Malinen */ 7319d64a3cfSJouni Malinen /* TODO: filter based on channel configuration */ 7329d64a3cfSJouni Malinen for (i = 0; i < sc->num_sec_wiphy; i++) { 7339d64a3cfSJouni Malinen struct ath_wiphy *aphy = sc->sec_wiphy[i]; 7349d64a3cfSJouni Malinen struct sk_buff *nskb; 7359d64a3cfSJouni Malinen if (aphy == NULL) 7369d64a3cfSJouni Malinen continue; 7379d64a3cfSJouni Malinen nskb = skb_copy(skb, GFP_ATOMIC); 7385ca42627SLuis R. Rodriguez if (!nskb) 7395ca42627SLuis R. Rodriguez continue; 740f1d58c25SJohannes Berg ieee80211_rx(aphy->hw, nskb); 7419d64a3cfSJouni Malinen } 742f1d58c25SJohannes Berg ieee80211_rx(sc->hw, skb); 7435ca42627SLuis R. Rodriguez } else 7449d64a3cfSJouni Malinen /* Deliver unicast frames based on receiver address */ 745b4afffc0SLuis R. Rodriguez ieee80211_rx(hw, skb); 7469d64a3cfSJouni Malinen } 7479d64a3cfSJouni Malinen 748203c4805SLuis R. Rodriguez int ath_rx_tasklet(struct ath_softc *sc, int flush) 749203c4805SLuis R. Rodriguez { 750203c4805SLuis R. Rodriguez #define PA2DESC(_sc, _pa) \ 751203c4805SLuis R. Rodriguez ((struct ath_desc *)((caddr_t)(_sc)->rx.rxdma.dd_desc + \ 752203c4805SLuis R. Rodriguez ((_pa) - (_sc)->rx.rxdma.dd_desc_paddr))) 753203c4805SLuis R. Rodriguez 754203c4805SLuis R. Rodriguez struct ath_buf *bf; 755203c4805SLuis R. Rodriguez struct ath_desc *ds; 75626ab2645SLuis R. Rodriguez struct ath_rx_status *rx_stats; 757203c4805SLuis R. Rodriguez struct sk_buff *skb = NULL, *requeue_skb; 7585ca42627SLuis R. Rodriguez struct ieee80211_rx_status *rxs; 759203c4805SLuis R. Rodriguez struct ath_hw *ah = sc->sc_ah; 76027c51f1aSLuis R. Rodriguez struct ath_common *common = ath9k_hw_common(ah); 761b4afffc0SLuis R. Rodriguez /* 762b4afffc0SLuis R. Rodriguez * The hw can techncically differ from common->hw when using ath9k 763b4afffc0SLuis R. Rodriguez * virtual wiphy so to account for that we iterate over the active 764b4afffc0SLuis R. Rodriguez * wiphys and find the appropriate wiphy and therefore hw. 765b4afffc0SLuis R. Rodriguez */ 766b4afffc0SLuis R. Rodriguez struct ieee80211_hw *hw = NULL; 767203c4805SLuis R. Rodriguez struct ieee80211_hdr *hdr; 768*c9b14170SLuis R. Rodriguez int retval; 769203c4805SLuis R. Rodriguez bool decrypt_error = false; 770203c4805SLuis R. Rodriguez 771203c4805SLuis R. Rodriguez spin_lock_bh(&sc->rx.rxbuflock); 772203c4805SLuis R. Rodriguez 773203c4805SLuis R. Rodriguez do { 774203c4805SLuis R. Rodriguez /* If handling rx interrupt and flush is in progress => exit */ 775203c4805SLuis R. Rodriguez if ((sc->sc_flags & SC_OP_RXFLUSH) && (flush == 0)) 776203c4805SLuis R. Rodriguez break; 777203c4805SLuis R. Rodriguez 778203c4805SLuis R. Rodriguez if (list_empty(&sc->rx.rxbuf)) { 779203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 780203c4805SLuis R. Rodriguez break; 781203c4805SLuis R. Rodriguez } 782203c4805SLuis R. Rodriguez 783203c4805SLuis R. Rodriguez bf = list_first_entry(&sc->rx.rxbuf, struct ath_buf, list); 784203c4805SLuis R. Rodriguez ds = bf->bf_desc; 785203c4805SLuis R. Rodriguez 786203c4805SLuis R. Rodriguez /* 787203c4805SLuis R. Rodriguez * Must provide the virtual address of the current 788203c4805SLuis R. Rodriguez * descriptor, the physical address, and the virtual 789203c4805SLuis R. Rodriguez * address of the next descriptor in the h/w chain. 790203c4805SLuis R. Rodriguez * This allows the HAL to look ahead to see if the 791203c4805SLuis R. Rodriguez * hardware is done with a descriptor by checking the 792203c4805SLuis R. Rodriguez * done bit in the following descriptor and the address 793203c4805SLuis R. Rodriguez * of the current descriptor the DMA engine is working 794203c4805SLuis R. Rodriguez * on. All this is necessary because of our use of 795203c4805SLuis R. Rodriguez * a self-linked list to avoid rx overruns. 796203c4805SLuis R. Rodriguez */ 797203c4805SLuis R. Rodriguez retval = ath9k_hw_rxprocdesc(ah, ds, 798203c4805SLuis R. Rodriguez bf->bf_daddr, 799203c4805SLuis R. Rodriguez PA2DESC(sc, ds->ds_link), 800203c4805SLuis R. Rodriguez 0); 801203c4805SLuis R. Rodriguez if (retval == -EINPROGRESS) { 802203c4805SLuis R. Rodriguez struct ath_buf *tbf; 803203c4805SLuis R. Rodriguez struct ath_desc *tds; 804203c4805SLuis R. Rodriguez 805203c4805SLuis R. Rodriguez if (list_is_last(&bf->list, &sc->rx.rxbuf)) { 806203c4805SLuis R. Rodriguez sc->rx.rxlink = NULL; 807203c4805SLuis R. Rodriguez break; 808203c4805SLuis R. Rodriguez } 809203c4805SLuis R. Rodriguez 810203c4805SLuis R. Rodriguez tbf = list_entry(bf->list.next, struct ath_buf, list); 811203c4805SLuis R. Rodriguez 812203c4805SLuis R. Rodriguez /* 813203c4805SLuis R. Rodriguez * On some hardware the descriptor status words could 814203c4805SLuis R. Rodriguez * get corrupted, including the done bit. Because of 815203c4805SLuis R. Rodriguez * this, check if the next descriptor's done bit is 816203c4805SLuis R. Rodriguez * set or not. 817203c4805SLuis R. Rodriguez * 818203c4805SLuis R. Rodriguez * If the next descriptor's done bit is set, the current 819203c4805SLuis R. Rodriguez * descriptor has been corrupted. Force s/w to discard 820203c4805SLuis R. Rodriguez * this descriptor and continue... 821203c4805SLuis R. Rodriguez */ 822203c4805SLuis R. Rodriguez 823203c4805SLuis R. Rodriguez tds = tbf->bf_desc; 824203c4805SLuis R. Rodriguez retval = ath9k_hw_rxprocdesc(ah, tds, tbf->bf_daddr, 825203c4805SLuis R. Rodriguez PA2DESC(sc, tds->ds_link), 0); 826203c4805SLuis R. Rodriguez if (retval == -EINPROGRESS) { 827203c4805SLuis R. Rodriguez break; 828203c4805SLuis R. Rodriguez } 829203c4805SLuis R. Rodriguez } 830203c4805SLuis R. Rodriguez 831203c4805SLuis R. Rodriguez skb = bf->bf_mpdu; 832203c4805SLuis R. Rodriguez if (!skb) 833203c4805SLuis R. Rodriguez continue; 834203c4805SLuis R. Rodriguez 835203c4805SLuis R. Rodriguez /* 836203c4805SLuis R. Rodriguez * Synchronize the DMA transfer with CPU before 837203c4805SLuis R. Rodriguez * 1. accessing the frame 838203c4805SLuis R. Rodriguez * 2. requeueing the same buffer to h/w 839203c4805SLuis R. Rodriguez */ 840203c4805SLuis R. Rodriguez dma_sync_single_for_cpu(sc->dev, bf->bf_buf_addr, 841cc861f74SLuis R. Rodriguez common->rx_bufsize, 842203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 843203c4805SLuis R. Rodriguez 844b4afffc0SLuis R. Rodriguez hdr = (struct ieee80211_hdr *) skb->data; 8455ca42627SLuis R. Rodriguez rxs = IEEE80211_SKB_RXCB(skb); 8465ca42627SLuis R. Rodriguez 847b4afffc0SLuis R. Rodriguez hw = ath_get_virt_hw(sc, hdr); 84826ab2645SLuis R. Rodriguez rx_stats = &ds->ds_rxstat; 849b4afffc0SLuis R. Rodriguez 850203c4805SLuis R. Rodriguez /* 851203c4805SLuis R. Rodriguez * If we're asked to flush receive queue, directly 852203c4805SLuis R. Rodriguez * chain it back at the queue without processing it. 853203c4805SLuis R. Rodriguez */ 854203c4805SLuis R. Rodriguez if (flush) 855203c4805SLuis R. Rodriguez goto requeue; 856203c4805SLuis R. Rodriguez 8571e875e9fSLuis R. Rodriguez retval = ath9k_rx_skb_preprocess(common, hw, skb, rx_stats, 8581e875e9fSLuis R. Rodriguez rxs, &decrypt_error); 8591e875e9fSLuis R. Rodriguez if (retval) 860203c4805SLuis R. Rodriguez goto requeue; 861203c4805SLuis R. Rodriguez 862203c4805SLuis R. Rodriguez /* Ensure we always have an skb to requeue once we are done 863203c4805SLuis R. Rodriguez * processing the current buffer's skb */ 864cc861f74SLuis R. Rodriguez requeue_skb = ath_rxbuf_alloc(common, common->rx_bufsize, GFP_ATOMIC); 865203c4805SLuis R. Rodriguez 866203c4805SLuis R. Rodriguez /* If there is no memory we ignore the current RX'd frame, 867203c4805SLuis R. Rodriguez * tell hardware it can give us a new frame using the old 868203c4805SLuis R. Rodriguez * skb and put it at the tail of the sc->rx.rxbuf list for 869203c4805SLuis R. Rodriguez * processing. */ 870203c4805SLuis R. Rodriguez if (!requeue_skb) 871203c4805SLuis R. Rodriguez goto requeue; 872203c4805SLuis R. Rodriguez 873203c4805SLuis R. Rodriguez /* Unmap the frame */ 874203c4805SLuis R. Rodriguez dma_unmap_single(sc->dev, bf->bf_buf_addr, 875cc861f74SLuis R. Rodriguez common->rx_bufsize, 876203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 877203c4805SLuis R. Rodriguez 87826ab2645SLuis R. Rodriguez skb_put(skb, rx_stats->rs_datalen); 879203c4805SLuis R. Rodriguez 880*c9b14170SLuis R. Rodriguez ath9k_rx_skb_postprocess(common, skb, rx_stats, 881*c9b14170SLuis R. Rodriguez rxs, decrypt_error); 882203c4805SLuis R. Rodriguez 883203c4805SLuis R. Rodriguez /* We will now give hardware our shiny new allocated skb */ 884203c4805SLuis R. Rodriguez bf->bf_mpdu = requeue_skb; 885203c4805SLuis R. Rodriguez bf->bf_buf_addr = dma_map_single(sc->dev, requeue_skb->data, 886cc861f74SLuis R. Rodriguez common->rx_bufsize, 887203c4805SLuis R. Rodriguez DMA_FROM_DEVICE); 888203c4805SLuis R. Rodriguez if (unlikely(dma_mapping_error(sc->dev, 889203c4805SLuis R. Rodriguez bf->bf_buf_addr))) { 890203c4805SLuis R. Rodriguez dev_kfree_skb_any(requeue_skb); 891203c4805SLuis R. Rodriguez bf->bf_mpdu = NULL; 892c46917bbSLuis R. Rodriguez ath_print(common, ATH_DBG_FATAL, 893203c4805SLuis R. Rodriguez "dma_mapping_error() on RX\n"); 8945ca42627SLuis R. Rodriguez ath_rx_send_to_mac80211(hw, sc, skb, rxs); 895203c4805SLuis R. Rodriguez break; 896203c4805SLuis R. Rodriguez } 897203c4805SLuis R. Rodriguez bf->bf_dmacontext = bf->bf_buf_addr; 898203c4805SLuis R. Rodriguez 899203c4805SLuis R. Rodriguez /* 900203c4805SLuis R. Rodriguez * change the default rx antenna if rx diversity chooses the 901203c4805SLuis R. Rodriguez * other antenna 3 times in a row. 902203c4805SLuis R. Rodriguez */ 903203c4805SLuis R. Rodriguez if (sc->rx.defant != ds->ds_rxstat.rs_antenna) { 904203c4805SLuis R. Rodriguez if (++sc->rx.rxotherant >= 3) 90526ab2645SLuis R. Rodriguez ath_setdefantenna(sc, rx_stats->rs_antenna); 906203c4805SLuis R. Rodriguez } else { 907203c4805SLuis R. Rodriguez sc->rx.rxotherant = 0; 908203c4805SLuis R. Rodriguez } 909203c4805SLuis R. Rodriguez 9109a23f9caSJouni Malinen if (unlikely(sc->sc_flags & (SC_OP_WAIT_FOR_BEACON | 911f0e9a860SGabor Juhos SC_OP_WAIT_FOR_CAB | 9129a23f9caSJouni Malinen SC_OP_WAIT_FOR_PSPOLL_DATA))) 913cc65965cSJouni Malinen ath_rx_ps(sc, skb); 914cc65965cSJouni Malinen 9155ca42627SLuis R. Rodriguez ath_rx_send_to_mac80211(hw, sc, skb, rxs); 916cc65965cSJouni Malinen 917203c4805SLuis R. Rodriguez requeue: 918203c4805SLuis R. Rodriguez list_move_tail(&bf->list, &sc->rx.rxbuf); 919203c4805SLuis R. Rodriguez ath_rx_buf_link(sc, bf); 920203c4805SLuis R. Rodriguez } while (1); 921203c4805SLuis R. Rodriguez 922203c4805SLuis R. Rodriguez spin_unlock_bh(&sc->rx.rxbuflock); 923203c4805SLuis R. Rodriguez 924203c4805SLuis R. Rodriguez return 0; 925203c4805SLuis R. Rodriguez #undef PA2DESC 926203c4805SLuis R. Rodriguez } 927