common.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. /* Common RX processing */
  26. /* Assumes you've already done the endian to CPU conversion */
  27. static bool ath9k_rx_accept(struct ath_common *common,
  28. struct sk_buff *skb,
  29. struct ieee80211_rx_status *rxs,
  30. struct ath_rx_status *rx_stats,
  31. bool *decrypt_error)
  32. {
  33. struct ath_hw *ah = common->ah;
  34. struct ieee80211_hdr *hdr;
  35. __le16 fc;
  36. hdr = (struct ieee80211_hdr *) skb->data;
  37. fc = hdr->frame_control;
  38. if (!rx_stats->rs_datalen)
  39. return false;
  40. /*
  41. * rs_status follows rs_datalen so if rs_datalen is too large
  42. * we can take a hint that hardware corrupted it, so ignore
  43. * those frames.
  44. */
  45. if (rx_stats->rs_datalen > common->rx_bufsize)
  46. return false;
  47. /*
  48. * rs_more indicates chained descriptors which can be used
  49. * to link buffers together for a sort of scatter-gather
  50. * operation.
  51. *
  52. * The rx_stats->rs_status will not be set until the end of the
  53. * chained descriptors so it can be ignored if rs_more is set. The
  54. * rs_more will be false at the last element of the chained
  55. * descriptors.
  56. */
  57. if (!rx_stats->rs_more && rx_stats->rs_status != 0) {
  58. if (rx_stats->rs_status & ATH9K_RXERR_CRC)
  59. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  60. if (rx_stats->rs_status & ATH9K_RXERR_PHY)
  61. return false;
  62. if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
  63. *decrypt_error = true;
  64. } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
  65. if (ieee80211_is_ctl(fc))
  66. /*
  67. * Sometimes, we get invalid
  68. * MIC failures on valid control frames.
  69. * Remove these mic errors.
  70. */
  71. rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
  72. else
  73. rxs->flag |= RX_FLAG_MMIC_ERROR;
  74. }
  75. /*
  76. * Reject error frames with the exception of
  77. * decryption and MIC failures. For monitor mode,
  78. * we also ignore the CRC error.
  79. */
  80. if (ah->opmode == NL80211_IFTYPE_MONITOR) {
  81. if (rx_stats->rs_status &
  82. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  83. ATH9K_RXERR_CRC))
  84. return false;
  85. } else {
  86. if (rx_stats->rs_status &
  87. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  88. return false;
  89. }
  90. }
  91. }
  92. return true;
  93. }
  94. static u8 ath9k_process_rate(struct ath_common *common,
  95. struct ieee80211_hw *hw,
  96. struct ath_rx_status *rx_stats,
  97. struct ieee80211_rx_status *rxs,
  98. struct sk_buff *skb)
  99. {
  100. struct ieee80211_supported_band *sband;
  101. enum ieee80211_band band;
  102. unsigned int i = 0;
  103. band = hw->conf.channel->band;
  104. sband = hw->wiphy->bands[band];
  105. if (rx_stats->rs_rate & 0x80) {
  106. /* HT rate */
  107. rxs->flag |= RX_FLAG_HT;
  108. if (rx_stats->rs_flags & ATH9K_RX_2040)
  109. rxs->flag |= RX_FLAG_40MHZ;
  110. if (rx_stats->rs_flags & ATH9K_RX_GI)
  111. rxs->flag |= RX_FLAG_SHORT_GI;
  112. return rx_stats->rs_rate & 0x7f;
  113. }
  114. for (i = 0; i < sband->n_bitrates; i++) {
  115. if (sband->bitrates[i].hw_value == rx_stats->rs_rate)
  116. return i;
  117. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  118. rxs->flag |= RX_FLAG_SHORTPRE;
  119. return i;
  120. }
  121. }
  122. /* No valid hardware bitrate found -- we should not get here */
  123. ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
  124. "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
  125. if ((common->debug_mask & ATH_DBG_XMIT))
  126. print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
  127. return 0;
  128. }
  129. static void ath9k_process_rssi(struct ath_common *common,
  130. struct ieee80211_hw *hw,
  131. struct sk_buff *skb,
  132. struct ath_rx_status *rx_stats)
  133. {
  134. struct ath_hw *ah = common->ah;
  135. struct ieee80211_sta *sta;
  136. struct ieee80211_hdr *hdr;
  137. struct ath_node *an;
  138. int last_rssi = ATH_RSSI_DUMMY_MARKER;
  139. __le16 fc;
  140. hdr = (struct ieee80211_hdr *)skb->data;
  141. fc = hdr->frame_control;
  142. rcu_read_lock();
  143. /*
  144. * XXX: use ieee80211_find_sta! This requires quite a bit of work
  145. * under the current ath9k virtual wiphy implementation as we have
  146. * no way of tying a vif to wiphy. Typically vifs are attached to
  147. * at least one sdata of a wiphy on mac80211 but with ath9k virtual
  148. * wiphy you'd have to iterate over every wiphy and each sdata.
  149. */
  150. sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
  151. if (sta) {
  152. an = (struct ath_node *) sta->drv_priv;
  153. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
  154. !rx_stats->rs_moreaggr)
  155. ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
  156. last_rssi = an->last_rssi;
  157. }
  158. rcu_read_unlock();
  159. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  160. rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
  161. ATH_RSSI_EP_MULTIPLIER);
  162. if (rx_stats->rs_rssi < 0)
  163. rx_stats->rs_rssi = 0;
  164. /* Update Beacon RSSI, this is used by ANI. */
  165. if (ieee80211_is_beacon(fc))
  166. ah->stats.avgbrssi = rx_stats->rs_rssi;
  167. }
  168. /*
  169. * For Decrypt or Demic errors, we only mark packet status here and always push
  170. * up the frame up to let mac80211 handle the actual error case, be it no
  171. * decryption key or real decryption error. This let us keep statistics there.
  172. */
  173. int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
  174. struct ieee80211_hw *hw,
  175. struct sk_buff *skb,
  176. struct ath_rx_status *rx_stats,
  177. struct ieee80211_rx_status *rx_status,
  178. bool *decrypt_error)
  179. {
  180. struct ath_hw *ah = common->ah;
  181. memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
  182. if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
  183. return -EINVAL;
  184. ath9k_process_rssi(common, hw, skb, rx_stats);
  185. rx_status->rate_idx = ath9k_process_rate(common, hw,
  186. rx_stats, rx_status, skb);
  187. rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
  188. rx_status->band = hw->conf.channel->band;
  189. rx_status->freq = hw->conf.channel->center_freq;
  190. rx_status->noise = common->ani.noise_floor;
  191. rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
  192. rx_status->antenna = rx_stats->rs_antenna;
  193. rx_status->flag |= RX_FLAG_TSFT;
  194. return 0;
  195. }
  196. EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
  197. void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
  198. struct sk_buff *skb,
  199. struct ath_rx_status *rx_stats,
  200. struct ieee80211_rx_status *rxs,
  201. bool decrypt_error)
  202. {
  203. struct ath_hw *ah = common->ah;
  204. struct ieee80211_hdr *hdr;
  205. int hdrlen, padpos, padsize;
  206. u8 keyix;
  207. __le16 fc;
  208. /* see if any padding is done by the hw and remove it */
  209. hdr = (struct ieee80211_hdr *) skb->data;
  210. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  211. fc = hdr->frame_control;
  212. padpos = ath9k_cmn_padpos(hdr->frame_control);
  213. /* The MAC header is padded to have 32-bit boundary if the
  214. * packet payload is non-zero. The general calculation for
  215. * padsize would take into account odd header lengths:
  216. * padsize = (4 - padpos % 4) % 4; However, since only
  217. * even-length headers are used, padding can only be 0 or 2
  218. * bytes and we can optimize this a bit. In addition, we must
  219. * not try to remove padding from short control frames that do
  220. * not have payload. */
  221. padsize = padpos & 3;
  222. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  223. memmove(skb->data + padsize, skb->data, padpos);
  224. skb_pull(skb, padsize);
  225. }
  226. keyix = rx_stats->rs_keyix;
  227. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  228. ieee80211_has_protected(fc)) {
  229. rxs->flag |= RX_FLAG_DECRYPTED;
  230. } else if (ieee80211_has_protected(fc)
  231. && !decrypt_error && skb->len >= hdrlen + 4) {
  232. keyix = skb->data[hdrlen + 3] >> 6;
  233. if (test_bit(keyix, common->keymap))
  234. rxs->flag |= RX_FLAG_DECRYPTED;
  235. }
  236. if (ah->sw_mgmt_crypto &&
  237. (rxs->flag & RX_FLAG_DECRYPTED) &&
  238. ieee80211_is_mgmt(fc))
  239. /* Use software decrypt for management frames. */
  240. rxs->flag &= ~RX_FLAG_DECRYPTED;
  241. }
  242. EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
  243. int ath9k_cmn_padpos(__le16 frame_control)
  244. {
  245. int padpos = 24;
  246. if (ieee80211_has_a4(frame_control)) {
  247. padpos += ETH_ALEN;
  248. }
  249. if (ieee80211_is_data_qos(frame_control)) {
  250. padpos += IEEE80211_QOS_CTL_LEN;
  251. }
  252. return padpos;
  253. }
  254. EXPORT_SYMBOL(ath9k_cmn_padpos);
  255. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  256. {
  257. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  258. if (tx_info->control.hw_key) {
  259. if (tx_info->control.hw_key->alg == ALG_WEP)
  260. return ATH9K_KEY_TYPE_WEP;
  261. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  262. return ATH9K_KEY_TYPE_TKIP;
  263. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  264. return ATH9K_KEY_TYPE_AES;
  265. }
  266. return ATH9K_KEY_TYPE_CLEAR;
  267. }
  268. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  269. static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
  270. enum nl80211_channel_type channel_type)
  271. {
  272. u32 chanmode = 0;
  273. switch (chan->band) {
  274. case IEEE80211_BAND_2GHZ:
  275. switch (channel_type) {
  276. case NL80211_CHAN_NO_HT:
  277. case NL80211_CHAN_HT20:
  278. chanmode = CHANNEL_G_HT20;
  279. break;
  280. case NL80211_CHAN_HT40PLUS:
  281. chanmode = CHANNEL_G_HT40PLUS;
  282. break;
  283. case NL80211_CHAN_HT40MINUS:
  284. chanmode = CHANNEL_G_HT40MINUS;
  285. break;
  286. }
  287. break;
  288. case IEEE80211_BAND_5GHZ:
  289. switch (channel_type) {
  290. case NL80211_CHAN_NO_HT:
  291. case NL80211_CHAN_HT20:
  292. chanmode = CHANNEL_A_HT20;
  293. break;
  294. case NL80211_CHAN_HT40PLUS:
  295. chanmode = CHANNEL_A_HT40PLUS;
  296. break;
  297. case NL80211_CHAN_HT40MINUS:
  298. chanmode = CHANNEL_A_HT40MINUS;
  299. break;
  300. }
  301. break;
  302. default:
  303. break;
  304. }
  305. return chanmode;
  306. }
  307. /*
  308. * Update internal channel flags.
  309. */
  310. void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
  311. struct ath9k_channel *ichan)
  312. {
  313. struct ieee80211_channel *chan = hw->conf.channel;
  314. struct ieee80211_conf *conf = &hw->conf;
  315. ichan->channel = chan->center_freq;
  316. ichan->chan = chan;
  317. if (chan->band == IEEE80211_BAND_2GHZ) {
  318. ichan->chanmode = CHANNEL_G;
  319. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  320. } else {
  321. ichan->chanmode = CHANNEL_A;
  322. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  323. }
  324. if (conf_is_ht(conf))
  325. ichan->chanmode = ath9k_get_extchanmode(chan,
  326. conf->channel_type);
  327. }
  328. EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
  329. /*
  330. * Get the internal channel reference.
  331. */
  332. struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
  333. struct ath_hw *ah)
  334. {
  335. struct ieee80211_channel *curchan = hw->conf.channel;
  336. struct ath9k_channel *channel;
  337. u8 chan_idx;
  338. chan_idx = curchan->hw_value;
  339. channel = &ah->channels[chan_idx];
  340. ath9k_cmn_update_ichannel(hw, channel);
  341. return channel;
  342. }
  343. EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
  344. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  345. struct ath9k_keyval *hk, const u8 *addr,
  346. bool authenticator)
  347. {
  348. struct ath_hw *ah = common->ah;
  349. const u8 *key_rxmic;
  350. const u8 *key_txmic;
  351. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  352. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  353. if (addr == NULL) {
  354. /*
  355. * Group key installation - only two key cache entries are used
  356. * regardless of splitmic capability since group key is only
  357. * used either for TX or RX.
  358. */
  359. if (authenticator) {
  360. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  361. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  362. } else {
  363. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  364. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  365. }
  366. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  367. }
  368. if (!common->splitmic) {
  369. /* TX and RX keys share the same key cache entry. */
  370. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  371. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  372. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  373. }
  374. /* Separate key cache entries for TX and RX */
  375. /* TX key goes at first index, RX key at +32. */
  376. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  377. if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
  378. /* TX MIC entry failed. No need to proceed further */
  379. ath_print(common, ATH_DBG_FATAL,
  380. "Setting TX MIC Key Failed\n");
  381. return 0;
  382. }
  383. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  384. /* XXX delete tx key on failure? */
  385. return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
  386. }
  387. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  388. {
  389. int i;
  390. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  391. if (test_bit(i, common->keymap) ||
  392. test_bit(i + 64, common->keymap))
  393. continue; /* At least one part of TKIP key allocated */
  394. if (common->splitmic &&
  395. (test_bit(i + 32, common->keymap) ||
  396. test_bit(i + 64 + 32, common->keymap)))
  397. continue; /* At least one part of TKIP key allocated */
  398. /* Found a free slot for a TKIP key */
  399. return i;
  400. }
  401. return -1;
  402. }
  403. static int ath_reserve_key_cache_slot(struct ath_common *common)
  404. {
  405. int i;
  406. /* First, try to find slots that would not be available for TKIP. */
  407. if (common->splitmic) {
  408. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  409. if (!test_bit(i, common->keymap) &&
  410. (test_bit(i + 32, common->keymap) ||
  411. test_bit(i + 64, common->keymap) ||
  412. test_bit(i + 64 + 32, common->keymap)))
  413. return i;
  414. if (!test_bit(i + 32, common->keymap) &&
  415. (test_bit(i, common->keymap) ||
  416. test_bit(i + 64, common->keymap) ||
  417. test_bit(i + 64 + 32, common->keymap)))
  418. return i + 32;
  419. if (!test_bit(i + 64, common->keymap) &&
  420. (test_bit(i , common->keymap) ||
  421. test_bit(i + 32, common->keymap) ||
  422. test_bit(i + 64 + 32, common->keymap)))
  423. return i + 64;
  424. if (!test_bit(i + 64 + 32, common->keymap) &&
  425. (test_bit(i, common->keymap) ||
  426. test_bit(i + 32, common->keymap) ||
  427. test_bit(i + 64, common->keymap)))
  428. return i + 64 + 32;
  429. }
  430. } else {
  431. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  432. if (!test_bit(i, common->keymap) &&
  433. test_bit(i + 64, common->keymap))
  434. return i;
  435. if (test_bit(i, common->keymap) &&
  436. !test_bit(i + 64, common->keymap))
  437. return i + 64;
  438. }
  439. }
  440. /* No partially used TKIP slots, pick any available slot */
  441. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  442. /* Do not allow slots that could be needed for TKIP group keys
  443. * to be used. This limitation could be removed if we know that
  444. * TKIP will not be used. */
  445. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  446. continue;
  447. if (common->splitmic) {
  448. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  449. continue;
  450. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  451. continue;
  452. }
  453. if (!test_bit(i, common->keymap))
  454. return i; /* Found a free slot for a key */
  455. }
  456. /* No free slot found */
  457. return -1;
  458. }
  459. /*
  460. * Configure encryption in the HW.
  461. */
  462. int ath9k_cmn_key_config(struct ath_common *common,
  463. struct ieee80211_vif *vif,
  464. struct ieee80211_sta *sta,
  465. struct ieee80211_key_conf *key)
  466. {
  467. struct ath_hw *ah = common->ah;
  468. struct ath9k_keyval hk;
  469. const u8 *mac = NULL;
  470. int ret = 0;
  471. int idx;
  472. memset(&hk, 0, sizeof(hk));
  473. switch (key->alg) {
  474. case ALG_WEP:
  475. hk.kv_type = ATH9K_CIPHER_WEP;
  476. break;
  477. case ALG_TKIP:
  478. hk.kv_type = ATH9K_CIPHER_TKIP;
  479. break;
  480. case ALG_CCMP:
  481. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  482. break;
  483. default:
  484. return -EOPNOTSUPP;
  485. }
  486. hk.kv_len = key->keylen;
  487. memcpy(hk.kv_val, key->key, key->keylen);
  488. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  489. /* For now, use the default keys for broadcast keys. This may
  490. * need to change with virtual interfaces. */
  491. idx = key->keyidx;
  492. } else if (key->keyidx) {
  493. if (WARN_ON(!sta))
  494. return -EOPNOTSUPP;
  495. mac = sta->addr;
  496. if (vif->type != NL80211_IFTYPE_AP) {
  497. /* Only keyidx 0 should be used with unicast key, but
  498. * allow this for client mode for now. */
  499. idx = key->keyidx;
  500. } else
  501. return -EIO;
  502. } else {
  503. if (WARN_ON(!sta))
  504. return -EOPNOTSUPP;
  505. mac = sta->addr;
  506. if (key->alg == ALG_TKIP)
  507. idx = ath_reserve_key_cache_slot_tkip(common);
  508. else
  509. idx = ath_reserve_key_cache_slot(common);
  510. if (idx < 0)
  511. return -ENOSPC; /* no free key cache entries */
  512. }
  513. if (key->alg == ALG_TKIP)
  514. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  515. vif->type == NL80211_IFTYPE_AP);
  516. else
  517. ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
  518. if (!ret)
  519. return -EIO;
  520. set_bit(idx, common->keymap);
  521. if (key->alg == ALG_TKIP) {
  522. set_bit(idx + 64, common->keymap);
  523. if (common->splitmic) {
  524. set_bit(idx + 32, common->keymap);
  525. set_bit(idx + 64 + 32, common->keymap);
  526. }
  527. }
  528. return idx;
  529. }
  530. EXPORT_SYMBOL(ath9k_cmn_key_config);
  531. /*
  532. * Delete Key.
  533. */
  534. void ath9k_cmn_key_delete(struct ath_common *common,
  535. struct ieee80211_key_conf *key)
  536. {
  537. struct ath_hw *ah = common->ah;
  538. ath9k_hw_keyreset(ah, key->hw_key_idx);
  539. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  540. return;
  541. clear_bit(key->hw_key_idx, common->keymap);
  542. if (key->alg != ALG_TKIP)
  543. return;
  544. clear_bit(key->hw_key_idx + 64, common->keymap);
  545. if (common->splitmic) {
  546. ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
  547. clear_bit(key->hw_key_idx + 32, common->keymap);
  548. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  549. }
  550. }
  551. EXPORT_SYMBOL(ath9k_cmn_key_delete);
  552. static int __init ath9k_cmn_init(void)
  553. {
  554. return 0;
  555. }
  556. module_init(ath9k_cmn_init);
  557. static void __exit ath9k_cmn_exit(void)
  558. {
  559. return;
  560. }
  561. module_exit(ath9k_cmn_exit);