linux/drivers/net/wireless/realtek/rtlwifi/rtl8821ae/hw.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2009-2010  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 "../efuse.h"
  28#include "../base.h"
  29#include "../regd.h"
  30#include "../cam.h"
  31#include "../ps.h"
  32#include "../pci.h"
  33#include "reg.h"
  34#include "def.h"
  35#include "phy.h"
  36#include "dm.h"
  37#include "fw.h"
  38#include "led.h"
  39#include "hw.h"
  40#include "../pwrseqcmd.h"
  41#include "pwrseq.h"
  42#include "../btcoexist/rtl_btc.h"
  43
  44#define LLT_CONFIG      5
  45
  46static void _rtl8821ae_return_beacon_queue_skb(struct ieee80211_hw *hw)
  47{
  48        struct rtl_priv *rtlpriv = rtl_priv(hw);
  49        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  50        struct rtl8192_tx_ring *ring = &rtlpci->tx_ring[BEACON_QUEUE];
  51        unsigned long flags;
  52
  53        spin_lock_irqsave(&rtlpriv->locks.irq_th_lock, flags);
  54        while (skb_queue_len(&ring->queue)) {
  55                struct rtl_tx_desc *entry = &ring->desc[ring->idx];
  56                struct sk_buff *skb = __skb_dequeue(&ring->queue);
  57
  58                pci_unmap_single(rtlpci->pdev,
  59                                 rtlpriv->cfg->ops->get_desc(
  60                                 (u8 *)entry, true, HW_DESC_TXBUFF_ADDR),
  61                                 skb->len, PCI_DMA_TODEVICE);
  62                kfree_skb(skb);
  63                ring->idx = (ring->idx + 1) % ring->entries;
  64        }
  65        spin_unlock_irqrestore(&rtlpriv->locks.irq_th_lock, flags);
  66}
  67
  68static void _rtl8821ae_set_bcn_ctrl_reg(struct ieee80211_hw *hw,
  69                                        u8 set_bits, u8 clear_bits)
  70{
  71        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  72        struct rtl_priv *rtlpriv = rtl_priv(hw);
  73
  74        rtlpci->reg_bcn_ctrl_val |= set_bits;
  75        rtlpci->reg_bcn_ctrl_val &= ~clear_bits;
  76
  77        rtl_write_byte(rtlpriv, REG_BCN_CTRL, (u8)rtlpci->reg_bcn_ctrl_val);
  78}
  79
  80void _rtl8821ae_stop_tx_beacon(struct ieee80211_hw *hw)
  81{
  82        struct rtl_priv *rtlpriv = rtl_priv(hw);
  83        u8 tmp1byte;
  84
  85        tmp1byte = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
  86        rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2, tmp1byte & (~BIT(6)));
  87        rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0x64);
  88        tmp1byte = rtl_read_byte(rtlpriv, REG_TBTT_PROHIBIT + 2);
  89        tmp1byte &= ~(BIT(0));
  90        rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 2, tmp1byte);
  91}
  92
  93void _rtl8821ae_resume_tx_beacon(struct ieee80211_hw *hw)
  94{
  95        struct rtl_priv *rtlpriv = rtl_priv(hw);
  96        u8 tmp1byte;
  97
  98        tmp1byte = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
  99        rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2, tmp1byte | BIT(6));
 100        rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0xff);
 101        tmp1byte = rtl_read_byte(rtlpriv, REG_TBTT_PROHIBIT + 2);
 102        tmp1byte |= BIT(0);
 103        rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 2, tmp1byte);
 104}
 105
 106static void _rtl8821ae_enable_bcn_sub_func(struct ieee80211_hw *hw)
 107{
 108        _rtl8821ae_set_bcn_ctrl_reg(hw, 0, BIT(1));
 109}
 110
 111static void _rtl8821ae_disable_bcn_sub_func(struct ieee80211_hw *hw)
 112{
 113        _rtl8821ae_set_bcn_ctrl_reg(hw, BIT(1), 0);
 114}
 115
 116static void _rtl8821ae_set_fw_clock_on(struct ieee80211_hw *hw,
 117                                       u8 rpwm_val, bool b_need_turn_off_ckk)
 118{
 119        struct rtl_priv *rtlpriv = rtl_priv(hw);
 120        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 121        bool b_support_remote_wake_up;
 122        u32 count = 0, isr_regaddr, content;
 123        bool b_schedule_timer = b_need_turn_off_ckk;
 124
 125        rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
 126                                        (u8 *)(&b_support_remote_wake_up));
 127
 128        if (!rtlhal->fw_ready)
 129                return;
 130        if (!rtlpriv->psc.fw_current_inpsmode)
 131                return;
 132
 133        while (1) {
 134                spin_lock_bh(&rtlpriv->locks.fw_ps_lock);
 135                if (rtlhal->fw_clk_change_in_progress) {
 136                        while (rtlhal->fw_clk_change_in_progress) {
 137                                spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 138                                count++;
 139                                udelay(100);
 140                                if (count > 1000)
 141                                        goto change_done;
 142                                spin_lock_bh(&rtlpriv->locks.fw_ps_lock);
 143                        }
 144                        spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 145                } else {
 146                        rtlhal->fw_clk_change_in_progress = false;
 147                        spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 148                        goto change_done;
 149                }
 150        }
 151change_done:
 152        if (IS_IN_LOW_POWER_STATE_8821AE(rtlhal->fw_ps_state)) {
 153                rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_SET_RPWM,
 154                                        (u8 *)(&rpwm_val));
 155                if (FW_PS_IS_ACK(rpwm_val)) {
 156                        isr_regaddr = REG_HISR;
 157                        content = rtl_read_dword(rtlpriv, isr_regaddr);
 158                        while (!(content & IMR_CPWM) && (count < 500)) {
 159                                udelay(50);
 160                                count++;
 161                                content = rtl_read_dword(rtlpriv, isr_regaddr);
 162                        }
 163
 164                        if (content & IMR_CPWM) {
 165                                rtl_write_word(rtlpriv, isr_regaddr, 0x0100);
 166                                rtlhal->fw_ps_state = FW_PS_STATE_RF_ON_8821AE;
 167                                RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
 168                                         "Receive CPWM INT!!! Set rtlhal->FwPSState = %X\n",
 169                                         rtlhal->fw_ps_state);
 170                        }
 171                }
 172
 173                spin_lock_bh(&rtlpriv->locks.fw_ps_lock);
 174                rtlhal->fw_clk_change_in_progress = false;
 175                spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 176                if (b_schedule_timer)
 177                        mod_timer(&rtlpriv->works.fw_clockoff_timer,
 178                                  jiffies + MSECS(10));
 179        } else  {
 180                spin_lock_bh(&rtlpriv->locks.fw_ps_lock);
 181                rtlhal->fw_clk_change_in_progress = false;
 182                spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 183        }
 184}
 185
 186static void _rtl8821ae_set_fw_clock_off(struct ieee80211_hw *hw,
 187                                        u8 rpwm_val)
 188{
 189        struct rtl_priv *rtlpriv = rtl_priv(hw);
 190        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 191        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
 192        struct rtl8192_tx_ring *ring;
 193        enum rf_pwrstate rtstate;
 194        bool b_schedule_timer = false;
 195        u8 queue;
 196
 197        if (!rtlhal->fw_ready)
 198                return;
 199        if (!rtlpriv->psc.fw_current_inpsmode)
 200                return;
 201        if (!rtlhal->allow_sw_to_change_hwclc)
 202                return;
 203        rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_RF_STATE, (u8 *)(&rtstate));
 204        if (rtstate == ERFOFF || rtlpriv->psc.inactive_pwrstate == ERFOFF)
 205                return;
 206
 207        for (queue = 0; queue < RTL_PCI_MAX_TX_QUEUE_COUNT; queue++) {
 208                ring = &rtlpci->tx_ring[queue];
 209                if (skb_queue_len(&ring->queue)) {
 210                        b_schedule_timer = true;
 211                        break;
 212                }
 213        }
 214
 215        if (b_schedule_timer) {
 216                mod_timer(&rtlpriv->works.fw_clockoff_timer,
 217                          jiffies + MSECS(10));
 218                return;
 219        }
 220
 221        if (FW_PS_STATE(rtlhal->fw_ps_state) !=
 222                FW_PS_STATE_RF_OFF_LOW_PWR_8821AE) {
 223                spin_lock_bh(&rtlpriv->locks.fw_ps_lock);
 224                if (!rtlhal->fw_clk_change_in_progress) {
 225                        rtlhal->fw_clk_change_in_progress = true;
 226                        spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 227                        rtlhal->fw_ps_state = FW_PS_STATE(rpwm_val);
 228                        rtl_write_word(rtlpriv, REG_HISR, 0x0100);
 229                        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM,
 230                                                      (u8 *)(&rpwm_val));
 231                        spin_lock_bh(&rtlpriv->locks.fw_ps_lock);
 232                        rtlhal->fw_clk_change_in_progress = false;
 233                        spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 234                } else {
 235                        spin_unlock_bh(&rtlpriv->locks.fw_ps_lock);
 236                        mod_timer(&rtlpriv->works.fw_clockoff_timer,
 237                                  jiffies + MSECS(10));
 238                }
 239        }
 240}
 241
 242static void _rtl8821ae_set_fw_ps_rf_on(struct ieee80211_hw *hw)
 243{
 244        u8 rpwm_val = 0;
 245
 246        rpwm_val |= (FW_PS_STATE_RF_OFF_8821AE | FW_PS_ACK);
 247        _rtl8821ae_set_fw_clock_on(hw, rpwm_val, true);
 248}
 249
 250static void _rtl8821ae_fwlps_leave(struct ieee80211_hw *hw)
 251{
 252        struct rtl_priv *rtlpriv = rtl_priv(hw);
 253        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 254        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 255        bool fw_current_inps = false;
 256        u8 rpwm_val = 0, fw_pwrmode = FW_PS_ACTIVE_MODE;
 257
 258        if (ppsc->low_power_enable) {
 259                rpwm_val = (FW_PS_STATE_ALL_ON_8821AE|FW_PS_ACK);/* RF on */
 260                _rtl8821ae_set_fw_clock_on(hw, rpwm_val, false);
 261                rtlhal->allow_sw_to_change_hwclc = false;
 262                rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_PWRMODE,
 263                                (u8 *)(&fw_pwrmode));
 264                rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
 265                                (u8 *)(&fw_current_inps));
 266        } else {
 267                rpwm_val = FW_PS_STATE_ALL_ON_8821AE;   /* RF on */
 268                rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SET_RPWM,
 269                                (u8 *)(&rpwm_val));
 270                rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_H2C_FW_PWRMODE,
 271                                (u8 *)(&fw_pwrmode));
 272                rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
 273                                (u8 *)(&fw_current_inps));
 274        }
 275}
 276
 277static void _rtl8821ae_fwlps_enter(struct ieee80211_hw *hw)
 278{
 279        struct rtl_priv *rtlpriv = rtl_priv(hw);
 280        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 281        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 282        bool fw_current_inps = true;
 283        u8 rpwm_val;
 284
 285        if (ppsc->low_power_enable) {
 286                rpwm_val = FW_PS_STATE_RF_OFF_LOW_PWR_8821AE;   /* RF off */
 287                rtlpriv->cfg->ops->set_hw_reg(hw,
 288                                HW_VAR_FW_PSMODE_STATUS,
 289                                (u8 *)(&fw_current_inps));
 290                rtlpriv->cfg->ops->set_hw_reg(hw,
 291                                HW_VAR_H2C_FW_PWRMODE,
 292                                (u8 *)(&ppsc->fwctrl_psmode));
 293                rtlhal->allow_sw_to_change_hwclc = true;
 294                _rtl8821ae_set_fw_clock_off(hw, rpwm_val);
 295        } else {
 296                rpwm_val = FW_PS_STATE_RF_OFF_8821AE;   /* RF off */
 297                rtlpriv->cfg->ops->set_hw_reg(hw,
 298                                HW_VAR_FW_PSMODE_STATUS,
 299                                (u8 *)(&fw_current_inps));
 300                rtlpriv->cfg->ops->set_hw_reg(hw,
 301                                HW_VAR_H2C_FW_PWRMODE,
 302                                (u8 *)(&ppsc->fwctrl_psmode));
 303                rtlpriv->cfg->ops->set_hw_reg(hw,
 304                                HW_VAR_SET_RPWM,
 305                                (u8 *)(&rpwm_val));
 306        }
 307}
 308
 309static void _rtl8821ae_download_rsvd_page(struct ieee80211_hw *hw,
 310                                          bool dl_whole_packets)
 311{
 312        struct rtl_priv *rtlpriv = rtl_priv(hw);
 313        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
 314        u8 tmp_regcr, tmp_reg422, bcnvalid_reg;
 315        u8 count = 0, dlbcn_count = 0;
 316        bool send_beacon = false;
 317
 318        tmp_regcr = rtl_read_byte(rtlpriv, REG_CR + 1);
 319        rtl_write_byte(rtlpriv, REG_CR + 1, (tmp_regcr | BIT(0)));
 320
 321        _rtl8821ae_set_bcn_ctrl_reg(hw, 0, BIT(3));
 322        _rtl8821ae_set_bcn_ctrl_reg(hw, BIT(4), 0);
 323
 324        tmp_reg422 = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
 325        rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2,
 326                       tmp_reg422 & (~BIT(6)));
 327        if (tmp_reg422 & BIT(6))
 328                send_beacon = true;
 329
 330        do {
 331                bcnvalid_reg = rtl_read_byte(rtlpriv, REG_TDECTRL + 2);
 332                rtl_write_byte(rtlpriv, REG_TDECTRL + 2,
 333                               (bcnvalid_reg | BIT(0)));
 334                _rtl8821ae_return_beacon_queue_skb(hw);
 335
 336                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
 337                        rtl8812ae_set_fw_rsvdpagepkt(hw, false,
 338                                                     dl_whole_packets);
 339                else
 340                        rtl8821ae_set_fw_rsvdpagepkt(hw, false,
 341                                                     dl_whole_packets);
 342
 343                bcnvalid_reg = rtl_read_byte(rtlpriv, REG_TDECTRL + 2);
 344                count = 0;
 345                while (!(bcnvalid_reg & BIT(0)) && count < 20) {
 346                        count++;
 347                        udelay(10);
 348                        bcnvalid_reg = rtl_read_byte(rtlpriv, REG_TDECTRL + 2);
 349                }
 350                dlbcn_count++;
 351        } while (!(bcnvalid_reg & BIT(0)) && dlbcn_count < 5);
 352
 353        if (!(bcnvalid_reg & BIT(0)))
 354                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
 355                         "Download RSVD page failed!\n");
 356        if (bcnvalid_reg & BIT(0) && rtlhal->enter_pnp_sleep) {
 357                rtl_write_byte(rtlpriv, REG_TDECTRL + 2, bcnvalid_reg | BIT(0));
 358                _rtl8821ae_return_beacon_queue_skb(hw);
 359                if (send_beacon) {
 360                        dlbcn_count = 0;
 361                        do {
 362                                rtl_write_byte(rtlpriv, REG_TDECTRL + 2,
 363                                               bcnvalid_reg | BIT(0));
 364
 365                                _rtl8821ae_return_beacon_queue_skb(hw);
 366
 367                                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
 368                                        rtl8812ae_set_fw_rsvdpagepkt(hw, true,
 369                                                                     false);
 370                                else
 371                                        rtl8821ae_set_fw_rsvdpagepkt(hw, true,
 372                                                                     false);
 373
 374                                /* check rsvd page download OK. */
 375                                bcnvalid_reg = rtl_read_byte(rtlpriv,
 376                                                             REG_TDECTRL + 2);
 377                                count = 0;
 378                                while (!(bcnvalid_reg & BIT(0)) && count < 20) {
 379                                        count++;
 380                                        udelay(10);
 381                                        bcnvalid_reg =
 382                                          rtl_read_byte(rtlpriv,
 383                                                        REG_TDECTRL + 2);
 384                                }
 385                                dlbcn_count++;
 386                        } while (!(bcnvalid_reg & BIT(0)) && dlbcn_count < 5);
 387
 388                        if (!(bcnvalid_reg & BIT(0)))
 389                                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
 390                                         "2 Download RSVD page failed!\n");
 391                }
 392        }
 393
 394        if (bcnvalid_reg & BIT(0))
 395                rtl_write_byte(rtlpriv, REG_TDECTRL + 2, BIT(0));
 396
 397        _rtl8821ae_set_bcn_ctrl_reg(hw, BIT(3), 0);
 398        _rtl8821ae_set_bcn_ctrl_reg(hw, 0, BIT(4));
 399
 400        if (send_beacon)
 401                rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2, tmp_reg422);
 402
 403        if (!rtlhal->enter_pnp_sleep) {
 404                tmp_regcr = rtl_read_byte(rtlpriv, REG_CR + 1);
 405                rtl_write_byte(rtlpriv, REG_CR + 1, (tmp_regcr & ~(BIT(0))));
 406        }
 407}
 408
 409void rtl8821ae_get_hw_reg(struct ieee80211_hw *hw, u8 variable, u8 *val)
 410{
 411        struct rtl_priv *rtlpriv = rtl_priv(hw);
 412        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 413        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
 414        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 415
 416        switch (variable) {
 417        case HW_VAR_ETHER_ADDR:
 418                *((u32 *)(val)) = rtl_read_dword(rtlpriv, REG_MACID);
 419                *((u16 *)(val+4)) = rtl_read_word(rtlpriv, REG_MACID + 4);
 420                break;
 421        case HW_VAR_BSSID:
 422                *((u32 *)(val)) = rtl_read_dword(rtlpriv, REG_BSSID);
 423                *((u16 *)(val+4)) = rtl_read_word(rtlpriv, REG_BSSID+4);
 424                break;
 425        case HW_VAR_MEDIA_STATUS:
 426                val[0] = rtl_read_byte(rtlpriv, MSR) & 0x3;
 427                break;
 428        case HW_VAR_SLOT_TIME:
 429                *((u8 *)(val)) = mac->slot_time;
 430                break;
 431        case HW_VAR_BEACON_INTERVAL:
 432                *((u16 *)(val)) = rtl_read_word(rtlpriv, REG_BCN_INTERVAL);
 433                break;
 434        case HW_VAR_ATIM_WINDOW:
 435                *((u16 *)(val)) =  rtl_read_word(rtlpriv, REG_ATIMWND);
 436                break;
 437        case HW_VAR_RCR:
 438                *((u32 *)(val)) = rtlpci->receive_config;
 439                break;
 440        case HW_VAR_RF_STATE:
 441                *((enum rf_pwrstate *)(val)) = ppsc->rfpwr_state;
 442                break;
 443        case HW_VAR_FWLPS_RF_ON:{
 444                enum rf_pwrstate rfstate;
 445                u32 val_rcr;
 446
 447                rtlpriv->cfg->ops->get_hw_reg(hw,
 448                                              HW_VAR_RF_STATE,
 449                                              (u8 *)(&rfstate));
 450                if (rfstate == ERFOFF) {
 451                        *((bool *)(val)) = true;
 452                } else {
 453                        val_rcr = rtl_read_dword(rtlpriv, REG_RCR);
 454                        val_rcr &= 0x00070000;
 455                        if (val_rcr)
 456                                *((bool *)(val)) = false;
 457                        else
 458                                *((bool *)(val)) = true;
 459                }
 460                break; }
 461        case HW_VAR_FW_PSMODE_STATUS:
 462                *((bool *)(val)) = ppsc->fw_current_inpsmode;
 463                break;
 464        case HW_VAR_CORRECT_TSF:{
 465                u64 tsf;
 466                u32 *ptsf_low = (u32 *)&tsf;
 467                u32 *ptsf_high = ((u32 *)&tsf) + 1;
 468
 469                *ptsf_high = rtl_read_dword(rtlpriv, (REG_TSFTR + 4));
 470                *ptsf_low = rtl_read_dword(rtlpriv, REG_TSFTR);
 471
 472                *((u64 *)(val)) = tsf;
 473
 474                break; }
 475        case HAL_DEF_WOWLAN:
 476                if (ppsc->wo_wlan_mode)
 477                        *((bool *)(val)) = true;
 478                else
 479                        *((bool *)(val)) = false;
 480                break;
 481        default:
 482                RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
 483                         "switch case %#x not processed\n", variable);
 484                break;
 485        }
 486}
 487
 488void rtl8821ae_set_hw_reg(struct ieee80211_hw *hw, u8 variable, u8 *val)
 489{
 490        struct rtl_priv *rtlpriv = rtl_priv(hw);
 491        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
 492        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
 493        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
 494        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 495        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 496        u8 idx;
 497
 498        switch (variable) {
 499        case HW_VAR_ETHER_ADDR:{
 500                        for (idx = 0; idx < ETH_ALEN; idx++) {
 501                                rtl_write_byte(rtlpriv, (REG_MACID + idx),
 502                                               val[idx]);
 503                        }
 504                        break;
 505                }
 506        case HW_VAR_BASIC_RATE:{
 507                        u16 b_rate_cfg = ((u16 *)val)[0];
 508                        b_rate_cfg = b_rate_cfg & 0x15f;
 509                        rtl_write_word(rtlpriv, REG_RRSR, b_rate_cfg);
 510                        break;
 511                }
 512        case HW_VAR_BSSID:{
 513                        for (idx = 0; idx < ETH_ALEN; idx++) {
 514                                rtl_write_byte(rtlpriv, (REG_BSSID + idx),
 515                                               val[idx]);
 516                        }
 517                        break;
 518                }
 519        case HW_VAR_SIFS:
 520                rtl_write_byte(rtlpriv, REG_SIFS_CTX + 1, val[0]);
 521                rtl_write_byte(rtlpriv, REG_SIFS_TRX + 1, val[0]);
 522
 523                rtl_write_byte(rtlpriv, REG_SPEC_SIFS + 1, val[0]);
 524                rtl_write_byte(rtlpriv, REG_MAC_SPEC_SIFS + 1, val[0]);
 525
 526                rtl_write_byte(rtlpriv, REG_RESP_SIFS_OFDM + 1, val[0]);
 527                rtl_write_byte(rtlpriv, REG_RESP_SIFS_OFDM, val[0]);
 528                break;
 529        case HW_VAR_R2T_SIFS:
 530                rtl_write_byte(rtlpriv, REG_RESP_SIFS_OFDM + 1, val[0]);
 531                break;
 532        case HW_VAR_SLOT_TIME:{
 533                u8 e_aci;
 534
 535                RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
 536                         "HW_VAR_SLOT_TIME %x\n", val[0]);
 537
 538                rtl_write_byte(rtlpriv, REG_SLOT, val[0]);
 539
 540                for (e_aci = 0; e_aci < AC_MAX; e_aci++) {
 541                        rtlpriv->cfg->ops->set_hw_reg(hw,
 542                                                      HW_VAR_AC_PARAM,
 543                                                      (u8 *)(&e_aci));
 544                }
 545                break; }
 546        case HW_VAR_ACK_PREAMBLE:{
 547                u8 reg_tmp;
 548                u8 short_preamble = (bool)(*(u8 *)val);
 549
 550                reg_tmp = rtl_read_byte(rtlpriv, REG_TRXPTCL_CTL+2);
 551                if (short_preamble) {
 552                        reg_tmp |= BIT(1);
 553                        rtl_write_byte(rtlpriv, REG_TRXPTCL_CTL + 2,
 554                                       reg_tmp);
 555                } else {
 556                        reg_tmp &= (~BIT(1));
 557                        rtl_write_byte(rtlpriv,
 558                                REG_TRXPTCL_CTL + 2,
 559                                reg_tmp);
 560                }
 561                break; }
 562        case HW_VAR_WPA_CONFIG:
 563                rtl_write_byte(rtlpriv, REG_SECCFG, *((u8 *)val));
 564                break;
 565        case HW_VAR_AMPDU_MIN_SPACE:{
 566                u8 min_spacing_to_set;
 567                u8 sec_min_space;
 568
 569                min_spacing_to_set = *((u8 *)val);
 570                if (min_spacing_to_set <= 7) {
 571                        sec_min_space = 0;
 572
 573                        if (min_spacing_to_set < sec_min_space)
 574                                min_spacing_to_set = sec_min_space;
 575
 576                        mac->min_space_cfg = ((mac->min_space_cfg &
 577                                               0xf8) |
 578                                              min_spacing_to_set);
 579
 580                        *val = min_spacing_to_set;
 581
 582                        RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
 583                                 "Set HW_VAR_AMPDU_MIN_SPACE: %#x\n",
 584                                  mac->min_space_cfg);
 585
 586                        rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE,
 587                                       mac->min_space_cfg);
 588                }
 589                break; }
 590        case HW_VAR_SHORTGI_DENSITY:{
 591                u8 density_to_set;
 592
 593                density_to_set = *((u8 *)val);
 594                mac->min_space_cfg |= (density_to_set << 3);
 595
 596                RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
 597                         "Set HW_VAR_SHORTGI_DENSITY: %#x\n",
 598                          mac->min_space_cfg);
 599
 600                rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE,
 601                               mac->min_space_cfg);
 602
 603                break; }
 604        case HW_VAR_AMPDU_FACTOR:{
 605                u32     ampdu_len =  (*((u8 *)val));
 606
 607                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
 608                        if (ampdu_len < VHT_AGG_SIZE_128K)
 609                                ampdu_len =
 610                                        (0x2000 << (*((u8 *)val))) - 1;
 611                        else
 612                                ampdu_len = 0x1ffff;
 613                } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
 614                        if (ampdu_len < HT_AGG_SIZE_64K)
 615                                ampdu_len =
 616                                        (0x2000 << (*((u8 *)val))) - 1;
 617                        else
 618                                ampdu_len = 0xffff;
 619                }
 620                ampdu_len |= BIT(31);
 621
 622                rtl_write_dword(rtlpriv,
 623                        REG_AMPDU_MAX_LENGTH_8812, ampdu_len);
 624                break; }
 625        case HW_VAR_AC_PARAM:{
 626                u8 e_aci = *((u8 *)val);
 627
 628                rtl8821ae_dm_init_edca_turbo(hw);
 629                if (rtlpci->acm_method != EACMWAY2_SW)
 630                        rtlpriv->cfg->ops->set_hw_reg(hw,
 631                                                      HW_VAR_ACM_CTRL,
 632                                                      (u8 *)(&e_aci));
 633                break; }
 634        case HW_VAR_ACM_CTRL:{
 635                u8 e_aci = *((u8 *)val);
 636                union aci_aifsn *p_aci_aifsn =
 637                    (union aci_aifsn *)(&mac->ac[0].aifs);
 638                u8 acm = p_aci_aifsn->f.acm;
 639                u8 acm_ctrl = rtl_read_byte(rtlpriv, REG_ACMHWCTRL);
 640
 641                acm_ctrl =
 642                    acm_ctrl | ((rtlpci->acm_method == 2) ? 0x0 : 0x1);
 643
 644                if (acm) {
 645                        switch (e_aci) {
 646                        case AC0_BE:
 647                                acm_ctrl |= ACMHW_BEQEN;
 648                                break;
 649                        case AC2_VI:
 650                                acm_ctrl |= ACMHW_VIQEN;
 651                                break;
 652                        case AC3_VO:
 653                                acm_ctrl |= ACMHW_VOQEN;
 654                                break;
 655                        default:
 656                                RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
 657                                         "HW_VAR_ACM_CTRL acm set failed: eACI is %d\n",
 658                                         acm);
 659                                break;
 660                        }
 661                } else {
 662                        switch (e_aci) {
 663                        case AC0_BE:
 664                                acm_ctrl &= (~ACMHW_BEQEN);
 665                                break;
 666                        case AC2_VI:
 667                                acm_ctrl &= (~ACMHW_VIQEN);
 668                                break;
 669                        case AC3_VO:
 670                                acm_ctrl &= (~ACMHW_VOQEN);
 671                                break;
 672                        default:
 673                                RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
 674                                         "switch case %#x not processed\n",
 675                                         e_aci);
 676                                break;
 677                        }
 678                }
 679
 680                RT_TRACE(rtlpriv, COMP_QOS, DBG_TRACE,
 681                         "SetHwReg8190pci(): [HW_VAR_ACM_CTRL] Write 0x%X\n",
 682                         acm_ctrl);
 683                rtl_write_byte(rtlpriv, REG_ACMHWCTRL, acm_ctrl);
 684                break; }
 685        case HW_VAR_RCR:
 686                rtl_write_dword(rtlpriv, REG_RCR, ((u32 *)(val))[0]);
 687                rtlpci->receive_config = ((u32 *)(val))[0];
 688                break;
 689        case HW_VAR_RETRY_LIMIT:{
 690                u8 retry_limit = ((u8 *)(val))[0];
 691
 692                rtl_write_word(rtlpriv, REG_RL,
 693                               retry_limit << RETRY_LIMIT_SHORT_SHIFT |
 694                               retry_limit << RETRY_LIMIT_LONG_SHIFT);
 695                break; }
 696        case HW_VAR_DUAL_TSF_RST:
 697                rtl_write_byte(rtlpriv, REG_DUAL_TSF_RST, (BIT(0) | BIT(1)));
 698                break;
 699        case HW_VAR_EFUSE_BYTES:
 700                rtlefuse->efuse_usedbytes = *((u16 *)val);
 701                break;
 702        case HW_VAR_EFUSE_USAGE:
 703                rtlefuse->efuse_usedpercentage = *((u8 *)val);
 704                break;
 705        case HW_VAR_IO_CMD:
 706                rtl8821ae_phy_set_io_cmd(hw, (*(enum io_type *)val));
 707                break;
 708        case HW_VAR_SET_RPWM:{
 709                u8 rpwm_val;
 710
 711                rpwm_val = rtl_read_byte(rtlpriv, REG_PCIE_HRPWM);
 712                udelay(1);
 713
 714                if (rpwm_val & BIT(7)) {
 715                        rtl_write_byte(rtlpriv, REG_PCIE_HRPWM,
 716                                       (*(u8 *)val));
 717                } else {
 718                        rtl_write_byte(rtlpriv, REG_PCIE_HRPWM,
 719                                       ((*(u8 *)val) | BIT(7)));
 720                }
 721
 722                break; }
 723        case HW_VAR_H2C_FW_PWRMODE:
 724                rtl8821ae_set_fw_pwrmode_cmd(hw, (*(u8 *)val));
 725                break;
 726        case HW_VAR_FW_PSMODE_STATUS:
 727                ppsc->fw_current_inpsmode = *((bool *)val);
 728                break;
 729        case HW_VAR_INIT_RTS_RATE:
 730                break;
 731        case HW_VAR_RESUME_CLK_ON:
 732                _rtl8821ae_set_fw_ps_rf_on(hw);
 733                break;
 734        case HW_VAR_FW_LPS_ACTION:{
 735                bool b_enter_fwlps = *((bool *)val);
 736
 737                if (b_enter_fwlps)
 738                        _rtl8821ae_fwlps_enter(hw);
 739                 else
 740                        _rtl8821ae_fwlps_leave(hw);
 741                 break; }
 742        case HW_VAR_H2C_FW_JOINBSSRPT:{
 743                u8 mstatus = (*(u8 *)val);
 744
 745                if (mstatus == RT_MEDIA_CONNECT) {
 746                        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AID,
 747                                                      NULL);
 748                        _rtl8821ae_download_rsvd_page(hw, false);
 749                }
 750                rtl8821ae_set_fw_media_status_rpt_cmd(hw, mstatus);
 751
 752                break; }
 753        case HW_VAR_H2C_FW_P2P_PS_OFFLOAD:
 754                rtl8821ae_set_p2p_ps_offload_cmd(hw, (*(u8 *)val));
 755                break;
 756        case HW_VAR_AID:{
 757                u16 u2btmp;
 758                u2btmp = rtl_read_word(rtlpriv, REG_BCN_PSR_RPT);
 759                u2btmp &= 0xC000;
 760                rtl_write_word(rtlpriv, REG_BCN_PSR_RPT, (u2btmp |
 761                               mac->assoc_id));
 762                break; }
 763        case HW_VAR_CORRECT_TSF:{
 764                u8 btype_ibss = ((u8 *)(val))[0];
 765
 766                if (btype_ibss)
 767                        _rtl8821ae_stop_tx_beacon(hw);
 768
 769                _rtl8821ae_set_bcn_ctrl_reg(hw, 0, BIT(3));
 770
 771                rtl_write_dword(rtlpriv, REG_TSFTR,
 772                                (u32)(mac->tsf & 0xffffffff));
 773                rtl_write_dword(rtlpriv, REG_TSFTR + 4,
 774                                (u32)((mac->tsf >> 32) & 0xffffffff));
 775
 776                _rtl8821ae_set_bcn_ctrl_reg(hw, BIT(3), 0);
 777
 778                if (btype_ibss)
 779                        _rtl8821ae_resume_tx_beacon(hw);
 780                break; }
 781        case HW_VAR_NAV_UPPER: {
 782                u32     us_nav_upper = *(u32 *)val;
 783
 784                if (us_nav_upper > HAL_92C_NAV_UPPER_UNIT * 0xFF) {
 785                        RT_TRACE(rtlpriv, COMP_INIT , DBG_WARNING,
 786                                 "The setting value (0x%08X us) of NAV_UPPER is larger than (%d * 0xFF)!!!\n",
 787                                 us_nav_upper, HAL_92C_NAV_UPPER_UNIT);
 788                        break;
 789                }
 790                rtl_write_byte(rtlpriv, REG_NAV_UPPER,
 791                               ((u8)((us_nav_upper +
 792                                HAL_92C_NAV_UPPER_UNIT - 1) /
 793                                HAL_92C_NAV_UPPER_UNIT)));
 794                break; }
 795        case HW_VAR_KEEP_ALIVE: {
 796                u8 array[2];
 797                array[0] = 0xff;
 798                array[1] = *((u8 *)val);
 799                rtl8821ae_fill_h2c_cmd(hw, H2C_8821AE_KEEP_ALIVE_CTRL, 2,
 800                                       array);
 801                break; }
 802        default:
 803                RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
 804                         "switch case %#x not processed\n", variable);
 805                break;
 806        }
 807}
 808
 809static bool _rtl8821ae_llt_write(struct ieee80211_hw *hw, u32 address, u32 data)
 810{
 811        struct rtl_priv *rtlpriv = rtl_priv(hw);
 812        bool status = true;
 813        long count = 0;
 814        u32 value = _LLT_INIT_ADDR(address) | _LLT_INIT_DATA(data) |
 815                    _LLT_OP(_LLT_WRITE_ACCESS);
 816
 817        rtl_write_dword(rtlpriv, REG_LLT_INIT, value);
 818
 819        do {
 820                value = rtl_read_dword(rtlpriv, REG_LLT_INIT);
 821                if (_LLT_NO_ACTIVE == _LLT_OP_VALUE(value))
 822                        break;
 823
 824                if (count > POLLING_LLT_THRESHOLD) {
 825                        pr_err("Failed to polling write LLT done at address %d!\n",
 826                               address);
 827                        status = false;
 828                        break;
 829                }
 830        } while (++count);
 831
 832        return status;
 833}
 834
 835static bool _rtl8821ae_llt_table_init(struct ieee80211_hw *hw)
 836{
 837        struct rtl_priv *rtlpriv = rtl_priv(hw);
 838        unsigned short i;
 839        u8 txpktbuf_bndy;
 840        u32 rqpn;
 841        u8 maxpage;
 842        bool status;
 843
 844        maxpage = 255;
 845        txpktbuf_bndy = 0xF7;
 846        rqpn = 0x80e60808;
 847
 848        rtl_write_byte(rtlpriv, REG_TRXFF_BNDY, txpktbuf_bndy);
 849        rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, MAX_RX_DMA_BUFFER_SIZE - 1);
 850
 851        rtl_write_byte(rtlpriv, REG_TDECTRL + 1, txpktbuf_bndy);
 852
 853        rtl_write_byte(rtlpriv, REG_TXPKTBUF_BCNQ_BDNY, txpktbuf_bndy);
 854        rtl_write_byte(rtlpriv, REG_TXPKTBUF_MGQ_BDNY, txpktbuf_bndy);
 855
 856        rtl_write_byte(rtlpriv, REG_PBP, 0x31);
 857        rtl_write_byte(rtlpriv, REG_RX_DRVINFO_SZ, 0x4);
 858
 859        for (i = 0; i < (txpktbuf_bndy - 1); i++) {
 860                status = _rtl8821ae_llt_write(hw, i, i + 1);
 861                if (!status)
 862                        return status;
 863        }
 864
 865        status = _rtl8821ae_llt_write(hw, (txpktbuf_bndy - 1), 0xFF);
 866        if (!status)
 867                return status;
 868
 869        for (i = txpktbuf_bndy; i < maxpage; i++) {
 870                status = _rtl8821ae_llt_write(hw, i, (i + 1));
 871                if (!status)
 872                        return status;
 873        }
 874
 875        status = _rtl8821ae_llt_write(hw, maxpage, txpktbuf_bndy);
 876        if (!status)
 877                return status;
 878
 879        rtl_write_dword(rtlpriv, REG_RQPN, rqpn);
 880
 881        rtl_write_byte(rtlpriv, REG_RQPN_NPQ, 0x00);
 882
 883        return true;
 884}
 885
 886static void _rtl8821ae_gen_refresh_led_state(struct ieee80211_hw *hw)
 887{
 888        struct rtl_priv *rtlpriv = rtl_priv(hw);
 889        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
 890        struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
 891        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 892
 893        if (rtlpriv->rtlhal.up_first_time)
 894                return;
 895
 896        if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS)
 897                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
 898                        rtl8812ae_sw_led_on(hw, pled0);
 899                else
 900                        rtl8821ae_sw_led_on(hw, pled0);
 901        else if (ppsc->rfoff_reason == RF_CHANGE_BY_INIT)
 902                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
 903                        rtl8812ae_sw_led_on(hw, pled0);
 904                else
 905                        rtl8821ae_sw_led_on(hw, pled0);
 906        else
 907                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
 908                        rtl8812ae_sw_led_off(hw, pled0);
 909                else
 910                        rtl8821ae_sw_led_off(hw, pled0);
 911}
 912
 913static bool _rtl8821ae_init_mac(struct ieee80211_hw *hw)
 914{
 915        struct rtl_priv *rtlpriv = rtl_priv(hw);
 916        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
 917        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
 918
 919        u8 bytetmp = 0;
 920        u16 wordtmp = 0;
 921        bool mac_func_enable = rtlhal->mac_func_enable;
 922
 923        rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x00);
 924
 925        /*Auto Power Down to CHIP-off State*/
 926        bytetmp = rtl_read_byte(rtlpriv, REG_APS_FSMCO + 1) & (~BIT(7));
 927        rtl_write_byte(rtlpriv, REG_APS_FSMCO + 1, bytetmp);
 928
 929        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
 930                /* HW Power on sequence*/
 931                if (!rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_ALL_MSK,
 932                                              PWR_FAB_ALL_MSK, PWR_INTF_PCI_MSK,
 933                                              RTL8812_NIC_ENABLE_FLOW)) {
 934                                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
 935                                         "init 8812 MAC Fail as power on failure\n");
 936                                return false;
 937                }
 938        } else {
 939                /* HW Power on sequence */
 940                if (!rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_A_MSK,
 941                                              PWR_FAB_ALL_MSK, PWR_INTF_PCI_MSK,
 942                                              RTL8821A_NIC_ENABLE_FLOW)){
 943                        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
 944                                "init 8821 MAC Fail as power on failure\n");
 945                        return false;
 946                }
 947        }
 948
 949        bytetmp = rtl_read_byte(rtlpriv, REG_APS_FSMCO) | BIT(4);
 950        rtl_write_byte(rtlpriv, REG_APS_FSMCO, bytetmp);
 951
 952        bytetmp = rtl_read_byte(rtlpriv, REG_CR);
 953        bytetmp = 0xff;
 954        rtl_write_byte(rtlpriv, REG_CR, bytetmp);
 955        mdelay(2);
 956
 957        bytetmp = 0xff;
 958        rtl_write_byte(rtlpriv, REG_HWSEQ_CTRL, bytetmp);
 959        mdelay(2);
 960
 961        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
 962                bytetmp = rtl_read_byte(rtlpriv, REG_SYS_CFG + 3);
 963                if (bytetmp & BIT(0)) {
 964                        bytetmp = rtl_read_byte(rtlpriv, 0x7c);
 965                        bytetmp |= BIT(6);
 966                        rtl_write_byte(rtlpriv, 0x7c, bytetmp);
 967                }
 968        }
 969
 970        bytetmp = rtl_read_byte(rtlpriv, REG_GPIO_MUXCFG + 1);
 971        bytetmp &= ~BIT(4);
 972        rtl_write_byte(rtlpriv, REG_GPIO_MUXCFG + 1, bytetmp);
 973
 974        rtl_write_word(rtlpriv, REG_CR, 0x2ff);
 975
 976        if (!mac_func_enable) {
 977                if (!_rtl8821ae_llt_table_init(hw))
 978                        return false;
 979        }
 980
 981        rtl_write_dword(rtlpriv, REG_HISR, 0xffffffff);
 982        rtl_write_dword(rtlpriv, REG_HISRE, 0xffffffff);
 983
 984        /* Enable FW Beamformer Interrupt */
 985        bytetmp = rtl_read_byte(rtlpriv, REG_FWIMR + 3);
 986        rtl_write_byte(rtlpriv, REG_FWIMR + 3, bytetmp | BIT(6));
 987
 988        wordtmp = rtl_read_word(rtlpriv, REG_TRXDMA_CTRL);
 989        wordtmp &= 0xf;
 990        wordtmp |= 0xF5B1;
 991        rtl_write_word(rtlpriv, REG_TRXDMA_CTRL, wordtmp);
 992
 993        rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 1, 0x1F);
 994        rtl_write_dword(rtlpriv, REG_RCR, rtlpci->receive_config);
 995        rtl_write_word(rtlpriv, REG_RXFLTMAP2, 0xFFFF);
 996        /*low address*/
 997        rtl_write_dword(rtlpriv, REG_BCNQ_DESA,
 998                        rtlpci->tx_ring[BEACON_QUEUE].dma & DMA_BIT_MASK(32));
 999        rtl_write_dword(rtlpriv, REG_MGQ_DESA,
1000                        rtlpci->tx_ring[MGNT_QUEUE].dma & DMA_BIT_MASK(32));
1001        rtl_write_dword(rtlpriv, REG_VOQ_DESA,
1002                        rtlpci->tx_ring[VO_QUEUE].dma & DMA_BIT_MASK(32));
1003        rtl_write_dword(rtlpriv, REG_VIQ_DESA,
1004                        rtlpci->tx_ring[VI_QUEUE].dma & DMA_BIT_MASK(32));
1005        rtl_write_dword(rtlpriv, REG_BEQ_DESA,
1006                        rtlpci->tx_ring[BE_QUEUE].dma & DMA_BIT_MASK(32));
1007        rtl_write_dword(rtlpriv, REG_BKQ_DESA,
1008                        rtlpci->tx_ring[BK_QUEUE].dma & DMA_BIT_MASK(32));
1009        rtl_write_dword(rtlpriv, REG_HQ_DESA,
1010                        rtlpci->tx_ring[HIGH_QUEUE].dma & DMA_BIT_MASK(32));
1011        rtl_write_dword(rtlpriv, REG_RX_DESA,
1012                        rtlpci->rx_ring[RX_MPDU_QUEUE].dma & DMA_BIT_MASK(32));
1013
1014        rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 3, 0x77);
1015
1016        rtl_write_dword(rtlpriv, REG_INT_MIG, 0);
1017
1018        rtl_write_dword(rtlpriv, REG_MCUTST_1, 0);
1019
1020        rtl_write_byte(rtlpriv, REG_SECONDARY_CCA_CTRL, 0x3);
1021        _rtl8821ae_gen_refresh_led_state(hw);
1022
1023        return true;
1024}
1025
1026static void _rtl8821ae_hw_configure(struct ieee80211_hw *hw)
1027{
1028        struct rtl_priv *rtlpriv = rtl_priv(hw);
1029        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1030        u32 reg_rrsr;
1031
1032        reg_rrsr = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
1033
1034        rtl_write_dword(rtlpriv, REG_RRSR, reg_rrsr);
1035        /* ARFB table 9 for 11ac 5G 2SS */
1036        rtl_write_dword(rtlpriv, REG_ARFR0 + 4, 0xfffff000);
1037        /* ARFB table 10 for 11ac 5G 1SS */
1038        rtl_write_dword(rtlpriv, REG_ARFR1 + 4, 0x003ff000);
1039        /* ARFB table 11 for 11ac 24G 1SS */
1040        rtl_write_dword(rtlpriv, REG_ARFR2, 0x00000015);
1041        rtl_write_dword(rtlpriv, REG_ARFR2 + 4, 0x003ff000);
1042        /* ARFB table 12 for 11ac 24G 1SS */
1043        rtl_write_dword(rtlpriv, REG_ARFR3, 0x00000015);
1044        rtl_write_dword(rtlpriv, REG_ARFR3 + 4, 0xffcff000);
1045        /* 0x420[7] = 0 , enable retry AMPDU in new AMPD not singal MPDU. */
1046        rtl_write_word(rtlpriv, REG_FWHW_TXQ_CTRL, 0x1F00);
1047        rtl_write_byte(rtlpriv, REG_AMPDU_MAX_TIME, 0x70);
1048
1049        /*Set retry limit*/
1050        rtl_write_word(rtlpriv, REG_RL, 0x0707);
1051
1052        /* Set Data / Response auto rate fallack retry count*/
1053        rtl_write_dword(rtlpriv, REG_DARFRC, 0x01000000);
1054        rtl_write_dword(rtlpriv, REG_DARFRC + 4, 0x07060504);
1055        rtl_write_dword(rtlpriv, REG_RARFRC, 0x01000000);
1056        rtl_write_dword(rtlpriv, REG_RARFRC + 4, 0x07060504);
1057
1058        rtlpci->reg_bcn_ctrl_val = 0x1d;
1059        rtl_write_byte(rtlpriv, REG_BCN_CTRL, rtlpci->reg_bcn_ctrl_val);
1060
1061        /* TBTT prohibit hold time. Suggested by designer TimChen. */
1062        rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0xff);
1063
1064        /* AGGR_BK_TIME Reg51A 0x16 */
1065        rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0040);
1066
1067        /*For Rx TP. Suggested by SD1 Richard. Added by tynli. 2010.04.12.*/
1068        rtl_write_dword(rtlpriv, REG_FAST_EDCA_CTRL, 0x03086666);
1069
1070        rtl_write_byte(rtlpriv, REG_HT_SINGLE_AMPDU, 0x80);
1071        rtl_write_byte(rtlpriv, REG_RX_PKT_LIMIT, 0x20);
1072        rtl_write_word(rtlpriv, REG_MAX_AGGR_NUM, 0x1F1F);
1073}
1074
1075static u16 _rtl8821ae_mdio_read(struct rtl_priv *rtlpriv, u8 addr)
1076{
1077        u16 ret = 0;
1078        u8 tmp = 0, count = 0;
1079
1080        rtl_write_byte(rtlpriv, REG_MDIO_CTL, addr | BIT(6));
1081        tmp = rtl_read_byte(rtlpriv, REG_MDIO_CTL) & BIT(6);
1082        count = 0;
1083        while (tmp && count < 20) {
1084                udelay(10);
1085                tmp = rtl_read_byte(rtlpriv, REG_MDIO_CTL) & BIT(6);
1086                count++;
1087        }
1088        if (0 == tmp)
1089                ret = rtl_read_word(rtlpriv, REG_MDIO_RDATA);
1090
1091        return ret;
1092}
1093
1094static void _rtl8821ae_mdio_write(struct rtl_priv *rtlpriv, u8 addr, u16 data)
1095{
1096        u8 tmp = 0, count = 0;
1097
1098        rtl_write_word(rtlpriv, REG_MDIO_WDATA, data);
1099        rtl_write_byte(rtlpriv, REG_MDIO_CTL, addr | BIT(5));
1100        tmp = rtl_read_byte(rtlpriv, REG_MDIO_CTL) & BIT(5);
1101        count = 0;
1102        while (tmp && count < 20) {
1103                udelay(10);
1104                tmp = rtl_read_byte(rtlpriv, REG_MDIO_CTL) & BIT(5);
1105                count++;
1106        }
1107}
1108
1109static u8 _rtl8821ae_dbi_read(struct rtl_priv *rtlpriv, u16 addr)
1110{
1111        u16 read_addr = addr & 0xfffc;
1112        u8 tmp = 0, count = 0, ret = 0;
1113
1114        rtl_write_word(rtlpriv, REG_DBI_ADDR, read_addr);
1115        rtl_write_byte(rtlpriv, REG_DBI_FLAG, 0x2);
1116        tmp = rtl_read_byte(rtlpriv, REG_DBI_FLAG);
1117        count = 0;
1118        while (tmp && count < 20) {
1119                udelay(10);
1120                tmp = rtl_read_byte(rtlpriv, REG_DBI_FLAG);
1121                count++;
1122        }
1123        if (0 == tmp) {
1124                read_addr = REG_DBI_RDATA + addr % 4;
1125                ret = rtl_read_word(rtlpriv, read_addr);
1126        }
1127        return ret;
1128}
1129
1130static void _rtl8821ae_dbi_write(struct rtl_priv *rtlpriv, u16 addr, u8 data)
1131{
1132        u8 tmp = 0, count = 0;
1133        u16 wrtie_addr, remainder = addr % 4;
1134
1135        wrtie_addr = REG_DBI_WDATA + remainder;
1136        rtl_write_byte(rtlpriv, wrtie_addr, data);
1137
1138        wrtie_addr = (addr & 0xfffc) | (BIT(0) << (remainder + 12));
1139        rtl_write_word(rtlpriv, REG_DBI_ADDR, wrtie_addr);
1140
1141        rtl_write_byte(rtlpriv, REG_DBI_FLAG, 0x1);
1142
1143        tmp = rtl_read_byte(rtlpriv, REG_DBI_FLAG);
1144        count = 0;
1145        while (tmp && count < 20) {
1146                udelay(10);
1147                tmp = rtl_read_byte(rtlpriv, REG_DBI_FLAG);
1148                count++;
1149        }
1150}
1151
1152static void _rtl8821ae_enable_aspm_back_door(struct ieee80211_hw *hw)
1153{
1154        struct rtl_priv *rtlpriv = rtl_priv(hw);
1155        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1156        u8 tmp;
1157
1158        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1159                if (_rtl8821ae_mdio_read(rtlpriv, 0x04) != 0x8544)
1160                        _rtl8821ae_mdio_write(rtlpriv, 0x04, 0x8544);
1161
1162                if (_rtl8821ae_mdio_read(rtlpriv, 0x0b) != 0x0070)
1163                        _rtl8821ae_mdio_write(rtlpriv, 0x0b, 0x0070);
1164        }
1165
1166        tmp = _rtl8821ae_dbi_read(rtlpriv, 0x70f);
1167        _rtl8821ae_dbi_write(rtlpriv, 0x70f, tmp | BIT(7));
1168
1169        tmp = _rtl8821ae_dbi_read(rtlpriv, 0x719);
1170        _rtl8821ae_dbi_write(rtlpriv, 0x719, tmp | BIT(3) | BIT(4));
1171
1172        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1173                tmp  = _rtl8821ae_dbi_read(rtlpriv, 0x718);
1174                _rtl8821ae_dbi_write(rtlpriv, 0x718, tmp|BIT(4));
1175        }
1176}
1177
1178void rtl8821ae_enable_hw_security_config(struct ieee80211_hw *hw)
1179{
1180        struct rtl_priv *rtlpriv = rtl_priv(hw);
1181        u8 sec_reg_value;
1182        u8 tmp;
1183
1184        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
1185                 "PairwiseEncAlgorithm = %d GroupEncAlgorithm = %d\n",
1186                  rtlpriv->sec.pairwise_enc_algorithm,
1187                  rtlpriv->sec.group_enc_algorithm);
1188
1189        if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
1190                RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1191                         "not open hw encryption\n");
1192                return;
1193        }
1194
1195        sec_reg_value = SCR_TXENCENABLE | SCR_RXDECENABLE;
1196
1197        if (rtlpriv->sec.use_defaultkey) {
1198                sec_reg_value |= SCR_TXUSEDK;
1199                sec_reg_value |= SCR_RXUSEDK;
1200        }
1201
1202        sec_reg_value |= (SCR_RXBCUSEDK | SCR_TXBCUSEDK);
1203
1204        tmp = rtl_read_byte(rtlpriv, REG_CR + 1);
1205        rtl_write_byte(rtlpriv, REG_CR + 1, tmp | BIT(1));
1206
1207        RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
1208                 "The SECR-value %x\n", sec_reg_value);
1209
1210        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_WPA_CONFIG, &sec_reg_value);
1211}
1212
1213/* Static MacID Mapping (cf. Used in MacIdDoStaticMapping) ---------- */
1214#define MAC_ID_STATIC_FOR_DEFAULT_PORT                          0
1215#define MAC_ID_STATIC_FOR_BROADCAST_MULTICAST           1
1216#define MAC_ID_STATIC_FOR_BT_CLIENT_START                               2
1217#define MAC_ID_STATIC_FOR_BT_CLIENT_END                         3
1218/* ----------------------------------------------------------- */
1219
1220static void rtl8821ae_macid_initialize_mediastatus(struct ieee80211_hw *hw)
1221{
1222        struct rtl_priv *rtlpriv = rtl_priv(hw);
1223        u8      media_rpt[4] = {RT_MEDIA_CONNECT, 1,
1224                MAC_ID_STATIC_FOR_BROADCAST_MULTICAST,
1225                MAC_ID_STATIC_FOR_BT_CLIENT_END};
1226
1227        rtlpriv->cfg->ops->set_hw_reg(hw,
1228                HW_VAR_H2C_FW_MEDIASTATUSRPT, media_rpt);
1229
1230        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1231                 "Initialize MacId media status: from %d to %d\n",
1232                 MAC_ID_STATIC_FOR_BROADCAST_MULTICAST,
1233                 MAC_ID_STATIC_FOR_BT_CLIENT_END);
1234}
1235
1236static bool _rtl8821ae_check_pcie_dma_hang(struct ieee80211_hw *hw)
1237{
1238        struct rtl_priv *rtlpriv = rtl_priv(hw);
1239        u8 tmp;
1240
1241        /* write reg 0x350 Bit[26]=1. Enable debug port. */
1242        tmp = rtl_read_byte(rtlpriv, REG_DBI_CTRL + 3);
1243        if (!(tmp & BIT(2))) {
1244                rtl_write_byte(rtlpriv, REG_DBI_CTRL + 3, (tmp | BIT(2)));
1245                mdelay(100);
1246        }
1247
1248        /* read reg 0x350 Bit[25] if 1 : RX hang */
1249        /* read reg 0x350 Bit[24] if 1 : TX hang */
1250        tmp = rtl_read_byte(rtlpriv, REG_DBI_CTRL + 3);
1251        if ((tmp & BIT(0)) || (tmp & BIT(1))) {
1252                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1253                         "CheckPcieDMAHang8821AE(): true! Reset PCIE DMA!\n");
1254                return true;
1255        } else {
1256                return false;
1257        }
1258}
1259
1260static bool _rtl8821ae_reset_pcie_interface_dma(struct ieee80211_hw *hw,
1261                                         bool mac_power_on,
1262                                         bool in_watchdog)
1263{
1264        struct rtl_priv *rtlpriv = rtl_priv(hw);
1265        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1266        u8 tmp;
1267        bool release_mac_rx_pause;
1268        u8 backup_pcie_dma_pause;
1269
1270        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "\n");
1271
1272        /* 1. Disable register write lock. 0x1c[1] = 0 */
1273        tmp = rtl_read_byte(rtlpriv, REG_RSV_CTRL);
1274        tmp &= ~(BIT(1));
1275        rtl_write_byte(rtlpriv, REG_RSV_CTRL, tmp);
1276        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1277                /* write 0xCC bit[2] = 1'b1 */
1278                tmp = rtl_read_byte(rtlpriv, REG_PMC_DBG_CTRL2);
1279                tmp |= BIT(2);
1280                rtl_write_byte(rtlpriv, REG_PMC_DBG_CTRL2, tmp);
1281        }
1282
1283        /* 2. Check and pause TRX DMA */
1284        /* write 0x284 bit[18] = 1'b1 */
1285        /* write 0x301 = 0xFF */
1286        tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
1287        if (tmp & BIT(2)) {
1288                /* Already pause before the function for another purpose. */
1289                release_mac_rx_pause = false;
1290        } else {
1291                rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL, (tmp | BIT(2)));
1292                release_mac_rx_pause = true;
1293        }
1294        backup_pcie_dma_pause = rtl_read_byte(rtlpriv, REG_PCIE_CTRL_REG + 1);
1295        if (backup_pcie_dma_pause != 0xFF)
1296                rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0xFF);
1297
1298        if (mac_power_on) {
1299                /* 3. reset TRX function */
1300                /* write 0x100 = 0x00 */
1301                rtl_write_byte(rtlpriv, REG_CR, 0);
1302        }
1303
1304        /* 4. Reset PCIe DMA. 0x3[0] = 0 */
1305        tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN + 1);
1306        tmp &= ~(BIT(0));
1307        rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN + 1, tmp);
1308
1309        /* 5. Enable PCIe DMA. 0x3[0] = 1 */
1310        tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN + 1);
1311        tmp |= BIT(0);
1312        rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN + 1, tmp);
1313
1314        if (mac_power_on) {
1315                /* 6. enable TRX function */
1316                /* write 0x100 = 0xFF */
1317                rtl_write_byte(rtlpriv, REG_CR, 0xFF);
1318
1319                /* We should init LLT & RQPN and
1320                 * prepare Tx/Rx descrptor address later
1321                 * because MAC function is reset.*/
1322        }
1323
1324        /* 7. Restore PCIe autoload down bit */
1325        /* 8812AE does not has the defination. */
1326        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1327                /* write 0xF8 bit[17] = 1'b1 */
1328                tmp = rtl_read_byte(rtlpriv, REG_MAC_PHY_CTRL_NORMAL + 2);
1329                tmp |= BIT(1);
1330                rtl_write_byte(rtlpriv, REG_MAC_PHY_CTRL_NORMAL + 2, tmp);
1331        }
1332
1333        /* In MAC power on state, BB and RF maybe in ON state,
1334         * if we release TRx DMA here.
1335         * it will cause packets to be started to Tx/Rx,
1336         * so we release Tx/Rx DMA later.*/
1337        if (!mac_power_on/* || in_watchdog*/) {
1338                /* 8. release TRX DMA */
1339                /* write 0x284 bit[18] = 1'b0 */
1340                /* write 0x301 = 0x00 */
1341                if (release_mac_rx_pause) {
1342                        tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
1343                        rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL,
1344                                       tmp & (~BIT(2)));
1345                }
1346                rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1,
1347                               backup_pcie_dma_pause);
1348        }
1349
1350        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1351                /* 9. lock system register */
1352                /* write 0xCC bit[2] = 1'b0 */
1353                tmp = rtl_read_byte(rtlpriv, REG_PMC_DBG_CTRL2);
1354                tmp &= ~(BIT(2));
1355                rtl_write_byte(rtlpriv, REG_PMC_DBG_CTRL2, tmp);
1356        }
1357        return true;
1358}
1359
1360static void _rtl8821ae_get_wakeup_reason(struct ieee80211_hw *hw)
1361{
1362        struct rtl_priv *rtlpriv = rtl_priv(hw);
1363        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1364        struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
1365        u8 fw_reason = 0;
1366        struct timeval ts;
1367
1368        fw_reason = rtl_read_byte(rtlpriv, REG_MCUTST_WOWLAN);
1369
1370        RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "WOL Read 0x1c7 = %02X\n",
1371                 fw_reason);
1372
1373        ppsc->wakeup_reason = 0;
1374
1375        do_gettimeofday(&ts);
1376        rtlhal->last_suspend_sec = ts.tv_sec;
1377
1378        switch (fw_reason) {
1379        case FW_WOW_V2_PTK_UPDATE_EVENT:
1380                ppsc->wakeup_reason = WOL_REASON_PTK_UPDATE;
1381                do_gettimeofday(&ts);
1382                ppsc->last_wakeup_time = ts.tv_sec*1000 + ts.tv_usec/1000;
1383                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1384                         "It's a WOL PTK Key update event!\n");
1385                break;
1386        case FW_WOW_V2_GTK_UPDATE_EVENT:
1387                ppsc->wakeup_reason = WOL_REASON_GTK_UPDATE;
1388                do_gettimeofday(&ts);
1389                ppsc->last_wakeup_time = ts.tv_sec*1000 + ts.tv_usec/1000;
1390                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1391                         "It's a WOL GTK Key update event!\n");
1392                break;
1393        case FW_WOW_V2_DISASSOC_EVENT:
1394                ppsc->wakeup_reason = WOL_REASON_DISASSOC;
1395                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1396                         "It's a disassociation event!\n");
1397                break;
1398        case FW_WOW_V2_DEAUTH_EVENT:
1399                ppsc->wakeup_reason = WOL_REASON_DEAUTH;
1400                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1401                         "It's a deauth event!\n");
1402                break;
1403        case FW_WOW_V2_FW_DISCONNECT_EVENT:
1404                ppsc->wakeup_reason = WOL_REASON_AP_LOST;
1405                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1406                         "It's a Fw disconnect decision (AP lost) event!\n");
1407        break;
1408        case FW_WOW_V2_MAGIC_PKT_EVENT:
1409                ppsc->wakeup_reason = WOL_REASON_MAGIC_PKT;
1410                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1411                         "It's a magic packet event!\n");
1412                break;
1413        case FW_WOW_V2_UNICAST_PKT_EVENT:
1414                ppsc->wakeup_reason = WOL_REASON_UNICAST_PKT;
1415                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1416                         "It's an unicast packet event!\n");
1417                break;
1418        case FW_WOW_V2_PATTERN_PKT_EVENT:
1419                ppsc->wakeup_reason = WOL_REASON_PATTERN_PKT;
1420                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1421                         "It's a pattern match event!\n");
1422                break;
1423        case FW_WOW_V2_RTD3_SSID_MATCH_EVENT:
1424                ppsc->wakeup_reason = WOL_REASON_RTD3_SSID_MATCH;
1425                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1426                         "It's an RTD3 Ssid match event!\n");
1427                break;
1428        case FW_WOW_V2_REALWOW_V2_WAKEUPPKT:
1429                ppsc->wakeup_reason = WOL_REASON_REALWOW_V2_WAKEUPPKT;
1430                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1431                         "It's an RealWoW wake packet event!\n");
1432                break;
1433        case FW_WOW_V2_REALWOW_V2_ACKLOST:
1434                ppsc->wakeup_reason = WOL_REASON_REALWOW_V2_ACKLOST;
1435                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1436                         "It's an RealWoW ack lost event!\n");
1437                break;
1438        default:
1439                RT_TRACE(rtlpriv, COMP_POWER, DBG_DMESG,
1440                         "WOL Read 0x1c7 = %02X, Unknown reason!\n",
1441                          fw_reason);
1442                break;
1443        }
1444}
1445
1446static void _rtl8821ae_init_trx_desc_hw_address(struct ieee80211_hw *hw)
1447{
1448        struct rtl_priv *rtlpriv = rtl_priv(hw);
1449        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1450
1451        /*low address*/
1452        rtl_write_dword(rtlpriv, REG_BCNQ_DESA,
1453                        rtlpci->tx_ring[BEACON_QUEUE].dma & DMA_BIT_MASK(32));
1454        rtl_write_dword(rtlpriv, REG_MGQ_DESA,
1455                        rtlpci->tx_ring[MGNT_QUEUE].dma & DMA_BIT_MASK(32));
1456        rtl_write_dword(rtlpriv, REG_VOQ_DESA,
1457                        rtlpci->tx_ring[VO_QUEUE].dma & DMA_BIT_MASK(32));
1458        rtl_write_dword(rtlpriv, REG_VIQ_DESA,
1459                        rtlpci->tx_ring[VI_QUEUE].dma & DMA_BIT_MASK(32));
1460        rtl_write_dword(rtlpriv, REG_BEQ_DESA,
1461                        rtlpci->tx_ring[BE_QUEUE].dma & DMA_BIT_MASK(32));
1462        rtl_write_dword(rtlpriv, REG_BKQ_DESA,
1463                        rtlpci->tx_ring[BK_QUEUE].dma & DMA_BIT_MASK(32));
1464        rtl_write_dword(rtlpriv, REG_HQ_DESA,
1465                        rtlpci->tx_ring[HIGH_QUEUE].dma & DMA_BIT_MASK(32));
1466        rtl_write_dword(rtlpriv, REG_RX_DESA,
1467                        rtlpci->rx_ring[RX_MPDU_QUEUE].dma & DMA_BIT_MASK(32));
1468}
1469
1470static bool _rtl8821ae_init_llt_table(struct ieee80211_hw *hw, u32 boundary)
1471{
1472        bool status = true;
1473        u32 i;
1474        u32 txpktbuf_bndy = boundary;
1475        u32 last_entry_of_txpktbuf = LAST_ENTRY_OF_TX_PKT_BUFFER;
1476
1477        for (i = 0 ; i < (txpktbuf_bndy - 1) ; i++) {
1478                status = _rtl8821ae_llt_write(hw, i , i + 1);
1479                if (!status)
1480                        return status;
1481        }
1482
1483        status = _rtl8821ae_llt_write(hw, (txpktbuf_bndy - 1), 0xFF);
1484        if (!status)
1485                return status;
1486
1487        for (i = txpktbuf_bndy ; i < last_entry_of_txpktbuf ; i++) {
1488                status = _rtl8821ae_llt_write(hw, i, (i + 1));
1489                if (!status)
1490                        return status;
1491        }
1492
1493        status = _rtl8821ae_llt_write(hw, last_entry_of_txpktbuf,
1494                                      txpktbuf_bndy);
1495        if (!status)
1496                return status;
1497
1498        return status;
1499}
1500
1501static bool _rtl8821ae_dynamic_rqpn(struct ieee80211_hw *hw, u32 boundary,
1502                             u16 npq_rqpn_value, u32 rqpn_val)
1503{
1504        struct rtl_priv *rtlpriv = rtl_priv(hw);
1505        u8 tmp;
1506        bool ret = true;
1507        u16 count = 0, tmp16;
1508        bool support_remote_wakeup;
1509
1510        rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
1511                                      (u8 *)(&support_remote_wakeup));
1512
1513        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1514                 "boundary=%#X, NPQ_RQPNValue=%#X, RQPNValue=%#X\n",
1515                  boundary, npq_rqpn_value, rqpn_val);
1516
1517        /* stop PCIe DMA
1518         * 1. 0x301[7:0] = 0xFE */
1519        rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0xFE);
1520
1521        /* wait TXFF empty
1522         * 2. polling till 0x41A[15:0]=0x07FF */
1523        tmp16 = rtl_read_word(rtlpriv, REG_TXPKT_EMPTY);
1524        while ((tmp16 & 0x07FF) != 0x07FF) {
1525                udelay(100);
1526                tmp16 = rtl_read_word(rtlpriv, REG_TXPKT_EMPTY);
1527                count++;
1528                if ((count % 200) == 0) {
1529                        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1530                                 "Tx queue is not empty for 20ms!\n");
1531                }
1532                if (count >= 1000) {
1533                        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1534                                 "Wait for Tx FIFO empty timeout!\n");
1535                        break;
1536                }
1537        }
1538
1539        /* TX pause
1540         * 3. reg 0x522=0xFF */
1541        rtl_write_byte(rtlpriv, REG_TXPAUSE, 0xFF);
1542
1543        /* Wait TX State Machine OK
1544         * 4. polling till reg 0x5FB~0x5F8 = 0x00000000 for 50ms */
1545        count = 0;
1546        while (rtl_read_byte(rtlpriv, REG_SCH_TXCMD) != 0) {
1547                udelay(100);
1548                count++;
1549                if (count >= 500) {
1550                        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1551                                 "Wait for TX State Machine ready timeout !!\n");
1552                        break;
1553                }
1554        }
1555
1556        /* stop RX DMA path
1557         * 5.   0x284[18] = 1
1558         * 6.   wait till 0x284[17] == 1
1559         * wait RX DMA idle */
1560        count = 0;
1561        tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
1562        rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL, (tmp | BIT(2)));
1563        do {
1564                tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
1565                udelay(10);
1566                count++;
1567        } while (!(tmp & BIT(1)) && count < 100);
1568
1569        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1570                 "Wait until Rx DMA Idle. count=%d REG[0x286]=0x%x\n",
1571                  count, tmp);
1572
1573        /* reset BB
1574         * 7.   0x02 [0] = 0 */
1575        tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN);
1576        tmp &= ~(BIT(0));
1577        rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, tmp);
1578
1579        /* Reset TRX MAC
1580         * 8.    0x100 = 0x00
1581         * Delay (1ms) */
1582        rtl_write_byte(rtlpriv, REG_CR, 0x00);
1583        udelay(1000);
1584
1585        /* Disable MAC Security Engine
1586         * 9.   0x100 bit[9]=0 */
1587        tmp = rtl_read_byte(rtlpriv, REG_CR + 1);
1588        tmp &= ~(BIT(1));
1589        rtl_write_byte(rtlpriv, REG_CR + 1, tmp);
1590
1591        /* To avoid DD-Tim Circuit hang
1592         * 10.  0x553 bit[5]=1 */
1593        tmp = rtl_read_byte(rtlpriv, REG_DUAL_TSF_RST);
1594        rtl_write_byte(rtlpriv, REG_DUAL_TSF_RST, (tmp | BIT(5)));
1595
1596        /* Enable MAC Security Engine
1597         * 11.  0x100 bit[9]=1 */
1598        tmp = rtl_read_byte(rtlpriv, REG_CR + 1);
1599        rtl_write_byte(rtlpriv, REG_CR + 1, (tmp | BIT(1)));
1600
1601        /* Enable TRX MAC
1602         * 12.   0x100 = 0xFF
1603         *      Delay (1ms) */
1604        rtl_write_byte(rtlpriv, REG_CR, 0xFF);
1605        udelay(1000);
1606
1607        /* Enable BB
1608         * 13.  0x02 [0] = 1 */
1609        tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN);
1610        rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN, (tmp | BIT(0)));
1611
1612        /* beacon setting
1613         * 14,15. set beacon head page (reg 0x209 and 0x424) */
1614        rtl_write_byte(rtlpriv, REG_TDECTRL + 1, (u8)boundary);
1615        rtl_write_byte(rtlpriv, REG_TXPKTBUF_BCNQ_BDNY, (u8)boundary);
1616        rtl_write_byte(rtlpriv, REG_TXPKTBUF_MGQ_BDNY, (u8)boundary);
1617
1618        /* 16.  WMAC_LBK_BF_HD 0x45D[7:0]
1619         * WMAC_LBK_BF_HD */
1620        rtl_write_byte(rtlpriv, REG_TXPKTBUF_WMAC_LBK_BF_HD,
1621                       (u8)boundary);
1622
1623        rtl_write_word(rtlpriv, REG_TRXFF_BNDY, boundary);
1624
1625        /* init LLT
1626         * 17. init LLT */
1627        if (!_rtl8821ae_init_llt_table(hw, boundary)) {
1628                RT_TRACE(rtlpriv, COMP_INIT, DBG_WARNING,
1629                         "Failed to init LLT table!\n");
1630                return false;
1631        }
1632
1633        /* reallocate RQPN
1634         * 18. reallocate RQPN and init LLT */
1635        rtl_write_word(rtlpriv, REG_RQPN_NPQ, npq_rqpn_value);
1636        rtl_write_dword(rtlpriv, REG_RQPN, rqpn_val);
1637
1638        /* release Tx pause
1639         * 19. 0x522=0x00 */
1640        rtl_write_byte(rtlpriv, REG_TXPAUSE, 0x00);
1641
1642        /* enable PCIE DMA
1643         * 20. 0x301[7:0] = 0x00
1644         * 21. 0x284[18] = 0 */
1645        rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0x00);
1646        tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
1647        rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL, (tmp&~BIT(2)));
1648
1649        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "End.\n");
1650        return ret;
1651}
1652
1653static void _rtl8821ae_simple_initialize_adapter(struct ieee80211_hw *hw)
1654{
1655        struct rtl_priv *rtlpriv = rtl_priv(hw);
1656        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1657        struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
1658
1659#if (USE_SPECIFIC_FW_TO_SUPPORT_WOWLAN == 1)
1660        /* Re-download normal Fw. */
1661        rtl8821ae_set_fw_related_for_wowlan(hw, false);
1662#endif
1663
1664        /* Re-Initialize LLT table. */
1665        if (rtlhal->re_init_llt_table) {
1666                u32 rqpn = 0x80e70808;
1667                u8 rqpn_npq = 0, boundary = 0xF8;
1668                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
1669                        rqpn = 0x80e90808;
1670                        boundary = 0xFA;
1671                }
1672                if (_rtl8821ae_dynamic_rqpn(hw, boundary, rqpn_npq, rqpn))
1673                        rtlhal->re_init_llt_table = false;
1674        }
1675
1676        ppsc->rfpwr_state = ERFON;
1677}
1678
1679static void _rtl8821ae_enable_l1off(struct ieee80211_hw *hw)
1680{
1681        u8 tmp  = 0;
1682        struct rtl_priv *rtlpriv = rtl_priv(hw);
1683
1684        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "--->\n");
1685
1686        tmp = _rtl8821ae_dbi_read(rtlpriv, 0x160);
1687        if (!(tmp & (BIT(2) | BIT(3)))) {
1688                RT_TRACE(rtlpriv, COMP_POWER | COMP_INIT, DBG_LOUD,
1689                         "0x160(%#x)return!!\n", tmp);
1690                return;
1691        }
1692
1693        tmp = _rtl8821ae_mdio_read(rtlpriv, 0x1b);
1694        _rtl8821ae_mdio_write(rtlpriv, 0x1b, (tmp | BIT(4)));
1695
1696        tmp = _rtl8821ae_dbi_read(rtlpriv, 0x718);
1697        _rtl8821ae_dbi_write(rtlpriv, 0x718, tmp | BIT(5));
1698
1699        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "<---\n");
1700}
1701
1702static void _rtl8821ae_enable_ltr(struct ieee80211_hw *hw)
1703{
1704        u8 tmp  = 0;
1705        struct rtl_priv *rtlpriv = rtl_priv(hw);
1706
1707        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "--->\n");
1708
1709        /* Check 0x98[10] */
1710        tmp = _rtl8821ae_dbi_read(rtlpriv, 0x99);
1711        if (!(tmp & BIT(2))) {
1712                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1713                         "<---0x99(%#x) return!!\n", tmp);
1714                return;
1715        }
1716
1717        /* LTR idle latency, 0x90 for 144us */
1718        rtl_write_dword(rtlpriv, 0x798, 0x88908890);
1719
1720        /* LTR active latency, 0x3c for 60us */
1721        rtl_write_dword(rtlpriv, 0x79c, 0x883c883c);
1722
1723        tmp = rtl_read_byte(rtlpriv, 0x7a4);
1724        rtl_write_byte(rtlpriv, 0x7a4, (tmp | BIT(4)));
1725
1726        tmp = rtl_read_byte(rtlpriv, 0x7a4);
1727        rtl_write_byte(rtlpriv, 0x7a4, (tmp & (~BIT(0))));
1728        rtl_write_byte(rtlpriv, 0x7a4, (tmp | BIT(0)));
1729
1730        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "<---\n");
1731}
1732
1733static bool _rtl8821ae_wowlan_initialize_adapter(struct ieee80211_hw *hw)
1734{
1735        struct rtl_priv *rtlpriv = rtl_priv(hw);
1736        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
1737        bool init_finished = true;
1738        u8 tmp = 0;
1739
1740        /* Get Fw wake up reason. */
1741        _rtl8821ae_get_wakeup_reason(hw);
1742
1743        /* Patch Pcie Rx DMA hang after S3/S4 several times.
1744         * The root cause has not be found. */
1745        if (_rtl8821ae_check_pcie_dma_hang(hw))
1746                _rtl8821ae_reset_pcie_interface_dma(hw, true, false);
1747
1748        /* Prepare Tx/Rx Desc Hw address. */
1749        _rtl8821ae_init_trx_desc_hw_address(hw);
1750
1751        /* Release Pcie Interface Rx DMA to allow wake packet DMA. */
1752        rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0xFE);
1753        RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "Enable PCIE Rx DMA.\n");
1754
1755        /* Check wake up event.
1756         * We should check wake packet bit before disable wowlan by H2C or
1757         * Fw will clear the bit. */
1758        tmp = rtl_read_byte(rtlpriv, REG_FTISR + 3);
1759        RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
1760                 "Read REG_FTISR 0x13f = %#X\n", tmp);
1761
1762        /* Set the WoWLAN related function control disable. */
1763        rtl8821ae_set_fw_wowlan_mode(hw, false);
1764        rtl8821ae_set_fw_remote_wake_ctrl_cmd(hw, 0);
1765
1766        if (rtlhal->hw_rof_enable) {
1767                tmp = rtl_read_byte(rtlpriv, REG_HSISR + 3);
1768                if (tmp & BIT(1)) {
1769                        /* Clear GPIO9 ISR */
1770                        rtl_write_byte(rtlpriv, REG_HSISR + 3, tmp | BIT(1));
1771                        init_finished = false;
1772                } else {
1773                        init_finished = true;
1774                }
1775        }
1776
1777        if (init_finished) {
1778                _rtl8821ae_simple_initialize_adapter(hw);
1779
1780                /* Release Pcie Interface Tx DMA. */
1781                rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0x00);
1782                /* Release Pcie RX DMA */
1783                rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL, 0x02);
1784
1785                tmp = rtl_read_byte(rtlpriv, REG_CR + 1);
1786                rtl_write_byte(rtlpriv, REG_CR + 1, (tmp & (~BIT(0))));
1787
1788                _rtl8821ae_enable_l1off(hw);
1789                _rtl8821ae_enable_ltr(hw);
1790        }
1791
1792        return init_finished;
1793}
1794
1795static void _rtl8812ae_bb8812_config_1t(struct ieee80211_hw *hw)
1796{
1797        /* BB OFDM RX Path_A */
1798        rtl_set_bbreg(hw, 0x808, 0xff, 0x11);
1799        /* BB OFDM TX Path_A */
1800        rtl_set_bbreg(hw, 0x80c, MASKLWORD, 0x1111);
1801        /* BB CCK R/Rx Path_A */
1802        rtl_set_bbreg(hw, 0xa04, 0x0c000000, 0x0);
1803        /* MCS support */
1804        rtl_set_bbreg(hw, 0x8bc, 0xc0000060, 0x4);
1805        /* RF Path_B HSSI OFF */
1806        rtl_set_bbreg(hw, 0xe00, 0xf, 0x4);
1807        /* RF Path_B Power Down */
1808        rtl_set_bbreg(hw, 0xe90, MASKDWORD, 0);
1809        /* ADDA Path_B OFF */
1810        rtl_set_bbreg(hw, 0xe60, MASKDWORD, 0);
1811        rtl_set_bbreg(hw, 0xe64, MASKDWORD, 0);
1812}
1813
1814static void _rtl8821ae_poweroff_adapter(struct ieee80211_hw *hw)
1815{
1816        struct rtl_priv *rtlpriv = rtl_priv(hw);
1817        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1818        u8 u1b_tmp;
1819
1820        rtlhal->mac_func_enable = false;
1821
1822        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1823                /* Combo (PCIe + USB) Card and PCIe-MF Card */
1824                /* 1. Run LPS WL RFOFF flow */
1825                /* RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1826                "=====>CardDisableRTL8812E,RTL8821A_NIC_LPS_ENTER_FLOW\n");
1827                */
1828                rtl_hal_pwrseqcmdparsing(rtlpriv,
1829                        PWR_CUT_ALL_MSK, PWR_FAB_ALL_MSK,
1830                        PWR_INTF_PCI_MSK, RTL8821A_NIC_LPS_ENTER_FLOW);
1831        }
1832        /* 2. 0x1F[7:0] = 0 */
1833        /* turn off RF */
1834        /* rtl_write_byte(rtlpriv, REG_RF_CTRL, 0x00); */
1835        if ((rtl_read_byte(rtlpriv, REG_MCUFWDL) & BIT(7)) &&
1836                rtlhal->fw_ready) {
1837                rtl8821ae_firmware_selfreset(hw);
1838        }
1839
1840        /* Reset MCU. Suggested by Filen. */
1841        u1b_tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN+1);
1842        rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN+1, (u1b_tmp & (~BIT(2))));
1843
1844        /* g.   MCUFWDL 0x80[1:0]=0      */
1845        /* reset MCU ready status */
1846        rtl_write_byte(rtlpriv, REG_MCUFWDL, 0x00);
1847
1848        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
1849                /* HW card disable configuration. */
1850                rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_ALL_MSK, PWR_FAB_ALL_MSK,
1851                        PWR_INTF_PCI_MSK, RTL8821A_NIC_DISABLE_FLOW);
1852        } else {
1853                /* HW card disable configuration. */
1854                rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_ALL_MSK, PWR_FAB_ALL_MSK,
1855                        PWR_INTF_PCI_MSK, RTL8812_NIC_DISABLE_FLOW);
1856        }
1857
1858        /* Reset MCU IO Wrapper */
1859        u1b_tmp = rtl_read_byte(rtlpriv, REG_RSV_CTRL + 1);
1860        rtl_write_byte(rtlpriv, REG_RSV_CTRL + 1, (u1b_tmp & (~BIT(0))));
1861        u1b_tmp = rtl_read_byte(rtlpriv, REG_RSV_CTRL + 1);
1862        rtl_write_byte(rtlpriv, REG_RSV_CTRL + 1, u1b_tmp | BIT(0));
1863
1864        /* 7. RSV_CTRL 0x1C[7:0] = 0x0E */
1865        /* lock ISO/CLK/Power control register */
1866        rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x0e);
1867}
1868
1869int rtl8821ae_hw_init(struct ieee80211_hw *hw)
1870{
1871        struct rtl_priv *rtlpriv = rtl_priv(hw);
1872        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1873        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1874        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1875        bool rtstatus = true;
1876        int err;
1877        u8 tmp_u1b;
1878        bool support_remote_wakeup;
1879        u32 nav_upper = WIFI_NAV_UPPER_US;
1880
1881        rtlhal->being_init_adapter = true;
1882        rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
1883                                      (u8 *)(&support_remote_wakeup));
1884        rtlpriv->intf_ops->disable_aspm(hw);
1885
1886        /*YP wowlan not considered*/
1887
1888        tmp_u1b = rtl_read_byte(rtlpriv, REG_CR);
1889        if (tmp_u1b != 0 && tmp_u1b != 0xEA) {
1890                rtlhal->mac_func_enable = true;
1891                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
1892                         "MAC has already power on.\n");
1893        } else {
1894                rtlhal->mac_func_enable = false;
1895                rtlhal->fw_ps_state = FW_PS_STATE_ALL_ON_8821AE;
1896        }
1897
1898        if (support_remote_wakeup &&
1899                rtlhal->wake_from_pnp_sleep &&
1900                rtlhal->mac_func_enable) {
1901                if (_rtl8821ae_wowlan_initialize_adapter(hw)) {
1902                        rtlhal->being_init_adapter = false;
1903                        return 0;
1904                }
1905        }
1906
1907        if (_rtl8821ae_check_pcie_dma_hang(hw)) {
1908                _rtl8821ae_reset_pcie_interface_dma(hw,
1909                                                    rtlhal->mac_func_enable,
1910                                                    false);
1911                rtlhal->mac_func_enable = false;
1912        }
1913
1914        /* Reset MAC/BB/RF status if it is not powered off
1915         * before calling initialize Hw flow to prevent
1916         * from interface and MAC status mismatch.
1917         * 2013.06.21, by tynli. Suggested by SD1 JackieLau. */
1918        if (rtlhal->mac_func_enable) {
1919                _rtl8821ae_poweroff_adapter(hw);
1920                rtlhal->mac_func_enable = false;
1921        }
1922
1923        rtstatus = _rtl8821ae_init_mac(hw);
1924        if (rtstatus != true) {
1925                pr_err("Init MAC failed\n");
1926                err = 1;
1927                return err;
1928        }
1929
1930        tmp_u1b = rtl_read_byte(rtlpriv, REG_SYS_CFG);
1931        tmp_u1b &= 0x7F;
1932        rtl_write_byte(rtlpriv, REG_SYS_CFG, tmp_u1b);
1933
1934        err = rtl8821ae_download_fw(hw, false);
1935        if (err) {
1936                RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1937                         "Failed to download FW. Init HW without FW now\n");
1938                err = 1;
1939                rtlhal->fw_ready = false;
1940                return err;
1941        } else {
1942                rtlhal->fw_ready = true;
1943        }
1944        ppsc->fw_current_inpsmode = false;
1945        rtlhal->fw_ps_state = FW_PS_STATE_ALL_ON_8821AE;
1946        rtlhal->fw_clk_change_in_progress = false;
1947        rtlhal->allow_sw_to_change_hwclc = false;
1948        rtlhal->last_hmeboxnum = 0;
1949
1950        /*SIC_Init(Adapter);
1951        if(rtlhal->AMPDUBurstMode)
1952                rtl_write_byte(rtlpriv,REG_AMPDU_BURST_MODE_8812,  0x7F);*/
1953
1954        rtl8821ae_phy_mac_config(hw);
1955        /* because last function modify RCR, so we update
1956         * rcr var here, or TP will unstable for receive_config
1957         * is wrong, RX RCR_ACRC32 will cause TP unstabel & Rx
1958         * RCR_APP_ICV will cause mac80211 unassoc for cisco 1252
1959        rtlpci->receive_config = rtl_read_dword(rtlpriv, REG_RCR);
1960        rtlpci->receive_config &= ~(RCR_ACRC32 | RCR_AICV);
1961        rtl_write_dword(rtlpriv, REG_RCR, rtlpci->receive_config);*/
1962        rtl8821ae_phy_bb_config(hw);
1963
1964        rtl8821ae_phy_rf_config(hw);
1965
1966        if (rtlpriv->phy.rf_type == RF_1T1R &&
1967                rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
1968                _rtl8812ae_bb8812_config_1t(hw);
1969
1970        _rtl8821ae_hw_configure(hw);
1971
1972        rtl8821ae_phy_switch_wirelessband(hw, BAND_ON_2_4G);
1973
1974        /*set wireless mode*/
1975
1976        rtlhal->mac_func_enable = true;
1977
1978        rtl_cam_reset_all_entry(hw);
1979
1980        rtl8821ae_enable_hw_security_config(hw);
1981
1982        ppsc->rfpwr_state = ERFON;
1983
1984        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
1985        _rtl8821ae_enable_aspm_back_door(hw);
1986        rtlpriv->intf_ops->enable_aspm(hw);
1987
1988        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE &&
1989            (rtlhal->rfe_type == 1 || rtlhal->rfe_type == 5))
1990                rtl_set_bbreg(hw, 0x900, 0x00000303, 0x0302);
1991
1992        rtl8821ae_bt_hw_init(hw);
1993        rtlpriv->rtlhal.being_init_adapter = false;
1994
1995        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_NAV_UPPER, (u8 *)&nav_upper);
1996
1997        /* rtl8821ae_dm_check_txpower_tracking(hw); */
1998        /* rtl8821ae_phy_lc_calibrate(hw); */
1999        if (support_remote_wakeup)
2000                rtl_write_byte(rtlpriv, REG_WOW_CTRL, 0);
2001
2002        /* Release Rx DMA*/
2003        tmp_u1b = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
2004        if (tmp_u1b & BIT(2)) {
2005                /* Release Rx DMA if needed*/
2006                tmp_u1b &= ~BIT(2);
2007                rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL, tmp_u1b);
2008        }
2009
2010        /* Release Tx/Rx PCIE DMA if*/
2011        rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0);
2012
2013        rtl8821ae_dm_init(hw);
2014        rtl8821ae_macid_initialize_mediastatus(hw);
2015
2016        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "rtl8821ae_hw_init() <====\n");
2017        return err;
2018}
2019
2020static enum version_8821ae _rtl8821ae_read_chip_version(struct ieee80211_hw *hw)
2021{
2022        struct rtl_priv *rtlpriv = rtl_priv(hw);
2023        struct rtl_phy *rtlphy = &rtlpriv->phy;
2024        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
2025        enum version_8821ae version = VERSION_UNKNOWN;
2026        u32 value32;
2027
2028        value32 = rtl_read_dword(rtlpriv, REG_SYS_CFG);
2029        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2030                 "ReadChipVersion8812A 0xF0 = 0x%x\n", value32);
2031
2032        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
2033                rtlphy->rf_type = RF_2T2R;
2034        else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE)
2035                rtlphy->rf_type = RF_1T1R;
2036
2037        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2038                 "RF_Type is %x!!\n", rtlphy->rf_type);
2039
2040        if (value32 & TRP_VAUX_EN) {
2041                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
2042                        if (rtlphy->rf_type == RF_2T2R)
2043                                version = VERSION_TEST_CHIP_2T2R_8812;
2044                        else
2045                                version = VERSION_TEST_CHIP_1T1R_8812;
2046                } else
2047                        version = VERSION_TEST_CHIP_8821;
2048        } else {
2049                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
2050                        u32 rtl_id = ((value32 & CHIP_VER_RTL_MASK) >> 12) + 1;
2051
2052                        if (rtlphy->rf_type == RF_2T2R)
2053                                version =
2054                                        (enum version_8821ae)(CHIP_8812
2055                                        | NORMAL_CHIP |
2056                                        RF_TYPE_2T2R);
2057                        else
2058                                version = (enum version_8821ae)(CHIP_8812
2059                                        | NORMAL_CHIP);
2060
2061                        version = (enum version_8821ae)(version | (rtl_id << 12));
2062                } else if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
2063                        u32 rtl_id = value32 & CHIP_VER_RTL_MASK;
2064
2065                        version = (enum version_8821ae)(CHIP_8821
2066                                | NORMAL_CHIP | rtl_id);
2067                }
2068        }
2069
2070        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8821AE) {
2071                /*WL_HWROF_EN.*/
2072                value32 = rtl_read_dword(rtlpriv, REG_MULTI_FUNC_CTRL);
2073                rtlhal->hw_rof_enable = ((value32 & WL_HWROF_EN) ? 1 : 0);
2074        }
2075
2076        switch (version) {
2077        case VERSION_TEST_CHIP_1T1R_8812:
2078                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2079                         "Chip Version ID: VERSION_TEST_CHIP_1T1R_8812\n");
2080                break;
2081        case VERSION_TEST_CHIP_2T2R_8812:
2082                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2083                         "Chip Version ID: VERSION_TEST_CHIP_2T2R_8812\n");
2084                break;
2085        case VERSION_NORMAL_TSMC_CHIP_1T1R_8812:
2086                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2087                         "Chip Version ID:VERSION_NORMAL_TSMC_CHIP_1T1R_8812\n");
2088                break;
2089        case VERSION_NORMAL_TSMC_CHIP_2T2R_8812:
2090                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2091                         "Chip Version ID: VERSION_NORMAL_TSMC_CHIP_2T2R_8812\n");
2092                break;
2093        case VERSION_NORMAL_TSMC_CHIP_1T1R_8812_C_CUT:
2094                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2095                         "Chip Version ID: VERSION_NORMAL_TSMC_CHIP_1T1R_8812 C CUT\n");
2096                break;
2097        case VERSION_NORMAL_TSMC_CHIP_2T2R_8812_C_CUT:
2098                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2099                         "Chip Version ID: VERSION_NORMAL_TSMC_CHIP_2T2R_8812 C CUT\n");
2100                break;
2101        case VERSION_TEST_CHIP_8821:
2102                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2103                         "Chip Version ID: VERSION_TEST_CHIP_8821\n");
2104                break;
2105        case VERSION_NORMAL_TSMC_CHIP_8821:
2106                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2107                         "Chip Version ID: VERSION_NORMAL_TSMC_CHIP_8821 A CUT\n");
2108                break;
2109        case VERSION_NORMAL_TSMC_CHIP_8821_B_CUT:
2110                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2111                         "Chip Version ID: VERSION_NORMAL_TSMC_CHIP_8821 B CUT\n");
2112                break;
2113        default:
2114                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2115                         "Chip Version ID: Unknown (0x%X)\n", version);
2116                break;
2117        }
2118
2119        return version;
2120}
2121
2122static int _rtl8821ae_set_media_status(struct ieee80211_hw *hw,
2123                                     enum nl80211_iftype type)
2124{
2125        struct rtl_priv *rtlpriv = rtl_priv(hw);
2126        u8 bt_msr = rtl_read_byte(rtlpriv, MSR);
2127        enum led_ctl_mode ledaction = LED_CTL_NO_LINK;
2128        bt_msr &= 0xfc;
2129
2130        rtl_write_dword(rtlpriv, REG_BCN_CTRL, 0);
2131        RT_TRACE(rtlpriv, COMP_BEACON, DBG_LOUD,
2132                "clear 0x550 when set HW_VAR_MEDIA_STATUS\n");
2133
2134        if (type == NL80211_IFTYPE_UNSPECIFIED ||
2135            type == NL80211_IFTYPE_STATION) {
2136                _rtl8821ae_stop_tx_beacon(hw);
2137                _rtl8821ae_enable_bcn_sub_func(hw);
2138        } else if (type == NL80211_IFTYPE_ADHOC ||
2139                type == NL80211_IFTYPE_AP) {
2140                _rtl8821ae_resume_tx_beacon(hw);
2141                _rtl8821ae_disable_bcn_sub_func(hw);
2142        } else {
2143                RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
2144                         "Set HW_VAR_MEDIA_STATUS: No such media status(%x).\n",
2145                         type);
2146        }
2147
2148        switch (type) {
2149        case NL80211_IFTYPE_UNSPECIFIED:
2150                bt_msr |= MSR_NOLINK;
2151                ledaction = LED_CTL_LINK;
2152                RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
2153                         "Set Network type to NO LINK!\n");
2154                break;
2155        case NL80211_IFTYPE_ADHOC:
2156                bt_msr |= MSR_ADHOC;
2157                RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
2158                         "Set Network type to Ad Hoc!\n");
2159                break;
2160        case NL80211_IFTYPE_STATION:
2161                bt_msr |= MSR_INFRA;
2162                ledaction = LED_CTL_LINK;
2163                RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
2164                         "Set Network type to STA!\n");
2165                break;
2166        case NL80211_IFTYPE_AP:
2167                bt_msr |= MSR_AP;
2168                RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
2169                         "Set Network type to AP!\n");
2170                break;
2171        default:
2172                pr_err("Network type %d not support!\n", type);
2173                return 1;
2174        }
2175
2176        rtl_write_byte(rtlpriv, MSR, bt_msr);
2177        rtlpriv->cfg->ops->led_control(hw, ledaction);
2178        if ((bt_msr & MSR_MASK) == MSR_AP)
2179                rtl_write_byte(rtlpriv, REG_BCNTCFG + 1, 0x00);
2180        else
2181                rtl_write_byte(rtlpriv, REG_BCNTCFG + 1, 0x66);
2182
2183        return 0;
2184}
2185
2186void rtl8821ae_set_check_bssid(struct ieee80211_hw *hw, bool check_bssid)
2187{
2188        struct rtl_priv *rtlpriv = rtl_priv(hw);
2189        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2190        u32 reg_rcr = rtlpci->receive_config;
2191
2192        if (rtlpriv->psc.rfpwr_state != ERFON)
2193                return;
2194
2195        if (check_bssid) {
2196                reg_rcr |= (RCR_CBSSID_DATA | RCR_CBSSID_BCN);
2197                rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
2198                                              (u8 *)(&reg_rcr));
2199                _rtl8821ae_set_bcn_ctrl_reg(hw, 0, BIT(4));
2200        } else if (!check_bssid) {
2201                reg_rcr &= (~(RCR_CBSSID_DATA | RCR_CBSSID_BCN));
2202                _rtl8821ae_set_bcn_ctrl_reg(hw, BIT(4), 0);
2203                rtlpriv->cfg->ops->set_hw_reg(hw,
2204                        HW_VAR_RCR, (u8 *)(&reg_rcr));
2205        }
2206}
2207
2208int rtl8821ae_set_network_type(struct ieee80211_hw *hw, enum nl80211_iftype type)
2209{
2210        struct rtl_priv *rtlpriv = rtl_priv(hw);
2211
2212        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "rtl8821ae_set_network_type!\n");
2213
2214        if (_rtl8821ae_set_media_status(hw, type))
2215                return -EOPNOTSUPP;
2216
2217        if (rtlpriv->mac80211.link_state == MAC80211_LINKED) {
2218                if (type != NL80211_IFTYPE_AP)
2219                        rtl8821ae_set_check_bssid(hw, true);
2220        } else {
2221                rtl8821ae_set_check_bssid(hw, false);
2222        }
2223
2224        return 0;
2225}
2226
2227/* don't set REG_EDCA_BE_PARAM here because mac80211 will send pkt when scan */
2228void rtl8821ae_set_qos(struct ieee80211_hw *hw, int aci)
2229{
2230        struct rtl_priv *rtlpriv = rtl_priv(hw);
2231        rtl8821ae_dm_init_edca_turbo(hw);
2232        switch (aci) {
2233        case AC1_BK:
2234                rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM, 0xa44f);
2235                break;
2236        case AC0_BE:
2237                /* rtl_write_dword(rtlpriv, REG_EDCA_BE_PARAM, u4b_ac_param); */
2238                break;
2239        case AC2_VI:
2240                rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM, 0x5e4322);
2241                break;
2242        case AC3_VO:
2243                rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM, 0x2f3222);
2244                break;
2245        default:
2246                WARN_ONCE(true, "rtl8821ae: invalid aci: %d !\n", aci);
2247                break;
2248        }
2249}
2250
2251static void rtl8821ae_clear_interrupt(struct ieee80211_hw *hw)
2252{
2253        struct rtl_priv *rtlpriv = rtl_priv(hw);
2254        u32 tmp = rtl_read_dword(rtlpriv, REG_HISR);
2255
2256        rtl_write_dword(rtlpriv, REG_HISR, tmp);
2257
2258        tmp = rtl_read_dword(rtlpriv, REG_HISRE);
2259        rtl_write_dword(rtlpriv, REG_HISRE, tmp);
2260
2261        tmp = rtl_read_dword(rtlpriv, REG_HSISR);
2262        rtl_write_dword(rtlpriv, REG_HSISR, tmp);
2263}
2264
2265void rtl8821ae_enable_interrupt(struct ieee80211_hw *hw)
2266{
2267        struct rtl_priv *rtlpriv = rtl_priv(hw);
2268        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2269
2270        if (rtlpci->int_clear)
2271                rtl8821ae_clear_interrupt(hw);/*clear it here first*/
2272
2273        rtl_write_dword(rtlpriv, REG_HIMR, rtlpci->irq_mask[0] & 0xFFFFFFFF);
2274        rtl_write_dword(rtlpriv, REG_HIMRE, rtlpci->irq_mask[1] & 0xFFFFFFFF);
2275        rtlpci->irq_enabled = true;
2276        /* there are some C2H CMDs have been sent before
2277        system interrupt is enabled, e.g., C2H, CPWM.
2278        *So we need to clear all C2H events that FW has
2279        notified, otherwise FW won't schedule any commands anymore.
2280        */
2281        /* rtl_write_byte(rtlpriv, REG_C2HEVT_CLEAR, 0); */
2282        /*enable system interrupt*/
2283        rtl_write_dword(rtlpriv, REG_HSIMR, rtlpci->sys_irq_mask & 0xFFFFFFFF);
2284}
2285
2286void rtl8821ae_disable_interrupt(struct ieee80211_hw *hw)
2287{
2288        struct rtl_priv *rtlpriv = rtl_priv(hw);
2289        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2290
2291        rtl_write_dword(rtlpriv, REG_HIMR, IMR_DISABLED);
2292        rtl_write_dword(rtlpriv, REG_HIMRE, IMR_DISABLED);
2293        rtlpci->irq_enabled = false;
2294        /*synchronize_irq(rtlpci->pdev->irq);*/
2295}
2296
2297static void _rtl8821ae_clear_pci_pme_status(struct ieee80211_hw *hw)
2298{
2299        struct rtl_priv *rtlpriv = rtl_priv(hw);
2300        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2301        u16 cap_hdr;
2302        u8 cap_pointer;
2303        u8 cap_id = 0xff;
2304        u8 pmcs_reg;
2305        u8 cnt = 0;
2306
2307        /* Get the Capability pointer first,
2308         * the Capability Pointer is located at
2309         * offset 0x34 from the Function Header */
2310
2311        pci_read_config_byte(rtlpci->pdev, 0x34, &cap_pointer);
2312        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2313                 "PCI configuration 0x34 = 0x%2x\n", cap_pointer);
2314
2315        do {
2316                pci_read_config_word(rtlpci->pdev, cap_pointer, &cap_hdr);
2317                cap_id = cap_hdr & 0xFF;
2318
2319                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2320                         "in pci configuration, cap_pointer%x = %x\n",
2321                          cap_pointer, cap_id);
2322
2323                if (cap_id == 0x01) {
2324                        break;
2325                } else {
2326                        /* point to next Capability */
2327                        cap_pointer = (cap_hdr >> 8) & 0xFF;
2328                        /* 0: end of pci capability, 0xff: invalid value */
2329                        if (cap_pointer == 0x00 || cap_pointer == 0xff) {
2330                                cap_id = 0xff;
2331                                break;
2332                        }
2333                }
2334        } while (cnt++ < 200);
2335
2336        if (cap_id == 0x01) {
2337                /* Get the PM CSR (Control/Status Register),
2338                 * The PME_Status is located at PM Capatibility offset 5, bit 7
2339                 */
2340                pci_read_config_byte(rtlpci->pdev, cap_pointer + 5, &pmcs_reg);
2341
2342                if (pmcs_reg & BIT(7)) {
2343                        /* PME event occured, clear the PM_Status by write 1 */
2344                        pmcs_reg = pmcs_reg | BIT(7);
2345
2346                        pci_write_config_byte(rtlpci->pdev, cap_pointer + 5,
2347                                              pmcs_reg);
2348                        /* Read it back to check */
2349                        pci_read_config_byte(rtlpci->pdev, cap_pointer + 5,
2350                                             &pmcs_reg);
2351                        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2352                                 "Clear PME status 0x%2x to 0x%2x\n",
2353                                  cap_pointer + 5, pmcs_reg);
2354                } else {
2355                        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
2356                                 "PME status(0x%2x) = 0x%2x\n",
2357                                  cap_pointer + 5, pmcs_reg);
2358                }
2359        } else {
2360                RT_TRACE(rtlpriv, COMP_INIT, DBG_WARNING,
2361                         "Cannot find PME Capability\n");
2362        }
2363}
2364
2365void rtl8821ae_card_disable(struct ieee80211_hw *hw)
2366{
2367        struct rtl_priv *rtlpriv = rtl_priv(hw);
2368        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
2369        struct rtl_ps_ctl *ppsc = rtl_psc(rtlpriv);
2370        struct rtl_mac *mac = rtl_mac(rtlpriv);
2371        enum nl80211_iftype opmode;
2372        bool support_remote_wakeup;
2373        u8 tmp;
2374        u32 count = 0;
2375
2376        rtlpriv->cfg->ops->get_hw_reg(hw, HAL_DEF_WOWLAN,
2377                                      (u8 *)(&support_remote_wakeup));
2378
2379        RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
2380
2381        if (!(support_remote_wakeup && mac->opmode == NL80211_IFTYPE_STATION)
2382            || !rtlhal->enter_pnp_sleep) {
2383                RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Normal Power off\n");
2384                mac->link_state = MAC80211_NOLINK;
2385                opmode = NL80211_IFTYPE_UNSPECIFIED;
2386                _rtl8821ae_set_media_status(hw, opmode);
2387                _rtl8821ae_poweroff_adapter(hw);
2388        } else {
2389                RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Wowlan Supported.\n");
2390                /* 3 <1> Prepare for configuring wowlan related infomations */
2391                /* Clear Fw WoWLAN event. */
2392                rtl_write_byte(rtlpriv, REG_MCUTST_WOWLAN, 0x0);
2393
2394#if (USE_SPECIFIC_FW_TO_SUPPORT_WOWLAN == 1)
2395                rtl8821ae_set_fw_related_for_wowlan(hw, true);
2396#endif
2397                /* Dynamically adjust Tx packet boundary
2398                 * for download reserved page packet.
2399                 * reserve 30 pages for rsvd page */
2400                if (_rtl8821ae_dynamic_rqpn(hw, 0xE0, 0x3, 0x80c20d0d))
2401                        rtlhal->re_init_llt_table = true;
2402
2403                /* 3 <2> Set Fw releted H2C cmd. */
2404
2405                /* Set WoWLAN related security information. */
2406                rtl8821ae_set_fw_global_info_cmd(hw);
2407
2408                _rtl8821ae_download_rsvd_page(hw, true);
2409
2410                /* Just enable AOAC related functions when we connect to AP. */
2411                printk("mac->link_state = %d\n", mac->link_state);
2412                if (mac->link_state >= MAC80211_LINKED &&
2413                    mac->opmode == NL80211_IFTYPE_STATION) {
2414                        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AID, NULL);
2415                        rtl8821ae_set_fw_media_status_rpt_cmd(hw,
2416                                                              RT_MEDIA_CONNECT);
2417
2418                        rtl8821ae_set_fw_wowlan_mode(hw, true);
2419                        /* Enable Fw Keep alive mechanism. */
2420                        rtl8821ae_set_fw_keep_alive_cmd(hw, true);
2421
2422                        /* Enable disconnect decision control. */
2423                        rtl8821ae_set_fw_disconnect_decision_ctrl_cmd(hw, true);
2424                }
2425
2426                /* 3 <3> Hw Configutations */
2427
2428                /* Wait untill Rx DMA Finished before host sleep.
2429                 * FW Pause Rx DMA may happens when received packet doing dma.
2430                 */
2431                rtl_write_byte(rtlpriv, REG_RXDMA_CONTROL, BIT(2));
2432
2433                tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
2434                count = 0;
2435                while (!(tmp & BIT(1)) && (count++ < 100)) {
2436                        udelay(10);
2437                        tmp = rtl_read_byte(rtlpriv, REG_RXDMA_CONTROL);
2438                }
2439                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2440                         "Wait Rx DMA Finished before host sleep. count=%d\n",
2441                          count);
2442
2443                /* reset trx ring */
2444                rtlpriv->intf_ops->reset_trx_ring(hw);
2445
2446                rtl_write_byte(rtlpriv, REG_APS_FSMCO + 1, 0x0);
2447
2448                _rtl8821ae_clear_pci_pme_status(hw);
2449                tmp = rtl_read_byte(rtlpriv, REG_SYS_CLKR);
2450                rtl_write_byte(rtlpriv, REG_SYS_CLKR, tmp | BIT(3));
2451                /* prevent 8051 to be reset by PERST */
2452                rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x20);
2453                rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x60);
2454        }
2455
2456        if (rtlpriv->rtlhal.driver_is_goingto_unload ||
2457            ppsc->rfoff_reason > RF_CHANGE_BY_PS)
2458                rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
2459        /* For wowlan+LPS+32k. */
2460        if (support_remote_wakeup && rtlhal->enter_pnp_sleep) {
2461                /* Set the WoWLAN related function control enable.
2462                 * It should be the last H2C cmd in the WoWLAN flow. */
2463                rtl8821ae_set_fw_remote_wake_ctrl_cmd(hw, 1);
2464
2465                /* Stop Pcie Interface Tx DMA. */
2466                rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 1, 0xff);
2467                RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD, "Stop PCIE Tx DMA.\n");
2468
2469                /* Wait for TxDMA idle. */
2470                count = 0;
2471                do {
2472                        tmp = rtl_read_byte(rtlpriv, REG_PCIE_CTRL_REG);
2473                        udelay(10);
2474                        count++;
2475                } while ((tmp != 0) && (count < 100));
2476                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2477                         "Wait Tx DMA Finished before host sleep. count=%d\n",
2478                          count);
2479
2480                if (rtlhal->hw_rof_enable) {
2481                        printk("hw_rof_enable\n");
2482                        tmp = rtl_read_byte(rtlpriv, REG_HSISR + 3);
2483                        rtl_write_byte(rtlpriv, REG_HSISR + 3, tmp | BIT(1));
2484                }
2485        }
2486        /* after power off we should do iqk again */
2487        rtlpriv->phy.iqk_initialized = false;
2488}
2489
2490void rtl8821ae_interrupt_recognized(struct ieee80211_hw *hw,
2491                                  u32 *p_inta, u32 *p_intb)
2492{
2493        struct rtl_priv *rtlpriv = rtl_priv(hw);
2494        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2495
2496        *p_inta = rtl_read_dword(rtlpriv, ISR) & rtlpci->irq_mask[0];
2497        rtl_write_dword(rtlpriv, ISR, *p_inta);
2498
2499        *p_intb = rtl_read_dword(rtlpriv, REG_HISRE) & rtlpci->irq_mask[1];
2500        rtl_write_dword(rtlpriv, REG_HISRE, *p_intb);
2501}
2502
2503void rtl8821ae_set_beacon_related_registers(struct ieee80211_hw *hw)
2504{
2505        struct rtl_priv *rtlpriv = rtl_priv(hw);
2506        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2507        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2508        u16 bcn_interval, atim_window;
2509
2510        bcn_interval = mac->beacon_interval;
2511        atim_window = 2;        /*FIX MERGE */
2512        rtl8821ae_disable_interrupt(hw);
2513        rtl_write_word(rtlpriv, REG_ATIMWND, atim_window);
2514        rtl_write_word(rtlpriv, REG_BCN_INTERVAL, bcn_interval);
2515        rtl_write_word(rtlpriv, REG_BCNTCFG, 0x660f);
2516        rtl_write_byte(rtlpriv, REG_RXTSF_OFFSET_CCK, 0x18);
2517        rtl_write_byte(rtlpriv, REG_RXTSF_OFFSET_OFDM, 0x18);
2518        rtl_write_byte(rtlpriv, 0x606, 0x30);
2519        rtlpci->reg_bcn_ctrl_val |= BIT(3);
2520        rtl_write_byte(rtlpriv, REG_BCN_CTRL, (u8)rtlpci->reg_bcn_ctrl_val);
2521        rtl8821ae_enable_interrupt(hw);
2522}
2523
2524void rtl8821ae_set_beacon_interval(struct ieee80211_hw *hw)
2525{
2526        struct rtl_priv *rtlpriv = rtl_priv(hw);
2527        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2528        u16 bcn_interval = mac->beacon_interval;
2529
2530        RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
2531                 "beacon_interval:%d\n", bcn_interval);
2532        rtl8821ae_disable_interrupt(hw);
2533        rtl_write_word(rtlpriv, REG_BCN_INTERVAL, bcn_interval);
2534        rtl8821ae_enable_interrupt(hw);
2535}
2536
2537void rtl8821ae_update_interrupt_mask(struct ieee80211_hw *hw,
2538                                   u32 add_msr, u32 rm_msr)
2539{
2540        struct rtl_priv *rtlpriv = rtl_priv(hw);
2541        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
2542
2543        RT_TRACE(rtlpriv, COMP_INTR, DBG_LOUD,
2544                 "add_msr:%x, rm_msr:%x\n", add_msr, rm_msr);
2545
2546        if (add_msr)
2547                rtlpci->irq_mask[0] |= add_msr;
2548        if (rm_msr)
2549                rtlpci->irq_mask[0] &= (~rm_msr);
2550        rtl8821ae_disable_interrupt(hw);
2551        rtl8821ae_enable_interrupt(hw);
2552}
2553
2554static u8 _rtl8821ae_get_chnl_group(u8 chnl)
2555{
2556        u8 group = 0;
2557
2558        if (chnl <= 14) {
2559                if (1 <= chnl && chnl <= 2)
2560                        group = 0;
2561        else if (3 <= chnl && chnl <= 5)
2562                        group = 1;
2563        else if (6 <= chnl && chnl <= 8)
2564                        group = 2;
2565        else if (9 <= chnl && chnl <= 11)
2566                        group = 3;
2567        else /*if (12 <= chnl && chnl <= 14)*/
2568                        group = 4;
2569        } else {
2570                if (36 <= chnl && chnl <= 42)
2571                        group = 0;
2572        else if (44 <= chnl && chnl <= 48)
2573                        group = 1;
2574        else if (50 <= chnl && chnl <= 58)
2575                        group = 2;
2576        else if (60 <= chnl && chnl <= 64)
2577                        group = 3;
2578        else if (100 <= chnl && chnl <= 106)
2579                        group = 4;
2580        else if (108 <= chnl && chnl <= 114)
2581                        group = 5;
2582        else if (116 <= chnl && chnl <= 122)
2583                        group = 6;
2584        else if (124 <= chnl && chnl <= 130)
2585                        group = 7;
2586        else if (132 <= chnl && chnl <= 138)
2587                        group = 8;
2588        else if (140 <= chnl && chnl <= 144)
2589                        group = 9;
2590        else if (149 <= chnl && chnl <= 155)
2591                        group = 10;
2592        else if (157 <= chnl && chnl <= 161)
2593                        group = 11;
2594        else if (165 <= chnl && chnl <= 171)
2595                        group = 12;
2596        else if (173 <= chnl && chnl <= 177)
2597                        group = 13;
2598        else
2599                WARN_ONCE(true,
2600                          "rtl8821ae: 5G, Channel %d in Group not found\n",
2601                          chnl);
2602        }
2603        return group;
2604}
2605
2606static void _rtl8821ae_read_power_value_fromprom(struct ieee80211_hw *hw,
2607        struct txpower_info_2g *pwrinfo24g,
2608        struct txpower_info_5g *pwrinfo5g,
2609        bool autoload_fail,
2610        u8 *hwinfo)
2611{
2612        struct rtl_priv *rtlpriv = rtl_priv(hw);
2613        u32 rfPath, eeAddr = EEPROM_TX_PWR_INX, group, TxCount = 0;
2614
2615        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2616                 "hal_ReadPowerValueFromPROM8821ae(): hwinfo[0x%x]=0x%x\n",
2617                 (eeAddr+1), hwinfo[eeAddr+1]);
2618        if (0xFF == hwinfo[eeAddr+1])  /*YJ,add,120316*/
2619                autoload_fail = true;
2620
2621        if (autoload_fail) {
2622                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
2623                         "auto load fail : Use Default value!\n");
2624                for (rfPath = 0 ; rfPath < MAX_RF_PATH ; rfPath++) {
2625                        /*2.4G default value*/
2626                        for (group = 0 ; group < MAX_CHNL_GROUP_24G; group++) {
2627                                pwrinfo24g->index_cck_base[rfPath][group] =     0x2D;
2628                                pwrinfo24g->index_bw40_base[rfPath][group] = 0x2D;
2629                        }
2630                        for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2631                                if (TxCount == 0) {
2632                                        pwrinfo24g->bw20_diff[rfPath][0] = 0x02;
2633                                        pwrinfo24g->ofdm_diff[rfPath][0] = 0x04;
2634                                } else {
2635                                        pwrinfo24g->bw20_diff[rfPath][TxCount] = 0xFE;
2636                                        pwrinfo24g->bw40_diff[rfPath][TxCount] = 0xFE;
2637                                        pwrinfo24g->cck_diff[rfPath][TxCount] = 0xFE;
2638                                        pwrinfo24g->ofdm_diff[rfPath][TxCount] = 0xFE;
2639                                }
2640                        }
2641                        /*5G default value*/
2642                        for (group = 0 ; group < MAX_CHNL_GROUP_5G; group++)
2643                                pwrinfo5g->index_bw40_base[rfPath][group] = 0x2A;
2644
2645                        for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2646                                if (TxCount == 0) {
2647                                        pwrinfo5g->ofdm_diff[rfPath][0] = 0x04;
2648                                        pwrinfo5g->bw20_diff[rfPath][0] = 0x00;
2649                                        pwrinfo5g->bw80_diff[rfPath][0] = 0xFE;
2650                                        pwrinfo5g->bw160_diff[rfPath][0] = 0xFE;
2651                                } else {
2652                                        pwrinfo5g->ofdm_diff[rfPath][0] = 0xFE;
2653                                        pwrinfo5g->bw20_diff[rfPath][0] = 0xFE;
2654                                        pwrinfo5g->bw40_diff[rfPath][0] = 0xFE;
2655                                        pwrinfo5g->bw80_diff[rfPath][0] = 0xFE;
2656                                        pwrinfo5g->bw160_diff[rfPath][0] = 0xFE;
2657                                }
2658                        }
2659                }
2660                return;
2661        }
2662
2663        rtl_priv(hw)->efuse.txpwr_fromeprom = true;
2664
2665        for (rfPath = 0 ; rfPath < MAX_RF_PATH ; rfPath++) {
2666                /*2.4G default value*/
2667                for (group = 0 ; group < MAX_CHNL_GROUP_24G; group++) {
2668                        pwrinfo24g->index_cck_base[rfPath][group] = hwinfo[eeAddr++];
2669                        if (pwrinfo24g->index_cck_base[rfPath][group] == 0xFF)
2670                                pwrinfo24g->index_cck_base[rfPath][group] = 0x2D;
2671                }
2672                for (group = 0 ; group < MAX_CHNL_GROUP_24G - 1; group++) {
2673                        pwrinfo24g->index_bw40_base[rfPath][group] = hwinfo[eeAddr++];
2674                        if (pwrinfo24g->index_bw40_base[rfPath][group] == 0xFF)
2675                                pwrinfo24g->index_bw40_base[rfPath][group] = 0x2D;
2676                }
2677                for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2678                        if (TxCount == 0) {
2679                                pwrinfo24g->bw40_diff[rfPath][TxCount] = 0;
2680                                /*bit sign number to 8 bit sign number*/
2681                                pwrinfo24g->bw20_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0xf0) >> 4;
2682                                if (pwrinfo24g->bw20_diff[rfPath][TxCount] & BIT(3))
2683                                        pwrinfo24g->bw20_diff[rfPath][TxCount] |= 0xF0;
2684                                /*bit sign number to 8 bit sign number*/
2685                                pwrinfo24g->ofdm_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0x0f);
2686                                if (pwrinfo24g->ofdm_diff[rfPath][TxCount] & BIT(3))
2687                                        pwrinfo24g->ofdm_diff[rfPath][TxCount] |= 0xF0;
2688
2689                                pwrinfo24g->cck_diff[rfPath][TxCount] = 0;
2690                                eeAddr++;
2691                        } else {
2692                                pwrinfo24g->bw40_diff[rfPath][TxCount] = (hwinfo[eeAddr]&0xf0) >> 4;
2693                                if (pwrinfo24g->bw40_diff[rfPath][TxCount] & BIT(3))
2694                                        pwrinfo24g->bw40_diff[rfPath][TxCount] |= 0xF0;
2695
2696                                pwrinfo24g->bw20_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0x0f);
2697                                if (pwrinfo24g->bw20_diff[rfPath][TxCount] & BIT(3))
2698                                        pwrinfo24g->bw20_diff[rfPath][TxCount] |= 0xF0;
2699
2700                                eeAddr++;
2701
2702                                pwrinfo24g->ofdm_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0xf0) >> 4;
2703                                if (pwrinfo24g->ofdm_diff[rfPath][TxCount] & BIT(3))
2704                                        pwrinfo24g->ofdm_diff[rfPath][TxCount] |= 0xF0;
2705
2706                                pwrinfo24g->cck_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0x0f);
2707                                if (pwrinfo24g->cck_diff[rfPath][TxCount] & BIT(3))
2708                                        pwrinfo24g->cck_diff[rfPath][TxCount] |= 0xF0;
2709
2710                                eeAddr++;
2711                        }
2712                }
2713
2714                /*5G default value*/
2715                for (group = 0 ; group < MAX_CHNL_GROUP_5G; group++) {
2716                        pwrinfo5g->index_bw40_base[rfPath][group] = hwinfo[eeAddr++];
2717                        if (pwrinfo5g->index_bw40_base[rfPath][group] == 0xFF)
2718                                pwrinfo5g->index_bw40_base[rfPath][group] = 0xFE;
2719                }
2720
2721                for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2722                        if (TxCount == 0) {
2723                                pwrinfo5g->bw40_diff[rfPath][TxCount] = 0;
2724
2725                                pwrinfo5g->bw20_diff[rfPath][0] = (hwinfo[eeAddr] & 0xf0) >> 4;
2726                                if (pwrinfo5g->bw20_diff[rfPath][TxCount] & BIT(3))
2727                                        pwrinfo5g->bw20_diff[rfPath][TxCount] |= 0xF0;
2728
2729                                pwrinfo5g->ofdm_diff[rfPath][0] = (hwinfo[eeAddr] & 0x0f);
2730                                if (pwrinfo5g->ofdm_diff[rfPath][TxCount] & BIT(3))
2731                                        pwrinfo5g->ofdm_diff[rfPath][TxCount] |= 0xF0;
2732
2733                                eeAddr++;
2734                        } else {
2735                                pwrinfo5g->bw40_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0xf0) >> 4;
2736                                if (pwrinfo5g->bw40_diff[rfPath][TxCount] & BIT(3))
2737                                        pwrinfo5g->bw40_diff[rfPath][TxCount] |= 0xF0;
2738
2739                                pwrinfo5g->bw20_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0x0f);
2740                                if (pwrinfo5g->bw20_diff[rfPath][TxCount] & BIT(3))
2741                                        pwrinfo5g->bw20_diff[rfPath][TxCount] |= 0xF0;
2742
2743                                eeAddr++;
2744                        }
2745                }
2746
2747                pwrinfo5g->ofdm_diff[rfPath][1] =       (hwinfo[eeAddr] & 0xf0) >> 4;
2748                pwrinfo5g->ofdm_diff[rfPath][2] =       (hwinfo[eeAddr] & 0x0f);
2749
2750                eeAddr++;
2751
2752                pwrinfo5g->ofdm_diff[rfPath][3] = (hwinfo[eeAddr] & 0x0f);
2753
2754                eeAddr++;
2755
2756                for (TxCount = 1; TxCount < MAX_TX_COUNT; TxCount++) {
2757                        if (pwrinfo5g->ofdm_diff[rfPath][TxCount] & BIT(3))
2758                                pwrinfo5g->ofdm_diff[rfPath][TxCount] |= 0xF0;
2759                }
2760                for (TxCount = 0; TxCount < MAX_TX_COUNT; TxCount++) {
2761                        pwrinfo5g->bw80_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0xf0) >> 4;
2762                        /* 4bit sign number to 8 bit sign number */
2763                        if (pwrinfo5g->bw80_diff[rfPath][TxCount] & BIT(3))
2764                                pwrinfo5g->bw80_diff[rfPath][TxCount] |= 0xF0;
2765                        /* 4bit sign number to 8 bit sign number */
2766                        pwrinfo5g->bw160_diff[rfPath][TxCount] = (hwinfo[eeAddr] & 0x0f);
2767                        if (pwrinfo5g->bw160_diff[rfPath][TxCount] & BIT(3))
2768                                pwrinfo5g->bw160_diff[rfPath][TxCount] |= 0xF0;
2769
2770                        eeAddr++;
2771                }
2772        }
2773}
2774#if 0
2775static void _rtl8812ae_read_txpower_info_from_hwpg(struct ieee80211_hw *hw,
2776                                                 bool autoload_fail,
2777                                                 u8 *hwinfo)
2778{
2779        struct rtl_priv *rtlpriv = rtl_priv(hw);
2780        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2781        struct txpower_info_2g pwrinfo24g;
2782        struct txpower_info_5g pwrinfo5g;
2783        u8 rf_path, index;
2784        u8 i;
2785
2786        _rtl8821ae_read_power_value_fromprom(hw, &pwrinfo24g,
2787                                        &pwrinfo5g, autoload_fail, hwinfo);
2788
2789        for (rf_path = 0; rf_path < 2; rf_path++) {
2790                for (i = 0; i < CHANNEL_MAX_NUMBER_2G; i++) {
2791                        index = _rtl8821ae_get_chnl_group(i + 1);
2792
2793                        if (i == CHANNEL_MAX_NUMBER_2G - 1) {
2794                                rtlefuse->txpwrlevel_cck[rf_path][i] =
2795                                        pwrinfo24g.index_cck_base[rf_path][5];
2796                                rtlefuse->txpwrlevel_ht40_1s[rf_path][i] =
2797                                        pwrinfo24g.index_bw40_base[rf_path][index];
2798                        } else {
2799                                rtlefuse->txpwrlevel_cck[rf_path][i] =
2800                                        pwrinfo24g.index_cck_base[rf_path][index];
2801                                rtlefuse->txpwrlevel_ht40_1s[rf_path][i] =
2802                                        pwrinfo24g.index_bw40_base[rf_path][index];
2803                        }
2804                }
2805
2806                for (i = 0; i < CHANNEL_MAX_NUMBER_5G; i++) {
2807                        index = _rtl8821ae_get_chnl_group(channel5g[i]);
2808                        rtlefuse->txpwr_5g_bw40base[rf_path][i] =
2809                                        pwrinfo5g.index_bw40_base[rf_path][index];
2810                }
2811                for (i = 0; i < CHANNEL_MAX_NUMBER_5G_80M; i++) {
2812                        u8 upper, lower;
2813                        index = _rtl8821ae_get_chnl_group(channel5g_80m[i]);
2814                        upper = pwrinfo5g.index_bw40_base[rf_path][index];
2815                        lower = pwrinfo5g.index_bw40_base[rf_path][index + 1];
2816
2817                        rtlefuse->txpwr_5g_bw80base[rf_path][i] = (upper + lower) / 2;
2818                }
2819                for (i = 0; i < MAX_TX_COUNT; i++) {
2820                        rtlefuse->txpwr_cckdiff[rf_path][i] =
2821                                pwrinfo24g.cck_diff[rf_path][i];
2822                        rtlefuse->txpwr_legacyhtdiff[rf_path][i] =
2823                                pwrinfo24g.ofdm_diff[rf_path][i];
2824                        rtlefuse->txpwr_ht20diff[rf_path][i] =
2825                                pwrinfo24g.bw20_diff[rf_path][i];
2826                        rtlefuse->txpwr_ht40diff[rf_path][i] =
2827                                pwrinfo24g.bw40_diff[rf_path][i];
2828
2829                        rtlefuse->txpwr_5g_ofdmdiff[rf_path][i] =
2830                                pwrinfo5g.ofdm_diff[rf_path][i];
2831                        rtlefuse->txpwr_5g_bw20diff[rf_path][i] =
2832                                pwrinfo5g.bw20_diff[rf_path][i];
2833                        rtlefuse->txpwr_5g_bw40diff[rf_path][i] =
2834                                pwrinfo5g.bw40_diff[rf_path][i];
2835                        rtlefuse->txpwr_5g_bw80diff[rf_path][i] =
2836                                pwrinfo5g.bw80_diff[rf_path][i];
2837                }
2838        }
2839
2840        if (!autoload_fail) {
2841                rtlefuse->eeprom_regulatory =
2842                        hwinfo[EEPROM_RF_BOARD_OPTION] & 0x07;/*bit0~2*/
2843                if (hwinfo[EEPROM_RF_BOARD_OPTION] == 0xFF)
2844                        rtlefuse->eeprom_regulatory = 0;
2845        } else {
2846                rtlefuse->eeprom_regulatory = 0;
2847        }
2848
2849        RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
2850        "eeprom_regulatory = 0x%x\n", rtlefuse->eeprom_regulatory);
2851}
2852#endif
2853static void _rtl8821ae_read_txpower_info_from_hwpg(struct ieee80211_hw *hw,
2854                                                 bool autoload_fail,
2855                                                 u8 *hwinfo)
2856{
2857        struct rtl_priv *rtlpriv = rtl_priv(hw);
2858        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2859        struct txpower_info_2g pwrinfo24g;
2860        struct txpower_info_5g pwrinfo5g;
2861        u8 rf_path, index;
2862        u8 i;
2863
2864        _rtl8821ae_read_power_value_fromprom(hw, &pwrinfo24g,
2865                &pwrinfo5g, autoload_fail, hwinfo);
2866
2867        for (rf_path = 0; rf_path < 2; rf_path++) {
2868                for (i = 0; i < CHANNEL_MAX_NUMBER_2G; i++) {
2869                        index = _rtl8821ae_get_chnl_group(i + 1);
2870
2871                        if (i == CHANNEL_MAX_NUMBER_2G - 1) {
2872                                rtlefuse->txpwrlevel_cck[rf_path][i] =
2873                                        pwrinfo24g.index_cck_base[rf_path][5];
2874                                rtlefuse->txpwrlevel_ht40_1s[rf_path][i] =
2875                                        pwrinfo24g.index_bw40_base[rf_path][index];
2876                        } else {
2877                                rtlefuse->txpwrlevel_cck[rf_path][i] =
2878                                        pwrinfo24g.index_cck_base[rf_path][index];
2879                                rtlefuse->txpwrlevel_ht40_1s[rf_path][i] =
2880                                        pwrinfo24g.index_bw40_base[rf_path][index];
2881                        }
2882                }
2883
2884                for (i = 0; i < CHANNEL_MAX_NUMBER_5G; i++) {
2885                        index = _rtl8821ae_get_chnl_group(channel5g[i]);
2886                        rtlefuse->txpwr_5g_bw40base[rf_path][i] =
2887                                pwrinfo5g.index_bw40_base[rf_path][index];
2888                }
2889                for (i = 0; i < CHANNEL_MAX_NUMBER_5G_80M; i++) {
2890                        u8 upper, lower;
2891                        index = _rtl8821ae_get_chnl_group(channel5g_80m[i]);
2892                        upper = pwrinfo5g.index_bw40_base[rf_path][index];
2893                        lower = pwrinfo5g.index_bw40_base[rf_path][index + 1];
2894
2895                        rtlefuse->txpwr_5g_bw80base[rf_path][i] = (upper + lower) / 2;
2896                }
2897                for (i = 0; i < MAX_TX_COUNT; i++) {
2898                        rtlefuse->txpwr_cckdiff[rf_path][i] =
2899                                pwrinfo24g.cck_diff[rf_path][i];
2900                        rtlefuse->txpwr_legacyhtdiff[rf_path][i] =
2901                                pwrinfo24g.ofdm_diff[rf_path][i];
2902                        rtlefuse->txpwr_ht20diff[rf_path][i] =
2903                                pwrinfo24g.bw20_diff[rf_path][i];
2904                        rtlefuse->txpwr_ht40diff[rf_path][i] =
2905                                pwrinfo24g.bw40_diff[rf_path][i];
2906
2907                        rtlefuse->txpwr_5g_ofdmdiff[rf_path][i] =
2908                                pwrinfo5g.ofdm_diff[rf_path][i];
2909                        rtlefuse->txpwr_5g_bw20diff[rf_path][i] =
2910                                pwrinfo5g.bw20_diff[rf_path][i];
2911                        rtlefuse->txpwr_5g_bw40diff[rf_path][i] =
2912                                pwrinfo5g.bw40_diff[rf_path][i];
2913                        rtlefuse->txpwr_5g_bw80diff[rf_path][i] =
2914                                pwrinfo5g.bw80_diff[rf_path][i];
2915                }
2916        }
2917        /*bit0~2*/
2918        if (!autoload_fail) {
2919                rtlefuse->eeprom_regulatory = hwinfo[EEPROM_RF_BOARD_OPTION] & 0x07;
2920                if (hwinfo[EEPROM_RF_BOARD_OPTION] == 0xFF)
2921                        rtlefuse->eeprom_regulatory = 0;
2922        } else {
2923                rtlefuse->eeprom_regulatory = 0;
2924        }
2925
2926        RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
2927        "eeprom_regulatory = 0x%x\n", rtlefuse->eeprom_regulatory);
2928}
2929
2930static void _rtl8812ae_read_pa_type(struct ieee80211_hw *hw, u8 *hwinfo,
2931                                    bool autoload_fail)
2932{
2933        struct rtl_priv *rtlpriv = rtl_priv(hw);
2934        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
2935
2936        if (!autoload_fail) {
2937                rtlhal->pa_type_2g = hwinfo[0xBC];
2938                rtlhal->lna_type_2g = hwinfo[0xBD];
2939                if (rtlhal->pa_type_2g == 0xFF && rtlhal->lna_type_2g == 0xFF) {
2940                        rtlhal->pa_type_2g = 0;
2941                        rtlhal->lna_type_2g = 0;
2942                }
2943                rtlhal->external_pa_2g = ((rtlhal->pa_type_2g & BIT(5)) &&
2944                                          (rtlhal->pa_type_2g & BIT(4))) ?
2945                                         1 : 0;
2946                rtlhal->external_lna_2g = ((rtlhal->lna_type_2g & BIT(7)) &&
2947                                           (rtlhal->lna_type_2g & BIT(3))) ?
2948                                          1 : 0;
2949
2950                rtlhal->pa_type_5g = hwinfo[0xBC];
2951                rtlhal->lna_type_5g = hwinfo[0xBF];
2952                if (rtlhal->pa_type_5g == 0xFF && rtlhal->lna_type_5g == 0xFF) {
2953                        rtlhal->pa_type_5g = 0;
2954                        rtlhal->lna_type_5g = 0;
2955                }
2956                rtlhal->external_pa_5g = ((rtlhal->pa_type_5g & BIT(1)) &&
2957                                          (rtlhal->pa_type_5g & BIT(0))) ?
2958                                         1 : 0;
2959                rtlhal->external_lna_5g = ((rtlhal->lna_type_5g & BIT(7)) &&
2960                                           (rtlhal->lna_type_5g & BIT(3))) ?
2961                                          1 : 0;
2962        } else {
2963                rtlhal->external_pa_2g  = 0;
2964                rtlhal->external_lna_2g = 0;
2965                rtlhal->external_pa_5g  = 0;
2966                rtlhal->external_lna_5g = 0;
2967        }
2968}
2969
2970static void _rtl8812ae_read_amplifier_type(struct ieee80211_hw *hw, u8 *hwinfo,
2971                                           bool autoload_fail)
2972{
2973        struct rtl_priv *rtlpriv = rtl_priv(hw);
2974        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
2975
2976        u8 ext_type_pa_2g_a  = (hwinfo[0xBD] & BIT(2))      >> 2; /* 0xBD[2] */
2977        u8 ext_type_pa_2g_b  = (hwinfo[0xBD] & BIT(6))      >> 6; /* 0xBD[6] */
2978        u8 ext_type_pa_5g_a  = (hwinfo[0xBF] & BIT(2))      >> 2; /* 0xBF[2] */
2979        u8 ext_type_pa_5g_b  = (hwinfo[0xBF] & BIT(6))      >> 6; /* 0xBF[6] */
2980        /* 0xBD[1:0] */
2981        u8 ext_type_lna_2g_a = (hwinfo[0xBD] & (BIT(1) | BIT(0))) >> 0;
2982        /* 0xBD[5:4] */
2983        u8 ext_type_lna_2g_b = (hwinfo[0xBD] & (BIT(5) | BIT(4))) >> 4;
2984        /* 0xBF[1:0] */
2985        u8 ext_type_lna_5g_a = (hwinfo[0xBF] & (BIT(1) | BIT(0))) >> 0;
2986        /* 0xBF[5:4] */
2987        u8 ext_type_lna_5g_b = (hwinfo[0xBF] & (BIT(5) | BIT(4))) >> 4;
2988
2989        _rtl8812ae_read_pa_type(hw, hwinfo, autoload_fail);
2990
2991        /* [2.4G] Path A and B are both extPA */
2992        if ((rtlhal->pa_type_2g & (BIT(5) | BIT(4))) == (BIT(5) | BIT(4)))
2993                rtlhal->type_gpa  = ext_type_pa_2g_b  << 2 | ext_type_pa_2g_a;
2994
2995        /* [5G] Path A and B are both extPA */
2996        if ((rtlhal->pa_type_5g & (BIT(1) | BIT(0))) == (BIT(1) | BIT(0)))
2997                rtlhal->type_apa  = ext_type_pa_5g_b  << 2 | ext_type_pa_5g_a;
2998
2999        /* [2.4G] Path A and B are both extLNA */
3000        if ((rtlhal->lna_type_2g & (BIT(7) | BIT(3))) == (BIT(7) | BIT(3)))
3001                rtlhal->type_glna = ext_type_lna_2g_b << 2 | ext_type_lna_2g_a;
3002
3003        /* [5G] Path A and B are both extLNA */
3004        if ((rtlhal->lna_type_5g & (BIT(7) | BIT(3))) == (BIT(7) | BIT(3)))
3005                rtlhal->type_alna = ext_type_lna_5g_b << 2 | ext_type_lna_5g_a;
3006}
3007
3008static void _rtl8821ae_read_pa_type(struct ieee80211_hw *hw, u8 *hwinfo,
3009                                    bool autoload_fail)
3010{
3011        struct rtl_priv *rtlpriv = rtl_priv(hw);
3012        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
3013
3014        if (!autoload_fail) {
3015                rtlhal->pa_type_2g = hwinfo[0xBC];
3016                rtlhal->lna_type_2g = hwinfo[0xBD];
3017                if (rtlhal->pa_type_2g == 0xFF && rtlhal->lna_type_2g == 0xFF) {
3018                        rtlhal->pa_type_2g = 0;
3019                        rtlhal->lna_type_2g = 0;
3020                }
3021                rtlhal->external_pa_2g = (rtlhal->pa_type_2g & BIT(5)) ? 1 : 0;
3022                rtlhal->external_lna_2g = (rtlhal->lna_type_2g & BIT(7)) ? 1 : 0;
3023
3024                rtlhal->pa_type_5g = hwinfo[0xBC];
3025                rtlhal->lna_type_5g = hwinfo[0xBF];
3026                if (rtlhal->pa_type_5g == 0xFF && rtlhal->lna_type_5g == 0xFF) {
3027                        rtlhal->pa_type_5g = 0;
3028                        rtlhal->lna_type_5g = 0;
3029                }
3030                rtlhal->external_pa_5g = (rtlhal->pa_type_5g & BIT(1)) ? 1 : 0;
3031                rtlhal->external_lna_5g = (rtlhal->lna_type_5g & BIT(7)) ? 1 : 0;
3032        } else {
3033                rtlhal->external_pa_2g  = 0;
3034                rtlhal->external_lna_2g = 0;
3035                rtlhal->external_pa_5g  = 0;
3036                rtlhal->external_lna_5g = 0;
3037        }
3038}
3039
3040static void _rtl8821ae_read_rfe_type(struct ieee80211_hw *hw, u8 *hwinfo,
3041                              bool autoload_fail)
3042{
3043        struct rtl_priv *rtlpriv = rtl_priv(hw);
3044        struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
3045
3046        if (!autoload_fail) {
3047                if (hwinfo[EEPROM_RFE_OPTION] & BIT(7)) {
3048                        if (rtlhal->external_lna_5g) {
3049                                if (rtlhal->external_pa_5g) {
3050                                        if (rtlhal->external_lna_2g &&
3051                                            rtlhal->external_pa_2g)
3052                                                rtlhal->rfe_type = 3;
3053                                        else
3054                                                rtlhal->rfe_type = 0;
3055                                } else {
3056                                        rtlhal->rfe_type = 2;
3057                                }
3058                        } else {
3059                                rtlhal->rfe_type = 4;
3060                        }
3061                } else {
3062                        rtlhal->rfe_type = hwinfo[EEPROM_RFE_OPTION] & 0x3F;
3063
3064                        if (rtlhal->rfe_type == 4 &&
3065                            (rtlhal->external_pa_5g ||
3066                             rtlhal->external_pa_2g ||
3067                             rtlhal->external_lna_5g ||
3068                             rtlhal->external_lna_2g)) {
3069                                if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE)
3070                                        rtlhal->rfe_type = 2;
3071                        }
3072                }
3073        } else {
3074                rtlhal->rfe_type = 0x04;
3075        }
3076
3077        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3078                 "RFE Type: 0x%2x\n", rtlhal->rfe_type);
3079}
3080
3081static void _rtl8812ae_read_bt_coexist_info_from_hwpg(struct ieee80211_hw *hw,
3082                                              bool auto_load_fail, u8 *hwinfo)
3083{
3084        struct rtl_priv *rtlpriv = rtl_priv(hw);
3085        u8 value;
3086
3087        if (!auto_load_fail) {
3088                value = *(u8 *)&hwinfo[EEPROM_RF_BOARD_OPTION];
3089                if (((value & 0xe0) >> 5) == 0x1)
3090                        rtlpriv->btcoexist.btc_info.btcoexist = 1;
3091                else
3092                        rtlpriv->btcoexist.btc_info.btcoexist = 0;
3093                rtlpriv->btcoexist.btc_info.bt_type = BT_RTL8812A;
3094
3095                value = hwinfo[EEPROM_RF_BT_SETTING];
3096                rtlpriv->btcoexist.btc_info.ant_num = (value & 0x1);
3097        } else {
3098                rtlpriv->btcoexist.btc_info.btcoexist = 0;
3099                rtlpriv->btcoexist.btc_info.bt_type = BT_RTL8812A;
3100                rtlpriv->btcoexist.btc_info.ant_num = ANT_X2;
3101        }
3102        /*move BT_InitHalVars() to init_sw_vars*/
3103}
3104
3105static void _rtl8821ae_read_bt_coexist_info_from_hwpg(struct ieee80211_hw *hw,
3106                                              bool auto_load_fail, u8 *hwinfo)
3107{
3108        struct rtl_priv *rtlpriv = rtl_priv(hw);
3109        u8 value;
3110        u32 tmpu_32;
3111
3112        if (!auto_load_fail) {
3113                tmpu_32 = rtl_read_dword(rtlpriv, REG_MULTI_FUNC_CTRL);
3114                if (tmpu_32 & BIT(18))
3115                        rtlpriv->btcoexist.btc_info.btcoexist = 1;
3116                else
3117                        rtlpriv->btcoexist.btc_info.btcoexist = 0;
3118                rtlpriv->btcoexist.btc_info.bt_type = BT_RTL8821A;
3119
3120                value = hwinfo[EEPROM_RF_BT_SETTING];
3121                rtlpriv->btcoexist.btc_info.ant_num = (value & 0x1);
3122        } else {
3123                rtlpriv->btcoexist.btc_info.btcoexist = 0;
3124                rtlpriv->btcoexist.btc_info.bt_type = BT_RTL8821A;
3125                rtlpriv->btcoexist.btc_info.ant_num = ANT_X2;
3126        }
3127        /*move BT_InitHalVars() to init_sw_vars*/
3128}
3129
3130static void _rtl8821ae_read_adapter_info(struct ieee80211_hw *hw, bool b_pseudo_test)
3131{
3132        struct rtl_priv *rtlpriv = rtl_priv(hw);
3133        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
3134        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3135        int params[] = {RTL_EEPROM_ID, EEPROM_VID, EEPROM_DID,
3136                        EEPROM_SVID, EEPROM_SMID, EEPROM_MAC_ADDR,
3137                        EEPROM_CHANNELPLAN, EEPROM_VERSION, EEPROM_CUSTOMER_ID,
3138                        COUNTRY_CODE_WORLD_WIDE_13};
3139        u8 *hwinfo;
3140
3141        if (b_pseudo_test) {
3142                ;/* need add */
3143        }
3144
3145        hwinfo = kzalloc(HWSET_MAX_SIZE, GFP_KERNEL);
3146        if (!hwinfo)
3147                return;
3148
3149        if (rtl_get_hwinfo(hw, rtlpriv, HWSET_MAX_SIZE, hwinfo, params))
3150                goto exit;
3151
3152        _rtl8821ae_read_txpower_info_from_hwpg(hw, rtlefuse->autoload_failflag,
3153                                               hwinfo);
3154
3155        if (rtlhal->hw_type == HARDWARE_TYPE_RTL8812AE) {
3156                _rtl8812ae_read_amplifier_type(hw, hwinfo,
3157                                               rtlefuse->autoload_failflag);
3158                _rtl8812ae_read_bt_coexist_info_from_hwpg(hw,
3159                                rtlefuse->autoload_failflag, hwinfo);
3160        } else {
3161                _rtl8821ae_read_pa_type(hw, hwinfo, rtlefuse->autoload_failflag);
3162                _rtl8821ae_read_bt_coexist_info_from_hwpg(hw,
3163                                rtlefuse->autoload_failflag, hwinfo);
3164        }
3165
3166        _rtl8821ae_read_rfe_type(hw, hwinfo, rtlefuse->autoload_failflag);
3167        /*board type*/
3168        rtlefuse->board_type = ODM_BOARD_DEFAULT;
3169        if (rtlhal->external_lna_2g != 0)
3170                rtlefuse->board_type |= ODM_BOARD_EXT_LNA;
3171        if (rtlhal->external_lna_5g != 0)
3172                rtlefuse->board_type |= ODM_BOARD_EXT_LNA_5G;
3173        if (rtlhal->external_pa_2g != 0)
3174                rtlefuse->board_type |= ODM_BOARD_EXT_PA;
3175        if (rtlhal->external_pa_5g != 0)
3176                rtlefuse->board_type |= ODM_BOARD_EXT_PA_5G;
3177
3178        if (rtlpriv->btcoexist.btc_info.btcoexist == 1)
3179                rtlefuse->board_type |= ODM_BOARD_BT;
3180
3181        rtlhal->board_type = rtlefuse->board_type;
3182        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3183                 "board_type = 0x%x\n", rtlefuse->board_type);
3184
3185        rtlefuse->eeprom_channelplan = *(u8 *)&hwinfo[EEPROM_CHANNELPLAN];
3186        if (rtlefuse->eeprom_channelplan == 0xff)
3187                rtlefuse->eeprom_channelplan = 0x7F;
3188
3189        /* set channel plan from efuse */
3190        rtlefuse->channel_plan = rtlefuse->eeprom_channelplan;
3191
3192        /*parse xtal*/
3193        rtlefuse->crystalcap = hwinfo[EEPROM_XTAL_8821AE];
3194        if (rtlefuse->crystalcap == 0xFF)
3195                rtlefuse->crystalcap = 0x20;
3196
3197        rtlefuse->eeprom_thermalmeter = *(u8 *)&hwinfo[EEPROM_THERMAL_METER];
3198        if ((rtlefuse->eeprom_thermalmeter == 0xff) ||
3199            rtlefuse->autoload_failflag) {
3200                rtlefuse->apk_thermalmeterignore = true;
3201                rtlefuse->eeprom_thermalmeter = 0xff;
3202        }
3203
3204        rtlefuse->thermalmeter[0] = rtlefuse->eeprom_thermalmeter;
3205        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3206                 "thermalmeter = 0x%x\n", rtlefuse->eeprom_thermalmeter);
3207
3208        if (!rtlefuse->autoload_failflag) {
3209                rtlefuse->antenna_div_cfg =
3210                  (hwinfo[EEPROM_RF_BOARD_OPTION] & 0x18) >> 3;
3211                if (hwinfo[EEPROM_RF_BOARD_OPTION] == 0xff)
3212                        rtlefuse->antenna_div_cfg = 0;
3213
3214                if (rtlpriv->btcoexist.btc_info.btcoexist == 1 &&
3215                    rtlpriv->btcoexist.btc_info.ant_num == ANT_X1)
3216                        rtlefuse->antenna_div_cfg = 0;
3217
3218                rtlefuse->antenna_div_type = hwinfo[EEPROM_RF_ANTENNA_OPT_88E];
3219                if (rtlefuse->antenna_div_type == 0xff)
3220                        rtlefuse->antenna_div_type = FIXED_HW_ANTDIV;
3221        } else {
3222                rtlefuse->antenna_div_cfg = 0;
3223                rtlefuse->antenna_div_type = 0;
3224        }
3225
3226        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
3227                "SWAS: bHwAntDiv = %x, TRxAntDivType = %x\n",
3228                rtlefuse->antenna_div_cfg, rtlefuse->antenna_div_type);
3229
3230        rtlpriv->ledctl.led_opendrain = true;
3231
3232        if (rtlhal->oem_id == RT_CID_DEFAULT) {
3233                switch (rtlefuse->eeprom_oemid) {
3234                case RT_CID_DEFAULT:
3235                        break;
3236                case EEPROM_CID_TOSHIBA:
3237                        rtlhal->oem_id = RT_CID_TOSHIBA;
3238                        break;
3239                case EEPROM_CID_CCX:
3240                        rtlhal->oem_id = RT_CID_CCX;
3241                        break;
3242                case EEPROM_CID_QMI:
3243                        rtlhal->oem_id = RT_CID_819X_QMI;
3244                        break;
3245                case EEPROM_CID_WHQL:
3246                        break;
3247                default:
3248                        break;
3249                }
3250        }
3251exit:
3252        kfree(hwinfo);
3253}
3254
3255/*static void _rtl8821ae_hal_customized_behavior(struct ieee80211_hw *hw)
3256{
3257        struct rtl_priv *rtlpriv = rtl_priv(hw);
3258        struct rtl_pci_priv *pcipriv = rtl_pcipriv(hw);
3259        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3260
3261        rtlpriv->ledctl.led_opendrain = true;
3262        switch (rtlhal->oem_id) {
3263        case RT_CID_819X_HP:
3264                rtlpriv->ledctl.led_opendrain = true;
3265                break;
3266        case RT_CID_819X_LENOVO:
3267        case RT_CID_DEFAULT:
3268        case RT_CID_TOSHIBA:
3269        case RT_CID_CCX:
3270        case RT_CID_819X_ACER:
3271        case RT_CID_WHQL:
3272        default:
3273                break;
3274        }
3275        RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
3276                 "RT Customized ID: 0x%02X\n", rtlhal->oem_id);
3277}*/
3278
3279void rtl8821ae_read_eeprom_info(struct ieee80211_hw *hw)
3280{
3281        struct rtl_priv *rtlpriv = rtl_priv(hw);
3282        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
3283        struct rtl_phy *rtlphy = &rtlpriv->phy;
3284        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3285        u8 tmp_u1b;
3286
3287        rtlhal->version = _rtl8821ae_read_chip_version(hw);
3288        if (get_rf_type(rtlphy) == RF_1T1R)
3289                rtlpriv->dm.rfpath_rxenable[0] = true;
3290        else
3291                rtlpriv->dm.rfpath_rxenable[0] =
3292                    rtlpriv->dm.rfpath_rxenable[1] = true;
3293        RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "VersionID = 0x%4x\n",
3294                                                rtlhal->version);
3295
3296        tmp_u1b = rtl_read_byte(rtlpriv, REG_9346CR);
3297        if (tmp_u1b & BIT(4)) {
3298                RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Boot from EEPROM\n");
3299                rtlefuse->epromtype = EEPROM_93C46;
3300        } else {
3301                RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Boot from EFUSE\n");
3302                rtlefuse->epromtype = EEPROM_BOOT_EFUSE;
3303        }
3304
3305        if (tmp_u1b & BIT(5)) {
3306                RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Autoload OK\n");
3307                rtlefuse->autoload_failflag = false;
3308                _rtl8821ae_read_adapter_info(hw, false);
3309        } else {
3310                pr_err("Autoload ERR!!\n");
3311        }
3312        /*hal_ReadRFType_8812A()*/
3313        /* _rtl8821ae_hal_customized_behavior(hw); */
3314}
3315
3316static void rtl8821ae_update_hal_rate_table(struct ieee80211_hw *hw,
3317                struct ieee80211_sta *sta)
3318{
3319        struct rtl_priv *rtlpriv = rtl_priv(hw);
3320        struct rtl_phy *rtlphy = &rtlpriv->phy;
3321        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
3322        struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
3323        u32 ratr_value;
3324        u8 ratr_index = 0;
3325        u8 b_nmode = mac->ht_enable;
3326        u8 mimo_ps = IEEE80211_SMPS_OFF;
3327        u16 shortgi_rate;
3328        u32 tmp_ratr_value;
3329        u8 curtxbw_40mhz = mac->bw_40;
3330        u8 b_curshortgi_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
3331                                1 : 0;
3332        u8 b_curshortgi_20mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
3333                                1 : 0;
3334        enum wireless_mode wirelessmode = mac->mode;
3335
3336        if (rtlhal->current_bandtype == BAND_ON_5G)
3337                ratr_value = sta->supp_rates[1] << 4;
3338        else
3339                ratr_value = sta->supp_rates[0];
3340        if (mac->opmode == NL80211_IFTYPE_ADHOC)
3341                ratr_value = 0xfff;
3342        ratr_value |= (sta->ht_cap.mcs.rx_mask[1] << 20 |
3343                        sta->ht_cap.mcs.rx_mask[0] << 12);
3344        switch (wirelessmode) {
3345        case WIRELESS_MODE_B:
3346                if (ratr_value & 0x0000000c)
3347                        ratr_value &= 0x0000000d;
3348                else
3349                        ratr_value &= 0x0000000f;
3350                break;
3351        case WIRELESS_MODE_G:
3352                ratr_value &= 0x00000FF5;
3353                break;
3354        case WIRELESS_MODE_N_24G:
3355        case WIRELESS_MODE_N_5G:
3356                b_nmode = 1;
3357                if (mimo_ps == IEEE80211_SMPS_STATIC) {
3358                        ratr_value &= 0x0007F005;
3359                } else {
3360                        u32 ratr_mask;
3361
3362                        if (get_rf_type(rtlphy) == RF_1T2R ||
3363                            get_rf_type(rtlphy) == RF_1T1R)
3364                                ratr_mask = 0x000ff005;
3365                        else
3366                                ratr_mask = 0x0f0ff005;
3367
3368                        ratr_value &= ratr_mask;
3369                }
3370                break;
3371        default:
3372                if (rtlphy->rf_type == RF_1T2R)
3373                        ratr_value &= 0x000ff0ff;
3374                else
3375                        ratr_value &= 0x0f0ff0ff;
3376
3377                break;
3378        }
3379
3380        if ((rtlpriv->btcoexist.bt_coexistence) &&
3381             (rtlpriv->btcoexist.bt_coexist_type == BT_CSR_BC4) &&
3382             (rtlpriv->btcoexist.bt_cur_state) &&
3383             (rtlpriv->btcoexist.bt_ant_isolation) &&
3384             ((rtlpriv->btcoexist.bt_service == BT_SCO) ||
3385             (rtlpriv->btcoexist.bt_service == BT_BUSY)))
3386                ratr_value &= 0x0fffcfc0;
3387        else
3388                ratr_value &= 0x0FFFFFFF;
3389
3390        if (b_nmode && ((curtxbw_40mhz &&
3391                         b_curshortgi_40mhz) || (!curtxbw_40mhz &&
3392                                                 b_curshortgi_20mhz))) {
3393                ratr_value |= 0x10000000;
3394                tmp_ratr_value = (ratr_value >> 12);
3395
3396                for (shortgi_rate = 15; shortgi_rate > 0; shortgi_rate--) {
3397                        if ((1 << shortgi_rate) & tmp_ratr_value)
3398                                break;
3399                }
3400
3401                shortgi_rate = (shortgi_rate << 12) | (shortgi_rate << 8) |
3402                    (shortgi_rate << 4) | (shortgi_rate);
3403        }
3404
3405        rtl_write_dword(rtlpriv, REG_ARFR0 + ratr_index * 4, ratr_value);
3406
3407        RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
3408                 "%x\n", rtl_read_dword(rtlpriv, REG_ARFR0));
3409}
3410
3411static u8 _rtl8821ae_mrate_idx_to_arfr_id(
3412        struct ieee80211_hw *hw, u8 rate_index,
3413        enum wireless_mode wirelessmode)
3414{
3415        struct rtl_priv *rtlpriv = rtl_priv(hw);
3416        struct rtl_phy *rtlphy = &rtlpriv->phy;
3417        u8 ret = 0;
3418        switch (rate_index) {
3419        case RATR_INX_WIRELESS_NGB:
3420                if (rtlphy->rf_type == RF_1T1R)
3421                        ret = 1;
3422                else
3423                        ret = 0;
3424                ; break;
3425        case RATR_INX_WIRELESS_N:
3426        case RATR_INX_WIRELESS_NG:
3427                if (rtlphy->rf_type == RF_1T1R)
3428                        ret = 5;
3429                else
3430                        ret = 4;
3431                ; break;
3432        case RATR_INX_WIRELESS_NB:
3433                if (rtlphy->rf_type == RF_1T1R)
3434                        ret = 3;
3435                else
3436                        ret = 2;
3437                ; break;
3438        case RATR_INX_WIRELESS_GB:
3439                ret = 6;
3440                break;
3441        case RATR_INX_WIRELESS_G:
3442                ret = 7;
3443                break;
3444        case RATR_INX_WIRELESS_B:
3445                ret = 8;
3446                break;
3447        case RATR_INX_WIRELESS_MC:
3448                if ((wirelessmode == WIRELESS_MODE_B)
3449                        || (wirelessmode == WIRELESS_MODE_G)
3450                        || (wirelessmode == WIRELESS_MODE_N_24G)
3451                        || (wirelessmode == WIRELESS_MODE_AC_24G))
3452                        ret = 6;
3453                else
3454                        ret = 7;
3455        case RATR_INX_WIRELESS_AC_5N:
3456                if (rtlphy->rf_type == RF_1T1R)
3457                        ret = 10;
3458                else
3459                        ret = 9;
3460                break;
3461        case RATR_INX_WIRELESS_AC_24N:
3462                if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80) {
3463                        if (rtlphy->rf_type == RF_1T1R)
3464                                ret = 10;
3465                        else
3466                                ret = 9;
3467                } else {
3468                        if (rtlphy->rf_type == RF_1T1R)
3469                                ret = 11;
3470                        else
3471                                ret = 12;
3472                }
3473                break;
3474        default:
3475                ret = 0; break;
3476        }
3477        return ret;
3478}
3479
3480static u32 _rtl8821ae_rate_to_bitmap_2ssvht(__le16 vht_rate)
3481{
3482        u8 i, j, tmp_rate;
3483        u32 rate_bitmap = 0;
3484
3485        for (i = j = 0; i < 4; i += 2, j += 10) {
3486                tmp_rate = (le16_to_cpu(vht_rate) >> i) & 3;
3487
3488                switch (tmp_rate) {
3489                case 2:
3490                        rate_bitmap = rate_bitmap | (0x03ff << j);
3491                        break;
3492                case 1:
3493                        rate_bitmap = rate_bitmap | (0x01ff << j);
3494                        break;
3495                case 0:
3496                        rate_bitmap = rate_bitmap | (0x00ff << j);
3497                        break;
3498                default:
3499                        break;
3500                }
3501        }
3502
3503        return rate_bitmap;
3504}
3505
3506static u32 _rtl8821ae_set_ra_vht_ratr_bitmap(struct ieee80211_hw *hw,
3507                                             enum wireless_mode wirelessmode,
3508                                             u32 ratr_bitmap)
3509{
3510        struct rtl_priv *rtlpriv = rtl_priv(hw);
3511        struct rtl_phy *rtlphy = &rtlpriv->phy;
3512        u32 ret_bitmap = ratr_bitmap;
3513
3514        if (rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_20_40
3515                || rtlphy->current_chan_bw == HT_CHANNEL_WIDTH_80)
3516                ret_bitmap = ratr_bitmap;
3517        else if (wirelessmode == WIRELESS_MODE_AC_5G
3518                || wirelessmode == WIRELESS_MODE_AC_24G) {
3519                if (rtlphy->rf_type == RF_1T1R)
3520                        ret_bitmap = ratr_bitmap & (~BIT21);
3521                else
3522                        ret_bitmap = ratr_bitmap & (~(BIT31|BIT21));
3523        }
3524
3525        return ret_bitmap;
3526}
3527
3528static u8 _rtl8821ae_get_vht_eni(enum wireless_mode wirelessmode,
3529                        u32 ratr_bitmap)
3530{
3531        u8 ret = 0;
3532        if (wirelessmode < WIRELESS_MODE_N_24G)
3533                ret =  0;
3534        else if (wirelessmode == WIRELESS_MODE_AC_24G) {
3535                if (ratr_bitmap & 0xfff00000)   /* Mix , 2SS */
3536                        ret = 3;
3537                else                                    /* Mix, 1SS */
3538                        ret = 2;
3539        } else if (wirelessmode == WIRELESS_MODE_AC_5G) {
3540                        ret = 1;
3541        } /* VHT */
3542
3543        return ret << 4;
3544}
3545
3546static u8 _rtl8821ae_get_ra_ldpc(struct ieee80211_hw *hw,
3547                             u8 mac_id, struct rtl_sta_info *sta_entry,
3548                             enum wireless_mode wirelessmode)
3549{
3550        u8 b_ldpc = 0;
3551        /*not support ldpc, do not open*/
3552        return b_ldpc << 2;
3553}
3554
3555static u8 _rtl8821ae_get_ra_rftype(struct ieee80211_hw *hw,
3556                          enum wireless_mode wirelessmode,
3557                          u32 ratr_bitmap)
3558{
3559        struct rtl_priv *rtlpriv = rtl_priv(hw);
3560        struct rtl_phy *rtlphy = &rtlpriv->phy;
3561        u8 rf_type = RF_1T1R;
3562
3563        if (rtlphy->rf_type == RF_1T1R)
3564                rf_type = RF_1T1R;
3565        else if (wirelessmode == WIRELESS_MODE_AC_5G
3566                || wirelessmode == WIRELESS_MODE_AC_24G
3567                || wirelessmode == WIRELESS_MODE_AC_ONLY) {
3568                if (ratr_bitmap & 0xffc00000)
3569                        rf_type = RF_2T2R;
3570        } else if (wirelessmode == WIRELESS_MODE_N_5G
3571                || wirelessmode == WIRELESS_MODE_N_24G) {
3572                if (ratr_bitmap & 0xfff00000)
3573                        rf_type = RF_2T2R;
3574        }
3575
3576        return rf_type;
3577}
3578
3579static bool _rtl8821ae_get_ra_shortgi(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
3580                              u8 mac_id)
3581{
3582        bool b_short_gi = false;
3583        u8 b_curshortgi_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
3584                                1 : 0;
3585        u8 b_curshortgi_20mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
3586                                1 : 0;
3587        u8 b_curshortgi_80mhz = 0;
3588        b_curshortgi_80mhz = (sta->vht_cap.cap &
3589                              IEEE80211_VHT_CAP_SHORT_GI_80) ? 1 : 0;
3590
3591        if (mac_id == MAC_ID_STATIC_FOR_BROADCAST_MULTICAST)
3592                        b_short_gi = false;
3593
3594        if (b_curshortgi_40mhz || b_curshortgi_80mhz
3595                || b_curshortgi_20mhz)
3596                b_short_gi = true;
3597
3598        return b_short_gi;
3599}
3600
3601static void rtl8821ae_update_hal_rate_mask(struct ieee80211_hw *hw,
3602                struct ieee80211_sta *sta, u8 rssi_level)
3603{
3604        struct rtl_priv *rtlpriv = rtl_priv(hw);
3605        struct rtl_phy *rtlphy = &rtlpriv->phy;
3606        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
3607        struct rtl_sta_info *sta_entry = NULL;
3608        u32 ratr_bitmap;
3609        u8 ratr_index;
3610        enum wireless_mode wirelessmode = 0;
3611        u8 curtxbw_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
3612                                ? 1 : 0;
3613        bool b_shortgi = false;
3614        u8 rate_mask[7];
3615        u8 macid = 0;
3616        u8 mimo_ps = IEEE80211_SMPS_OFF;
3617        u8 rf_type;
3618
3619        sta_entry = (struct rtl_sta_info *)sta->drv_priv;
3620        wirelessmode = sta_entry->wireless_mode;
3621
3622        RT_TRACE(rtlpriv, COMP_RATR, DBG_LOUD,
3623                 "wireless mode = 0x%x\n", wirelessmode);
3624        if (mac->opmode == NL80211_IFTYPE_STATION ||
3625                mac->opmode == NL80211_IFTYPE_MESH_POINT) {
3626                curtxbw_40mhz = mac->bw_40;
3627        } else if (mac->opmode == NL80211_IFTYPE_AP ||
3628                mac->opmode == NL80211_IFTYPE_ADHOC)
3629                macid = sta->aid + 1;
3630        if (wirelessmode == WIRELESS_MODE_N_5G ||
3631            wirelessmode == WIRELESS_MODE_AC_5G ||
3632            wirelessmode == WIRELESS_MODE_A)
3633                ratr_bitmap = sta->supp_rates[NL80211_BAND_5GHZ] << 4;
3634        else
3635                ratr_bitmap = sta->supp_rates[NL80211_BAND_2GHZ];
3636
3637        if (mac->opmode == NL80211_IFTYPE_ADHOC)
3638                ratr_bitmap = 0xfff;
3639
3640        if (wirelessmode == WIRELESS_MODE_N_24G
3641                || wirelessmode == WIRELESS_MODE_N_5G)
3642                ratr_bitmap |= (sta->ht_cap.mcs.rx_mask[1] << 20 |
3643                                sta->ht_cap.mcs.rx_mask[0] << 12);
3644        else if (wirelessmode == WIRELESS_MODE_AC_24G
3645                || wirelessmode == WIRELESS_MODE_AC_5G
3646                || wirelessmode == WIRELESS_MODE_AC_ONLY)
3647                ratr_bitmap |= _rtl8821ae_rate_to_bitmap_2ssvht(
3648                                sta->vht_cap.vht_mcs.rx_mcs_map) << 12;
3649
3650        b_shortgi = _rtl8821ae_get_ra_shortgi(hw, sta, macid);
3651        rf_type = _rtl8821ae_get_ra_rftype(hw, wirelessmode, ratr_bitmap);
3652
3653/*mac id owner*/
3654        switch (wirelessmode) {
3655        case WIRELESS_MODE_B:
3656                ratr_index = RATR_INX_WIRELESS_B;
3657                if (ratr_bitmap & 0x0000000c)
3658                        ratr_bitmap &= 0x0000000d;
3659                else
3660                        ratr_bitmap &= 0x0000000f;
3661                break;
3662        case WIRELESS_MODE_G:
3663                ratr_index = RATR_INX_WIRELESS_GB;
3664
3665                if (rssi_level == 1)
3666                        ratr_bitmap &= 0x00000f00;
3667                else if (rssi_level == 2)
3668                        ratr_bitmap &= 0x00000ff0;
3669                else
3670                        ratr_bitmap &= 0x00000ff5;
3671                break;
3672        case WIRELESS_MODE_A:
3673                ratr_index = RATR_INX_WIRELESS_G;
3674                ratr_bitmap &= 0x00000ff0;
3675                break;
3676        case WIRELESS_MODE_N_24G:
3677        case WIRELESS_MODE_N_5G:
3678                if (wirelessmode == WIRELESS_MODE_N_24G)
3679                        ratr_index = RATR_INX_WIRELESS_NGB;
3680                else
3681                        ratr_index = RATR_INX_WIRELESS_NG;
3682
3683                if (mimo_ps == IEEE80211_SMPS_STATIC
3684                        || mimo_ps == IEEE80211_SMPS_DYNAMIC) {
3685                        if (rssi_level == 1)
3686                                ratr_bitmap &= 0x000f0000;
3687                        else if (rssi_level == 2)
3688                                ratr_bitmap &= 0x000ff000;
3689                        else
3690                                ratr_bitmap &= 0x000ff005;
3691                } else {
3692                        if (rf_type == RF_1T1R) {
3693                                if (curtxbw_40mhz) {
3694                                        if (rssi_level == 1)
3695                                                ratr_bitmap &= 0x000f0000;
3696                                        else if (rssi_level == 2)
3697                                                ratr_bitmap &= 0x000ff000;
3698                                        else
3699                                                ratr_bitmap &= 0x000ff015;
3700                                } else {
3701                                        if (rssi_level == 1)
3702                                                ratr_bitmap &= 0x000f0000;
3703                                        else if (rssi_level == 2)
3704                                                ratr_bitmap &= 0x000ff000;
3705                                        else
3706                                                ratr_bitmap &= 0x000ff005;
3707                                }
3708                        } else {
3709                                if (curtxbw_40mhz) {
3710                                        if (rssi_level == 1)
3711                                                ratr_bitmap &= 0x0fff0000;
3712                                        else if (rssi_level == 2)
3713                                                ratr_bitmap &= 0x0ffff000;
3714                                        else
3715                                                ratr_bitmap &= 0x0ffff015;
3716                                } else {
3717                                        if (rssi_level == 1)
3718                                                ratr_bitmap &= 0x0fff0000;
3719                                        else if (rssi_level == 2)
3720                                                ratr_bitmap &= 0x0ffff000;
3721                                        else
3722                                                ratr_bitmap &= 0x0ffff005;
3723                                }
3724                        }
3725                }
3726                break;
3727
3728        case WIRELESS_MODE_AC_24G:
3729                ratr_index = RATR_INX_WIRELESS_AC_24N;
3730                if (rssi_level == 1)
3731                        ratr_bitmap &= 0xfc3f0000;
3732                else if (rssi_level == 2)
3733                        ratr_bitmap &= 0xfffff000;
3734                else
3735                        ratr_bitmap &= 0xffffffff;
3736                break;
3737
3738        case WIRELESS_MODE_AC_5G:
3739                ratr_index = RATR_INX_WIRELESS_AC_5N;
3740
3741                if (rf_type == RF_1T1R) {
3742                        if (rssi_level == 1)    /*add by Gary for ac-series*/
3743                                ratr_bitmap &= 0x003f8000;
3744                        else if (rssi_level == 2)
3745                                ratr_bitmap &= 0x003ff000;
3746                        else
3747                                ratr_bitmap &= 0x003ff010;
3748                } else {
3749                        if (rssi_level == 1)
3750                                ratr_bitmap &= 0xfe3f8000;
3751                        else if (rssi_level == 2)
3752                                ratr_bitmap &= 0xfffff000;
3753                        else
3754                                ratr_bitmap &= 0xfffff010;
3755                }
3756                break;
3757
3758        default:
3759                ratr_index = RATR_INX_WIRELESS_NGB;
3760
3761                if (rf_type == RF_1T2R)
3762                        ratr_bitmap &= 0x000ff0ff;
3763                else
3764                        ratr_bitmap &= 0x0f8ff0ff;
3765                break;
3766        }
3767
3768        ratr_index = _rtl8821ae_mrate_idx_to_arfr_id(hw, ratr_index, wirelessmode);
3769        sta_entry->ratr_index = ratr_index;
3770        ratr_bitmap = _rtl8821ae_set_ra_vht_ratr_bitmap(hw, wirelessmode,
3771                                                        ratr_bitmap);
3772
3773        RT_TRACE(rtlpriv, COMP_RATR, DBG_LOUD,
3774                 "ratr_bitmap :%x\n", ratr_bitmap);
3775
3776        /* *(u32 *)& rate_mask = EF4BYTE((ratr_bitmap & 0x0fffffff) |
3777                                       (ratr_index << 28)); */
3778
3779        rate_mask[0] = macid;
3780        rate_mask[1] = ratr_index | (b_shortgi ? 0x80 : 0x00);
3781        rate_mask[2] = rtlphy->current_chan_bw
3782                           | _rtl8821ae_get_vht_eni(wirelessmode, ratr_bitmap)
3783                           | _rtl8821ae_get_ra_ldpc(hw, macid, sta_entry, wirelessmode);
3784
3785        rate_mask[3] = (u8)(ratr_bitmap & 0x000000ff);
3786        rate_mask[4] = (u8)((ratr_bitmap & 0x0000ff00) >> 8);
3787        rate_mask[5] = (u8)((ratr_bitmap & 0x00ff0000) >> 16);
3788        rate_mask[6] = (u8)((ratr_bitmap & 0xff000000) >> 24);
3789
3790        RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
3791                 "Rate_index:%x, ratr_val:%x, %x:%x:%x:%x:%x:%x:%x\n",
3792                 ratr_index, ratr_bitmap,
3793                 rate_mask[0], rate_mask[1],
3794                 rate_mask[2], rate_mask[3],
3795                 rate_mask[4], rate_mask[5],
3796                 rate_mask[6]);
3797        rtl8821ae_fill_h2c_cmd(hw, H2C_8821AE_RA_MASK, 7, rate_mask);
3798        _rtl8821ae_set_bcn_ctrl_reg(hw, BIT(3), 0);
3799}
3800
3801void rtl8821ae_update_hal_rate_tbl(struct ieee80211_hw *hw,
3802                struct ieee80211_sta *sta, u8 rssi_level)
3803{
3804        struct rtl_priv *rtlpriv = rtl_priv(hw);
3805        if (rtlpriv->dm.useramask)
3806                rtl8821ae_update_hal_rate_mask(hw, sta, rssi_level);
3807        else
3808                /*RT_TRACE(rtlpriv, COMP_RATR,DBG_LOUD,
3809                           "rtl8821ae_update_hal_rate_tbl() Error! 8821ae FW RA Only\n");*/
3810                rtl8821ae_update_hal_rate_table(hw, sta);
3811}
3812
3813void rtl8821ae_update_channel_access_setting(struct ieee80211_hw *hw)
3814{
3815        struct rtl_priv *rtlpriv = rtl_priv(hw);
3816        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
3817        u16 wireless_mode = mac->mode;
3818        u8 sifs_timer, r2t_sifs;
3819
3820        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME,
3821                                      (u8 *)&mac->slot_time);
3822        if (wireless_mode == WIRELESS_MODE_G)
3823                sifs_timer = 0x0a;
3824        else
3825                sifs_timer = 0x0e;
3826        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SIFS, (u8 *)&sifs_timer);
3827
3828        r2t_sifs = 0xa;
3829
3830        if (wireless_mode == WIRELESS_MODE_AC_5G &&
3831            (mac->vht_ldpc_cap & LDPC_VHT_ENABLE_RX) &&
3832            (mac->vht_stbc_cap & STBC_VHT_ENABLE_RX)) {
3833                if (mac->vendor == PEER_ATH)
3834                        r2t_sifs = 0x8;
3835                else
3836                        r2t_sifs = 0xa;
3837        } else if (wireless_mode == WIRELESS_MODE_AC_5G) {
3838                r2t_sifs = 0xa;
3839        }
3840
3841        rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_R2T_SIFS, (u8 *)&r2t_sifs);
3842}
3843
3844bool rtl8821ae_gpio_radio_on_off_checking(struct ieee80211_hw *hw, u8 *valid)
3845{
3846        struct rtl_priv *rtlpriv = rtl_priv(hw);
3847        struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
3848        struct rtl_phy *rtlphy = &rtlpriv->phy;
3849        enum rf_pwrstate e_rfpowerstate_toset, cur_rfstate;
3850        u8 u1tmp = 0;
3851        bool b_actuallyset = false;
3852
3853        if (rtlpriv->rtlhal.being_init_adapter)
3854                return false;
3855
3856        if (ppsc->swrf_processing)
3857                return false;
3858
3859        spin_lock(&rtlpriv->locks.rf_ps_lock);
3860        if (ppsc->rfchange_inprogress) {
3861                spin_unlock(&rtlpriv->locks.rf_ps_lock);
3862                return false;
3863        } else {
3864                ppsc->rfchange_inprogress = true;
3865                spin_unlock(&rtlpriv->locks.rf_ps_lock);
3866        }
3867
3868        cur_rfstate = ppsc->rfpwr_state;
3869
3870        rtl_write_byte(rtlpriv, REG_GPIO_IO_SEL_2,
3871                        rtl_read_byte(rtlpriv,
3872                                        REG_GPIO_IO_SEL_2) & ~(BIT(1)));
3873
3874        u1tmp = rtl_read_byte(rtlpriv, REG_GPIO_PIN_CTRL_2);
3875
3876        if (rtlphy->polarity_ctl)
3877                e_rfpowerstate_toset = (u1tmp & BIT(1)) ? ERFOFF : ERFON;
3878        else
3879                e_rfpowerstate_toset = (u1tmp & BIT(1)) ? ERFON : ERFOFF;
3880
3881        if ((ppsc->hwradiooff) && (e_rfpowerstate_toset == ERFON)) {
3882                RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
3883                         "GPIOChangeRF  - HW Radio ON, RF ON\n");
3884
3885                e_rfpowerstate_toset = ERFON;
3886                ppsc->hwradiooff = false;
3887                b_actuallyset = true;
3888        } else if ((!ppsc->hwradiooff)
3889                   && (e_rfpowerstate_toset == ERFOFF)) {
3890                RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
3891                         "GPIOChangeRF  - HW Radio OFF, RF OFF\n");
3892
3893                e_rfpowerstate_toset = ERFOFF;
3894                ppsc->hwradiooff = true;
3895                b_actuallyset = true;
3896        }
3897
3898        if (b_actuallyset) {
3899                spin_lock(&rtlpriv->locks.rf_ps_lock);
3900                ppsc->rfchange_inprogress = false;
3901                spin_unlock(&rtlpriv->locks.rf_ps_lock);
3902        } else {
3903                if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC)
3904                        RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
3905
3906                spin_lock(&rtlpriv->locks.rf_ps_lock);
3907                ppsc->rfchange_inprogress = false;
3908                spin_unlock(&rtlpriv->locks.rf_ps_lock);
3909        }
3910
3911        *valid = 1;
3912        return !ppsc->hwradiooff;
3913}
3914
3915void rtl8821ae_set_key(struct ieee80211_hw *hw, u32 key_index,
3916                     u8 *p_macaddr, bool is_group, u8 enc_algo,
3917                     bool is_wepkey, bool clear_all)
3918{
3919        struct rtl_priv *rtlpriv = rtl_priv(hw);
3920        struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
3921        struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
3922        u8 *macaddr = p_macaddr;
3923        u32 entry_id = 0;
3924        bool is_pairwise = false;
3925
3926        static u8 cam_const_addr[4][6] = {
3927                {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
3928                {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
3929                {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
3930                {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
3931        };
3932        static u8 cam_const_broad[] = {
3933                0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3934        };
3935
3936        if (clear_all) {
3937                u8 idx = 0;
3938                u8 cam_offset = 0;
3939                u8 clear_number = 5;
3940
3941                RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "clear_all\n");
3942
3943                for (idx = 0; idx < clear_number; idx++) {
3944                        rtl_cam_mark_invalid(hw, cam_offset + idx);
3945                        rtl_cam_empty_entry(hw, cam_offset + idx);
3946
3947                        if (idx < 5) {
3948                                memset(rtlpriv->sec.key_buf[idx], 0,
3949                                       MAX_KEY_LEN);
3950                                rtlpriv->sec.key_len[idx] = 0;
3951                        }
3952                }
3953        } else {
3954                switch (enc_algo) {
3955                case WEP40_ENCRYPTION:
3956                        enc_algo = CAM_WEP40;
3957                        break;
3958                case WEP104_ENCRYPTION:
3959                        enc_algo = CAM_WEP104;
3960                        break;
3961                case TKIP_ENCRYPTION:
3962                        enc_algo = CAM_TKIP;
3963                        break;
3964                case AESCCMP_ENCRYPTION:
3965                        enc_algo = CAM_AES;
3966                        break;
3967                default:
3968                        RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
3969                                 "switch case %#x not processed\n", enc_algo);
3970                        enc_algo = CAM_TKIP;
3971                        break;
3972                }
3973
3974                if (is_wepkey || rtlpriv->sec.use_defaultkey) {
3975                        macaddr = cam_const_addr[key_index];
3976                        entry_id = key_index;
3977                } else {
3978                        if (is_group) {
3979                                macaddr = cam_const_broad;
3980                                entry_id = key_index;
3981                        } else {
3982                                if (mac->opmode == NL80211_IFTYPE_AP) {
3983                                        entry_id = rtl_cam_get_free_entry(hw, p_macaddr);
3984                                        if (entry_id >=  TOTAL_CAM_ENTRY) {
3985                                                pr_err("an not find free hwsecurity cam entry\n");
3986                                                return;
3987                                        }
3988                                } else {
3989                                        entry_id = CAM_PAIRWISE_KEY_POSITION;
3990                                }
3991
3992                                key_index = PAIRWISE_KEYIDX;
3993                                is_pairwise = true;
3994                        }
3995                }
3996
3997                if (rtlpriv->sec.key_len[key_index] == 0) {
3998                        RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
3999                                 "delete one entry, entry_id is %d\n",
4000                                 entry_id);
4001                        if (mac->opmode == NL80211_IFTYPE_AP)
4002                                rtl_cam_del_entry(hw, p_macaddr);
4003                        rtl_cam_delete_one_entry(hw, p_macaddr, entry_id);
4004                } else {
4005                        RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
4006                                 "add one entry\n");
4007                        if (is_pairwise) {
4008                                RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
4009                                         "set Pairwise key\n");
4010
4011                                rtl_cam_add_one_entry(hw, macaddr, key_index,
4012                                                      entry_id, enc_algo,
4013                                                      CAM_CONFIG_NO_USEDK,
4014                                                      rtlpriv->sec.key_buf[key_index]);
4015                        } else {
4016                                RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
4017                                         "set group key\n");
4018
4019                                if (mac->opmode == NL80211_IFTYPE_ADHOC) {
4020                                        rtl_cam_add_one_entry(hw,
4021                                                        rtlefuse->dev_addr,
4022                                                        PAIRWISE_KEYIDX,
4023                                                        CAM_PAIRWISE_KEY_POSITION,
4024                                                        enc_algo,
4025                                                        CAM_CONFIG_NO_USEDK,
4026                                                        rtlpriv->sec.key_buf
4027                                                        [entry_id]);
4028                                }
4029
4030                                rtl_cam_add_one_entry(hw, macaddr, key_index,
4031                                                entry_id, enc_algo,
4032                                                CAM_CONFIG_NO_USEDK,
4033                                                rtlpriv->sec.key_buf[entry_id]);
4034                        }
4035                }
4036        }
4037}
4038
4039void rtl8821ae_bt_reg_init(struct ieee80211_hw *hw)
4040{
4041        struct rtl_priv *rtlpriv = rtl_priv(hw);
4042
4043        /* 0:Low, 1:High, 2:From Efuse. */
4044        rtlpriv->btcoexist.reg_bt_iso = 2;
4045        /* 0:Idle, 1:None-SCO, 2:SCO, 3:From Counter. */
4046        rtlpriv->btcoexist.reg_bt_sco = 3;
4047        /* 0:Disable BT control A-MPDU, 1:Enable BT control A-MPDU. */
4048        rtlpriv->btcoexist.reg_bt_sco = 0;
4049}
4050
4051void rtl8821ae_bt_hw_init(struct ieee80211_hw *hw)
4052{
4053        struct rtl_priv *rtlpriv = rtl_priv(hw);
4054
4055        if (rtlpriv->cfg->ops->get_btc_status())
4056                rtlpriv->btcoexist.btc_ops->btc_init_hw_config(rtlpriv);
4057}
4058
4059void rtl8821ae_suspend(struct ieee80211_hw *hw)
4060{
4061}
4062
4063void rtl8821ae_resume(struct ieee80211_hw *hw)
4064{
4065}
4066
4067/* Turn on AAP (RCR:bit 0) for promicuous mode. */
4068void rtl8821ae_allow_all_destaddr(struct ieee80211_hw *hw,
4069        bool allow_all_da, bool write_into_reg)
4070{
4071        struct rtl_priv *rtlpriv = rtl_priv(hw);
4072        struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
4073
4074        if (allow_all_da) /* Set BIT0 */
4075                rtlpci->receive_config |= RCR_AAP;
4076        else /* Clear BIT0 */
4077                rtlpci->receive_config &= ~RCR_AAP;
4078
4079        if (write_into_reg)
4080                rtl_write_dword(rtlpriv, REG_RCR, rtlpci->receive_config);
4081
4082        RT_TRACE(rtlpriv, COMP_TURBO | COMP_INIT, DBG_LOUD,
4083                "receive_config=0x%08X, write_into_reg=%d\n",
4084                rtlpci->receive_config, write_into_reg);
4085}
4086
4087/* WKFMCAMAddAllEntry8812 */
4088void rtl8821ae_add_wowlan_pattern(struct ieee80211_hw *hw,
4089                                  struct rtl_wow_pattern *rtl_pattern,
4090                                  u8 index)
4091{
4092        struct rtl_priv *rtlpriv = rtl_priv(hw);
4093        u32 cam = 0;
4094        u8 addr = 0;
4095        u16 rxbuf_addr;
4096        u8 tmp, count = 0;
4097        u16 cam_start;
4098        u16 offset;
4099
4100        /* Count the WFCAM entry start offset. */
4101
4102        /* RX page size = 128 byte */
4103        offset = MAX_RX_DMA_BUFFER_SIZE_8812 / 128;
4104        /* We should start from the boundry */
4105        cam_start = offset * 128;
4106
4107        /* Enable Rx packet buffer access. */
4108        rtl_write_byte(rtlpriv, REG_PKT_BUFF_ACCESS_CTRL, RXPKT_BUF_SELECT);
4109        for (addr = 0; addr < WKFMCAM_ADDR_NUM; addr++) {
4110                /* Set Rx packet buffer offset.
4111                 * RxBufer pointer increases 1,
4112                 * we can access 8 bytes in Rx packet buffer.
4113                 * CAM start offset (unit: 1 byte) =  index*WKFMCAM_SIZE
4114                 * RxBufer addr = (CAM start offset +
4115                 *                 per entry offset of a WKFM CAM)/8
4116                 *      * index: The index of the wake up frame mask
4117                 *      * WKFMCAM_SIZE: the total size of one WKFM CAM
4118                 *      * per entry offset of a WKFM CAM: Addr*4 bytes
4119                 */
4120                rxbuf_addr = (cam_start + index * WKFMCAM_SIZE + addr * 4) >> 3;
4121                /* Set R/W start offset */
4122                rtl_write_word(rtlpriv, REG_PKTBUF_DBG_CTRL, rxbuf_addr);
4123
4124                if (addr == 0) {
4125                        cam = BIT(31) | rtl_pattern->crc;
4126
4127                        if (rtl_pattern->type == UNICAST_PATTERN)
4128                                cam |= BIT(24);
4129                        else if (rtl_pattern->type == MULTICAST_PATTERN)
4130                                cam |= BIT(25);
4131                        else if (rtl_pattern->type == BROADCAST_PATTERN)
4132                                cam |= BIT(26);
4133
4134                        rtl_write_dword(rtlpriv, REG_PKTBUF_DBG_DATA_L, cam);
4135                        RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
4136                                 "WRITE entry[%d] 0x%x: %x\n", addr,
4137                                  REG_PKTBUF_DBG_DATA_L, cam);
4138
4139                        /* Write to Rx packet buffer. */
4140                        rtl_write_word(rtlpriv, REG_RXPKTBUF_CTRL, 0x0f01);
4141                } else if (addr == 2 || addr == 4) {/* WKFM[127:0] */
4142                        cam = rtl_pattern->mask[addr - 2];
4143
4144                        rtl_write_dword(rtlpriv, REG_PKTBUF_DBG_DATA_L, cam);
4145                        RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
4146                                 "WRITE entry[%d] 0x%x: %x\n", addr,
4147                                  REG_PKTBUF_DBG_DATA_L, cam);
4148
4149                        rtl_write_word(rtlpriv, REG_RXPKTBUF_CTRL, 0x0f01);
4150                } else if (addr == 3 || addr == 5) {/* WKFM[127:0] */
4151                        cam = rtl_pattern->mask[addr - 2];
4152
4153                        rtl_write_dword(rtlpriv, REG_PKTBUF_DBG_DATA_H, cam);
4154                        RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
4155                                 "WRITE entry[%d] 0x%x: %x\n", addr,
4156                                  REG_PKTBUF_DBG_DATA_H, cam);
4157
4158                        rtl_write_word(rtlpriv, REG_RXPKTBUF_CTRL, 0xf001);
4159                }
4160
4161                count = 0;
4162                do {
4163                        tmp = rtl_read_byte(rtlpriv, REG_RXPKTBUF_CTRL);
4164                        udelay(2);
4165                        count++;
4166                } while (tmp && count < 100);
4167
4168                WARN_ONCE((count >= 100),
4169                          "rtl8821ae: Write wake up frame mask FAIL %d value!\n",
4170                          tmp);
4171        }
4172        /* Disable Rx packet buffer access. */
4173        rtl_write_byte(rtlpriv, REG_PKT_BUFF_ACCESS_CTRL,
4174                       DISABLE_TRXPKT_BUF_ACCESS);
4175}
4176