1d27a76faSLarry Finger // SPDX-License-Identifier: GPL-2.0
2d27a76faSLarry Finger /* Copyright(c) 2009-2012 Realtek Corporation.*/
3f1d2b4d3SLarry Finger
4f1d2b4d3SLarry Finger #include "wifi.h"
5f1d2b4d3SLarry Finger #include "base.h"
6f1d2b4d3SLarry Finger #include "rc.h"
7f1d2b4d3SLarry Finger
8f1d2b4d3SLarry Finger /*
9f1d2b4d3SLarry Finger *Finds the highest rate index we can use
10f1d2b4d3SLarry Finger *if skb is special data like DHCP/EAPOL, we set should
11f1d2b4d3SLarry Finger *it to lowest rate CCK_1M, otherwise we set rate to
12f1d2b4d3SLarry Finger *highest rate based on wireless mode used for iwconfig
13f1d2b4d3SLarry Finger *show Tx rate.
14f1d2b4d3SLarry Finger */
_rtl_rc_get_highest_rix(struct rtl_priv * rtlpriv,struct ieee80211_sta * sta,struct sk_buff * skb,bool not_data)15f1d2b4d3SLarry Finger static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
16f1d2b4d3SLarry Finger struct ieee80211_sta *sta,
17f1d2b4d3SLarry Finger struct sk_buff *skb, bool not_data)
18f1d2b4d3SLarry Finger {
19f1d2b4d3SLarry Finger struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
20f1d2b4d3SLarry Finger struct rtl_phy *rtlphy = &(rtlpriv->phy);
21f1d2b4d3SLarry Finger struct rtl_sta_info *sta_entry = NULL;
2273fb2705SLarry Finger u16 wireless_mode = 0;
2350da5da1SPing-Ke Shih u8 nss;
2450da5da1SPing-Ke Shih struct ieee80211_tx_rate rate;
2550da5da1SPing-Ke Shih
2650da5da1SPing-Ke Shih switch (get_rf_type(rtlphy)) {
2750da5da1SPing-Ke Shih case RF_4T4R:
2850da5da1SPing-Ke Shih nss = 4;
2950da5da1SPing-Ke Shih break;
3050da5da1SPing-Ke Shih case RF_3T3R:
3150da5da1SPing-Ke Shih nss = 3;
3250da5da1SPing-Ke Shih break;
3350da5da1SPing-Ke Shih case RF_2T2R:
3450da5da1SPing-Ke Shih nss = 2;
3550da5da1SPing-Ke Shih break;
3650da5da1SPing-Ke Shih default:
3750da5da1SPing-Ke Shih nss = 1;
3850da5da1SPing-Ke Shih break;
3950da5da1SPing-Ke Shih }
40f1d2b4d3SLarry Finger
41f1d2b4d3SLarry Finger /*
42f1d2b4d3SLarry Finger *this rate is no use for true rate, firmware
43f1d2b4d3SLarry Finger *will control rate at all it just used for
44f1d2b4d3SLarry Finger *1.show in iwconfig in B/G mode
45f1d2b4d3SLarry Finger *2.in rtl_get_tcb_desc when we check rate is
46f1d2b4d3SLarry Finger * 1M we will not use FW rate but user rate.
47f1d2b4d3SLarry Finger */
48f1d2b4d3SLarry Finger
49f1d2b4d3SLarry Finger if (sta) {
50f1d2b4d3SLarry Finger sta_entry = (struct rtl_sta_info *)sta->drv_priv;
51f1d2b4d3SLarry Finger wireless_mode = sta_entry->wireless_mode;
52f1d2b4d3SLarry Finger }
53f1d2b4d3SLarry Finger
54f1d2b4d3SLarry Finger if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true, false) ||
55f1d2b4d3SLarry Finger not_data) {
56f1d2b4d3SLarry Finger return 0;
57f1d2b4d3SLarry Finger } else {
58f1d2b4d3SLarry Finger if (rtlhal->current_bandtype == BAND_ON_2_4G) {
59f1d2b4d3SLarry Finger if (wireless_mode == WIRELESS_MODE_B) {
60f1d2b4d3SLarry Finger return B_MODE_MAX_RIX;
61f1d2b4d3SLarry Finger } else if (wireless_mode == WIRELESS_MODE_G) {
62f1d2b4d3SLarry Finger return G_MODE_MAX_RIX;
63f1d2b4d3SLarry Finger } else if (wireless_mode == WIRELESS_MODE_N_24G) {
6450da5da1SPing-Ke Shih if (nss == 1)
65f1d2b4d3SLarry Finger return N_MODE_MCS7_RIX;
66f1d2b4d3SLarry Finger else
67f1d2b4d3SLarry Finger return N_MODE_MCS15_RIX;
68f1d2b4d3SLarry Finger } else if (wireless_mode == WIRELESS_MODE_AC_24G) {
69046d2e7cSSriram R if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) {
7050da5da1SPing-Ke Shih ieee80211_rate_set_vht(&rate,
7150da5da1SPing-Ke Shih AC_MODE_MCS8_RIX,
7250da5da1SPing-Ke Shih nss);
7350da5da1SPing-Ke Shih goto out;
7450da5da1SPing-Ke Shih } else {
7550da5da1SPing-Ke Shih ieee80211_rate_set_vht(&rate,
7650da5da1SPing-Ke Shih AC_MODE_MCS9_RIX,
7750da5da1SPing-Ke Shih nss);
7850da5da1SPing-Ke Shih goto out;
7950da5da1SPing-Ke Shih }
80f1d2b4d3SLarry Finger }
81f1d2b4d3SLarry Finger return 0;
82f1d2b4d3SLarry Finger } else {
83f1d2b4d3SLarry Finger if (wireless_mode == WIRELESS_MODE_A) {
84f1d2b4d3SLarry Finger return A_MODE_MAX_RIX;
85f1d2b4d3SLarry Finger } else if (wireless_mode == WIRELESS_MODE_N_5G) {
8650da5da1SPing-Ke Shih if (nss == 1)
87f1d2b4d3SLarry Finger return N_MODE_MCS7_RIX;
88f1d2b4d3SLarry Finger else
89f1d2b4d3SLarry Finger return N_MODE_MCS15_RIX;
90f1d2b4d3SLarry Finger } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
91046d2e7cSSriram R if (sta->deflink.bandwidth == IEEE80211_STA_RX_BW_20) {
9250da5da1SPing-Ke Shih ieee80211_rate_set_vht(&rate,
9350da5da1SPing-Ke Shih AC_MODE_MCS8_RIX,
9450da5da1SPing-Ke Shih nss);
9550da5da1SPing-Ke Shih goto out;
9650da5da1SPing-Ke Shih } else {
9750da5da1SPing-Ke Shih ieee80211_rate_set_vht(&rate,
9850da5da1SPing-Ke Shih AC_MODE_MCS9_RIX,
9950da5da1SPing-Ke Shih nss);
10050da5da1SPing-Ke Shih goto out;
10150da5da1SPing-Ke Shih }
102f1d2b4d3SLarry Finger }
103f1d2b4d3SLarry Finger return 0;
104f1d2b4d3SLarry Finger }
105f1d2b4d3SLarry Finger }
10650da5da1SPing-Ke Shih
10750da5da1SPing-Ke Shih out:
10850da5da1SPing-Ke Shih return rate.idx;
109f1d2b4d3SLarry Finger }
110f1d2b4d3SLarry Finger
_rtl_rc_rate_set_series(struct rtl_priv * rtlpriv,struct ieee80211_sta * sta,struct ieee80211_tx_rate * rate,struct ieee80211_tx_rate_control * txrc,u8 tries,s8 rix,int rtsctsenable,bool not_data)111f1d2b4d3SLarry Finger static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
112f1d2b4d3SLarry Finger struct ieee80211_sta *sta,
113f1d2b4d3SLarry Finger struct ieee80211_tx_rate *rate,
114f1d2b4d3SLarry Finger struct ieee80211_tx_rate_control *txrc,
11508aba42fSArnd Bergmann u8 tries, s8 rix, int rtsctsenable,
116f1d2b4d3SLarry Finger bool not_data)
117f1d2b4d3SLarry Finger {
118f1d2b4d3SLarry Finger struct rtl_mac *mac = rtl_mac(rtlpriv);
119f1d2b4d3SLarry Finger struct rtl_sta_info *sta_entry = NULL;
12073fb2705SLarry Finger u16 wireless_mode = 0;
121f1d2b4d3SLarry Finger u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
122f1d2b4d3SLarry Finger
123f1d2b4d3SLarry Finger if (sta) {
124046d2e7cSSriram R sgi_20 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
125046d2e7cSSriram R sgi_40 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
126046d2e7cSSriram R sgi_80 = sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
127f1d2b4d3SLarry Finger sta_entry = (struct rtl_sta_info *)sta->drv_priv;
128f1d2b4d3SLarry Finger wireless_mode = sta_entry->wireless_mode;
129f1d2b4d3SLarry Finger }
130f1d2b4d3SLarry Finger rate->count = tries;
131f1d2b4d3SLarry Finger rate->idx = rix >= 0x00 ? rix : 0x00;
132f1d2b4d3SLarry Finger
133f1d2b4d3SLarry Finger if (!not_data) {
134f1d2b4d3SLarry Finger if (txrc->short_preamble)
135f1d2b4d3SLarry Finger rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
136f1d2b4d3SLarry Finger if (mac->opmode == NL80211_IFTYPE_AP ||
137f1d2b4d3SLarry Finger mac->opmode == NL80211_IFTYPE_ADHOC) {
138046d2e7cSSriram R if (sta && (sta->deflink.ht_cap.cap &
139f1d2b4d3SLarry Finger IEEE80211_HT_CAP_SUP_WIDTH_20_40))
140f1d2b4d3SLarry Finger rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
141046d2e7cSSriram R if (sta && sta->deflink.vht_cap.vht_supported)
142f1d2b4d3SLarry Finger rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
143f1d2b4d3SLarry Finger } else {
144f1d2b4d3SLarry Finger if (mac->bw_80)
145f1d2b4d3SLarry Finger rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
14650da5da1SPing-Ke Shih else if (mac->bw_40)
14750da5da1SPing-Ke Shih rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
148f1d2b4d3SLarry Finger }
149f1d2b4d3SLarry Finger
150f1d2b4d3SLarry Finger if (sgi_20 || sgi_40 || sgi_80)
151f1d2b4d3SLarry Finger rate->flags |= IEEE80211_TX_RC_SHORT_GI;
152046d2e7cSSriram R if (sta && sta->deflink.ht_cap.ht_supported &&
153d63589cbSFrank A. Cancio Bello (wireless_mode == WIRELESS_MODE_N_5G ||
154d63589cbSFrank A. Cancio Bello wireless_mode == WIRELESS_MODE_N_24G))
155f1d2b4d3SLarry Finger rate->flags |= IEEE80211_TX_RC_MCS;
156046d2e7cSSriram R if (sta && sta->deflink.vht_cap.vht_supported &&
157d76d65fdSLarry Finger (wireless_mode == WIRELESS_MODE_AC_5G ||
158d76d65fdSLarry Finger wireless_mode == WIRELESS_MODE_AC_24G ||
159d76d65fdSLarry Finger wireless_mode == WIRELESS_MODE_AC_ONLY))
160d76d65fdSLarry Finger rate->flags |= IEEE80211_TX_RC_VHT_MCS;
161f1d2b4d3SLarry Finger }
162f1d2b4d3SLarry Finger }
163f1d2b4d3SLarry Finger
rtl_get_rate(void * ppriv,struct ieee80211_sta * sta,void * priv_sta,struct ieee80211_tx_rate_control * txrc)164f1d2b4d3SLarry Finger static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
165f1d2b4d3SLarry Finger void *priv_sta,
166f1d2b4d3SLarry Finger struct ieee80211_tx_rate_control *txrc)
167f1d2b4d3SLarry Finger {
168f1d2b4d3SLarry Finger struct rtl_priv *rtlpriv = ppriv;
169f1d2b4d3SLarry Finger struct sk_buff *skb = txrc->skb;
170f1d2b4d3SLarry Finger struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
171f1d2b4d3SLarry Finger struct ieee80211_tx_rate *rates = tx_info->control.rates;
172f1d2b4d3SLarry Finger __le16 fc = rtl_get_fc(skb);
173f1d2b4d3SLarry Finger u8 try_per_rate, i, rix;
174f1d2b4d3SLarry Finger bool not_data = !ieee80211_is_data(fc);
175f1d2b4d3SLarry Finger
176f1d2b4d3SLarry Finger rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
177f1d2b4d3SLarry Finger try_per_rate = 1;
178f1d2b4d3SLarry Finger _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
179f1d2b4d3SLarry Finger try_per_rate, rix, 1, not_data);
180f1d2b4d3SLarry Finger
181f1d2b4d3SLarry Finger if (!not_data) {
182f1d2b4d3SLarry Finger for (i = 1; i < 4; i++)
183f1d2b4d3SLarry Finger _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
184f1d2b4d3SLarry Finger txrc, i, (rix - i), 1,
185f1d2b4d3SLarry Finger not_data);
186f1d2b4d3SLarry Finger }
187f1d2b4d3SLarry Finger }
188f1d2b4d3SLarry Finger
_rtl_tx_aggr_check(struct rtl_priv * rtlpriv,struct rtl_sta_info * sta_entry,u16 tid)189f1d2b4d3SLarry Finger static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
190f1d2b4d3SLarry Finger struct rtl_sta_info *sta_entry, u16 tid)
191f1d2b4d3SLarry Finger {
192f1d2b4d3SLarry Finger struct rtl_mac *mac = rtl_mac(rtlpriv);
193f1d2b4d3SLarry Finger
194f1d2b4d3SLarry Finger if (mac->act_scanning)
195f1d2b4d3SLarry Finger return false;
196f1d2b4d3SLarry Finger
197f1d2b4d3SLarry Finger if (mac->opmode == NL80211_IFTYPE_STATION &&
198f1d2b4d3SLarry Finger mac->cnt_after_linked < 3)
199f1d2b4d3SLarry Finger return false;
200f1d2b4d3SLarry Finger
201f1d2b4d3SLarry Finger if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
202f1d2b4d3SLarry Finger return true;
203f1d2b4d3SLarry Finger
204f1d2b4d3SLarry Finger return false;
205f1d2b4d3SLarry Finger }
206f1d2b4d3SLarry Finger
207f1d2b4d3SLarry Finger /*mac80211 Rate Control callbacks*/
rtl_tx_status(void * ppriv,struct ieee80211_supported_band * sband,struct ieee80211_sta * sta,void * priv_sta,struct sk_buff * skb)208f1d2b4d3SLarry Finger static void rtl_tx_status(void *ppriv,
209f1d2b4d3SLarry Finger struct ieee80211_supported_band *sband,
210f1d2b4d3SLarry Finger struct ieee80211_sta *sta, void *priv_sta,
211f1d2b4d3SLarry Finger struct sk_buff *skb)
212f1d2b4d3SLarry Finger {
213f1d2b4d3SLarry Finger struct rtl_priv *rtlpriv = ppriv;
214f1d2b4d3SLarry Finger struct rtl_mac *mac = rtl_mac(rtlpriv);
215f1d2b4d3SLarry Finger struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
216f1d2b4d3SLarry Finger __le16 fc = rtl_get_fc(skb);
217f1d2b4d3SLarry Finger struct rtl_sta_info *sta_entry;
218f1d2b4d3SLarry Finger
219f1d2b4d3SLarry Finger if (!priv_sta || !ieee80211_is_data(fc))
220f1d2b4d3SLarry Finger return;
221f1d2b4d3SLarry Finger
222f1d2b4d3SLarry Finger if (rtl_is_special_data(mac->hw, skb, true, true))
223f1d2b4d3SLarry Finger return;
224f1d2b4d3SLarry Finger
225f1d2b4d3SLarry Finger if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
226f1d2b4d3SLarry Finger is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
227f1d2b4d3SLarry Finger return;
228f1d2b4d3SLarry Finger
229f1d2b4d3SLarry Finger if (sta) {
230f1d2b4d3SLarry Finger /* Check if aggregation has to be enabled for this tid */
231f1d2b4d3SLarry Finger sta_entry = (struct rtl_sta_info *)sta->drv_priv;
232046d2e7cSSriram R if (sta->deflink.ht_cap.ht_supported &&
233f1d2b4d3SLarry Finger !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
234f1d2b4d3SLarry Finger if (ieee80211_is_data_qos(fc)) {
235f1d2b4d3SLarry Finger u8 tid = rtl_get_tid(skb);
236b16abaafSLarry Finger
237f1d2b4d3SLarry Finger if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
238f1d2b4d3SLarry Finger tid)) {
239f1d2b4d3SLarry Finger sta_entry->tids[tid].agg.agg_state =
240f1d2b4d3SLarry Finger RTL_AGG_PROGRESS;
241f1d2b4d3SLarry Finger ieee80211_start_tx_ba_session(sta, tid,
242f1d2b4d3SLarry Finger 5000);
243f1d2b4d3SLarry Finger }
244f1d2b4d3SLarry Finger }
245f1d2b4d3SLarry Finger }
246f1d2b4d3SLarry Finger }
247f1d2b4d3SLarry Finger }
248f1d2b4d3SLarry Finger
rtl_rate_init(void * ppriv,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * priv_sta)249f1d2b4d3SLarry Finger static void rtl_rate_init(void *ppriv,
250f1d2b4d3SLarry Finger struct ieee80211_supported_band *sband,
251f1d2b4d3SLarry Finger struct cfg80211_chan_def *chandef,
252f1d2b4d3SLarry Finger struct ieee80211_sta *sta, void *priv_sta)
253f1d2b4d3SLarry Finger {
254f1d2b4d3SLarry Finger }
255f1d2b4d3SLarry Finger
rtl_rate_update(void * ppriv,struct ieee80211_supported_band * sband,struct cfg80211_chan_def * chandef,struct ieee80211_sta * sta,void * priv_sta,u32 changed)256f1d2b4d3SLarry Finger static void rtl_rate_update(void *ppriv,
257f1d2b4d3SLarry Finger struct ieee80211_supported_band *sband,
258f1d2b4d3SLarry Finger struct cfg80211_chan_def *chandef,
259f1d2b4d3SLarry Finger struct ieee80211_sta *sta, void *priv_sta,
260f1d2b4d3SLarry Finger u32 changed)
261f1d2b4d3SLarry Finger {
262f1d2b4d3SLarry Finger }
263f1d2b4d3SLarry Finger
rtl_rate_alloc(struct ieee80211_hw * hw)2646cb5f3eaSJohannes Berg static void *rtl_rate_alloc(struct ieee80211_hw *hw)
265f1d2b4d3SLarry Finger {
266f1d2b4d3SLarry Finger struct rtl_priv *rtlpriv = rtl_priv(hw);
267f1d2b4d3SLarry Finger return rtlpriv;
268f1d2b4d3SLarry Finger }
269f1d2b4d3SLarry Finger
rtl_rate_free(void * rtlpriv)270f1d2b4d3SLarry Finger static void rtl_rate_free(void *rtlpriv)
271f1d2b4d3SLarry Finger {
272f1d2b4d3SLarry Finger return;
273f1d2b4d3SLarry Finger }
274f1d2b4d3SLarry Finger
rtl_rate_alloc_sta(void * ppriv,struct ieee80211_sta * sta,gfp_t gfp)275f1d2b4d3SLarry Finger static void *rtl_rate_alloc_sta(void *ppriv,
276f1d2b4d3SLarry Finger struct ieee80211_sta *sta, gfp_t gfp)
277f1d2b4d3SLarry Finger {
278f1d2b4d3SLarry Finger struct rtl_priv *rtlpriv = ppriv;
279f1d2b4d3SLarry Finger struct rtl_rate_priv *rate_priv;
280f1d2b4d3SLarry Finger
2812ea70bb0SShreeya Patel rate_priv = kzalloc(sizeof(*rate_priv), gfp);
2822ea70bb0SShreeya Patel if (!rate_priv)
283f1d2b4d3SLarry Finger return NULL;
284f1d2b4d3SLarry Finger
285f1d2b4d3SLarry Finger rtlpriv->rate_priv = rate_priv;
286f1d2b4d3SLarry Finger
287f1d2b4d3SLarry Finger return rate_priv;
288f1d2b4d3SLarry Finger }
289f1d2b4d3SLarry Finger
rtl_rate_free_sta(void * rtlpriv,struct ieee80211_sta * sta,void * priv_sta)290f1d2b4d3SLarry Finger static void rtl_rate_free_sta(void *rtlpriv,
291f1d2b4d3SLarry Finger struct ieee80211_sta *sta, void *priv_sta)
292f1d2b4d3SLarry Finger {
293f1d2b4d3SLarry Finger struct rtl_rate_priv *rate_priv = priv_sta;
294b16abaafSLarry Finger
295f1d2b4d3SLarry Finger kfree(rate_priv);
296f1d2b4d3SLarry Finger }
297f1d2b4d3SLarry Finger
298cb1b8262SBhumika Goyal static const struct rate_control_ops rtl_rate_ops = {
299f1d2b4d3SLarry Finger .name = "rtl_rc",
300f1d2b4d3SLarry Finger .alloc = rtl_rate_alloc,
301f1d2b4d3SLarry Finger .free = rtl_rate_free,
302f1d2b4d3SLarry Finger .alloc_sta = rtl_rate_alloc_sta,
303f1d2b4d3SLarry Finger .free_sta = rtl_rate_free_sta,
304f1d2b4d3SLarry Finger .rate_init = rtl_rate_init,
305f1d2b4d3SLarry Finger .rate_update = rtl_rate_update,
306f1d2b4d3SLarry Finger .tx_status = rtl_tx_status,
307f1d2b4d3SLarry Finger .get_rate = rtl_get_rate,
308f1d2b4d3SLarry Finger };
309f1d2b4d3SLarry Finger
rtl_rate_control_register(void)310f1d2b4d3SLarry Finger int rtl_rate_control_register(void)
311f1d2b4d3SLarry Finger {
312f1d2b4d3SLarry Finger return ieee80211_rate_control_register(&rtl_rate_ops);
313f1d2b4d3SLarry Finger }
314f1d2b4d3SLarry Finger
rtl_rate_control_unregister(void)315f1d2b4d3SLarry Finger void rtl_rate_control_unregister(void)
316f1d2b4d3SLarry Finger {
317f1d2b4d3SLarry Finger ieee80211_rate_control_unregister(&rtl_rate_ops);
318f1d2b4d3SLarry Finger }
319