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);
160 static void ath9k_process_rssi(struct ath_common *common,
161 struct ieee80211_hw *hw,
163 struct ath_rx_status *rx_stats)
165 struct ath_hw *ah = common->ah;
166 struct ieee80211_sta *sta;
167 struct ieee80211_hdr *hdr;
169 int last_rssi = ATH_RSSI_DUMMY_MARKER;
172 hdr = (struct ieee80211_hdr *)skb->data;
173 fc = hdr->frame_control;
177 * XXX: use ieee80211_find_sta! This requires quite a bit of work
178 * under the current ath9k virtual wiphy implementation as we have
179 * no way of tying a vif to wiphy. Typically vifs are attached to
180 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
181 * wiphy you'd have to iterate over every wiphy and each sdata.
183 sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
185 an = (struct ath_node *) sta->drv_priv;
186 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
187 !rx_stats->rs_moreaggr)
188 ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
189 last_rssi = an->last_rssi;
193 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
194 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
195 ATH_RSSI_EP_MULTIPLIER);
196 if (rx_stats->rs_rssi < 0)
197 rx_stats->rs_rssi = 0;
199 /* Update Beacon RSSI, this is used by ANI. */
200 if (ieee80211_is_beacon(fc))
201 ah->stats.avgbrssi = rx_stats->rs_rssi;
205 * For Decrypt or Demic errors, we only mark packet status here and always push
206 * up the frame up to let mac80211 handle the actual error case, be it no
207 * decryption key or real decryption error. This let us keep statistics there.
209 int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
210 struct ieee80211_hw *hw,
212 struct ath_rx_status *rx_stats,
213 struct ieee80211_rx_status *rx_status,
216 struct ath_hw *ah = common->ah;
218 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
221 * everything but the rate is checked here, the rate check is done
222 * separately to avoid doing two lookups for a rate for each frame.
224 if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
227 ath9k_process_rssi(common, hw, skb, rx_stats);
229 if (ath9k_process_rate(common, hw, rx_stats, rx_status, skb))
232 rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
233 rx_status->band = hw->conf.channel->band;
234 rx_status->freq = hw->conf.channel->center_freq;
235 rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
236 rx_status->antenna = rx_stats->rs_antenna;
237 rx_status->flag |= RX_FLAG_TSFT;
241 EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
243 void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
245 struct ath_rx_status *rx_stats,
246 struct ieee80211_rx_status *rxs,
249 struct ath_hw *ah = common->ah;
250 struct ieee80211_hdr *hdr;
251 int hdrlen, padpos, padsize;
255 /* see if any padding is done by the hw and remove it */
256 hdr = (struct ieee80211_hdr *) skb->data;
257 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
258 fc = hdr->frame_control;
259 padpos = ath9k_cmn_padpos(hdr->frame_control);
261 /* The MAC header is padded to have 32-bit boundary if the
262 * packet payload is non-zero. The general calculation for
263 * padsize would take into account odd header lengths:
264 * padsize = (4 - padpos % 4) % 4; However, since only
265 * even-length headers are used, padding can only be 0 or 2
266 * bytes and we can optimize this a bit. In addition, we must
267 * not try to remove padding from short control frames that do
268 * not have payload. */
269 padsize = padpos & 3;
270 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
271 memmove(skb->data + padsize, skb->data, padpos);
272 skb_pull(skb, padsize);
275 keyix = rx_stats->rs_keyix;
277 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
278 ieee80211_has_protected(fc)) {
279 rxs->flag |= RX_FLAG_DECRYPTED;
280 } else if (ieee80211_has_protected(fc)
281 && !decrypt_error && skb->len >= hdrlen + 4) {
282 keyix = skb->data[hdrlen + 3] >> 6;
284 if (test_bit(keyix, common->keymap))
285 rxs->flag |= RX_FLAG_DECRYPTED;
287 if (ah->sw_mgmt_crypto &&
288 (rxs->flag & RX_FLAG_DECRYPTED) &&
289 ieee80211_is_mgmt(fc))
290 /* Use software decrypt for management frames. */
291 rxs->flag &= ~RX_FLAG_DECRYPTED;
293 EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
295 int ath9k_cmn_padpos(__le16 frame_control)
298 if (ieee80211_has_a4(frame_control)) {
301 if (ieee80211_is_data_qos(frame_control)) {
302 padpos += IEEE80211_QOS_CTL_LEN;
307 EXPORT_SYMBOL(ath9k_cmn_padpos);
309 int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
311 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
313 if (tx_info->control.hw_key) {
314 if (tx_info->control.hw_key->alg == ALG_WEP)
315 return ATH9K_KEY_TYPE_WEP;
316 else if (tx_info->control.hw_key->alg == ALG_TKIP)
317 return ATH9K_KEY_TYPE_TKIP;
318 else if (tx_info->control.hw_key->alg == ALG_CCMP)
319 return ATH9K_KEY_TYPE_AES;
322 return ATH9K_KEY_TYPE_CLEAR;
324 EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
326 static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
327 enum nl80211_channel_type channel_type)
331 switch (chan->band) {
332 case IEEE80211_BAND_2GHZ:
333 switch (channel_type) {
334 case NL80211_CHAN_NO_HT:
335 case NL80211_CHAN_HT20:
336 chanmode = CHANNEL_G_HT20;
338 case NL80211_CHAN_HT40PLUS:
339 chanmode = CHANNEL_G_HT40PLUS;
341 case NL80211_CHAN_HT40MINUS:
342 chanmode = CHANNEL_G_HT40MINUS;
346 case IEEE80211_BAND_5GHZ:
347 switch (channel_type) {
348 case NL80211_CHAN_NO_HT:
349 case NL80211_CHAN_HT20:
350 chanmode = CHANNEL_A_HT20;
352 case NL80211_CHAN_HT40PLUS:
353 chanmode = CHANNEL_A_HT40PLUS;
355 case NL80211_CHAN_HT40MINUS:
356 chanmode = CHANNEL_A_HT40MINUS;
368 * Update internal channel flags.
370 void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
371 struct ath9k_channel *ichan)
373 struct ieee80211_channel *chan = hw->conf.channel;
374 struct ieee80211_conf *conf = &hw->conf;
376 ichan->channel = chan->center_freq;
379 if (chan->band == IEEE80211_BAND_2GHZ) {
380 ichan->chanmode = CHANNEL_G;
381 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
383 ichan->chanmode = CHANNEL_A;
384 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
387 if (conf_is_ht(conf))
388 ichan->chanmode = ath9k_get_extchanmode(chan,
391 EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
394 * Get the internal channel reference.
396 struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
399 struct ieee80211_channel *curchan = hw->conf.channel;
400 struct ath9k_channel *channel;
403 chan_idx = curchan->hw_value;
404 channel = &ah->channels[chan_idx];
405 ath9k_cmn_update_ichannel(hw, channel);
409 EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
411 static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
412 struct ath9k_keyval *hk, const u8 *addr,
415 struct ath_hw *ah = common->ah;
419 key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
420 key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
424 * Group key installation - only two key cache entries are used
425 * regardless of splitmic capability since group key is only
426 * used either for TX or RX.
429 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
430 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
432 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
433 memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
435 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
437 if (!common->splitmic) {
438 /* TX and RX keys share the same key cache entry. */
439 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
440 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
441 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
444 /* Separate key cache entries for TX and RX */
446 /* TX key goes at first index, RX key at +32. */
447 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
448 if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
449 /* TX MIC entry failed. No need to proceed further */
450 ath_print(common, ATH_DBG_FATAL,
451 "Setting TX MIC Key Failed\n");
455 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
456 /* XXX delete tx key on failure? */
457 return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
460 static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
464 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
465 if (test_bit(i, common->keymap) ||
466 test_bit(i + 64, common->keymap))
467 continue; /* At least one part of TKIP key allocated */
468 if (common->splitmic &&
469 (test_bit(i + 32, common->keymap) ||
470 test_bit(i + 64 + 32, common->keymap)))
471 continue; /* At least one part of TKIP key allocated */
473 /* Found a free slot for a TKIP key */
479 static int ath_reserve_key_cache_slot(struct ath_common *common)
483 /* First, try to find slots that would not be available for TKIP. */
484 if (common->splitmic) {
485 for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
486 if (!test_bit(i, common->keymap) &&
487 (test_bit(i + 32, common->keymap) ||
488 test_bit(i + 64, common->keymap) ||
489 test_bit(i + 64 + 32, common->keymap)))
491 if (!test_bit(i + 32, common->keymap) &&
492 (test_bit(i, common->keymap) ||
493 test_bit(i + 64, common->keymap) ||
494 test_bit(i + 64 + 32, common->keymap)))
496 if (!test_bit(i + 64, common->keymap) &&
497 (test_bit(i , common->keymap) ||
498 test_bit(i + 32, common->keymap) ||
499 test_bit(i + 64 + 32, common->keymap)))
501 if (!test_bit(i + 64 + 32, common->keymap) &&
502 (test_bit(i, common->keymap) ||
503 test_bit(i + 32, common->keymap) ||
504 test_bit(i + 64, common->keymap)))
508 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
509 if (!test_bit(i, common->keymap) &&
510 test_bit(i + 64, common->keymap))
512 if (test_bit(i, common->keymap) &&
513 !test_bit(i + 64, common->keymap))
518 /* No partially used TKIP slots, pick any available slot */
519 for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
520 /* Do not allow slots that could be needed for TKIP group keys
521 * to be used. This limitation could be removed if we know that
522 * TKIP will not be used. */
523 if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
525 if (common->splitmic) {
526 if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
528 if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
532 if (!test_bit(i, common->keymap))
533 return i; /* Found a free slot for a key */
536 /* No free slot found */
541 * Configure encryption in the HW.
543 int ath9k_cmn_key_config(struct ath_common *common,
544 struct ieee80211_vif *vif,
545 struct ieee80211_sta *sta,
546 struct ieee80211_key_conf *key)
548 struct ath_hw *ah = common->ah;
549 struct ath9k_keyval hk;
550 const u8 *mac = NULL;
554 memset(&hk, 0, sizeof(hk));
558 hk.kv_type = ATH9K_CIPHER_WEP;
561 hk.kv_type = ATH9K_CIPHER_TKIP;
564 hk.kv_type = ATH9K_CIPHER_AES_CCM;
570 hk.kv_len = key->keylen;
571 memcpy(hk.kv_val, key->key, key->keylen);
573 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
574 /* For now, use the default keys for broadcast keys. This may
575 * need to change with virtual interfaces. */
577 } else if (key->keyidx) {
582 if (vif->type != NL80211_IFTYPE_AP) {
583 /* Only keyidx 0 should be used with unicast key, but
584 * allow this for client mode for now. */
593 if (key->alg == ALG_TKIP)
594 idx = ath_reserve_key_cache_slot_tkip(common);
596 idx = ath_reserve_key_cache_slot(common);
598 return -ENOSPC; /* no free key cache entries */
601 if (key->alg == ALG_TKIP)
602 ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
603 vif->type == NL80211_IFTYPE_AP);
605 ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
610 set_bit(idx, common->keymap);
611 if (key->alg == ALG_TKIP) {
612 set_bit(idx + 64, common->keymap);
613 if (common->splitmic) {
614 set_bit(idx + 32, common->keymap);
615 set_bit(idx + 64 + 32, common->keymap);
621 EXPORT_SYMBOL(ath9k_cmn_key_config);
626 void ath9k_cmn_key_delete(struct ath_common *common,
627 struct ieee80211_key_conf *key)
629 struct ath_hw *ah = common->ah;
631 ath9k_hw_keyreset(ah, key->hw_key_idx);
632 if (key->hw_key_idx < IEEE80211_WEP_NKID)
635 clear_bit(key->hw_key_idx, common->keymap);
636 if (key->alg != ALG_TKIP)
639 clear_bit(key->hw_key_idx + 64, common->keymap);
640 if (common->splitmic) {
641 ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
642 clear_bit(key->hw_key_idx + 32, common->keymap);
643 clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
646 EXPORT_SYMBOL(ath9k_cmn_key_delete);
648 static int __init ath9k_cmn_init(void)
652 module_init(ath9k_cmn_init);
654 static void __exit ath9k_cmn_exit(void)
658 module_exit(ath9k_cmn_exit);