GNU Linux-libre 4.19.211-gnu1
[releases.git] / drivers / staging / rtlwifi / rc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2009-2012  Realtek Corporation.
5  *
6  * Contact Information:
7  * wlanfae <wlanfae@realtek.com>
8  * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
9  * Hsinchu 300, Taiwan.
10  *
11  * Larry Finger <Larry.Finger@lwfinger.net>
12  *
13  *****************************************************************************/
14
15 #include "wifi.h"
16 #include "base.h"
17 #include "rc.h"
18
19 /*
20  *Finds the highest rate index we can use
21  *if skb is special data like DHCP/EAPOL, we set should
22  *it to lowest rate CCK_1M, otherwise we set rate to
23  *highest rate based on wireless mode used for iwconfig
24  *show Tx rate.
25  */
26 static u8 _rtl_rc_get_highest_rix(struct rtl_priv *rtlpriv,
27                                   struct ieee80211_sta *sta,
28                                   struct sk_buff *skb, bool not_data)
29 {
30         struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
31         struct rtl_phy *rtlphy = &rtlpriv->phy;
32         struct rtl_sta_info *sta_entry = NULL;
33         u16 wireless_mode = 0;
34         u8 nss; /* NSS -1 */
35
36         if (get_rf_type(rtlphy) >= RF_4T4R)
37                 nss = 3;
38         else if (get_rf_type(rtlphy) >= RF_3T3R)
39                 nss = 2;
40         else if (get_rf_type(rtlphy) >= RF_2T2R)
41                 nss = 1;
42         else
43                 nss = 0;
44
45         /*
46          *this rate is no use for true rate, firmware
47          *will control rate at all it just used for
48          *1.show in iwconfig in B/G mode
49          *2.in rtl_get_tcb_desc when we check rate is
50          *      1M we will not use FW rate but user rate.
51          */
52
53         if (sta) {
54                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
55                 wireless_mode = sta_entry->wireless_mode;
56         }
57
58         if (rtl_is_special_data(rtlpriv->mac80211.hw, skb, true, false) ||
59             not_data) {
60                 return 0;
61         }
62         if (rtlhal->current_bandtype == BAND_ON_2_4G) {
63                 if (wireless_mode == WIRELESS_MODE_B) {
64                         return B_MODE_MAX_RIX;
65                 } else if (wireless_mode == WIRELESS_MODE_G) {
66                         return G_MODE_MAX_RIX;
67                 } else if (wireless_mode == WIRELESS_MODE_N_24G) {
68                         if (nss == 0)
69                                 return N_MODE_MCS7_RIX;
70                         else
71                                 return N_MODE_MCS15_RIX;
72                 } else if (wireless_mode == WIRELESS_MODE_AC_24G) {
73                         if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
74                                 return AC_MODE_MCS8_RIX | (nss << 4);
75                         else
76                                 return AC_MODE_MCS9_RIX | (nss << 4);
77                 }
78                 return 0;
79         }
80         if (wireless_mode == WIRELESS_MODE_A) {
81                 return A_MODE_MAX_RIX;
82         } else if (wireless_mode == WIRELESS_MODE_N_5G) {
83                 if (nss == 0)
84                         return N_MODE_MCS7_RIX;
85                 else
86                         return N_MODE_MCS15_RIX;
87         } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
88                 if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
89                         return AC_MODE_MCS8_RIX | (nss << 4);
90                 else
91                         return AC_MODE_MCS9_RIX | (nss << 4);
92         }
93         return 0;
94 }
95
96 static void _rtl_rc_rate_set_series(struct rtl_priv *rtlpriv,
97                                     struct ieee80211_sta *sta,
98                                     struct ieee80211_tx_rate *rate,
99                                     struct ieee80211_tx_rate_control *txrc,
100                                     u8 tries, s8 rix, int rtsctsenable,
101                                     bool not_data)
102 {
103         struct rtl_mac *mac = rtl_mac(rtlpriv);
104         struct rtl_sta_info *sta_entry = NULL;
105         u16 wireless_mode = 0;
106         u8 sgi_20 = 0, sgi_40 = 0, sgi_80 = 0;
107
108         if (sta) {
109                 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
110                 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
111                 sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
112                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
113                 wireless_mode = sta_entry->wireless_mode;
114         }
115         rate->count = tries;
116         rate->idx = rix >= 0x00 ? rix : 0x00;
117         if ((rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
118              rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8822BE) &&
119             wireless_mode == WIRELESS_MODE_AC_5G)
120                 rate->idx |= 0x10;/*2NSS for 8812AE, 8822BE*/
121
122         if (!not_data) {
123                 if (txrc->short_preamble)
124                         rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
125                 if (mac->opmode == NL80211_IFTYPE_AP ||
126                     mac->opmode == NL80211_IFTYPE_ADHOC) {
127                         if (sta && (sta->ht_cap.cap &
128                                     IEEE80211_HT_CAP_SUP_WIDTH_20_40))
129                                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
130                         if (sta && sta->vht_cap.vht_supported)
131                                 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
132                 } else {
133                         if (mac->bw_80)
134                                 rate->flags |= IEEE80211_TX_RC_80_MHZ_WIDTH;
135                         else if (mac->bw_40)
136                                 rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
137                 }
138
139                 if (sgi_20 || sgi_40 || sgi_80)
140                         rate->flags |= IEEE80211_TX_RC_SHORT_GI;
141                 if (sta && sta->ht_cap.ht_supported &&
142                     (wireless_mode == WIRELESS_MODE_N_5G ||
143                      wireless_mode == WIRELESS_MODE_N_24G))
144                         rate->flags |= IEEE80211_TX_RC_MCS;
145                 if (sta && sta->vht_cap.vht_supported &&
146                     (wireless_mode == WIRELESS_MODE_AC_5G ||
147                      wireless_mode == WIRELESS_MODE_AC_24G ||
148                      wireless_mode == WIRELESS_MODE_AC_ONLY))
149                         rate->flags |= IEEE80211_TX_RC_VHT_MCS;
150         }
151 }
152
153 static void rtl_get_rate(void *ppriv, struct ieee80211_sta *sta,
154                          void *priv_sta,
155                          struct ieee80211_tx_rate_control *txrc)
156 {
157         struct rtl_priv *rtlpriv = ppriv;
158         struct sk_buff *skb = txrc->skb;
159         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
160         struct ieee80211_tx_rate *rates = tx_info->control.rates;
161         __le16 fc = rtl_get_fc(skb);
162         u8 try_per_rate, i, rix;
163         bool not_data = !ieee80211_is_data(fc);
164
165         if (rate_control_send_low(sta, priv_sta, txrc))
166                 return;
167
168         rix = _rtl_rc_get_highest_rix(rtlpriv, sta, skb, not_data);
169         try_per_rate = 1;
170         _rtl_rc_rate_set_series(rtlpriv, sta, &rates[0], txrc,
171                                 try_per_rate, rix, 1, not_data);
172
173         if (!not_data) {
174                 for (i = 1; i < 4; i++)
175                         _rtl_rc_rate_set_series(rtlpriv, sta, &rates[i],
176                                                 txrc, i, (rix - i), 1,
177                                                 not_data);
178         }
179 }
180
181 static bool _rtl_tx_aggr_check(struct rtl_priv *rtlpriv,
182                                struct rtl_sta_info *sta_entry, u16 tid)
183 {
184         struct rtl_mac *mac = rtl_mac(rtlpriv);
185
186         if (mac->act_scanning)
187                 return false;
188
189         if (mac->opmode == NL80211_IFTYPE_STATION &&
190             mac->cnt_after_linked < 3)
191                 return false;
192
193         if (sta_entry->tids[tid].agg.agg_state == RTL_AGG_STOP)
194                 return true;
195
196         return false;
197 }
198
199 /*mac80211 Rate Control callbacks*/
200 static void rtl_tx_status(void *ppriv,
201                           struct ieee80211_supported_band *sband,
202                           struct ieee80211_sta *sta, void *priv_sta,
203                           struct sk_buff *skb)
204 {
205         struct rtl_priv *rtlpriv = ppriv;
206         struct rtl_mac *mac = rtl_mac(rtlpriv);
207         struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
208         __le16 fc = rtl_get_fc(skb);
209         struct rtl_sta_info *sta_entry;
210
211         if (!priv_sta || !ieee80211_is_data(fc))
212                 return;
213
214         if (rtl_is_special_data(mac->hw, skb, true, true))
215                 return;
216
217         if (is_multicast_ether_addr(ieee80211_get_DA(hdr)) ||
218             is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
219                 return;
220
221         if (sta) {
222                 /* Check if aggregation has to be enabled for this tid */
223                 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
224                 if (sta->ht_cap.ht_supported &&
225                     !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
226                         if (ieee80211_is_data_qos(fc)) {
227                                 u8 tid = rtl_get_tid(skb);
228
229                                 if (_rtl_tx_aggr_check(rtlpriv, sta_entry,
230                                                        tid)) {
231                                         sta_entry->tids[tid].agg.agg_state =
232                                                 RTL_AGG_PROGRESS;
233                                         ieee80211_start_tx_ba_session(sta, tid,
234                                                                       5000);
235                                 }
236                         }
237                 }
238         }
239 }
240
241 static void rtl_rate_init(void *ppriv,
242                           struct ieee80211_supported_band *sband,
243                           struct cfg80211_chan_def *chandef,
244                           struct ieee80211_sta *sta, void *priv_sta)
245 {
246 }
247
248 static void rtl_rate_update(void *ppriv,
249                             struct ieee80211_supported_band *sband,
250                             struct cfg80211_chan_def *chandef,
251                             struct ieee80211_sta *sta, void *priv_sta,
252                             u32 changed)
253 {
254 }
255
256 static void *rtl_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
257 {
258         struct rtl_priv *rtlpriv = rtl_priv(hw);
259         return rtlpriv;
260 }
261
262 static void rtl_rate_free(void *rtlpriv)
263 {
264 }
265
266 static void *rtl_rate_alloc_sta(void *ppriv,
267                                 struct ieee80211_sta *sta, gfp_t gfp)
268 {
269         struct rtl_priv *rtlpriv = ppriv;
270         struct rtl_rate_priv *rate_priv;
271
272         rate_priv = kzalloc(sizeof(*rate_priv), gfp);
273         if (!rate_priv)
274                 return NULL;
275
276         rtlpriv->rate_priv = rate_priv;
277
278         return rate_priv;
279 }
280
281 static void rtl_rate_free_sta(void *rtlpriv,
282                               struct ieee80211_sta *sta, void *priv_sta)
283 {
284         struct rtl_rate_priv *rate_priv = priv_sta;
285
286         kfree(rate_priv);
287 }
288
289 static const struct rate_control_ops rtl_rate_ops = {
290         .name = "rtl_rc",
291         .alloc = rtl_rate_alloc,
292         .free = rtl_rate_free,
293         .alloc_sta = rtl_rate_alloc_sta,
294         .free_sta = rtl_rate_free_sta,
295         .rate_init = rtl_rate_init,
296         .rate_update = rtl_rate_update,
297         .tx_status = rtl_tx_status,
298         .get_rate = rtl_get_rate,
299 };
300
301 int rtl_rate_control_register(void)
302 {
303         return ieee80211_rate_control_register(&rtl_rate_ops);
304 }
305
306 void rtl_rate_control_unregister(void)
307 {
308         ieee80211_rate_control_unregister(&rtl_rate_ops);
309 }