common.c 11 KB

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