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