common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright (c) 2009 Atheros Communications Inc.
  3. *
  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.
  7. *
  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.
  15. */
  16. /*
  17. * Module for common driver code between ath9k and ath9k_htc
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include "common.h"
  22. MODULE_AUTHOR("Atheros Communications");
  23. MODULE_DESCRIPTION("Shared library for Atheros wireless 802.11n LAN cards.");
  24. MODULE_LICENSE("Dual BSD/GPL");
  25. int ath9k_cmn_padpos(__le16 frame_control)
  26. {
  27. int padpos = 24;
  28. if (ieee80211_has_a4(frame_control)) {
  29. padpos += ETH_ALEN;
  30. }
  31. if (ieee80211_is_data_qos(frame_control)) {
  32. padpos += IEEE80211_QOS_CTL_LEN;
  33. }
  34. return padpos;
  35. }
  36. EXPORT_SYMBOL(ath9k_cmn_padpos);
  37. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  38. {
  39. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  40. if (tx_info->control.hw_key) {
  41. switch (tx_info->control.hw_key->cipher) {
  42. case WLAN_CIPHER_SUITE_WEP40:
  43. case WLAN_CIPHER_SUITE_WEP104:
  44. return ATH9K_KEY_TYPE_WEP;
  45. case WLAN_CIPHER_SUITE_TKIP:
  46. return ATH9K_KEY_TYPE_TKIP;
  47. case WLAN_CIPHER_SUITE_CCMP:
  48. return ATH9K_KEY_TYPE_AES;
  49. default:
  50. break;
  51. }
  52. }
  53. return ATH9K_KEY_TYPE_CLEAR;
  54. }
  55. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  56. static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
  57. enum nl80211_channel_type channel_type)
  58. {
  59. u32 chanmode = 0;
  60. switch (chan->band) {
  61. case IEEE80211_BAND_2GHZ:
  62. switch (channel_type) {
  63. case NL80211_CHAN_NO_HT:
  64. case NL80211_CHAN_HT20:
  65. chanmode = CHANNEL_G_HT20;
  66. break;
  67. case NL80211_CHAN_HT40PLUS:
  68. chanmode = CHANNEL_G_HT40PLUS;
  69. break;
  70. case NL80211_CHAN_HT40MINUS:
  71. chanmode = CHANNEL_G_HT40MINUS;
  72. break;
  73. }
  74. break;
  75. case IEEE80211_BAND_5GHZ:
  76. switch (channel_type) {
  77. case NL80211_CHAN_NO_HT:
  78. case NL80211_CHAN_HT20:
  79. chanmode = CHANNEL_A_HT20;
  80. break;
  81. case NL80211_CHAN_HT40PLUS:
  82. chanmode = CHANNEL_A_HT40PLUS;
  83. break;
  84. case NL80211_CHAN_HT40MINUS:
  85. chanmode = CHANNEL_A_HT40MINUS;
  86. break;
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. return chanmode;
  93. }
  94. /*
  95. * Update internal channel flags.
  96. */
  97. void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
  98. struct ath9k_channel *ichan)
  99. {
  100. struct ieee80211_channel *chan = hw->conf.channel;
  101. struct ieee80211_conf *conf = &hw->conf;
  102. ichan->channel = chan->center_freq;
  103. ichan->chan = chan;
  104. if (chan->band == IEEE80211_BAND_2GHZ) {
  105. ichan->chanmode = CHANNEL_G;
  106. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  107. } else {
  108. ichan->chanmode = CHANNEL_A;
  109. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  110. }
  111. if (conf_is_ht(conf))
  112. ichan->chanmode = ath9k_get_extchanmode(chan,
  113. conf->channel_type);
  114. }
  115. EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
  116. /*
  117. * Get the internal channel reference.
  118. */
  119. struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
  120. struct ath_hw *ah)
  121. {
  122. struct ieee80211_channel *curchan = hw->conf.channel;
  123. struct ath9k_channel *channel;
  124. u8 chan_idx;
  125. chan_idx = curchan->hw_value;
  126. channel = &ah->channels[chan_idx];
  127. ath9k_cmn_update_ichannel(hw, channel);
  128. return channel;
  129. }
  130. EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
  131. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  132. struct ath9k_keyval *hk, const u8 *addr,
  133. bool authenticator)
  134. {
  135. struct ath_hw *ah = common->ah;
  136. const u8 *key_rxmic;
  137. const u8 *key_txmic;
  138. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  139. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  140. if (addr == NULL) {
  141. /*
  142. * Group key installation - only two key cache entries are used
  143. * regardless of splitmic capability since group key is only
  144. * used either for TX or RX.
  145. */
  146. if (authenticator) {
  147. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  148. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  149. } else {
  150. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  151. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  152. }
  153. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  154. }
  155. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  156. /* TX and RX keys share the same key cache entry. */
  157. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  158. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  159. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  160. }
  161. /* Separate key cache entries for TX and RX */
  162. /* TX key goes at first index, RX key at +32. */
  163. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  164. if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
  165. /* TX MIC entry failed. No need to proceed further */
  166. ath_print(common, ATH_DBG_FATAL,
  167. "Setting TX MIC Key Failed\n");
  168. return 0;
  169. }
  170. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  171. /* XXX delete tx key on failure? */
  172. return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
  173. }
  174. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  175. {
  176. int i;
  177. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  178. if (test_bit(i, common->keymap) ||
  179. test_bit(i + 64, common->keymap))
  180. continue; /* At least one part of TKIP key allocated */
  181. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) &&
  182. (test_bit(i + 32, common->keymap) ||
  183. test_bit(i + 64 + 32, common->keymap)))
  184. continue; /* At least one part of TKIP key allocated */
  185. /* Found a free slot for a TKIP key */
  186. return i;
  187. }
  188. return -1;
  189. }
  190. static int ath_reserve_key_cache_slot(struct ath_common *common,
  191. u32 cipher)
  192. {
  193. int i;
  194. if (cipher == WLAN_CIPHER_SUITE_TKIP)
  195. return ath_reserve_key_cache_slot_tkip(common);
  196. /* First, try to find slots that would not be available for TKIP. */
  197. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  198. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  199. if (!test_bit(i, common->keymap) &&
  200. (test_bit(i + 32, common->keymap) ||
  201. test_bit(i + 64, common->keymap) ||
  202. test_bit(i + 64 + 32, common->keymap)))
  203. return i;
  204. if (!test_bit(i + 32, common->keymap) &&
  205. (test_bit(i, common->keymap) ||
  206. test_bit(i + 64, common->keymap) ||
  207. test_bit(i + 64 + 32, common->keymap)))
  208. return i + 32;
  209. if (!test_bit(i + 64, common->keymap) &&
  210. (test_bit(i , common->keymap) ||
  211. test_bit(i + 32, common->keymap) ||
  212. test_bit(i + 64 + 32, common->keymap)))
  213. return i + 64;
  214. if (!test_bit(i + 64 + 32, common->keymap) &&
  215. (test_bit(i, common->keymap) ||
  216. test_bit(i + 32, common->keymap) ||
  217. test_bit(i + 64, common->keymap)))
  218. return i + 64 + 32;
  219. }
  220. } else {
  221. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  222. if (!test_bit(i, common->keymap) &&
  223. test_bit(i + 64, common->keymap))
  224. return i;
  225. if (test_bit(i, common->keymap) &&
  226. !test_bit(i + 64, common->keymap))
  227. return i + 64;
  228. }
  229. }
  230. /* No partially used TKIP slots, pick any available slot */
  231. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  232. /* Do not allow slots that could be needed for TKIP group keys
  233. * to be used. This limitation could be removed if we know that
  234. * TKIP will not be used. */
  235. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  236. continue;
  237. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  238. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  239. continue;
  240. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  241. continue;
  242. }
  243. if (!test_bit(i, common->keymap))
  244. return i; /* Found a free slot for a key */
  245. }
  246. /* No free slot found */
  247. return -1;
  248. }
  249. /*
  250. * Configure encryption in the HW.
  251. */
  252. int ath9k_cmn_key_config(struct ath_common *common,
  253. struct ieee80211_vif *vif,
  254. struct ieee80211_sta *sta,
  255. struct ieee80211_key_conf *key)
  256. {
  257. struct ath_hw *ah = common->ah;
  258. struct ath9k_keyval hk;
  259. const u8 *mac = NULL;
  260. u8 gmac[ETH_ALEN];
  261. int ret = 0;
  262. int idx;
  263. memset(&hk, 0, sizeof(hk));
  264. switch (key->cipher) {
  265. case WLAN_CIPHER_SUITE_WEP40:
  266. case WLAN_CIPHER_SUITE_WEP104:
  267. hk.kv_type = ATH9K_CIPHER_WEP;
  268. break;
  269. case WLAN_CIPHER_SUITE_TKIP:
  270. hk.kv_type = ATH9K_CIPHER_TKIP;
  271. break;
  272. case WLAN_CIPHER_SUITE_CCMP:
  273. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  274. break;
  275. default:
  276. return -EOPNOTSUPP;
  277. }
  278. hk.kv_len = key->keylen;
  279. memcpy(hk.kv_val, key->key, key->keylen);
  280. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  281. switch (vif->type) {
  282. case NL80211_IFTYPE_AP:
  283. memcpy(gmac, vif->addr, ETH_ALEN);
  284. gmac[0] |= 0x01;
  285. mac = gmac;
  286. idx = ath_reserve_key_cache_slot(common, key->cipher);
  287. break;
  288. case NL80211_IFTYPE_ADHOC:
  289. if (!sta) {
  290. idx = key->keyidx;
  291. break;
  292. }
  293. memcpy(gmac, sta->addr, ETH_ALEN);
  294. gmac[0] |= 0x01;
  295. mac = gmac;
  296. idx = ath_reserve_key_cache_slot(common, key->cipher);
  297. break;
  298. default:
  299. idx = key->keyidx;
  300. break;
  301. }
  302. } else if (key->keyidx) {
  303. if (WARN_ON(!sta))
  304. return -EOPNOTSUPP;
  305. mac = sta->addr;
  306. if (vif->type != NL80211_IFTYPE_AP) {
  307. /* Only keyidx 0 should be used with unicast key, but
  308. * allow this for client mode for now. */
  309. idx = key->keyidx;
  310. } else
  311. return -EIO;
  312. } else {
  313. if (WARN_ON(!sta))
  314. return -EOPNOTSUPP;
  315. mac = sta->addr;
  316. idx = ath_reserve_key_cache_slot(common, key->cipher);
  317. }
  318. if (idx < 0)
  319. return -ENOSPC; /* no free key cache entries */
  320. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  321. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  322. vif->type == NL80211_IFTYPE_AP);
  323. else
  324. ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
  325. if (!ret)
  326. return -EIO;
  327. set_bit(idx, common->keymap);
  328. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  329. set_bit(idx + 64, common->keymap);
  330. set_bit(idx, common->tkip_keymap);
  331. set_bit(idx + 64, common->tkip_keymap);
  332. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  333. set_bit(idx + 32, common->keymap);
  334. set_bit(idx + 64 + 32, common->keymap);
  335. set_bit(idx + 32, common->tkip_keymap);
  336. set_bit(idx + 64 + 32, common->tkip_keymap);
  337. }
  338. }
  339. return idx;
  340. }
  341. EXPORT_SYMBOL(ath9k_cmn_key_config);
  342. /*
  343. * Delete Key.
  344. */
  345. void ath9k_cmn_key_delete(struct ath_common *common,
  346. struct ieee80211_key_conf *key)
  347. {
  348. struct ath_hw *ah = common->ah;
  349. ath9k_hw_keyreset(ah, key->hw_key_idx);
  350. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  351. return;
  352. clear_bit(key->hw_key_idx, common->keymap);
  353. if (key->cipher != WLAN_CIPHER_SUITE_TKIP)
  354. return;
  355. clear_bit(key->hw_key_idx + 64, common->keymap);
  356. clear_bit(key->hw_key_idx, common->tkip_keymap);
  357. clear_bit(key->hw_key_idx + 64, common->tkip_keymap);
  358. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  359. ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
  360. clear_bit(key->hw_key_idx + 32, common->keymap);
  361. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  362. clear_bit(key->hw_key_idx + 32, common->tkip_keymap);
  363. clear_bit(key->hw_key_idx + 64 + 32, common->tkip_keymap);
  364. }
  365. }
  366. EXPORT_SYMBOL(ath9k_cmn_key_delete);
  367. int ath9k_cmn_count_streams(unsigned int chainmask, int max)
  368. {
  369. int streams = 0;
  370. do {
  371. if (++streams == max)
  372. break;
  373. } while ((chainmask = chainmask & (chainmask - 1)));
  374. return streams;
  375. }
  376. EXPORT_SYMBOL(ath9k_cmn_count_streams);
  377. /*
  378. * Configures appropriate weight based on stomp type.
  379. */
  380. void ath9k_cmn_btcoex_bt_stomp(struct ath_common *common,
  381. enum ath_stomp_type stomp_type)
  382. {
  383. struct ath_hw *ah = common->ah;
  384. switch (stomp_type) {
  385. case ATH_BTCOEX_STOMP_ALL:
  386. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  387. AR_STOMP_ALL_WLAN_WGHT);
  388. break;
  389. case ATH_BTCOEX_STOMP_LOW:
  390. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  391. AR_STOMP_LOW_WLAN_WGHT);
  392. break;
  393. case ATH_BTCOEX_STOMP_NONE:
  394. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  395. AR_STOMP_NONE_WLAN_WGHT);
  396. break;
  397. default:
  398. ath_print(common, ATH_DBG_BTCOEX,
  399. "Invalid Stomptype\n");
  400. break;
  401. }
  402. ath9k_hw_btcoex_enable(ah);
  403. }
  404. EXPORT_SYMBOL(ath9k_cmn_btcoex_bt_stomp);
  405. static int __init ath9k_cmn_init(void)
  406. {
  407. return 0;
  408. }
  409. module_init(ath9k_cmn_init);
  410. static void __exit ath9k_cmn_exit(void)
  411. {
  412. return;
  413. }
  414. module_exit(ath9k_cmn_exit);