key.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright (c) 2009 Atheros Communications Inc.
  3. * Copyright (c) 2010 Bruno Randolf <br1@einfach.org>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/export.h>
  18. #include <asm/unaligned.h>
  19. #include <net/mac80211.h>
  20. #include "ath.h"
  21. #include "reg.h"
  22. #define REG_READ (common->ops->read)
  23. #define REG_WRITE(_ah, _reg, _val) (common->ops->write)(_ah, _val, _reg)
  24. #define ENABLE_REGWRITE_BUFFER(_ah) \
  25. if (common->ops->enable_write_buffer) \
  26. common->ops->enable_write_buffer((_ah));
  27. #define REGWRITE_BUFFER_FLUSH(_ah) \
  28. if (common->ops->write_flush) \
  29. common->ops->write_flush((_ah));
  30. #define IEEE80211_WEP_NKID 4 /* number of key ids */
  31. /************************/
  32. /* Key Cache Management */
  33. /************************/
  34. bool ath_hw_keyreset(struct ath_common *common, u16 entry)
  35. {
  36. u32 keyType;
  37. void *ah = common->ah;
  38. if (entry >= common->keymax) {
  39. ath_err(common, "keycache entry %u out of range\n", entry);
  40. return false;
  41. }
  42. keyType = REG_READ(ah, AR_KEYTABLE_TYPE(entry));
  43. ENABLE_REGWRITE_BUFFER(ah);
  44. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), 0);
  45. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), 0);
  46. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), 0);
  47. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), 0);
  48. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), 0);
  49. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), AR_KEYTABLE_TYPE_CLR);
  50. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), 0);
  51. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), 0);
  52. if (keyType == AR_KEYTABLE_TYPE_TKIP) {
  53. u16 micentry = entry + 64;
  54. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), 0);
  55. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  56. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), 0);
  57. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  58. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  59. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  60. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  61. AR_KEYTABLE_TYPE_CLR);
  62. }
  63. }
  64. REGWRITE_BUFFER_FLUSH(ah);
  65. return true;
  66. }
  67. EXPORT_SYMBOL(ath_hw_keyreset);
  68. static bool ath_hw_keysetmac(struct ath_common *common,
  69. u16 entry, const u8 *mac)
  70. {
  71. u32 macHi, macLo;
  72. u32 unicast_flag = AR_KEYTABLE_VALID;
  73. void *ah = common->ah;
  74. if (entry >= common->keymax) {
  75. ath_err(common, "keycache entry %u out of range\n", entry);
  76. return false;
  77. }
  78. if (mac != NULL) {
  79. /*
  80. * AR_KEYTABLE_VALID indicates that the address is a unicast
  81. * address, which must match the transmitter address for
  82. * decrypting frames.
  83. * Not setting this bit allows the hardware to use the key
  84. * for multicast frame decryption.
  85. */
  86. if (mac[0] & 0x01)
  87. unicast_flag = 0;
  88. macLo = get_unaligned_le32(mac);
  89. macHi = get_unaligned_le16(mac + 4);
  90. macLo >>= 1;
  91. macLo |= (macHi & 1) << 31;
  92. macHi >>= 1;
  93. } else {
  94. macLo = macHi = 0;
  95. }
  96. ENABLE_REGWRITE_BUFFER(ah);
  97. REG_WRITE(ah, AR_KEYTABLE_MAC0(entry), macLo);
  98. REG_WRITE(ah, AR_KEYTABLE_MAC1(entry), macHi | unicast_flag);
  99. REGWRITE_BUFFER_FLUSH(ah);
  100. return true;
  101. }
  102. static bool ath_hw_set_keycache_entry(struct ath_common *common, u16 entry,
  103. const struct ath_keyval *k,
  104. const u8 *mac)
  105. {
  106. void *ah = common->ah;
  107. u32 key0, key1, key2, key3, key4;
  108. u32 keyType;
  109. if (entry >= common->keymax) {
  110. ath_err(common, "keycache entry %u out of range\n", entry);
  111. return false;
  112. }
  113. switch (k->kv_type) {
  114. case ATH_CIPHER_AES_OCB:
  115. keyType = AR_KEYTABLE_TYPE_AES;
  116. break;
  117. case ATH_CIPHER_AES_CCM:
  118. if (!(common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)) {
  119. ath_dbg(common, ATH_DBG_ANY,
  120. "AES-CCM not supported by this mac rev\n");
  121. return false;
  122. }
  123. keyType = AR_KEYTABLE_TYPE_CCM;
  124. break;
  125. case ATH_CIPHER_TKIP:
  126. keyType = AR_KEYTABLE_TYPE_TKIP;
  127. if (entry + 64 >= common->keymax) {
  128. ath_dbg(common, ATH_DBG_ANY,
  129. "entry %u inappropriate for TKIP\n", entry);
  130. return false;
  131. }
  132. break;
  133. case ATH_CIPHER_WEP:
  134. if (k->kv_len < WLAN_KEY_LEN_WEP40) {
  135. ath_dbg(common, ATH_DBG_ANY,
  136. "WEP key length %u too small\n", k->kv_len);
  137. return false;
  138. }
  139. if (k->kv_len <= WLAN_KEY_LEN_WEP40)
  140. keyType = AR_KEYTABLE_TYPE_40;
  141. else if (k->kv_len <= WLAN_KEY_LEN_WEP104)
  142. keyType = AR_KEYTABLE_TYPE_104;
  143. else
  144. keyType = AR_KEYTABLE_TYPE_128;
  145. break;
  146. case ATH_CIPHER_CLR:
  147. keyType = AR_KEYTABLE_TYPE_CLR;
  148. break;
  149. default:
  150. ath_err(common, "cipher %u not supported\n", k->kv_type);
  151. return false;
  152. }
  153. key0 = get_unaligned_le32(k->kv_val + 0);
  154. key1 = get_unaligned_le16(k->kv_val + 4);
  155. key2 = get_unaligned_le32(k->kv_val + 6);
  156. key3 = get_unaligned_le16(k->kv_val + 10);
  157. key4 = get_unaligned_le32(k->kv_val + 12);
  158. if (k->kv_len <= WLAN_KEY_LEN_WEP104)
  159. key4 &= 0xff;
  160. /*
  161. * Note: Key cache registers access special memory area that requires
  162. * two 32-bit writes to actually update the values in the internal
  163. * memory. Consequently, the exact order and pairs used here must be
  164. * maintained.
  165. */
  166. if (keyType == AR_KEYTABLE_TYPE_TKIP) {
  167. u16 micentry = entry + 64;
  168. /*
  169. * Write inverted key[47:0] first to avoid Michael MIC errors
  170. * on frames that could be sent or received at the same time.
  171. * The correct key will be written in the end once everything
  172. * else is ready.
  173. */
  174. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), ~key0);
  175. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), ~key1);
  176. /* Write key[95:48] */
  177. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  178. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  179. /* Write key[127:96] and key type */
  180. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  181. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  182. /* Write MAC address for the entry */
  183. (void) ath_hw_keysetmac(common, entry, mac);
  184. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  185. /*
  186. * TKIP uses two key cache entries:
  187. * Michael MIC TX/RX keys in the same key cache entry
  188. * (idx = main index + 64):
  189. * key0 [31:0] = RX key [31:0]
  190. * key1 [15:0] = TX key [31:16]
  191. * key1 [31:16] = reserved
  192. * key2 [31:0] = RX key [63:32]
  193. * key3 [15:0] = TX key [15:0]
  194. * key3 [31:16] = reserved
  195. * key4 [31:0] = TX key [63:32]
  196. */
  197. u32 mic0, mic1, mic2, mic3, mic4;
  198. mic0 = get_unaligned_le32(k->kv_mic + 0);
  199. mic2 = get_unaligned_le32(k->kv_mic + 4);
  200. mic1 = get_unaligned_le16(k->kv_txmic + 2) & 0xffff;
  201. mic3 = get_unaligned_le16(k->kv_txmic + 0) & 0xffff;
  202. mic4 = get_unaligned_le32(k->kv_txmic + 4);
  203. ENABLE_REGWRITE_BUFFER(ah);
  204. /* Write RX[31:0] and TX[31:16] */
  205. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  206. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), mic1);
  207. /* Write RX[63:32] and TX[15:0] */
  208. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  209. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), mic3);
  210. /* Write TX[63:32] and keyType(reserved) */
  211. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), mic4);
  212. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  213. AR_KEYTABLE_TYPE_CLR);
  214. REGWRITE_BUFFER_FLUSH(ah);
  215. } else {
  216. /*
  217. * TKIP uses four key cache entries (two for group
  218. * keys):
  219. * Michael MIC TX/RX keys are in different key cache
  220. * entries (idx = main index + 64 for TX and
  221. * main index + 32 + 96 for RX):
  222. * key0 [31:0] = TX/RX MIC key [31:0]
  223. * key1 [31:0] = reserved
  224. * key2 [31:0] = TX/RX MIC key [63:32]
  225. * key3 [31:0] = reserved
  226. * key4 [31:0] = reserved
  227. *
  228. * Upper layer code will call this function separately
  229. * for TX and RX keys when these registers offsets are
  230. * used.
  231. */
  232. u32 mic0, mic2;
  233. mic0 = get_unaligned_le32(k->kv_mic + 0);
  234. mic2 = get_unaligned_le32(k->kv_mic + 4);
  235. ENABLE_REGWRITE_BUFFER(ah);
  236. /* Write MIC key[31:0] */
  237. REG_WRITE(ah, AR_KEYTABLE_KEY0(micentry), mic0);
  238. REG_WRITE(ah, AR_KEYTABLE_KEY1(micentry), 0);
  239. /* Write MIC key[63:32] */
  240. REG_WRITE(ah, AR_KEYTABLE_KEY2(micentry), mic2);
  241. REG_WRITE(ah, AR_KEYTABLE_KEY3(micentry), 0);
  242. /* Write TX[63:32] and keyType(reserved) */
  243. REG_WRITE(ah, AR_KEYTABLE_KEY4(micentry), 0);
  244. REG_WRITE(ah, AR_KEYTABLE_TYPE(micentry),
  245. AR_KEYTABLE_TYPE_CLR);
  246. REGWRITE_BUFFER_FLUSH(ah);
  247. }
  248. ENABLE_REGWRITE_BUFFER(ah);
  249. /* MAC address registers are reserved for the MIC entry */
  250. REG_WRITE(ah, AR_KEYTABLE_MAC0(micentry), 0);
  251. REG_WRITE(ah, AR_KEYTABLE_MAC1(micentry), 0);
  252. /*
  253. * Write the correct (un-inverted) key[47:0] last to enable
  254. * TKIP now that all other registers are set with correct
  255. * values.
  256. */
  257. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  258. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  259. REGWRITE_BUFFER_FLUSH(ah);
  260. } else {
  261. ENABLE_REGWRITE_BUFFER(ah);
  262. /* Write key[47:0] */
  263. REG_WRITE(ah, AR_KEYTABLE_KEY0(entry), key0);
  264. REG_WRITE(ah, AR_KEYTABLE_KEY1(entry), key1);
  265. /* Write key[95:48] */
  266. REG_WRITE(ah, AR_KEYTABLE_KEY2(entry), key2);
  267. REG_WRITE(ah, AR_KEYTABLE_KEY3(entry), key3);
  268. /* Write key[127:96] and key type */
  269. REG_WRITE(ah, AR_KEYTABLE_KEY4(entry), key4);
  270. REG_WRITE(ah, AR_KEYTABLE_TYPE(entry), keyType);
  271. REGWRITE_BUFFER_FLUSH(ah);
  272. /* Write MAC address for the entry */
  273. (void) ath_hw_keysetmac(common, entry, mac);
  274. }
  275. return true;
  276. }
  277. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  278. struct ath_keyval *hk, const u8 *addr,
  279. bool authenticator)
  280. {
  281. const u8 *key_rxmic;
  282. const u8 *key_txmic;
  283. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  284. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  285. if (addr == NULL) {
  286. /*
  287. * Group key installation - only two key cache entries are used
  288. * regardless of splitmic capability since group key is only
  289. * used either for TX or RX.
  290. */
  291. if (authenticator) {
  292. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  293. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  294. } else {
  295. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  296. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  297. }
  298. return ath_hw_set_keycache_entry(common, keyix, hk, addr);
  299. }
  300. if (common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) {
  301. /* TX and RX keys share the same key cache entry. */
  302. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  303. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  304. return ath_hw_set_keycache_entry(common, keyix, hk, addr);
  305. }
  306. /* Separate key cache entries for TX and RX */
  307. /* TX key goes at first index, RX key at +32. */
  308. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  309. if (!ath_hw_set_keycache_entry(common, keyix, hk, NULL)) {
  310. /* TX MIC entry failed. No need to proceed further */
  311. ath_err(common, "Setting TX MIC Key Failed\n");
  312. return 0;
  313. }
  314. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  315. /* XXX delete tx key on failure? */
  316. return ath_hw_set_keycache_entry(common, keyix + 32, hk, addr);
  317. }
  318. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  319. {
  320. int i;
  321. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  322. if (test_bit(i, common->keymap) ||
  323. test_bit(i + 64, common->keymap))
  324. continue; /* At least one part of TKIP key allocated */
  325. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED) &&
  326. (test_bit(i + 32, common->keymap) ||
  327. test_bit(i + 64 + 32, common->keymap)))
  328. continue; /* At least one part of TKIP key allocated */
  329. /* Found a free slot for a TKIP key */
  330. return i;
  331. }
  332. return -1;
  333. }
  334. static int ath_reserve_key_cache_slot(struct ath_common *common,
  335. u32 cipher)
  336. {
  337. int i;
  338. if (cipher == WLAN_CIPHER_SUITE_TKIP)
  339. return ath_reserve_key_cache_slot_tkip(common);
  340. /* First, try to find slots that would not be available for TKIP. */
  341. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  342. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  343. if (!test_bit(i, common->keymap) &&
  344. (test_bit(i + 32, common->keymap) ||
  345. test_bit(i + 64, common->keymap) ||
  346. test_bit(i + 64 + 32, common->keymap)))
  347. return i;
  348. if (!test_bit(i + 32, common->keymap) &&
  349. (test_bit(i, common->keymap) ||
  350. test_bit(i + 64, common->keymap) ||
  351. test_bit(i + 64 + 32, common->keymap)))
  352. return i + 32;
  353. if (!test_bit(i + 64, common->keymap) &&
  354. (test_bit(i , common->keymap) ||
  355. test_bit(i + 32, common->keymap) ||
  356. test_bit(i + 64 + 32, common->keymap)))
  357. return i + 64;
  358. if (!test_bit(i + 64 + 32, common->keymap) &&
  359. (test_bit(i, common->keymap) ||
  360. test_bit(i + 32, common->keymap) ||
  361. test_bit(i + 64, common->keymap)))
  362. return i + 64 + 32;
  363. }
  364. } else {
  365. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  366. if (!test_bit(i, common->keymap) &&
  367. test_bit(i + 64, common->keymap))
  368. return i;
  369. if (test_bit(i, common->keymap) &&
  370. !test_bit(i + 64, common->keymap))
  371. return i + 64;
  372. }
  373. }
  374. /* No partially used TKIP slots, pick any available slot */
  375. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  376. /* Do not allow slots that could be needed for TKIP group keys
  377. * to be used. This limitation could be removed if we know that
  378. * TKIP will not be used. */
  379. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  380. continue;
  381. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  382. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  383. continue;
  384. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  385. continue;
  386. }
  387. if (!test_bit(i, common->keymap))
  388. return i; /* Found a free slot for a key */
  389. }
  390. /* No free slot found */
  391. return -1;
  392. }
  393. /*
  394. * Configure encryption in the HW.
  395. */
  396. int ath_key_config(struct ath_common *common,
  397. struct ieee80211_vif *vif,
  398. struct ieee80211_sta *sta,
  399. struct ieee80211_key_conf *key)
  400. {
  401. struct ath_keyval hk;
  402. const u8 *mac = NULL;
  403. u8 gmac[ETH_ALEN];
  404. int ret = 0;
  405. int idx;
  406. memset(&hk, 0, sizeof(hk));
  407. switch (key->cipher) {
  408. case 0:
  409. hk.kv_type = ATH_CIPHER_CLR;
  410. break;
  411. case WLAN_CIPHER_SUITE_WEP40:
  412. case WLAN_CIPHER_SUITE_WEP104:
  413. hk.kv_type = ATH_CIPHER_WEP;
  414. break;
  415. case WLAN_CIPHER_SUITE_TKIP:
  416. hk.kv_type = ATH_CIPHER_TKIP;
  417. break;
  418. case WLAN_CIPHER_SUITE_CCMP:
  419. hk.kv_type = ATH_CIPHER_AES_CCM;
  420. break;
  421. default:
  422. return -EOPNOTSUPP;
  423. }
  424. hk.kv_len = key->keylen;
  425. if (key->keylen)
  426. memcpy(hk.kv_val, key->key, key->keylen);
  427. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  428. switch (vif->type) {
  429. case NL80211_IFTYPE_AP:
  430. memcpy(gmac, vif->addr, ETH_ALEN);
  431. gmac[0] |= 0x01;
  432. mac = gmac;
  433. idx = ath_reserve_key_cache_slot(common, key->cipher);
  434. break;
  435. case NL80211_IFTYPE_ADHOC:
  436. if (!sta) {
  437. idx = key->keyidx;
  438. break;
  439. }
  440. memcpy(gmac, sta->addr, ETH_ALEN);
  441. gmac[0] |= 0x01;
  442. mac = gmac;
  443. idx = ath_reserve_key_cache_slot(common, key->cipher);
  444. break;
  445. default:
  446. idx = key->keyidx;
  447. break;
  448. }
  449. } else if (key->keyidx) {
  450. if (WARN_ON(!sta))
  451. return -EOPNOTSUPP;
  452. mac = sta->addr;
  453. if (vif->type != NL80211_IFTYPE_AP) {
  454. /* Only keyidx 0 should be used with unicast key, but
  455. * allow this for client mode for now. */
  456. idx = key->keyidx;
  457. } else
  458. return -EIO;
  459. } else {
  460. if (WARN_ON(!sta))
  461. return -EOPNOTSUPP;
  462. mac = sta->addr;
  463. idx = ath_reserve_key_cache_slot(common, key->cipher);
  464. }
  465. if (idx < 0)
  466. return -ENOSPC; /* no free key cache entries */
  467. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  468. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  469. vif->type == NL80211_IFTYPE_AP);
  470. else
  471. ret = ath_hw_set_keycache_entry(common, idx, &hk, mac);
  472. if (!ret)
  473. return -EIO;
  474. set_bit(idx, common->keymap);
  475. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  476. set_bit(idx + 64, common->keymap);
  477. set_bit(idx, common->tkip_keymap);
  478. set_bit(idx + 64, common->tkip_keymap);
  479. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  480. set_bit(idx + 32, common->keymap);
  481. set_bit(idx + 64 + 32, common->keymap);
  482. set_bit(idx + 32, common->tkip_keymap);
  483. set_bit(idx + 64 + 32, common->tkip_keymap);
  484. }
  485. }
  486. return idx;
  487. }
  488. EXPORT_SYMBOL(ath_key_config);
  489. /*
  490. * Delete Key.
  491. */
  492. void ath_key_delete(struct ath_common *common, struct ieee80211_key_conf *key)
  493. {
  494. ath_hw_keyreset(common, key->hw_key_idx);
  495. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  496. return;
  497. clear_bit(key->hw_key_idx, common->keymap);
  498. if (key->cipher != WLAN_CIPHER_SUITE_TKIP)
  499. return;
  500. clear_bit(key->hw_key_idx + 64, common->keymap);
  501. clear_bit(key->hw_key_idx, common->tkip_keymap);
  502. clear_bit(key->hw_key_idx + 64, common->tkip_keymap);
  503. if (!(common->crypt_caps & ATH_CRYPT_CAP_MIC_COMBINED)) {
  504. ath_hw_keyreset(common, key->hw_key_idx + 32);
  505. clear_bit(key->hw_key_idx + 32, common->keymap);
  506. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  507. clear_bit(key->hw_key_idx + 32, common->tkip_keymap);
  508. clear_bit(key->hw_key_idx + 64 + 32, common->tkip_keymap);
  509. }
  510. }
  511. EXPORT_SYMBOL(ath_key_delete);