common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. rxs->flag |= RX_FLAG_DECRYPTED;
  229. } else if (ieee80211_has_protected(fc)
  230. && !decrypt_error && skb->len >= hdrlen + 4) {
  231. keyix = skb->data[hdrlen + 3] >> 6;
  232. if (test_bit(keyix, common->keymap))
  233. rxs->flag |= RX_FLAG_DECRYPTED;
  234. }
  235. if (ah->sw_mgmt_crypto &&
  236. (rxs->flag & RX_FLAG_DECRYPTED) &&
  237. ieee80211_is_mgmt(fc))
  238. /* Use software decrypt for management frames. */
  239. rxs->flag &= ~RX_FLAG_DECRYPTED;
  240. }
  241. EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
  242. int ath9k_cmn_padpos(__le16 frame_control)
  243. {
  244. int padpos = 24;
  245. if (ieee80211_has_a4(frame_control)) {
  246. padpos += ETH_ALEN;
  247. }
  248. if (ieee80211_is_data_qos(frame_control)) {
  249. padpos += IEEE80211_QOS_CTL_LEN;
  250. }
  251. return padpos;
  252. }
  253. EXPORT_SYMBOL(ath9k_cmn_padpos);
  254. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  255. {
  256. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  257. if (tx_info->control.hw_key) {
  258. if (tx_info->control.hw_key->alg == ALG_WEP)
  259. return ATH9K_KEY_TYPE_WEP;
  260. else if (tx_info->control.hw_key->alg == ALG_TKIP)
  261. return ATH9K_KEY_TYPE_TKIP;
  262. else if (tx_info->control.hw_key->alg == ALG_CCMP)
  263. return ATH9K_KEY_TYPE_AES;
  264. }
  265. return ATH9K_KEY_TYPE_CLEAR;
  266. }
  267. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  268. /*
  269. * Calculate the RX filter to be set in the HW.
  270. */
  271. u32 ath9k_cmn_calcrxfilter(struct ieee80211_hw *hw, struct ath_hw *ah,
  272. unsigned int rxfilter)
  273. {
  274. #define RX_FILTER_PRESERVE (ATH9K_RX_FILTER_PHYERR | ATH9K_RX_FILTER_PHYRADAR)
  275. u32 rfilt;
  276. rfilt = (ath9k_hw_getrxfilter(ah) & RX_FILTER_PRESERVE)
  277. | ATH9K_RX_FILTER_UCAST | ATH9K_RX_FILTER_BCAST
  278. | ATH9K_RX_FILTER_MCAST;
  279. /* If not a STA, enable processing of Probe Requests */
  280. if (ah->opmode != NL80211_IFTYPE_STATION)
  281. rfilt |= ATH9K_RX_FILTER_PROBEREQ;
  282. /*
  283. * Set promiscuous mode when FIF_PROMISC_IN_BSS is enabled for station
  284. * mode interface or when in monitor mode. AP mode does not need this
  285. * since it receives all in-BSS frames anyway.
  286. */
  287. if (((ah->opmode != NL80211_IFTYPE_AP) &&
  288. (rxfilter & FIF_PROMISC_IN_BSS)) ||
  289. (ah->opmode == NL80211_IFTYPE_MONITOR))
  290. rfilt |= ATH9K_RX_FILTER_PROM;
  291. if (rxfilter & FIF_CONTROL)
  292. rfilt |= ATH9K_RX_FILTER_CONTROL;
  293. if ((ah->opmode == NL80211_IFTYPE_STATION) &&
  294. !(rxfilter & FIF_BCN_PRBRESP_PROMISC))
  295. rfilt |= ATH9K_RX_FILTER_MYBEACON;
  296. else
  297. rfilt |= ATH9K_RX_FILTER_BEACON;
  298. if ((AR_SREV_9280_10_OR_LATER(ah) ||
  299. AR_SREV_9285_10_OR_LATER(ah)) &&
  300. (ah->opmode == NL80211_IFTYPE_AP) &&
  301. (rxfilter & FIF_PSPOLL))
  302. rfilt |= ATH9K_RX_FILTER_PSPOLL;
  303. if (conf_is_ht(&hw->conf))
  304. rfilt |= ATH9K_RX_FILTER_COMP_BAR;
  305. return rfilt;
  306. #undef RX_FILTER_PRESERVE
  307. }
  308. EXPORT_SYMBOL(ath9k_cmn_calcrxfilter);
  309. /*
  310. * Recv initialization for opmode change.
  311. */
  312. void ath9k_cmn_opmode_init(struct ieee80211_hw *hw, struct ath_hw *ah,
  313. unsigned int rxfilter)
  314. {
  315. struct ath_common *common = ath9k_hw_common(ah);
  316. u32 rfilt, mfilt[2];
  317. /* configure rx filter */
  318. rfilt = ath9k_cmn_calcrxfilter(hw, ah, rxfilter);
  319. ath9k_hw_setrxfilter(ah, rfilt);
  320. /* configure bssid mask */
  321. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  322. ath_hw_setbssidmask(common);
  323. /* configure operational mode */
  324. ath9k_hw_setopmode(ah);
  325. /* Handle any link-level address change. */
  326. ath9k_hw_setmac(ah, common->macaddr);
  327. /* calculate and install multicast filter */
  328. mfilt[0] = mfilt[1] = ~0;
  329. ath9k_hw_setmcastfilter(ah, mfilt[0], mfilt[1]);
  330. }
  331. EXPORT_SYMBOL(ath9k_cmn_opmode_init);
  332. static u32 ath9k_get_extchanmode(struct ieee80211_channel *chan,
  333. enum nl80211_channel_type channel_type)
  334. {
  335. u32 chanmode = 0;
  336. switch (chan->band) {
  337. case IEEE80211_BAND_2GHZ:
  338. switch (channel_type) {
  339. case NL80211_CHAN_NO_HT:
  340. case NL80211_CHAN_HT20:
  341. chanmode = CHANNEL_G_HT20;
  342. break;
  343. case NL80211_CHAN_HT40PLUS:
  344. chanmode = CHANNEL_G_HT40PLUS;
  345. break;
  346. case NL80211_CHAN_HT40MINUS:
  347. chanmode = CHANNEL_G_HT40MINUS;
  348. break;
  349. }
  350. break;
  351. case IEEE80211_BAND_5GHZ:
  352. switch (channel_type) {
  353. case NL80211_CHAN_NO_HT:
  354. case NL80211_CHAN_HT20:
  355. chanmode = CHANNEL_A_HT20;
  356. break;
  357. case NL80211_CHAN_HT40PLUS:
  358. chanmode = CHANNEL_A_HT40PLUS;
  359. break;
  360. case NL80211_CHAN_HT40MINUS:
  361. chanmode = CHANNEL_A_HT40MINUS;
  362. break;
  363. }
  364. break;
  365. default:
  366. break;
  367. }
  368. return chanmode;
  369. }
  370. /*
  371. * Update internal channel flags.
  372. */
  373. void ath9k_cmn_update_ichannel(struct ieee80211_hw *hw,
  374. struct ath9k_channel *ichan)
  375. {
  376. struct ieee80211_channel *chan = hw->conf.channel;
  377. struct ieee80211_conf *conf = &hw->conf;
  378. ichan->channel = chan->center_freq;
  379. ichan->chan = chan;
  380. if (chan->band == IEEE80211_BAND_2GHZ) {
  381. ichan->chanmode = CHANNEL_G;
  382. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  383. } else {
  384. ichan->chanmode = CHANNEL_A;
  385. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  386. }
  387. if (conf_is_ht(conf))
  388. ichan->chanmode = ath9k_get_extchanmode(chan,
  389. conf->channel_type);
  390. }
  391. EXPORT_SYMBOL(ath9k_cmn_update_ichannel);
  392. /*
  393. * Get the internal channel reference.
  394. */
  395. struct ath9k_channel *ath9k_cmn_get_curchannel(struct ieee80211_hw *hw,
  396. struct ath_hw *ah)
  397. {
  398. struct ieee80211_channel *curchan = hw->conf.channel;
  399. struct ath9k_channel *channel;
  400. u8 chan_idx;
  401. chan_idx = curchan->hw_value;
  402. channel = &ah->channels[chan_idx];
  403. ath9k_cmn_update_ichannel(hw, channel);
  404. return channel;
  405. }
  406. EXPORT_SYMBOL(ath9k_cmn_get_curchannel);
  407. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  408. struct ath9k_keyval *hk, const u8 *addr,
  409. bool authenticator)
  410. {
  411. struct ath_hw *ah = common->ah;
  412. const u8 *key_rxmic;
  413. const u8 *key_txmic;
  414. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  415. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  416. if (addr == NULL) {
  417. /*
  418. * Group key installation - only two key cache entries are used
  419. * regardless of splitmic capability since group key is only
  420. * used either for TX or RX.
  421. */
  422. if (authenticator) {
  423. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  424. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  425. } else {
  426. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  427. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  428. }
  429. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  430. }
  431. if (!common->splitmic) {
  432. /* TX and RX keys share the same key cache entry. */
  433. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  434. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  435. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  436. }
  437. /* Separate key cache entries for TX and RX */
  438. /* TX key goes at first index, RX key at +32. */
  439. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  440. if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
  441. /* TX MIC entry failed. No need to proceed further */
  442. ath_print(common, ATH_DBG_FATAL,
  443. "Setting TX MIC Key Failed\n");
  444. return 0;
  445. }
  446. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  447. /* XXX delete tx key on failure? */
  448. return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
  449. }
  450. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  451. {
  452. int i;
  453. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  454. if (test_bit(i, common->keymap) ||
  455. test_bit(i + 64, common->keymap))
  456. continue; /* At least one part of TKIP key allocated */
  457. if (common->splitmic &&
  458. (test_bit(i + 32, common->keymap) ||
  459. test_bit(i + 64 + 32, common->keymap)))
  460. continue; /* At least one part of TKIP key allocated */
  461. /* Found a free slot for a TKIP key */
  462. return i;
  463. }
  464. return -1;
  465. }
  466. static int ath_reserve_key_cache_slot(struct ath_common *common)
  467. {
  468. int i;
  469. /* First, try to find slots that would not be available for TKIP. */
  470. if (common->splitmic) {
  471. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  472. if (!test_bit(i, common->keymap) &&
  473. (test_bit(i + 32, common->keymap) ||
  474. test_bit(i + 64, common->keymap) ||
  475. test_bit(i + 64 + 32, common->keymap)))
  476. return i;
  477. if (!test_bit(i + 32, common->keymap) &&
  478. (test_bit(i, common->keymap) ||
  479. test_bit(i + 64, common->keymap) ||
  480. test_bit(i + 64 + 32, common->keymap)))
  481. return i + 32;
  482. if (!test_bit(i + 64, common->keymap) &&
  483. (test_bit(i , common->keymap) ||
  484. test_bit(i + 32, common->keymap) ||
  485. test_bit(i + 64 + 32, common->keymap)))
  486. return i + 64;
  487. if (!test_bit(i + 64 + 32, common->keymap) &&
  488. (test_bit(i, common->keymap) ||
  489. test_bit(i + 32, common->keymap) ||
  490. test_bit(i + 64, common->keymap)))
  491. return i + 64 + 32;
  492. }
  493. } else {
  494. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  495. if (!test_bit(i, common->keymap) &&
  496. test_bit(i + 64, common->keymap))
  497. return i;
  498. if (test_bit(i, common->keymap) &&
  499. !test_bit(i + 64, common->keymap))
  500. return i + 64;
  501. }
  502. }
  503. /* No partially used TKIP slots, pick any available slot */
  504. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  505. /* Do not allow slots that could be needed for TKIP group keys
  506. * to be used. This limitation could be removed if we know that
  507. * TKIP will not be used. */
  508. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  509. continue;
  510. if (common->splitmic) {
  511. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  512. continue;
  513. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  514. continue;
  515. }
  516. if (!test_bit(i, common->keymap))
  517. return i; /* Found a free slot for a key */
  518. }
  519. /* No free slot found */
  520. return -1;
  521. }
  522. /*
  523. * Configure encryption in the HW.
  524. */
  525. int ath9k_cmn_key_config(struct ath_common *common,
  526. struct ieee80211_vif *vif,
  527. struct ieee80211_sta *sta,
  528. struct ieee80211_key_conf *key)
  529. {
  530. struct ath_hw *ah = common->ah;
  531. struct ath9k_keyval hk;
  532. const u8 *mac = NULL;
  533. int ret = 0;
  534. int idx;
  535. memset(&hk, 0, sizeof(hk));
  536. switch (key->alg) {
  537. case ALG_WEP:
  538. hk.kv_type = ATH9K_CIPHER_WEP;
  539. break;
  540. case ALG_TKIP:
  541. hk.kv_type = ATH9K_CIPHER_TKIP;
  542. break;
  543. case ALG_CCMP:
  544. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  545. break;
  546. default:
  547. return -EOPNOTSUPP;
  548. }
  549. hk.kv_len = key->keylen;
  550. memcpy(hk.kv_val, key->key, key->keylen);
  551. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  552. /* For now, use the default keys for broadcast keys. This may
  553. * need to change with virtual interfaces. */
  554. idx = key->keyidx;
  555. } else if (key->keyidx) {
  556. if (WARN_ON(!sta))
  557. return -EOPNOTSUPP;
  558. mac = sta->addr;
  559. if (vif->type != NL80211_IFTYPE_AP) {
  560. /* Only keyidx 0 should be used with unicast key, but
  561. * allow this for client mode for now. */
  562. idx = key->keyidx;
  563. } else
  564. return -EIO;
  565. } else {
  566. if (WARN_ON(!sta))
  567. return -EOPNOTSUPP;
  568. mac = sta->addr;
  569. if (key->alg == ALG_TKIP)
  570. idx = ath_reserve_key_cache_slot_tkip(common);
  571. else
  572. idx = ath_reserve_key_cache_slot(common);
  573. if (idx < 0)
  574. return -ENOSPC; /* no free key cache entries */
  575. }
  576. if (key->alg == ALG_TKIP)
  577. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  578. vif->type == NL80211_IFTYPE_AP);
  579. else
  580. ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
  581. if (!ret)
  582. return -EIO;
  583. set_bit(idx, common->keymap);
  584. if (key->alg == ALG_TKIP) {
  585. set_bit(idx + 64, common->keymap);
  586. if (common->splitmic) {
  587. set_bit(idx + 32, common->keymap);
  588. set_bit(idx + 64 + 32, common->keymap);
  589. }
  590. }
  591. return idx;
  592. }
  593. EXPORT_SYMBOL(ath9k_cmn_key_config);
  594. /*
  595. * Delete Key.
  596. */
  597. void ath9k_cmn_key_delete(struct ath_common *common,
  598. struct ieee80211_key_conf *key)
  599. {
  600. struct ath_hw *ah = common->ah;
  601. ath9k_hw_keyreset(ah, key->hw_key_idx);
  602. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  603. return;
  604. clear_bit(key->hw_key_idx, common->keymap);
  605. if (key->alg != ALG_TKIP)
  606. return;
  607. clear_bit(key->hw_key_idx + 64, common->keymap);
  608. if (common->splitmic) {
  609. ath9k_hw_keyreset(ah, key->hw_key_idx + 32);
  610. clear_bit(key->hw_key_idx + 32, common->keymap);
  611. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  612. }
  613. }
  614. EXPORT_SYMBOL(ath9k_cmn_key_delete);
  615. static int __init ath9k_cmn_init(void)
  616. {
  617. return 0;
  618. }
  619. module_init(ath9k_cmn_init);
  620. static void __exit ath9k_cmn_exit(void)
  621. {
  622. return;
  623. }
  624. module_exit(ath9k_cmn_exit);