2 * Copyright (c) 2009-2011 Atheros Communications Inc.
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * 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 /* Assumes you've already done the endian to CPU conversion */
31 bool ath9k_cmn_rx_accept(struct ath_common
*common
,
32 struct ieee80211_hdr
*hdr
,
33 struct ieee80211_rx_status
*rxs
,
34 struct ath_rx_status
*rx_stats
,
36 unsigned int rxfilter
)
38 struct ath_hw
*ah
= common
->ah
;
39 bool is_mc
, is_valid_tkip
, strip_mic
, mic_error
;
42 fc
= hdr
->frame_control
;
44 is_mc
= !!is_multicast_ether_addr(hdr
->addr1
);
45 is_valid_tkip
= rx_stats
->rs_keyix
!= ATH9K_RXKEYIX_INVALID
&&
46 test_bit(rx_stats
->rs_keyix
, common
->tkip_keymap
);
47 strip_mic
= is_valid_tkip
&& ieee80211_is_data(fc
) &&
48 ieee80211_has_protected(fc
) &&
49 !(rx_stats
->rs_status
&
50 (ATH9K_RXERR_DECRYPT
| ATH9K_RXERR_CRC
| ATH9K_RXERR_MIC
|
51 ATH9K_RXERR_KEYMISS
));
54 * Key miss events are only relevant for pairwise keys where the
55 * descriptor does contain a valid key index. This has been observed
56 * mostly with CCMP encryption.
58 if (rx_stats
->rs_keyix
== ATH9K_RXKEYIX_INVALID
||
59 !test_bit(rx_stats
->rs_keyix
, common
->ccmp_keymap
))
60 rx_stats
->rs_status
&= ~ATH9K_RXERR_KEYMISS
;
62 mic_error
= is_valid_tkip
&& !ieee80211_is_ctl(fc
) &&
63 !ieee80211_has_morefrags(fc
) &&
64 !(le16_to_cpu(hdr
->seq_ctrl
) & IEEE80211_SCTL_FRAG
) &&
65 (rx_stats
->rs_status
& ATH9K_RXERR_MIC
);
68 * The rx_stats->rs_status will not be set until the end of the
69 * chained descriptors so it can be ignored if rs_more is set. The
70 * rs_more will be false at the last element of the chained
73 if (rx_stats
->rs_status
!= 0) {
76 if (rx_stats
->rs_status
& ATH9K_RXERR_CRC
) {
77 rxs
->flag
|= RX_FLAG_FAILED_FCS_CRC
;
81 if ((rx_stats
->rs_status
& ATH9K_RXERR_DECRYPT
) ||
82 (!is_mc
&& (rx_stats
->rs_status
& ATH9K_RXERR_KEYMISS
))) {
83 *decrypt_error
= true;
89 * Reject error frames with the exception of
90 * decryption and MIC failures. For monitor mode,
91 * we also ignore the CRC error.
93 status_mask
= ATH9K_RXERR_DECRYPT
| ATH9K_RXERR_MIC
|
96 if (ah
->is_monitoring
&& (rxfilter
& FIF_FCSFAIL
))
97 status_mask
|= ATH9K_RXERR_CRC
;
99 if (rx_stats
->rs_status
& ~status_mask
)
104 * For unicast frames the MIC error bit can have false positives,
105 * so all MIC error reports need to be validated in software.
106 * False negatives are not common, so skip software verification
107 * if the hardware considers the MIC valid.
110 rxs
->flag
|= RX_FLAG_MMIC_STRIPPED
;
111 else if (is_mc
&& mic_error
)
112 rxs
->flag
|= RX_FLAG_MMIC_ERROR
;
116 EXPORT_SYMBOL(ath9k_cmn_rx_accept
);
118 void ath9k_cmn_rx_skb_postprocess(struct ath_common
*common
,
120 struct ath_rx_status
*rx_stats
,
121 struct ieee80211_rx_status
*rxs
,
124 struct ath_hw
*ah
= common
->ah
;
125 struct ieee80211_hdr
*hdr
;
126 int hdrlen
, padpos
, padsize
;
130 /* see if any padding is done by the hw and remove it */
131 hdr
= (struct ieee80211_hdr
*) skb
->data
;
132 hdrlen
= ieee80211_get_hdrlen_from_skb(skb
);
133 fc
= hdr
->frame_control
;
134 padpos
= ieee80211_hdrlen(fc
);
136 /* The MAC header is padded to have 32-bit boundary if the
137 * packet payload is non-zero. The general calculation for
138 * padsize would take into account odd header lengths:
139 * padsize = (4 - padpos % 4) % 4; However, since only
140 * even-length headers are used, padding can only be 0 or 2
141 * bytes and we can optimize this a bit. In addition, we must
142 * not try to remove padding from short control frames that do
143 * not have payload. */
144 padsize
= padpos
& 3;
145 if (padsize
&& skb
->len
>=padpos
+padsize
+FCS_LEN
) {
146 memmove(skb
->data
+ padsize
, skb
->data
, padpos
);
147 skb_pull(skb
, padsize
);
150 keyix
= rx_stats
->rs_keyix
;
152 if (!(keyix
== ATH9K_RXKEYIX_INVALID
) && !decrypt_error
&&
153 ieee80211_has_protected(fc
)) {
154 rxs
->flag
|= RX_FLAG_DECRYPTED
;
155 } else if (ieee80211_has_protected(fc
)
156 && !decrypt_error
&& skb
->len
>= hdrlen
+ 4) {
157 keyix
= skb
->data
[hdrlen
+ 3] >> 6;
159 if (test_bit(keyix
, common
->keymap
))
160 rxs
->flag
|= RX_FLAG_DECRYPTED
;
162 if (ah
->sw_mgmt_crypto_rx
&&
163 (rxs
->flag
& RX_FLAG_DECRYPTED
) &&
164 ieee80211_is_mgmt(fc
))
165 /* Use software decrypt for management frames. */
166 rxs
->flag
&= ~RX_FLAG_DECRYPTED
;
168 EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess
);
170 int ath9k_cmn_process_rate(struct ath_common
*common
,
171 struct ieee80211_hw
*hw
,
172 struct ath_rx_status
*rx_stats
,
173 struct ieee80211_rx_status
*rxs
)
175 struct ieee80211_supported_band
*sband
;
176 enum ieee80211_band band
;
178 struct ath_hw
*ah
= common
->ah
;
180 band
= ah
->curchan
->chan
->band
;
181 sband
= hw
->wiphy
->bands
[band
];
183 if (IS_CHAN_QUARTER_RATE(ah
->curchan
))
184 rxs
->flag
|= RX_FLAG_5MHZ
;
185 else if (IS_CHAN_HALF_RATE(ah
->curchan
))
186 rxs
->flag
|= RX_FLAG_10MHZ
;
188 if (rx_stats
->rs_rate
& 0x80) {
190 rxs
->flag
|= RX_FLAG_HT
;
191 rxs
->flag
|= rx_stats
->flag
;
192 rxs
->rate_idx
= rx_stats
->rs_rate
& 0x7f;
196 for (i
= 0; i
< sband
->n_bitrates
; i
++) {
197 if (sband
->bitrates
[i
].hw_value
== rx_stats
->rs_rate
) {
201 if (sband
->bitrates
[i
].hw_value_short
== rx_stats
->rs_rate
) {
202 rxs
->flag
|= RX_FLAG_SHORTPRE
;
210 EXPORT_SYMBOL(ath9k_cmn_process_rate
);
212 void ath9k_cmn_process_rssi(struct ath_common
*common
,
213 struct ieee80211_hw
*hw
,
214 struct ath_rx_status
*rx_stats
,
215 struct ieee80211_rx_status
*rxs
)
217 struct ath_hw
*ah
= common
->ah
;
219 int rssi
= rx_stats
->rs_rssi
;
223 * RSSI is not available for subframes in an A-MPDU.
225 if (rx_stats
->rs_moreaggr
) {
226 rxs
->flag
|= RX_FLAG_NO_SIGNAL_VAL
;
231 * Check if the RSSI for the last subframe in an A-MPDU
232 * or an unaggregated frame is valid.
234 if (rx_stats
->rs_rssi
== ATH9K_RSSI_BAD
) {
235 rxs
->flag
|= RX_FLAG_NO_SIGNAL_VAL
;
239 for (i
= 0, j
= 0; i
< ARRAY_SIZE(rx_stats
->rs_rssi_ctl
); i
++) {
242 if (!(ah
->rxchainmask
& BIT(i
)))
245 rssi
= rx_stats
->rs_rssi_ctl
[i
];
246 if (rssi
!= ATH9K_RSSI_BAD
) {
247 rxs
->chains
|= BIT(j
);
248 rxs
->chain_signal
[j
] = ah
->noise
+ rssi
;
254 * Update Beacon RSSI, this is used by ANI.
256 if (rx_stats
->is_mybeacon
&&
257 ((ah
->opmode
== NL80211_IFTYPE_STATION
) ||
258 (ah
->opmode
== NL80211_IFTYPE_ADHOC
))) {
259 ATH_RSSI_LPF(common
->last_rssi
, rx_stats
->rs_rssi
);
260 last_rssi
= common
->last_rssi
;
262 if (likely(last_rssi
!= ATH_RSSI_DUMMY_MARKER
))
263 rssi
= ATH_EP_RND(last_rssi
, ATH_RSSI_EP_MULTIPLIER
);
267 ah
->stats
.avgbrssi
= rssi
;
270 rxs
->signal
= ah
->noise
+ rx_stats
->rs_rssi
;
272 EXPORT_SYMBOL(ath9k_cmn_process_rssi
);
274 int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff
*skb
)
276 struct ieee80211_tx_info
*tx_info
= IEEE80211_SKB_CB(skb
);
278 if (tx_info
->control
.hw_key
) {
279 switch (tx_info
->control
.hw_key
->cipher
) {
280 case WLAN_CIPHER_SUITE_WEP40
:
281 case WLAN_CIPHER_SUITE_WEP104
:
282 return ATH9K_KEY_TYPE_WEP
;
283 case WLAN_CIPHER_SUITE_TKIP
:
284 return ATH9K_KEY_TYPE_TKIP
;
285 case WLAN_CIPHER_SUITE_CCMP
:
286 return ATH9K_KEY_TYPE_AES
;
292 return ATH9K_KEY_TYPE_CLEAR
;
294 EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype
);
297 * Update internal channel flags.
299 static void ath9k_cmn_update_ichannel(struct ath9k_channel
*ichan
,
300 struct cfg80211_chan_def
*chandef
)
302 struct ieee80211_channel
*chan
= chandef
->chan
;
305 ichan
->channel
= chan
->center_freq
;
308 if (chan
->band
== IEEE80211_BAND_5GHZ
)
309 flags
|= CHANNEL_5GHZ
;
311 switch (chandef
->width
) {
312 case NL80211_CHAN_WIDTH_5
:
313 flags
|= CHANNEL_QUARTER
;
315 case NL80211_CHAN_WIDTH_10
:
316 flags
|= CHANNEL_HALF
;
318 case NL80211_CHAN_WIDTH_20_NOHT
:
320 case NL80211_CHAN_WIDTH_20
:
323 case NL80211_CHAN_WIDTH_40
:
324 if (chandef
->center_freq1
> chandef
->chan
->center_freq
)
325 flags
|= CHANNEL_HT40PLUS
| CHANNEL_HT
;
327 flags
|= CHANNEL_HT40MINUS
| CHANNEL_HT
;
333 ichan
->channelFlags
= flags
;
337 * Get the internal channel reference.
339 struct ath9k_channel
*ath9k_cmn_get_channel(struct ieee80211_hw
*hw
,
341 struct cfg80211_chan_def
*chandef
)
343 struct ieee80211_channel
*curchan
= chandef
->chan
;
344 struct ath9k_channel
*channel
;
346 channel
= &ah
->channels
[curchan
->hw_value
];
347 ath9k_cmn_update_ichannel(channel
, chandef
);
351 EXPORT_SYMBOL(ath9k_cmn_get_channel
);
353 int ath9k_cmn_count_streams(unsigned int chainmask
, int max
)
358 if (++streams
== max
)
360 } while ((chainmask
= chainmask
& (chainmask
- 1)));
364 EXPORT_SYMBOL(ath9k_cmn_count_streams
);
366 void ath9k_cmn_update_txpow(struct ath_hw
*ah
, u16 cur_txpow
,
367 u16 new_txpow
, u16
*txpower
)
369 struct ath_regulatory
*reg
= ath9k_hw_regulatory(ah
);
371 if (reg
->power_limit
!= new_txpow
)
372 ath9k_hw_set_txpowerlimit(ah
, new_txpow
, false);
374 /* read back in case value is clamped */
375 *txpower
= reg
->max_power_level
;
377 EXPORT_SYMBOL(ath9k_cmn_update_txpow
);
379 void ath9k_cmn_init_crypto(struct ath_hw
*ah
)
381 struct ath_common
*common
= ath9k_hw_common(ah
);
384 /* Get the hardware key cache size. */
385 common
->keymax
= AR_KEYTABLE_SIZE
;
388 * Check whether the separate key cache entries
389 * are required to handle both tx+rx MIC keys.
390 * With split mic keys the number of stations is limited
391 * to 27 otherwise 59.
393 if (ah
->misc_mode
& AR_PCU_MIC_NEW_LOC_ENA
)
394 common
->crypt_caps
|= ATH_CRYPT_CAP_MIC_COMBINED
;
397 * Reset the key cache since some parts do not
398 * reset the contents on initial power up.
400 for (i
= 0; i
< common
->keymax
; i
++)
401 ath_hw_keyreset(common
, (u16
) i
);
403 EXPORT_SYMBOL(ath9k_cmn_init_crypto
);
405 static int __init
ath9k_cmn_init(void)
409 module_init(ath9k_cmn_init
);
411 static void __exit
ath9k_cmn_exit(void)
415 module_exit(ath9k_cmn_exit
);