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