rc.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. if (sta) {
  57. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  58. wireless_mode = sta_entry->wireless_mode;
  59. } else {
  60. return 0;
  61. }
  62. } else {
  63. wireless_mode = rtlmac->mode;
  64. }
  65. if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true) ||
  66. not_data) {
  67. return 0;
  68. } else {
  69. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  70. if (wireless_mode == WIRELESS_MODE_B) {
  71. return B_MODE_MAX_RIX;
  72. } else if (wireless_mode == WIRELESS_MODE_G) {
  73. return G_MODE_MAX_RIX;
  74. } else {
  75. if (get_rf_type(rtlphy) != RF_2T2R)
  76. return N_MODE_MCS7_RIX;
  77. else
  78. return N_MODE_MCS15_RIX;
  79. }
  80. } else {
  81. if (wireless_mode == WIRELESS_MODE_A) {
  82. return A_MODE_MAX_RIX;
  83. } else {
  84. if (get_rf_type(rtlphy) != RF_2T2R)
  85. return N_MODE_MCS7_RIX;
  86. else
  87. return N_MODE_MCS15_RIX;
  88. }
  89. }
  90. }
  91. }
  92. static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
  93. struct ieee80211_sta *sta,
  94. struct ieee80211_tx_rate *rate,
  95. struct ieee80211_tx_rate_control *txrc,
  96. u8 tries, char rix, int rtsctsenable,
  97. bool not_data)
  98. {
  99. struct rtl_mac *mac = rtl_mac(rtlpriv);
  100. u8 sgi_20 = 0, sgi_40 = 0;
  101. if (sta) {
  102. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  103. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  104. }
  105. rate->count = tries;
  106. rate->idx = rix >= 0x00 ? rix : 0x00;
  107. if (!not_data) {
  108. if (txrc->short_preamble)
  109. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  110. if (mac->opmode == NL80211_IFTYPE_AP ||
  111. mac->opmode == NL80211_IFTYPE_ADHOC) {
  112. if (sta && (sta->ht_cap.cap &
  113. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  114. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  115. } else {
  116. if (mac->bw_40)
  117. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  118. }
  119. if (sgi_20 || sgi_40)
  120. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  121. if (sta && sta->ht_cap.ht_supported)
  122. rate->flags |= IEEE80211_TX_RC_MCS;
  123. }
  124. }
  125. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  126. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  127. {
  128. struct rtl_priv *rtlpriv = ppriv;
  129. struct sk_buff *skb = txrc->skb;
  130. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  131. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  132. __le16 fc = rtl_get_fc(skb);
  133. u8 try_per_rate, i, rix;
  134. bool not_data = !ieee80211_is_data(fc);
  135. if (rate_control_send_low(sta, priv_sta, txrc))
  136. return;
  137. rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
  138. try_per_rate = 1;
  139. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
  140. try_per_rate, rix, 1, not_data);
  141. if (!not_data) {
  142. for (i = 1; i < 4; i++)
  143. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
  144. txrc, i, (rix - i), 1,
  145. not_data);
  146. }
  147. }
  148. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
  149. struct rtl_sta_info *sta_entry, u16 tid)
  150. {
  151. struct rtl_mac *mac = rtl_mac(rtlpriv);
  152. if (mac->act_scanning)
  153. return false;
  154. if (mac->opmode == NL80211_IFTYPE_STATION &&
  155. mac->cnt_after_linked < 3)
  156. return false;
  157. if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
  158. return true;
  159. return false;
  160. }
  161. /*mac80211 Rate Control callbacks*/
  162. static void rtl_tx_status(void *ppriv,
  163. struct ieee80211_supported_band *sband,
  164. struct ieee80211_sta *sta, void *priv_sta,
  165. struct sk_buff *skb)
  166. {
  167. struct rtl_priv *rtlpriv = ppriv;
  168. struct rtl_mac *mac = rtl_mac(rtlpriv);
  169. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  170. __le16 fc = rtl_get_fc(skb);
  171. struct rtl_sta_info *sta_entry;
  172. if (!priv_sta || !ieee80211_is_data(fc))
  173. return;
  174. if (rtl_is_special_data(mac->hw, skb, true))
  175. return;
  176. if (is_multicast_ether_addr(ieee80211_get_DA(hdr))
  177. || is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  178. return;
  179. if (sta) {
  180. /* Check if aggregation has to be enabled for this tid */
  181. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  182. if ((sta->ht_cap.ht_supported) &&
  183. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  184. if (ieee80211_is_data_qos(fc)) {
  185. u8 tid = rtl_get_tid(skb);
  186. if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
  187. tid)) {
  188. sta_entry->tids[tid].agg.agg_state =
  189. RTL_AGG_PROGRESS;
  190. ieee80211_start_tx_ba_session(sta,
  191. tid, 5000);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. static void rtl_rate_init(void *ppriv,
  198. struct ieee80211_supported_band *sband,
  199. struct ieee80211_sta *sta, void *priv_sta)
  200. {
  201. }
  202. static void rtl_rate_update(void *ppriv,
  203. struct ieee80211_supported_band *sband,
  204. struct ieee80211_sta *sta, void *priv_sta,
  205. u32 changed,
  206. enum nl80211_channel_type oper_chan_type)
  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. }