rc.c 8.2 KB

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