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(struct ieee80211_hw *hw, u8 rate_index,
 689                            enum wireless_mode wirelessmode)
 690{
 691        struct rtl_priv *rtlpriv = rtl_priv(hw);
 692        struct rtl_phy *rtlphy = &rtlpriv->phy;
 693        u8 ret = 0;
 694
 695        switch (rate_index) {
 696        case RATR_INX_WIRELESS_NGB:
 697                if (rtlphy->rf_type == RF_1T1R)
 698                        ret = RATEID_IDX_BGN_40M_1SS;
 699                else
 700                        ret = RATEID_IDX_BGN_40M_2SS;
 701                break;
 702        case RATR_INX_WIRELESS_N:
 703        case RATR_INX_WIRELESS_NG:
 704                if (rtlphy->rf_type == RF_1T1R)
 705                        ret = RATEID_IDX_GN_N1SS;
 706                else
 707                        ret = RATEID_IDX_GN_N2SS;
 708                break;
 709        case RATR_INX_WIRELESS_NB:
 710                if (rtlphy->rf_type == RF_1T1R)
 711                        ret = RATEID_IDX_BGN_20M_1SS_BN;
 712                else
 713                        ret = RATEID_IDX_BGN_20M_2SS_BN;
 714                break;
 715        case RATR_INX_WIRELESS_GB:
 716                ret = RATEID_IDX_BG;
 717                break;
 718        case RATR_INX_WIRELESS_G:
 719                ret = RATEID_IDX_G;
 720                break;
 721        case RATR_INX_WIRELESS_B:
 722                ret = RATEID_IDX_B;
 723                break;
 724        case RATR_INX_WIRELESS_MC:
 725                if (wirelessmode == WIRELESS_MODE_B ||
 726                    wirelessmode == WIRELESS_MODE_G ||
 727                    wirelessmode == WIRELESS_MODE_N_24G ||
 728                    wirelessmode == WIRELESS_MODE_AC_24G)
 729                        ret = RATEID_IDX_BG;
 730                else
 731                        ret = RATEID_IDX_G;
 732                break;
 733        case RATR_INX_WIRELESS_AC_5N:
 734                if (rtlphy->rf_type == RF_1T1R)
 735                        ret = RATEID_IDX_VHT_1SS;
 736                else
 737                        ret = RATEID_IDX_VHT_2SS;
 738                break;
 739        case RATR_INX_WIRELESS_AC_24N:
 740                if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
 741                        if (rtlphy->rf_type == RF_1T1R)
 742                                ret = RATEID_IDX_VHT_1SS;
 743                        else
 744                                ret = RATEID_IDX_VHT_2SS;
 745                } else {
 746                        if (rtlphy->rf_type == RF_1T1R)
 747                                ret = RATEID_IDX_MIX1;
 748                        else
 749                                ret = RATEID_IDX_MIX2;
 750                }
 751                break;
 752        default:
 753                ret = RATEID_IDX_BGN_40M_2SS;
 754                break;
 755        }
 756        return ret;
 757}
 758
 759static inline u8 _rtl_rate_id(struct ieee80211_hw *hw,
 760                              struct rtl_sta_info *sta_entry,
 761                              int rate_index)
 762{
 763        struct rtl_priv *rtlpriv = rtl_priv(hw);
 764
 765        if (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID) {
 766                int wireless_mode = sta_entry ?
 767                        sta_entry->wireless_mode : WIRELESS_MODE_G;
 768
 769                return rtl_mrate_idx_to_arfr_id(hw, rate_index, wireless_mode);
 770        } else {
 771                return rate_index;
 772        }
 773}
 774
 775static void _rtl_txrate_selectmode(struct ieee80211_hw *hw,
 776                                   struct ieee80211_sta *sta,
 777                                   struct rtl_tcb_desc *tcb_desc)
 778{
 779        struct rtl_priv *rtlpriv = rtl_priv(hw);
 780        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 781        struct rtl_sta_info *sta_entry = NULL;
 782        u8 ratr_index = _rtl_rate_id(hw, sta_entry, RATR_INX_WIRELESS_MC);
 783
 784        if (sta) {
 785                sta_entry = (struct rtl_sta_info *)sta->drv_priv;
 786                ratr_index = sta_entry->ratr_index;
 787        }
 788        if (!tcb_desc->disable_ratefallback || !tcb_desc->use_driver_rate) {
 789                if (mac->opmode == NL80211_IFTYPE_STATION) {
 790                        tcb_desc->ratr_index = 0;
 791                } else if (mac->opmode == NL80211_IFTYPE_ADHOC ||
 792                                mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 793                        if (tcb_desc->multicast || tcb_desc->broadcast) {
 794                                tcb_desc->hw_rate =
 795                                    rtlpriv->cfg->maps[RTL_RC_CCK_RATE2M];
 796                                tcb_desc->use_driver_rate = 1;
 797                                tcb_desc->ratr_index =
 798                                        _rtl_rate_id(hw, sta_entry,
 799                                                     RATR_INX_WIRELESS_MC);
 800                        } else {
 801                                tcb_desc->ratr_index = ratr_index;
 802                        }
 803                } else if (mac->opmode == NL80211_IFTYPE_AP) {
 804                        tcb_desc->ratr_index = ratr_index;
 805                }
 806        }
 807
 808        if (rtlpriv->dm.useramask) {
 809                tcb_desc->ratr_index = ratr_index;
 810                /* TODO we will differentiate adhoc and station future  */
 811                if (mac->opmode == NL80211_IFTYPE_STATION ||
 812                    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 813                        tcb_desc->mac_id = 0;
 814
 815                        if (sta &&
 816                            (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_RATEID))
 817                                ;       /* use sta_entry->ratr_index */
 818                        else if (mac->mode == WIRELESS_MODE_AC_5G)
 819                                tcb_desc->ratr_index =
 820                                        _rtl_rate_id(hw, sta_entry,
 821                                                     RATR_INX_WIRELESS_AC_5N);
 822                        else if (mac->mode == WIRELESS_MODE_AC_24G)
 823                                tcb_desc->ratr_index =
 824                                        _rtl_rate_id(hw, sta_entry,
 825                                                     RATR_INX_WIRELESS_AC_24N);
 826                        else if (mac->mode == WIRELESS_MODE_N_24G)
 827                                tcb_desc->ratr_index =
 828                                        _rtl_rate_id(hw, sta_entry,
 829                                                     RATR_INX_WIRELESS_NGB);
 830                        else if (mac->mode == WIRELESS_MODE_N_5G)
 831                                tcb_desc->ratr_index =
 832                                        _rtl_rate_id(hw, sta_entry,
 833                                                     RATR_INX_WIRELESS_NG);
 834                        else if (mac->mode & WIRELESS_MODE_G)
 835                                tcb_desc->ratr_index =
 836                                        _rtl_rate_id(hw, sta_entry,
 837                                                     RATR_INX_WIRELESS_GB);
 838                        else if (mac->mode & WIRELESS_MODE_B)
 839                                tcb_desc->ratr_index =
 840                                        _rtl_rate_id(hw, sta_entry,
 841                                                     RATR_INX_WIRELESS_B);
 842                        else if (mac->mode & WIRELESS_MODE_A)
 843                                tcb_desc->ratr_index =
 844                                        _rtl_rate_id(hw, sta_entry,
 845                                                     RATR_INX_WIRELESS_G);
 846
 847                } else if (mac->opmode == NL80211_IFTYPE_AP ||
 848                        mac->opmode == NL80211_IFTYPE_ADHOC) {
 849                        if (sta) {
 850                                if (sta->aid > 0)
 851                                        tcb_desc->mac_id = sta->aid + 1;
 852                                else
 853                                        tcb_desc->mac_id = 1;
 854                        } else {
 855                                tcb_desc->mac_id = 0;
 856                        }
 857                }
 858        }
 859}
 860
 861static void _rtl_query_bandwidth_mode(struct ieee80211_hw *hw,
 862                                      struct ieee80211_sta *sta,
 863                                      struct rtl_tcb_desc *tcb_desc)
 864{
 865        struct rtl_priv *rtlpriv = rtl_priv(hw);
 866        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 867
 868        tcb_desc->packet_bw = false;
 869        if (!sta)
 870                return;
 871        if (mac->opmode == NL80211_IFTYPE_AP ||
 872            mac->opmode == NL80211_IFTYPE_ADHOC ||
 873            mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 874                if (!(sta->ht_cap.ht_supported) ||
 875                    !(sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
 876                        return;
 877        } else if (mac->opmode == NL80211_IFTYPE_STATION) {
 878                if (!mac->bw_40 || !(sta->ht_cap.ht_supported))
 879                        return;
 880        }
 881        if (tcb_desc->multicast || tcb_desc->broadcast)
 882                return;
 883
 884        /*use legency rate, shall use 20MHz */
 885        if (tcb_desc->hw_rate <= rtlpriv->cfg->maps[RTL_RC_OFDM_RATE54M])
 886                return;
 887
 888        tcb_desc->packet_bw = HT_CHANNEL_WIDTH_20_40;
 889
 890        if (rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8812AE ||
 891            rtlpriv->rtlhal.hw_type == HARDWARE_TYPE_RTL8821AE ||
 892            (rtlpriv->cfg->spec_ver & RTL_SPEC_SUPPORT_VHT)) {
 893                if (mac->opmode == NL80211_IFTYPE_AP ||
 894                    mac->opmode == NL80211_IFTYPE_ADHOC ||
 895                    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
 896                        if (!(sta->vht_cap.vht_supported))
 897                                return;
 898                } else if (mac->opmode == NL80211_IFTYPE_STATION) {
 899                        if (!mac->bw_80 ||
 900                            !(sta->vht_cap.vht_supported))
 901                                return;
 902                }
 903                if (tcb_desc->hw_rate <=
 904                        rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15])
 905                        return;
 906                tcb_desc->packet_bw = HT_CHANNEL_WIDTH_80;
 907        }
 908}
 909
 910static u8 _rtl_get_vht_highest_n_rate(struct ieee80211_hw *hw,
 911                                      struct ieee80211_sta *sta)
 912{
 913        struct rtl_priv *rtlpriv = rtl_priv(hw);
 914        struct rtl_phy *rtlphy = &rtlpriv->phy;
 915        u8 hw_rate;
 916        u16 tx_mcs_map = le16_to_cpu(sta->vht_cap.vht_mcs.tx_mcs_map);
 917
 918        if ((get_rf_type(rtlphy) == RF_2T2R) &&
 919            (tx_mcs_map & 0x000c) != 0x000c) {
 920                if ((tx_mcs_map & 0x000c) >> 2 ==
 921                        IEEE80211_VHT_MCS_SUPPORT_0_7)
 922                        hw_rate =
 923                        rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS7];
 924                else if ((tx_mcs_map  & 0x000c) >> 2 ==
 925                        IEEE80211_VHT_MCS_SUPPORT_0_8)
 926                        hw_rate =
 927                        rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS8];
 928                else
 929                        hw_rate =
 930                        rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
 931        } else {
 932                if ((tx_mcs_map  & 0x0003) ==
 933                        IEEE80211_VHT_MCS_SUPPORT_0_7)
 934                        hw_rate =
 935                        rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS7];
 936                else if ((tx_mcs_map  & 0x0003) ==
 937                        IEEE80211_VHT_MCS_SUPPORT_0_8)
 938                        hw_rate =
 939                        rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS8];
 940                else
 941                        hw_rate =
 942                        rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
 943        }
 944
 945        return hw_rate;
 946}
 947
 948static u8 _rtl_get_highest_n_rate(struct ieee80211_hw *hw,
 949                                  struct ieee80211_sta *sta)
 950{
 951        struct rtl_priv *rtlpriv = rtl_priv(hw);
 952        struct rtl_phy *rtlphy = &rtlpriv->phy;
 953        u8 hw_rate;
 954
 955        if (get_rf_type(rtlphy) == RF_2T2R &&
 956            sta->ht_cap.mcs.rx_mask[1] != 0)
 957                hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15];
 958        else
 959                hw_rate = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS7];
 960
 961        return hw_rate;
 962}
 963
 964/* mac80211's rate_idx is like this:
 965 *
 966 * 2.4G band:rx_status->band == NL80211_BAND_2GHZ
 967 *
 968 * B/G rate:
 969 * (rx_status->flag & RX_FLAG_HT) = 0,
 970 * DESC_RATE1M-->DESC_RATE54M ==> idx is 0-->11,
 971 *
 972 * N rate:
 973 * (rx_status->flag & RX_FLAG_HT) = 1,
 974 * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
 975 *
 976 * 5G band:rx_status->band == NL80211_BAND_5GHZ
 977 * A rate:
 978 * (rx_status->flag & RX_FLAG_HT) = 0,
 979 * DESC_RATE6M-->DESC_RATE54M ==> idx is 0-->7,
 980 *
 981 * N rate:
 982 * (rx_status->flag & RX_FLAG_HT) = 1,
 983 * DESC_RATEMCS0-->DESC_RATEMCS15 ==> idx is 0-->15
 984 *
 985 * VHT rates:
 986 * DESC_RATEVHT1SS_MCS0-->DESC_RATEVHT1SS_MCS9 ==> idx is 0-->9
 987 * DESC_RATEVHT2SS_MCS0-->DESC_RATEVHT2SS_MCS9 ==> idx is 0-->9
 988 */
 989int rtlwifi_rate_mapping(struct ieee80211_hw *hw, bool isht, bool isvht,
 990                         u8 desc_rate)
 991{
 992        int rate_idx;
 993
 994        if (isvht) {
 995                switch (desc_rate) {
 996                case DESC_RATEVHT1SS_MCS0:
 997                        rate_idx = 0;
 998                        break;
 999                case DESC_RATEVHT1SS_MCS1:
1000                        rate_idx = 1;
1001                        break;
1002                case DESC_RATEVHT1SS_MCS2:
1003                        rate_idx = 2;
1004                        break;
1005                case DESC_RATEVHT1SS_MCS3:
1006                        rate_idx = 3;
1007                        break;
1008                case DESC_RATEVHT1SS_MCS4:
1009                        rate_idx = 4;
1010                        break;
1011                case DESC_RATEVHT1SS_MCS5:
1012                        rate_idx = 5;
1013                        break;
1014                case DESC_RATEVHT1SS_MCS6:
1015                        rate_idx = 6;
1016                        break;
1017                case DESC_RATEVHT1SS_MCS7:
1018                        rate_idx = 7;
1019                        break;
1020                case DESC_RATEVHT1SS_MCS8:
1021                        rate_idx = 8;
1022                        break;
1023                case DESC_RATEVHT1SS_MCS9:
1024                        rate_idx = 9;
1025                        break;
1026                case DESC_RATEVHT2SS_MCS0:
1027                        rate_idx = 0;
1028                        break;
1029                case DESC_RATEVHT2SS_MCS1:
1030                        rate_idx = 1;
1031                        break;
1032                case DESC_RATEVHT2SS_MCS2:
1033                        rate_idx = 2;
1034                        break;
1035                case DESC_RATEVHT2SS_MCS3:
1036                        rate_idx = 3;
1037                        break;
1038                case DESC_RATEVHT2SS_MCS4:
1039                        rate_idx = 4;
1040                        break;
1041                case DESC_RATEVHT2SS_MCS5:
1042                        rate_idx = 5;
1043                        break;
1044                case DESC_RATEVHT2SS_MCS6:
1045                        rate_idx = 6;
1046                        break;
1047                case DESC_RATEVHT2SS_MCS7:
1048                        rate_idx = 7;
1049                        break;
1050                case DESC_RATEVHT2SS_MCS8:
1051                        rate_idx = 8;
1052                        break;
1053                case DESC_RATEVHT2SS_MCS9:
1054                        rate_idx = 9;
1055                        break;
1056                default:
1057                        rate_idx = 0;
1058                        break;
1059                }
1060                return rate_idx;
1061        }
1062        if (!isht) {
1063                if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ) {
1064                        switch (desc_rate) {
1065                        case DESC_RATE1M:
1066                                rate_idx = 0;
1067                                break;
1068                        case DESC_RATE2M:
1069                                rate_idx = 1;
1070                                break;
1071                        case DESC_RATE5_5M:
1072                                rate_idx = 2;
1073                                break;
1074                        case DESC_RATE11M:
1075                                rate_idx = 3;
1076                                break;
1077                        case DESC_RATE6M:
1078                                rate_idx = 4;
1079                                break;
1080                        case DESC_RATE9M:
1081                                rate_idx = 5;
1082                                break;
1083                        case DESC_RATE12M:
1084                                rate_idx = 6;
1085                                break;
1086                        case DESC_RATE18M:
1087                                rate_idx = 7;
1088                                break;
1089                        case DESC_RATE24M:
1090                                rate_idx = 8;
1091                                break;
1092                        case DESC_RATE36M:
1093                                rate_idx = 9;
1094                                break;
1095                        case DESC_RATE48M:
1096                                rate_idx = 10;
1097                                break;
1098                        case DESC_RATE54M:
1099                                rate_idx = 11;
1100                                break;
1101                        default:
1102                                rate_idx = 0;
1103                                break;
1104                        }
1105                } else {
1106                        switch (desc_rate) {
1107                        case DESC_RATE6M:
1108                                rate_idx = 0;
1109                                break;
1110                        case DESC_RATE9M:
1111                                rate_idx = 1;
1112                                break;
1113                        case DESC_RATE12M:
1114                                rate_idx = 2;
1115                                break;
1116                        case DESC_RATE18M:
1117                                rate_idx = 3;
1118                                break;
1119                        case DESC_RATE24M:
1120                                rate_idx = 4;
1121                                break;
1122                        case DESC_RATE36M:
1123                                rate_idx = 5;
1124                                break;
1125                        case DESC_RATE48M:
1126                                rate_idx = 6;
1127                                break;
1128                        case DESC_RATE54M:
1129                                rate_idx = 7;
1130                                break;
1131                        default:
1132                                rate_idx = 0;
1133                                break;
1134                        }
1135                }
1136        } else {
1137                switch (desc_rate) {
1138                case DESC_RATEMCS0:
1139                        rate_idx = 0;
1140                        break;
1141                case DESC_RATEMCS1:
1142                        rate_idx = 1;
1143                        break;
1144                case DESC_RATEMCS2:
1145                        rate_idx = 2;
1146                        break;
1147                case DESC_RATEMCS3:
1148                        rate_idx = 3;
1149                        break;
1150                case DESC_RATEMCS4:
1151                        rate_idx = 4;
1152                        break;
1153                case DESC_RATEMCS5:
1154                        rate_idx = 5;
1155                        break;
1156                case DESC_RATEMCS6:
1157                        rate_idx = 6;
1158                        break;
1159                case DESC_RATEMCS7:
1160                        rate_idx = 7;
1161                        break;
1162                case DESC_RATEMCS8:
1163                        rate_idx = 8;
1164                        break;
1165                case DESC_RATEMCS9:
1166                        rate_idx = 9;
1167                        break;
1168                case DESC_RATEMCS10:
1169                        rate_idx = 10;
1170                        break;
1171                case DESC_RATEMCS11:
1172                        rate_idx = 11;
1173                        break;
1174                case DESC_RATEMCS12:
1175                        rate_idx = 12;
1176                        break;
1177                case DESC_RATEMCS13:
1178                        rate_idx = 13;
1179                        break;
1180                case DESC_RATEMCS14:
1181                        rate_idx = 14;
1182                        break;
1183                case DESC_RATEMCS15:
1184                        rate_idx = 15;
1185                        break;
1186                default:
1187                        rate_idx = 0;
1188                        break;
1189                }
1190        }
1191        return rate_idx;
1192}
1193
1194static u8 _rtl_get_tx_hw_rate(struct ieee80211_hw *hw,
1195                              struct ieee80211_tx_info *info)
1196{
1197        struct rtl_priv *rtlpriv = rtl_priv(hw);
1198        struct ieee80211_tx_rate *r = &info->status.rates[0];
1199        struct ieee80211_rate *txrate;
1200        u8 hw_value = 0x0;
1201
1202        if (r->flags & IEEE80211_TX_RC_MCS) {
1203                /* HT MCS0-15 */
1204                hw_value = rtlpriv->cfg->maps[RTL_RC_HT_RATEMCS15] - 15 +
1205                           r->idx;
1206        } else if (r->flags & IEEE80211_TX_RC_VHT_MCS) {
1207                /* VHT MCS0-9, NSS */
1208                if (ieee80211_rate_get_vht_nss(r) == 2)
1209                        hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_2SS_MCS9];
1210                else
1211                        hw_value = rtlpriv->cfg->maps[RTL_RC_VHT_RATE_1SS_MCS9];
1212
1213                hw_value = hw_value - 9 + ieee80211_rate_get_vht_mcs(r);
1214        } else {
1215                /* legacy */
1216                txrate = ieee80211_get_tx_rate(hw, info);
1217
1218                if (txrate)
1219                        hw_value = txrate->hw_value;
1220        }
1221
1222        /* check 5G band */
1223        if (rtlpriv->rtlhal.current_bandtype == BAND_ON_5G &&
1224            hw_value < rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M])
1225                hw_value = rtlpriv->cfg->maps[RTL_RC_OFDM_RATE6M];
1226
1227        return hw_value;
1228}
1229
1230void rtl_get_tcb_desc(struct ieee80211_hw *hw,
1231                      struct ieee80211_tx_info *info,
1232                      struct ieee80211_sta *sta,
1233                      struct sk_buff *skb, struct rtl_tcb_desc *tcb_desc)
1234{
1235        struct rtl_priv *rtlpriv = rtl_priv(hw);
1236        struct rtl_mac *rtlmac = rtl_mac(rtl_priv(hw));
1237        struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1238        struct rtl_sta_info *sta_entry =
1239                (sta ? (struct rtl_sta_info *)sta->drv_priv : NULL);
1240
1241        __le16 fc = rtl_get_fc(skb);
1242
1243        tcb_desc->hw_rate = _rtl_get_tx_hw_rate(hw, info);
1244
1245        if (rtl_is_tx_report_skb(hw, skb))
1246                tcb_desc->use_spe_rpt = 1;
1247
1248        if (!ieee80211_is_data(fc)) {
1249                tcb_desc->use_driver_rate = true;
1250                tcb_desc->ratr_index = _rtl_rate_id(hw, sta_entry,
1251                                                    RATR_INX_WIRELESS_MC);
1252                tcb_desc->disable_ratefallback = 1;
1253                tcb_desc->mac_id = 0;
1254                tcb_desc->packet_bw = false;
1255
1256                return;
1257        }
1258
1259        /*
1260         * We set data rate INX 0
1261         * in rtl_rc.c if skb is special data or
1262         * mgt which need low data rate.
1263         */
1264
1265        /*
1266         * So tcb_desc->hw_rate is just used for
1267         * special data and mgt frames
1268         */
1269        if (info->control.rates[0].idx == 0 || ieee80211_is_nullfunc(fc)) {
1270                tcb_desc->use_driver_rate = true;
1271                tcb_desc->ratr_index = _rtl_rate_id(hw, sta_entry,
1272                                                    RATR_INX_WIRELESS_MC);
1273
1274                tcb_desc->disable_ratefallback = 1;
1275        } else if (sta && sta->vht_cap.vht_supported) {
1276                /*
1277                 * Because hw will never use hw_rate
1278                 * when tcb_desc->use_driver_rate = false
1279                 * so we never set highest N rate here,
1280                 * and N rate will all be controlled by FW
1281                 * when tcb_desc->use_driver_rate = false
1282                 */
1283                tcb_desc->hw_rate = _rtl_get_vht_highest_n_rate(hw, sta);
1284        } else if (sta && sta->ht_cap.ht_supported) {
1285                tcb_desc->hw_rate = _rtl_get_highest_n_rate(hw, sta);
1286        } else {
1287                enum rtl_var_map var = RTL_RC_OFDM_RATE54M;
1288
1289                if (rtlmac->mode == WIRELESS_MODE_B)
1290                        var = RTL_RC_CCK_RATE11M;
1291
1292                tcb_desc->hw_rate = rtlpriv->cfg->maps[var];
1293        }
1294
1295        if (is_multicast_ether_addr(hdr->addr1))
1296                tcb_desc->multicast = 1;
1297        else if (is_broadcast_ether_addr(hdr->addr1))
1298                tcb_desc->broadcast = 1;
1299
1300        _rtl_txrate_selectmode(hw, sta, tcb_desc);
1301        _rtl_query_bandwidth_mode(hw, sta, tcb_desc);
1302        _rtl_qurey_shortpreamble_mode(hw, tcb_desc, info);
1303        _rtl_query_shortgi(hw, sta, tcb_desc, info);
1304        _rtl_query_protection_mode(hw, tcb_desc, info);
1305}
1306
1307bool rtl_tx_mgmt_proc(struct ieee80211_hw *hw, struct sk_buff *skb)
1308{
1309        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1310        struct rtl_priv *rtlpriv = rtl_priv(hw);
1311        __le16 fc = rtl_get_fc(skb);
1312
1313        if (rtlpriv->dm.supp_phymode_switch &&
1314            mac->link_state < MAC80211_LINKED &&
1315            (ieee80211_is_auth(fc) || ieee80211_is_probe_req(fc))) {
1316                if (rtlpriv->cfg->ops->chk_switch_dmdp)
1317                        rtlpriv->cfg->ops->chk_switch_dmdp(hw);
1318        }
1319        if (ieee80211_is_auth(fc)) {
1320                RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG, "MAC80211_LINKING\n");
1321
1322                mac->link_state = MAC80211_LINKING;
1323                /* Dul mac */
1324                rtlpriv->phy.need_iqk = true;
1325        }
1326        return true;
1327}
1328
1329struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw, u8 *sa,
1330                                u8 *bssid, u16 tid);
1331
1332static void process_agg_start(struct ieee80211_hw *hw,
1333                              struct ieee80211_hdr *hdr, u16 tid)
1334{
1335        struct rtl_priv *rtlpriv = rtl_priv(hw);
1336        struct ieee80211_rx_status rx_status = { 0 };
1337        struct sk_buff *skb_delba = NULL;
1338
1339        skb_delba = rtl_make_del_ba(hw, hdr->addr2, hdr->addr3, tid);
1340        if (skb_delba) {
1341                rx_status.freq = hw->conf.chandef.chan->center_freq;
1342                rx_status.band = hw->conf.chandef.chan->band;
1343                rx_status.flag |= RX_FLAG_DECRYPTED;
1344                rx_status.flag |= RX_FLAG_MACTIME_START;
1345                rx_status.rate_idx = 0;
1346                rx_status.signal = 50 + 10;
1347                memcpy(IEEE80211_SKB_RXCB(skb_delba),
1348                       &rx_status, sizeof(rx_status));
1349                RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG,
1350                              "fake del\n",
1351                              skb_delba->data,
1352                              skb_delba->len);
1353                ieee80211_rx_irqsafe(hw, skb_delba);
1354        }
1355}
1356
1357bool rtl_action_proc(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx)
1358{
1359        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1360        struct ieee80211_hdr *hdr = rtl_get_hdr(skb);
1361        struct rtl_priv *rtlpriv = rtl_priv(hw);
1362        __le16 fc = rtl_get_fc(skb);
1363        u8 *act = (u8 *)(((u8 *)skb->data + MAC80211_3ADDR_LEN));
1364        u8 category;
1365
1366        if (!ieee80211_is_action(fc))
1367                return true;
1368
1369        category = *act;
1370        act++;
1371        switch (category) {
1372        case ACT_CAT_BA:
1373                switch (*act) {
1374                case ACT_ADDBAREQ:
1375                        if (mac->act_scanning)
1376                                return false;
1377
1378                        RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1379                                 "%s ACT_ADDBAREQ From :%pM\n",
1380                                 is_tx ? "Tx" : "Rx", hdr->addr2);
1381                        RT_PRINT_DATA(rtlpriv, COMP_INIT, DBG_DMESG, "req\n",
1382                                      skb->data, skb->len);
1383                        if (!is_tx) {
1384                                struct ieee80211_sta *sta = NULL;
1385                                struct rtl_sta_info *sta_entry = NULL;
1386                                struct rtl_tid_data *tid_data;
1387                                struct ieee80211_mgmt *mgmt = (void *)skb->data;
1388                                u16 capab = 0, tid = 0;
1389
1390                                rcu_read_lock();
1391                                sta = rtl_find_sta(hw, hdr->addr3);
1392                                if (!sta) {
1393                                        RT_TRACE(rtlpriv, COMP_SEND | COMP_RECV,
1394                                                 DBG_DMESG, "sta is NULL\n");
1395                                        rcu_read_unlock();
1396                                        return true;
1397                                }
1398
1399                                sta_entry =
1400                                        (struct rtl_sta_info *)sta->drv_priv;
1401                                if (!sta_entry) {
1402                                        rcu_read_unlock();
1403                                        return true;
1404                                }
1405                                capab =
1406                                  le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1407                                tid = (capab &
1408                                       IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1409                                if (tid >= MAX_TID_COUNT) {
1410                                        rcu_read_unlock();
1411                                        return true;
1412                                }
1413                                tid_data = &sta_entry->tids[tid];
1414                                if (tid_data->agg.rx_agg_state ==
1415                                    RTL_RX_AGG_START)
1416                                        process_agg_start(hw, hdr, tid);
1417                                rcu_read_unlock();
1418                        }
1419                        break;
1420                case ACT_ADDBARSP:
1421                        RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1422                                 "%s ACT_ADDBARSP From :%pM\n",
1423                                  is_tx ? "Tx" : "Rx", hdr->addr2);
1424                        break;
1425                case ACT_DELBA:
1426                        RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1427                                 "ACT_ADDBADEL From :%pM\n", hdr->addr2);
1428                        break;
1429                }
1430                break;
1431        default:
1432                break;
1433        }
1434
1435        return true;
1436}
1437
1438static void setup_special_tx(struct rtl_priv *rtlpriv, struct rtl_ps_ctl *ppsc,
1439                             int type)
1440{
1441        struct ieee80211_hw *hw = rtlpriv->hw;
1442
1443        rtlpriv->ra.is_special_data = true;
1444        if (rtlpriv->cfg->ops->get_btc_status())
1445                rtlpriv->btcoexist.btc_ops->btc_special_packet_notify(rtlpriv,
1446                                                                      type);
1447        rtl_lps_leave(hw);
1448        ppsc->last_delaylps_stamp_jiffies = jiffies;
1449}
1450
1451static const u8 *rtl_skb_ether_type_ptr(struct ieee80211_hw *hw,
1452                                        struct sk_buff *skb, bool is_enc)
1453{
1454        struct rtl_priv *rtlpriv = rtl_priv(hw);
1455        u8 mac_hdr_len = ieee80211_get_hdrlen_from_skb(skb);
1456        u8 encrypt_header_len = 0;
1457        u8 offset;
1458
1459        switch (rtlpriv->sec.pairwise_enc_algorithm) {
1460        case WEP40_ENCRYPTION:
1461        case WEP104_ENCRYPTION:
1462                encrypt_header_len = 4;/*WEP_IV_LEN*/
1463                break;
1464        case TKIP_ENCRYPTION:
1465                encrypt_header_len = 8;/*TKIP_IV_LEN*/
1466                break;
1467        case AESCCMP_ENCRYPTION:
1468                encrypt_header_len = 8;/*CCMP_HDR_LEN;*/
1469                break;
1470        default:
1471                break;
1472        }
1473
1474        offset = mac_hdr_len + SNAP_SIZE;
1475        if (is_enc)
1476                offset += encrypt_header_len;
1477
1478        return skb->data + offset;
1479}
1480
1481/*should call before software enc*/
1482u8 rtl_is_special_data(struct ieee80211_hw *hw, struct sk_buff *skb, u8 is_tx,
1483                       bool is_enc)
1484{
1485        struct rtl_priv *rtlpriv = rtl_priv(hw);
1486        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1487        __le16 fc = rtl_get_fc(skb);
1488        u16 ether_type;
1489        const u8 *ether_type_ptr;
1490        const struct iphdr *ip;
1491
1492        if (!ieee80211_is_data(fc))
1493                goto end;
1494
1495        ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, is_enc);
1496        ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1497
1498        if (ether_type == ETH_P_IP) {
1499                ip = (struct iphdr *)((u8 *)ether_type_ptr +
1500                     PROTOC_TYPE_SIZE);
1501                if (ip->protocol == IPPROTO_UDP) {
1502                        struct udphdr *udp = (struct udphdr *)((u8 *)ip +
1503                                                               (ip->ihl << 2));
1504                        if (((((u8 *)udp)[1] == 68) &&
1505                             (((u8 *)udp)[3] == 67)) ||
1506                            ((((u8 *)udp)[1] == 67) &&
1507                             (((u8 *)udp)[3] == 68))) {
1508                                /* 68 : UDP BOOTP client
1509                                 * 67 : UDP BOOTP server
1510                                 */
1511                                RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV),
1512                                         DBG_DMESG, "dhcp %s !!\n",
1513                                         (is_tx) ? "Tx" : "Rx");
1514
1515                                if (is_tx)
1516                                        setup_special_tx(rtlpriv, ppsc,
1517                                                         PACKET_DHCP);
1518
1519                                return true;
1520                        }
1521                }
1522        } else if (ether_type == ETH_P_ARP) {
1523                if (is_tx)
1524                        setup_special_tx(rtlpriv, ppsc, PACKET_ARP);
1525
1526                return true;
1527        } else if (ether_type == ETH_P_PAE) {
1528                /* EAPOL is seen as in-4way */
1529                rtlpriv->btcoexist.btc_info.in_4way = true;
1530                rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
1531                rtlpriv->btcoexist.btc_info.in_4way_ts = jiffies;
1532
1533                RT_TRACE(rtlpriv, (COMP_SEND | COMP_RECV), DBG_DMESG,
1534                         "802.1X %s EAPOL pkt!!\n", (is_tx) ? "Tx" : "Rx");
1535
1536                if (is_tx) {
1537                        rtlpriv->ra.is_special_data = true;
1538                        rtl_lps_leave(hw);
1539                        ppsc->last_delaylps_stamp_jiffies = jiffies;
1540
1541                        setup_special_tx(rtlpriv, ppsc, PACKET_EAPOL);
1542                }
1543
1544                return true;
1545        } else if (ether_type == ETH_P_IPV6) {
1546                /* TODO: Handle any IPv6 cases that need special handling.
1547                 * For now, always return false
1548                 */
1549                goto end;
1550        }
1551
1552end:
1553        rtlpriv->ra.is_special_data = false;
1554        return false;
1555}
1556
1557bool rtl_is_tx_report_skb(struct ieee80211_hw *hw, struct sk_buff *skb)
1558{
1559        u16 ether_type;
1560        const u8 *ether_type_ptr;
1561
1562        ether_type_ptr = rtl_skb_ether_type_ptr(hw, skb, true);
1563        ether_type = be16_to_cpup((__be16 *)ether_type_ptr);
1564
1565        /* EAPOL */
1566        if (ether_type == ETH_P_PAE)
1567                return true;
1568
1569        return false;
1570}
1571
1572static u16 rtl_get_tx_report_sn(struct ieee80211_hw *hw)
1573{
1574        struct rtl_priv *rtlpriv = rtl_priv(hw);
1575        struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1576        u16 sn;
1577
1578        /*
1579         * SW_DEFINE[11:8] are reserved (driver fills zeros)
1580         * SW_DEFINE[7:2] are used by driver
1581         * SW_DEFINE[1:0] are reserved for firmware (driver fills zeros)
1582         */
1583        sn = (atomic_inc_return(&tx_report->sn) & 0x003F) << 2;
1584
1585        tx_report->last_sent_sn = sn;
1586        tx_report->last_sent_time = jiffies;
1587
1588        RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1589                 "Send TX-Report sn=0x%X\n", sn);
1590
1591        return sn;
1592}
1593
1594void rtl_get_tx_report(struct rtl_tcb_desc *ptcb_desc, u8 *pdesc,
1595                       struct ieee80211_hw *hw)
1596{
1597        if (ptcb_desc->use_spe_rpt) {
1598                u16 sn = rtl_get_tx_report_sn(hw);
1599
1600                SET_TX_DESC_SPE_RPT(pdesc, 1);
1601                SET_TX_DESC_SW_DEFINE(pdesc, sn);
1602        }
1603}
1604
1605void rtl_tx_report_handler(struct ieee80211_hw *hw, u8 *tmp_buf, u8 c2h_cmd_len)
1606{
1607        struct rtl_priv *rtlpriv = rtl_priv(hw);
1608        struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1609        u16 sn;
1610        u8 st, retry;
1611
1612        if (rtlpriv->cfg->spec_ver & RTL_SPEC_NEW_FW_C2H) {
1613                sn = tmp_buf[6];
1614                st = tmp_buf[7] & 0xC0;
1615                retry = tmp_buf[8] & 0x3F;
1616        } else {
1617                sn = ((tmp_buf[7] & 0x0F) << 8) | tmp_buf[6];
1618                st = tmp_buf[0] & 0xC0;
1619                retry = tmp_buf[2] & 0x3F;
1620        }
1621
1622        tx_report->last_recv_sn = sn;
1623
1624        RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_DMESG,
1625                 "Recv TX-Report st=0x%02X sn=0x%X retry=0x%X\n",
1626                 st, sn, retry);
1627}
1628
1629bool rtl_check_tx_report_acked(struct ieee80211_hw *hw)
1630{
1631        struct rtl_priv *rtlpriv = rtl_priv(hw);
1632        struct rtl_tx_report *tx_report = &rtlpriv->tx_report;
1633
1634        if (tx_report->last_sent_sn == tx_report->last_recv_sn)
1635                return true;
1636
1637        if (time_before(tx_report->last_sent_time + 3 * HZ, jiffies)) {
1638                RT_TRACE(rtlpriv, COMP_TX_REPORT, DBG_WARNING,
1639                         "Check TX-Report timeout!! s_sn=0x%X r_sn=0x%X\n",
1640                         tx_report->last_sent_sn, tx_report->last_recv_sn);
1641                return true;    /* 3 sec. (timeout) seen as acked */
1642        }
1643
1644        return false;
1645}
1646
1647void rtl_wait_tx_report_acked(struct ieee80211_hw *hw, u32 wait_ms)
1648{
1649        struct rtl_priv *rtlpriv = rtl_priv(hw);
1650        int i;
1651
1652        for (i = 0; i < wait_ms; i++) {
1653                if (rtl_check_tx_report_acked(hw))
1654                        break;
1655                usleep_range(1000, 2000);
1656                RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1657                         "Wait 1ms (%d/%d) to disable key.\n", i, wait_ms);
1658        }
1659}
1660
1661u32 rtl_get_hal_edca_param(struct ieee80211_hw *hw,
1662                           struct ieee80211_vif *vif,
1663                           enum wireless_mode wirelessmode,
1664                           struct ieee80211_tx_queue_params *param)
1665{
1666        u32 reg = 0;
1667        u8 sifstime = 10;
1668        u8 slottime = 20;
1669
1670        /* AIFS = AIFSN * slot time + SIFS */
1671        switch (wirelessmode) {
1672        case WIRELESS_MODE_A:
1673        case WIRELESS_MODE_N_24G:
1674        case WIRELESS_MODE_N_5G:
1675        case WIRELESS_MODE_AC_5G:
1676        case WIRELESS_MODE_AC_24G:
1677                sifstime = 16;
1678                slottime = 9;
1679                break;
1680        case WIRELESS_MODE_G:
1681                slottime = (vif->bss_conf.use_short_slot ? 9 : 20);
1682                break;
1683        default:
1684                break;
1685        }
1686
1687        reg |= (param->txop & 0x7FF) << 16;
1688        reg |= (fls(param->cw_max) & 0xF) << 12;
1689        reg |= (fls(param->cw_min) & 0xF) << 8;
1690        reg |= (param->aifs & 0x0F) * slottime + sifstime;
1691
1692        return reg;
1693}
1694
1695/*********************************************************
1696 *
1697 * functions called by core.c
1698 *
1699 *********************************************************/
1700int rtl_tx_agg_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1701                     struct ieee80211_sta *sta, u16 tid, u16 *ssn)
1702{
1703        struct rtl_priv *rtlpriv = rtl_priv(hw);
1704        struct rtl_tid_data *tid_data;
1705        struct rtl_sta_info *sta_entry = NULL;
1706
1707        if (!sta)
1708                return -EINVAL;
1709
1710        if (unlikely(tid >= MAX_TID_COUNT))
1711                return -EINVAL;
1712
1713        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1714        if (!sta_entry)
1715                return -ENXIO;
1716        tid_data = &sta_entry->tids[tid];
1717
1718        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1719                 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1720                 tid_data->seq_number);
1721
1722        *ssn = tid_data->seq_number;
1723        tid_data->agg.agg_state = RTL_AGG_START;
1724
1725        ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1726        return 0;
1727}
1728
1729int rtl_tx_agg_stop(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1730                    struct ieee80211_sta *sta, u16 tid)
1731{
1732        struct rtl_priv *rtlpriv = rtl_priv(hw);
1733        struct rtl_tid_data *tid_data;
1734        struct rtl_sta_info *sta_entry = NULL;
1735
1736        if (!sta)
1737                return -EINVAL;
1738
1739        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1740                 "on ra = %pM tid = %d\n", sta->addr, tid);
1741
1742        if (unlikely(tid >= MAX_TID_COUNT))
1743                return -EINVAL;
1744
1745        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1746        tid_data = &sta_entry->tids[tid];
1747        sta_entry->tids[tid].agg.agg_state = RTL_AGG_STOP;
1748
1749        ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1750        return 0;
1751}
1752
1753int rtl_rx_agg_start(struct ieee80211_hw *hw,
1754                     struct ieee80211_sta *sta, u16 tid)
1755{
1756        struct rtl_priv *rtlpriv = rtl_priv(hw);
1757        struct rtl_tid_data *tid_data;
1758        struct rtl_sta_info *sta_entry = NULL;
1759        u8 reject_agg;
1760
1761        if (!sta)
1762                return -EINVAL;
1763
1764        if (unlikely(tid >= MAX_TID_COUNT))
1765                return -EINVAL;
1766
1767        if (rtlpriv->cfg->ops->get_btc_status()) {
1768                rtlpriv->btcoexist.btc_ops->btc_get_ampdu_cfg(rtlpriv,
1769                                                              &reject_agg,
1770                                                              NULL, NULL);
1771                if (reject_agg)
1772                        return -EINVAL;
1773        }
1774
1775        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1776        if (!sta_entry)
1777                return -ENXIO;
1778        tid_data = &sta_entry->tids[tid];
1779
1780        RT_TRACE(rtlpriv, COMP_RECV, DBG_DMESG,
1781                 "on ra = %pM tid = %d seq:%d\n", sta->addr, tid,
1782                 tid_data->seq_number);
1783
1784        tid_data->agg.rx_agg_state = RTL_RX_AGG_START;
1785        return 0;
1786}
1787
1788int rtl_rx_agg_stop(struct ieee80211_hw *hw,
1789                    struct ieee80211_sta *sta, u16 tid)
1790{
1791        struct rtl_priv *rtlpriv = rtl_priv(hw);
1792        struct rtl_sta_info *sta_entry = NULL;
1793
1794        if (!sta)
1795                return -EINVAL;
1796
1797        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1798                 "on ra = %pM tid = %d\n", sta->addr, tid);
1799
1800        if (unlikely(tid >= MAX_TID_COUNT))
1801                return -EINVAL;
1802
1803        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1804        sta_entry->tids[tid].agg.rx_agg_state = RTL_RX_AGG_STOP;
1805
1806        return 0;
1807}
1808
1809int rtl_tx_agg_oper(struct ieee80211_hw *hw,
1810                    struct ieee80211_sta *sta, u16 tid)
1811{
1812        struct rtl_priv *rtlpriv = rtl_priv(hw);
1813        struct rtl_sta_info *sta_entry = NULL;
1814
1815        if (!sta)
1816                return -EINVAL;
1817
1818        RT_TRACE(rtlpriv, COMP_SEND, DBG_DMESG,
1819                 "on ra = %pM tid = %d\n", sta->addr, tid);
1820
1821        if (unlikely(tid >= MAX_TID_COUNT))
1822                return -EINVAL;
1823
1824        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1825        sta_entry->tids[tid].agg.agg_state = RTL_AGG_OPERATIONAL;
1826
1827        return 0;
1828}
1829
1830void rtl_rx_ampdu_apply(struct rtl_priv *rtlpriv)
1831{
1832        struct rtl_btc_ops *btc_ops = rtlpriv->btcoexist.btc_ops;
1833        u8 reject_agg = 0, ctrl_agg_size = 0, agg_size = 0;
1834
1835        if (rtlpriv->cfg->ops->get_btc_status())
1836                btc_ops->btc_get_ampdu_cfg(rtlpriv, &reject_agg,
1837                                           &ctrl_agg_size, &agg_size);
1838
1839        RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_DMESG,
1840                 "Set RX AMPDU: coex - reject=%d, ctrl_agg_size=%d, size=%d",
1841                 reject_agg, ctrl_agg_size, agg_size);
1842
1843        rtlpriv->hw->max_rx_aggregation_subframes =
1844                (ctrl_agg_size ? agg_size : IEEE80211_MAX_AMPDU_BUF_HT);
1845}
1846
1847/*********************************************************
1848 *
1849 * wq & timer callback functions
1850 *
1851 *********************************************************/
1852/* this function is used for roaming */
1853void rtl_beacon_statistic(struct ieee80211_hw *hw, struct sk_buff *skb)
1854{
1855        struct rtl_priv *rtlpriv = rtl_priv(hw);
1856        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1857
1858        if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1859                return;
1860
1861        if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
1862                return;
1863
1864        /* check if this really is a beacon */
1865        if (!ieee80211_is_beacon(hdr->frame_control) &&
1866            !ieee80211_is_probe_resp(hdr->frame_control))
1867                return;
1868
1869        /* min. beacon length + FCS_LEN */
1870        if (skb->len <= 40 + FCS_LEN)
1871                return;
1872
1873        /* and only beacons from the associated BSSID, please */
1874        if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
1875                return;
1876
1877        rtlpriv->link_info.bcn_rx_inperiod++;
1878}
1879
1880static void rtl_free_entries_from_scan_list(struct ieee80211_hw *hw)
1881{
1882        struct rtl_priv *rtlpriv = rtl_priv(hw);
1883        struct rtl_bssid_entry *entry, *next;
1884
1885        list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1886                list_del(&entry->list);
1887                kfree(entry);
1888                rtlpriv->scan_list.num--;
1889        }
1890}
1891
1892void rtl_scan_list_expire(struct ieee80211_hw *hw)
1893{
1894        struct rtl_priv *rtlpriv = rtl_priv(hw);
1895        struct rtl_bssid_entry *entry, *next;
1896        unsigned long flags;
1897
1898        spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
1899
1900        list_for_each_entry_safe(entry, next, &rtlpriv->scan_list.list, list) {
1901                /* 180 seconds */
1902                if (jiffies_to_msecs(jiffies - entry->age) < 180000)
1903                        continue;
1904
1905                list_del(&entry->list);
1906                rtlpriv->scan_list.num--;
1907
1908                RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1909                         "BSSID=%pM is expire in scan list (total=%d)\n",
1910                         entry->bssid, rtlpriv->scan_list.num);
1911                kfree(entry);
1912        }
1913
1914        spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
1915
1916        rtlpriv->btcoexist.btc_info.ap_num = rtlpriv->scan_list.num;
1917}
1918
1919void rtl_collect_scan_list(struct ieee80211_hw *hw, struct sk_buff *skb)
1920{
1921        struct rtl_priv *rtlpriv = rtl_priv(hw);
1922        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1923        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1924        unsigned long flags;
1925
1926        struct rtl_bssid_entry *entry;
1927        bool entry_found = false;
1928
1929        /* check if it is scanning */
1930        if (!mac->act_scanning)
1931                return;
1932
1933        /* check if this really is a beacon */
1934        if (!ieee80211_is_beacon(hdr->frame_control) &&
1935            !ieee80211_is_probe_resp(hdr->frame_control))
1936                return;
1937
1938        spin_lock_irqsave(&rtlpriv->locks.scan_list_lock, flags);
1939
1940        list_for_each_entry(entry, &rtlpriv->scan_list.list, list) {
1941                if (memcmp(entry->bssid, hdr->addr3, ETH_ALEN) == 0) {
1942                        list_del_init(&entry->list);
1943                        entry_found = true;
1944                        RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1945                                 "Update BSSID=%pM to scan list (total=%d)\n",
1946                                 hdr->addr3, rtlpriv->scan_list.num);
1947                        break;
1948                }
1949        }
1950
1951        if (!entry_found) {
1952                entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1953
1954                if (!entry)
1955                        goto label_err;
1956
1957                memcpy(entry->bssid, hdr->addr3, ETH_ALEN);
1958                rtlpriv->scan_list.num++;
1959
1960                RT_TRACE(rtlpriv, COMP_SCAN, DBG_LOUD,
1961                         "Add BSSID=%pM to scan list (total=%d)\n",
1962                         hdr->addr3, rtlpriv->scan_list.num);
1963        }
1964
1965        entry->age = jiffies;
1966
1967        list_add_tail(&entry->list, &rtlpriv->scan_list.list);
1968
1969label_err:
1970        spin_unlock_irqrestore(&rtlpriv->locks.scan_list_lock, flags);
1971}
1972
1973void rtl_watchdog_wq_callback(void *data)
1974{
1975        struct rtl_works *rtlworks = container_of_dwork_rtl(data,
1976                                                            struct rtl_works,
1977                                                            watchdog_wq);
1978        struct ieee80211_hw *hw = rtlworks->hw;
1979        struct rtl_priv *rtlpriv = rtl_priv(hw);
1980        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1981        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1982        bool busytraffic = false;
1983        bool tx_busy_traffic = false;
1984        bool rx_busy_traffic = false;
1985        bool higher_busytraffic = false;
1986        bool higher_busyrxtraffic = false;
1987        u8 idx, tid;
1988        u32 rx_cnt_inp4eriod = 0;
1989        u32 tx_cnt_inp4eriod = 0;
1990        u32 aver_rx_cnt_inperiod = 0;
1991        u32 aver_tx_cnt_inperiod = 0;
1992        u32 aver_tidtx_inperiod[MAX_TID_COUNT] = {0};
1993        u32 tidtx_inp4eriod[MAX_TID_COUNT] = {0};
1994
1995        if (is_hal_stop(rtlhal))
1996                return;
1997
1998        /* <1> Determine if action frame is allowed */
1999        if (mac->link_state > MAC80211_NOLINK) {
2000                if (mac->cnt_after_linked < 20)
2001                        mac->cnt_after_linked++;
2002        } else {
2003                mac->cnt_after_linked = 0;
2004        }
2005
2006        /* <2> to check if traffic busy, if
2007         * busytraffic we don't change channel
2008         */
2009        if (mac->link_state >= MAC80211_LINKED) {
2010                /* (1) get aver_rx_cnt_inperiod & aver_tx_cnt_inperiod */
2011                for (idx = 0; idx <= 2; idx++) {
2012                        rtlpriv->link_info.num_rx_in4period[idx] =
2013                            rtlpriv->link_info.num_rx_in4period[idx + 1];
2014                        rtlpriv->link_info.num_tx_in4period[idx] =
2015                            rtlpriv->link_info.num_tx_in4period[idx + 1];
2016                }
2017                rtlpriv->link_info.num_rx_in4period[3] =
2018                    rtlpriv->link_info.num_rx_inperiod;
2019                rtlpriv->link_info.num_tx_in4period[3] =
2020                    rtlpriv->link_info.num_tx_inperiod;
2021                for (idx = 0; idx <= 3; idx++) {
2022                        rx_cnt_inp4eriod +=
2023                            rtlpriv->link_info.num_rx_in4period[idx];
2024                        tx_cnt_inp4eriod +=
2025                            rtlpriv->link_info.num_tx_in4period[idx];
2026                }
2027                aver_rx_cnt_inperiod = rx_cnt_inp4eriod / 4;
2028                aver_tx_cnt_inperiod = tx_cnt_inp4eriod / 4;
2029
2030                /* (2) check traffic busy */
2031                if (aver_rx_cnt_inperiod > 100 || aver_tx_cnt_inperiod > 100) {
2032                        busytraffic = true;
2033                        if (aver_rx_cnt_inperiod > aver_tx_cnt_inperiod)
2034                                rx_busy_traffic = true;
2035                        else
2036                                tx_busy_traffic = false;
2037                }
2038
2039                /* Higher Tx/Rx data. */
2040                if (aver_rx_cnt_inperiod > 4000 ||
2041                    aver_tx_cnt_inperiod > 4000) {
2042                        higher_busytraffic = true;
2043
2044                        /* Extremely high Rx data. */
2045                        if (aver_rx_cnt_inperiod > 5000)
2046                                higher_busyrxtraffic = true;
2047                }
2048
2049                /* check every tid's tx traffic */
2050                for (tid = 0; tid <= 7; tid++) {
2051                        for (idx = 0; idx <= 2; idx++)
2052                                rtlpriv->link_info.tidtx_in4period[tid][idx] =
2053                                        rtlpriv->link_info.tidtx_in4period[tid]
2054                                        [idx + 1];
2055                        rtlpriv->link_info.tidtx_in4period[tid][3] =
2056                                rtlpriv->link_info.tidtx_inperiod[tid];
2057
2058                        for (idx = 0; idx <= 3; idx++)
2059                                tidtx_inp4eriod[tid] +=
2060                                   rtlpriv->link_info.tidtx_in4period[tid][idx];
2061                        aver_tidtx_inperiod[tid] = tidtx_inp4eriod[tid] / 4;
2062                        if (aver_tidtx_inperiod[tid] > 5000)
2063                                rtlpriv->link_info.higher_busytxtraffic[tid] =
2064                                                                        true;
2065                        else
2066                                rtlpriv->link_info.higher_busytxtraffic[tid] =
2067                                                                        false;
2068                }
2069
2070                /* PS is controlled by coex. */
2071                if (rtlpriv->cfg->ops->get_btc_status() &&
2072                    rtlpriv->btcoexist.btc_ops->btc_is_bt_ctrl_lps(rtlpriv))
2073                        goto label_lps_done;
2074
2075                if (rtlpriv->link_info.num_rx_inperiod +
2076                      rtlpriv->link_info.num_tx_inperiod > 8 ||
2077                    rtlpriv->link_info.num_rx_inperiod > 2)
2078                        rtl_lps_leave(hw);
2079                else
2080                        rtl_lps_enter(hw);
2081
2082label_lps_done:
2083                ;
2084        }
2085
2086        rtlpriv->link_info.num_rx_inperiod = 0;
2087        rtlpriv->link_info.num_tx_inperiod = 0;
2088        for (tid = 0; tid <= 7; tid++)
2089                rtlpriv->link_info.tidtx_inperiod[tid] = 0;
2090
2091        rtlpriv->link_info.busytraffic = busytraffic;
2092        rtlpriv->link_info.higher_busytraffic = higher_busytraffic;
2093        rtlpriv->link_info.rx_busy_traffic = rx_busy_traffic;
2094        rtlpriv->link_info.tx_busy_traffic = tx_busy_traffic;
2095        rtlpriv->link_info.higher_busyrxtraffic = higher_busyrxtraffic;
2096
2097        rtlpriv->stats.txbytesunicast_inperiod =
2098                rtlpriv->stats.txbytesunicast -
2099                rtlpriv->stats.txbytesunicast_last;
2100        rtlpriv->stats.rxbytesunicast_inperiod =
2101                rtlpriv->stats.rxbytesunicast -
2102                rtlpriv->stats.rxbytesunicast_last;
2103        rtlpriv->stats.txbytesunicast_last = rtlpriv->stats.txbytesunicast;
2104        rtlpriv->stats.rxbytesunicast_last = rtlpriv->stats.rxbytesunicast;
2105
2106        rtlpriv->stats.txbytesunicast_inperiod_tp =
2107                (u32)(rtlpriv->stats.txbytesunicast_inperiod * 8 / 2 /
2108                1024 / 1024);
2109        rtlpriv->stats.rxbytesunicast_inperiod_tp =
2110                (u32)(rtlpriv->stats.rxbytesunicast_inperiod * 8 / 2 /
2111                1024 / 1024);
2112
2113        /* <3> DM */
2114        if (!rtlpriv->cfg->mod_params->disable_watchdog)
2115                rtlpriv->cfg->ops->dm_watchdog(hw);
2116
2117        /* <4> roaming */
2118        if (mac->link_state == MAC80211_LINKED &&
2119            mac->opmode == NL80211_IFTYPE_STATION) {
2120                if ((rtlpriv->link_info.bcn_rx_inperiod +
2121                    rtlpriv->link_info.num_rx_inperiod) == 0) {
2122                        rtlpriv->link_info.roam_times++;
2123                        RT_TRACE(rtlpriv, COMP_ERR, DBG_DMESG,
2124                                 "AP off for %d s\n",
2125                                (rtlpriv->link_info.roam_times * 2));
2126
2127                        /* if we can't recv beacon for 10s,
2128                         * we should reconnect this AP
2129                         */
2130                        if (rtlpriv->link_info.roam_times >= 5) {
2131                                pr_err("AP off, try to reconnect now\n");
2132                                rtlpriv->link_info.roam_times = 0;
2133                                ieee80211_connection_loss(rtlpriv->mac80211.vif);
2134                        }
2135                } else {
2136                        rtlpriv->link_info.roam_times = 0;
2137                }
2138        }
2139
2140        if (rtlpriv->cfg->ops->get_btc_status())
2141                rtlpriv->btcoexist.btc_ops->btc_periodical(rtlpriv);
2142
2143        if (rtlpriv->btcoexist.btc_info.in_4way) {
2144                if (time_after(jiffies, rtlpriv->btcoexist.btc_info.in_4way_ts +
2145                               msecs_to_jiffies(IN_4WAY_TIMEOUT_TIME)))
2146                        rtlpriv->btcoexist.btc_info.in_4way = false;
2147        }
2148
2149        rtlpriv->link_info.bcn_rx_inperiod = 0;
2150
2151        /* <6> scan list */
2152        rtl_scan_list_expire(hw);
2153}
2154
2155void rtl_watch_dog_timer_callback(struct timer_list *t)
2156{
2157        struct rtl_priv *rtlpriv = from_timer(rtlpriv, t, works.watchdog_timer);
2158
2159        queue_delayed_work(rtlpriv->works.rtl_wq,
2160                           &rtlpriv->works.watchdog_wq, 0);
2161
2162        mod_timer(&rtlpriv->works.watchdog_timer,
2163                  jiffies + MSECS(RTL_WATCH_DOG_TIME));
2164}
2165
2166void rtl_fwevt_wq_callback(void *data)
2167{
2168        struct rtl_works *rtlworks =
2169                container_of_dwork_rtl(data, struct rtl_works, fwevt_wq);
2170        struct ieee80211_hw *hw = rtlworks->hw;
2171        struct rtl_priv *rtlpriv = rtl_priv(hw);
2172
2173        rtlpriv->cfg->ops->c2h_command_handle(hw);
2174}
2175
2176void rtl_c2hcmd_enqueue(struct ieee80211_hw *hw, u8 tag, u8 len, u8 *val)
2177{
2178        struct rtl_priv *rtlpriv = rtl_priv(hw);
2179        unsigned long flags;
2180        struct rtl_c2hcmd *c2hcmd;
2181
2182        c2hcmd = kmalloc(sizeof(*c2hcmd),
2183                         in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
2184
2185        if (!c2hcmd)
2186                goto label_err;
2187
2188        c2hcmd->val = kmalloc(len,
2189                              in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
2190
2191        if (!c2hcmd->val)
2192                goto label_err2;
2193
2194        /* fill data */
2195        c2hcmd->tag = tag;
2196        c2hcmd->len = len;
2197        memcpy(c2hcmd->val, val, len);
2198
2199        /* enqueue */
2200        spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
2201
2202        list_add_tail(&c2hcmd->list, &rtlpriv->c2hcmd_list);
2203
2204        spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
2205
2206        /* wake up wq */
2207        queue_delayed_work(rtlpriv->works.rtl_wq, &rtlpriv->works.c2hcmd_wq, 0);
2208
2209        return;
2210
2211label_err2:
2212        kfree(c2hcmd);
2213
2214label_err:
2215        RT_TRACE(rtlpriv, COMP_CMD, DBG_WARNING,
2216                 "C2H cmd enqueue fail.\n");
2217}
2218
2219void rtl_c2hcmd_launcher(struct ieee80211_hw *hw, int exec)
2220{
2221        struct rtl_priv *rtlpriv = rtl_priv(hw);
2222        unsigned long flags;
2223        struct rtl_c2hcmd *c2hcmd;
2224        int i;
2225
2226        for (i = 0; i < 200; i++) {
2227                /* dequeue a task */
2228                spin_lock_irqsave(&rtlpriv->locks.c2hcmd_lock, flags);
2229
2230                c2hcmd = list_first_entry_or_null(&rtlpriv->c2hcmd_list,
2231                                                  struct rtl_c2hcmd, list);
2232
2233                if (c2hcmd)
2234                        list_del(&c2hcmd->list);
2235
2236                spin_unlock_irqrestore(&rtlpriv->locks.c2hcmd_lock, flags);
2237
2238                /* do it */
2239                if (!c2hcmd)
2240                        break;
2241
2242                if (rtlpriv->cfg->ops->c2h_content_parsing && exec)
2243                        rtlpriv->cfg->ops->c2h_content_parsing(hw,
2244                                        c2hcmd->tag, c2hcmd->len, c2hcmd->val);
2245
2246                /* free */
2247                kfree(c2hcmd->val);
2248
2249                kfree(c2hcmd);
2250        }
2251}
2252
2253void rtl_c2hcmd_wq_callback(void *data)
2254{
2255        struct rtl_works *rtlworks = container_of_dwork_rtl(data,
2256                                                            struct rtl_works,
2257                                                            c2hcmd_wq);
2258        struct ieee80211_hw *hw = rtlworks->hw;
2259
2260        rtl_c2hcmd_launcher(hw, 1);
2261}
2262
2263void rtl_easy_concurrent_retrytimer_callback(struct timer_list *t)
2264{
2265        struct rtl_priv *rtlpriv =
2266                from_timer(rtlpriv, t, works.dualmac_easyconcurrent_retrytimer);
2267        struct ieee80211_hw *hw = rtlpriv->hw;
2268        struct rtl_priv *buddy_priv = rtlpriv->buddy_priv;
2269
2270        if (!buddy_priv)
2271                return;
2272
2273        rtlpriv->cfg->ops->dualmac_easy_concurrent(hw);
2274}
2275
2276/*********************************************************
2277 *
2278 * frame process functions
2279 *
2280 *********************************************************/
2281u8 *rtl_find_ie(u8 *data, unsigned int len, u8 ie)
2282{
2283        struct ieee80211_mgmt *mgmt = (void *)data;
2284        u8 *pos, *end;
2285
2286        pos = (u8 *)mgmt->u.beacon.variable;
2287        end = data + len;
2288        while (pos < end) {
2289                if (pos + 2 + pos[1] > end)
2290                        return NULL;
2291
2292                if (pos[0] == ie)
2293                        return pos;
2294
2295                pos += 2 + pos[1];
2296        }
2297        return NULL;
2298}
2299
2300/* when we use 2 rx ants we send IEEE80211_SMPS_OFF */
2301/* when we use 1 rx ant we send IEEE80211_SMPS_STATIC */
2302static struct sk_buff *rtl_make_smps_action(struct ieee80211_hw *hw,
2303                                            enum ieee80211_smps_mode smps,
2304                                            u8 *da, u8 *bssid)
2305{
2306        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2307        struct sk_buff *skb;
2308        struct ieee80211_mgmt *action_frame;
2309
2310        /* 27 = header + category + action + smps mode */
2311        skb = dev_alloc_skb(27 + hw->extra_tx_headroom);
2312        if (!skb)
2313                return NULL;
2314
2315        skb_reserve(skb, hw->extra_tx_headroom);
2316        action_frame = skb_put_zero(skb, 27);
2317        memcpy(action_frame->da, da, ETH_ALEN);
2318        memcpy(action_frame->sa, rtlefuse->dev_addr, ETH_ALEN);
2319        memcpy(action_frame->bssid, bssid, ETH_ALEN);
2320        action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2321                                                  IEEE80211_STYPE_ACTION);
2322        action_frame->u.action.category = WLAN_CATEGORY_HT;
2323        action_frame->u.action.u.ht_smps.action = WLAN_HT_ACTION_SMPS;
2324        switch (smps) {
2325        case IEEE80211_SMPS_AUTOMATIC:/* 0 */
2326        case IEEE80211_SMPS_NUM_MODES:/* 4 */
2327                WARN_ON(1);
2328        /* fall through */
2329        case IEEE80211_SMPS_OFF:/* 1 */ /*MIMO_PS_NOLIMIT*/
2330                action_frame->u.action.u.ht_smps.smps_control =
2331                                WLAN_HT_SMPS_CONTROL_DISABLED;/* 0 */
2332                break;
2333        case IEEE80211_SMPS_STATIC:/* 2 */ /*MIMO_PS_STATIC*/
2334                action_frame->u.action.u.ht_smps.smps_control =
2335                                WLAN_HT_SMPS_CONTROL_STATIC;/* 1 */
2336                break;
2337        case IEEE80211_SMPS_DYNAMIC:/* 3 */ /*MIMO_PS_DYNAMIC*/
2338                action_frame->u.action.u.ht_smps.smps_control =
2339                                WLAN_HT_SMPS_CONTROL_DYNAMIC;/* 3 */
2340                break;
2341        }
2342
2343        return skb;
2344}
2345
2346int rtl_send_smps_action(struct ieee80211_hw *hw,
2347                         struct ieee80211_sta *sta,
2348                         enum ieee80211_smps_mode smps)
2349{
2350        struct rtl_priv *rtlpriv = rtl_priv(hw);
2351        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2352        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2353        struct sk_buff *skb = NULL;
2354        struct rtl_tcb_desc tcb_desc;
2355        u8 bssid[ETH_ALEN] = {0};
2356
2357        memset(&tcb_desc, 0, sizeof(struct rtl_tcb_desc));
2358
2359        if (rtlpriv->mac80211.act_scanning)
2360                goto err_free;
2361
2362        if (!sta)
2363                goto err_free;
2364
2365        if (unlikely(is_hal_stop(rtlhal) || ppsc->rfpwr_state != ERFON))
2366                goto err_free;
2367
2368        if (!test_bit(RTL_STATUS_INTERFACE_START, &rtlpriv->status))
2369                goto err_free;
2370
2371        if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP)
2372                memcpy(bssid, rtlpriv->efuse.dev_addr, ETH_ALEN);
2373        else
2374                memcpy(bssid, rtlpriv->mac80211.bssid, ETH_ALEN);
2375
2376        skb = rtl_make_smps_action(hw, smps, sta->addr, bssid);
2377        /* this is a type = mgmt * stype = action frame */
2378        if (skb) {
2379                struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2380                struct rtl_sta_info *sta_entry =
2381                        (struct rtl_sta_info *)sta->drv_priv;
2382                sta_entry->mimo_ps = smps;
2383                /* rtlpriv->cfg->ops->update_rate_tbl(hw, sta, 0, true); */
2384
2385                info->control.rates[0].idx = 0;
2386                info->band = hw->conf.chandef.chan->band;
2387                rtlpriv->intf_ops->adapter_tx(hw, sta, skb, &tcb_desc);
2388        }
2389        return 1;
2390
2391err_free:
2392        return 0;
2393}
2394
2395void rtl_phy_scan_operation_backup(struct ieee80211_hw *hw, u8 operation)
2396{
2397        struct rtl_priv *rtlpriv = rtl_priv(hw);
2398        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2399        enum io_type iotype;
2400
2401        if (!is_hal_stop(rtlhal)) {
2402                switch (operation) {
2403                case SCAN_OPT_BACKUP:
2404                        iotype = IO_CMD_PAUSE_DM_BY_SCAN;
2405                        rtlpriv->cfg->ops->set_hw_reg(hw,
2406                                                      HW_VAR_IO_CMD,
2407                                                      (u8 *)&iotype);
2408                        break;
2409                case SCAN_OPT_RESTORE:
2410                        iotype = IO_CMD_RESUME_DM_BY_SCAN;
2411                        rtlpriv->cfg->ops->set_hw_reg(hw,
2412                                                      HW_VAR_IO_CMD,
2413                                                      (u8 *)&iotype);
2414                        break;
2415                default:
2416                        pr_err("Unknown Scan Backup operation.\n");
2417                        break;
2418                }
2419        }
2420}
2421
2422/* because mac80211 have issues when can receive del ba
2423 * so here we just make a fake del_ba if we receive a ba_req
2424 * but rx_agg was opened to let mac80211 release some ba
2425 * related resources, so please this del_ba for tx
2426 */
2427struct sk_buff *rtl_make_del_ba(struct ieee80211_hw *hw,
2428                                u8 *sa, u8 *bssid, u16 tid)
2429{
2430        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2431        struct sk_buff *skb;
2432        struct ieee80211_mgmt *action_frame;
2433        u16 params;
2434
2435        /* 27 = header + category + action + smps mode */
2436        skb = dev_alloc_skb(34 + hw->extra_tx_headroom);
2437        if (!skb)
2438                return NULL;
2439
2440        skb_reserve(skb, hw->extra_tx_headroom);
2441        action_frame = skb_put_zero(skb, 34);
2442        memcpy(action_frame->sa, sa, ETH_ALEN);
2443        memcpy(action_frame->da, rtlefuse->dev_addr, ETH_ALEN);
2444        memcpy(action_frame->bssid, bssid, ETH_ALEN);
2445        action_frame->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2446                                                  IEEE80211_STYPE_ACTION);
2447        action_frame->u.action.category = WLAN_CATEGORY_BACK;
2448        action_frame->u.action.u.delba.action_code = WLAN_ACTION_DELBA;
2449        params = (u16)(1 << 11);        /* bit 11 initiator */
2450        params |= (u16)(tid << 12);     /* bit 15:12 TID number */
2451
2452        action_frame->u.action.u.delba.params = cpu_to_le16(params);
2453        action_frame->u.action.u.delba.reason_code =
2454                cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
2455
2456        return skb;
2457}
2458
2459bool rtl_check_beacon_key(struct ieee80211_hw *hw, void *data, unsigned int len)
2460{
2461        struct rtl_priv *rtlpriv = rtl_priv(hw);
2462        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2463        struct rtl_phy *rtlphy = &rtlpriv->phy;
2464        struct ieee80211_hdr *hdr = data;
2465        struct ieee80211_ht_cap *ht_cap_ie;
2466        struct ieee80211_ht_operation *ht_oper_ie = NULL;
2467        struct rtl_beacon_keys bcn_key = {};
2468        struct rtl_beacon_keys *cur_bcn_key;
2469        u8 *ht_cap;
2470        u8 ht_cap_len;
2471        u8 *ht_oper;
2472        u8 ht_oper_len;
2473        u8 *ds_param;
2474        u8 ds_param_len;
2475
2476        if (mac->opmode != NL80211_IFTYPE_STATION)
2477                return false;
2478
2479        /* check if this really is a beacon*/
2480        if (!ieee80211_is_beacon(hdr->frame_control))
2481                return false;
2482
2483        /* min. beacon length + FCS_LEN */
2484        if (len <= 40 + FCS_LEN)
2485                return false;
2486
2487        cur_bcn_key = &mac->cur_beacon_keys;
2488
2489        if (rtlpriv->mac80211.link_state == MAC80211_NOLINK) {
2490                if (cur_bcn_key->valid) {
2491                        cur_bcn_key->valid = false;
2492                        RT_TRACE(rtlpriv, COMP_BEACON, DBG_LOUD,
2493                                 "Reset cur_beacon_keys.valid to false!\n");
2494                }
2495                return false;
2496        }
2497
2498        /* and only beacons from the associated BSSID, please */
2499        if (!ether_addr_equal(hdr->addr3, rtlpriv->mac80211.bssid))
2500                return false;
2501
2502        /***** Parsing DS Param IE ******/
2503        ds_param = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_DS_PARAMS);
2504
2505        if (ds_param && !(ds_param[1] < sizeof(*ds_param))) {
2506                ds_param_len = ds_param[1];
2507                bcn_key.bcn_channel = ds_param[2];
2508        } else {
2509                ds_param = NULL;
2510        }
2511
2512        /***** Parsing HT Cap. IE ******/
2513        ht_cap = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_HT_CAPABILITY);
2514
2515        if (ht_cap && !(ht_cap[1] < sizeof(*ht_cap))) {
2516                ht_cap_len = ht_cap[1];
2517                ht_cap_ie = (struct ieee80211_ht_cap *)&ht_cap[2];
2518                bcn_key.ht_cap_info = ht_cap_ie->cap_info;
2519        } else  {
2520                ht_cap = NULL;
2521        }
2522
2523        /***** Parsing HT Info. IE ******/
2524        ht_oper = rtl_find_ie(data, len - FCS_LEN, WLAN_EID_HT_OPERATION);
2525
2526        if (ht_oper && !(ht_oper[1] < sizeof(*ht_oper))) {
2527                ht_oper_len = ht_oper[1];
2528                ht_oper_ie = (struct ieee80211_ht_operation *)&ht_oper[2];
2529        } else {
2530                ht_oper = NULL;
2531        }
2532
2533        /* update bcn_key */
2534
2535        if (!ds_param && ht_oper && ht_oper_ie)
2536                bcn_key.bcn_channel = ht_oper_ie->primary_chan;
2537
2538        if (ht_oper && ht_oper_ie)
2539                bcn_key.ht_info_infos_0_sco = ht_oper_ie->ht_param & 0x03;
2540
2541        bcn_key.valid = true;
2542
2543        /* update cur_beacon_keys or compare beacon key */
2544        if (rtlpriv->mac80211.link_state != MAC80211_LINKED &&
2545            rtlpriv->mac80211.link_state != MAC80211_LINKED_SCANNING)
2546                return true;
2547
2548        if (!cur_bcn_key->valid) {
2549                /* update cur_beacon_keys */
2550                memcpy(cur_bcn_key, &bcn_key, sizeof(bcn_key));
2551                cur_bcn_key->valid = true;
2552
2553                RT_TRACE(rtlpriv, COMP_BEACON, DBG_LOUD,
2554                         "Beacon key update!ch=%d, ht_cap_info=0x%x, sco=0x%x\n",
2555                         cur_bcn_key->bcn_channel,
2556                         cur_bcn_key->ht_cap_info,
2557                         cur_bcn_key->ht_info_infos_0_sco);
2558                return true;
2559        }
2560
2561        /* compare beacon key */
2562        if (!memcmp(cur_bcn_key, &bcn_key, sizeof(bcn_key))) {
2563                /* same beacon key */
2564                mac->new_beacon_cnt = 0;
2565                goto chk_exit;
2566        }
2567
2568        if (cur_bcn_key->bcn_channel == bcn_key.bcn_channel &&
2569            cur_bcn_key->ht_cap_info == bcn_key.ht_cap_info) {
2570                /* Beacon HT info IE, secondary channel offset check */
2571                /* 40M -> 20M */
2572                if (cur_bcn_key->ht_info_infos_0_sco >
2573                    bcn_key.ht_info_infos_0_sco) {
2574                        /* Not a new beacon */
2575                        RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2576                                 "Beacon BW change! sco:0x%x -> 0x%x\n",
2577                                 cur_bcn_key->ht_info_infos_0_sco,
2578                                 bcn_key.ht_info_infos_0_sco);
2579
2580                        cur_bcn_key->ht_info_infos_0_sco =
2581                                        bcn_key.ht_info_infos_0_sco;
2582                } else {
2583                        /* 20M -> 40M */
2584                        if (rtlphy->max_ht_chan_bw >= HT_CHANNEL_WIDTH_20_40) {
2585                                /* Not a new beacon */
2586                                RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2587                                         "Beacon BW change! sco:0x%x -> 0x%x\n",
2588                                         cur_bcn_key->ht_info_infos_0_sco,
2589                                         bcn_key.ht_info_infos_0_sco);
2590
2591                                cur_bcn_key->ht_info_infos_0_sco =
2592                                        bcn_key.ht_info_infos_0_sco;
2593                        } else {
2594                                mac->new_beacon_cnt++;
2595                        }
2596                }
2597        } else {
2598                mac->new_beacon_cnt++;
2599        }
2600
2601        if (mac->new_beacon_cnt == 1) {
2602                RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2603                         "Get new beacon.\n");
2604                RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2605                         "Cur : ch=%d, ht_cap=0x%x, sco=0x%x\n",
2606                         cur_bcn_key->bcn_channel,
2607                         cur_bcn_key->ht_cap_info,
2608                         cur_bcn_key->ht_info_infos_0_sco);
2609                RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2610                         "New RX : ch=%d, ht_cap=0x%x, sco=0x%x\n",
2611                         bcn_key.bcn_channel,
2612                         bcn_key.ht_cap_info,
2613                         bcn_key.ht_info_infos_0_sco);
2614
2615        } else if (mac->new_beacon_cnt > 1) {
2616                RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2617                         "new beacon cnt: %d\n",
2618                         mac->new_beacon_cnt);
2619        }
2620
2621        if (mac->new_beacon_cnt > 3) {
2622                ieee80211_connection_loss(rtlpriv->mac80211.vif);
2623                RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2624                         "new beacon cnt >3, disconnect !\n");
2625        }
2626
2627chk_exit:
2628
2629        return true;
2630}
2631
2632/*********************************************************
2633 *
2634 * IOT functions
2635 *
2636 *********************************************************/
2637static bool rtl_chk_vendor_ouisub(struct ieee80211_hw *hw,
2638                                  struct octet_string vendor_ie)
2639{
2640        struct rtl_priv *rtlpriv = rtl_priv(hw);
2641        bool matched = false;
2642        static u8 athcap_1[] = { 0x00, 0x03, 0x7F };
2643        static u8 athcap_2[] = { 0x00, 0x13, 0x74 };
2644        static u8 broadcap_1[] = { 0x00, 0x10, 0x18 };
2645        static u8 broadcap_2[] = { 0x00, 0x0a, 0xf7 };
2646        static u8 broadcap_3[] = { 0x00, 0x05, 0xb5 };
2647        static u8 racap[] = { 0x00, 0x0c, 0x43 };
2648        static u8 ciscocap[] = { 0x00, 0x40, 0x96 };
2649        static u8 marvcap[] = { 0x00, 0x50, 0x43 };
2650
2651        if (memcmp(vendor_ie.octet, athcap_1, 3) == 0 ||
2652            memcmp(vendor_ie.octet, athcap_2, 3) == 0) {
2653                rtlpriv->mac80211.vendor = PEER_ATH;
2654                matched = true;
2655        } else if (memcmp(vendor_ie.octet, broadcap_1, 3) == 0 ||
2656                   memcmp(vendor_ie.octet, broadcap_2, 3) == 0 ||
2657                   memcmp(vendor_ie.octet, broadcap_3, 3) == 0) {
2658                rtlpriv->mac80211.vendor = PEER_BROAD;
2659                matched = true;
2660        } else if (memcmp(vendor_ie.octet, racap, 3) == 0) {
2661                rtlpriv->mac80211.vendor = PEER_RAL;
2662                matched = true;
2663        } else if (memcmp(vendor_ie.octet, ciscocap, 3) == 0) {
2664                rtlpriv->mac80211.vendor = PEER_CISCO;
2665                matched = true;
2666        } else if (memcmp(vendor_ie.octet, marvcap, 3) == 0) {
2667                rtlpriv->mac80211.vendor = PEER_MARV;
2668                matched = true;
2669        }
2670
2671        return matched;
2672}
2673
2674static bool rtl_find_221_ie(struct ieee80211_hw *hw, u8 *data,
2675                            unsigned int len)
2676{
2677        struct ieee80211_mgmt *mgmt = (void *)data;
2678        struct octet_string vendor_ie;
2679        u8 *pos, *end;
2680
2681        pos = (u8 *)mgmt->u.beacon.variable;
2682        end = data + len;
2683        while (pos < end) {
2684                if (pos[0] == 221) {
2685                        vendor_ie.length = pos[1];
2686                        vendor_ie.octet = &pos[2];
2687                        if (rtl_chk_vendor_ouisub(hw, vendor_ie))
2688                                return true;
2689                }
2690
2691                if (pos + 2 + pos[1] > end)
2692                        return false;
2693
2694                pos += 2 + pos[1];
2695        }
2696        return false;
2697}
2698
2699void rtl_recognize_peer(struct ieee80211_hw *hw, u8 *data, unsigned int len)
2700{
2701        struct rtl_priv *rtlpriv = rtl_priv(hw);
2702        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2703        struct ieee80211_hdr *hdr = (void *)data;
2704        u32 vendor = PEER_UNKNOWN;
2705
2706        static u8 ap3_1[3] = { 0x00, 0x14, 0xbf };
2707        static u8 ap3_2[3] = { 0x00, 0x1a, 0x70 };
2708        static u8 ap3_3[3] = { 0x00, 0x1d, 0x7e };
2709        static u8 ap4_1[3] = { 0x00, 0x90, 0xcc };
2710        static u8 ap4_2[3] = { 0x00, 0x0e, 0x2e };
2711        static u8 ap4_3[3] = { 0x00, 0x18, 0x02 };
2712        static u8 ap4_4[3] = { 0x00, 0x17, 0x3f };
2713        static u8 ap4_5[3] = { 0x00, 0x1c, 0xdf };
2714        static u8 ap5_1[3] = { 0x00, 0x1c, 0xf0 };
2715        static u8 ap5_2[3] = { 0x00, 0x21, 0x91 };
2716        static u8 ap5_3[3] = { 0x00, 0x24, 0x01 };
2717        static u8 ap5_4[3] = { 0x00, 0x15, 0xe9 };
2718        static u8 ap5_5[3] = { 0x00, 0x17, 0x9A };
2719        static u8 ap5_6[3] = { 0x00, 0x18, 0xE7 };
2720        static u8 ap6_1[3] = { 0x00, 0x17, 0x94 };
2721        static u8 ap7_1[3] = { 0x00, 0x14, 0xa4 };
2722
2723        if (mac->opmode != NL80211_IFTYPE_STATION)
2724                return;
2725
2726        if (mac->link_state == MAC80211_NOLINK) {
2727                mac->vendor = PEER_UNKNOWN;
2728                return;
2729        }
2730
2731        if (mac->cnt_after_linked > 2)
2732                return;
2733
2734        /* check if this really is a beacon */
2735        if (!ieee80211_is_beacon(hdr->frame_control))
2736                return;
2737
2738        /* min. beacon length + FCS_LEN */
2739        if (len <= 40 + FCS_LEN)
2740                return;
2741
2742        /* and only beacons from the associated BSSID, please */
2743        if (!ether_addr_equal_64bits(hdr->addr3, rtlpriv->mac80211.bssid))
2744                return;
2745
2746        if (rtl_find_221_ie(hw, data, len))
2747                vendor = mac->vendor;
2748
2749        if ((memcmp(mac->bssid, ap5_1, 3) == 0) ||
2750            (memcmp(mac->bssid, ap5_2, 3) == 0) ||
2751            (memcmp(mac->bssid, ap5_3, 3) == 0) ||
2752            (memcmp(mac->bssid, ap5_4, 3) == 0) ||
2753            (memcmp(mac->bssid, ap5_5, 3) == 0) ||
2754            (memcmp(mac->bssid, ap5_6, 3) == 0) ||
2755            vendor == PEER_ATH) {
2756                vendor = PEER_ATH;
2757                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ath find\n");
2758        } else if ((memcmp(mac->bssid, ap4_4, 3) == 0) ||
2759                (memcmp(mac->bssid, ap4_5, 3) == 0) ||
2760                (memcmp(mac->bssid, ap4_1, 3) == 0) ||
2761                (memcmp(mac->bssid, ap4_2, 3) == 0) ||
2762                (memcmp(mac->bssid, ap4_3, 3) == 0) ||
2763                vendor == PEER_RAL) {
2764                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>ral find\n");
2765                vendor = PEER_RAL;
2766        } else if (memcmp(mac->bssid, ap6_1, 3) == 0 ||
2767                vendor == PEER_CISCO) {
2768                vendor = PEER_CISCO;
2769                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>cisco find\n");
2770        } else if ((memcmp(mac->bssid, ap3_1, 3) == 0) ||
2771                (memcmp(mac->bssid, ap3_2, 3) == 0) ||
2772                (memcmp(mac->bssid, ap3_3, 3) == 0) ||
2773                vendor == PEER_BROAD) {
2774                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>broad find\n");
2775                vendor = PEER_BROAD;
2776        } else if (memcmp(mac->bssid, ap7_1, 3) == 0 ||
2777                vendor == PEER_MARV) {
2778                vendor = PEER_MARV;
2779                RT_TRACE(rtlpriv, COMP_MAC80211, DBG_LOUD, "=>marv find\n");
2780        }
2781
2782        mac->vendor = vendor;
2783}
2784
2785MODULE_AUTHOR("lizhaoming       <chaoming_li@realsil.com.cn>");
2786MODULE_AUTHOR("Realtek WlanFAE  <wlanfae@realtek.com>");
2787MODULE_AUTHOR("Larry Finger     <Larry.FInger@lwfinger.net>");
2788MODULE_LICENSE("GPL");
2789MODULE_DESCRIPTION("Realtek 802.11n PCI wireless core");
2790
2791struct rtl_global_var rtl_global_var = {};
2792
2793int rtl_core_module_init(void)
2794{
2795        if (rtl_rate_control_register())
2796                pr_err("rtl: Unable to register rtl_rc, use default RC !!\n");
2797
2798        /* add debugfs */
2799        rtl_debugfs_add_topdir();
2800
2801        /* init some global vars */
2802        INIT_LIST_HEAD(&rtl_global_var.glb_priv_list);
2803        spin_lock_init(&rtl_global_var.glb_list_lock);
2804
2805        return 0;
2806}
2807
2808void rtl_core_module_exit(void)
2809{
2810        /*RC*/
2811        rtl_rate_control_unregister();
2812
2813        /* remove debugfs */
2814        rtl_debugfs_remove_topdir();
2815}
2816