blob: bdc379178e87955c5456028a43657f97862670af [file] [log] [blame]
Larry Finger0c817332010-12-08 11:12:31 -06001/******************************************************************************
2 *
Larry Fingera8d76062012-01-07 20:46:42 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Larry Finger0c817332010-12-08 11:12:31 -06004 *
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 *
Larry Finger0c817332010-12-08 11:12:31 -060014 * 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
Larry Finger0c817332010-12-08 11:12:31 -060026#include "wifi.h"
27#include "rc.h"
28#include "base.h"
29#include "efuse.h"
30#include "cam.h"
31#include "ps.h"
32#include "regd.h"
Larry Finger9afa2e42014-09-22 09:39:21 -050033#include "pci.h"
Larry Fingerd273bb22012-01-27 13:59:25 -060034#include <linux/ip.h>
35#include <linux/module.h>
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -050036#include <linux/udp.h>
Larry Fingerd273bb22012-01-27 13:59:25 -060037
Larry Finger0c817332010-12-08 11:12:31 -060038/*
Tristan Pourcelote1fa7462012-02-09 14:48:09 +010039 *NOTICE!!!: This file will be very big, we should
40 *keep it clear under following roles:
Larry Finger0c817332010-12-08 11:12:31 -060041 *
Tristan Pourcelote1fa7462012-02-09 14:48:09 +010042 *This file include following parts, so, if you add new
Larry Finger0c817332010-12-08 11:12:31 -060043 *functions into this file, please check which part it
44 *should includes. or check if you should add new part
45 *for this file:
46 *
47 *1) mac80211 init functions
48 *2) tx information functions
49 *3) functions called by core.c
50 *4) wq & timer callback functions
51 *5) frame process functions
Chaoming_Liacd48572011-04-25 12:52:54 -050052 *6) IOT functions
53 *7) sysfs functions
Larry Finger26634c42013-03-24 22:06:33 -050054 *8) vif functions
55 *9) ...
Larry Finger0c817332010-12-08 11:12:31 -060056 */
57
58/*********************************************************
59 *
60 * mac80211 init functions
61 *
62 *********************************************************/
Chaoming_Liacd48572011-04-25 12:52:54 -050063static struct ieee80211_channel rtl_channeltable_2g[] = {
Larry Finger0c817332010-12-08 11:12:31 -060064 {.center_freq = 2412, .hw_value = 1,},
65 {.center_freq = 2417, .hw_value = 2,},
66 {.center_freq = 2422, .hw_value = 3,},
67 {.center_freq = 2427, .hw_value = 4,},
68 {.center_freq = 2432, .hw_value = 5,},
69 {.center_freq = 2437, .hw_value = 6,},
70 {.center_freq = 2442, .hw_value = 7,},
71 {.center_freq = 2447, .hw_value = 8,},
72 {.center_freq = 2452, .hw_value = 9,},
73 {.center_freq = 2457, .hw_value = 10,},
74 {.center_freq = 2462, .hw_value = 11,},
75 {.center_freq = 2467, .hw_value = 12,},
76 {.center_freq = 2472, .hw_value = 13,},
77 {.center_freq = 2484, .hw_value = 14,},
78};
79
Chaoming_Liacd48572011-04-25 12:52:54 -050080static struct ieee80211_channel rtl_channeltable_5g[] = {
81 {.center_freq = 5180, .hw_value = 36,},
82 {.center_freq = 5200, .hw_value = 40,},
83 {.center_freq = 5220, .hw_value = 44,},
84 {.center_freq = 5240, .hw_value = 48,},
85 {.center_freq = 5260, .hw_value = 52,},
86 {.center_freq = 5280, .hw_value = 56,},
87 {.center_freq = 5300, .hw_value = 60,},
88 {.center_freq = 5320, .hw_value = 64,},
89 {.center_freq = 5500, .hw_value = 100,},
90 {.center_freq = 5520, .hw_value = 104,},
91 {.center_freq = 5540, .hw_value = 108,},
92 {.center_freq = 5560, .hw_value = 112,},
93 {.center_freq = 5580, .hw_value = 116,},
94 {.center_freq = 5600, .hw_value = 120,},
95 {.center_freq = 5620, .hw_value = 124,},
96 {.center_freq = 5640, .hw_value = 128,},
97 {.center_freq = 5660, .hw_value = 132,},
98 {.center_freq = 5680, .hw_value = 136,},
99 {.center_freq = 5700, .hw_value = 140,},
100 {.center_freq = 5745, .hw_value = 149,},
101 {.center_freq = 5765, .hw_value = 153,},
102 {.center_freq = 5785, .hw_value = 157,},
103 {.center_freq = 5805, .hw_value = 161,},
104 {.center_freq = 5825, .hw_value = 165,},
105};
106
107static struct ieee80211_rate rtl_ratetable_2g[] = {
Larry Finger0c817332010-12-08 11:12:31 -0600108 {.bitrate = 10, .hw_value = 0x00,},
109 {.bitrate = 20, .hw_value = 0x01,},
110 {.bitrate = 55, .hw_value = 0x02,},
111 {.bitrate = 110, .hw_value = 0x03,},
112 {.bitrate = 60, .hw_value = 0x04,},
113 {.bitrate = 90, .hw_value = 0x05,},
114 {.bitrate = 120, .hw_value = 0x06,},
115 {.bitrate = 180, .hw_value = 0x07,},
116 {.bitrate = 240, .hw_value = 0x08,},
117 {.bitrate = 360, .hw_value = 0x09,},
118 {.bitrate = 480, .hw_value = 0x0a,},
119 {.bitrate = 540, .hw_value = 0x0b,},
120};
121
Chaoming_Liacd48572011-04-25 12:52:54 -0500122static struct ieee80211_rate rtl_ratetable_5g[] = {
123 {.bitrate = 60, .hw_value = 0x04,},
124 {.bitrate = 90, .hw_value = 0x05,},
125 {.bitrate = 120, .hw_value = 0x06,},
126 {.bitrate = 180, .hw_value = 0x07,},
127 {.bitrate = 240, .hw_value = 0x08,},
128 {.bitrate = 360, .hw_value = 0x09,},
129 {.bitrate = 480, .hw_value = 0x0a,},
130 {.bitrate = 540, .hw_value = 0x0b,},
131};
132
Larry Finger0c817332010-12-08 11:12:31 -0600133static const struct ieee80211_supported_band rtl_band_2ghz = {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200134 .band = NL80211_BAND_2GHZ,
Larry Finger0c817332010-12-08 11:12:31 -0600135
Chaoming_Liacd48572011-04-25 12:52:54 -0500136 .channels = rtl_channeltable_2g,
137 .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
Larry Finger0c817332010-12-08 11:12:31 -0600138
Chaoming_Liacd48572011-04-25 12:52:54 -0500139 .bitrates = rtl_ratetable_2g,
140 .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
Larry Finger0c817332010-12-08 11:12:31 -0600141
142 .ht_cap = {0},
143};
144
Chaoming_Liacd48572011-04-25 12:52:54 -0500145static struct ieee80211_supported_band rtl_band_5ghz = {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200146 .band = NL80211_BAND_5GHZ,
Chaoming_Liacd48572011-04-25 12:52:54 -0500147
148 .channels = rtl_channeltable_5g,
149 .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
150
151 .bitrates = rtl_ratetable_5g,
152 .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
153
154 .ht_cap = {0},
155};
156
157static const u8 tid_to_ac[] = {
158 2, /* IEEE80211_AC_BE */
159 3, /* IEEE80211_AC_BK */
160 3, /* IEEE80211_AC_BK */
161 2, /* IEEE80211_AC_BE */
162 1, /* IEEE80211_AC_VI */
163 1, /* IEEE80211_AC_VI */
164 0, /* IEEE80211_AC_VO */
165 0, /* IEEE80211_AC_VO */
166};
167
Larry Finger2a00def2012-07-11 14:32:33 -0500168u8 rtl_tid_to_ac(u8 tid)
Chaoming_Liacd48572011-04-25 12:52:54 -0500169{
170 return tid_to_ac[tid];
171}
Larry Finger6f334c22013-07-12 15:32:15 -0500172EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
Chaoming_Liacd48572011-04-25 12:52:54 -0500173
Larry Finger0c817332010-12-08 11:12:31 -0600174static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
175 struct ieee80211_sta_ht_cap *ht_cap)
176{
177 struct rtl_priv *rtlpriv = rtl_priv(hw);
178 struct rtl_phy *rtlphy = &(rtlpriv->phy);
179
180 ht_cap->ht_supported = true;
181 ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
182 IEEE80211_HT_CAP_SGI_40 |
183 IEEE80211_HT_CAP_SGI_20 |
184 IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
185
Chaoming_Liacd48572011-04-25 12:52:54 -0500186 if (rtlpriv->rtlhal.disable_amsdu_8k)
187 ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
188
Larry Finger0c817332010-12-08 11:12:31 -0600189 /*
190 *Maximum length of AMPDU that the STA can receive.
191 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
192 */
193 ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
194
195 /*Minimum MPDU start spacing , */
196 ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
197
198 ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
199
Johannes Berg57fbcce2016-04-12 15:56:15 +0200200 /*hw->wiphy->bands[NL80211_BAND_2GHZ]
Larry Finger0c817332010-12-08 11:12:31 -0600201 *base on ant_num
202 *rx_mask: RX mask
Larry Finger26634c42013-03-24 22:06:33 -0500203 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
204 *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
205 *if rx_ant >= 3 rx_mask[2]= 0xff;
206 *if BW_40 rx_mask[4]= 0x01;
Larry Finger0c817332010-12-08 11:12:31 -0600207 *highest supported RX rate
208 */
Larry Finger26634c42013-03-24 22:06:33 -0500209 if (rtlpriv->dm.supp_phymode_switch) {
Larry Fingerb03d9682016-12-15 12:22:58 -0600210 pr_info("Support phy mode switch\n");
Larry Finger0c817332010-12-08 11:12:31 -0600211
212 ht_cap->mcs.rx_mask[0] = 0xFF;
213 ht_cap->mcs.rx_mask[1] = 0xFF;
214 ht_cap->mcs.rx_mask[4] = 0x01;
215
Larry Finger17c9ac62011-02-19 16:29:57 -0600216 ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
Larry Finger26634c42013-03-24 22:06:33 -0500217 } else {
218 if (get_rf_type(rtlphy) == RF_1T2R ||
219 get_rf_type(rtlphy) == RF_2T2R) {
220 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
221 "1T2R or 2T2R\n");
222 ht_cap->mcs.rx_mask[0] = 0xFF;
223 ht_cap->mcs.rx_mask[1] = 0xFF;
224 ht_cap->mcs.rx_mask[4] = 0x01;
Larry Finger0c817332010-12-08 11:12:31 -0600225
Larry Finger26634c42013-03-24 22:06:33 -0500226 ht_cap->mcs.rx_highest =
227 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
228 } else if (get_rf_type(rtlphy) == RF_1T1R) {
229 RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
Larry Finger0c817332010-12-08 11:12:31 -0600230
Larry Finger26634c42013-03-24 22:06:33 -0500231 ht_cap->mcs.rx_mask[0] = 0xFF;
232 ht_cap->mcs.rx_mask[1] = 0x00;
233 ht_cap->mcs.rx_mask[4] = 0x01;
Larry Finger0c817332010-12-08 11:12:31 -0600234
Larry Finger26634c42013-03-24 22:06:33 -0500235 ht_cap->mcs.rx_highest =
236 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
237 }
Larry Finger0c817332010-12-08 11:12:31 -0600238 }
239}
240
Larry Finger9afa2e42014-09-22 09:39:21 -0500241static void _rtl_init_hw_vht_capab(struct ieee80211_hw *hw,
242 struct ieee80211_sta_vht_cap *vht_cap)
243{
244 struct rtl_priv *rtlpriv = rtl_priv(hw);
245 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
246
247 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
248 u16 mcs_map;
249
250 vht_cap->vht_supported = true;
251 vht_cap->cap =
252 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
253 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
254 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
255 IEEE80211_VHT_CAP_SHORT_GI_80 |
256 IEEE80211_VHT_CAP_TXSTBC |
257 IEEE80211_VHT_CAP_RXSTBC_1 |
258 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
259 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
260 IEEE80211_VHT_CAP_HTC_VHT |
261 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
262 IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
263 IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
264 0;
265
266 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
267 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
268 IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
269 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
270 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
271 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
272 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
273 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
274
275 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
276 vht_cap->vht_mcs.rx_highest =
277 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
278 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
279 vht_cap->vht_mcs.tx_highest =
280 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
281 } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
282 u16 mcs_map;
283
284 vht_cap->vht_supported = true;
285 vht_cap->cap =
286 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895 |
287 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991 |
288 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
289 IEEE80211_VHT_CAP_SHORT_GI_80 |
290 IEEE80211_VHT_CAP_TXSTBC |
291 IEEE80211_VHT_CAP_RXSTBC_1 |
292 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
293 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
294 IEEE80211_VHT_CAP_HTC_VHT |
295 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
296 IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
297 IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
298 0;
299
300 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
301 IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
302 IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
303 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
304 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
305 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
306 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
307 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
308
309 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
310 vht_cap->vht_mcs.rx_highest =
311 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
312 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
313 vht_cap->vht_mcs.tx_highest =
314 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
315 }
316}
317
Larry Finger0c817332010-12-08 11:12:31 -0600318static void _rtl_init_mac80211(struct ieee80211_hw *hw)
319{
Chaoming_Liacd48572011-04-25 12:52:54 -0500320 struct rtl_priv *rtlpriv = rtl_priv(hw);
321 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
Larry Finger0c817332010-12-08 11:12:31 -0600322 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
323 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
324 struct ieee80211_supported_band *sband;
325
Larry Finger9afa2e42014-09-22 09:39:21 -0500326 if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
327 rtlhal->bandset == BAND_ON_BOTH) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500328 /* 1: 2.4 G bands */
329 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200330 sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
Larry Finger0c817332010-12-08 11:12:31 -0600331
Johannes Berg57fbcce2016-04-12 15:56:15 +0200332 /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500333 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200334 memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]), &rtl_band_2ghz,
Chaoming_Liacd48572011-04-25 12:52:54 -0500335 sizeof(struct ieee80211_supported_band));
Larry Finger0c817332010-12-08 11:12:31 -0600336
Chaoming_Liacd48572011-04-25 12:52:54 -0500337 /* <3> init ht cap base on ant_num */
338 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
Larry Finger0c817332010-12-08 11:12:31 -0600339
Chaoming_Liacd48572011-04-25 12:52:54 -0500340 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200341 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500342
343 /* 2: 5 G bands */
344 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200345 sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
Chaoming_Liacd48572011-04-25 12:52:54 -0500346
Johannes Berg57fbcce2016-04-12 15:56:15 +0200347 /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500348 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200349 memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]), &rtl_band_5ghz,
Chaoming_Liacd48572011-04-25 12:52:54 -0500350 sizeof(struct ieee80211_supported_band));
351
352 /* <3> init ht cap base on ant_num */
353 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
354
Larry Finger9afa2e42014-09-22 09:39:21 -0500355 _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
Chaoming_Liacd48572011-04-25 12:52:54 -0500356 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200357 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500358 } else {
359 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
360 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200361 sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
Chaoming_Liacd48572011-04-25 12:52:54 -0500362
Johannes Berg57fbcce2016-04-12 15:56:15 +0200363 /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500364 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200365 memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]),
Larry Finger9afa2e42014-09-22 09:39:21 -0500366 &rtl_band_2ghz,
367 sizeof(struct ieee80211_supported_band));
Chaoming_Liacd48572011-04-25 12:52:54 -0500368
369 /* <3> init ht cap base on ant_num */
370 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
371
372 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200373 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500374 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
375 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200376 sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
Chaoming_Liacd48572011-04-25 12:52:54 -0500377
Johannes Berg57fbcce2016-04-12 15:56:15 +0200378 /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500379 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200380 memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]),
Larry Finger9afa2e42014-09-22 09:39:21 -0500381 &rtl_band_5ghz,
382 sizeof(struct ieee80211_supported_band));
Chaoming_Liacd48572011-04-25 12:52:54 -0500383
384 /* <3> init ht cap base on ant_num */
385 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
386
Larry Finger9afa2e42014-09-22 09:39:21 -0500387 _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
Chaoming_Liacd48572011-04-25 12:52:54 -0500388 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200389 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500390 } else {
Larry Fingerb03d9682016-12-15 12:22:58 -0600391 pr_err("Err BAND %d\n",
392 rtlhal->current_bandtype);
Chaoming_Liacd48572011-04-25 12:52:54 -0500393 }
394 }
Larry Finger0c817332010-12-08 11:12:31 -0600395 /* <5> set hw caps */
Johannes Berg30686bf2015-06-02 21:39:54 +0200396 ieee80211_hw_set(hw, SIGNAL_DBM);
397 ieee80211_hw_set(hw, RX_INCLUDES_FCS);
398 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
399 ieee80211_hw_set(hw, CONNECTION_MONITOR);
400 ieee80211_hw_set(hw, MFP_CAPABLE);
401 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
Larry Finger0c817332010-12-08 11:12:31 -0600402
Chaoming_Liacd48572011-04-25 12:52:54 -0500403 /* swlps or hwlps has been set in diff chip in init_sw_vars */
Johannes Berg30686bf2015-06-02 21:39:54 +0200404 if (rtlpriv->psc.swctrl_lps) {
405 ieee80211_hw_set(hw, SUPPORTS_PS);
406 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
407 }
Larry Finger0c817332010-12-08 11:12:31 -0600408 hw->wiphy->interface_modes =
Chaoming_Liacd48572011-04-25 12:52:54 -0500409 BIT(NL80211_IFTYPE_AP) |
410 BIT(NL80211_IFTYPE_STATION) |
Larry Finger26634c42013-03-24 22:06:33 -0500411 BIT(NL80211_IFTYPE_ADHOC) |
412 BIT(NL80211_IFTYPE_MESH_POINT) |
413 BIT(NL80211_IFTYPE_P2P_CLIENT) |
414 BIT(NL80211_IFTYPE_P2P_GO);
Larry Finger26634c42013-03-24 22:06:33 -0500415 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
Larry Finger9afa2e42014-09-22 09:39:21 -0500416
417 hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
418
Larry Finger0c817332010-12-08 11:12:31 -0600419 hw->wiphy->rts_threshold = 2347;
420
421 hw->queues = AC_MAX;
422 hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
423
424 /* TODO: Correct this value for our hw */
425 /* TODO: define these hard code value */
Chaoming_Liacd48572011-04-25 12:52:54 -0500426 hw->max_listen_interval = 10;
Larry Finger0c817332010-12-08 11:12:31 -0600427 hw->max_rate_tries = 4;
428 /* hw->max_rates = 1; */
Chaoming_Liacd48572011-04-25 12:52:54 -0500429 hw->sta_data_size = sizeof(struct rtl_sta_info);
Larry Finger0c817332010-12-08 11:12:31 -0600430
Larry Finger9afa2e42014-09-22 09:39:21 -0500431/* wowlan is not supported by kernel if CONFIG_PM is not defined */
432#ifdef CONFIG_PM
433 if (rtlpriv->psc.wo_wlan_mode) {
434 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
435 rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
436 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
437 rtlpriv->wowlan.n_patterns =
438 MAX_SUPPORT_WOL_PATTERN_NUM;
439 rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
440 rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
441 }
442 hw->wiphy->wowlan = &rtlpriv->wowlan;
443 }
444#endif
445
Larry Finger0c817332010-12-08 11:12:31 -0600446 /* <6> mac address */
447 if (is_valid_ether_addr(rtlefuse->dev_addr)) {
448 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
449 } else {
Larry Fingerabfabc92011-11-17 12:14:44 -0600450 u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
451 get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
452 SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
Larry Finger0c817332010-12-08 11:12:31 -0600453 }
Larry Finger0c817332010-12-08 11:12:31 -0600454}
455
456static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
457{
458 struct rtl_priv *rtlpriv = rtl_priv(hw);
459
460 /* <1> timer */
Larry Finger0c817332010-12-08 11:12:31 -0600461 setup_timer(&rtlpriv->works.watchdog_timer,
462 rtl_watch_dog_timer_callback, (unsigned long)hw);
Larry Finger26634c42013-03-24 22:06:33 -0500463 setup_timer(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
464 rtl_easy_concurrent_retrytimer_callback, (unsigned long)hw);
Larry Finger0c817332010-12-08 11:12:31 -0600465 /* <2> work queue */
466 rtlpriv->works.hw = hw;
Kees Cook598a0df2014-10-20 14:57:08 -0700467 rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
Larry Finger0c817332010-12-08 11:12:31 -0600468 INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
469 (void *)rtl_watchdog_wq_callback);
470 INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
471 (void *)rtl_ips_nic_off_wq_callback);
Chaoming_Liacd48572011-04-25 12:52:54 -0500472 INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
473 (void *)rtl_swlps_wq_callback);
474 INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
475 (void *)rtl_swlps_rfon_wq_callback);
Larry Finger26634c42013-03-24 22:06:33 -0500476 INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
477 (void *)rtl_fwevt_wq_callback);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600478 INIT_DELAYED_WORK(&rtlpriv->works.c2hcmd_wq,
479 (void *)rtl_c2hcmd_wq_callback);
Larry Finger0c817332010-12-08 11:12:31 -0600480
481}
482
483void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
484{
485 struct rtl_priv *rtlpriv = rtl_priv(hw);
486
487 del_timer_sync(&rtlpriv->works.watchdog_timer);
488
489 cancel_delayed_work(&rtlpriv->works.watchdog_wq);
490 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
Chaoming_Liacd48572011-04-25 12:52:54 -0500491 cancel_delayed_work(&rtlpriv->works.ps_work);
492 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
Larry Finger26634c42013-03-24 22:06:33 -0500493 cancel_delayed_work(&rtlpriv->works.fwevt_wq);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600494 cancel_delayed_work(&rtlpriv->works.c2hcmd_wq);
Larry Finger0c817332010-12-08 11:12:31 -0600495}
Larry Finger6f334c22013-07-12 15:32:15 -0500496EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
Larry Finger0c817332010-12-08 11:12:31 -0600497
498void rtl_init_rfkill(struct ieee80211_hw *hw)
499{
500 struct rtl_priv *rtlpriv = rtl_priv(hw);
501
502 bool radio_state;
503 bool blocked;
504 u8 valid = 0;
505
Chaoming Li228bdfc2011-04-10 18:30:23 -0500506 /*set init state to on */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000507 rtlpriv->rfkill.rfkill_state = true;
Chaoming Li228bdfc2011-04-10 18:30:23 -0500508 wiphy_rfkill_set_hw_state(hw->wiphy, 0);
509
Larry Finger0c817332010-12-08 11:12:31 -0600510 radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
511
Larry Fingere6d8a812010-12-21 19:40:40 -0600512 if (valid) {
Larry Finger9afa2e42014-09-22 09:39:21 -0500513 pr_info("rtlwifi: wireless switch is %s\n",
Joe Perches292b1192011-07-20 08:51:35 -0700514 rtlpriv->rfkill.rfkill_state ? "on" : "off");
Chaoming Li228bdfc2011-04-10 18:30:23 -0500515
Larry Finger0c817332010-12-08 11:12:31 -0600516 rtlpriv->rfkill.rfkill_state = radio_state;
517
518 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
519 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
520 }
521
522 wiphy_rfkill_start_polling(hw->wiphy);
523}
Larry Fingerb0302ab2012-01-30 09:54:49 -0600524EXPORT_SYMBOL(rtl_init_rfkill);
Larry Finger0c817332010-12-08 11:12:31 -0600525
526void rtl_deinit_rfkill(struct ieee80211_hw *hw)
527{
528 wiphy_rfkill_stop_polling(hw->wiphy);
529}
Larry Finger6f334c22013-07-12 15:32:15 -0500530EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
Larry Finger0c817332010-12-08 11:12:31 -0600531
532int rtl_init_core(struct ieee80211_hw *hw)
533{
534 struct rtl_priv *rtlpriv = rtl_priv(hw);
535 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
536
537 /* <1> init mac80211 */
538 _rtl_init_mac80211(hw);
539 rtlmac->hw = hw;
540
541 /* <2> rate control register */
Chaoming Li375ff4c2011-02-28 16:40:28 -0600542 hw->rate_control_algorithm = "rtl_rc";
Larry Finger0c817332010-12-08 11:12:31 -0600543
544 /*
545 * <3> init CRDA must come after init
546 * mac80211 hw in _rtl_init_mac80211.
547 */
548 if (rtl_regd_init(hw, rtl_reg_notifier)) {
Larry Fingerb03d9682016-12-15 12:22:58 -0600549 pr_err("REGD init failed\n");
Larry Finger0c817332010-12-08 11:12:31 -0600550 return 1;
Larry Finger0c817332010-12-08 11:12:31 -0600551 }
552
553 /* <4> locks */
Larry Finger8a09d6d2010-12-16 11:13:57 -0600554 mutex_init(&rtlpriv->locks.conf_mutex);
Larry Fingerb9116b9a2011-12-16 21:17:16 -0600555 spin_lock_init(&rtlpriv->locks.ips_lock);
Larry Finger0c817332010-12-08 11:12:31 -0600556 spin_lock_init(&rtlpriv->locks.irq_th_lock);
557 spin_lock_init(&rtlpriv->locks.h2c_lock);
558 spin_lock_init(&rtlpriv->locks.rf_ps_lock);
559 spin_lock_init(&rtlpriv->locks.rf_lock);
Chaoming_Liacd48572011-04-25 12:52:54 -0500560 spin_lock_init(&rtlpriv->locks.waitq_lock);
Larry Finger26634c42013-03-24 22:06:33 -0500561 spin_lock_init(&rtlpriv->locks.entry_list_lock);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600562 spin_lock_init(&rtlpriv->locks.c2hcmd_lock);
Chaoming_Liacd48572011-04-25 12:52:54 -0500563 spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
Larry Finger26634c42013-03-24 22:06:33 -0500564 spin_lock_init(&rtlpriv->locks.check_sendpkt_lock);
565 spin_lock_init(&rtlpriv->locks.fw_ps_lock);
566 spin_lock_init(&rtlpriv->locks.lps_lock);
Larry Finger9afa2e42014-09-22 09:39:21 -0500567 spin_lock_init(&rtlpriv->locks.iqk_lock);
Larry Finger26634c42013-03-24 22:06:33 -0500568 /* <5> init list */
569 INIT_LIST_HEAD(&rtlpriv->entry_list);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600570 INIT_LIST_HEAD(&rtlpriv->c2hcmd_list);
Larry Finger0c817332010-12-08 11:12:31 -0600571
572 rtlmac->link_state = MAC80211_NOLINK;
573
Larry Finger26634c42013-03-24 22:06:33 -0500574 /* <6> init deferred work */
Larry Finger0c817332010-12-08 11:12:31 -0600575 _rtl_init_deferred_work(hw);
576
577 return 0;
578}
Larry Finger6f334c22013-07-12 15:32:15 -0500579EXPORT_SYMBOL_GPL(rtl_init_core);
Larry Finger0c817332010-12-08 11:12:31 -0600580
581void rtl_deinit_core(struct ieee80211_hw *hw)
582{
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600583 rtl_c2hcmd_launcher(hw, 0);
Larry Finger0c817332010-12-08 11:12:31 -0600584}
Larry Finger6f334c22013-07-12 15:32:15 -0500585EXPORT_SYMBOL_GPL(rtl_deinit_core);
Larry Finger0c817332010-12-08 11:12:31 -0600586
587void rtl_init_rx_config(struct ieee80211_hw *hw)
588{
589 struct rtl_priv *rtlpriv = rtl_priv(hw);
590 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
591
592 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
Larry Finger0c817332010-12-08 11:12:31 -0600593}
Larry Finger6f334c22013-07-12 15:32:15 -0500594EXPORT_SYMBOL_GPL(rtl_init_rx_config);
Larry Finger0c817332010-12-08 11:12:31 -0600595
596/*********************************************************
597 *
598 * tx information functions
599 *
600 *********************************************************/
601static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
602 struct rtl_tcb_desc *tcb_desc,
603 struct ieee80211_tx_info *info)
604{
605 struct rtl_priv *rtlpriv = rtl_priv(hw);
606 u8 rate_flag = info->control.rates[0].flags;
607
608 tcb_desc->use_shortpreamble = false;
609
610 /* 1M can only use Long Preamble. 11B spec */
611 if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
612 return;
613 else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
614 tcb_desc->use_shortpreamble = true;
615
616 return;
617}
618
619static void _rtl_query_shortgi(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500620 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600621 struct rtl_tcb_desc *tcb_desc,
622 struct ieee80211_tx_info *info)
623{
624 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
625 u8 rate_flag = info->control.rates[0].flags;
Chaoming_Liacd48572011-04-25 12:52:54 -0500626 u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
Larry Finger9afa2e42014-09-22 09:39:21 -0500627 u8 sgi_80 = 0, bw_80 = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600628 tcb_desc->use_shortgi = false;
629
Chaoming_Liacd48572011-04-25 12:52:54 -0500630 if (sta == NULL)
Larry Finger0c817332010-12-08 11:12:31 -0600631 return;
632
Chaoming_Liacd48572011-04-25 12:52:54 -0500633 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
634 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
Larry Finger9afa2e42014-09-22 09:39:21 -0500635 sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
Chaoming_Liacd48572011-04-25 12:52:54 -0500636
Larry Finger9afa2e42014-09-22 09:39:21 -0500637 if ((!sta->ht_cap.ht_supported) && (!sta->vht_cap.vht_supported))
Larry Finger0c817332010-12-08 11:12:31 -0600638 return;
639
Chaoming_Liacd48572011-04-25 12:52:54 -0500640 if (!sgi_40 && !sgi_20)
641 return;
642
Larry Finger9afa2e42014-09-22 09:39:21 -0500643 if (mac->opmode == NL80211_IFTYPE_STATION) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500644 bw_40 = mac->bw_40;
Larry Finger9afa2e42014-09-22 09:39:21 -0500645 bw_80 = mac->bw_80;
646 } else if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Finger26634c42013-03-24 22:06:33 -0500647 mac->opmode == NL80211_IFTYPE_ADHOC ||
Larry Finger9afa2e42014-09-22 09:39:21 -0500648 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
649 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
650 bw_80 = sta->vht_cap.vht_supported;
651 }
Chaoming_Liacd48572011-04-25 12:52:54 -0500652
Larry Finger9afa2e42014-09-22 09:39:21 -0500653 if (bw_80) {
654 if (sgi_80)
655 tcb_desc->use_shortgi = true;
656 else
657 tcb_desc->use_shortgi = false;
658 } else {
659 if (bw_40 && sgi_40)
660 tcb_desc->use_shortgi = true;
661 else if (!bw_40 && sgi_20)
662 tcb_desc->use_shortgi = true;
663 }
Larry Finger0c817332010-12-08 11:12:31 -0600664
665 if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
666 tcb_desc->use_shortgi = false;
Larry Finger0c817332010-12-08 11:12:31 -0600667}
668
669static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
670 struct rtl_tcb_desc *tcb_desc,
671 struct ieee80211_tx_info *info)
672{
673 struct rtl_priv *rtlpriv = rtl_priv(hw);
674 u8 rate_flag = info->control.rates[0].flags;
675
676 /* Common Settings */
Larry Finger7ea47242011-02-19 16:28:57 -0600677 tcb_desc->rts_stbc = false;
678 tcb_desc->cts_enable = false;
Larry Finger0c817332010-12-08 11:12:31 -0600679 tcb_desc->rts_sc = 0;
Larry Finger7ea47242011-02-19 16:28:57 -0600680 tcb_desc->rts_bw = false;
681 tcb_desc->rts_use_shortpreamble = false;
682 tcb_desc->rts_use_shortgi = false;
Larry Finger0c817332010-12-08 11:12:31 -0600683
684 if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
685 /* Use CTS-to-SELF in protection mode. */
Larry Finger7ea47242011-02-19 16:28:57 -0600686 tcb_desc->rts_enable = true;
687 tcb_desc->cts_enable = true;
Larry Finger0c817332010-12-08 11:12:31 -0600688 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
689 } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
690 /* Use RTS-CTS in protection mode. */
Larry Finger7ea47242011-02-19 16:28:57 -0600691 tcb_desc->rts_enable = true;
Larry Finger0c817332010-12-08 11:12:31 -0600692 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
693 }
Larry Finger0c817332010-12-08 11:12:31 -0600694}
695
696static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500697 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600698 struct rtl_tcb_desc *tcb_desc)
699{
700 struct rtl_priv *rtlpriv = rtl_priv(hw);
701 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -0500702 struct rtl_sta_info *sta_entry = NULL;
703 u8 ratr_index = 7;
Larry Finger0c817332010-12-08 11:12:31 -0600704
Chaoming_Liacd48572011-04-25 12:52:54 -0500705 if (sta) {
706 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
707 ratr_index = sta_entry->ratr_index;
708 }
Larry Finger0c817332010-12-08 11:12:31 -0600709 if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500710 if (mac->opmode == NL80211_IFTYPE_STATION) {
Larry Finger0c817332010-12-08 11:12:31 -0600711 tcb_desc->ratr_index = 0;
Larry Finger26634c42013-03-24 22:06:33 -0500712 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
Larry Finger9afa2e42014-09-22 09:39:21 -0500713 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
Larry Finger7ea47242011-02-19 16:28:57 -0600714 if (tcb_desc->multicast || tcb_desc->broadcast) {
Larry Finger0c817332010-12-08 11:12:31 -0600715 tcb_desc->hw_rate =
716 rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
717 tcb_desc->use_driver_rate = 1;
Larry Finger26634c42013-03-24 22:06:33 -0500718 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -0600719 } else {
Larry Finger26634c42013-03-24 22:06:33 -0500720 tcb_desc->ratr_index = ratr_index;
Larry Finger0c817332010-12-08 11:12:31 -0600721 }
Chaoming_Liacd48572011-04-25 12:52:54 -0500722 } else if (mac->opmode == NL80211_IFTYPE_AP) {
723 tcb_desc->ratr_index = ratr_index;
Larry Finger0c817332010-12-08 11:12:31 -0600724 }
725 }
726
Larry Finger7ea47242011-02-19 16:28:57 -0600727 if (rtlpriv->dm.useramask) {
Larry Finger26634c42013-03-24 22:06:33 -0500728 tcb_desc->ratr_index = ratr_index;
729 /* TODO we will differentiate adhoc and station future */
730 if (mac->opmode == NL80211_IFTYPE_STATION ||
731 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500732 tcb_desc->mac_id = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600733
Larry Finger9afa2e42014-09-22 09:39:21 -0500734 if (mac->mode == WIRELESS_MODE_AC_5G)
735 tcb_desc->ratr_index =
736 RATR_INX_WIRELESS_AC_5N;
737 else if (mac->mode == WIRELESS_MODE_AC_24G)
738 tcb_desc->ratr_index =
739 RATR_INX_WIRELESS_AC_24N;
740 else if (mac->mode == WIRELESS_MODE_N_24G)
Chaoming_Liacd48572011-04-25 12:52:54 -0500741 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
742 else if (mac->mode == WIRELESS_MODE_N_5G)
743 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
744 else if (mac->mode & WIRELESS_MODE_G)
745 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
746 else if (mac->mode & WIRELESS_MODE_B)
747 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
748 else if (mac->mode & WIRELESS_MODE_A)
749 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
Larry Finger9afa2e42014-09-22 09:39:21 -0500750
Chaoming_Liacd48572011-04-25 12:52:54 -0500751 } else if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Finger9afa2e42014-09-22 09:39:21 -0500752 mac->opmode == NL80211_IFTYPE_ADHOC) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500753 if (NULL != sta) {
754 if (sta->aid > 0)
755 tcb_desc->mac_id = sta->aid + 1;
756 else
757 tcb_desc->mac_id = 1;
758 } else {
759 tcb_desc->mac_id = 0;
760 }
Larry Finger0c817332010-12-08 11:12:31 -0600761 }
762 }
Larry Finger0c817332010-12-08 11:12:31 -0600763}
764
765static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500766 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600767 struct rtl_tcb_desc *tcb_desc)
768{
769 struct rtl_priv *rtlpriv = rtl_priv(hw);
770 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
771
Larry Finger7ea47242011-02-19 16:28:57 -0600772 tcb_desc->packet_bw = false;
Chaoming_Liacd48572011-04-25 12:52:54 -0500773 if (!sta)
Larry Finger0c817332010-12-08 11:12:31 -0600774 return;
Chaoming_Liacd48572011-04-25 12:52:54 -0500775 if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Finger26634c42013-03-24 22:06:33 -0500776 mac->opmode == NL80211_IFTYPE_ADHOC ||
777 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
Larry Finger9afa2e42014-09-22 09:39:21 -0500778 if (!(sta->ht_cap.ht_supported) ||
779 !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Chaoming_Liacd48572011-04-25 12:52:54 -0500780 return;
781 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
782 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
783 return;
784 }
Larry Finger7ea47242011-02-19 16:28:57 -0600785 if (tcb_desc->multicast || tcb_desc->broadcast)
Larry Finger0c817332010-12-08 11:12:31 -0600786 return;
787
788 /*use legency rate, shall use 20MHz */
789 if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
790 return;
791
Larry Finger9afa2e42014-09-22 09:39:21 -0500792 tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
793
794 if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
795 rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8821AE) {
796 if (mac->opmode == NL80211_IFTYPE_AP ||
797 mac->opmode == NL80211_IFTYPE_ADHOC ||
798 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
799 if (!(sta->vht_cap.vht_supported))
800 return;
801 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
802 if (!mac->bw_80 ||
803 !(sta->vht_cap.vht_supported))
804 return;
805 }
806 if (tcb_desc->hw_rate <=
807 rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
808 return;
809 tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
810 }
Larry Finger0c817332010-12-08 11:12:31 -0600811}
812
Larry Finger9afa2e42014-09-22 09:39:21 -0500813static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
814 struct ieee80211_sta *sta)
Larry Finger0c817332010-12-08 11:12:31 -0600815{
816 struct rtl_priv *rtlpriv = rtl_priv(hw);
817 struct rtl_phy *rtlphy = &(rtlpriv->phy);
818 u8 hw_rate;
Larry Finger9afa2e42014-09-22 09:39:21 -0500819 u16 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
Larry Finger0c817332010-12-08 11:12:31 -0600820
Larry Finger9afa2e42014-09-22 09:39:21 -0500821 if ((get_rf_type(rtlphy) == RF_2T2R) &&
822 (tx_mcs_map & 0x000c) != 0x000c) {
823 if ((tx_mcs_map & 0x000c) >> 2 ==
824 IEEE80211_VHT_MCS_SUPPORT_0_7)
825 hw_rate =
826 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
827 else if ((tx_mcs_map & 0x000c) >> 2 ==
828 IEEE80211_VHT_MCS_SUPPORT_0_8)
829 hw_rate =
830 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
831 else
832 hw_rate =
833 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
834 } else {
835 if ((tx_mcs_map & 0x0003) ==
836 IEEE80211_VHT_MCS_SUPPORT_0_7)
837 hw_rate =
838 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
839 else if ((tx_mcs_map & 0x0003) ==
840 IEEE80211_VHT_MCS_SUPPORT_0_8)
841 hw_rate =
842 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
843 else
844 hw_rate =
845 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
846 }
847
848 return hw_rate;
849}
850
851static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
852 struct ieee80211_sta *sta)
853{
854 struct rtl_priv *rtlpriv = rtl_priv(hw);
855 struct rtl_phy *rtlphy = &rtlpriv->phy;
856 u8 hw_rate;
857
858 if ((get_rf_type(rtlphy) == RF_2T2R) &&
859 (sta->ht_cap.mcs.rx_mask[1] != 0))
Larry Finger0c817332010-12-08 11:12:31 -0600860 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
861 else
862 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
863
864 return hw_rate;
865}
866
Larry Finger7ad0ce32011-08-22 16:50:14 -0500867/* mac80211's rate_idx is like this:
868 *
Johannes Berg57fbcce2016-04-12 15:56:15 +0200869 * 2.4G band:rx_status->band == NL80211_BAND_2GHZ
Larry Finger7ad0ce32011-08-22 16:50:14 -0500870 *
871 * B/G rate:
872 * (rx_status->flag & RX_FLAG_HT) = 0,
Larry Fingere0e776a2014-12-18 03:05:36 -0600873 * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
Larry Finger7ad0ce32011-08-22 16:50:14 -0500874 *
875 * N rate:
876 * (rx_status->flag & RX_FLAG_HT) = 1,
Larry Fingere0e776a2014-12-18 03:05:36 -0600877 * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
Larry Finger7ad0ce32011-08-22 16:50:14 -0500878 *
Johannes Berg57fbcce2016-04-12 15:56:15 +0200879 * 5G band:rx_status->band == NL80211_BAND_5GHZ
Larry Finger7ad0ce32011-08-22 16:50:14 -0500880 * A rate:
881 * (rx_status->flag & RX_FLAG_HT) = 0,
Larry Fingere0e776a2014-12-18 03:05:36 -0600882 * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
Larry Finger7ad0ce32011-08-22 16:50:14 -0500883 *
884 * N rate:
885 * (rx_status->flag & RX_FLAG_HT) = 1,
Larry Fingere0e776a2014-12-18 03:05:36 -0600886 * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
Larry Fingerfd3cb222014-12-18 03:05:40 -0600887 *
888 * VHT rates:
889 * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
890 * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
Larry Finger7ad0ce32011-08-22 16:50:14 -0500891 */
Larry Fingerfd3cb222014-12-18 03:05:40 -0600892int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
893 u8 desc_rate)
Larry Finger7ad0ce32011-08-22 16:50:14 -0500894{
895 int rate_idx;
896
Larry Fingerfd3cb222014-12-18 03:05:40 -0600897 if (isvht) {
898 switch (desc_rate) {
899 case DESC_RATEVHT1SS_MCS0:
900 rate_idx = 0;
901 break;
902 case DESC_RATEVHT1SS_MCS1:
903 rate_idx = 1;
904 break;
905 case DESC_RATEVHT1SS_MCS2:
906 rate_idx = 2;
907 break;
908 case DESC_RATEVHT1SS_MCS3:
909 rate_idx = 3;
910 break;
911 case DESC_RATEVHT1SS_MCS4:
912 rate_idx = 4;
913 break;
914 case DESC_RATEVHT1SS_MCS5:
915 rate_idx = 5;
916 break;
917 case DESC_RATEVHT1SS_MCS6:
918 rate_idx = 6;
919 break;
920 case DESC_RATEVHT1SS_MCS7:
921 rate_idx = 7;
922 break;
923 case DESC_RATEVHT1SS_MCS8:
924 rate_idx = 8;
925 break;
926 case DESC_RATEVHT1SS_MCS9:
927 rate_idx = 9;
928 break;
929 case DESC_RATEVHT2SS_MCS0:
930 rate_idx = 0;
931 break;
932 case DESC_RATEVHT2SS_MCS1:
933 rate_idx = 1;
934 break;
935 case DESC_RATEVHT2SS_MCS2:
936 rate_idx = 2;
937 break;
938 case DESC_RATEVHT2SS_MCS3:
939 rate_idx = 3;
940 break;
941 case DESC_RATEVHT2SS_MCS4:
942 rate_idx = 4;
943 break;
944 case DESC_RATEVHT2SS_MCS5:
945 rate_idx = 5;
946 break;
947 case DESC_RATEVHT2SS_MCS6:
948 rate_idx = 6;
949 break;
950 case DESC_RATEVHT2SS_MCS7:
951 rate_idx = 7;
952 break;
953 case DESC_RATEVHT2SS_MCS8:
954 rate_idx = 8;
955 break;
956 case DESC_RATEVHT2SS_MCS9:
957 rate_idx = 9;
958 break;
959 default:
960 rate_idx = 0;
961 break;
962 }
963 return rate_idx;
964 }
Larry Finger7ad0ce32011-08-22 16:50:14 -0500965 if (false == isht) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200966 if (NL80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
Larry Finger7ad0ce32011-08-22 16:50:14 -0500967 switch (desc_rate) {
Larry Fingere0e776a2014-12-18 03:05:36 -0600968 case DESC_RATE1M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500969 rate_idx = 0;
970 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600971 case DESC_RATE2M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500972 rate_idx = 1;
973 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600974 case DESC_RATE5_5M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500975 rate_idx = 2;
976 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600977 case DESC_RATE11M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500978 rate_idx = 3;
979 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600980 case DESC_RATE6M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500981 rate_idx = 4;
982 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600983 case DESC_RATE9M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500984 rate_idx = 5;
985 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600986 case DESC_RATE12M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500987 rate_idx = 6;
988 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600989 case DESC_RATE18M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500990 rate_idx = 7;
991 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600992 case DESC_RATE24M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500993 rate_idx = 8;
994 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600995 case DESC_RATE36M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500996 rate_idx = 9;
997 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600998 case DESC_RATE48M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500999 rate_idx = 10;
1000 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001001 case DESC_RATE54M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001002 rate_idx = 11;
1003 break;
1004 default:
1005 rate_idx = 0;
1006 break;
1007 }
1008 } else {
1009 switch (desc_rate) {
Larry Fingere0e776a2014-12-18 03:05:36 -06001010 case DESC_RATE6M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001011 rate_idx = 0;
1012 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001013 case DESC_RATE9M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001014 rate_idx = 1;
1015 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001016 case DESC_RATE12M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001017 rate_idx = 2;
1018 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001019 case DESC_RATE18M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001020 rate_idx = 3;
1021 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001022 case DESC_RATE24M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001023 rate_idx = 4;
1024 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001025 case DESC_RATE36M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001026 rate_idx = 5;
1027 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001028 case DESC_RATE48M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001029 rate_idx = 6;
1030 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001031 case DESC_RATE54M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001032 rate_idx = 7;
1033 break;
1034 default:
1035 rate_idx = 0;
1036 break;
1037 }
1038 }
Larry Finger7ad0ce32011-08-22 16:50:14 -05001039 } else {
Larry Finger7ad0ce32011-08-22 16:50:14 -05001040 switch (desc_rate) {
Larry Fingere0e776a2014-12-18 03:05:36 -06001041 case DESC_RATEMCS0:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001042 rate_idx = 0;
1043 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001044 case DESC_RATEMCS1:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001045 rate_idx = 1;
1046 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001047 case DESC_RATEMCS2:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001048 rate_idx = 2;
1049 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001050 case DESC_RATEMCS3:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001051 rate_idx = 3;
1052 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001053 case DESC_RATEMCS4:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001054 rate_idx = 4;
1055 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001056 case DESC_RATEMCS5:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001057 rate_idx = 5;
1058 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001059 case DESC_RATEMCS6:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001060 rate_idx = 6;
1061 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001062 case DESC_RATEMCS7:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001063 rate_idx = 7;
1064 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001065 case DESC_RATEMCS8:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001066 rate_idx = 8;
1067 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001068 case DESC_RATEMCS9:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001069 rate_idx = 9;
1070 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001071 case DESC_RATEMCS10:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001072 rate_idx = 10;
1073 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001074 case DESC_RATEMCS11:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001075 rate_idx = 11;
1076 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001077 case DESC_RATEMCS12:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001078 rate_idx = 12;
1079 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001080 case DESC_RATEMCS13:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001081 rate_idx = 13;
1082 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001083 case DESC_RATEMCS14:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001084 rate_idx = 14;
1085 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001086 case DESC_RATEMCS15:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001087 rate_idx = 15;
1088 break;
1089 default:
1090 rate_idx = 0;
1091 break;
1092 }
1093 }
1094 return rate_idx;
1095}
1096EXPORT_SYMBOL(rtlwifi_rate_mapping);
1097
Larry Finger0c817332010-12-08 11:12:31 -06001098void rtl_get_tcb_desc(struct ieee80211_hw *hw,
1099 struct ieee80211_tx_info *info,
Chaoming_Liacd48572011-04-25 12:52:54 -05001100 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -06001101 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
1102{
1103 struct rtl_priv *rtlpriv = rtl_priv(hw);
1104 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001105 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001106 struct ieee80211_rate *txrate;
Larry Finger9afa2e42014-09-22 09:39:21 -05001107 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001108
Chaoming_Liacd48572011-04-25 12:52:54 -05001109 txrate = ieee80211_get_tx_rate(hw, info);
Larry Fingere89f7692012-04-06 16:54:17 -05001110 if (txrate)
1111 tcb_desc->hw_rate = txrate->hw_value;
Larry Finger0c817332010-12-08 11:12:31 -06001112
1113 if (ieee80211_is_data(fc)) {
Larry Finger0c817332010-12-08 11:12:31 -06001114 /*
Chaoming_Liacd48572011-04-25 12:52:54 -05001115 *we set data rate INX 0
Larry Finger0c817332010-12-08 11:12:31 -06001116 *in rtl_rc.c if skb is special data or
1117 *mgt which need low data rate.
1118 */
1119
1120 /*
1121 *So tcb_desc->hw_rate is just used for
1122 *special data and mgt frames
1123 */
Chaoming Li09e92f02011-10-11 21:28:48 -05001124 if (info->control.rates[0].idx == 0 ||
Chaoming_Liacd48572011-04-25 12:52:54 -05001125 ieee80211_is_nullfunc(fc)) {
Larry Finger0c817332010-12-08 11:12:31 -06001126 tcb_desc->use_driver_rate = true;
Chaoming_Liacd48572011-04-25 12:52:54 -05001127 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -06001128
Larry Finger0c817332010-12-08 11:12:31 -06001129 tcb_desc->disable_ratefallback = 1;
1130 } else {
1131 /*
1132 *because hw will nerver use hw_rate
1133 *when tcb_desc->use_driver_rate = false
1134 *so we never set highest N rate here,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001135 *and N rate will all be controlled by FW
Larry Finger0c817332010-12-08 11:12:31 -06001136 *when tcb_desc->use_driver_rate = false
1137 */
Larry Finger9afa2e42014-09-22 09:39:21 -05001138 if (sta && sta->vht_cap.vht_supported) {
1139 tcb_desc->hw_rate =
1140 _rtl_get_vht_highest_n_rate(hw, sta);
Larry Finger0c817332010-12-08 11:12:31 -06001141 } else {
Larry Finger9afa2e42014-09-22 09:39:21 -05001142 if (sta && (sta->ht_cap.ht_supported)) {
Larry Finger0c817332010-12-08 11:12:31 -06001143 tcb_desc->hw_rate =
Larry Finger9afa2e42014-09-22 09:39:21 -05001144 _rtl_get_highest_n_rate(hw, sta);
Larry Finger0c817332010-12-08 11:12:31 -06001145 } else {
Larry Finger9afa2e42014-09-22 09:39:21 -05001146 if (rtlmac->mode == WIRELESS_MODE_B) {
1147 tcb_desc->hw_rate =
1148 rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
1149 } else {
1150 tcb_desc->hw_rate =
1151 rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
1152 }
Larry Finger0c817332010-12-08 11:12:31 -06001153 }
1154 }
1155 }
1156
1157 if (is_multicast_ether_addr(ieee80211_get_DA(hdr)))
Larry Finger7ea47242011-02-19 16:28:57 -06001158 tcb_desc->multicast = 1;
Larry Finger0c817332010-12-08 11:12:31 -06001159 else if (is_broadcast_ether_addr(ieee80211_get_DA(hdr)))
Larry Finger7ea47242011-02-19 16:28:57 -06001160 tcb_desc->broadcast = 1;
Larry Finger0c817332010-12-08 11:12:31 -06001161
Chaoming_Liacd48572011-04-25 12:52:54 -05001162 _rtl_txrate_selectmode(hw, sta, tcb_desc);
1163 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -06001164 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
Chaoming_Liacd48572011-04-25 12:52:54 -05001165 _rtl_query_shortgi(hw, sta, tcb_desc, info);
Larry Finger0c817332010-12-08 11:12:31 -06001166 _rtl_query_protection_mode(hw, tcb_desc, info);
1167 } else {
1168 tcb_desc->use_driver_rate = true;
Chaoming_Liacd48572011-04-25 12:52:54 -05001169 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -06001170 tcb_desc->disable_ratefallback = 1;
1171 tcb_desc->mac_id = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001172 tcb_desc->packet_bw = false;
Larry Finger0c817332010-12-08 11:12:31 -06001173 }
1174}
1175EXPORT_SYMBOL(rtl_get_tcb_desc);
1176
Larry Finger9afa2e42014-09-22 09:39:21 -05001177bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
1178{
1179 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1180 struct rtl_priv *rtlpriv = rtl_priv(hw);
1181 __le16 fc = rtl_get_fc(skb);
1182
1183 if (rtlpriv->dm.supp_phymode_switch &&
1184 mac->link_state < MAC80211_LINKED &&
1185 (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
1186 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1187 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1188 }
1189 if (ieee80211_is_auth(fc)) {
1190 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
1191 rtl_ips_nic_on(hw);
1192
1193 mac->link_state = MAC80211_LINKING;
1194 /* Dul mac */
1195 rtlpriv->phy.need_iqk = true;
1196
1197 }
1198
1199 return true;
1200}
1201EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
1202
1203struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
1204 u8 *bssid, u16 tid);
1205
1206static void process_agg_start(struct ieee80211_hw *hw,
1207 struct ieee80211_hdr *hdr, u16 tid)
Larry Finger26634c42013-03-24 22:06:33 -05001208{
1209 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger26634c42013-03-24 22:06:33 -05001210 struct ieee80211_rx_status rx_status = { 0 };
Larry Finger9afa2e42014-09-22 09:39:21 -05001211 struct sk_buff *skb_delba = NULL;
Larry Finger26634c42013-03-24 22:06:33 -05001212
Larry Finger9afa2e42014-09-22 09:39:21 -05001213 skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
1214 if (skb_delba) {
1215 rx_status.freq = hw->conf.chandef.chan->center_freq;
1216 rx_status.band = hw->conf.chandef.chan->band;
1217 rx_status.flag |= RX_FLAG_DECRYPTED;
1218 rx_status.flag |= RX_FLAG_MACTIME_START;
1219 rx_status.rate_idx = 0;
1220 rx_status.signal = 50 + 10;
1221 memcpy(IEEE80211_SKB_RXCB(skb_delba),
1222 &rx_status, sizeof(rx_status));
1223 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1224 "fake del\n",
1225 skb_delba->data,
1226 skb_delba->len);
1227 ieee80211_rx_irqsafe(hw, skb_delba);
Larry Finger26634c42013-03-24 22:06:33 -05001228 }
Larry Finger26634c42013-03-24 22:06:33 -05001229}
1230
Larry Finger0c817332010-12-08 11:12:31 -06001231bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1232{
1233 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001234 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001235 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001236 __le16 fc = rtl_get_fc(skb);
1237 u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
Larry Finger0c817332010-12-08 11:12:31 -06001238 u8 category;
1239
1240 if (!ieee80211_is_action(fc))
1241 return true;
1242
1243 category = *act;
1244 act++;
1245 switch (category) {
1246 case ACT_CAT_BA:
1247 switch (*act) {
1248 case ACT_ADDBAREQ:
1249 if (mac->act_scanning)
1250 return false;
1251
1252 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Larry Finger9afa2e42014-09-22 09:39:21 -05001253 "%s ACT_ADDBAREQ From :%pM\n",
1254 is_tx ? "Tx" : "Rx", hdr->addr2);
Larry Finger26634c42013-03-24 22:06:33 -05001255 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
Larry Finger9afa2e42014-09-22 09:39:21 -05001256 skb->data, skb->len);
1257 if (!is_tx) {
1258 struct ieee80211_sta *sta = NULL;
1259 struct rtl_sta_info *sta_entry = NULL;
1260 struct rtl_tid_data *tid_data;
1261 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1262 u16 capab = 0, tid = 0;
1263
1264 rcu_read_lock();
1265 sta = rtl_find_sta(hw, hdr->addr3);
1266 if (sta == NULL) {
1267 RT_TRACE(rtlpriv, COMP_SEND | COMP_RECV,
1268 DBG_DMESG, "sta is NULL\n");
1269 rcu_read_unlock();
Larry Finger26634c42013-03-24 22:06:33 -05001270 return true;
Larry Finger9afa2e42014-09-22 09:39:21 -05001271 }
1272
1273 sta_entry =
1274 (struct rtl_sta_info *)sta->drv_priv;
1275 if (!sta_entry) {
1276 rcu_read_unlock();
1277 return true;
1278 }
1279 capab =
1280 le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1281 tid = (capab &
1282 IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1283 tid_data = &sta_entry->tids[tid];
1284 if (tid_data->agg.rx_agg_state ==
1285 RTL_RX_AGG_START)
1286 process_agg_start(hw, hdr, tid);
1287 rcu_read_unlock();
1288 }
Larry Finger0c817332010-12-08 11:12:31 -06001289 break;
1290 case ACT_ADDBARSP:
1291 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001292 "%s ACT_ADDBARSP From :%pM\n",
Larry Finger9afa2e42014-09-22 09:39:21 -05001293 is_tx ? "Tx" : "Rx", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -06001294 break;
1295 case ACT_DELBA:
1296 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001297 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -06001298 break;
1299 }
1300 break;
1301 default:
1302 break;
1303 }
1304
1305 return true;
1306}
Larry Finger6f334c22013-07-12 15:32:15 -05001307EXPORT_SYMBOL_GPL(rtl_action_proc);
Larry Finger0c817332010-12-08 11:12:31 -06001308
Larry Finger9afa2e42014-09-22 09:39:21 -05001309static void setup_arp_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc)
1310{
Larry Fingerba9f93f2016-11-26 14:43:35 -06001311 struct ieee80211_hw *hw = rtlpriv->hw;
1312
Larry Finger9afa2e42014-09-22 09:39:21 -05001313 rtlpriv->ra.is_special_data = true;
1314 if (rtlpriv->cfg->ops->get_btc_status())
1315 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1316 rtlpriv, 1);
Larry Fingerba9f93f2016-11-26 14:43:35 -06001317 rtl_lps_leave(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001318 ppsc->last_delaylps_stamp_jiffies = jiffies;
1319}
1320
Larry Finger0c817332010-12-08 11:12:31 -06001321/*should call before software enc*/
Taehee Yoocad737d2015-03-28 00:22:39 +09001322u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
1323 bool is_enc)
Larry Finger0c817332010-12-08 11:12:31 -06001324{
1325 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001326 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001327 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001328 u16 ether_type;
1329 u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
Larry Finger9afa2e42014-09-22 09:39:21 -05001330 u8 encrypt_header_len = 0;
1331 u8 offset;
Larry Finger0c817332010-12-08 11:12:31 -06001332 const struct iphdr *ip;
1333
1334 if (!ieee80211_is_data(fc))
Larry Finger9afa2e42014-09-22 09:39:21 -05001335 goto end;
Larry Finger0c817332010-12-08 11:12:31 -06001336
Larry Finger9afa2e42014-09-22 09:39:21 -05001337 switch (rtlpriv->sec.pairwise_enc_algorithm) {
1338 case WEP40_ENCRYPTION:
1339 case WEP104_ENCRYPTION:
1340 encrypt_header_len = 4;/*WEP_IV_LEN*/
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001341 break;
Larry Finger9afa2e42014-09-22 09:39:21 -05001342 case TKIP_ENCRYPTION:
1343 encrypt_header_len = 8;/*TKIP_IV_LEN*/
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001344 break;
Larry Finger9afa2e42014-09-22 09:39:21 -05001345 case AESCCMP_ENCRYPTION:
1346 encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001347 break;
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001348 default:
Larry Finger9afa2e42014-09-22 09:39:21 -05001349 break;
Larry Finger0c817332010-12-08 11:12:31 -06001350 }
Larry Finger9afa2e42014-09-22 09:39:21 -05001351
Taehee Yoocad737d2015-03-28 00:22:39 +09001352 offset = mac_hdr_len + SNAP_SIZE;
1353 if (is_enc)
1354 offset += encrypt_header_len;
Larry Finger9afa2e42014-09-22 09:39:21 -05001355 ether_type = be16_to_cpup((__be16 *)(skb->data + offset));
1356
1357 if (ETH_P_IP == ether_type) {
1358 ip = (struct iphdr *)((u8 *)skb->data + offset +
1359 PROTOC_TYPE_SIZE);
1360 if (IPPROTO_UDP == ip->protocol) {
1361 struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1362 (ip->ihl << 2));
1363 if (((((u8 *)udp)[1] == 68) &&
1364 (((u8 *)udp)[3] == 67)) ||
1365 ((((u8 *)udp)[1] == 67) &&
1366 (((u8 *)udp)[3] == 68))) {
1367 /* 68 : UDP BOOTP client
1368 * 67 : UDP BOOTP server
1369 */
1370 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1371 DBG_DMESG, "dhcp %s !!\n",
1372 (is_tx) ? "Tx" : "Rx");
1373
1374 if (is_tx)
1375 setup_arp_tx(rtlpriv, ppsc);
1376 return true;
1377 }
1378 }
1379 } else if (ETH_P_ARP == ether_type) {
1380 if (is_tx)
1381 setup_arp_tx(rtlpriv, ppsc);
1382
1383 return true;
1384 } else if (ETH_P_PAE == ether_type) {
1385 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1386 "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
1387
1388 if (is_tx) {
1389 rtlpriv->ra.is_special_data = true;
Larry Fingerba9f93f2016-11-26 14:43:35 -06001390 rtl_lps_leave(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001391 ppsc->last_delaylps_stamp_jiffies = jiffies;
1392 }
1393
1394 return true;
Larry Fingerc8f03452015-02-24 09:23:01 -06001395 } else if (ETH_P_IPV6 == ether_type) {
1396 /* TODO: Handle any IPv6 cases that need special handling.
1397 * For now, always return false
1398 */
1399 goto end;
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001400 }
Larry Finger9afa2e42014-09-22 09:39:21 -05001401
1402end:
1403 rtlpriv->ra.is_special_data = false;
1404 return false;
Larry Finger0c817332010-12-08 11:12:31 -06001405}
Larry Finger6f334c22013-07-12 15:32:15 -05001406EXPORT_SYMBOL_GPL(rtl_is_special_data);
Larry Finger0c817332010-12-08 11:12:31 -06001407
1408/*********************************************************
1409 *
1410 * functions called by core.c
1411 *
1412 *********************************************************/
Larry Fingerf7953b22014-09-22 09:39:20 -05001413int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1414 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Larry Finger0c817332010-12-08 11:12:31 -06001415{
1416 struct rtl_priv *rtlpriv = rtl_priv(hw);
1417 struct rtl_tid_data *tid_data;
Chaoming_Liacd48572011-04-25 12:52:54 -05001418 struct rtl_sta_info *sta_entry = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001419
Chaoming_Liacd48572011-04-25 12:52:54 -05001420 if (sta == NULL)
1421 return -EINVAL;
Larry Finger0c817332010-12-08 11:12:31 -06001422
1423 if (unlikely(tid >= MAX_TID_COUNT))
1424 return -EINVAL;
1425
Chaoming_Liacd48572011-04-25 12:52:54 -05001426 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1427 if (!sta_entry)
Larry Finger0c817332010-12-08 11:12:31 -06001428 return -ENXIO;
Chaoming_Liacd48572011-04-25 12:52:54 -05001429 tid_data = &sta_entry->tids[tid];
Larry Finger0c817332010-12-08 11:12:31 -06001430
Larry Finger9afa2e42014-09-22 09:39:21 -05001431 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1432 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1433 tid_data->seq_number);
Larry Finger0c817332010-12-08 11:12:31 -06001434
Chaoming_Liacd48572011-04-25 12:52:54 -05001435 *ssn = tid_data->seq_number;
1436 tid_data->agg.agg_state = RTL_AGG_START;
1437
Larry Finger9afa2e42014-09-22 09:39:21 -05001438 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Larry Finger0c817332010-12-08 11:12:31 -06001439 return 0;
1440}
1441
Larry Fingerf7953b22014-09-22 09:39:20 -05001442int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1443 struct ieee80211_sta *sta, u16 tid)
Larry Finger0c817332010-12-08 11:12:31 -06001444{
Larry Finger0c817332010-12-08 11:12:31 -06001445 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001446 struct rtl_tid_data *tid_data;
Chaoming_Liacd48572011-04-25 12:52:54 -05001447 struct rtl_sta_info *sta_entry = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001448
Chaoming_Liacd48572011-04-25 12:52:54 -05001449 if (sta == NULL)
1450 return -EINVAL;
1451
Larry Finger9afa2e42014-09-22 09:39:21 -05001452 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1453 "on ra = %pM tid = %d\n", sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001454
Larry Finger0c817332010-12-08 11:12:31 -06001455 if (unlikely(tid >= MAX_TID_COUNT))
1456 return -EINVAL;
1457
Chaoming_Liacd48572011-04-25 12:52:54 -05001458 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
Larry Finger9afa2e42014-09-22 09:39:21 -05001459 tid_data = &sta_entry->tids[tid];
Chaoming_Liacd48572011-04-25 12:52:54 -05001460 sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
Larry Finger0c817332010-12-08 11:12:31 -06001461
Larry Finger9afa2e42014-09-22 09:39:21 -05001462 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001463 return 0;
1464}
Larry Finger0c817332010-12-08 11:12:31 -06001465
Larry Finger26634c42013-03-24 22:06:33 -05001466int rtl_rx_agg_start(struct ieee80211_hw *hw,
1467 struct ieee80211_sta *sta, u16 tid)
1468{
1469 struct rtl_priv *rtlpriv = rtl_priv(hw);
1470 struct rtl_tid_data *tid_data;
1471 struct rtl_sta_info *sta_entry = NULL;
1472
1473 if (sta == NULL)
1474 return -EINVAL;
1475
1476 if (unlikely(tid >= MAX_TID_COUNT))
1477 return -EINVAL;
1478
1479 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1480 if (!sta_entry)
1481 return -ENXIO;
1482 tid_data = &sta_entry->tids[tid];
1483
1484 RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1485 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1486 tid_data->seq_number);
1487
1488 tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1489 return 0;
1490}
1491
1492int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1493 struct ieee80211_sta *sta, u16 tid)
1494{
1495 struct rtl_priv *rtlpriv = rtl_priv(hw);
1496 struct rtl_sta_info *sta_entry = NULL;
1497
1498 if (sta == NULL)
1499 return -EINVAL;
1500
Larry Finger26634c42013-03-24 22:06:33 -05001501 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1502 "on ra = %pM tid = %d\n", sta->addr, tid);
1503
1504 if (unlikely(tid >= MAX_TID_COUNT))
1505 return -EINVAL;
1506
1507 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1508 sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1509
1510 return 0;
1511}
Chaoming_Liacd48572011-04-25 12:52:54 -05001512int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1513 struct ieee80211_sta *sta, u16 tid)
1514{
1515 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Liacd48572011-04-25 12:52:54 -05001516 struct rtl_sta_info *sta_entry = NULL;
1517
1518 if (sta == NULL)
1519 return -EINVAL;
1520
Larry Finger9afa2e42014-09-22 09:39:21 -05001521 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1522 "on ra = %pM tid = %d\n", sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001523
1524 if (unlikely(tid >= MAX_TID_COUNT))
1525 return -EINVAL;
1526
1527 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
Chaoming_Liacd48572011-04-25 12:52:54 -05001528 sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
Larry Finger0c817332010-12-08 11:12:31 -06001529
1530 return 0;
1531}
1532
1533/*********************************************************
1534 *
1535 * wq & timer callback functions
1536 *
1537 *********************************************************/
Larry Finger26634c42013-03-24 22:06:33 -05001538/* this function is used for roaming */
1539void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1540{
1541 struct rtl_priv *rtlpriv = rtl_priv(hw);
1542 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1543
1544 if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1545 return;
1546
1547 if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1548 return;
1549
1550 /* check if this really is a beacon */
1551 if (!ieee80211_is_beacon(hdr->frame_control) &&
1552 !ieee80211_is_probe_resp(hdr->frame_control))
1553 return;
1554
1555 /* min. beacon length + FCS_LEN */
1556 if (skb->len <= 40 + FCS_LEN)
1557 return;
1558
1559 /* and only beacons from the associated BSSID, please */
Larry Finger9afa2e42014-09-22 09:39:21 -05001560 if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
Larry Finger26634c42013-03-24 22:06:33 -05001561 return;
1562
1563 rtlpriv->link_info.bcn_rx_inperiod++;
1564}
Larry Finger6f334c22013-07-12 15:32:15 -05001565EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
Larry Finger26634c42013-03-24 22:06:33 -05001566
Larry Finger0c817332010-12-08 11:12:31 -06001567void rtl_watchdog_wq_callback(void *data)
1568{
1569 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1570 struct rtl_works,
1571 watchdog_wq);
1572 struct ieee80211_hw *hw = rtlworks->hw;
1573 struct rtl_priv *rtlpriv = rtl_priv(hw);
1574 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1575 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Larry Finger7ea47242011-02-19 16:28:57 -06001576 bool busytraffic = false;
Larry Finger26634c42013-03-24 22:06:33 -05001577 bool tx_busy_traffic = false;
1578 bool rx_busy_traffic = false;
Larry Finger7ea47242011-02-19 16:28:57 -06001579 bool higher_busytraffic = false;
1580 bool higher_busyrxtraffic = false;
Chaoming_Liacd48572011-04-25 12:52:54 -05001581 u8 idx, tid;
Larry Finger0c817332010-12-08 11:12:31 -06001582 u32 rx_cnt_inp4eriod = 0;
1583 u32 tx_cnt_inp4eriod = 0;
1584 u32 aver_rx_cnt_inperiod = 0;
1585 u32 aver_tx_cnt_inperiod = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001586 u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1587 u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
Larry Finger0c817332010-12-08 11:12:31 -06001588
1589 if (is_hal_stop(rtlhal))
1590 return;
1591
1592 /* <1> Determine if action frame is allowed */
1593 if (mac->link_state > MAC80211_NOLINK) {
1594 if (mac->cnt_after_linked < 20)
1595 mac->cnt_after_linked++;
1596 } else {
1597 mac->cnt_after_linked = 0;
1598 }
1599
Larry Finger9afa2e42014-09-22 09:39:21 -05001600 /* <2> to check if traffic busy, if
Larry Finger0c817332010-12-08 11:12:31 -06001601 * busytraffic we don't change channel
1602 */
1603 if (mac->link_state >= MAC80211_LINKED) {
1604
1605 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1606 for (idx = 0; idx <= 2; idx++) {
1607 rtlpriv->link_info.num_rx_in4period[idx] =
1608 rtlpriv->link_info.num_rx_in4period[idx + 1];
1609 rtlpriv->link_info.num_tx_in4period[idx] =
1610 rtlpriv->link_info.num_tx_in4period[idx + 1];
1611 }
1612 rtlpriv->link_info.num_rx_in4period[3] =
1613 rtlpriv->link_info.num_rx_inperiod;
1614 rtlpriv->link_info.num_tx_in4period[3] =
1615 rtlpriv->link_info.num_tx_inperiod;
1616 for (idx = 0; idx <= 3; idx++) {
1617 rx_cnt_inp4eriod +=
1618 rtlpriv->link_info.num_rx_in4period[idx];
1619 tx_cnt_inp4eriod +=
1620 rtlpriv->link_info.num_tx_in4period[idx];
1621 }
1622 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1623 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1624
1625 /* (2) check traffic busy */
Larry Finger26634c42013-03-24 22:06:33 -05001626 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
Larry Finger7ea47242011-02-19 16:28:57 -06001627 busytraffic = true;
Larry Finger26634c42013-03-24 22:06:33 -05001628 if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1629 rx_busy_traffic = true;
1630 else
1631 tx_busy_traffic = false;
1632 }
Larry Finger0c817332010-12-08 11:12:31 -06001633
1634 /* Higher Tx/Rx data. */
1635 if (aver_rx_cnt_inperiod > 4000 ||
1636 aver_tx_cnt_inperiod > 4000) {
Larry Finger7ea47242011-02-19 16:28:57 -06001637 higher_busytraffic = true;
Larry Finger0c817332010-12-08 11:12:31 -06001638
1639 /* Extremely high Rx data. */
1640 if (aver_rx_cnt_inperiod > 5000)
Larry Finger7ea47242011-02-19 16:28:57 -06001641 higher_busyrxtraffic = true;
Chaoming_Liacd48572011-04-25 12:52:54 -05001642 }
1643
1644 /* check every tid's tx traffic */
1645 for (tid = 0; tid <= 7; tid++) {
1646 for (idx = 0; idx <= 2; idx++)
1647 rtlpriv->link_info.tidtx_in4period[tid][idx] =
Larry Finger9afa2e42014-09-22 09:39:21 -05001648 rtlpriv->link_info.tidtx_in4period[tid]
1649 [idx + 1];
Chaoming_Liacd48572011-04-25 12:52:54 -05001650 rtlpriv->link_info.tidtx_in4period[tid][3] =
1651 rtlpriv->link_info.tidtx_inperiod[tid];
1652
1653 for (idx = 0; idx <= 3; idx++)
1654 tidtx_inp4eriod[tid] +=
Larry Finger9afa2e42014-09-22 09:39:21 -05001655 rtlpriv->link_info.tidtx_in4period[tid][idx];
Chaoming_Liacd48572011-04-25 12:52:54 -05001656 aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1657 if (aver_tidtx_inperiod[tid] > 5000)
1658 rtlpriv->link_info.higher_busytxtraffic[tid] =
Larry Finger9afa2e42014-09-22 09:39:21 -05001659 true;
Larry Finger0c817332010-12-08 11:12:31 -06001660 else
Chaoming_Liacd48572011-04-25 12:52:54 -05001661 rtlpriv->link_info.higher_busytxtraffic[tid] =
Larry Finger9afa2e42014-09-22 09:39:21 -05001662 false;
Larry Finger0c817332010-12-08 11:12:31 -06001663 }
1664
1665 if (((rtlpriv->link_info.num_rx_inperiod +
1666 rtlpriv->link_info.num_tx_inperiod) > 8) ||
1667 (rtlpriv->link_info.num_rx_inperiod > 2))
Larry Fingerfd09ff92014-09-22 09:39:18 -05001668 rtl_lps_leave(hw);
wang yanqing873ffe12016-05-03 00:38:36 +08001669 else
1670 rtl_lps_enter(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001671 }
1672
1673 rtlpriv->link_info.num_rx_inperiod = 0;
1674 rtlpriv->link_info.num_tx_inperiod = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001675 for (tid = 0; tid <= 7; tid++)
1676 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001677
Larry Finger7ea47242011-02-19 16:28:57 -06001678 rtlpriv->link_info.busytraffic = busytraffic;
1679 rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
Larry Finger26634c42013-03-24 22:06:33 -05001680 rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
1681 rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
Larry Finger7ea47242011-02-19 16:28:57 -06001682 rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
Larry Finger0c817332010-12-08 11:12:31 -06001683
Chaoming_Liacd48572011-04-25 12:52:54 -05001684 /* <3> DM */
Larry Finger9afa2e42014-09-22 09:39:21 -05001685 if (!rtlpriv->cfg->mod_params->disable_watchdog)
1686 rtlpriv->cfg->ops->dm_watchdog(hw);
Larry Finger26634c42013-03-24 22:06:33 -05001687
1688 /* <4> roaming */
1689 if (mac->link_state == MAC80211_LINKED &&
1690 mac->opmode == NL80211_IFTYPE_STATION) {
1691 if ((rtlpriv->link_info.bcn_rx_inperiod +
Larry Finger9afa2e42014-09-22 09:39:21 -05001692 rtlpriv->link_info.num_rx_inperiod) == 0) {
Larry Finger26634c42013-03-24 22:06:33 -05001693 rtlpriv->link_info.roam_times++;
1694 RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
1695 "AP off for %d s\n",
Larry Finger9afa2e42014-09-22 09:39:21 -05001696 (rtlpriv->link_info.roam_times * 2));
Larry Finger26634c42013-03-24 22:06:33 -05001697
Larry Finger9afa2e42014-09-22 09:39:21 -05001698 /* if we can't recv beacon for 10s,
1699 * we should reconnect this AP
Larry Finger26634c42013-03-24 22:06:33 -05001700 */
Larry Finger9afa2e42014-09-22 09:39:21 -05001701 if (rtlpriv->link_info.roam_times >= 5) {
Larry Fingerb03d9682016-12-15 12:22:58 -06001702 pr_err("AP off, try to reconnect now\n");
Larry Finger26634c42013-03-24 22:06:33 -05001703 rtlpriv->link_info.roam_times = 0;
Larry Finger9afa2e42014-09-22 09:39:21 -05001704 ieee80211_connection_loss(
1705 rtlpriv->mac80211.vif);
Larry Finger26634c42013-03-24 22:06:33 -05001706 }
1707 } else {
1708 rtlpriv->link_info.roam_times = 0;
1709 }
1710 }
Larry Finger9afa2e42014-09-22 09:39:21 -05001711
1712 if (rtlpriv->cfg->ops->get_btc_status())
1713 rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
1714
Larry Finger26634c42013-03-24 22:06:33 -05001715 rtlpriv->link_info.bcn_rx_inperiod = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001716}
1717
1718void rtl_watch_dog_timer_callback(unsigned long data)
1719{
1720 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1721 struct rtl_priv *rtlpriv = rtl_priv(hw);
1722
1723 queue_delayed_work(rtlpriv->works.rtl_wq,
1724 &rtlpriv->works.watchdog_wq, 0);
1725
1726 mod_timer(&rtlpriv->works.watchdog_timer,
1727 jiffies + MSECS(RTL_WATCH_DOG_TIME));
1728}
Larry Finger26634c42013-03-24 22:06:33 -05001729void rtl_fwevt_wq_callback(void *data)
1730{
1731 struct rtl_works *rtlworks =
1732 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
1733 struct ieee80211_hw *hw = rtlworks->hw;
1734 struct rtl_priv *rtlpriv = rtl_priv(hw);
1735
1736 rtlpriv->cfg->ops->c2h_command_handle(hw);
1737}
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -06001738
1739void rtl_c2hcmd_enqueue(struct ieee80211_hw *hw, u8 tag, u8 len, u8 *val)
1740{
1741 struct rtl_priv *rtlpriv = rtl_priv(hw);
1742 unsigned long flags;
1743 struct rtl_c2hcmd *c2hcmd;
1744
Larry Finger893dc682017-03-21 09:24:11 -05001745 c2hcmd = kmalloc(sizeof(*c2hcmd),
1746 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -06001747
1748 if (!c2hcmd)
1749 goto label_err;
1750
Larry Finger893dc682017-03-21 09:24:11 -05001751 c2hcmd->val = kmalloc(len,
1752 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -06001753
1754 if (!c2hcmd->val)
1755 goto label_err2;
1756
1757 /* fill data */
1758 c2hcmd->tag = tag;
1759 c2hcmd->len = len;
1760 memcpy(c2hcmd->val, val, len);
1761
1762 /* enqueue */
1763 spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
1764
1765 list_add_tail(&c2hcmd->list, &rtlpriv->c2hcmd_list);
1766
1767 spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
1768
1769 /* wake up wq */
1770 queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.c2hcmd_wq, 0);
1771
1772 return;
1773
1774label_err2:
1775 kfree(c2hcmd);
1776
1777label_err:
1778 RT_TRACE(rtlpriv, COMP_CMD, DBG_WARNING,
1779 "C2H cmd enqueue fail.\n");
1780}
1781EXPORT_SYMBOL(rtl_c2hcmd_enqueue);
1782
1783void rtl_c2hcmd_launcher(struct ieee80211_hw *hw, int exec)
1784{
1785 struct rtl_priv *rtlpriv = rtl_priv(hw);
1786 unsigned long flags;
1787 struct rtl_c2hcmd *c2hcmd;
1788 int i;
1789
1790 for (i = 0; i < 200; i++) {
1791 /* dequeue a task */
1792 spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
1793
1794 c2hcmd = list_first_entry_or_null(&rtlpriv->c2hcmd_list,
1795 struct rtl_c2hcmd, list);
1796
1797 if (c2hcmd)
1798 list_del(&c2hcmd->list);
1799
1800 spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
1801
1802 /* do it */
1803 if (!c2hcmd)
1804 break;
1805
1806 if (rtlpriv->cfg->ops->c2h_content_parsing && exec)
1807 rtlpriv->cfg->ops->c2h_content_parsing(hw,
1808 c2hcmd->tag, c2hcmd->len, c2hcmd->val);
1809
1810 /* free */
1811 kfree(c2hcmd->val);
1812
1813 kfree(c2hcmd);
1814 }
1815}
1816
1817void rtl_c2hcmd_wq_callback(void *data)
1818{
1819 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1820 struct rtl_works,
1821 c2hcmd_wq);
1822 struct ieee80211_hw *hw = rtlworks->hw;
1823
1824 rtl_c2hcmd_launcher(hw, 1);
1825}
1826
Larry Finger26634c42013-03-24 22:06:33 -05001827void rtl_easy_concurrent_retrytimer_callback(unsigned long data)
1828{
1829 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
1830 struct rtl_priv *rtlpriv = rtl_priv(hw);
1831 struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
1832
1833 if (buddy_priv == NULL)
1834 return;
1835
1836 rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
1837}
Larry Finger0c817332010-12-08 11:12:31 -06001838/*********************************************************
1839 *
Chaoming_Liacd48572011-04-25 12:52:54 -05001840 * frame process functions
1841 *
1842 *********************************************************/
1843u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
1844{
1845 struct ieee80211_mgmt *mgmt = (void *)data;
1846 u8 *pos, *end;
1847
1848 pos = (u8 *)mgmt->u.beacon.variable;
1849 end = data + len;
1850 while (pos < end) {
1851 if (pos + 2 + pos[1] > end)
1852 return NULL;
1853
1854 if (pos[0] == ie)
1855 return pos;
1856
1857 pos += 2 + pos[1];
1858 }
1859 return NULL;
1860}
1861
1862/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
1863/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
Larry Fingerd3bb1422011-04-25 13:23:20 -05001864static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
Larry Finger9afa2e42014-09-22 09:39:21 -05001865 enum ieee80211_smps_mode smps,
1866 u8 *da, u8 *bssid)
Chaoming_Liacd48572011-04-25 12:52:54 -05001867{
1868 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1869 struct sk_buff *skb;
1870 struct ieee80211_mgmt *action_frame;
1871
1872 /* 27 = header + category + action + smps mode */
1873 skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
1874 if (!skb)
1875 return NULL;
1876
1877 skb_reserve(skb, hw->extra_tx_headroom);
1878 action_frame = (void *)skb_put(skb, 27);
1879 memset(action_frame, 0, 27);
1880 memcpy(action_frame->da, da, ETH_ALEN);
1881 memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
1882 memcpy(action_frame->bssid, bssid, ETH_ALEN);
1883 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1884 IEEE80211_STYPE_ACTION);
1885 action_frame->u.action.category = WLAN_CATEGORY_HT;
1886 action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
1887 switch (smps) {
1888 case IEEE80211_SMPS_AUTOMATIC:/* 0 */
1889 case IEEE80211_SMPS_NUM_MODES:/* 4 */
1890 WARN_ON(1);
Larry Finger9afa2e42014-09-22 09:39:21 -05001891 /* Here will get a 'MISSING_BREAK' in Coverity Test, just ignore it.
1892 * According to Kernel Code, here is right.
1893 */
Chaoming_Liacd48572011-04-25 12:52:54 -05001894 case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
1895 action_frame->u.action.u.ht_smps.smps_control =
1896 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
1897 break;
1898 case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
1899 action_frame->u.action.u.ht_smps.smps_control =
1900 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
1901 break;
1902 case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
1903 action_frame->u.action.u.ht_smps.smps_control =
1904 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
1905 break;
1906 }
1907
1908 return skb;
1909}
1910
1911int rtl_send_smps_action(struct ieee80211_hw *hw,
Larry Finger9afa2e42014-09-22 09:39:21 -05001912 struct ieee80211_sta *sta,
1913 enum ieee80211_smps_mode smps)
Chaoming_Liacd48572011-04-25 12:52:54 -05001914{
1915 struct rtl_priv *rtlpriv = rtl_priv(hw);
1916 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1917 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
Larry Finger26634c42013-03-24 22:06:33 -05001918 struct sk_buff *skb = NULL;
Chaoming_Liacd48572011-04-25 12:52:54 -05001919 struct rtl_tcb_desc tcb_desc;
Larry Finger26634c42013-03-24 22:06:33 -05001920 u8 bssid[ETH_ALEN] = {0};
1921
Chaoming_Liacd48572011-04-25 12:52:54 -05001922 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
1923
1924 if (rtlpriv->mac80211.act_scanning)
1925 goto err_free;
1926
1927 if (!sta)
1928 goto err_free;
1929
1930 if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
1931 goto err_free;
1932
1933 if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
1934 goto err_free;
1935
Larry Finger26634c42013-03-24 22:06:33 -05001936 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
1937 memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
1938 else
1939 memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
1940
1941 skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001942 /* this is a type = mgmt * stype = action frame */
1943 if (skb) {
1944 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1945 struct rtl_sta_info *sta_entry =
1946 (struct rtl_sta_info *) sta->drv_priv;
1947 sta_entry->mimo_ps = smps;
Larry Finger9afa2e42014-09-22 09:39:21 -05001948 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0); */
Chaoming_Liacd48572011-04-25 12:52:54 -05001949
1950 info->control.rates[0].idx = 0;
Karl Beldan675a0b02013-03-25 16:26:57 +01001951 info->band = hw->conf.chandef.chan->band;
Thomas Huehn36323f82012-07-23 21:33:42 +02001952 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
Chaoming_Liacd48572011-04-25 12:52:54 -05001953 }
Larry Finger26634c42013-03-24 22:06:33 -05001954 return 1;
1955
Chaoming_Liacd48572011-04-25 12:52:54 -05001956err_free:
1957 return 0;
1958}
Larry Finger26634c42013-03-24 22:06:33 -05001959EXPORT_SYMBOL(rtl_send_smps_action);
1960
Larry Finger4f4826d2013-09-26 13:25:27 -05001961void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
1962{
1963 struct rtl_priv *rtlpriv = rtl_priv(hw);
1964 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1965 enum io_type iotype;
1966
1967 if (!is_hal_stop(rtlhal)) {
1968 switch (operation) {
1969 case SCAN_OPT_BACKUP:
1970 iotype = IO_CMD_PAUSE_DM_BY_SCAN;
1971 rtlpriv->cfg->ops->set_hw_reg(hw,
1972 HW_VAR_IO_CMD,
1973 (u8 *)&iotype);
1974 break;
1975 case SCAN_OPT_RESTORE:
1976 iotype = IO_CMD_RESUME_DM_BY_SCAN;
1977 rtlpriv->cfg->ops->set_hw_reg(hw,
1978 HW_VAR_IO_CMD,
1979 (u8 *)&iotype);
1980 break;
1981 default:
Larry Fingerb03d9682016-12-15 12:22:58 -06001982 pr_err("Unknown Scan Backup operation.\n");
Larry Finger4f4826d2013-09-26 13:25:27 -05001983 break;
1984 }
1985 }
1986}
1987EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
1988
Larry Finger9afa2e42014-09-22 09:39:21 -05001989/* because mac80211 have issues when can receive del ba
1990 * so here we just make a fake del_ba if we receive a ba_req
1991 * but rx_agg was opened to let mac80211 release some ba
1992 * related resources, so please this del_ba for tx
Larry Finger26634c42013-03-24 22:06:33 -05001993 */
1994struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
1995 u8 *sa, u8 *bssid, u16 tid)
1996{
1997 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1998 struct sk_buff *skb;
1999 struct ieee80211_mgmt *action_frame;
2000 u16 params;
2001
2002 /* 27 = header + category + action + smps mode */
2003 skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
2004 if (!skb)
2005 return NULL;
2006
2007 skb_reserve(skb, hw->extra_tx_headroom);
2008 action_frame = (void *)skb_put(skb, 34);
2009 memset(action_frame, 0, 34);
2010 memcpy(action_frame->sa, sa, ETH_ALEN);
2011 memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
2012 memcpy(action_frame->bssid, bssid, ETH_ALEN);
2013 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2014 IEEE80211_STYPE_ACTION);
2015 action_frame->u.action.category = WLAN_CATEGORY_BACK;
2016 action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
2017 params = (u16)(1 << 11); /* bit 11 initiator */
Larry Finger9afa2e42014-09-22 09:39:21 -05002018 params |= (u16)(tid << 12); /* bit 15:12 TID number */
Larry Finger26634c42013-03-24 22:06:33 -05002019
2020 action_frame->u.action.u.delba.params = cpu_to_le16(params);
2021 action_frame->u.action.u.delba.reason_code =
2022 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
2023
2024 return skb;
2025}
Chaoming_Liacd48572011-04-25 12:52:54 -05002026
2027/*********************************************************
2028 *
2029 * IOT functions
2030 *
2031 *********************************************************/
2032static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
Larry Finger9afa2e42014-09-22 09:39:21 -05002033 struct octet_string vendor_ie)
Chaoming_Liacd48572011-04-25 12:52:54 -05002034{
2035 struct rtl_priv *rtlpriv = rtl_priv(hw);
2036 bool matched = false;
2037 static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
2038 static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
2039 static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
2040 static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
2041 static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
2042 static u8 racap[] = { 0x00, 0x0c, 0x43 };
2043 static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
2044 static u8 marvcap[] = { 0x00, 0x50, 0x43 };
2045
2046 if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
2047 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
2048 rtlpriv->mac80211.vendor = PEER_ATH;
2049 matched = true;
2050 } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
2051 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
2052 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
2053 rtlpriv->mac80211.vendor = PEER_BROAD;
2054 matched = true;
2055 } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
2056 rtlpriv->mac80211.vendor = PEER_RAL;
2057 matched = true;
2058 } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
2059 rtlpriv->mac80211.vendor = PEER_CISCO;
2060 matched = true;
2061 } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
2062 rtlpriv->mac80211.vendor = PEER_MARV;
2063 matched = true;
2064 }
2065
2066 return matched;
2067}
2068
Larry Fingerd3bb1422011-04-25 13:23:20 -05002069static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
Chaoming_Liacd48572011-04-25 12:52:54 -05002070 unsigned int len)
2071{
2072 struct ieee80211_mgmt *mgmt = (void *)data;
2073 struct octet_string vendor_ie;
2074 u8 *pos, *end;
2075
2076 pos = (u8 *)mgmt->u.beacon.variable;
2077 end = data + len;
2078 while (pos < end) {
2079 if (pos[0] == 221) {
2080 vendor_ie.length = pos[1];
2081 vendor_ie.octet = &pos[2];
2082 if (rtl_chk_vendor_ouisub(hw, vendor_ie))
2083 return true;
2084 }
2085
2086 if (pos + 2 + pos[1] > end)
2087 return false;
2088
2089 pos += 2 + pos[1];
2090 }
2091 return false;
2092}
2093
2094void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
2095{
2096 struct rtl_priv *rtlpriv = rtl_priv(hw);
2097 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2098 struct ieee80211_hdr *hdr = (void *)data;
2099 u32 vendor = PEER_UNKNOWN;
2100
2101 static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
2102 static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
2103 static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
2104 static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
2105 static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
2106 static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
2107 static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
2108 static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
2109 static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
2110 static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
2111 static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
2112 static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
2113 static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
2114 static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
2115 static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
2116 static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
2117
2118 if (mac->opmode != NL80211_IFTYPE_STATION)
2119 return;
2120
2121 if (mac->link_state == MAC80211_NOLINK) {
2122 mac->vendor = PEER_UNKNOWN;
2123 return;
2124 }
2125
2126 if (mac->cnt_after_linked > 2)
2127 return;
2128
2129 /* check if this really is a beacon */
2130 if (!ieee80211_is_beacon(hdr->frame_control))
2131 return;
2132
2133 /* min. beacon length + FCS_LEN */
2134 if (len <= 40 + FCS_LEN)
2135 return;
2136
2137 /* and only beacons from the associated BSSID, please */
Julia Lawall90908e12013-12-30 19:15:02 +01002138 if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
Chaoming_Liacd48572011-04-25 12:52:54 -05002139 return;
2140
2141 if (rtl_find_221_ie(hw, data, len))
2142 vendor = mac->vendor;
2143
2144 if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
2145 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
2146 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
2147 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
2148 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
2149 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
2150 vendor == PEER_ATH) {
2151 vendor = PEER_ATH;
Joe Perchesf30d7502012-01-04 19:40:41 -08002152 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002153 } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
2154 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
2155 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
2156 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
2157 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
2158 vendor == PEER_RAL) {
Joe Perchesf30d7502012-01-04 19:40:41 -08002159 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002160 vendor = PEER_RAL;
2161 } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
2162 vendor == PEER_CISCO) {
2163 vendor = PEER_CISCO;
Joe Perchesf30d7502012-01-04 19:40:41 -08002164 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002165 } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
2166 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
2167 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
2168 vendor == PEER_BROAD) {
Joe Perchesf30d7502012-01-04 19:40:41 -08002169 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002170 vendor = PEER_BROAD;
2171 } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
2172 vendor == PEER_MARV) {
2173 vendor = PEER_MARV;
Joe Perchesf30d7502012-01-04 19:40:41 -08002174 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002175 }
2176
2177 mac->vendor = vendor;
2178}
Larry Finger6f334c22013-07-12 15:32:15 -05002179EXPORT_SYMBOL_GPL(rtl_recognize_peer);
Chaoming_Liacd48572011-04-25 12:52:54 -05002180
Larry Finger0c817332010-12-08 11:12:31 -06002181MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
2182MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
2183MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
2184MODULE_LICENSE("GPL");
2185MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2186
Larry Finger6f334c22013-07-12 15:32:15 -05002187struct rtl_global_var rtl_global_var = {};
2188EXPORT_SYMBOL_GPL(rtl_global_var);
Larry Finger26634c42013-03-24 22:06:33 -05002189
Larry Finger0c817332010-12-08 11:12:31 -06002190static int __init rtl_core_module_init(void)
2191{
Chaoming Li375ff4c2011-02-28 16:40:28 -06002192 if (rtl_rate_control_register())
Larry Finger9afa2e42014-09-22 09:39:21 -05002193 pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002194
Larry Finger26634c42013-03-24 22:06:33 -05002195 /* init some global vars */
Larry Finger6f334c22013-07-12 15:32:15 -05002196 INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2197 spin_lock_init(&rtl_global_var.glb_list_lock);
Larry Finger26634c42013-03-24 22:06:33 -05002198
Larry Finger0c817332010-12-08 11:12:31 -06002199 return 0;
2200}
2201
2202static void __exit rtl_core_module_exit(void)
2203{
Chaoming_Liacd48572011-04-25 12:52:54 -05002204 /*RC*/
Chaoming Li375ff4c2011-02-28 16:40:28 -06002205 rtl_rate_control_unregister();
Larry Finger0c817332010-12-08 11:12:31 -06002206}
2207
2208module_init(rtl_core_module_init);
2209module_exit(rtl_core_module_exit);