dm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2014 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. * 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. #include "../wifi.h"
  26. #include "../base.h"
  27. #include "../pci.h"
  28. #include "../core.h"
  29. #include "reg.h"
  30. #include "def.h"
  31. #include "phy.h"
  32. #include "dm.h"
  33. #include "fw.h"
  34. #include "trx.h"
  35. static const u32 ofdmswing_table[OFDM_TABLE_SIZE] = {
  36. 0x7f8001fe, /* 0, +6.0dB */
  37. 0x788001e2, /* 1, +5.5dB */
  38. 0x71c001c7, /* 2, +5.0dB */
  39. 0x6b8001ae, /* 3, +4.5dB */
  40. 0x65400195, /* 4, +4.0dB */
  41. 0x5fc0017f, /* 5, +3.5dB */
  42. 0x5a400169, /* 6, +3.0dB */
  43. 0x55400155, /* 7, +2.5dB */
  44. 0x50800142, /* 8, +2.0dB */
  45. 0x4c000130, /* 9, +1.5dB */
  46. 0x47c0011f, /* 10, +1.0dB */
  47. 0x43c0010f, /* 11, +0.5dB */
  48. 0x40000100, /* 12, +0dB */
  49. 0x3c8000f2, /* 13, -0.5dB */
  50. 0x390000e4, /* 14, -1.0dB */
  51. 0x35c000d7, /* 15, -1.5dB */
  52. 0x32c000cb, /* 16, -2.0dB */
  53. 0x300000c0, /* 17, -2.5dB */
  54. 0x2d4000b5, /* 18, -3.0dB */
  55. 0x2ac000ab, /* 19, -3.5dB */
  56. 0x288000a2, /* 20, -4.0dB */
  57. 0x26000098, /* 21, -4.5dB */
  58. 0x24000090, /* 22, -5.0dB */
  59. 0x22000088, /* 23, -5.5dB */
  60. 0x20000080, /* 24, -6.0dB */
  61. 0x1e400079, /* 25, -6.5dB */
  62. 0x1c800072, /* 26, -7.0dB */
  63. 0x1b00006c, /* 27. -7.5dB */
  64. 0x19800066, /* 28, -8.0dB */
  65. 0x18000060, /* 29, -8.5dB */
  66. 0x16c0005b, /* 30, -9.0dB */
  67. 0x15800056, /* 31, -9.5dB */
  68. 0x14400051, /* 32, -10.0dB */
  69. 0x1300004c, /* 33, -10.5dB */
  70. 0x12000048, /* 34, -11.0dB */
  71. 0x11000044, /* 35, -11.5dB */
  72. 0x10000040, /* 36, -12.0dB */
  73. 0x0f00003c, /* 37, -12.5dB */
  74. 0x0e400039, /* 38, -13.0dB */
  75. 0x0d800036, /* 39, -13.5dB */
  76. 0x0cc00033, /* 40, -14.0dB */
  77. 0x0c000030, /* 41, -14.5dB */
  78. 0x0b40002d, /* 42, -15.0dB */
  79. };
  80. static const u8 cckswing_table_ch1ch13[CCK_TABLE_SIZE][8] = {
  81. {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04}, /* 0, +0dB */
  82. {0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04}, /* 1, -0.5dB */
  83. {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03}, /* 2, -1.0dB */
  84. {0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03}, /* 3, -1.5dB */
  85. {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03}, /* 4, -2.0dB */
  86. {0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03}, /* 5, -2.5dB */
  87. {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03}, /* 6, -3.0dB */
  88. {0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03}, /* 7, -3.5dB */
  89. {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02}, /* 8, -4.0dB */
  90. {0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02}, /* 9, -4.5dB */
  91. {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02}, /* 10, -5.0dB */
  92. {0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02}, /* 11, -5.5dB */
  93. {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02}, /* 12, -6.0dB */
  94. {0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02}, /* 13, -6.5dB */
  95. {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02}, /* 14, -7.0dB */
  96. {0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02}, /* 15, -7.5dB */
  97. {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01}, /* 16, -8.0dB */
  98. {0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02}, /* 17, -8.5dB */
  99. {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01}, /* 18, -9.0dB */
  100. {0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01}, /* 19, -9.5dB */
  101. {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01}, /* 20, -10.0dB */
  102. {0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01}, /* 21, -10.5dB */
  103. {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01}, /* 22, -11.0dB */
  104. {0x0e, 0x0e, 0x0c, 0x0a, 0x08, 0x05, 0x02, 0x01}, /* 23, -11.5dB */
  105. {0x0d, 0x0d, 0x0c, 0x0a, 0x07, 0x05, 0x02, 0x01}, /* 24, -12.0dB */
  106. {0x0d, 0x0c, 0x0b, 0x09, 0x07, 0x04, 0x02, 0x01}, /* 25, -12.5dB */
  107. {0x0c, 0x0c, 0x0a, 0x09, 0x06, 0x04, 0x02, 0x01}, /* 26, -13.0dB */
  108. {0x0b, 0x0b, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x01}, /* 27, -13.5dB */
  109. {0x0b, 0x0a, 0x09, 0x08, 0x06, 0x04, 0x02, 0x01}, /* 28, -14.0dB */
  110. {0x0a, 0x0a, 0x09, 0x07, 0x05, 0x03, 0x02, 0x01}, /* 29, -14.5dB */
  111. {0x0a, 0x09, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01}, /* 30, -15.0dB */
  112. {0x09, 0x09, 0x08, 0x06, 0x05, 0x03, 0x01, 0x01}, /* 31, -15.5dB */
  113. {0x09, 0x08, 0x07, 0x06, 0x04, 0x03, 0x01, 0x01} /* 32, -16.0dB */
  114. };
  115. static const u8 cckswing_table_ch14[CCK_TABLE_SIZE][8] = {
  116. {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00}, /* 0, +0dB */
  117. {0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00}, /* 1, -0.5dB */
  118. {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00}, /* 2, -1.0dB */
  119. {0x2d, 0x2d, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00}, /* 3, -1.5dB */
  120. {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00}, /* 4, -2.0dB */
  121. {0x28, 0x28, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00}, /* 5, -2.5dB */
  122. {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00}, /* 6, -3.0dB */
  123. {0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00}, /* 7, -3.5dB */
  124. {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00}, /* 8, -4.0dB */
  125. {0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00}, /* 9, -4.5dB */
  126. {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00}, /* 10, -5.0dB */
  127. {0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00}, /* 11, -5.5dB */
  128. {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00}, /* 12, -6.0dB */
  129. {0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00}, /* 13, -6.5dB */
  130. {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00}, /* 14, -7.0dB */
  131. {0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00}, /* 15, -7.5dB */
  132. {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00}, /* 16, -8.0dB */
  133. {0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00}, /* 17, -8.5dB */
  134. {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00}, /* 18, -9.0dB */
  135. {0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00}, /* 19, -9.5dB */
  136. {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00}, /* 20, -10.0dB */
  137. {0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00}, /* 21, -10.5dB */
  138. {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00}, /* 22, -11.0dB */
  139. {0x0e, 0x0e, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00}, /* 23, -11.5dB */
  140. {0x0d, 0x0d, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00}, /* 24, -12.0dB */
  141. {0x0d, 0x0c, 0x0b, 0x06, 0x00, 0x00, 0x00, 0x00}, /* 25, -12.5dB */
  142. {0x0c, 0x0c, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00}, /* 26, -13.0dB */
  143. {0x0b, 0x0b, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00}, /* 27, -13.5dB */
  144. {0x0b, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 28, -14.0dB */
  145. {0x0a, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 29, -14.5dB */
  146. {0x0a, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 30, -15.0dB */
  147. {0x09, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00}, /* 31, -15.5dB */
  148. {0x09, 0x08, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00} /* 32, -16.0dB */
  149. };
  150. static void rtl92ee_dm_false_alarm_counter_statistics(struct ieee80211_hw *hw)
  151. {
  152. u32 ret_value;
  153. struct rtl_priv *rtlpriv = rtl_priv(hw);
  154. struct false_alarm_statistics *falsealm_cnt = &rtlpriv->falsealm_cnt;
  155. rtl_set_bbreg(hw, DM_REG_OFDM_FA_HOLDC_11N, BIT(31), 1);
  156. rtl_set_bbreg(hw, DM_REG_OFDM_FA_RSTD_11N, BIT(31), 1);
  157. ret_value = rtl_get_bbreg(hw, DM_REG_OFDM_FA_TYPE1_11N, MASKDWORD);
  158. falsealm_cnt->cnt_fast_fsync_fail = (ret_value & 0xffff);
  159. falsealm_cnt->cnt_sb_search_fail = ((ret_value & 0xffff0000) >> 16);
  160. ret_value = rtl_get_bbreg(hw, DM_REG_OFDM_FA_TYPE2_11N, MASKDWORD);
  161. falsealm_cnt->cnt_ofdm_cca = (ret_value & 0xffff);
  162. falsealm_cnt->cnt_parity_fail = ((ret_value & 0xffff0000) >> 16);
  163. ret_value = rtl_get_bbreg(hw, DM_REG_OFDM_FA_TYPE3_11N, MASKDWORD);
  164. falsealm_cnt->cnt_rate_illegal = (ret_value & 0xffff);
  165. falsealm_cnt->cnt_crc8_fail = ((ret_value & 0xffff0000) >> 16);
  166. ret_value = rtl_get_bbreg(hw, DM_REG_OFDM_FA_TYPE4_11N, MASKDWORD);
  167. falsealm_cnt->cnt_mcs_fail = (ret_value & 0xffff);
  168. falsealm_cnt->cnt_ofdm_fail = falsealm_cnt->cnt_parity_fail +
  169. falsealm_cnt->cnt_rate_illegal +
  170. falsealm_cnt->cnt_crc8_fail +
  171. falsealm_cnt->cnt_mcs_fail +
  172. falsealm_cnt->cnt_fast_fsync_fail +
  173. falsealm_cnt->cnt_sb_search_fail;
  174. ret_value = rtl_get_bbreg(hw, DM_REG_SC_CNT_11N, MASKDWORD);
  175. falsealm_cnt->cnt_bw_lsc = (ret_value & 0xffff);
  176. falsealm_cnt->cnt_bw_usc = ((ret_value & 0xffff0000) >> 16);
  177. rtl_set_bbreg(hw, DM_REG_CCK_FA_RST_11N, BIT(12), 1);
  178. rtl_set_bbreg(hw, DM_REG_CCK_FA_RST_11N, BIT(14), 1);
  179. ret_value = rtl_get_bbreg(hw, DM_REG_CCK_FA_LSB_11N, MASKBYTE0);
  180. falsealm_cnt->cnt_cck_fail = ret_value;
  181. ret_value = rtl_get_bbreg(hw, DM_REG_CCK_FA_MSB_11N, MASKBYTE3);
  182. falsealm_cnt->cnt_cck_fail += (ret_value & 0xff) << 8;
  183. ret_value = rtl_get_bbreg(hw, DM_REG_CCK_CCA_CNT_11N, MASKDWORD);
  184. falsealm_cnt->cnt_cck_cca = ((ret_value & 0xff) << 8) |
  185. ((ret_value & 0xFF00) >> 8);
  186. falsealm_cnt->cnt_all = falsealm_cnt->cnt_fast_fsync_fail +
  187. falsealm_cnt->cnt_sb_search_fail +
  188. falsealm_cnt->cnt_parity_fail +
  189. falsealm_cnt->cnt_rate_illegal +
  190. falsealm_cnt->cnt_crc8_fail +
  191. falsealm_cnt->cnt_mcs_fail +
  192. falsealm_cnt->cnt_cck_fail;
  193. falsealm_cnt->cnt_cca_all = falsealm_cnt->cnt_ofdm_cca +
  194. falsealm_cnt->cnt_cck_cca;
  195. /*reset false alarm counter registers*/
  196. rtl_set_bbreg(hw, DM_REG_OFDM_FA_RSTC_11N, BIT(31), 1);
  197. rtl_set_bbreg(hw, DM_REG_OFDM_FA_RSTC_11N, BIT(31), 0);
  198. rtl_set_bbreg(hw, DM_REG_OFDM_FA_RSTD_11N, BIT(27), 1);
  199. rtl_set_bbreg(hw, DM_REG_OFDM_FA_RSTD_11N, BIT(27), 0);
  200. /*update ofdm counter*/
  201. rtl_set_bbreg(hw, DM_REG_OFDM_FA_HOLDC_11N, BIT(31), 0);
  202. rtl_set_bbreg(hw, DM_REG_OFDM_FA_RSTD_11N, BIT(31), 0);
  203. /*reset CCK CCA counter*/
  204. rtl_set_bbreg(hw, DM_REG_CCK_FA_RST_11N, BIT(13) | BIT(12), 0);
  205. rtl_set_bbreg(hw, DM_REG_CCK_FA_RST_11N, BIT(13) | BIT(12), 2);
  206. /*reset CCK FA counter*/
  207. rtl_set_bbreg(hw, DM_REG_CCK_FA_RST_11N, BIT(15) | BIT(14), 0);
  208. rtl_set_bbreg(hw, DM_REG_CCK_FA_RST_11N, BIT(15) | BIT(14), 2);
  209. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  210. "cnt_parity_fail = %d, cnt_rate_illegal = %d, cnt_crc8_fail = %d, cnt_mcs_fail = %d\n",
  211. falsealm_cnt->cnt_parity_fail,
  212. falsealm_cnt->cnt_rate_illegal,
  213. falsealm_cnt->cnt_crc8_fail, falsealm_cnt->cnt_mcs_fail);
  214. RT_TRACE(rtlpriv, COMP_DIG, DBG_TRACE,
  215. "cnt_ofdm_fail = %x, cnt_cck_fail = %x, cnt_all = %x\n",
  216. falsealm_cnt->cnt_ofdm_fail,
  217. falsealm_cnt->cnt_cck_fail, falsealm_cnt->cnt_all);
  218. }
  219. static void rtl92ee_dm_cck_packet_detection_thresh(struct ieee80211_hw *hw)
  220. {
  221. struct rtl_priv *rtlpriv = rtl_priv(hw);
  222. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  223. u8 cur_cck_cca_thresh;
  224. if (rtlpriv->mac80211.link_state >= MAC80211_LINKED) {
  225. if (dm_dig->rssi_val_min > 25) {
  226. cur_cck_cca_thresh = 0xcd;
  227. } else if ((dm_dig->rssi_val_min <= 25) &&
  228. (dm_dig->rssi_val_min > 10)) {
  229. cur_cck_cca_thresh = 0x83;
  230. } else {
  231. if (rtlpriv->falsealm_cnt.cnt_cck_fail > 1000)
  232. cur_cck_cca_thresh = 0x83;
  233. else
  234. cur_cck_cca_thresh = 0x40;
  235. }
  236. } else {
  237. if (rtlpriv->falsealm_cnt.cnt_cck_fail > 1000)
  238. cur_cck_cca_thresh = 0x83;
  239. else
  240. cur_cck_cca_thresh = 0x40;
  241. }
  242. rtl92ee_dm_write_cck_cca_thres(hw, cur_cck_cca_thresh);
  243. }
  244. static void rtl92ee_dm_dig(struct ieee80211_hw *hw)
  245. {
  246. struct rtl_priv *rtlpriv = rtl_priv(hw);
  247. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  248. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  249. u8 dig_min_0, dig_maxofmin;
  250. bool bfirstconnect , bfirstdisconnect;
  251. u8 dm_dig_max, dm_dig_min;
  252. u8 current_igi = dm_dig->cur_igvalue;
  253. u8 offset;
  254. /* AP,BT */
  255. if (mac->act_scanning)
  256. return;
  257. dig_min_0 = dm_dig->dig_min_0;
  258. bfirstconnect = (mac->link_state >= MAC80211_LINKED) &&
  259. !dm_dig->media_connect_0;
  260. bfirstdisconnect = (mac->link_state < MAC80211_LINKED) &&
  261. dm_dig->media_connect_0;
  262. dm_dig_max = 0x5a;
  263. dm_dig_min = DM_DIG_MIN;
  264. dig_maxofmin = DM_DIG_MAX_AP;
  265. if (mac->link_state >= MAC80211_LINKED) {
  266. if ((dm_dig->rssi_val_min + 10) > dm_dig_max)
  267. dm_dig->rx_gain_max = dm_dig_max;
  268. else if ((dm_dig->rssi_val_min + 10) < dm_dig_min)
  269. dm_dig->rx_gain_max = dm_dig_min;
  270. else
  271. dm_dig->rx_gain_max = dm_dig->rssi_val_min + 10;
  272. if (rtlpriv->dm.one_entry_only) {
  273. offset = 0;
  274. if (dm_dig->rssi_val_min - offset < dm_dig_min)
  275. dig_min_0 = dm_dig_min;
  276. else if (dm_dig->rssi_val_min - offset >
  277. dig_maxofmin)
  278. dig_min_0 = dig_maxofmin;
  279. else
  280. dig_min_0 = dm_dig->rssi_val_min - offset;
  281. } else {
  282. dig_min_0 = dm_dig_min;
  283. }
  284. } else {
  285. dm_dig->rx_gain_max = dm_dig_max;
  286. dig_min_0 = dm_dig_min;
  287. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD, "no link\n");
  288. }
  289. if (rtlpriv->falsealm_cnt.cnt_all > 10000) {
  290. if (dm_dig->large_fa_hit != 3)
  291. dm_dig->large_fa_hit++;
  292. if (dm_dig->forbidden_igi < current_igi) {
  293. dm_dig->forbidden_igi = current_igi;
  294. dm_dig->large_fa_hit = 1;
  295. }
  296. if (dm_dig->large_fa_hit >= 3) {
  297. if (dm_dig->forbidden_igi + 1 > dm_dig->rx_gain_max)
  298. dm_dig->rx_gain_min =
  299. dm_dig->rx_gain_max;
  300. else
  301. dm_dig->rx_gain_min =
  302. dm_dig->forbidden_igi + 1;
  303. dm_dig->recover_cnt = 3600;
  304. }
  305. } else {
  306. if (dm_dig->recover_cnt != 0) {
  307. dm_dig->recover_cnt--;
  308. } else {
  309. if (dm_dig->large_fa_hit < 3) {
  310. if ((dm_dig->forbidden_igi - 1) <
  311. dig_min_0) {
  312. dm_dig->forbidden_igi = dig_min_0;
  313. dm_dig->rx_gain_min =
  314. dig_min_0;
  315. } else {
  316. dm_dig->forbidden_igi--;
  317. dm_dig->rx_gain_min =
  318. dm_dig->forbidden_igi + 1;
  319. }
  320. } else {
  321. dm_dig->large_fa_hit = 0;
  322. }
  323. }
  324. }
  325. if (rtlpriv->dm.dbginfo.num_qry_beacon_pkt < 5)
  326. dm_dig->rx_gain_min = dm_dig_min;
  327. if (dm_dig->rx_gain_min > dm_dig->rx_gain_max)
  328. dm_dig->rx_gain_min = dm_dig->rx_gain_max;
  329. if (mac->link_state >= MAC80211_LINKED) {
  330. if (bfirstconnect) {
  331. if (dm_dig->rssi_val_min <= dig_maxofmin)
  332. current_igi = dm_dig->rssi_val_min;
  333. else
  334. current_igi = dig_maxofmin;
  335. dm_dig->large_fa_hit = 0;
  336. } else {
  337. if (rtlpriv->falsealm_cnt.cnt_all > DM_DIG_FA_TH2)
  338. current_igi += 4;
  339. else if (rtlpriv->falsealm_cnt.cnt_all > DM_DIG_FA_TH1)
  340. current_igi += 2;
  341. else if (rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH0)
  342. current_igi -= 2;
  343. if (rtlpriv->dm.dbginfo.num_qry_beacon_pkt < 5 &&
  344. rtlpriv->falsealm_cnt.cnt_all < DM_DIG_FA_TH1)
  345. current_igi = dm_dig->rx_gain_min;
  346. }
  347. } else {
  348. if (bfirstdisconnect) {
  349. current_igi = dm_dig->rx_gain_min;
  350. } else {
  351. if (rtlpriv->falsealm_cnt.cnt_all > 10000)
  352. current_igi += 4;
  353. else if (rtlpriv->falsealm_cnt.cnt_all > 8000)
  354. current_igi += 2;
  355. else if (rtlpriv->falsealm_cnt.cnt_all < 500)
  356. current_igi -= 2;
  357. }
  358. }
  359. if (current_igi > dm_dig->rx_gain_max)
  360. current_igi = dm_dig->rx_gain_max;
  361. if (current_igi < dm_dig->rx_gain_min)
  362. current_igi = dm_dig->rx_gain_min;
  363. rtl92ee_dm_write_dig(hw , current_igi);
  364. dm_dig->media_connect_0 = ((mac->link_state >= MAC80211_LINKED) ?
  365. true : false);
  366. dm_dig->dig_min_0 = dig_min_0;
  367. }
  368. void rtl92ee_dm_write_cck_cca_thres(struct ieee80211_hw *hw, u8 cur_thres)
  369. {
  370. struct rtl_priv *rtlpriv = rtl_priv(hw);
  371. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  372. if (dm_dig->cur_cck_cca_thres != cur_thres)
  373. rtl_write_byte(rtlpriv, DM_REG_CCK_CCA_11N, cur_thres);
  374. dm_dig->pre_cck_cca_thres = dm_dig->cur_cck_cca_thres;
  375. dm_dig->cur_cck_cca_thres = cur_thres;
  376. }
  377. void rtl92ee_dm_write_dig(struct ieee80211_hw *hw, u8 current_igi)
  378. {
  379. struct rtl_priv *rtlpriv = rtl_priv(hw);
  380. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  381. if (dm_dig->stop_dig)
  382. return;
  383. if (dm_dig->cur_igvalue != current_igi) {
  384. rtl_set_bbreg(hw, ROFDM0_XAAGCCORE1, 0x7f, current_igi);
  385. if (rtlpriv->phy.rf_type != RF_1T1R)
  386. rtl_set_bbreg(hw, ROFDM0_XBAGCCORE1, 0x7f, current_igi);
  387. }
  388. dm_dig->pre_igvalue = dm_dig->cur_igvalue;
  389. dm_dig->cur_igvalue = current_igi;
  390. }
  391. static void rtl92ee_rssi_dump_to_register(struct ieee80211_hw *hw)
  392. {
  393. struct rtl_priv *rtlpriv = rtl_priv(hw);
  394. rtl_write_byte(rtlpriv, RA_RSSIDUMP,
  395. rtlpriv->stats.rx_rssi_percentage[0]);
  396. rtl_write_byte(rtlpriv, RB_RSSIDUMP,
  397. rtlpriv->stats.rx_rssi_percentage[1]);
  398. /*It seems the following values are not initialized.
  399. *According to Windows code,
  400. *these value will only be valid with JAGUAR chips
  401. */
  402. /* Rx EVM */
  403. rtl_write_byte(rtlpriv, RS1_RXEVMDUMP, rtlpriv->stats.rx_evm_dbm[0]);
  404. rtl_write_byte(rtlpriv, RS2_RXEVMDUMP, rtlpriv->stats.rx_evm_dbm[1]);
  405. /* Rx SNR */
  406. rtl_write_byte(rtlpriv, RA_RXSNRDUMP,
  407. (u8)(rtlpriv->stats.rx_snr_db[0]));
  408. rtl_write_byte(rtlpriv, RB_RXSNRDUMP,
  409. (u8)(rtlpriv->stats.rx_snr_db[1]));
  410. /* Rx Cfo_Short */
  411. rtl_write_word(rtlpriv, RA_CFOSHORTDUMP,
  412. rtlpriv->stats.rx_cfo_short[0]);
  413. rtl_write_word(rtlpriv, RB_CFOSHORTDUMP,
  414. rtlpriv->stats.rx_cfo_short[1]);
  415. /* Rx Cfo_Tail */
  416. rtl_write_word(rtlpriv, RA_CFOLONGDUMP, rtlpriv->stats.rx_cfo_tail[0]);
  417. rtl_write_word(rtlpriv, RB_CFOLONGDUMP, rtlpriv->stats.rx_cfo_tail[1]);
  418. }
  419. static void rtl92ee_dm_find_minimum_rssi(struct ieee80211_hw *hw)
  420. {
  421. struct rtl_priv *rtlpriv = rtl_priv(hw);
  422. struct dig_t *rtl_dm_dig = &rtlpriv->dm_digtable;
  423. struct rtl_mac *mac = rtl_mac(rtlpriv);
  424. /* Determine the minimum RSSI */
  425. if ((mac->link_state < MAC80211_LINKED) &&
  426. (rtlpriv->dm.entry_min_undec_sm_pwdb == 0)) {
  427. rtl_dm_dig->min_undec_pwdb_for_dm = 0;
  428. RT_TRACE(rtlpriv, COMP_BB_POWERSAVING, DBG_LOUD,
  429. "Not connected to any\n");
  430. }
  431. if (mac->link_state >= MAC80211_LINKED) {
  432. if (mac->opmode == NL80211_IFTYPE_AP ||
  433. mac->opmode == NL80211_IFTYPE_ADHOC) {
  434. rtl_dm_dig->min_undec_pwdb_for_dm =
  435. rtlpriv->dm.entry_min_undec_sm_pwdb;
  436. RT_TRACE(rtlpriv, COMP_BB_POWERSAVING, DBG_LOUD,
  437. "AP Client PWDB = 0x%lx\n",
  438. rtlpriv->dm.entry_min_undec_sm_pwdb);
  439. } else {
  440. rtl_dm_dig->min_undec_pwdb_for_dm =
  441. rtlpriv->dm.undec_sm_pwdb;
  442. RT_TRACE(rtlpriv, COMP_BB_POWERSAVING, DBG_LOUD,
  443. "STA Default Port PWDB = 0x%x\n",
  444. rtl_dm_dig->min_undec_pwdb_for_dm);
  445. }
  446. } else {
  447. rtl_dm_dig->min_undec_pwdb_for_dm =
  448. rtlpriv->dm.entry_min_undec_sm_pwdb;
  449. RT_TRACE(rtlpriv, COMP_BB_POWERSAVING, DBG_LOUD,
  450. "AP Ext Port or disconnect PWDB = 0x%x\n",
  451. rtl_dm_dig->min_undec_pwdb_for_dm);
  452. }
  453. RT_TRACE(rtlpriv, COMP_DIG, DBG_LOUD,
  454. "MinUndecoratedPWDBForDM =%d\n",
  455. rtl_dm_dig->min_undec_pwdb_for_dm);
  456. }
  457. static void rtl92ee_dm_check_rssi_monitor(struct ieee80211_hw *hw)
  458. {
  459. struct rtl_priv *rtlpriv = rtl_priv(hw);
  460. struct dig_t *dm_dig = &rtlpriv->dm_digtable;
  461. struct rtl_mac *mac = rtl_mac(rtlpriv);
  462. struct rtl_dm *dm = rtl_dm(rtlpriv);
  463. struct rtl_sta_info *drv_priv;
  464. u8 h2c[4] = { 0 };
  465. long max = 0, min = 0xff;
  466. u8 i = 0;
  467. if (mac->opmode == NL80211_IFTYPE_AP ||
  468. mac->opmode == NL80211_IFTYPE_ADHOC ||
  469. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  470. /* AP & ADHOC & MESH */
  471. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  472. list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
  473. struct rssi_sta *stat = &drv_priv->rssi_stat;
  474. if (stat->undec_sm_pwdb < min)
  475. min = stat->undec_sm_pwdb;
  476. if (stat->undec_sm_pwdb > max)
  477. max = stat->undec_sm_pwdb;
  478. h2c[3] = 0;
  479. h2c[2] = (u8)(dm->undec_sm_pwdb & 0xFF);
  480. h2c[1] = 0x20;
  481. h2c[0] = ++i;
  482. rtl92ee_fill_h2c_cmd(hw, H2C_92E_RSSI_REPORT, 4, h2c);
  483. }
  484. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  485. /* If associated entry is found */
  486. if (max != 0) {
  487. dm->entry_max_undec_sm_pwdb = max;
  488. RTPRINT(rtlpriv, FDM, DM_PWDB,
  489. "EntryMaxPWDB = 0x%lx(%ld)\n", max, max);
  490. } else {
  491. dm->entry_max_undec_sm_pwdb = 0;
  492. }
  493. /* If associated entry is found */
  494. if (min != 0xff) {
  495. dm->entry_min_undec_sm_pwdb = min;
  496. RTPRINT(rtlpriv, FDM, DM_PWDB,
  497. "EntryMinPWDB = 0x%lx(%ld)\n", min, min);
  498. } else {
  499. dm->entry_min_undec_sm_pwdb = 0;
  500. }
  501. }
  502. /* Indicate Rx signal strength to FW. */
  503. if (dm->useramask) {
  504. h2c[3] = 0;
  505. h2c[2] = (u8)(dm->undec_sm_pwdb & 0xFF);
  506. h2c[1] = 0x20;
  507. h2c[0] = 0;
  508. rtl92ee_fill_h2c_cmd(hw, H2C_92E_RSSI_REPORT, 4, h2c);
  509. } else {
  510. rtl_write_byte(rtlpriv, 0x4fe, dm->undec_sm_pwdb);
  511. }
  512. rtl92ee_rssi_dump_to_register(hw);
  513. rtl92ee_dm_find_minimum_rssi(hw);
  514. dm_dig->rssi_val_min = rtlpriv->dm_digtable.min_undec_pwdb_for_dm;
  515. }
  516. static void rtl92ee_dm_init_primary_cca_check(struct ieee80211_hw *hw)
  517. {
  518. struct rtl_priv *rtlpriv = rtl_priv(hw);
  519. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  520. struct dynamic_primary_cca *primarycca = &rtlpriv->primarycca;
  521. rtlhal->rts_en = 0;
  522. primarycca->dup_rts_flag = 0;
  523. primarycca->intf_flag = 0;
  524. primarycca->intf_type = 0;
  525. primarycca->monitor_flag = 0;
  526. primarycca->ch_offset = 0;
  527. primarycca->mf_state = 0;
  528. }
  529. static bool rtl92ee_dm_is_edca_turbo_disable(struct ieee80211_hw *hw)
  530. {
  531. struct rtl_priv *rtlpriv = rtl_priv(hw);
  532. if (rtlpriv->mac80211.mode == WIRELESS_MODE_B)
  533. return true;
  534. return false;
  535. }
  536. void rtl92ee_dm_init_edca_turbo(struct ieee80211_hw *hw)
  537. {
  538. struct rtl_priv *rtlpriv = rtl_priv(hw);
  539. rtlpriv->dm.current_turbo_edca = false;
  540. rtlpriv->dm.is_cur_rdlstate = false;
  541. rtlpriv->dm.is_any_nonbepkts = false;
  542. }
  543. static void rtl92ee_dm_check_edca_turbo(struct ieee80211_hw *hw)
  544. {
  545. struct rtl_priv *rtlpriv = rtl_priv(hw);
  546. static u64 last_txok_cnt;
  547. static u64 last_rxok_cnt;
  548. u64 cur_txok_cnt = 0;
  549. u64 cur_rxok_cnt = 0;
  550. u32 edca_be_ul = 0x5ea42b;
  551. u32 edca_be_dl = 0x5ea42b; /*not sure*/
  552. u32 edca_be = 0x5ea42b;
  553. bool is_cur_rdlstate;
  554. bool b_edca_turbo_on = false;
  555. if (rtlpriv->dm.dbginfo.num_non_be_pkt > 0x100)
  556. rtlpriv->dm.is_any_nonbepkts = true;
  557. rtlpriv->dm.dbginfo.num_non_be_pkt = 0;
  558. cur_txok_cnt = rtlpriv->stats.txbytesunicast - last_txok_cnt;
  559. cur_rxok_cnt = rtlpriv->stats.rxbytesunicast - last_rxok_cnt;
  560. /*b_bias_on_rx = false;*/
  561. b_edca_turbo_on = ((!rtlpriv->dm.is_any_nonbepkts) &&
  562. (!rtlpriv->dm.disable_framebursting)) ?
  563. true : false;
  564. if (rtl92ee_dm_is_edca_turbo_disable(hw))
  565. goto check_exit;
  566. if (b_edca_turbo_on) {
  567. is_cur_rdlstate = (cur_rxok_cnt > cur_txok_cnt * 4) ?
  568. true : false;
  569. edca_be = is_cur_rdlstate ? edca_be_dl : edca_be_ul;
  570. rtl_write_dword(rtlpriv , REG_EDCA_BE_PARAM , edca_be);
  571. rtlpriv->dm.is_cur_rdlstate = is_cur_rdlstate;
  572. rtlpriv->dm.current_turbo_edca = true;
  573. } else {
  574. if (rtlpriv->dm.current_turbo_edca) {
  575. u8 tmp = AC0_BE;
  576. rtlpriv->cfg->ops->set_hw_reg(hw, HW_VAR_AC_PARAM,
  577. (u8 *)(&tmp));
  578. }
  579. rtlpriv->dm.current_turbo_edca = false;
  580. }
  581. check_exit:
  582. rtlpriv->dm.is_any_nonbepkts = false;
  583. last_txok_cnt = rtlpriv->stats.txbytesunicast;
  584. last_rxok_cnt = rtlpriv->stats.rxbytesunicast;
  585. }
  586. static void rtl92ee_dm_dynamic_edcca(struct ieee80211_hw *hw)
  587. {
  588. struct rtl_priv *rtlpriv = rtl_priv(hw);
  589. u8 reg_c50 , reg_c58;
  590. bool fw_current_in_ps_mode = false;
  591. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
  592. (u8 *)(&fw_current_in_ps_mode));
  593. if (fw_current_in_ps_mode)
  594. return;
  595. reg_c50 = rtl_get_bbreg(hw, ROFDM0_XAAGCCORE1, MASKBYTE0);
  596. reg_c58 = rtl_get_bbreg(hw, ROFDM0_XBAGCCORE1, MASKBYTE0);
  597. if (reg_c50 > 0x28 && reg_c58 > 0x28) {
  598. if (!rtlpriv->rtlhal.pre_edcca_enable) {
  599. rtl_write_byte(rtlpriv, ROFDM0_ECCATHRESHOLD, 0x03);
  600. rtl_write_byte(rtlpriv, ROFDM0_ECCATHRESHOLD + 2, 0x00);
  601. rtlpriv->rtlhal.pre_edcca_enable = true;
  602. }
  603. } else if (reg_c50 < 0x25 && reg_c58 < 0x25) {
  604. if (rtlpriv->rtlhal.pre_edcca_enable) {
  605. rtl_write_byte(rtlpriv, ROFDM0_ECCATHRESHOLD, 0x7f);
  606. rtl_write_byte(rtlpriv, ROFDM0_ECCATHRESHOLD + 2, 0x7f);
  607. rtlpriv->rtlhal.pre_edcca_enable = false;
  608. }
  609. }
  610. }
  611. static void rtl92ee_dm_adaptivity(struct ieee80211_hw *hw)
  612. {
  613. rtl92ee_dm_dynamic_edcca(hw);
  614. }
  615. static void rtl92ee_dm_write_dynamic_cca(struct ieee80211_hw *hw,
  616. u8 cur_mf_state)
  617. {
  618. struct dynamic_primary_cca *primarycca = &rtl_priv(hw)->primarycca;
  619. if (primarycca->mf_state != cur_mf_state)
  620. rtl_set_bbreg(hw, DM_REG_L1SBD_PD_CH_11N, BIT(8) | BIT(7),
  621. cur_mf_state);
  622. primarycca->mf_state = cur_mf_state;
  623. }
  624. static void rtl92ee_dm_dynamic_primary_cca_ckeck(struct ieee80211_hw *hw)
  625. {
  626. struct rtl_priv *rtlpriv = rtl_priv(hw);
  627. struct false_alarm_statistics *falsealm_cnt = &rtlpriv->falsealm_cnt;
  628. struct dynamic_primary_cca *primarycca = &rtlpriv->primarycca;
  629. bool is40mhz = false;
  630. u64 ofdm_cca, ofdm_fa, bw_usc_cnt, bw_lsc_cnt;
  631. u8 sec_ch_offset;
  632. u8 cur_mf_state;
  633. static u8 count_down = MONITOR_TIME;
  634. ofdm_cca = falsealm_cnt->cnt_ofdm_cca;
  635. ofdm_fa = falsealm_cnt->cnt_ofdm_fail;
  636. bw_usc_cnt = falsealm_cnt->cnt_bw_usc;
  637. bw_lsc_cnt = falsealm_cnt->cnt_bw_lsc;
  638. is40mhz = rtlpriv->mac80211.bw_40;
  639. sec_ch_offset = rtlpriv->mac80211.cur_40_prime_sc;
  640. /* NIC: 2: sec is below, 1: sec is above */
  641. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP) {
  642. cur_mf_state = MF_USC_LSC;
  643. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  644. return;
  645. }
  646. if (rtlpriv->mac80211.link_state < MAC80211_LINKED)
  647. return;
  648. if (is40mhz)
  649. return;
  650. if (primarycca->pricca_flag == 0) {
  651. /* Primary channel is above
  652. * NOTE: duplicate CTS can remove this condition
  653. */
  654. if (sec_ch_offset == 2) {
  655. if ((ofdm_cca > OFDMCCA_TH) &&
  656. (bw_lsc_cnt > (bw_usc_cnt + BW_IND_BIAS)) &&
  657. (ofdm_fa > (ofdm_cca >> 1))) {
  658. primarycca->intf_type = 1;
  659. primarycca->intf_flag = 1;
  660. cur_mf_state = MF_USC;
  661. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  662. primarycca->pricca_flag = 1;
  663. } else if ((ofdm_cca > OFDMCCA_TH) &&
  664. (bw_lsc_cnt > (bw_usc_cnt + BW_IND_BIAS)) &&
  665. (ofdm_fa < (ofdm_cca >> 1))) {
  666. primarycca->intf_type = 2;
  667. primarycca->intf_flag = 1;
  668. cur_mf_state = MF_USC;
  669. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  670. primarycca->pricca_flag = 1;
  671. primarycca->dup_rts_flag = 1;
  672. rtlpriv->rtlhal.rts_en = 1;
  673. } else {
  674. primarycca->intf_type = 0;
  675. primarycca->intf_flag = 0;
  676. cur_mf_state = MF_USC_LSC;
  677. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  678. rtlpriv->rtlhal.rts_en = 0;
  679. primarycca->dup_rts_flag = 0;
  680. }
  681. } else if (sec_ch_offset == 1) {
  682. if ((ofdm_cca > OFDMCCA_TH) &&
  683. (bw_usc_cnt > (bw_lsc_cnt + BW_IND_BIAS)) &&
  684. (ofdm_fa > (ofdm_cca >> 1))) {
  685. primarycca->intf_type = 1;
  686. primarycca->intf_flag = 1;
  687. cur_mf_state = MF_LSC;
  688. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  689. primarycca->pricca_flag = 1;
  690. } else if ((ofdm_cca > OFDMCCA_TH) &&
  691. (bw_usc_cnt > (bw_lsc_cnt + BW_IND_BIAS)) &&
  692. (ofdm_fa < (ofdm_cca >> 1))) {
  693. primarycca->intf_type = 2;
  694. primarycca->intf_flag = 1;
  695. cur_mf_state = MF_LSC;
  696. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  697. primarycca->pricca_flag = 1;
  698. primarycca->dup_rts_flag = 1;
  699. rtlpriv->rtlhal.rts_en = 1;
  700. } else {
  701. primarycca->intf_type = 0;
  702. primarycca->intf_flag = 0;
  703. cur_mf_state = MF_USC_LSC;
  704. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  705. rtlpriv->rtlhal.rts_en = 0;
  706. primarycca->dup_rts_flag = 0;
  707. }
  708. }
  709. } else {/* PrimaryCCA->PriCCA_flag==1 */
  710. count_down--;
  711. if (count_down == 0) {
  712. count_down = MONITOR_TIME;
  713. primarycca->pricca_flag = 0;
  714. cur_mf_state = MF_USC_LSC;
  715. /* default */
  716. rtl92ee_dm_write_dynamic_cca(hw, cur_mf_state);
  717. rtlpriv->rtlhal.rts_en = 0;
  718. primarycca->dup_rts_flag = 0;
  719. primarycca->intf_type = 0;
  720. primarycca->intf_flag = 0;
  721. }
  722. }
  723. }
  724. static void rtl92ee_dm_dynamic_atc_switch(struct ieee80211_hw *hw)
  725. {
  726. struct rtl_priv *rtlpriv = rtl_priv(hw);
  727. struct rtl_dm *rtldm = rtl_dm(rtl_priv(hw));
  728. u8 crystal_cap;
  729. u32 packet_count;
  730. int cfo_khz_a , cfo_khz_b , cfo_ave = 0, adjust_xtal = 0;
  731. int cfo_ave_diff;
  732. if (rtlpriv->mac80211.link_state < MAC80211_LINKED) {
  733. if (rtldm->atc_status == ATC_STATUS_OFF) {
  734. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(11),
  735. ATC_STATUS_ON);
  736. rtldm->atc_status = ATC_STATUS_ON;
  737. }
  738. /* Disable CFO tracking for BT */
  739. if (rtlpriv->cfg->ops->get_btc_status()) {
  740. if (!rtlpriv->btcoexist.btc_ops->
  741. btc_is_bt_disabled(rtlpriv)) {
  742. RT_TRACE(rtlpriv, COMP_BT_COEXIST, DBG_LOUD,
  743. "odm_DynamicATCSwitch(): Disable CFO tracking for BT!!\n");
  744. return;
  745. }
  746. }
  747. /* Reset Crystal Cap */
  748. if (rtldm->crystal_cap != rtlpriv->efuse.crystalcap) {
  749. rtldm->crystal_cap = rtlpriv->efuse.crystalcap;
  750. crystal_cap = rtldm->crystal_cap & 0x3f;
  751. rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0xFFF000,
  752. (crystal_cap | (crystal_cap << 6)));
  753. }
  754. } else {
  755. cfo_khz_a = (int)(rtldm->cfo_tail[0] * 3125) / 1280;
  756. cfo_khz_b = (int)(rtldm->cfo_tail[1] * 3125) / 1280;
  757. packet_count = rtldm->packet_count;
  758. if (packet_count == rtldm->packet_count_pre)
  759. return;
  760. rtldm->packet_count_pre = packet_count;
  761. if (rtlpriv->phy.rf_type == RF_1T1R)
  762. cfo_ave = cfo_khz_a;
  763. else
  764. cfo_ave = (int)(cfo_khz_a + cfo_khz_b) >> 1;
  765. cfo_ave_diff = (rtldm->cfo_ave_pre >= cfo_ave) ?
  766. (rtldm->cfo_ave_pre - cfo_ave) :
  767. (cfo_ave - rtldm->cfo_ave_pre);
  768. if (cfo_ave_diff > 20 && rtldm->large_cfo_hit == 0) {
  769. rtldm->large_cfo_hit = 1;
  770. return;
  771. }
  772. rtldm->large_cfo_hit = 0;
  773. rtldm->cfo_ave_pre = cfo_ave;
  774. if (cfo_ave >= -rtldm->cfo_threshold &&
  775. cfo_ave <= rtldm->cfo_threshold && rtldm->is_freeze == 0) {
  776. if (rtldm->cfo_threshold == CFO_THRESHOLD_XTAL) {
  777. rtldm->cfo_threshold = CFO_THRESHOLD_XTAL + 10;
  778. rtldm->is_freeze = 1;
  779. } else {
  780. rtldm->cfo_threshold = CFO_THRESHOLD_XTAL;
  781. }
  782. }
  783. if (cfo_ave > rtldm->cfo_threshold && rtldm->crystal_cap < 0x3f)
  784. adjust_xtal = ((cfo_ave - CFO_THRESHOLD_XTAL) >> 2) + 1;
  785. else if ((cfo_ave < -rtlpriv->dm.cfo_threshold) &&
  786. rtlpriv->dm.crystal_cap > 0)
  787. adjust_xtal = ((cfo_ave + CFO_THRESHOLD_XTAL) >> 2) - 1;
  788. if (adjust_xtal != 0) {
  789. rtldm->is_freeze = 0;
  790. rtldm->crystal_cap += adjust_xtal;
  791. if (rtldm->crystal_cap > 0x3f)
  792. rtldm->crystal_cap = 0x3f;
  793. else if (rtldm->crystal_cap < 0)
  794. rtldm->crystal_cap = 0;
  795. crystal_cap = rtldm->crystal_cap & 0x3f;
  796. rtl_set_bbreg(hw, REG_MAC_PHY_CTRL, 0xFFF000,
  797. (crystal_cap | (crystal_cap << 6)));
  798. }
  799. if (cfo_ave < CFO_THRESHOLD_ATC &&
  800. cfo_ave > -CFO_THRESHOLD_ATC) {
  801. if (rtldm->atc_status == ATC_STATUS_ON) {
  802. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(11),
  803. ATC_STATUS_OFF);
  804. rtldm->atc_status = ATC_STATUS_OFF;
  805. }
  806. } else {
  807. if (rtldm->atc_status == ATC_STATUS_OFF) {
  808. rtl_set_bbreg(hw, ROFDM1_CFOTRACKING, BIT(11),
  809. ATC_STATUS_ON);
  810. rtldm->atc_status = ATC_STATUS_ON;
  811. }
  812. }
  813. }
  814. }
  815. static void rtl92ee_dm_init_txpower_tracking(struct ieee80211_hw *hw)
  816. {
  817. struct rtl_priv *rtlpriv = rtl_priv(hw);
  818. struct rtl_dm *dm = rtl_dm(rtlpriv);
  819. u8 path;
  820. dm->txpower_tracking = true;
  821. dm->default_ofdm_index = 30;
  822. dm->default_cck_index = 20;
  823. dm->swing_idx_cck_base = dm->default_cck_index;
  824. dm->cck_index = dm->default_cck_index;
  825. for (path = RF90_PATH_A; path < MAX_RF_PATH; path++) {
  826. dm->swing_idx_ofdm_base[path] = dm->default_ofdm_index;
  827. dm->ofdm_index[path] = dm->default_ofdm_index;
  828. dm->delta_power_index[path] = 0;
  829. dm->delta_power_index_last[path] = 0;
  830. dm->power_index_offset[path] = 0;
  831. }
  832. }
  833. void rtl92ee_dm_init_rate_adaptive_mask(struct ieee80211_hw *hw)
  834. {
  835. struct rtl_priv *rtlpriv = rtl_priv(hw);
  836. struct rate_adaptive *p_ra = &rtlpriv->ra;
  837. p_ra->ratr_state = DM_RATR_STA_INIT;
  838. p_ra->pre_ratr_state = DM_RATR_STA_INIT;
  839. if (rtlpriv->dm.dm_type == DM_TYPE_BYDRIVER)
  840. rtlpriv->dm.useramask = true;
  841. else
  842. rtlpriv->dm.useramask = false;
  843. p_ra->ldpc_thres = 35;
  844. p_ra->use_ldpc = false;
  845. p_ra->high_rssi_thresh_for_ra = 50;
  846. p_ra->low_rssi_thresh_for_ra40m = 20;
  847. }
  848. static bool _rtl92ee_dm_ra_state_check(struct ieee80211_hw *hw,
  849. s32 rssi, u8 *ratr_state)
  850. {
  851. struct rtl_priv *rtlpriv = rtl_priv(hw);
  852. struct rate_adaptive *p_ra = &rtlpriv->ra;
  853. const u8 go_up_gap = 5;
  854. u32 high_rssithresh_for_ra = p_ra->high_rssi_thresh_for_ra;
  855. u32 low_rssithresh_for_ra = p_ra->low_rssi_thresh_for_ra40m;
  856. u8 state;
  857. /* Threshold Adjustment:
  858. * when RSSI state trends to go up one or two levels,
  859. * make sure RSSI is high enough.
  860. * Here GoUpGap is added to solve
  861. * the boundary's level alternation issue.
  862. */
  863. switch (*ratr_state) {
  864. case DM_RATR_STA_INIT:
  865. case DM_RATR_STA_HIGH:
  866. break;
  867. case DM_RATR_STA_MIDDLE:
  868. high_rssithresh_for_ra += go_up_gap;
  869. break;
  870. case DM_RATR_STA_LOW:
  871. high_rssithresh_for_ra += go_up_gap;
  872. low_rssithresh_for_ra += go_up_gap;
  873. break;
  874. default:
  875. RT_TRACE(rtlpriv, COMP_RATR, DBG_DMESG,
  876. "wrong rssi level setting %d !\n", *ratr_state);
  877. break;
  878. }
  879. /* Decide RATRState by RSSI. */
  880. if (rssi > high_rssithresh_for_ra)
  881. state = DM_RATR_STA_HIGH;
  882. else if (rssi > low_rssithresh_for_ra)
  883. state = DM_RATR_STA_MIDDLE;
  884. else
  885. state = DM_RATR_STA_LOW;
  886. if (*ratr_state != state) {
  887. *ratr_state = state;
  888. return true;
  889. }
  890. return false;
  891. }
  892. static void rtl92ee_dm_refresh_rate_adaptive_mask(struct ieee80211_hw *hw)
  893. {
  894. struct rtl_priv *rtlpriv = rtl_priv(hw);
  895. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  896. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  897. struct rate_adaptive *p_ra = &rtlpriv->ra;
  898. struct ieee80211_sta *sta = NULL;
  899. if (is_hal_stop(rtlhal)) {
  900. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  901. "driver is going to unload\n");
  902. return;
  903. }
  904. if (!rtlpriv->dm.useramask) {
  905. RT_TRACE(rtlpriv, COMP_RATE, DBG_LOUD,
  906. "driver does not control rate adaptive mask\n");
  907. return;
  908. }
  909. if (mac->link_state == MAC80211_LINKED &&
  910. mac->opmode == NL80211_IFTYPE_STATION) {
  911. if (rtlpriv->dm.undec_sm_pwdb < p_ra->ldpc_thres) {
  912. p_ra->use_ldpc = true;
  913. p_ra->lower_rts_rate = true;
  914. } else if (rtlpriv->dm.undec_sm_pwdb >
  915. (p_ra->ldpc_thres - 5)) {
  916. p_ra->use_ldpc = false;
  917. p_ra->lower_rts_rate = false;
  918. }
  919. if (_rtl92ee_dm_ra_state_check(hw, rtlpriv->dm.undec_sm_pwdb,
  920. &p_ra->ratr_state)) {
  921. rcu_read_lock();
  922. sta = rtl_find_sta(hw, mac->bssid);
  923. if (sta)
  924. rtlpriv->cfg->ops->update_rate_tbl(hw, sta,
  925. p_ra->ratr_state,
  926. true);
  927. rcu_read_unlock();
  928. p_ra->pre_ratr_state = p_ra->ratr_state;
  929. }
  930. }
  931. }
  932. static void rtl92ee_dm_init_dynamic_atc_switch(struct ieee80211_hw *hw)
  933. {
  934. struct rtl_priv *rtlpriv = rtl_priv(hw);
  935. rtlpriv->dm.crystal_cap = rtlpriv->efuse.crystalcap;
  936. rtlpriv->dm.atc_status = rtl_get_bbreg(hw, ROFDM1_CFOTRACKING, BIT(11));
  937. rtlpriv->dm.cfo_threshold = CFO_THRESHOLD_XTAL;
  938. }
  939. void rtl92ee_dm_init(struct ieee80211_hw *hw)
  940. {
  941. struct rtl_priv *rtlpriv = rtl_priv(hw);
  942. u32 cur_igvalue = rtl_get_bbreg(hw, DM_REG_IGI_A_11N, DM_BIT_IGI_11N);
  943. rtlpriv->dm.dm_type = DM_TYPE_BYDRIVER;
  944. rtl_dm_diginit(hw, cur_igvalue);
  945. rtl92ee_dm_init_rate_adaptive_mask(hw);
  946. rtl92ee_dm_init_primary_cca_check(hw);
  947. rtl92ee_dm_init_edca_turbo(hw);
  948. rtl92ee_dm_init_txpower_tracking(hw);
  949. rtl92ee_dm_init_dynamic_atc_switch(hw);
  950. }
  951. static void rtl92ee_dm_common_info_self_update(struct ieee80211_hw *hw)
  952. {
  953. struct rtl_priv *rtlpriv = rtl_priv(hw);
  954. struct rtl_sta_info *drv_priv;
  955. u8 cnt = 0;
  956. rtlpriv->dm.one_entry_only = false;
  957. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_STATION &&
  958. rtlpriv->mac80211.link_state >= MAC80211_LINKED) {
  959. rtlpriv->dm.one_entry_only = true;
  960. return;
  961. }
  962. if (rtlpriv->mac80211.opmode == NL80211_IFTYPE_AP ||
  963. rtlpriv->mac80211.opmode == NL80211_IFTYPE_ADHOC ||
  964. rtlpriv->mac80211.opmode == NL80211_IFTYPE_MESH_POINT) {
  965. spin_lock_bh(&rtlpriv->locks.entry_list_lock);
  966. list_for_each_entry(drv_priv, &rtlpriv->entry_list, list) {
  967. cnt++;
  968. }
  969. spin_unlock_bh(&rtlpriv->locks.entry_list_lock);
  970. if (cnt == 1)
  971. rtlpriv->dm.one_entry_only = true;
  972. }
  973. }
  974. void rtl92ee_dm_dynamic_arfb_select(struct ieee80211_hw *hw,
  975. u8 rate, bool collision_state)
  976. {
  977. struct rtl_priv *rtlpriv = rtl_priv(hw);
  978. if (rate >= DESC92C_RATEMCS8 && rate <= DESC92C_RATEMCS12) {
  979. if (collision_state == 1) {
  980. if (rate == DESC92C_RATEMCS12) {
  981. rtl_write_dword(rtlpriv, REG_DARFRC, 0x0);
  982. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  983. 0x07060501);
  984. } else if (rate == DESC92C_RATEMCS11) {
  985. rtl_write_dword(rtlpriv, REG_DARFRC, 0x0);
  986. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  987. 0x07070605);
  988. } else if (rate == DESC92C_RATEMCS10) {
  989. rtl_write_dword(rtlpriv, REG_DARFRC, 0x0);
  990. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  991. 0x08080706);
  992. } else if (rate == DESC92C_RATEMCS9) {
  993. rtl_write_dword(rtlpriv, REG_DARFRC, 0x0);
  994. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  995. 0x08080707);
  996. } else {
  997. rtl_write_dword(rtlpriv, REG_DARFRC, 0x0);
  998. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  999. 0x09090808);
  1000. }
  1001. } else { /* collision_state == 0 */
  1002. if (rate == DESC92C_RATEMCS12) {
  1003. rtl_write_dword(rtlpriv, REG_DARFRC,
  1004. 0x05010000);
  1005. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1006. 0x09080706);
  1007. } else if (rate == DESC92C_RATEMCS11) {
  1008. rtl_write_dword(rtlpriv, REG_DARFRC,
  1009. 0x06050000);
  1010. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1011. 0x09080807);
  1012. } else if (rate == DESC92C_RATEMCS10) {
  1013. rtl_write_dword(rtlpriv, REG_DARFRC,
  1014. 0x07060000);
  1015. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1016. 0x0a090908);
  1017. } else if (rate == DESC92C_RATEMCS9) {
  1018. rtl_write_dword(rtlpriv, REG_DARFRC,
  1019. 0x07070000);
  1020. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1021. 0x0a090808);
  1022. } else {
  1023. rtl_write_dword(rtlpriv, REG_DARFRC,
  1024. 0x08080000);
  1025. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1026. 0x0b0a0909);
  1027. }
  1028. }
  1029. } else { /* MCS13~MCS15, 1SS, G-mode */
  1030. if (collision_state == 1) {
  1031. if (rate == DESC92C_RATEMCS15) {
  1032. rtl_write_dword(rtlpriv, REG_DARFRC,
  1033. 0x00000000);
  1034. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1035. 0x05040302);
  1036. } else if (rate == DESC92C_RATEMCS14) {
  1037. rtl_write_dword(rtlpriv, REG_DARFRC,
  1038. 0x00000000);
  1039. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1040. 0x06050302);
  1041. } else if (rate == DESC92C_RATEMCS13) {
  1042. rtl_write_dword(rtlpriv, REG_DARFRC,
  1043. 0x00000000);
  1044. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1045. 0x07060502);
  1046. } else {
  1047. rtl_write_dword(rtlpriv, REG_DARFRC,
  1048. 0x00000000);
  1049. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1050. 0x06050402);
  1051. }
  1052. } else{ /* collision_state == 0 */
  1053. if (rate == DESC92C_RATEMCS15) {
  1054. rtl_write_dword(rtlpriv, REG_DARFRC,
  1055. 0x03020000);
  1056. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1057. 0x07060504);
  1058. } else if (rate == DESC92C_RATEMCS14) {
  1059. rtl_write_dword(rtlpriv, REG_DARFRC,
  1060. 0x03020000);
  1061. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1062. 0x08070605);
  1063. } else if (rate == DESC92C_RATEMCS13) {
  1064. rtl_write_dword(rtlpriv, REG_DARFRC,
  1065. 0x05020000);
  1066. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1067. 0x09080706);
  1068. } else {
  1069. rtl_write_dword(rtlpriv, REG_DARFRC,
  1070. 0x04020000);
  1071. rtl_write_dword(rtlpriv, REG_DARFRC + 4,
  1072. 0x08070605);
  1073. }
  1074. }
  1075. }
  1076. }
  1077. void rtl92ee_dm_watchdog(struct ieee80211_hw *hw)
  1078. {
  1079. struct rtl_priv *rtlpriv = rtl_priv(hw);
  1080. struct rtl_ps_ctl *ppsc = rtl_psc(rtl_priv(hw));
  1081. bool fw_current_inpsmode = false;
  1082. bool fw_ps_awake = true;
  1083. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FW_PSMODE_STATUS,
  1084. (u8 *)(&fw_current_inpsmode));
  1085. rtlpriv->cfg->ops->get_hw_reg(hw, HW_VAR_FWLPS_RF_ON,
  1086. (u8 *)(&fw_ps_awake));
  1087. if (ppsc->p2p_ps_info.p2p_ps_mode)
  1088. fw_ps_awake = false;
  1089. spin_lock(&rtlpriv->locks.rf_ps_lock);
  1090. if ((ppsc->rfpwr_state == ERFON) &&
  1091. ((!fw_current_inpsmode) && fw_ps_awake) &&
  1092. (!ppsc->rfchange_inprogress)) {
  1093. rtl92ee_dm_common_info_self_update(hw);
  1094. rtl92ee_dm_false_alarm_counter_statistics(hw);
  1095. rtl92ee_dm_check_rssi_monitor(hw);
  1096. rtl92ee_dm_dig(hw);
  1097. rtl92ee_dm_adaptivity(hw);
  1098. rtl92ee_dm_cck_packet_detection_thresh(hw);
  1099. rtl92ee_dm_refresh_rate_adaptive_mask(hw);
  1100. rtl92ee_dm_check_edca_turbo(hw);
  1101. rtl92ee_dm_dynamic_atc_switch(hw);
  1102. rtl92ee_dm_dynamic_primary_cca_ckeck(hw);
  1103. }
  1104. spin_unlock(&rtlpriv->locks.rf_ps_lock);
  1105. }