1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009-2012  Realtek Corporation.*/
3 
4 #include "../wifi.h"
5 #include "../efuse.h"
6 #include "../base.h"
7 #include "../regd.h"
8 #include "../cam.h"
9 #include "../ps.h"
10 #include "../pci.h"
11 #include "reg.h"
12 #include "def.h"
13 #include "phy.h"
14 #include "../rtl8723com/phy_common.h"
15 #include "dm.h"
16 #include "../rtl8723com/dm_common.h"
17 #include "fw.h"
18 #include "../rtl8723com/fw_common.h"
19 #include "led.h"
20 #include "hw.h"
21 #include "../pwrseqcmd.h"
22 #include "pwrseq.h"
23 #include "btc.h"
24 
25 #define LLT_CONFIG	5
26 
27 static void _rtl8723e_set_bcn_ctrl_reg(struct ieee80211_hw *hw,
28 				       u8 set_bits, u8 clear_bits)
29 {
30 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
31 	struct rtl_priv *rtlpriv = rtl_priv(hw);
32 
33 	rtlpci->reg_bcn_ctrl_val |= set_bits;
34 	rtlpci->reg_bcn_ctrl_val &= ~clear_bits;
35 
36 	rtl_write_byte(rtlpriv, REG_BCN_CTRL, (u8) rtlpci->reg_bcn_ctrl_val);
37 }
38 
39 static void _rtl8723e_stop_tx_beacon(struct ieee80211_hw *hw)
40 {
41 	struct rtl_priv *rtlpriv = rtl_priv(hw);
42 	u8 tmp1byte;
43 
44 	tmp1byte = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
45 	rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2, tmp1byte & (~BIT(6)));
46 	rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0x64);
47 	tmp1byte = rtl_read_byte(rtlpriv, REG_TBTT_PROHIBIT + 2);
48 	tmp1byte &= ~(BIT(0));
49 	rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 2, tmp1byte);
50 }
51 
52 static void _rtl8723e_resume_tx_beacon(struct ieee80211_hw *hw)
53 {
54 	struct rtl_priv *rtlpriv = rtl_priv(hw);
55 	u8 tmp1byte;
56 
57 	tmp1byte = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2);
58 	rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2, tmp1byte | BIT(6));
59 	rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0xff);
60 	tmp1byte = rtl_read_byte(rtlpriv, REG_TBTT_PROHIBIT + 2);
61 	tmp1byte |= BIT(1);
62 	rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 2, tmp1byte);
63 }
64 
65 static void _rtl8723e_enable_bcn_sub_func(struct ieee80211_hw *hw)
66 {
67 	_rtl8723e_set_bcn_ctrl_reg(hw, 0, BIT(1));
68 }
69 
70 static void _rtl8723e_disable_bcn_sub_func(struct ieee80211_hw *hw)
71 {
72 	_rtl8723e_set_bcn_ctrl_reg(hw, BIT(1), 0);
73 }
74 
75 void rtl8723e_get_hw_reg(struct ieee80211_hw *hw, u8 variable, u8 *val)
76 {
77 	struct rtl_priv *rtlpriv = rtl_priv(hw);
78 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
79 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
80 
81 	switch (variable) {
82 	case HW_VAR_RCR:
83 		*((u32 *)(val)) = rtlpci->receive_config;
84 		break;
85 	case HW_VAR_RF_STATE:
86 		*((enum rf_pwrstate *)(val)) = ppsc->rfpwr_state;
87 		break;
88 	case HW_VAR_FWLPS_RF_ON:{
89 			enum rf_pwrstate rfstate;
90 			u32 val_rcr;
91 
92 			rtlpriv->cfg->ops->get_hw_reg(hw,
93 						      HW_VAR_RF_STATE,
94 						      (u8 *)(&rfstate));
95 			if (rfstate == ERFOFF) {
96 				*((bool *)(val)) = true;
97 			} else {
98 				val_rcr = rtl_read_dword(rtlpriv, REG_RCR);
99 				val_rcr &= 0x00070000;
100 				if (val_rcr)
101 					*((bool *)(val)) = false;
102 				else
103 					*((bool *)(val)) = true;
104 			}
105 			break;
106 		}
107 	case HW_VAR_FW_PSMODE_STATUS:
108 		*((bool *)(val)) = ppsc->fw_current_inpsmode;
109 		break;
110 	case HW_VAR_CORRECT_TSF:{
111 			u64 tsf;
112 			u32 *ptsf_low = (u32 *)&tsf;
113 			u32 *ptsf_high = ((u32 *)&tsf) + 1;
114 
115 			*ptsf_high = rtl_read_dword(rtlpriv, (REG_TSFTR + 4));
116 			*ptsf_low = rtl_read_dword(rtlpriv, REG_TSFTR);
117 
118 			*((u64 *)(val)) = tsf;
119 
120 			break;
121 		}
122 	case HAL_DEF_WOWLAN:
123 		break;
124 	default:
125 		RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
126 			 "switch case %#x not processed\n", variable);
127 		break;
128 	}
129 }
130 
131 void rtl8723e_set_hw_reg(struct ieee80211_hw *hw, u8 variable, u8 *val)
132 {
133 	struct rtl_priv *rtlpriv = rtl_priv(hw);
134 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
135 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
136 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
137 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
138 	u8 idx;
139 
140 	switch (variable) {
141 	case HW_VAR_ETHER_ADDR:{
142 			for (idx = 0; idx < ETH_ALEN; idx++) {
143 				rtl_write_byte(rtlpriv, (REG_MACID + idx),
144 					       val[idx]);
145 			}
146 			break;
147 		}
148 	case HW_VAR_BASIC_RATE:{
149 			u16 b_rate_cfg = ((u16 *)val)[0];
150 			u8 rate_index = 0;
151 
152 			b_rate_cfg = b_rate_cfg & 0x15f;
153 			b_rate_cfg |= 0x01;
154 			rtl_write_byte(rtlpriv, REG_RRSR, b_rate_cfg & 0xff);
155 			rtl_write_byte(rtlpriv, REG_RRSR + 1,
156 				       (b_rate_cfg >> 8) & 0xff);
157 			while (b_rate_cfg > 0x1) {
158 				b_rate_cfg = (b_rate_cfg >> 1);
159 				rate_index++;
160 			}
161 			rtl_write_byte(rtlpriv, REG_INIRTS_RATE_SEL,
162 				       rate_index);
163 			break;
164 		}
165 	case HW_VAR_BSSID:{
166 			for (idx = 0; idx < ETH_ALEN; idx++) {
167 				rtl_write_byte(rtlpriv, (REG_BSSID + idx),
168 					       val[idx]);
169 			}
170 			break;
171 		}
172 	case HW_VAR_SIFS:{
173 			rtl_write_byte(rtlpriv, REG_SIFS_CTX + 1, val[0]);
174 			rtl_write_byte(rtlpriv, REG_SIFS_TRX + 1, val[1]);
175 
176 			rtl_write_byte(rtlpriv, REG_SPEC_SIFS + 1, val[0]);
177 			rtl_write_byte(rtlpriv, REG_MAC_SPEC_SIFS + 1, val[0]);
178 
179 			if (!mac->ht_enable)
180 				rtl_write_word(rtlpriv, REG_RESP_SIFS_OFDM,
181 					       0x0e0e);
182 			else
183 				rtl_write_word(rtlpriv, REG_RESP_SIFS_OFDM,
184 					       *((u16 *)val));
185 			break;
186 		}
187 	case HW_VAR_SLOT_TIME:{
188 			u8 e_aci;
189 
190 			RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
191 				 "HW_VAR_SLOT_TIME %x\n", val[0]);
192 
193 			rtl_write_byte(rtlpriv, REG_SLOT, val[0]);
194 
195 			for (e_aci = 0; e_aci < AC_MAX; e_aci++) {
196 				rtlpriv->cfg->ops->set_hw_reg(hw,
197 							      HW_VAR_AC_PARAM,
198 							      (u8 *)(&e_aci));
199 			}
200 			break;
201 		}
202 	case HW_VAR_ACK_PREAMBLE:{
203 			u8 reg_tmp;
204 			u8 short_preamble = (bool)(*(u8 *)val);
205 
206 			reg_tmp = (mac->cur_40_prime_sc) << 5;
207 			if (short_preamble)
208 				reg_tmp |= 0x80;
209 
210 			rtl_write_byte(rtlpriv, REG_RRSR + 2, reg_tmp);
211 			break;
212 		}
213 	case HW_VAR_AMPDU_MIN_SPACE:{
214 			u8 min_spacing_to_set;
215 			u8 sec_min_space;
216 
217 			min_spacing_to_set = *((u8 *)val);
218 			if (min_spacing_to_set <= 7) {
219 				sec_min_space = 0;
220 
221 				if (min_spacing_to_set < sec_min_space)
222 					min_spacing_to_set = sec_min_space;
223 
224 				mac->min_space_cfg = ((mac->min_space_cfg &
225 						       0xf8) |
226 						      min_spacing_to_set);
227 
228 				*val = min_spacing_to_set;
229 
230 				RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
231 					 "Set HW_VAR_AMPDU_MIN_SPACE: %#x\n",
232 					  mac->min_space_cfg);
233 
234 				rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE,
235 					       mac->min_space_cfg);
236 			}
237 			break;
238 		}
239 	case HW_VAR_SHORTGI_DENSITY:{
240 			u8 density_to_set;
241 
242 			density_to_set = *((u8 *)val);
243 			mac->min_space_cfg |= (density_to_set << 3);
244 
245 			RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
246 				 "Set HW_VAR_SHORTGI_DENSITY: %#x\n",
247 				  mac->min_space_cfg);
248 
249 			rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE,
250 				       mac->min_space_cfg);
251 
252 			break;
253 		}
254 	case HW_VAR_AMPDU_FACTOR:{
255 			u8 regtoset_normal[4] = { 0x41, 0xa8, 0x72, 0xb9 };
256 			u8 regtoset_bt[4] = {0x31, 0x74, 0x42, 0x97};
257 			u8 factor_toset;
258 			u8 *p_regtoset = NULL;
259 			u8 index = 0;
260 
261 			if ((rtlpriv->btcoexist.bt_coexistence) &&
262 			    (rtlpriv->btcoexist.bt_coexist_type ==
263 				BT_CSR_BC4))
264 				p_regtoset = regtoset_bt;
265 			else
266 				p_regtoset = regtoset_normal;
267 
268 			factor_toset = *((u8 *)val);
269 			if (factor_toset <= 3) {
270 				factor_toset = (1 << (factor_toset + 2));
271 				if (factor_toset > 0xf)
272 					factor_toset = 0xf;
273 
274 				for (index = 0; index < 4; index++) {
275 					if ((p_regtoset[index] & 0xf0) >
276 					    (factor_toset << 4))
277 						p_regtoset[index] =
278 						    (p_regtoset[index] & 0x0f) |
279 						    (factor_toset << 4);
280 
281 					if ((p_regtoset[index] & 0x0f) >
282 					    factor_toset)
283 						p_regtoset[index] =
284 						    (p_regtoset[index] & 0xf0) |
285 						    (factor_toset);
286 
287 					rtl_write_byte(rtlpriv,
288 						       (REG_AGGLEN_LMT + index),
289 						       p_regtoset[index]);
290 				}
291 
292 				RT_TRACE(rtlpriv, COMP_MLME, DBG_LOUD,
293 					 "Set HW_VAR_AMPDU_FACTOR: %#x\n",
294 					  factor_toset);
295 			}
296 			break;
297 		}
298 	case HW_VAR_AC_PARAM:{
299 			u8 e_aci = *((u8 *)val);
300 
301 			rtl8723_dm_init_edca_turbo(hw);
302 
303 			if (rtlpci->acm_method != EACMWAY2_SW)
304 				rtlpriv->cfg->ops->set_hw_reg(hw,
305 							      HW_VAR_ACM_CTRL,
306 							      (u8 *)(&e_aci));
307 			break;
308 		}
309 	case HW_VAR_ACM_CTRL:{
310 			u8 e_aci = *((u8 *)val);
311 			union aci_aifsn *p_aci_aifsn =
312 			    (union aci_aifsn *)(&mac->ac[0].aifs);
313 			u8 acm = p_aci_aifsn->f.acm;
314 			u8 acm_ctrl = rtl_read_byte(rtlpriv, REG_ACMHWCTRL);
315 
316 			acm_ctrl =
317 			    acm_ctrl | ((rtlpci->acm_method == 2) ? 0x0 : 0x1);
318 
319 			if (acm) {
320 				switch (e_aci) {
321 				case AC0_BE:
322 					acm_ctrl |= ACMHW_BEQEN;
323 					break;
324 				case AC2_VI:
325 					acm_ctrl |= ACMHW_VIQEN;
326 					break;
327 				case AC3_VO:
328 					acm_ctrl |= ACMHW_VOQEN;
329 					break;
330 				default:
331 					RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
332 						 "HW_VAR_ACM_CTRL acm set failed: eACI is %d\n",
333 						 acm);
334 					break;
335 				}
336 			} else {
337 				switch (e_aci) {
338 				case AC0_BE:
339 					acm_ctrl &= (~ACMHW_BEQEN);
340 					break;
341 				case AC2_VI:
342 					acm_ctrl &= (~ACMHW_VIQEN);
343 					break;
344 				case AC3_VO:
345 					acm_ctrl &= (~ACMHW_VOQEN);
346 					break;
347 				default:
348 					RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
349 						 "switch case %#x not processed\n",
350 						 e_aci);
351 					break;
352 				}
353 			}
354 
355 			RT_TRACE(rtlpriv, COMP_QOS, DBG_TRACE,
356 				 "SetHwReg8190pci(): [HW_VAR_ACM_CTRL] Write 0x%X\n",
357 				 acm_ctrl);
358 			rtl_write_byte(rtlpriv, REG_ACMHWCTRL, acm_ctrl);
359 			break;
360 		}
361 	case HW_VAR_RCR:{
362 			rtl_write_dword(rtlpriv, REG_RCR, ((u32 *)(val))[0]);
363 			rtlpci->receive_config = ((u32 *)(val))[0];
364 			break;
365 		}
366 	case HW_VAR_RETRY_LIMIT:{
367 			u8 retry_limit = ((u8 *)(val))[0];
368 
369 			rtl_write_word(rtlpriv, REG_RL,
370 				       retry_limit << RETRY_LIMIT_SHORT_SHIFT |
371 				       retry_limit << RETRY_LIMIT_LONG_SHIFT);
372 			break;
373 		}
374 	case HW_VAR_DUAL_TSF_RST:
375 		rtl_write_byte(rtlpriv, REG_DUAL_TSF_RST, (BIT(0) | BIT(1)));
376 		break;
377 	case HW_VAR_EFUSE_BYTES:
378 		rtlefuse->efuse_usedbytes = *((u16 *)val);
379 		break;
380 	case HW_VAR_EFUSE_USAGE:
381 		rtlefuse->efuse_usedpercentage = *((u8 *)val);
382 		break;
383 	case HW_VAR_IO_CMD:
384 		rtl8723e_phy_set_io_cmd(hw, (*(enum io_type *)val));
385 		break;
386 	case HW_VAR_WPA_CONFIG:
387 		rtl_write_byte(rtlpriv, REG_SECCFG, *((u8 *)val));
388 		break;
389 	case HW_VAR_SET_RPWM:{
390 			u8 rpwm_val;
391 
392 			rpwm_val = rtl_read_byte(rtlpriv, REG_PCIE_HRPWM);
393 			udelay(1);
394 
395 			if (rpwm_val & BIT(7)) {
396 				rtl_write_byte(rtlpriv, REG_PCIE_HRPWM,
397 					       (*(u8 *)val));
398 			} else {
399 				rtl_write_byte(rtlpriv, REG_PCIE_HRPWM,
400 					       ((*(u8 *)val) | BIT(7)));
401 			}
402 
403 			break;
404 		}
405 	case HW_VAR_H2C_FW_PWRMODE:{
406 			u8 psmode = (*(u8 *)val);
407 
408 			if (psmode != FW_PS_ACTIVE_MODE)
409 				rtl8723e_dm_rf_saving(hw, true);
410 
411 			rtl8723e_set_fw_pwrmode_cmd(hw, (*(u8 *)val));
412 			break;
413 		}
414 	case HW_VAR_FW_PSMODE_STATUS:
415 		ppsc->fw_current_inpsmode = *((bool *)val);
416 		break;
417 	case HW_VAR_H2C_FW_JOINBSSRPT:{
418 			u8 mstatus = (*(u8 *)val);
419 			u8 tmp_regcr, tmp_reg422;
420 			bool b_recover = false;
421 
422 			if (mstatus == RT_MEDIA_CONNECT) {
423 				rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AID,
424 							      NULL);
425 
426 				tmp_regcr = rtl_read_byte(rtlpriv, REG_CR + 1);
427 				rtl_write_byte(rtlpriv, REG_CR + 1,
428 					       (tmp_regcr | BIT(0)));
429 
430 				_rtl8723e_set_bcn_ctrl_reg(hw, 0, BIT(3));
431 				_rtl8723e_set_bcn_ctrl_reg(hw, BIT(4), 0);
432 
433 				tmp_reg422 =
434 				    rtl_read_byte(rtlpriv,
435 						  REG_FWHW_TXQ_CTRL + 2);
436 				if (tmp_reg422 & BIT(6))
437 					b_recover = true;
438 				rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 2,
439 					       tmp_reg422 & (~BIT(6)));
440 
441 				rtl8723e_set_fw_rsvdpagepkt(hw, 0);
442 
443 				_rtl8723e_set_bcn_ctrl_reg(hw, BIT(3), 0);
444 				_rtl8723e_set_bcn_ctrl_reg(hw, 0, BIT(4));
445 
446 				if (b_recover) {
447 					rtl_write_byte(rtlpriv,
448 						       REG_FWHW_TXQ_CTRL + 2,
449 						       tmp_reg422);
450 				}
451 
452 				rtl_write_byte(rtlpriv, REG_CR + 1,
453 					       (tmp_regcr & ~(BIT(0))));
454 			}
455 			rtl8723e_set_fw_joinbss_report_cmd(hw, (*(u8 *)val));
456 
457 			break;
458 		}
459 	case HW_VAR_H2C_FW_P2P_PS_OFFLOAD:{
460 		rtl8723e_set_p2p_ps_offload_cmd(hw, (*(u8 *)val));
461 		break;
462 	}
463 	case HW_VAR_AID:{
464 			u16 u2btmp;
465 
466 			u2btmp = rtl_read_word(rtlpriv, REG_BCN_PSR_RPT);
467 			u2btmp &= 0xC000;
468 			rtl_write_word(rtlpriv, REG_BCN_PSR_RPT,
469 				       (u2btmp | mac->assoc_id));
470 
471 			break;
472 		}
473 	case HW_VAR_CORRECT_TSF:{
474 			u8 btype_ibss = ((u8 *)(val))[0];
475 
476 			if (btype_ibss)
477 				_rtl8723e_stop_tx_beacon(hw);
478 
479 			_rtl8723e_set_bcn_ctrl_reg(hw, 0, BIT(3));
480 
481 			rtl_write_dword(rtlpriv, REG_TSFTR,
482 					(u32)(mac->tsf & 0xffffffff));
483 			rtl_write_dword(rtlpriv, REG_TSFTR + 4,
484 					(u32)((mac->tsf >> 32) & 0xffffffff));
485 
486 			_rtl8723e_set_bcn_ctrl_reg(hw, BIT(3), 0);
487 
488 			if (btype_ibss)
489 				_rtl8723e_resume_tx_beacon(hw);
490 
491 			break;
492 		}
493 	case HW_VAR_FW_LPS_ACTION:{
494 			bool b_enter_fwlps = *((bool *)val);
495 			u8 rpwm_val, fw_pwrmode;
496 			bool fw_current_inps;
497 
498 			if (b_enter_fwlps) {
499 				rpwm_val = 0x02;	/* RF off */
500 				fw_current_inps = true;
501 				rtlpriv->cfg->ops->set_hw_reg(hw,
502 						HW_VAR_FW_PSMODE_STATUS,
503 						(u8 *)(&fw_current_inps));
504 				rtlpriv->cfg->ops->set_hw_reg(hw,
505 						HW_VAR_H2C_FW_PWRMODE,
506 						(u8 *)(&ppsc->fwctrl_psmode));
507 
508 				rtlpriv->cfg->ops->set_hw_reg(hw,
509 						HW_VAR_SET_RPWM,
510 						(u8 *)(&rpwm_val));
511 			} else {
512 				rpwm_val = 0x0C;	/* RF on */
513 				fw_pwrmode = FW_PS_ACTIVE_MODE;
514 				fw_current_inps = false;
515 				rtlpriv->cfg->ops->set_hw_reg(hw,
516 							      HW_VAR_SET_RPWM,
517 							      (u8 *)(&rpwm_val));
518 				rtlpriv->cfg->ops->set_hw_reg(hw,
519 						HW_VAR_H2C_FW_PWRMODE,
520 						(u8 *)(&fw_pwrmode));
521 
522 				rtlpriv->cfg->ops->set_hw_reg(hw,
523 						HW_VAR_FW_PSMODE_STATUS,
524 						(u8 *)(&fw_current_inps));
525 			}
526 			 break;
527 		}
528 	default:
529 		RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
530 			 "switch case %#x not processed\n", variable);
531 		break;
532 	}
533 }
534 
535 static bool _rtl8723e_llt_write(struct ieee80211_hw *hw, u32 address, u32 data)
536 {
537 	struct rtl_priv *rtlpriv = rtl_priv(hw);
538 	bool status = true;
539 	long count = 0;
540 	u32 value = _LLT_INIT_ADDR(address) |
541 	    _LLT_INIT_DATA(data) | _LLT_OP(_LLT_WRITE_ACCESS);
542 
543 	rtl_write_dword(rtlpriv, REG_LLT_INIT, value);
544 
545 	do {
546 		value = rtl_read_dword(rtlpriv, REG_LLT_INIT);
547 		if (_LLT_NO_ACTIVE == _LLT_OP_VALUE(value))
548 			break;
549 
550 		if (count > POLLING_LLT_THRESHOLD) {
551 			pr_err("Failed to polling write LLT done at address %d!\n",
552 			       address);
553 			status = false;
554 			break;
555 		}
556 	} while (++count);
557 
558 	return status;
559 }
560 
561 static bool _rtl8723e_llt_table_init(struct ieee80211_hw *hw)
562 {
563 	struct rtl_priv *rtlpriv = rtl_priv(hw);
564 	unsigned short i;
565 	u8 txpktbuf_bndy;
566 	u8 maxpage;
567 	bool status;
568 	u8 ubyte;
569 
570 #if LLT_CONFIG == 1
571 	maxpage = 255;
572 	txpktbuf_bndy = 252;
573 #elif LLT_CONFIG == 2
574 	maxpage = 127;
575 	txpktbuf_bndy = 124;
576 #elif LLT_CONFIG == 3
577 	maxpage = 255;
578 	txpktbuf_bndy = 174;
579 #elif LLT_CONFIG == 4
580 	maxpage = 255;
581 	txpktbuf_bndy = 246;
582 #elif LLT_CONFIG == 5
583 	maxpage = 255;
584 	txpktbuf_bndy = 246;
585 #endif
586 
587 	rtl_write_byte(rtlpriv, REG_CR, 0x8B);
588 
589 #if LLT_CONFIG == 1
590 	rtl_write_byte(rtlpriv, REG_RQPN_NPQ, 0x1c);
591 	rtl_write_dword(rtlpriv, REG_RQPN, 0x80a71c1c);
592 #elif LLT_CONFIG == 2
593 	rtl_write_dword(rtlpriv, REG_RQPN, 0x845B1010);
594 #elif LLT_CONFIG == 3
595 	rtl_write_dword(rtlpriv, REG_RQPN, 0x84838484);
596 #elif LLT_CONFIG == 4
597 	rtl_write_dword(rtlpriv, REG_RQPN, 0x80bd1c1c);
598 #elif LLT_CONFIG == 5
599 	rtl_write_word(rtlpriv, REG_RQPN_NPQ, 0x0000);
600 
601 	rtl_write_dword(rtlpriv, REG_RQPN, 0x80ac1c29);
602 	rtl_write_byte(rtlpriv, REG_RQPN_NPQ, 0x03);
603 #endif
604 
605 	rtl_write_dword(rtlpriv, REG_TRXFF_BNDY, (0x27FF0000 | txpktbuf_bndy));
606 	rtl_write_byte(rtlpriv, REG_TDECTRL + 1, txpktbuf_bndy);
607 
608 	rtl_write_byte(rtlpriv, REG_TXPKTBUF_BCNQ_BDNY, txpktbuf_bndy);
609 	rtl_write_byte(rtlpriv, REG_TXPKTBUF_MGQ_BDNY, txpktbuf_bndy);
610 
611 	rtl_write_byte(rtlpriv, 0x45D, txpktbuf_bndy);
612 	rtl_write_byte(rtlpriv, REG_PBP, 0x11);
613 	rtl_write_byte(rtlpriv, REG_RX_DRVINFO_SZ, 0x4);
614 
615 	for (i = 0; i < (txpktbuf_bndy - 1); i++) {
616 		status = _rtl8723e_llt_write(hw, i, i + 1);
617 		if (!status)
618 			return status;
619 	}
620 
621 	status = _rtl8723e_llt_write(hw, (txpktbuf_bndy - 1), 0xFF);
622 	if (!status)
623 		return status;
624 
625 	for (i = txpktbuf_bndy; i < maxpage; i++) {
626 		status = _rtl8723e_llt_write(hw, i, (i + 1));
627 		if (!status)
628 			return status;
629 	}
630 
631 	status = _rtl8723e_llt_write(hw, maxpage, txpktbuf_bndy);
632 	if (!status)
633 		return status;
634 
635 	rtl_write_byte(rtlpriv, REG_CR, 0xff);
636 	ubyte = rtl_read_byte(rtlpriv, REG_RQPN + 3);
637 	rtl_write_byte(rtlpriv, REG_RQPN + 3, ubyte | BIT(7));
638 
639 	return true;
640 }
641 
642 static void _rtl8723e_gen_refresh_led_state(struct ieee80211_hw *hw)
643 {
644 	struct rtl_priv *rtlpriv = rtl_priv(hw);
645 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
646 	struct rtl_led *pled0 = &rtlpriv->ledctl.sw_led0;
647 
648 	if (rtlpriv->rtlhal.up_first_time)
649 		return;
650 
651 	if (ppsc->rfoff_reason == RF_CHANGE_BY_IPS)
652 		rtl8723e_sw_led_on(hw, pled0);
653 	else if (ppsc->rfoff_reason == RF_CHANGE_BY_INIT)
654 		rtl8723e_sw_led_on(hw, pled0);
655 	else
656 		rtl8723e_sw_led_off(hw, pled0);
657 }
658 
659 static bool _rtl8712e_init_mac(struct ieee80211_hw *hw)
660 {
661 	struct rtl_priv *rtlpriv = rtl_priv(hw);
662 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
663 
664 	unsigned char bytetmp;
665 	unsigned short wordtmp;
666 	u16 retry = 0;
667 	u16 tmpu2b;
668 	bool mac_func_enable;
669 
670 	rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x00);
671 	bytetmp = rtl_read_byte(rtlpriv, REG_CR);
672 	if (bytetmp == 0xFF)
673 		mac_func_enable = true;
674 	else
675 		mac_func_enable = false;
676 
677 	/* HW Power on sequence */
678 	if (!rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_ALL_MSK, PWR_FAB_ALL_MSK,
679 		PWR_INTF_PCI_MSK, RTL8723_NIC_ENABLE_FLOW))
680 		return false;
681 
682 	bytetmp = rtl_read_byte(rtlpriv, REG_PCIE_CTRL_REG+2);
683 	rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG+2, bytetmp | BIT(4));
684 
685 	/* eMAC time out function enable, 0x369[7]=1 */
686 	bytetmp = rtl_read_byte(rtlpriv, 0x369);
687 	rtl_write_byte(rtlpriv, 0x369, bytetmp | BIT(7));
688 
689 	/* ePHY reg 0x1e bit[4]=1 using MDIO interface,
690 	 * we should do this before Enabling ASPM backdoor.
691 	 */
692 	do {
693 		rtl_write_word(rtlpriv, 0x358, 0x5e);
694 		udelay(100);
695 		rtl_write_word(rtlpriv, 0x356, 0xc280);
696 		rtl_write_word(rtlpriv, 0x354, 0xc290);
697 		rtl_write_word(rtlpriv, 0x358, 0x3e);
698 		udelay(100);
699 		rtl_write_word(rtlpriv, 0x358, 0x5e);
700 		udelay(100);
701 		tmpu2b = rtl_read_word(rtlpriv, 0x356);
702 		retry++;
703 	} while (tmpu2b != 0xc290 && retry < 100);
704 
705 	if (retry >= 100) {
706 		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
707 			 "InitMAC(): ePHY configure fail!!!\n");
708 		return false;
709 	}
710 
711 	rtl_write_word(rtlpriv, REG_CR, 0x2ff);
712 	rtl_write_word(rtlpriv, REG_CR + 1, 0x06);
713 
714 	if (!mac_func_enable) {
715 		if (!_rtl8723e_llt_table_init(hw))
716 			return false;
717 	}
718 
719 	rtl_write_dword(rtlpriv, REG_HISR, 0xffffffff);
720 	rtl_write_byte(rtlpriv, REG_HISRE, 0xff);
721 
722 	rtl_write_word(rtlpriv, REG_TRXFF_BNDY + 2, 0x27ff);
723 
724 	wordtmp = rtl_read_word(rtlpriv, REG_TRXDMA_CTRL);
725 	wordtmp &= 0xf;
726 	wordtmp |= 0xF771;
727 	rtl_write_word(rtlpriv, REG_TRXDMA_CTRL, wordtmp);
728 
729 	rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL + 1, 0x1F);
730 	rtl_write_dword(rtlpriv, REG_RCR, rtlpci->receive_config);
731 	rtl_write_word(rtlpriv, REG_RXFLTMAP2, 0xFFFF);
732 	rtl_write_dword(rtlpriv, REG_TCR, rtlpci->transmit_config);
733 
734 	rtl_write_byte(rtlpriv, 0x4d0, 0x0);
735 
736 	rtl_write_dword(rtlpriv, REG_BCNQ_DESA,
737 			((u64) rtlpci->tx_ring[BEACON_QUEUE].dma) &
738 			DMA_BIT_MASK(32));
739 	rtl_write_dword(rtlpriv, REG_MGQ_DESA,
740 			(u64) rtlpci->tx_ring[MGNT_QUEUE].dma &
741 			DMA_BIT_MASK(32));
742 	rtl_write_dword(rtlpriv, REG_VOQ_DESA,
743 			(u64) rtlpci->tx_ring[VO_QUEUE].dma & DMA_BIT_MASK(32));
744 	rtl_write_dword(rtlpriv, REG_VIQ_DESA,
745 			(u64) rtlpci->tx_ring[VI_QUEUE].dma & DMA_BIT_MASK(32));
746 	rtl_write_dword(rtlpriv, REG_BEQ_DESA,
747 			(u64) rtlpci->tx_ring[BE_QUEUE].dma & DMA_BIT_MASK(32));
748 	rtl_write_dword(rtlpriv, REG_BKQ_DESA,
749 			(u64) rtlpci->tx_ring[BK_QUEUE].dma & DMA_BIT_MASK(32));
750 	rtl_write_dword(rtlpriv, REG_HQ_DESA,
751 			(u64) rtlpci->tx_ring[HIGH_QUEUE].dma &
752 			DMA_BIT_MASK(32));
753 	rtl_write_dword(rtlpriv, REG_RX_DESA,
754 			(u64) rtlpci->rx_ring[RX_MPDU_QUEUE].dma &
755 			DMA_BIT_MASK(32));
756 
757 	rtl_write_byte(rtlpriv, REG_PCIE_CTRL_REG + 3, 0x74);
758 
759 	rtl_write_dword(rtlpriv, REG_INT_MIG, 0);
760 
761 	bytetmp = rtl_read_byte(rtlpriv, REG_APSD_CTRL);
762 	rtl_write_byte(rtlpriv, REG_APSD_CTRL, bytetmp & ~BIT(6));
763 	do {
764 		retry++;
765 		bytetmp = rtl_read_byte(rtlpriv, REG_APSD_CTRL);
766 	} while ((retry < 200) && (bytetmp & BIT(7)));
767 
768 	_rtl8723e_gen_refresh_led_state(hw);
769 
770 	rtl_write_dword(rtlpriv, REG_MCUTST_1, 0x0);
771 
772 	return true;
773 }
774 
775 static void _rtl8723e_hw_configure(struct ieee80211_hw *hw)
776 {
777 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
778 	struct rtl_priv *rtlpriv = rtl_priv(hw);
779 	u8 reg_bw_opmode;
780 	u32 reg_prsr;
781 
782 	reg_bw_opmode = BW_OPMODE_20MHZ;
783 	reg_prsr = RATE_ALL_CCK | RATE_ALL_OFDM_AG;
784 
785 	rtl_write_byte(rtlpriv, REG_INIRTS_RATE_SEL, 0x8);
786 
787 	rtl_write_byte(rtlpriv, REG_BWOPMODE, reg_bw_opmode);
788 
789 	rtl_write_dword(rtlpriv, REG_RRSR, reg_prsr);
790 
791 	rtl_write_byte(rtlpriv, REG_SLOT, 0x09);
792 
793 	rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE, 0x0);
794 
795 	rtl_write_word(rtlpriv, REG_FWHW_TXQ_CTRL, 0x1F80);
796 
797 	rtl_write_word(rtlpriv, REG_RL, 0x0707);
798 
799 	rtl_write_dword(rtlpriv, REG_BAR_MODE_CTRL, 0x02012802);
800 
801 	rtl_write_byte(rtlpriv, REG_HWSEQ_CTRL, 0xFF);
802 
803 	rtl_write_dword(rtlpriv, REG_DARFRC, 0x01000000);
804 	rtl_write_dword(rtlpriv, REG_DARFRC + 4, 0x07060504);
805 	rtl_write_dword(rtlpriv, REG_RARFRC, 0x01000000);
806 	rtl_write_dword(rtlpriv, REG_RARFRC + 4, 0x07060504);
807 
808 	if ((rtlpriv->btcoexist.bt_coexistence) &&
809 	    (rtlpriv->btcoexist.bt_coexist_type == BT_CSR_BC4))
810 		rtl_write_dword(rtlpriv, REG_AGGLEN_LMT, 0x97427431);
811 	else
812 		rtl_write_dword(rtlpriv, REG_AGGLEN_LMT, 0xb972a841);
813 
814 	rtl_write_byte(rtlpriv, REG_ATIMWND, 0x2);
815 
816 	rtl_write_byte(rtlpriv, REG_BCN_MAX_ERR, 0xff);
817 
818 	rtlpci->reg_bcn_ctrl_val = 0x1f;
819 	rtl_write_byte(rtlpriv, REG_BCN_CTRL, rtlpci->reg_bcn_ctrl_val);
820 
821 	rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0xff);
822 
823 	rtl_write_byte(rtlpriv, REG_TBTT_PROHIBIT + 1, 0xff);
824 
825 	rtl_write_byte(rtlpriv, REG_PIFS, 0x1C);
826 	rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
827 
828 	if ((rtlpriv->btcoexist.bt_coexistence) &&
829 	    (rtlpriv->btcoexist.bt_coexist_type == BT_CSR_BC4)) {
830 		rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0020);
831 		rtl_write_word(rtlpriv, REG_PROT_MODE_CTRL, 0x0402);
832 	} else {
833 		rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0020);
834 		rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0020);
835 	}
836 
837 	if ((rtlpriv->btcoexist.bt_coexistence) &&
838 	    (rtlpriv->btcoexist.bt_coexist_type == BT_CSR_BC4))
839 		rtl_write_dword(rtlpriv, REG_FAST_EDCA_CTRL, 0x03086666);
840 	else
841 		rtl_write_dword(rtlpriv, REG_FAST_EDCA_CTRL, 0x086666);
842 
843 	rtl_write_byte(rtlpriv, REG_ACKTO, 0x40);
844 
845 	rtl_write_word(rtlpriv, REG_SPEC_SIFS, 0x1010);
846 	rtl_write_word(rtlpriv, REG_MAC_SPEC_SIFS, 0x1010);
847 
848 	rtl_write_word(rtlpriv, REG_SIFS_CTX, 0x1010);
849 
850 	rtl_write_word(rtlpriv, REG_SIFS_TRX, 0x1010);
851 
852 	rtl_write_dword(rtlpriv, REG_MAR, 0xffffffff);
853 	rtl_write_dword(rtlpriv, REG_MAR + 4, 0xffffffff);
854 
855 	rtl_write_dword(rtlpriv, 0x394, 0x1);
856 }
857 
858 static void _rtl8723e_enable_aspm_back_door(struct ieee80211_hw *hw)
859 {
860 	struct rtl_priv *rtlpriv = rtl_priv(hw);
861 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
862 
863 	rtl_write_byte(rtlpriv, 0x34b, 0x93);
864 	rtl_write_word(rtlpriv, 0x350, 0x870c);
865 	rtl_write_byte(rtlpriv, 0x352, 0x1);
866 
867 	if (ppsc->support_backdoor)
868 		rtl_write_byte(rtlpriv, 0x349, 0x1b);
869 	else
870 		rtl_write_byte(rtlpriv, 0x349, 0x03);
871 
872 	rtl_write_word(rtlpriv, 0x350, 0x2718);
873 	rtl_write_byte(rtlpriv, 0x352, 0x1);
874 }
875 
876 void rtl8723e_enable_hw_security_config(struct ieee80211_hw *hw)
877 {
878 	struct rtl_priv *rtlpriv = rtl_priv(hw);
879 	u8 sec_reg_value;
880 
881 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
882 		 "PairwiseEncAlgorithm = %d GroupEncAlgorithm = %d\n",
883 		  rtlpriv->sec.pairwise_enc_algorithm,
884 		  rtlpriv->sec.group_enc_algorithm);
885 
886 	if (rtlpriv->cfg->mod_params->sw_crypto || rtlpriv->sec.use_sw_sec) {
887 		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
888 			 "not open hw encryption\n");
889 		return;
890 	}
891 
892 	sec_reg_value = SCR_TXENCENABLE | SCR_RXDECENABLE;
893 
894 	if (rtlpriv->sec.use_defaultkey) {
895 		sec_reg_value |= SCR_TXUSEDK;
896 		sec_reg_value |= SCR_RXUSEDK;
897 	}
898 
899 	sec_reg_value |= (SCR_RXBCUSEDK | SCR_TXBCUSEDK);
900 
901 	rtl_write_byte(rtlpriv, REG_CR + 1, 0x02);
902 
903 	RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
904 		 "The SECR-value %x\n", sec_reg_value);
905 
906 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_WPA_CONFIG, &sec_reg_value);
907 
908 }
909 
910 int rtl8723e_hw_init(struct ieee80211_hw *hw)
911 {
912 	struct rtl_priv *rtlpriv = rtl_priv(hw);
913 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
914 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
915 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
916 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
917 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
918 	bool rtstatus = true;
919 	int err;
920 	u8 tmp_u1b;
921 	unsigned long flags;
922 
923 	rtlpriv->rtlhal.being_init_adapter = true;
924 	/* As this function can take a very long time (up to 350 ms)
925 	 * and can be called with irqs disabled, reenable the irqs
926 	 * to let the other devices continue being serviced.
927 	 *
928 	 * It is safe doing so since our own interrupts will only be enabled
929 	 * in a subsequent step.
930 	 */
931 	local_save_flags(flags);
932 	local_irq_enable();
933 	rtlhal->fw_ready = false;
934 
935 	rtlpriv->intf_ops->disable_aspm(hw);
936 	rtstatus = _rtl8712e_init_mac(hw);
937 	if (!rtstatus) {
938 		pr_err("Init MAC failed\n");
939 		err = 1;
940 		goto exit;
941 	}
942 
943 	err = rtl8723_download_fw(hw, false, FW_8723A_POLLING_TIMEOUT_COUNT);
944 	if (err) {
945 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
946 			 "Failed to download FW. Init HW without FW now..\n");
947 		err = 1;
948 		goto exit;
949 	}
950 	rtlhal->fw_ready = true;
951 
952 	rtlhal->last_hmeboxnum = 0;
953 	rtl8723e_phy_mac_config(hw);
954 	/* because last function modify RCR, so we update
955 	 * rcr var here, or TP will unstable for receive_config
956 	 * is wrong, RX RCR_ACRC32 will cause TP unstable & Rx
957 	 * RCR_APP_ICV will cause mac80211 unassoc for cisco 1252
958 	 */
959 	rtlpci->receive_config = rtl_read_dword(rtlpriv, REG_RCR);
960 	rtlpci->receive_config &= ~(RCR_ACRC32 | RCR_AICV);
961 	rtl_write_dword(rtlpriv, REG_RCR, rtlpci->receive_config);
962 
963 	rtl8723e_phy_bb_config(hw);
964 	rtlphy->rf_mode = RF_OP_BY_SW_3WIRE;
965 	rtl8723e_phy_rf_config(hw);
966 	if (IS_VENDOR_UMC_A_CUT(rtlhal->version)) {
967 		rtl_set_rfreg(hw, RF90_PATH_A, RF_RX_G1, MASKDWORD, 0x30255);
968 		rtl_set_rfreg(hw, RF90_PATH_A, RF_RX_G2, MASKDWORD, 0x50a00);
969 	} else if (IS_81XXC_VENDOR_UMC_B_CUT(rtlhal->version)) {
970 		rtl_set_rfreg(hw, RF90_PATH_A, 0x0C, MASKDWORD, 0x894AE);
971 		rtl_set_rfreg(hw, RF90_PATH_A, 0x0A, MASKDWORD, 0x1AF31);
972 		rtl_set_rfreg(hw, RF90_PATH_A, RF_IPA, MASKDWORD, 0x8F425);
973 		rtl_set_rfreg(hw, RF90_PATH_A, RF_SYN_G2, MASKDWORD, 0x4F200);
974 		rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK1, MASKDWORD, 0x44053);
975 		rtl_set_rfreg(hw, RF90_PATH_A, RF_RCK2, MASKDWORD, 0x80201);
976 	}
977 	rtlphy->rfreg_chnlval[0] = rtl_get_rfreg(hw, (enum radio_path)0,
978 						 RF_CHNLBW, RFREG_OFFSET_MASK);
979 	rtlphy->rfreg_chnlval[1] = rtl_get_rfreg(hw, (enum radio_path)1,
980 						 RF_CHNLBW, RFREG_OFFSET_MASK);
981 	rtl_set_bbreg(hw, RFPGA0_RFMOD, BCCKEN, 0x1);
982 	rtl_set_bbreg(hw, RFPGA0_RFMOD, BOFDMEN, 0x1);
983 	rtl_set_bbreg(hw, RFPGA0_ANALOGPARAMETER2, BIT(10), 1);
984 	_rtl8723e_hw_configure(hw);
985 	rtl_cam_reset_all_entry(hw);
986 	rtl8723e_enable_hw_security_config(hw);
987 
988 	ppsc->rfpwr_state = ERFON;
989 
990 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_ETHER_ADDR, mac->mac_addr);
991 	_rtl8723e_enable_aspm_back_door(hw);
992 	rtlpriv->intf_ops->enable_aspm(hw);
993 
994 	rtl8723e_bt_hw_init(hw);
995 
996 	if (ppsc->rfpwr_state == ERFON) {
997 		rtl8723e_phy_set_rfpath_switch(hw, 1);
998 		if (rtlphy->iqk_initialized) {
999 			rtl8723e_phy_iq_calibrate(hw, true);
1000 		} else {
1001 			rtl8723e_phy_iq_calibrate(hw, false);
1002 			rtlphy->iqk_initialized = true;
1003 		}
1004 
1005 		rtl8723e_dm_check_txpower_tracking(hw);
1006 		rtl8723e_phy_lc_calibrate(hw);
1007 	}
1008 
1009 	tmp_u1b = efuse_read_1byte(hw, 0x1FA);
1010 	if (!(tmp_u1b & BIT(0))) {
1011 		rtl_set_rfreg(hw, RF90_PATH_A, 0x15, 0x0F, 0x05);
1012 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "PA BIAS path A\n");
1013 	}
1014 
1015 	if (!(tmp_u1b & BIT(4))) {
1016 		tmp_u1b = rtl_read_byte(rtlpriv, 0x16);
1017 		tmp_u1b &= 0x0F;
1018 		rtl_write_byte(rtlpriv, 0x16, tmp_u1b | 0x80);
1019 		udelay(10);
1020 		rtl_write_byte(rtlpriv, 0x16, tmp_u1b | 0x90);
1021 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE, "under 1.5V\n");
1022 	}
1023 	rtl8723e_dm_init(hw);
1024 exit:
1025 	local_irq_restore(flags);
1026 	rtlpriv->rtlhal.being_init_adapter = false;
1027 	return err;
1028 }
1029 
1030 static enum version_8723e _rtl8723e_read_chip_version(struct ieee80211_hw *hw)
1031 {
1032 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1033 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1034 	enum version_8723e version = 0x0000;
1035 	u32 value32;
1036 
1037 	value32 = rtl_read_dword(rtlpriv, REG_SYS_CFG);
1038 	if (value32 & TRP_VAUX_EN) {
1039 		version = (enum version_8723e)(version |
1040 			((value32 & VENDOR_ID) ? CHIP_VENDOR_UMC : 0));
1041 		/* RTL8723 with BT function. */
1042 		version = (enum version_8723e)(version |
1043 			((value32 & BT_FUNC) ? CHIP_8723 : 0));
1044 
1045 	} else {
1046 		/* Normal mass production chip. */
1047 		version = (enum version_8723e) NORMAL_CHIP;
1048 		version = (enum version_8723e)(version |
1049 			((value32 & VENDOR_ID) ? CHIP_VENDOR_UMC : 0));
1050 		/* RTL8723 with BT function. */
1051 		version = (enum version_8723e)(version |
1052 			((value32 & BT_FUNC) ? CHIP_8723 : 0));
1053 		if (IS_CHIP_VENDOR_UMC(version))
1054 			version = (enum version_8723e)(version |
1055 			((value32 & CHIP_VER_RTL_MASK)));/* IC version (CUT) */
1056 		if (IS_8723_SERIES(version)) {
1057 			value32 = rtl_read_dword(rtlpriv, REG_GPIO_OUTSTS);
1058 			/* ROM code version. */
1059 			version = (enum version_8723e)(version |
1060 				((value32 & RF_RL_ID)>>20));
1061 		}
1062 	}
1063 
1064 	if (IS_8723_SERIES(version)) {
1065 		value32 = rtl_read_dword(rtlpriv, REG_MULTI_FUNC_CTRL);
1066 		rtlphy->polarity_ctl = ((value32 & WL_HWPDN_SL) ?
1067 					RT_POLARITY_HIGH_ACT :
1068 					RT_POLARITY_LOW_ACT);
1069 	}
1070 	switch (version) {
1071 	case VERSION_TEST_UMC_CHIP_8723:
1072 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1073 			 "Chip Version ID: VERSION_TEST_UMC_CHIP_8723.\n");
1074 			break;
1075 	case VERSION_NORMAL_UMC_CHIP_8723_1T1R_A_CUT:
1076 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1077 			 "Chip Version ID: VERSION_NORMAL_UMC_CHIP_8723_1T1R_A_CUT.\n");
1078 		break;
1079 	case VERSION_NORMAL_UMC_CHIP_8723_1T1R_B_CUT:
1080 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1081 			 "Chip Version ID: VERSION_NORMAL_UMC_CHIP_8723_1T1R_B_CUT.\n");
1082 		break;
1083 	default:
1084 		pr_err("Chip Version ID: Unknown. Bug?\n");
1085 		break;
1086 	}
1087 
1088 	if (IS_8723_SERIES(version))
1089 		rtlphy->rf_type = RF_1T1R;
1090 
1091 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Chip RF Type: %s\n",
1092 		(rtlphy->rf_type == RF_2T2R) ? "RF_2T2R" : "RF_1T1R");
1093 
1094 	return version;
1095 }
1096 
1097 static int _rtl8723e_set_media_status(struct ieee80211_hw *hw,
1098 				      enum nl80211_iftype type)
1099 {
1100 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1101 	u8 bt_msr = rtl_read_byte(rtlpriv, MSR) & 0xfc;
1102 	enum led_ctl_mode ledaction = LED_CTL_NO_LINK;
1103 	u8 mode = MSR_NOLINK;
1104 
1105 	rtl_write_dword(rtlpriv, REG_BCN_CTRL, 0);
1106 	RT_TRACE(rtlpriv, COMP_BEACON, DBG_LOUD,
1107 		"clear 0x550 when set HW_VAR_MEDIA_STATUS\n");
1108 
1109 	switch (type) {
1110 	case NL80211_IFTYPE_UNSPECIFIED:
1111 		mode = MSR_NOLINK;
1112 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1113 			"Set Network type to NO LINK!\n");
1114 		break;
1115 	case NL80211_IFTYPE_ADHOC:
1116 		mode = MSR_ADHOC;
1117 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1118 			"Set Network type to Ad Hoc!\n");
1119 		break;
1120 	case NL80211_IFTYPE_STATION:
1121 		mode = MSR_INFRA;
1122 		ledaction = LED_CTL_LINK;
1123 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1124 			"Set Network type to STA!\n");
1125 		break;
1126 	case NL80211_IFTYPE_AP:
1127 		mode = MSR_AP;
1128 		ledaction = LED_CTL_LINK;
1129 		RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
1130 			"Set Network type to AP!\n");
1131 		break;
1132 	default:
1133 		pr_err("Network type %d not support!\n", type);
1134 		return 1;
1135 		break;
1136 	}
1137 
1138 	/* MSR_INFRA == Link in infrastructure network;
1139 	 * MSR_ADHOC == Link in ad hoc network;
1140 	 * Therefore, check link state is necessary.
1141 	 *
1142 	 * MSR_AP == AP mode; link state is not cared here.
1143 	 */
1144 	if (mode != MSR_AP &&
1145 	    rtlpriv->mac80211.link_state < MAC80211_LINKED) {
1146 		mode = MSR_NOLINK;
1147 		ledaction = LED_CTL_NO_LINK;
1148 	}
1149 	if (mode == MSR_NOLINK || mode == MSR_INFRA) {
1150 		_rtl8723e_stop_tx_beacon(hw);
1151 		_rtl8723e_enable_bcn_sub_func(hw);
1152 	} else if (mode == MSR_ADHOC || mode == MSR_AP) {
1153 		_rtl8723e_resume_tx_beacon(hw);
1154 		_rtl8723e_disable_bcn_sub_func(hw);
1155 	} else {
1156 		RT_TRACE(rtlpriv, COMP_ERR, DBG_WARNING,
1157 			 "Set HW_VAR_MEDIA_STATUS: No such media status(%x).\n",
1158 			 mode);
1159 	}
1160 
1161 	rtl_write_byte(rtlpriv, MSR, bt_msr | mode);
1162 	rtlpriv->cfg->ops->led_control(hw, ledaction);
1163 	if (mode == MSR_AP)
1164 		rtl_write_byte(rtlpriv, REG_BCNTCFG + 1, 0x00);
1165 	else
1166 		rtl_write_byte(rtlpriv, REG_BCNTCFG + 1, 0x66);
1167 	return 0;
1168 }
1169 
1170 void rtl8723e_set_check_bssid(struct ieee80211_hw *hw, bool check_bssid)
1171 {
1172 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1173 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1174 	u32 reg_rcr = rtlpci->receive_config;
1175 
1176 	if (rtlpriv->psc.rfpwr_state != ERFON)
1177 		return;
1178 
1179 	if (check_bssid) {
1180 		reg_rcr |= (RCR_CBSSID_DATA | RCR_CBSSID_BCN);
1181 		rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_RCR,
1182 					      (u8 *)(&reg_rcr));
1183 		_rtl8723e_set_bcn_ctrl_reg(hw, 0, BIT(4));
1184 	} else if (!check_bssid) {
1185 		reg_rcr &= (~(RCR_CBSSID_DATA | RCR_CBSSID_BCN));
1186 		_rtl8723e_set_bcn_ctrl_reg(hw, BIT(4), 0);
1187 		rtlpriv->cfg->ops->set_hw_reg(hw,
1188 			HW_VAR_RCR, (u8 *)(&reg_rcr));
1189 	}
1190 }
1191 
1192 int rtl8723e_set_network_type(struct ieee80211_hw *hw,
1193 			      enum nl80211_iftype type)
1194 {
1195 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1196 
1197 	if (_rtl8723e_set_media_status(hw, type))
1198 		return -EOPNOTSUPP;
1199 
1200 	if (rtlpriv->mac80211.link_state == MAC80211_LINKED) {
1201 		if (type != NL80211_IFTYPE_AP)
1202 			rtl8723e_set_check_bssid(hw, true);
1203 	} else {
1204 		rtl8723e_set_check_bssid(hw, false);
1205 	}
1206 
1207 	return 0;
1208 }
1209 
1210 /* don't set REG_EDCA_BE_PARAM here
1211  * because mac80211 will send pkt when scan
1212  */
1213 void rtl8723e_set_qos(struct ieee80211_hw *hw, int aci)
1214 {
1215 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1216 
1217 	rtl8723_dm_init_edca_turbo(hw);
1218 	switch (aci) {
1219 	case AC1_BK:
1220 		rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM, 0xa44f);
1221 		break;
1222 	case AC0_BE:
1223 		break;
1224 	case AC2_VI:
1225 		rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM, 0x5e4322);
1226 		break;
1227 	case AC3_VO:
1228 		rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM, 0x2f3222);
1229 		break;
1230 	default:
1231 		WARN_ONCE(true, "rtl8723ae: invalid aci: %d !\n", aci);
1232 		break;
1233 	}
1234 }
1235 
1236 void rtl8723e_enable_interrupt(struct ieee80211_hw *hw)
1237 {
1238 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1239 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1240 
1241 	rtl_write_dword(rtlpriv, 0x3a8, rtlpci->irq_mask[0] & 0xFFFFFFFF);
1242 	rtl_write_dword(rtlpriv, 0x3ac, rtlpci->irq_mask[1] & 0xFFFFFFFF);
1243 	rtlpci->irq_enabled = true;
1244 }
1245 
1246 void rtl8723e_disable_interrupt(struct ieee80211_hw *hw)
1247 {
1248 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1249 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1250 	rtl_write_dword(rtlpriv, 0x3a8, IMR8190_DISABLED);
1251 	rtl_write_dword(rtlpriv, 0x3ac, IMR8190_DISABLED);
1252 	rtlpci->irq_enabled = false;
1253 	/*synchronize_irq(rtlpci->pdev->irq);*/
1254 }
1255 
1256 static void _rtl8723e_poweroff_adapter(struct ieee80211_hw *hw)
1257 {
1258 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1259 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1260 	u8 u1b_tmp;
1261 
1262 	/* Combo (PCIe + USB) Card and PCIe-MF Card */
1263 	/* 1. Run LPS WL RFOFF flow */
1264 	rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_ALL_MSK, PWR_FAB_ALL_MSK,
1265 				 PWR_INTF_PCI_MSK, RTL8723_NIC_LPS_ENTER_FLOW);
1266 
1267 	/* 2. 0x1F[7:0] = 0 */
1268 	/* turn off RF */
1269 	rtl_write_byte(rtlpriv, REG_RF_CTRL, 0x00);
1270 	if ((rtl_read_byte(rtlpriv, REG_MCUFWDL) & BIT(7)) &&
1271 	    rtlhal->fw_ready) {
1272 		rtl8723ae_firmware_selfreset(hw);
1273 	}
1274 
1275 	/* Reset MCU. Suggested by Filen. */
1276 	u1b_tmp = rtl_read_byte(rtlpriv, REG_SYS_FUNC_EN+1);
1277 	rtl_write_byte(rtlpriv, REG_SYS_FUNC_EN+1, (u1b_tmp & (~BIT(2))));
1278 
1279 	/* g.	MCUFWDL 0x80[1:0]=0	 */
1280 	/* reset MCU ready status */
1281 	rtl_write_byte(rtlpriv, REG_MCUFWDL, 0x00);
1282 
1283 	/* HW card disable configuration. */
1284 	rtl_hal_pwrseqcmdparsing(rtlpriv, PWR_CUT_ALL_MSK, PWR_FAB_ALL_MSK,
1285 		PWR_INTF_PCI_MSK, RTL8723_NIC_DISABLE_FLOW);
1286 
1287 	/* Reset MCU IO Wrapper */
1288 	u1b_tmp = rtl_read_byte(rtlpriv, REG_RSV_CTRL + 1);
1289 	rtl_write_byte(rtlpriv, REG_RSV_CTRL + 1, (u1b_tmp & (~BIT(0))));
1290 	u1b_tmp = rtl_read_byte(rtlpriv, REG_RSV_CTRL + 1);
1291 	rtl_write_byte(rtlpriv, REG_RSV_CTRL + 1, u1b_tmp | BIT(0));
1292 
1293 	/* 7. RSV_CTRL 0x1C[7:0] = 0x0E */
1294 	/* lock ISO/CLK/Power control register */
1295 	rtl_write_byte(rtlpriv, REG_RSV_CTRL, 0x0e);
1296 }
1297 
1298 void rtl8723e_card_disable(struct ieee80211_hw *hw)
1299 {
1300 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1301 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
1302 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1303 	enum nl80211_iftype opmode;
1304 
1305 	mac->link_state = MAC80211_NOLINK;
1306 	opmode = NL80211_IFTYPE_UNSPECIFIED;
1307 	_rtl8723e_set_media_status(hw, opmode);
1308 	if (rtlpriv->rtlhal.driver_is_goingto_unload ||
1309 	    ppsc->rfoff_reason > RF_CHANGE_BY_PS)
1310 		rtlpriv->cfg->ops->led_control(hw, LED_CTL_POWER_OFF);
1311 	RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
1312 	_rtl8723e_poweroff_adapter(hw);
1313 
1314 	/* after power off we should do iqk again */
1315 	rtlpriv->phy.iqk_initialized = false;
1316 }
1317 
1318 void rtl8723e_interrupt_recognized(struct ieee80211_hw *hw,
1319 				   struct rtl_int *intvec)
1320 {
1321 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1322 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1323 
1324 	intvec->inta = rtl_read_dword(rtlpriv, 0x3a0) & rtlpci->irq_mask[0];
1325 	rtl_write_dword(rtlpriv, 0x3a0, intvec->inta);
1326 }
1327 
1328 void rtl8723e_set_beacon_related_registers(struct ieee80211_hw *hw)
1329 {
1330 
1331 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1332 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1333 	u16 bcn_interval, atim_window;
1334 
1335 	bcn_interval = mac->beacon_interval;
1336 	atim_window = 2;	/*FIX MERGE */
1337 	rtl8723e_disable_interrupt(hw);
1338 	rtl_write_word(rtlpriv, REG_ATIMWND, atim_window);
1339 	rtl_write_word(rtlpriv, REG_BCN_INTERVAL, bcn_interval);
1340 	rtl_write_word(rtlpriv, REG_BCNTCFG, 0x660f);
1341 	rtl_write_byte(rtlpriv, REG_RXTSF_OFFSET_CCK, 0x18);
1342 	rtl_write_byte(rtlpriv, REG_RXTSF_OFFSET_OFDM, 0x18);
1343 	rtl_write_byte(rtlpriv, 0x606, 0x30);
1344 	rtl8723e_enable_interrupt(hw);
1345 }
1346 
1347 void rtl8723e_set_beacon_interval(struct ieee80211_hw *hw)
1348 {
1349 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1350 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1351 	u16 bcn_interval = mac->beacon_interval;
1352 
1353 	RT_TRACE(rtlpriv, COMP_BEACON, DBG_DMESG,
1354 		 "beacon_interval:%d\n", bcn_interval);
1355 	rtl8723e_disable_interrupt(hw);
1356 	rtl_write_word(rtlpriv, REG_BCN_INTERVAL, bcn_interval);
1357 	rtl8723e_enable_interrupt(hw);
1358 }
1359 
1360 void rtl8723e_update_interrupt_mask(struct ieee80211_hw *hw,
1361 				    u32 add_msr, u32 rm_msr)
1362 {
1363 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1364 	struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
1365 
1366 	RT_TRACE(rtlpriv, COMP_INTR, DBG_LOUD,
1367 		 "add_msr:%x, rm_msr:%x\n", add_msr, rm_msr);
1368 
1369 	if (add_msr)
1370 		rtlpci->irq_mask[0] |= add_msr;
1371 	if (rm_msr)
1372 		rtlpci->irq_mask[0] &= (~rm_msr);
1373 	rtl8723e_disable_interrupt(hw);
1374 	rtl8723e_enable_interrupt(hw);
1375 }
1376 
1377 static u8 _rtl8723e_get_chnl_group(u8 chnl)
1378 {
1379 	u8 group;
1380 
1381 	if (chnl < 3)
1382 		group = 0;
1383 	else if (chnl < 9)
1384 		group = 1;
1385 	else
1386 		group = 2;
1387 	return group;
1388 }
1389 
1390 static void _rtl8723e_read_txpower_info_from_hwpg(struct ieee80211_hw *hw,
1391 						  bool autoload_fail,
1392 						  u8 *hwinfo)
1393 {
1394 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1395 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1396 	u8 rf_path, index, tempval;
1397 	u16 i;
1398 
1399 	for (rf_path = 0; rf_path < 1; rf_path++) {
1400 		for (i = 0; i < 3; i++) {
1401 			if (!autoload_fail) {
1402 				rtlefuse->eeprom_chnlarea_txpwr_cck[rf_path][i] =
1403 				    hwinfo[EEPROM_TXPOWERCCK + rf_path * 3 + i];
1404 				rtlefuse->eeprom_chnlarea_txpwr_ht40_1s[rf_path][i] =
1405 				    hwinfo[EEPROM_TXPOWERHT40_1S + rf_path * 3 + i];
1406 			} else {
1407 				rtlefuse->eeprom_chnlarea_txpwr_cck[rf_path][i] =
1408 				    EEPROM_DEFAULT_TXPOWERLEVEL;
1409 				rtlefuse->eeprom_chnlarea_txpwr_ht40_1s[rf_path][i] =
1410 				    EEPROM_DEFAULT_TXPOWERLEVEL;
1411 			}
1412 		}
1413 	}
1414 
1415 	for (i = 0; i < 3; i++) {
1416 		if (!autoload_fail)
1417 			tempval = hwinfo[EEPROM_TXPOWERHT40_2SDIFF + i];
1418 		else
1419 			tempval = EEPROM_DEFAULT_HT40_2SDIFF;
1420 		rtlefuse->eprom_chnl_txpwr_ht40_2sdf[RF90_PATH_A][i] =
1421 		    (tempval & 0xf);
1422 		rtlefuse->eprom_chnl_txpwr_ht40_2sdf[RF90_PATH_B][i] =
1423 		    ((tempval & 0xf0) >> 4);
1424 	}
1425 
1426 	for (rf_path = 0; rf_path < 2; rf_path++)
1427 		for (i = 0; i < 3; i++)
1428 			RTPRINT(rtlpriv, FINIT, INIT_EEPROM,
1429 				"RF(%d) EEPROM CCK Area(%d) = 0x%x\n", rf_path,
1430 				 i, rtlefuse->eeprom_chnlarea_txpwr_cck
1431 					[rf_path][i]);
1432 	for (rf_path = 0; rf_path < 2; rf_path++)
1433 		for (i = 0; i < 3; i++)
1434 			RTPRINT(rtlpriv, FINIT, INIT_EEPROM,
1435 				"RF(%d) EEPROM HT40 1S Area(%d) = 0x%x\n",
1436 				rf_path, i,
1437 				rtlefuse->eeprom_chnlarea_txpwr_ht40_1s
1438 					[rf_path][i]);
1439 	for (rf_path = 0; rf_path < 2; rf_path++)
1440 		for (i = 0; i < 3; i++)
1441 			RTPRINT(rtlpriv, FINIT, INIT_EEPROM,
1442 				"RF(%d) EEPROM HT40 2S Diff Area(%d) = 0x%x\n",
1443 				 rf_path, i,
1444 				 rtlefuse->eprom_chnl_txpwr_ht40_2sdf
1445 					[rf_path][i]);
1446 
1447 	for (rf_path = 0; rf_path < 2; rf_path++) {
1448 		for (i = 0; i < 14; i++) {
1449 			index = _rtl8723e_get_chnl_group((u8)i);
1450 
1451 			rtlefuse->txpwrlevel_cck[rf_path][i] =
1452 				rtlefuse->eeprom_chnlarea_txpwr_cck
1453 					[rf_path][index];
1454 			rtlefuse->txpwrlevel_ht40_1s[rf_path][i] =
1455 				rtlefuse->eeprom_chnlarea_txpwr_ht40_1s
1456 					[rf_path][index];
1457 
1458 			if ((rtlefuse->eeprom_chnlarea_txpwr_ht40_1s
1459 					[rf_path][index] -
1460 			     rtlefuse->eprom_chnl_txpwr_ht40_2sdf
1461 					[rf_path][index]) > 0) {
1462 				rtlefuse->txpwrlevel_ht40_2s[rf_path][i] =
1463 				  rtlefuse->eeprom_chnlarea_txpwr_ht40_1s
1464 				  [rf_path][index] -
1465 				  rtlefuse->eprom_chnl_txpwr_ht40_2sdf
1466 				  [rf_path][index];
1467 			} else {
1468 				rtlefuse->txpwrlevel_ht40_2s[rf_path][i] = 0;
1469 			}
1470 		}
1471 
1472 		for (i = 0; i < 14; i++) {
1473 			RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1474 				"RF(%d)-Ch(%d) [CCK / HT40_1S / HT40_2S] = [0x%x / 0x%x / 0x%x]\n",
1475 				rf_path, i,
1476 				rtlefuse->txpwrlevel_cck[rf_path][i],
1477 				rtlefuse->txpwrlevel_ht40_1s[rf_path][i],
1478 				rtlefuse->txpwrlevel_ht40_2s[rf_path][i]);
1479 		}
1480 	}
1481 
1482 	for (i = 0; i < 3; i++) {
1483 		if (!autoload_fail) {
1484 			rtlefuse->eeprom_pwrlimit_ht40[i] =
1485 			    hwinfo[EEPROM_TXPWR_GROUP + i];
1486 			rtlefuse->eeprom_pwrlimit_ht20[i] =
1487 			    hwinfo[EEPROM_TXPWR_GROUP + 3 + i];
1488 		} else {
1489 			rtlefuse->eeprom_pwrlimit_ht40[i] = 0;
1490 			rtlefuse->eeprom_pwrlimit_ht20[i] = 0;
1491 		}
1492 	}
1493 
1494 	for (rf_path = 0; rf_path < 2; rf_path++) {
1495 		for (i = 0; i < 14; i++) {
1496 			index = _rtl8723e_get_chnl_group((u8)i);
1497 
1498 			if (rf_path == RF90_PATH_A) {
1499 				rtlefuse->pwrgroup_ht20[rf_path][i] =
1500 				  (rtlefuse->eeprom_pwrlimit_ht20[index] & 0xf);
1501 				rtlefuse->pwrgroup_ht40[rf_path][i] =
1502 				  (rtlefuse->eeprom_pwrlimit_ht40[index] & 0xf);
1503 			} else if (rf_path == RF90_PATH_B) {
1504 				rtlefuse->pwrgroup_ht20[rf_path][i] =
1505 				  ((rtlefuse->eeprom_pwrlimit_ht20[index] &
1506 				   0xf0) >> 4);
1507 				rtlefuse->pwrgroup_ht40[rf_path][i] =
1508 				  ((rtlefuse->eeprom_pwrlimit_ht40[index] &
1509 				   0xf0) >> 4);
1510 			}
1511 
1512 			RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1513 				"RF-%d pwrgroup_ht20[%d] = 0x%x\n", rf_path, i,
1514 				rtlefuse->pwrgroup_ht20[rf_path][i]);
1515 			RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1516 				"RF-%d pwrgroup_ht40[%d] = 0x%x\n", rf_path, i,
1517 				rtlefuse->pwrgroup_ht40[rf_path][i]);
1518 		}
1519 	}
1520 
1521 	for (i = 0; i < 14; i++) {
1522 		index = _rtl8723e_get_chnl_group((u8)i);
1523 
1524 		if (!autoload_fail)
1525 			tempval = hwinfo[EEPROM_TXPOWERHT20DIFF + index];
1526 		else
1527 			tempval = EEPROM_DEFAULT_HT20_DIFF;
1528 
1529 		rtlefuse->txpwr_ht20diff[RF90_PATH_A][i] = (tempval & 0xF);
1530 		rtlefuse->txpwr_ht20diff[RF90_PATH_B][i] =
1531 		    ((tempval >> 4) & 0xF);
1532 
1533 		if (rtlefuse->txpwr_ht20diff[RF90_PATH_A][i] & BIT(3))
1534 			rtlefuse->txpwr_ht20diff[RF90_PATH_A][i] |= 0xF0;
1535 
1536 		if (rtlefuse->txpwr_ht20diff[RF90_PATH_B][i] & BIT(3))
1537 			rtlefuse->txpwr_ht20diff[RF90_PATH_B][i] |= 0xF0;
1538 
1539 		index = _rtl8723e_get_chnl_group((u8)i);
1540 
1541 		if (!autoload_fail)
1542 			tempval = hwinfo[EEPROM_TXPOWER_OFDMDIFF + index];
1543 		else
1544 			tempval = EEPROM_DEFAULT_LEGACYHTTXPOWERDIFF;
1545 
1546 		rtlefuse->txpwr_legacyhtdiff[RF90_PATH_A][i] = (tempval & 0xF);
1547 		rtlefuse->txpwr_legacyhtdiff[RF90_PATH_B][i] =
1548 		    ((tempval >> 4) & 0xF);
1549 	}
1550 
1551 	rtlefuse->legacy_ht_txpowerdiff =
1552 	    rtlefuse->txpwr_legacyhtdiff[RF90_PATH_A][7];
1553 
1554 	for (i = 0; i < 14; i++)
1555 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1556 			"RF-A Ht20 to HT40 Diff[%d] = 0x%x\n", i,
1557 			 rtlefuse->txpwr_ht20diff[RF90_PATH_A][i]);
1558 	for (i = 0; i < 14; i++)
1559 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1560 			"RF-A Legacy to Ht40 Diff[%d] = 0x%x\n", i,
1561 			 rtlefuse->txpwr_legacyhtdiff[RF90_PATH_A][i]);
1562 	for (i = 0; i < 14; i++)
1563 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1564 			"RF-B Ht20 to HT40 Diff[%d] = 0x%x\n", i,
1565 			 rtlefuse->txpwr_ht20diff[RF90_PATH_B][i]);
1566 	for (i = 0; i < 14; i++)
1567 		RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1568 			"RF-B Legacy to HT40 Diff[%d] = 0x%x\n", i,
1569 			 rtlefuse->txpwr_legacyhtdiff[RF90_PATH_B][i]);
1570 
1571 	if (!autoload_fail)
1572 		rtlefuse->eeprom_regulatory = (hwinfo[RF_OPTION1] & 0x7);
1573 	else
1574 		rtlefuse->eeprom_regulatory = 0;
1575 	RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1576 		"eeprom_regulatory = 0x%x\n", rtlefuse->eeprom_regulatory);
1577 
1578 	if (!autoload_fail)
1579 		rtlefuse->eeprom_tssi[RF90_PATH_A] = hwinfo[EEPROM_TSSI_A];
1580 	else
1581 		rtlefuse->eeprom_tssi[RF90_PATH_A] = EEPROM_DEFAULT_TSSI;
1582 
1583 	RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1584 		"TSSI_A = 0x%x, TSSI_B = 0x%x\n",
1585 		 rtlefuse->eeprom_tssi[RF90_PATH_A],
1586 		 rtlefuse->eeprom_tssi[RF90_PATH_B]);
1587 
1588 	if (!autoload_fail)
1589 		tempval = hwinfo[EEPROM_THERMAL_METER];
1590 	else
1591 		tempval = EEPROM_DEFAULT_THERMALMETER;
1592 	rtlefuse->eeprom_thermalmeter = (tempval & 0x1f);
1593 
1594 	if (rtlefuse->eeprom_thermalmeter == 0x1f || autoload_fail)
1595 		rtlefuse->apk_thermalmeterignore = true;
1596 
1597 	rtlefuse->thermalmeter[0] = rtlefuse->eeprom_thermalmeter;
1598 	RTPRINT(rtlpriv, FINIT, INIT_TXPOWER,
1599 		"thermalmeter = 0x%x\n", rtlefuse->eeprom_thermalmeter);
1600 }
1601 
1602 static void _rtl8723e_read_adapter_info(struct ieee80211_hw *hw,
1603 					bool b_pseudo_test)
1604 {
1605 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1606 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1607 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1608 	int params[] = {RTL8190_EEPROM_ID, EEPROM_VID, EEPROM_DID,
1609 			EEPROM_SVID, EEPROM_SMID, EEPROM_MAC_ADDR,
1610 			EEPROM_CHANNELPLAN, EEPROM_VERSION, EEPROM_CUSTOMER_ID,
1611 			COUNTRY_CODE_WORLD_WIDE_13};
1612 	u8 *hwinfo;
1613 
1614 	if (b_pseudo_test) {
1615 		/* need add */
1616 		return;
1617 	}
1618 	hwinfo = kzalloc(HWSET_MAX_SIZE, GFP_KERNEL);
1619 	if (!hwinfo)
1620 		return;
1621 
1622 	if (rtl_get_hwinfo(hw, rtlpriv, HWSET_MAX_SIZE, hwinfo, params))
1623 		goto exit;
1624 
1625 	_rtl8723e_read_txpower_info_from_hwpg(hw, rtlefuse->autoload_failflag,
1626 					      hwinfo);
1627 
1628 	rtl8723e_read_bt_coexist_info_from_hwpg(hw,
1629 			rtlefuse->autoload_failflag, hwinfo);
1630 
1631 	if (rtlhal->oem_id != RT_CID_DEFAULT)
1632 		goto exit;
1633 
1634 	switch (rtlefuse->eeprom_oemid) {
1635 	case EEPROM_CID_DEFAULT:
1636 		switch (rtlefuse->eeprom_did) {
1637 		case 0x8176:
1638 			switch (rtlefuse->eeprom_svid) {
1639 			case 0x10EC:
1640 				switch (rtlefuse->eeprom_smid) {
1641 				case 0x6151 ... 0x6152:
1642 				case 0x6154 ... 0x6155:
1643 				case 0x6177 ... 0x6180:
1644 				case 0x7151 ... 0x7152:
1645 				case 0x7154 ... 0x7155:
1646 				case 0x7177 ... 0x7180:
1647 				case 0x8151 ... 0x8152:
1648 				case 0x8154 ... 0x8155:
1649 				case 0x8181 ... 0x8182:
1650 				case 0x8184 ... 0x8185:
1651 				case 0x9151 ... 0x9152:
1652 				case 0x9154 ... 0x9155:
1653 				case 0x9181 ... 0x9182:
1654 				case 0x9184 ... 0x9185:
1655 					rtlhal->oem_id = RT_CID_TOSHIBA;
1656 					break;
1657 				case 0x6191 ... 0x6193:
1658 				case 0x7191 ... 0x7193:
1659 				case 0x8191 ... 0x8193:
1660 				case 0x9191 ... 0x9193:
1661 					rtlhal->oem_id = RT_CID_819X_SAMSUNG;
1662 					break;
1663 				case 0x8197:
1664 				case 0x9196:
1665 					rtlhal->oem_id = RT_CID_819X_CLEVO;
1666 					break;
1667 				case 0x8203:
1668 					rtlhal->oem_id = RT_CID_819X_PRONETS;
1669 					break;
1670 				case 0x8195:
1671 				case 0x9195:
1672 				case 0x7194:
1673 				case 0x8200 ... 0x8202:
1674 				case 0x9200:
1675 					rtlhal->oem_id = RT_CID_819X_LENOVO;
1676 					break;
1677 				}
1678 				break;
1679 			case 0x1025:
1680 				rtlhal->oem_id = RT_CID_819X_ACER;
1681 				break;
1682 			case 0x1028:
1683 				switch (rtlefuse->eeprom_smid) {
1684 				case 0x8194:
1685 				case 0x8198:
1686 				case 0x9197 ... 0x9198:
1687 					rtlhal->oem_id = RT_CID_819X_DELL;
1688 					break;
1689 				}
1690 				break;
1691 			case 0x103C:
1692 				switch (rtlefuse->eeprom_smid) {
1693 				case 0x1629:
1694 					rtlhal->oem_id = RT_CID_819X_HP;
1695 				}
1696 				break;
1697 			case 0x1A32:
1698 				switch (rtlefuse->eeprom_smid) {
1699 				case 0x2315:
1700 					rtlhal->oem_id = RT_CID_819X_QMI;
1701 					break;
1702 				}
1703 				break;
1704 			case 0x1043:
1705 				switch (rtlefuse->eeprom_smid) {
1706 				case 0x84B5:
1707 					rtlhal->oem_id =
1708 						RT_CID_819X_EDIMAX_ASUS;
1709 				}
1710 				break;
1711 			}
1712 			break;
1713 		case 0x8178:
1714 			switch (rtlefuse->eeprom_svid) {
1715 			case 0x10ec:
1716 				switch (rtlefuse->eeprom_smid) {
1717 				case 0x6181 ... 0x6182:
1718 				case 0x6184 ... 0x6185:
1719 				case 0x7181 ... 0x7182:
1720 				case 0x7184 ... 0x7185:
1721 				case 0x8181 ... 0x8182:
1722 				case 0x8184 ... 0x8185:
1723 				case 0x9181 ... 0x9182:
1724 				case 0x9184 ... 0x9185:
1725 					rtlhal->oem_id = RT_CID_TOSHIBA;
1726 					break;
1727 				case 0x8186:
1728 					rtlhal->oem_id =
1729 						RT_CID_819X_PRONETS;
1730 					break;
1731 				}
1732 				break;
1733 			case 0x1025:
1734 				rtlhal->oem_id = RT_CID_819X_ACER;
1735 				break;
1736 			case 0x1043:
1737 				switch (rtlefuse->eeprom_smid) {
1738 				case 0x8486:
1739 					rtlhal->oem_id =
1740 					     RT_CID_819X_EDIMAX_ASUS;
1741 				}
1742 				break;
1743 			}
1744 			break;
1745 		}
1746 		break;
1747 	case EEPROM_CID_TOSHIBA:
1748 		rtlhal->oem_id = RT_CID_TOSHIBA;
1749 		break;
1750 	case EEPROM_CID_CCX:
1751 		rtlhal->oem_id = RT_CID_CCX;
1752 		break;
1753 	case EEPROM_CID_QMI:
1754 		rtlhal->oem_id = RT_CID_819X_QMI;
1755 		break;
1756 	case EEPROM_CID_WHQL:
1757 		break;
1758 	default:
1759 		rtlhal->oem_id = RT_CID_DEFAULT;
1760 		break;
1761 	}
1762 exit:
1763 	kfree(hwinfo);
1764 }
1765 
1766 static void _rtl8723e_hal_customized_behavior(struct ieee80211_hw *hw)
1767 {
1768 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1769 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1770 
1771 	rtlpriv->ledctl.led_opendrain = true;
1772 	switch (rtlhal->oem_id) {
1773 	case RT_CID_819X_HP:
1774 		rtlpriv->ledctl.led_opendrain = true;
1775 		break;
1776 	case RT_CID_819X_LENOVO:
1777 	case RT_CID_DEFAULT:
1778 	case RT_CID_TOSHIBA:
1779 	case RT_CID_CCX:
1780 	case RT_CID_819X_ACER:
1781 	case RT_CID_WHQL:
1782 	default:
1783 		break;
1784 	}
1785 	RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
1786 		 "RT Customized ID: 0x%02X\n", rtlhal->oem_id);
1787 }
1788 
1789 void rtl8723e_read_eeprom_info(struct ieee80211_hw *hw)
1790 {
1791 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1792 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
1793 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1794 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1795 	u8 tmp_u1b;
1796 	u32 value32;
1797 
1798 	value32 = rtl_read_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST]);
1799 	value32 = (value32 & ~EFUSE_SEL_MASK) | EFUSE_SEL(EFUSE_WIFI_SEL_0);
1800 	rtl_write_dword(rtlpriv, rtlpriv->cfg->maps[EFUSE_TEST], value32);
1801 
1802 	rtlhal->version = _rtl8723e_read_chip_version(hw);
1803 
1804 	if (get_rf_type(rtlphy) == RF_1T1R)
1805 		rtlpriv->dm.rfpath_rxenable[0] = true;
1806 	else
1807 		rtlpriv->dm.rfpath_rxenable[0] =
1808 		    rtlpriv->dm.rfpath_rxenable[1] = true;
1809 	RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "VersionID = 0x%4x\n",
1810 						rtlhal->version);
1811 
1812 	tmp_u1b = rtl_read_byte(rtlpriv, REG_9346CR);
1813 	if (tmp_u1b & BIT(4)) {
1814 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Boot from EEPROM\n");
1815 		rtlefuse->epromtype = EEPROM_93C46;
1816 	} else {
1817 		RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG, "Boot from EFUSE\n");
1818 		rtlefuse->epromtype = EEPROM_BOOT_EFUSE;
1819 	}
1820 	if (tmp_u1b & BIT(5)) {
1821 		RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "Autoload OK\n");
1822 		rtlefuse->autoload_failflag = false;
1823 		_rtl8723e_read_adapter_info(hw, false);
1824 	} else {
1825 		rtlefuse->autoload_failflag = true;
1826 		_rtl8723e_read_adapter_info(hw, false);
1827 		pr_err("Autoload ERR!!\n");
1828 	}
1829 	_rtl8723e_hal_customized_behavior(hw);
1830 }
1831 
1832 static void rtl8723e_update_hal_rate_table(struct ieee80211_hw *hw,
1833 					   struct ieee80211_sta *sta)
1834 {
1835 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1836 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1837 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1838 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1839 	u32 ratr_value;
1840 	u8 ratr_index = 0;
1841 	u8 b_nmode = mac->ht_enable;
1842 	u16 shortgi_rate;
1843 	u32 tmp_ratr_value;
1844 	u8 curtxbw_40mhz = mac->bw_40;
1845 	u8 curshortgi_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
1846 				1 : 0;
1847 	u8 curshortgi_20mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
1848 				1 : 0;
1849 	enum wireless_mode wirelessmode = mac->mode;
1850 	u32 ratr_mask;
1851 
1852 	if (rtlhal->current_bandtype == BAND_ON_5G)
1853 		ratr_value = sta->supp_rates[1] << 4;
1854 	else
1855 		ratr_value = sta->supp_rates[0];
1856 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
1857 		ratr_value = 0xfff;
1858 	ratr_value |= (sta->ht_cap.mcs.rx_mask[1] << 20 |
1859 			sta->ht_cap.mcs.rx_mask[0] << 12);
1860 	switch (wirelessmode) {
1861 	case WIRELESS_MODE_B:
1862 		if (ratr_value & 0x0000000c)
1863 			ratr_value &= 0x0000000d;
1864 		else
1865 			ratr_value &= 0x0000000f;
1866 		break;
1867 	case WIRELESS_MODE_G:
1868 		ratr_value &= 0x00000FF5;
1869 		break;
1870 	case WIRELESS_MODE_N_24G:
1871 	case WIRELESS_MODE_N_5G:
1872 		b_nmode = 1;
1873 		if (get_rf_type(rtlphy) == RF_1T2R ||
1874 		    get_rf_type(rtlphy) == RF_1T1R)
1875 			ratr_mask = 0x000ff005;
1876 		else
1877 			ratr_mask = 0x0f0ff005;
1878 
1879 		ratr_value &= ratr_mask;
1880 		break;
1881 	default:
1882 		if (rtlphy->rf_type == RF_1T2R)
1883 			ratr_value &= 0x000ff0ff;
1884 		else
1885 			ratr_value &= 0x0f0ff0ff;
1886 
1887 		break;
1888 	}
1889 
1890 	if ((rtlpriv->btcoexist.bt_coexistence) &&
1891 	    (rtlpriv->btcoexist.bt_coexist_type == BT_CSR_BC4) &&
1892 	    (rtlpriv->btcoexist.bt_cur_state) &&
1893 	    (rtlpriv->btcoexist.bt_ant_isolation) &&
1894 	    ((rtlpriv->btcoexist.bt_service == BT_SCO) ||
1895 	    (rtlpriv->btcoexist.bt_service == BT_BUSY)))
1896 		ratr_value &= 0x0fffcfc0;
1897 	else
1898 		ratr_value &= 0x0FFFFFFF;
1899 
1900 	if (b_nmode &&
1901 	    ((curtxbw_40mhz && curshortgi_40mhz) ||
1902 	     (!curtxbw_40mhz && curshortgi_20mhz))) {
1903 		ratr_value |= 0x10000000;
1904 		tmp_ratr_value = (ratr_value >> 12);
1905 
1906 		for (shortgi_rate = 15; shortgi_rate > 0; shortgi_rate--) {
1907 			if ((1 << shortgi_rate) & tmp_ratr_value)
1908 				break;
1909 		}
1910 
1911 		shortgi_rate = (shortgi_rate << 12) | (shortgi_rate << 8) |
1912 		    (shortgi_rate << 4) | (shortgi_rate);
1913 	}
1914 
1915 	rtl_write_dword(rtlpriv, REG_ARFR0 + ratr_index * 4, ratr_value);
1916 
1917 	RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
1918 		 "%x\n", rtl_read_dword(rtlpriv, REG_ARFR0));
1919 }
1920 
1921 static void rtl8723e_update_hal_rate_mask(struct ieee80211_hw *hw,
1922 					  struct ieee80211_sta *sta,
1923 					  u8 rssi_level, bool update_bw)
1924 {
1925 	struct rtl_priv *rtlpriv = rtl_priv(hw);
1926 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
1927 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
1928 	struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
1929 	struct rtl_sta_info *sta_entry = NULL;
1930 	u32 ratr_bitmap;
1931 	u8 ratr_index;
1932 	u8 curtxbw_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
1933 				? 1 : 0;
1934 	u8 curshortgi_40mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
1935 				1 : 0;
1936 	u8 curshortgi_20mhz = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
1937 				1 : 0;
1938 	enum wireless_mode wirelessmode = 0;
1939 	bool shortgi = false;
1940 	u8 rate_mask[5];
1941 	u8 macid = 0;
1942 	/*u8 mimo_ps = IEEE80211_SMPS_OFF;*/
1943 
1944 	sta_entry = (struct rtl_sta_info *)sta->drv_priv;
1945 	wirelessmode = sta_entry->wireless_mode;
1946 	if (mac->opmode == NL80211_IFTYPE_STATION)
1947 		curtxbw_40mhz = mac->bw_40;
1948 	else if (mac->opmode == NL80211_IFTYPE_AP ||
1949 		mac->opmode == NL80211_IFTYPE_ADHOC)
1950 		macid = sta->aid + 1;
1951 
1952 	if (rtlhal->current_bandtype == BAND_ON_5G)
1953 		ratr_bitmap = sta->supp_rates[1] << 4;
1954 	else
1955 		ratr_bitmap = sta->supp_rates[0];
1956 	if (mac->opmode == NL80211_IFTYPE_ADHOC)
1957 		ratr_bitmap = 0xfff;
1958 	ratr_bitmap |= (sta->ht_cap.mcs.rx_mask[1] << 20 |
1959 			sta->ht_cap.mcs.rx_mask[0] << 12);
1960 	switch (wirelessmode) {
1961 	case WIRELESS_MODE_B:
1962 		ratr_index = RATR_INX_WIRELESS_B;
1963 		if (ratr_bitmap & 0x0000000c)
1964 			ratr_bitmap &= 0x0000000d;
1965 		else
1966 			ratr_bitmap &= 0x0000000f;
1967 		break;
1968 	case WIRELESS_MODE_G:
1969 		ratr_index = RATR_INX_WIRELESS_GB;
1970 
1971 		if (rssi_level == 1)
1972 			ratr_bitmap &= 0x00000f00;
1973 		else if (rssi_level == 2)
1974 			ratr_bitmap &= 0x00000ff0;
1975 		else
1976 			ratr_bitmap &= 0x00000ff5;
1977 		break;
1978 	case WIRELESS_MODE_A:
1979 		ratr_index = RATR_INX_WIRELESS_G;
1980 		ratr_bitmap &= 0x00000ff0;
1981 		break;
1982 	case WIRELESS_MODE_N_24G:
1983 	case WIRELESS_MODE_N_5G:
1984 		ratr_index = RATR_INX_WIRELESS_NGB;
1985 		if (rtlphy->rf_type == RF_1T2R ||
1986 		    rtlphy->rf_type == RF_1T1R) {
1987 			if (curtxbw_40mhz) {
1988 				if (rssi_level == 1)
1989 					ratr_bitmap &= 0x000f0000;
1990 				else if (rssi_level == 2)
1991 					ratr_bitmap &= 0x000ff000;
1992 				else
1993 					ratr_bitmap &= 0x000ff015;
1994 			} else {
1995 				if (rssi_level == 1)
1996 					ratr_bitmap &= 0x000f0000;
1997 				else if (rssi_level == 2)
1998 					ratr_bitmap &= 0x000ff000;
1999 				else
2000 					ratr_bitmap &= 0x000ff005;
2001 			}
2002 		} else {
2003 			if (curtxbw_40mhz) {
2004 				if (rssi_level == 1)
2005 					ratr_bitmap &= 0x0f0f0000;
2006 				else if (rssi_level == 2)
2007 					ratr_bitmap &= 0x0f0ff000;
2008 				else
2009 					ratr_bitmap &= 0x0f0ff015;
2010 			} else {
2011 				if (rssi_level == 1)
2012 					ratr_bitmap &= 0x0f0f0000;
2013 				else if (rssi_level == 2)
2014 					ratr_bitmap &= 0x0f0ff000;
2015 				else
2016 					ratr_bitmap &= 0x0f0ff005;
2017 			}
2018 		}
2019 
2020 		if ((curtxbw_40mhz && curshortgi_40mhz) ||
2021 		    (!curtxbw_40mhz && curshortgi_20mhz)) {
2022 			if (macid == 0)
2023 				shortgi = true;
2024 			else if (macid == 1)
2025 				shortgi = false;
2026 		}
2027 		break;
2028 	default:
2029 		ratr_index = RATR_INX_WIRELESS_NGB;
2030 
2031 		if (rtlphy->rf_type == RF_1T2R)
2032 			ratr_bitmap &= 0x000ff0ff;
2033 		else
2034 			ratr_bitmap &= 0x0f0ff0ff;
2035 		break;
2036 	}
2037 	sta_entry->ratr_index = ratr_index;
2038 
2039 	RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
2040 		 "ratr_bitmap :%x\n", ratr_bitmap);
2041 	*(u32 *)&rate_mask = (ratr_bitmap & 0x0fffffff) |
2042 			     (ratr_index << 28);
2043 	rate_mask[4] = macid | (shortgi ? 0x20 : 0x00) | 0x80;
2044 	RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
2045 		 "Rate_index:%x, ratr_val:%x, %x:%x:%x:%x:%x\n",
2046 		  ratr_index, ratr_bitmap,
2047 		  rate_mask[0], rate_mask[1],
2048 		  rate_mask[2], rate_mask[3],
2049 		  rate_mask[4]);
2050 	rtl8723e_fill_h2c_cmd(hw, H2C_RA_MASK, 5, rate_mask);
2051 }
2052 
2053 void rtl8723e_update_hal_rate_tbl(struct ieee80211_hw *hw,
2054 				  struct ieee80211_sta *sta, u8 rssi_level,
2055 				  bool update_bw)
2056 {
2057 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2058 
2059 	if (rtlpriv->dm.useramask)
2060 		rtl8723e_update_hal_rate_mask(hw, sta, rssi_level, update_bw);
2061 	else
2062 		rtl8723e_update_hal_rate_table(hw, sta);
2063 }
2064 
2065 void rtl8723e_update_channel_access_setting(struct ieee80211_hw *hw)
2066 {
2067 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2068 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2069 	u16 sifs_timer;
2070 
2071 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SLOT_TIME, &mac->slot_time);
2072 	if (!mac->ht_enable)
2073 		sifs_timer = 0x0a0a;
2074 	else
2075 		sifs_timer = 0x1010;
2076 	rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_SIFS, (u8 *)&sifs_timer);
2077 }
2078 
2079 bool rtl8723e_gpio_radio_on_off_checking(struct ieee80211_hw *hw, u8 *valid)
2080 {
2081 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2082 	struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
2083 	struct rtl_phy *rtlphy = &(rtlpriv->phy);
2084 	enum rf_pwrstate e_rfpowerstate_toset;
2085 	u8 u1tmp;
2086 	bool b_actuallyset = false;
2087 
2088 	if (rtlpriv->rtlhal.being_init_adapter)
2089 		return false;
2090 
2091 	if (ppsc->swrf_processing)
2092 		return false;
2093 
2094 	spin_lock(&rtlpriv->locks.rf_ps_lock);
2095 	if (ppsc->rfchange_inprogress) {
2096 		spin_unlock(&rtlpriv->locks.rf_ps_lock);
2097 		return false;
2098 	} else {
2099 		ppsc->rfchange_inprogress = true;
2100 		spin_unlock(&rtlpriv->locks.rf_ps_lock);
2101 	}
2102 
2103 	rtl_write_byte(rtlpriv, REG_GPIO_IO_SEL_2,
2104 		       rtl_read_byte(rtlpriv, REG_GPIO_IO_SEL_2)&~(BIT(1)));
2105 
2106 	u1tmp = rtl_read_byte(rtlpriv, REG_GPIO_PIN_CTRL_2);
2107 
2108 	if (rtlphy->polarity_ctl)
2109 		e_rfpowerstate_toset = (u1tmp & BIT(1)) ? ERFOFF : ERFON;
2110 	else
2111 		e_rfpowerstate_toset = (u1tmp & BIT(1)) ? ERFON : ERFOFF;
2112 
2113 	if (ppsc->hwradiooff && (e_rfpowerstate_toset == ERFON)) {
2114 		RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
2115 			 "GPIOChangeRF  - HW Radio ON, RF ON\n");
2116 
2117 		e_rfpowerstate_toset = ERFON;
2118 		ppsc->hwradiooff = false;
2119 		b_actuallyset = true;
2120 	} else if (!ppsc->hwradiooff && (e_rfpowerstate_toset == ERFOFF)) {
2121 		RT_TRACE(rtlpriv, COMP_RF, DBG_DMESG,
2122 			 "GPIOChangeRF  - HW Radio OFF, RF OFF\n");
2123 
2124 		e_rfpowerstate_toset = ERFOFF;
2125 		ppsc->hwradiooff = true;
2126 		b_actuallyset = true;
2127 	}
2128 
2129 	if (b_actuallyset) {
2130 		spin_lock(&rtlpriv->locks.rf_ps_lock);
2131 		ppsc->rfchange_inprogress = false;
2132 		spin_unlock(&rtlpriv->locks.rf_ps_lock);
2133 	} else {
2134 		if (ppsc->reg_rfps_level & RT_RF_OFF_LEVL_HALT_NIC)
2135 			RT_SET_PS_LEVEL(ppsc, RT_RF_OFF_LEVL_HALT_NIC);
2136 
2137 		spin_lock(&rtlpriv->locks.rf_ps_lock);
2138 		ppsc->rfchange_inprogress = false;
2139 		spin_unlock(&rtlpriv->locks.rf_ps_lock);
2140 	}
2141 
2142 	*valid = 1;
2143 	return !ppsc->hwradiooff;
2144 
2145 }
2146 
2147 void rtl8723e_set_key(struct ieee80211_hw *hw, u32 key_index,
2148 		      u8 *p_macaddr, bool is_group, u8 enc_algo,
2149 		      bool is_wepkey, bool clear_all)
2150 {
2151 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2152 	struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
2153 	struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
2154 	u8 *macaddr = p_macaddr;
2155 	u32 entry_id = 0;
2156 	bool is_pairwise = false;
2157 
2158 	static u8 cam_const_addr[4][6] = {
2159 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
2160 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
2161 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
2162 		{0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
2163 	};
2164 	static u8 cam_const_broad[] = {
2165 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2166 	};
2167 
2168 	if (clear_all) {
2169 		u8 idx = 0;
2170 		u8 cam_offset = 0;
2171 		u8 clear_number = 5;
2172 
2173 		RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "clear_all\n");
2174 
2175 		for (idx = 0; idx < clear_number; idx++) {
2176 			rtl_cam_mark_invalid(hw, cam_offset + idx);
2177 			rtl_cam_empty_entry(hw, cam_offset + idx);
2178 
2179 			if (idx < 5) {
2180 				memset(rtlpriv->sec.key_buf[idx], 0,
2181 				       MAX_KEY_LEN);
2182 				rtlpriv->sec.key_len[idx] = 0;
2183 			}
2184 		}
2185 
2186 	} else {
2187 		switch (enc_algo) {
2188 		case WEP40_ENCRYPTION:
2189 			enc_algo = CAM_WEP40;
2190 			break;
2191 		case WEP104_ENCRYPTION:
2192 			enc_algo = CAM_WEP104;
2193 			break;
2194 		case TKIP_ENCRYPTION:
2195 			enc_algo = CAM_TKIP;
2196 			break;
2197 		case AESCCMP_ENCRYPTION:
2198 			enc_algo = CAM_AES;
2199 			break;
2200 		default:
2201 			RT_TRACE(rtlpriv, COMP_ERR, DBG_LOUD,
2202 				 "switch case %#x not processed\n", enc_algo);
2203 			enc_algo = CAM_TKIP;
2204 			break;
2205 		}
2206 
2207 		if (is_wepkey || rtlpriv->sec.use_defaultkey) {
2208 			macaddr = cam_const_addr[key_index];
2209 			entry_id = key_index;
2210 		} else {
2211 			if (is_group) {
2212 				macaddr = cam_const_broad;
2213 				entry_id = key_index;
2214 			} else {
2215 				if (mac->opmode == NL80211_IFTYPE_AP) {
2216 					entry_id =
2217 					  rtl_cam_get_free_entry(hw, p_macaddr);
2218 					if (entry_id >=  TOTAL_CAM_ENTRY) {
2219 						pr_err("Can not find free hw security cam entry\n");
2220 						return;
2221 					}
2222 				} else {
2223 					entry_id = CAM_PAIRWISE_KEY_POSITION;
2224 				}
2225 
2226 				key_index = PAIRWISE_KEYIDX;
2227 				is_pairwise = true;
2228 			}
2229 		}
2230 
2231 		if (rtlpriv->sec.key_len[key_index] == 0) {
2232 			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
2233 				 "delete one entry, entry_id is %d\n",
2234 				 entry_id);
2235 			if (mac->opmode == NL80211_IFTYPE_AP)
2236 				rtl_cam_del_entry(hw, p_macaddr);
2237 			rtl_cam_delete_one_entry(hw, p_macaddr, entry_id);
2238 		} else {
2239 			RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
2240 				 "add one entry\n");
2241 			if (is_pairwise) {
2242 				RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
2243 					 "set Pairwise key\n");
2244 
2245 				rtl_cam_add_one_entry(hw, macaddr, key_index,
2246 						      entry_id, enc_algo,
2247 						      CAM_CONFIG_NO_USEDK,
2248 						      rtlpriv->sec.key_buf[key_index]);
2249 			} else {
2250 				RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
2251 					 "set group key\n");
2252 
2253 				if (mac->opmode == NL80211_IFTYPE_ADHOC) {
2254 					rtl_cam_add_one_entry(hw,
2255 							rtlefuse->dev_addr,
2256 							PAIRWISE_KEYIDX,
2257 							CAM_PAIRWISE_KEY_POSITION,
2258 							enc_algo,
2259 							CAM_CONFIG_NO_USEDK,
2260 							rtlpriv->sec.key_buf
2261 							[entry_id]);
2262 				}
2263 
2264 				rtl_cam_add_one_entry(hw, macaddr, key_index,
2265 						entry_id, enc_algo,
2266 						CAM_CONFIG_NO_USEDK,
2267 						rtlpriv->sec.key_buf[entry_id]);
2268 			}
2269 
2270 		}
2271 	}
2272 }
2273 
2274 static void rtl8723e_bt_var_init(struct ieee80211_hw *hw)
2275 {
2276 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2277 
2278 	rtlpriv->btcoexist.bt_coexistence =
2279 		rtlpriv->btcoexist.eeprom_bt_coexist;
2280 	rtlpriv->btcoexist.bt_ant_num =
2281 		rtlpriv->btcoexist.eeprom_bt_ant_num;
2282 	rtlpriv->btcoexist.bt_coexist_type =
2283 		rtlpriv->btcoexist.eeprom_bt_type;
2284 
2285 	rtlpriv->btcoexist.bt_ant_isolation =
2286 		rtlpriv->btcoexist.eeprom_bt_ant_isol;
2287 
2288 	rtlpriv->btcoexist.bt_radio_shared_type =
2289 		rtlpriv->btcoexist.eeprom_bt_radio_shared;
2290 
2291 	RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2292 		 "BT Coexistence = 0x%x\n",
2293 		 rtlpriv->btcoexist.bt_coexistence);
2294 
2295 	if (rtlpriv->btcoexist.bt_coexistence) {
2296 		rtlpriv->btcoexist.bt_busy_traffic = false;
2297 		rtlpriv->btcoexist.bt_traffic_mode_set = false;
2298 		rtlpriv->btcoexist.bt_non_traffic_mode_set = false;
2299 
2300 		rtlpriv->btcoexist.cstate = 0;
2301 		rtlpriv->btcoexist.previous_state = 0;
2302 
2303 		if (rtlpriv->btcoexist.bt_ant_num == ANT_X2) {
2304 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2305 				 "BlueTooth BT_Ant_Num = Antx2\n");
2306 		} else if (rtlpriv->btcoexist.bt_ant_num == ANT_X1) {
2307 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2308 				 "BlueTooth BT_Ant_Num = Antx1\n");
2309 		}
2310 		switch (rtlpriv->btcoexist.bt_coexist_type) {
2311 		case BT_2WIRE:
2312 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2313 				 "BlueTooth BT_CoexistType = BT_2Wire\n");
2314 			break;
2315 		case BT_ISSC_3WIRE:
2316 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2317 				 "BlueTooth BT_CoexistType = BT_ISSC_3Wire\n");
2318 			break;
2319 		case BT_ACCEL:
2320 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2321 				 "BlueTooth BT_CoexistType = BT_ACCEL\n");
2322 			break;
2323 		case BT_CSR_BC4:
2324 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2325 				 "BlueTooth BT_CoexistType = BT_CSR_BC4\n");
2326 			break;
2327 		case BT_CSR_BC8:
2328 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2329 				 "BlueTooth BT_CoexistType = BT_CSR_BC8\n");
2330 			break;
2331 		case BT_RTL8756:
2332 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2333 				 "BlueTooth BT_CoexistType = BT_RTL8756\n");
2334 			break;
2335 		default:
2336 			RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2337 				 "BlueTooth BT_CoexistType = Unknown\n");
2338 			break;
2339 		}
2340 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2341 			 "BlueTooth BT_Ant_isolation = %d\n",
2342 			 rtlpriv->btcoexist.bt_ant_isolation);
2343 		RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_TRACE,
2344 			 "BT_RadioSharedType = 0x%x\n",
2345 			 rtlpriv->btcoexist.bt_radio_shared_type);
2346 		rtlpriv->btcoexist.bt_active_zero_cnt = 0;
2347 		rtlpriv->btcoexist.cur_bt_disabled = false;
2348 		rtlpriv->btcoexist.pre_bt_disabled = false;
2349 	}
2350 }
2351 
2352 void rtl8723e_read_bt_coexist_info_from_hwpg(struct ieee80211_hw *hw,
2353 					     bool auto_load_fail, u8 *hwinfo)
2354 {
2355 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2356 	u8 value;
2357 	u32 tmpu_32;
2358 
2359 	if (!auto_load_fail) {
2360 		tmpu_32 = rtl_read_dword(rtlpriv, REG_MULTI_FUNC_CTRL);
2361 		if (tmpu_32 & BIT(18))
2362 			rtlpriv->btcoexist.eeprom_bt_coexist = 1;
2363 		else
2364 			rtlpriv->btcoexist.eeprom_bt_coexist = 0;
2365 		value = hwinfo[RF_OPTION4];
2366 		rtlpriv->btcoexist.eeprom_bt_type = BT_RTL8723A;
2367 		rtlpriv->btcoexist.eeprom_bt_ant_num = (value & 0x1);
2368 		rtlpriv->btcoexist.eeprom_bt_ant_isol = ((value & 0x10) >> 4);
2369 		rtlpriv->btcoexist.eeprom_bt_radio_shared =
2370 		  ((value & 0x20) >> 5);
2371 	} else {
2372 		rtlpriv->btcoexist.eeprom_bt_coexist = 0;
2373 		rtlpriv->btcoexist.eeprom_bt_type = BT_RTL8723A;
2374 		rtlpriv->btcoexist.eeprom_bt_ant_num = ANT_X2;
2375 		rtlpriv->btcoexist.eeprom_bt_ant_isol = 0;
2376 		rtlpriv->btcoexist.eeprom_bt_radio_shared = BT_RADIO_SHARED;
2377 	}
2378 
2379 	rtl8723e_bt_var_init(hw);
2380 }
2381 
2382 void rtl8723e_bt_reg_init(struct ieee80211_hw *hw)
2383 {
2384 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2385 
2386 	/* 0:Low, 1:High, 2:From Efuse. */
2387 	rtlpriv->btcoexist.reg_bt_iso = 2;
2388 	/* 0:Idle, 1:None-SCO, 2:SCO, 3:From Counter. */
2389 	rtlpriv->btcoexist.reg_bt_sco = 3;
2390 	/* 0:Disable BT control A-MPDU, 1:Enable BT control A-MPDU. */
2391 	rtlpriv->btcoexist.reg_bt_sco = 0;
2392 }
2393 
2394 void rtl8723e_bt_hw_init(struct ieee80211_hw *hw)
2395 {
2396 	struct rtl_priv *rtlpriv = rtl_priv(hw);
2397 
2398 	if (rtlpriv->cfg->ops->get_btc_status())
2399 		rtlpriv->btcoexist.btc_ops->btc_init_hw_config(rtlpriv);
2400 }
2401 
2402 void rtl8723e_suspend(struct ieee80211_hw *hw)
2403 {
2404 }
2405 
2406 void rtl8723e_resume(struct ieee80211_hw *hw)
2407 {
2408 }
2409