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