common.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. if (rx_stats->rs_more) {
  48. /*
  49. * Frame spans multiple descriptors; this cannot happen yet
  50. * as we don't support jumbograms. If not in monitor mode,
  51. * discard the frame. Enable this if you want to see
  52. * error frames in Monitor mode.
  53. */
  54. if (ah->opmode != NL80211_IFTYPE_MONITOR)
  55. return false;
  56. } else if (rx_stats->rs_status != 0) {
  57. if (rx_stats->rs_status & ATH9K_RXERR_CRC)
  58. rxs->flag |= RX_FLAG_FAILED_FCS_CRC;
  59. if (rx_stats->rs_status & ATH9K_RXERR_PHY)
  60. return false;
  61. if (rx_stats->rs_status & ATH9K_RXERR_DECRYPT) {
  62. *decrypt_error = true;
  63. } else if (rx_stats->rs_status & ATH9K_RXERR_MIC) {
  64. if (ieee80211_is_ctl(fc))
  65. /*
  66. * Sometimes, we get invalid
  67. * MIC failures on valid control frames.
  68. * Remove these mic errors.
  69. */
  70. rx_stats->rs_status &= ~ATH9K_RXERR_MIC;
  71. else
  72. rxs->flag |= RX_FLAG_MMIC_ERROR;
  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. if (ah->opmode == NL80211_IFTYPE_MONITOR) {
  80. if (rx_stats->rs_status &
  81. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC |
  82. ATH9K_RXERR_CRC))
  83. return false;
  84. } else {
  85. if (rx_stats->rs_status &
  86. ~(ATH9K_RXERR_DECRYPT | ATH9K_RXERR_MIC)) {
  87. return false;
  88. }
  89. }
  90. }
  91. return true;
  92. }
  93. static u8 ath9k_process_rate(struct ath_common *common,
  94. struct ieee80211_hw *hw,
  95. struct ath_rx_status *rx_stats,
  96. struct ieee80211_rx_status *rxs,
  97. struct sk_buff *skb)
  98. {
  99. struct ieee80211_supported_band *sband;
  100. enum ieee80211_band band;
  101. unsigned int i = 0;
  102. band = hw->conf.channel->band;
  103. sband = hw->wiphy->bands[band];
  104. if (rx_stats->rs_rate & 0x80) {
  105. /* HT rate */
  106. rxs->flag |= RX_FLAG_HT;
  107. if (rx_stats->rs_flags & ATH9K_RX_2040)
  108. rxs->flag |= RX_FLAG_40MHZ;
  109. if (rx_stats->rs_flags & ATH9K_RX_GI)
  110. rxs->flag |= RX_FLAG_SHORT_GI;
  111. return rx_stats->rs_rate & 0x7f;
  112. }
  113. for (i = 0; i < sband->n_bitrates; i++) {
  114. if (sband->bitrates[i].hw_value == rx_stats->rs_rate)
  115. return i;
  116. if (sband->bitrates[i].hw_value_short == rx_stats->rs_rate) {
  117. rxs->flag |= RX_FLAG_SHORTPRE;
  118. return i;
  119. }
  120. }
  121. /* No valid hardware bitrate found -- we should not get here */
  122. ath_print(common, ATH_DBG_XMIT, "unsupported hw bitrate detected "
  123. "0x%02x using 1 Mbit\n", rx_stats->rs_rate);
  124. if ((common->debug_mask & ATH_DBG_XMIT))
  125. print_hex_dump_bytes("", DUMP_PREFIX_NONE, skb->data, skb->len);
  126. return 0;
  127. }
  128. static void ath9k_process_rssi(struct ath_common *common,
  129. struct ieee80211_hw *hw,
  130. struct sk_buff *skb,
  131. struct ath_rx_status *rx_stats)
  132. {
  133. struct ath_hw *ah = common->ah;
  134. struct ieee80211_sta *sta;
  135. struct ieee80211_hdr *hdr;
  136. struct ath_node *an;
  137. int last_rssi = ATH_RSSI_DUMMY_MARKER;
  138. __le16 fc;
  139. hdr = (struct ieee80211_hdr *)skb->data;
  140. fc = hdr->frame_control;
  141. rcu_read_lock();
  142. /*
  143. * XXX: use ieee80211_find_sta! This requires quite a bit of work
  144. * under the current ath9k virtual wiphy implementation as we have
  145. * no way of tying a vif to wiphy. Typically vifs are attached to
  146. * at least one sdata of a wiphy on mac80211 but with ath9k virtual
  147. * wiphy you'd have to iterate over every wiphy and each sdata.
  148. */
  149. sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
  150. if (sta) {
  151. an = (struct ath_node *) sta->drv_priv;
  152. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
  153. !rx_stats->rs_moreaggr)
  154. ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
  155. last_rssi = an->last_rssi;
  156. }
  157. rcu_read_unlock();
  158. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  159. rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
  160. ATH_RSSI_EP_MULTIPLIER);
  161. if (rx_stats->rs_rssi < 0)
  162. rx_stats->rs_rssi = 0;
  163. else if (rx_stats->rs_rssi > 127)
  164. rx_stats->rs_rssi = 127;
  165. /* Update Beacon RSSI, this is used by ANI. */
  166. if (ieee80211_is_beacon(fc))
  167. ah->stats.avgbrssi = rx_stats->rs_rssi;
  168. }
  169. /*
  170. * For Decrypt or Demic errors, we only mark packet status here and always push
  171. * up the frame up to let mac80211 handle the actual error case, be it no
  172. * decryption key or real decryption error. This let us keep statistics there.
  173. */
  174. int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
  175. struct ieee80211_hw *hw,
  176. struct sk_buff *skb,
  177. struct ath_rx_status *rx_stats,
  178. struct ieee80211_rx_status *rx_status,
  179. bool *decrypt_error)
  180. {
  181. struct ath_hw *ah = common->ah;
  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, 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. /* The MAC header is padded to have 32-bit boundary if the
  213. * packet payload is non-zero. The general calculation for
  214. * padsize would take into account odd header lengths:
  215. * padsize = (4 - hdrlen % 4) % 4; However, since only
  216. * even-length headers are used, padding can only be 0 or 2
  217. * bytes and we can optimize this a bit. In addition, we must
  218. * not try to remove padding from short control frames that do
  219. * not have payload. */
  220. padsize = hdrlen & 3;
  221. if (padsize && hdrlen >= 24) {
  222. memmove(skb->data + padsize, skb->data, hdrlen);
  223. skb_pull(skb, padsize);
  224. }
  225. keyix = rx_stats->rs_keyix;
  226. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
  227. rxs->flag |= RX_FLAG_DECRYPTED;
  228. } else if (ieee80211_has_protected(fc)
  229. && !decrypt_error && skb->len >= hdrlen + 4) {
  230. keyix = skb->data[hdrlen + 3] >> 6;
  231. if (test_bit(keyix, common->keymap))
  232. rxs->flag |= RX_FLAG_DECRYPTED;
  233. }
  234. if (ah->sw_mgmt_crypto &&
  235. (rxs->flag & RX_FLAG_DECRYPTED) &&
  236. ieee80211_is_mgmt(fc))
  237. /* Use software decrypt for management frames. */
  238. rxs->flag &= ~RX_FLAG_DECRYPTED;
  239. }
  240. EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
  241. static int __init ath9k_cmn_init(void)
  242. {
  243. return 0;
  244. }
  245. module_init(ath9k_cmn_init);
  246. static void __exit ath9k_cmn_exit(void)
  247. {
  248. return;
  249. }
  250. module_exit(ath9k_cmn_exit);