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