rc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. u16 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, s8 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. u16 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. if (sta && sta->vht_cap.vht_supported &&
  133. (wireless_mode == WIRELESS_MODE_AC_5G ||
  134. wireless_mode == WIRELESS_MODE_AC_24G ||
  135. wireless_mode == WIRELESS_MODE_AC_ONLY))
  136. rate->flags |= IEEE80211_TX_RC_VHT_MCS;
  137. }
  138. }
  139. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  140. void *priv_sta,
  141. struct ieee80211_tx_rate_control *txrc)
  142. {
  143. struct rtl_priv *rtlpriv = ppriv;
  144. struct sk_buff *skb = txrc->skb;
  145. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  146. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  147. __le16 fc = rtl_get_fc(skb);
  148. u8 try_per_rate, i, rix;
  149. bool not_data = !ieee80211_is_data(fc);
  150. if (rate_control_send_low(sta, priv_sta, txrc))
  151. return;
  152. rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
  153. try_per_rate = 1;
  154. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
  155. try_per_rate, rix, 1, not_data);
  156. if (!not_data) {
  157. for (i = 1; i < 4; i++)
  158. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
  159. txrc, i, (rix - i), 1,
  160. not_data);
  161. }
  162. }
  163. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
  164. struct rtl_sta_info *sta_entry, u16 tid)
  165. {
  166. struct rtl_mac *mac = rtl_mac(rtlpriv);
  167. if (mac->act_scanning)
  168. return false;
  169. if (mac->opmode == NL80211_IFTYPE_STATION &&
  170. mac->cnt_after_linked < 3)
  171. return false;
  172. if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
  173. return true;
  174. return false;
  175. }
  176. /*mac80211 Rate Control callbacks*/
  177. static void rtl_tx_status(void *ppriv,
  178. struct ieee80211_supported_band *sband,
  179. struct ieee80211_sta *sta, void *priv_sta,
  180. struct sk_buff *skb)
  181. {
  182. struct rtl_priv *rtlpriv = ppriv;
  183. struct rtl_mac *mac = rtl_mac(rtlpriv);
  184. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  185. __le16 fc = rtl_get_fc(skb);
  186. struct rtl_sta_info *sta_entry;
  187. if (!priv_sta || !ieee80211_is_data(fc))
  188. return;
  189. if (rtl_is_special_data(mac->hw, skb, true, true))
  190. return;
  191. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
  192. is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  193. return;
  194. if (sta) {
  195. /* Check if aggregation has to be enabled for this tid */
  196. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  197. if ((sta->ht_cap.ht_supported) &&
  198. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  199. if (ieee80211_is_data_qos(fc)) {
  200. u8 tid = rtl_get_tid(skb);
  201. if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
  202. tid)) {
  203. sta_entry->tids[tid].agg.agg_state =
  204. RTL_AGG_PROGRESS;
  205. ieee80211_start_tx_ba_session(sta, tid,
  206. 5000);
  207. }
  208. }
  209. }
  210. }
  211. }
  212. static void rtl_rate_init(void *ppriv,
  213. struct ieee80211_supported_band *sband,
  214. struct cfg80211_chan_def *chandef,
  215. struct ieee80211_sta *sta, void *priv_sta)
  216. {
  217. }
  218. static void rtl_rate_update(void *ppriv,
  219. struct ieee80211_supported_band *sband,
  220. struct cfg80211_chan_def *chandef,
  221. struct ieee80211_sta *sta, void *priv_sta,
  222. u32 changed)
  223. {
  224. }
  225. static void *rtl_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  226. {
  227. struct rtl_priv *rtlpriv = rtl_priv(hw);
  228. return rtlpriv;
  229. }
  230. static void rtl_rate_free(void *rtlpriv)
  231. {
  232. return;
  233. }
  234. static void *rtl_rate_alloc_sta(void *ppriv,
  235. struct ieee80211_sta *sta, gfp_t gfp)
  236. {
  237. struct rtl_priv *rtlpriv = ppriv;
  238. struct rtl_rate_priv *rate_priv;
  239. rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
  240. if (!rate_priv) {
  241. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  242. "Unable to allocate private rc structure\n");
  243. return NULL;
  244. }
  245. rtlpriv->rate_priv = rate_priv;
  246. return rate_priv;
  247. }
  248. static void rtl_rate_free_sta(void *rtlpriv,
  249. struct ieee80211_sta *sta, void *priv_sta)
  250. {
  251. struct rtl_rate_priv *rate_priv = priv_sta;
  252. kfree(rate_priv);
  253. }
  254. static struct rate_control_ops rtl_rate_ops = {
  255. .name = "rtl_rc",
  256. .alloc = rtl_rate_alloc,
  257. .free = rtl_rate_free,
  258. .alloc_sta = rtl_rate_alloc_sta,
  259. .free_sta = rtl_rate_free_sta,
  260. .rate_init = rtl_rate_init,
  261. .rate_update = rtl_rate_update,
  262. .tx_status = rtl_tx_status,
  263. .get_rate = rtl_get_rate,
  264. };
  265. int rtl_rate_control_register(void)
  266. {
  267. return ieee80211_rate_control_register(&rtl_rate_ops);
  268. }
  269. void rtl_rate_control_unregister(void)
  270. {
  271. ieee80211_rate_control_unregister(&rtl_rate_ops);
  272. }