mac.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2009-2012 Realtek Corporation. All rights reserved.
  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 "../pci.h"
  31. #include "../usb.h"
  32. #include "../ps.h"
  33. #include "../cam.h"
  34. #include "../stats.h"
  35. #include "reg.h"
  36. #include "def.h"
  37. #include "phy.h"
  38. #include "rf.h"
  39. #include "dm.h"
  40. #include "mac.h"
  41. #include "trx.h"
  42. #include "../rtl8192c/fw_common.h"
  43. #include <linux/module.h>
  44. /* macro to shorten lines */
  45. #define LINK_Q ui_link_quality
  46. #define RX_EVM rx_evm_percentage
  47. #define RX_SIGQ rx_mimo_sig_qual
  48. void rtl92c_read_chip_version(struct ieee80211_hw *hw)
  49. {
  50. struct rtl_priv *rtlpriv = rtl_priv(hw);
  51. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  52. struct rtl_hal *rtlhal = rtl_hal(rtlpriv);
  53. enum version_8192c chip_version = VERSION_UNKNOWN;
  54. const char *versionid;
  55. u32 value32;
  56. value32 = rtl_read_dword(rtlpriv, REG_SYS_CFG);
  57. if (value32 & TRP_VAUX_EN) {
  58. chip_version = (value32 & TYPE_ID) ? VERSION_TEST_CHIP_92C :
  59. VERSION_TEST_CHIP_88C;
  60. } else {
  61. /* Normal mass production chip. */
  62. chip_version = NORMAL_CHIP;
  63. chip_version |= ((value32 & TYPE_ID) ? CHIP_92C : 0);
  64. chip_version |= ((value32 & VENDOR_ID) ? CHIP_VENDOR_UMC : 0);
  65. /* RTL8723 with BT function. */
  66. chip_version |= ((value32 & BT_FUNC) ? CHIP_8723 : 0);
  67. if (IS_VENDOR_UMC(chip_version))
  68. chip_version |= ((value32 & CHIP_VER_RTL_MASK) ?
  69. CHIP_VENDOR_UMC_B_CUT : 0);
  70. if (IS_92C_SERIAL(chip_version)) {
  71. value32 = rtl_read_dword(rtlpriv, REG_HPON_FSM);
  72. chip_version |= ((CHIP_BONDING_IDENTIFIER(value32) ==
  73. CHIP_BONDING_92C_1T2R) ? CHIP_92C_1T2R : 0);
  74. } else if (IS_8723_SERIES(chip_version)) {
  75. value32 = rtl_read_dword(rtlpriv, REG_GPIO_OUTSTS);
  76. chip_version |= ((value32 & RF_RL_ID) ?
  77. CHIP_8723_DRV_REV : 0);
  78. }
  79. }
  80. rtlhal->version = (enum version_8192c)chip_version;
  81. pr_info("Chip version 0x%x\n", chip_version);
  82. switch (rtlhal->version) {
  83. case VERSION_NORMAL_TSMC_CHIP_92C_1T2R:
  84. versionid = "NORMAL_B_CHIP_92C";
  85. break;
  86. case VERSION_NORMAL_TSMC_CHIP_92C:
  87. versionid = "NORMAL_TSMC_CHIP_92C";
  88. break;
  89. case VERSION_NORMAL_TSMC_CHIP_88C:
  90. versionid = "NORMAL_TSMC_CHIP_88C";
  91. break;
  92. case VERSION_NORMAL_UMC_CHIP_92C_1T2R_A_CUT:
  93. versionid = "NORMAL_UMC_CHIP_i92C_1T2R_A_CUT";
  94. break;
  95. case VERSION_NORMAL_UMC_CHIP_92C_A_CUT:
  96. versionid = "NORMAL_UMC_CHIP_92C_A_CUT";
  97. break;
  98. case VERSION_NORMAL_UMC_CHIP_88C_A_CUT:
  99. versionid = "NORMAL_UMC_CHIP_88C_A_CUT";
  100. break;
  101. case VERSION_NORMAL_UMC_CHIP_92C_1T2R_B_CUT:
  102. versionid = "NORMAL_UMC_CHIP_92C_1T2R_B_CUT";
  103. break;
  104. case VERSION_NORMAL_UMC_CHIP_92C_B_CUT:
  105. versionid = "NORMAL_UMC_CHIP_92C_B_CUT";
  106. break;
  107. case VERSION_NORMAL_UMC_CHIP_88C_B_CUT:
  108. versionid = "NORMAL_UMC_CHIP_88C_B_CUT";
  109. break;
  110. case VERSION_NORMA_UMC_CHIP_8723_1T1R_A_CUT:
  111. versionid = "NORMAL_UMC_CHIP_8723_1T1R_A_CUT";
  112. break;
  113. case VERSION_NORMA_UMC_CHIP_8723_1T1R_B_CUT:
  114. versionid = "NORMAL_UMC_CHIP_8723_1T1R_B_CUT";
  115. break;
  116. case VERSION_TEST_CHIP_92C:
  117. versionid = "TEST_CHIP_92C";
  118. break;
  119. case VERSION_TEST_CHIP_88C:
  120. versionid = "TEST_CHIP_88C";
  121. break;
  122. default:
  123. versionid = "UNKNOWN";
  124. break;
  125. }
  126. RT_TRACE(rtlpriv, COMP_INIT, DBG_TRACE,
  127. "Chip Version ID: %s\n", versionid);
  128. if (IS_92C_SERIAL(rtlhal->version))
  129. rtlphy->rf_type =
  130. (IS_92C_1T2R(rtlhal->version)) ? RF_1T2R : RF_2T2R;
  131. else
  132. rtlphy->rf_type = RF_1T1R;
  133. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD,
  134. "Chip RF Type: %s\n",
  135. rtlphy->rf_type == RF_2T2R ? "RF_2T2R" : "RF_1T1R");
  136. if (get_rf_type(rtlphy) == RF_1T1R)
  137. rtlpriv->dm.rfpath_rxenable[0] = true;
  138. else
  139. rtlpriv->dm.rfpath_rxenable[0] =
  140. rtlpriv->dm.rfpath_rxenable[1] = true;
  141. RT_TRACE(rtlpriv, COMP_INIT, DBG_LOUD, "VersionID = 0x%4x\n",
  142. rtlhal->version);
  143. }
  144. /**
  145. * writeLLT - LLT table write access
  146. * @io: io callback
  147. * @address: LLT logical address.
  148. * @data: LLT data content
  149. *
  150. * Realtek hardware access function.
  151. *
  152. */
  153. bool rtl92c_llt_write(struct ieee80211_hw *hw, u32 address, u32 data)
  154. {
  155. struct rtl_priv *rtlpriv = rtl_priv(hw);
  156. bool status = true;
  157. long count = 0;
  158. u32 value = _LLT_INIT_ADDR(address) |
  159. _LLT_INIT_DATA(data) | _LLT_OP(_LLT_WRITE_ACCESS);
  160. rtl_write_dword(rtlpriv, REG_LLT_INIT, value);
  161. do {
  162. value = rtl_read_dword(rtlpriv, REG_LLT_INIT);
  163. if (_LLT_NO_ACTIVE == _LLT_OP_VALUE(value))
  164. break;
  165. if (count > POLLING_LLT_THRESHOLD) {
  166. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  167. "Failed to polling write LLT done at address %d! _LLT_OP_VALUE(%x)\n",
  168. address, _LLT_OP_VALUE(value));
  169. status = false;
  170. break;
  171. }
  172. } while (++count);
  173. return status;
  174. }
  175. /**
  176. * rtl92c_init_LLT_table - Init LLT table
  177. * @io: io callback
  178. * @boundary:
  179. *
  180. * Realtek hardware access function.
  181. *
  182. */
  183. bool rtl92c_init_llt_table(struct ieee80211_hw *hw, u32 boundary)
  184. {
  185. bool rst = true;
  186. u32 i;
  187. for (i = 0; i < (boundary - 1); i++) {
  188. rst = rtl92c_llt_write(hw, i , i + 1);
  189. if (true != rst) {
  190. pr_err("===> %s #1 fail\n", __func__);
  191. return rst;
  192. }
  193. }
  194. /* end of list */
  195. rst = rtl92c_llt_write(hw, (boundary - 1), 0xFF);
  196. if (true != rst) {
  197. pr_err("===> %s #2 fail\n", __func__);
  198. return rst;
  199. }
  200. /* Make the other pages as ring buffer
  201. * This ring buffer is used as beacon buffer if we config this MAC
  202. * as two MAC transfer.
  203. * Otherwise used as local loopback buffer.
  204. */
  205. for (i = boundary; i < LLT_LAST_ENTRY_OF_TX_PKT_BUFFER; i++) {
  206. rst = rtl92c_llt_write(hw, i, (i + 1));
  207. if (true != rst) {
  208. pr_err("===> %s #3 fail\n", __func__);
  209. return rst;
  210. }
  211. }
  212. /* Let last entry point to the start entry of ring buffer */
  213. rst = rtl92c_llt_write(hw, LLT_LAST_ENTRY_OF_TX_PKT_BUFFER, boundary);
  214. if (true != rst) {
  215. pr_err("===> %s #4 fail\n", __func__);
  216. return rst;
  217. }
  218. return rst;
  219. }
  220. void rtl92c_set_key(struct ieee80211_hw *hw, u32 key_index,
  221. u8 *p_macaddr, bool is_group, u8 enc_algo,
  222. bool is_wepkey, bool clear_all)
  223. {
  224. struct rtl_priv *rtlpriv = rtl_priv(hw);
  225. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  226. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  227. u8 *macaddr = p_macaddr;
  228. u32 entry_id = 0;
  229. bool is_pairwise = false;
  230. static u8 cam_const_addr[4][6] = {
  231. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  232. {0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
  233. {0x00, 0x00, 0x00, 0x00, 0x00, 0x02},
  234. {0x00, 0x00, 0x00, 0x00, 0x00, 0x03}
  235. };
  236. static u8 cam_const_broad[] = {
  237. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  238. };
  239. if (clear_all) {
  240. u8 idx = 0;
  241. u8 cam_offset = 0;
  242. u8 clear_number = 5;
  243. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG, "clear_all\n");
  244. for (idx = 0; idx < clear_number; idx++) {
  245. rtl_cam_mark_invalid(hw, cam_offset + idx);
  246. rtl_cam_empty_entry(hw, cam_offset + idx);
  247. if (idx < 5) {
  248. memset(rtlpriv->sec.key_buf[idx], 0,
  249. MAX_KEY_LEN);
  250. rtlpriv->sec.key_len[idx] = 0;
  251. }
  252. }
  253. } else {
  254. switch (enc_algo) {
  255. case WEP40_ENCRYPTION:
  256. enc_algo = CAM_WEP40;
  257. break;
  258. case WEP104_ENCRYPTION:
  259. enc_algo = CAM_WEP104;
  260. break;
  261. case TKIP_ENCRYPTION:
  262. enc_algo = CAM_TKIP;
  263. break;
  264. case AESCCMP_ENCRYPTION:
  265. enc_algo = CAM_AES;
  266. break;
  267. default:
  268. RT_TRACE(rtlpriv, COMP_ERR, DBG_EMERG,
  269. "illegal switch case\n");
  270. enc_algo = CAM_TKIP;
  271. break;
  272. }
  273. if (is_wepkey || rtlpriv->sec.use_defaultkey) {
  274. macaddr = cam_const_addr[key_index];
  275. entry_id = key_index;
  276. } else {
  277. if (is_group) {
  278. macaddr = cam_const_broad;
  279. entry_id = key_index;
  280. } else {
  281. if (mac->opmode == NL80211_IFTYPE_AP ||
  282. mac->opmode == NL80211_IFTYPE_MESH_POINT) {
  283. entry_id = rtl_cam_get_free_entry(hw,
  284. p_macaddr);
  285. if (entry_id >= TOTAL_CAM_ENTRY) {
  286. RT_TRACE(rtlpriv, COMP_SEC,
  287. DBG_EMERG,
  288. "Can not find free hw security cam entry\n");
  289. return;
  290. }
  291. } else {
  292. entry_id = CAM_PAIRWISE_KEY_POSITION;
  293. }
  294. key_index = PAIRWISE_KEYIDX;
  295. is_pairwise = true;
  296. }
  297. }
  298. if (rtlpriv->sec.key_len[key_index] == 0) {
  299. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  300. "delete one entry\n");
  301. if (mac->opmode == NL80211_IFTYPE_AP ||
  302. mac->opmode == NL80211_IFTYPE_MESH_POINT)
  303. rtl_cam_del_entry(hw, p_macaddr);
  304. rtl_cam_delete_one_entry(hw, p_macaddr, entry_id);
  305. } else {
  306. RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
  307. "The insert KEY length is %d\n",
  308. rtlpriv->sec.key_len[PAIRWISE_KEYIDX]);
  309. RT_TRACE(rtlpriv, COMP_SEC, DBG_LOUD,
  310. "The insert KEY is %x %x\n",
  311. rtlpriv->sec.key_buf[0][0],
  312. rtlpriv->sec.key_buf[0][1]);
  313. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  314. "add one entry\n");
  315. if (is_pairwise) {
  316. RT_PRINT_DATA(rtlpriv, COMP_SEC, DBG_LOUD,
  317. "Pairwise Key content",
  318. rtlpriv->sec.pairwise_key,
  319. rtlpriv->sec.
  320. key_len[PAIRWISE_KEYIDX]);
  321. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  322. "set Pairwise key\n");
  323. rtl_cam_add_one_entry(hw, macaddr, key_index,
  324. entry_id, enc_algo,
  325. CAM_CONFIG_NO_USEDK,
  326. rtlpriv->sec.
  327. key_buf[key_index]);
  328. } else {
  329. RT_TRACE(rtlpriv, COMP_SEC, DBG_DMESG,
  330. "set group key\n");
  331. if (mac->opmode == NL80211_IFTYPE_ADHOC) {
  332. rtl_cam_add_one_entry(hw,
  333. rtlefuse->dev_addr,
  334. PAIRWISE_KEYIDX,
  335. CAM_PAIRWISE_KEY_POSITION,
  336. enc_algo,
  337. CAM_CONFIG_NO_USEDK,
  338. rtlpriv->sec.key_buf
  339. [entry_id]);
  340. }
  341. rtl_cam_add_one_entry(hw, macaddr, key_index,
  342. entry_id, enc_algo,
  343. CAM_CONFIG_NO_USEDK,
  344. rtlpriv->sec.key_buf[entry_id]);
  345. }
  346. }
  347. }
  348. }
  349. u32 rtl92c_get_txdma_status(struct ieee80211_hw *hw)
  350. {
  351. struct rtl_priv *rtlpriv = rtl_priv(hw);
  352. return rtl_read_dword(rtlpriv, REG_TXDMA_STATUS);
  353. }
  354. void rtl92c_enable_interrupt(struct ieee80211_hw *hw)
  355. {
  356. struct rtl_priv *rtlpriv = rtl_priv(hw);
  357. struct rtl_hal *rtlhal = rtl_hal(rtl_priv(hw));
  358. struct rtl_pci *rtlpci = rtl_pcidev(rtl_pcipriv(hw));
  359. struct rtl_usb *rtlusb = rtl_usbdev(rtl_usbpriv(hw));
  360. if (IS_HARDWARE_TYPE_8192CE(rtlhal)) {
  361. rtl_write_dword(rtlpriv, REG_HIMR, rtlpci->irq_mask[0] &
  362. 0xFFFFFFFF);
  363. rtl_write_dword(rtlpriv, REG_HIMRE, rtlpci->irq_mask[1] &
  364. 0xFFFFFFFF);
  365. } else {
  366. rtl_write_dword(rtlpriv, REG_HIMR, rtlusb->irq_mask[0] &
  367. 0xFFFFFFFF);
  368. rtl_write_dword(rtlpriv, REG_HIMRE, rtlusb->irq_mask[1] &
  369. 0xFFFFFFFF);
  370. }
  371. }
  372. void rtl92c_init_interrupt(struct ieee80211_hw *hw)
  373. {
  374. rtl92c_enable_interrupt(hw);
  375. }
  376. void rtl92c_disable_interrupt(struct ieee80211_hw *hw)
  377. {
  378. struct rtl_priv *rtlpriv = rtl_priv(hw);
  379. rtl_write_dword(rtlpriv, REG_HIMR, IMR8190_DISABLED);
  380. rtl_write_dword(rtlpriv, REG_HIMRE, IMR8190_DISABLED);
  381. }
  382. void rtl92c_set_qos(struct ieee80211_hw *hw, int aci)
  383. {
  384. struct rtl_priv *rtlpriv = rtl_priv(hw);
  385. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  386. u32 u4b_ac_param;
  387. rtl92c_dm_init_edca_turbo(hw);
  388. u4b_ac_param = (u32) mac->ac[aci].aifs;
  389. u4b_ac_param |=
  390. ((u32) le16_to_cpu(mac->ac[aci].cw_min) & 0xF) <<
  391. AC_PARAM_ECW_MIN_OFFSET;
  392. u4b_ac_param |=
  393. ((u32) le16_to_cpu(mac->ac[aci].cw_max) & 0xF) <<
  394. AC_PARAM_ECW_MAX_OFFSET;
  395. u4b_ac_param |= (u32) le16_to_cpu(mac->ac[aci].tx_op) <<
  396. AC_PARAM_TXOP_OFFSET;
  397. RT_TRACE(rtlpriv, COMP_QOS, DBG_LOUD, "queue:%x, ac_param:%x\n",
  398. aci, u4b_ac_param);
  399. switch (aci) {
  400. case AC1_BK:
  401. rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM, u4b_ac_param);
  402. break;
  403. case AC0_BE:
  404. rtl_write_dword(rtlpriv, REG_EDCA_BE_PARAM, u4b_ac_param);
  405. break;
  406. case AC2_VI:
  407. rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM, u4b_ac_param);
  408. break;
  409. case AC3_VO:
  410. rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM, u4b_ac_param);
  411. break;
  412. default:
  413. RT_ASSERT(false, "invalid aci: %d !\n", aci);
  414. break;
  415. }
  416. }
  417. /*-------------------------------------------------------------------------
  418. * HW MAC Address
  419. *-------------------------------------------------------------------------*/
  420. void rtl92c_set_mac_addr(struct ieee80211_hw *hw, const u8 *addr)
  421. {
  422. u32 i;
  423. struct rtl_priv *rtlpriv = rtl_priv(hw);
  424. for (i = 0 ; i < ETH_ALEN ; i++)
  425. rtl_write_byte(rtlpriv, (REG_MACID + i), *(addr+i));
  426. RT_TRACE(rtlpriv, COMP_CMD, DBG_DMESG,
  427. "MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
  428. rtl_read_byte(rtlpriv, REG_MACID),
  429. rtl_read_byte(rtlpriv, REG_MACID+1),
  430. rtl_read_byte(rtlpriv, REG_MACID+2),
  431. rtl_read_byte(rtlpriv, REG_MACID+3),
  432. rtl_read_byte(rtlpriv, REG_MACID+4),
  433. rtl_read_byte(rtlpriv, REG_MACID+5));
  434. }
  435. void rtl92c_init_driver_info_size(struct ieee80211_hw *hw, u8 size)
  436. {
  437. struct rtl_priv *rtlpriv = rtl_priv(hw);
  438. rtl_write_byte(rtlpriv, REG_RX_DRVINFO_SZ, size);
  439. }
  440. int rtl92c_set_network_type(struct ieee80211_hw *hw, enum nl80211_iftype type)
  441. {
  442. u8 value;
  443. struct rtl_priv *rtlpriv = rtl_priv(hw);
  444. switch (type) {
  445. case NL80211_IFTYPE_UNSPECIFIED:
  446. value = NT_NO_LINK;
  447. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  448. "Set Network type to NO LINK!\n");
  449. break;
  450. case NL80211_IFTYPE_ADHOC:
  451. value = NT_LINK_AD_HOC;
  452. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  453. "Set Network type to Ad Hoc!\n");
  454. break;
  455. case NL80211_IFTYPE_STATION:
  456. value = NT_LINK_AP;
  457. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  458. "Set Network type to STA!\n");
  459. break;
  460. case NL80211_IFTYPE_AP:
  461. value = NT_AS_AP;
  462. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  463. "Set Network type to AP!\n");
  464. break;
  465. default:
  466. RT_TRACE(rtlpriv, COMP_INIT, DBG_DMESG,
  467. "Network type %d not supported!\n", type);
  468. return -EOPNOTSUPP;
  469. }
  470. rtl_write_byte(rtlpriv, MSR, value);
  471. return 0;
  472. }
  473. void rtl92c_init_network_type(struct ieee80211_hw *hw)
  474. {
  475. rtl92c_set_network_type(hw, NL80211_IFTYPE_UNSPECIFIED);
  476. }
  477. void rtl92c_init_adaptive_ctrl(struct ieee80211_hw *hw)
  478. {
  479. u16 value16;
  480. u32 value32;
  481. struct rtl_priv *rtlpriv = rtl_priv(hw);
  482. /* Response Rate Set */
  483. value32 = rtl_read_dword(rtlpriv, REG_RRSR);
  484. value32 &= ~RATE_BITMAP_ALL;
  485. value32 |= RATE_RRSR_CCK_ONLY_1M;
  486. rtl_write_dword(rtlpriv, REG_RRSR, value32);
  487. /* SIFS (used in NAV) */
  488. value16 = _SPEC_SIFS_CCK(0x10) | _SPEC_SIFS_OFDM(0x10);
  489. rtl_write_word(rtlpriv, REG_SPEC_SIFS, value16);
  490. /* Retry Limit */
  491. value16 = _LRL(0x30) | _SRL(0x30);
  492. rtl_write_dword(rtlpriv, REG_RL, value16);
  493. }
  494. void rtl92c_init_rate_fallback(struct ieee80211_hw *hw)
  495. {
  496. struct rtl_priv *rtlpriv = rtl_priv(hw);
  497. /* Set Data Auto Rate Fallback Retry Count register. */
  498. rtl_write_dword(rtlpriv, REG_DARFRC, 0x00000000);
  499. rtl_write_dword(rtlpriv, REG_DARFRC+4, 0x10080404);
  500. rtl_write_dword(rtlpriv, REG_RARFRC, 0x04030201);
  501. rtl_write_dword(rtlpriv, REG_RARFRC+4, 0x08070605);
  502. }
  503. static void rtl92c_set_cck_sifs(struct ieee80211_hw *hw, u8 trx_sifs,
  504. u8 ctx_sifs)
  505. {
  506. struct rtl_priv *rtlpriv = rtl_priv(hw);
  507. rtl_write_byte(rtlpriv, REG_SIFS_CCK, trx_sifs);
  508. rtl_write_byte(rtlpriv, (REG_SIFS_CCK + 1), ctx_sifs);
  509. }
  510. static void rtl92c_set_ofdm_sifs(struct ieee80211_hw *hw, u8 trx_sifs,
  511. u8 ctx_sifs)
  512. {
  513. struct rtl_priv *rtlpriv = rtl_priv(hw);
  514. rtl_write_byte(rtlpriv, REG_SIFS_OFDM, trx_sifs);
  515. rtl_write_byte(rtlpriv, (REG_SIFS_OFDM + 1), ctx_sifs);
  516. }
  517. void rtl92c_init_edca_param(struct ieee80211_hw *hw,
  518. u16 queue, u16 txop, u8 cw_min, u8 cw_max, u8 aifs)
  519. {
  520. /* sequence: VO, VI, BE, BK ==> the same as 92C hardware design.
  521. * referenc : enum nl80211_txq_q or ieee80211_set_wmm_default function.
  522. */
  523. u32 value;
  524. struct rtl_priv *rtlpriv = rtl_priv(hw);
  525. value = (u32)aifs;
  526. value |= ((u32)cw_min & 0xF) << 8;
  527. value |= ((u32)cw_max & 0xF) << 12;
  528. value |= (u32)txop << 16;
  529. /* 92C hardware register sequence is the same as queue number. */
  530. rtl_write_dword(rtlpriv, (REG_EDCA_VO_PARAM + (queue * 4)), value);
  531. }
  532. void rtl92c_init_edca(struct ieee80211_hw *hw)
  533. {
  534. u16 value16;
  535. struct rtl_priv *rtlpriv = rtl_priv(hw);
  536. /* disable EDCCA count down, to reduce collison and retry */
  537. value16 = rtl_read_word(rtlpriv, REG_RD_CTRL);
  538. value16 |= DIS_EDCA_CNT_DWN;
  539. rtl_write_word(rtlpriv, REG_RD_CTRL, value16);
  540. /* Update SIFS timing. ??????????
  541. * pHalData->SifsTime = 0x0e0e0a0a; */
  542. rtl92c_set_cck_sifs(hw, 0xa, 0xa);
  543. rtl92c_set_ofdm_sifs(hw, 0xe, 0xe);
  544. /* Set CCK/OFDM SIFS to be 10us. */
  545. rtl_write_word(rtlpriv, REG_SIFS_CCK, 0x0a0a);
  546. rtl_write_word(rtlpriv, REG_SIFS_OFDM, 0x1010);
  547. rtl_write_word(rtlpriv, REG_PROT_MODE_CTRL, 0x0204);
  548. rtl_write_dword(rtlpriv, REG_BAR_MODE_CTRL, 0x014004);
  549. /* TXOP */
  550. rtl_write_dword(rtlpriv, REG_EDCA_BE_PARAM, 0x005EA42B);
  551. rtl_write_dword(rtlpriv, REG_EDCA_BK_PARAM, 0x0000A44F);
  552. rtl_write_dword(rtlpriv, REG_EDCA_VI_PARAM, 0x005EA324);
  553. rtl_write_dword(rtlpriv, REG_EDCA_VO_PARAM, 0x002FA226);
  554. /* PIFS */
  555. rtl_write_byte(rtlpriv, REG_PIFS, 0x1C);
  556. /* AGGR BREAK TIME Register */
  557. rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
  558. rtl_write_word(rtlpriv, REG_NAV_PROT_LEN, 0x0040);
  559. rtl_write_byte(rtlpriv, REG_BCNDMATIM, 0x02);
  560. rtl_write_byte(rtlpriv, REG_ATIMWND, 0x02);
  561. }
  562. void rtl92c_init_ampdu_aggregation(struct ieee80211_hw *hw)
  563. {
  564. struct rtl_priv *rtlpriv = rtl_priv(hw);
  565. rtl_write_dword(rtlpriv, REG_AGGLEN_LMT, 0x99997631);
  566. rtl_write_byte(rtlpriv, REG_AGGR_BREAK_TIME, 0x16);
  567. /* init AMPDU aggregation number, tuning for Tx's TP, */
  568. rtl_write_word(rtlpriv, 0x4CA, 0x0708);
  569. }
  570. void rtl92c_init_beacon_max_error(struct ieee80211_hw *hw)
  571. {
  572. struct rtl_priv *rtlpriv = rtl_priv(hw);
  573. rtl_write_byte(rtlpriv, REG_BCN_MAX_ERR, 0xFF);
  574. }
  575. void rtl92c_init_rdg_setting(struct ieee80211_hw *hw)
  576. {
  577. struct rtl_priv *rtlpriv = rtl_priv(hw);
  578. rtl_write_byte(rtlpriv, REG_RD_CTRL, 0xFF);
  579. rtl_write_word(rtlpriv, REG_RD_NAV_NXT, 0x200);
  580. rtl_write_byte(rtlpriv, REG_RD_RESP_PKT_TH, 0x05);
  581. }
  582. void rtl92c_init_retry_function(struct ieee80211_hw *hw)
  583. {
  584. u8 value8;
  585. struct rtl_priv *rtlpriv = rtl_priv(hw);
  586. value8 = rtl_read_byte(rtlpriv, REG_FWHW_TXQ_CTRL);
  587. value8 |= EN_AMPDU_RTY_NEW;
  588. rtl_write_byte(rtlpriv, REG_FWHW_TXQ_CTRL, value8);
  589. /* Set ACK timeout */
  590. rtl_write_byte(rtlpriv, REG_ACKTO, 0x40);
  591. }
  592. void rtl92c_disable_fast_edca(struct ieee80211_hw *hw)
  593. {
  594. struct rtl_priv *rtlpriv = rtl_priv(hw);
  595. rtl_write_word(rtlpriv, REG_FAST_EDCA_CTRL, 0);
  596. }
  597. void rtl92c_set_min_space(struct ieee80211_hw *hw, bool is2T)
  598. {
  599. struct rtl_priv *rtlpriv = rtl_priv(hw);
  600. u8 value = is2T ? MAX_MSS_DENSITY_2T : MAX_MSS_DENSITY_1T;
  601. rtl_write_byte(rtlpriv, REG_AMPDU_MIN_SPACE, value);
  602. }
  603. u16 rtl92c_get_mgt_filter(struct ieee80211_hw *hw)
  604. {
  605. struct rtl_priv *rtlpriv = rtl_priv(hw);
  606. return rtl_read_word(rtlpriv, REG_RXFLTMAP0);
  607. }
  608. void rtl92c_set_mgt_filter(struct ieee80211_hw *hw, u16 filter)
  609. {
  610. struct rtl_priv *rtlpriv = rtl_priv(hw);
  611. rtl_write_word(rtlpriv, REG_RXFLTMAP0, filter);
  612. }
  613. u16 rtl92c_get_ctrl_filter(struct ieee80211_hw *hw)
  614. {
  615. struct rtl_priv *rtlpriv = rtl_priv(hw);
  616. return rtl_read_word(rtlpriv, REG_RXFLTMAP1);
  617. }
  618. void rtl92c_set_ctrl_filter(struct ieee80211_hw *hw, u16 filter)
  619. {
  620. struct rtl_priv *rtlpriv = rtl_priv(hw);
  621. rtl_write_word(rtlpriv, REG_RXFLTMAP1, filter);
  622. }
  623. u16 rtl92c_get_data_filter(struct ieee80211_hw *hw)
  624. {
  625. struct rtl_priv *rtlpriv = rtl_priv(hw);
  626. return rtl_read_word(rtlpriv, REG_RXFLTMAP2);
  627. }
  628. void rtl92c_set_data_filter(struct ieee80211_hw *hw, u16 filter)
  629. {
  630. struct rtl_priv *rtlpriv = rtl_priv(hw);
  631. rtl_write_word(rtlpriv, REG_RXFLTMAP2, filter);
  632. }
  633. /*==============================================================*/
  634. static u8 _rtl92c_query_rxpwrpercentage(char antpower)
  635. {
  636. if ((antpower <= -100) || (antpower >= 20))
  637. return 0;
  638. else if (antpower >= 0)
  639. return 100;
  640. else
  641. return 100 + antpower;
  642. }
  643. static u8 _rtl92c_evm_db_to_percentage(char value)
  644. {
  645. char ret_val;
  646. ret_val = value;
  647. if (ret_val >= 0)
  648. ret_val = 0;
  649. if (ret_val <= -33)
  650. ret_val = -33;
  651. ret_val = 0 - ret_val;
  652. ret_val *= 3;
  653. if (ret_val == 99)
  654. ret_val = 100;
  655. return ret_val;
  656. }
  657. static long _rtl92c_signal_scale_mapping(struct ieee80211_hw *hw,
  658. long currsig)
  659. {
  660. long retsig;
  661. if (currsig >= 61 && currsig <= 100)
  662. retsig = 90 + ((currsig - 60) / 4);
  663. else if (currsig >= 41 && currsig <= 60)
  664. retsig = 78 + ((currsig - 40) / 2);
  665. else if (currsig >= 31 && currsig <= 40)
  666. retsig = 66 + (currsig - 30);
  667. else if (currsig >= 21 && currsig <= 30)
  668. retsig = 54 + (currsig - 20);
  669. else if (currsig >= 5 && currsig <= 20)
  670. retsig = 42 + (((currsig - 5) * 2) / 3);
  671. else if (currsig == 4)
  672. retsig = 36;
  673. else if (currsig == 3)
  674. retsig = 27;
  675. else if (currsig == 2)
  676. retsig = 18;
  677. else if (currsig == 1)
  678. retsig = 9;
  679. else
  680. retsig = currsig;
  681. return retsig;
  682. }
  683. static void _rtl92c_query_rxphystatus(struct ieee80211_hw *hw,
  684. struct rtl_stats *pstats,
  685. struct rx_desc_92c *p_desc,
  686. struct rx_fwinfo_92c *p_drvinfo,
  687. bool packet_match_bssid,
  688. bool packet_toself,
  689. bool packet_beacon)
  690. {
  691. struct rtl_priv *rtlpriv = rtl_priv(hw);
  692. struct rtl_phy *rtlphy = &(rtlpriv->phy);
  693. struct phy_sts_cck_8192s_t *cck_buf;
  694. s8 rx_pwr_all = 0, rx_pwr[4];
  695. u8 rf_rx_num = 0, evm, pwdb_all;
  696. u8 i, max_spatial_stream;
  697. u32 rssi, total_rssi = 0;
  698. bool in_powersavemode = false;
  699. bool is_cck_rate;
  700. u8 *pdesc = (u8 *)p_desc;
  701. is_cck_rate = RX_HAL_IS_CCK_RATE(p_desc->rxmcs);
  702. pstats->packet_matchbssid = packet_match_bssid;
  703. pstats->packet_toself = packet_toself;
  704. pstats->packet_beacon = packet_beacon;
  705. pstats->is_cck = is_cck_rate;
  706. pstats->RX_SIGQ[0] = -1;
  707. pstats->RX_SIGQ[1] = -1;
  708. if (is_cck_rate) {
  709. u8 report, cck_highpwr;
  710. cck_buf = (struct phy_sts_cck_8192s_t *)p_drvinfo;
  711. if (!in_powersavemode)
  712. cck_highpwr = rtlphy->cck_high_power;
  713. else
  714. cck_highpwr = false;
  715. if (!cck_highpwr) {
  716. u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
  717. report = cck_buf->cck_agc_rpt & 0xc0;
  718. report = report >> 6;
  719. switch (report) {
  720. case 0x3:
  721. rx_pwr_all = -46 - (cck_agc_rpt & 0x3e);
  722. break;
  723. case 0x2:
  724. rx_pwr_all = -26 - (cck_agc_rpt & 0x3e);
  725. break;
  726. case 0x1:
  727. rx_pwr_all = -12 - (cck_agc_rpt & 0x3e);
  728. break;
  729. case 0x0:
  730. rx_pwr_all = 16 - (cck_agc_rpt & 0x3e);
  731. break;
  732. }
  733. } else {
  734. u8 cck_agc_rpt = cck_buf->cck_agc_rpt;
  735. report = p_drvinfo->cfosho[0] & 0x60;
  736. report = report >> 5;
  737. switch (report) {
  738. case 0x3:
  739. rx_pwr_all = -46 - ((cck_agc_rpt & 0x1f) << 1);
  740. break;
  741. case 0x2:
  742. rx_pwr_all = -26 - ((cck_agc_rpt & 0x1f) << 1);
  743. break;
  744. case 0x1:
  745. rx_pwr_all = -12 - ((cck_agc_rpt & 0x1f) << 1);
  746. break;
  747. case 0x0:
  748. rx_pwr_all = 16 - ((cck_agc_rpt & 0x1f) << 1);
  749. break;
  750. }
  751. }
  752. pwdb_all = _rtl92c_query_rxpwrpercentage(rx_pwr_all);
  753. pstats->rx_pwdb_all = pwdb_all;
  754. pstats->recvsignalpower = rx_pwr_all;
  755. if (packet_match_bssid) {
  756. u8 sq;
  757. if (pstats->rx_pwdb_all > 40)
  758. sq = 100;
  759. else {
  760. sq = cck_buf->sq_rpt;
  761. if (sq > 64)
  762. sq = 0;
  763. else if (sq < 20)
  764. sq = 100;
  765. else
  766. sq = ((64 - sq) * 100) / 44;
  767. }
  768. pstats->signalquality = sq;
  769. pstats->RX_SIGQ[0] = sq;
  770. pstats->RX_SIGQ[1] = -1;
  771. }
  772. } else {
  773. rtlpriv->dm.rfpath_rxenable[0] =
  774. rtlpriv->dm.rfpath_rxenable[1] = true;
  775. for (i = RF90_PATH_A; i < RF90_PATH_MAX; i++) {
  776. if (rtlpriv->dm.rfpath_rxenable[i])
  777. rf_rx_num++;
  778. rx_pwr[i] =
  779. ((p_drvinfo->gain_trsw[i] & 0x3f) * 2) - 110;
  780. rssi = _rtl92c_query_rxpwrpercentage(rx_pwr[i]);
  781. total_rssi += rssi;
  782. rtlpriv->stats.rx_snr_db[i] =
  783. (long)(p_drvinfo->rxsnr[i] / 2);
  784. if (packet_match_bssid)
  785. pstats->rx_mimo_signalstrength[i] = (u8) rssi;
  786. }
  787. rx_pwr_all = ((p_drvinfo->pwdb_all >> 1) & 0x7f) - 110;
  788. pwdb_all = _rtl92c_query_rxpwrpercentage(rx_pwr_all);
  789. pstats->rx_pwdb_all = pwdb_all;
  790. pstats->rxpower = rx_pwr_all;
  791. pstats->recvsignalpower = rx_pwr_all;
  792. if (GET_RX_DESC_RX_MCS(pdesc) &&
  793. GET_RX_DESC_RX_MCS(pdesc) >= DESC_RATEMCS8 &&
  794. GET_RX_DESC_RX_MCS(pdesc) <= DESC_RATEMCS15)
  795. max_spatial_stream = 2;
  796. else
  797. max_spatial_stream = 1;
  798. for (i = 0; i < max_spatial_stream; i++) {
  799. evm = _rtl92c_evm_db_to_percentage(p_drvinfo->rxevm[i]);
  800. if (packet_match_bssid) {
  801. if (i == 0)
  802. pstats->signalquality =
  803. (u8) (evm & 0xff);
  804. pstats->RX_SIGQ[i] =
  805. (u8) (evm & 0xff);
  806. }
  807. }
  808. }
  809. if (is_cck_rate)
  810. pstats->signalstrength =
  811. (u8) (_rtl92c_signal_scale_mapping(hw, pwdb_all));
  812. else if (rf_rx_num != 0)
  813. pstats->signalstrength =
  814. (u8) (_rtl92c_signal_scale_mapping
  815. (hw, total_rssi /= rf_rx_num));
  816. }
  817. void rtl92c_translate_rx_signal_stuff(struct ieee80211_hw *hw,
  818. struct sk_buff *skb,
  819. struct rtl_stats *pstats,
  820. struct rx_desc_92c *pdesc,
  821. struct rx_fwinfo_92c *p_drvinfo)
  822. {
  823. struct rtl_mac *mac = rtl_mac(rtl_priv(hw));
  824. struct rtl_efuse *rtlefuse = rtl_efuse(rtl_priv(hw));
  825. struct ieee80211_hdr *hdr;
  826. u8 *tmp_buf;
  827. u8 *praddr;
  828. __le16 fc;
  829. u16 type, cpu_fc;
  830. bool packet_matchbssid, packet_toself, packet_beacon = false;
  831. tmp_buf = skb->data + pstats->rx_drvinfo_size + pstats->rx_bufshift;
  832. hdr = (struct ieee80211_hdr *)tmp_buf;
  833. fc = hdr->frame_control;
  834. cpu_fc = le16_to_cpu(fc);
  835. type = WLAN_FC_GET_TYPE(fc);
  836. praddr = hdr->addr1;
  837. packet_matchbssid =
  838. ((IEEE80211_FTYPE_CTL != type) &&
  839. ether_addr_equal(mac->bssid,
  840. (cpu_fc & IEEE80211_FCTL_TODS) ? hdr->addr1 :
  841. (cpu_fc & IEEE80211_FCTL_FROMDS) ? hdr->addr2 :
  842. hdr->addr3) &&
  843. (!pstats->hwerror) && (!pstats->crc) && (!pstats->icv));
  844. packet_toself = packet_matchbssid &&
  845. ether_addr_equal(praddr, rtlefuse->dev_addr);
  846. if (ieee80211_is_beacon(fc))
  847. packet_beacon = true;
  848. _rtl92c_query_rxphystatus(hw, pstats, pdesc, p_drvinfo,
  849. packet_matchbssid, packet_toself,
  850. packet_beacon);
  851. rtl_process_phyinfo(hw, tmp_buf, pstats);
  852. }