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