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