rc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. u8 nss;
  44. struct ieee80211_tx_rate rate;
  45. switch (get_rf_type(rtlphy)) {
  46. case RF_4T4R:
  47. nss = 4;
  48. break;
  49. case RF_3T3R:
  50. nss = 3;
  51. break;
  52. case RF_2T2R:
  53. nss = 2;
  54. break;
  55. default:
  56. nss = 1;
  57. break;
  58. }
  59. /*
  60. *this rate is no use for true rate, firmware
  61. *will control rate at all it just used for
  62. *1.show in iwconfig in B/G mode
  63. *2.in rtl_get_tcb_desc when we check rate is
  64. * 1M we will not use FW rate but user rate.
  65. */
  66. if (sta) {
  67. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  68. wireless_mode = sta_entry->wireless_mode;
  69. }
  70. if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true, false) ||
  71. not_data) {
  72. return 0;
  73. } else {
  74. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  75. if (wireless_mode == WIRELESS_MODE_B) {
  76. return B_MODE_MAX_RIX;
  77. } else if (wireless_mode == WIRELESS_MODE_G) {
  78. return G_MODE_MAX_RIX;
  79. } else if (wireless_mode == WIRELESS_MODE_N_24G) {
  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_24G) {
  85. if (sta->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. } else {
  99. if (wireless_mode == WIRELESS_MODE_A) {
  100. return A_MODE_MAX_RIX;
  101. } else if (wireless_mode == WIRELESS_MODE_N_5G) {
  102. if (nss == 1)
  103. return N_MODE_MCS7_RIX;
  104. else
  105. return N_MODE_MCS15_RIX;
  106. } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
  107. if (sta->bandwidth == IEEE80211_STA_RX_BW_20) {
  108. ieee80211_rate_set_vht(&rate,
  109. AC_MODE_MCS8_RIX,
  110. nss);
  111. goto out;
  112. } else {
  113. ieee80211_rate_set_vht(&rate,
  114. AC_MODE_MCS9_RIX,
  115. nss);
  116. goto out;
  117. }
  118. }
  119. return 0;
  120. }
  121. }
  122. out:
  123. return rate.idx;
  124. }
  125. static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
  126. struct ieee80211_sta *sta,
  127. struct ieee80211_tx_rate *rate,
  128. struct ieee80211_tx_rate_control *txrc,
  129. u8 tries, s8 rix, int rtsctsenable,
  130. bool not_data)
  131. {
  132. struct rtl_mac *mac = rtl_mac(rtlpriv);
  133. struct rtl_sta_info *sta_entry = NULL;
  134. u16 wireless_mode = 0;
  135. u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
  136. if (sta) {
  137. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  138. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  139. sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
  140. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  141. wireless_mode = sta_entry->wireless_mode;
  142. }
  143. rate->count = tries;
  144. rate->idx = rix >= 0x00 ? rix : 0x00;
  145. if (!not_data) {
  146. if (txrc->short_preamble)
  147. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  148. if (mac->opmode == NL80211_IFTYPE_AP ||
  149. mac->opmode == NL80211_IFTYPE_ADHOC) {
  150. if (sta && (sta->ht_cap.cap &
  151. IEEE80211_HT_CAP_SUP_WIDTH_20_40))
  152. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  153. if (sta && sta->vht_cap.vht_supported)
  154. rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  155. } else {
  156. if (mac->bw_80)
  157. rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
  158. else if (mac->bw_40)
  159. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  160. }
  161. if (sgi_20 || sgi_40 || sgi_80)
  162. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  163. if (sta && sta->ht_cap.ht_supported &&
  164. (wireless_mode == WIRELESS_MODE_N_5G ||
  165. wireless_mode == WIRELESS_MODE_N_24G))
  166. rate->flags |= IEEE80211_TX_RC_MCS;
  167. if (sta && sta->vht_cap.vht_supported &&
  168. (wireless_mode == WIRELESS_MODE_AC_5G ||
  169. wireless_mode == WIRELESS_MODE_AC_24G ||
  170. wireless_mode == WIRELESS_MODE_AC_ONLY))
  171. rate->flags |= IEEE80211_TX_RC_VHT_MCS;
  172. }
  173. }
  174. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  175. void *priv_sta,
  176. struct ieee80211_tx_rate_control *txrc)
  177. {
  178. struct rtl_priv *rtlpriv = ppriv;
  179. struct sk_buff *skb = txrc->skb;
  180. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  181. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  182. __le16 fc = rtl_get_fc(skb);
  183. u8 try_per_rate, i, rix;
  184. bool not_data = !ieee80211_is_data(fc);
  185. if (rate_control_send_low(sta, priv_sta, txrc))
  186. return;
  187. rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
  188. try_per_rate = 1;
  189. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
  190. try_per_rate, rix, 1, not_data);
  191. if (!not_data) {
  192. for (i = 1; i < 4; i++)
  193. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
  194. txrc, i, (rix - i), 1,
  195. not_data);
  196. }
  197. }
  198. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
  199. struct rtl_sta_info *sta_entry, u16 tid)
  200. {
  201. struct rtl_mac *mac = rtl_mac(rtlpriv);
  202. if (mac->act_scanning)
  203. return false;
  204. if (mac->opmode == NL80211_IFTYPE_STATION &&
  205. mac->cnt_after_linked < 3)
  206. return false;
  207. if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
  208. return true;
  209. return false;
  210. }
  211. /*mac80211 Rate Control callbacks*/
  212. static void rtl_tx_status(void *ppriv,
  213. struct ieee80211_supported_band *sband,
  214. struct ieee80211_sta *sta, void *priv_sta,
  215. struct sk_buff *skb)
  216. {
  217. struct rtl_priv *rtlpriv = ppriv;
  218. struct rtl_mac *mac = rtl_mac(rtlpriv);
  219. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  220. __le16 fc = rtl_get_fc(skb);
  221. struct rtl_sta_info *sta_entry;
  222. if (!priv_sta || !ieee80211_is_data(fc))
  223. return;
  224. if (rtl_is_special_data(mac->hw, skb, true, true))
  225. return;
  226. if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
  227. is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  228. return;
  229. if (sta) {
  230. /* Check if aggregation has to be enabled for this tid */
  231. sta_entry = (struct rtl_sta_info *)sta->drv_priv;
  232. if (sta->ht_cap.ht_supported &&
  233. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  234. if (ieee80211_is_data_qos(fc)) {
  235. u8 tid = rtl_get_tid(skb);
  236. if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
  237. tid)) {
  238. sta_entry->tids[tid].agg.agg_state =
  239. RTL_AGG_PROGRESS;
  240. ieee80211_start_tx_ba_session(sta, tid,
  241. 5000);
  242. }
  243. }
  244. }
  245. }
  246. }
  247. static void rtl_rate_init(void *ppriv,
  248. struct ieee80211_supported_band *sband,
  249. struct cfg80211_chan_def *chandef,
  250. struct ieee80211_sta *sta, void *priv_sta)
  251. {
  252. }
  253. static void rtl_rate_update(void *ppriv,
  254. struct ieee80211_supported_band *sband,
  255. struct cfg80211_chan_def *chandef,
  256. struct ieee80211_sta *sta, void *priv_sta,
  257. u32 changed)
  258. {
  259. }
  260. static void *rtl_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  261. {
  262. struct rtl_priv *rtlpriv = rtl_priv(hw);
  263. return rtlpriv;
  264. }
  265. static void rtl_rate_free(void *rtlpriv)
  266. {
  267. return;
  268. }
  269. static void *rtl_rate_alloc_sta(void *ppriv,
  270. struct ieee80211_sta *sta, gfp_t gfp)
  271. {
  272. struct rtl_priv *rtlpriv = ppriv;
  273. struct rtl_rate_priv *rate_priv;
  274. rate_priv = kzalloc(sizeof(*rate_priv), gfp);
  275. if (!rate_priv)
  276. return NULL;
  277. rtlpriv->rate_priv = rate_priv;
  278. return rate_priv;
  279. }
  280. static void rtl_rate_free_sta(void *rtlpriv,
  281. struct ieee80211_sta *sta, void *priv_sta)
  282. {
  283. struct rtl_rate_priv *rate_priv = priv_sta;
  284. kfree(rate_priv);
  285. }
  286. static const struct rate_control_ops rtl_rate_ops = {
  287. .name = "rtl_rc",
  288. .alloc = rtl_rate_alloc,
  289. .free = rtl_rate_free,
  290. .alloc_sta = rtl_rate_alloc_sta,
  291. .free_sta = rtl_rate_free_sta,
  292. .rate_init = rtl_rate_init,
  293. .rate_update = rtl_rate_update,
  294. .tx_status = rtl_tx_status,
  295. .get_rate = rtl_get_rate,
  296. };
  297. int rtl_rate_control_register(void)
  298. {
  299. return ieee80211_rate_control_register(&rtl_rate_ops);
  300. }
  301. void rtl_rate_control_unregister(void)
  302. {
  303. ieee80211_rate_control_unregister(&rtl_rate_ops);
  304. }