rc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. * Larry Finger <Larry.Finger@lwfinger.net>
  27. *
  28. *****************************************************************************/
  29. #include "wifi.h"
  30. #include "base.h"
  31. #include "rc.h"
  32. /*
  33. *Finds the highest rate index we can use
  34. *if skb is special data like DHCP/EAPOL, we set should
  35. *it to lowest rate CCK_1M, otherwise we set rate to
  36. *CCK11M or OFDM_54M based on wireless mode.
  37. */
  38. static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
  39. struct ieee80211_sta *sta,
  40. struct sk_buff *skb, bool not_data)
  41. {
  42. struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
  43. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  44. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  45. struct rtl_sta_info *sta_entry = NULL;
  46. u8 wireless_mode = 0;
  47. /*
  48. *this rate is no use for true rate, firmware
  49. *will control rate at all it just used for
  50. *1.show in iwconfig in B/G mode
  51. *2.in rtl_get_tcb_desc when we check rate is
  52. * 1M we will not use FW rate but user rate.
  53. */
  54. if (rtlmac->opmode == NL80211_IFTYPE_AP ||
  55. rtlmac->opmode == NL80211_IFTYPE_ADHOC ||
  56. rtlmac->opmode == NL80211_IFTYPE_MESH_POINT) {
  57. if (sta) {
  58. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  59. wireless_mode = sta_entry->wireless_mode;
  60. } else {
  61. return 0;
  62. }
  63. } else {
  64. wireless_mode = rtlmac->mode;
  65. }
  66. if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true) ||
  67. not_data) {
  68. return 0;
  69. } else {
  70. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  71. if (wireless_mode == WIRELESS_MODE_B) {
  72. return B_MODE_MAX_RIX;
  73. } else if (wireless_mode == WIRELESS_MODE_G) {
  74. return G_MODE_MAX_RIX;
  75. } else {
  76. if (get_rf_type(rtlphy) != RF_2T2R)
  77. return N_MODE_MCS7_RIX;
  78. else
  79. return N_MODE_MCS15_RIX;
  80. }
  81. } else {
  82. if (wireless_mode == WIRELESS_MODE_A) {
  83. return A_MODE_MAX_RIX;
  84. } else {
  85. if (get_rf_type(rtlphy) != RF_2T2R)
  86. return N_MODE_MCS7_RIX;
  87. else
  88. return N_MODE_MCS15_RIX;
  89. }
  90. }
  91. }
  92. }
  93. static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
  94. struct ieee80211_sta *sta,
  95. struct ieee80211_tx_rate *rate,
  96. struct ieee80211_tx_rate_control *txrc,
  97. u8 tries, char rix, int rtsctsenable,
  98. bool not_data)
  99. {
  100. struct rtl_mac *mac = rtl_mac(rtlpriv);
  101. u8 sgi_20 = 0, sgi_40 = 0;
  102. if (sta) {
  103. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  104. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  105. }
  106. rate->count = tries;
  107. rate->idx = rix >= 0x00 ? rix : 0x00;
  108. if (!not_data) {
  109. if (txrc->short_preamble)
  110. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  111. if (mac->opmode == NL80211_IFTYPE_AP ||
  112. mac->opmode == NL80211_IFTYPE_ADHOC) {
  113. if (sta && (sta->ht_cap.cap &
  114. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  115. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  116. } else {
  117. if (mac->bw_40)
  118. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  119. }
  120. if (sgi_20 || sgi_40)
  121. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  122. if (sta && sta->ht_cap.ht_supported)
  123. rate->flags |= IEEE80211_TX_RC_MCS;
  124. }
  125. }
  126. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  127. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  128. {
  129. struct rtl_priv *rtlpriv = ppriv;
  130. struct sk_buff *skb = txrc->skb;
  131. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  132. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  133. __le16 fc = rtl_get_fc(skb);
  134. u8 try_per_rate, i, rix;
  135. bool not_data = !ieee80211_is_data(fc);
  136. if (rate_control_send_low(sta, priv_sta, txrc))
  137. return;
  138. rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
  139. try_per_rate = 1;
  140. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
  141. try_per_rate, rix, 1, not_data);
  142. if (!not_data) {
  143. for (i = 1; i < 4; i++)
  144. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
  145. txrc, i, (rix - i), 1,
  146. not_data);
  147. }
  148. }
  149. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
  150. struct rtl_sta_info *sta_entry, u16 tid)
  151. {
  152. struct rtl_mac *mac = rtl_mac(rtlpriv);
  153. if (mac->act_scanning)
  154. return false;
  155. if (mac->opmode == NL80211_IFTYPE_STATION &&
  156. mac->cnt_after_linked < 3)
  157. return false;
  158. if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
  159. return true;
  160. return false;
  161. }
  162. /*mac80211 Rate Control callbacks*/
  163. static void rtl_tx_status(void *ppriv,
  164. struct ieee80211_supported_band *sband,
  165. struct ieee80211_sta *sta, void *priv_sta,
  166. struct sk_buff *skb)
  167. {
  168. struct rtl_priv *rtlpriv = ppriv;
  169. struct rtl_mac *mac = rtl_mac(rtlpriv);
  170. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  171. __le16 fc = rtl_get_fc(skb);
  172. struct rtl_sta_info *sta_entry;
  173. if (!priv_sta || !ieee80211_is_data(fc))
  174. return;
  175. if (rtl_is_special_data(mac->hw, skb, true))
  176. return;
  177. if (is_multicast_ether_addr(ieee80211_get_DA(hdr))
  178. || is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  179. return;
  180. if (sta) {
  181. /* Check if aggregation has to be enabled for this tid */
  182. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  183. if ((sta->ht_cap.ht_supported) &&
  184. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  185. if (ieee80211_is_data_qos(fc)) {
  186. u8 tid = rtl_get_tid(skb);
  187. if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
  188. tid)) {
  189. sta_entry->tids[tid].agg.agg_state =
  190. RTL_AGG_PROGRESS;
  191. ieee80211_start_tx_ba_session(sta,
  192. tid, 5000);
  193. }
  194. }
  195. }
  196. }
  197. }
  198. static void rtl_rate_init(void *ppriv,
  199. struct ieee80211_supported_band *sband,
  200. struct ieee80211_sta *sta, void *priv_sta)
  201. {
  202. }
  203. static void rtl_rate_update(void *ppriv,
  204. struct ieee80211_supported_band *sband,
  205. struct ieee80211_sta *sta, void *priv_sta,
  206. u32 changed)
  207. {
  208. }
  209. static void *rtl_rate_alloc(struct ieee80211_hw *hw,
  210. struct dentry *debugfsdir)
  211. {
  212. struct rtl_priv *rtlpriv = rtl_priv(hw);
  213. return rtlpriv;
  214. }
  215. static void rtl_rate_free(void *rtlpriv)
  216. {
  217. return;
  218. }
  219. static void *rtl_rate_alloc_sta(void *ppriv,
  220. struct ieee80211_sta *sta, gfp_t gfp)
  221. {
  222. struct rtl_priv *rtlpriv = ppriv;
  223. struct rtl_rate_priv *rate_priv;
  224. rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
  225. if (!rate_priv) {
  226. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  227. "Unable to allocate private rc structure\n");
  228. return NULL;
  229. }
  230. rtlpriv->rate_priv = rate_priv;
  231. return rate_priv;
  232. }
  233. static void rtl_rate_free_sta(void *rtlpriv,
  234. struct ieee80211_sta *sta, void *priv_sta)
  235. {
  236. struct rtl_rate_priv *rate_priv = priv_sta;
  237. kfree(rate_priv);
  238. }
  239. static struct rate_control_ops rtl_rate_ops = {
  240. .module = NULL,
  241. .name = "rtl_rc",
  242. .alloc = rtl_rate_alloc,
  243. .free = rtl_rate_free,
  244. .alloc_sta = rtl_rate_alloc_sta,
  245. .free_sta = rtl_rate_free_sta,
  246. .rate_init = rtl_rate_init,
  247. .rate_update = rtl_rate_update,
  248. .tx_status = rtl_tx_status,
  249. .get_rate = rtl_get_rate,
  250. };
  251. int rtl_rate_control_register(void)
  252. {
  253. return ieee80211_rate_control_register(&rtl_rate_ops);
  254. }
  255. void rtl_rate_control_unregister(void)
  256. {
  257. ieee80211_rate_control_unregister(&rtl_rate_ops);
  258. }