rc.c 8.2 KB

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