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)
57 * rs_more indicates chained descriptors which can be used
58 * to link buffers together for a sort of scatter-gather
60 * reject the frame, we don't support scatter-gather yet and
61 * the frame is probably corrupt anyway
63 if (rx_stats->rs_more)
67 * The rx_stats->rs_status will not be set until the end of the
68 * chained descriptors so it can be ignored if rs_more is set. The
69 * rs_more will be false at the last element of the chained
72 if (rx_stats->rs_status != 0) {
73 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
74 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
75 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
78 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
79 *decrypt_error = true;
80 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
81 if (ieee80211_is_ctl(fc))
83 * Sometimes, we get invalid
84 * MIC failures on valid control frames.
85 * Remove these mic errors.
87 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
89 rxs->flag |= RX_FLAG_MMIC_ERROR;
92 * Reject error frames with the exception of
93 * decryption and MIC failures. For monitor mode,
94 * we also ignore the CRC error.
96 if (ah->opmode == NL80211_IFTYPE_MONITOR) {
97 if (rx_stats->rs_status &
98 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
102 if (rx_stats->rs_status &
103 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
111 static int ath9k_process_rate(struct ath_common *common,
112 struct ieee80211_hw *hw,
113 struct ath_rx_status *rx_stats,
114 struct ieee80211_rx_status *rxs,
117 struct ieee80211_supported_band *sband;
118 enum ieee80211_band band;
121 band = hw->conf.channel->band;
122 sband = hw->wiphy->bands[band];
124 if (rx_stats->rs_rate & 0x80) {
126 rxs->flag |= RX_FLAG_HT;
127 if (rx_stats->rs_flags & ATH9K_RX_2040)
128 rxs->flag |= RX_FLAG_40MHZ;
129 if (rx_stats->rs_flags & ATH9K_RX_GI)
130 rxs->flag |= RX_FLAG_SHORT_GI;
131 rxs->rate_idx = rx_stats->rs_rate & 0x7f;
135 for (i = 0; i < sband->n_bitrates; i++) {
136 if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
140 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
141 rxs->flag |= RX_FLAG_SHORTPRE;
148 * No valid hardware bitrate found -- we should not get here
149 * because hardware has already validated this frame as OK.
151 ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
152 "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
153 if ((common->debug_mask & ATH_DBG_XMIT))
154 print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
159 static void ath9k_process_rssi(struct ath_common *common,
160 struct ieee80211_hw *hw,
162 struct ath_rx_status *rx_stats)
164 struct ath_hw *ah = common->ah;
165 struct ieee80211_sta *sta;
166 struct ieee80211_hdr *hdr;
168 int last_rssi = ATH_RSSI_DUMMY_MARKER;
171 hdr = (struct ieee80211_hdr *)skb->data;
172 fc = hdr->frame_control;
176 * XXX: use ieee80211_find_sta! This requires quite a bit of work
177 * under the current ath9k virtual wiphy implementation as we have
178 * no way of tying a vif to wiphy. Typically vifs are attached to
179 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
180 * wiphy you'd have to iterate over every wiphy and each sdata.
182 sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
184 an = (struct ath_node *) sta->drv_priv;
185 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
186 !rx_stats->rs_moreaggr)
187 ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
188 last_rssi = an->last_rssi;
192 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
193 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
194 ATH_RSSI_EP_MULTIPLIER);
195 if (rx_stats->rs_rssi < 0)
196 rx_stats->rs_rssi = 0;
198 /* Update Beacon RSSI, this is used by ANI. */
199 if (ieee80211_is_beacon(fc))
200 ah->stats.avgbrssi = rx_stats->rs_rssi;
204 * For Decrypt or Demic errors, we only mark packet status here and always push
205 * up the frame up to let mac80211 handle the actual error case, be it no
206 * decryption key or real decryption error. This let us keep statistics there.
208 int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
209 struct ieee80211_hw *hw,
211 struct ath_rx_status *rx_stats,
212 struct ieee80211_rx_status *rx_status,
215 struct ath_hw *ah = common->ah;
217 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
220 * everything but the rate is checked here, the rate check is done
221 * separately to avoid doing two lookups for a rate for each frame.
223 if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
226 ath9k_process_rssi(common, hw, skb, rx_stats);
228 if (ath9k_process_rate(common, hw, rx_stats, rx_status, skb))
231 rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
232 rx_status->band = hw->conf.channel->band;
233 rx_status->freq = hw->conf.channel->center_freq;
234 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
235 rx_status->antenna = rx_stats->rs_antenna;
236 rx_status->flag |= RX_FLAG_TSFT;
240 EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
242 void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
244 struct ath_rx_status *rx_stats,
245 struct ieee80211_rx_status *rxs,
248 struct ath_hw *ah = common->ah;
249 struct ieee80211_hdr *hdr;
250 int hdrlen, padpos, padsize;
254 /* see if any padding is done by the hw and remove it */
255 hdr = (struct ieee80211_hdr *) skb->data;
256 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
257 fc = hdr->frame_control;
258 padpos = ath9k_cmn_padpos(hdr->frame_control);
260 /* The MAC header is padded to have 32-bit boundary if the
261 * packet payload is non-zero. The general calculation for
262 * padsize would take into account odd header lengths:
263 * padsize = (4 - padpos % 4) % 4; However, since only
264 * even-length headers are used, padding can only be 0 or 2
265 * bytes and we can optimize this a bit. In addition, we must
266 * not try to remove padding from short control frames that do
267 * not have payload. */
268 padsize = padpos & 3;
269 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
270 memmove(skb->data + padsize, skb->data, padpos);
271 skb_pull(skb, padsize);
274 keyix = rx_stats->rs_keyix;
276 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
277 ieee80211_has_protected(fc)) {
278 rxs->flag |= RX_FLAG_DECRYPTED;
279 } else if (ieee80211_has_protected(fc)
280 && !decrypt_error && skb->len >= hdrlen + 4) {
281 keyix = skb->data[hdrlen + 3] >> 6;
283 if (test_bit(keyix, common->keymap))
284 rxs->flag |= RX_FLAG_DECRYPTED;
286 if (ah->sw_mgmt_crypto &&
287 (rxs->flag & RX_FLAG_DECRYPTED) &&
288 ieee80211_is_mgmt(fc))
289 /* Use software decrypt for management frames. */
290 rxs->flag &= ~RX_FLAG_DECRYPTED;
292 EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
294 int ath9k_cmn_padpos(__le16 frame_control)
297 if (ieee80211_has_a4(frame_control)) {
300 if (ieee80211_is_data_qos(frame_control)) {
301 padpos += IEEE80211_QOS_CTL_LEN;
306 EXPORT_SYMBOL(ath9k_cmn_padpos);
308 int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
310 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
312 if (tx_info->control.hw_key) {
313 if (tx_info->control.hw_key->alg == ALG_WEP)
314 return ATH9K_KEY_TYPE_WEP;
315 else if (tx_info->control.hw_key->alg == ALG_TKIP)
316 return ATH9K_KEY_TYPE_TKIP;
317 else if (tx_info->control.hw_key->alg == ALG_CCMP)
318 return ATH9K_KEY_TYPE_AES;
321 return ATH9K_KEY_TYPE_CLEAR;
323 EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
325 static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
326 enum nl80211_channel_type channel_type)
330 switch (chan->band) {
331 case IEEE80211_BAND_2GHZ:
332 switch (channel_type) {
333 case NL80211_CHAN_NO_HT:
334 case NL80211_CHAN_HT20:
335 chanmode = CHANNEL_G_HT20;
337 case NL80211_CHAN_HT40PLUS:
338 chanmode = CHANNEL_G_HT40PLUS;
340 case NL80211_CHAN_HT40MINUS:
341 chanmode = CHANNEL_G_HT40MINUS;
345 case IEEE80211_BAND_5GHZ:
346 switch (channel_type) {
347 case NL80211_CHAN_NO_HT:
348 case NL80211_CHAN_HT20:
349 chanmode = CHANNEL_A_HT20;
351 case NL80211_CHAN_HT40PLUS:
352 chanmode = CHANNEL_A_HT40PLUS;
354 case NL80211_CHAN_HT40MINUS:
355 chanmode = CHANNEL_A_HT40MINUS;
367 * Update internal channel flags.
369 void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
370 struct ath9k_channel *ichan)
372 struct ieee80211_channel *chan = hw->conf.channel;
373 struct ieee80211_conf *conf = &hw->conf;
375 ichan->channel = chan->center_freq;
378 if (chan->band == IEEE80211_BAND_2GHZ) {
379 ichan->chanmode = CHANNEL_G;
380 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
382 ichan->chanmode = CHANNEL_A;
383 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
386 if (conf_is_ht(conf))
387 ichan->chanmode = ath9k_get_extchanmode(chan,
390 EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
393 * Get the internal channel reference.
395 struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
398 struct ieee80211_channel *curchan = hw->conf.channel;
399 struct ath9k_channel *channel;
402 chan_idx = curchan->hw_value;
403 channel = &ah->channels[chan_idx];
404 ath9k_cmn_update_ichannel(hw, channel);
408 EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
410 static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
411 struct ath9k_keyval *hk, const u8 *addr,
414 struct ath_hw *ah = common->ah;
418 key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
419 key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
423 * Group key installation - only two key cache entries are used
424 * regardless of splitmic capability since group key is only
425 * used either for TX or RX.
428 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
429 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
431 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
432 memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
434 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
436 if (!common->splitmic) {
437 /* TX and RX keys share the same key cache entry. */
438 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
439 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
440 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
443 /* Separate key cache entries for TX and RX */
445 /* TX key goes at first index, RX key at +32. */
446 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
447 if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
448 /* TX MIC entry failed. No need to proceed further */
449 ath_print(common, ATH_DBG_FATAL,
450 "Setting TX MIC Key Failed\n");
454 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
455 /* XXX delete tx key on failure? */
456 return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
459 static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
463 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
464 if (test_bit(i, common->keymap) ||
465 test_bit(i + 64, common->keymap))
466 continue; /* At least one part of TKIP key allocated */
467 if (common->splitmic &&
468 (test_bit(i + 32, common->keymap) ||
469 test_bit(i + 64 + 32, common->keymap)))
470 continue; /* At least one part of TKIP key allocated */
472 /* Found a free slot for a TKIP key */
478 static int ath_reserve_key_cache_slot(struct ath_common *common)
482 /* First, try to find slots that would not be available for TKIP. */
483 if (common->splitmic) {
484 for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
485 if (!test_bit(i, common->keymap) &&
486 (test_bit(i + 32, common->keymap) ||
487 test_bit(i + 64, common->keymap) ||
488 test_bit(i + 64 + 32, common->keymap)))
490 if (!test_bit(i + 32, common->keymap) &&
491 (test_bit(i, common->keymap) ||
492 test_bit(i + 64, common->keymap) ||
493 test_bit(i + 64 + 32, common->keymap)))
495 if (!test_bit(i + 64, common->keymap) &&
496 (test_bit(i , common->keymap) ||
497 test_bit(i + 32, common->keymap) ||
498 test_bit(i + 64 + 32, common->keymap)))
500 if (!test_bit(i + 64 + 32, common->keymap) &&
501 (test_bit(i, common->keymap) ||
502 test_bit(i + 32, common->keymap) ||
503 test_bit(i + 64, common->keymap)))
507 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
508 if (!test_bit(i, common->keymap) &&
509 test_bit(i + 64, common->keymap))
511 if (test_bit(i, common->keymap) &&
512 !test_bit(i + 64, common->keymap))
517 /* No partially used TKIP slots, pick any available slot */
518 for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
519 /* Do not allow slots that could be needed for TKIP group keys
520 * to be used. This limitation could be removed if we know that
521 * TKIP will not be used. */
522 if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
524 if (common->splitmic) {
525 if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
527 if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
531 if (!test_bit(i, common->keymap))
532 return i; /* Found a free slot for a key */
535 /* No free slot found */
540 * Configure encryption in the HW.
542 int ath9k_cmn_key_config(struct ath_common *common,
543 struct ieee80211_vif *vif,
544 struct ieee80211_sta *sta,
545 struct ieee80211_key_conf *key)
547 struct ath_hw *ah = common->ah;
548 struct ath9k_keyval hk;
549 const u8 *mac = NULL;
553 memset(&hk, 0, sizeof(hk));
557 hk.kv_type = ATH9K_CIPHER_WEP;
560 hk.kv_type = ATH9K_CIPHER_TKIP;
563 hk.kv_type = ATH9K_CIPHER_AES_CCM;
569 hk.kv_len = key->keylen;
570 memcpy(hk.kv_val, key->key, key->keylen);
572 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
573 /* For now, use the default keys for broadcast keys. This may
574 * need to change with virtual interfaces. */
576 } else if (key->keyidx) {
581 if (vif->type != NL80211_IFTYPE_AP) {
582 /* Only keyidx 0 should be used with unicast key, but
583 * allow this for client mode for now. */
592 if (key->alg == ALG_TKIP)
593 idx = ath_reserve_key_cache_slot_tkip(common);
595 idx = ath_reserve_key_cache_slot(common);
597 return -ENOSPC; /* no free key cache entries */
600 if (key->alg == ALG_TKIP)
601 ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
602 vif->type == NL80211_IFTYPE_AP);
604 ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
609 set_bit(idx, common->keymap);
610 if (key->alg == ALG_TKIP) {
611 set_bit(idx + 64, common->keymap);
612 if (common->splitmic) {
613 set_bit(idx + 32, common->keymap);
614 set_bit(idx + 64 + 32, common->keymap);
620 EXPORT_SYMBOL(ath9k_cmn_key_config);
625 void ath9k_cmn_key_delete(struct ath_common *common,
626 struct ieee80211_key_conf *key)
628 struct ath_hw *ah = common->ah;
630 ath9k_hw_keyreset(ah, key->hw_key_idx);
631 if (key->hw_key_idx < IEEE80211_WEP_NKID)
634 clear_bit(key->hw_key_idx, common->keymap);
635 if (key->alg != ALG_TKIP)
638 clear_bit(key->hw_key_idx + 64, common->keymap);
639 if (common->splitmic) {
640 ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
641 clear_bit(key->hw_key_idx + 32, common->keymap);
642 clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
645 EXPORT_SYMBOL(ath9k_cmn_key_delete);
647 static int __init ath9k_cmn_init(void)
651 module_init(ath9k_cmn_init);
653 static void __exit ath9k_cmn_exit(void)
657 module_exit(ath9k_cmn_exit);