blob: 74c14ce28238eed70f8ad798f0724642147411d3 [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
Larry Fingera8d76062012-01-07 20:46:42 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Larry Finger0c817332010-12-08 11:12:31 -06004 *
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 *
Larry Finger0c817332010-12-08 11:12:31 -060014 * 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
26#include "wifi.h"
27#include "base.h"
28#include "rc.h"
29
30/*
31 *Finds the highest rate index we can use
32 *if skb is special data like DHCP/EAPOL, we set should
33 *it to lowest rate CCK_1M, otherwise we set rate to
Larry Fingerf3a97e92014-09-22 09:39:24 -050034 *highest rate based on wireless mode used for iwconfig
35 *show Tx rate.
Larry Finger0c817332010-12-08 11:12:31 -060036 */
37static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
Chaoming_Lic6a9de02011-04-25 12:53:19 -050038 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -060039 struct sk_buff *skb, bool not_data)
40{
Chaoming_Lic6a9de02011-04-25 12:53:19 -050041 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
42 struct rtl_phy *rtlphy = &(rtlpriv->phy);
43 struct rtl_sta_info *sta_entry = NULL;
44 u8 wireless_mode = 0;
Larry Finger0c817332010-12-08 11:12:31 -060045
46 /*
47 *this rate is no use for true rate, firmware
48 *will control rate at all it just used for
49 *1.show in iwconfig in B/G mode
50 *2.in rtl_get_tcb_desc when we check rate is
51 * 1M we will not use FW rate but user rate.
52 */
Larry Fingerf3a97e92014-09-22 09:39:24 -050053
54 if (sta) {
55 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
56 wireless_mode = sta_entry->wireless_mode;
Chaoming_Lic6a9de02011-04-25 12:53:19 -050057 }
58
Taehee Yoocad737d2015-03-28 00:22:39 +090059 if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true, false) ||
60 not_data) {
Chaoming_Lic6a9de02011-04-25 12:53:19 -050061 return 0;
62 } else {
63 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
64 if (wireless_mode == WIRELESS_MODE_B) {
65 return B_MODE_MAX_RIX;
66 } else if (wireless_mode == WIRELESS_MODE_G) {
67 return G_MODE_MAX_RIX;
Larry Fingerf3a97e92014-09-22 09:39:24 -050068 } else if (wireless_mode == WIRELESS_MODE_N_24G) {
Chaoming_Lic6a9de02011-04-25 12:53:19 -050069 if (get_rf_type(rtlphy) != RF_2T2R)
70 return N_MODE_MCS7_RIX;
71 else
72 return N_MODE_MCS15_RIX;
Larry Fingerf3a97e92014-09-22 09:39:24 -050073 } else if (wireless_mode == WIRELESS_MODE_AC_24G) {
74 return AC_MODE_MCS9_RIX;
Chaoming_Lic6a9de02011-04-25 12:53:19 -050075 }
Larry Fingerf3a97e92014-09-22 09:39:24 -050076 return 0;
Chaoming_Lic6a9de02011-04-25 12:53:19 -050077 } else {
78 if (wireless_mode == WIRELESS_MODE_A) {
79 return A_MODE_MAX_RIX;
Larry Fingerf3a97e92014-09-22 09:39:24 -050080 } else if (wireless_mode == WIRELESS_MODE_N_5G) {
Chaoming_Lic6a9de02011-04-25 12:53:19 -050081 if (get_rf_type(rtlphy) != RF_2T2R)
82 return N_MODE_MCS7_RIX;
83 else
84 return N_MODE_MCS15_RIX;
Larry Fingerf3a97e92014-09-22 09:39:24 -050085 } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
86 return AC_MODE_MCS9_RIX;
Chaoming_Lic6a9de02011-04-25 12:53:19 -050087 }
Larry Fingerf3a97e92014-09-22 09:39:24 -050088 return 0;
Chaoming_Lic6a9de02011-04-25 12:53:19 -050089 }
Larry Finger0c817332010-12-08 11:12:31 -060090 }
91}
92
93static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
Chaoming_Lic6a9de02011-04-25 12:53:19 -050094 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -060095 struct ieee80211_tx_rate *rate,
96 struct ieee80211_tx_rate_control *txrc,
Chaoming_Lic6a9de02011-04-25 12:53:19 -050097 u8 tries, char rix, int rtsctsenable,
Larry Finger0c817332010-12-08 11:12:31 -060098 bool not_data)
99{
100 struct rtl_mac *mac = rtl_mac(rtlpriv);
Larry Fingerf3a97e92014-09-22 09:39:24 -0500101 struct rtl_sta_info *sta_entry = NULL;
102 u8 wireless_mode = 0;
103 u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600104
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500105 if (sta) {
106 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
107 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
Larry Fingerf3a97e92014-09-22 09:39:24 -0500108 sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
109 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
110 wireless_mode = sta_entry->wireless_mode;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500111 }
Larry Finger0c817332010-12-08 11:12:31 -0600112 rate->count = tries;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500113 rate->idx = rix >= 0x00 ? rix : 0x00;
Larry Fingerf3a97e92014-09-22 09:39:24 -0500114 if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE &&
115 wireless_mode == WIRELESS_MODE_AC_5G)
116 rate->idx += 0x10;/*2NSS for 8812AE*/
Larry Finger0c817332010-12-08 11:12:31 -0600117
118 if (!not_data) {
119 if (txrc->short_preamble)
120 rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500121 if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Fingerf3a97e92014-09-22 09:39:24 -0500122 mac->opmode == NL80211_IFTYPE_ADHOC) {
123 if (sta && (sta->ht_cap.cap &
124 IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500125 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Larry Fingerf3a97e92014-09-22 09:39:24 -0500126 if (sta && (sta->vht_cap.vht_supported))
127 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500128 } else {
129 if (mac->bw_40)
130 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
Larry Fingerf3a97e92014-09-22 09:39:24 -0500131 if (mac->bw_80)
132 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500133 }
Larry Fingerf3a97e92014-09-22 09:39:24 -0500134
135 if (sgi_20 || sgi_40 || sgi_80)
Larry Finger0c817332010-12-08 11:12:31 -0600136 rate->flags |= IEEE80211_TX_RC_SHORT_GI;
Larry Fingerf3a97e92014-09-22 09:39:24 -0500137 if (sta && sta->ht_cap.ht_supported &&
138 ((wireless_mode == WIRELESS_MODE_N_5G) ||
139 (wireless_mode == WIRELESS_MODE_N_24G)))
Larry Finger0c817332010-12-08 11:12:31 -0600140 rate->flags |= IEEE80211_TX_RC_MCS;
141 }
142}
143
144static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
Larry Fingerf3a97e92014-09-22 09:39:24 -0500145 void *priv_sta,
146 struct ieee80211_tx_rate_control *txrc)
Larry Finger0c817332010-12-08 11:12:31 -0600147{
148 struct rtl_priv *rtlpriv = ppriv;
149 struct sk_buff *skb = txrc->skb;
150 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
151 struct ieee80211_tx_rate *rates = tx_info->control.rates;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500152 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -0600153 u8 try_per_rate, i, rix;
154 bool not_data = !ieee80211_is_data(fc);
155
156 if (rate_control_send_low(sta, priv_sta, txrc))
157 return;
158
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500159 rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
Larry Finger0c817332010-12-08 11:12:31 -0600160 try_per_rate = 1;
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500161 _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
Larry Finger0c817332010-12-08 11:12:31 -0600162 try_per_rate, rix, 1, not_data);
163
164 if (!not_data) {
165 for (i = 1; i < 4; i++)
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500166 _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
Larry Finger0c817332010-12-08 11:12:31 -0600167 txrc, i, (rix - i), 1,
168 not_data);
169 }
170}
171
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500172static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
Larry Fingerf3a97e92014-09-22 09:39:24 -0500173 struct rtl_sta_info *sta_entry, u16 tid)
Larry Finger0c817332010-12-08 11:12:31 -0600174{
175 struct rtl_mac *mac = rtl_mac(rtlpriv);
176
177 if (mac->act_scanning)
178 return false;
179
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500180 if (mac->opmode == NL80211_IFTYPE_STATION &&
Larry Fingerf3a97e92014-09-22 09:39:24 -0500181 mac->cnt_after_linked < 3)
Larry Finger0c817332010-12-08 11:12:31 -0600182 return false;
183
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500184 if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
Larry Finger0c817332010-12-08 11:12:31 -0600185 return true;
186
187 return false;
188}
189
190/*mac80211 Rate Control callbacks*/
191static void rtl_tx_status(void *ppriv,
192 struct ieee80211_supported_band *sband,
193 struct ieee80211_sta *sta, void *priv_sta,
194 struct sk_buff *skb)
195{
196 struct rtl_priv *rtlpriv = ppriv;
197 struct rtl_mac *mac = rtl_mac(rtlpriv);
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500198 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
199 __le16 fc = rtl_get_fc(skb);
200 struct rtl_sta_info *sta_entry;
Larry Finger0c817332010-12-08 11:12:31 -0600201
202 if (!priv_sta || !ieee80211_is_data(fc))
203 return;
204
Taehee Yoocad737d2015-03-28 00:22:39 +0900205 if (rtl_is_special_data(mac->hw, skb, true, true))
Larry Finger0c817332010-12-08 11:12:31 -0600206 return;
207
Larry Fingerf3a97e92014-09-22 09:39:24 -0500208 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
209 is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
Larry Finger0c817332010-12-08 11:12:31 -0600210 return;
211
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500212 if (sta) {
213 /* Check if aggregation has to be enabled for this tid */
214 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
Mike McCormacke10542c2011-06-20 10:47:51 +0900215 if ((sta->ht_cap.ht_supported) &&
Larry Fingerf3a97e92014-09-22 09:39:24 -0500216 !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500217 if (ieee80211_is_data_qos(fc)) {
218 u8 tid = rtl_get_tid(skb);
219 if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
Larry Fingerf3a97e92014-09-22 09:39:24 -0500220 tid)) {
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500221 sta_entry->tids[tid].agg.agg_state =
Larry Fingerf3a97e92014-09-22 09:39:24 -0500222 RTL_AGG_PROGRESS;
223 ieee80211_start_tx_ba_session(sta, tid,
224 5000);
Chaoming_Lic6a9de02011-04-25 12:53:19 -0500225 }
226 }
Larry Finger0c817332010-12-08 11:12:31 -0600227 }
228 }
229}
230
Catalin Iacobc88d0dc2013-02-11 22:18:05 +0100231static void rtl_rate_init(void *ppriv,
232 struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +0200233 struct cfg80211_chan_def *chandef,
Catalin Iacobc88d0dc2013-02-11 22:18:05 +0100234 struct ieee80211_sta *sta, void *priv_sta)
235{
236}
237
Larry Fingerf3a97e92014-09-22 09:39:24 -0500238static void rtl_rate_update(void *ppriv,
239 struct ieee80211_supported_band *sband,
240 struct cfg80211_chan_def *chandef,
241 struct ieee80211_sta *sta, void *priv_sta,
242 u32 changed)
243{
244}
245
246static void *rtl_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Larry Finger0c817332010-12-08 11:12:31 -0600247{
248 struct rtl_priv *rtlpriv = rtl_priv(hw);
249 return rtlpriv;
250}
251
252static void rtl_rate_free(void *rtlpriv)
253{
254 return;
255}
256
257static void *rtl_rate_alloc_sta(void *ppriv,
258 struct ieee80211_sta *sta, gfp_t gfp)
259{
260 struct rtl_priv *rtlpriv = ppriv;
261 struct rtl_rate_priv *rate_priv;
262
263 rate_priv = kzalloc(sizeof(struct rtl_rate_priv), gfp);
264 if (!rate_priv) {
265 RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
Joe Perchesf30d7502012-01-04 19:40:41 -0800266 "Unable to allocate private rc structure\n");
Larry Finger0c817332010-12-08 11:12:31 -0600267 return NULL;
268 }
269
270 rtlpriv->rate_priv = rate_priv;
271
272 return rate_priv;
273}
274
275static void rtl_rate_free_sta(void *rtlpriv,
276 struct ieee80211_sta *sta, void *priv_sta)
277{
278 struct rtl_rate_priv *rate_priv = priv_sta;
279 kfree(rate_priv);
280}
281
Larry Fingerf3a97e92014-09-22 09:39:24 -0500282static struct rate_control_ops rtl_rate_ops = {
Larry Finger0c817332010-12-08 11:12:31 -0600283 .name = "rtl_rc",
284 .alloc = rtl_rate_alloc,
285 .free = rtl_rate_free,
286 .alloc_sta = rtl_rate_alloc_sta,
287 .free_sta = rtl_rate_free_sta,
Catalin Iacobc88d0dc2013-02-11 22:18:05 +0100288 .rate_init = rtl_rate_init,
Larry Fingerf3a97e92014-09-22 09:39:24 -0500289 .rate_update = rtl_rate_update,
Larry Finger0c817332010-12-08 11:12:31 -0600290 .tx_status = rtl_tx_status,
291 .get_rate = rtl_get_rate,
292};
293
294int rtl_rate_control_register(void)
295{
296 return ieee80211_rate_control_register(&rtl_rate_ops);
297}
298
299void rtl_rate_control_unregister(void)
300{
301 ieee80211_rate_control_unregister(&rtl_rate_ops);
302}