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