blob: 7fa435e95c0e37297ec3d572652a801104071326 [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
Ping-Ke Shihba3cca42018-01-19 14:45:49 +0800247 if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE ||
248 rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE) {
Larry Finger9afa2e42014-09-22 09:39:21 -0500249 u16 mcs_map;
250
251 vht_cap->vht_supported = true;
252 vht_cap->cap =
Larry Finger9afa2e42014-09-22 09:39:21 -0500253 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
254 IEEE80211_VHT_CAP_SHORT_GI_80 |
255 IEEE80211_VHT_CAP_TXSTBC |
256 IEEE80211_VHT_CAP_RXSTBC_1 |
257 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
258 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
259 IEEE80211_VHT_CAP_HTC_VHT |
260 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
261 IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
262 IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
263 0;
264
265 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
266 IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
267 IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
268 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
269 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
270 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
271 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
272 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
273
274 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
275 vht_cap->vht_mcs.rx_highest =
276 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
277 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
278 vht_cap->vht_mcs.tx_highest =
279 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
280 } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
281 u16 mcs_map;
282
283 vht_cap->vht_supported = true;
284 vht_cap->cap =
Larry Finger9afa2e42014-09-22 09:39:21 -0500285 IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
286 IEEE80211_VHT_CAP_SHORT_GI_80 |
287 IEEE80211_VHT_CAP_TXSTBC |
288 IEEE80211_VHT_CAP_RXSTBC_1 |
289 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
290 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
291 IEEE80211_VHT_CAP_HTC_VHT |
292 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
293 IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
294 IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
295 0;
296
297 mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
298 IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
299 IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
300 IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
301 IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
302 IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
303 IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
304 IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
305
306 vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
307 vht_cap->vht_mcs.rx_highest =
308 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
309 vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
310 vht_cap->vht_mcs.tx_highest =
311 cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
312 }
313}
314
Larry Finger0c817332010-12-08 11:12:31 -0600315static void _rtl_init_mac80211(struct ieee80211_hw *hw)
316{
Chaoming_Liacd48572011-04-25 12:52:54 -0500317 struct rtl_priv *rtlpriv = rtl_priv(hw);
318 struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
Larry Finger0c817332010-12-08 11:12:31 -0600319 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
320 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
321 struct ieee80211_supported_band *sband;
322
Larry Finger9afa2e42014-09-22 09:39:21 -0500323 if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
324 rtlhal->bandset == BAND_ON_BOTH) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500325 /* 1: 2.4 G bands */
326 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200327 sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
Larry Finger0c817332010-12-08 11:12:31 -0600328
Johannes Berg57fbcce2016-04-12 15:56:15 +0200329 /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500330 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200331 memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]), &rtl_band_2ghz,
Chaoming_Liacd48572011-04-25 12:52:54 -0500332 sizeof(struct ieee80211_supported_band));
Larry Finger0c817332010-12-08 11:12:31 -0600333
Chaoming_Liacd48572011-04-25 12:52:54 -0500334 /* <3> init ht cap base on ant_num */
335 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
Larry Finger0c817332010-12-08 11:12:31 -0600336
Chaoming_Liacd48572011-04-25 12:52:54 -0500337 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200338 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500339
340 /* 2: 5 G bands */
341 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200342 sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
Chaoming_Liacd48572011-04-25 12:52:54 -0500343
Johannes Berg57fbcce2016-04-12 15:56:15 +0200344 /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500345 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200346 memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]), &rtl_band_5ghz,
Chaoming_Liacd48572011-04-25 12:52:54 -0500347 sizeof(struct ieee80211_supported_band));
348
349 /* <3> init ht cap base on ant_num */
350 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
351
Larry Finger9afa2e42014-09-22 09:39:21 -0500352 _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
Chaoming_Liacd48572011-04-25 12:52:54 -0500353 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200354 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500355 } else {
356 if (rtlhal->current_bandtype == BAND_ON_2_4G) {
357 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200358 sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
Chaoming_Liacd48572011-04-25 12:52:54 -0500359
Johannes Berg57fbcce2016-04-12 15:56:15 +0200360 /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500361 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200362 memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]),
Larry Finger9afa2e42014-09-22 09:39:21 -0500363 &rtl_band_2ghz,
364 sizeof(struct ieee80211_supported_band));
Chaoming_Liacd48572011-04-25 12:52:54 -0500365
366 /* <3> init ht cap base on ant_num */
367 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
368
369 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200370 hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500371 } else if (rtlhal->current_bandtype == BAND_ON_5G) {
372 /* <1> use mac->bands as mem for hw->wiphy->bands */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200373 sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
Chaoming_Liacd48572011-04-25 12:52:54 -0500374
Johannes Berg57fbcce2016-04-12 15:56:15 +0200375 /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
Chaoming_Liacd48572011-04-25 12:52:54 -0500376 * to default value(1T1R) */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200377 memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]),
Larry Finger9afa2e42014-09-22 09:39:21 -0500378 &rtl_band_5ghz,
379 sizeof(struct ieee80211_supported_band));
Chaoming_Liacd48572011-04-25 12:52:54 -0500380
381 /* <3> init ht cap base on ant_num */
382 _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
383
Larry Finger9afa2e42014-09-22 09:39:21 -0500384 _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
Chaoming_Liacd48572011-04-25 12:52:54 -0500385 /* <4> set mac->sband to wiphy->sband */
Johannes Berg57fbcce2016-04-12 15:56:15 +0200386 hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
Chaoming_Liacd48572011-04-25 12:52:54 -0500387 } else {
Larry Fingerb03d9682016-12-15 12:22:58 -0600388 pr_err("Err BAND %d\n",
389 rtlhal->current_bandtype);
Chaoming_Liacd48572011-04-25 12:52:54 -0500390 }
391 }
Larry Finger0c817332010-12-08 11:12:31 -0600392 /* <5> set hw caps */
Johannes Berg30686bf2015-06-02 21:39:54 +0200393 ieee80211_hw_set(hw, SIGNAL_DBM);
394 ieee80211_hw_set(hw, RX_INCLUDES_FCS);
395 ieee80211_hw_set(hw, AMPDU_AGGREGATION);
396 ieee80211_hw_set(hw, CONNECTION_MONITOR);
397 ieee80211_hw_set(hw, MFP_CAPABLE);
398 ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
Ping-Ke Shih5f906692018-01-18 09:26:14 +0800399 ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
Larry Finger0c817332010-12-08 11:12:31 -0600400
Chaoming_Liacd48572011-04-25 12:52:54 -0500401 /* swlps or hwlps has been set in diff chip in init_sw_vars */
Johannes Berg30686bf2015-06-02 21:39:54 +0200402 if (rtlpriv->psc.swctrl_lps) {
403 ieee80211_hw_set(hw, SUPPORTS_PS);
404 ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
405 }
Ping-Ke Shihe332e2a22017-06-18 11:12:42 -0500406 if (rtlpriv->psc.fwctrl_lps) {
407 ieee80211_hw_set(hw, SUPPORTS_PS);
408 ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
409 }
Larry Finger0c817332010-12-08 11:12:31 -0600410 hw->wiphy->interface_modes =
Chaoming_Liacd48572011-04-25 12:52:54 -0500411 BIT(NL80211_IFTYPE_AP) |
412 BIT(NL80211_IFTYPE_STATION) |
Larry Finger26634c42013-03-24 22:06:33 -0500413 BIT(NL80211_IFTYPE_ADHOC) |
414 BIT(NL80211_IFTYPE_MESH_POINT) |
415 BIT(NL80211_IFTYPE_P2P_CLIENT) |
416 BIT(NL80211_IFTYPE_P2P_GO);
Larry Finger26634c42013-03-24 22:06:33 -0500417 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
Larry Finger9afa2e42014-09-22 09:39:21 -0500418
419 hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
420
Larry Finger0c817332010-12-08 11:12:31 -0600421 hw->wiphy->rts_threshold = 2347;
422
423 hw->queues = AC_MAX;
424 hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
425
426 /* TODO: Correct this value for our hw */
Souptick Joarder9d546192017-08-02 17:59:15 +0530427 hw->max_listen_interval = MAX_LISTEN_INTERVAL;
428 hw->max_rate_tries = MAX_RATE_TRIES;
Larry Finger0c817332010-12-08 11:12:31 -0600429 /* hw->max_rates = 1; */
Chaoming_Liacd48572011-04-25 12:52:54 -0500430 hw->sta_data_size = sizeof(struct rtl_sta_info);
Larry Finger0c817332010-12-08 11:12:31 -0600431
Larry Finger9afa2e42014-09-22 09:39:21 -0500432/* wowlan is not supported by kernel if CONFIG_PM is not defined */
433#ifdef CONFIG_PM
434 if (rtlpriv->psc.wo_wlan_mode) {
435 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
436 rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
437 if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
438 rtlpriv->wowlan.n_patterns =
439 MAX_SUPPORT_WOL_PATTERN_NUM;
440 rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
441 rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
442 }
443 hw->wiphy->wowlan = &rtlpriv->wowlan;
444 }
445#endif
446
Larry Finger0c817332010-12-08 11:12:31 -0600447 /* <6> mac address */
448 if (is_valid_ether_addr(rtlefuse->dev_addr)) {
449 SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
450 } else {
Larry Fingerabfabc92011-11-17 12:14:44 -0600451 u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
452 get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
453 SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
Larry Finger0c817332010-12-08 11:12:31 -0600454 }
Larry Finger0c817332010-12-08 11:12:31 -0600455}
456
457static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
458{
459 struct rtl_priv *rtlpriv = rtl_priv(hw);
460
461 /* <1> timer */
Kees Cook7c51d17c2017-10-24 02:28:29 -0700462 timer_setup(&rtlpriv->works.watchdog_timer,
463 rtl_watch_dog_timer_callback, 0);
464 timer_setup(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
465 rtl_easy_concurrent_retrytimer_callback, 0);
Larry Finger0c817332010-12-08 11:12:31 -0600466 /* <2> work queue */
467 rtlpriv->works.hw = hw;
Kees Cook598a0df2014-10-20 14:57:08 -0700468 rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
Larry Finger0c817332010-12-08 11:12:31 -0600469 INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
470 (void *)rtl_watchdog_wq_callback);
471 INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
472 (void *)rtl_ips_nic_off_wq_callback);
Chaoming_Liacd48572011-04-25 12:52:54 -0500473 INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
474 (void *)rtl_swlps_wq_callback);
475 INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
476 (void *)rtl_swlps_rfon_wq_callback);
Larry Finger26634c42013-03-24 22:06:33 -0500477 INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
478 (void *)rtl_fwevt_wq_callback);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600479 INIT_DELAYED_WORK(&rtlpriv->works.c2hcmd_wq,
480 (void *)rtl_c2hcmd_wq_callback);
Larry Finger0c817332010-12-08 11:12:31 -0600481
482}
483
484void rtl_deinit_deferred_work(struct ieee80211_hw *hw)
485{
486 struct rtl_priv *rtlpriv = rtl_priv(hw);
487
488 del_timer_sync(&rtlpriv->works.watchdog_timer);
489
490 cancel_delayed_work(&rtlpriv->works.watchdog_wq);
491 cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
Chaoming_Liacd48572011-04-25 12:52:54 -0500492 cancel_delayed_work(&rtlpriv->works.ps_work);
493 cancel_delayed_work(&rtlpriv->works.ps_rfon_wq);
Larry Finger26634c42013-03-24 22:06:33 -0500494 cancel_delayed_work(&rtlpriv->works.fwevt_wq);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600495 cancel_delayed_work(&rtlpriv->works.c2hcmd_wq);
Larry Finger0c817332010-12-08 11:12:31 -0600496}
Larry Finger6f334c22013-07-12 15:32:15 -0500497EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
Larry Finger0c817332010-12-08 11:12:31 -0600498
499void rtl_init_rfkill(struct ieee80211_hw *hw)
500{
501 struct rtl_priv *rtlpriv = rtl_priv(hw);
502
503 bool radio_state;
504 bool blocked;
505 u8 valid = 0;
506
Chaoming Li228bdfc2011-04-10 18:30:23 -0500507 /*set init state to on */
Rusty Russell3db1cd52011-12-19 13:56:45 +0000508 rtlpriv->rfkill.rfkill_state = true;
Chaoming Li228bdfc2011-04-10 18:30:23 -0500509 wiphy_rfkill_set_hw_state(hw->wiphy, 0);
510
Larry Finger0c817332010-12-08 11:12:31 -0600511 radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
512
Larry Fingere6d8a812010-12-21 19:40:40 -0600513 if (valid) {
Larry Finger9afa2e42014-09-22 09:39:21 -0500514 pr_info("rtlwifi: wireless switch is %s\n",
Joe Perches292b1192011-07-20 08:51:35 -0700515 rtlpriv->rfkill.rfkill_state ? "on" : "off");
Chaoming Li228bdfc2011-04-10 18:30:23 -0500516
Larry Finger0c817332010-12-08 11:12:31 -0600517 rtlpriv->rfkill.rfkill_state = radio_state;
518
519 blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
520 wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
521 }
522
523 wiphy_rfkill_start_polling(hw->wiphy);
524}
Larry Fingerb0302ab2012-01-30 09:54:49 -0600525EXPORT_SYMBOL(rtl_init_rfkill);
Larry Finger0c817332010-12-08 11:12:31 -0600526
527void rtl_deinit_rfkill(struct ieee80211_hw *hw)
528{
529 wiphy_rfkill_stop_polling(hw->wiphy);
530}
Larry Finger6f334c22013-07-12 15:32:15 -0500531EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
Larry Finger0c817332010-12-08 11:12:31 -0600532
533int rtl_init_core(struct ieee80211_hw *hw)
534{
535 struct rtl_priv *rtlpriv = rtl_priv(hw);
536 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
537
538 /* <1> init mac80211 */
539 _rtl_init_mac80211(hw);
540 rtlmac->hw = hw;
541
542 /* <2> rate control register */
Chaoming Li375ff4c2011-02-28 16:40:28 -0600543 hw->rate_control_algorithm = "rtl_rc";
Larry Finger0c817332010-12-08 11:12:31 -0600544
545 /*
546 * <3> init CRDA must come after init
547 * mac80211 hw in _rtl_init_mac80211.
548 */
549 if (rtl_regd_init(hw, rtl_reg_notifier)) {
Larry Fingerb03d9682016-12-15 12:22:58 -0600550 pr_err("REGD init failed\n");
Larry Finger0c817332010-12-08 11:12:31 -0600551 return 1;
Larry Finger0c817332010-12-08 11:12:31 -0600552 }
553
554 /* <4> locks */
Larry Finger8a09d6d2010-12-16 11:13:57 -0600555 mutex_init(&rtlpriv->locks.conf_mutex);
Ping-Ke Shiha3fa3662018-01-17 14:15:21 +0800556 mutex_init(&rtlpriv->locks.ips_mutex);
557 mutex_init(&rtlpriv->locks.lps_mutex);
Larry Finger0c817332010-12-08 11:12:31 -0600558 spin_lock_init(&rtlpriv->locks.irq_th_lock);
559 spin_lock_init(&rtlpriv->locks.h2c_lock);
560 spin_lock_init(&rtlpriv->locks.rf_ps_lock);
561 spin_lock_init(&rtlpriv->locks.rf_lock);
Chaoming_Liacd48572011-04-25 12:52:54 -0500562 spin_lock_init(&rtlpriv->locks.waitq_lock);
Larry Finger26634c42013-03-24 22:06:33 -0500563 spin_lock_init(&rtlpriv->locks.entry_list_lock);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600564 spin_lock_init(&rtlpriv->locks.c2hcmd_lock);
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -0500565 spin_lock_init(&rtlpriv->locks.scan_list_lock);
Chaoming_Liacd48572011-04-25 12:52:54 -0500566 spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
Larry Finger26634c42013-03-24 22:06:33 -0500567 spin_lock_init(&rtlpriv->locks.fw_ps_lock);
Larry Finger9afa2e42014-09-22 09:39:21 -0500568 spin_lock_init(&rtlpriv->locks.iqk_lock);
Larry Finger26634c42013-03-24 22:06:33 -0500569 /* <5> init list */
570 INIT_LIST_HEAD(&rtlpriv->entry_list);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600571 INIT_LIST_HEAD(&rtlpriv->c2hcmd_list);
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -0500572 INIT_LIST_HEAD(&rtlpriv->scan_list.list);
Larry Finger0c817332010-12-08 11:12:31 -0600573
574 rtlmac->link_state = MAC80211_NOLINK;
575
Larry Finger26634c42013-03-24 22:06:33 -0500576 /* <6> init deferred work */
Larry Finger0c817332010-12-08 11:12:31 -0600577 _rtl_init_deferred_work(hw);
578
579 return 0;
580}
Larry Finger6f334c22013-07-12 15:32:15 -0500581EXPORT_SYMBOL_GPL(rtl_init_core);
Larry Finger0c817332010-12-08 11:12:31 -0600582
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -0500583static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw);
584
Larry Finger0c817332010-12-08 11:12:31 -0600585void rtl_deinit_core(struct ieee80211_hw *hw)
586{
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -0600587 rtl_c2hcmd_launcher(hw, 0);
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -0500588 rtl_free_entries_from_scan_list(hw);
Larry Finger0c817332010-12-08 11:12:31 -0600589}
Larry Finger6f334c22013-07-12 15:32:15 -0500590EXPORT_SYMBOL_GPL(rtl_deinit_core);
Larry Finger0c817332010-12-08 11:12:31 -0600591
592void rtl_init_rx_config(struct ieee80211_hw *hw)
593{
594 struct rtl_priv *rtlpriv = rtl_priv(hw);
595 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
596
597 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
Larry Finger0c817332010-12-08 11:12:31 -0600598}
Larry Finger6f334c22013-07-12 15:32:15 -0500599EXPORT_SYMBOL_GPL(rtl_init_rx_config);
Larry Finger0c817332010-12-08 11:12:31 -0600600
601/*********************************************************
602 *
603 * tx information functions
604 *
605 *********************************************************/
606static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
607 struct rtl_tcb_desc *tcb_desc,
608 struct ieee80211_tx_info *info)
609{
610 struct rtl_priv *rtlpriv = rtl_priv(hw);
611 u8 rate_flag = info->control.rates[0].flags;
612
613 tcb_desc->use_shortpreamble = false;
614
615 /* 1M can only use Long Preamble. 11B spec */
616 if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
617 return;
618 else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
619 tcb_desc->use_shortpreamble = true;
620
621 return;
622}
623
624static void _rtl_query_shortgi(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500625 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600626 struct rtl_tcb_desc *tcb_desc,
627 struct ieee80211_tx_info *info)
628{
629 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
630 u8 rate_flag = info->control.rates[0].flags;
Chaoming_Liacd48572011-04-25 12:52:54 -0500631 u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
Larry Finger9afa2e42014-09-22 09:39:21 -0500632 u8 sgi_80 = 0, bw_80 = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600633 tcb_desc->use_shortgi = false;
634
Chaoming_Liacd48572011-04-25 12:52:54 -0500635 if (sta == NULL)
Larry Finger0c817332010-12-08 11:12:31 -0600636 return;
637
Chaoming_Liacd48572011-04-25 12:52:54 -0500638 sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
639 sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
Larry Finger9afa2e42014-09-22 09:39:21 -0500640 sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
Chaoming_Liacd48572011-04-25 12:52:54 -0500641
Larry Finger9afa2e42014-09-22 09:39:21 -0500642 if ((!sta->ht_cap.ht_supported) && (!sta->vht_cap.vht_supported))
Larry Finger0c817332010-12-08 11:12:31 -0600643 return;
644
Chaoming_Liacd48572011-04-25 12:52:54 -0500645 if (!sgi_40 && !sgi_20)
646 return;
647
Larry Finger9afa2e42014-09-22 09:39:21 -0500648 if (mac->opmode == NL80211_IFTYPE_STATION) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500649 bw_40 = mac->bw_40;
Larry Finger9afa2e42014-09-22 09:39:21 -0500650 bw_80 = mac->bw_80;
651 } else if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Finger26634c42013-03-24 22:06:33 -0500652 mac->opmode == NL80211_IFTYPE_ADHOC ||
Larry Finger9afa2e42014-09-22 09:39:21 -0500653 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
654 bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
655 bw_80 = sta->vht_cap.vht_supported;
656 }
Chaoming_Liacd48572011-04-25 12:52:54 -0500657
Larry Finger9afa2e42014-09-22 09:39:21 -0500658 if (bw_80) {
659 if (sgi_80)
660 tcb_desc->use_shortgi = true;
661 else
662 tcb_desc->use_shortgi = false;
663 } else {
664 if (bw_40 && sgi_40)
665 tcb_desc->use_shortgi = true;
666 else if (!bw_40 && sgi_20)
667 tcb_desc->use_shortgi = true;
668 }
Larry Finger0c817332010-12-08 11:12:31 -0600669
670 if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
671 tcb_desc->use_shortgi = false;
Larry Finger0c817332010-12-08 11:12:31 -0600672}
673
674static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
675 struct rtl_tcb_desc *tcb_desc,
676 struct ieee80211_tx_info *info)
677{
678 struct rtl_priv *rtlpriv = rtl_priv(hw);
679 u8 rate_flag = info->control.rates[0].flags;
680
681 /* Common Settings */
Larry Finger7ea47242011-02-19 16:28:57 -0600682 tcb_desc->rts_stbc = false;
683 tcb_desc->cts_enable = false;
Larry Finger0c817332010-12-08 11:12:31 -0600684 tcb_desc->rts_sc = 0;
Larry Finger7ea47242011-02-19 16:28:57 -0600685 tcb_desc->rts_bw = false;
686 tcb_desc->rts_use_shortpreamble = false;
687 tcb_desc->rts_use_shortgi = false;
Larry Finger0c817332010-12-08 11:12:31 -0600688
689 if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
690 /* Use CTS-to-SELF in protection mode. */
Larry Finger7ea47242011-02-19 16:28:57 -0600691 tcb_desc->rts_enable = true;
692 tcb_desc->cts_enable = true;
Larry Finger0c817332010-12-08 11:12:31 -0600693 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
694 } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
695 /* Use RTS-CTS in protection mode. */
Larry Finger7ea47242011-02-19 16:28:57 -0600696 tcb_desc->rts_enable = true;
Larry Finger0c817332010-12-08 11:12:31 -0600697 tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
698 }
Larry Finger0c817332010-12-08 11:12:31 -0600699}
700
701static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500702 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600703 struct rtl_tcb_desc *tcb_desc)
704{
705 struct rtl_priv *rtlpriv = rtl_priv(hw);
706 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -0500707 struct rtl_sta_info *sta_entry = NULL;
708 u8 ratr_index = 7;
Larry Finger0c817332010-12-08 11:12:31 -0600709
Chaoming_Liacd48572011-04-25 12:52:54 -0500710 if (sta) {
711 sta_entry = (struct rtl_sta_info *) sta->drv_priv;
712 ratr_index = sta_entry->ratr_index;
713 }
Larry Finger0c817332010-12-08 11:12:31 -0600714 if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500715 if (mac->opmode == NL80211_IFTYPE_STATION) {
Larry Finger0c817332010-12-08 11:12:31 -0600716 tcb_desc->ratr_index = 0;
Larry Finger26634c42013-03-24 22:06:33 -0500717 } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
Larry Finger9afa2e42014-09-22 09:39:21 -0500718 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
Larry Finger7ea47242011-02-19 16:28:57 -0600719 if (tcb_desc->multicast || tcb_desc->broadcast) {
Larry Finger0c817332010-12-08 11:12:31 -0600720 tcb_desc->hw_rate =
721 rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
722 tcb_desc->use_driver_rate = 1;
Larry Finger26634c42013-03-24 22:06:33 -0500723 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -0600724 } else {
Larry Finger26634c42013-03-24 22:06:33 -0500725 tcb_desc->ratr_index = ratr_index;
Larry Finger0c817332010-12-08 11:12:31 -0600726 }
Chaoming_Liacd48572011-04-25 12:52:54 -0500727 } else if (mac->opmode == NL80211_IFTYPE_AP) {
728 tcb_desc->ratr_index = ratr_index;
Larry Finger0c817332010-12-08 11:12:31 -0600729 }
730 }
731
Larry Finger7ea47242011-02-19 16:28:57 -0600732 if (rtlpriv->dm.useramask) {
Larry Finger26634c42013-03-24 22:06:33 -0500733 tcb_desc->ratr_index = ratr_index;
734 /* TODO we will differentiate adhoc and station future */
735 if (mac->opmode == NL80211_IFTYPE_STATION ||
736 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500737 tcb_desc->mac_id = 0;
Larry Finger0c817332010-12-08 11:12:31 -0600738
Larry Finger9afa2e42014-09-22 09:39:21 -0500739 if (mac->mode == WIRELESS_MODE_AC_5G)
740 tcb_desc->ratr_index =
741 RATR_INX_WIRELESS_AC_5N;
742 else if (mac->mode == WIRELESS_MODE_AC_24G)
743 tcb_desc->ratr_index =
744 RATR_INX_WIRELESS_AC_24N;
745 else if (mac->mode == WIRELESS_MODE_N_24G)
Chaoming_Liacd48572011-04-25 12:52:54 -0500746 tcb_desc->ratr_index = RATR_INX_WIRELESS_NGB;
747 else if (mac->mode == WIRELESS_MODE_N_5G)
748 tcb_desc->ratr_index = RATR_INX_WIRELESS_NG;
749 else if (mac->mode & WIRELESS_MODE_G)
750 tcb_desc->ratr_index = RATR_INX_WIRELESS_GB;
751 else if (mac->mode & WIRELESS_MODE_B)
752 tcb_desc->ratr_index = RATR_INX_WIRELESS_B;
753 else if (mac->mode & WIRELESS_MODE_A)
754 tcb_desc->ratr_index = RATR_INX_WIRELESS_G;
Larry Finger9afa2e42014-09-22 09:39:21 -0500755
Chaoming_Liacd48572011-04-25 12:52:54 -0500756 } else if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Finger9afa2e42014-09-22 09:39:21 -0500757 mac->opmode == NL80211_IFTYPE_ADHOC) {
Chaoming_Liacd48572011-04-25 12:52:54 -0500758 if (NULL != sta) {
759 if (sta->aid > 0)
760 tcb_desc->mac_id = sta->aid + 1;
761 else
762 tcb_desc->mac_id = 1;
763 } else {
764 tcb_desc->mac_id = 0;
765 }
Larry Finger0c817332010-12-08 11:12:31 -0600766 }
767 }
Larry Finger0c817332010-12-08 11:12:31 -0600768}
769
770static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
Chaoming_Liacd48572011-04-25 12:52:54 -0500771 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -0600772 struct rtl_tcb_desc *tcb_desc)
773{
774 struct rtl_priv *rtlpriv = rtl_priv(hw);
775 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
776
Larry Finger7ea47242011-02-19 16:28:57 -0600777 tcb_desc->packet_bw = false;
Chaoming_Liacd48572011-04-25 12:52:54 -0500778 if (!sta)
Larry Finger0c817332010-12-08 11:12:31 -0600779 return;
Chaoming_Liacd48572011-04-25 12:52:54 -0500780 if (mac->opmode == NL80211_IFTYPE_AP ||
Larry Finger26634c42013-03-24 22:06:33 -0500781 mac->opmode == NL80211_IFTYPE_ADHOC ||
782 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
Larry Finger9afa2e42014-09-22 09:39:21 -0500783 if (!(sta->ht_cap.ht_supported) ||
784 !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
Chaoming_Liacd48572011-04-25 12:52:54 -0500785 return;
786 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
787 if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
788 return;
789 }
Larry Finger7ea47242011-02-19 16:28:57 -0600790 if (tcb_desc->multicast || tcb_desc->broadcast)
Larry Finger0c817332010-12-08 11:12:31 -0600791 return;
792
793 /*use legency rate, shall use 20MHz */
794 if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
795 return;
796
Larry Finger9afa2e42014-09-22 09:39:21 -0500797 tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
798
799 if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
800 rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8821AE) {
801 if (mac->opmode == NL80211_IFTYPE_AP ||
802 mac->opmode == NL80211_IFTYPE_ADHOC ||
803 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
804 if (!(sta->vht_cap.vht_supported))
805 return;
806 } else if (mac->opmode == NL80211_IFTYPE_STATION) {
807 if (!mac->bw_80 ||
808 !(sta->vht_cap.vht_supported))
809 return;
810 }
811 if (tcb_desc->hw_rate <=
812 rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
813 return;
814 tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
815 }
Larry Finger0c817332010-12-08 11:12:31 -0600816}
817
Larry Finger9afa2e42014-09-22 09:39:21 -0500818static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
819 struct ieee80211_sta *sta)
Larry Finger0c817332010-12-08 11:12:31 -0600820{
821 struct rtl_priv *rtlpriv = rtl_priv(hw);
822 struct rtl_phy *rtlphy = &(rtlpriv->phy);
823 u8 hw_rate;
Larry Finger9afa2e42014-09-22 09:39:21 -0500824 u16 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
Larry Finger0c817332010-12-08 11:12:31 -0600825
Larry Finger9afa2e42014-09-22 09:39:21 -0500826 if ((get_rf_type(rtlphy) == RF_2T2R) &&
827 (tx_mcs_map & 0x000c) != 0x000c) {
828 if ((tx_mcs_map & 0x000c) >> 2 ==
829 IEEE80211_VHT_MCS_SUPPORT_0_7)
830 hw_rate =
831 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
832 else if ((tx_mcs_map & 0x000c) >> 2 ==
833 IEEE80211_VHT_MCS_SUPPORT_0_8)
834 hw_rate =
Larry Fingera7986ce2017-10-14 20:54:02 -0500835 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS8];
Larry Finger9afa2e42014-09-22 09:39:21 -0500836 else
837 hw_rate =
838 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
839 } else {
840 if ((tx_mcs_map & 0x0003) ==
841 IEEE80211_VHT_MCS_SUPPORT_0_7)
842 hw_rate =
843 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
844 else if ((tx_mcs_map & 0x0003) ==
845 IEEE80211_VHT_MCS_SUPPORT_0_8)
846 hw_rate =
Larry Fingera7986ce2017-10-14 20:54:02 -0500847 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS8];
Larry Finger9afa2e42014-09-22 09:39:21 -0500848 else
849 hw_rate =
850 rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
851 }
852
853 return hw_rate;
854}
855
856static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
857 struct ieee80211_sta *sta)
858{
859 struct rtl_priv *rtlpriv = rtl_priv(hw);
860 struct rtl_phy *rtlphy = &rtlpriv->phy;
861 u8 hw_rate;
862
Frank A. Cancio Bellod63589c2017-12-29 16:31:08 +0800863 if (get_rf_type(rtlphy) == RF_2T2R &&
864 sta->ht_cap.mcs.rx_mask[1] != 0)
Larry Finger0c817332010-12-08 11:12:31 -0600865 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
866 else
867 hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
868
869 return hw_rate;
870}
871
Larry Finger7ad0ce32011-08-22 16:50:14 -0500872/* mac80211's rate_idx is like this:
873 *
Johannes Berg57fbcce2016-04-12 15:56:15 +0200874 * 2.4G band:rx_status->band == NL80211_BAND_2GHZ
Larry Finger7ad0ce32011-08-22 16:50:14 -0500875 *
876 * B/G rate:
877 * (rx_status->flag & RX_FLAG_HT) = 0,
Larry Fingere0e776a2014-12-18 03:05:36 -0600878 * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
Larry Finger7ad0ce32011-08-22 16:50:14 -0500879 *
880 * N rate:
881 * (rx_status->flag & RX_FLAG_HT) = 1,
Larry Fingere0e776a2014-12-18 03:05:36 -0600882 * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
Larry Finger7ad0ce32011-08-22 16:50:14 -0500883 *
Johannes Berg57fbcce2016-04-12 15:56:15 +0200884 * 5G band:rx_status->band == NL80211_BAND_5GHZ
Larry Finger7ad0ce32011-08-22 16:50:14 -0500885 * A rate:
886 * (rx_status->flag & RX_FLAG_HT) = 0,
Larry Fingere0e776a2014-12-18 03:05:36 -0600887 * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
Larry Finger7ad0ce32011-08-22 16:50:14 -0500888 *
889 * N rate:
890 * (rx_status->flag & RX_FLAG_HT) = 1,
Larry Fingere0e776a2014-12-18 03:05:36 -0600891 * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
Larry Fingerfd3cb222014-12-18 03:05:40 -0600892 *
893 * VHT rates:
894 * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
895 * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
Larry Finger7ad0ce32011-08-22 16:50:14 -0500896 */
Larry Fingerfd3cb222014-12-18 03:05:40 -0600897int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
898 u8 desc_rate)
Larry Finger7ad0ce32011-08-22 16:50:14 -0500899{
900 int rate_idx;
901
Larry Fingerfd3cb222014-12-18 03:05:40 -0600902 if (isvht) {
903 switch (desc_rate) {
904 case DESC_RATEVHT1SS_MCS0:
905 rate_idx = 0;
906 break;
907 case DESC_RATEVHT1SS_MCS1:
908 rate_idx = 1;
909 break;
910 case DESC_RATEVHT1SS_MCS2:
911 rate_idx = 2;
912 break;
913 case DESC_RATEVHT1SS_MCS3:
914 rate_idx = 3;
915 break;
916 case DESC_RATEVHT1SS_MCS4:
917 rate_idx = 4;
918 break;
919 case DESC_RATEVHT1SS_MCS5:
920 rate_idx = 5;
921 break;
922 case DESC_RATEVHT1SS_MCS6:
923 rate_idx = 6;
924 break;
925 case DESC_RATEVHT1SS_MCS7:
926 rate_idx = 7;
927 break;
928 case DESC_RATEVHT1SS_MCS8:
929 rate_idx = 8;
930 break;
931 case DESC_RATEVHT1SS_MCS9:
932 rate_idx = 9;
933 break;
934 case DESC_RATEVHT2SS_MCS0:
935 rate_idx = 0;
936 break;
937 case DESC_RATEVHT2SS_MCS1:
938 rate_idx = 1;
939 break;
940 case DESC_RATEVHT2SS_MCS2:
941 rate_idx = 2;
942 break;
943 case DESC_RATEVHT2SS_MCS3:
944 rate_idx = 3;
945 break;
946 case DESC_RATEVHT2SS_MCS4:
947 rate_idx = 4;
948 break;
949 case DESC_RATEVHT2SS_MCS5:
950 rate_idx = 5;
951 break;
952 case DESC_RATEVHT2SS_MCS6:
953 rate_idx = 6;
954 break;
955 case DESC_RATEVHT2SS_MCS7:
956 rate_idx = 7;
957 break;
958 case DESC_RATEVHT2SS_MCS8:
959 rate_idx = 8;
960 break;
961 case DESC_RATEVHT2SS_MCS9:
962 rate_idx = 9;
963 break;
964 default:
965 rate_idx = 0;
966 break;
967 }
968 return rate_idx;
969 }
Larry Finger7ad0ce32011-08-22 16:50:14 -0500970 if (false == isht) {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200971 if (NL80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
Larry Finger7ad0ce32011-08-22 16:50:14 -0500972 switch (desc_rate) {
Larry Fingere0e776a2014-12-18 03:05:36 -0600973 case DESC_RATE1M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500974 rate_idx = 0;
975 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600976 case DESC_RATE2M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500977 rate_idx = 1;
978 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600979 case DESC_RATE5_5M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500980 rate_idx = 2;
981 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600982 case DESC_RATE11M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500983 rate_idx = 3;
984 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600985 case DESC_RATE6M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500986 rate_idx = 4;
987 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600988 case DESC_RATE9M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500989 rate_idx = 5;
990 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600991 case DESC_RATE12M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500992 rate_idx = 6;
993 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600994 case DESC_RATE18M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500995 rate_idx = 7;
996 break;
Larry Fingere0e776a2014-12-18 03:05:36 -0600997 case DESC_RATE24M:
Larry Finger7ad0ce32011-08-22 16:50:14 -0500998 rate_idx = 8;
999 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001000 case DESC_RATE36M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001001 rate_idx = 9;
1002 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001003 case DESC_RATE48M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001004 rate_idx = 10;
1005 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001006 case DESC_RATE54M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001007 rate_idx = 11;
1008 break;
1009 default:
1010 rate_idx = 0;
1011 break;
1012 }
1013 } else {
1014 switch (desc_rate) {
Larry Fingere0e776a2014-12-18 03:05:36 -06001015 case DESC_RATE6M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001016 rate_idx = 0;
1017 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001018 case DESC_RATE9M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001019 rate_idx = 1;
1020 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001021 case DESC_RATE12M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001022 rate_idx = 2;
1023 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001024 case DESC_RATE18M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001025 rate_idx = 3;
1026 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001027 case DESC_RATE24M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001028 rate_idx = 4;
1029 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001030 case DESC_RATE36M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001031 rate_idx = 5;
1032 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001033 case DESC_RATE48M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001034 rate_idx = 6;
1035 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001036 case DESC_RATE54M:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001037 rate_idx = 7;
1038 break;
1039 default:
1040 rate_idx = 0;
1041 break;
1042 }
1043 }
Larry Finger7ad0ce32011-08-22 16:50:14 -05001044 } else {
Larry Finger7ad0ce32011-08-22 16:50:14 -05001045 switch (desc_rate) {
Larry Fingere0e776a2014-12-18 03:05:36 -06001046 case DESC_RATEMCS0:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001047 rate_idx = 0;
1048 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001049 case DESC_RATEMCS1:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001050 rate_idx = 1;
1051 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001052 case DESC_RATEMCS2:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001053 rate_idx = 2;
1054 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001055 case DESC_RATEMCS3:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001056 rate_idx = 3;
1057 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001058 case DESC_RATEMCS4:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001059 rate_idx = 4;
1060 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001061 case DESC_RATEMCS5:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001062 rate_idx = 5;
1063 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001064 case DESC_RATEMCS6:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001065 rate_idx = 6;
1066 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001067 case DESC_RATEMCS7:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001068 rate_idx = 7;
1069 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001070 case DESC_RATEMCS8:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001071 rate_idx = 8;
1072 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001073 case DESC_RATEMCS9:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001074 rate_idx = 9;
1075 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001076 case DESC_RATEMCS10:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001077 rate_idx = 10;
1078 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001079 case DESC_RATEMCS11:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001080 rate_idx = 11;
1081 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001082 case DESC_RATEMCS12:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001083 rate_idx = 12;
1084 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001085 case DESC_RATEMCS13:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001086 rate_idx = 13;
1087 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001088 case DESC_RATEMCS14:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001089 rate_idx = 14;
1090 break;
Larry Fingere0e776a2014-12-18 03:05:36 -06001091 case DESC_RATEMCS15:
Larry Finger7ad0ce32011-08-22 16:50:14 -05001092 rate_idx = 15;
1093 break;
1094 default:
1095 rate_idx = 0;
1096 break;
1097 }
1098 }
1099 return rate_idx;
1100}
1101EXPORT_SYMBOL(rtlwifi_rate_mapping);
1102
Ping-Ke Shihcdc9c7a2017-09-29 14:47:54 -05001103static u8 _rtl_get_tx_hw_rate(struct ieee80211_hw *hw,
1104 struct ieee80211_tx_info *info)
1105{
1106 struct rtl_priv *rtlpriv = rtl_priv(hw);
1107 struct ieee80211_tx_rate *r = &info->status.rates[0];
1108 struct ieee80211_rate *txrate;
1109 u8 hw_value = 0x0;
1110
1111 if (r->flags & IEEE80211_TX_RC_MCS) {
1112 /* HT MCS0-15 */
1113 hw_value = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15] - 15 +
1114 r->idx;
1115 } else if (r->flags & IEEE80211_TX_RC_VHT_MCS) {
1116 /* VHT MCS0-9, NSS */
1117 if (ieee80211_rate_get_vht_nss(r) == 2)
1118 hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
1119 else
1120 hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
1121
1122 hw_value = hw_value - 9 + ieee80211_rate_get_vht_mcs(r);
1123 } else {
1124 /* legacy */
1125 txrate = ieee80211_get_tx_rate(hw, info);
1126
1127 if (txrate)
1128 hw_value = txrate->hw_value;
1129 }
1130
1131 /* check 5G band */
1132 if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G &&
1133 hw_value < rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M])
1134 hw_value = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M];
1135
1136 return hw_value;
1137}
1138
Larry Finger0c817332010-12-08 11:12:31 -06001139void rtl_get_tcb_desc(struct ieee80211_hw *hw,
1140 struct ieee80211_tx_info *info,
Chaoming_Liacd48572011-04-25 12:52:54 -05001141 struct ieee80211_sta *sta,
Larry Finger0c817332010-12-08 11:12:31 -06001142 struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
1143{
1144 struct rtl_priv *rtlpriv = rtl_priv(hw);
1145 struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001146 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
Ping-Ke Shihcdc9c7a2017-09-29 14:47:54 -05001147
Larry Finger9afa2e42014-09-22 09:39:21 -05001148 __le16 fc = rtl_get_fc(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001149
Ping-Ke Shihcdc9c7a2017-09-29 14:47:54 -05001150 tcb_desc->hw_rate = _rtl_get_tx_hw_rate(hw, info);
Larry Finger0c817332010-12-08 11:12:31 -06001151
Ping-Ke Shih84795802017-06-18 11:12:44 -05001152 if (rtl_is_tx_report_skb(hw, skb))
1153 tcb_desc->use_spe_rpt = 1;
1154
Larry Finger0c817332010-12-08 11:12:31 -06001155 if (ieee80211_is_data(fc)) {
Larry Finger0c817332010-12-08 11:12:31 -06001156 /*
Chaoming_Liacd48572011-04-25 12:52:54 -05001157 *we set data rate INX 0
Larry Finger0c817332010-12-08 11:12:31 -06001158 *in rtl_rc.c if skb is special data or
1159 *mgt which need low data rate.
1160 */
1161
1162 /*
1163 *So tcb_desc->hw_rate is just used for
1164 *special data and mgt frames
1165 */
Chaoming Li09e92f02011-10-11 21:28:48 -05001166 if (info->control.rates[0].idx == 0 ||
Chaoming_Liacd48572011-04-25 12:52:54 -05001167 ieee80211_is_nullfunc(fc)) {
Larry Finger0c817332010-12-08 11:12:31 -06001168 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
Larry Finger0c817332010-12-08 11:12:31 -06001171 tcb_desc->disable_ratefallback = 1;
1172 } else {
1173 /*
1174 *because hw will nerver use hw_rate
1175 *when tcb_desc->use_driver_rate = false
1176 *so we never set highest N rate here,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001177 *and N rate will all be controlled by FW
Larry Finger0c817332010-12-08 11:12:31 -06001178 *when tcb_desc->use_driver_rate = false
1179 */
Larry Finger9afa2e42014-09-22 09:39:21 -05001180 if (sta && sta->vht_cap.vht_supported) {
1181 tcb_desc->hw_rate =
1182 _rtl_get_vht_highest_n_rate(hw, sta);
Larry Finger0c817332010-12-08 11:12:31 -06001183 } else {
Frank A. Cancio Bellod63589c2017-12-29 16:31:08 +08001184 if (sta && sta->ht_cap.ht_supported) {
Larry Finger0c817332010-12-08 11:12:31 -06001185 tcb_desc->hw_rate =
Larry Finger9afa2e42014-09-22 09:39:21 -05001186 _rtl_get_highest_n_rate(hw, sta);
Larry Finger0c817332010-12-08 11:12:31 -06001187 } else {
Larry Finger9afa2e42014-09-22 09:39:21 -05001188 if (rtlmac->mode == WIRELESS_MODE_B) {
1189 tcb_desc->hw_rate =
1190 rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
1191 } else {
1192 tcb_desc->hw_rate =
1193 rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
1194 }
Larry Finger0c817332010-12-08 11:12:31 -06001195 }
1196 }
1197 }
1198
Ping-Ke Shihe7c52862017-07-02 13:12:36 -05001199 if (is_multicast_ether_addr(hdr->addr1))
Larry Finger7ea47242011-02-19 16:28:57 -06001200 tcb_desc->multicast = 1;
Ping-Ke Shihe7c52862017-07-02 13:12:36 -05001201 else if (is_broadcast_ether_addr(hdr->addr1))
Larry Finger7ea47242011-02-19 16:28:57 -06001202 tcb_desc->broadcast = 1;
Larry Finger0c817332010-12-08 11:12:31 -06001203
Chaoming_Liacd48572011-04-25 12:52:54 -05001204 _rtl_txrate_selectmode(hw, sta, tcb_desc);
1205 _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
Larry Finger0c817332010-12-08 11:12:31 -06001206 _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
Chaoming_Liacd48572011-04-25 12:52:54 -05001207 _rtl_query_shortgi(hw, sta, tcb_desc, info);
Larry Finger0c817332010-12-08 11:12:31 -06001208 _rtl_query_protection_mode(hw, tcb_desc, info);
1209 } else {
1210 tcb_desc->use_driver_rate = true;
Chaoming_Liacd48572011-04-25 12:52:54 -05001211 tcb_desc->ratr_index = RATR_INX_WIRELESS_MC;
Larry Finger0c817332010-12-08 11:12:31 -06001212 tcb_desc->disable_ratefallback = 1;
1213 tcb_desc->mac_id = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001214 tcb_desc->packet_bw = false;
Larry Finger0c817332010-12-08 11:12:31 -06001215 }
1216}
1217EXPORT_SYMBOL(rtl_get_tcb_desc);
1218
Larry Finger9afa2e42014-09-22 09:39:21 -05001219bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
1220{
1221 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1222 struct rtl_priv *rtlpriv = rtl_priv(hw);
1223 __le16 fc = rtl_get_fc(skb);
1224
1225 if (rtlpriv->dm.supp_phymode_switch &&
1226 mac->link_state < MAC80211_LINKED &&
1227 (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
1228 if (rtlpriv->cfg->ops->chk_switch_dmdp)
1229 rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1230 }
1231 if (ieee80211_is_auth(fc)) {
1232 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
Larry Finger9afa2e42014-09-22 09:39:21 -05001233
1234 mac->link_state = MAC80211_LINKING;
1235 /* Dul mac */
1236 rtlpriv->phy.need_iqk = true;
1237
1238 }
1239
1240 return true;
1241}
1242EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
1243
1244struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
1245 u8 *bssid, u16 tid);
1246
1247static void process_agg_start(struct ieee80211_hw *hw,
1248 struct ieee80211_hdr *hdr, u16 tid)
Larry Finger26634c42013-03-24 22:06:33 -05001249{
1250 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger26634c42013-03-24 22:06:33 -05001251 struct ieee80211_rx_status rx_status = { 0 };
Larry Finger9afa2e42014-09-22 09:39:21 -05001252 struct sk_buff *skb_delba = NULL;
Larry Finger26634c42013-03-24 22:06:33 -05001253
Larry Finger9afa2e42014-09-22 09:39:21 -05001254 skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
1255 if (skb_delba) {
1256 rx_status.freq = hw->conf.chandef.chan->center_freq;
1257 rx_status.band = hw->conf.chandef.chan->band;
1258 rx_status.flag |= RX_FLAG_DECRYPTED;
1259 rx_status.flag |= RX_FLAG_MACTIME_START;
1260 rx_status.rate_idx = 0;
1261 rx_status.signal = 50 + 10;
1262 memcpy(IEEE80211_SKB_RXCB(skb_delba),
1263 &rx_status, sizeof(rx_status));
1264 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1265 "fake del\n",
1266 skb_delba->data,
1267 skb_delba->len);
1268 ieee80211_rx_irqsafe(hw, skb_delba);
Larry Finger26634c42013-03-24 22:06:33 -05001269 }
Larry Finger26634c42013-03-24 22:06:33 -05001270}
1271
Larry Finger0c817332010-12-08 11:12:31 -06001272bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1273{
1274 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Chaoming_Liacd48572011-04-25 12:52:54 -05001275 struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
Larry Finger0c817332010-12-08 11:12:31 -06001276 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001277 __le16 fc = rtl_get_fc(skb);
1278 u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
Larry Finger0c817332010-12-08 11:12:31 -06001279 u8 category;
1280
1281 if (!ieee80211_is_action(fc))
1282 return true;
1283
1284 category = *act;
1285 act++;
1286 switch (category) {
1287 case ACT_CAT_BA:
1288 switch (*act) {
1289 case ACT_ADDBAREQ:
1290 if (mac->act_scanning)
1291 return false;
1292
1293 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Larry Finger9afa2e42014-09-22 09:39:21 -05001294 "%s ACT_ADDBAREQ From :%pM\n",
1295 is_tx ? "Tx" : "Rx", hdr->addr2);
Larry Finger26634c42013-03-24 22:06:33 -05001296 RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
Larry Finger9afa2e42014-09-22 09:39:21 -05001297 skb->data, skb->len);
1298 if (!is_tx) {
1299 struct ieee80211_sta *sta = NULL;
1300 struct rtl_sta_info *sta_entry = NULL;
1301 struct rtl_tid_data *tid_data;
1302 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1303 u16 capab = 0, tid = 0;
1304
1305 rcu_read_lock();
1306 sta = rtl_find_sta(hw, hdr->addr3);
1307 if (sta == NULL) {
1308 RT_TRACE(rtlpriv, COMP_SEND | COMP_RECV,
1309 DBG_DMESG, "sta is NULL\n");
1310 rcu_read_unlock();
Larry Finger26634c42013-03-24 22:06:33 -05001311 return true;
Larry Finger9afa2e42014-09-22 09:39:21 -05001312 }
1313
1314 sta_entry =
1315 (struct rtl_sta_info *)sta->drv_priv;
1316 if (!sta_entry) {
1317 rcu_read_unlock();
1318 return true;
1319 }
1320 capab =
1321 le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1322 tid = (capab &
1323 IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
Dan Carpenter5e0c1f02017-12-29 16:31:03 +08001324 if (tid >= MAX_TID_COUNT) {
1325 rcu_read_unlock();
1326 return true;
1327 }
Larry Finger9afa2e42014-09-22 09:39:21 -05001328 tid_data = &sta_entry->tids[tid];
1329 if (tid_data->agg.rx_agg_state ==
1330 RTL_RX_AGG_START)
1331 process_agg_start(hw, hdr, tid);
1332 rcu_read_unlock();
1333 }
Larry Finger0c817332010-12-08 11:12:31 -06001334 break;
1335 case ACT_ADDBARSP:
1336 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001337 "%s ACT_ADDBARSP From :%pM\n",
Larry Finger9afa2e42014-09-22 09:39:21 -05001338 is_tx ? "Tx" : "Rx", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -06001339 break;
1340 case ACT_DELBA:
1341 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
Joe Perchesf30d7502012-01-04 19:40:41 -08001342 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
Larry Finger0c817332010-12-08 11:12:31 -06001343 break;
1344 }
1345 break;
1346 default:
1347 break;
1348 }
1349
1350 return true;
1351}
Larry Finger6f334c22013-07-12 15:32:15 -05001352EXPORT_SYMBOL_GPL(rtl_action_proc);
Larry Finger0c817332010-12-08 11:12:31 -06001353
Ping-Ke Shih5b757ba2017-06-18 11:12:45 -05001354static void setup_special_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc,
1355 int type)
Larry Finger9afa2e42014-09-22 09:39:21 -05001356{
Larry Fingerba9f93f2016-11-26 14:43:35 -06001357 struct ieee80211_hw *hw = rtlpriv->hw;
1358
Larry Finger9afa2e42014-09-22 09:39:21 -05001359 rtlpriv->ra.is_special_data = true;
1360 if (rtlpriv->cfg->ops->get_btc_status())
1361 rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
Ping-Ke Shih5b757ba2017-06-18 11:12:45 -05001362 rtlpriv, type);
Larry Fingerba9f93f2016-11-26 14:43:35 -06001363 rtl_lps_leave(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001364 ppsc->last_delaylps_stamp_jiffies = jiffies;
1365}
1366
Ping-Ke Shih84795802017-06-18 11:12:44 -05001367static const u8 *rtl_skb_ether_type_ptr(struct ieee80211_hw *hw,
1368 struct sk_buff *skb, bool is_enc)
Larry Finger0c817332010-12-08 11:12:31 -06001369{
1370 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Finger0c817332010-12-08 11:12:31 -06001371 u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
Larry Finger9afa2e42014-09-22 09:39:21 -05001372 u8 encrypt_header_len = 0;
1373 u8 offset;
Larry Finger0c817332010-12-08 11:12:31 -06001374
Larry Finger9afa2e42014-09-22 09:39:21 -05001375 switch (rtlpriv->sec.pairwise_enc_algorithm) {
1376 case WEP40_ENCRYPTION:
1377 case WEP104_ENCRYPTION:
1378 encrypt_header_len = 4;/*WEP_IV_LEN*/
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001379 break;
Larry Finger9afa2e42014-09-22 09:39:21 -05001380 case TKIP_ENCRYPTION:
1381 encrypt_header_len = 8;/*TKIP_IV_LEN*/
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001382 break;
Larry Finger9afa2e42014-09-22 09:39:21 -05001383 case AESCCMP_ENCRYPTION:
1384 encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001385 break;
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001386 default:
Larry Finger9afa2e42014-09-22 09:39:21 -05001387 break;
Larry Finger0c817332010-12-08 11:12:31 -06001388 }
Larry Finger9afa2e42014-09-22 09:39:21 -05001389
Taehee Yoocad737d2015-03-28 00:22:39 +09001390 offset = mac_hdr_len + SNAP_SIZE;
1391 if (is_enc)
1392 offset += encrypt_header_len;
Ping-Ke Shih84795802017-06-18 11:12:44 -05001393
1394 return skb->data + offset;
1395}
1396
1397/*should call before software enc*/
1398u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
1399 bool is_enc)
1400{
1401 struct rtl_priv *rtlpriv = rtl_priv(hw);
1402 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1403 __le16 fc = rtl_get_fc(skb);
1404 u16 ether_type;
1405 const u8 *ether_type_ptr;
1406 const struct iphdr *ip;
1407
1408 if (!ieee80211_is_data(fc))
1409 goto end;
1410
1411 ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, is_enc);
1412 ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
Larry Finger9afa2e42014-09-22 09:39:21 -05001413
1414 if (ETH_P_IP == ether_type) {
Ping-Ke Shih84795802017-06-18 11:12:44 -05001415 ip = (struct iphdr *)((u8 *)ether_type_ptr +
Larry Finger9afa2e42014-09-22 09:39:21 -05001416 PROTOC_TYPE_SIZE);
1417 if (IPPROTO_UDP == ip->protocol) {
1418 struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1419 (ip->ihl << 2));
1420 if (((((u8 *)udp)[1] == 68) &&
1421 (((u8 *)udp)[3] == 67)) ||
1422 ((((u8 *)udp)[1] == 67) &&
1423 (((u8 *)udp)[3] == 68))) {
1424 /* 68 : UDP BOOTP client
1425 * 67 : UDP BOOTP server
1426 */
1427 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1428 DBG_DMESG, "dhcp %s !!\n",
1429 (is_tx) ? "Tx" : "Rx");
1430
1431 if (is_tx)
Ping-Ke Shih5b757ba2017-06-18 11:12:45 -05001432 setup_special_tx(rtlpriv, ppsc,
1433 PACKET_DHCP);
1434
Larry Finger9afa2e42014-09-22 09:39:21 -05001435 return true;
1436 }
1437 }
1438 } else if (ETH_P_ARP == ether_type) {
1439 if (is_tx)
Ping-Ke Shih5b757ba2017-06-18 11:12:45 -05001440 setup_special_tx(rtlpriv, ppsc, PACKET_ARP);
Larry Finger9afa2e42014-09-22 09:39:21 -05001441
1442 return true;
1443 } else if (ETH_P_PAE == ether_type) {
Ping-Ke Shih11f35c92017-07-02 13:12:30 -05001444 /* EAPOL is seens as in-4way */
1445 rtlpriv->btcoexist.btc_info.in_4way = true;
1446 rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
1447 rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
1448
Larry Finger9afa2e42014-09-22 09:39:21 -05001449 RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1450 "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
1451
1452 if (is_tx) {
1453 rtlpriv->ra.is_special_data = true;
Larry Fingerba9f93f2016-11-26 14:43:35 -06001454 rtl_lps_leave(hw);
Larry Finger9afa2e42014-09-22 09:39:21 -05001455 ppsc->last_delaylps_stamp_jiffies = jiffies;
Ping-Ke Shih5b757ba2017-06-18 11:12:45 -05001456
1457 setup_special_tx(rtlpriv, ppsc, PACKET_EAPOL);
Larry Finger9afa2e42014-09-22 09:39:21 -05001458 }
1459
1460 return true;
Larry Fingerc8f03452015-02-24 09:23:01 -06001461 } else if (ETH_P_IPV6 == ether_type) {
1462 /* TODO: Handle any IPv6 cases that need special handling.
1463 * For now, always return false
1464 */
1465 goto end;
Mark Cave-Ayland0c5d63f2013-11-02 14:28:35 -05001466 }
Larry Finger9afa2e42014-09-22 09:39:21 -05001467
1468end:
1469 rtlpriv->ra.is_special_data = false;
1470 return false;
Larry Finger0c817332010-12-08 11:12:31 -06001471}
Larry Finger6f334c22013-07-12 15:32:15 -05001472EXPORT_SYMBOL_GPL(rtl_is_special_data);
Larry Finger0c817332010-12-08 11:12:31 -06001473
Ping-Ke Shih84795802017-06-18 11:12:44 -05001474bool rtl_is_tx_report_skb(struct ieee80211_hw *hw, struct sk_buff *skb)
1475{
1476 u16 ether_type;
1477 const u8 *ether_type_ptr;
1478
1479 ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, true);
1480 ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1481
1482 /* EAPOL */
1483 if (ether_type == ETH_P_PAE)
1484 return true;
1485
1486 return false;
1487}
1488
1489static u16 rtl_get_tx_report_sn(struct ieee80211_hw *hw)
1490{
1491 struct rtl_priv *rtlpriv = rtl_priv(hw);
1492 struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1493 u16 sn;
1494
1495 sn = atomic_inc_return(&tx_report->sn) & 0x0FFF;
1496
1497 tx_report->last_sent_sn = sn;
1498 tx_report->last_sent_time = jiffies;
1499
1500 RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1501 "Send TX-Report sn=0x%X\n", sn);
1502
1503 return sn;
1504}
1505
1506void rtl_get_tx_report(struct rtl_tcb_desc *ptcb_desc, u8 *pdesc,
1507 struct ieee80211_hw *hw)
1508{
1509 if (ptcb_desc->use_spe_rpt) {
1510 u16 sn = rtl_get_tx_report_sn(hw);
1511
1512 SET_TX_DESC_SPE_RPT(pdesc, 1);
1513 SET_TX_DESC_SW_DEFINE(pdesc, sn);
1514 }
1515}
1516EXPORT_SYMBOL_GPL(rtl_get_tx_report);
1517
1518void rtl_tx_report_handler(struct ieee80211_hw *hw, u8 *tmp_buf, u8 c2h_cmd_len)
1519{
1520 struct rtl_priv *rtlpriv = rtl_priv(hw);
1521 struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1522 u16 sn;
1523
1524 sn = ((tmp_buf[7] & 0x0F) << 8) | tmp_buf[6];
1525
1526 tx_report->last_recv_sn = sn;
1527
1528 RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1529 "Recv TX-Report st=0x%02X sn=0x%X retry=0x%X\n",
1530 tmp_buf[0], sn, tmp_buf[2]);
1531}
1532EXPORT_SYMBOL_GPL(rtl_tx_report_handler);
1533
1534bool rtl_check_tx_report_acked(struct ieee80211_hw *hw)
1535{
1536 struct rtl_priv *rtlpriv = rtl_priv(hw);
1537 struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1538
1539 if (tx_report->last_sent_sn == tx_report->last_recv_sn)
1540 return true;
1541
1542 if (time_before(tx_report->last_sent_time + 3 * HZ, jiffies)) {
1543 RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_WARNING,
1544 "Check TX-Report timeout!!\n");
1545 return true; /* 3 sec. (timeout) seen as acked */
1546 }
1547
1548 return false;
1549}
1550
1551void rtl_wait_tx_report_acked(struct ieee80211_hw *hw, u32 wait_ms)
1552{
1553 struct rtl_priv *rtlpriv = rtl_priv(hw);
1554 int i;
1555
1556 for (i = 0; i < wait_ms; i++) {
1557 if (rtl_check_tx_report_acked(hw))
1558 break;
1559 usleep_range(1000, 2000);
1560 RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1561 "Wait 1ms (%d/%d) to disable key.\n", i, wait_ms);
1562 }
1563}
Ping-Ke Shihc1816f12017-09-29 14:47:55 -05001564
1565u32 rtl_get_hal_edca_param(struct ieee80211_hw *hw,
1566 struct ieee80211_vif *vif,
1567 enum wireless_mode wirelessmode,
1568 struct ieee80211_tx_queue_params *param)
1569{
1570 u32 reg = 0;
1571 u8 sifstime = 10;
1572 u8 slottime = 20;
1573
1574 /* AIFS = AIFSN * slot time + SIFS */
1575 switch (wirelessmode) {
1576 case WIRELESS_MODE_A:
1577 case WIRELESS_MODE_N_24G:
1578 case WIRELESS_MODE_N_5G:
1579 case WIRELESS_MODE_AC_5G:
1580 case WIRELESS_MODE_AC_24G:
1581 sifstime = 16;
1582 slottime = 9;
1583 break;
1584 case WIRELESS_MODE_G:
1585 slottime = (vif->bss_conf.use_short_slot ? 9 : 20);
1586 break;
1587 default:
1588 break;
1589 }
1590
1591 reg |= (param->txop & 0x7FF) << 16;
1592 reg |= (fls(param->cw_max) & 0xF) << 12;
1593 reg |= (fls(param->cw_min) & 0xF) << 8;
1594 reg |= (param->aifs & 0x0F) * slottime + sifstime;
1595
1596 return reg;
1597}
1598EXPORT_SYMBOL_GPL(rtl_get_hal_edca_param);
1599
Larry Finger0c817332010-12-08 11:12:31 -06001600/*********************************************************
1601 *
1602 * functions called by core.c
1603 *
1604 *********************************************************/
Larry Fingerf7953b22014-09-22 09:39:20 -05001605int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1606 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
Larry Finger0c817332010-12-08 11:12:31 -06001607{
1608 struct rtl_priv *rtlpriv = rtl_priv(hw);
1609 struct rtl_tid_data *tid_data;
Chaoming_Liacd48572011-04-25 12:52:54 -05001610 struct rtl_sta_info *sta_entry = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001611
Chaoming_Liacd48572011-04-25 12:52:54 -05001612 if (sta == NULL)
1613 return -EINVAL;
Larry Finger0c817332010-12-08 11:12:31 -06001614
1615 if (unlikely(tid >= MAX_TID_COUNT))
1616 return -EINVAL;
1617
Chaoming_Liacd48572011-04-25 12:52:54 -05001618 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1619 if (!sta_entry)
Larry Finger0c817332010-12-08 11:12:31 -06001620 return -ENXIO;
Chaoming_Liacd48572011-04-25 12:52:54 -05001621 tid_data = &sta_entry->tids[tid];
Larry Finger0c817332010-12-08 11:12:31 -06001622
Larry Finger9afa2e42014-09-22 09:39:21 -05001623 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1624 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
Ping-Ke Shih1d1aa8f2017-10-24 10:03:31 +08001625 *ssn);
Larry Finger0c817332010-12-08 11:12:31 -06001626
Chaoming_Liacd48572011-04-25 12:52:54 -05001627 tid_data->agg.agg_state = RTL_AGG_START;
1628
Larry Finger9afa2e42014-09-22 09:39:21 -05001629 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Larry Finger0c817332010-12-08 11:12:31 -06001630 return 0;
1631}
1632
Larry Fingerf7953b22014-09-22 09:39:20 -05001633int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1634 struct ieee80211_sta *sta, u16 tid)
Larry Finger0c817332010-12-08 11:12:31 -06001635{
Larry Finger0c817332010-12-08 11:12:31 -06001636 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Liacd48572011-04-25 12:52:54 -05001637 struct rtl_sta_info *sta_entry = NULL;
Larry Finger0c817332010-12-08 11:12:31 -06001638
Chaoming_Liacd48572011-04-25 12:52:54 -05001639 if (sta == NULL)
1640 return -EINVAL;
1641
Larry Finger9afa2e42014-09-22 09:39:21 -05001642 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1643 "on ra = %pM tid = %d\n", sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001644
Larry Finger0c817332010-12-08 11:12:31 -06001645 if (unlikely(tid >= MAX_TID_COUNT))
1646 return -EINVAL;
1647
Chaoming_Liacd48572011-04-25 12:52:54 -05001648 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
Chaoming_Liacd48572011-04-25 12:52:54 -05001649 sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
Larry Finger0c817332010-12-08 11:12:31 -06001650
Larry Finger9afa2e42014-09-22 09:39:21 -05001651 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001652 return 0;
1653}
Larry Finger0c817332010-12-08 11:12:31 -06001654
Larry Finger26634c42013-03-24 22:06:33 -05001655int rtl_rx_agg_start(struct ieee80211_hw *hw,
1656 struct ieee80211_sta *sta, u16 tid)
1657{
1658 struct rtl_priv *rtlpriv = rtl_priv(hw);
1659 struct rtl_tid_data *tid_data;
1660 struct rtl_sta_info *sta_entry = NULL;
Ping-Ke Shih26356642017-06-18 11:12:47 -05001661 u8 reject_agg;
Larry Finger26634c42013-03-24 22:06:33 -05001662
1663 if (sta == NULL)
1664 return -EINVAL;
1665
1666 if (unlikely(tid >= MAX_TID_COUNT))
1667 return -EINVAL;
1668
Ping-Ke Shih26356642017-06-18 11:12:47 -05001669 if (rtlpriv->cfg->ops->get_btc_status()) {
1670 rtlpriv->btcoexist.btc_ops->btc_get_ampdu_cfg(rtlpriv,
1671 &reject_agg,
1672 NULL, NULL);
1673 if (reject_agg)
1674 return -EINVAL;
1675 }
1676
Larry Finger26634c42013-03-24 22:06:33 -05001677 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1678 if (!sta_entry)
1679 return -ENXIO;
1680 tid_data = &sta_entry->tids[tid];
1681
1682 RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
Ping-Ke Shih1d1aa8f2017-10-24 10:03:31 +08001683 "on ra = %pM tid = %d\n", sta->addr, tid);
Larry Finger26634c42013-03-24 22:06:33 -05001684
1685 tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1686 return 0;
1687}
1688
1689int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1690 struct ieee80211_sta *sta, u16 tid)
1691{
1692 struct rtl_priv *rtlpriv = rtl_priv(hw);
1693 struct rtl_sta_info *sta_entry = NULL;
1694
1695 if (sta == NULL)
1696 return -EINVAL;
1697
Larry Finger26634c42013-03-24 22:06:33 -05001698 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1699 "on ra = %pM tid = %d\n", sta->addr, tid);
1700
1701 if (unlikely(tid >= MAX_TID_COUNT))
1702 return -EINVAL;
1703
1704 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1705 sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1706
1707 return 0;
1708}
Chaoming_Liacd48572011-04-25 12:52:54 -05001709int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1710 struct ieee80211_sta *sta, u16 tid)
1711{
1712 struct rtl_priv *rtlpriv = rtl_priv(hw);
Chaoming_Liacd48572011-04-25 12:52:54 -05001713 struct rtl_sta_info *sta_entry = NULL;
1714
1715 if (sta == NULL)
1716 return -EINVAL;
1717
Larry Finger9afa2e42014-09-22 09:39:21 -05001718 RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1719 "on ra = %pM tid = %d\n", sta->addr, tid);
Chaoming_Liacd48572011-04-25 12:52:54 -05001720
1721 if (unlikely(tid >= MAX_TID_COUNT))
1722 return -EINVAL;
1723
1724 sta_entry = (struct rtl_sta_info *)sta->drv_priv;
Chaoming_Liacd48572011-04-25 12:52:54 -05001725 sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
Larry Finger0c817332010-12-08 11:12:31 -06001726
1727 return 0;
1728}
1729
Ping-Ke Shih26356642017-06-18 11:12:47 -05001730void rtl_rx_ampdu_apply(struct rtl_priv *rtlpriv)
1731{
1732 struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
Nicolas Ioosse4779162017-12-10 20:51:59 +01001733 u8 reject_agg = 0, ctrl_agg_size = 0, agg_size = 0;
Ping-Ke Shih26356642017-06-18 11:12:47 -05001734
1735 if (rtlpriv->cfg->ops->get_btc_status())
1736 btc_ops->btc_get_ampdu_cfg(rtlpriv, &reject_agg,
1737 &ctrl_agg_size, &agg_size);
1738
1739 RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
1740 "Set RX AMPDU: coex - reject=%d, ctrl_agg_size=%d, size=%d",
1741 reject_agg, ctrl_agg_size, agg_size);
1742
1743 rtlpriv->hw->max_rx_aggregation_subframes =
1744 (ctrl_agg_size ? agg_size : IEEE80211_MAX_AMPDU_BUF);
1745}
1746EXPORT_SYMBOL(rtl_rx_ampdu_apply);
1747
Larry Finger0c817332010-12-08 11:12:31 -06001748/*********************************************************
1749 *
1750 * wq & timer callback functions
1751 *
1752 *********************************************************/
Larry Finger26634c42013-03-24 22:06:33 -05001753/* this function is used for roaming */
1754void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1755{
1756 struct rtl_priv *rtlpriv = rtl_priv(hw);
1757 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1758
1759 if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1760 return;
1761
1762 if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1763 return;
1764
1765 /* check if this really is a beacon */
1766 if (!ieee80211_is_beacon(hdr->frame_control) &&
1767 !ieee80211_is_probe_resp(hdr->frame_control))
1768 return;
1769
1770 /* min. beacon length + FCS_LEN */
1771 if (skb->len <= 40 + FCS_LEN)
1772 return;
1773
1774 /* and only beacons from the associated BSSID, please */
Larry Finger9afa2e42014-09-22 09:39:21 -05001775 if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
Larry Finger26634c42013-03-24 22:06:33 -05001776 return;
1777
1778 rtlpriv->link_info.bcn_rx_inperiod++;
1779}
Larry Finger6f334c22013-07-12 15:32:15 -05001780EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
Larry Finger26634c42013-03-24 22:06:33 -05001781
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -05001782static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw)
1783{
1784 struct rtl_priv *rtlpriv = rtl_priv(hw);
1785 struct rtl_bssid_entry *entry, *next;
1786
1787 list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1788 list_del(&entry->list);
1789 kfree(entry);
1790 rtlpriv->scan_list.num--;
1791 }
1792}
1793
1794void rtl_scan_list_expire(struct ieee80211_hw *hw)
1795{
1796 struct rtl_priv *rtlpriv = rtl_priv(hw);
1797 struct rtl_bssid_entry *entry, *next;
1798 unsigned long flags;
1799
1800 spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
1801
1802 list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1803 /* 180 seconds */
1804 if (jiffies_to_msecs(jiffies - entry->age) < 180000)
1805 continue;
1806
1807 list_del(&entry->list);
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -05001808 rtlpriv->scan_list.num--;
1809
1810 RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1811 "BSSID=%pM is expire in scan list (total=%d)\n",
1812 entry->bssid, rtlpriv->scan_list.num);
Colin Ian Kingd0116f62017-06-30 11:08:43 +01001813 kfree(entry);
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -05001814 }
1815
1816 spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
1817
1818 rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1819}
1820
1821void rtl_collect_scan_list(struct ieee80211_hw *hw, struct sk_buff *skb)
1822{
1823 struct rtl_priv *rtlpriv = rtl_priv(hw);
1824 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1825 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1826 unsigned long flags;
1827
1828 struct rtl_bssid_entry *entry;
1829 bool entry_found = false;
1830
1831 /* check if it is scanning */
1832 if (!mac->act_scanning)
1833 return;
1834
1835 /* check if this really is a beacon */
1836 if (!ieee80211_is_beacon(hdr->frame_control) &&
1837 !ieee80211_is_probe_resp(hdr->frame_control))
1838 return;
1839
1840 spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
1841
1842 list_for_each_entry(entry, &rtlpriv->scan_list.list, list) {
1843 if (memcmp(entry->bssid, hdr->addr3, ETH_ALEN) == 0) {
1844 list_del_init(&entry->list);
1845 entry_found = true;
1846 RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1847 "Update BSSID=%pM to scan list (total=%d)\n",
1848 hdr->addr3, rtlpriv->scan_list.num);
1849 break;
1850 }
1851 }
1852
1853 if (!entry_found) {
1854 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1855
1856 if (!entry)
1857 goto label_err;
1858
1859 memcpy(entry->bssid, hdr->addr3, ETH_ALEN);
1860 rtlpriv->scan_list.num++;
1861
1862 RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1863 "Add BSSID=%pM to scan list (total=%d)\n",
1864 hdr->addr3, rtlpriv->scan_list.num);
1865 }
1866
1867 entry->age = jiffies;
1868
1869 list_add_tail(&entry->list, &rtlpriv->scan_list.list);
1870
1871label_err:
1872 spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
1873}
1874EXPORT_SYMBOL(rtl_collect_scan_list);
1875
Larry Finger0c817332010-12-08 11:12:31 -06001876void rtl_watchdog_wq_callback(void *data)
1877{
1878 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1879 struct rtl_works,
1880 watchdog_wq);
1881 struct ieee80211_hw *hw = rtlworks->hw;
1882 struct rtl_priv *rtlpriv = rtl_priv(hw);
1883 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1884 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Larry Finger7ea47242011-02-19 16:28:57 -06001885 bool busytraffic = false;
Larry Finger26634c42013-03-24 22:06:33 -05001886 bool tx_busy_traffic = false;
1887 bool rx_busy_traffic = false;
Larry Finger7ea47242011-02-19 16:28:57 -06001888 bool higher_busytraffic = false;
1889 bool higher_busyrxtraffic = false;
Chaoming_Liacd48572011-04-25 12:52:54 -05001890 u8 idx, tid;
Larry Finger0c817332010-12-08 11:12:31 -06001891 u32 rx_cnt_inp4eriod = 0;
1892 u32 tx_cnt_inp4eriod = 0;
1893 u32 aver_rx_cnt_inperiod = 0;
1894 u32 aver_tx_cnt_inperiod = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001895 u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1896 u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
Larry Finger0c817332010-12-08 11:12:31 -06001897
1898 if (is_hal_stop(rtlhal))
1899 return;
1900
1901 /* <1> Determine if action frame is allowed */
1902 if (mac->link_state > MAC80211_NOLINK) {
1903 if (mac->cnt_after_linked < 20)
1904 mac->cnt_after_linked++;
1905 } else {
1906 mac->cnt_after_linked = 0;
1907 }
1908
Larry Finger9afa2e42014-09-22 09:39:21 -05001909 /* <2> to check if traffic busy, if
Larry Finger0c817332010-12-08 11:12:31 -06001910 * busytraffic we don't change channel
1911 */
1912 if (mac->link_state >= MAC80211_LINKED) {
1913
1914 /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
1915 for (idx = 0; idx <= 2; idx++) {
1916 rtlpriv->link_info.num_rx_in4period[idx] =
1917 rtlpriv->link_info.num_rx_in4period[idx + 1];
1918 rtlpriv->link_info.num_tx_in4period[idx] =
1919 rtlpriv->link_info.num_tx_in4period[idx + 1];
1920 }
1921 rtlpriv->link_info.num_rx_in4period[3] =
1922 rtlpriv->link_info.num_rx_inperiod;
1923 rtlpriv->link_info.num_tx_in4period[3] =
1924 rtlpriv->link_info.num_tx_inperiod;
1925 for (idx = 0; idx <= 3; idx++) {
1926 rx_cnt_inp4eriod +=
1927 rtlpriv->link_info.num_rx_in4period[idx];
1928 tx_cnt_inp4eriod +=
1929 rtlpriv->link_info.num_tx_in4period[idx];
1930 }
1931 aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
1932 aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
1933
1934 /* (2) check traffic busy */
Larry Finger26634c42013-03-24 22:06:33 -05001935 if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
Larry Finger7ea47242011-02-19 16:28:57 -06001936 busytraffic = true;
Larry Finger26634c42013-03-24 22:06:33 -05001937 if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
1938 rx_busy_traffic = true;
1939 else
1940 tx_busy_traffic = false;
1941 }
Larry Finger0c817332010-12-08 11:12:31 -06001942
1943 /* Higher Tx/Rx data. */
1944 if (aver_rx_cnt_inperiod > 4000 ||
1945 aver_tx_cnt_inperiod > 4000) {
Larry Finger7ea47242011-02-19 16:28:57 -06001946 higher_busytraffic = true;
Larry Finger0c817332010-12-08 11:12:31 -06001947
1948 /* Extremely high Rx data. */
1949 if (aver_rx_cnt_inperiod > 5000)
Larry Finger7ea47242011-02-19 16:28:57 -06001950 higher_busyrxtraffic = true;
Chaoming_Liacd48572011-04-25 12:52:54 -05001951 }
1952
1953 /* check every tid's tx traffic */
1954 for (tid = 0; tid <= 7; tid++) {
1955 for (idx = 0; idx <= 2; idx++)
1956 rtlpriv->link_info.tidtx_in4period[tid][idx] =
Larry Finger9afa2e42014-09-22 09:39:21 -05001957 rtlpriv->link_info.tidtx_in4period[tid]
1958 [idx + 1];
Chaoming_Liacd48572011-04-25 12:52:54 -05001959 rtlpriv->link_info.tidtx_in4period[tid][3] =
1960 rtlpriv->link_info.tidtx_inperiod[tid];
1961
1962 for (idx = 0; idx <= 3; idx++)
1963 tidtx_inp4eriod[tid] +=
Larry Finger9afa2e42014-09-22 09:39:21 -05001964 rtlpriv->link_info.tidtx_in4period[tid][idx];
Chaoming_Liacd48572011-04-25 12:52:54 -05001965 aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
1966 if (aver_tidtx_inperiod[tid] > 5000)
1967 rtlpriv->link_info.higher_busytxtraffic[tid] =
Larry Finger9afa2e42014-09-22 09:39:21 -05001968 true;
Larry Finger0c817332010-12-08 11:12:31 -06001969 else
Chaoming_Liacd48572011-04-25 12:52:54 -05001970 rtlpriv->link_info.higher_busytxtraffic[tid] =
Larry Finger9afa2e42014-09-22 09:39:21 -05001971 false;
Larry Finger0c817332010-12-08 11:12:31 -06001972 }
1973
Ping-Ke Shih42213f22017-06-18 11:12:49 -05001974 /* PS is controlled by coex. */
1975 if (rtlpriv->cfg->ops->get_btc_status() &&
1976 rtlpriv->btcoexist.btc_ops->btc_is_bt_ctrl_lps(rtlpriv))
1977 goto label_lps_done;
1978
Frank A. Cancio Bellod63589c2017-12-29 16:31:08 +08001979 if (rtlpriv->link_info.num_rx_inperiod +
1980 rtlpriv->link_info.num_tx_inperiod > 8 ||
1981 rtlpriv->link_info.num_rx_inperiod > 2)
Larry Fingerfd09ff92014-09-22 09:39:18 -05001982 rtl_lps_leave(hw);
wang yanqing873ffe12016-05-03 00:38:36 +08001983 else
1984 rtl_lps_enter(hw);
Ping-Ke Shih42213f22017-06-18 11:12:49 -05001985
1986label_lps_done:
1987 ;
Larry Finger0c817332010-12-08 11:12:31 -06001988 }
1989
1990 rtlpriv->link_info.num_rx_inperiod = 0;
1991 rtlpriv->link_info.num_tx_inperiod = 0;
Chaoming_Liacd48572011-04-25 12:52:54 -05001992 for (tid = 0; tid <= 7; tid++)
1993 rtlpriv->link_info.tidtx_inperiod[tid] = 0;
Larry Finger0c817332010-12-08 11:12:31 -06001994
Larry Finger7ea47242011-02-19 16:28:57 -06001995 rtlpriv->link_info.busytraffic = busytraffic;
1996 rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
Larry Finger26634c42013-03-24 22:06:33 -05001997 rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
1998 rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
Larry Finger7ea47242011-02-19 16:28:57 -06001999 rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
Larry Finger0c817332010-12-08 11:12:31 -06002000
Ping-Ke Shih74451b92017-09-29 14:47:56 -05002001 rtlpriv->stats.txbytesunicast_inperiod =
2002 rtlpriv->stats.txbytesunicast -
2003 rtlpriv->stats.txbytesunicast_last;
2004 rtlpriv->stats.rxbytesunicast_inperiod =
2005 rtlpriv->stats.rxbytesunicast -
2006 rtlpriv->stats.rxbytesunicast_last;
2007 rtlpriv->stats.txbytesunicast_last = rtlpriv->stats.txbytesunicast;
2008 rtlpriv->stats.rxbytesunicast_last = rtlpriv->stats.rxbytesunicast;
2009
2010 rtlpriv->stats.txbytesunicast_inperiod_tp =
2011 (u32)(rtlpriv->stats.txbytesunicast_inperiod * 8 / 2 /
2012 1024 / 1024);
2013 rtlpriv->stats.rxbytesunicast_inperiod_tp =
2014 (u32)(rtlpriv->stats.rxbytesunicast_inperiod * 8 / 2 /
2015 1024 / 1024);
2016
Chaoming_Liacd48572011-04-25 12:52:54 -05002017 /* <3> DM */
Larry Finger9afa2e42014-09-22 09:39:21 -05002018 if (!rtlpriv->cfg->mod_params->disable_watchdog)
2019 rtlpriv->cfg->ops->dm_watchdog(hw);
Larry Finger26634c42013-03-24 22:06:33 -05002020
2021 /* <4> roaming */
2022 if (mac->link_state == MAC80211_LINKED &&
2023 mac->opmode == NL80211_IFTYPE_STATION) {
2024 if ((rtlpriv->link_info.bcn_rx_inperiod +
Larry Finger9afa2e42014-09-22 09:39:21 -05002025 rtlpriv->link_info.num_rx_inperiod) == 0) {
Larry Finger26634c42013-03-24 22:06:33 -05002026 rtlpriv->link_info.roam_times++;
2027 RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
2028 "AP off for %d s\n",
Larry Finger9afa2e42014-09-22 09:39:21 -05002029 (rtlpriv->link_info.roam_times * 2));
Larry Finger26634c42013-03-24 22:06:33 -05002030
Larry Finger9afa2e42014-09-22 09:39:21 -05002031 /* if we can't recv beacon for 10s,
2032 * we should reconnect this AP
Larry Finger26634c42013-03-24 22:06:33 -05002033 */
Larry Finger9afa2e42014-09-22 09:39:21 -05002034 if (rtlpriv->link_info.roam_times >= 5) {
Larry Fingerb03d9682016-12-15 12:22:58 -06002035 pr_err("AP off, try to reconnect now\n");
Larry Finger26634c42013-03-24 22:06:33 -05002036 rtlpriv->link_info.roam_times = 0;
Larry Finger9afa2e42014-09-22 09:39:21 -05002037 ieee80211_connection_loss(
2038 rtlpriv->mac80211.vif);
Larry Finger26634c42013-03-24 22:06:33 -05002039 }
2040 } else {
2041 rtlpriv->link_info.roam_times = 0;
2042 }
2043 }
Larry Finger9afa2e42014-09-22 09:39:21 -05002044
2045 if (rtlpriv->cfg->ops->get_btc_status())
2046 rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
2047
Ping-Ke Shih11f35c92017-07-02 13:12:30 -05002048 if (rtlpriv->btcoexist.btc_info.in_4way) {
2049 if (time_after(jiffies, rtlpriv->btcoexist.btc_info.in_4way_ts +
2050 msecs_to_jiffies(IN_4WAY_TIMEOUT_TIME)))
2051 rtlpriv->btcoexist.btc_info.in_4way = false;
2052 }
2053
Larry Finger26634c42013-03-24 22:06:33 -05002054 rtlpriv->link_info.bcn_rx_inperiod = 0;
Ping-Ke Shihc76ab8e2017-06-21 12:15:37 -05002055
2056 /* <6> scan list */
2057 rtl_scan_list_expire(hw);
Larry Finger0c817332010-12-08 11:12:31 -06002058}
2059
Kees Cook7c51d17c2017-10-24 02:28:29 -07002060void rtl_watch_dog_timer_callback(struct timer_list *t)
Larry Finger0c817332010-12-08 11:12:31 -06002061{
Kees Cook7c51d17c2017-10-24 02:28:29 -07002062 struct rtl_priv *rtlpriv = from_timer(rtlpriv, t, works.watchdog_timer);
Larry Finger0c817332010-12-08 11:12:31 -06002063
2064 queue_delayed_work(rtlpriv->works.rtl_wq,
2065 &rtlpriv->works.watchdog_wq, 0);
2066
2067 mod_timer(&rtlpriv->works.watchdog_timer,
2068 jiffies + MSECS(RTL_WATCH_DOG_TIME));
2069}
Larry Finger26634c42013-03-24 22:06:33 -05002070void rtl_fwevt_wq_callback(void *data)
2071{
2072 struct rtl_works *rtlworks =
2073 container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
2074 struct ieee80211_hw *hw = rtlworks->hw;
2075 struct rtl_priv *rtlpriv = rtl_priv(hw);
2076
2077 rtlpriv->cfg->ops->c2h_command_handle(hw);
2078}
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -06002079
2080void rtl_c2hcmd_enqueue(struct ieee80211_hw *hw, u8 tag, u8 len, u8 *val)
2081{
2082 struct rtl_priv *rtlpriv = rtl_priv(hw);
2083 unsigned long flags;
2084 struct rtl_c2hcmd *c2hcmd;
2085
Larry Finger893dc682017-03-21 09:24:11 -05002086 c2hcmd = kmalloc(sizeof(*c2hcmd),
2087 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -06002088
2089 if (!c2hcmd)
2090 goto label_err;
2091
Larry Finger893dc682017-03-21 09:24:11 -05002092 c2hcmd->val = kmalloc(len,
2093 in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
Ping-Ke Shihcceb0a52017-02-06 21:30:08 -06002094
2095 if (!c2hcmd->val)
2096 goto label_err2;
2097
2098 /* fill data */
2099 c2hcmd->tag = tag;
2100 c2hcmd->len = len;
2101 memcpy(c2hcmd->val, val, len);
2102
2103 /* enqueue */
2104 spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
2105
2106 list_add_tail(&c2hcmd->list, &rtlpriv->c2hcmd_list);
2107
2108 spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
2109
2110 /* wake up wq */
2111 queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.c2hcmd_wq, 0);
2112
2113 return;
2114
2115label_err2:
2116 kfree(c2hcmd);
2117
2118label_err:
2119 RT_TRACE(rtlpriv, COMP_CMD, DBG_WARNING,
2120 "C2H cmd enqueue fail.\n");
2121}
2122EXPORT_SYMBOL(rtl_c2hcmd_enqueue);
2123
2124void rtl_c2hcmd_launcher(struct ieee80211_hw *hw, int exec)
2125{
2126 struct rtl_priv *rtlpriv = rtl_priv(hw);
2127 unsigned long flags;
2128 struct rtl_c2hcmd *c2hcmd;
2129 int i;
2130
2131 for (i = 0; i < 200; i++) {
2132 /* dequeue a task */
2133 spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
2134
2135 c2hcmd = list_first_entry_or_null(&rtlpriv->c2hcmd_list,
2136 struct rtl_c2hcmd, list);
2137
2138 if (c2hcmd)
2139 list_del(&c2hcmd->list);
2140
2141 spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
2142
2143 /* do it */
2144 if (!c2hcmd)
2145 break;
2146
2147 if (rtlpriv->cfg->ops->c2h_content_parsing && exec)
2148 rtlpriv->cfg->ops->c2h_content_parsing(hw,
2149 c2hcmd->tag, c2hcmd->len, c2hcmd->val);
2150
2151 /* free */
2152 kfree(c2hcmd->val);
2153
2154 kfree(c2hcmd);
2155 }
2156}
2157
2158void rtl_c2hcmd_wq_callback(void *data)
2159{
2160 struct rtl_works *rtlworks = container_of_dwork_rtl(data,
2161 struct rtl_works,
2162 c2hcmd_wq);
2163 struct ieee80211_hw *hw = rtlworks->hw;
2164
2165 rtl_c2hcmd_launcher(hw, 1);
2166}
2167
Kees Cook7c51d17c2017-10-24 02:28:29 -07002168void rtl_easy_concurrent_retrytimer_callback(struct timer_list *t)
Larry Finger26634c42013-03-24 22:06:33 -05002169{
Kees Cook7c51d17c2017-10-24 02:28:29 -07002170 struct rtl_priv *rtlpriv =
2171 from_timer(rtlpriv, t, works.dualmac_easyconcurrent_retrytimer);
2172 struct ieee80211_hw *hw = rtlpriv->hw;
Larry Finger26634c42013-03-24 22:06:33 -05002173 struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
2174
2175 if (buddy_priv == NULL)
2176 return;
2177
2178 rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
2179}
Larry Finger0c817332010-12-08 11:12:31 -06002180/*********************************************************
2181 *
Chaoming_Liacd48572011-04-25 12:52:54 -05002182 * frame process functions
2183 *
2184 *********************************************************/
2185u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
2186{
2187 struct ieee80211_mgmt *mgmt = (void *)data;
2188 u8 *pos, *end;
2189
2190 pos = (u8 *)mgmt->u.beacon.variable;
2191 end = data + len;
2192 while (pos < end) {
2193 if (pos + 2 + pos[1] > end)
2194 return NULL;
2195
2196 if (pos[0] == ie)
2197 return pos;
2198
2199 pos += 2 + pos[1];
2200 }
2201 return NULL;
2202}
2203
2204/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
2205/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
Larry Fingerd3bb1422011-04-25 13:23:20 -05002206static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
Larry Finger9afa2e42014-09-22 09:39:21 -05002207 enum ieee80211_smps_mode smps,
2208 u8 *da, u8 *bssid)
Chaoming_Liacd48572011-04-25 12:52:54 -05002209{
2210 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2211 struct sk_buff *skb;
2212 struct ieee80211_mgmt *action_frame;
2213
2214 /* 27 = header + category + action + smps mode */
2215 skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
2216 if (!skb)
2217 return NULL;
2218
2219 skb_reserve(skb, hw->extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02002220 action_frame = skb_put_zero(skb, 27);
Chaoming_Liacd48572011-04-25 12:52:54 -05002221 memcpy(action_frame->da, da, ETH_ALEN);
2222 memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
2223 memcpy(action_frame->bssid, bssid, ETH_ALEN);
2224 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2225 IEEE80211_STYPE_ACTION);
2226 action_frame->u.action.category = WLAN_CATEGORY_HT;
2227 action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
2228 switch (smps) {
2229 case IEEE80211_SMPS_AUTOMATIC:/* 0 */
2230 case IEEE80211_SMPS_NUM_MODES:/* 4 */
2231 WARN_ON(1);
Gustavo A. R. Silva032cf9a2017-12-29 16:31:07 +08002232 /* fall through */
Chaoming_Liacd48572011-04-25 12:52:54 -05002233 case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
2234 action_frame->u.action.u.ht_smps.smps_control =
2235 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
2236 break;
2237 case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
2238 action_frame->u.action.u.ht_smps.smps_control =
2239 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
2240 break;
2241 case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
2242 action_frame->u.action.u.ht_smps.smps_control =
2243 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
2244 break;
2245 }
2246
2247 return skb;
2248}
2249
2250int rtl_send_smps_action(struct ieee80211_hw *hw,
Larry Finger9afa2e42014-09-22 09:39:21 -05002251 struct ieee80211_sta *sta,
2252 enum ieee80211_smps_mode smps)
Chaoming_Liacd48572011-04-25 12:52:54 -05002253{
2254 struct rtl_priv *rtlpriv = rtl_priv(hw);
2255 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2256 struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
Larry Finger26634c42013-03-24 22:06:33 -05002257 struct sk_buff *skb = NULL;
Chaoming_Liacd48572011-04-25 12:52:54 -05002258 struct rtl_tcb_desc tcb_desc;
Larry Finger26634c42013-03-24 22:06:33 -05002259 u8 bssid[ETH_ALEN] = {0};
2260
Chaoming_Liacd48572011-04-25 12:52:54 -05002261 memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
2262
2263 if (rtlpriv->mac80211.act_scanning)
2264 goto err_free;
2265
2266 if (!sta)
2267 goto err_free;
2268
2269 if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
2270 goto err_free;
2271
2272 if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
2273 goto err_free;
2274
Larry Finger26634c42013-03-24 22:06:33 -05002275 if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
2276 memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
2277 else
2278 memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
2279
2280 skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
Chaoming_Liacd48572011-04-25 12:52:54 -05002281 /* this is a type = mgmt * stype = action frame */
2282 if (skb) {
2283 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2284 struct rtl_sta_info *sta_entry =
2285 (struct rtl_sta_info *) sta->drv_priv;
2286 sta_entry->mimo_ps = smps;
Ping-Ke Shih1d22b172017-09-29 14:47:59 -05002287 /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true); */
Chaoming_Liacd48572011-04-25 12:52:54 -05002288
2289 info->control.rates[0].idx = 0;
Karl Beldan675a0b02013-03-25 16:26:57 +01002290 info->band = hw->conf.chandef.chan->band;
Thomas Huehn36323f82012-07-23 21:33:42 +02002291 rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
Chaoming_Liacd48572011-04-25 12:52:54 -05002292 }
Larry Finger26634c42013-03-24 22:06:33 -05002293 return 1;
2294
Chaoming_Liacd48572011-04-25 12:52:54 -05002295err_free:
2296 return 0;
2297}
Larry Finger26634c42013-03-24 22:06:33 -05002298EXPORT_SYMBOL(rtl_send_smps_action);
2299
Larry Finger4f4826d2013-09-26 13:25:27 -05002300void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
2301{
2302 struct rtl_priv *rtlpriv = rtl_priv(hw);
2303 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2304 enum io_type iotype;
2305
2306 if (!is_hal_stop(rtlhal)) {
2307 switch (operation) {
2308 case SCAN_OPT_BACKUP:
2309 iotype = IO_CMD_PAUSE_DM_BY_SCAN;
2310 rtlpriv->cfg->ops->set_hw_reg(hw,
2311 HW_VAR_IO_CMD,
2312 (u8 *)&iotype);
2313 break;
2314 case SCAN_OPT_RESTORE:
2315 iotype = IO_CMD_RESUME_DM_BY_SCAN;
2316 rtlpriv->cfg->ops->set_hw_reg(hw,
2317 HW_VAR_IO_CMD,
2318 (u8 *)&iotype);
2319 break;
2320 default:
Larry Fingerb03d9682016-12-15 12:22:58 -06002321 pr_err("Unknown Scan Backup operation.\n");
Larry Finger4f4826d2013-09-26 13:25:27 -05002322 break;
2323 }
2324 }
2325}
2326EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
2327
Larry Finger9afa2e42014-09-22 09:39:21 -05002328/* because mac80211 have issues when can receive del ba
2329 * so here we just make a fake del_ba if we receive a ba_req
2330 * but rx_agg was opened to let mac80211 release some ba
2331 * related resources, so please this del_ba for tx
Larry Finger26634c42013-03-24 22:06:33 -05002332 */
2333struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
2334 u8 *sa, u8 *bssid, u16 tid)
2335{
2336 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2337 struct sk_buff *skb;
2338 struct ieee80211_mgmt *action_frame;
2339 u16 params;
2340
2341 /* 27 = header + category + action + smps mode */
2342 skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
2343 if (!skb)
2344 return NULL;
2345
2346 skb_reserve(skb, hw->extra_tx_headroom);
Johannes Bergb080db52017-06-16 14:29:19 +02002347 action_frame = skb_put_zero(skb, 34);
Larry Finger26634c42013-03-24 22:06:33 -05002348 memcpy(action_frame->sa, sa, ETH_ALEN);
2349 memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
2350 memcpy(action_frame->bssid, bssid, ETH_ALEN);
2351 action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2352 IEEE80211_STYPE_ACTION);
2353 action_frame->u.action.category = WLAN_CATEGORY_BACK;
2354 action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
2355 params = (u16)(1 << 11); /* bit 11 initiator */
Larry Finger9afa2e42014-09-22 09:39:21 -05002356 params |= (u16)(tid << 12); /* bit 15:12 TID number */
Larry Finger26634c42013-03-24 22:06:33 -05002357
2358 action_frame->u.action.u.delba.params = cpu_to_le16(params);
2359 action_frame->u.action.u.delba.reason_code =
2360 cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
2361
2362 return skb;
2363}
Chaoming_Liacd48572011-04-25 12:52:54 -05002364
2365/*********************************************************
2366 *
2367 * IOT functions
2368 *
2369 *********************************************************/
2370static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
Larry Finger9afa2e42014-09-22 09:39:21 -05002371 struct octet_string vendor_ie)
Chaoming_Liacd48572011-04-25 12:52:54 -05002372{
2373 struct rtl_priv *rtlpriv = rtl_priv(hw);
2374 bool matched = false;
2375 static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
2376 static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
2377 static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
2378 static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
2379 static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
2380 static u8 racap[] = { 0x00, 0x0c, 0x43 };
2381 static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
2382 static u8 marvcap[] = { 0x00, 0x50, 0x43 };
2383
2384 if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
2385 memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
2386 rtlpriv->mac80211.vendor = PEER_ATH;
2387 matched = true;
2388 } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
2389 memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
2390 memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
2391 rtlpriv->mac80211.vendor = PEER_BROAD;
2392 matched = true;
2393 } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
2394 rtlpriv->mac80211.vendor = PEER_RAL;
2395 matched = true;
2396 } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
2397 rtlpriv->mac80211.vendor = PEER_CISCO;
2398 matched = true;
2399 } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
2400 rtlpriv->mac80211.vendor = PEER_MARV;
2401 matched = true;
2402 }
2403
2404 return matched;
2405}
2406
Larry Fingerd3bb1422011-04-25 13:23:20 -05002407static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
Chaoming_Liacd48572011-04-25 12:52:54 -05002408 unsigned int len)
2409{
2410 struct ieee80211_mgmt *mgmt = (void *)data;
2411 struct octet_string vendor_ie;
2412 u8 *pos, *end;
2413
2414 pos = (u8 *)mgmt->u.beacon.variable;
2415 end = data + len;
2416 while (pos < end) {
2417 if (pos[0] == 221) {
2418 vendor_ie.length = pos[1];
2419 vendor_ie.octet = &pos[2];
2420 if (rtl_chk_vendor_ouisub(hw, vendor_ie))
2421 return true;
2422 }
2423
2424 if (pos + 2 + pos[1] > end)
2425 return false;
2426
2427 pos += 2 + pos[1];
2428 }
2429 return false;
2430}
2431
2432void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
2433{
2434 struct rtl_priv *rtlpriv = rtl_priv(hw);
2435 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2436 struct ieee80211_hdr *hdr = (void *)data;
2437 u32 vendor = PEER_UNKNOWN;
2438
2439 static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
2440 static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
2441 static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
2442 static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
2443 static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
2444 static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
2445 static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
2446 static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
2447 static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
2448 static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
2449 static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
2450 static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
2451 static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
2452 static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
2453 static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
2454 static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
2455
2456 if (mac->opmode != NL80211_IFTYPE_STATION)
2457 return;
2458
2459 if (mac->link_state == MAC80211_NOLINK) {
2460 mac->vendor = PEER_UNKNOWN;
2461 return;
2462 }
2463
2464 if (mac->cnt_after_linked > 2)
2465 return;
2466
2467 /* check if this really is a beacon */
2468 if (!ieee80211_is_beacon(hdr->frame_control))
2469 return;
2470
2471 /* min. beacon length + FCS_LEN */
2472 if (len <= 40 + FCS_LEN)
2473 return;
2474
2475 /* and only beacons from the associated BSSID, please */
Julia Lawall90908e12013-12-30 19:15:02 +01002476 if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
Chaoming_Liacd48572011-04-25 12:52:54 -05002477 return;
2478
2479 if (rtl_find_221_ie(hw, data, len))
2480 vendor = mac->vendor;
2481
2482 if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
2483 (memcmp(mac->bssid, ap5_2, 3) == 0) ||
2484 (memcmp(mac->bssid, ap5_3, 3) == 0) ||
2485 (memcmp(mac->bssid, ap5_4, 3) == 0) ||
2486 (memcmp(mac->bssid, ap5_5, 3) == 0) ||
2487 (memcmp(mac->bssid, ap5_6, 3) == 0) ||
2488 vendor == PEER_ATH) {
2489 vendor = PEER_ATH;
Joe Perchesf30d7502012-01-04 19:40:41 -08002490 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002491 } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
2492 (memcmp(mac->bssid, ap4_5, 3) == 0) ||
2493 (memcmp(mac->bssid, ap4_1, 3) == 0) ||
2494 (memcmp(mac->bssid, ap4_2, 3) == 0) ||
2495 (memcmp(mac->bssid, ap4_3, 3) == 0) ||
2496 vendor == PEER_RAL) {
Joe Perchesf30d7502012-01-04 19:40:41 -08002497 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002498 vendor = PEER_RAL;
2499 } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
2500 vendor == PEER_CISCO) {
2501 vendor = PEER_CISCO;
Joe Perchesf30d7502012-01-04 19:40:41 -08002502 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002503 } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
2504 (memcmp(mac->bssid, ap3_2, 3) == 0) ||
2505 (memcmp(mac->bssid, ap3_3, 3) == 0) ||
2506 vendor == PEER_BROAD) {
Joe Perchesf30d7502012-01-04 19:40:41 -08002507 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002508 vendor = PEER_BROAD;
2509 } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
2510 vendor == PEER_MARV) {
2511 vendor = PEER_MARV;
Joe Perchesf30d7502012-01-04 19:40:41 -08002512 RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002513 }
2514
2515 mac->vendor = vendor;
2516}
Larry Finger6f334c22013-07-12 15:32:15 -05002517EXPORT_SYMBOL_GPL(rtl_recognize_peer);
Chaoming_Liacd48572011-04-25 12:52:54 -05002518
Larry Finger0c817332010-12-08 11:12:31 -06002519MODULE_AUTHOR("lizhaoming <chaoming_li@realsil.com.cn>");
2520MODULE_AUTHOR("Realtek WlanFAE <wlanfae@realtek.com>");
2521MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
2522MODULE_LICENSE("GPL");
2523MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2524
Larry Finger6f334c22013-07-12 15:32:15 -05002525struct rtl_global_var rtl_global_var = {};
2526EXPORT_SYMBOL_GPL(rtl_global_var);
Larry Finger26634c42013-03-24 22:06:33 -05002527
Larry Finger0c817332010-12-08 11:12:31 -06002528static int __init rtl_core_module_init(void)
2529{
Chaoming Li375ff4c2011-02-28 16:40:28 -06002530 if (rtl_rate_control_register())
Larry Finger9afa2e42014-09-22 09:39:21 -05002531 pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
Chaoming_Liacd48572011-04-25 12:52:54 -05002532
Ping-Ke Shih610247f2017-12-29 16:31:10 +08002533 /* add debugfs */
2534 rtl_debugfs_add_topdir();
2535
Larry Finger26634c42013-03-24 22:06:33 -05002536 /* init some global vars */
Larry Finger6f334c22013-07-12 15:32:15 -05002537 INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2538 spin_lock_init(&rtl_global_var.glb_list_lock);
Larry Finger26634c42013-03-24 22:06:33 -05002539
Larry Finger0c817332010-12-08 11:12:31 -06002540 return 0;
2541}
2542
2543static void __exit rtl_core_module_exit(void)
2544{
Chaoming_Liacd48572011-04-25 12:52:54 -05002545 /*RC*/
Chaoming Li375ff4c2011-02-28 16:40:28 -06002546 rtl_rate_control_unregister();
Ping-Ke Shih610247f2017-12-29 16:31:10 +08002547
2548 /* remove debugfs */
2549 rtl_debugfs_remove_topdir();
Larry Finger0c817332010-12-08 11:12:31 -06002550}
2551
2552module_init(rtl_core_module_init);
2553module_exit(rtl_core_module_exit);