blob: 44f510a94b09cc66dce12c5470ac094c029edaa8 [file] [log] [blame]
Chaoming Li9fe255e2011-05-03 09:47:55 -05001/******************************************************************************
2 *
Larry Fingerca742cd2012-01-07 20:46:47 -06003 * Copyright(c) 2009-2012 Realtek Corporation.
Chaoming Li9fe255e2011-05-03 09:47:55 -05004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
Chaoming Li9fe255e2011-05-03 09:47:55 -050014 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
16 *
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19 * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
20 * Hsinchu 300, Taiwan.
21 *
22 * Larry Finger <Larry.Finger@lwfinger.net>
23 *
24 *****************************************************************************/
25
26#include "../wifi.h"
27#include "../base.h"
Larry Finger6f8214b2015-01-06 09:58:04 -060028#include "../core.h"
Chaoming Li9fe255e2011-05-03 09:47:55 -050029#include "reg.h"
30#include "def.h"
31#include "phy.h"
32#include "dm.h"
33#include "fw.h"
34
Chaoming Li9fe255e2011-05-03 09:47:55 -050035static const u32 edca_setting_dl[PEER_MAX] = {
36 0xa44f, /* 0 UNKNOWN */
37 0x5ea44f, /* 1 REALTEK_90 */
38 0x5ea44f, /* 2 REALTEK_92SE */
39 0xa630, /* 3 BROAD */
40 0xa44f, /* 4 RAL */
41 0xa630, /* 5 ATH */
42 0xa630, /* 6 CISCO */
43 0xa42b, /* 7 MARV */
44};
45
46static const u32 edca_setting_dl_gmode[PEER_MAX] = {
47 0x4322, /* 0 UNKNOWN */
48 0xa44f, /* 1 REALTEK_90 */
49 0x5ea44f, /* 2 REALTEK_92SE */
50 0xa42b, /* 3 BROAD */
51 0x5e4322, /* 4 RAL */
52 0x4322, /* 5 ATH */
53 0xa430, /* 6 CISCO */
54 0x5ea44f, /* 7 MARV */
55};
56
57static const u32 edca_setting_ul[PEER_MAX] = {
58 0x5e4322, /* 0 UNKNOWN */
59 0xa44f, /* 1 REALTEK_90 */
60 0x5ea44f, /* 2 REALTEK_92SE */
61 0x5ea322, /* 3 BROAD */
62 0x5ea422, /* 4 RAL */
63 0x5ea322, /* 5 ATH */
64 0x3ea44f, /* 6 CISCO */
65 0x5ea44f, /* 7 MARV */
66};
67
68static void _rtl92s_dm_check_edca_turbo(struct ieee80211_hw *hw)
69{
70 struct rtl_priv *rtlpriv = rtl_priv(hw);
71 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
72
73 static u64 last_txok_cnt;
74 static u64 last_rxok_cnt;
75 u64 cur_txok_cnt = 0;
76 u64 cur_rxok_cnt = 0;
77
78 u32 edca_be_ul = edca_setting_ul[mac->vendor];
79 u32 edca_be_dl = edca_setting_dl[mac->vendor];
80 u32 edca_gmode = edca_setting_dl_gmode[mac->vendor];
81
82 if (mac->link_state != MAC80211_LINKED) {
83 rtlpriv->dm.current_turbo_edca = false;
84 goto dm_checkedcaturbo_exit;
85 }
86
87 if ((!rtlpriv->dm.is_any_nonbepkts) &&
88 (!rtlpriv->dm.disable_framebursting)) {
89 cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
90 cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
91
92 if (rtlpriv->phy.rf_type == RF_1T2R) {
93 if (cur_txok_cnt > 4 * cur_rxok_cnt) {
94 /* Uplink TP is present. */
95 if (rtlpriv->dm.is_cur_rdlstate ||
96 !rtlpriv->dm.current_turbo_edca) {
97 rtl_write_dword(rtlpriv, EDCAPARA_BE,
98 edca_be_ul);
99 rtlpriv->dm.is_cur_rdlstate = false;
100 }
101 } else {/* Balance TP is present. */
102 if (!rtlpriv->dm.is_cur_rdlstate ||
103 !rtlpriv->dm.current_turbo_edca) {
104 if (mac->mode == WIRELESS_MODE_G ||
105 mac->mode == WIRELESS_MODE_B)
106 rtl_write_dword(rtlpriv,
107 EDCAPARA_BE,
108 edca_gmode);
109 else
110 rtl_write_dword(rtlpriv,
111 EDCAPARA_BE,
112 edca_be_dl);
113 rtlpriv->dm.is_cur_rdlstate = true;
114 }
115 }
116 rtlpriv->dm.current_turbo_edca = true;
117 } else {
118 if (cur_rxok_cnt > 4 * cur_txok_cnt) {
119 if (!rtlpriv->dm.is_cur_rdlstate ||
120 !rtlpriv->dm.current_turbo_edca) {
121 if (mac->mode == WIRELESS_MODE_G ||
122 mac->mode == WIRELESS_MODE_B)
123 rtl_write_dword(rtlpriv,
124 EDCAPARA_BE,
125 edca_gmode);
126 else
127 rtl_write_dword(rtlpriv,
128 EDCAPARA_BE,
129 edca_be_dl);
130 rtlpriv->dm.is_cur_rdlstate = true;
131 }
132 } else {
133 if (rtlpriv->dm.is_cur_rdlstate ||
134 !rtlpriv->dm.current_turbo_edca) {
135 rtl_write_dword(rtlpriv, EDCAPARA_BE,
136 edca_be_ul);
137 rtlpriv->dm.is_cur_rdlstate = false;
138 }
139 }
140 rtlpriv->dm.current_turbo_edca = true;
141 }
142 } else {
143 if (rtlpriv->dm.current_turbo_edca) {
144 u8 tmp = AC0_BE;
145 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM,
Joe Perches2c208892012-06-04 12:44:17 +0000146 &tmp);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500147 rtlpriv->dm.current_turbo_edca = false;
148 }
149 }
150
151dm_checkedcaturbo_exit:
152 rtlpriv->dm.is_any_nonbepkts = false;
153 last_txok_cnt = rtlpriv->stats.txbytesunicast;
154 last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
155}
156
157static void _rtl92s_dm_txpowertracking_callback_thermalmeter(
158 struct ieee80211_hw *hw)
159{
160 struct rtl_priv *rtlpriv = rtl_priv(hw);
161 struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
162 u8 thermalvalue = 0;
Larry Finger2455c922013-03-24 22:06:38 -0500163 u32 fw_cmd = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500164
165 rtlpriv->dm.txpower_trackinginit = true;
166
167 thermalvalue = (u8)rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER, 0x1f);
168
169 RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800170 "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermal meter 0x%x\n",
171 thermalvalue,
172 rtlpriv->dm.thermalvalue, rtlefuse->eeprom_thermalmeter);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500173
174 if (thermalvalue) {
175 rtlpriv->dm.thermalvalue = thermalvalue;
Larry Finger2455c922013-03-24 22:06:38 -0500176 if (hal_get_firmwareversion(rtlpriv) >= 0x35) {
177 rtl92s_phy_set_fw_cmd(hw, FW_CMD_TXPWR_TRACK_THERMAL);
178 } else {
179 fw_cmd = (FW_TXPWR_TRACK_THERMAL |
180 (rtlpriv->efuse.thermalmeter[0] << 8) |
181 (thermalvalue << 16));
182
183 RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
184 "Write to FW Thermal Val = 0x%x\n", fw_cmd);
185
186 rtl_write_dword(rtlpriv, WFM5, fw_cmd);
187 rtl92s_phy_chk_fwcmd_iodone(hw);
188 }
Chaoming Li9fe255e2011-05-03 09:47:55 -0500189 }
190
191 rtlpriv->dm.txpowercount = 0;
192}
193
194static void _rtl92s_dm_check_txpowertracking_thermalmeter(
195 struct ieee80211_hw *hw)
196{
197 struct rtl_priv *rtlpriv = rtl_priv(hw);
198 struct rtl_phy *rtlphy = &(rtlpriv->phy);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500199 u8 tx_power_checkcnt = 5;
200
201 /* 2T2R TP issue */
202 if (rtlphy->rf_type == RF_2T2R)
203 return;
204
205 if (!rtlpriv->dm.txpower_tracking)
206 return;
207
208 if (rtlpriv->dm.txpowercount <= tx_power_checkcnt) {
209 rtlpriv->dm.txpowercount++;
210 return;
211 }
212
Hans Ulli Kroll1637c1b2015-06-07 13:19:16 +0200213 if (!rtlpriv->dm.tm_trigger) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500214 rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER,
215 RFREG_OFFSET_MASK, 0x60);
Hans Ulli Kroll1637c1b2015-06-07 13:19:16 +0200216 rtlpriv->dm.tm_trigger = 1;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500217 } else {
218 _rtl92s_dm_txpowertracking_callback_thermalmeter(hw);
Hans Ulli Kroll1637c1b2015-06-07 13:19:16 +0200219 rtlpriv->dm.tm_trigger = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500220 }
221}
222
223static void _rtl92s_dm_refresh_rateadaptive_mask(struct ieee80211_hw *hw)
224{
225 struct rtl_priv *rtlpriv = rtl_priv(hw);
226 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
227 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
228 struct rate_adaptive *ra = &(rtlpriv->ra);
Larry Finger2455c922013-03-24 22:06:38 -0500229 struct ieee80211_sta *sta = NULL;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500230 u32 low_rssi_thresh = 0;
231 u32 middle_rssi_thresh = 0;
232 u32 high_rssi_thresh = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500233
234 if (is_hal_stop(rtlhal))
235 return;
236
237 if (!rtlpriv->dm.useramask)
238 return;
239
Larry Finger2455c922013-03-24 22:06:38 -0500240 if (hal_get_firmwareversion(rtlpriv) >= 61 &&
241 !rtlpriv->dm.inform_fw_driverctrldm) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500242 rtl92s_phy_set_fw_cmd(hw, FW_CMD_CTRL_DM_BY_DRIVER);
243 rtlpriv->dm.inform_fw_driverctrldm = true;
244 }
245
Chaoming Li9fe255e2011-05-03 09:47:55 -0500246 if ((mac->link_state == MAC80211_LINKED) &&
247 (mac->opmode == NL80211_IFTYPE_STATION)) {
248 switch (ra->pre_ratr_state) {
249 case DM_RATR_STA_HIGH:
250 high_rssi_thresh = 40;
251 middle_rssi_thresh = 30;
252 low_rssi_thresh = 20;
253 break;
254 case DM_RATR_STA_MIDDLE:
255 high_rssi_thresh = 44;
256 middle_rssi_thresh = 30;
257 low_rssi_thresh = 20;
258 break;
259 case DM_RATR_STA_LOW:
260 high_rssi_thresh = 44;
261 middle_rssi_thresh = 34;
262 low_rssi_thresh = 20;
263 break;
264 case DM_RATR_STA_ULTRALOW:
265 high_rssi_thresh = 44;
266 middle_rssi_thresh = 34;
267 low_rssi_thresh = 24;
268 break;
269 default:
270 high_rssi_thresh = 44;
271 middle_rssi_thresh = 34;
272 low_rssi_thresh = 24;
273 break;
274 }
275
Larry Fingerda17fcf2012-10-25 13:46:31 -0500276 if (rtlpriv->dm.undec_sm_pwdb > (long)high_rssi_thresh) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500277 ra->ratr_state = DM_RATR_STA_HIGH;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500278 } else if (rtlpriv->dm.undec_sm_pwdb >
Chaoming Li9fe255e2011-05-03 09:47:55 -0500279 (long)middle_rssi_thresh) {
280 ra->ratr_state = DM_RATR_STA_LOW;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500281 } else if (rtlpriv->dm.undec_sm_pwdb >
Chaoming Li9fe255e2011-05-03 09:47:55 -0500282 (long)low_rssi_thresh) {
283 ra->ratr_state = DM_RATR_STA_LOW;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500284 } else {
285 ra->ratr_state = DM_RATR_STA_ULTRALOW;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500286 }
287
288 if (ra->pre_ratr_state != ra->ratr_state) {
Joe Perchesf30d7502012-01-04 19:40:41 -0800289 RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
290 "RSSI = %ld RSSI_LEVEL = %d PreState = %d, CurState = %d\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500291 rtlpriv->dm.undec_sm_pwdb, ra->ratr_state,
Joe Perchesf30d7502012-01-04 19:40:41 -0800292 ra->pre_ratr_state, ra->ratr_state);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500293
Larry Finger2455c922013-03-24 22:06:38 -0500294 rcu_read_lock();
295 sta = rtl_find_sta(hw, mac->bssid);
296 if (sta)
297 rtlpriv->cfg->ops->update_rate_tbl(hw, sta,
Ping-Ke Shih1d22b172017-09-29 14:47:59 -0500298 ra->ratr_state,
299 true);
Larry Finger2455c922013-03-24 22:06:38 -0500300 rcu_read_unlock();
301
Chaoming Li9fe255e2011-05-03 09:47:55 -0500302 ra->pre_ratr_state = ra->ratr_state;
303 }
304 }
Chaoming Li9fe255e2011-05-03 09:47:55 -0500305}
306
307static void _rtl92s_dm_switch_baseband_mrc(struct ieee80211_hw *hw)
308{
309 struct rtl_priv *rtlpriv = rtl_priv(hw);
310 struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
311 struct rtl_phy *rtlphy = &(rtlpriv->phy);
312 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
313 bool current_mrc;
314 bool enable_mrc = true;
315 long tmpentry_maxpwdb = 0;
316 u8 rssi_a = 0;
317 u8 rssi_b = 0;
318
319 if (is_hal_stop(rtlhal))
320 return;
321
322 if ((rtlphy->rf_type == RF_1T1R) || (rtlphy->rf_type == RF_2T2R))
323 return;
324
325 rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MRC, (u8 *)(&current_mrc));
326
327 if (mac->link_state >= MAC80211_LINKED) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500328 if (rtlpriv->dm.undec_sm_pwdb > tmpentry_maxpwdb) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500329 rssi_a = rtlpriv->stats.rx_rssi_percentage[RF90_PATH_A];
330 rssi_b = rtlpriv->stats.rx_rssi_percentage[RF90_PATH_B];
331 }
332 }
333
334 /* MRC settings would NOT affect TP on Wireless B mode. */
335 if (mac->mode != WIRELESS_MODE_B) {
336 if ((rssi_a == 0) && (rssi_b == 0)) {
337 enable_mrc = true;
338 } else if (rssi_b > 30) {
339 /* Turn on B-Path */
340 enable_mrc = true;
341 } else if (rssi_b < 5) {
342 /* Turn off B-path */
343 enable_mrc = false;
344 /* Take care of RSSI differentiation. */
345 } else if (rssi_a > 15 && (rssi_a >= rssi_b)) {
346 if ((rssi_a - rssi_b) > 15)
347 /* Turn off B-path */
348 enable_mrc = false;
349 else if ((rssi_a - rssi_b) < 10)
350 /* Turn on B-Path */
351 enable_mrc = true;
352 else
353 enable_mrc = current_mrc;
354 } else {
355 /* Turn on B-Path */
356 enable_mrc = true;
357 }
358 }
359
360 /* Update MRC settings if needed. */
361 if (enable_mrc != current_mrc)
362 rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_MRC,
363 (u8 *)&enable_mrc);
364
365}
366
367void rtl92s_dm_init_edca_turbo(struct ieee80211_hw *hw)
368{
369 struct rtl_priv *rtlpriv = rtl_priv(hw);
370
371 rtlpriv->dm.current_turbo_edca = false;
372 rtlpriv->dm.is_any_nonbepkts = false;
373 rtlpriv->dm.is_cur_rdlstate = false;
374}
375
376static void _rtl92s_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
377{
378 struct rtl_priv *rtlpriv = rtl_priv(hw);
379 struct rate_adaptive *ra = &(rtlpriv->ra);
380
381 ra->ratr_state = DM_RATR_STA_MAX;
382 ra->pre_ratr_state = DM_RATR_STA_MAX;
383
Larry Finger2455c922013-03-24 22:06:38 -0500384 if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER &&
385 hal_get_firmwareversion(rtlpriv) >= 60)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500386 rtlpriv->dm.useramask = true;
387 else
388 rtlpriv->dm.useramask = false;
389
390 rtlpriv->dm.useramask = false;
391 rtlpriv->dm.inform_fw_driverctrldm = false;
392}
393
394static void _rtl92s_dm_init_txpowertracking_thermalmeter(
395 struct ieee80211_hw *hw)
396{
397 struct rtl_priv *rtlpriv = rtl_priv(hw);
398
399 rtlpriv->dm.txpower_tracking = true;
400 rtlpriv->dm.txpowercount = 0;
401 rtlpriv->dm.txpower_trackinginit = false;
402}
403
404static void _rtl92s_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
405{
406 struct rtl_priv *rtlpriv = rtl_priv(hw);
407 struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
408 u32 ret_value;
409
410 ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
411 falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
412
413 ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
414 falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
415 falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
416 ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
417 falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
418
419 falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
420 falsealm_cnt->cnt_rate_illegal + falsealm_cnt->cnt_crc8_fail +
421 falsealm_cnt->cnt_mcs_fail;
422
423 /* read CCK false alarm */
424 ret_value = rtl_get_bbreg(hw, 0xc64, MASKDWORD);
425 falsealm_cnt->cnt_cck_fail = (ret_value & 0xffff);
426 falsealm_cnt->cnt_all = falsealm_cnt->cnt_ofdm_fail +
427 falsealm_cnt->cnt_cck_fail;
428}
429
430static void rtl92s_backoff_enable_flag(struct ieee80211_hw *hw)
431{
432 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Fingere0602752012-04-19 16:32:44 -0500433 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500434 struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
435
Larry Fingere0602752012-04-19 16:32:44 -0500436 if (falsealm_cnt->cnt_all > digtable->fa_highthresh) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500437 if ((digtable->back_val - 6) <
Larry Fingere0602752012-04-19 16:32:44 -0500438 digtable->backoffval_range_min)
Larry Fingerda17fcf2012-10-25 13:46:31 -0500439 digtable->back_val = digtable->backoffval_range_min;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500440 else
Larry Fingerda17fcf2012-10-25 13:46:31 -0500441 digtable->back_val -= 6;
Larry Fingere0602752012-04-19 16:32:44 -0500442 } else if (falsealm_cnt->cnt_all < digtable->fa_lowthresh) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500443 if ((digtable->back_val + 6) >
Larry Fingere0602752012-04-19 16:32:44 -0500444 digtable->backoffval_range_max)
Larry Fingerda17fcf2012-10-25 13:46:31 -0500445 digtable->back_val =
Larry Fingere0602752012-04-19 16:32:44 -0500446 digtable->backoffval_range_max;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500447 else
Larry Fingerda17fcf2012-10-25 13:46:31 -0500448 digtable->back_val += 6;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500449 }
450}
451
452static void _rtl92s_dm_initial_gain_sta_beforeconnect(struct ieee80211_hw *hw)
453{
454 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Fingere0602752012-04-19 16:32:44 -0500455 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500456 struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
457 static u8 initialized, force_write;
458 u8 initial_gain = 0;
459
Larry Fingerda17fcf2012-10-25 13:46:31 -0500460 if ((digtable->pre_sta_cstate == digtable->cur_sta_cstate) ||
461 (digtable->cur_sta_cstate == DIG_STA_BEFORE_CONNECT)) {
462 if (digtable->cur_sta_cstate == DIG_STA_BEFORE_CONNECT) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500463 if (rtlpriv->psc.rfpwr_state != ERFON)
464 return;
465
Larry Fingere0602752012-04-19 16:32:44 -0500466 if (digtable->backoff_enable_flag)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500467 rtl92s_backoff_enable_flag(hw);
468 else
Larry Fingered0fb7e2015-01-06 09:58:12 -0600469 digtable->back_val = DM_DIG_BACKOFF_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500470
Larry Fingerda17fcf2012-10-25 13:46:31 -0500471 if ((digtable->rssi_val + 10 - digtable->back_val) >
Larry Fingere6deaf82013-03-24 22:06:55 -0500472 digtable->rx_gain_max)
Larry Fingere0602752012-04-19 16:32:44 -0500473 digtable->cur_igvalue =
Larry Fingere6deaf82013-03-24 22:06:55 -0500474 digtable->rx_gain_max;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500475 else if ((digtable->rssi_val + 10 - digtable->back_val)
Larry Fingere6deaf82013-03-24 22:06:55 -0500476 < digtable->rx_gain_min)
Larry Fingere0602752012-04-19 16:32:44 -0500477 digtable->cur_igvalue =
Larry Fingere6deaf82013-03-24 22:06:55 -0500478 digtable->rx_gain_min;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500479 else
Daniel Stamerdac67972012-11-29 17:09:26 +0100480 digtable->cur_igvalue = digtable->rssi_val + 10
481 - digtable->back_val;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500482
483 if (falsealm_cnt->cnt_all > 10000)
Larry Fingere0602752012-04-19 16:32:44 -0500484 digtable->cur_igvalue =
485 (digtable->cur_igvalue > 0x33) ?
486 digtable->cur_igvalue : 0x33;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500487
488 if (falsealm_cnt->cnt_all > 16000)
Larry Fingere0602752012-04-19 16:32:44 -0500489 digtable->cur_igvalue =
Larry Fingere6deaf82013-03-24 22:06:55 -0500490 digtable->rx_gain_max;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500491 /* connected -> connected or disconnected -> disconnected */
492 } else {
493 /* Firmware control DIG, do nothing in driver dm */
494 return;
495 }
496 /* disconnected -> connected or connected ->
497 * disconnected or beforeconnect->(dis)connected */
498 } else {
499 /* Enable FW DIG */
Larry Fingere0602752012-04-19 16:32:44 -0500500 digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500501 rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_ENABLE);
502
Larry Fingered0fb7e2015-01-06 09:58:12 -0600503 digtable->back_val = DM_DIG_BACKOFF_MAX;
Larry Fingere0602752012-04-19 16:32:44 -0500504 digtable->cur_igvalue = rtlpriv->phy.default_initialgain[0];
505 digtable->pre_igvalue = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500506 return;
507 }
508
509 /* Forced writing to prevent from fw-dig overwriting. */
Larry Fingere0602752012-04-19 16:32:44 -0500510 if (digtable->pre_igvalue != rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1,
Chaoming Li9fe255e2011-05-03 09:47:55 -0500511 MASKBYTE0))
512 force_write = 1;
513
Larry Fingere0602752012-04-19 16:32:44 -0500514 if ((digtable->pre_igvalue != digtable->cur_igvalue) ||
Chaoming Li9fe255e2011-05-03 09:47:55 -0500515 !initialized || force_write) {
516 /* Disable FW DIG */
517 rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_DISABLE);
518
Larry Fingere0602752012-04-19 16:32:44 -0500519 initial_gain = (u8)digtable->cur_igvalue;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500520
521 /* Set initial gain. */
522 rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0, initial_gain);
523 rtl_set_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0, initial_gain);
Larry Fingere0602752012-04-19 16:32:44 -0500524 digtable->pre_igvalue = digtable->cur_igvalue;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500525 initialized = 1;
526 force_write = 0;
527 }
528}
529
530static void _rtl92s_dm_ctrl_initgain_bytwoport(struct ieee80211_hw *hw)
531{
532 struct rtl_priv *rtlpriv = rtl_priv(hw);
Daniel Stamerdac67972012-11-29 17:09:26 +0100533 struct dig_t *dig = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500534
535 if (rtlpriv->mac80211.act_scanning)
536 return;
537
538 /* Decide the current status and if modify initial gain or not */
539 if (rtlpriv->mac80211.link_state >= MAC80211_LINKED ||
540 rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
Daniel Stamerdac67972012-11-29 17:09:26 +0100541 dig->cur_sta_cstate = DIG_STA_CONNECT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500542 else
Daniel Stamerdac67972012-11-29 17:09:26 +0100543 dig->cur_sta_cstate = DIG_STA_DISCONNECT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500544
Daniel Stamerdac67972012-11-29 17:09:26 +0100545 dig->rssi_val = rtlpriv->dm.undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500546
547 /* Change dig mode to rssi */
Daniel Stamerdac67972012-11-29 17:09:26 +0100548 if (dig->cur_sta_cstate != DIG_STA_DISCONNECT) {
549 if (dig->dig_twoport_algorithm ==
Chaoming Li9fe255e2011-05-03 09:47:55 -0500550 DIG_TWO_PORT_ALGO_FALSE_ALARM) {
Daniel Stamerdac67972012-11-29 17:09:26 +0100551 dig->dig_twoport_algorithm = DIG_TWO_PORT_ALGO_RSSI;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500552 rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_MODE_SS);
553 }
554 }
555
556 _rtl92s_dm_false_alarm_counter_statistics(hw);
557 _rtl92s_dm_initial_gain_sta_beforeconnect(hw);
558
Daniel Stamerdac67972012-11-29 17:09:26 +0100559 dig->pre_sta_cstate = dig->cur_sta_cstate;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500560}
561
562static void _rtl92s_dm_ctrl_initgain_byrssi(struct ieee80211_hw *hw)
563{
564 struct rtl_priv *rtlpriv = rtl_priv(hw);
565 struct rtl_phy *rtlphy = &(rtlpriv->phy);
Larry Fingere0602752012-04-19 16:32:44 -0500566 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500567
568 /* 2T2R TP issue */
569 if (rtlphy->rf_type == RF_2T2R)
570 return;
571
572 if (!rtlpriv->dm.dm_initialgain_enable)
573 return;
574
Larry Fingere0602752012-04-19 16:32:44 -0500575 if (digtable->dig_enable_flag == false)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500576 return;
577
578 _rtl92s_dm_ctrl_initgain_bytwoport(hw);
579}
580
581static void _rtl92s_dm_dynamic_txpower(struct ieee80211_hw *hw)
582{
583 struct rtl_priv *rtlpriv = rtl_priv(hw);
584 struct rtl_phy *rtlphy = &(rtlpriv->phy);
585 struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
Larry Fingerda17fcf2012-10-25 13:46:31 -0500586 long undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500587 long txpwr_threshold_lv1, txpwr_threshold_lv2;
588
589 /* 2T2R TP issue */
590 if (rtlphy->rf_type == RF_2T2R)
591 return;
592
593 if (!rtlpriv->dm.dynamic_txpower_enable ||
594 rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
595 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
596 return;
597 }
598
599 if ((mac->link_state < MAC80211_LINKED) &&
Larry Fingerda17fcf2012-10-25 13:46:31 -0500600 (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
Chaoming Li9fe255e2011-05-03 09:47:55 -0500601 RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
Joe Perchesf30d7502012-01-04 19:40:41 -0800602 "Not connected to any\n");
Chaoming Li9fe255e2011-05-03 09:47:55 -0500603
604 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
605
606 rtlpriv->dm.last_dtp_lvl = TX_HIGHPWR_LEVEL_NORMAL;
607 return;
608 }
609
610 if (mac->link_state >= MAC80211_LINKED) {
611 if (mac->opmode == NL80211_IFTYPE_ADHOC) {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500612 undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500613 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800614 "AP Client PWDB = 0x%lx\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500615 undec_sm_pwdb);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500616 } else {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500617 undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500618 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800619 "STA Default Port PWDB = 0x%lx\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500620 undec_sm_pwdb);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500621 }
622 } else {
Larry Fingerda17fcf2012-10-25 13:46:31 -0500623 undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500624
625 RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
Joe Perchesf30d7502012-01-04 19:40:41 -0800626 "AP Ext Port PWDB = 0x%lx\n",
Larry Fingerda17fcf2012-10-25 13:46:31 -0500627 undec_sm_pwdb);
Chaoming Li9fe255e2011-05-03 09:47:55 -0500628 }
629
630 txpwr_threshold_lv2 = TX_POWER_NEAR_FIELD_THRESH_LVL2;
631 txpwr_threshold_lv1 = TX_POWER_NEAR_FIELD_THRESH_LVL1;
632
633 if (rtl_get_bbreg(hw, 0xc90, MASKBYTE0) == 1)
634 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500635 else if (undec_sm_pwdb >= txpwr_threshold_lv2)
Chaoming Li9fe255e2011-05-03 09:47:55 -0500636 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL2;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500637 else if ((undec_sm_pwdb < (txpwr_threshold_lv2 - 3)) &&
638 (undec_sm_pwdb >= txpwr_threshold_lv1))
Chaoming Li9fe255e2011-05-03 09:47:55 -0500639 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL1;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500640 else if (undec_sm_pwdb < (txpwr_threshold_lv1 - 3))
Chaoming Li9fe255e2011-05-03 09:47:55 -0500641 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
642
643 if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl))
644 rtl92s_phy_set_txpower(hw, rtlphy->current_channel);
645
646 rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
647}
648
649static void _rtl92s_dm_init_dig(struct ieee80211_hw *hw)
650{
651 struct rtl_priv *rtlpriv = rtl_priv(hw);
Larry Fingere0602752012-04-19 16:32:44 -0500652 struct dig_t *digtable = &rtlpriv->dm_digtable;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500653
654 /* Disable DIG scheme now.*/
Larry Fingere0602752012-04-19 16:32:44 -0500655 digtable->dig_enable_flag = true;
656 digtable->backoff_enable_flag = true;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500657
658 if ((rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER) &&
659 (hal_get_firmwareversion(rtlpriv) >= 0x3c))
Larry Fingere0602752012-04-19 16:32:44 -0500660 digtable->dig_algorithm = DIG_ALGO_BY_TOW_PORT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500661 else
Larry Fingere0602752012-04-19 16:32:44 -0500662 digtable->dig_algorithm =
Chaoming Li9fe255e2011-05-03 09:47:55 -0500663 DIG_ALGO_BEFORE_CONNECT_BY_RSSI_AND_ALARM;
664
Larry Fingere0602752012-04-19 16:32:44 -0500665 digtable->dig_twoport_algorithm = DIG_TWO_PORT_ALGO_RSSI;
666 digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
667 /* off=by real rssi value, on=by digtable->rssi_val for new dig */
668 digtable->dig_dbgmode = DM_DBG_OFF;
669 digtable->dig_slgorithm_switch = 0;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500670
671 /* 2007/10/04 MH Define init gain threshol. */
Larry Fingere0602752012-04-19 16:32:44 -0500672 digtable->dig_state = DM_STA_DIG_MAX;
673 digtable->dig_highpwrstate = DM_STA_DIG_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500674
Larry Fingerda17fcf2012-10-25 13:46:31 -0500675 digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
676 digtable->pre_sta_cstate = DIG_STA_DISCONNECT;
677 digtable->cur_ap_cstate = DIG_AP_DISCONNECT;
678 digtable->pre_ap_cstate = DIG_AP_DISCONNECT;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500679
Larry Fingere0602752012-04-19 16:32:44 -0500680 digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
681 digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500682
Larry Fingere0602752012-04-19 16:32:44 -0500683 digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
684 digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500685
Larry Fingere0602752012-04-19 16:32:44 -0500686 digtable->rssi_highpower_lowthresh = DM_DIG_HIGH_PWR_THRESH_LOW;
687 digtable->rssi_highpower_highthresh = DM_DIG_HIGH_PWR_THRESH_HIGH;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500688
689 /* for dig debug rssi value */
Larry Fingere0602752012-04-19 16:32:44 -0500690 digtable->rssi_val = 50;
Larry Fingered0fb7e2015-01-06 09:58:12 -0600691 digtable->back_val = DM_DIG_BACKOFF_MAX;
Larry Fingere6deaf82013-03-24 22:06:55 -0500692 digtable->rx_gain_max = DM_DIG_MAX;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500693
Larry Fingere6deaf82013-03-24 22:06:55 -0500694 digtable->rx_gain_min = DM_DIG_MIN;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500695
Larry Fingere0602752012-04-19 16:32:44 -0500696 digtable->backoffval_range_max = DM_DIG_BACKOFF_MAX;
697 digtable->backoffval_range_min = DM_DIG_BACKOFF_MIN;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500698}
699
700static void _rtl92s_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
701{
702 struct rtl_priv *rtlpriv = rtl_priv(hw);
703
704 if ((hal_get_firmwareversion(rtlpriv) >= 60) &&
705 (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER))
706 rtlpriv->dm.dynamic_txpower_enable = true;
707 else
708 rtlpriv->dm.dynamic_txpower_enable = false;
709
710 rtlpriv->dm.last_dtp_lvl = TX_HIGHPWR_LEVEL_NORMAL;
711 rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
712}
713
714void rtl92s_dm_init(struct ieee80211_hw *hw)
715{
716 struct rtl_priv *rtlpriv = rtl_priv(hw);
717
718 rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
Larry Fingerda17fcf2012-10-25 13:46:31 -0500719 rtlpriv->dm.undec_sm_pwdb = -1;
Chaoming Li9fe255e2011-05-03 09:47:55 -0500720
721 _rtl92s_dm_init_dynamic_txpower(hw);
722 rtl92s_dm_init_edca_turbo(hw);
723 _rtl92s_dm_init_rate_adaptive_mask(hw);
724 _rtl92s_dm_init_txpowertracking_thermalmeter(hw);
725 _rtl92s_dm_init_dig(hw);
726
727 rtl_write_dword(rtlpriv, WFM5, FW_CCA_CHK_ENABLE);
728}
729
730void rtl92s_dm_watchdog(struct ieee80211_hw *hw)
731{
732 _rtl92s_dm_check_edca_turbo(hw);
733 _rtl92s_dm_check_txpowertracking_thermalmeter(hw);
734 _rtl92s_dm_ctrl_initgain_byrssi(hw);
735 _rtl92s_dm_dynamic_txpower(hw);
736 _rtl92s_dm_refresh_rateadaptive_mask(hw);
737 _rtl92s_dm_switch_baseband_mrc(hw);
738}
739