rc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. * Larry Finger <Larry.Finger@lwfinger.net>
  27. *
  28. *****************************************************************************/
  29. #include "wifi.h"
  30. #include "base.h"
  31. #include "rc.h"
  32. /*
  33. *Finds the highest rate index we can use
  34. *if skb is special data like DHCP/EAPOL, we set should
  35. *it to lowest rate CCK_1M, otherwise we set rate to
  36. *CCK11M or OFDM_54M based on wireless mode.
  37. */
  38. static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
  39. struct ieee80211_sta *sta,
  40. struct sk_buff *skb, bool not_data)
  41. {
  42. struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
  43. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  44. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  45. struct rtl_sta_info *sta_entry = NULL;
  46. u8 wireless_mode = 0;
  47. /*
  48. *this rate is no use for true rate, firmware
  49. *will control rate at all it just used for
  50. *1.show in iwconfig in B/G mode
  51. *2.in rtl_get_tcb_desc when we check rate is
  52. * 1M we will not use FW rate but user rate.
  53. */
  54. if (rtlmac->opmode == NL80211_IFTYPE_AP ||
  55. rtlmac->opmode == NL80211_IFTYPE_ADHOC ||
  56. rtlmac->opmode == NL80211_IFTYPE_MESH_POINT) {
  57. if (sta) {
  58. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  59. wireless_mode = sta_entry->wireless_mode;
  60. } else {
  61. return 0;
  62. }
  63. } else {
  64. wireless_mode = rtlmac->mode;
  65. }
  66. if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true) ||
  67. not_data) {
  68. return 0;
  69. } else {
  70. if (rtlhal->current_bandtype == BAND_ON_2_4G) {
  71. if (wireless_mode == WIRELESS_MODE_B) {
  72. return B_MODE_MAX_RIX;
  73. } else if (wireless_mode == WIRELESS_MODE_G) {
  74. return G_MODE_MAX_RIX;
  75. } else {
  76. if (get_rf_type(rtlphy) != RF_2T2R)
  77. return N_MODE_MCS7_RIX;
  78. else
  79. return N_MODE_MCS15_RIX;
  80. }
  81. } else {
  82. if (wireless_mode == WIRELESS_MODE_A) {
  83. return A_MODE_MAX_RIX;
  84. } else {
  85. if (get_rf_type(rtlphy) != RF_2T2R)
  86. return N_MODE_MCS7_RIX;
  87. else
  88. return N_MODE_MCS15_RIX;
  89. }
  90. }
  91. }
  92. }
  93. static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
  94. struct ieee80211_sta *sta,
  95. struct ieee80211_tx_rate *rate,
  96. struct ieee80211_tx_rate_control *txrc,
  97. u8 tries, char rix, int rtsctsenable,
  98. bool not_data)
  99. {
  100. struct rtl_mac *mac = rtl_mac(rtlpriv);
  101. u8 sgi_20 = 0, sgi_40 = 0;
  102. if (sta) {
  103. sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  104. sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  105. }
  106. rate->count = tries;
  107. rate->idx = rix >= 0x00 ? rix : 0x00;
  108. if (!not_data) {
  109. if (txrc->short_preamble)
  110. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  111. if (mac->opmode == NL80211_IFTYPE_AP ||
  112. mac->opmode == NL80211_IFTYPE_ADHOC) {
  113. if (sta && (sta->bandwidth >= IEEE80211_STA_RX_BW_40))
  114. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  115. } else {
  116. if (mac->bw_40)
  117. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  118. }
  119. if (sgi_20 || sgi_40)
  120. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  121. if (sta && sta->ht_cap.ht_supported)
  122. rate->flags |= IEEE80211_TX_RC_MCS;
  123. }
  124. }
  125. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  126. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  127. {
  128. struct rtl_priv *rtlpriv = ppriv;
  129. struct sk_buff *skb = txrc->skb;
  130. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  131. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  132. __le16 fc = rtl_get_fc(skb);
  133. u8 try_per_rate, i, rix;
  134. bool not_data = !ieee80211_is_data(fc);
  135. if (rate_control_send_low(sta, priv_sta, txrc))
  136. return;
  137. rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
  138. try_per_rate = 1;
  139. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
  140. try_per_rate, rix, 1, not_data);
  141. if (!not_data) {
  142. for (i = 1; i < 4; i++)
  143. _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
  144. txrc, i, (rix - i), 1,
  145. not_data);
  146. }
  147. }
  148. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
  149. struct rtl_sta_info *sta_entry, u16 tid)
  150. {
  151. struct rtl_mac *mac = rtl_mac(rtlpriv);
  152. if (mac->act_scanning)
  153. return false;
  154. if (mac->opmode == NL80211_IFTYPE_STATION &&
  155. mac->cnt_after_linked < 3)
  156. return false;
  157. if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
  158. return true;
  159. return false;
  160. }
  161. /*mac80211 Rate Control callbacks*/
  162. static void rtl_tx_status(void *ppriv,
  163. struct ieee80211_supported_band *sband,
  164. struct ieee80211_sta *sta, void *priv_sta,
  165. struct sk_buff *skb)
  166. {
  167. struct rtl_priv *rtlpriv = ppriv;
  168. struct rtl_mac *mac = rtl_mac(rtlpriv);
  169. struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
  170. __le16 fc = rtl_get_fc(skb);
  171. struct rtl_sta_info *sta_entry;
  172. if (!priv_sta || !ieee80211_is_data(fc))
  173. return;
  174. if (rtl_is_special_data(mac->hw, skb, true))
  175. return;
  176. if (is_multicast_ether_addr(ieee80211_get_DA(hdr))
  177. || is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  178. return;
  179. if (sta) {
  180. /* Check if aggregation has to be enabled for this tid */
  181. sta_entry = (struct rtl_sta_info *) sta->drv_priv;
  182. if ((sta->ht_cap.ht_supported) &&
  183. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  184. if (ieee80211_is_data_qos(fc)) {
  185. u8 tid = rtl_get_tid(skb);
  186. if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
  187. tid)) {
  188. sta_entry->tids[tid].agg.agg_state =
  189. RTL_AGG_PROGRESS;
  190. ieee80211_start_tx_ba_session(sta,
  191. tid, 5000);
  192. }
  193. }
  194. }
  195. }
  196. }
  197. static void rtl_rate_init(void *ppriv,
  198. struct ieee80211_supported_band *sband,
  199. struct cfg80211_chan_def *chandef,
  200. struct ieee80211_sta *sta, void *priv_sta)
  201. {
  202. }
  203. static void *rtl_rate_alloc(struct ieee80211_hw *hw,
  204. struct dentry *debugfsdir)
  205. {
  206. struct rtl_priv *rtlpriv = rtl_priv(hw);
  207. return rtlpriv;
  208. }
  209. static void rtl_rate_free(void *rtlpriv)
  210. {
  211. return;
  212. }
  213. static void *rtl_rate_alloc_sta(void *ppriv,
  214. struct ieee80211_sta *sta, gfp_t gfp)
  215. {
  216. struct rtl_priv *rtlpriv = ppriv;
  217. struct rtl_rate_priv *rate_priv;
  218. rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
  219. if (!rate_priv) {
  220. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  221. "Unable to allocate private rc structure\n");
  222. return NULL;
  223. }
  224. rtlpriv->rate_priv = rate_priv;
  225. return rate_priv;
  226. }
  227. static void rtl_rate_free_sta(void *rtlpriv,
  228. struct ieee80211_sta *sta, void *priv_sta)
  229. {
  230. struct rtl_rate_priv *rate_priv = priv_sta;
  231. kfree(rate_priv);
  232. }
  233. static struct rate_control_ops rtl_rate_ops = {
  234. .module = NULL,
  235. .name = "rtl_rc",
  236. .alloc = rtl_rate_alloc,
  237. .free = rtl_rate_free,
  238. .alloc_sta = rtl_rate_alloc_sta,
  239. .free_sta = rtl_rate_free_sta,
  240. .rate_init = rtl_rate_init,
  241. .tx_status = rtl_tx_status,
  242. .get_rate = rtl_get_rate,
  243. };
  244. int rtl_rate_control_register(void)
  245. {
  246. return ieee80211_rate_control_register(&rtl_rate_ops);
  247. }
  248. void rtl_rate_control_unregister(void)
  249. {
  250. ieee80211_rate_control_unregister(&rtl_rate_ops);
  251. }