xref: /linux/drivers/net/wireless/realtek/rtlwifi/rtl8192cu/mac.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
1b44febedSLarry Finger // SPDX-License-Identifier: GPL-2.0
2b44febedSLarry Finger /* Copyright(c) 2009-2012  Realtek Corporation.*/
3f1d2b4d3SLarry Finger 
4f1d2b4d3SLarry Finger #include "../wifi.h"
5f1d2b4d3SLarry Finger #include "../pci.h"
6f1d2b4d3SLarry Finger #include "../usb.h"
7f1d2b4d3SLarry Finger #include "../ps.h"
8f1d2b4d3SLarry Finger #include "../cam.h"
9f1d2b4d3SLarry Finger #include "../stats.h"
10f1d2b4d3SLarry Finger #include "reg.h"
11f1d2b4d3SLarry Finger #include "def.h"
12f1d2b4d3SLarry Finger #include "phy.h"
13f1d2b4d3SLarry Finger #include "rf.h"
14f1d2b4d3SLarry Finger #include "dm.h"
15f1d2b4d3SLarry Finger #include "mac.h"
16f1d2b4d3SLarry Finger #include "trx.h"
17f1d2b4d3SLarry Finger #include "../rtl8192c/fw_common.h"
18f1d2b4d3SLarry Finger 
19f1d2b4d3SLarry Finger #include <linux/module.h>
20f1d2b4d3SLarry Finger 
21f1d2b4d3SLarry Finger /* macro to shorten lines */
22f1d2b4d3SLarry Finger 
23f1d2b4d3SLarry Finger #define LINK_Q	ui_link_quality
24f1d2b4d3SLarry Finger #define RX_EVM	rx_evm_percentage
25f1d2b4d3SLarry Finger #define RX_SIGQ	rx_mimo_sig_qual
26f1d2b4d3SLarry Finger 
rtl92c_read_chip_version(struct ieee80211_hw * hw)27f1d2b4d3SLarry Finger void rtl92c_read_chip_version(struct ieee80211_hw *hw)
28f1d2b4d3SLarry Finger {
29f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
30f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
31f1d2b4d3SLarry Finger 	struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
32f1d2b4d3SLarry Finger 	enum version_8192c chip_version = VERSION_UNKNOWN;
33f1d2b4d3SLarry Finger 	const char *versionid;
34f1d2b4d3SLarry Finger 	u32 value32;
35f1d2b4d3SLarry Finger 
36f1d2b4d3SLarry Finger 	value32 = rtl_read_dword(rtlpriv, REG_SYS_CFG);
37f1d2b4d3SLarry Finger 	if (value32 & TRP_VAUX_EN) {
38f1d2b4d3SLarry Finger 		chip_version = (value32 & TYPE_ID) ? VERSION_TEST_CHIP_92C :
39f1d2b4d3SLarry Finger 			       VERSION_TEST_CHIP_88C;
40f1d2b4d3SLarry Finger 	} else {
41f1d2b4d3SLarry Finger 		/* Normal mass production chip. */
42f1d2b4d3SLarry Finger 		chip_version = NORMAL_CHIP;
43f1d2b4d3SLarry Finger 		chip_version |= ((value32 & TYPE_ID) ? CHIP_92C : 0);
44f1d2b4d3SLarry Finger 		chip_version |= ((value32 & VENDOR_ID) ? CHIP_VENDOR_UMC : 0);
45f1d2b4d3SLarry Finger 		if (IS_VENDOR_UMC(chip_version))
46f1d2b4d3SLarry Finger 			chip_version |= ((value32 & CHIP_VER_RTL_MASK) ?
47f1d2b4d3SLarry Finger 					 CHIP_VENDOR_UMC_B_CUT : 0);
48f1d2b4d3SLarry Finger 		if (IS_92C_SERIAL(chip_version)) {
49f1d2b4d3SLarry Finger 			value32 = rtl_read_dword(rtlpriv, REG_HPON_FSM);
50f1d2b4d3SLarry Finger 			chip_version |= ((CHIP_BONDING_IDENTIFIER(value32) ==
51f1d2b4d3SLarry Finger 				 CHIP_BONDING_92C_1T2R) ? CHIP_92C_1T2R : 0);
52f1d2b4d3SLarry Finger 		}
53f1d2b4d3SLarry Finger 	}
54f1d2b4d3SLarry Finger 	rtlhal->version  = (enum version_8192c)chip_version;
55f1d2b4d3SLarry Finger 	pr_info("Chip version 0x%x\n", chip_version);
56f1d2b4d3SLarry Finger 	switch (rtlhal->version) {
57f1d2b4d3SLarry Finger 	case VERSION_NORMAL_TSMC_CHIP_92C_1T2R:
58f1d2b4d3SLarry Finger 		versionid = "NORMAL_B_CHIP_92C";
59f1d2b4d3SLarry Finger 		break;
60f1d2b4d3SLarry Finger 	case VERSION_NORMAL_TSMC_CHIP_92C:
61f1d2b4d3SLarry Finger 		versionid = "NORMAL_TSMC_CHIP_92C";
62f1d2b4d3SLarry Finger 		break;
63f1d2b4d3SLarry Finger 	case VERSION_NORMAL_TSMC_CHIP_88C:
64f1d2b4d3SLarry Finger 		versionid = "NORMAL_TSMC_CHIP_88C";
65f1d2b4d3SLarry Finger 		break;
66f1d2b4d3SLarry Finger 	case VERSION_NORMAL_UMC_CHIP_92C_1T2R_A_CUT:
67f1d2b4d3SLarry Finger 		versionid = "NORMAL_UMC_CHIP_i92C_1T2R_A_CUT";
68f1d2b4d3SLarry Finger 		break;
69f1d2b4d3SLarry Finger 	case VERSION_NORMAL_UMC_CHIP_92C_A_CUT:
70f1d2b4d3SLarry Finger 		versionid = "NORMAL_UMC_CHIP_92C_A_CUT";
71f1d2b4d3SLarry Finger 		break;
72f1d2b4d3SLarry Finger 	case VERSION_NORMAL_UMC_CHIP_88C_A_CUT:
73f1d2b4d3SLarry Finger 		versionid = "NORMAL_UMC_CHIP_88C_A_CUT";
74f1d2b4d3SLarry Finger 		break;
75f1d2b4d3SLarry Finger 	case VERSION_NORMAL_UMC_CHIP_92C_1T2R_B_CUT:
76f1d2b4d3SLarry Finger 		versionid = "NORMAL_UMC_CHIP_92C_1T2R_B_CUT";
77f1d2b4d3SLarry Finger 		break;
78f1d2b4d3SLarry Finger 	case VERSION_NORMAL_UMC_CHIP_92C_B_CUT:
79f1d2b4d3SLarry Finger 		versionid = "NORMAL_UMC_CHIP_92C_B_CUT";
80f1d2b4d3SLarry Finger 		break;
81f1d2b4d3SLarry Finger 	case VERSION_NORMAL_UMC_CHIP_88C_B_CUT:
82f1d2b4d3SLarry Finger 		versionid = "NORMAL_UMC_CHIP_88C_B_CUT";
83f1d2b4d3SLarry Finger 		break;
84f1d2b4d3SLarry Finger 	case VERSION_TEST_CHIP_92C:
85f1d2b4d3SLarry Finger 		versionid = "TEST_CHIP_92C";
86f1d2b4d3SLarry Finger 		break;
87f1d2b4d3SLarry Finger 	case VERSION_TEST_CHIP_88C:
88f1d2b4d3SLarry Finger 		versionid = "TEST_CHIP_88C";
89f1d2b4d3SLarry Finger 		break;
90f1d2b4d3SLarry Finger 	default:
91f1d2b4d3SLarry Finger 		versionid = "UNKNOWN";
92f1d2b4d3SLarry Finger 		break;
93f1d2b4d3SLarry Finger 	}
9434d7f007SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_TRACE,
95f1d2b4d3SLarry Finger 		"Chip Version ID: %s\n", versionid);
96f1d2b4d3SLarry Finger 
97f1d2b4d3SLarry Finger 	if (IS_92C_SERIAL(rtlhal->version))
98f1d2b4d3SLarry Finger 		rtlphy->rf_type =
99f1d2b4d3SLarry Finger 			 (IS_92C_1T2R(rtlhal->version)) ? RF_1T2R : RF_2T2R;
100f1d2b4d3SLarry Finger 	else
101f1d2b4d3SLarry Finger 		rtlphy->rf_type = RF_1T1R;
10234d7f007SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD,
103f1d2b4d3SLarry Finger 		"Chip RF Type: %s\n",
104*bcfcbf23SBitterblue Smith 		rtlphy->rf_type == RF_2T2R ? "RF_2T2R" :
105*bcfcbf23SBitterblue Smith 		rtlphy->rf_type == RF_1T2R ? "RF_1T2R" : "RF_1T1R");
106f1d2b4d3SLarry Finger 	if (get_rf_type(rtlphy) == RF_1T1R)
107f1d2b4d3SLarry Finger 		rtlpriv->dm.rfpath_rxenable[0] = true;
108f1d2b4d3SLarry Finger 	else
109f1d2b4d3SLarry Finger 		rtlpriv->dm.rfpath_rxenable[0] =
110f1d2b4d3SLarry Finger 		    rtlpriv->dm.rfpath_rxenable[1] = true;
11134d7f007SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "VersionID = 0x%4x\n",
112f1d2b4d3SLarry Finger 		rtlhal->version);
113f1d2b4d3SLarry Finger }
114f1d2b4d3SLarry Finger 
115f1d2b4d3SLarry Finger /**
1169a0fb950SYang Shen  * rtl92c_llt_write - LLT table write access
117230f874eSLee Jones  * @hw: Pointer to the ieee80211_hw structure.
118f1d2b4d3SLarry Finger  * @address: LLT logical address.
119f1d2b4d3SLarry Finger  * @data: LLT data content
120f1d2b4d3SLarry Finger  *
121f1d2b4d3SLarry Finger  * Realtek hardware access function.
122f1d2b4d3SLarry Finger  *
123f1d2b4d3SLarry Finger  */
rtl92c_llt_write(struct ieee80211_hw * hw,u32 address,u32 data)124f1d2b4d3SLarry Finger bool rtl92c_llt_write(struct ieee80211_hw *hw, u32 address, u32 data)
125f1d2b4d3SLarry Finger {
126f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
127f1d2b4d3SLarry Finger 	bool status = true;
128f1d2b4d3SLarry Finger 	long count = 0;
129f1d2b4d3SLarry Finger 	u32 value = _LLT_INIT_ADDR(address) |
130f1d2b4d3SLarry Finger 	    _LLT_INIT_DATA(data) | _LLT_OP(_LLT_WRITE_ACCESS);
131f1d2b4d3SLarry Finger 
132f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_LLT_INIT, value);
133f1d2b4d3SLarry Finger 	do {
134f1d2b4d3SLarry Finger 		value = rtl_read_dword(rtlpriv, REG_LLT_INIT);
135f1d2b4d3SLarry Finger 		if (_LLT_NO_ACTIVE == _LLT_OP_VALUE(value))
136f1d2b4d3SLarry Finger 			break;
137f1d2b4d3SLarry Finger 		if (count > POLLING_LLT_THRESHOLD) {
138c38af3f0SLarry Finger 			pr_err("Failed to polling write LLT done at address %d! _LLT_OP_VALUE(%x)\n",
139f1d2b4d3SLarry Finger 			       address, _LLT_OP_VALUE(value));
140f1d2b4d3SLarry Finger 			status = false;
141f1d2b4d3SLarry Finger 			break;
142f1d2b4d3SLarry Finger 		}
143f1d2b4d3SLarry Finger 	} while (++count);
144f1d2b4d3SLarry Finger 	return status;
145f1d2b4d3SLarry Finger }
1466d255202SLarry Finger 
147f1d2b4d3SLarry Finger /**
1489a0fb950SYang Shen  * rtl92c_init_llt_table - Init LLT table
149230f874eSLee Jones  * @hw: Pointer to the ieee80211_hw structure.
150230f874eSLee Jones  * @boundary: Page boundary.
151f1d2b4d3SLarry Finger  *
152f1d2b4d3SLarry Finger  * Realtek hardware access function.
153f1d2b4d3SLarry Finger  */
rtl92c_init_llt_table(struct ieee80211_hw * hw,u32 boundary)154f1d2b4d3SLarry Finger bool rtl92c_init_llt_table(struct ieee80211_hw *hw, u32 boundary)
155f1d2b4d3SLarry Finger {
156f1d2b4d3SLarry Finger 	bool rst = true;
157f1d2b4d3SLarry Finger 	u32	i;
158f1d2b4d3SLarry Finger 
159f1d2b4d3SLarry Finger 	for (i = 0; i < (boundary - 1); i++) {
160f1d2b4d3SLarry Finger 		rst = rtl92c_llt_write(hw, i , i + 1);
16103ce8159SZheng Bin 		if (!rst) {
162f1d2b4d3SLarry Finger 			pr_err("===> %s #1 fail\n", __func__);
163f1d2b4d3SLarry Finger 			return rst;
164f1d2b4d3SLarry Finger 		}
165f1d2b4d3SLarry Finger 	}
166f1d2b4d3SLarry Finger 	/* end of list */
167f1d2b4d3SLarry Finger 	rst = rtl92c_llt_write(hw, (boundary - 1), 0xFF);
16803ce8159SZheng Bin 	if (!rst) {
169f1d2b4d3SLarry Finger 		pr_err("===> %s #2 fail\n", __func__);
170f1d2b4d3SLarry Finger 		return rst;
171f1d2b4d3SLarry Finger 	}
172f1d2b4d3SLarry Finger 	/* Make the other pages as ring buffer
173f1d2b4d3SLarry Finger 	 * This ring buffer is used as beacon buffer if we config this MAC
174f1d2b4d3SLarry Finger 	 *  as two MAC transfer.
175f1d2b4d3SLarry Finger 	 * Otherwise used as local loopback buffer.
176f1d2b4d3SLarry Finger 	 */
177f1d2b4d3SLarry Finger 	for (i = boundary; i < LLT_LAST_ENTRY_OF_TX_PKT_BUFFER; i++) {
178f1d2b4d3SLarry Finger 		rst = rtl92c_llt_write(hw, i, (i + 1));
17903ce8159SZheng Bin 		if (!rst) {
180f1d2b4d3SLarry Finger 			pr_err("===> %s #3 fail\n", __func__);
181f1d2b4d3SLarry Finger 			return rst;
182f1d2b4d3SLarry Finger 		}
183f1d2b4d3SLarry Finger 	}
184f1d2b4d3SLarry Finger 	/* Let last entry point to the start entry of ring buffer */
185f1d2b4d3SLarry Finger 	rst = rtl92c_llt_write(hw, LLT_LAST_ENTRY_OF_TX_PKT_BUFFER, boundary);
18603ce8159SZheng Bin 	if (!rst) {
187f1d2b4d3SLarry Finger 		pr_err("===> %s #4 fail\n", __func__);
188f1d2b4d3SLarry Finger 		return rst;
189f1d2b4d3SLarry Finger 	}
190f1d2b4d3SLarry Finger 	return rst;
191f1d2b4d3SLarry Finger }
1926d255202SLarry Finger 
rtl92c_set_key(struct ieee80211_hw * hw,u32 key_index,u8 * p_macaddr,bool is_group,u8 enc_algo,bool is_wepkey,bool clear_all)193f1d2b4d3SLarry Finger void rtl92c_set_key(struct ieee80211_hw *hw, u32 key_index,
194f1d2b4d3SLarry Finger 		     u8 *p_macaddr, bool is_group, u8 enc_algo,
195f1d2b4d3SLarry Finger 		     bool is_wepkey, bool clear_all)
196f1d2b4d3SLarry Finger {
197f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
198f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
199f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
200f1d2b4d3SLarry Finger 	u8 *macaddr = p_macaddr;
201f1d2b4d3SLarry Finger 	u32 entry_id = 0;
202f1d2b4d3SLarry Finger 	bool is_pairwise = false;
203f1d2b4d3SLarry Finger 	static u8 cam_const_addr[4][6] = {
204f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
205f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
206f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
207f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
208f1d2b4d3SLarry Finger 	};
209f1d2b4d3SLarry Finger 	static u8 cam_const_broad[] = {
210f1d2b4d3SLarry Finger 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
211f1d2b4d3SLarry Finger 	};
212f1d2b4d3SLarry Finger 
213f1d2b4d3SLarry Finger 	if (clear_all) {
214f1d2b4d3SLarry Finger 		u8 idx = 0;
215f1d2b4d3SLarry Finger 		u8 cam_offset = 0;
216f1d2b4d3SLarry Finger 		u8 clear_number = 5;
217f1d2b4d3SLarry Finger 
21834d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "clear_all\n");
219f1d2b4d3SLarry Finger 		for (idx = 0; idx < clear_number; idx++) {
220f1d2b4d3SLarry Finger 			rtl_cam_mark_invalid(hw, cam_offset + idx);
221f1d2b4d3SLarry Finger 			rtl_cam_empty_entry(hw, cam_offset + idx);
222f1d2b4d3SLarry Finger 			if (idx < 5) {
223f1d2b4d3SLarry Finger 				memset(rtlpriv->sec.key_buf[idx], 0,
224f1d2b4d3SLarry Finger 				       MAX_KEY_LEN);
225f1d2b4d3SLarry Finger 				rtlpriv->sec.key_len[idx] = 0;
226f1d2b4d3SLarry Finger 			}
227f1d2b4d3SLarry Finger 		}
228f1d2b4d3SLarry Finger 	} else {
229f1d2b4d3SLarry Finger 		switch (enc_algo) {
230f1d2b4d3SLarry Finger 		case WEP40_ENCRYPTION:
231f1d2b4d3SLarry Finger 			enc_algo = CAM_WEP40;
232f1d2b4d3SLarry Finger 			break;
233f1d2b4d3SLarry Finger 		case WEP104_ENCRYPTION:
234f1d2b4d3SLarry Finger 			enc_algo = CAM_WEP104;
235f1d2b4d3SLarry Finger 			break;
236f1d2b4d3SLarry Finger 		case TKIP_ENCRYPTION:
237f1d2b4d3SLarry Finger 			enc_algo = CAM_TKIP;
238f1d2b4d3SLarry Finger 			break;
239f1d2b4d3SLarry Finger 		case AESCCMP_ENCRYPTION:
240f1d2b4d3SLarry Finger 			enc_algo = CAM_AES;
241f1d2b4d3SLarry Finger 			break;
242f1d2b4d3SLarry Finger 		default:
243c38af3f0SLarry Finger 			pr_err("illegal switch case\n");
244f1d2b4d3SLarry Finger 			enc_algo = CAM_TKIP;
245f1d2b4d3SLarry Finger 			break;
246f1d2b4d3SLarry Finger 		}
247f1d2b4d3SLarry Finger 		if (is_wepkey || rtlpriv->sec.use_defaultkey) {
248f1d2b4d3SLarry Finger 			macaddr = cam_const_addr[key_index];
249f1d2b4d3SLarry Finger 			entry_id = key_index;
250f1d2b4d3SLarry Finger 		} else {
251f1d2b4d3SLarry Finger 			if (is_group) {
252f1d2b4d3SLarry Finger 				macaddr = cam_const_broad;
253f1d2b4d3SLarry Finger 				entry_id = key_index;
254f1d2b4d3SLarry Finger 			} else {
255f1d2b4d3SLarry Finger 				if (mac->opmode == NL80211_IFTYPE_AP ||
256f1d2b4d3SLarry Finger 				    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
257f1d2b4d3SLarry Finger 					entry_id = rtl_cam_get_free_entry(hw,
258f1d2b4d3SLarry Finger 								 p_macaddr);
259f1d2b4d3SLarry Finger 					if (entry_id >=  TOTAL_CAM_ENTRY) {
260c38af3f0SLarry Finger 						pr_err("Can not find free hw security cam entry\n");
261f1d2b4d3SLarry Finger 						return;
262f1d2b4d3SLarry Finger 					}
263f1d2b4d3SLarry Finger 				} else {
264f1d2b4d3SLarry Finger 					entry_id = CAM_PAIRWISE_KEY_POSITION;
265f1d2b4d3SLarry Finger 				}
266f1d2b4d3SLarry Finger 
267f1d2b4d3SLarry Finger 				key_index = PAIRWISE_KEYIDX;
268f1d2b4d3SLarry Finger 				is_pairwise = true;
269f1d2b4d3SLarry Finger 			}
270f1d2b4d3SLarry Finger 		}
271f1d2b4d3SLarry Finger 		if (rtlpriv->sec.key_len[key_index] == 0) {
27234d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
273f1d2b4d3SLarry Finger 				"delete one entry\n");
274f1d2b4d3SLarry Finger 			if (mac->opmode == NL80211_IFTYPE_AP ||
275f1d2b4d3SLarry Finger 			    mac->opmode == NL80211_IFTYPE_MESH_POINT)
276f1d2b4d3SLarry Finger 				rtl_cam_del_entry(hw, p_macaddr);
277f1d2b4d3SLarry Finger 			rtl_cam_delete_one_entry(hw, p_macaddr, entry_id);
278f1d2b4d3SLarry Finger 		} else {
27934d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_LOUD,
280f1d2b4d3SLarry Finger 				"The insert KEY length is %d\n",
281f1d2b4d3SLarry Finger 				 rtlpriv->sec.key_len[PAIRWISE_KEYIDX]);
28234d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_LOUD,
283f1d2b4d3SLarry Finger 				"The insert KEY is %x %x\n",
284f1d2b4d3SLarry Finger 				rtlpriv->sec.key_buf[0][0],
285f1d2b4d3SLarry Finger 				rtlpriv->sec.key_buf[0][1]);
28634d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
287f1d2b4d3SLarry Finger 				"add one entry\n");
288f1d2b4d3SLarry Finger 			if (is_pairwise) {
289f1d2b4d3SLarry Finger 				RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_LOUD,
290f1d2b4d3SLarry Finger 					      "Pairwise Key content",
291f1d2b4d3SLarry Finger 					      rtlpriv->sec.pairwise_key,
292f1d2b4d3SLarry Finger 					      rtlpriv->sec.
293f1d2b4d3SLarry Finger 					      key_len[PAIRWISE_KEYIDX]);
29434d7f007SLarry Finger 				rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
295f1d2b4d3SLarry Finger 					"set Pairwise key\n");
296f1d2b4d3SLarry Finger 
297f1d2b4d3SLarry Finger 				rtl_cam_add_one_entry(hw, macaddr, key_index,
298f1d2b4d3SLarry Finger 						entry_id, enc_algo,
299f1d2b4d3SLarry Finger 						CAM_CONFIG_NO_USEDK,
300f1d2b4d3SLarry Finger 						rtlpriv->sec.
301f1d2b4d3SLarry Finger 						key_buf[key_index]);
302f1d2b4d3SLarry Finger 			} else {
30334d7f007SLarry Finger 				rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
304f1d2b4d3SLarry Finger 					"set group key\n");
305f1d2b4d3SLarry Finger 				if (mac->opmode == NL80211_IFTYPE_ADHOC) {
306f1d2b4d3SLarry Finger 					rtl_cam_add_one_entry(hw,
307f1d2b4d3SLarry Finger 						rtlefuse->dev_addr,
308f1d2b4d3SLarry Finger 						PAIRWISE_KEYIDX,
309f1d2b4d3SLarry Finger 						CAM_PAIRWISE_KEY_POSITION,
310f1d2b4d3SLarry Finger 						enc_algo,
311f1d2b4d3SLarry Finger 						CAM_CONFIG_NO_USEDK,
312f1d2b4d3SLarry Finger 						rtlpriv->sec.key_buf
313f1d2b4d3SLarry Finger 						[entry_id]);
314f1d2b4d3SLarry Finger 				}
315f1d2b4d3SLarry Finger 				rtl_cam_add_one_entry(hw, macaddr, key_index,
316f1d2b4d3SLarry Finger 						entry_id, enc_algo,
317f1d2b4d3SLarry Finger 						CAM_CONFIG_NO_USEDK,
318f1d2b4d3SLarry Finger 						rtlpriv->sec.key_buf[entry_id]);
319f1d2b4d3SLarry Finger 			}
320f1d2b4d3SLarry Finger 		}
321f1d2b4d3SLarry Finger 	}
322f1d2b4d3SLarry Finger }
323f1d2b4d3SLarry Finger 
rtl92c_get_txdma_status(struct ieee80211_hw * hw)324f1d2b4d3SLarry Finger u32 rtl92c_get_txdma_status(struct ieee80211_hw *hw)
325f1d2b4d3SLarry Finger {
326f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
327f1d2b4d3SLarry Finger 
328f1d2b4d3SLarry Finger 	return rtl_read_dword(rtlpriv, REG_TXDMA_STATUS);
329f1d2b4d3SLarry Finger }
330f1d2b4d3SLarry Finger 
rtl92c_enable_interrupt(struct ieee80211_hw * hw)331f1d2b4d3SLarry Finger void rtl92c_enable_interrupt(struct ieee80211_hw *hw)
332f1d2b4d3SLarry Finger {
333f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
334f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
335f1d2b4d3SLarry Finger 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
336f1d2b4d3SLarry Finger 
33758438d9aSPing-Ke Shih 	if (IS_HARDWARE_TYPE_8192CE(rtlpriv)) {
338f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMR, rtlpci->irq_mask[0] &
339f1d2b4d3SLarry Finger 				0xFFFFFFFF);
340f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMRE, rtlpci->irq_mask[1] &
341f1d2b4d3SLarry Finger 				0xFFFFFFFF);
342f1d2b4d3SLarry Finger 	} else {
343f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMR, rtlusb->irq_mask[0] &
344f1d2b4d3SLarry Finger 				0xFFFFFFFF);
345f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMRE, rtlusb->irq_mask[1] &
346f1d2b4d3SLarry Finger 				0xFFFFFFFF);
347f1d2b4d3SLarry Finger 	}
348f1d2b4d3SLarry Finger }
349f1d2b4d3SLarry Finger 
rtl92c_init_interrupt(struct ieee80211_hw * hw)350f1d2b4d3SLarry Finger void rtl92c_init_interrupt(struct ieee80211_hw *hw)
351f1d2b4d3SLarry Finger {
352f1d2b4d3SLarry Finger 	 rtl92c_enable_interrupt(hw);
353f1d2b4d3SLarry Finger }
354f1d2b4d3SLarry Finger 
rtl92c_disable_interrupt(struct ieee80211_hw * hw)355f1d2b4d3SLarry Finger void rtl92c_disable_interrupt(struct ieee80211_hw *hw)
356f1d2b4d3SLarry Finger {
357f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
358f1d2b4d3SLarry Finger 
359f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_HIMR, IMR8190_DISABLED);
360f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_HIMRE, IMR8190_DISABLED);
361f1d2b4d3SLarry Finger }
362f1d2b4d3SLarry Finger 
rtl92c_set_qos(struct ieee80211_hw * hw,int aci)363f1d2b4d3SLarry Finger void rtl92c_set_qos(struct ieee80211_hw *hw, int aci)
364f1d2b4d3SLarry Finger {
365f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
366f1d2b4d3SLarry Finger 
367f1d2b4d3SLarry Finger 	rtl92c_dm_init_edca_turbo(hw);
368f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM, (u8 *)&aci);
369f1d2b4d3SLarry Finger }
370f1d2b4d3SLarry Finger 
rtl92c_init_driver_info_size(struct ieee80211_hw * hw,u8 size)371f1d2b4d3SLarry Finger void rtl92c_init_driver_info_size(struct ieee80211_hw *hw, u8 size)
372f1d2b4d3SLarry Finger {
373f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
3746d255202SLarry Finger 
375f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_RX_DRVINFO_SZ, size);
376f1d2b4d3SLarry Finger }
377f1d2b4d3SLarry Finger 
rtl92c_set_network_type(struct ieee80211_hw * hw,enum nl80211_iftype type)378f1d2b4d3SLarry Finger int rtl92c_set_network_type(struct ieee80211_hw *hw, enum nl80211_iftype type)
379f1d2b4d3SLarry Finger {
380f1d2b4d3SLarry Finger 	u8 value;
381f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
382f1d2b4d3SLarry Finger 
383f1d2b4d3SLarry Finger 	switch (type) {
384f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_UNSPECIFIED:
385f1d2b4d3SLarry Finger 		value = NT_NO_LINK;
38634d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
387f1d2b4d3SLarry Finger 			"Set Network type to NO LINK!\n");
388f1d2b4d3SLarry Finger 		break;
389f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_ADHOC:
390f1d2b4d3SLarry Finger 		value = NT_LINK_AD_HOC;
39134d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
392f1d2b4d3SLarry Finger 			"Set Network type to Ad Hoc!\n");
393f1d2b4d3SLarry Finger 		break;
394f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_STATION:
395f1d2b4d3SLarry Finger 		value = NT_LINK_AP;
39634d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
397f1d2b4d3SLarry Finger 			"Set Network type to STA!\n");
398f1d2b4d3SLarry Finger 		break;
399f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_AP:
400f1d2b4d3SLarry Finger 		value = NT_AS_AP;
40134d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
402f1d2b4d3SLarry Finger 			"Set Network type to AP!\n");
403f1d2b4d3SLarry Finger 		break;
404f1d2b4d3SLarry Finger 	default:
40534d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
406f1d2b4d3SLarry Finger 			"Network type %d not supported!\n", type);
407f1d2b4d3SLarry Finger 		return -EOPNOTSUPP;
408f1d2b4d3SLarry Finger 	}
409f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, MSR, value);
410f1d2b4d3SLarry Finger 	return 0;
411f1d2b4d3SLarry Finger }
412f1d2b4d3SLarry Finger 
rtl92c_init_network_type(struct ieee80211_hw * hw)413f1d2b4d3SLarry Finger void rtl92c_init_network_type(struct ieee80211_hw *hw)
414f1d2b4d3SLarry Finger {
415f1d2b4d3SLarry Finger 	rtl92c_set_network_type(hw, NL80211_IFTYPE_UNSPECIFIED);
416f1d2b4d3SLarry Finger }
417f1d2b4d3SLarry Finger 
rtl92c_init_adaptive_ctrl(struct ieee80211_hw * hw)418f1d2b4d3SLarry Finger void rtl92c_init_adaptive_ctrl(struct ieee80211_hw *hw)
419f1d2b4d3SLarry Finger {
420f1d2b4d3SLarry Finger 	u16	value16;
421f1d2b4d3SLarry Finger 	u32	value32;
422f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
423f1d2b4d3SLarry Finger 
424f1d2b4d3SLarry Finger 	/* Response Rate Set */
425f1d2b4d3SLarry Finger 	value32 = rtl_read_dword(rtlpriv, REG_RRSR);
426f1d2b4d3SLarry Finger 	value32 &= ~RATE_BITMAP_ALL;
427f1d2b4d3SLarry Finger 	value32 |= RATE_RRSR_CCK_ONLY_1M;
428f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_RRSR, value32);
429f1d2b4d3SLarry Finger 	/* SIFS (used in NAV) */
430f1d2b4d3SLarry Finger 	value16 = _SPEC_SIFS_CCK(0x10) | _SPEC_SIFS_OFDM(0x10);
431f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv,  REG_SPEC_SIFS, value16);
432f1d2b4d3SLarry Finger 	/* Retry Limit */
433f1d2b4d3SLarry Finger 	value16 = _LRL(0x30) | _SRL(0x30);
434f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_RL, value16);
435f1d2b4d3SLarry Finger }
436f1d2b4d3SLarry Finger 
rtl92c_init_rate_fallback(struct ieee80211_hw * hw)437f1d2b4d3SLarry Finger void rtl92c_init_rate_fallback(struct ieee80211_hw *hw)
438f1d2b4d3SLarry Finger {
439f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
440f1d2b4d3SLarry Finger 
441f1d2b4d3SLarry Finger 	/* Set Data Auto Rate Fallback Retry Count register. */
442f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_DARFRC, 0x00000000);
443f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_DARFRC+4, 0x10080404);
444f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_RARFRC, 0x04030201);
445f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_RARFRC+4, 0x08070605);
446f1d2b4d3SLarry Finger }
447f1d2b4d3SLarry Finger 
rtl92c_set_cck_sifs(struct ieee80211_hw * hw,u8 trx_sifs,u8 ctx_sifs)448f1d2b4d3SLarry Finger static void rtl92c_set_cck_sifs(struct ieee80211_hw *hw, u8 trx_sifs,
449f1d2b4d3SLarry Finger 				u8 ctx_sifs)
450f1d2b4d3SLarry Finger {
451f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
452f1d2b4d3SLarry Finger 
453f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_SIFS_CCK, trx_sifs);
454f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, (REG_SIFS_CCK + 1), ctx_sifs);
455f1d2b4d3SLarry Finger }
456f1d2b4d3SLarry Finger 
rtl92c_set_ofdm_sifs(struct ieee80211_hw * hw,u8 trx_sifs,u8 ctx_sifs)457f1d2b4d3SLarry Finger static void rtl92c_set_ofdm_sifs(struct ieee80211_hw *hw, u8 trx_sifs,
458f1d2b4d3SLarry Finger 				 u8 ctx_sifs)
459f1d2b4d3SLarry Finger {
460f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
461f1d2b4d3SLarry Finger 
462f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_SIFS_OFDM, trx_sifs);
463f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, (REG_SIFS_OFDM + 1), ctx_sifs);
464f1d2b4d3SLarry Finger }
465f1d2b4d3SLarry Finger 
rtl92c_init_edca_param(struct ieee80211_hw * hw,u16 queue,u16 txop,u8 cw_min,u8 cw_max,u8 aifs)466f1d2b4d3SLarry Finger void rtl92c_init_edca_param(struct ieee80211_hw *hw,
467f1d2b4d3SLarry Finger 			    u16 queue, u16 txop, u8 cw_min, u8 cw_max, u8 aifs)
468f1d2b4d3SLarry Finger {
469f1d2b4d3SLarry Finger 	/* sequence: VO, VI, BE, BK ==> the same as 92C hardware design.
470f1d2b4d3SLarry Finger 	 * referenc : enum nl80211_txq_q or ieee80211_set_wmm_default function.
471f1d2b4d3SLarry Finger 	 */
472f1d2b4d3SLarry Finger 	u32 value;
473f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
474f1d2b4d3SLarry Finger 
475f1d2b4d3SLarry Finger 	value = (u32)aifs;
476f1d2b4d3SLarry Finger 	value |= ((u32)cw_min & 0xF) << 8;
477f1d2b4d3SLarry Finger 	value |= ((u32)cw_max & 0xF) << 12;
478f1d2b4d3SLarry Finger 	value |= (u32)txop << 16;
479f1d2b4d3SLarry Finger 	/* 92C hardware register sequence is the same as queue number. */
480f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, (REG_EDCA_VO_PARAM + (queue * 4)), value);
481f1d2b4d3SLarry Finger }
482f1d2b4d3SLarry Finger 
rtl92c_init_edca(struct ieee80211_hw * hw)483f1d2b4d3SLarry Finger void rtl92c_init_edca(struct ieee80211_hw *hw)
484f1d2b4d3SLarry Finger {
485f1d2b4d3SLarry Finger 	u16 value16;
486f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
487f1d2b4d3SLarry Finger 
488f1d2b4d3SLarry Finger 	/* disable EDCCA count down, to reduce collison and retry */
489f1d2b4d3SLarry Finger 	value16 = rtl_read_word(rtlpriv, REG_RD_CTRL);
490f1d2b4d3SLarry Finger 	value16 |= DIS_EDCA_CNT_DWN;
491f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_RD_CTRL, value16);
492f1d2b4d3SLarry Finger 	/* Update SIFS timing.  ??????????
493f1d2b4d3SLarry Finger 	 * pHalData->SifsTime = 0x0e0e0a0a; */
494f1d2b4d3SLarry Finger 	rtl92c_set_cck_sifs(hw, 0xa, 0xa);
495f1d2b4d3SLarry Finger 	rtl92c_set_ofdm_sifs(hw, 0xe, 0xe);
496f1d2b4d3SLarry Finger 	/* Set CCK/OFDM SIFS to be 10us. */
497f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_SIFS_CCK, 0x0a0a);
498f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_SIFS_OFDM, 0x1010);
499f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_PROT_MODE_CTRL, 0x0204);
500f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_BAR_MODE_CTRL, 0x014004);
501f1d2b4d3SLarry Finger 	/* TXOP */
502f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_BE_PARAM, 0x005EA42B);
503f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM, 0x0000A44F);
504f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM, 0x005EA324);
505f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM, 0x002FA226);
506f1d2b4d3SLarry Finger 	/* PIFS */
507f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_PIFS, 0x1C);
508f1d2b4d3SLarry Finger 	/* AGGR BREAK TIME Register */
509f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
510f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0040);
511f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_BCNDMATIM, 0x02);
512f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_ATIMWND, 0x02);
513f1d2b4d3SLarry Finger }
514f1d2b4d3SLarry Finger 
rtl92c_init_ampdu_aggregation(struct ieee80211_hw * hw)515f1d2b4d3SLarry Finger void rtl92c_init_ampdu_aggregation(struct ieee80211_hw *hw)
516f1d2b4d3SLarry Finger {
517f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
518f1d2b4d3SLarry Finger 
519f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_AGGLEN_LMT, 0x99997631);
520f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
521f1d2b4d3SLarry Finger 	/* init AMPDU aggregation number, tuning for Tx's TP, */
522f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, 0x4CA, 0x0708);
523f1d2b4d3SLarry Finger }
524f1d2b4d3SLarry Finger 
rtl92c_init_beacon_max_error(struct ieee80211_hw * hw)525f1d2b4d3SLarry Finger void rtl92c_init_beacon_max_error(struct ieee80211_hw *hw)
526f1d2b4d3SLarry Finger {
527f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
528f1d2b4d3SLarry Finger 
529f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_BCN_MAX_ERR, 0xFF);
530f1d2b4d3SLarry Finger }
531f1d2b4d3SLarry Finger 
rtl92c_init_rdg_setting(struct ieee80211_hw * hw)532f1d2b4d3SLarry Finger void rtl92c_init_rdg_setting(struct ieee80211_hw *hw)
533f1d2b4d3SLarry Finger {
534f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
535f1d2b4d3SLarry Finger 
536f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_RD_CTRL, 0xFF);
537f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_RD_NAV_NXT, 0x200);
538f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_RD_RESP_PKT_TH, 0x05);
539f1d2b4d3SLarry Finger }
540f1d2b4d3SLarry Finger 
rtl92c_init_retry_function(struct ieee80211_hw * hw)541f1d2b4d3SLarry Finger void rtl92c_init_retry_function(struct ieee80211_hw *hw)
542f1d2b4d3SLarry Finger {
543f1d2b4d3SLarry Finger 	u8	value8;
544f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
545f1d2b4d3SLarry Finger 
546f1d2b4d3SLarry Finger 	value8 = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL);
547f1d2b4d3SLarry Finger 	value8 |= EN_AMPDU_RTY_NEW;
548f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL, value8);
549f1d2b4d3SLarry Finger 	/* Set ACK timeout */
550f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_ACKTO, 0x40);
551f1d2b4d3SLarry Finger }
552f1d2b4d3SLarry Finger 
rtl92c_disable_fast_edca(struct ieee80211_hw * hw)553f1d2b4d3SLarry Finger void rtl92c_disable_fast_edca(struct ieee80211_hw *hw)
554f1d2b4d3SLarry Finger {
555f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
556f1d2b4d3SLarry Finger 
557f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_FAST_EDCA_CTRL, 0);
558f1d2b4d3SLarry Finger }
559f1d2b4d3SLarry Finger 
rtl92c_set_min_space(struct ieee80211_hw * hw,bool is2T)560f1d2b4d3SLarry Finger void rtl92c_set_min_space(struct ieee80211_hw *hw, bool is2T)
561f1d2b4d3SLarry Finger {
562f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
563f1d2b4d3SLarry Finger 	u8 value = is2T ? MAX_MSS_DENSITY_2T : MAX_MSS_DENSITY_1T;
564f1d2b4d3SLarry Finger 
565f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE, value);
566f1d2b4d3SLarry Finger }
567f1d2b4d3SLarry Finger 
568f1d2b4d3SLarry Finger /*==============================================================*/
569f1d2b4d3SLarry Finger 
_rtl92c_query_rxphystatus(struct ieee80211_hw * hw,struct rtl_stats * pstats,struct rx_desc_92c * p_desc,struct rx_fwinfo_92c * p_drvinfo,bool packet_match_bssid,bool packet_toself,bool packet_beacon)570f1d2b4d3SLarry Finger static void _rtl92c_query_rxphystatus(struct ieee80211_hw *hw,
571f1d2b4d3SLarry Finger 				      struct rtl_stats *pstats,
572f1d2b4d3SLarry Finger 				      struct rx_desc_92c *p_desc,
573f1d2b4d3SLarry Finger 				      struct rx_fwinfo_92c *p_drvinfo,
574f1d2b4d3SLarry Finger 				      bool packet_match_bssid,
575f1d2b4d3SLarry Finger 				      bool packet_toself,
576f1d2b4d3SLarry Finger 				      bool packet_beacon)
577f1d2b4d3SLarry Finger {
578f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
579f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
580f1d2b4d3SLarry Finger 	struct phy_sts_cck_8192s_t *cck_buf;
581f1d2b4d3SLarry Finger 	s8 rx_pwr_all = 0, rx_pwr[4];
582f1d2b4d3SLarry Finger 	u8 rf_rx_num = 0, evm, pwdb_all;
583f1d2b4d3SLarry Finger 	u8 i, max_spatial_stream;
584f1d2b4d3SLarry Finger 	u32 rssi, total_rssi = 0;
585f1d2b4d3SLarry Finger 	bool in_powersavemode = false;
586f1d2b4d3SLarry Finger 	bool is_cck_rate;
587afd708f7SLarry Finger 	__le32 *pdesc = (__le32 *)p_desc;
588f1d2b4d3SLarry Finger 
589f1d2b4d3SLarry Finger 	is_cck_rate = RX_HAL_IS_CCK_RATE(p_desc->rxmcs);
590f1d2b4d3SLarry Finger 	pstats->packet_matchbssid = packet_match_bssid;
591f1d2b4d3SLarry Finger 	pstats->packet_toself = packet_toself;
592f1d2b4d3SLarry Finger 	pstats->packet_beacon = packet_beacon;
593f1d2b4d3SLarry Finger 	pstats->is_cck = is_cck_rate;
594f1d2b4d3SLarry Finger 	pstats->RX_SIGQ[0] = -1;
595f1d2b4d3SLarry Finger 	pstats->RX_SIGQ[1] = -1;
596f1d2b4d3SLarry Finger 	if (is_cck_rate) {
597f1d2b4d3SLarry Finger 		u8 report, cck_highpwr;
5986d255202SLarry Finger 
599f1d2b4d3SLarry Finger 		cck_buf = (struct phy_sts_cck_8192s_t *)p_drvinfo;
600f1d2b4d3SLarry Finger 		if (!in_powersavemode)
601f1d2b4d3SLarry Finger 			cck_highpwr = rtlphy->cck_high_power;
602f1d2b4d3SLarry Finger 		else
603f1d2b4d3SLarry Finger 			cck_highpwr = false;
604f1d2b4d3SLarry Finger 		if (!cck_highpwr) {
605f1d2b4d3SLarry Finger 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
6066d255202SLarry Finger 
607f1d2b4d3SLarry Finger 			report = cck_buf->cck_agc_rpt & 0xc0;
608f1d2b4d3SLarry Finger 			report = report >> 6;
609f1d2b4d3SLarry Finger 			switch (report) {
610f1d2b4d3SLarry Finger 			case 0x3:
611f1d2b4d3SLarry Finger 				rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
612f1d2b4d3SLarry Finger 				break;
613f1d2b4d3SLarry Finger 			case 0x2:
614f1d2b4d3SLarry Finger 				rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
615f1d2b4d3SLarry Finger 				break;
616f1d2b4d3SLarry Finger 			case 0x1:
617f1d2b4d3SLarry Finger 				rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
618f1d2b4d3SLarry Finger 				break;
619f1d2b4d3SLarry Finger 			case 0x0:
620f1d2b4d3SLarry Finger 				rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
621f1d2b4d3SLarry Finger 				break;
622f1d2b4d3SLarry Finger 			}
623f1d2b4d3SLarry Finger 		} else {
624f1d2b4d3SLarry Finger 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
6256d255202SLarry Finger 
626f1d2b4d3SLarry Finger 			report = p_drvinfo->cfosho[0] & 0x60;
627f1d2b4d3SLarry Finger 			report = report >> 5;
628f1d2b4d3SLarry Finger 			switch (report) {
629f1d2b4d3SLarry Finger 			case 0x3:
630f1d2b4d3SLarry Finger 				rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f) << 1);
631f1d2b4d3SLarry Finger 				break;
632f1d2b4d3SLarry Finger 			case 0x2:
633f1d2b4d3SLarry Finger 				rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f) << 1);
634f1d2b4d3SLarry Finger 				break;
635f1d2b4d3SLarry Finger 			case 0x1:
636f1d2b4d3SLarry Finger 				rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f) << 1);
637f1d2b4d3SLarry Finger 				break;
638f1d2b4d3SLarry Finger 			case 0x0:
639f1d2b4d3SLarry Finger 				rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f) << 1);
640f1d2b4d3SLarry Finger 				break;
641f1d2b4d3SLarry Finger 			}
642f1d2b4d3SLarry Finger 		}
643b8a19dd6SMichael Straube 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
644f1d2b4d3SLarry Finger 		pstats->rx_pwdb_all = pwdb_all;
645f1d2b4d3SLarry Finger 		pstats->recvsignalpower = rx_pwr_all;
646f1d2b4d3SLarry Finger 		if (packet_match_bssid) {
647f1d2b4d3SLarry Finger 			u8 sq;
6486d255202SLarry Finger 
649f1d2b4d3SLarry Finger 			if (pstats->rx_pwdb_all > 40)
650f1d2b4d3SLarry Finger 				sq = 100;
651f1d2b4d3SLarry Finger 			else {
652f1d2b4d3SLarry Finger 				sq = cck_buf->sq_rpt;
653f1d2b4d3SLarry Finger 				if (sq > 64)
654f1d2b4d3SLarry Finger 					sq = 0;
655f1d2b4d3SLarry Finger 				else if (sq < 20)
656f1d2b4d3SLarry Finger 					sq = 100;
657f1d2b4d3SLarry Finger 				else
658f1d2b4d3SLarry Finger 					sq = ((64 - sq) * 100) / 44;
659f1d2b4d3SLarry Finger 			}
660f1d2b4d3SLarry Finger 			pstats->signalquality = sq;
661f1d2b4d3SLarry Finger 			pstats->RX_SIGQ[0] = sq;
662f1d2b4d3SLarry Finger 			pstats->RX_SIGQ[1] = -1;
663f1d2b4d3SLarry Finger 		}
664f1d2b4d3SLarry Finger 	} else {
665f1d2b4d3SLarry Finger 		rtlpriv->dm.rfpath_rxenable[0] =
666f1d2b4d3SLarry Finger 		    rtlpriv->dm.rfpath_rxenable[1] = true;
667f1d2b4d3SLarry Finger 		for (i = RF90_PATH_A; i < RF90_PATH_MAX; i++) {
668f1d2b4d3SLarry Finger 			if (rtlpriv->dm.rfpath_rxenable[i])
669f1d2b4d3SLarry Finger 				rf_rx_num++;
670f1d2b4d3SLarry Finger 			rx_pwr[i] =
671f1d2b4d3SLarry Finger 			    ((p_drvinfo->gain_trsw[i] & 0x3f) * 2) - 110;
672b8a19dd6SMichael Straube 			rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
673f1d2b4d3SLarry Finger 			total_rssi += rssi;
674f1d2b4d3SLarry Finger 			rtlpriv->stats.rx_snr_db[i] =
675f1d2b4d3SLarry Finger 			    (long)(p_drvinfo->rxsnr[i] / 2);
676f1d2b4d3SLarry Finger 
677f1d2b4d3SLarry Finger 			if (packet_match_bssid)
678f1d2b4d3SLarry Finger 				pstats->rx_mimo_signalstrength[i] = (u8) rssi;
679f1d2b4d3SLarry Finger 		}
680f1d2b4d3SLarry Finger 		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
681b8a19dd6SMichael Straube 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
682f1d2b4d3SLarry Finger 		pstats->rx_pwdb_all = pwdb_all;
683f1d2b4d3SLarry Finger 		pstats->rxpower = rx_pwr_all;
684f1d2b4d3SLarry Finger 		pstats->recvsignalpower = rx_pwr_all;
6853925ae06SLarry Finger 		if (get_rx_desc_rx_mcs(pdesc) &&
6863925ae06SLarry Finger 		    get_rx_desc_rx_mcs(pdesc) >= DESC_RATEMCS8 &&
6873925ae06SLarry Finger 		    get_rx_desc_rx_mcs(pdesc) <= DESC_RATEMCS15)
688f1d2b4d3SLarry Finger 			max_spatial_stream = 2;
689f1d2b4d3SLarry Finger 		else
690f1d2b4d3SLarry Finger 			max_spatial_stream = 1;
691f1d2b4d3SLarry Finger 		for (i = 0; i < max_spatial_stream; i++) {
692622c19edSMichael Straube 			evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
693f1d2b4d3SLarry Finger 			if (packet_match_bssid) {
694f1d2b4d3SLarry Finger 				if (i == 0)
695f1d2b4d3SLarry Finger 					pstats->signalquality =
696f1d2b4d3SLarry Finger 					    (u8) (evm & 0xff);
697f1d2b4d3SLarry Finger 				pstats->RX_SIGQ[i] =
698f1d2b4d3SLarry Finger 				    (u8) (evm & 0xff);
699f1d2b4d3SLarry Finger 			}
700f1d2b4d3SLarry Finger 		}
701f1d2b4d3SLarry Finger 	}
702f1d2b4d3SLarry Finger 	if (is_cck_rate)
703f1d2b4d3SLarry Finger 		pstats->signalstrength =
704fef91a2bSMichael Straube 		    (u8)(rtl_signal_scale_mapping(hw, pwdb_all));
705f1d2b4d3SLarry Finger 	else if (rf_rx_num != 0)
706f1d2b4d3SLarry Finger 		pstats->signalstrength =
707fef91a2bSMichael Straube 		    (u8)(rtl_signal_scale_mapping(hw, total_rssi /= rf_rx_num));
708f1d2b4d3SLarry Finger }
709f1d2b4d3SLarry Finger 
rtl92c_translate_rx_signal_stuff(struct ieee80211_hw * hw,struct sk_buff * skb,struct rtl_stats * pstats,struct rx_desc_92c * pdesc,struct rx_fwinfo_92c * p_drvinfo)710f1d2b4d3SLarry Finger void rtl92c_translate_rx_signal_stuff(struct ieee80211_hw *hw,
711f1d2b4d3SLarry Finger 					       struct sk_buff *skb,
712f1d2b4d3SLarry Finger 					       struct rtl_stats *pstats,
713f1d2b4d3SLarry Finger 					       struct rx_desc_92c *pdesc,
714f1d2b4d3SLarry Finger 					       struct rx_fwinfo_92c *p_drvinfo)
715f1d2b4d3SLarry Finger {
716f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
717f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
718f1d2b4d3SLarry Finger 	struct ieee80211_hdr *hdr;
719f1d2b4d3SLarry Finger 	u8 *tmp_buf;
720f1d2b4d3SLarry Finger 	u8 *praddr;
721f1d2b4d3SLarry Finger 	__le16 fc;
722f1d2b4d3SLarry Finger 	u16 type, cpu_fc;
723f1d2b4d3SLarry Finger 	bool packet_matchbssid, packet_toself, packet_beacon = false;
724f1d2b4d3SLarry Finger 
725f1d2b4d3SLarry Finger 	tmp_buf = skb->data + pstats->rx_drvinfo_size + pstats->rx_bufshift;
726f1d2b4d3SLarry Finger 	hdr = (struct ieee80211_hdr *)tmp_buf;
727f1d2b4d3SLarry Finger 	fc = hdr->frame_control;
728f1d2b4d3SLarry Finger 	cpu_fc = le16_to_cpu(fc);
729f1d2b4d3SLarry Finger 	type = WLAN_FC_GET_TYPE(fc);
730f1d2b4d3SLarry Finger 	praddr = hdr->addr1;
731f1d2b4d3SLarry Finger 	packet_matchbssid =
732f1d2b4d3SLarry Finger 	    ((IEEE80211_FTYPE_CTL != type) &&
733f1d2b4d3SLarry Finger 	     ether_addr_equal(mac->bssid,
734f1d2b4d3SLarry Finger 			      (cpu_fc & IEEE80211_FCTL_TODS) ? hdr->addr1 :
735f1d2b4d3SLarry Finger 			      (cpu_fc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 :
736f1d2b4d3SLarry Finger 			      hdr->addr3) &&
737f1d2b4d3SLarry Finger 	     (!pstats->hwerror) && (!pstats->crc) && (!pstats->icv));
738f1d2b4d3SLarry Finger 
739f1d2b4d3SLarry Finger 	packet_toself = packet_matchbssid &&
740f1d2b4d3SLarry Finger 	    ether_addr_equal(praddr, rtlefuse->dev_addr);
741f1d2b4d3SLarry Finger 	if (ieee80211_is_beacon(fc))
742f1d2b4d3SLarry Finger 		packet_beacon = true;
743f1d2b4d3SLarry Finger 	_rtl92c_query_rxphystatus(hw, pstats, pdesc, p_drvinfo,
744f1d2b4d3SLarry Finger 				   packet_matchbssid, packet_toself,
745f1d2b4d3SLarry Finger 				   packet_beacon);
746f1d2b4d3SLarry Finger 	rtl_process_phyinfo(hw, tmp_buf, pstats);
747f1d2b4d3SLarry Finger }
748