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