rc.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, false) ||
  55. not_data) {
  56. return 0;
  57. } else {
  58. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  59. if (wireless_mode == WIRELESS_MODE_B) {
  60. return B_MODE_MAX_RIX;
  61. } else if (wireless_mode == WIRELESS_MODE_G) {
  62. return G_MODE_MAX_RIX;
  63. } else if (wireless_mode == WIRELESS_MODE_N_24G) {
  64. if (get_rf_type(rtlphy) != RF_2T2R)
  65. return N_MODE_MCS7_RIX;
  66. else
  67. return N_MODE_MCS15_RIX;
  68. } else if (wireless_mode == WIRELESS_MODE_AC_24G) {
  69. return AC_MODE_MCS9_RIX;
  70. }
  71. return 0;
  72. } else {
  73. if (wireless_mode == WIRELESS_MODE_A) {
  74. return A_MODE_MAX_RIX;
  75. } else if (wireless_mode == WIRELESS_MODE_N_5G) {
  76. if (get_rf_type(rtlphy) != RF_2T2R)
  77. return N_MODE_MCS7_RIX;
  78. else
  79. return N_MODE_MCS15_RIX;
  80. } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
  81. return AC_MODE_MCS9_RIX;
  82. }
  83. return 0;
  84. }
  85. }
  86. }
  87. static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
  88. struct ieee80211_sta *sta,
  89. struct ieee80211_tx_rate *rate,
  90. struct ieee80211_tx_rate_control *txrc,
  91. u8 tries, char rix, int rtsctsenable,
  92. bool not_data)
  93. {
  94. struct rtl_mac *mac = rtl_mac(rtlpriv);
  95. struct rtl_sta_info *sta_entry = NULL;
  96. u8 wireless_mode = 0;
  97. u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
  98. if (sta) {
  99. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  100. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  101. sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
  102. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  103. wireless_mode = sta_entry->wireless_mode;
  104. }
  105. rate->count = tries;
  106. rate->idx = rix >= 0x00 ? rix : 0x00;
  107. if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE &&
  108. wireless_mode == WIRELESS_MODE_AC_5G)
  109. rate->idx += 0x10;/*2NSS for 8812AE*/
  110. if (!not_data) {
  111. if (txrc->short_preamble)
  112. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  113. if (mac->opmode == NL80211_IFTYPE_AP ||
  114. mac->opmode == NL80211_IFTYPE_ADHOC) {
  115. if (sta && (sta->ht_cap.cap &
  116. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  117. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  118. if (sta && (sta->vht_cap.vht_supported))
  119. rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  120. } else {
  121. if (mac->bw_40)
  122. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  123. if (mac->bw_80)
  124. rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  125. }
  126. if (sgi_20 || sgi_40 || sgi_80)
  127. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  128. if (sta && sta->ht_cap.ht_supported &&
  129. ((wireless_mode == WIRELESS_MODE_N_5G) ||
  130. (wireless_mode == WIRELESS_MODE_N_24G)))
  131. rate->flags |= IEEE80211_TX_RC_MCS;
  132. }
  133. }
  134. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  135. void *priv_sta,
  136. struct ieee80211_tx_rate_control *txrc)
  137. {
  138. struct rtl_priv *rtlpriv = ppriv;
  139. struct sk_buff *skb = txrc->skb;
  140. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  141. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  142. __le16 fc = rtl_get_fc(skb);
  143. u8 try_per_rate, i, rix;
  144. bool not_data = !ieee80211_is_data(fc);
  145. if (rate_control_send_low(sta, priv_sta, txrc))
  146. return;
  147. rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
  148. try_per_rate = 1;
  149. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
  150. try_per_rate, rix, 1, not_data);
  151. if (!not_data) {
  152. for (i = 1; i < 4; i++)
  153. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
  154. txrc, i, (rix - i), 1,
  155. not_data);
  156. }
  157. }
  158. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
  159. struct rtl_sta_info *sta_entry, u16 tid)
  160. {
  161. struct rtl_mac *mac = rtl_mac(rtlpriv);
  162. if (mac->act_scanning)
  163. return false;
  164. if (mac->opmode == NL80211_IFTYPE_STATION &&
  165. mac->cnt_after_linked < 3)
  166. return false;
  167. if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
  168. return true;
  169. return false;
  170. }
  171. /*mac80211 Rate Control callbacks*/
  172. static void rtl_tx_status(void *ppriv,
  173. struct ieee80211_supported_band *sband,
  174. struct ieee80211_sta *sta, void *priv_sta,
  175. struct sk_buff *skb)
  176. {
  177. struct rtl_priv *rtlpriv = ppriv;
  178. struct rtl_mac *mac = rtl_mac(rtlpriv);
  179. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  180. __le16 fc = rtl_get_fc(skb);
  181. struct rtl_sta_info *sta_entry;
  182. if (!priv_sta || !ieee80211_is_data(fc))
  183. return;
  184. if (rtl_is_special_data(mac->hw, skb, true, true))
  185. return;
  186. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
  187. is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  188. return;
  189. if (sta) {
  190. /* Check if aggregation has to be enabled for this tid */
  191. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  192. if ((sta->ht_cap.ht_supported) &&
  193. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  194. if (ieee80211_is_data_qos(fc)) {
  195. u8 tid = rtl_get_tid(skb);
  196. if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
  197. tid)) {
  198. sta_entry->tids[tid].agg.agg_state =
  199. RTL_AGG_PROGRESS;
  200. ieee80211_start_tx_ba_session(sta, tid,
  201. 5000);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. static void rtl_rate_init(void *ppriv,
  208. struct ieee80211_supported_band *sband,
  209. struct cfg80211_chan_def *chandef,
  210. struct ieee80211_sta *sta, void *priv_sta)
  211. {
  212. }
  213. static void rtl_rate_update(void *ppriv,
  214. struct ieee80211_supported_band *sband,
  215. struct cfg80211_chan_def *chandef,
  216. struct ieee80211_sta *sta, void *priv_sta,
  217. u32 changed)
  218. {
  219. }
  220. static void *rtl_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  221. {
  222. struct rtl_priv *rtlpriv = rtl_priv(hw);
  223. return rtlpriv;
  224. }
  225. static void rtl_rate_free(void *rtlpriv)
  226. {
  227. return;
  228. }
  229. static void *rtl_rate_alloc_sta(void *ppriv,
  230. struct ieee80211_sta *sta, gfp_t gfp)
  231. {
  232. struct rtl_priv *rtlpriv = ppriv;
  233. struct rtl_rate_priv *rate_priv;
  234. rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
  235. if (!rate_priv) {
  236. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  237. "Unable to allocate private rc structure\n");
  238. return NULL;
  239. }
  240. rtlpriv->rate_priv = rate_priv;
  241. return rate_priv;
  242. }
  243. static void rtl_rate_free_sta(void *rtlpriv,
  244. struct ieee80211_sta *sta, void *priv_sta)
  245. {
  246. struct rtl_rate_priv *rate_priv = priv_sta;
  247. kfree(rate_priv);
  248. }
  249. static struct rate_control_ops rtl_rate_ops = {
  250. .name = "rtl_rc",
  251. .alloc = rtl_rate_alloc,
  252. .free = rtl_rate_free,
  253. .alloc_sta = rtl_rate_alloc_sta,
  254. .free_sta = rtl_rate_free_sta,
  255. .rate_init = rtl_rate_init,
  256. .rate_update = rtl_rate_update,
  257. .tx_status = rtl_tx_status,
  258. .get_rate = rtl_get_rate,
  259. };
  260. int rtl_rate_control_register(void)
  261. {
  262. return ieee80211_rate_control_register(&rtl_rate_ops);
  263. }
  264. void rtl_rate_control_unregister(void)
  265. {
  266. ieee80211_rate_control_unregister(&rtl_rate_ops);
  267. }