2 * Copyright (c) 2009 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Module for common driver code between ath9k and ath9k_htc
21 #include <linux/kernel.h>
22 #include <linux/module.h>
26 MODULE_AUTHOR("Atheros Communications");
27 MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards.");
28 MODULE_LICENSE("Dual BSD/GPL");
30 /* Common RX processing */
32 /* Assumes you've already done the endian to CPU conversion */
33 static bool ath9k_rx_accept(struct ath_common *common,
35 struct ieee80211_rx_status *rxs,
36 struct ath_rx_status *rx_stats,
39 struct ath_hw *ah = common->ah;
40 struct ieee80211_hdr *hdr;
43 hdr = (struct ieee80211_hdr *) skb->data;
44 fc = hdr->frame_control;
46 if (!rx_stats->rs_datalen)
49 * rs_status follows rs_datalen so if rs_datalen is too large
50 * we can take a hint that hardware corrupted it, so ignore
53 if (rx_stats->rs_datalen > common->rx_bufsize)
56 if (rx_stats->rs_more) {
58 * Frame spans multiple descriptors; this cannot happen yet
59 * as we don't support jumbograms. If not in monitor mode,
60 * discard the frame. Enable this if you want to see
61 * error frames in Monitor mode.
63 if (ah->opmode != NL80211_IFTYPE_MONITOR)
65 } else if (rx_stats->rs_status != 0) {
66 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
67 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
68 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
71 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
72 *decrypt_error = true;
73 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
74 if (ieee80211_is_ctl(fc))
76 * Sometimes, we get invalid
77 * MIC failures on valid control frames.
78 * Remove these mic errors.
80 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
82 rxs->flag |= RX_FLAG_MMIC_ERROR;
85 * Reject error frames with the exception of
86 * decryption and MIC failures. For monitor mode,
87 * we also ignore the CRC error.
89 if (ah->opmode == NL80211_IFTYPE_MONITOR) {
90 if (rx_stats->rs_status &
91 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
95 if (rx_stats->rs_status &
96 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
104 static u8 ath9k_process_rate(struct ath_common *common,
105 struct ieee80211_hw *hw,
106 struct ath_rx_status *rx_stats,
107 struct ieee80211_rx_status *rxs,
110 struct ieee80211_supported_band *sband;
111 enum ieee80211_band band;
114 band = hw->conf.channel->band;
115 sband = hw->wiphy->bands[band];
117 if (rx_stats->rs_rate & 0x80) {
119 rxs->flag |= RX_FLAG_HT;
120 if (rx_stats->rs_flags & ATH9K_RX_2040)
121 rxs->flag |= RX_FLAG_40MHZ;
122 if (rx_stats->rs_flags & ATH9K_RX_GI)
123 rxs->flag |= RX_FLAG_SHORT_GI;
124 return rx_stats->rs_rate & 0x7f;
127 for (i = 0; i < sband->n_bitrates; i++) {
128 if (sband->bitrates[i].hw_value == rx_stats->rs_rate)
130 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
131 rxs->flag |= RX_FLAG_SHORTPRE;
136 /* No valid hardware bitrate found -- we should not get here */
137 ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
138 "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
139 if ((common->debug_mask & ATH_DBG_XMIT))
140 print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
146 * Theory for reporting quality:
148 * At a hardware RSSI of 45 you will be able to use MCS 7 reliably.
149 * At a hardware RSSI of 45 you will be able to use MCS 15 reliably.
150 * At a hardware RSSI of 35 you should be able use 54 Mbps reliably.
152 * MCS 7 is the highets MCS index usable by a 1-stream device.
153 * MCS 15 is the highest MCS index usable by a 2-stream device.
155 * All ath9k devices are either 1-stream or 2-stream.
157 * How many bars you see is derived from the qual reporting.
159 * A more elaborate scheme can be used here but it requires tables
160 * of SNR/throughput for each possible mode used. For the MCS table
161 * you can refer to the wireless wiki:
163 * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
166 static int ath9k_compute_qual(struct ieee80211_hw *hw,
167 struct ath_rx_status *rx_stats)
171 if (conf_is_ht(&hw->conf))
172 qual = rx_stats->rs_rssi * 100 / 45;
174 qual = rx_stats->rs_rssi * 100 / 35;
177 * rssi can be more than 45 though, anything above that
178 * should be considered at 100%
186 static void ath9k_process_rssi(struct ath_common *common,
187 struct ieee80211_hw *hw,
189 struct ath_rx_status *rx_stats)
191 struct ath_hw *ah = common->ah;
192 struct ieee80211_sta *sta;
193 struct ieee80211_hdr *hdr;
195 int last_rssi = ATH_RSSI_DUMMY_MARKER;
198 hdr = (struct ieee80211_hdr *)skb->data;
199 fc = hdr->frame_control;
203 * XXX: use ieee80211_find_sta! This requires quite a bit of work
204 * under the current ath9k virtual wiphy implementation as we have
205 * no way of tying a vif to wiphy. Typically vifs are attached to
206 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
207 * wiphy you'd have to iterate over every wiphy and each sdata.
209 sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
211 an = (struct ath_node *) sta->drv_priv;
212 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
213 !rx_stats->rs_moreaggr)
214 ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
215 last_rssi = an->last_rssi;
219 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
220 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
221 ATH_RSSI_EP_MULTIPLIER);
222 if (rx_stats->rs_rssi < 0)
223 rx_stats->rs_rssi = 0;
224 else if (rx_stats->rs_rssi > 127)
225 rx_stats->rs_rssi = 127;
227 /* Update Beacon RSSI, this is used by ANI. */
228 if (ieee80211_is_beacon(fc))
229 ah->stats.avgbrssi = rx_stats->rs_rssi;
233 * For Decrypt or Demic errors, we only mark packet status here and always push
234 * up the frame up to let mac80211 handle the actual error case, be it no
235 * decryption key or real decryption error. This let us keep statistics there.
237 int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
238 struct ieee80211_hw *hw,
240 struct ath_rx_status *rx_stats,
241 struct ieee80211_rx_status *rx_status,
244 struct ath_hw *ah = common->ah;
246 if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
249 ath9k_process_rssi(common, hw, skb, rx_stats);
251 rx_status->rate_idx = ath9k_process_rate(common, hw,
252 rx_stats, rx_status, skb);
253 rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
254 rx_status->band = hw->conf.channel->band;
255 rx_status->freq = hw->conf.channel->center_freq;
256 rx_status->noise = common->ani.noise_floor;
257 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
258 rx_status->antenna = rx_stats->rs_antenna;
259 rx_status->qual = ath9k_compute_qual(hw, rx_stats);
260 rx_status->flag |= RX_FLAG_TSFT;
264 EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
266 void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
268 struct ath_rx_status *rx_stats,
269 struct ieee80211_rx_status *rxs,
272 struct ath_hw *ah = common->ah;
273 struct ieee80211_hdr *hdr;
278 /* see if any padding is done by the hw and remove it */
279 hdr = (struct ieee80211_hdr *) skb->data;
280 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
281 fc = hdr->frame_control;
283 /* The MAC header is padded to have 32-bit boundary if the
284 * packet payload is non-zero. The general calculation for
285 * padsize would take into account odd header lengths:
286 * padsize = (4 - hdrlen % 4) % 4; However, since only
287 * even-length headers are used, padding can only be 0 or 2
288 * bytes and we can optimize this a bit. In addition, we must
289 * not try to remove padding from short control frames that do
290 * not have payload. */
291 padsize = hdrlen & 3;
292 if (padsize && hdrlen >= 24) {
293 memmove(skb->data + padsize, skb->data, hdrlen);
294 skb_pull(skb, padsize);
297 keyix = rx_stats->rs_keyix;
299 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
300 rxs->flag |= RX_FLAG_DECRYPTED;
301 } else if (ieee80211_has_protected(fc)
302 && !decrypt_error && skb->len >= hdrlen + 4) {
303 keyix = skb->data[hdrlen + 3] >> 6;
305 if (test_bit(keyix, common->keymap))
306 rxs->flag |= RX_FLAG_DECRYPTED;
308 if (ah->sw_mgmt_crypto &&
309 (rxs->flag & RX_FLAG_DECRYPTED) &&
310 ieee80211_is_mgmt(fc))
311 /* Use software decrypt for management frames. */
312 rxs->flag &= ~RX_FLAG_DECRYPTED;
314 EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
316 static int __init ath9k_cmn_init(void)
320 module_init(ath9k_cmn_init);
322 static void __exit ath9k_cmn_exit(void)
326 module_exit(ath9k_cmn_exit);