dm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * wlanfae <wlanfae@realtek.com>
  23. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  24. * Hsinchu 300, Taiwan.
  25. *
  26. * Larry Finger <Larry.Finger@lwfinger.net>
  27. *
  28. *****************************************************************************/
  29. #include "../wifi.h"
  30. #include "../base.h"
  31. #include "../core.h"
  32. #include "reg.h"
  33. #include "def.h"
  34. #include "phy.h"
  35. #include "dm.h"
  36. #include "fw.h"
  37. static const u32 edca_setting_dl[PEER_MAX] = {
  38. 0xa44f, /* 0 UNKNOWN */
  39. 0x5ea44f, /* 1 REALTEK_90 */
  40. 0x5ea44f, /* 2 REALTEK_92SE */
  41. 0xa630, /* 3 BROAD */
  42. 0xa44f, /* 4 RAL */
  43. 0xa630, /* 5 ATH */
  44. 0xa630, /* 6 CISCO */
  45. 0xa42b, /* 7 MARV */
  46. };
  47. static const u32 edca_setting_dl_gmode[PEER_MAX] = {
  48. 0x4322, /* 0 UNKNOWN */
  49. 0xa44f, /* 1 REALTEK_90 */
  50. 0x5ea44f, /* 2 REALTEK_92SE */
  51. 0xa42b, /* 3 BROAD */
  52. 0x5e4322, /* 4 RAL */
  53. 0x4322, /* 5 ATH */
  54. 0xa430, /* 6 CISCO */
  55. 0x5ea44f, /* 7 MARV */
  56. };
  57. static 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. static void _rtl92s_dm_check_edca_turbo(struct ieee80211_hw *hw)
  68. {
  69. struct rtl_priv *rtlpriv = rtl_priv(hw);
  70. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  71. static u64 last_txok_cnt;
  72. static u64 last_rxok_cnt;
  73. u64 cur_txok_cnt = 0;
  74. u64 cur_rxok_cnt = 0;
  75. u32 edca_be_ul = edca_setting_ul[mac->vendor];
  76. u32 edca_be_dl = edca_setting_dl[mac->vendor];
  77. u32 edca_gmode = edca_setting_dl_gmode[mac->vendor];
  78. if (mac->link_state != MAC80211_LINKED) {
  79. rtlpriv->dm.current_turbo_edca = false;
  80. goto dm_checkedcaturbo_exit;
  81. }
  82. if ((!rtlpriv->dm.is_any_nonbepkts) &&
  83. (!rtlpriv->dm.disable_framebursting)) {
  84. cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
  85. cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
  86. if (rtlpriv->phy.rf_type == RF_1T2R) {
  87. if (cur_txok_cnt > 4 * cur_rxok_cnt) {
  88. /* Uplink TP is present. */
  89. if (rtlpriv->dm.is_cur_rdlstate ||
  90. !rtlpriv->dm.current_turbo_edca) {
  91. rtl_write_dword(rtlpriv, EDCAPARA_BE,
  92. edca_be_ul);
  93. rtlpriv->dm.is_cur_rdlstate = false;
  94. }
  95. } else {/* Balance TP is present. */
  96. if (!rtlpriv->dm.is_cur_rdlstate ||
  97. !rtlpriv->dm.current_turbo_edca) {
  98. if (mac->mode == WIRELESS_MODE_G ||
  99. mac->mode == WIRELESS_MODE_B)
  100. rtl_write_dword(rtlpriv,
  101. EDCAPARA_BE,
  102. edca_gmode);
  103. else
  104. rtl_write_dword(rtlpriv,
  105. EDCAPARA_BE,
  106. edca_be_dl);
  107. rtlpriv->dm.is_cur_rdlstate = true;
  108. }
  109. }
  110. rtlpriv->dm.current_turbo_edca = true;
  111. } else {
  112. if (cur_rxok_cnt > 4 * cur_txok_cnt) {
  113. if (!rtlpriv->dm.is_cur_rdlstate ||
  114. !rtlpriv->dm.current_turbo_edca) {
  115. if (mac->mode == WIRELESS_MODE_G ||
  116. mac->mode == WIRELESS_MODE_B)
  117. rtl_write_dword(rtlpriv,
  118. EDCAPARA_BE,
  119. edca_gmode);
  120. else
  121. rtl_write_dword(rtlpriv,
  122. EDCAPARA_BE,
  123. edca_be_dl);
  124. rtlpriv->dm.is_cur_rdlstate = true;
  125. }
  126. } else {
  127. if (rtlpriv->dm.is_cur_rdlstate ||
  128. !rtlpriv->dm.current_turbo_edca) {
  129. rtl_write_dword(rtlpriv, EDCAPARA_BE,
  130. edca_be_ul);
  131. rtlpriv->dm.is_cur_rdlstate = false;
  132. }
  133. }
  134. rtlpriv->dm.current_turbo_edca = true;
  135. }
  136. } else {
  137. if (rtlpriv->dm.current_turbo_edca) {
  138. u8 tmp = AC0_BE;
  139. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM,
  140. &tmp);
  141. rtlpriv->dm.current_turbo_edca = false;
  142. }
  143. }
  144. dm_checkedcaturbo_exit:
  145. rtlpriv->dm.is_any_nonbepkts = false;
  146. last_txok_cnt = rtlpriv->stats.txbytesunicast;
  147. last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
  148. }
  149. static void _rtl92s_dm_txpowertracking_callback_thermalmeter(
  150. struct ieee80211_hw *hw)
  151. {
  152. struct rtl_priv *rtlpriv = rtl_priv(hw);
  153. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  154. u8 thermalvalue = 0;
  155. u32 fw_cmd = 0;
  156. rtlpriv->dm.txpower_trackinginit = true;
  157. thermalvalue = (u8)rtl_get_rfreg(hw, RF90_PATH_A, RF_T_METER, 0x1f);
  158. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  159. "Readback Thermal Meter = 0x%x pre thermal meter 0x%x eeprom_thermal meter 0x%x\n",
  160. thermalvalue,
  161. rtlpriv->dm.thermalvalue, rtlefuse->eeprom_thermalmeter);
  162. if (thermalvalue) {
  163. rtlpriv->dm.thermalvalue = thermalvalue;
  164. if (hal_get_firmwareversion(rtlpriv) >= 0x35) {
  165. rtl92s_phy_set_fw_cmd(hw, FW_CMD_TXPWR_TRACK_THERMAL);
  166. } else {
  167. fw_cmd = (FW_TXPWR_TRACK_THERMAL |
  168. (rtlpriv->efuse.thermalmeter[0] << 8) |
  169. (thermalvalue << 16));
  170. RT_TRACE(rtlpriv, COMP_POWER_TRACKING, DBG_LOUD,
  171. "Write to FW Thermal Val = 0x%x\n", fw_cmd);
  172. rtl_write_dword(rtlpriv, WFM5, fw_cmd);
  173. rtl92s_phy_chk_fwcmd_iodone(hw);
  174. }
  175. }
  176. rtlpriv->dm.txpowercount = 0;
  177. }
  178. static void _rtl92s_dm_check_txpowertracking_thermalmeter(
  179. struct ieee80211_hw *hw)
  180. {
  181. struct rtl_priv *rtlpriv = rtl_priv(hw);
  182. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  183. u8 tx_power_checkcnt = 5;
  184. /* 2T2R TP issue */
  185. if (rtlphy->rf_type == RF_2T2R)
  186. return;
  187. if (!rtlpriv->dm.txpower_tracking)
  188. return;
  189. if (rtlpriv->dm.txpowercount <= tx_power_checkcnt) {
  190. rtlpriv->dm.txpowercount++;
  191. return;
  192. }
  193. if (!rtlpriv->dm.tm_trigger) {
  194. rtl_set_rfreg(hw, RF90_PATH_A, RF_T_METER,
  195. RFREG_OFFSET_MASK, 0x60);
  196. rtlpriv->dm.tm_trigger = 1;
  197. } else {
  198. _rtl92s_dm_txpowertracking_callback_thermalmeter(hw);
  199. rtlpriv->dm.tm_trigger = 0;
  200. }
  201. }
  202. static void _rtl92s_dm_refresh_rateadaptive_mask(struct ieee80211_hw *hw)
  203. {
  204. struct rtl_priv *rtlpriv = rtl_priv(hw);
  205. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  206. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  207. struct rate_adaptive *ra = &(rtlpriv->ra);
  208. struct ieee80211_sta *sta = NULL;
  209. u32 low_rssi_thresh = 0;
  210. u32 middle_rssi_thresh = 0;
  211. u32 high_rssi_thresh = 0;
  212. if (is_hal_stop(rtlhal))
  213. return;
  214. if (!rtlpriv->dm.useramask)
  215. return;
  216. if (hal_get_firmwareversion(rtlpriv) >= 61 &&
  217. !rtlpriv->dm.inform_fw_driverctrldm) {
  218. rtl92s_phy_set_fw_cmd(hw, FW_CMD_CTRL_DM_BY_DRIVER);
  219. rtlpriv->dm.inform_fw_driverctrldm = true;
  220. }
  221. if ((mac->link_state == MAC80211_LINKED) &&
  222. (mac->opmode == NL80211_IFTYPE_STATION)) {
  223. switch (ra->pre_ratr_state) {
  224. case DM_RATR_STA_HIGH:
  225. high_rssi_thresh = 40;
  226. middle_rssi_thresh = 30;
  227. low_rssi_thresh = 20;
  228. break;
  229. case DM_RATR_STA_MIDDLE:
  230. high_rssi_thresh = 44;
  231. middle_rssi_thresh = 30;
  232. low_rssi_thresh = 20;
  233. break;
  234. case DM_RATR_STA_LOW:
  235. high_rssi_thresh = 44;
  236. middle_rssi_thresh = 34;
  237. low_rssi_thresh = 20;
  238. break;
  239. case DM_RATR_STA_ULTRALOW:
  240. high_rssi_thresh = 44;
  241. middle_rssi_thresh = 34;
  242. low_rssi_thresh = 24;
  243. break;
  244. default:
  245. high_rssi_thresh = 44;
  246. middle_rssi_thresh = 34;
  247. low_rssi_thresh = 24;
  248. break;
  249. }
  250. if (rtlpriv->dm.undec_sm_pwdb > (long)high_rssi_thresh) {
  251. ra->ratr_state = DM_RATR_STA_HIGH;
  252. } else if (rtlpriv->dm.undec_sm_pwdb >
  253. (long)middle_rssi_thresh) {
  254. ra->ratr_state = DM_RATR_STA_LOW;
  255. } else if (rtlpriv->dm.undec_sm_pwdb >
  256. (long)low_rssi_thresh) {
  257. ra->ratr_state = DM_RATR_STA_LOW;
  258. } else {
  259. ra->ratr_state = DM_RATR_STA_ULTRALOW;
  260. }
  261. if (ra->pre_ratr_state != ra->ratr_state) {
  262. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  263. "RSSI = %ld RSSI_LEVEL = %d PreState = %d, CurState = %d\n",
  264. rtlpriv->dm.undec_sm_pwdb, ra->ratr_state,
  265. ra->pre_ratr_state, ra->ratr_state);
  266. rcu_read_lock();
  267. sta = rtl_find_sta(hw, mac->bssid);
  268. if (sta)
  269. rtlpriv->cfg->ops->update_rate_tbl(hw, sta,
  270. ra->ratr_state);
  271. rcu_read_unlock();
  272. ra->pre_ratr_state = ra->ratr_state;
  273. }
  274. }
  275. }
  276. static void _rtl92s_dm_switch_baseband_mrc(struct ieee80211_hw *hw)
  277. {
  278. struct rtl_priv *rtlpriv = rtl_priv(hw);
  279. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  280. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  281. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  282. bool current_mrc;
  283. bool enable_mrc = true;
  284. long tmpentry_maxpwdb = 0;
  285. u8 rssi_a = 0;
  286. u8 rssi_b = 0;
  287. if (is_hal_stop(rtlhal))
  288. return;
  289. if ((rtlphy->rf_type == RF_1T1R) || (rtlphy->rf_type == RF_2T2R))
  290. return;
  291. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_MRC, (u8 *)(&current_mrc));
  292. if (mac->link_state >= MAC80211_LINKED) {
  293. if (rtlpriv->dm.undec_sm_pwdb > tmpentry_maxpwdb) {
  294. rssi_a = rtlpriv->stats.rx_rssi_percentage[RF90_PATH_A];
  295. rssi_b = rtlpriv->stats.rx_rssi_percentage[RF90_PATH_B];
  296. }
  297. }
  298. /* MRC settings would NOT affect TP on Wireless B mode. */
  299. if (mac->mode != WIRELESS_MODE_B) {
  300. if ((rssi_a == 0) && (rssi_b == 0)) {
  301. enable_mrc = true;
  302. } else if (rssi_b > 30) {
  303. /* Turn on B-Path */
  304. enable_mrc = true;
  305. } else if (rssi_b < 5) {
  306. /* Turn off B-path */
  307. enable_mrc = false;
  308. /* Take care of RSSI differentiation. */
  309. } else if (rssi_a > 15 && (rssi_a >= rssi_b)) {
  310. if ((rssi_a - rssi_b) > 15)
  311. /* Turn off B-path */
  312. enable_mrc = false;
  313. else if ((rssi_a - rssi_b) < 10)
  314. /* Turn on B-Path */
  315. enable_mrc = true;
  316. else
  317. enable_mrc = current_mrc;
  318. } else {
  319. /* Turn on B-Path */
  320. enable_mrc = true;
  321. }
  322. }
  323. /* Update MRC settings if needed. */
  324. if (enable_mrc != current_mrc)
  325. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_MRC,
  326. (u8 *)&enable_mrc);
  327. }
  328. void rtl92s_dm_init_edca_turbo(struct ieee80211_hw *hw)
  329. {
  330. struct rtl_priv *rtlpriv = rtl_priv(hw);
  331. rtlpriv->dm.current_turbo_edca = false;
  332. rtlpriv->dm.is_any_nonbepkts = false;
  333. rtlpriv->dm.is_cur_rdlstate = false;
  334. }
  335. static void _rtl92s_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
  336. {
  337. struct rtl_priv *rtlpriv = rtl_priv(hw);
  338. struct rate_adaptive *ra = &(rtlpriv->ra);
  339. ra->ratr_state = DM_RATR_STA_MAX;
  340. ra->pre_ratr_state = DM_RATR_STA_MAX;
  341. if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER &&
  342. hal_get_firmwareversion(rtlpriv) >= 60)
  343. rtlpriv->dm.useramask = true;
  344. else
  345. rtlpriv->dm.useramask = false;
  346. rtlpriv->dm.useramask = false;
  347. rtlpriv->dm.inform_fw_driverctrldm = false;
  348. }
  349. static void _rtl92s_dm_init_txpowertracking_thermalmeter(
  350. struct ieee80211_hw *hw)
  351. {
  352. struct rtl_priv *rtlpriv = rtl_priv(hw);
  353. rtlpriv->dm.txpower_tracking = true;
  354. rtlpriv->dm.txpowercount = 0;
  355. rtlpriv->dm.txpower_trackinginit = false;
  356. }
  357. static void _rtl92s_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
  358. {
  359. struct rtl_priv *rtlpriv = rtl_priv(hw);
  360. struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
  361. u32 ret_value;
  362. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER1, MASKDWORD);
  363. falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
  364. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER2, MASKDWORD);
  365. falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
  366. falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
  367. ret_value = rtl_get_bbreg(hw, ROFDM_PHYCOUNTER3, MASKDWORD);
  368. falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
  369. falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
  370. falsealm_cnt->cnt_rate_illegal + falsealm_cnt->cnt_crc8_fail +
  371. falsealm_cnt->cnt_mcs_fail;
  372. /* read CCK false alarm */
  373. ret_value = rtl_get_bbreg(hw, 0xc64, MASKDWORD);
  374. falsealm_cnt->cnt_cck_fail = (ret_value & 0xffff);
  375. falsealm_cnt->cnt_all = falsealm_cnt->cnt_ofdm_fail +
  376. falsealm_cnt->cnt_cck_fail;
  377. }
  378. static void rtl92s_backoff_enable_flag(struct ieee80211_hw *hw)
  379. {
  380. struct rtl_priv *rtlpriv = rtl_priv(hw);
  381. struct dig_t *digtable = &rtlpriv->dm_digtable;
  382. struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
  383. if (falsealm_cnt->cnt_all > digtable->fa_highthresh) {
  384. if ((digtable->back_val - 6) <
  385. digtable->backoffval_range_min)
  386. digtable->back_val = digtable->backoffval_range_min;
  387. else
  388. digtable->back_val -= 6;
  389. } else if (falsealm_cnt->cnt_all < digtable->fa_lowthresh) {
  390. if ((digtable->back_val + 6) >
  391. digtable->backoffval_range_max)
  392. digtable->back_val =
  393. digtable->backoffval_range_max;
  394. else
  395. digtable->back_val += 6;
  396. }
  397. }
  398. static void _rtl92s_dm_initial_gain_sta_beforeconnect(struct ieee80211_hw *hw)
  399. {
  400. struct rtl_priv *rtlpriv = rtl_priv(hw);
  401. struct dig_t *digtable = &rtlpriv->dm_digtable;
  402. struct false_alarm_statistics *falsealm_cnt = &(rtlpriv->falsealm_cnt);
  403. static u8 initialized, force_write;
  404. u8 initial_gain = 0;
  405. if ((digtable->pre_sta_cstate == digtable->cur_sta_cstate) ||
  406. (digtable->cur_sta_cstate == DIG_STA_BEFORE_CONNECT)) {
  407. if (digtable->cur_sta_cstate == DIG_STA_BEFORE_CONNECT) {
  408. if (rtlpriv->psc.rfpwr_state != ERFON)
  409. return;
  410. if (digtable->backoff_enable_flag)
  411. rtl92s_backoff_enable_flag(hw);
  412. else
  413. digtable->back_val = DM_DIG_BACKOFF_MAX;
  414. if ((digtable->rssi_val + 10 - digtable->back_val) >
  415. digtable->rx_gain_max)
  416. digtable->cur_igvalue =
  417. digtable->rx_gain_max;
  418. else if ((digtable->rssi_val + 10 - digtable->back_val)
  419. < digtable->rx_gain_min)
  420. digtable->cur_igvalue =
  421. digtable->rx_gain_min;
  422. else
  423. digtable->cur_igvalue = digtable->rssi_val + 10
  424. - digtable->back_val;
  425. if (falsealm_cnt->cnt_all > 10000)
  426. digtable->cur_igvalue =
  427. (digtable->cur_igvalue > 0x33) ?
  428. digtable->cur_igvalue : 0x33;
  429. if (falsealm_cnt->cnt_all > 16000)
  430. digtable->cur_igvalue =
  431. digtable->rx_gain_max;
  432. /* connected -> connected or disconnected -> disconnected */
  433. } else {
  434. /* Firmware control DIG, do nothing in driver dm */
  435. return;
  436. }
  437. /* disconnected -> connected or connected ->
  438. * disconnected or beforeconnect->(dis)connected */
  439. } else {
  440. /* Enable FW DIG */
  441. digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  442. rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_ENABLE);
  443. digtable->back_val = DM_DIG_BACKOFF_MAX;
  444. digtable->cur_igvalue = rtlpriv->phy.default_initialgain[0];
  445. digtable->pre_igvalue = 0;
  446. return;
  447. }
  448. /* Forced writing to prevent from fw-dig overwriting. */
  449. if (digtable->pre_igvalue != rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1,
  450. MASKBYTE0))
  451. force_write = 1;
  452. if ((digtable->pre_igvalue != digtable->cur_igvalue) ||
  453. !initialized || force_write) {
  454. /* Disable FW DIG */
  455. rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_DISABLE);
  456. initial_gain = (u8)digtable->cur_igvalue;
  457. /* Set initial gain. */
  458. rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0, initial_gain);
  459. rtl_set_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0, initial_gain);
  460. digtable->pre_igvalue = digtable->cur_igvalue;
  461. initialized = 1;
  462. force_write = 0;
  463. }
  464. }
  465. static void _rtl92s_dm_ctrl_initgain_bytwoport(struct ieee80211_hw *hw)
  466. {
  467. struct rtl_priv *rtlpriv = rtl_priv(hw);
  468. struct dig_t *dig = &rtlpriv->dm_digtable;
  469. if (rtlpriv->mac80211.act_scanning)
  470. return;
  471. /* Decide the current status and if modify initial gain or not */
  472. if (rtlpriv->mac80211.link_state >= MAC80211_LINKED ||
  473. rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC)
  474. dig->cur_sta_cstate = DIG_STA_CONNECT;
  475. else
  476. dig->cur_sta_cstate = DIG_STA_DISCONNECT;
  477. dig->rssi_val = rtlpriv->dm.undec_sm_pwdb;
  478. /* Change dig mode to rssi */
  479. if (dig->cur_sta_cstate != DIG_STA_DISCONNECT) {
  480. if (dig->dig_twoport_algorithm ==
  481. DIG_TWO_PORT_ALGO_FALSE_ALARM) {
  482. dig->dig_twoport_algorithm = DIG_TWO_PORT_ALGO_RSSI;
  483. rtl92s_phy_set_fw_cmd(hw, FW_CMD_DIG_MODE_SS);
  484. }
  485. }
  486. _rtl92s_dm_false_alarm_counter_statistics(hw);
  487. _rtl92s_dm_initial_gain_sta_beforeconnect(hw);
  488. dig->pre_sta_cstate = dig->cur_sta_cstate;
  489. }
  490. static void _rtl92s_dm_ctrl_initgain_byrssi(struct ieee80211_hw *hw)
  491. {
  492. struct rtl_priv *rtlpriv = rtl_priv(hw);
  493. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  494. struct dig_t *digtable = &rtlpriv->dm_digtable;
  495. /* 2T2R TP issue */
  496. if (rtlphy->rf_type == RF_2T2R)
  497. return;
  498. if (!rtlpriv->dm.dm_initialgain_enable)
  499. return;
  500. if (digtable->dig_enable_flag == false)
  501. return;
  502. _rtl92s_dm_ctrl_initgain_bytwoport(hw);
  503. }
  504. static void _rtl92s_dm_dynamic_txpower(struct ieee80211_hw *hw)
  505. {
  506. struct rtl_priv *rtlpriv = rtl_priv(hw);
  507. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  508. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  509. long undec_sm_pwdb;
  510. long txpwr_threshold_lv1, txpwr_threshold_lv2;
  511. /* 2T2R TP issue */
  512. if (rtlphy->rf_type == RF_2T2R)
  513. return;
  514. if (!rtlpriv->dm.dynamic_txpower_enable ||
  515. rtlpriv->dm.dm_flag & HAL_DM_HIPWR_DISABLE) {
  516. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  517. return;
  518. }
  519. if ((mac->link_state < MAC80211_LINKED) &&
  520. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  521. RT_TRACE(rtlpriv, COMP_POWER, DBG_TRACE,
  522. "Not connected to any\n");
  523. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  524. rtlpriv->dm.last_dtp_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  525. return;
  526. }
  527. if (mac->link_state >= MAC80211_LINKED) {
  528. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  529. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  530. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  531. "AP Client PWDB = 0x%lx\n",
  532. undec_sm_pwdb);
  533. } else {
  534. undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
  535. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  536. "STA Default Port PWDB = 0x%lx\n",
  537. undec_sm_pwdb);
  538. }
  539. } else {
  540. undec_sm_pwdb = rtlpriv->dm.entry_min_undec_sm_pwdb;
  541. RT_TRACE(rtlpriv, COMP_POWER, DBG_LOUD,
  542. "AP Ext Port PWDB = 0x%lx\n",
  543. undec_sm_pwdb);
  544. }
  545. txpwr_threshold_lv2 = TX_POWER_NEAR_FIELD_THRESH_LVL2;
  546. txpwr_threshold_lv1 = TX_POWER_NEAR_FIELD_THRESH_LVL1;
  547. if (rtl_get_bbreg(hw, 0xc90, MASKBYTE0) == 1)
  548. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  549. else if (undec_sm_pwdb >= txpwr_threshold_lv2)
  550. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL2;
  551. else if ((undec_sm_pwdb < (txpwr_threshold_lv2 - 3)) &&
  552. (undec_sm_pwdb >= txpwr_threshold_lv1))
  553. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL1;
  554. else if (undec_sm_pwdb < (txpwr_threshold_lv1 - 3))
  555. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  556. if ((rtlpriv->dm.dynamic_txhighpower_lvl != rtlpriv->dm.last_dtp_lvl))
  557. rtl92s_phy_set_txpower(hw, rtlphy->current_channel);
  558. rtlpriv->dm.last_dtp_lvl = rtlpriv->dm.dynamic_txhighpower_lvl;
  559. }
  560. static void _rtl92s_dm_init_dig(struct ieee80211_hw *hw)
  561. {
  562. struct rtl_priv *rtlpriv = rtl_priv(hw);
  563. struct dig_t *digtable = &rtlpriv->dm_digtable;
  564. /* Disable DIG scheme now.*/
  565. digtable->dig_enable_flag = true;
  566. digtable->backoff_enable_flag = true;
  567. if ((rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER) &&
  568. (hal_get_firmwareversion(rtlpriv) >= 0x3c))
  569. digtable->dig_algorithm = DIG_ALGO_BY_TOW_PORT;
  570. else
  571. digtable->dig_algorithm =
  572. DIG_ALGO_BEFORE_CONNECT_BY_RSSI_AND_ALARM;
  573. digtable->dig_twoport_algorithm = DIG_TWO_PORT_ALGO_RSSI;
  574. digtable->dig_ext_port_stage = DIG_EXT_PORT_STAGE_MAX;
  575. /* off=by real rssi value, on=by digtable->rssi_val for new dig */
  576. digtable->dig_dbgmode = DM_DBG_OFF;
  577. digtable->dig_slgorithm_switch = 0;
  578. /* 2007/10/04 MH Define init gain threshol. */
  579. digtable->dig_state = DM_STA_DIG_MAX;
  580. digtable->dig_highpwrstate = DM_STA_DIG_MAX;
  581. digtable->cur_sta_cstate = DIG_STA_DISCONNECT;
  582. digtable->pre_sta_cstate = DIG_STA_DISCONNECT;
  583. digtable->cur_ap_cstate = DIG_AP_DISCONNECT;
  584. digtable->pre_ap_cstate = DIG_AP_DISCONNECT;
  585. digtable->rssi_lowthresh = DM_DIG_THRESH_LOW;
  586. digtable->rssi_highthresh = DM_DIG_THRESH_HIGH;
  587. digtable->fa_lowthresh = DM_FALSEALARM_THRESH_LOW;
  588. digtable->fa_highthresh = DM_FALSEALARM_THRESH_HIGH;
  589. digtable->rssi_highpower_lowthresh = DM_DIG_HIGH_PWR_THRESH_LOW;
  590. digtable->rssi_highpower_highthresh = DM_DIG_HIGH_PWR_THRESH_HIGH;
  591. /* for dig debug rssi value */
  592. digtable->rssi_val = 50;
  593. digtable->back_val = DM_DIG_BACKOFF_MAX;
  594. digtable->rx_gain_max = DM_DIG_MAX;
  595. digtable->rx_gain_min = DM_DIG_MIN;
  596. digtable->backoffval_range_max = DM_DIG_BACKOFF_MAX;
  597. digtable->backoffval_range_min = DM_DIG_BACKOFF_MIN;
  598. }
  599. static void _rtl92s_dm_init_dynamic_txpower(struct ieee80211_hw *hw)
  600. {
  601. struct rtl_priv *rtlpriv = rtl_priv(hw);
  602. if ((hal_get_firmwareversion(rtlpriv) >= 60) &&
  603. (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER))
  604. rtlpriv->dm.dynamic_txpower_enable = true;
  605. else
  606. rtlpriv->dm.dynamic_txpower_enable = false;
  607. rtlpriv->dm.last_dtp_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  608. rtlpriv->dm.dynamic_txhighpower_lvl = TX_HIGHPWR_LEVEL_NORMAL;
  609. }
  610. void rtl92s_dm_init(struct ieee80211_hw *hw)
  611. {
  612. struct rtl_priv *rtlpriv = rtl_priv(hw);
  613. rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
  614. rtlpriv->dm.undec_sm_pwdb = -1;
  615. _rtl92s_dm_init_dynamic_txpower(hw);
  616. rtl92s_dm_init_edca_turbo(hw);
  617. _rtl92s_dm_init_rate_adaptive_mask(hw);
  618. _rtl92s_dm_init_txpowertracking_thermalmeter(hw);
  619. _rtl92s_dm_init_dig(hw);
  620. rtl_write_dword(rtlpriv, WFM5, FW_CCA_CHK_ENABLE);
  621. }
  622. void rtl92s_dm_watchdog(struct ieee80211_hw *hw)
  623. {
  624. _rtl92s_dm_check_edca_turbo(hw);
  625. _rtl92s_dm_check_txpowertracking_thermalmeter(hw);
  626. _rtl92s_dm_ctrl_initgain_byrssi(hw);
  627. _rtl92s_dm_dynamic_txpower(hw);
  628. _rtl92s_dm_refresh_rateadaptive_mask(hw);
  629. _rtl92s_dm_switch_baseband_mrc(hw);
  630. }