xref: /openbmc/linux/drivers/net/wireless/realtek/rtlwifi/rtl8192cu/mac.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
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",
104f1d2b4d3SLarry Finger 		rtlphy->rf_type == RF_2T2R ? "RF_2T2R" : "RF_1T1R");
105f1d2b4d3SLarry Finger 	if (get_rf_type(rtlphy) == RF_1T1R)
106f1d2b4d3SLarry Finger 		rtlpriv->dm.rfpath_rxenable[0] = true;
107f1d2b4d3SLarry Finger 	else
108f1d2b4d3SLarry Finger 		rtlpriv->dm.rfpath_rxenable[0] =
109f1d2b4d3SLarry Finger 		    rtlpriv->dm.rfpath_rxenable[1] = true;
11034d7f007SLarry Finger 	rtl_dbg(rtlpriv, COMP_INIT, DBG_LOUD, "VersionID = 0x%4x\n",
111f1d2b4d3SLarry Finger 		rtlhal->version);
112f1d2b4d3SLarry Finger }
113f1d2b4d3SLarry Finger 
114f1d2b4d3SLarry Finger /**
115*9a0fb950SYang Shen  * rtl92c_llt_write - LLT table write access
116230f874eSLee Jones  * @hw: Pointer to the ieee80211_hw structure.
117f1d2b4d3SLarry Finger  * @address: LLT logical address.
118f1d2b4d3SLarry Finger  * @data: LLT data content
119f1d2b4d3SLarry Finger  *
120f1d2b4d3SLarry Finger  * Realtek hardware access function.
121f1d2b4d3SLarry Finger  *
122f1d2b4d3SLarry Finger  */
rtl92c_llt_write(struct ieee80211_hw * hw,u32 address,u32 data)123f1d2b4d3SLarry Finger bool rtl92c_llt_write(struct ieee80211_hw *hw, u32 address, u32 data)
124f1d2b4d3SLarry Finger {
125f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
126f1d2b4d3SLarry Finger 	bool status = true;
127f1d2b4d3SLarry Finger 	long count = 0;
128f1d2b4d3SLarry Finger 	u32 value = _LLT_INIT_ADDR(address) |
129f1d2b4d3SLarry Finger 	    _LLT_INIT_DATA(data) | _LLT_OP(_LLT_WRITE_ACCESS);
130f1d2b4d3SLarry Finger 
131f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_LLT_INIT, value);
132f1d2b4d3SLarry Finger 	do {
133f1d2b4d3SLarry Finger 		value = rtl_read_dword(rtlpriv, REG_LLT_INIT);
134f1d2b4d3SLarry Finger 		if (_LLT_NO_ACTIVE == _LLT_OP_VALUE(value))
135f1d2b4d3SLarry Finger 			break;
136f1d2b4d3SLarry Finger 		if (count > POLLING_LLT_THRESHOLD) {
137c38af3f0SLarry Finger 			pr_err("Failed to polling write LLT done at address %d! _LLT_OP_VALUE(%x)\n",
138f1d2b4d3SLarry Finger 			       address, _LLT_OP_VALUE(value));
139f1d2b4d3SLarry Finger 			status = false;
140f1d2b4d3SLarry Finger 			break;
141f1d2b4d3SLarry Finger 		}
142f1d2b4d3SLarry Finger 	} while (++count);
143f1d2b4d3SLarry Finger 	return status;
144f1d2b4d3SLarry Finger }
1456d255202SLarry Finger 
146f1d2b4d3SLarry Finger /**
147*9a0fb950SYang Shen  * rtl92c_init_llt_table - Init LLT table
148230f874eSLee Jones  * @hw: Pointer to the ieee80211_hw structure.
149230f874eSLee Jones  * @boundary: Page boundary.
150f1d2b4d3SLarry Finger  *
151f1d2b4d3SLarry Finger  * Realtek hardware access function.
152f1d2b4d3SLarry Finger  */
rtl92c_init_llt_table(struct ieee80211_hw * hw,u32 boundary)153f1d2b4d3SLarry Finger bool rtl92c_init_llt_table(struct ieee80211_hw *hw, u32 boundary)
154f1d2b4d3SLarry Finger {
155f1d2b4d3SLarry Finger 	bool rst = true;
156f1d2b4d3SLarry Finger 	u32	i;
157f1d2b4d3SLarry Finger 
158f1d2b4d3SLarry Finger 	for (i = 0; i < (boundary - 1); i++) {
159f1d2b4d3SLarry Finger 		rst = rtl92c_llt_write(hw, i , i + 1);
16003ce8159SZheng Bin 		if (!rst) {
161f1d2b4d3SLarry Finger 			pr_err("===> %s #1 fail\n", __func__);
162f1d2b4d3SLarry Finger 			return rst;
163f1d2b4d3SLarry Finger 		}
164f1d2b4d3SLarry Finger 	}
165f1d2b4d3SLarry Finger 	/* end of list */
166f1d2b4d3SLarry Finger 	rst = rtl92c_llt_write(hw, (boundary - 1), 0xFF);
16703ce8159SZheng Bin 	if (!rst) {
168f1d2b4d3SLarry Finger 		pr_err("===> %s #2 fail\n", __func__);
169f1d2b4d3SLarry Finger 		return rst;
170f1d2b4d3SLarry Finger 	}
171f1d2b4d3SLarry Finger 	/* Make the other pages as ring buffer
172f1d2b4d3SLarry Finger 	 * This ring buffer is used as beacon buffer if we config this MAC
173f1d2b4d3SLarry Finger 	 *  as two MAC transfer.
174f1d2b4d3SLarry Finger 	 * Otherwise used as local loopback buffer.
175f1d2b4d3SLarry Finger 	 */
176f1d2b4d3SLarry Finger 	for (i = boundary; i < LLT_LAST_ENTRY_OF_TX_PKT_BUFFER; i++) {
177f1d2b4d3SLarry Finger 		rst = rtl92c_llt_write(hw, i, (i + 1));
17803ce8159SZheng Bin 		if (!rst) {
179f1d2b4d3SLarry Finger 			pr_err("===> %s #3 fail\n", __func__);
180f1d2b4d3SLarry Finger 			return rst;
181f1d2b4d3SLarry Finger 		}
182f1d2b4d3SLarry Finger 	}
183f1d2b4d3SLarry Finger 	/* Let last entry point to the start entry of ring buffer */
184f1d2b4d3SLarry Finger 	rst = rtl92c_llt_write(hw, LLT_LAST_ENTRY_OF_TX_PKT_BUFFER, boundary);
18503ce8159SZheng Bin 	if (!rst) {
186f1d2b4d3SLarry Finger 		pr_err("===> %s #4 fail\n", __func__);
187f1d2b4d3SLarry Finger 		return rst;
188f1d2b4d3SLarry Finger 	}
189f1d2b4d3SLarry Finger 	return rst;
190f1d2b4d3SLarry Finger }
1916d255202SLarry 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)192f1d2b4d3SLarry Finger void rtl92c_set_key(struct ieee80211_hw *hw, u32 key_index,
193f1d2b4d3SLarry Finger 		     u8 *p_macaddr, bool is_group, u8 enc_algo,
194f1d2b4d3SLarry Finger 		     bool is_wepkey, bool clear_all)
195f1d2b4d3SLarry Finger {
196f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
197f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
198f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
199f1d2b4d3SLarry Finger 	u8 *macaddr = p_macaddr;
200f1d2b4d3SLarry Finger 	u32 entry_id = 0;
201f1d2b4d3SLarry Finger 	bool is_pairwise = false;
202f1d2b4d3SLarry Finger 	static u8 cam_const_addr[4][6] = {
203f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
204f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
205f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
206f1d2b4d3SLarry Finger 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
207f1d2b4d3SLarry Finger 	};
208f1d2b4d3SLarry Finger 	static u8 cam_const_broad[] = {
209f1d2b4d3SLarry Finger 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
210f1d2b4d3SLarry Finger 	};
211f1d2b4d3SLarry Finger 
212f1d2b4d3SLarry Finger 	if (clear_all) {
213f1d2b4d3SLarry Finger 		u8 idx = 0;
214f1d2b4d3SLarry Finger 		u8 cam_offset = 0;
215f1d2b4d3SLarry Finger 		u8 clear_number = 5;
216f1d2b4d3SLarry Finger 
21734d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG, "clear_all\n");
218f1d2b4d3SLarry Finger 		for (idx = 0; idx < clear_number; idx++) {
219f1d2b4d3SLarry Finger 			rtl_cam_mark_invalid(hw, cam_offset + idx);
220f1d2b4d3SLarry Finger 			rtl_cam_empty_entry(hw, cam_offset + idx);
221f1d2b4d3SLarry Finger 			if (idx < 5) {
222f1d2b4d3SLarry Finger 				memset(rtlpriv->sec.key_buf[idx], 0,
223f1d2b4d3SLarry Finger 				       MAX_KEY_LEN);
224f1d2b4d3SLarry Finger 				rtlpriv->sec.key_len[idx] = 0;
225f1d2b4d3SLarry Finger 			}
226f1d2b4d3SLarry Finger 		}
227f1d2b4d3SLarry Finger 	} else {
228f1d2b4d3SLarry Finger 		switch (enc_algo) {
229f1d2b4d3SLarry Finger 		case WEP40_ENCRYPTION:
230f1d2b4d3SLarry Finger 			enc_algo = CAM_WEP40;
231f1d2b4d3SLarry Finger 			break;
232f1d2b4d3SLarry Finger 		case WEP104_ENCRYPTION:
233f1d2b4d3SLarry Finger 			enc_algo = CAM_WEP104;
234f1d2b4d3SLarry Finger 			break;
235f1d2b4d3SLarry Finger 		case TKIP_ENCRYPTION:
236f1d2b4d3SLarry Finger 			enc_algo = CAM_TKIP;
237f1d2b4d3SLarry Finger 			break;
238f1d2b4d3SLarry Finger 		case AESCCMP_ENCRYPTION:
239f1d2b4d3SLarry Finger 			enc_algo = CAM_AES;
240f1d2b4d3SLarry Finger 			break;
241f1d2b4d3SLarry Finger 		default:
242c38af3f0SLarry Finger 			pr_err("illegal switch case\n");
243f1d2b4d3SLarry Finger 			enc_algo = CAM_TKIP;
244f1d2b4d3SLarry Finger 			break;
245f1d2b4d3SLarry Finger 		}
246f1d2b4d3SLarry Finger 		if (is_wepkey || rtlpriv->sec.use_defaultkey) {
247f1d2b4d3SLarry Finger 			macaddr = cam_const_addr[key_index];
248f1d2b4d3SLarry Finger 			entry_id = key_index;
249f1d2b4d3SLarry Finger 		} else {
250f1d2b4d3SLarry Finger 			if (is_group) {
251f1d2b4d3SLarry Finger 				macaddr = cam_const_broad;
252f1d2b4d3SLarry Finger 				entry_id = key_index;
253f1d2b4d3SLarry Finger 			} else {
254f1d2b4d3SLarry Finger 				if (mac->opmode == NL80211_IFTYPE_AP ||
255f1d2b4d3SLarry Finger 				    mac->opmode == NL80211_IFTYPE_MESH_POINT) {
256f1d2b4d3SLarry Finger 					entry_id = rtl_cam_get_free_entry(hw,
257f1d2b4d3SLarry Finger 								 p_macaddr);
258f1d2b4d3SLarry Finger 					if (entry_id >=  TOTAL_CAM_ENTRY) {
259c38af3f0SLarry Finger 						pr_err("Can not find free hw security cam entry\n");
260f1d2b4d3SLarry Finger 						return;
261f1d2b4d3SLarry Finger 					}
262f1d2b4d3SLarry Finger 				} else {
263f1d2b4d3SLarry Finger 					entry_id = CAM_PAIRWISE_KEY_POSITION;
264f1d2b4d3SLarry Finger 				}
265f1d2b4d3SLarry Finger 
266f1d2b4d3SLarry Finger 				key_index = PAIRWISE_KEYIDX;
267f1d2b4d3SLarry Finger 				is_pairwise = true;
268f1d2b4d3SLarry Finger 			}
269f1d2b4d3SLarry Finger 		}
270f1d2b4d3SLarry Finger 		if (rtlpriv->sec.key_len[key_index] == 0) {
27134d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
272f1d2b4d3SLarry Finger 				"delete one entry\n");
273f1d2b4d3SLarry Finger 			if (mac->opmode == NL80211_IFTYPE_AP ||
274f1d2b4d3SLarry Finger 			    mac->opmode == NL80211_IFTYPE_MESH_POINT)
275f1d2b4d3SLarry Finger 				rtl_cam_del_entry(hw, p_macaddr);
276f1d2b4d3SLarry Finger 			rtl_cam_delete_one_entry(hw, p_macaddr, entry_id);
277f1d2b4d3SLarry Finger 		} else {
27834d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_LOUD,
279f1d2b4d3SLarry Finger 				"The insert KEY length is %d\n",
280f1d2b4d3SLarry Finger 				 rtlpriv->sec.key_len[PAIRWISE_KEYIDX]);
28134d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_LOUD,
282f1d2b4d3SLarry Finger 				"The insert KEY is %x %x\n",
283f1d2b4d3SLarry Finger 				rtlpriv->sec.key_buf[0][0],
284f1d2b4d3SLarry Finger 				rtlpriv->sec.key_buf[0][1]);
28534d7f007SLarry Finger 			rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
286f1d2b4d3SLarry Finger 				"add one entry\n");
287f1d2b4d3SLarry Finger 			if (is_pairwise) {
288f1d2b4d3SLarry Finger 				RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_LOUD,
289f1d2b4d3SLarry Finger 					      "Pairwise Key content",
290f1d2b4d3SLarry Finger 					      rtlpriv->sec.pairwise_key,
291f1d2b4d3SLarry Finger 					      rtlpriv->sec.
292f1d2b4d3SLarry Finger 					      key_len[PAIRWISE_KEYIDX]);
29334d7f007SLarry Finger 				rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
294f1d2b4d3SLarry Finger 					"set Pairwise key\n");
295f1d2b4d3SLarry Finger 
296f1d2b4d3SLarry Finger 				rtl_cam_add_one_entry(hw, macaddr, key_index,
297f1d2b4d3SLarry Finger 						entry_id, enc_algo,
298f1d2b4d3SLarry Finger 						CAM_CONFIG_NO_USEDK,
299f1d2b4d3SLarry Finger 						rtlpriv->sec.
300f1d2b4d3SLarry Finger 						key_buf[key_index]);
301f1d2b4d3SLarry Finger 			} else {
30234d7f007SLarry Finger 				rtl_dbg(rtlpriv, COMP_SEC, DBG_DMESG,
303f1d2b4d3SLarry Finger 					"set group key\n");
304f1d2b4d3SLarry Finger 				if (mac->opmode == NL80211_IFTYPE_ADHOC) {
305f1d2b4d3SLarry Finger 					rtl_cam_add_one_entry(hw,
306f1d2b4d3SLarry Finger 						rtlefuse->dev_addr,
307f1d2b4d3SLarry Finger 						PAIRWISE_KEYIDX,
308f1d2b4d3SLarry Finger 						CAM_PAIRWISE_KEY_POSITION,
309f1d2b4d3SLarry Finger 						enc_algo,
310f1d2b4d3SLarry Finger 						CAM_CONFIG_NO_USEDK,
311f1d2b4d3SLarry Finger 						rtlpriv->sec.key_buf
312f1d2b4d3SLarry Finger 						[entry_id]);
313f1d2b4d3SLarry Finger 				}
314f1d2b4d3SLarry Finger 				rtl_cam_add_one_entry(hw, macaddr, key_index,
315f1d2b4d3SLarry Finger 						entry_id, enc_algo,
316f1d2b4d3SLarry Finger 						CAM_CONFIG_NO_USEDK,
317f1d2b4d3SLarry Finger 						rtlpriv->sec.key_buf[entry_id]);
318f1d2b4d3SLarry Finger 			}
319f1d2b4d3SLarry Finger 		}
320f1d2b4d3SLarry Finger 	}
321f1d2b4d3SLarry Finger }
322f1d2b4d3SLarry Finger 
rtl92c_get_txdma_status(struct ieee80211_hw * hw)323f1d2b4d3SLarry Finger u32 rtl92c_get_txdma_status(struct ieee80211_hw *hw)
324f1d2b4d3SLarry Finger {
325f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
326f1d2b4d3SLarry Finger 
327f1d2b4d3SLarry Finger 	return rtl_read_dword(rtlpriv, REG_TXDMA_STATUS);
328f1d2b4d3SLarry Finger }
329f1d2b4d3SLarry Finger 
rtl92c_enable_interrupt(struct ieee80211_hw * hw)330f1d2b4d3SLarry Finger void rtl92c_enable_interrupt(struct ieee80211_hw *hw)
331f1d2b4d3SLarry Finger {
332f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
333f1d2b4d3SLarry Finger 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
334f1d2b4d3SLarry Finger 	struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
335f1d2b4d3SLarry Finger 
33658438d9aSPing-Ke Shih 	if (IS_HARDWARE_TYPE_8192CE(rtlpriv)) {
337f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMR, rtlpci->irq_mask[0] &
338f1d2b4d3SLarry Finger 				0xFFFFFFFF);
339f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMRE, rtlpci->irq_mask[1] &
340f1d2b4d3SLarry Finger 				0xFFFFFFFF);
341f1d2b4d3SLarry Finger 	} else {
342f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMR, rtlusb->irq_mask[0] &
343f1d2b4d3SLarry Finger 				0xFFFFFFFF);
344f1d2b4d3SLarry Finger 		rtl_write_dword(rtlpriv, REG_HIMRE, rtlusb->irq_mask[1] &
345f1d2b4d3SLarry Finger 				0xFFFFFFFF);
346f1d2b4d3SLarry Finger 	}
347f1d2b4d3SLarry Finger }
348f1d2b4d3SLarry Finger 
rtl92c_init_interrupt(struct ieee80211_hw * hw)349f1d2b4d3SLarry Finger void rtl92c_init_interrupt(struct ieee80211_hw *hw)
350f1d2b4d3SLarry Finger {
351f1d2b4d3SLarry Finger 	 rtl92c_enable_interrupt(hw);
352f1d2b4d3SLarry Finger }
353f1d2b4d3SLarry Finger 
rtl92c_disable_interrupt(struct ieee80211_hw * hw)354f1d2b4d3SLarry Finger void rtl92c_disable_interrupt(struct ieee80211_hw *hw)
355f1d2b4d3SLarry Finger {
356f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
357f1d2b4d3SLarry Finger 
358f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_HIMR, IMR8190_DISABLED);
359f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_HIMRE, IMR8190_DISABLED);
360f1d2b4d3SLarry Finger }
361f1d2b4d3SLarry Finger 
rtl92c_set_qos(struct ieee80211_hw * hw,int aci)362f1d2b4d3SLarry Finger void rtl92c_set_qos(struct ieee80211_hw *hw, int aci)
363f1d2b4d3SLarry Finger {
364f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
365f1d2b4d3SLarry Finger 
366f1d2b4d3SLarry Finger 	rtl92c_dm_init_edca_turbo(hw);
367f1d2b4d3SLarry Finger 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM, (u8 *)&aci);
368f1d2b4d3SLarry Finger }
369f1d2b4d3SLarry Finger 
rtl92c_init_driver_info_size(struct ieee80211_hw * hw,u8 size)370f1d2b4d3SLarry Finger void rtl92c_init_driver_info_size(struct ieee80211_hw *hw, u8 size)
371f1d2b4d3SLarry Finger {
372f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
3736d255202SLarry Finger 
374f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_RX_DRVINFO_SZ, size);
375f1d2b4d3SLarry Finger }
376f1d2b4d3SLarry Finger 
rtl92c_set_network_type(struct ieee80211_hw * hw,enum nl80211_iftype type)377f1d2b4d3SLarry Finger int rtl92c_set_network_type(struct ieee80211_hw *hw, enum nl80211_iftype type)
378f1d2b4d3SLarry Finger {
379f1d2b4d3SLarry Finger 	u8 value;
380f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
381f1d2b4d3SLarry Finger 
382f1d2b4d3SLarry Finger 	switch (type) {
383f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_UNSPECIFIED:
384f1d2b4d3SLarry Finger 		value = NT_NO_LINK;
38534d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
386f1d2b4d3SLarry Finger 			"Set Network type to NO LINK!\n");
387f1d2b4d3SLarry Finger 		break;
388f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_ADHOC:
389f1d2b4d3SLarry Finger 		value = NT_LINK_AD_HOC;
39034d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
391f1d2b4d3SLarry Finger 			"Set Network type to Ad Hoc!\n");
392f1d2b4d3SLarry Finger 		break;
393f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_STATION:
394f1d2b4d3SLarry Finger 		value = NT_LINK_AP;
39534d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
396f1d2b4d3SLarry Finger 			"Set Network type to STA!\n");
397f1d2b4d3SLarry Finger 		break;
398f1d2b4d3SLarry Finger 	case NL80211_IFTYPE_AP:
399f1d2b4d3SLarry Finger 		value = NT_AS_AP;
40034d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
401f1d2b4d3SLarry Finger 			"Set Network type to AP!\n");
402f1d2b4d3SLarry Finger 		break;
403f1d2b4d3SLarry Finger 	default:
40434d7f007SLarry Finger 		rtl_dbg(rtlpriv, COMP_INIT, DBG_DMESG,
405f1d2b4d3SLarry Finger 			"Network type %d not supported!\n", type);
406f1d2b4d3SLarry Finger 		return -EOPNOTSUPP;
407f1d2b4d3SLarry Finger 	}
408f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, MSR, value);
409f1d2b4d3SLarry Finger 	return 0;
410f1d2b4d3SLarry Finger }
411f1d2b4d3SLarry Finger 
rtl92c_init_network_type(struct ieee80211_hw * hw)412f1d2b4d3SLarry Finger void rtl92c_init_network_type(struct ieee80211_hw *hw)
413f1d2b4d3SLarry Finger {
414f1d2b4d3SLarry Finger 	rtl92c_set_network_type(hw, NL80211_IFTYPE_UNSPECIFIED);
415f1d2b4d3SLarry Finger }
416f1d2b4d3SLarry Finger 
rtl92c_init_adaptive_ctrl(struct ieee80211_hw * hw)417f1d2b4d3SLarry Finger void rtl92c_init_adaptive_ctrl(struct ieee80211_hw *hw)
418f1d2b4d3SLarry Finger {
419f1d2b4d3SLarry Finger 	u16	value16;
420f1d2b4d3SLarry Finger 	u32	value32;
421f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
422f1d2b4d3SLarry Finger 
423f1d2b4d3SLarry Finger 	/* Response Rate Set */
424f1d2b4d3SLarry Finger 	value32 = rtl_read_dword(rtlpriv, REG_RRSR);
425f1d2b4d3SLarry Finger 	value32 &= ~RATE_BITMAP_ALL;
426f1d2b4d3SLarry Finger 	value32 |= RATE_RRSR_CCK_ONLY_1M;
427f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_RRSR, value32);
428f1d2b4d3SLarry Finger 	/* SIFS (used in NAV) */
429f1d2b4d3SLarry Finger 	value16 = _SPEC_SIFS_CCK(0x10) | _SPEC_SIFS_OFDM(0x10);
430f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv,  REG_SPEC_SIFS, value16);
431f1d2b4d3SLarry Finger 	/* Retry Limit */
432f1d2b4d3SLarry Finger 	value16 = _LRL(0x30) | _SRL(0x30);
433f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_RL, value16);
434f1d2b4d3SLarry Finger }
435f1d2b4d3SLarry Finger 
rtl92c_init_rate_fallback(struct ieee80211_hw * hw)436f1d2b4d3SLarry Finger void rtl92c_init_rate_fallback(struct ieee80211_hw *hw)
437f1d2b4d3SLarry Finger {
438f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
439f1d2b4d3SLarry Finger 
440f1d2b4d3SLarry Finger 	/* Set Data Auto Rate Fallback Retry Count register. */
441f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_DARFRC, 0x00000000);
442f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_DARFRC+4, 0x10080404);
443f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_RARFRC, 0x04030201);
444f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv,  REG_RARFRC+4, 0x08070605);
445f1d2b4d3SLarry Finger }
446f1d2b4d3SLarry Finger 
rtl92c_set_cck_sifs(struct ieee80211_hw * hw,u8 trx_sifs,u8 ctx_sifs)447f1d2b4d3SLarry Finger static void rtl92c_set_cck_sifs(struct ieee80211_hw *hw, u8 trx_sifs,
448f1d2b4d3SLarry Finger 				u8 ctx_sifs)
449f1d2b4d3SLarry Finger {
450f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
451f1d2b4d3SLarry Finger 
452f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_SIFS_CCK, trx_sifs);
453f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, (REG_SIFS_CCK + 1), ctx_sifs);
454f1d2b4d3SLarry Finger }
455f1d2b4d3SLarry Finger 
rtl92c_set_ofdm_sifs(struct ieee80211_hw * hw,u8 trx_sifs,u8 ctx_sifs)456f1d2b4d3SLarry Finger static void rtl92c_set_ofdm_sifs(struct ieee80211_hw *hw, u8 trx_sifs,
457f1d2b4d3SLarry Finger 				 u8 ctx_sifs)
458f1d2b4d3SLarry Finger {
459f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
460f1d2b4d3SLarry Finger 
461f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_SIFS_OFDM, trx_sifs);
462f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, (REG_SIFS_OFDM + 1), ctx_sifs);
463f1d2b4d3SLarry Finger }
464f1d2b4d3SLarry Finger 
rtl92c_init_edca_param(struct ieee80211_hw * hw,u16 queue,u16 txop,u8 cw_min,u8 cw_max,u8 aifs)465f1d2b4d3SLarry Finger void rtl92c_init_edca_param(struct ieee80211_hw *hw,
466f1d2b4d3SLarry Finger 			    u16 queue, u16 txop, u8 cw_min, u8 cw_max, u8 aifs)
467f1d2b4d3SLarry Finger {
468f1d2b4d3SLarry Finger 	/* sequence: VO, VI, BE, BK ==> the same as 92C hardware design.
469f1d2b4d3SLarry Finger 	 * referenc : enum nl80211_txq_q or ieee80211_set_wmm_default function.
470f1d2b4d3SLarry Finger 	 */
471f1d2b4d3SLarry Finger 	u32 value;
472f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
473f1d2b4d3SLarry Finger 
474f1d2b4d3SLarry Finger 	value = (u32)aifs;
475f1d2b4d3SLarry Finger 	value |= ((u32)cw_min & 0xF) << 8;
476f1d2b4d3SLarry Finger 	value |= ((u32)cw_max & 0xF) << 12;
477f1d2b4d3SLarry Finger 	value |= (u32)txop << 16;
478f1d2b4d3SLarry Finger 	/* 92C hardware register sequence is the same as queue number. */
479f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, (REG_EDCA_VO_PARAM + (queue * 4)), value);
480f1d2b4d3SLarry Finger }
481f1d2b4d3SLarry Finger 
rtl92c_init_edca(struct ieee80211_hw * hw)482f1d2b4d3SLarry Finger void rtl92c_init_edca(struct ieee80211_hw *hw)
483f1d2b4d3SLarry Finger {
484f1d2b4d3SLarry Finger 	u16 value16;
485f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
486f1d2b4d3SLarry Finger 
487f1d2b4d3SLarry Finger 	/* disable EDCCA count down, to reduce collison and retry */
488f1d2b4d3SLarry Finger 	value16 = rtl_read_word(rtlpriv, REG_RD_CTRL);
489f1d2b4d3SLarry Finger 	value16 |= DIS_EDCA_CNT_DWN;
490f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_RD_CTRL, value16);
491f1d2b4d3SLarry Finger 	/* Update SIFS timing.  ??????????
492f1d2b4d3SLarry Finger 	 * pHalData->SifsTime = 0x0e0e0a0a; */
493f1d2b4d3SLarry Finger 	rtl92c_set_cck_sifs(hw, 0xa, 0xa);
494f1d2b4d3SLarry Finger 	rtl92c_set_ofdm_sifs(hw, 0xe, 0xe);
495f1d2b4d3SLarry Finger 	/* Set CCK/OFDM SIFS to be 10us. */
496f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_SIFS_CCK, 0x0a0a);
497f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_SIFS_OFDM, 0x1010);
498f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_PROT_MODE_CTRL, 0x0204);
499f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_BAR_MODE_CTRL, 0x014004);
500f1d2b4d3SLarry Finger 	/* TXOP */
501f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_BE_PARAM, 0x005EA42B);
502f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM, 0x0000A44F);
503f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM, 0x005EA324);
504f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM, 0x002FA226);
505f1d2b4d3SLarry Finger 	/* PIFS */
506f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_PIFS, 0x1C);
507f1d2b4d3SLarry Finger 	/* AGGR BREAK TIME Register */
508f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
509f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0040);
510f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_BCNDMATIM, 0x02);
511f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_ATIMWND, 0x02);
512f1d2b4d3SLarry Finger }
513f1d2b4d3SLarry Finger 
rtl92c_init_ampdu_aggregation(struct ieee80211_hw * hw)514f1d2b4d3SLarry Finger void rtl92c_init_ampdu_aggregation(struct ieee80211_hw *hw)
515f1d2b4d3SLarry Finger {
516f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
517f1d2b4d3SLarry Finger 
518f1d2b4d3SLarry Finger 	rtl_write_dword(rtlpriv, REG_AGGLEN_LMT, 0x99997631);
519f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
520f1d2b4d3SLarry Finger 	/* init AMPDU aggregation number, tuning for Tx's TP, */
521f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, 0x4CA, 0x0708);
522f1d2b4d3SLarry Finger }
523f1d2b4d3SLarry Finger 
rtl92c_init_beacon_max_error(struct ieee80211_hw * hw)524f1d2b4d3SLarry Finger void rtl92c_init_beacon_max_error(struct ieee80211_hw *hw)
525f1d2b4d3SLarry Finger {
526f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
527f1d2b4d3SLarry Finger 
528f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_BCN_MAX_ERR, 0xFF);
529f1d2b4d3SLarry Finger }
530f1d2b4d3SLarry Finger 
rtl92c_init_rdg_setting(struct ieee80211_hw * hw)531f1d2b4d3SLarry Finger void rtl92c_init_rdg_setting(struct ieee80211_hw *hw)
532f1d2b4d3SLarry Finger {
533f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
534f1d2b4d3SLarry Finger 
535f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_RD_CTRL, 0xFF);
536f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_RD_NAV_NXT, 0x200);
537f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_RD_RESP_PKT_TH, 0x05);
538f1d2b4d3SLarry Finger }
539f1d2b4d3SLarry Finger 
rtl92c_init_retry_function(struct ieee80211_hw * hw)540f1d2b4d3SLarry Finger void rtl92c_init_retry_function(struct ieee80211_hw *hw)
541f1d2b4d3SLarry Finger {
542f1d2b4d3SLarry Finger 	u8	value8;
543f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
544f1d2b4d3SLarry Finger 
545f1d2b4d3SLarry Finger 	value8 = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL);
546f1d2b4d3SLarry Finger 	value8 |= EN_AMPDU_RTY_NEW;
547f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL, value8);
548f1d2b4d3SLarry Finger 	/* Set ACK timeout */
549f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_ACKTO, 0x40);
550f1d2b4d3SLarry Finger }
551f1d2b4d3SLarry Finger 
rtl92c_disable_fast_edca(struct ieee80211_hw * hw)552f1d2b4d3SLarry Finger void rtl92c_disable_fast_edca(struct ieee80211_hw *hw)
553f1d2b4d3SLarry Finger {
554f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
555f1d2b4d3SLarry Finger 
556f1d2b4d3SLarry Finger 	rtl_write_word(rtlpriv, REG_FAST_EDCA_CTRL, 0);
557f1d2b4d3SLarry Finger }
558f1d2b4d3SLarry Finger 
rtl92c_set_min_space(struct ieee80211_hw * hw,bool is2T)559f1d2b4d3SLarry Finger void rtl92c_set_min_space(struct ieee80211_hw *hw, bool is2T)
560f1d2b4d3SLarry Finger {
561f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
562f1d2b4d3SLarry Finger 	u8 value = is2T ? MAX_MSS_DENSITY_2T : MAX_MSS_DENSITY_1T;
563f1d2b4d3SLarry Finger 
564f1d2b4d3SLarry Finger 	rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE, value);
565f1d2b4d3SLarry Finger }
566f1d2b4d3SLarry Finger 
567f1d2b4d3SLarry Finger /*==============================================================*/
568f1d2b4d3SLarry 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)569f1d2b4d3SLarry Finger static void _rtl92c_query_rxphystatus(struct ieee80211_hw *hw,
570f1d2b4d3SLarry Finger 				      struct rtl_stats *pstats,
571f1d2b4d3SLarry Finger 				      struct rx_desc_92c *p_desc,
572f1d2b4d3SLarry Finger 				      struct rx_fwinfo_92c *p_drvinfo,
573f1d2b4d3SLarry Finger 				      bool packet_match_bssid,
574f1d2b4d3SLarry Finger 				      bool packet_toself,
575f1d2b4d3SLarry Finger 				      bool packet_beacon)
576f1d2b4d3SLarry Finger {
577f1d2b4d3SLarry Finger 	struct rtl_priv *rtlpriv = rtl_priv(hw);
578f1d2b4d3SLarry Finger 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
579f1d2b4d3SLarry Finger 	struct phy_sts_cck_8192s_t *cck_buf;
580f1d2b4d3SLarry Finger 	s8 rx_pwr_all = 0, rx_pwr[4];
581f1d2b4d3SLarry Finger 	u8 rf_rx_num = 0, evm, pwdb_all;
582f1d2b4d3SLarry Finger 	u8 i, max_spatial_stream;
583f1d2b4d3SLarry Finger 	u32 rssi, total_rssi = 0;
584f1d2b4d3SLarry Finger 	bool in_powersavemode = false;
585f1d2b4d3SLarry Finger 	bool is_cck_rate;
586afd708f7SLarry Finger 	__le32 *pdesc = (__le32 *)p_desc;
587f1d2b4d3SLarry Finger 
588f1d2b4d3SLarry Finger 	is_cck_rate = RX_HAL_IS_CCK_RATE(p_desc->rxmcs);
589f1d2b4d3SLarry Finger 	pstats->packet_matchbssid = packet_match_bssid;
590f1d2b4d3SLarry Finger 	pstats->packet_toself = packet_toself;
591f1d2b4d3SLarry Finger 	pstats->packet_beacon = packet_beacon;
592f1d2b4d3SLarry Finger 	pstats->is_cck = is_cck_rate;
593f1d2b4d3SLarry Finger 	pstats->RX_SIGQ[0] = -1;
594f1d2b4d3SLarry Finger 	pstats->RX_SIGQ[1] = -1;
595f1d2b4d3SLarry Finger 	if (is_cck_rate) {
596f1d2b4d3SLarry Finger 		u8 report, cck_highpwr;
5976d255202SLarry Finger 
598f1d2b4d3SLarry Finger 		cck_buf = (struct phy_sts_cck_8192s_t *)p_drvinfo;
599f1d2b4d3SLarry Finger 		if (!in_powersavemode)
600f1d2b4d3SLarry Finger 			cck_highpwr = rtlphy->cck_high_power;
601f1d2b4d3SLarry Finger 		else
602f1d2b4d3SLarry Finger 			cck_highpwr = false;
603f1d2b4d3SLarry Finger 		if (!cck_highpwr) {
604f1d2b4d3SLarry Finger 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
6056d255202SLarry Finger 
606f1d2b4d3SLarry Finger 			report = cck_buf->cck_agc_rpt & 0xc0;
607f1d2b4d3SLarry Finger 			report = report >> 6;
608f1d2b4d3SLarry Finger 			switch (report) {
609f1d2b4d3SLarry Finger 			case 0x3:
610f1d2b4d3SLarry Finger 				rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
611f1d2b4d3SLarry Finger 				break;
612f1d2b4d3SLarry Finger 			case 0x2:
613f1d2b4d3SLarry Finger 				rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
614f1d2b4d3SLarry Finger 				break;
615f1d2b4d3SLarry Finger 			case 0x1:
616f1d2b4d3SLarry Finger 				rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
617f1d2b4d3SLarry Finger 				break;
618f1d2b4d3SLarry Finger 			case 0x0:
619f1d2b4d3SLarry Finger 				rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
620f1d2b4d3SLarry Finger 				break;
621f1d2b4d3SLarry Finger 			}
622f1d2b4d3SLarry Finger 		} else {
623f1d2b4d3SLarry Finger 			u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
6246d255202SLarry Finger 
625f1d2b4d3SLarry Finger 			report = p_drvinfo->cfosho[0] & 0x60;
626f1d2b4d3SLarry Finger 			report = report >> 5;
627f1d2b4d3SLarry Finger 			switch (report) {
628f1d2b4d3SLarry Finger 			case 0x3:
629f1d2b4d3SLarry Finger 				rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f) << 1);
630f1d2b4d3SLarry Finger 				break;
631f1d2b4d3SLarry Finger 			case 0x2:
632f1d2b4d3SLarry Finger 				rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f) << 1);
633f1d2b4d3SLarry Finger 				break;
634f1d2b4d3SLarry Finger 			case 0x1:
635f1d2b4d3SLarry Finger 				rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f) << 1);
636f1d2b4d3SLarry Finger 				break;
637f1d2b4d3SLarry Finger 			case 0x0:
638f1d2b4d3SLarry Finger 				rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f) << 1);
639f1d2b4d3SLarry Finger 				break;
640f1d2b4d3SLarry Finger 			}
641f1d2b4d3SLarry Finger 		}
642b8a19dd6SMichael Straube 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
643f1d2b4d3SLarry Finger 		pstats->rx_pwdb_all = pwdb_all;
644f1d2b4d3SLarry Finger 		pstats->recvsignalpower = rx_pwr_all;
645f1d2b4d3SLarry Finger 		if (packet_match_bssid) {
646f1d2b4d3SLarry Finger 			u8 sq;
6476d255202SLarry Finger 
648f1d2b4d3SLarry Finger 			if (pstats->rx_pwdb_all > 40)
649f1d2b4d3SLarry Finger 				sq = 100;
650f1d2b4d3SLarry Finger 			else {
651f1d2b4d3SLarry Finger 				sq = cck_buf->sq_rpt;
652f1d2b4d3SLarry Finger 				if (sq > 64)
653f1d2b4d3SLarry Finger 					sq = 0;
654f1d2b4d3SLarry Finger 				else if (sq < 20)
655f1d2b4d3SLarry Finger 					sq = 100;
656f1d2b4d3SLarry Finger 				else
657f1d2b4d3SLarry Finger 					sq = ((64 - sq) * 100) / 44;
658f1d2b4d3SLarry Finger 			}
659f1d2b4d3SLarry Finger 			pstats->signalquality = sq;
660f1d2b4d3SLarry Finger 			pstats->RX_SIGQ[0] = sq;
661f1d2b4d3SLarry Finger 			pstats->RX_SIGQ[1] = -1;
662f1d2b4d3SLarry Finger 		}
663f1d2b4d3SLarry Finger 	} else {
664f1d2b4d3SLarry Finger 		rtlpriv->dm.rfpath_rxenable[0] =
665f1d2b4d3SLarry Finger 		    rtlpriv->dm.rfpath_rxenable[1] = true;
666f1d2b4d3SLarry Finger 		for (i = RF90_PATH_A; i < RF90_PATH_MAX; i++) {
667f1d2b4d3SLarry Finger 			if (rtlpriv->dm.rfpath_rxenable[i])
668f1d2b4d3SLarry Finger 				rf_rx_num++;
669f1d2b4d3SLarry Finger 			rx_pwr[i] =
670f1d2b4d3SLarry Finger 			    ((p_drvinfo->gain_trsw[i] & 0x3f) * 2) - 110;
671b8a19dd6SMichael Straube 			rssi = rtl_query_rxpwrpercentage(rx_pwr[i]);
672f1d2b4d3SLarry Finger 			total_rssi += rssi;
673f1d2b4d3SLarry Finger 			rtlpriv->stats.rx_snr_db[i] =
674f1d2b4d3SLarry Finger 			    (long)(p_drvinfo->rxsnr[i] / 2);
675f1d2b4d3SLarry Finger 
676f1d2b4d3SLarry Finger 			if (packet_match_bssid)
677f1d2b4d3SLarry Finger 				pstats->rx_mimo_signalstrength[i] = (u8) rssi;
678f1d2b4d3SLarry Finger 		}
679f1d2b4d3SLarry Finger 		rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
680b8a19dd6SMichael Straube 		pwdb_all = rtl_query_rxpwrpercentage(rx_pwr_all);
681f1d2b4d3SLarry Finger 		pstats->rx_pwdb_all = pwdb_all;
682f1d2b4d3SLarry Finger 		pstats->rxpower = rx_pwr_all;
683f1d2b4d3SLarry Finger 		pstats->recvsignalpower = rx_pwr_all;
6843925ae06SLarry Finger 		if (get_rx_desc_rx_mcs(pdesc) &&
6853925ae06SLarry Finger 		    get_rx_desc_rx_mcs(pdesc) >= DESC_RATEMCS8 &&
6863925ae06SLarry Finger 		    get_rx_desc_rx_mcs(pdesc) <= DESC_RATEMCS15)
687f1d2b4d3SLarry Finger 			max_spatial_stream = 2;
688f1d2b4d3SLarry Finger 		else
689f1d2b4d3SLarry Finger 			max_spatial_stream = 1;
690f1d2b4d3SLarry Finger 		for (i = 0; i < max_spatial_stream; i++) {
691622c19edSMichael Straube 			evm = rtl_evm_db_to_percentage(p_drvinfo->rxevm[i]);
692f1d2b4d3SLarry Finger 			if (packet_match_bssid) {
693f1d2b4d3SLarry Finger 				if (i == 0)
694f1d2b4d3SLarry Finger 					pstats->signalquality =
695f1d2b4d3SLarry Finger 					    (u8) (evm & 0xff);
696f1d2b4d3SLarry Finger 				pstats->RX_SIGQ[i] =
697f1d2b4d3SLarry Finger 				    (u8) (evm & 0xff);
698f1d2b4d3SLarry Finger 			}
699f1d2b4d3SLarry Finger 		}
700f1d2b4d3SLarry Finger 	}
701f1d2b4d3SLarry Finger 	if (is_cck_rate)
702f1d2b4d3SLarry Finger 		pstats->signalstrength =
703fef91a2bSMichael Straube 		    (u8)(rtl_signal_scale_mapping(hw, pwdb_all));
704f1d2b4d3SLarry Finger 	else if (rf_rx_num != 0)
705f1d2b4d3SLarry Finger 		pstats->signalstrength =
706fef91a2bSMichael Straube 		    (u8)(rtl_signal_scale_mapping(hw, total_rssi /= rf_rx_num));
707f1d2b4d3SLarry Finger }
708f1d2b4d3SLarry 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)709f1d2b4d3SLarry Finger void rtl92c_translate_rx_signal_stuff(struct ieee80211_hw *hw,
710f1d2b4d3SLarry Finger 					       struct sk_buff *skb,
711f1d2b4d3SLarry Finger 					       struct rtl_stats *pstats,
712f1d2b4d3SLarry Finger 					       struct rx_desc_92c *pdesc,
713f1d2b4d3SLarry Finger 					       struct rx_fwinfo_92c *p_drvinfo)
714f1d2b4d3SLarry Finger {
715f1d2b4d3SLarry Finger 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
716f1d2b4d3SLarry Finger 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
717f1d2b4d3SLarry Finger 	struct ieee80211_hdr *hdr;
718f1d2b4d3SLarry Finger 	u8 *tmp_buf;
719f1d2b4d3SLarry Finger 	u8 *praddr;
720f1d2b4d3SLarry Finger 	__le16 fc;
721f1d2b4d3SLarry Finger 	u16 type, cpu_fc;
722f1d2b4d3SLarry Finger 	bool packet_matchbssid, packet_toself, packet_beacon = false;
723f1d2b4d3SLarry Finger 
724f1d2b4d3SLarry Finger 	tmp_buf = skb->data + pstats->rx_drvinfo_size + pstats->rx_bufshift;
725f1d2b4d3SLarry Finger 	hdr = (struct ieee80211_hdr *)tmp_buf;
726f1d2b4d3SLarry Finger 	fc = hdr->frame_control;
727f1d2b4d3SLarry Finger 	cpu_fc = le16_to_cpu(fc);
728f1d2b4d3SLarry Finger 	type = WLAN_FC_GET_TYPE(fc);
729f1d2b4d3SLarry Finger 	praddr = hdr->addr1;
730f1d2b4d3SLarry Finger 	packet_matchbssid =
731f1d2b4d3SLarry Finger 	    ((IEEE80211_FTYPE_CTL != type) &&
732f1d2b4d3SLarry Finger 	     ether_addr_equal(mac->bssid,
733f1d2b4d3SLarry Finger 			      (cpu_fc & IEEE80211_FCTL_TODS) ? hdr->addr1 :
734f1d2b4d3SLarry Finger 			      (cpu_fc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 :
735f1d2b4d3SLarry Finger 			      hdr->addr3) &&
736f1d2b4d3SLarry Finger 	     (!pstats->hwerror) && (!pstats->crc) && (!pstats->icv));
737f1d2b4d3SLarry Finger 
738f1d2b4d3SLarry Finger 	packet_toself = packet_matchbssid &&
739f1d2b4d3SLarry Finger 	    ether_addr_equal(praddr, rtlefuse->dev_addr);
740f1d2b4d3SLarry Finger 	if (ieee80211_is_beacon(fc))
741f1d2b4d3SLarry Finger 		packet_beacon = true;
742f1d2b4d3SLarry Finger 	_rtl92c_query_rxphystatus(hw, pstats, pdesc, p_drvinfo,
743f1d2b4d3SLarry Finger 				   packet_matchbssid, packet_toself,
744f1d2b4d3SLarry Finger 				   packet_beacon);
745f1d2b4d3SLarry Finger 	rtl_process_phyinfo(hw, tmp_buf, pstats);
746f1d2b4d3SLarry Finger }
747