Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2009-2012  Realtek Corporation.*/
0003 
0004 #include "wifi.h"
0005 #include "rc.h"
0006 #include "base.h"
0007 #include "efuse.h"
0008 #include "cam.h"
0009 #include "ps.h"
0010 #include "regd.h"
0011 #include "pci.h"
0012 #include <linux/ip.h>
0013 #include <linux/module.h>
0014 #include <linux/udp.h>
0015 
0016 /*
0017  *NOTICE!!!: This file will be very big, we should
0018  *keep it clear under following roles:
0019  *
0020  *This file include following parts, so, if you add new
0021  *functions into this file, please check which part it
0022  *should includes. or check if you should add new part
0023  *for this file:
0024  *
0025  *1) mac80211 init functions
0026  *2) tx information functions
0027  *3) functions called by core.c
0028  *4) wq & timer callback functions
0029  *5) frame process functions
0030  *6) IOT functions
0031  *7) sysfs functions
0032  *8) vif functions
0033  *9) ...
0034  */
0035 
0036 /*********************************************************
0037  *
0038  * mac80211 init functions
0039  *
0040  *********************************************************/
0041 static struct ieee80211_channel rtl_channeltable_2g[] = {
0042     {.center_freq = 2412, .hw_value = 1,},
0043     {.center_freq = 2417, .hw_value = 2,},
0044     {.center_freq = 2422, .hw_value = 3,},
0045     {.center_freq = 2427, .hw_value = 4,},
0046     {.center_freq = 2432, .hw_value = 5,},
0047     {.center_freq = 2437, .hw_value = 6,},
0048     {.center_freq = 2442, .hw_value = 7,},
0049     {.center_freq = 2447, .hw_value = 8,},
0050     {.center_freq = 2452, .hw_value = 9,},
0051     {.center_freq = 2457, .hw_value = 10,},
0052     {.center_freq = 2462, .hw_value = 11,},
0053     {.center_freq = 2467, .hw_value = 12,},
0054     {.center_freq = 2472, .hw_value = 13,},
0055     {.center_freq = 2484, .hw_value = 14,},
0056 };
0057 
0058 static struct ieee80211_channel rtl_channeltable_5g[] = {
0059     {.center_freq = 5180, .hw_value = 36,},
0060     {.center_freq = 5200, .hw_value = 40,},
0061     {.center_freq = 5220, .hw_value = 44,},
0062     {.center_freq = 5240, .hw_value = 48,},
0063     {.center_freq = 5260, .hw_value = 52,},
0064     {.center_freq = 5280, .hw_value = 56,},
0065     {.center_freq = 5300, .hw_value = 60,},
0066     {.center_freq = 5320, .hw_value = 64,},
0067     {.center_freq = 5500, .hw_value = 100,},
0068     {.center_freq = 5520, .hw_value = 104,},
0069     {.center_freq = 5540, .hw_value = 108,},
0070     {.center_freq = 5560, .hw_value = 112,},
0071     {.center_freq = 5580, .hw_value = 116,},
0072     {.center_freq = 5600, .hw_value = 120,},
0073     {.center_freq = 5620, .hw_value = 124,},
0074     {.center_freq = 5640, .hw_value = 128,},
0075     {.center_freq = 5660, .hw_value = 132,},
0076     {.center_freq = 5680, .hw_value = 136,},
0077     {.center_freq = 5700, .hw_value = 140,},
0078     {.center_freq = 5745, .hw_value = 149,},
0079     {.center_freq = 5765, .hw_value = 153,},
0080     {.center_freq = 5785, .hw_value = 157,},
0081     {.center_freq = 5805, .hw_value = 161,},
0082     {.center_freq = 5825, .hw_value = 165,},
0083 };
0084 
0085 static struct ieee80211_rate rtl_ratetable_2g[] = {
0086     {.bitrate = 10, .hw_value = 0x00,},
0087     {.bitrate = 20, .hw_value = 0x01,},
0088     {.bitrate = 55, .hw_value = 0x02,},
0089     {.bitrate = 110, .hw_value = 0x03,},
0090     {.bitrate = 60, .hw_value = 0x04,},
0091     {.bitrate = 90, .hw_value = 0x05,},
0092     {.bitrate = 120, .hw_value = 0x06,},
0093     {.bitrate = 180, .hw_value = 0x07,},
0094     {.bitrate = 240, .hw_value = 0x08,},
0095     {.bitrate = 360, .hw_value = 0x09,},
0096     {.bitrate = 480, .hw_value = 0x0a,},
0097     {.bitrate = 540, .hw_value = 0x0b,},
0098 };
0099 
0100 static struct ieee80211_rate rtl_ratetable_5g[] = {
0101     {.bitrate = 60, .hw_value = 0x04,},
0102     {.bitrate = 90, .hw_value = 0x05,},
0103     {.bitrate = 120, .hw_value = 0x06,},
0104     {.bitrate = 180, .hw_value = 0x07,},
0105     {.bitrate = 240, .hw_value = 0x08,},
0106     {.bitrate = 360, .hw_value = 0x09,},
0107     {.bitrate = 480, .hw_value = 0x0a,},
0108     {.bitrate = 540, .hw_value = 0x0b,},
0109 };
0110 
0111 static const struct ieee80211_supported_band rtl_band_2ghz = {
0112     .band = NL80211_BAND_2GHZ,
0113 
0114     .channels = rtl_channeltable_2g,
0115     .n_channels = ARRAY_SIZE(rtl_channeltable_2g),
0116 
0117     .bitrates = rtl_ratetable_2g,
0118     .n_bitrates = ARRAY_SIZE(rtl_ratetable_2g),
0119 
0120     .ht_cap = {0},
0121 };
0122 
0123 static struct ieee80211_supported_band rtl_band_5ghz = {
0124     .band = NL80211_BAND_5GHZ,
0125 
0126     .channels = rtl_channeltable_5g,
0127     .n_channels = ARRAY_SIZE(rtl_channeltable_5g),
0128 
0129     .bitrates = rtl_ratetable_5g,
0130     .n_bitrates = ARRAY_SIZE(rtl_ratetable_5g),
0131 
0132     .ht_cap = {0},
0133 };
0134 
0135 static const u8 tid_to_ac[] = {
0136     2, /* IEEE80211_AC_BE */
0137     3, /* IEEE80211_AC_BK */
0138     3, /* IEEE80211_AC_BK */
0139     2, /* IEEE80211_AC_BE */
0140     1, /* IEEE80211_AC_VI */
0141     1, /* IEEE80211_AC_VI */
0142     0, /* IEEE80211_AC_VO */
0143     0, /* IEEE80211_AC_VO */
0144 };
0145 
0146 u8 rtl_tid_to_ac(u8 tid)
0147 {
0148     return tid_to_ac[tid];
0149 }
0150 EXPORT_SYMBOL_GPL(rtl_tid_to_ac);
0151 
0152 static void _rtl_init_hw_ht_capab(struct ieee80211_hw *hw,
0153                   struct ieee80211_sta_ht_cap *ht_cap)
0154 {
0155     struct rtl_priv *rtlpriv = rtl_priv(hw);
0156     struct rtl_phy *rtlphy = &(rtlpriv->phy);
0157 
0158     ht_cap->ht_supported = true;
0159     ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
0160         IEEE80211_HT_CAP_SGI_40 |
0161         IEEE80211_HT_CAP_SGI_20 |
0162         IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
0163 
0164     if (rtlpriv->rtlhal.disable_amsdu_8k)
0165         ht_cap->cap &= ~IEEE80211_HT_CAP_MAX_AMSDU;
0166 
0167     /*
0168      *Maximum length of AMPDU that the STA can receive.
0169      *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
0170      */
0171     ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
0172 
0173     /*Minimum MPDU start spacing , */
0174     ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
0175 
0176     ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
0177 
0178     /*hw->wiphy->bands[NL80211_BAND_2GHZ]
0179      *base on ant_num
0180      *rx_mask: RX mask
0181      *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
0182      *if rx_ant = 2 rx_mask[1]= 0xff;==>MCS8-MCS15
0183      *if rx_ant >= 3 rx_mask[2]= 0xff;
0184      *if BW_40 rx_mask[4]= 0x01;
0185      *highest supported RX rate
0186      */
0187     if (rtlpriv->dm.supp_phymode_switch) {
0188         pr_info("Support phy mode switch\n");
0189 
0190         ht_cap->mcs.rx_mask[0] = 0xFF;
0191         ht_cap->mcs.rx_mask[1] = 0xFF;
0192         ht_cap->mcs.rx_mask[4] = 0x01;
0193 
0194         ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
0195     } else {
0196         if (get_rf_type(rtlphy) == RF_1T2R ||
0197             get_rf_type(rtlphy) == RF_2T2R) {
0198             rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
0199                 "1T2R or 2T2R\n");
0200             ht_cap->mcs.rx_mask[0] = 0xFF;
0201             ht_cap->mcs.rx_mask[1] = 0xFF;
0202             ht_cap->mcs.rx_mask[4] = 0x01;
0203 
0204             ht_cap->mcs.rx_highest =
0205                  cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS15);
0206         } else if (get_rf_type(rtlphy) == RF_1T1R) {
0207             rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG, "1T1R\n");
0208 
0209             ht_cap->mcs.rx_mask[0] = 0xFF;
0210             ht_cap->mcs.rx_mask[1] = 0x00;
0211             ht_cap->mcs.rx_mask[4] = 0x01;
0212 
0213             ht_cap->mcs.rx_highest =
0214                  cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
0215         }
0216     }
0217 }
0218 
0219 static void _rtl_init_hw_vht_capab(struct ieee80211_hw *hw,
0220                    struct ieee80211_sta_vht_cap *vht_cap)
0221 {
0222     struct rtl_priv *rtlpriv = rtl_priv(hw);
0223     struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
0224 
0225     if (!(rtlpriv->cfg->spec_ver & RTL_SPEC_SUPPORT_VHT))
0226         return;
0227 
0228     if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE ||
0229         rtlhal->hw_type == HARDWARE_TYPE_RTL8822BE) {
0230         u16 mcs_map;
0231 
0232         vht_cap->vht_supported = true;
0233         vht_cap->cap =
0234             IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
0235             IEEE80211_VHT_CAP_SHORT_GI_80 |
0236             IEEE80211_VHT_CAP_TXSTBC |
0237             IEEE80211_VHT_CAP_RXSTBC_1 |
0238             IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
0239             IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
0240             IEEE80211_VHT_CAP_HTC_VHT |
0241             IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
0242             IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
0243             IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
0244             0;
0245 
0246         mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
0247             IEEE80211_VHT_MCS_SUPPORT_0_9 << 2 |
0248             IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
0249             IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
0250             IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
0251             IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
0252             IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
0253             IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
0254 
0255         vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
0256         vht_cap->vht_mcs.rx_highest =
0257             cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
0258         vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
0259         vht_cap->vht_mcs.tx_highest =
0260             cpu_to_le16(MAX_BIT_RATE_SHORT_GI_2NSS_80MHZ_MCS9);
0261     } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
0262         u16 mcs_map;
0263 
0264         vht_cap->vht_supported = true;
0265         vht_cap->cap =
0266             IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
0267             IEEE80211_VHT_CAP_SHORT_GI_80 |
0268             IEEE80211_VHT_CAP_TXSTBC |
0269             IEEE80211_VHT_CAP_RXSTBC_1 |
0270             IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
0271             IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
0272             IEEE80211_VHT_CAP_HTC_VHT |
0273             IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK |
0274             IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN |
0275             IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN |
0276             0;
0277 
0278         mcs_map = IEEE80211_VHT_MCS_SUPPORT_0_9 << 0 |
0279             IEEE80211_VHT_MCS_NOT_SUPPORTED << 2 |
0280             IEEE80211_VHT_MCS_NOT_SUPPORTED << 4 |
0281             IEEE80211_VHT_MCS_NOT_SUPPORTED << 6 |
0282             IEEE80211_VHT_MCS_NOT_SUPPORTED << 8 |
0283             IEEE80211_VHT_MCS_NOT_SUPPORTED << 10 |
0284             IEEE80211_VHT_MCS_NOT_SUPPORTED << 12 |
0285             IEEE80211_VHT_MCS_NOT_SUPPORTED << 14;
0286 
0287         vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
0288         vht_cap->vht_mcs.rx_highest =
0289             cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
0290         vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
0291         vht_cap->vht_mcs.tx_highest =
0292             cpu_to_le16(MAX_BIT_RATE_SHORT_GI_1NSS_80MHZ_MCS9);
0293     }
0294 }
0295 
0296 static void _rtl_init_mac80211(struct ieee80211_hw *hw)
0297 {
0298     struct rtl_priv *rtlpriv = rtl_priv(hw);
0299     struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
0300     struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
0301     struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
0302     struct ieee80211_supported_band *sband;
0303 
0304     if (rtlhal->macphymode == SINGLEMAC_SINGLEPHY &&
0305         rtlhal->bandset == BAND_ON_BOTH) {
0306         /* 1: 2.4 G bands */
0307         /* <1> use  mac->bands as mem for hw->wiphy->bands */
0308         sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
0309 
0310         /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
0311          * to default value(1T1R) */
0312         memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]), &rtl_band_2ghz,
0313                 sizeof(struct ieee80211_supported_band));
0314 
0315         /* <3> init ht cap base on ant_num */
0316         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
0317 
0318         /* <4> set mac->sband to wiphy->sband */
0319         hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
0320 
0321         /* 2: 5 G bands */
0322         /* <1> use  mac->bands as mem for hw->wiphy->bands */
0323         sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
0324 
0325         /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
0326          * to default value(1T1R) */
0327         memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]), &rtl_band_5ghz,
0328                 sizeof(struct ieee80211_supported_band));
0329 
0330         /* <3> init ht cap base on ant_num */
0331         _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
0332 
0333         _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
0334         /* <4> set mac->sband to wiphy->sband */
0335         hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
0336     } else {
0337         if (rtlhal->current_bandtype == BAND_ON_2_4G) {
0338             /* <1> use  mac->bands as mem for hw->wiphy->bands */
0339             sband = &(rtlmac->bands[NL80211_BAND_2GHZ]);
0340 
0341             /* <2> set hw->wiphy->bands[NL80211_BAND_2GHZ]
0342              * to default value(1T1R) */
0343             memcpy(&(rtlmac->bands[NL80211_BAND_2GHZ]),
0344                    &rtl_band_2ghz,
0345                    sizeof(struct ieee80211_supported_band));
0346 
0347             /* <3> init ht cap base on ant_num */
0348             _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
0349 
0350             /* <4> set mac->sband to wiphy->sband */
0351             hw->wiphy->bands[NL80211_BAND_2GHZ] = sband;
0352         } else if (rtlhal->current_bandtype == BAND_ON_5G) {
0353             /* <1> use  mac->bands as mem for hw->wiphy->bands */
0354             sband = &(rtlmac->bands[NL80211_BAND_5GHZ]);
0355 
0356             /* <2> set hw->wiphy->bands[NL80211_BAND_5GHZ]
0357              * to default value(1T1R) */
0358             memcpy(&(rtlmac->bands[NL80211_BAND_5GHZ]),
0359                    &rtl_band_5ghz,
0360                    sizeof(struct ieee80211_supported_band));
0361 
0362             /* <3> init ht cap base on ant_num */
0363             _rtl_init_hw_ht_capab(hw, &sband->ht_cap);
0364 
0365             _rtl_init_hw_vht_capab(hw, &sband->vht_cap);
0366             /* <4> set mac->sband to wiphy->sband */
0367             hw->wiphy->bands[NL80211_BAND_5GHZ] = sband;
0368         } else {
0369             pr_err("Err BAND %d\n",
0370                    rtlhal->current_bandtype);
0371         }
0372     }
0373     /* <5> set hw caps */
0374     ieee80211_hw_set(hw, SIGNAL_DBM);
0375     ieee80211_hw_set(hw, RX_INCLUDES_FCS);
0376     ieee80211_hw_set(hw, AMPDU_AGGREGATION);
0377     ieee80211_hw_set(hw, MFP_CAPABLE);
0378     ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
0379     ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
0380     ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
0381 
0382     /* swlps or hwlps has been set in diff chip in init_sw_vars */
0383     if (rtlpriv->psc.swctrl_lps) {
0384         ieee80211_hw_set(hw, SUPPORTS_PS);
0385         ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
0386     }
0387     if (rtlpriv->psc.fwctrl_lps) {
0388         ieee80211_hw_set(hw, SUPPORTS_PS);
0389         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
0390     }
0391     hw->wiphy->interface_modes =
0392         BIT(NL80211_IFTYPE_AP) |
0393         BIT(NL80211_IFTYPE_STATION) |
0394         BIT(NL80211_IFTYPE_ADHOC) |
0395         BIT(NL80211_IFTYPE_MESH_POINT) |
0396         BIT(NL80211_IFTYPE_P2P_CLIENT) |
0397         BIT(NL80211_IFTYPE_P2P_GO);
0398     hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
0399 
0400     hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
0401 
0402     hw->wiphy->rts_threshold = 2347;
0403 
0404     hw->queues = AC_MAX;
0405     hw->extra_tx_headroom = RTL_TX_HEADER_SIZE;
0406 
0407     /* TODO: Correct this value for our hw */
0408     hw->max_listen_interval = MAX_LISTEN_INTERVAL;
0409     hw->max_rate_tries = MAX_RATE_TRIES;
0410     /* hw->max_rates = 1; */
0411     hw->sta_data_size = sizeof(struct rtl_sta_info);
0412 
0413 /* wowlan is not supported by kernel if CONFIG_PM is not defined */
0414 #ifdef CONFIG_PM
0415     if (rtlpriv->psc.wo_wlan_mode) {
0416         if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_MAGIC_PACKET)
0417             rtlpriv->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT;
0418         if (rtlpriv->psc.wo_wlan_mode & WAKE_ON_PATTERN_MATCH) {
0419             rtlpriv->wowlan.n_patterns =
0420                 MAX_SUPPORT_WOL_PATTERN_NUM;
0421             rtlpriv->wowlan.pattern_min_len = MIN_WOL_PATTERN_SIZE;
0422             rtlpriv->wowlan.pattern_max_len = MAX_WOL_PATTERN_SIZE;
0423         }
0424         hw->wiphy->wowlan = &rtlpriv->wowlan;
0425     }
0426 #endif
0427 
0428     /* <6> mac address */
0429     if (is_valid_ether_addr(rtlefuse->dev_addr)) {
0430         SET_IEEE80211_PERM_ADDR(hw, rtlefuse->dev_addr);
0431     } else {
0432         u8 rtlmac1[] = { 0x00, 0xe0, 0x4c, 0x81, 0x92, 0x00 };
0433 
0434         get_random_bytes((rtlmac1 + (ETH_ALEN - 1)), 1);
0435         SET_IEEE80211_PERM_ADDR(hw, rtlmac1);
0436     }
0437 }
0438 
0439 static void rtl_watchdog_wq_callback(struct work_struct *work);
0440 static void rtl_fwevt_wq_callback(struct work_struct *work);
0441 static void rtl_c2hcmd_wq_callback(struct work_struct *work);
0442 
0443 static int _rtl_init_deferred_work(struct ieee80211_hw *hw)
0444 {
0445     struct rtl_priv *rtlpriv = rtl_priv(hw);
0446     struct workqueue_struct *wq;
0447 
0448     wq = alloc_workqueue("%s", 0, 0, rtlpriv->cfg->name);
0449     if (!wq)
0450         return -ENOMEM;
0451 
0452     /* <1> timer */
0453     timer_setup(&rtlpriv->works.watchdog_timer,
0454             rtl_watch_dog_timer_callback, 0);
0455     timer_setup(&rtlpriv->works.dualmac_easyconcurrent_retrytimer,
0456             rtl_easy_concurrent_retrytimer_callback, 0);
0457     /* <2> work queue */
0458     rtlpriv->works.hw = hw;
0459     rtlpriv->works.rtl_wq = wq;
0460 
0461     INIT_DELAYED_WORK(&rtlpriv->works.watchdog_wq,
0462               rtl_watchdog_wq_callback);
0463     INIT_DELAYED_WORK(&rtlpriv->works.ips_nic_off_wq,
0464               rtl_ips_nic_off_wq_callback);
0465     INIT_DELAYED_WORK(&rtlpriv->works.ps_work, rtl_swlps_wq_callback);
0466     INIT_DELAYED_WORK(&rtlpriv->works.ps_rfon_wq,
0467               rtl_swlps_rfon_wq_callback);
0468     INIT_DELAYED_WORK(&rtlpriv->works.fwevt_wq, rtl_fwevt_wq_callback);
0469     INIT_DELAYED_WORK(&rtlpriv->works.c2hcmd_wq, rtl_c2hcmd_wq_callback);
0470     return 0;
0471 }
0472 
0473 void rtl_deinit_deferred_work(struct ieee80211_hw *hw, bool ips_wq)
0474 {
0475     struct rtl_priv *rtlpriv = rtl_priv(hw);
0476 
0477     del_timer_sync(&rtlpriv->works.watchdog_timer);
0478 
0479     cancel_delayed_work_sync(&rtlpriv->works.watchdog_wq);
0480     if (ips_wq)
0481         cancel_delayed_work(&rtlpriv->works.ips_nic_off_wq);
0482     else
0483         cancel_delayed_work_sync(&rtlpriv->works.ips_nic_off_wq);
0484     cancel_delayed_work_sync(&rtlpriv->works.ps_work);
0485     cancel_delayed_work_sync(&rtlpriv->works.ps_rfon_wq);
0486     cancel_delayed_work_sync(&rtlpriv->works.fwevt_wq);
0487     cancel_delayed_work_sync(&rtlpriv->works.c2hcmd_wq);
0488 }
0489 EXPORT_SYMBOL_GPL(rtl_deinit_deferred_work);
0490 
0491 void rtl_init_rfkill(struct ieee80211_hw *hw)
0492 {
0493     struct rtl_priv *rtlpriv = rtl_priv(hw);
0494 
0495     bool radio_state;
0496     bool blocked;
0497     u8 valid = 0;
0498 
0499     /*set init state to on */
0500     rtlpriv->rfkill.rfkill_state = true;
0501     wiphy_rfkill_set_hw_state(hw->wiphy, 0);
0502 
0503     radio_state = rtlpriv->cfg->ops->radio_onoff_checking(hw, &valid);
0504 
0505     if (valid) {
0506         pr_info("rtlwifi: wireless switch is %s\n",
0507             rtlpriv->rfkill.rfkill_state ? "on" : "off");
0508 
0509         rtlpriv->rfkill.rfkill_state = radio_state;
0510 
0511         blocked = rtlpriv->rfkill.rfkill_state != 1;
0512         wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
0513     }
0514 
0515     wiphy_rfkill_start_polling(hw->wiphy);
0516 }
0517 EXPORT_SYMBOL(rtl_init_rfkill);
0518 
0519 void rtl_deinit_rfkill(struct ieee80211_hw *hw)
0520 {
0521     wiphy_rfkill_stop_polling(hw->wiphy);
0522 }
0523 EXPORT_SYMBOL_GPL(rtl_deinit_rfkill);
0524 
0525 int rtl_init_core(struct ieee80211_hw *hw)
0526 {
0527     struct rtl_priv *rtlpriv = rtl_priv(hw);
0528     struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
0529 
0530     /* <1> init mac80211 */
0531     _rtl_init_mac80211(hw);
0532     rtlmac->hw = hw;
0533 
0534     /* <2> rate control register */
0535     hw->rate_control_algorithm = "rtl_rc";
0536 
0537     /*
0538      * <3> init CRDA must come after init
0539      * mac80211 hw  in _rtl_init_mac80211.
0540      */
0541     if (rtl_regd_init(hw, rtl_reg_notifier)) {
0542         pr_err("REGD init failed\n");
0543         return 1;
0544     }
0545 
0546     /* <4> locks */
0547     mutex_init(&rtlpriv->locks.conf_mutex);
0548     mutex_init(&rtlpriv->locks.ips_mutex);
0549     mutex_init(&rtlpriv->locks.lps_mutex);
0550     spin_lock_init(&rtlpriv->locks.irq_th_lock);
0551     spin_lock_init(&rtlpriv->locks.h2c_lock);
0552     spin_lock_init(&rtlpriv->locks.rf_ps_lock);
0553     spin_lock_init(&rtlpriv->locks.rf_lock);
0554     spin_lock_init(&rtlpriv->locks.waitq_lock);
0555     spin_lock_init(&rtlpriv->locks.entry_list_lock);
0556     spin_lock_init(&rtlpriv->locks.scan_list_lock);
0557     spin_lock_init(&rtlpriv->locks.cck_and_rw_pagea_lock);
0558     spin_lock_init(&rtlpriv->locks.fw_ps_lock);
0559     spin_lock_init(&rtlpriv->locks.iqk_lock);
0560     /* <5> init list */
0561     INIT_LIST_HEAD(&rtlpriv->entry_list);
0562     INIT_LIST_HEAD(&rtlpriv->scan_list.list);
0563     skb_queue_head_init(&rtlpriv->tx_report.queue);
0564     skb_queue_head_init(&rtlpriv->c2hcmd_queue);
0565 
0566     rtlmac->link_state = MAC80211_NOLINK;
0567 
0568     /* <6> init deferred work */
0569     return _rtl_init_deferred_work(hw);
0570 }
0571 EXPORT_SYMBOL_GPL(rtl_init_core);
0572 
0573 static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw);
0574 static void rtl_free_entries_from_ack_queue(struct ieee80211_hw *hw,
0575                         bool timeout);
0576 
0577 void rtl_deinit_core(struct ieee80211_hw *hw)
0578 {
0579     rtl_c2hcmd_launcher(hw, 0);
0580     rtl_free_entries_from_scan_list(hw);
0581     rtl_free_entries_from_ack_queue(hw, false);
0582 }
0583 EXPORT_SYMBOL_GPL(rtl_deinit_core);
0584 
0585 void rtl_init_rx_config(struct ieee80211_hw *hw)
0586 {
0587     struct rtl_priv *rtlpriv = rtl_priv(hw);
0588     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0589 
0590     rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RCR, (u8 *) (&mac->rx_conf));
0591 }
0592 EXPORT_SYMBOL_GPL(rtl_init_rx_config);
0593 
0594 /*********************************************************
0595  *
0596  * tx information functions
0597  *
0598  *********************************************************/
0599 static void _rtl_qurey_shortpreamble_mode(struct ieee80211_hw *hw,
0600                       struct rtl_tcb_desc *tcb_desc,
0601                       struct ieee80211_tx_info *info)
0602 {
0603     struct rtl_priv *rtlpriv = rtl_priv(hw);
0604     u8 rate_flag = info->control.rates[0].flags;
0605 
0606     tcb_desc->use_shortpreamble = false;
0607 
0608     /* 1M can only use Long Preamble. 11B spec */
0609     if (tcb_desc->hw_rate == rtlpriv->cfg->maps[RTL_RC_CCK_RATE1M])
0610         return;
0611     else if (rate_flag & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
0612         tcb_desc->use_shortpreamble = true;
0613 
0614     return;
0615 }
0616 
0617 static void _rtl_query_shortgi(struct ieee80211_hw *hw,
0618                    struct ieee80211_sta *sta,
0619                    struct rtl_tcb_desc *tcb_desc,
0620                    struct ieee80211_tx_info *info)
0621 {
0622     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0623     u8 rate_flag = info->control.rates[0].flags;
0624     u8 sgi_40 = 0, sgi_20 = 0, bw_40 = 0;
0625     u8 sgi_80 = 0, bw_80 = 0;
0626 
0627     tcb_desc->use_shortgi = false;
0628 
0629     if (sta == NULL)
0630         return;
0631 
0632     sgi_40 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
0633     sgi_20 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
0634     sgi_80 = sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SHORT_GI_80;
0635 
0636     if (!sta->deflink.ht_cap.ht_supported &&
0637         !sta->deflink.vht_cap.vht_supported)
0638         return;
0639 
0640     if (!sgi_40 && !sgi_20)
0641         return;
0642 
0643     if (mac->opmode == NL80211_IFTYPE_STATION) {
0644         bw_40 = mac->bw_40;
0645         bw_80 = mac->bw_80;
0646     } else if (mac->opmode == NL80211_IFTYPE_AP ||
0647          mac->opmode == NL80211_IFTYPE_ADHOC ||
0648          mac->opmode == NL80211_IFTYPE_MESH_POINT) {
0649         bw_40 = sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
0650         bw_80 = sta->deflink.vht_cap.vht_supported;
0651     }
0652 
0653     if (bw_80) {
0654         if (sgi_80)
0655             tcb_desc->use_shortgi = true;
0656         else
0657             tcb_desc->use_shortgi = false;
0658     } else {
0659         if (bw_40 && sgi_40)
0660             tcb_desc->use_shortgi = true;
0661         else if (!bw_40 && sgi_20)
0662             tcb_desc->use_shortgi = true;
0663     }
0664 
0665     if (!(rate_flag & IEEE80211_TX_RC_SHORT_GI))
0666         tcb_desc->use_shortgi = false;
0667 }
0668 
0669 static void _rtl_query_protection_mode(struct ieee80211_hw *hw,
0670                        struct rtl_tcb_desc *tcb_desc,
0671                        struct ieee80211_tx_info *info)
0672 {
0673     struct rtl_priv *rtlpriv = rtl_priv(hw);
0674     u8 rate_flag = info->control.rates[0].flags;
0675 
0676     /* Common Settings */
0677     tcb_desc->rts_stbc = false;
0678     tcb_desc->cts_enable = false;
0679     tcb_desc->rts_sc = 0;
0680     tcb_desc->rts_bw = false;
0681     tcb_desc->rts_use_shortpreamble = false;
0682     tcb_desc->rts_use_shortgi = false;
0683 
0684     if (rate_flag & IEEE80211_TX_RC_USE_CTS_PROTECT) {
0685         /* Use CTS-to-SELF in protection mode. */
0686         tcb_desc->rts_enable = true;
0687         tcb_desc->cts_enable = true;
0688         tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
0689     } else if (rate_flag & IEEE80211_TX_RC_USE_RTS_CTS) {
0690         /* Use RTS-CTS in protection mode. */
0691         tcb_desc->rts_enable = true;
0692         tcb_desc->rts_rate = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE24M];
0693     }
0694 }
0695 
0696 u8 rtl_mrate_idx_to_arfr_id(struct ieee80211_hw *hw, u8 rate_index,
0697                 enum wireless_mode wirelessmode)
0698 {
0699     struct rtl_priv *rtlpriv = rtl_priv(hw);
0700     struct rtl_phy *rtlphy = &rtlpriv->phy;
0701     u8 ret = 0;
0702 
0703     switch (rate_index) {
0704     case RATR_INX_WIRELESS_NGB:
0705         if (rtlphy->rf_type == RF_1T1R)
0706             ret = RATEID_IDX_BGN_40M_1SS;
0707         else
0708             ret = RATEID_IDX_BGN_40M_2SS;
0709         ; break;
0710     case RATR_INX_WIRELESS_N:
0711     case RATR_INX_WIRELESS_NG:
0712         if (rtlphy->rf_type == RF_1T1R)
0713             ret = RATEID_IDX_GN_N1SS;
0714         else
0715             ret = RATEID_IDX_GN_N2SS;
0716         ; break;
0717     case RATR_INX_WIRELESS_NB:
0718         if (rtlphy->rf_type == RF_1T1R)
0719             ret = RATEID_IDX_BGN_20M_1SS_BN;
0720         else
0721             ret = RATEID_IDX_BGN_20M_2SS_BN;
0722         ; break;
0723     case RATR_INX_WIRELESS_GB:
0724         ret = RATEID_IDX_BG;
0725         break;
0726     case RATR_INX_WIRELESS_G:
0727         ret = RATEID_IDX_G;
0728         break;
0729     case RATR_INX_WIRELESS_B:
0730         ret = RATEID_IDX_B;
0731         break;
0732     case RATR_INX_WIRELESS_MC:
0733         if (wirelessmode == WIRELESS_MODE_B ||
0734             wirelessmode == WIRELESS_MODE_G ||
0735             wirelessmode == WIRELESS_MODE_N_24G ||
0736             wirelessmode == WIRELESS_MODE_AC_24G)
0737             ret = RATEID_IDX_BG;
0738         else
0739             ret = RATEID_IDX_G;
0740         break;
0741     case RATR_INX_WIRELESS_AC_5N:
0742         if (rtlphy->rf_type == RF_1T1R)
0743             ret = RATEID_IDX_VHT_1SS;
0744         else
0745             ret = RATEID_IDX_VHT_2SS;
0746         break;
0747     case RATR_INX_WIRELESS_AC_24N:
0748         if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
0749             if (rtlphy->rf_type == RF_1T1R)
0750                 ret = RATEID_IDX_VHT_1SS;
0751             else
0752                 ret = RATEID_IDX_VHT_2SS;
0753         } else {
0754             if (rtlphy->rf_type == RF_1T1R)
0755                 ret = RATEID_IDX_MIX1;
0756             else
0757                 ret = RATEID_IDX_MIX2;
0758         }
0759         break;
0760     default:
0761         ret = RATEID_IDX_BGN_40M_2SS;
0762         break;
0763     }
0764     return ret;
0765 }
0766 EXPORT_SYMBOL(rtl_mrate_idx_to_arfr_id);
0767 
0768 static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
0769                    struct ieee80211_sta *sta,
0770                    struct rtl_tcb_desc *tcb_desc)
0771 {
0772 #define SET_RATE_ID(rate_id)                    \
0773     ({typeof(rate_id) _id = rate_id;            \
0774       ((rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) ? \
0775         rtl_mrate_idx_to_arfr_id(hw, _id,       \
0776             (sta_entry ? sta_entry->wireless_mode : \
0777              WIRELESS_MODE_G)) :            \
0778         _id); })
0779 
0780     struct rtl_priv *rtlpriv = rtl_priv(hw);
0781     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0782     struct rtl_sta_info *sta_entry = NULL;
0783     u8 ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
0784 
0785     if (sta) {
0786         sta_entry = (struct rtl_sta_info *) sta->drv_priv;
0787         ratr_index = sta_entry->ratr_index;
0788     }
0789     if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
0790         if (mac->opmode == NL80211_IFTYPE_STATION) {
0791             tcb_desc->ratr_index = 0;
0792         } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
0793                 mac->opmode == NL80211_IFTYPE_MESH_POINT) {
0794             if (tcb_desc->multicast || tcb_desc->broadcast) {
0795                 tcb_desc->hw_rate =
0796                     rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
0797                 tcb_desc->use_driver_rate = 1;
0798                 tcb_desc->ratr_index =
0799                     SET_RATE_ID(RATR_INX_WIRELESS_MC);
0800             } else {
0801                 tcb_desc->ratr_index = ratr_index;
0802             }
0803         } else if (mac->opmode == NL80211_IFTYPE_AP) {
0804             tcb_desc->ratr_index = ratr_index;
0805         }
0806     }
0807 
0808     if (rtlpriv->dm.useramask) {
0809         tcb_desc->ratr_index = ratr_index;
0810         /* TODO we will differentiate adhoc and station future  */
0811         if (mac->opmode == NL80211_IFTYPE_STATION ||
0812             mac->opmode == NL80211_IFTYPE_MESH_POINT) {
0813             tcb_desc->mac_id = 0;
0814 
0815             if (sta &&
0816                 (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID))
0817                 ;   /* use sta_entry->ratr_index */
0818             else if (mac->mode == WIRELESS_MODE_AC_5G)
0819                 tcb_desc->ratr_index =
0820                     SET_RATE_ID(RATR_INX_WIRELESS_AC_5N);
0821             else if (mac->mode == WIRELESS_MODE_AC_24G)
0822                 tcb_desc->ratr_index =
0823                     SET_RATE_ID(RATR_INX_WIRELESS_AC_24N);
0824             else if (mac->mode == WIRELESS_MODE_N_24G)
0825                 tcb_desc->ratr_index =
0826                     SET_RATE_ID(RATR_INX_WIRELESS_NGB);
0827             else if (mac->mode == WIRELESS_MODE_N_5G)
0828                 tcb_desc->ratr_index =
0829                     SET_RATE_ID(RATR_INX_WIRELESS_NG);
0830             else if (mac->mode & WIRELESS_MODE_G)
0831                 tcb_desc->ratr_index =
0832                     SET_RATE_ID(RATR_INX_WIRELESS_GB);
0833             else if (mac->mode & WIRELESS_MODE_B)
0834                 tcb_desc->ratr_index =
0835                     SET_RATE_ID(RATR_INX_WIRELESS_B);
0836             else if (mac->mode & WIRELESS_MODE_A)
0837                 tcb_desc->ratr_index =
0838                     SET_RATE_ID(RATR_INX_WIRELESS_G);
0839 
0840         } else if (mac->opmode == NL80211_IFTYPE_AP ||
0841             mac->opmode == NL80211_IFTYPE_ADHOC) {
0842             if (NULL != sta) {
0843                 if (sta->aid > 0)
0844                     tcb_desc->mac_id = sta->aid + 1;
0845                 else
0846                     tcb_desc->mac_id = 1;
0847             } else {
0848                 tcb_desc->mac_id = 0;
0849             }
0850         }
0851     }
0852 #undef SET_RATE_ID
0853 }
0854 
0855 static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
0856                       struct ieee80211_sta *sta,
0857                       struct rtl_tcb_desc *tcb_desc)
0858 {
0859     struct rtl_priv *rtlpriv = rtl_priv(hw);
0860     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
0861 
0862     tcb_desc->packet_bw = false;
0863     if (!sta)
0864         return;
0865     if (mac->opmode == NL80211_IFTYPE_AP ||
0866         mac->opmode == NL80211_IFTYPE_ADHOC ||
0867         mac->opmode == NL80211_IFTYPE_MESH_POINT) {
0868         if (!(sta->deflink.ht_cap.ht_supported) ||
0869             !(sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
0870             return;
0871     } else if (mac->opmode == NL80211_IFTYPE_STATION) {
0872         if (!mac->bw_40 || !(sta->deflink.ht_cap.ht_supported))
0873             return;
0874     }
0875     if (tcb_desc->multicast || tcb_desc->broadcast)
0876         return;
0877 
0878     /*use legency rate, shall use 20MHz */
0879     if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
0880         return;
0881 
0882     tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
0883 
0884     if (rtlpriv->cfg->spec_ver & RTL_SPEC_SUPPORT_VHT) {
0885         if (mac->opmode == NL80211_IFTYPE_AP ||
0886             mac->opmode == NL80211_IFTYPE_ADHOC ||
0887             mac->opmode == NL80211_IFTYPE_MESH_POINT) {
0888             if (!(sta->deflink.vht_cap.vht_supported))
0889                 return;
0890         } else if (mac->opmode == NL80211_IFTYPE_STATION) {
0891             if (!mac->bw_80 ||
0892                 !(sta->deflink.vht_cap.vht_supported))
0893                 return;
0894         }
0895         if (tcb_desc->hw_rate <=
0896             rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
0897             return;
0898         tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
0899     }
0900 }
0901 
0902 static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
0903                       struct ieee80211_sta *sta)
0904 {
0905     struct rtl_priv *rtlpriv = rtl_priv(hw);
0906     struct rtl_phy *rtlphy = &(rtlpriv->phy);
0907     u8 hw_rate;
0908     u16 tx_mcs_map = le16_to_cpu(sta->deflink.vht_cap.vht_mcs.tx_mcs_map);
0909 
0910     if ((get_rf_type(rtlphy) == RF_2T2R) &&
0911         (tx_mcs_map & 0x000c) != 0x000c) {
0912         if ((tx_mcs_map & 0x000c) >> 2 ==
0913             IEEE80211_VHT_MCS_SUPPORT_0_7)
0914             hw_rate =
0915             rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
0916         else if ((tx_mcs_map  & 0x000c) >> 2 ==
0917             IEEE80211_VHT_MCS_SUPPORT_0_8)
0918             hw_rate =
0919             rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS8];
0920         else
0921             hw_rate =
0922             rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
0923     } else {
0924         if ((tx_mcs_map  & 0x0003) ==
0925             IEEE80211_VHT_MCS_SUPPORT_0_7)
0926             hw_rate =
0927             rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
0928         else if ((tx_mcs_map  & 0x0003) ==
0929             IEEE80211_VHT_MCS_SUPPORT_0_8)
0930             hw_rate =
0931             rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS8];
0932         else
0933             hw_rate =
0934             rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
0935     }
0936 
0937     return hw_rate;
0938 }
0939 
0940 static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
0941                   struct ieee80211_sta *sta)
0942 {
0943     struct rtl_priv *rtlpriv = rtl_priv(hw);
0944     struct rtl_phy *rtlphy = &rtlpriv->phy;
0945     u8 hw_rate;
0946 
0947     if (get_rf_type(rtlphy) == RF_2T2R &&
0948         sta->deflink.ht_cap.mcs.rx_mask[1] != 0)
0949         hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
0950     else
0951         hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
0952 
0953     return hw_rate;
0954 }
0955 
0956 /* mac80211's rate_idx is like this:
0957  *
0958  * 2.4G band:rx_status->band == NL80211_BAND_2GHZ
0959  *
0960  * B/G rate:
0961  * (rx_status->flag & RX_FLAG_HT) = 0,
0962  * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
0963  *
0964  * N rate:
0965  * (rx_status->flag & RX_FLAG_HT) = 1,
0966  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
0967  *
0968  * 5G band:rx_status->band == NL80211_BAND_5GHZ
0969  * A rate:
0970  * (rx_status->flag & RX_FLAG_HT) = 0,
0971  * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
0972  *
0973  * N rate:
0974  * (rx_status->flag & RX_FLAG_HT) = 1,
0975  * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
0976  *
0977  * VHT rates:
0978  * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
0979  * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
0980  */
0981 int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
0982              u8 desc_rate)
0983 {
0984     int rate_idx;
0985 
0986     if (isvht) {
0987         switch (desc_rate) {
0988         case DESC_RATEVHT1SS_MCS0:
0989             rate_idx = 0;
0990             break;
0991         case DESC_RATEVHT1SS_MCS1:
0992             rate_idx = 1;
0993             break;
0994         case DESC_RATEVHT1SS_MCS2:
0995             rate_idx = 2;
0996             break;
0997         case DESC_RATEVHT1SS_MCS3:
0998             rate_idx = 3;
0999             break;
1000         case DESC_RATEVHT1SS_MCS4:
1001             rate_idx = 4;
1002             break;
1003         case DESC_RATEVHT1SS_MCS5:
1004             rate_idx = 5;
1005             break;
1006         case DESC_RATEVHT1SS_MCS6:
1007             rate_idx = 6;
1008             break;
1009         case DESC_RATEVHT1SS_MCS7:
1010             rate_idx = 7;
1011             break;
1012         case DESC_RATEVHT1SS_MCS8:
1013             rate_idx = 8;
1014             break;
1015         case DESC_RATEVHT1SS_MCS9:
1016             rate_idx = 9;
1017             break;
1018         case DESC_RATEVHT2SS_MCS0:
1019             rate_idx = 0;
1020             break;
1021         case DESC_RATEVHT2SS_MCS1:
1022             rate_idx = 1;
1023             break;
1024         case DESC_RATEVHT2SS_MCS2:
1025             rate_idx = 2;
1026             break;
1027         case DESC_RATEVHT2SS_MCS3:
1028             rate_idx = 3;
1029             break;
1030         case DESC_RATEVHT2SS_MCS4:
1031             rate_idx = 4;
1032             break;
1033         case DESC_RATEVHT2SS_MCS5:
1034             rate_idx = 5;
1035             break;
1036         case DESC_RATEVHT2SS_MCS6:
1037             rate_idx = 6;
1038             break;
1039         case DESC_RATEVHT2SS_MCS7:
1040             rate_idx = 7;
1041             break;
1042         case DESC_RATEVHT2SS_MCS8:
1043             rate_idx = 8;
1044             break;
1045         case DESC_RATEVHT2SS_MCS9:
1046             rate_idx = 9;
1047             break;
1048         default:
1049             rate_idx = 0;
1050             break;
1051         }
1052         return rate_idx;
1053     }
1054     if (false == isht) {
1055         if (NL80211_BAND_2GHZ == hw->conf.chandef.chan->band) {
1056             switch (desc_rate) {
1057             case DESC_RATE1M:
1058                 rate_idx = 0;
1059                 break;
1060             case DESC_RATE2M:
1061                 rate_idx = 1;
1062                 break;
1063             case DESC_RATE5_5M:
1064                 rate_idx = 2;
1065                 break;
1066             case DESC_RATE11M:
1067                 rate_idx = 3;
1068                 break;
1069             case DESC_RATE6M:
1070                 rate_idx = 4;
1071                 break;
1072             case DESC_RATE9M:
1073                 rate_idx = 5;
1074                 break;
1075             case DESC_RATE12M:
1076                 rate_idx = 6;
1077                 break;
1078             case DESC_RATE18M:
1079                 rate_idx = 7;
1080                 break;
1081             case DESC_RATE24M:
1082                 rate_idx = 8;
1083                 break;
1084             case DESC_RATE36M:
1085                 rate_idx = 9;
1086                 break;
1087             case DESC_RATE48M:
1088                 rate_idx = 10;
1089                 break;
1090             case DESC_RATE54M:
1091                 rate_idx = 11;
1092                 break;
1093             default:
1094                 rate_idx = 0;
1095                 break;
1096             }
1097         } else {
1098             switch (desc_rate) {
1099             case DESC_RATE6M:
1100                 rate_idx = 0;
1101                 break;
1102             case DESC_RATE9M:
1103                 rate_idx = 1;
1104                 break;
1105             case DESC_RATE12M:
1106                 rate_idx = 2;
1107                 break;
1108             case DESC_RATE18M:
1109                 rate_idx = 3;
1110                 break;
1111             case DESC_RATE24M:
1112                 rate_idx = 4;
1113                 break;
1114             case DESC_RATE36M:
1115                 rate_idx = 5;
1116                 break;
1117             case DESC_RATE48M:
1118                 rate_idx = 6;
1119                 break;
1120             case DESC_RATE54M:
1121                 rate_idx = 7;
1122                 break;
1123             default:
1124                 rate_idx = 0;
1125                 break;
1126             }
1127         }
1128     } else {
1129         switch (desc_rate) {
1130         case DESC_RATEMCS0:
1131             rate_idx = 0;
1132             break;
1133         case DESC_RATEMCS1:
1134             rate_idx = 1;
1135             break;
1136         case DESC_RATEMCS2:
1137             rate_idx = 2;
1138             break;
1139         case DESC_RATEMCS3:
1140             rate_idx = 3;
1141             break;
1142         case DESC_RATEMCS4:
1143             rate_idx = 4;
1144             break;
1145         case DESC_RATEMCS5:
1146             rate_idx = 5;
1147             break;
1148         case DESC_RATEMCS6:
1149             rate_idx = 6;
1150             break;
1151         case DESC_RATEMCS7:
1152             rate_idx = 7;
1153             break;
1154         case DESC_RATEMCS8:
1155             rate_idx = 8;
1156             break;
1157         case DESC_RATEMCS9:
1158             rate_idx = 9;
1159             break;
1160         case DESC_RATEMCS10:
1161             rate_idx = 10;
1162             break;
1163         case DESC_RATEMCS11:
1164             rate_idx = 11;
1165             break;
1166         case DESC_RATEMCS12:
1167             rate_idx = 12;
1168             break;
1169         case DESC_RATEMCS13:
1170             rate_idx = 13;
1171             break;
1172         case DESC_RATEMCS14:
1173             rate_idx = 14;
1174             break;
1175         case DESC_RATEMCS15:
1176             rate_idx = 15;
1177             break;
1178         default:
1179             rate_idx = 0;
1180             break;
1181         }
1182     }
1183     return rate_idx;
1184 }
1185 EXPORT_SYMBOL(rtlwifi_rate_mapping);
1186 
1187 static u8 _rtl_get_tx_hw_rate(struct ieee80211_hw *hw,
1188                   struct ieee80211_tx_info *info)
1189 {
1190     struct rtl_priv *rtlpriv = rtl_priv(hw);
1191     struct ieee80211_tx_rate *r = &info->status.rates[0];
1192     struct ieee80211_rate *txrate;
1193     u8 hw_value = 0x0;
1194 
1195     if (r->flags & IEEE80211_TX_RC_MCS) {
1196         /* HT MCS0-15 */
1197         hw_value = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15] - 15 +
1198                r->idx;
1199     } else if (r->flags & IEEE80211_TX_RC_VHT_MCS) {
1200         /* VHT MCS0-9, NSS */
1201         if (ieee80211_rate_get_vht_nss(r) == 2)
1202             hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
1203         else
1204             hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
1205 
1206         hw_value = hw_value - 9 + ieee80211_rate_get_vht_mcs(r);
1207     } else {
1208         /* legacy */
1209         txrate = ieee80211_get_tx_rate(hw, info);
1210 
1211         if (txrate)
1212             hw_value = txrate->hw_value;
1213     }
1214 
1215     /* check 5G band */
1216     if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G &&
1217         hw_value < rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M])
1218         hw_value = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M];
1219 
1220     return hw_value;
1221 }
1222 
1223 void rtl_get_tcb_desc(struct ieee80211_hw *hw,
1224               struct ieee80211_tx_info *info,
1225               struct ieee80211_sta *sta,
1226               struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
1227 {
1228 #define SET_RATE_ID(rate_id)                    \
1229     ({typeof(rate_id) _id = rate_id;            \
1230       ((rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) ? \
1231         rtl_mrate_idx_to_arfr_id(hw, _id,       \
1232             (sta_entry ? sta_entry->wireless_mode : \
1233              WIRELESS_MODE_G)) :            \
1234         _id); })
1235 
1236     struct rtl_priv *rtlpriv = rtl_priv(hw);
1237     struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1238     struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1239     struct rtl_sta_info *sta_entry =
1240         (sta ? (struct rtl_sta_info *)sta->drv_priv : NULL);
1241 
1242     __le16 fc = rtl_get_fc(skb);
1243 
1244     tcb_desc->hw_rate = _rtl_get_tx_hw_rate(hw, info);
1245 
1246     if (rtl_is_tx_report_skb(hw, skb))
1247         tcb_desc->use_spe_rpt = 1;
1248 
1249     if (ieee80211_is_data(fc)) {
1250         /*
1251          *we set data rate INX 0
1252          *in rtl_rc.c   if skb is special data or
1253          *mgt which need low data rate.
1254          */
1255 
1256         /*
1257          *So tcb_desc->hw_rate is just used for
1258          *special data and mgt frames
1259          */
1260         if (info->control.rates[0].idx == 0 ||
1261                 ieee80211_is_nullfunc(fc)) {
1262             tcb_desc->use_driver_rate = true;
1263             tcb_desc->ratr_index =
1264                     SET_RATE_ID(RATR_INX_WIRELESS_MC);
1265 
1266             tcb_desc->disable_ratefallback = 1;
1267         } else {
1268             /*
1269              *because hw will nerver use hw_rate
1270              *when tcb_desc->use_driver_rate = false
1271              *so we never set highest N rate here,
1272              *and N rate will all be controlled by FW
1273              *when tcb_desc->use_driver_rate = false
1274              */
1275             if (sta && sta->deflink.vht_cap.vht_supported) {
1276                 tcb_desc->hw_rate =
1277                 _rtl_get_vht_highest_n_rate(hw, sta);
1278             } else {
1279                 if (sta && sta->deflink.ht_cap.ht_supported) {
1280                     tcb_desc->hw_rate =
1281                         _rtl_get_highest_n_rate(hw, sta);
1282                 } else {
1283                     if (rtlmac->mode == WIRELESS_MODE_B) {
1284                         tcb_desc->hw_rate =
1285                             rtlpriv->cfg->maps[RTL_RC_CCK_RATE11M];
1286                     } else {
1287                         tcb_desc->hw_rate =
1288                             rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M];
1289                     }
1290                 }
1291             }
1292         }
1293 
1294         if (is_multicast_ether_addr(hdr->addr1))
1295             tcb_desc->multicast = 1;
1296         else if (is_broadcast_ether_addr(hdr->addr1))
1297             tcb_desc->broadcast = 1;
1298 
1299         _rtl_txrate_selectmode(hw, sta, tcb_desc);
1300         _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
1301         _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
1302         _rtl_query_shortgi(hw, sta, tcb_desc, info);
1303         _rtl_query_protection_mode(hw, tcb_desc, info);
1304     } else {
1305         tcb_desc->use_driver_rate = true;
1306         tcb_desc->ratr_index = SET_RATE_ID(RATR_INX_WIRELESS_MC);
1307         tcb_desc->disable_ratefallback = 1;
1308         tcb_desc->mac_id = 0;
1309         tcb_desc->packet_bw = false;
1310     }
1311 #undef SET_RATE_ID
1312 }
1313 EXPORT_SYMBOL(rtl_get_tcb_desc);
1314 
1315 bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
1316 {
1317     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1318     struct rtl_priv *rtlpriv = rtl_priv(hw);
1319     __le16 fc = rtl_get_fc(skb);
1320 
1321     if (rtlpriv->dm.supp_phymode_switch &&
1322         mac->link_state < MAC80211_LINKED &&
1323         (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
1324         if (rtlpriv->cfg->ops->chk_switch_dmdp)
1325             rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1326     }
1327     if (ieee80211_is_auth(fc)) {
1328         rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
1329 
1330         mac->link_state = MAC80211_LINKING;
1331         /* Dul mac */
1332         rtlpriv->phy.need_iqk = true;
1333 
1334     }
1335 
1336     return true;
1337 }
1338 EXPORT_SYMBOL_GPL(rtl_tx_mgmt_proc);
1339 
1340 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
1341                 u8 *bssid, u16 tid);
1342 
1343 static void process_agg_start(struct ieee80211_hw *hw,
1344                   struct ieee80211_hdr *hdr, u16 tid)
1345 {
1346     struct rtl_priv *rtlpriv = rtl_priv(hw);
1347     struct ieee80211_rx_status rx_status = { 0 };
1348     struct sk_buff *skb_delba = NULL;
1349 
1350     skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
1351     if (skb_delba) {
1352         rx_status.freq = hw->conf.chandef.chan->center_freq;
1353         rx_status.band = hw->conf.chandef.chan->band;
1354         rx_status.flag |= RX_FLAG_DECRYPTED;
1355         rx_status.flag |= RX_FLAG_MACTIME_START;
1356         rx_status.rate_idx = 0;
1357         rx_status.signal = 50 + 10;
1358         memcpy(IEEE80211_SKB_RXCB(skb_delba),
1359                &rx_status, sizeof(rx_status));
1360         RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1361                   "fake del\n",
1362                   skb_delba->data,
1363                   skb_delba->len);
1364         ieee80211_rx_irqsafe(hw, skb_delba);
1365     }
1366 }
1367 
1368 bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1369 {
1370     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1371     struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1372     struct rtl_priv *rtlpriv = rtl_priv(hw);
1373     __le16 fc = rtl_get_fc(skb);
1374     u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
1375     u8 category;
1376 
1377     if (!ieee80211_is_action(fc))
1378         return true;
1379 
1380     category = *act;
1381     act++;
1382     switch (category) {
1383     case ACT_CAT_BA:
1384         switch (*act) {
1385         case ACT_ADDBAREQ:
1386             if (mac->act_scanning)
1387                 return false;
1388 
1389             rtl_dbg(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1390                 "%s ACT_ADDBAREQ From :%pM\n",
1391                 is_tx ? "Tx" : "Rx", hdr->addr2);
1392             RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1393                 skb->data, skb->len);
1394             if (!is_tx) {
1395                 struct ieee80211_sta *sta = NULL;
1396                 struct rtl_sta_info *sta_entry = NULL;
1397                 struct rtl_tid_data *tid_data;
1398                 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1399                 u16 capab = 0, tid = 0;
1400 
1401                 rcu_read_lock();
1402                 sta = rtl_find_sta(hw, hdr->addr3);
1403                 if (sta == NULL) {
1404                     rtl_dbg(rtlpriv, COMP_SEND | COMP_RECV,
1405                         DBG_DMESG, "sta is NULL\n");
1406                     rcu_read_unlock();
1407                     return true;
1408                 }
1409 
1410                 sta_entry =
1411                     (struct rtl_sta_info *)sta->drv_priv;
1412                 if (!sta_entry) {
1413                     rcu_read_unlock();
1414                     return true;
1415                 }
1416                 capab =
1417                   le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1418                 tid = (capab &
1419                        IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1420                 if (tid >= MAX_TID_COUNT) {
1421                     rcu_read_unlock();
1422                     return true;
1423                 }
1424                 tid_data = &sta_entry->tids[tid];
1425                 if (tid_data->agg.rx_agg_state ==
1426                     RTL_RX_AGG_START)
1427                     process_agg_start(hw, hdr, tid);
1428                 rcu_read_unlock();
1429             }
1430             break;
1431         case ACT_ADDBARSP:
1432             rtl_dbg(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1433                 "%s ACT_ADDBARSP From :%pM\n",
1434                 is_tx ? "Tx" : "Rx", hdr->addr2);
1435             break;
1436         case ACT_DELBA:
1437             rtl_dbg(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1438                 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1439             break;
1440         }
1441         break;
1442     default:
1443         break;
1444     }
1445 
1446     return true;
1447 }
1448 EXPORT_SYMBOL_GPL(rtl_action_proc);
1449 
1450 static void setup_special_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc,
1451                  int type)
1452 {
1453     struct ieee80211_hw *hw = rtlpriv->hw;
1454 
1455     rtlpriv->ra.is_special_data = true;
1456     if (rtlpriv->cfg->ops->get_btc_status())
1457         rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(
1458                     rtlpriv, type);
1459     rtl_lps_leave(hw, false);
1460     ppsc->last_delaylps_stamp_jiffies = jiffies;
1461 }
1462 
1463 static const u8 *rtl_skb_ether_type_ptr(struct ieee80211_hw *hw,
1464                     struct sk_buff *skb, bool is_enc)
1465 {
1466     struct rtl_priv *rtlpriv = rtl_priv(hw);
1467     u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1468     u8 encrypt_header_len = 0;
1469     u8 offset;
1470 
1471     switch (rtlpriv->sec.pairwise_enc_algorithm) {
1472     case WEP40_ENCRYPTION:
1473     case WEP104_ENCRYPTION:
1474         encrypt_header_len = 4;/*WEP_IV_LEN*/
1475         break;
1476     case TKIP_ENCRYPTION:
1477         encrypt_header_len = 8;/*TKIP_IV_LEN*/
1478         break;
1479     case AESCCMP_ENCRYPTION:
1480         encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
1481         break;
1482     default:
1483         break;
1484     }
1485 
1486     offset = mac_hdr_len + SNAP_SIZE;
1487     if (is_enc)
1488         offset += encrypt_header_len;
1489 
1490     return skb->data + offset;
1491 }
1492 
1493 /*should call before software enc*/
1494 u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
1495                bool is_enc)
1496 {
1497     struct rtl_priv *rtlpriv = rtl_priv(hw);
1498     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1499     __le16 fc = rtl_get_fc(skb);
1500     u16 ether_type;
1501     const u8 *ether_type_ptr;
1502     const struct iphdr *ip;
1503 
1504     if (!ieee80211_is_data(fc))
1505         goto end;
1506 
1507     ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, is_enc);
1508     ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1509 
1510     if (ETH_P_IP == ether_type) {
1511         ip = (struct iphdr *)((u8 *)ether_type_ptr +
1512              PROTOC_TYPE_SIZE);
1513         if (IPPROTO_UDP == ip->protocol) {
1514             struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1515                                    (ip->ihl << 2));
1516             if (((((u8 *)udp)[1] == 68) &&
1517                  (((u8 *)udp)[3] == 67)) ||
1518                 ((((u8 *)udp)[1] == 67) &&
1519                  (((u8 *)udp)[3] == 68))) {
1520                 /* 68 : UDP BOOTP client
1521                  * 67 : UDP BOOTP server
1522                  */
1523                 rtl_dbg(rtlpriv, (COMP_SEND | COMP_RECV),
1524                     DBG_DMESG, "dhcp %s !!\n",
1525                     (is_tx) ? "Tx" : "Rx");
1526 
1527                 if (is_tx)
1528                     setup_special_tx(rtlpriv, ppsc,
1529                              PACKET_DHCP);
1530 
1531                 return true;
1532             }
1533         }
1534     } else if (ETH_P_ARP == ether_type) {
1535         if (is_tx)
1536             setup_special_tx(rtlpriv, ppsc, PACKET_ARP);
1537 
1538         return true;
1539     } else if (ETH_P_PAE == ether_type) {
1540         /* EAPOL is seens as in-4way */
1541         rtlpriv->btcoexist.btc_info.in_4way = true;
1542         rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
1543 
1544         rtl_dbg(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1545             "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
1546 
1547         if (is_tx) {
1548             rtlpriv->ra.is_special_data = true;
1549             rtl_lps_leave(hw, false);
1550             ppsc->last_delaylps_stamp_jiffies = jiffies;
1551 
1552             setup_special_tx(rtlpriv, ppsc, PACKET_EAPOL);
1553         }
1554 
1555         return true;
1556     } else if (ETH_P_IPV6 == ether_type) {
1557         /* TODO: Handle any IPv6 cases that need special handling.
1558          * For now, always return false
1559          */
1560         goto end;
1561     }
1562 
1563 end:
1564     rtlpriv->ra.is_special_data = false;
1565     return false;
1566 }
1567 EXPORT_SYMBOL_GPL(rtl_is_special_data);
1568 
1569 void rtl_tx_ackqueue(struct ieee80211_hw *hw, struct sk_buff *skb)
1570 {
1571     struct rtl_priv *rtlpriv = rtl_priv(hw);
1572     struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1573 
1574     __skb_queue_tail(&tx_report->queue, skb);
1575 }
1576 EXPORT_SYMBOL_GPL(rtl_tx_ackqueue);
1577 
1578 static void rtl_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1579               bool ack)
1580 {
1581     struct rtl_priv *rtlpriv = rtl_priv(hw);
1582     struct ieee80211_tx_info *info;
1583 
1584     info = IEEE80211_SKB_CB(skb);
1585     ieee80211_tx_info_clear_status(info);
1586     if (ack) {
1587         rtl_dbg(rtlpriv, COMP_TX_REPORT, DBG_LOUD,
1588             "tx report: ack\n");
1589         info->flags |= IEEE80211_TX_STAT_ACK;
1590     } else {
1591         rtl_dbg(rtlpriv, COMP_TX_REPORT, DBG_LOUD,
1592             "tx report: not ack\n");
1593         info->flags &= ~IEEE80211_TX_STAT_ACK;
1594     }
1595     ieee80211_tx_status_irqsafe(hw, skb);
1596 }
1597 
1598 bool rtl_is_tx_report_skb(struct ieee80211_hw *hw, struct sk_buff *skb)
1599 {
1600     u16 ether_type;
1601     const u8 *ether_type_ptr;
1602     __le16 fc = rtl_get_fc(skb);
1603 
1604     ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, true);
1605     ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1606 
1607     if (ether_type == ETH_P_PAE || ieee80211_is_nullfunc(fc))
1608         return true;
1609 
1610     return false;
1611 }
1612 
1613 static u16 rtl_get_tx_report_sn(struct ieee80211_hw *hw,
1614                 struct rtlwifi_tx_info *tx_info)
1615 {
1616     struct rtl_priv *rtlpriv = rtl_priv(hw);
1617     struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1618     u16 sn;
1619 
1620     /* SW_DEFINE[11:8] are reserved (driver fills zeros)
1621      * SW_DEFINE[7:2] are used by driver
1622      * SW_DEFINE[1:0] are reserved for firmware (driver fills zeros)
1623      */
1624     sn = (atomic_inc_return(&tx_report->sn) & 0x003F) << 2;
1625 
1626     tx_report->last_sent_sn = sn;
1627     tx_report->last_sent_time = jiffies;
1628     tx_info->sn = sn;
1629     tx_info->send_time = tx_report->last_sent_time;
1630     rtl_dbg(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1631         "Send TX-Report sn=0x%X\n", sn);
1632 
1633     return sn;
1634 }
1635 
1636 void rtl_set_tx_report(struct rtl_tcb_desc *ptcb_desc, u8 *pdesc,
1637                struct ieee80211_hw *hw, struct rtlwifi_tx_info *tx_info)
1638 {
1639     if (ptcb_desc->use_spe_rpt) {
1640         u16 sn = rtl_get_tx_report_sn(hw, tx_info);
1641 
1642         SET_TX_DESC_SPE_RPT(pdesc, 1);
1643         SET_TX_DESC_SW_DEFINE(pdesc, sn);
1644     }
1645 }
1646 EXPORT_SYMBOL_GPL(rtl_set_tx_report);
1647 
1648 void rtl_tx_report_handler(struct ieee80211_hw *hw, u8 *tmp_buf, u8 c2h_cmd_len)
1649 {
1650     struct rtl_priv *rtlpriv = rtl_priv(hw);
1651     struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1652     struct rtlwifi_tx_info *tx_info;
1653     struct sk_buff_head *queue = &tx_report->queue;
1654     struct sk_buff *skb;
1655     u16 sn;
1656     u8 st, retry;
1657 
1658     if (rtlpriv->cfg->spec_ver & RTL_SPEC_EXT_C2H) {
1659         sn = GET_TX_REPORT_SN_V2(tmp_buf);
1660         st = GET_TX_REPORT_ST_V2(tmp_buf);
1661         retry = GET_TX_REPORT_RETRY_V2(tmp_buf);
1662     } else {
1663         sn = GET_TX_REPORT_SN_V1(tmp_buf);
1664         st = GET_TX_REPORT_ST_V1(tmp_buf);
1665         retry = GET_TX_REPORT_RETRY_V1(tmp_buf);
1666     }
1667 
1668     tx_report->last_recv_sn = sn;
1669 
1670     skb_queue_walk(queue, skb) {
1671         tx_info = rtl_tx_skb_cb_info(skb);
1672         if (tx_info->sn == sn) {
1673             skb_unlink(skb, queue);
1674             rtl_tx_status(hw, skb, st == 0);
1675             break;
1676         }
1677     }
1678     rtl_dbg(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1679         "Recv TX-Report st=0x%02X sn=0x%X retry=0x%X\n",
1680         st, sn, retry);
1681 }
1682 EXPORT_SYMBOL_GPL(rtl_tx_report_handler);
1683 
1684 bool rtl_check_tx_report_acked(struct ieee80211_hw *hw)
1685 {
1686     struct rtl_priv *rtlpriv = rtl_priv(hw);
1687     struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1688 
1689     if (tx_report->last_sent_sn == tx_report->last_recv_sn)
1690         return true;
1691 
1692     if (time_before(tx_report->last_sent_time + 3 * HZ, jiffies)) {
1693         rtl_dbg(rtlpriv, COMP_TX_REPORT, DBG_WARNING,
1694             "Check TX-Report timeout!! s_sn=0x%X r_sn=0x%X\n",
1695             tx_report->last_sent_sn, tx_report->last_recv_sn);
1696         return true;    /* 3 sec. (timeout) seen as acked */
1697     }
1698 
1699     return false;
1700 }
1701 
1702 void rtl_wait_tx_report_acked(struct ieee80211_hw *hw, u32 wait_ms)
1703 {
1704     struct rtl_priv *rtlpriv = rtl_priv(hw);
1705     int i;
1706 
1707     for (i = 0; i < wait_ms; i++) {
1708         if (rtl_check_tx_report_acked(hw))
1709             break;
1710         usleep_range(1000, 2000);
1711         rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
1712             "Wait 1ms (%d/%d) to disable key.\n", i, wait_ms);
1713     }
1714 }
1715 
1716 u32 rtl_get_hal_edca_param(struct ieee80211_hw *hw,
1717                struct ieee80211_vif *vif,
1718                enum wireless_mode wirelessmode,
1719                struct ieee80211_tx_queue_params *param)
1720 {
1721     u32 reg = 0;
1722     u8 sifstime = 10;
1723     u8 slottime = 20;
1724 
1725     /* AIFS = AIFSN * slot time + SIFS */
1726     switch (wirelessmode) {
1727     case WIRELESS_MODE_A:
1728     case WIRELESS_MODE_N_24G:
1729     case WIRELESS_MODE_N_5G:
1730     case WIRELESS_MODE_AC_5G:
1731     case WIRELESS_MODE_AC_24G:
1732         sifstime = 16;
1733         slottime = 9;
1734         break;
1735     case WIRELESS_MODE_G:
1736         slottime = (vif->bss_conf.use_short_slot ? 9 : 20);
1737         break;
1738     default:
1739         break;
1740     }
1741 
1742     reg |= (param->txop & 0x7FF) << 16;
1743     reg |= (fls(param->cw_max) & 0xF) << 12;
1744     reg |= (fls(param->cw_min) & 0xF) << 8;
1745     reg |= (param->aifs & 0x0F) * slottime + sifstime;
1746 
1747     return reg;
1748 }
1749 EXPORT_SYMBOL_GPL(rtl_get_hal_edca_param);
1750 
1751 /*********************************************************
1752  *
1753  * functions called by core.c
1754  *
1755  *********************************************************/
1756 int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1757              struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1758 {
1759     struct rtl_priv *rtlpriv = rtl_priv(hw);
1760     struct rtl_tid_data *tid_data;
1761     struct rtl_sta_info *sta_entry = NULL;
1762 
1763     if (sta == NULL)
1764         return -EINVAL;
1765 
1766     if (unlikely(tid >= MAX_TID_COUNT))
1767         return -EINVAL;
1768 
1769     sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1770     if (!sta_entry)
1771         return -ENXIO;
1772     tid_data = &sta_entry->tids[tid];
1773 
1774     rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG,
1775         "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1776         *ssn);
1777 
1778     tid_data->agg.agg_state = RTL_AGG_START;
1779 
1780     return IEEE80211_AMPDU_TX_START_IMMEDIATE;
1781 }
1782 
1783 int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1784             struct ieee80211_sta *sta, u16 tid)
1785 {
1786     struct rtl_priv *rtlpriv = rtl_priv(hw);
1787     struct rtl_sta_info *sta_entry = NULL;
1788 
1789     if (sta == NULL)
1790         return -EINVAL;
1791 
1792     rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG,
1793         "on ra = %pM tid = %d\n", sta->addr, tid);
1794 
1795     if (unlikely(tid >= MAX_TID_COUNT))
1796         return -EINVAL;
1797 
1798     sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1799     sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1800 
1801     ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1802     return 0;
1803 }
1804 
1805 int rtl_rx_agg_start(struct ieee80211_hw *hw,
1806              struct ieee80211_sta *sta, u16 tid)
1807 {
1808     struct rtl_priv *rtlpriv = rtl_priv(hw);
1809     struct rtl_tid_data *tid_data;
1810     struct rtl_sta_info *sta_entry = NULL;
1811     u8 reject_agg;
1812 
1813     if (sta == NULL)
1814         return -EINVAL;
1815 
1816     if (unlikely(tid >= MAX_TID_COUNT))
1817         return -EINVAL;
1818 
1819     if (rtlpriv->cfg->ops->get_btc_status()) {
1820         rtlpriv->btcoexist.btc_ops->btc_get_ampdu_cfg(rtlpriv,
1821                                   &reject_agg,
1822                                   NULL, NULL);
1823         if (reject_agg)
1824             return -EINVAL;
1825     }
1826 
1827     sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1828     if (!sta_entry)
1829         return -ENXIO;
1830     tid_data = &sta_entry->tids[tid];
1831 
1832     rtl_dbg(rtlpriv, COMP_RECV, DBG_DMESG,
1833         "on ra = %pM tid = %d\n", sta->addr, tid);
1834 
1835     tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1836     return 0;
1837 }
1838 
1839 int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1840             struct ieee80211_sta *sta, u16 tid)
1841 {
1842     struct rtl_priv *rtlpriv = rtl_priv(hw);
1843     struct rtl_sta_info *sta_entry = NULL;
1844 
1845     if (sta == NULL)
1846         return -EINVAL;
1847 
1848     rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG,
1849         "on ra = %pM tid = %d\n", sta->addr, tid);
1850 
1851     if (unlikely(tid >= MAX_TID_COUNT))
1852         return -EINVAL;
1853 
1854     sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1855     sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1856 
1857     return 0;
1858 }
1859 
1860 int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1861         struct ieee80211_sta *sta, u16 tid)
1862 {
1863     struct rtl_priv *rtlpriv = rtl_priv(hw);
1864     struct rtl_sta_info *sta_entry = NULL;
1865 
1866     if (sta == NULL)
1867         return -EINVAL;
1868 
1869     rtl_dbg(rtlpriv, COMP_SEND, DBG_DMESG,
1870         "on ra = %pM tid = %d\n", sta->addr, tid);
1871 
1872     if (unlikely(tid >= MAX_TID_COUNT))
1873         return -EINVAL;
1874 
1875     sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1876     sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1877 
1878     return 0;
1879 }
1880 
1881 void rtl_rx_ampdu_apply(struct rtl_priv *rtlpriv)
1882 {
1883     struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
1884     u8 reject_agg = 0, ctrl_agg_size = 0, agg_size = 0;
1885 
1886     if (rtlpriv->cfg->ops->get_btc_status())
1887         btc_ops->btc_get_ampdu_cfg(rtlpriv, &reject_agg,
1888                        &ctrl_agg_size, &agg_size);
1889 
1890     rtl_dbg(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
1891         "Set RX AMPDU: coex - reject=%d, ctrl_agg_size=%d, size=%d",
1892         reject_agg, ctrl_agg_size, agg_size);
1893 
1894     rtlpriv->hw->max_rx_aggregation_subframes =
1895         (ctrl_agg_size ? agg_size : IEEE80211_MAX_AMPDU_BUF_HT);
1896 }
1897 EXPORT_SYMBOL(rtl_rx_ampdu_apply);
1898 
1899 /*********************************************************
1900  *
1901  * wq & timer callback functions
1902  *
1903  *********************************************************/
1904 /* this function is used for roaming */
1905 void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1906 {
1907     struct rtl_priv *rtlpriv = rtl_priv(hw);
1908     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1909 
1910     if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1911         return;
1912 
1913     if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1914         return;
1915 
1916     /* check if this really is a beacon */
1917     if (!ieee80211_is_beacon(hdr->frame_control) &&
1918         !ieee80211_is_probe_resp(hdr->frame_control))
1919         return;
1920 
1921     /* min. beacon length + FCS_LEN */
1922     if (skb->len <= 40 + FCS_LEN)
1923         return;
1924 
1925     /* and only beacons from the associated BSSID, please */
1926     if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1927         return;
1928 
1929     rtlpriv->link_info.bcn_rx_inperiod++;
1930 }
1931 EXPORT_SYMBOL_GPL(rtl_beacon_statistic);
1932 
1933 static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw)
1934 {
1935     struct rtl_priv *rtlpriv = rtl_priv(hw);
1936     struct rtl_bssid_entry *entry, *next;
1937 
1938     list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1939         list_del(&entry->list);
1940         kfree(entry);
1941         rtlpriv->scan_list.num--;
1942     }
1943 }
1944 
1945 static void rtl_free_entries_from_ack_queue(struct ieee80211_hw *hw,
1946                         bool chk_timeout)
1947 {
1948     struct rtl_priv *rtlpriv = rtl_priv(hw);
1949     struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1950     struct sk_buff_head *queue = &tx_report->queue;
1951     struct sk_buff *skb, *tmp;
1952     struct rtlwifi_tx_info *tx_info;
1953 
1954     skb_queue_walk_safe(queue, skb, tmp) {
1955         tx_info = rtl_tx_skb_cb_info(skb);
1956         if (chk_timeout &&
1957             time_after(tx_info->send_time + HZ, jiffies))
1958             continue;
1959         skb_unlink(skb, queue);
1960         rtl_tx_status(hw, skb, false);
1961     }
1962 }
1963 
1964 void rtl_scan_list_expire(struct ieee80211_hw *hw)
1965 {
1966     struct rtl_priv *rtlpriv = rtl_priv(hw);
1967     struct rtl_bssid_entry *entry, *next;
1968     unsigned long flags;
1969 
1970     spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
1971 
1972     list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1973         /* 180 seconds */
1974         if (jiffies_to_msecs(jiffies - entry->age) < 180000)
1975             continue;
1976 
1977         list_del(&entry->list);
1978         rtlpriv->scan_list.num--;
1979 
1980         rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
1981             "BSSID=%pM is expire in scan list (total=%d)\n",
1982             entry->bssid, rtlpriv->scan_list.num);
1983         kfree(entry);
1984     }
1985 
1986     spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
1987 
1988     rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1989 }
1990 
1991 void rtl_collect_scan_list(struct ieee80211_hw *hw, struct sk_buff *skb)
1992 {
1993     struct rtl_priv *rtlpriv = rtl_priv(hw);
1994     struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1995     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1996     unsigned long flags;
1997 
1998     struct rtl_bssid_entry *entry = NULL, *iter;
1999 
2000     /* check if it is scanning */
2001     if (!mac->act_scanning)
2002         return;
2003 
2004     /* check if this really is a beacon */
2005     if (!ieee80211_is_beacon(hdr->frame_control) &&
2006         !ieee80211_is_probe_resp(hdr->frame_control))
2007         return;
2008 
2009     spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
2010 
2011     list_for_each_entry(iter, &rtlpriv->scan_list.list, list) {
2012         if (memcmp(iter->bssid, hdr->addr3, ETH_ALEN) == 0) {
2013             list_del_init(&iter->list);
2014             entry = iter;
2015             rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
2016                 "Update BSSID=%pM to scan list (total=%d)\n",
2017                 hdr->addr3, rtlpriv->scan_list.num);
2018             break;
2019         }
2020     }
2021 
2022     if (!entry) {
2023         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
2024 
2025         if (!entry)
2026             goto label_err;
2027 
2028         memcpy(entry->bssid, hdr->addr3, ETH_ALEN);
2029         rtlpriv->scan_list.num++;
2030 
2031         rtl_dbg(rtlpriv, COMP_SCAN, DBG_LOUD,
2032             "Add BSSID=%pM to scan list (total=%d)\n",
2033             hdr->addr3, rtlpriv->scan_list.num);
2034     }
2035 
2036     entry->age = jiffies;
2037 
2038     list_add_tail(&entry->list, &rtlpriv->scan_list.list);
2039 
2040 label_err:
2041     spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
2042 }
2043 EXPORT_SYMBOL(rtl_collect_scan_list);
2044 
2045 static void rtl_watchdog_wq_callback(struct work_struct *work)
2046 {
2047     struct rtl_works *rtlworks = container_of(work, struct rtl_works,
2048                           watchdog_wq.work);
2049     struct ieee80211_hw *hw = rtlworks->hw;
2050     struct rtl_priv *rtlpriv = rtl_priv(hw);
2051     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2052     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2053     bool busytraffic = false;
2054     bool tx_busy_traffic = false;
2055     bool rx_busy_traffic = false;
2056     bool higher_busytraffic = false;
2057     bool higher_busyrxtraffic = false;
2058     u8 idx, tid;
2059     u32 rx_cnt_inp4eriod = 0;
2060     u32 tx_cnt_inp4eriod = 0;
2061     u32 aver_rx_cnt_inperiod = 0;
2062     u32 aver_tx_cnt_inperiod = 0;
2063     u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
2064     u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
2065 
2066     if (is_hal_stop(rtlhal))
2067         return;
2068 
2069     /* <1> Determine if action frame is allowed */
2070     if (mac->link_state > MAC80211_NOLINK) {
2071         if (mac->cnt_after_linked < 20)
2072             mac->cnt_after_linked++;
2073     } else {
2074         mac->cnt_after_linked = 0;
2075     }
2076 
2077     /* <2> to check if traffic busy, if
2078      * busytraffic we don't change channel
2079      */
2080     if (mac->link_state >= MAC80211_LINKED) {
2081         /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
2082         for (idx = 0; idx <= 2; idx++) {
2083             rtlpriv->link_info.num_rx_in4period[idx] =
2084                 rtlpriv->link_info.num_rx_in4period[idx + 1];
2085             rtlpriv->link_info.num_tx_in4period[idx] =
2086                 rtlpriv->link_info.num_tx_in4period[idx + 1];
2087         }
2088         rtlpriv->link_info.num_rx_in4period[3] =
2089             rtlpriv->link_info.num_rx_inperiod;
2090         rtlpriv->link_info.num_tx_in4period[3] =
2091             rtlpriv->link_info.num_tx_inperiod;
2092         for (idx = 0; idx <= 3; idx++) {
2093             rx_cnt_inp4eriod +=
2094                 rtlpriv->link_info.num_rx_in4period[idx];
2095             tx_cnt_inp4eriod +=
2096                 rtlpriv->link_info.num_tx_in4period[idx];
2097         }
2098         aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
2099         aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
2100 
2101         /* (2) check traffic busy */
2102         if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
2103             busytraffic = true;
2104             if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
2105                 rx_busy_traffic = true;
2106             else
2107                 tx_busy_traffic = false;
2108         }
2109 
2110         /* Higher Tx/Rx data. */
2111         if (aver_rx_cnt_inperiod > 4000 ||
2112             aver_tx_cnt_inperiod > 4000) {
2113             higher_busytraffic = true;
2114 
2115             /* Extremely high Rx data. */
2116             if (aver_rx_cnt_inperiod > 5000)
2117                 higher_busyrxtraffic = true;
2118         }
2119 
2120         /* check every tid's tx traffic */
2121         for (tid = 0; tid <= 7; tid++) {
2122             for (idx = 0; idx <= 2; idx++)
2123                 rtlpriv->link_info.tidtx_in4period[tid][idx] =
2124                     rtlpriv->link_info.tidtx_in4period[tid]
2125                     [idx + 1];
2126             rtlpriv->link_info.tidtx_in4period[tid][3] =
2127                 rtlpriv->link_info.tidtx_inperiod[tid];
2128 
2129             for (idx = 0; idx <= 3; idx++)
2130                 tidtx_inp4eriod[tid] +=
2131                    rtlpriv->link_info.tidtx_in4period[tid][idx];
2132             aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
2133             if (aver_tidtx_inperiod[tid] > 5000)
2134                 rtlpriv->link_info.higher_busytxtraffic[tid] =
2135                                     true;
2136             else
2137                 rtlpriv->link_info.higher_busytxtraffic[tid] =
2138                                     false;
2139         }
2140 
2141         /* PS is controlled by coex. */
2142         if (rtlpriv->cfg->ops->get_btc_status() &&
2143             rtlpriv->btcoexist.btc_ops->btc_is_bt_ctrl_lps(rtlpriv))
2144             goto label_lps_done;
2145 
2146         if (rtlpriv->link_info.num_rx_inperiod +
2147               rtlpriv->link_info.num_tx_inperiod > 8 ||
2148             rtlpriv->link_info.num_rx_inperiod > 2)
2149             rtl_lps_leave(hw, true);
2150         else
2151             rtl_lps_enter(hw, true);
2152 
2153 label_lps_done:
2154         ;
2155     }
2156 
2157     for (tid = 0; tid <= 7; tid++)
2158         rtlpriv->link_info.tidtx_inperiod[tid] = 0;
2159 
2160     rtlpriv->link_info.busytraffic = busytraffic;
2161     rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
2162     rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
2163     rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
2164     rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
2165 
2166     rtlpriv->stats.txbytesunicast_inperiod =
2167         rtlpriv->stats.txbytesunicast -
2168         rtlpriv->stats.txbytesunicast_last;
2169     rtlpriv->stats.rxbytesunicast_inperiod =
2170         rtlpriv->stats.rxbytesunicast -
2171         rtlpriv->stats.rxbytesunicast_last;
2172     rtlpriv->stats.txbytesunicast_last = rtlpriv->stats.txbytesunicast;
2173     rtlpriv->stats.rxbytesunicast_last = rtlpriv->stats.rxbytesunicast;
2174 
2175     rtlpriv->stats.txbytesunicast_inperiod_tp =
2176         (u32)(rtlpriv->stats.txbytesunicast_inperiod * 8 / 2 /
2177         1024 / 1024);
2178     rtlpriv->stats.rxbytesunicast_inperiod_tp =
2179         (u32)(rtlpriv->stats.rxbytesunicast_inperiod * 8 / 2 /
2180         1024 / 1024);
2181 
2182     /* <3> DM */
2183     if (!rtlpriv->cfg->mod_params->disable_watchdog)
2184         rtlpriv->cfg->ops->dm_watchdog(hw);
2185 
2186     /* <4> roaming */
2187     if (mac->link_state == MAC80211_LINKED &&
2188         mac->opmode == NL80211_IFTYPE_STATION) {
2189         if ((rtlpriv->link_info.bcn_rx_inperiod +
2190             rtlpriv->link_info.num_rx_inperiod) == 0) {
2191             rtlpriv->link_info.roam_times++;
2192             rtl_dbg(rtlpriv, COMP_ERR, DBG_DMESG,
2193                 "AP off for %d s\n",
2194                 (rtlpriv->link_info.roam_times * 2));
2195 
2196             /* if we can't recv beacon for 10s,
2197              * we should reconnect this AP
2198              */
2199             if (rtlpriv->link_info.roam_times >= 5) {
2200                 pr_err("AP off, try to reconnect now\n");
2201                 rtlpriv->link_info.roam_times = 0;
2202                 ieee80211_connection_loss(
2203                     rtlpriv->mac80211.vif);
2204             }
2205         } else {
2206             rtlpriv->link_info.roam_times = 0;
2207         }
2208     }
2209 
2210     if (rtlpriv->cfg->ops->get_btc_status())
2211         rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
2212 
2213     if (rtlpriv->btcoexist.btc_info.in_4way) {
2214         if (time_after(jiffies, rtlpriv->btcoexist.btc_info.in_4way_ts +
2215                    msecs_to_jiffies(IN_4WAY_TIMEOUT_TIME)))
2216             rtlpriv->btcoexist.btc_info.in_4way = false;
2217     }
2218 
2219     rtlpriv->link_info.num_rx_inperiod = 0;
2220     rtlpriv->link_info.num_tx_inperiod = 0;
2221     rtlpriv->link_info.bcn_rx_inperiod = 0;
2222 
2223     /* <6> scan list */
2224     rtl_scan_list_expire(hw);
2225 
2226     /* <7> check ack queue */
2227     rtl_free_entries_from_ack_queue(hw, true);
2228 }
2229 
2230 void rtl_watch_dog_timer_callback(struct timer_list *t)
2231 {
2232     struct rtl_priv *rtlpriv = from_timer(rtlpriv, t, works.watchdog_timer);
2233 
2234     queue_delayed_work(rtlpriv->works.rtl_wq,
2235                &rtlpriv->works.watchdog_wq, 0);
2236 
2237     mod_timer(&rtlpriv->works.watchdog_timer,
2238           jiffies + MSECS(RTL_WATCH_DOG_TIME));
2239 }
2240 
2241 static void rtl_fwevt_wq_callback(struct work_struct *work)
2242 {
2243     struct rtl_works *rtlworks = container_of(work, struct rtl_works,
2244                           fwevt_wq.work);
2245     struct ieee80211_hw *hw = rtlworks->hw;
2246     struct rtl_priv *rtlpriv = rtl_priv(hw);
2247 
2248     rtlpriv->cfg->ops->c2h_command_handle(hw);
2249 }
2250 
2251 static void rtl_c2h_content_parsing(struct ieee80211_hw *hw,
2252                     struct sk_buff *skb);
2253 
2254 static bool rtl_c2h_fast_cmd(struct ieee80211_hw *hw, struct sk_buff *skb)
2255 {
2256     u8 cmd_id = GET_C2H_CMD_ID(skb->data);
2257 
2258     switch (cmd_id) {
2259     case C2H_BT_MP:
2260         return true;
2261     default:
2262         break;
2263     }
2264 
2265     return false;
2266 }
2267 
2268 void rtl_c2hcmd_enqueue(struct ieee80211_hw *hw, struct sk_buff *skb)
2269 {
2270     struct rtl_priv *rtlpriv = rtl_priv(hw);
2271 
2272     if (rtl_c2h_fast_cmd(hw, skb)) {
2273         rtl_c2h_content_parsing(hw, skb);
2274         kfree_skb(skb);
2275         return;
2276     }
2277 
2278     /* enqueue */
2279     skb_queue_tail(&rtlpriv->c2hcmd_queue, skb);
2280 
2281     /* wake up wq */
2282     queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.c2hcmd_wq, 0);
2283 }
2284 EXPORT_SYMBOL(rtl_c2hcmd_enqueue);
2285 
2286 static void rtl_c2h_content_parsing(struct ieee80211_hw *hw,
2287                     struct sk_buff *skb)
2288 {
2289     struct rtl_priv *rtlpriv = rtl_priv(hw);
2290     struct rtl_hal_ops *hal_ops = rtlpriv->cfg->ops;
2291     const struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
2292     u8 cmd_id, cmd_len;
2293     u8 *cmd_buf = NULL;
2294 
2295     cmd_id = GET_C2H_CMD_ID(skb->data);
2296     cmd_len = skb->len - C2H_DATA_OFFSET;
2297     cmd_buf = GET_C2H_DATA_PTR(skb->data);
2298 
2299     switch (cmd_id) {
2300     case C2H_DBG:
2301         rtl_dbg(rtlpriv, COMP_FW, DBG_LOUD, "[C2H], C2H_DBG!!\n");
2302         break;
2303     case C2H_TXBF:
2304         rtl_dbg(rtlpriv, COMP_FW, DBG_TRACE,
2305             "[C2H], C2H_TXBF!!\n");
2306         break;
2307     case C2H_TX_REPORT:
2308         rtl_tx_report_handler(hw, cmd_buf, cmd_len);
2309         break;
2310     case C2H_RA_RPT:
2311         if (hal_ops->c2h_ra_report_handler)
2312             hal_ops->c2h_ra_report_handler(hw, cmd_buf, cmd_len);
2313         break;
2314     case C2H_BT_INFO:
2315         rtl_dbg(rtlpriv, COMP_FW, DBG_TRACE,
2316             "[C2H], C2H_BT_INFO!!\n");
2317         if (rtlpriv->cfg->ops->get_btc_status())
2318             btc_ops->btc_btinfo_notify(rtlpriv, cmd_buf, cmd_len);
2319         break;
2320     case C2H_BT_MP:
2321         rtl_dbg(rtlpriv, COMP_FW, DBG_TRACE,
2322             "[C2H], C2H_BT_MP!!\n");
2323         if (rtlpriv->cfg->ops->get_btc_status())
2324             btc_ops->btc_btmpinfo_notify(rtlpriv, cmd_buf, cmd_len);
2325         break;
2326     default:
2327         rtl_dbg(rtlpriv, COMP_FW, DBG_TRACE,
2328             "[C2H], Unknown packet!! cmd_id(%#X)!\n", cmd_id);
2329         break;
2330     }
2331 }
2332 
2333 void rtl_c2hcmd_launcher(struct ieee80211_hw *hw, int exec)
2334 {
2335     struct rtl_priv *rtlpriv = rtl_priv(hw);
2336     struct sk_buff *skb;
2337     int i;
2338 
2339     for (i = 0; i < 200; i++) {
2340         /* dequeue a task */
2341         skb = skb_dequeue(&rtlpriv->c2hcmd_queue);
2342 
2343         /* do it */
2344         if (!skb)
2345             break;
2346 
2347         rtl_dbg(rtlpriv, COMP_FW, DBG_DMESG, "C2H rx_desc_shift=%d\n",
2348             *((u8 *)skb->cb));
2349         RT_PRINT_DATA(rtlpriv, COMP_FW, DBG_DMESG,
2350                   "C2H data: ", skb->data, skb->len);
2351 
2352         if (exec)
2353             rtl_c2h_content_parsing(hw, skb);
2354 
2355         /* free */
2356         dev_kfree_skb_any(skb);
2357     }
2358 }
2359 
2360 static void rtl_c2hcmd_wq_callback(struct work_struct *work)
2361 {
2362     struct rtl_works *rtlworks = container_of(work, struct rtl_works,
2363                           c2hcmd_wq.work);
2364     struct ieee80211_hw *hw = rtlworks->hw;
2365 
2366     rtl_c2hcmd_launcher(hw, 1);
2367 }
2368 
2369 void rtl_easy_concurrent_retrytimer_callback(struct timer_list *t)
2370 {
2371     struct rtl_priv *rtlpriv =
2372         from_timer(rtlpriv, t, works.dualmac_easyconcurrent_retrytimer);
2373     struct ieee80211_hw *hw = rtlpriv->hw;
2374     struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
2375 
2376     if (buddy_priv == NULL)
2377         return;
2378 
2379     rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
2380 }
2381 
2382 /*********************************************************
2383  *
2384  * frame process functions
2385  *
2386  *********************************************************/
2387 u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
2388 {
2389     struct ieee80211_mgmt *mgmt = (void *)data;
2390     u8 *pos, *end;
2391 
2392     pos = (u8 *)mgmt->u.beacon.variable;
2393     end = data + len;
2394     while (pos < end) {
2395         if (pos + 2 + pos[1] > end)
2396             return NULL;
2397 
2398         if (pos[0] == ie)
2399             return pos;
2400 
2401         pos += 2 + pos[1];
2402     }
2403     return NULL;
2404 }
2405 
2406 /* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
2407 /* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
2408 static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
2409                      enum ieee80211_smps_mode smps,
2410                      u8 *da, u8 *bssid)
2411 {
2412     struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2413     struct sk_buff *skb;
2414     struct ieee80211_mgmt *action_frame;
2415 
2416     /* 27 = header + category + action + smps mode */
2417     skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
2418     if (!skb)
2419         return NULL;
2420 
2421     skb_reserve(skb, hw->extra_tx_headroom);
2422     action_frame = skb_put_zero(skb, 27);
2423     memcpy(action_frame->da, da, ETH_ALEN);
2424     memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
2425     memcpy(action_frame->bssid, bssid, ETH_ALEN);
2426     action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2427                           IEEE80211_STYPE_ACTION);
2428     action_frame->u.action.category = WLAN_CATEGORY_HT;
2429     action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
2430     switch (smps) {
2431     case IEEE80211_SMPS_AUTOMATIC:/* 0 */
2432     case IEEE80211_SMPS_NUM_MODES:/* 4 */
2433         WARN_ON(1);
2434         fallthrough;
2435     case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
2436         action_frame->u.action.u.ht_smps.smps_control =
2437                 WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
2438         break;
2439     case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
2440         action_frame->u.action.u.ht_smps.smps_control =
2441                 WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
2442         break;
2443     case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
2444         action_frame->u.action.u.ht_smps.smps_control =
2445                 WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
2446         break;
2447     }
2448 
2449     return skb;
2450 }
2451 
2452 int rtl_send_smps_action(struct ieee80211_hw *hw,
2453              struct ieee80211_sta *sta,
2454              enum ieee80211_smps_mode smps)
2455 {
2456     struct rtl_priv *rtlpriv = rtl_priv(hw);
2457     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2458     struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2459     struct sk_buff *skb = NULL;
2460     struct rtl_tcb_desc tcb_desc;
2461     u8 bssid[ETH_ALEN] = {0};
2462 
2463     memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
2464 
2465     if (rtlpriv->mac80211.act_scanning)
2466         goto err_free;
2467 
2468     if (!sta)
2469         goto err_free;
2470 
2471     if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
2472         goto err_free;
2473 
2474     if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
2475         goto err_free;
2476 
2477     if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
2478         memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
2479     else
2480         memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
2481 
2482     skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
2483     /* this is a type = mgmt * stype = action frame */
2484     if (skb) {
2485         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2486         struct rtl_sta_info *sta_entry =
2487             (struct rtl_sta_info *) sta->drv_priv;
2488         sta_entry->mimo_ps = smps;
2489         /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true); */
2490 
2491         info->control.rates[0].idx = 0;
2492         info->band = hw->conf.chandef.chan->band;
2493         rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
2494     }
2495     return 1;
2496 
2497 err_free:
2498     return 0;
2499 }
2500 EXPORT_SYMBOL(rtl_send_smps_action);
2501 
2502 void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
2503 {
2504     struct rtl_priv *rtlpriv = rtl_priv(hw);
2505     struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2506     enum io_type iotype;
2507 
2508     if (!is_hal_stop(rtlhal)) {
2509         switch (operation) {
2510         case SCAN_OPT_BACKUP:
2511             iotype = IO_CMD_PAUSE_DM_BY_SCAN;
2512             rtlpriv->cfg->ops->set_hw_reg(hw,
2513                               HW_VAR_IO_CMD,
2514                               (u8 *)&iotype);
2515             break;
2516         case SCAN_OPT_RESTORE:
2517             iotype = IO_CMD_RESUME_DM_BY_SCAN;
2518             rtlpriv->cfg->ops->set_hw_reg(hw,
2519                               HW_VAR_IO_CMD,
2520                               (u8 *)&iotype);
2521             break;
2522         default:
2523             pr_err("Unknown Scan Backup operation.\n");
2524             break;
2525         }
2526     }
2527 }
2528 EXPORT_SYMBOL(rtl_phy_scan_operation_backup);
2529 
2530 /* because mac80211 have issues when can receive del ba
2531  * so here we just make a fake del_ba if we receive a ba_req
2532  * but rx_agg was opened to let mac80211 release some ba
2533  * related resources, so please this del_ba for tx
2534  */
2535 struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
2536                 u8 *sa, u8 *bssid, u16 tid)
2537 {
2538     struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2539     struct sk_buff *skb;
2540     struct ieee80211_mgmt *action_frame;
2541     u16 params;
2542 
2543     /* 27 = header + category + action + smps mode */
2544     skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
2545     if (!skb)
2546         return NULL;
2547 
2548     skb_reserve(skb, hw->extra_tx_headroom);
2549     action_frame = skb_put_zero(skb, 34);
2550     memcpy(action_frame->sa, sa, ETH_ALEN);
2551     memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
2552     memcpy(action_frame->bssid, bssid, ETH_ALEN);
2553     action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2554                           IEEE80211_STYPE_ACTION);
2555     action_frame->u.action.category = WLAN_CATEGORY_BACK;
2556     action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
2557     params = (u16)(1 << 11);    /* bit 11 initiator */
2558     params |= (u16)(tid << 12); /* bit 15:12 TID number */
2559 
2560     action_frame->u.action.u.delba.params = cpu_to_le16(params);
2561     action_frame->u.action.u.delba.reason_code =
2562         cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
2563 
2564     return skb;
2565 }
2566 
2567 /*********************************************************
2568  *
2569  * IOT functions
2570  *
2571  *********************************************************/
2572 static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
2573                   struct octet_string vendor_ie)
2574 {
2575     struct rtl_priv *rtlpriv = rtl_priv(hw);
2576     bool matched = false;
2577     static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
2578     static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
2579     static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
2580     static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
2581     static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
2582     static u8 racap[] = { 0x00, 0x0c, 0x43 };
2583     static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
2584     static u8 marvcap[] = { 0x00, 0x50, 0x43 };
2585 
2586     if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
2587         memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
2588         rtlpriv->mac80211.vendor = PEER_ATH;
2589         matched = true;
2590     } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
2591         memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
2592         memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
2593         rtlpriv->mac80211.vendor = PEER_BROAD;
2594         matched = true;
2595     } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
2596         rtlpriv->mac80211.vendor = PEER_RAL;
2597         matched = true;
2598     } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
2599         rtlpriv->mac80211.vendor = PEER_CISCO;
2600         matched = true;
2601     } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
2602         rtlpriv->mac80211.vendor = PEER_MARV;
2603         matched = true;
2604     }
2605 
2606     return matched;
2607 }
2608 
2609 static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
2610         unsigned int len)
2611 {
2612     struct ieee80211_mgmt *mgmt = (void *)data;
2613     struct octet_string vendor_ie;
2614     u8 *pos, *end;
2615 
2616     pos = (u8 *)mgmt->u.beacon.variable;
2617     end = data + len;
2618     while (pos < end) {
2619         if (pos[0] == 221) {
2620             vendor_ie.length = pos[1];
2621             vendor_ie.octet = &pos[2];
2622             if (rtl_chk_vendor_ouisub(hw, vendor_ie))
2623                 return true;
2624         }
2625 
2626         if (pos + 2 + pos[1] > end)
2627             return false;
2628 
2629         pos += 2 + pos[1];
2630     }
2631     return false;
2632 }
2633 
2634 void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
2635 {
2636     struct rtl_priv *rtlpriv = rtl_priv(hw);
2637     struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2638     struct ieee80211_hdr *hdr = (void *)data;
2639     u32 vendor = PEER_UNKNOWN;
2640 
2641     static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
2642     static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
2643     static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
2644     static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
2645     static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
2646     static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
2647     static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
2648     static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
2649     static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
2650     static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
2651     static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
2652     static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
2653     static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
2654     static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
2655     static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
2656     static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
2657 
2658     if (mac->opmode != NL80211_IFTYPE_STATION)
2659         return;
2660 
2661     if (mac->link_state == MAC80211_NOLINK) {
2662         mac->vendor = PEER_UNKNOWN;
2663         return;
2664     }
2665 
2666     if (mac->cnt_after_linked > 2)
2667         return;
2668 
2669     /* check if this really is a beacon */
2670     if (!ieee80211_is_beacon(hdr->frame_control))
2671         return;
2672 
2673     /* min. beacon length + FCS_LEN */
2674     if (len <= 40 + FCS_LEN)
2675         return;
2676 
2677     /* and only beacons from the associated BSSID, please */
2678     if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
2679         return;
2680 
2681     if (rtl_find_221_ie(hw, data, len))
2682         vendor = mac->vendor;
2683 
2684     if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
2685         (memcmp(mac->bssid, ap5_2, 3) == 0) ||
2686         (memcmp(mac->bssid, ap5_3, 3) == 0) ||
2687         (memcmp(mac->bssid, ap5_4, 3) == 0) ||
2688         (memcmp(mac->bssid, ap5_5, 3) == 0) ||
2689         (memcmp(mac->bssid, ap5_6, 3) == 0) ||
2690         vendor == PEER_ATH) {
2691         vendor = PEER_ATH;
2692         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
2693     } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
2694         (memcmp(mac->bssid, ap4_5, 3) == 0) ||
2695         (memcmp(mac->bssid, ap4_1, 3) == 0) ||
2696         (memcmp(mac->bssid, ap4_2, 3) == 0) ||
2697         (memcmp(mac->bssid, ap4_3, 3) == 0) ||
2698         vendor == PEER_RAL) {
2699         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
2700         vendor = PEER_RAL;
2701     } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
2702         vendor == PEER_CISCO) {
2703         vendor = PEER_CISCO;
2704         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
2705     } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
2706         (memcmp(mac->bssid, ap3_2, 3) == 0) ||
2707         (memcmp(mac->bssid, ap3_3, 3) == 0) ||
2708         vendor == PEER_BROAD) {
2709         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
2710         vendor = PEER_BROAD;
2711     } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
2712         vendor == PEER_MARV) {
2713         vendor = PEER_MARV;
2714         rtl_dbg(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
2715     }
2716 
2717     mac->vendor = vendor;
2718 }
2719 EXPORT_SYMBOL_GPL(rtl_recognize_peer);
2720 
2721 MODULE_AUTHOR("lizhaoming   <chaoming_li@realsil.com.cn>");
2722 MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
2723 MODULE_AUTHOR("Larry Finger <Larry.FInger@lwfinger.net>");
2724 MODULE_LICENSE("GPL");
2725 MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2726 
2727 struct rtl_global_var rtl_global_var = {};
2728 EXPORT_SYMBOL_GPL(rtl_global_var);
2729 
2730 static int __init rtl_core_module_init(void)
2731 {
2732     BUILD_BUG_ON(TX_PWR_BY_RATE_NUM_RATE < TX_PWR_BY_RATE_NUM_SECTION);
2733     BUILD_BUG_ON(MAX_RATE_SECTION_NUM != MAX_RATE_SECTION);
2734     BUILD_BUG_ON(MAX_BASE_NUM_IN_PHY_REG_PG_24G != MAX_RATE_SECTION);
2735     BUILD_BUG_ON(MAX_BASE_NUM_IN_PHY_REG_PG_5G != (MAX_RATE_SECTION - 1));
2736 
2737     if (rtl_rate_control_register())
2738         pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
2739 
2740     /* add debugfs */
2741     rtl_debugfs_add_topdir();
2742 
2743     /* init some global vars */
2744     INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2745     spin_lock_init(&rtl_global_var.glb_list_lock);
2746 
2747     return 0;
2748 }
2749 
2750 static void __exit rtl_core_module_exit(void)
2751 {
2752     /*RC*/
2753     rtl_rate_control_unregister();
2754 
2755     /* remove debugfs */
2756     rtl_debugfs_remove_topdir();
2757 }
2758 
2759 module_init(rtl_core_module_init);
2760 module_exit(rtl_core_module_exit);