1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "wifi.h"
5 #include "rc.h"
6 #include "base.h"
7 #include "efuse.h"
8 #include "cam.h"
9 #include "ps.h"
10 #include "regd.h"
11 #include "pci.h"
12 #include <linux/ip.h>
13 #include <linux/module.h>
14 #include <linux/udp.h>
15 
16 /*
17  *NOTICE!!!: This file will be very big, we should
18  *keep it clear under following roles:
19  *
20  *This file include following parts, so, if you add new
21  *functions into this file, please check which part it
22  *should includes. or check if you should add new part
23  *for this file:
24  *
25  *1) mac80211 init functions
26  *2) tx information functions
27  *3) functions called by core.c
28  *4) wq & timer callback functions
29  *5) frame process functions
30  *6) IOT functions
31  *7) sysfs functions
32  *8) vif functions
33  *9) ...
34  */
35 
36 /*********************************************************
37  *
38  * mac80211 init functions
39  *
40  *********************************************************/
41 static struct ieee80211_channel rtl_channeltable_2g[] = {
42 	{.center_freq = 2412, .hw_value = 1,},
43 	{.center_freq = 2417, .hw_value = 2,},
44 	{.center_freq = 2422, .hw_value = 3,},
45 	{.center_freq = 2427, .hw_value = 4,},
46 	{.center_freq = 2432, .hw_value = 5,},
47 	{.center_freq = 2437, .hw_value = 6,},
48 	{.center_freq = 2442, .hw_value = 7,},
49 	{.center_freq = 2447, .hw_value = 8,},
50 	{.center_freq = 2452, .hw_value = 9,},
51 	{.center_freq = 2457, .hw_value = 10,},
52 	{.center_freq = 2462, .hw_value = 11,},
53 	{.center_freq = 2467, .hw_value = 12,},
54 	{.center_freq = 2472, .hw_value = 13,},
55 	{.center_freq = 2484, .hw_value = 14,},
56 };
57 
58 static struct ieee80211_channel rtl_channeltable_5g[] = {
59 	{.center_freq = 5180, .hw_value = 36,},
60 	{.center_freq = 5200, .hw_value = 40,},
61 	{.center_freq = 5220, .hw_value = 44,},
62 	{.center_freq = 5240, .hw_value = 48,},
63 	{.center_freq = 5260, .hw_value = 52,},
64 	{.center_freq = 5280, .hw_value = 56,},
65 	{.center_freq = 5300, .hw_value = 60,},
66 	{.center_freq = 5320, .hw_value = 64,},
67 	{.center_freq = 5500, .hw_value = 100,},
68 	{.center_freq = 5520, .hw_value = 104,},
69 	{.center_freq = 5540, .hw_value = 108,},
70 	{.center_freq = 5560, .hw_value = 112,},
71 	{.center_freq = 5580, .hw_value = 116,},
72 	{.center_freq = 5600, .hw_value = 120,},
73 	{.center_freq = 5620, .hw_value = 124,},
74 	{.center_freq = 5640, .hw_value = 128,},
75 	{.center_freq = 5660, .hw_value = 132,},
76 	{.center_freq = 5680, .hw_value = 136,},
77 	{.center_freq = 5700, .hw_value = 140,},
78 	{.center_freq = 5745, .hw_value = 149,},
79 	{.center_freq = 5765, .hw_value = 153,},
80 	{.center_freq = 5785, .hw_value = 157,},
81 	{.center_freq = 5805, .hw_value = 161,},
82 	{.center_freq = 5825, .hw_value = 165,},
83 };
84 
85 static struct ieee80211_rate rtl_ratetable_2g[] = {
86 	{.bitrate = 10, .hw_value = 0x00,},
87 	{.bitrate = 20, .hw_value = 0x01,},
88 	{.bitrate = 55, .hw_value = 0x02,},
89 	{.bitrate = 110, .hw_value = 0x03,},
90 	{.bitrate = 60, .hw_value = 0x04,},
91 	{.bitrate = 90, .hw_value = 0x05,},
92 	{.bitrate = 120, .hw_value = 0x06,},
93 	{.bitrate = 180, .hw_value = 0x07,},
94 	{.bitrate = 240, .hw_value = 0x08,},
95 	{.bitrate = 360, .hw_value = 0x09,},
96 	{.bitrate = 480, .hw_value = 0x0a,},
97 	{.bitrate = 540, .hw_value = 0x0b,},
98 };
99 
100 static struct ieee80211_rate rtl_ratetable_5g[] = {
101 	{.bitrate = 60, .hw_value = 0x04,},
102 	{.bitrate = 90, .hw_value = 0x05,},
103 	{.bitrate = 120, .hw_value = 0x06,},
104 	{.bitrate = 180, .hw_value = 0x07,},
105 	{.bitrate = 240, .hw_value = 0x08,},
106 	{.bitrate = 360, .hw_value = 0x09,},
107 	{.bitrate = 480, .hw_value = 0x0a,},
108 	{.bitrate = 540, .hw_value = 0x0b,},
109 };
110 
111 static const struct ieee80211_supported_band rtl_band_2ghz = {
112 	.band = NL80211_BAND_2GHZ,
113 
114 	.channels = rtl_channeltable_2g,
115 	.n_channels = ARRAY_SIZE(rtl_channeltable_2g),
116 
117 	.bitrates = rtl_ratetable_2g,
118 	.n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
119 
120 	.ht_cap = {0},
121 };
122 
123 static struct ieee80211_supported_band rtl_band_5ghz = {
124 	.band = NL80211_BAND_5GHZ,
125 
126 	.channels = rtl_channeltable_5g,
127 	.n_channels = ARRAY_SIZE(rtl_channeltable_5g),
128 
129 	.bitrates = rtl_ratetable_5g,
130 	.n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
131 
132 	.ht_cap = {0},
133 };
134 
135 static const u8 tid_to_ac[] = {
136 	2, /* IEEE80211_AC_BE */
137 	3, /* IEEE80211_AC_BK */
138 	3, /* IEEE80211_AC_BK */
139 	2, /* IEEE80211_AC_BE */
140 	1, /* IEEE80211_AC_VI */
141 	1, /* IEEE80211_AC_VI */
142 	0, /* IEEE80211_AC_VO */
143 	0, /* IEEE80211_AC_VO */
144 };
145 
146 u8 rtl_tid_to_ac(u8 tid)
147 {
148 	return tid_to_ac[tid];
149 }
150 EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
151 
152 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
153 				  struct ieee80211_sta_ht_cap *ht_cap)
154 {
155 	struct rtl_priv *rtlpriv = rtl_priv(hw);
156 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
157 
158 	ht_cap->ht_supported = true;
159 	ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
160 	    IEEE80211_HT_CAP_SGI_40 |
161 	    IEEE80211_HT_CAP_SGI_20 |
162 	    IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
163 
164 	if (rtlpriv->rtlhal.disable_amsdu_8k)
165 		ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
166 
167 	/*
168 	 *Maximum length of AMPDU that the STA can receive.
169 	 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
170 	 */
171 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
172 
173 	/*Minimum MPDU start spacing , */
174 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
175 
176 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
177 
178 	/*hw->wiphy->bands[NL80211_BAND_2GHZ]
179 	 *base on ant_num
180 	 *rx_mask: RX mask
181 	 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
182 	 *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
183 	 *if rx_ant >= 3 rx_mask[2]= 0xff;
184 	 *if BW_40 rx_mask[4]= 0x01;
185 	 *highest supported RX rate
186 	 */
187 	if (rtlpriv->dm.supp_phymode_switch) {
188 		pr_info("Support phy mode switch\n");
189 
190 		ht_cap->mcs.rx_mask[0] = 0xFF;
191 		ht_cap->mcs.rx_mask[1] = 0xFF;
192 		ht_cap->mcs.rx_mask[4] = 0x01;
193 
194 		ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
195 	} else {
196 		if (get_rf_type(rtlphy) == RF_1T2R ||
197 		    get_rf_type(rtlphy) == RF_2T2R) {
198 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
199 				 "1T2R or 2T2R\n");
200 			ht_cap->mcs.rx_mask[0] = 0xFF;
201 			ht_cap->mcs.rx_mask[1] = 0xFF;
202 			ht_cap->mcs.rx_mask[4] = 0x01;
203 
204 			ht_cap->mcs.rx_highest =
205 				 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
206 		} else if (get_rf_type(rtlphy) == RF_1T1R) {
207 			RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
208 
209 			ht_cap->mcs.rx_mask[0] = 0xFF;
210 			ht_cap->mcs.rx_mask[1] = 0x00;
211 			ht_cap->mcs.rx_mask[4] = 0x01;
212 
213 			ht_cap->mcs.rx_highest =
214 				 cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
215 		}
216 	}
217 }
218 
219 static void _rtl_init_hw_vht_capab(struct ieee80211_hw *hw,
220 				   struct ieee80211_sta_vht_cap *vht_cap)
221 {
222 	struct rtl_priv *rtlpriv = rtl_priv(hw);
223 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
224 
225 	if (!(rtlpriv->cfg->spec_ver & RTL_SPEC_SUPPORT_VHT))
226 		return;
227 
228 	if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE ||
229 	    rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE) {
230 		u16 mcs_map;
231 
232 		vht_cap->vht_supported = true;
233 		vht_cap->cap =
234 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
235 			IEEE80211_VHT_CAP_SHORT_GI_80 |
236 			IEEE80211_VHT_CAP_TXSTBC |
237 			IEEE80211_VHT_CAP_RXSTBC_1 |
238 			IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
239 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
240 			IEEE80211_VHT_CAP_HTC_VHT |
241 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
242 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
243 			IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
244 			0;
245 
246 		mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
247 			IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
248 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
249 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
250 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
251 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
252 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
253 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
254 
255 		vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
256 		vht_cap->vht_mcs.rx_highest =
257 			cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
258 		vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
259 		vht_cap->vht_mcs.tx_highest =
260 			cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
261 	} else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
262 		u16 mcs_map;
263 
264 		vht_cap->vht_supported = true;
265 		vht_cap->cap =
266 			IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
267 			IEEE80211_VHT_CAP_SHORT_GI_80 |
268 			IEEE80211_VHT_CAP_TXSTBC |
269 			IEEE80211_VHT_CAP_RXSTBC_1 |
270 			IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
271 			IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
272 			IEEE80211_VHT_CAP_HTC_VHT |
273 			IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
274 			IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
275 			IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
276 			0;
277 
278 		mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
279 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
280 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
281 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
282 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
283 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
284 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
285 			IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
286 
287 		vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
288 		vht_cap->vht_mcs.rx_highest =
289 			cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
290 		vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
291 		vht_cap->vht_mcs.tx_highest =
292 			cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
293 	}
294 }
295 
296 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
297 {
298 	struct rtl_priv *rtlpriv = rtl_priv(hw);
299 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
300 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
301 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
302 	struct ieee80211_supported_band *sband;
303 
304 	if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
305 	    rtlhal->bandset == BAND_ON_BOTH) {
306 		/* 1: 2.4 G bands */
307 		/* <1> use  mac->bands as mem for hw->wiphy->bands */
308 		sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
309 
310 		/* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
311 		 * to default value(1T1R) */
312 		memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]), &rtl_band_2ghz,
313 				sizeof(struct ieee80211_supported_band));
314 
315 		/* <3> init ht cap base on ant_num */
316 		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
317 
318 		/* <4> set mac->sband to wiphy->sband */
319 		hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
320 
321 		/* 2: 5 G bands */
322 		/* <1> use  mac->bands as mem for hw->wiphy->bands */
323 		sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
324 
325 		/* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
326 		 * to default value(1T1R) */
327 		memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]), &rtl_band_5ghz,
328 				sizeof(struct ieee80211_supported_band));
329 
330 		/* <3> init ht cap base on ant_num */
331 		_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
332 
333 		_rtl_init_hw_vht_capab(hw, &sband->vht_cap);
334 		/* <4> set mac->sband to wiphy->sband */
335 		hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
336 	} else {
337 		if (rtlhal->current_bandtype == BAND_ON_2_4G) {
338 			/* <1> use  mac->bands as mem for hw->wiphy->bands */
339 			sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
340 
341 			/* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
342 			 * to default value(1T1R) */
343 			memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]),
344 			       &rtl_band_2ghz,
345 			       sizeof(struct ieee80211_supported_band));
346 
347 			/* <3> init ht cap base on ant_num */
348 			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
349 
350 			/* <4> set mac->sband to wiphy->sband */
351 			hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
352 		} else if (rtlhal->current_bandtype == BAND_ON_5G) {
353 			/* <1> use  mac->bands as mem for hw->wiphy->bands */
354 			sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
355 
356 			/* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
357 			 * to default value(1T1R) */
358 			memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]),
359 			       &rtl_band_5ghz,
360 			       sizeof(struct ieee80211_supported_band));
361 
362 			/* <3> init ht cap base on ant_num */
363 			_rtl_init_hw_ht_capab(hw, &sband->ht_cap);
364 
365 			_rtl_init_hw_vht_capab(hw, &sband->vht_cap);
366 			/* <4> set mac->sband to wiphy->sband */
367 			hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
368 		} else {
369 			pr_err("Err BAND %d\n",
370 			       rtlhal->current_bandtype);
371 		}
372 	}
373 	/* <5> set hw caps */
374 	ieee80211_hw_set(hw, SIGNAL_DBM);
375 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
376 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
377 	ieee80211_hw_set(hw, MFP_CAPABLE);
378 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
379 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
380 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
381 
382 	/* swlps or hwlps has been set in diff chip in init_sw_vars */
383 	if (rtlpriv->psc.swctrl_lps) {
384 		ieee80211_hw_set(hw, SUPPORTS_PS);
385 		ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
386 	}
387 	if (rtlpriv->psc.fwctrl_lps) {
388 		ieee80211_hw_set(hw, SUPPORTS_PS);
389 		ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
390 	}
391 	hw->wiphy->interface_modes =
392 	    BIT(NL80211_IFTYPE_AP) |
393 	    BIT(NL80211_IFTYPE_STATION) |
394 	    BIT(NL80211_IFTYPE_ADHOC) |
395 	    BIT(NL80211_IFTYPE_MESH_POINT) |
396 	    BIT(NL80211_IFTYPE_P2P_CLIENT) |
397 	    BIT(NL80211_IFTYPE_P2P_GO);
398 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
399 
400 	hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
401 
402 	hw->wiphy->rts_threshold = 2347;
403 
404 	hw->queues = AC_MAX;
405 	hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
406 
407 	/* TODO: Correct this value for our hw */
408 	hw->max_listen_interval = MAX_LISTEN_INTERVAL;
409 	hw->max_rate_tries = MAX_RATE_TRIES;
410 	/* hw->max_rates = 1; */
411 	hw->sta_data_size = sizeof(struct rtl_sta_info);
412 
413 /* wowlan is not supported by kernel if CONFIG_PM is not defined */
414 #ifdef CONFIG_PM
415 	if (rtlpriv->psc.wo_wlan_mode) {
416 		if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
417 			rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
418 		if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
419 			rtlpriv->wowlan.n_patterns =
420 				MAX_SUPPORT_WOL_PATTERN_NUM;
421 			rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
422 			rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
423 		}
424 		hw->wiphy->wowlan = &rtlpriv->wowlan;
425 	}
426 #endif
427 
428 	/* <6> mac address */
429 	if (is_valid_ether_addr(rtlefuse->dev_addr)) {
430 		SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
431 	} else {
432 		u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
433 
434 		get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
435 		SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
436 	}
437 }
438 
439 static void _rtl_init_deferred_work(struct ieee80211_hw *hw)
440 {
441 	struct rtl_priv *rtlpriv = rtl_priv(hw);
442 
443 	/* <1> timer */
444 	timer_setup(&rtlpriv->works.watchdog_timer,
445 		    rtl_watch_dog_timer_callback, 0);
446 	timer_setup(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
447 		    rtl_easy_concurrent_retrytimer_callback, 0);
448 	/* <2> work queue */
449 	rtlpriv->works.hw = hw;
450 	rtlpriv->works.rtl_wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
451 	INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
452 			  (void *)rtl_watchdog_wq_callback);
453 	INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
454 			  (void *)rtl_ips_nic_off_wq_callback);
455 	INIT_DELAYED_WORK(&rtlpriv->works.ps_work,
456 			  (void *)rtl_swlps_wq_callback);
457 	INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
458 			  (void *)rtl_swlps_rfon_wq_callback);
459 	INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq,
460 			  (void *)rtl_fwevt_wq_callback);
461 	INIT_DELAYED_WORK(&rtlpriv->works.c2hcmd_wq,
462 			  (void *)rtl_c2hcmd_wq_callback);
463 }
464 
465 void rtl_deinit_deferred_work(struct ieee80211_hw *hw, bool ips_wq)
466 {
467 	struct rtl_priv *rtlpriv = rtl_priv(hw);
468 
469 	del_timer_sync(&rtlpriv->works.watchdog_timer);
470 
471 	cancel_delayed_work_sync(&rtlpriv->works.watchdog_wq);
472 	if (ips_wq)
473 		cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
474 	else
475 		cancel_delayed_work_sync(&rtlpriv->works.ips_nic_off_wq);
476 	cancel_delayed_work_sync(&rtlpriv->works.ps_work);
477 	cancel_delayed_work_sync(&rtlpriv->works.ps_rfon_wq);
478 	cancel_delayed_work_sync(&rtlpriv->works.fwevt_wq);
479 	cancel_delayed_work_sync(&rtlpriv->works.c2hcmd_wq);
480 }
481 EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
482 
483 void rtl_init_rfkill(struct ieee80211_hw *hw)
484 {
485 	struct rtl_priv *rtlpriv = rtl_priv(hw);
486 
487 	bool radio_state;
488 	bool blocked;
489 	u8 valid = 0;
490 
491 	/*set init state to on */
492 	rtlpriv->rfkill.rfkill_state = true;
493 	wiphy_rfkill_set_hw_state(hw->wiphy, 0);
494 
495 	radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
496 
497 	if (valid) {
498 		pr_info("rtlwifi: wireless switch is %s\n",
499 			rtlpriv->rfkill.rfkill_state ? "on" : "off");
500 
501 		rtlpriv->rfkill.rfkill_state = radio_state;
502 
503 		blocked = (rtlpriv->rfkill.rfkill_state == 1) ? 0 : 1;
504 		wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
505 	}
506 
507 	wiphy_rfkill_start_polling(hw->wiphy);
508 }
509 EXPORT_SYMBOL(rtl_init_rfkill);
510 
511 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
512 {
513 	wiphy_rfkill_stop_polling(hw->wiphy);
514 }
515 EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
516 
517 int rtl_init_core(struct ieee80211_hw *hw)
518 {
519 	struct rtl_priv *rtlpriv = rtl_priv(hw);
520 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
521 
522 	/* <1> init mac80211 */
523 	_rtl_init_mac80211(hw);
524 	rtlmac->hw = hw;
525 
526 	/* <2> rate control register */
527 	hw->rate_control_algorithm = "rtl_rc";
528 
529 	/*
530 	 * <3> init CRDA must come after init
531 	 * mac80211 hw  in _rtl_init_mac80211.
532 	 */
533 	if (rtl_regd_init(hw, rtl_reg_notifier)) {
534 		pr_err("REGD init failed\n");
535 		return 1;
536 	}
537 
538 	/* <4> locks */
539 	mutex_init(&rtlpriv->locks.conf_mutex);
540 	mutex_init(&rtlpriv->locks.ips_mutex);
541 	mutex_init(&rtlpriv->locks.lps_mutex);
542 	spin_lock_init(&rtlpriv->locks.irq_th_lock);
543 	spin_lock_init(&rtlpriv->locks.h2c_lock);
544 	spin_lock_init(&rtlpriv->locks.rf_ps_lock);
545 	spin_lock_init(&rtlpriv->locks.rf_lock);
546 	spin_lock_init(&rtlpriv->locks.waitq_lock);
547 	spin_lock_init(&rtlpriv->locks.entry_list_lock);
548 	spin_lock_init(&rtlpriv->locks.c2hcmd_lock);
549 	spin_lock_init(&rtlpriv->locks.scan_list_lock);
550 	spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
551 	spin_lock_init(&rtlpriv->locks.fw_ps_lock);
552 	spin_lock_init(&rtlpriv->locks.iqk_lock);
553 	/* <5> init list */
554 	INIT_LIST_HEAD(&rtlpriv->entry_list);
555 	INIT_LIST_HEAD(&rtlpriv->scan_list.list);
556 	skb_queue_head_init(&rtlpriv->tx_report.queue);
557 	skb_queue_head_init(&rtlpriv->c2hcmd_queue);
558 
559 	rtlmac->link_state = MAC80211_NOLINK;
560 
561 	/* <6> init deferred work */
562 	_rtl_init_deferred_work(hw);
563 
564 	return 0;
565 }
566 EXPORT_SYMBOL_GPL(rtl_init_core);
567 
568 static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw);
569 static void rtl_free_entries_from_ack_queue(struct ieee80211_hw *hw,
570 					    bool timeout);
571 
572 void rtl_deinit_core(struct ieee80211_hw *hw)
573 {
574 	rtl_c2hcmd_launcher(hw, 0);
575 	rtl_free_entries_from_scan_list(hw);
576 	rtl_free_entries_from_ack_queue(hw, false);
577 }
578 EXPORT_SYMBOL_GPL(rtl_deinit_core);
579 
580 void rtl_init_rx_config(struct ieee80211_hw *hw)
581 {
582 	struct rtl_priv *rtlpriv = rtl_priv(hw);
583 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
584 
585 	rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
586 }
587 EXPORT_SYMBOL_GPL(rtl_init_rx_config);
588 
589 /*********************************************************
590  *
591  * tx information functions
592  *
593  *********************************************************/
594 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
595 					  struct rtl_tcb_desc *tcb_desc,
596 					  struct ieee80211_tx_info *info)
597 {
598 	struct rtl_priv *rtlpriv = rtl_priv(hw);
599 	u8 rate_flag = info->control.rates[0].flags;
600 
601 	tcb_desc->use_shortpreamble = false;
602 
603 	/* 1M can only use Long Preamble. 11B spec */
604 	if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
605 		return;
606 	else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
607 		tcb_desc->use_shortpreamble = true;
608 
609 	return;
610 }
611 
612 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
613 			       struct ieee80211_sta *sta,
614 			       struct rtl_tcb_desc *tcb_desc,
615 			       struct ieee80211_tx_info *info)
616 {
617 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
618 	u8 rate_flag = info->control.rates[0].flags;
619 	u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
620 	u8 sgi_80 = 0, bw_80 = 0;
621 
622 	tcb_desc->use_shortgi = false;
623 
624 	if (sta == NULL)
625 		return;
626 
627 	sgi_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
628 	sgi_20 = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
629 	sgi_80 = sta->vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
630 
631 	if ((!sta->ht_cap.ht_supported) && (!sta->vht_cap.vht_supported))
632 		return;
633 
634 	if (!sgi_40 && !sgi_20)
635 		return;
636 
637 	if (mac->opmode == NL80211_IFTYPE_STATION) {
638 		bw_40 = mac->bw_40;
639 		bw_80 = mac->bw_80;
640 	} else if (mac->opmode == NL80211_IFTYPE_AP ||
641 		 mac->opmode == NL80211_IFTYPE_ADHOC ||
642 		 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
643 		bw_40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
644 		bw_80 = sta->vht_cap.vht_supported;
645 	}
646 
647 	if (bw_80) {
648 		if (sgi_80)
649 			tcb_desc->use_shortgi = true;
650 		else
651 			tcb_desc->use_shortgi = false;
652 	} else {
653 		if (bw_40 && sgi_40)
654 			tcb_desc->use_shortgi = true;
655 		else if (!bw_40 && sgi_20)
656 			tcb_desc->use_shortgi = true;
657 	}
658 
659 	if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
660 		tcb_desc->use_shortgi = false;
661 }
662 
663 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
664 				       struct rtl_tcb_desc *tcb_desc,
665 				       struct ieee80211_tx_info *info)
666 {
667 	struct rtl_priv *rtlpriv = rtl_priv(hw);
668 	u8 rate_flag = info->control.rates[0].flags;
669 
670 	/* Common Settings */
671 	tcb_desc->rts_stbc = false;
672 	tcb_desc->cts_enable = false;
673 	tcb_desc->rts_sc = 0;
674 	tcb_desc->rts_bw = false;
675 	tcb_desc->rts_use_shortpreamble = false;
676 	tcb_desc->rts_use_shortgi = false;
677 
678 	if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
679 		/* Use CTS-to-SELF in protection mode. */
680 		tcb_desc->rts_enable = true;
681 		tcb_desc->cts_enable = true;
682 		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
683 	} else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
684 		/* Use RTS-CTS in protection mode. */
685 		tcb_desc->rts_enable = true;
686 		tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
687 	}
688 }
689 
690 u8 rtl_mrate_idx_to_arfr_id(struct ieee80211_hw *hw, u8 rate_index,
691 			    enum wireless_mode wirelessmode)
692 {
693 	struct rtl_priv *rtlpriv = rtl_priv(hw);
694 	struct rtl_phy *rtlphy = &rtlpriv->phy;
695 	u8 ret = 0;
696 
697 	switch (rate_index) {
698 	case RATR_INX_WIRELESS_NGB:
699 		if (rtlphy->rf_type == RF_1T1R)
700 			ret = RATEID_IDX_BGN_40M_1SS;
701 		else
702 			ret = RATEID_IDX_BGN_40M_2SS;
703 		; break;
704 	case RATR_INX_WIRELESS_N:
705 	case RATR_INX_WIRELESS_NG:
706 		if (rtlphy->rf_type == RF_1T1R)
707 			ret = RATEID_IDX_GN_N1SS;
708 		else
709 			ret = RATEID_IDX_GN_N2SS;
710 		; break;
711 	case RATR_INX_WIRELESS_NB:
712 		if (rtlphy->rf_type == RF_1T1R)
713 			ret = RATEID_IDX_BGN_20M_1SS_BN;
714 		else
715 			ret = RATEID_IDX_BGN_20M_2SS_BN;
716 		; break;
717 	case RATR_INX_WIRELESS_GB:
718 		ret = RATEID_IDX_BG;
719 		break;
720 	case RATR_INX_WIRELESS_G:
721 		ret = RATEID_IDX_G;
722 		break;
723 	case RATR_INX_WIRELESS_B:
724 		ret = RATEID_IDX_B;
725 		break;
726 	case RATR_INX_WIRELESS_MC:
727 		if (wirelessmode == WIRELESS_MODE_B ||
728 		    wirelessmode == WIRELESS_MODE_G ||
729 		    wirelessmode == WIRELESS_MODE_N_24G ||
730 		    wirelessmode == WIRELESS_MODE_AC_24G)
731 			ret = RATEID_IDX_BG;
732 		else
733 			ret = RATEID_IDX_G;
734 		break;
735 	case RATR_INX_WIRELESS_AC_5N:
736 		if (rtlphy->rf_type == RF_1T1R)
737 			ret = RATEID_IDX_VHT_1SS;
738 		else
739 			ret = RATEID_IDX_VHT_2SS;
740 		break;
741 	case RATR_INX_WIRELESS_AC_24N:
742 		if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
743 			if (rtlphy->rf_type == RF_1T1R)
744 				ret = RATEID_IDX_VHT_1SS;
745 			else
746 				ret = RATEID_IDX_VHT_2SS;
747 		} else {
748 			if (rtlphy->rf_type == RF_1T1R)
749 				ret = RATEID_IDX_MIX1;
750 			else
751 				ret = RATEID_IDX_MIX2;
752 		}
753 		break;
754 	default:
755 		ret = RATEID_IDX_BGN_40M_2SS;
756 		break;
757 	}
758 	return ret;
759 }
760 EXPORT_SYMBOL(rtl_mrate_idx_to_arfr_id);
761 
762 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
763 				   struct ieee80211_sta *sta,
764 				   struct rtl_tcb_desc *tcb_desc)
765 {
766 #define SET_RATE_ID(rate_id)					\
767 	({typeof(rate_id) _id = rate_id;			\
768 	  ((rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) ?	\
769 		rtl_mrate_idx_to_arfr_id(hw, _id,		\
770 			(sta_entry ? sta_entry->wireless_mode :	\
771 			 WIRELESS_MODE_G)) :			\
772 		_id); })
773 
774 	struct rtl_priv *rtlpriv = rtl_priv(hw);
775 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
776 	struct rtl_sta_info *sta_entry = NULL;
777 	u8 ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
778 
779 	if (sta) {
780 		sta_entry = (struct rtl_sta_info *) sta->drv_priv;
781 		ratr_index = sta_entry->ratr_index;
782 	}
783 	if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
784 		if (mac->opmode == NL80211_IFTYPE_STATION) {
785 			tcb_desc->ratr_index = 0;
786 		} else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
787 				mac->opmode == NL80211_IFTYPE_MESH_POINT) {
788 			if (tcb_desc->multicast || tcb_desc->broadcast) {
789 				tcb_desc->hw_rate =
790 				    rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
791 				tcb_desc->use_driver_rate = 1;
792 				tcb_desc->ratr_index =
793 					SET_RATE_ID(RATR_INX_WIRELESS_MC);
794 			} else {
795 				tcb_desc->ratr_index = ratr_index;
796 			}
797 		} else if (mac->opmode == NL80211_IFTYPE_AP) {
798 			tcb_desc->ratr_index = ratr_index;
799 		}
800 	}
801 
802 	if (rtlpriv->dm.useramask) {
803 		tcb_desc->ratr_index = ratr_index;
804 		/* TODO we will differentiate adhoc and station future  */
805 		if (mac->opmode == NL80211_IFTYPE_STATION ||
806 		    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
807 			tcb_desc->mac_id = 0;
808 
809 			if (sta &&
810 			    (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID))
811 				;	/* use sta_entry->ratr_index */
812 			else if (mac->mode == WIRELESS_MODE_AC_5G)
813 				tcb_desc->ratr_index =
814 					SET_RATE_ID(RATR_INX_WIRELESS_AC_5N);
815 			else if (mac->mode == WIRELESS_MODE_AC_24G)
816 				tcb_desc->ratr_index =
817 					SET_RATE_ID(RATR_INX_WIRELESS_AC_24N);
818 			else if (mac->mode == WIRELESS_MODE_N_24G)
819 				tcb_desc->ratr_index =
820 					SET_RATE_ID(RATR_INX_WIRELESS_NGB);
821 			else if (mac->mode == WIRELESS_MODE_N_5G)
822 				tcb_desc->ratr_index =
823 					SET_RATE_ID(RATR_INX_WIRELESS_NG);
824 			else if (mac->mode & WIRELESS_MODE_G)
825 				tcb_desc->ratr_index =
826 					SET_RATE_ID(RATR_INX_WIRELESS_GB);
827 			else if (mac->mode & WIRELESS_MODE_B)
828 				tcb_desc->ratr_index =
829 					SET_RATE_ID(RATR_INX_WIRELESS_B);
830 			else if (mac->mode & WIRELESS_MODE_A)
831 				tcb_desc->ratr_index =
832 					SET_RATE_ID(RATR_INX_WIRELESS_G);
833 
834 		} else if (mac->opmode == NL80211_IFTYPE_AP ||
835 			mac->opmode == NL80211_IFTYPE_ADHOC) {
836 			if (NULL != sta) {
837 				if (sta->aid > 0)
838 					tcb_desc->mac_id = sta->aid + 1;
839 				else
840 					tcb_desc->mac_id = 1;
841 			} else {
842 				tcb_desc->mac_id = 0;
843 			}
844 		}
845 	}
846 #undef SET_RATE_ID
847 }
848 
849 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
850 				      struct ieee80211_sta *sta,
851 				      struct rtl_tcb_desc *tcb_desc)
852 {
853 	struct rtl_priv *rtlpriv = rtl_priv(hw);
854 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
855 
856 	tcb_desc->packet_bw = false;
857 	if (!sta)
858 		return;
859 	if (mac->opmode == NL80211_IFTYPE_AP ||
860 	    mac->opmode == NL80211_IFTYPE_ADHOC ||
861 	    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
862 		if (!(sta->ht_cap.ht_supported) ||
863 		    !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
864 			return;
865 	} else if (mac->opmode == NL80211_IFTYPE_STATION) {
866 		if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
867 			return;
868 	}
869 	if (tcb_desc->multicast || tcb_desc->broadcast)
870 		return;
871 
872 	/*use legency rate, shall use 20MHz */
873 	if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
874 		return;
875 
876 	tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
877 
878 	if (rtlpriv->cfg->spec_ver & RTL_SPEC_SUPPORT_VHT) {
879 		if (mac->opmode == NL80211_IFTYPE_AP ||
880 		    mac->opmode == NL80211_IFTYPE_ADHOC ||
881 		    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
882 			if (!(sta->vht_cap.vht_supported))
883 				return;
884 		} else if (mac->opmode == NL80211_IFTYPE_STATION) {
885 			if (!mac->bw_80 ||
886 			    !(sta->vht_cap.vht_supported))
887 				return;
888 		}
889 		if (tcb_desc->hw_rate <=
890 			rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
891 			return;
892 		tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
893 	}
894 }
895 
896 static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
897 				      struct ieee80211_sta *sta)
898 {
899 	struct rtl_priv *rtlpriv = rtl_priv(hw);
900 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
901 	u8 hw_rate;
902 	u16 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
903 
904 	if ((get_rf_type(rtlphy) == RF_2T2R) &&
905 	    (tx_mcs_map & 0x000c) != 0x000c) {
906 		if ((tx_mcs_map & 0x000c) >> 2 ==
907 			IEEE80211_VHT_MCS_SUPPORT_0_7)
908 			hw_rate =
909 			rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
910 		else if ((tx_mcs_map  & 0x000c) >> 2 ==
911 			IEEE80211_VHT_MCS_SUPPORT_0_8)
912 			hw_rate =
913 			rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS8];
914 		else
915 			hw_rate =
916 			rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
917 	} else {
918 		if ((tx_mcs_map  & 0x0003) ==
919 			IEEE80211_VHT_MCS_SUPPORT_0_7)
920 			hw_rate =
921 			rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
922 		else if ((tx_mcs_map  & 0x0003) ==
923 			IEEE80211_VHT_MCS_SUPPORT_0_8)
924 			hw_rate =
925 			rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS8];
926 		else
927 			hw_rate =
928 			rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
929 	}
930 
931 	return hw_rate;
932 }
933 
934 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
935 				  struct ieee80211_sta *sta)
936 {
937 	struct rtl_priv *rtlpriv = rtl_priv(hw);
938 	struct rtl_phy *rtlphy = &rtlpriv->phy;
939 	u8 hw_rate;
940 
941 	if (get_rf_type(rtlphy) == RF_2T2R &&
942 	    sta->ht_cap.mcs.rx_mask[1] != 0)
943 		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
944 	else
945 		hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
946 
947 	return hw_rate;
948 }
949 
950 /* mac80211's rate_idx is like this:
951  *
952  * 2.4G band:rx_status->band == NL80211_BAND_2GHZ
953  *
954  * B/G rate:
955  * (rx_status->flag & RX_FLAG_HT) = 0,
956  * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
957  *
958  * N rate:
959  * (rx_status->flag & RX_FLAG_HT) = 1,
960  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
961  *
962  * 5G band:rx_status->band == NL80211_BAND_5GHZ
963  * A rate:
964  * (rx_status->flag & RX_FLAG_HT) = 0,
965  * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
966  *
967  * N rate:
968  * (rx_status->flag & RX_FLAG_HT) = 1,
969  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
970  *
971  * VHT rates:
972  * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
973  * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
974  */
975 int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
976 			 u8 desc_rate)
977 {
978 	int rate_idx;
979 
980 	if (isvht) {
981 		switch (desc_rate) {
982 		case DESC_RATEVHT1SS_MCS0:
983 			rate_idx = 0;
984 			break;
985 		case DESC_RATEVHT1SS_MCS1:
986 			rate_idx = 1;
987 			break;
988 		case DESC_RATEVHT1SS_MCS2:
989 			rate_idx = 2;
990 			break;
991 		case DESC_RATEVHT1SS_MCS3:
992 			rate_idx = 3;
993 			break;
994 		case DESC_RATEVHT1SS_MCS4:
995 			rate_idx = 4;
996 			break;
997 		case DESC_RATEVHT1SS_MCS5:
998 			rate_idx = 5;
999 			break;
1000 		case DESC_RATEVHT1SS_MCS6:
1001 			rate_idx = 6;
1002 			break;
1003 		case DESC_RATEVHT1SS_MCS7:
1004 			rate_idx = 7;
1005 			break;
1006 		case DESC_RATEVHT1SS_MCS8:
1007 			rate_idx = 8;
1008 			break;
1009 		case DESC_RATEVHT1SS_MCS9:
1010 			rate_idx = 9;
1011 			break;
1012 		case DESC_RATEVHT2SS_MCS0:
1013 			rate_idx = 0;
1014 			break;
1015 		case DESC_RATEVHT2SS_MCS1:
1016 			rate_idx = 1;
1017 			break;
1018 		case DESC_RATEVHT2SS_MCS2:
1019 			rate_idx = 2;
1020 			break;
1021 		case DESC_RATEVHT2SS_MCS3:
1022 			rate_idx = 3;
1023 			break;
1024 		case DESC_RATEVHT2SS_MCS4:
1025 			rate_idx = 4;
1026 			break;
1027 		case DESC_RATEVHT2SS_MCS5:
1028 			rate_idx = 5;
1029 			break;
1030 		case DESC_RATEVHT2SS_MCS6:
1031 			rate_idx = 6;
1032 			break;
1033 		case DESC_RATEVHT2SS_MCS7:
1034 			rate_idx = 7;
1035 			break;
1036 		case DESC_RATEVHT2SS_MCS8:
1037 			rate_idx = 8;
1038 			break;
1039 		case DESC_RATEVHT2SS_MCS9:
1040 			rate_idx = 9;
1041 			break;
1042 		default:
1043 			rate_idx = 0;
1044 			break;
1045 		}
1046 		return rate_idx;
1047 	}
1048 	if (false == isht) {
1049 		if (NL80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
1050 			switch (desc_rate) {
1051 			case DESC_RATE1M:
1052 				rate_idx = 0;
1053 				break;
1054 			case DESC_RATE2M:
1055 				rate_idx = 1;
1056 				break;
1057 			case DESC_RATE5_5M:
1058 				rate_idx = 2;
1059 				break;
1060 			case DESC_RATE11M:
1061 				rate_idx = 3;
1062 				break;
1063 			case DESC_RATE6M:
1064 				rate_idx = 4;
1065 				break;
1066 			case DESC_RATE9M:
1067 				rate_idx = 5;
1068 				break;
1069 			case DESC_RATE12M:
1070 				rate_idx = 6;
1071 				break;
1072 			case DESC_RATE18M:
1073 				rate_idx = 7;
1074 				break;
1075 			case DESC_RATE24M:
1076 				rate_idx = 8;
1077 				break;
1078 			case DESC_RATE36M:
1079 				rate_idx = 9;
1080 				break;
1081 			case DESC_RATE48M:
1082 				rate_idx = 10;
1083 				break;
1084 			case DESC_RATE54M:
1085 				rate_idx = 11;
1086 				break;
1087 			default:
1088 				rate_idx = 0;
1089 				break;
1090 			}
1091 		} else {
1092 			switch (desc_rate) {
1093 			case DESC_RATE6M:
1094 				rate_idx = 0;
1095 				break;
1096 			case DESC_RATE9M:
1097 				rate_idx = 1;
1098 				break;
1099 			case DESC_RATE12M:
1100 				rate_idx = 2;
1101 				break;
1102 			case DESC_RATE18M:
1103 				rate_idx = 3;
1104 				break;
1105 			case DESC_RATE24M:
1106 				rate_idx = 4;
1107 				break;
1108 			case DESC_RATE36M:
1109 				rate_idx = 5;
1110 				break;
1111 			case DESC_RATE48M:
1112 				rate_idx = 6;
1113 				break;
1114 			case DESC_RATE54M:
1115 				rate_idx = 7;
1116 				break;
1117 			default:
1118 				rate_idx = 0;
1119 				break;
1120 			}
1121 		}
1122 	} else {
1123 		switch (desc_rate) {
1124 		case DESC_RATEMCS0:
1125 			rate_idx = 0;
1126 			break;
1127 		case DESC_RATEMCS1:
1128 			rate_idx = 1;
1129 			break;
1130 		case DESC_RATEMCS2:
1131 			rate_idx = 2;
1132 			break;
1133 		case DESC_RATEMCS3:
1134 			rate_idx = 3;
1135 			break;
1136 		case DESC_RATEMCS4:
1137 			rate_idx = 4;
1138 			break;
1139 		case DESC_RATEMCS5:
1140 			rate_idx = 5;
1141 			break;
1142 		case DESC_RATEMCS6:
1143 			rate_idx = 6;
1144 			break;
1145 		case DESC_RATEMCS7:
1146 			rate_idx = 7;
1147 			break;
1148 		case DESC_RATEMCS8:
1149 			rate_idx = 8;
1150 			break;
1151 		case DESC_RATEMCS9:
1152 			rate_idx = 9;
1153 			break;
1154 		case DESC_RATEMCS10:
1155 			rate_idx = 10;
1156 			break;
1157 		case DESC_RATEMCS11:
1158 			rate_idx = 11;
1159 			break;
1160 		case DESC_RATEMCS12:
1161 			rate_idx = 12;
1162 			break;
1163 		case DESC_RATEMCS13:
1164 			rate_idx = 13;
1165 			break;
1166 		case DESC_RATEMCS14:
1167 			rate_idx = 14;
1168 			break;
1169 		case DESC_RATEMCS15:
1170 			rate_idx = 15;
1171 			break;
1172 		default:
1173 			rate_idx = 0;
1174 			break;
1175 		}
1176 	}
1177 	return rate_idx;
1178 }
1179 EXPORT_SYMBOL(rtlwifi_rate_mapping);
1180 
1181 static u8 _rtl_get_tx_hw_rate(struct ieee80211_hw *hw,
1182 			      struct ieee80211_tx_info *info)
1183 {
1184 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1185 	struct ieee80211_tx_rate *r = &info->status.rates[0];
1186 	struct ieee80211_rate *txrate;
1187 	u8 hw_value = 0x0;
1188 
1189 	if (r->flags & IEEE80211_TX_RC_MCS) {
1190 		/* HT MCS0-15 */
1191 		hw_value = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15] - 15 +
1192 			   r->idx;
1193 	} else if (r->flags & IEEE80211_TX_RC_VHT_MCS) {
1194 		/* VHT MCS0-9, NSS */
1195 		if (ieee80211_rate_get_vht_nss(r) == 2)
1196 			hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
1197 		else
1198 			hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
1199 
1200 		hw_value = hw_value - 9 + ieee80211_rate_get_vht_mcs(r);
1201 	} else {
1202 		/* legacy */
1203 		txrate = ieee80211_get_tx_rate(hw, info);
1204 
1205 		if (txrate)
1206 			hw_value = txrate->hw_value;
1207 	}
1208 
1209 	/* check 5G band */
1210 	if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G &&
1211 	    hw_value < rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M])
1212 		hw_value = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M];
1213 
1214 	return hw_value;
1215 }
1216 
1217 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
1218 		      struct ieee80211_tx_info *info,
1219 		      struct ieee80211_sta *sta,
1220 		      struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
1221 {
1222 #define SET_RATE_ID(rate_id)					\
1223 	({typeof(rate_id) _id = rate_id;			\
1224 	  ((rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) ?	\
1225 		rtl_mrate_idx_to_arfr_id(hw, _id,		\
1226 			(sta_entry ? sta_entry->wireless_mode :	\
1227 			 WIRELESS_MODE_G)) :			\
1228 		_id); })
1229 
1230 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1231 	struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1232 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1233 	struct rtl_sta_info *sta_entry =
1234 		(sta ? (struct rtl_sta_info *)sta->drv_priv : NULL);
1235 
1236 	__le16 fc = rtl_get_fc(skb);
1237 
1238 	tcb_desc->hw_rate = _rtl_get_tx_hw_rate(hw, info);
1239 
1240 	if (rtl_is_tx_report_skb(hw, skb))
1241 		tcb_desc->use_spe_rpt = 1;
1242 
1243 	if (ieee80211_is_data(fc)) {
1244 		/*
1245 		 *we set data rate INX 0
1246 		 *in rtl_rc.c   if skb is special data or
1247 		 *mgt which need low data rate.
1248 		 */
1249 
1250 		/*
1251 		 *So tcb_desc->hw_rate is just used for
1252 		 *special data and mgt frames
1253 		 */
1254 		if (info->control.rates[0].idx == 0 ||
1255 				ieee80211_is_nullfunc(fc)) {
1256 			tcb_desc->use_driver_rate = true;
1257 			tcb_desc->ratr_index =
1258 					SET_RATE_ID(RATR_INX_WIRELESS_MC);
1259 
1260 			tcb_desc->disable_ratefallback = 1;
1261 		} else {
1262 			/*
1263 			 *because hw will nerver use hw_rate
1264 			 *when tcb_desc->use_driver_rate = false
1265 			 *so we never set highest N rate here,
1266 			 *and N rate will all be controlled by FW
1267 			 *when tcb_desc->use_driver_rate = false
1268 			 */
1269 			if (sta && sta->vht_cap.vht_supported) {
1270 				tcb_desc->hw_rate =
1271 				_rtl_get_vht_highest_n_rate(hw, sta);
1272 			} else {
1273 				if (sta && sta->ht_cap.ht_supported) {
1274 					tcb_desc->hw_rate =
1275 						_rtl_get_highest_n_rate(hw, sta);
1276 				} else {
1277 					if (rtlmac->mode == WIRELESS_MODE_B) {
1278 						tcb_desc->hw_rate =
1279 						    rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
1280 					} else {
1281 						tcb_desc->hw_rate =
1282 						    rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
1283 					}
1284 				}
1285 			}
1286 		}
1287 
1288 		if (is_multicast_ether_addr(hdr->addr1))
1289 			tcb_desc->multicast = 1;
1290 		else if (is_broadcast_ether_addr(hdr->addr1))
1291 			tcb_desc->broadcast = 1;
1292 
1293 		_rtl_txrate_selectmode(hw, sta, tcb_desc);
1294 		_rtl_query_bandwidth_mode(hw, sta, tcb_desc);
1295 		_rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
1296 		_rtl_query_shortgi(hw, sta, tcb_desc, info);
1297 		_rtl_query_protection_mode(hw, tcb_desc, info);
1298 	} else {
1299 		tcb_desc->use_driver_rate = true;
1300 		tcb_desc->ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
1301 		tcb_desc->disable_ratefallback = 1;
1302 		tcb_desc->mac_id = 0;
1303 		tcb_desc->packet_bw = false;
1304 	}
1305 #undef SET_RATE_ID
1306 }
1307 EXPORT_SYMBOL(rtl_get_tcb_desc);
1308 
1309 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
1310 {
1311 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1312 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1313 	__le16 fc = rtl_get_fc(skb);
1314 
1315 	if (rtlpriv->dm.supp_phymode_switch &&
1316 	    mac->link_state < MAC80211_LINKED &&
1317 	    (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
1318 		if (rtlpriv->cfg->ops->chk_switch_dmdp)
1319 			rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1320 	}
1321 	if (ieee80211_is_auth(fc)) {
1322 		RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
1323 
1324 		mac->link_state = MAC80211_LINKING;
1325 		/* Dul mac */
1326 		rtlpriv->phy.need_iqk = true;
1327 
1328 	}
1329 
1330 	return true;
1331 }
1332 EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
1333 
1334 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
1335 				u8 *bssid, u16 tid);
1336 
1337 static void process_agg_start(struct ieee80211_hw *hw,
1338 			      struct ieee80211_hdr *hdr, u16 tid)
1339 {
1340 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1341 	struct ieee80211_rx_status rx_status = { 0 };
1342 	struct sk_buff *skb_delba = NULL;
1343 
1344 	skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
1345 	if (skb_delba) {
1346 		rx_status.freq = hw->conf.chandef.chan->center_freq;
1347 		rx_status.band = hw->conf.chandef.chan->band;
1348 		rx_status.flag |= RX_FLAG_DECRYPTED;
1349 		rx_status.flag |= RX_FLAG_MACTIME_START;
1350 		rx_status.rate_idx = 0;
1351 		rx_status.signal = 50 + 10;
1352 		memcpy(IEEE80211_SKB_RXCB(skb_delba),
1353 		       &rx_status, sizeof(rx_status));
1354 		RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1355 			      "fake del\n",
1356 			      skb_delba->data,
1357 			      skb_delba->len);
1358 		ieee80211_rx_irqsafe(hw, skb_delba);
1359 	}
1360 }
1361 
1362 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1363 {
1364 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1365 	struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1366 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1367 	__le16 fc = rtl_get_fc(skb);
1368 	u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
1369 	u8 category;
1370 
1371 	if (!ieee80211_is_action(fc))
1372 		return true;
1373 
1374 	category = *act;
1375 	act++;
1376 	switch (category) {
1377 	case ACT_CAT_BA:
1378 		switch (*act) {
1379 		case ACT_ADDBAREQ:
1380 			if (mac->act_scanning)
1381 				return false;
1382 
1383 			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1384 				"%s ACT_ADDBAREQ From :%pM\n",
1385 				is_tx ? "Tx" : "Rx", hdr->addr2);
1386 			RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1387 				skb->data, skb->len);
1388 			if (!is_tx) {
1389 				struct ieee80211_sta *sta = NULL;
1390 				struct rtl_sta_info *sta_entry = NULL;
1391 				struct rtl_tid_data *tid_data;
1392 				struct ieee80211_mgmt *mgmt = (void *)skb->data;
1393 				u16 capab = 0, tid = 0;
1394 
1395 				rcu_read_lock();
1396 				sta = rtl_find_sta(hw, hdr->addr3);
1397 				if (sta == NULL) {
1398 					RT_TRACE(rtlpriv, COMP_SEND | COMP_RECV,
1399 						 DBG_DMESG, "sta is NULL\n");
1400 					rcu_read_unlock();
1401 					return true;
1402 				}
1403 
1404 				sta_entry =
1405 					(struct rtl_sta_info *)sta->drv_priv;
1406 				if (!sta_entry) {
1407 					rcu_read_unlock();
1408 					return true;
1409 				}
1410 				capab =
1411 				  le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1412 				tid = (capab &
1413 				       IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1414 				if (tid >= MAX_TID_COUNT) {
1415 					rcu_read_unlock();
1416 					return true;
1417 				}
1418 				tid_data = &sta_entry->tids[tid];
1419 				if (tid_data->agg.rx_agg_state ==
1420 				    RTL_RX_AGG_START)
1421 					process_agg_start(hw, hdr, tid);
1422 				rcu_read_unlock();
1423 			}
1424 			break;
1425 		case ACT_ADDBARSP:
1426 			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1427 				 "%s ACT_ADDBARSP From :%pM\n",
1428 				  is_tx ? "Tx" : "Rx", hdr->addr2);
1429 			break;
1430 		case ACT_DELBA:
1431 			RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1432 				 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1433 			break;
1434 		}
1435 		break;
1436 	default:
1437 		break;
1438 	}
1439 
1440 	return true;
1441 }
1442 EXPORT_SYMBOL_GPL(rtl_action_proc);
1443 
1444 static void setup_special_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc,
1445 			     int type)
1446 {
1447 	struct ieee80211_hw *hw = rtlpriv->hw;
1448 
1449 	rtlpriv->ra.is_special_data = true;
1450 	if (rtlpriv->cfg->ops->get_btc_status())
1451 		rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1452 					rtlpriv, type);
1453 	rtl_lps_leave(hw);
1454 	ppsc->last_delaylps_stamp_jiffies = jiffies;
1455 }
1456 
1457 static const u8 *rtl_skb_ether_type_ptr(struct ieee80211_hw *hw,
1458 					struct sk_buff *skb, bool is_enc)
1459 {
1460 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1461 	u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1462 	u8 encrypt_header_len = 0;
1463 	u8 offset;
1464 
1465 	switch (rtlpriv->sec.pairwise_enc_algorithm) {
1466 	case WEP40_ENCRYPTION:
1467 	case WEP104_ENCRYPTION:
1468 		encrypt_header_len = 4;/*WEP_IV_LEN*/
1469 		break;
1470 	case TKIP_ENCRYPTION:
1471 		encrypt_header_len = 8;/*TKIP_IV_LEN*/
1472 		break;
1473 	case AESCCMP_ENCRYPTION:
1474 		encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
1475 		break;
1476 	default:
1477 		break;
1478 	}
1479 
1480 	offset = mac_hdr_len + SNAP_SIZE;
1481 	if (is_enc)
1482 		offset += encrypt_header_len;
1483 
1484 	return skb->data + offset;
1485 }
1486 
1487 /*should call before software enc*/
1488 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
1489 		       bool is_enc)
1490 {
1491 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1492 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1493 	__le16 fc = rtl_get_fc(skb);
1494 	u16 ether_type;
1495 	const u8 *ether_type_ptr;
1496 	const struct iphdr *ip;
1497 
1498 	if (!ieee80211_is_data(fc))
1499 		goto end;
1500 
1501 	ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, is_enc);
1502 	ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1503 
1504 	if (ETH_P_IP == ether_type) {
1505 		ip = (struct iphdr *)((u8 *)ether_type_ptr +
1506 		     PROTOC_TYPE_SIZE);
1507 		if (IPPROTO_UDP == ip->protocol) {
1508 			struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1509 							       (ip->ihl << 2));
1510 			if (((((u8 *)udp)[1] == 68) &&
1511 			     (((u8 *)udp)[3] == 67)) ||
1512 			    ((((u8 *)udp)[1] == 67) &&
1513 			     (((u8 *)udp)[3] == 68))) {
1514 				/* 68 : UDP BOOTP client
1515 				 * 67 : UDP BOOTP server
1516 				 */
1517 				RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1518 					 DBG_DMESG, "dhcp %s !!\n",
1519 					 (is_tx) ? "Tx" : "Rx");
1520 
1521 				if (is_tx)
1522 					setup_special_tx(rtlpriv, ppsc,
1523 							 PACKET_DHCP);
1524 
1525 				return true;
1526 			}
1527 		}
1528 	} else if (ETH_P_ARP == ether_type) {
1529 		if (is_tx)
1530 			setup_special_tx(rtlpriv, ppsc, PACKET_ARP);
1531 
1532 		return true;
1533 	} else if (ETH_P_PAE == ether_type) {
1534 		/* EAPOL is seens as in-4way */
1535 		rtlpriv->btcoexist.btc_info.in_4way = true;
1536 		rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
1537 
1538 		RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1539 			 "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
1540 
1541 		if (is_tx) {
1542 			rtlpriv->ra.is_special_data = true;
1543 			rtl_lps_leave(hw);
1544 			ppsc->last_delaylps_stamp_jiffies = jiffies;
1545 
1546 			setup_special_tx(rtlpriv, ppsc, PACKET_EAPOL);
1547 		}
1548 
1549 		return true;
1550 	} else if (ETH_P_IPV6 == ether_type) {
1551 		/* TODO: Handle any IPv6 cases that need special handling.
1552 		 * For now, always return false
1553 		 */
1554 		goto end;
1555 	}
1556 
1557 end:
1558 	rtlpriv->ra.is_special_data = false;
1559 	return false;
1560 }
1561 EXPORT_SYMBOL_GPL(rtl_is_special_data);
1562 
1563 void rtl_tx_ackqueue(struct ieee80211_hw *hw, struct sk_buff *skb)
1564 {
1565 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1566 	struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1567 
1568 	__skb_queue_tail(&tx_report->queue, skb);
1569 }
1570 EXPORT_SYMBOL_GPL(rtl_tx_ackqueue);
1571 
1572 static void rtl_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1573 			  bool ack)
1574 {
1575 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1576 	struct ieee80211_tx_info *info;
1577 
1578 	info = IEEE80211_SKB_CB(skb);
1579 	ieee80211_tx_info_clear_status(info);
1580 	if (ack) {
1581 		RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_LOUD,
1582 			 "tx report: ack\n");
1583 		info->flags |= IEEE80211_TX_STAT_ACK;
1584 	} else {
1585 		RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_LOUD,
1586 			 "tx report: not ack\n");
1587 		info->flags &= ~IEEE80211_TX_STAT_ACK;
1588 	}
1589 	ieee80211_tx_status_irqsafe(hw, skb);
1590 }
1591 
1592 bool rtl_is_tx_report_skb(struct ieee80211_hw *hw, struct sk_buff *skb)
1593 {
1594 	u16 ether_type;
1595 	const u8 *ether_type_ptr;
1596 	__le16 fc = rtl_get_fc(skb);
1597 
1598 	ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, true);
1599 	ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1600 
1601 	if (ether_type == ETH_P_PAE || ieee80211_is_nullfunc(fc))
1602 		return true;
1603 
1604 	return false;
1605 }
1606 
1607 static u16 rtl_get_tx_report_sn(struct ieee80211_hw *hw,
1608 				struct rtlwifi_tx_info *tx_info)
1609 {
1610 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1611 	struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1612 	u16 sn;
1613 
1614 	/* SW_DEFINE[11:8] are reserved (driver fills zeros)
1615 	 * SW_DEFINE[7:2] are used by driver
1616 	 * SW_DEFINE[1:0] are reserved for firmware (driver fills zeros)
1617 	 */
1618 	sn = (atomic_inc_return(&tx_report->sn) & 0x003F) << 2;
1619 
1620 	tx_report->last_sent_sn = sn;
1621 	tx_report->last_sent_time = jiffies;
1622 	tx_info->sn = sn;
1623 	tx_info->send_time = tx_report->last_sent_time;
1624 	RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1625 		 "Send TX-Report sn=0x%X\n", sn);
1626 
1627 	return sn;
1628 }
1629 
1630 void rtl_set_tx_report(struct rtl_tcb_desc *ptcb_desc, u8 *pdesc,
1631 		       struct ieee80211_hw *hw, struct rtlwifi_tx_info *tx_info)
1632 {
1633 	if (ptcb_desc->use_spe_rpt) {
1634 		u16 sn = rtl_get_tx_report_sn(hw, tx_info);
1635 
1636 		SET_TX_DESC_SPE_RPT(pdesc, 1);
1637 		SET_TX_DESC_SW_DEFINE(pdesc, sn);
1638 	}
1639 }
1640 EXPORT_SYMBOL_GPL(rtl_set_tx_report);
1641 
1642 void rtl_tx_report_handler(struct ieee80211_hw *hw, u8 *tmp_buf, u8 c2h_cmd_len)
1643 {
1644 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1645 	struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1646 	struct rtlwifi_tx_info *tx_info;
1647 	struct sk_buff_head *queue = &tx_report->queue;
1648 	struct sk_buff *skb;
1649 	u16 sn;
1650 	u8 st, retry;
1651 
1652 	if (rtlpriv->cfg->spec_ver & RTL_SPEC_EXT_C2H) {
1653 		sn = GET_TX_REPORT_SN_V2(tmp_buf);
1654 		st = GET_TX_REPORT_ST_V2(tmp_buf);
1655 		retry = GET_TX_REPORT_RETRY_V2(tmp_buf);
1656 	} else {
1657 		sn = GET_TX_REPORT_SN_V1(tmp_buf);
1658 		st = GET_TX_REPORT_ST_V1(tmp_buf);
1659 		retry = GET_TX_REPORT_RETRY_V1(tmp_buf);
1660 	}
1661 
1662 	tx_report->last_recv_sn = sn;
1663 
1664 	skb_queue_walk(queue, skb) {
1665 		tx_info = rtl_tx_skb_cb_info(skb);
1666 		if (tx_info->sn == sn) {
1667 			skb_unlink(skb, queue);
1668 			rtl_tx_status(hw, skb, st == 0);
1669 			break;
1670 		}
1671 	}
1672 	RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1673 		 "Recv TX-Report st=0x%02X sn=0x%X retry=0x%X\n",
1674 		 st, sn, retry);
1675 }
1676 EXPORT_SYMBOL_GPL(rtl_tx_report_handler);
1677 
1678 bool rtl_check_tx_report_acked(struct ieee80211_hw *hw)
1679 {
1680 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1681 	struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1682 
1683 	if (tx_report->last_sent_sn == tx_report->last_recv_sn)
1684 		return true;
1685 
1686 	if (time_before(tx_report->last_sent_time + 3 * HZ, jiffies)) {
1687 		RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_WARNING,
1688 			 "Check TX-Report timeout!! s_sn=0x%X r_sn=0x%X\n",
1689 			 tx_report->last_sent_sn, tx_report->last_recv_sn);
1690 		return true;	/* 3 sec. (timeout) seen as acked */
1691 	}
1692 
1693 	return false;
1694 }
1695 
1696 void rtl_wait_tx_report_acked(struct ieee80211_hw *hw, u32 wait_ms)
1697 {
1698 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1699 	int i;
1700 
1701 	for (i = 0; i < wait_ms; i++) {
1702 		if (rtl_check_tx_report_acked(hw))
1703 			break;
1704 		usleep_range(1000, 2000);
1705 		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1706 			 "Wait 1ms (%d/%d) to disable key.\n", i, wait_ms);
1707 	}
1708 }
1709 
1710 u32 rtl_get_hal_edca_param(struct ieee80211_hw *hw,
1711 			   struct ieee80211_vif *vif,
1712 			   enum wireless_mode wirelessmode,
1713 			   struct ieee80211_tx_queue_params *param)
1714 {
1715 	u32 reg = 0;
1716 	u8 sifstime = 10;
1717 	u8 slottime = 20;
1718 
1719 	/* AIFS = AIFSN * slot time + SIFS */
1720 	switch (wirelessmode) {
1721 	case WIRELESS_MODE_A:
1722 	case WIRELESS_MODE_N_24G:
1723 	case WIRELESS_MODE_N_5G:
1724 	case WIRELESS_MODE_AC_5G:
1725 	case WIRELESS_MODE_AC_24G:
1726 		sifstime = 16;
1727 		slottime = 9;
1728 		break;
1729 	case WIRELESS_MODE_G:
1730 		slottime = (vif->bss_conf.use_short_slot ? 9 : 20);
1731 		break;
1732 	default:
1733 		break;
1734 	}
1735 
1736 	reg |= (param->txop & 0x7FF) << 16;
1737 	reg |= (fls(param->cw_max) & 0xF) << 12;
1738 	reg |= (fls(param->cw_min) & 0xF) << 8;
1739 	reg |= (param->aifs & 0x0F) * slottime + sifstime;
1740 
1741 	return reg;
1742 }
1743 EXPORT_SYMBOL_GPL(rtl_get_hal_edca_param);
1744 
1745 /*********************************************************
1746  *
1747  * functions called by core.c
1748  *
1749  *********************************************************/
1750 int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1751 		     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1752 {
1753 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1754 	struct rtl_tid_data *tid_data;
1755 	struct rtl_sta_info *sta_entry = NULL;
1756 
1757 	if (sta == NULL)
1758 		return -EINVAL;
1759 
1760 	if (unlikely(tid >= MAX_TID_COUNT))
1761 		return -EINVAL;
1762 
1763 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1764 	if (!sta_entry)
1765 		return -ENXIO;
1766 	tid_data = &sta_entry->tids[tid];
1767 
1768 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1769 		 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1770 		 *ssn);
1771 
1772 	tid_data->agg.agg_state = RTL_AGG_START;
1773 
1774 	ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1775 	return 0;
1776 }
1777 
1778 int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1779 		    struct ieee80211_sta *sta, u16 tid)
1780 {
1781 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1782 	struct rtl_sta_info *sta_entry = NULL;
1783 
1784 	if (sta == NULL)
1785 		return -EINVAL;
1786 
1787 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1788 		 "on ra = %pM tid = %d\n", sta->addr, tid);
1789 
1790 	if (unlikely(tid >= MAX_TID_COUNT))
1791 		return -EINVAL;
1792 
1793 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1794 	sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1795 
1796 	ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1797 	return 0;
1798 }
1799 
1800 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1801 		     struct ieee80211_sta *sta, u16 tid)
1802 {
1803 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1804 	struct rtl_tid_data *tid_data;
1805 	struct rtl_sta_info *sta_entry = NULL;
1806 	u8 reject_agg;
1807 
1808 	if (sta == NULL)
1809 		return -EINVAL;
1810 
1811 	if (unlikely(tid >= MAX_TID_COUNT))
1812 		return -EINVAL;
1813 
1814 	if (rtlpriv->cfg->ops->get_btc_status()) {
1815 		rtlpriv->btcoexist.btc_ops->btc_get_ampdu_cfg(rtlpriv,
1816 							      &reject_agg,
1817 							      NULL, NULL);
1818 		if (reject_agg)
1819 			return -EINVAL;
1820 	}
1821 
1822 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1823 	if (!sta_entry)
1824 		return -ENXIO;
1825 	tid_data = &sta_entry->tids[tid];
1826 
1827 	RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1828 		 "on ra = %pM tid = %d\n", sta->addr, tid);
1829 
1830 	tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1831 	return 0;
1832 }
1833 
1834 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1835 		    struct ieee80211_sta *sta, u16 tid)
1836 {
1837 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1838 	struct rtl_sta_info *sta_entry = NULL;
1839 
1840 	if (sta == NULL)
1841 		return -EINVAL;
1842 
1843 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1844 		 "on ra = %pM tid = %d\n", sta->addr, tid);
1845 
1846 	if (unlikely(tid >= MAX_TID_COUNT))
1847 		return -EINVAL;
1848 
1849 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1850 	sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1851 
1852 	return 0;
1853 }
1854 
1855 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1856 		struct ieee80211_sta *sta, u16 tid)
1857 {
1858 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1859 	struct rtl_sta_info *sta_entry = NULL;
1860 
1861 	if (sta == NULL)
1862 		return -EINVAL;
1863 
1864 	RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1865 		 "on ra = %pM tid = %d\n", sta->addr, tid);
1866 
1867 	if (unlikely(tid >= MAX_TID_COUNT))
1868 		return -EINVAL;
1869 
1870 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1871 	sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1872 
1873 	return 0;
1874 }
1875 
1876 void rtl_rx_ampdu_apply(struct rtl_priv *rtlpriv)
1877 {
1878 	struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
1879 	u8 reject_agg = 0, ctrl_agg_size = 0, agg_size = 0;
1880 
1881 	if (rtlpriv->cfg->ops->get_btc_status())
1882 		btc_ops->btc_get_ampdu_cfg(rtlpriv, &reject_agg,
1883 					   &ctrl_agg_size, &agg_size);
1884 
1885 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
1886 		 "Set RX AMPDU: coex - reject=%d, ctrl_agg_size=%d, size=%d",
1887 		 reject_agg, ctrl_agg_size, agg_size);
1888 
1889 	rtlpriv->hw->max_rx_aggregation_subframes =
1890 		(ctrl_agg_size ? agg_size : IEEE80211_MAX_AMPDU_BUF_HT);
1891 }
1892 EXPORT_SYMBOL(rtl_rx_ampdu_apply);
1893 
1894 /*********************************************************
1895  *
1896  * wq & timer callback functions
1897  *
1898  *********************************************************/
1899 /* this function is used for roaming */
1900 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1901 {
1902 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1903 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1904 
1905 	if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1906 		return;
1907 
1908 	if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1909 		return;
1910 
1911 	/* check if this really is a beacon */
1912 	if (!ieee80211_is_beacon(hdr->frame_control) &&
1913 	    !ieee80211_is_probe_resp(hdr->frame_control))
1914 		return;
1915 
1916 	/* min. beacon length + FCS_LEN */
1917 	if (skb->len <= 40 + FCS_LEN)
1918 		return;
1919 
1920 	/* and only beacons from the associated BSSID, please */
1921 	if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1922 		return;
1923 
1924 	rtlpriv->link_info.bcn_rx_inperiod++;
1925 }
1926 EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
1927 
1928 static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw)
1929 {
1930 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1931 	struct rtl_bssid_entry *entry, *next;
1932 
1933 	list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1934 		list_del(&entry->list);
1935 		kfree(entry);
1936 		rtlpriv->scan_list.num--;
1937 	}
1938 }
1939 
1940 static void rtl_free_entries_from_ack_queue(struct ieee80211_hw *hw,
1941 					    bool chk_timeout)
1942 {
1943 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1944 	struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1945 	struct sk_buff_head *queue = &tx_report->queue;
1946 	struct sk_buff *skb, *tmp;
1947 	struct rtlwifi_tx_info *tx_info;
1948 
1949 	skb_queue_walk_safe(queue, skb, tmp) {
1950 		tx_info = rtl_tx_skb_cb_info(skb);
1951 		if (chk_timeout &&
1952 		    time_after(tx_info->send_time + HZ, jiffies))
1953 			continue;
1954 		skb_unlink(skb, queue);
1955 		rtl_tx_status(hw, skb, false);
1956 	}
1957 }
1958 
1959 void rtl_scan_list_expire(struct ieee80211_hw *hw)
1960 {
1961 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1962 	struct rtl_bssid_entry *entry, *next;
1963 	unsigned long flags;
1964 
1965 	spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
1966 
1967 	list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1968 		/* 180 seconds */
1969 		if (jiffies_to_msecs(jiffies - entry->age) < 180000)
1970 			continue;
1971 
1972 		list_del(&entry->list);
1973 		rtlpriv->scan_list.num--;
1974 
1975 		RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1976 			 "BSSID=%pM is expire in scan list (total=%d)\n",
1977 			 entry->bssid, rtlpriv->scan_list.num);
1978 		kfree(entry);
1979 	}
1980 
1981 	spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
1982 
1983 	rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1984 }
1985 
1986 void rtl_collect_scan_list(struct ieee80211_hw *hw, struct sk_buff *skb)
1987 {
1988 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1989 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1990 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1991 	unsigned long flags;
1992 
1993 	struct rtl_bssid_entry *entry;
1994 	bool entry_found = false;
1995 
1996 	/* check if it is scanning */
1997 	if (!mac->act_scanning)
1998 		return;
1999 
2000 	/* check if this really is a beacon */
2001 	if (!ieee80211_is_beacon(hdr->frame_control) &&
2002 	    !ieee80211_is_probe_resp(hdr->frame_control))
2003 		return;
2004 
2005 	spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
2006 
2007 	list_for_each_entry(entry, &rtlpriv->scan_list.list, list) {
2008 		if (memcmp(entry->bssid, hdr->addr3, ETH_ALEN) == 0) {
2009 			list_del_init(&entry->list);
2010 			entry_found = true;
2011 			RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
2012 				 "Update BSSID=%pM to scan list (total=%d)\n",
2013 				 hdr->addr3, rtlpriv->scan_list.num);
2014 			break;
2015 		}
2016 	}
2017 
2018 	if (!entry_found) {
2019 		entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2020 
2021 		if (!entry)
2022 			goto label_err;
2023 
2024 		memcpy(entry->bssid, hdr->addr3, ETH_ALEN);
2025 		rtlpriv->scan_list.num++;
2026 
2027 		RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
2028 			 "Add BSSID=%pM to scan list (total=%d)\n",
2029 			 hdr->addr3, rtlpriv->scan_list.num);
2030 	}
2031 
2032 	entry->age = jiffies;
2033 
2034 	list_add_tail(&entry->list, &rtlpriv->scan_list.list);
2035 
2036 label_err:
2037 	spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
2038 }
2039 EXPORT_SYMBOL(rtl_collect_scan_list);
2040 
2041 void rtl_watchdog_wq_callback(void *data)
2042 {
2043 	struct rtl_works *rtlworks = container_of_dwork_rtl(data,
2044 							    struct rtl_works,
2045 							    watchdog_wq);
2046 	struct ieee80211_hw *hw = rtlworks->hw;
2047 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2048 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2049 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2050 	bool busytraffic = false;
2051 	bool tx_busy_traffic = false;
2052 	bool rx_busy_traffic = false;
2053 	bool higher_busytraffic = false;
2054 	bool higher_busyrxtraffic = false;
2055 	u8 idx, tid;
2056 	u32 rx_cnt_inp4eriod = 0;
2057 	u32 tx_cnt_inp4eriod = 0;
2058 	u32 aver_rx_cnt_inperiod = 0;
2059 	u32 aver_tx_cnt_inperiod = 0;
2060 	u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
2061 	u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
2062 
2063 	if (is_hal_stop(rtlhal))
2064 		return;
2065 
2066 	/* <1> Determine if action frame is allowed */
2067 	if (mac->link_state > MAC80211_NOLINK) {
2068 		if (mac->cnt_after_linked < 20)
2069 			mac->cnt_after_linked++;
2070 	} else {
2071 		mac->cnt_after_linked = 0;
2072 	}
2073 
2074 	/* <2> to check if traffic busy, if
2075 	 * busytraffic we don't change channel
2076 	 */
2077 	if (mac->link_state >= MAC80211_LINKED) {
2078 		/* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
2079 		for (idx = 0; idx <= 2; idx++) {
2080 			rtlpriv->link_info.num_rx_in4period[idx] =
2081 			    rtlpriv->link_info.num_rx_in4period[idx + 1];
2082 			rtlpriv->link_info.num_tx_in4period[idx] =
2083 			    rtlpriv->link_info.num_tx_in4period[idx + 1];
2084 		}
2085 		rtlpriv->link_info.num_rx_in4period[3] =
2086 		    rtlpriv->link_info.num_rx_inperiod;
2087 		rtlpriv->link_info.num_tx_in4period[3] =
2088 		    rtlpriv->link_info.num_tx_inperiod;
2089 		for (idx = 0; idx <= 3; idx++) {
2090 			rx_cnt_inp4eriod +=
2091 			    rtlpriv->link_info.num_rx_in4period[idx];
2092 			tx_cnt_inp4eriod +=
2093 			    rtlpriv->link_info.num_tx_in4period[idx];
2094 		}
2095 		aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
2096 		aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
2097 
2098 		/* (2) check traffic busy */
2099 		if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
2100 			busytraffic = true;
2101 			if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
2102 				rx_busy_traffic = true;
2103 			else
2104 				tx_busy_traffic = false;
2105 		}
2106 
2107 		/* Higher Tx/Rx data. */
2108 		if (aver_rx_cnt_inperiod > 4000 ||
2109 		    aver_tx_cnt_inperiod > 4000) {
2110 			higher_busytraffic = true;
2111 
2112 			/* Extremely high Rx data. */
2113 			if (aver_rx_cnt_inperiod > 5000)
2114 				higher_busyrxtraffic = true;
2115 		}
2116 
2117 		/* check every tid's tx traffic */
2118 		for (tid = 0; tid <= 7; tid++) {
2119 			for (idx = 0; idx <= 2; idx++)
2120 				rtlpriv->link_info.tidtx_in4period[tid][idx] =
2121 					rtlpriv->link_info.tidtx_in4period[tid]
2122 					[idx + 1];
2123 			rtlpriv->link_info.tidtx_in4period[tid][3] =
2124 				rtlpriv->link_info.tidtx_inperiod[tid];
2125 
2126 			for (idx = 0; idx <= 3; idx++)
2127 				tidtx_inp4eriod[tid] +=
2128 				   rtlpriv->link_info.tidtx_in4period[tid][idx];
2129 			aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
2130 			if (aver_tidtx_inperiod[tid] > 5000)
2131 				rtlpriv->link_info.higher_busytxtraffic[tid] =
2132 									true;
2133 			else
2134 				rtlpriv->link_info.higher_busytxtraffic[tid] =
2135 									false;
2136 		}
2137 
2138 		/* PS is controlled by coex. */
2139 		if (rtlpriv->cfg->ops->get_btc_status() &&
2140 		    rtlpriv->btcoexist.btc_ops->btc_is_bt_ctrl_lps(rtlpriv))
2141 			goto label_lps_done;
2142 
2143 		if (rtlpriv->link_info.num_rx_inperiod +
2144 		      rtlpriv->link_info.num_tx_inperiod > 8 ||
2145 		    rtlpriv->link_info.num_rx_inperiod > 2)
2146 			rtl_lps_leave(hw);
2147 		else
2148 			rtl_lps_enter(hw);
2149 
2150 label_lps_done:
2151 		;
2152 	}
2153 
2154 	for (tid = 0; tid <= 7; tid++)
2155 		rtlpriv->link_info.tidtx_inperiod[tid] = 0;
2156 
2157 	rtlpriv->link_info.busytraffic = busytraffic;
2158 	rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
2159 	rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
2160 	rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
2161 	rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
2162 
2163 	rtlpriv->stats.txbytesunicast_inperiod =
2164 		rtlpriv->stats.txbytesunicast -
2165 		rtlpriv->stats.txbytesunicast_last;
2166 	rtlpriv->stats.rxbytesunicast_inperiod =
2167 		rtlpriv->stats.rxbytesunicast -
2168 		rtlpriv->stats.rxbytesunicast_last;
2169 	rtlpriv->stats.txbytesunicast_last = rtlpriv->stats.txbytesunicast;
2170 	rtlpriv->stats.rxbytesunicast_last = rtlpriv->stats.rxbytesunicast;
2171 
2172 	rtlpriv->stats.txbytesunicast_inperiod_tp =
2173 		(u32)(rtlpriv->stats.txbytesunicast_inperiod * 8 / 2 /
2174 		1024 / 1024);
2175 	rtlpriv->stats.rxbytesunicast_inperiod_tp =
2176 		(u32)(rtlpriv->stats.rxbytesunicast_inperiod * 8 / 2 /
2177 		1024 / 1024);
2178 
2179 	/* <3> DM */
2180 	if (!rtlpriv->cfg->mod_params->disable_watchdog)
2181 		rtlpriv->cfg->ops->dm_watchdog(hw);
2182 
2183 	/* <4> roaming */
2184 	if (mac->link_state == MAC80211_LINKED &&
2185 	    mac->opmode == NL80211_IFTYPE_STATION) {
2186 		if ((rtlpriv->link_info.bcn_rx_inperiod +
2187 		    rtlpriv->link_info.num_rx_inperiod) == 0) {
2188 			rtlpriv->link_info.roam_times++;
2189 			RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
2190 				 "AP off for %d s\n",
2191 				(rtlpriv->link_info.roam_times * 2));
2192 
2193 			/* if we can't recv beacon for 10s,
2194 			 * we should reconnect this AP
2195 			 */
2196 			if (rtlpriv->link_info.roam_times >= 5) {
2197 				pr_err("AP off, try to reconnect now\n");
2198 				rtlpriv->link_info.roam_times = 0;
2199 				ieee80211_connection_loss(
2200 					rtlpriv->mac80211.vif);
2201 			}
2202 		} else {
2203 			rtlpriv->link_info.roam_times = 0;
2204 		}
2205 	}
2206 
2207 	if (rtlpriv->cfg->ops->get_btc_status())
2208 		rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
2209 
2210 	if (rtlpriv->btcoexist.btc_info.in_4way) {
2211 		if (time_after(jiffies, rtlpriv->btcoexist.btc_info.in_4way_ts +
2212 			       msecs_to_jiffies(IN_4WAY_TIMEOUT_TIME)))
2213 			rtlpriv->btcoexist.btc_info.in_4way = false;
2214 	}
2215 
2216 	rtlpriv->link_info.num_rx_inperiod = 0;
2217 	rtlpriv->link_info.num_tx_inperiod = 0;
2218 	rtlpriv->link_info.bcn_rx_inperiod = 0;
2219 
2220 	/* <6> scan list */
2221 	rtl_scan_list_expire(hw);
2222 
2223 	/* <7> check ack queue */
2224 	rtl_free_entries_from_ack_queue(hw, true);
2225 }
2226 
2227 void rtl_watch_dog_timer_callback(struct timer_list *t)
2228 {
2229 	struct rtl_priv *rtlpriv = from_timer(rtlpriv, t, works.watchdog_timer);
2230 
2231 	queue_delayed_work(rtlpriv->works.rtl_wq,
2232 			   &rtlpriv->works.watchdog_wq, 0);
2233 
2234 	mod_timer(&rtlpriv->works.watchdog_timer,
2235 		  jiffies + MSECS(RTL_WATCH_DOG_TIME));
2236 }
2237 
2238 void rtl_fwevt_wq_callback(void *data)
2239 {
2240 	struct rtl_works *rtlworks =
2241 		container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
2242 	struct ieee80211_hw *hw = rtlworks->hw;
2243 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2244 
2245 	rtlpriv->cfg->ops->c2h_command_handle(hw);
2246 }
2247 
2248 static void rtl_c2h_content_parsing(struct ieee80211_hw *hw,
2249 				    struct sk_buff *skb);
2250 
2251 static bool rtl_c2h_fast_cmd(struct ieee80211_hw *hw, struct sk_buff *skb)
2252 {
2253 	u8 cmd_id = GET_C2H_CMD_ID(skb->data);
2254 
2255 	switch (cmd_id) {
2256 	case C2H_BT_MP:
2257 		return true;
2258 	default:
2259 		break;
2260 	}
2261 
2262 	return false;
2263 }
2264 
2265 void rtl_c2hcmd_enqueue(struct ieee80211_hw *hw, struct sk_buff *skb)
2266 {
2267 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2268 	unsigned long flags;
2269 
2270 	if (rtl_c2h_fast_cmd(hw, skb)) {
2271 		rtl_c2h_content_parsing(hw, skb);
2272 		kfree_skb(skb);
2273 		return;
2274 	}
2275 
2276 	/* enqueue */
2277 	spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
2278 
2279 	__skb_queue_tail(&rtlpriv->c2hcmd_queue, skb);
2280 
2281 	spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
2282 
2283 	/* wake up wq */
2284 	queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.c2hcmd_wq, 0);
2285 }
2286 EXPORT_SYMBOL(rtl_c2hcmd_enqueue);
2287 
2288 static void rtl_c2h_content_parsing(struct ieee80211_hw *hw,
2289 				    struct sk_buff *skb)
2290 {
2291 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2292 	struct rtl_hal_ops *hal_ops = rtlpriv->cfg->ops;
2293 	const struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
2294 	u8 cmd_id, cmd_len;
2295 	u8 *cmd_buf = NULL;
2296 
2297 	cmd_id = GET_C2H_CMD_ID(skb->data);
2298 	cmd_len = skb->len - C2H_DATA_OFFSET;
2299 	cmd_buf = GET_C2H_DATA_PTR(skb->data);
2300 
2301 	switch (cmd_id) {
2302 	case C2H_DBG:
2303 		RT_TRACE(rtlpriv, COMP_FW, DBG_LOUD, "[C2H], C2H_DBG!!\n");
2304 		break;
2305 	case C2H_TXBF:
2306 		RT_TRACE(rtlpriv, COMP_FW, DBG_TRACE,
2307 			 "[C2H], C2H_TXBF!!\n");
2308 		break;
2309 	case C2H_TX_REPORT:
2310 		rtl_tx_report_handler(hw, cmd_buf, cmd_len);
2311 		break;
2312 	case C2H_RA_RPT:
2313 		if (hal_ops->c2h_ra_report_handler)
2314 			hal_ops->c2h_ra_report_handler(hw, cmd_buf, cmd_len);
2315 		break;
2316 	case C2H_BT_INFO:
2317 		RT_TRACE(rtlpriv, COMP_FW, DBG_TRACE,
2318 			 "[C2H], C2H_BT_INFO!!\n");
2319 		if (rtlpriv->cfg->ops->get_btc_status())
2320 			btc_ops->btc_btinfo_notify(rtlpriv, cmd_buf, cmd_len);
2321 		break;
2322 	case C2H_BT_MP:
2323 		RT_TRACE(rtlpriv, COMP_FW, DBG_TRACE,
2324 			 "[C2H], C2H_BT_MP!!\n");
2325 		if (rtlpriv->cfg->ops->get_btc_status())
2326 			btc_ops->btc_btmpinfo_notify(rtlpriv, cmd_buf, cmd_len);
2327 		break;
2328 	default:
2329 		RT_TRACE(rtlpriv, COMP_FW, DBG_TRACE,
2330 			 "[C2H], Unknown packet!! cmd_id(%#X)!\n", cmd_id);
2331 		break;
2332 	}
2333 }
2334 
2335 void rtl_c2hcmd_launcher(struct ieee80211_hw *hw, int exec)
2336 {
2337 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2338 	struct sk_buff *skb;
2339 	unsigned long flags;
2340 	int i;
2341 
2342 	for (i = 0; i < 200; i++) {
2343 		/* dequeue a task */
2344 		spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
2345 
2346 		skb = __skb_dequeue(&rtlpriv->c2hcmd_queue);
2347 
2348 		spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
2349 
2350 		/* do it */
2351 		if (!skb)
2352 			break;
2353 
2354 		RT_TRACE(rtlpriv, COMP_FW, DBG_DMESG, "C2H rx_desc_shift=%d\n",
2355 			 *((u8 *)skb->cb));
2356 		RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_DMESG,
2357 			      "C2H data: ", skb->data, skb->len);
2358 
2359 		if (exec)
2360 			rtl_c2h_content_parsing(hw, skb);
2361 
2362 		/* free */
2363 		dev_kfree_skb_any(skb);
2364 	}
2365 }
2366 
2367 void rtl_c2hcmd_wq_callback(void *data)
2368 {
2369 	struct rtl_works *rtlworks = container_of_dwork_rtl(data,
2370 							    struct rtl_works,
2371 							    c2hcmd_wq);
2372 	struct ieee80211_hw *hw = rtlworks->hw;
2373 
2374 	rtl_c2hcmd_launcher(hw, 1);
2375 }
2376 
2377 void rtl_easy_concurrent_retrytimer_callback(struct timer_list *t)
2378 {
2379 	struct rtl_priv *rtlpriv =
2380 		from_timer(rtlpriv, t, works.dualmac_easyconcurrent_retrytimer);
2381 	struct ieee80211_hw *hw = rtlpriv->hw;
2382 	struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
2383 
2384 	if (buddy_priv == NULL)
2385 		return;
2386 
2387 	rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
2388 }
2389 
2390 /*********************************************************
2391  *
2392  * frame process functions
2393  *
2394  *********************************************************/
2395 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
2396 {
2397 	struct ieee80211_mgmt *mgmt = (void *)data;
2398 	u8 *pos, *end;
2399 
2400 	pos = (u8 *)mgmt->u.beacon.variable;
2401 	end = data + len;
2402 	while (pos < end) {
2403 		if (pos + 2 + pos[1] > end)
2404 			return NULL;
2405 
2406 		if (pos[0] == ie)
2407 			return pos;
2408 
2409 		pos += 2 + pos[1];
2410 	}
2411 	return NULL;
2412 }
2413 
2414 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
2415 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
2416 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
2417 				     enum ieee80211_smps_mode smps,
2418 				     u8 *da, u8 *bssid)
2419 {
2420 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2421 	struct sk_buff *skb;
2422 	struct ieee80211_mgmt *action_frame;
2423 
2424 	/* 27 = header + category + action + smps mode */
2425 	skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
2426 	if (!skb)
2427 		return NULL;
2428 
2429 	skb_reserve(skb, hw->extra_tx_headroom);
2430 	action_frame = skb_put_zero(skb, 27);
2431 	memcpy(action_frame->da, da, ETH_ALEN);
2432 	memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
2433 	memcpy(action_frame->bssid, bssid, ETH_ALEN);
2434 	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2435 						  IEEE80211_STYPE_ACTION);
2436 	action_frame->u.action.category = WLAN_CATEGORY_HT;
2437 	action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
2438 	switch (smps) {
2439 	case IEEE80211_SMPS_AUTOMATIC:/* 0 */
2440 	case IEEE80211_SMPS_NUM_MODES:/* 4 */
2441 		WARN_ON(1);
2442 	/* fall through */
2443 	case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
2444 		action_frame->u.action.u.ht_smps.smps_control =
2445 				WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
2446 		break;
2447 	case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
2448 		action_frame->u.action.u.ht_smps.smps_control =
2449 				WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
2450 		break;
2451 	case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
2452 		action_frame->u.action.u.ht_smps.smps_control =
2453 				WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
2454 		break;
2455 	}
2456 
2457 	return skb;
2458 }
2459 
2460 int rtl_send_smps_action(struct ieee80211_hw *hw,
2461 			 struct ieee80211_sta *sta,
2462 			 enum ieee80211_smps_mode smps)
2463 {
2464 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2465 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2466 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2467 	struct sk_buff *skb = NULL;
2468 	struct rtl_tcb_desc tcb_desc;
2469 	u8 bssid[ETH_ALEN] = {0};
2470 
2471 	memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
2472 
2473 	if (rtlpriv->mac80211.act_scanning)
2474 		goto err_free;
2475 
2476 	if (!sta)
2477 		goto err_free;
2478 
2479 	if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
2480 		goto err_free;
2481 
2482 	if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
2483 		goto err_free;
2484 
2485 	if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
2486 		memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
2487 	else
2488 		memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
2489 
2490 	skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
2491 	/* this is a type = mgmt * stype = action frame */
2492 	if (skb) {
2493 		struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2494 		struct rtl_sta_info *sta_entry =
2495 			(struct rtl_sta_info *) sta->drv_priv;
2496 		sta_entry->mimo_ps = smps;
2497 		/* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true); */
2498 
2499 		info->control.rates[0].idx = 0;
2500 		info->band = hw->conf.chandef.chan->band;
2501 		rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
2502 	}
2503 	return 1;
2504 
2505 err_free:
2506 	return 0;
2507 }
2508 EXPORT_SYMBOL(rtl_send_smps_action);
2509 
2510 void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
2511 {
2512 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2513 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2514 	enum io_type iotype;
2515 
2516 	if (!is_hal_stop(rtlhal)) {
2517 		switch (operation) {
2518 		case SCAN_OPT_BACKUP:
2519 			iotype = IO_CMD_PAUSE_DM_BY_SCAN;
2520 			rtlpriv->cfg->ops->set_hw_reg(hw,
2521 						      HW_VAR_IO_CMD,
2522 						      (u8 *)&iotype);
2523 			break;
2524 		case SCAN_OPT_RESTORE:
2525 			iotype = IO_CMD_RESUME_DM_BY_SCAN;
2526 			rtlpriv->cfg->ops->set_hw_reg(hw,
2527 						      HW_VAR_IO_CMD,
2528 						      (u8 *)&iotype);
2529 			break;
2530 		default:
2531 			pr_err("Unknown Scan Backup operation.\n");
2532 			break;
2533 		}
2534 	}
2535 }
2536 EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
2537 
2538 /* because mac80211 have issues when can receive del ba
2539  * so here we just make a fake del_ba if we receive a ba_req
2540  * but rx_agg was opened to let mac80211 release some ba
2541  * related resources, so please this del_ba for tx
2542  */
2543 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
2544 				u8 *sa, u8 *bssid, u16 tid)
2545 {
2546 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2547 	struct sk_buff *skb;
2548 	struct ieee80211_mgmt *action_frame;
2549 	u16 params;
2550 
2551 	/* 27 = header + category + action + smps mode */
2552 	skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
2553 	if (!skb)
2554 		return NULL;
2555 
2556 	skb_reserve(skb, hw->extra_tx_headroom);
2557 	action_frame = skb_put_zero(skb, 34);
2558 	memcpy(action_frame->sa, sa, ETH_ALEN);
2559 	memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
2560 	memcpy(action_frame->bssid, bssid, ETH_ALEN);
2561 	action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2562 						  IEEE80211_STYPE_ACTION);
2563 	action_frame->u.action.category = WLAN_CATEGORY_BACK;
2564 	action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
2565 	params = (u16)(1 << 11);	/* bit 11 initiator */
2566 	params |= (u16)(tid << 12);	/* bit 15:12 TID number */
2567 
2568 	action_frame->u.action.u.delba.params = cpu_to_le16(params);
2569 	action_frame->u.action.u.delba.reason_code =
2570 		cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
2571 
2572 	return skb;
2573 }
2574 
2575 /*********************************************************
2576  *
2577  * IOT functions
2578  *
2579  *********************************************************/
2580 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
2581 				  struct octet_string vendor_ie)
2582 {
2583 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2584 	bool matched = false;
2585 	static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
2586 	static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
2587 	static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
2588 	static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
2589 	static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
2590 	static u8 racap[] = { 0x00, 0x0c, 0x43 };
2591 	static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
2592 	static u8 marvcap[] = { 0x00, 0x50, 0x43 };
2593 
2594 	if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
2595 		memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
2596 		rtlpriv->mac80211.vendor = PEER_ATH;
2597 		matched = true;
2598 	} else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
2599 		memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
2600 		memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
2601 		rtlpriv->mac80211.vendor = PEER_BROAD;
2602 		matched = true;
2603 	} else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
2604 		rtlpriv->mac80211.vendor = PEER_RAL;
2605 		matched = true;
2606 	} else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
2607 		rtlpriv->mac80211.vendor = PEER_CISCO;
2608 		matched = true;
2609 	} else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
2610 		rtlpriv->mac80211.vendor = PEER_MARV;
2611 		matched = true;
2612 	}
2613 
2614 	return matched;
2615 }
2616 
2617 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
2618 		unsigned int len)
2619 {
2620 	struct ieee80211_mgmt *mgmt = (void *)data;
2621 	struct octet_string vendor_ie;
2622 	u8 *pos, *end;
2623 
2624 	pos = (u8 *)mgmt->u.beacon.variable;
2625 	end = data + len;
2626 	while (pos < end) {
2627 		if (pos[0] == 221) {
2628 			vendor_ie.length = pos[1];
2629 			vendor_ie.octet = &pos[2];
2630 			if (rtl_chk_vendor_ouisub(hw, vendor_ie))
2631 				return true;
2632 		}
2633 
2634 		if (pos + 2 + pos[1] > end)
2635 			return false;
2636 
2637 		pos += 2 + pos[1];
2638 	}
2639 	return false;
2640 }
2641 
2642 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
2643 {
2644 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2645 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2646 	struct ieee80211_hdr *hdr = (void *)data;
2647 	u32 vendor = PEER_UNKNOWN;
2648 
2649 	static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
2650 	static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
2651 	static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
2652 	static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
2653 	static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
2654 	static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
2655 	static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
2656 	static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
2657 	static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
2658 	static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
2659 	static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
2660 	static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
2661 	static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
2662 	static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
2663 	static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
2664 	static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
2665 
2666 	if (mac->opmode != NL80211_IFTYPE_STATION)
2667 		return;
2668 
2669 	if (mac->link_state == MAC80211_NOLINK) {
2670 		mac->vendor = PEER_UNKNOWN;
2671 		return;
2672 	}
2673 
2674 	if (mac->cnt_after_linked > 2)
2675 		return;
2676 
2677 	/* check if this really is a beacon */
2678 	if (!ieee80211_is_beacon(hdr->frame_control))
2679 		return;
2680 
2681 	/* min. beacon length + FCS_LEN */
2682 	if (len <= 40 + FCS_LEN)
2683 		return;
2684 
2685 	/* and only beacons from the associated BSSID, please */
2686 	if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
2687 		return;
2688 
2689 	if (rtl_find_221_ie(hw, data, len))
2690 		vendor = mac->vendor;
2691 
2692 	if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
2693 		(memcmp(mac->bssid, ap5_2, 3) == 0) ||
2694 		(memcmp(mac->bssid, ap5_3, 3) == 0) ||
2695 		(memcmp(mac->bssid, ap5_4, 3) == 0) ||
2696 		(memcmp(mac->bssid, ap5_5, 3) == 0) ||
2697 		(memcmp(mac->bssid, ap5_6, 3) == 0) ||
2698 		vendor == PEER_ATH) {
2699 		vendor = PEER_ATH;
2700 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
2701 	} else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
2702 		(memcmp(mac->bssid, ap4_5, 3) == 0) ||
2703 		(memcmp(mac->bssid, ap4_1, 3) == 0) ||
2704 		(memcmp(mac->bssid, ap4_2, 3) == 0) ||
2705 		(memcmp(mac->bssid, ap4_3, 3) == 0) ||
2706 		vendor == PEER_RAL) {
2707 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
2708 		vendor = PEER_RAL;
2709 	} else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
2710 		vendor == PEER_CISCO) {
2711 		vendor = PEER_CISCO;
2712 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
2713 	} else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
2714 		(memcmp(mac->bssid, ap3_2, 3) == 0) ||
2715 		(memcmp(mac->bssid, ap3_3, 3) == 0) ||
2716 		vendor == PEER_BROAD) {
2717 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
2718 		vendor = PEER_BROAD;
2719 	} else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
2720 		vendor == PEER_MARV) {
2721 		vendor = PEER_MARV;
2722 		RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
2723 	}
2724 
2725 	mac->vendor = vendor;
2726 }
2727 EXPORT_SYMBOL_GPL(rtl_recognize_peer);
2728 
2729 MODULE_AUTHOR("lizhaoming	<chaoming_li@realsil.com.cn>");
2730 MODULE_AUTHOR("Realtek WlanFAE	<wlanfae@realtek.com>");
2731 MODULE_AUTHOR("Larry Finger	<Larry.FInger@lwfinger.net>");
2732 MODULE_LICENSE("GPL");
2733 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2734 
2735 struct rtl_global_var rtl_global_var = {};
2736 EXPORT_SYMBOL_GPL(rtl_global_var);
2737 
2738 static int __init rtl_core_module_init(void)
2739 {
2740 	BUILD_BUG_ON(TX_PWR_BY_RATE_NUM_RATE < TX_PWR_BY_RATE_NUM_SECTION);
2741 	BUILD_BUG_ON(MAX_RATE_SECTION_NUM != MAX_RATE_SECTION);
2742 	BUILD_BUG_ON(MAX_BASE_NUM_IN_PHY_REG_PG_24G != MAX_RATE_SECTION);
2743 	BUILD_BUG_ON(MAX_BASE_NUM_IN_PHY_REG_PG_5G != (MAX_RATE_SECTION - 1));
2744 
2745 	if (rtl_rate_control_register())
2746 		pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
2747 
2748 	/* add debugfs */
2749 	rtl_debugfs_add_topdir();
2750 
2751 	/* init some global vars */
2752 	INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2753 	spin_lock_init(&rtl_global_var.glb_list_lock);
2754 
2755 	return 0;
2756 }
2757 
2758 static void __exit rtl_core_module_exit(void)
2759 {
2760 	/*RC*/
2761 	rtl_rate_control_unregister();
2762 
2763 	/* remove debugfs */
2764 	rtl_debugfs_remove_topdir();
2765 }
2766 
2767 module_init(rtl_core_module_init);
2768 module_exit(rtl_core_module_exit);
2769