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