common.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /*
  129. * Theory for reporting quality:
  130. *
  131. * At a hardware RSSI of 45 you will be able to use MCS 7 reliably.
  132. * At a hardware RSSI of 45 you will be able to use MCS 15 reliably.
  133. * At a hardware RSSI of 35 you should be able use 54 Mbps reliably.
  134. *
  135. * MCS 7 is the highets MCS index usable by a 1-stream device.
  136. * MCS 15 is the highest MCS index usable by a 2-stream device.
  137. *
  138. * All ath9k devices are either 1-stream or 2-stream.
  139. *
  140. * How many bars you see is derived from the qual reporting.
  141. *
  142. * A more elaborate scheme can be used here but it requires tables
  143. * of SNR/throughput for each possible mode used. For the MCS table
  144. * you can refer to the wireless wiki:
  145. *
  146. * http://wireless.kernel.org/en/developers/Documentation/ieee80211/802.11n
  147. *
  148. */
  149. static int ath9k_compute_qual(struct ieee80211_hw *hw,
  150. struct ath_rx_status *rx_stats)
  151. {
  152. int qual;
  153. if (conf_is_ht(&hw->conf))
  154. qual = rx_stats->rs_rssi * 100 / 45;
  155. else
  156. qual = rx_stats->rs_rssi * 100 / 35;
  157. /*
  158. * rssi can be more than 45 though, anything above that
  159. * should be considered at 100%
  160. */
  161. if (qual > 100)
  162. qual = 100;
  163. return qual;
  164. }
  165. static void ath9k_process_rssi(struct ath_common *common,
  166. struct ieee80211_hw *hw,
  167. struct sk_buff *skb,
  168. struct ath_rx_status *rx_stats)
  169. {
  170. struct ath_hw *ah = common->ah;
  171. struct ieee80211_sta *sta;
  172. struct ieee80211_hdr *hdr;
  173. struct ath_node *an;
  174. int last_rssi = ATH_RSSI_DUMMY_MARKER;
  175. __le16 fc;
  176. hdr = (struct ieee80211_hdr *)skb->data;
  177. fc = hdr->frame_control;
  178. rcu_read_lock();
  179. /*
  180. * XXX: use ieee80211_find_sta! This requires quite a bit of work
  181. * under the current ath9k virtual wiphy implementation as we have
  182. * no way of tying a vif to wiphy. Typically vifs are attached to
  183. * at least one sdata of a wiphy on mac80211 but with ath9k virtual
  184. * wiphy you'd have to iterate over every wiphy and each sdata.
  185. */
  186. sta = ieee80211_find_sta_by_hw(hw, hdr->addr2);
  187. if (sta) {
  188. an = (struct ath_node *) sta->drv_priv;
  189. if (rx_stats->rs_rssi != ATH9K_RSSI_BAD &&
  190. !rx_stats->rs_moreaggr)
  191. ATH_RSSI_LPF(an->last_rssi, rx_stats->rs_rssi);
  192. last_rssi = an->last_rssi;
  193. }
  194. rcu_read_unlock();
  195. if (likely(last_rssi != ATH_RSSI_DUMMY_MARKER))
  196. rx_stats->rs_rssi = ATH_EP_RND(last_rssi,
  197. ATH_RSSI_EP_MULTIPLIER);
  198. if (rx_stats->rs_rssi < 0)
  199. rx_stats->rs_rssi = 0;
  200. else if (rx_stats->rs_rssi > 127)
  201. rx_stats->rs_rssi = 127;
  202. /* Update Beacon RSSI, this is used by ANI. */
  203. if (ieee80211_is_beacon(fc))
  204. ah->stats.avgbrssi = rx_stats->rs_rssi;
  205. }
  206. /*
  207. * For Decrypt or Demic errors, we only mark packet status here and always push
  208. * up the frame up to let mac80211 handle the actual error case, be it no
  209. * decryption key or real decryption error. This let us keep statistics there.
  210. */
  211. int ath9k_cmn_rx_skb_preprocess(struct ath_common *common,
  212. struct ieee80211_hw *hw,
  213. struct sk_buff *skb,
  214. struct ath_rx_status *rx_stats,
  215. struct ieee80211_rx_status *rx_status,
  216. bool *decrypt_error)
  217. {
  218. struct ath_hw *ah = common->ah;
  219. if (!ath9k_rx_accept(common, skb, rx_status, rx_stats, decrypt_error))
  220. return -EINVAL;
  221. ath9k_process_rssi(common, hw, skb, rx_stats);
  222. rx_status->rate_idx = ath9k_process_rate(common, hw,
  223. rx_stats, rx_status, skb);
  224. rx_status->mactime = ath9k_hw_extend_tsf(ah, rx_stats->rs_tstamp);
  225. rx_status->band = hw->conf.channel->band;
  226. rx_status->freq = hw->conf.channel->center_freq;
  227. rx_status->noise = common->ani.noise_floor;
  228. rx_status->signal = ATH_DEFAULT_NOISE_FLOOR + rx_stats->rs_rssi;
  229. rx_status->antenna = rx_stats->rs_antenna;
  230. rx_status->qual = ath9k_compute_qual(hw, rx_stats);
  231. rx_status->flag |= RX_FLAG_TSFT;
  232. return 0;
  233. }
  234. EXPORT_SYMBOL(ath9k_cmn_rx_skb_preprocess);
  235. void ath9k_cmn_rx_skb_postprocess(struct ath_common *common,
  236. struct sk_buff *skb,
  237. struct ath_rx_status *rx_stats,
  238. struct ieee80211_rx_status *rxs,
  239. bool decrypt_error)
  240. {
  241. struct ath_hw *ah = common->ah;
  242. struct ieee80211_hdr *hdr;
  243. int hdrlen, padsize;
  244. u8 keyix;
  245. __le16 fc;
  246. /* see if any padding is done by the hw and remove it */
  247. hdr = (struct ieee80211_hdr *) skb->data;
  248. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  249. fc = hdr->frame_control;
  250. /* The MAC header is padded to have 32-bit boundary if the
  251. * packet payload is non-zero. The general calculation for
  252. * padsize would take into account odd header lengths:
  253. * padsize = (4 - hdrlen % 4) % 4; However, since only
  254. * even-length headers are used, padding can only be 0 or 2
  255. * bytes and we can optimize this a bit. In addition, we must
  256. * not try to remove padding from short control frames that do
  257. * not have payload. */
  258. padsize = hdrlen & 3;
  259. if (padsize && hdrlen >= 24) {
  260. memmove(skb->data + padsize, skb->data, hdrlen);
  261. skb_pull(skb, padsize);
  262. }
  263. keyix = rx_stats->rs_keyix;
  264. if (!(keyix == ATH9K_RXKEYIX_INVALID) && !decrypt_error) {
  265. rxs->flag |= RX_FLAG_DECRYPTED;
  266. } else if (ieee80211_has_protected(fc)
  267. && !decrypt_error && skb->len >= hdrlen + 4) {
  268. keyix = skb->data[hdrlen + 3] >> 6;
  269. if (test_bit(keyix, common->keymap))
  270. rxs->flag |= RX_FLAG_DECRYPTED;
  271. }
  272. if (ah->sw_mgmt_crypto &&
  273. (rxs->flag & RX_FLAG_DECRYPTED) &&
  274. ieee80211_is_mgmt(fc))
  275. /* Use software decrypt for management frames. */
  276. rxs->flag &= ~RX_FLAG_DECRYPTED;
  277. }
  278. EXPORT_SYMBOL(ath9k_cmn_rx_skb_postprocess);
  279. static int __init ath9k_cmn_init(void)
  280. {
  281. return 0;
  282. }
  283. module_init(ath9k_cmn_init);
  284. static void __exit ath9k_cmn_exit(void)
  285. {
  286. return;
  287. }
  288. module_exit(ath9k_cmn_exit);