blob: 80edf7a302cab3716b4e56d7720533e1a706d508 [file] [log] [blame]
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -08001/*
2 * Copyright (c) 2009 Atheros Communications Inc.
3 *
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.
7 *
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.
15 */
16
17/*
18 * Module for common driver code between ath9k and ath9k_htc
19 */
20
21#include <linux/kernel.h>
22#include <linux/module.h>
23
24#include "common.h"
25
26MODULE_AUTHOR("Atheros Communications");
27MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards.");
28MODULE_LICENSE("Dual BSD/GPL");
29
30/* Common RX processing */
31
32/* Assumes you've already done the endian to CPU conversion */
33static bool ath9k_rx_accept(struct ath_common *common,
34 struct sk_buff *skb,
35 struct ieee80211_rx_status *rxs,
36 struct ath_rx_status *rx_stats,
37 bool *decrypt_error)
38{
39 struct ath_hw *ah = common->ah;
40 struct ieee80211_hdr *hdr;
41 __le16 fc;
42
43 hdr = (struct ieee80211_hdr *) skb->data;
44 fc = hdr->frame_control;
45
46 if (!rx_stats->rs_datalen)
47 return false;
48 /*
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
51 * those frames.
52 */
53 if (rx_stats->rs_datalen > common->rx_bufsize)
54 return false;
55
56 if (rx_stats->rs_more) {
57 /*
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.
62 */
63 if (ah->opmode != NL80211_IFTYPE_MONITOR)
64 return false;
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)
69 return false;
70
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))
75 /*
76 * Sometimes, we get invalid
77 * MIC failures on valid control frames.
78 * Remove these mic errors.
79 */
80 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
81 else
82 rxs->flag |= RX_FLAG_MMIC_ERROR;
83 }
84 /*
85 * Reject error frames with the exception of
86 * decryption and MIC failures. For monitor mode,
87 * we also ignore the CRC error.
88 */
89 if (ah->opmode == NL80211_IFTYPE_MONITOR) {
90 if (rx_stats->rs_status &
91 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
92 ATH9K_RXERR_CRC))
93 return false;
94 } else {
95 if (rx_stats->rs_status &
96 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
97 return false;
98 }
99 }
100 }
101 return true;
102}
103
104static 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,
108 struct sk_buff *skb)
109{
110 struct ieee80211_supported_band *sband;
111 enum ieee80211_band band;
112 unsigned int i = 0;
113
114 band = hw->conf.channel->band;
115 sband = hw->wiphy->bands[band];
116
117 if (rx_stats->rs_rate & 0x80) {
118 /* HT rate */
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;
125 }
126
127 for (i = 0; i < sband->n_bitrates; i++) {
128 if (sband->bitrates[i].hw_value == rx_stats->rs_rate)
129 return i;
130 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
131 rxs->flag |= RX_FLAG_SHORTPRE;
132 return i;
133 }
134 }
135
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);
141
142 return 0;
143}
144
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -0800145static void ath9k_process_rssi(struct ath_common *common,
146 struct ieee80211_hw *hw,
147 struct sk_buff *skb,
148 struct ath_rx_status *rx_stats)
149{
150 struct ath_hw *ah = common->ah;
151 struct ieee80211_sta *sta;
152 struct ieee80211_hdr *hdr;
153 struct ath_node *an;
154 int last_rssi = ATH_RSSI_DUMMY_MARKER;
155 __le16 fc;
156
157 hdr = (struct ieee80211_hdr *)skb->data;
158 fc = hdr->frame_control;
159
160 rcu_read_lock();
161 /*
162 * XXX: use ieee80211_find_sta! This requires quite a bit of work
163 * under the current ath9k virtual wiphy implementation as we have
164 * no way of tying a vif to wiphy. Typically vifs are attached to
165 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
166 * wiphy you'd have to iterate over every wiphy and each sdata.
167 */
168 sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
169 if (sta) {
170 an = (struct ath_node *) sta->drv_priv;
171 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
172 !rx_stats->rs_moreaggr)
173 ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
174 last_rssi = an->last_rssi;
175 }
176 rcu_read_unlock();
177
178 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
179 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
180 ATH_RSSI_EP_MULTIPLIER);
181 if (rx_stats->rs_rssi < 0)
182 rx_stats->rs_rssi = 0;
183 else if (rx_stats->rs_rssi > 127)
184 rx_stats->rs_rssi = 127;
185
186 /* Update Beacon RSSI, this is used by ANI. */
187 if (ieee80211_is_beacon(fc))
188 ah->stats.avgbrssi = rx_stats->rs_rssi;
189}
190
191/*
192 * For Decrypt or Demic errors, we only mark packet status here and always push
193 * up the frame up to let mac80211 handle the actual error case, be it no
194 * decryption key or real decryption error. This let us keep statistics there.
195 */
196int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
197 struct ieee80211_hw *hw,
198 struct sk_buff *skb,
199 struct ath_rx_status *rx_stats,
200 struct ieee80211_rx_status *rx_status,
201 bool *decrypt_error)
202{
203 struct ath_hw *ah = common->ah;
204
205 if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
206 return -EINVAL;
207
208 ath9k_process_rssi(common, hw, skb, rx_stats);
209
210 rx_status->rate_idx = ath9k_process_rate(common, hw,
211 rx_stats, rx_status, skb);
212 rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
213 rx_status->band = hw->conf.channel->band;
214 rx_status->freq = hw->conf.channel->center_freq;
215 rx_status->noise = common->ani.noise_floor;
216 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
217 rx_status->antenna = rx_stats->rs_antenna;
Luis R. Rodriguezdb86f072009-11-05 08:44:39 -0800218 rx_status->flag |= RX_FLAG_TSFT;
219
220 return 0;
221}
222EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
223
224void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
225 struct sk_buff *skb,
226 struct ath_rx_status *rx_stats,
227 struct ieee80211_rx_status *rxs,
228 bool decrypt_error)
229{
230 struct ath_hw *ah = common->ah;
231 struct ieee80211_hdr *hdr;
232 int hdrlen, padsize;
233 u8 keyix;
234 __le16 fc;
235
236 /* see if any padding is done by the hw and remove it */
237 hdr = (struct ieee80211_hdr *) skb->data;
238 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
239 fc = hdr->frame_control;
240
241 /* The MAC header is padded to have 32-bit boundary if the
242 * packet payload is non-zero. The general calculation for
243 * padsize would take into account odd header lengths:
244 * padsize = (4 - hdrlen % 4) % 4; However, since only
245 * even-length headers are used, padding can only be 0 or 2
246 * bytes and we can optimize this a bit. In addition, we must
247 * not try to remove padding from short control frames that do
248 * not have payload. */
249 padsize = hdrlen & 3;
250 if (padsize && hdrlen >= 24) {
251 memmove(skb->data + padsize, skb->data, hdrlen);
252 skb_pull(skb, padsize);
253 }
254
255 keyix = rx_stats->rs_keyix;
256
257 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
258 rxs->flag |= RX_FLAG_DECRYPTED;
259 } else if (ieee80211_has_protected(fc)
260 && !decrypt_error && skb->len >= hdrlen + 4) {
261 keyix = skb->data[hdrlen + 3] >> 6;
262
263 if (test_bit(keyix, common->keymap))
264 rxs->flag |= RX_FLAG_DECRYPTED;
265 }
266 if (ah->sw_mgmt_crypto &&
267 (rxs->flag & RX_FLAG_DECRYPTED) &&
268 ieee80211_is_mgmt(fc))
269 /* Use software decrypt for management frames. */
270 rxs->flag &= ~RX_FLAG_DECRYPTED;
271}
272EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
273
274static int __init ath9k_cmn_init(void)
275{
276 return 0;
277}
278module_init(ath9k_cmn_init);
279
280static void __exit ath9k_cmn_exit(void)
281{
282 return;
283}
284module_exit(ath9k_cmn_exit);