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