Linux Audio

Check our new training course

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