rc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2010 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 sk_buff *skb, bool not_data)
  40. {
  41. struct rtl_mac *rtlmac = rtl_mac(rtlpriv);
  42. /*
  43. *mgt use 1M, although we have check it
  44. *before this function use rate_control_send_low,
  45. *we still check it here
  46. */
  47. if (not_data)
  48. return rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  49. /*
  50. *this rate is no use for true rate, firmware
  51. *will control rate at all it just used for
  52. *1.show in iwconfig in B/G mode
  53. *2.in rtl_get_tcb_desc when we check rate is
  54. * 1M we will not use FW rate but user rate.
  55. */
  56. if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true)) {
  57. return rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M];
  58. } else {
  59. if (rtlmac->mode == WIRELESS_MODE_B)
  60. return rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
  61. else
  62. return rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
  63. }
  64. }
  65. static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
  66. struct ieee80211_tx_rate *rate,
  67. struct ieee80211_tx_rate_control *txrc,
  68. u8 tries, u8 rix, int rtsctsenable,
  69. bool not_data)
  70. {
  71. struct rtl_mac *mac = rtl_mac(rtlpriv);
  72. rate->count = tries;
  73. rate->idx = (rix > 0x2) ? rix : 0x2;
  74. if (!not_data) {
  75. if (txrc->short_preamble)
  76. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  77. if (mac->bw_40)
  78. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  79. if (mac->sgi_20 || mac->sgi_40)
  80. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  81. if (mac->ht_enable)
  82. rate->flags |= IEEE80211_TX_RC_MCS;
  83. }
  84. }
  85. static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
  86. void *priv_sta, struct ieee80211_tx_rate_control *txrc)
  87. {
  88. struct rtl_priv *rtlpriv = ppriv;
  89. struct sk_buff *skb = txrc->skb;
  90. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  91. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  92. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  93. __le16 fc = hdr->frame_control;
  94. u8 try_per_rate, i, rix;
  95. bool not_data = !ieee80211_is_data(fc);
  96. if (rate_control_send_low(sta, priv_sta, txrc))
  97. return;
  98. rix = _rtl_rc_get_highest_rix(rtlpriv, skb, not_data);
  99. try_per_rate = 1;
  100. _rtl_rc_rate_set_series(rtlpriv, &rates[0], txrc,
  101. try_per_rate, rix, 1, not_data);
  102. if (!not_data) {
  103. for (i = 1; i < 4; i++)
  104. _rtl_rc_rate_set_series(rtlpriv, &rates[i],
  105. txrc, i, (rix - i), 1,
  106. not_data);
  107. }
  108. }
  109. static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv, u16 tid)
  110. {
  111. struct rtl_mac *mac = rtl_mac(rtlpriv);
  112. if (mac->act_scanning)
  113. return false;
  114. if (mac->cnt_after_linked < 3)
  115. return false;
  116. if (mac->tids[tid].agg.agg_state == RTL_AGG_OFF)
  117. return true;
  118. return false;
  119. }
  120. /*mac80211 Rate Control callbacks*/
  121. static void rtl_tx_status(void *ppriv,
  122. struct ieee80211_supported_band *sband,
  123. struct ieee80211_sta *sta, void *priv_sta,
  124. struct sk_buff *skb)
  125. {
  126. struct rtl_priv *rtlpriv = ppriv;
  127. struct rtl_mac *mac = rtl_mac(rtlpriv);
  128. struct ieee80211_hdr *hdr;
  129. __le16 fc;
  130. hdr = (struct ieee80211_hdr *)skb->data;
  131. fc = hdr->frame_control;
  132. if (!priv_sta || !ieee80211_is_data(fc))
  133. return;
  134. if (rtl_is_special_data(mac->hw, skb, true))
  135. return;
  136. if (is_multicast_ether_addr(ieee80211_get_DA(hdr))
  137. || is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
  138. return;
  139. /* Check if aggregation has to be enabled for this tid */
  140. if (conf_is_ht(&mac->hw->conf) &&
  141. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  142. if (ieee80211_is_data_qos(fc)) {
  143. u8 *qc, tid;
  144. qc = ieee80211_get_qos_ctl(hdr);
  145. tid = qc[0] & 0xf;
  146. if (_rtl_tx_aggr_check(rtlpriv, tid))
  147. ieee80211_start_tx_ba_session(sta, tid, 5000);
  148. }
  149. }
  150. }
  151. static void rtl_rate_init(void *ppriv,
  152. struct ieee80211_supported_band *sband,
  153. struct ieee80211_sta *sta, void *priv_sta)
  154. {
  155. struct rtl_priv *rtlpriv = ppriv;
  156. struct rtl_mac *mac = rtl_mac(rtlpriv);
  157. u8 is_ht = conf_is_ht(&mac->hw->conf);
  158. if ((mac->opmode == NL80211_IFTYPE_STATION) ||
  159. (mac->opmode == NL80211_IFTYPE_MESH_POINT) ||
  160. (mac->opmode == NL80211_IFTYPE_ADHOC)) {
  161. switch (sband->band) {
  162. case IEEE80211_BAND_2GHZ:
  163. rtlpriv->rate_priv->cur_ratetab_idx =
  164. RATR_INX_WIRELESS_G;
  165. if (is_ht)
  166. rtlpriv->rate_priv->cur_ratetab_idx =
  167. RATR_INX_WIRELESS_NGB;
  168. break;
  169. case IEEE80211_BAND_5GHZ:
  170. rtlpriv->rate_priv->cur_ratetab_idx =
  171. RATR_INX_WIRELESS_A;
  172. if (is_ht)
  173. rtlpriv->rate_priv->cur_ratetab_idx =
  174. RATR_INX_WIRELESS_NGB;
  175. break;
  176. default:
  177. RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
  178. ("Invalid band\n"));
  179. rtlpriv->rate_priv->cur_ratetab_idx =
  180. RATR_INX_WIRELESS_NGB;
  181. break;
  182. }
  183. RT_TRACE(rtlpriv, COMP_RATE, DBG_DMESG,
  184. ("Choosing rate table index: %d\n",
  185. rtlpriv->rate_priv->cur_ratetab_idx));
  186. }
  187. }
  188. static void rtl_rate_update(void *ppriv,
  189. struct ieee80211_supported_band *sband,
  190. struct ieee80211_sta *sta, void *priv_sta,
  191. u32 changed,
  192. enum nl80211_channel_type oper_chan_type)
  193. {
  194. struct rtl_priv *rtlpriv = ppriv;
  195. struct rtl_mac *mac = rtl_mac(rtlpriv);
  196. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  197. bool oper_cw40 = false, oper_sgi40;
  198. bool local_cw40 = mac->bw_40;
  199. bool local_sgi40 = mac->sgi_40;
  200. u8 is_ht = conf_is_ht(&mac->hw->conf);
  201. if (changed & IEEE80211_RC_HT_CHANGED) {
  202. if (mac->opmode != NL80211_IFTYPE_STATION)
  203. return;
  204. if (rtlhal->hw->conf.channel_type == NL80211_CHAN_HT40MINUS ||
  205. rtlhal->hw->conf.channel_type == NL80211_CHAN_HT40PLUS)
  206. oper_cw40 = true;
  207. oper_sgi40 = mac->sgi_40;
  208. if ((local_cw40 != oper_cw40) || (local_sgi40 != oper_sgi40)) {
  209. switch (sband->band) {
  210. case IEEE80211_BAND_2GHZ:
  211. rtlpriv->rate_priv->cur_ratetab_idx =
  212. RATR_INX_WIRELESS_G;
  213. if (is_ht)
  214. rtlpriv->rate_priv->cur_ratetab_idx =
  215. RATR_INX_WIRELESS_NGB;
  216. break;
  217. case IEEE80211_BAND_5GHZ:
  218. rtlpriv->rate_priv->cur_ratetab_idx =
  219. RATR_INX_WIRELESS_A;
  220. if (is_ht)
  221. rtlpriv->rate_priv->cur_ratetab_idx =
  222. RATR_INX_WIRELESS_NGB;
  223. break;
  224. default:
  225. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  226. ("Invalid band\n"));
  227. rtlpriv->rate_priv->cur_ratetab_idx =
  228. RATR_INX_WIRELESS_NGB;
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. static void *rtl_rate_alloc(struct ieee80211_hw *hw,
  235. struct dentry *debugfsdir)
  236. {
  237. struct rtl_priv *rtlpriv = rtl_priv(hw);
  238. return rtlpriv;
  239. }
  240. static void rtl_rate_free(void *rtlpriv)
  241. {
  242. return;
  243. }
  244. static void *rtl_rate_alloc_sta(void *ppriv,
  245. struct ieee80211_sta *sta, gfp_t gfp)
  246. {
  247. struct rtl_priv *rtlpriv = ppriv;
  248. struct rtl_rate_priv *rate_priv;
  249. rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
  250. if (!rate_priv) {
  251. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  252. ("Unable to allocate private rc structure\n"));
  253. return NULL;
  254. }
  255. rtlpriv->rate_priv = rate_priv;
  256. return rate_priv;
  257. }
  258. static void rtl_rate_free_sta(void *rtlpriv,
  259. struct ieee80211_sta *sta, void *priv_sta)
  260. {
  261. struct rtl_rate_priv *rate_priv = priv_sta;
  262. kfree(rate_priv);
  263. }
  264. static struct rate_control_ops rtl_rate_ops = {
  265. .module = NULL,
  266. .name = "rtl_rc",
  267. .alloc = rtl_rate_alloc,
  268. .free = rtl_rate_free,
  269. .alloc_sta = rtl_rate_alloc_sta,
  270. .free_sta = rtl_rate_free_sta,
  271. .rate_init = rtl_rate_init,
  272. .rate_update = rtl_rate_update,
  273. .tx_status = rtl_tx_status,
  274. .get_rate = rtl_get_rate,
  275. };
  276. int rtl_rate_control_register(void)
  277. {
  278. return ieee80211_rate_control_register(&rtl_rate_ops);
  279. }
  280. void rtl_rate_control_unregister(void)
  281. {
  282. ieee80211_rate_control_unregister(&rtl_rate_ops);
  283. }