common.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. static int __init ath9k_cmn_init(void)
  255. {
  256. return 0;
  257. }
  258. module_init(ath9k_cmn_init);
  259. static void __exit ath9k_cmn_exit(void)
  260. {
  261. return;
  262. }
  263. module_exit(ath9k_cmn_exit);