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