common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 ieee80211_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->flag |= RX_FLAG_5MHZ;
  158. else if (IS_CHAN_HALF_RATE(ah->curchan))
  159. rxs->flag |= RX_FLAG_10MHZ;
  160. if (rx_stats->rs_rate & 0x80) {
  161. /* HT rate */
  162. rxs->flag |= RX_FLAG_HT;
  163. rxs->flag |= rx_stats->flag;
  164. rxs->rate_idx = rx_stats->rs_rate & 0x7f;
  165. return 0;
  166. }
  167. for (i = 0; i < sband->n_bitrates; i++) {
  168. if (sband->bitrates[i].hw_value == rx_stats->rs_rate) {
  169. rxs->rate_idx = i;
  170. return 0;
  171. }
  172. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  173. rxs->flag |= RX_FLAG_SHORTPRE;
  174. rxs->rate_idx = i;
  175. return 0;
  176. }
  177. }
  178. return -EINVAL;
  179. }
  180. EXPORT_SYMBOL(ath9k_cmn_process_rate);
  181. void ath9k_cmn_process_rssi(struct ath_common *common,
  182. struct ieee80211_hw *hw,
  183. struct ath_rx_status *rx_stats,
  184. struct ieee80211_rx_status *rxs)
  185. {
  186. struct ath_hw *ah = common->ah;
  187. int last_rssi;
  188. int rssi = rx_stats->rs_rssi;
  189. int i, j;
  190. /*
  191. * RSSI is not available for subframes in an A-MPDU.
  192. */
  193. if (rx_stats->rs_moreaggr) {
  194. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  195. return;
  196. }
  197. /*
  198. * Check if the RSSI for the last subframe in an A-MPDU
  199. * or an unaggregated frame is valid.
  200. */
  201. if (rx_stats->rs_rssi == ATH9K_RSSI_BAD) {
  202. rxs->flag |= RX_FLAG_NO_SIGNAL_VAL;
  203. return;
  204. }
  205. for (i = 0, j = 0; i < ARRAY_SIZE(rx_stats->rs_rssi_ctl); i++) {
  206. s8 rssi;
  207. if (!(ah->rxchainmask & BIT(i)))
  208. continue;
  209. rssi = rx_stats->rs_rssi_ctl[i];
  210. if (rssi != ATH9K_RSSI_BAD) {
  211. rxs->chains |= BIT(j);
  212. rxs->chain_signal[j] = ah->noise + rssi;
  213. }
  214. j++;
  215. }
  216. /*
  217. * Update Beacon RSSI, this is used by ANI.
  218. */
  219. if (rx_stats->is_mybeacon &&
  220. ((ah->opmode == NL80211_IFTYPE_STATION) ||
  221. (ah->opmode == NL80211_IFTYPE_ADHOC))) {
  222. ATH_RSSI_LPF(common->last_rssi, rx_stats->rs_rssi);
  223. last_rssi = common->last_rssi;
  224. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  225. rssi = ATH_EP_RND(last_rssi, ATH_RSSI_EP_MULTIPLIER);
  226. if (rssi < 0)
  227. rssi = 0;
  228. ah->stats.avgbrssi = rssi;
  229. }
  230. rxs->signal = ah->noise + rx_stats->rs_rssi;
  231. }
  232. EXPORT_SYMBOL(ath9k_cmn_process_rssi);
  233. int ath9k_cmn_get_hw_crypto_keytype(struct sk_buff *skb)
  234. {
  235. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  236. if (tx_info->control.hw_key) {
  237. switch (tx_info->control.hw_key->cipher) {
  238. case WLAN_CIPHER_SUITE_WEP40:
  239. case WLAN_CIPHER_SUITE_WEP104:
  240. return ATH9K_KEY_TYPE_WEP;
  241. case WLAN_CIPHER_SUITE_TKIP:
  242. return ATH9K_KEY_TYPE_TKIP;
  243. case WLAN_CIPHER_SUITE_CCMP:
  244. return ATH9K_KEY_TYPE_AES;
  245. default:
  246. break;
  247. }
  248. }
  249. return ATH9K_KEY_TYPE_CLEAR;
  250. }
  251. EXPORT_SYMBOL(ath9k_cmn_get_hw_crypto_keytype);
  252. /*
  253. * Update internal channel flags.
  254. */
  255. static void ath9k_cmn_update_ichannel(struct ath9k_channel *ichan,
  256. struct cfg80211_chan_def *chandef)
  257. {
  258. struct ieee80211_channel *chan = chandef->chan;
  259. u16 flags = 0;
  260. ichan->channel = chan->center_freq;
  261. ichan->chan = chan;
  262. if (chan->band == IEEE80211_BAND_5GHZ)
  263. flags |= CHANNEL_5GHZ;
  264. switch (chandef->width) {
  265. case NL80211_CHAN_WIDTH_5:
  266. flags |= CHANNEL_QUARTER;
  267. break;
  268. case NL80211_CHAN_WIDTH_10:
  269. flags |= CHANNEL_HALF;
  270. break;
  271. case NL80211_CHAN_WIDTH_20_NOHT:
  272. break;
  273. case NL80211_CHAN_WIDTH_20:
  274. flags |= CHANNEL_HT;
  275. break;
  276. case NL80211_CHAN_WIDTH_40:
  277. if (chandef->center_freq1 > chandef->chan->center_freq)
  278. flags |= CHANNEL_HT40PLUS | CHANNEL_HT;
  279. else
  280. flags |= CHANNEL_HT40MINUS | CHANNEL_HT;
  281. break;
  282. default:
  283. WARN_ON(1);
  284. }
  285. ichan->channelFlags = flags;
  286. }
  287. /*
  288. * Get the internal channel reference.
  289. */
  290. struct ath9k_channel *ath9k_cmn_get_channel(struct ieee80211_hw *hw,
  291. struct ath_hw *ah,
  292. struct cfg80211_chan_def *chandef)
  293. {
  294. struct ieee80211_channel *curchan = chandef->chan;
  295. struct ath9k_channel *channel;
  296. channel = &ah->channels[curchan->hw_value];
  297. ath9k_cmn_update_ichannel(channel, chandef);
  298. return channel;
  299. }
  300. EXPORT_SYMBOL(ath9k_cmn_get_channel);
  301. int ath9k_cmn_count_streams(unsigned int chainmask, int max)
  302. {
  303. int streams = 0;
  304. do {
  305. if (++streams == max)
  306. break;
  307. } while ((chainmask = chainmask & (chainmask - 1)));
  308. return streams;
  309. }
  310. EXPORT_SYMBOL(ath9k_cmn_count_streams);
  311. void ath9k_cmn_update_txpow(struct ath_hw *ah, u16 cur_txpow,
  312. u16 new_txpow, u16 *txpower)
  313. {
  314. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  315. if (reg->power_limit != new_txpow)
  316. ath9k_hw_set_txpowerlimit(ah, new_txpow, false);
  317. /* read back in case value is clamped */
  318. *txpower = reg->max_power_level;
  319. }
  320. EXPORT_SYMBOL(ath9k_cmn_update_txpow);
  321. void ath9k_cmn_init_crypto(struct ath_hw *ah)
  322. {
  323. struct ath_common *common = ath9k_hw_common(ah);
  324. int i = 0;
  325. /* Get the hardware key cache size. */
  326. common->keymax = AR_KEYTABLE_SIZE;
  327. /*
  328. * Check whether the separate key cache entries
  329. * are required to handle both tx+rx MIC keys.
  330. * With split mic keys the number of stations is limited
  331. * to 27 otherwise 59.
  332. */
  333. if (ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA)
  334. common->crypt_caps |= ATH_CRYPT_CAP_MIC_COMBINED;
  335. /*
  336. * Reset the key cache since some parts do not
  337. * reset the contents on initial power up.
  338. */
  339. for (i = 0; i < common->keymax; i++)
  340. ath_hw_keyreset(common, (u16) i);
  341. }
  342. EXPORT_SYMBOL(ath9k_cmn_init_crypto);
  343. static int __init ath9k_cmn_init(void)
  344. {
  345. return 0;
  346. }
  347. module_init(ath9k_cmn_init);
  348. static void __exit ath9k_cmn_exit(void)
  349. {
  350. return;
  351. }
  352. module_exit(ath9k_cmn_exit);