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