ath9k: remove usage of deprecated noise value
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / ath / ath9k / common.c
blobb4424a623cf5d2b0aa80019b906237183984c799
1 /*
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>
24 #include "common.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,
34 struct sk_buff *skb,
35 struct ieee80211_rx_status *rxs,
36 struct ath_rx_status *rx_stats,
37 bool *decrypt_error)
39 struct ath_hw *ah = common->ah;
40 struct ieee80211_hdr *hdr;
41 __le16 fc;
43 hdr = (struct ieee80211_hdr *) skb->data;
44 fc = hdr->frame_control;
46 if (!rx_stats->rs_datalen)
47 return false;
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.
53 if (rx_stats->rs_datalen > common->rx_bufsize)
54 return false;
57 * rs_more indicates chained descriptors which can be used
58 * to link buffers together for a sort of scatter-gather
59 * operation.
61 * The rx_stats->rs_status will not be set until the end of the
62 * chained descriptors so it can be ignored if rs_more is set. The
63 * rs_more will be false at the last element of the chained
64 * descriptors.
66 if (!rx_stats->rs_more && rx_stats->rs_status != 0) {
67 if (rx_stats->rs_status & ATH9K_RXERR_CRC)
68 rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
69 if (rx_stats->rs_status & ATH9K_RXERR_PHY)
70 return false;
72 if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
73 *decrypt_error = true;
74 } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
75 if (ieee80211_is_ctl(fc))
77 * Sometimes, we get invalid
78 * MIC failures on valid control frames.
79 * Remove these mic errors.
81 rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
82 else
83 rxs->flag |= RX_FLAG_MMIC_ERROR;
86 * Reject error frames with the exception of
87 * decryption and MIC failures. For monitor mode,
88 * we also ignore the CRC error.
90 if (ah->opmode == NL80211_IFTYPE_MONITOR) {
91 if (rx_stats->rs_status &
92 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
93 ATH9K_RXERR_CRC))
94 return false;
95 } else {
96 if (rx_stats->rs_status &
97 ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
98 return false;
102 return true;
105 static u8 ath9k_process_rate(struct ath_common *common,
106 struct ieee80211_hw *hw,
107 struct ath_rx_status *rx_stats,
108 struct ieee80211_rx_status *rxs,
109 struct sk_buff *skb)
111 struct ieee80211_supported_band *sband;
112 enum ieee80211_band band;
113 unsigned int i = 0;
115 band = hw->conf.channel->band;
116 sband = hw->wiphy->bands[band];
118 if (rx_stats->rs_rate & 0x80) {
119 /* HT rate */
120 rxs->flag |= RX_FLAG_HT;
121 if (rx_stats->rs_flags & ATH9K_RX_2040)
122 rxs->flag |= RX_FLAG_40MHZ;
123 if (rx_stats->rs_flags & ATH9K_RX_GI)
124 rxs->flag |= RX_FLAG_SHORT_GI;
125 return rx_stats->rs_rate & 0x7f;
128 for (i = 0; i < sband->n_bitrates; i++) {
129 if (sband->bitrates[i].hw_value == rx_stats->rs_rate)
130 return i;
131 if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
132 rxs->flag |= RX_FLAG_SHORTPRE;
133 return i;
137 /* No valid hardware bitrate found -- we should not get here */
138 ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
139 "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
140 if ((common->debug_mask & ATH_DBG_XMIT))
141 print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
143 return 0;
146 static void ath9k_process_rssi(struct ath_common *common,
147 struct ieee80211_hw *hw,
148 struct sk_buff *skb,
149 struct ath_rx_status *rx_stats)
151 struct ath_hw *ah = common->ah;
152 struct ieee80211_sta *sta;
153 struct ieee80211_hdr *hdr;
154 struct ath_node *an;
155 int last_rssi = ATH_RSSI_DUMMY_MARKER;
156 __le16 fc;
158 hdr = (struct ieee80211_hdr *)skb->data;
159 fc = hdr->frame_control;
161 rcu_read_lock();
163 * XXX: use ieee80211_find_sta! This requires quite a bit of work
164 * under the current ath9k virtual wiphy implementation as we have
165 * no way of tying a vif to wiphy. Typically vifs are attached to
166 * at least one sdata of a wiphy on mac80211 but with ath9k virtual
167 * wiphy you'd have to iterate over every wiphy and each sdata.
169 sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
170 if (sta) {
171 an = (struct ath_node *) sta->drv_priv;
172 if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
173 !rx_stats->rs_moreaggr)
174 ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
175 last_rssi = an->last_rssi;
177 rcu_read_unlock();
179 if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
180 rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
181 ATH_RSSI_EP_MULTIPLIER);
182 if (rx_stats->rs_rssi < 0)
183 rx_stats->rs_rssi = 0;
185 /* Update Beacon RSSI, this is used by ANI. */
186 if (ieee80211_is_beacon(fc))
187 ah->stats.avgbrssi = rx_stats->rs_rssi;
191 * For Decrypt or Demic errors, we only mark packet status here and always push
192 * up the frame up to let mac80211 handle the actual error case, be it no
193 * decryption key or real decryption error. This let us keep statistics there.
195 int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
196 struct ieee80211_hw *hw,
197 struct sk_buff *skb,
198 struct ath_rx_status *rx_stats,
199 struct ieee80211_rx_status *rx_status,
200 bool *decrypt_error)
202 struct ath_hw *ah = common->ah;
204 memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
205 if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
206 return -EINVAL;
208 ath9k_process_rssi(common, hw, skb, rx_stats);
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->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
216 rx_status->antenna = rx_stats->rs_antenna;
217 rx_status->flag |= RX_FLAG_TSFT;
219 return 0;
221 EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
223 void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
224 struct sk_buff *skb,
225 struct ath_rx_status *rx_stats,
226 struct ieee80211_rx_status *rxs,
227 bool decrypt_error)
229 struct ath_hw *ah = common->ah;
230 struct ieee80211_hdr *hdr;
231 int hdrlen, padpos, padsize;
232 u8 keyix;
233 __le16 fc;
235 /* see if any padding is done by the hw and remove it */
236 hdr = (struct ieee80211_hdr *) skb->data;
237 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
238 fc = hdr->frame_control;
239 padpos = ath9k_cmn_padpos(hdr->frame_control);
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 - padpos % 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 = padpos & 3;
250 if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
251 memmove(skb->data + padsize, skb->data, padpos);
252 skb_pull(skb, padsize);
255 keyix = rx_stats->rs_keyix;
257 if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
258 ieee80211_has_protected(fc)) {
259 rxs->flag |= RX_FLAG_DECRYPTED;
260 } else if (ieee80211_has_protected(fc)
261 && !decrypt_error && skb->len >= hdrlen + 4) {
262 keyix = skb->data[hdrlen + 3] >> 6;
264 if (test_bit(keyix, common->keymap))
265 rxs->flag |= RX_FLAG_DECRYPTED;
267 if (ah->sw_mgmt_crypto &&
268 (rxs->flag & RX_FLAG_DECRYPTED) &&
269 ieee80211_is_mgmt(fc))
270 /* Use software decrypt for management frames. */
271 rxs->flag &= ~RX_FLAG_DECRYPTED;
273 EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
275 int ath9k_cmn_padpos(__le16 frame_control)
277 int padpos = 24;
278 if (ieee80211_has_a4(frame_control)) {
279 padpos += ETH_ALEN;
281 if (ieee80211_is_data_qos(frame_control)) {
282 padpos += IEEE80211_QOS_CTL_LEN;
285 return padpos;
287 EXPORT_SYMBOL(ath9k_cmn_padpos);
289 int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
291 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
293 if (tx_info->control.hw_key) {
294 if (tx_info->control.hw_key->alg == ALG_WEP)
295 return ATH9K_KEY_TYPE_WEP;
296 else if (tx_info->control.hw_key->alg == ALG_TKIP)
297 return ATH9K_KEY_TYPE_TKIP;
298 else if (tx_info->control.hw_key->alg == ALG_CCMP)
299 return ATH9K_KEY_TYPE_AES;
302 return ATH9K_KEY_TYPE_CLEAR;
304 EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
306 static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
307 enum nl80211_channel_type channel_type)
309 u32 chanmode = 0;
311 switch (chan->band) {
312 case IEEE80211_BAND_2GHZ:
313 switch (channel_type) {
314 case NL80211_CHAN_NO_HT:
315 case NL80211_CHAN_HT20:
316 chanmode = CHANNEL_G_HT20;
317 break;
318 case NL80211_CHAN_HT40PLUS:
319 chanmode = CHANNEL_G_HT40PLUS;
320 break;
321 case NL80211_CHAN_HT40MINUS:
322 chanmode = CHANNEL_G_HT40MINUS;
323 break;
325 break;
326 case IEEE80211_BAND_5GHZ:
327 switch (channel_type) {
328 case NL80211_CHAN_NO_HT:
329 case NL80211_CHAN_HT20:
330 chanmode = CHANNEL_A_HT20;
331 break;
332 case NL80211_CHAN_HT40PLUS:
333 chanmode = CHANNEL_A_HT40PLUS;
334 break;
335 case NL80211_CHAN_HT40MINUS:
336 chanmode = CHANNEL_A_HT40MINUS;
337 break;
339 break;
340 default:
341 break;
344 return chanmode;
348 * Update internal channel flags.
350 void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
351 struct ath9k_channel *ichan)
353 struct ieee80211_channel *chan = hw->conf.channel;
354 struct ieee80211_conf *conf = &hw->conf;
356 ichan->channel = chan->center_freq;
357 ichan->chan = chan;
359 if (chan->band == IEEE80211_BAND_2GHZ) {
360 ichan->chanmode = CHANNEL_G;
361 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
362 } else {
363 ichan->chanmode = CHANNEL_A;
364 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
367 if (conf_is_ht(conf))
368 ichan->chanmode = ath9k_get_extchanmode(chan,
369 conf->channel_type);
371 EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
374 * Get the internal channel reference.
376 struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
377 struct ath_hw *ah)
379 struct ieee80211_channel *curchan = hw->conf.channel;
380 struct ath9k_channel *channel;
381 u8 chan_idx;
383 chan_idx = curchan->hw_value;
384 channel = &ah->channels[chan_idx];
385 ath9k_cmn_update_ichannel(hw, channel);
387 return channel;
389 EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
391 static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
392 struct ath9k_keyval *hk, const u8 *addr,
393 bool authenticator)
395 struct ath_hw *ah = common->ah;
396 const u8 *key_rxmic;
397 const u8 *key_txmic;
399 key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
400 key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
402 if (addr == NULL) {
404 * Group key installation - only two key cache entries are used
405 * regardless of splitmic capability since group key is only
406 * used either for TX or RX.
408 if (authenticator) {
409 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
410 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
411 } else {
412 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
413 memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
415 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
417 if (!common->splitmic) {
418 /* TX and RX keys share the same key cache entry. */
419 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
420 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
421 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
424 /* Separate key cache entries for TX and RX */
426 /* TX key goes at first index, RX key at +32. */
427 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
428 if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
429 /* TX MIC entry failed. No need to proceed further */
430 ath_print(common, ATH_DBG_FATAL,
431 "Setting TX MIC Key Failed\n");
432 return 0;
435 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
436 /* XXX delete tx key on failure? */
437 return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
440 static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
442 int i;
444 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
445 if (test_bit(i, common->keymap) ||
446 test_bit(i + 64, common->keymap))
447 continue; /* At least one part of TKIP key allocated */
448 if (common->splitmic &&
449 (test_bit(i + 32, common->keymap) ||
450 test_bit(i + 64 + 32, common->keymap)))
451 continue; /* At least one part of TKIP key allocated */
453 /* Found a free slot for a TKIP key */
454 return i;
456 return -1;
459 static int ath_reserve_key_cache_slot(struct ath_common *common)
461 int i;
463 /* First, try to find slots that would not be available for TKIP. */
464 if (common->splitmic) {
465 for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
466 if (!test_bit(i, common->keymap) &&
467 (test_bit(i + 32, common->keymap) ||
468 test_bit(i + 64, common->keymap) ||
469 test_bit(i + 64 + 32, common->keymap)))
470 return i;
471 if (!test_bit(i + 32, common->keymap) &&
472 (test_bit(i, common->keymap) ||
473 test_bit(i + 64, common->keymap) ||
474 test_bit(i + 64 + 32, common->keymap)))
475 return i + 32;
476 if (!test_bit(i + 64, common->keymap) &&
477 (test_bit(i , common->keymap) ||
478 test_bit(i + 32, common->keymap) ||
479 test_bit(i + 64 + 32, common->keymap)))
480 return i + 64;
481 if (!test_bit(i + 64 + 32, common->keymap) &&
482 (test_bit(i, common->keymap) ||
483 test_bit(i + 32, common->keymap) ||
484 test_bit(i + 64, common->keymap)))
485 return i + 64 + 32;
487 } else {
488 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
489 if (!test_bit(i, common->keymap) &&
490 test_bit(i + 64, common->keymap))
491 return i;
492 if (test_bit(i, common->keymap) &&
493 !test_bit(i + 64, common->keymap))
494 return i + 64;
498 /* No partially used TKIP slots, pick any available slot */
499 for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
500 /* Do not allow slots that could be needed for TKIP group keys
501 * to be used. This limitation could be removed if we know that
502 * TKIP will not be used. */
503 if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
504 continue;
505 if (common->splitmic) {
506 if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
507 continue;
508 if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
509 continue;
512 if (!test_bit(i, common->keymap))
513 return i; /* Found a free slot for a key */
516 /* No free slot found */
517 return -1;
521 * Configure encryption in the HW.
523 int ath9k_cmn_key_config(struct ath_common *common,
524 struct ieee80211_vif *vif,
525 struct ieee80211_sta *sta,
526 struct ieee80211_key_conf *key)
528 struct ath_hw *ah = common->ah;
529 struct ath9k_keyval hk;
530 const u8 *mac = NULL;
531 int ret = 0;
532 int idx;
534 memset(&hk, 0, sizeof(hk));
536 switch (key->alg) {
537 case ALG_WEP:
538 hk.kv_type = ATH9K_CIPHER_WEP;
539 break;
540 case ALG_TKIP:
541 hk.kv_type = ATH9K_CIPHER_TKIP;
542 break;
543 case ALG_CCMP:
544 hk.kv_type = ATH9K_CIPHER_AES_CCM;
545 break;
546 default:
547 return -EOPNOTSUPP;
550 hk.kv_len = key->keylen;
551 memcpy(hk.kv_val, key->key, key->keylen);
553 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
554 /* For now, use the default keys for broadcast keys. This may
555 * need to change with virtual interfaces. */
556 idx = key->keyidx;
557 } else if (key->keyidx) {
558 if (WARN_ON(!sta))
559 return -EOPNOTSUPP;
560 mac = sta->addr;
562 if (vif->type != NL80211_IFTYPE_AP) {
563 /* Only keyidx 0 should be used with unicast key, but
564 * allow this for client mode for now. */
565 idx = key->keyidx;
566 } else
567 return -EIO;
568 } else {
569 if (WARN_ON(!sta))
570 return -EOPNOTSUPP;
571 mac = sta->addr;
573 if (key->alg == ALG_TKIP)
574 idx = ath_reserve_key_cache_slot_tkip(common);
575 else
576 idx = ath_reserve_key_cache_slot(common);
577 if (idx < 0)
578 return -ENOSPC; /* no free key cache entries */
581 if (key->alg == ALG_TKIP)
582 ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
583 vif->type == NL80211_IFTYPE_AP);
584 else
585 ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
587 if (!ret)
588 return -EIO;
590 set_bit(idx, common->keymap);
591 if (key->alg == ALG_TKIP) {
592 set_bit(idx + 64, common->keymap);
593 if (common->splitmic) {
594 set_bit(idx + 32, common->keymap);
595 set_bit(idx + 64 + 32, common->keymap);
599 return idx;
601 EXPORT_SYMBOL(ath9k_cmn_key_config);
604 * Delete Key.
606 void ath9k_cmn_key_delete(struct ath_common *common,
607 struct ieee80211_key_conf *key)
609 struct ath_hw *ah = common->ah;
611 ath9k_hw_keyreset(ah, key->hw_key_idx);
612 if (key->hw_key_idx < IEEE80211_WEP_NKID)
613 return;
615 clear_bit(key->hw_key_idx, common->keymap);
616 if (key->alg != ALG_TKIP)
617 return;
619 clear_bit(key->hw_key_idx + 64, common->keymap);
620 if (common->splitmic) {
621 ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
622 clear_bit(key->hw_key_idx + 32, common->keymap);
623 clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
626 EXPORT_SYMBOL(ath9k_cmn_key_delete);
628 static int __init ath9k_cmn_init(void)
630 return 0;
632 module_init(ath9k_cmn_init);
634 static void __exit ath9k_cmn_exit(void)
636 return;
638 module_exit(ath9k_cmn_exit);