common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2009-2011 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. /* Assumes you've already done the endian to CPU conversion */
  26. bool ath9k_cmn_rx_accept(struct ath_common *common,
  27. struct ieee80211_hdr *hdr,
  28. struct ieee80211_rx_status *rxs,
  29. struct ath_rx_status *rx_stats,
  30. bool *decrypt_error,
  31. unsigned int rxfilter)
  32. {
  33. struct ath_hw *ah = common->ah;
  34. bool is_mc, is_valid_tkip, strip_mic, mic_error;
  35. __le16 fc;
  36. fc = hdr->frame_control;
  37. is_mc = !!is_multicast_ether_addr(hdr->addr1);
  38. is_valid_tkip = rx_stats->rs_keyix != ATH9K_RXKEYIX_INVALID &&
  39. test_bit(rx_stats->rs_keyix, common->tkip_keymap);
  40. strip_mic = is_valid_tkip && ieee80211_is_data(fc) &&
  41. ieee80211_has_protected(fc) &&
  42. !(rx_stats->rs_status &
  43. (ATH9K_RXERR_DECRYPT | ATH9K_RXERR_CRC | ATH9K_RXERR_MIC |
  44. ATH9K_RXERR_KEYMISS));
  45. /*
  46. * Key miss events are only relevant for pairwise keys where the
  47. * descriptor does contain a valid key index. This has been observed
  48. * mostly with CCMP encryption.
  49. */
  50. if (rx_stats->rs_keyix == ATH9K_RXKEYIX_INVALID ||
  51. !test_bit(rx_stats->rs_keyix, common->ccmp_keymap))
  52. rx_stats->rs_status &= ~ATH9K_RXERR_KEYMISS;
  53. mic_error = is_valid_tkip && !ieee80211_is_ctl(fc) &&
  54. !ieee80211_has_morefrags(fc) &&
  55. !(le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG) &&
  56. (rx_stats->rs_status & ATH9K_RXERR_MIC);
  57. /*
  58. * The rx_stats->rs_status will not be set until the end of the
  59. * chained descriptors so it can be ignored if rs_more is set. The
  60. * rs_more will be false at the last element of the chained
  61. * descriptors.
  62. */
  63. if (rx_stats->rs_status != 0) {
  64. u8 status_mask;
  65. if (rx_stats->rs_status & ATH9K_RXERR_CRC) {
  66. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  67. mic_error = false;
  68. }
  69. if ((rx_stats->rs_status & ATH9K_RXERR_DECRYPT) ||
  70. (!is_mc && (rx_stats->rs_status & ATH9K_RXERR_KEYMISS))) {
  71. *decrypt_error = true;
  72. mic_error = false;
  73. }
  74. /*
  75. * Reject error frames with the exception of
  76. * decryption and MIC failures. For monitor mode,
  77. * we also ignore the CRC error.
  78. */
  79. status_mask = ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  80. ATH9K_RXERR_KEYMISS;
  81. if (ah->is_monitoring && (rxfilter & FIF_FCSFAIL))
  82. status_mask |= ATH9K_RXERR_CRC;
  83. if (rx_stats->rs_status & ~status_mask)
  84. return false;
  85. }
  86. /*
  87. * For unicast frames the MIC error bit can have false positives,
  88. * so all MIC error reports need to be validated in software.
  89. * False negatives are not common, so skip software verification
  90. * if the hardware considers the MIC valid.
  91. */
  92. if (strip_mic)
  93. rxs->flag |= RX_FLAG_MMIC_STRIPPED;
  94. else if (is_mc && mic_error)
  95. rxs->flag |= RX_FLAG_MMIC_ERROR;
  96. return true;
  97. }
  98. EXPORT_SYMBOL(ath9k_cmn_rx_accept);
  99. void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
  100. struct sk_buff *skb,
  101. struct ath_rx_status *rx_stats,
  102. struct ieee80211_rx_status *rxs,
  103. bool decrypt_error)
  104. {
  105. struct ath_hw *ah = common->ah;
  106. struct ieee80211_hdr *hdr;
  107. int hdrlen, padpos, padsize;
  108. u8 keyix;
  109. __le16 fc;
  110. /* see if any padding is done by the hw and remove it */
  111. hdr = (struct ieee80211_hdr *) skb->data;
  112. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  113. fc = hdr->frame_control;
  114. padpos = ieee80211_hdrlen(fc);
  115. /* The MAC header is padded to have 32-bit boundary if the
  116. * packet payload is non-zero. The general calculation for
  117. * padsize would take into account odd header lengths:
  118. * padsize = (4 - padpos % 4) % 4; However, since only
  119. * even-length headers are used, padding can only be 0 or 2
  120. * bytes and we can optimize this a bit. In addition, we must
  121. * not try to remove padding from short control frames that do
  122. * not have payload. */
  123. padsize = padpos & 3;
  124. if (padsize && skb->len>=padpos+padsize+FCS_LEN) {
  125. memmove(skb->data + padsize, skb->data, padpos);
  126. skb_pull(skb, padsize);
  127. }
  128. keyix = rx_stats->rs_keyix;
  129. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error &&
  130. ieee80211_has_protected(fc)) {
  131. rxs->flag |= RX_FLAG_DECRYPTED;
  132. } else if (ieee80211_has_protected(fc)
  133. && !decrypt_error && skb->len >= hdrlen + 4) {
  134. keyix = skb->data[hdrlen + 3] >> 6;
  135. if (test_bit(keyix, common->keymap))
  136. rxs->flag |= RX_FLAG_DECRYPTED;
  137. }
  138. if (ah->sw_mgmt_crypto_rx &&
  139. (rxs->flag & RX_FLAG_DECRYPTED) &&
  140. ieee80211_is_mgmt(fc))
  141. /* Use software decrypt for management frames. */
  142. rxs->flag &= ~RX_FLAG_DECRYPTED;
  143. }
  144. EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
  145. int ath9k_cmn_process_rate(struct ath_common *common,
  146. struct ieee80211_hw *hw,
  147. struct ath_rx_status *rx_stats,
  148. struct ieee80211_rx_status *rxs)
  149. {
  150. struct ieee80211_supported_band *sband;
  151. enum nl80211_band band;
  152. unsigned int i = 0;
  153. struct ath_hw *ah = common->ah;
  154. band = ah->curchan->chan->band;
  155. sband = hw->wiphy->bands[band];
  156. if (IS_CHAN_QUARTER_RATE(ah->curchan))
  157. rxs->bw = RATE_INFO_BW_5;
  158. else if (IS_CHAN_HALF_RATE(ah->curchan))
  159. rxs->bw = RATE_INFO_BW_10;
  160. if (rx_stats->rs_rate & 0x80) {
  161. /* HT rate */
  162. rxs->encoding = RX_ENC_HT;
  163. rxs->enc_flags |= rx_stats->enc_flags;
  164. rxs->bw = rx_stats->bw;
  165. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  166. return 0;
  167. }
  168. for (i = 0; i < sband->n_bitrates; i++) {
  169. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  170. rxs->rate_idx = i;
  171. return 0;
  172. }
  173. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  174. rxs->enc_flags |= RX_ENC_FLAG_SHORTPRE;
  175. rxs->rate_idx = i;
  176. return 0;
  177. }
  178. }
  179. return -EINVAL;
  180. }
  181. EXPORT_SYMBOL(ath9k_cmn_process_rate);
  182. void ath9k_cmn_process_rssi(struct ath_common *common,
  183. struct ieee80211_hw *hw,
  184. struct ath_rx_status *rx_stats,
  185. struct ieee80211_rx_status *rxs)
  186. {
  187. struct ath_hw *ah = common->ah;
  188. int last_rssi;
  189. int rssi = rx_stats->rs_rssi;
  190. int i, j;
  191. /*
  192. * RSSI is not available for subframes in an A-MPDU.
  193. */
  194. if (rx_stats->rs_moreaggr) {
  195. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  196. return;
  197. }
  198. /*
  199. * Check if the RSSI for the last subframe in an A-MPDU
  200. * or an unaggregated frame is valid.
  201. */
  202. if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
  203. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  204. return;
  205. }
  206. for (i = 0, j = 0; i < ARRAY_SIZE(rx_stats->rs_rssi_ctl); i++) {
  207. s8 rssi;
  208. if (!(ah->rxchainmask & BIT(i)))
  209. continue;
  210. rssi = rx_stats->rs_rssi_ctl[i];
  211. if (rssi != ATH9K_RSSI_BAD) {
  212. rxs->chains |= BIT(j);
  213. rxs->chain_signal[j] = ah->noise + rssi;
  214. }
  215. j++;
  216. }
  217. /*
  218. * Update Beacon RSSI, this is used by ANI.
  219. */
  220. if (rx_stats->is_mybeacon &&
  221. ((ah->opmode == NL80211_IFTYPE_STATION) ||
  222. (ah->opmode == NL80211_IFTYPE_ADHOC))) {
  223. ATH_RSSI_LPF(common->last_rssi, rx_stats->rs_rssi);
  224. last_rssi = common->last_rssi;
  225. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  226. rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
  227. if (rssi < 0)
  228. rssi = 0;
  229. ah->stats.avgbrssi = rssi;
  230. }
  231. rxs->signal = ah->noise + rx_stats->rs_rssi;
  232. }
  233. EXPORT_SYMBOL(ath9k_cmn_process_rssi);
  234. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  235. {
  236. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  237. if (tx_info->control.hw_key) {
  238. switch (tx_info->control.hw_key->cipher) {
  239. case WLAN_CIPHER_SUITE_WEP40:
  240. case WLAN_CIPHER_SUITE_WEP104:
  241. return ATH9K_KEY_TYPE_WEP;
  242. case WLAN_CIPHER_SUITE_TKIP:
  243. return ATH9K_KEY_TYPE_TKIP;
  244. case WLAN_CIPHER_SUITE_CCMP:
  245. return ATH9K_KEY_TYPE_AES;
  246. default:
  247. break;
  248. }
  249. }
  250. return ATH9K_KEY_TYPE_CLEAR;
  251. }
  252. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  253. /*
  254. * Update internal channel flags.
  255. */
  256. static void ath9k_cmn_update_ichannel(struct ath9k_channel *ichan,
  257. struct cfg80211_chan_def *chandef)
  258. {
  259. struct ieee80211_channel *chan = chandef->chan;
  260. u16 flags = 0;
  261. ichan->channel = chan->center_freq;
  262. ichan->chan = chan;
  263. if (chan->band == NL80211_BAND_5GHZ)
  264. flags |= CHANNEL_5GHZ;
  265. switch (chandef->width) {
  266. case NL80211_CHAN_WIDTH_5:
  267. flags |= CHANNEL_QUARTER;
  268. break;
  269. case NL80211_CHAN_WIDTH_10:
  270. flags |= CHANNEL_HALF;
  271. break;
  272. case NL80211_CHAN_WIDTH_20_NOHT:
  273. break;
  274. case NL80211_CHAN_WIDTH_20:
  275. flags |= CHANNEL_HT;
  276. break;
  277. case NL80211_CHAN_WIDTH_40:
  278. if (chandef->center_freq1 > chandef->chan->center_freq)
  279. flags |= CHANNEL_HT40PLUS | CHANNEL_HT;
  280. else
  281. flags |= CHANNEL_HT40MINUS | CHANNEL_HT;
  282. break;
  283. default:
  284. WARN_ON(1);
  285. }
  286. ichan->channelFlags = flags;
  287. }
  288. /*
  289. * Get the internal channel reference.
  290. */
  291. struct ath9k_channel *ath9k_cmn_get_channel(struct ieee80211_hw *hw,
  292. struct ath_hw *ah,
  293. struct cfg80211_chan_def *chandef)
  294. {
  295. struct ieee80211_channel *curchan = chandef->chan;
  296. struct ath9k_channel *channel;
  297. channel = &ah->channels[curchan->hw_value];
  298. ath9k_cmn_update_ichannel(channel, chandef);
  299. return channel;
  300. }
  301. EXPORT_SYMBOL(ath9k_cmn_get_channel);
  302. int ath9k_cmn_count_streams(unsigned int chainmask, int max)
  303. {
  304. int streams = 0;
  305. do {
  306. if (++streams == max)
  307. break;
  308. } while ((chainmask = chainmask & (chainmask - 1)));
  309. return streams;
  310. }
  311. EXPORT_SYMBOL(ath9k_cmn_count_streams);
  312. void ath9k_cmn_update_txpow(struct ath_hw *ah, u16 cur_txpow,
  313. u16 new_txpow, u16 *txpower)
  314. {
  315. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  316. if (ah->curchan && reg->power_limit != new_txpow)
  317. ath9k_hw_set_txpowerlimit(ah, new_txpow, false);
  318. /* read back in case value is clamped */
  319. *txpower = reg->max_power_level;
  320. }
  321. EXPORT_SYMBOL(ath9k_cmn_update_txpow);
  322. void ath9k_cmn_init_crypto(struct ath_hw *ah)
  323. {
  324. struct ath_common *common = ath9k_hw_common(ah);
  325. int i = 0;
  326. /* Get the hardware key cache size. */
  327. common->keymax = AR_KEYTABLE_SIZE;
  328. /*
  329. * Check whether the separate key cache entries
  330. * are required to handle both tx+rx MIC keys.
  331. * With split mic keys the number of stations is limited
  332. * to 27 otherwise 59.
  333. */
  334. if (ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA)
  335. common->crypt_caps |= ATH_CRYPT_CAP_MIC_COMBINED;
  336. /*
  337. * Reset the key cache since some parts do not
  338. * reset the contents on initial power up.
  339. */
  340. for (i = 0; i < common->keymax; i++)
  341. ath_hw_keyreset(common, (u16) i);
  342. }
  343. EXPORT_SYMBOL(ath9k_cmn_init_crypto);
  344. static int __init ath9k_cmn_init(void)
  345. {
  346. return 0;
  347. }
  348. module_init(ath9k_cmn_init);
  349. static void __exit ath9k_cmn_exit(void)
  350. {
  351. return;
  352. }
  353. module_exit(ath9k_cmn_exit);