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 int ath9k_cmn_padpos(__le16 frame_control)
33 if (ieee80211_has_a4(frame_control)) {
36 if (ieee80211_is_data_qos(frame_control)) {
37 padpos += IEEE80211_QOS_CTL_LEN;
42 EXPORT_SYMBOL(ath9k_cmn_padpos);
44 int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
46 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
48 if (tx_info->control.hw_key) {
49 if (tx_info->control.hw_key->alg == ALG_WEP)
50 return ATH9K_KEY_TYPE_WEP;
51 else if (tx_info->control.hw_key->alg == ALG_TKIP)
52 return ATH9K_KEY_TYPE_TKIP;
53 else if (tx_info->control.hw_key->alg == ALG_CCMP)
54 return ATH9K_KEY_TYPE_AES;
57 return ATH9K_KEY_TYPE_CLEAR;
59 EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
61 static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
62 enum nl80211_channel_type channel_type)
67 case IEEE80211_BAND_2GHZ:
68 switch (channel_type) {
69 case NL80211_CHAN_NO_HT:
70 case NL80211_CHAN_HT20:
71 chanmode = CHANNEL_G_HT20;
73 case NL80211_CHAN_HT40PLUS:
74 chanmode = CHANNEL_G_HT40PLUS;
76 case NL80211_CHAN_HT40MINUS:
77 chanmode = CHANNEL_G_HT40MINUS;
81 case IEEE80211_BAND_5GHZ:
82 switch (channel_type) {
83 case NL80211_CHAN_NO_HT:
84 case NL80211_CHAN_HT20:
85 chanmode = CHANNEL_A_HT20;
87 case NL80211_CHAN_HT40PLUS:
88 chanmode = CHANNEL_A_HT40PLUS;
90 case NL80211_CHAN_HT40MINUS:
91 chanmode = CHANNEL_A_HT40MINUS;
103 * Update internal channel flags.
105 void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
106 struct ath9k_channel *ichan)
108 struct ieee80211_channel *chan = hw->conf.channel;
109 struct ieee80211_conf *conf = &hw->conf;
111 ichan->channel = chan->center_freq;
114 if (chan->band == IEEE80211_BAND_2GHZ) {
115 ichan->chanmode = CHANNEL_G;
116 ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
118 ichan->chanmode = CHANNEL_A;
119 ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
122 if (conf_is_ht(conf))
123 ichan->chanmode = ath9k_get_extchanmode(chan,
126 EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
129 * Get the internal channel reference.
131 struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
134 struct ieee80211_channel *curchan = hw->conf.channel;
135 struct ath9k_channel *channel;
138 chan_idx = curchan->hw_value;
139 channel = &ah->channels[chan_idx];
140 ath9k_cmn_update_ichannel(hw, channel);
144 EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
146 static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
147 struct ath9k_keyval *hk, const u8 *addr,
150 struct ath_hw *ah = common->ah;
154 key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
155 key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
159 * Group key installation - only two key cache entries are used
160 * regardless of splitmic capability since group key is only
161 * used either for TX or RX.
164 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
165 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
167 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
168 memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
170 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
172 if (!common->splitmic) {
173 /* TX and RX keys share the same key cache entry. */
174 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
175 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
176 return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
179 /* Separate key cache entries for TX and RX */
181 /* TX key goes at first index, RX key at +32. */
182 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
183 if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
184 /* TX MIC entry failed. No need to proceed further */
185 ath_print(common, ATH_DBG_FATAL,
186 "Setting TX MIC Key Failed\n");
190 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
191 /* XXX delete tx key on failure? */
192 return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
195 static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
199 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
200 if (test_bit(i, common->keymap) ||
201 test_bit(i + 64, common->keymap))
202 continue; /* At least one part of TKIP key allocated */
203 if (common->splitmic &&
204 (test_bit(i + 32, common->keymap) ||
205 test_bit(i + 64 + 32, common->keymap)))
206 continue; /* At least one part of TKIP key allocated */
208 /* Found a free slot for a TKIP key */
214 static int ath_reserve_key_cache_slot(struct ath_common *common,
215 enum ieee80211_key_alg alg)
220 return ath_reserve_key_cache_slot_tkip(common);
222 /* First, try to find slots that would not be available for TKIP. */
223 if (common->splitmic) {
224 for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
225 if (!test_bit(i, common->keymap) &&
226 (test_bit(i + 32, common->keymap) ||
227 test_bit(i + 64, common->keymap) ||
228 test_bit(i + 64 + 32, common->keymap)))
230 if (!test_bit(i + 32, common->keymap) &&
231 (test_bit(i, common->keymap) ||
232 test_bit(i + 64, common->keymap) ||
233 test_bit(i + 64 + 32, common->keymap)))
235 if (!test_bit(i + 64, common->keymap) &&
236 (test_bit(i , common->keymap) ||
237 test_bit(i + 32, common->keymap) ||
238 test_bit(i + 64 + 32, common->keymap)))
240 if (!test_bit(i + 64 + 32, common->keymap) &&
241 (test_bit(i, common->keymap) ||
242 test_bit(i + 32, common->keymap) ||
243 test_bit(i + 64, common->keymap)))
247 for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
248 if (!test_bit(i, common->keymap) &&
249 test_bit(i + 64, common->keymap))
251 if (test_bit(i, common->keymap) &&
252 !test_bit(i + 64, common->keymap))
257 /* No partially used TKIP slots, pick any available slot */
258 for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
259 /* Do not allow slots that could be needed for TKIP group keys
260 * to be used. This limitation could be removed if we know that
261 * TKIP will not be used. */
262 if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
264 if (common->splitmic) {
265 if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
267 if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
271 if (!test_bit(i, common->keymap))
272 return i; /* Found a free slot for a key */
275 /* No free slot found */
280 * Configure encryption in the HW.
282 int ath9k_cmn_key_config(struct ath_common *common,
283 struct ieee80211_vif *vif,
284 struct ieee80211_sta *sta,
285 struct ieee80211_key_conf *key)
287 struct ath_hw *ah = common->ah;
288 struct ath9k_keyval hk;
289 const u8 *mac = NULL;
294 memset(&hk, 0, sizeof(hk));
298 hk.kv_type = ATH9K_CIPHER_WEP;
301 hk.kv_type = ATH9K_CIPHER_TKIP;
304 hk.kv_type = ATH9K_CIPHER_AES_CCM;
310 hk.kv_len = key->keylen;
311 memcpy(hk.kv_val, key->key, key->keylen);
313 if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
315 case NL80211_IFTYPE_AP:
316 memcpy(gmac, vif->addr, ETH_ALEN);
319 idx = ath_reserve_key_cache_slot(common, key->alg);
321 case NL80211_IFTYPE_ADHOC:
326 memcpy(gmac, sta->addr, ETH_ALEN);
329 idx = ath_reserve_key_cache_slot(common, key->alg);
335 } else if (key->keyidx) {
340 if (vif->type != NL80211_IFTYPE_AP) {
341 /* Only keyidx 0 should be used with unicast key, but
342 * allow this for client mode for now. */
351 idx = ath_reserve_key_cache_slot(common, key->alg);
355 return -ENOSPC; /* no free key cache entries */
357 if (key->alg == ALG_TKIP)
358 ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
359 vif->type == NL80211_IFTYPE_AP);
361 ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
366 set_bit(idx, common->keymap);
367 if (key->alg == ALG_TKIP) {
368 set_bit(idx + 64, common->keymap);
369 if (common->splitmic) {
370 set_bit(idx + 32, common->keymap);
371 set_bit(idx + 64 + 32, common->keymap);
377 EXPORT_SYMBOL(ath9k_cmn_key_config);
382 void ath9k_cmn_key_delete(struct ath_common *common,
383 struct ieee80211_key_conf *key)
385 struct ath_hw *ah = common->ah;
387 ath9k_hw_keyreset(ah, key->hw_key_idx);
388 if (key->hw_key_idx < IEEE80211_WEP_NKID)
391 clear_bit(key->hw_key_idx, common->keymap);
392 if (key->alg != ALG_TKIP)
395 clear_bit(key->hw_key_idx + 64, common->keymap);
396 if (common->splitmic) {
397 ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
398 clear_bit(key->hw_key_idx + 32, common->keymap);
399 clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
402 EXPORT_SYMBOL(ath9k_cmn_key_delete);
404 int ath9k_cmn_count_streams(unsigned int chainmask, int max)
409 if (++streams == max)
411 } while ((chainmask = chainmask & (chainmask - 1)));
415 EXPORT_SYMBOL(ath9k_cmn_count_streams);
417 static int __init ath9k_cmn_init(void)
421 module_init(ath9k_cmn_init);
423 static void __exit ath9k_cmn_exit(void)
427 module_exit(ath9k_cmn_exit);