rc.c 8.0 KB

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