wow.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /* Copyright(c) 2019-2022 Realtek Corporation
  3. */
  4. #include "cam.h"
  5. #include "core.h"
  6. #include "debug.h"
  7. #include "fw.h"
  8. #include "mac.h"
  9. #include "phy.h"
  10. #include "ps.h"
  11. #include "reg.h"
  12. #include "util.h"
  13. #include "wow.h"
  14. static void rtw89_wow_leave_deep_ps(struct rtw89_dev *rtwdev)
  15. {
  16. __rtw89_leave_ps_mode(rtwdev);
  17. }
  18. static void rtw89_wow_enter_deep_ps(struct rtw89_dev *rtwdev)
  19. {
  20. struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
  21. struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
  22. __rtw89_enter_ps_mode(rtwdev, rtwvif);
  23. }
  24. static void rtw89_wow_enter_lps(struct rtw89_dev *rtwdev)
  25. {
  26. struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
  27. struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
  28. rtw89_enter_lps(rtwdev, rtwvif, false);
  29. }
  30. static void rtw89_wow_leave_lps(struct rtw89_dev *rtwdev)
  31. {
  32. rtw89_leave_lps(rtwdev);
  33. }
  34. static int rtw89_wow_config_mac(struct rtw89_dev *rtwdev, bool enable_wow)
  35. {
  36. const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
  37. int ret;
  38. if (enable_wow) {
  39. ret = rtw89_mac_resize_ple_rx_quota(rtwdev, true);
  40. if (ret) {
  41. rtw89_err(rtwdev, "[ERR]patch rx qta %d\n", ret);
  42. return ret;
  43. }
  44. rtw89_write32_set(rtwdev, R_AX_RX_FUNCTION_STOP, B_AX_HDR_RX_STOP);
  45. rtw89_write32_clr(rtwdev, mac->rx_fltr, B_AX_SNIFFER_MODE);
  46. rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, false);
  47. rtw89_write32(rtwdev, R_AX_ACTION_FWD0, 0);
  48. rtw89_write32(rtwdev, R_AX_ACTION_FWD1, 0);
  49. rtw89_write32(rtwdev, R_AX_TF_FWD, 0);
  50. rtw89_write32(rtwdev, R_AX_HW_RPT_FWD, 0);
  51. } else {
  52. ret = rtw89_mac_resize_ple_rx_quota(rtwdev, false);
  53. if (ret) {
  54. rtw89_err(rtwdev, "[ERR]patch rx qta %d\n", ret);
  55. return ret;
  56. }
  57. rtw89_write32_clr(rtwdev, R_AX_RX_FUNCTION_STOP, B_AX_HDR_RX_STOP);
  58. rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
  59. rtw89_write32(rtwdev, R_AX_ACTION_FWD0, TRXCFG_MPDU_PROC_ACT_FRWD);
  60. rtw89_write32(rtwdev, R_AX_TF_FWD, TRXCFG_MPDU_PROC_TF_FRWD);
  61. }
  62. return 0;
  63. }
  64. static void rtw89_wow_set_rx_filter(struct rtw89_dev *rtwdev, bool enable)
  65. {
  66. enum rtw89_mac_fwd_target fwd_target = enable ?
  67. RTW89_FWD_DONT_CARE :
  68. RTW89_FWD_TO_HOST;
  69. rtw89_mac_typ_fltr_opt(rtwdev, RTW89_MGNT, fwd_target, RTW89_MAC_0);
  70. rtw89_mac_typ_fltr_opt(rtwdev, RTW89_CTRL, fwd_target, RTW89_MAC_0);
  71. rtw89_mac_typ_fltr_opt(rtwdev, RTW89_DATA, fwd_target, RTW89_MAC_0);
  72. }
  73. static void rtw89_wow_show_wakeup_reason(struct rtw89_dev *rtwdev)
  74. {
  75. enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
  76. struct cfg80211_wowlan_nd_info nd_info;
  77. struct cfg80211_wowlan_wakeup wakeup = {
  78. .pattern_idx = -1,
  79. };
  80. u32 wow_reason_reg;
  81. u8 reason;
  82. if (chip_id == RTL8852A || chip_id == RTL8852B || chip_id == RTL8851B)
  83. wow_reason_reg = R_AX_C2HREG_DATA3 + 3;
  84. else
  85. wow_reason_reg = R_AX_C2HREG_DATA3_V1 + 3;
  86. reason = rtw89_read8(rtwdev, wow_reason_reg);
  87. switch (reason) {
  88. case RTW89_WOW_RSN_RX_DEAUTH:
  89. wakeup.disconnect = true;
  90. rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx deauth\n");
  91. break;
  92. case RTW89_WOW_RSN_DISCONNECT:
  93. wakeup.disconnect = true;
  94. rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: AP is off\n");
  95. break;
  96. case RTW89_WOW_RSN_RX_MAGIC_PKT:
  97. wakeup.magic_pkt = true;
  98. rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx magic packet\n");
  99. break;
  100. case RTW89_WOW_RSN_RX_GTK_REKEY:
  101. wakeup.gtk_rekey_failure = true;
  102. rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx gtk rekey\n");
  103. break;
  104. case RTW89_WOW_RSN_RX_PATTERN_MATCH:
  105. /* Current firmware and driver don't report pattern index
  106. * Use pattern_idx to 0 defaultly.
  107. */
  108. wakeup.pattern_idx = 0;
  109. rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx pattern match packet\n");
  110. break;
  111. case RTW89_WOW_RSN_RX_NLO:
  112. /* Current firmware and driver don't report ssid index.
  113. * Use 0 for n_matches based on its comment.
  114. */
  115. nd_info.n_matches = 0;
  116. wakeup.net_detect = &nd_info;
  117. rtw89_debug(rtwdev, RTW89_DBG_WOW, "Rx NLO\n");
  118. break;
  119. default:
  120. rtw89_warn(rtwdev, "Unknown wakeup reason %x\n", reason);
  121. ieee80211_report_wowlan_wakeup(rtwdev->wow.wow_vif, NULL,
  122. GFP_KERNEL);
  123. return;
  124. }
  125. ieee80211_report_wowlan_wakeup(rtwdev->wow.wow_vif, &wakeup,
  126. GFP_KERNEL);
  127. }
  128. static void rtw89_wow_vif_iter(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
  129. {
  130. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  131. struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
  132. /* Current wowlan function support setting of only one STATION vif.
  133. * So when one suitable vif is found, stop the iteration.
  134. */
  135. if (rtw_wow->wow_vif || vif->type != NL80211_IFTYPE_STATION)
  136. return;
  137. switch (rtwvif->net_type) {
  138. case RTW89_NET_TYPE_INFRA:
  139. rtw_wow->wow_vif = vif;
  140. break;
  141. case RTW89_NET_TYPE_NO_LINK:
  142. default:
  143. break;
  144. }
  145. }
  146. static u16 __rtw89_cal_crc16(u8 data, u16 crc)
  147. {
  148. u8 shift_in, data_bit;
  149. u8 crc_bit4, crc_bit11, crc_bit15;
  150. u16 crc_result;
  151. int index;
  152. for (index = 0; index < 8; index++) {
  153. crc_bit15 = crc & BIT(15) ? 1 : 0;
  154. data_bit = data & BIT(index) ? 1 : 0;
  155. shift_in = crc_bit15 ^ data_bit;
  156. crc_result = crc << 1;
  157. if (shift_in == 0)
  158. crc_result &= ~BIT(0);
  159. else
  160. crc_result |= BIT(0);
  161. crc_bit11 = (crc & BIT(11) ? 1 : 0) ^ shift_in;
  162. if (crc_bit11 == 0)
  163. crc_result &= ~BIT(12);
  164. else
  165. crc_result |= BIT(12);
  166. crc_bit4 = (crc & BIT(4) ? 1 : 0) ^ shift_in;
  167. if (crc_bit4 == 0)
  168. crc_result &= ~BIT(5);
  169. else
  170. crc_result |= BIT(5);
  171. crc = crc_result;
  172. }
  173. return crc;
  174. }
  175. static u16 rtw89_calc_crc(u8 *pdata, int length)
  176. {
  177. u16 crc = 0xffff;
  178. int i;
  179. for (i = 0; i < length; i++)
  180. crc = __rtw89_cal_crc16(pdata[i], crc);
  181. /* get 1' complement */
  182. return ~crc;
  183. }
  184. static int rtw89_wow_pattern_get_type(struct rtw89_vif *rtwvif,
  185. struct rtw89_wow_cam_info *rtw_pattern,
  186. const u8 *pattern, u8 da_mask)
  187. {
  188. u8 da[ETH_ALEN];
  189. ether_addr_copy_mask(da, pattern, da_mask);
  190. /* Each pattern is divided into different kinds by DA address
  191. * a. DA is broadcast address: set bc = 0;
  192. * b. DA is multicast address: set mc = 0
  193. * c. DA is unicast address same as dev's mac address: set uc = 0
  194. * d. DA is unmasked. Also called wildcard type: set uc = bc = mc = 0
  195. * e. Others is invalid type.
  196. */
  197. if (is_broadcast_ether_addr(da))
  198. rtw_pattern->bc = true;
  199. else if (is_multicast_ether_addr(da))
  200. rtw_pattern->mc = true;
  201. else if (ether_addr_equal(da, rtwvif->mac_addr) &&
  202. da_mask == GENMASK(5, 0))
  203. rtw_pattern->uc = true;
  204. else if (!da_mask) /*da_mask == 0 mean wildcard*/
  205. return 0;
  206. else
  207. return -EPERM;
  208. return 0;
  209. }
  210. static int rtw89_wow_pattern_generate(struct rtw89_dev *rtwdev,
  211. struct rtw89_vif *rtwvif,
  212. const struct cfg80211_pkt_pattern *pkt_pattern,
  213. struct rtw89_wow_cam_info *rtw_pattern)
  214. {
  215. u8 mask_hw[RTW89_MAX_PATTERN_MASK_SIZE * 4] = {0};
  216. u8 content[RTW89_MAX_PATTERN_SIZE] = {0};
  217. const u8 *mask;
  218. const u8 *pattern;
  219. u8 mask_len;
  220. u16 count;
  221. u32 len;
  222. int i, ret;
  223. pattern = pkt_pattern->pattern;
  224. len = pkt_pattern->pattern_len;
  225. mask = pkt_pattern->mask;
  226. mask_len = DIV_ROUND_UP(len, 8);
  227. memset(rtw_pattern, 0, sizeof(*rtw_pattern));
  228. ret = rtw89_wow_pattern_get_type(rtwvif, rtw_pattern, pattern,
  229. mask[0] & GENMASK(5, 0));
  230. if (ret)
  231. return ret;
  232. /* translate mask from os to mask for hw
  233. * pattern from OS uses 'ethenet frame', like this:
  234. * | 6 | 6 | 2 | 20 | Variable | 4 |
  235. * |--------+--------+------+-----------+------------+-----|
  236. * | 802.3 Mac Header | IP Header | TCP Packet | FCS |
  237. * | DA | SA | Type |
  238. *
  239. * BUT, packet catched by our HW is in '802.11 frame', begin from LLC
  240. * | 24 or 30 | 6 | 2 | 20 | Variable | 4 |
  241. * |-------------------+--------+------+-----------+------------+-----|
  242. * | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS |
  243. * | Others | Tpye |
  244. *
  245. * Therefore, we need translate mask_from_OS to mask_to_hw.
  246. * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
  247. * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
  248. * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
  249. */
  250. /* Shift 6 bits */
  251. for (i = 0; i < mask_len - 1; i++) {
  252. mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6)) |
  253. u8_get_bits(mask[i + 1], GENMASK(5, 0)) << 2;
  254. }
  255. mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6));
  256. /* Set bit 0-5 to zero */
  257. mask_hw[0] &= ~GENMASK(5, 0);
  258. memcpy(rtw_pattern->mask, mask_hw, sizeof(rtw_pattern->mask));
  259. /* To get the wake up pattern from the mask.
  260. * We do not count first 12 bits which means
  261. * DA[6] and SA[6] in the pattern to match HW design.
  262. */
  263. count = 0;
  264. for (i = 12; i < len; i++) {
  265. if ((mask[i / 8] >> (i % 8)) & 0x01) {
  266. content[count] = pattern[i];
  267. count++;
  268. }
  269. }
  270. rtw_pattern->crc = rtw89_calc_crc(content, count);
  271. return 0;
  272. }
  273. static int rtw89_wow_parse_patterns(struct rtw89_dev *rtwdev,
  274. struct rtw89_vif *rtwvif,
  275. struct cfg80211_wowlan *wowlan)
  276. {
  277. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  278. struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
  279. int i;
  280. int ret;
  281. if (!wowlan->n_patterns || !wowlan->patterns)
  282. return 0;
  283. for (i = 0; i < wowlan->n_patterns; i++) {
  284. rtw_pattern = &rtw_wow->patterns[i];
  285. ret = rtw89_wow_pattern_generate(rtwdev, rtwvif,
  286. &wowlan->patterns[i],
  287. rtw_pattern);
  288. if (ret) {
  289. rtw89_err(rtwdev, "failed to generate pattern(%d)\n", i);
  290. rtw_wow->pattern_cnt = 0;
  291. return ret;
  292. }
  293. rtw_pattern->r_w = true;
  294. rtw_pattern->idx = i;
  295. rtw_pattern->negative_pattern_match = false;
  296. rtw_pattern->skip_mac_hdr = true;
  297. rtw_pattern->valid = true;
  298. }
  299. rtw_wow->pattern_cnt = wowlan->n_patterns;
  300. return 0;
  301. }
  302. static void rtw89_wow_pattern_clear_cam(struct rtw89_dev *rtwdev)
  303. {
  304. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  305. struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
  306. int i = 0;
  307. for (i = 0; i < rtw_wow->pattern_cnt; i++) {
  308. rtw_pattern = &rtw_wow->patterns[i];
  309. rtw_pattern->valid = false;
  310. rtw89_fw_wow_cam_update(rtwdev, rtw_pattern);
  311. }
  312. }
  313. static void rtw89_wow_pattern_write(struct rtw89_dev *rtwdev)
  314. {
  315. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  316. struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
  317. int i;
  318. for (i = 0; i < rtw_wow->pattern_cnt; i++)
  319. rtw89_fw_wow_cam_update(rtwdev, rtw_pattern + i);
  320. }
  321. static void rtw89_wow_pattern_clear(struct rtw89_dev *rtwdev)
  322. {
  323. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  324. rtw89_wow_pattern_clear_cam(rtwdev);
  325. rtw_wow->pattern_cnt = 0;
  326. memset(rtw_wow->patterns, 0, sizeof(rtw_wow->patterns));
  327. }
  328. static void rtw89_wow_clear_wakeups(struct rtw89_dev *rtwdev)
  329. {
  330. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  331. rtw_wow->wow_vif = NULL;
  332. rtw89_core_release_all_bits_map(rtw_wow->flags, RTW89_WOW_FLAG_NUM);
  333. rtw_wow->pattern_cnt = 0;
  334. }
  335. static int rtw89_wow_set_wakeups(struct rtw89_dev *rtwdev,
  336. struct cfg80211_wowlan *wowlan)
  337. {
  338. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  339. struct rtw89_vif *rtwvif;
  340. if (wowlan->disconnect)
  341. set_bit(RTW89_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags);
  342. if (wowlan->magic_pkt)
  343. set_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags);
  344. rtw89_for_each_rtwvif(rtwdev, rtwvif)
  345. rtw89_wow_vif_iter(rtwdev, rtwvif);
  346. if (!rtw_wow->wow_vif)
  347. return -EPERM;
  348. rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv;
  349. return rtw89_wow_parse_patterns(rtwdev, rtwvif, wowlan);
  350. }
  351. static int rtw89_wow_cfg_wake(struct rtw89_dev *rtwdev, bool wow)
  352. {
  353. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  354. struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
  355. struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
  356. struct ieee80211_sta *wow_sta;
  357. struct rtw89_sta *rtwsta = NULL;
  358. int ret;
  359. wow_sta = ieee80211_find_sta(wow_vif, rtwvif->bssid);
  360. if (wow_sta)
  361. rtwsta = (struct rtw89_sta *)wow_sta->drv_priv;
  362. if (wow) {
  363. if (rtw_wow->pattern_cnt)
  364. rtwvif->wowlan_pattern = true;
  365. if (test_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
  366. rtwvif->wowlan_magic = true;
  367. } else {
  368. rtwvif->wowlan_pattern = false;
  369. rtwvif->wowlan_magic = false;
  370. }
  371. ret = rtw89_fw_h2c_wow_wakeup_ctrl(rtwdev, rtwvif, wow);
  372. if (ret) {
  373. rtw89_err(rtwdev, "failed to fw wow wakeup ctrl\n");
  374. return ret;
  375. }
  376. if (wow) {
  377. ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif, rtwsta);
  378. if (ret) {
  379. rtw89_err(rtwdev, "failed to update dctl cam sec entry: %d\n",
  380. ret);
  381. return ret;
  382. }
  383. }
  384. ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
  385. if (ret) {
  386. rtw89_warn(rtwdev, "failed to send h2c cam\n");
  387. return ret;
  388. }
  389. ret = rtw89_fw_h2c_wow_global(rtwdev, rtwvif, wow);
  390. if (ret) {
  391. rtw89_err(rtwdev, "failed to fw wow global\n");
  392. return ret;
  393. }
  394. return 0;
  395. }
  396. static int rtw89_wow_check_fw_status(struct rtw89_dev *rtwdev, bool wow_enable)
  397. {
  398. u8 polling;
  399. int ret;
  400. ret = read_poll_timeout_atomic(rtw89_read8_mask, polling,
  401. wow_enable == !!polling,
  402. 50, 50000, false, rtwdev,
  403. R_AX_WOW_CTRL, B_AX_WOW_WOWEN);
  404. if (ret)
  405. rtw89_err(rtwdev, "failed to check wow status %s\n",
  406. wow_enable ? "enabled" : "disabled");
  407. return ret;
  408. }
  409. static int rtw89_wow_swap_fw(struct rtw89_dev *rtwdev, bool wow)
  410. {
  411. enum rtw89_fw_type fw_type = wow ? RTW89_FW_WOWLAN : RTW89_FW_NORMAL;
  412. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  413. struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
  414. struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
  415. const struct rtw89_chip_info *chip = rtwdev->chip;
  416. bool include_bb = !!chip->bbmcu_nr;
  417. struct ieee80211_sta *wow_sta;
  418. struct rtw89_sta *rtwsta = NULL;
  419. bool is_conn = true;
  420. int ret;
  421. rtw89_hci_disable_intr(rtwdev);
  422. wow_sta = ieee80211_find_sta(wow_vif, rtwvif->bssid);
  423. if (wow_sta)
  424. rtwsta = (struct rtw89_sta *)wow_sta->drv_priv;
  425. else
  426. is_conn = false;
  427. ret = rtw89_fw_download(rtwdev, fw_type, include_bb);
  428. if (ret) {
  429. rtw89_warn(rtwdev, "download fw failed\n");
  430. return ret;
  431. }
  432. rtw89_phy_init_rf_reg(rtwdev, true);
  433. ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
  434. RTW89_ROLE_FW_RESTORE);
  435. if (ret) {
  436. rtw89_warn(rtwdev, "failed to send h2c role maintain\n");
  437. return ret;
  438. }
  439. ret = rtw89_fw_h2c_assoc_cmac_tbl(rtwdev, wow_vif, wow_sta);
  440. if (ret) {
  441. rtw89_warn(rtwdev, "failed to send h2c assoc cmac tbl\n");
  442. return ret;
  443. }
  444. if (!is_conn)
  445. rtw89_cam_reset_keys(rtwdev);
  446. ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, !is_conn);
  447. if (ret) {
  448. rtw89_warn(rtwdev, "failed to send h2c join info\n");
  449. return ret;
  450. }
  451. ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
  452. if (ret) {
  453. rtw89_warn(rtwdev, "failed to send h2c cam\n");
  454. return ret;
  455. }
  456. if (is_conn) {
  457. ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwvif, rtwsta->mac_id);
  458. if (ret) {
  459. rtw89_warn(rtwdev, "failed to send h2c general packet\n");
  460. return ret;
  461. }
  462. rtw89_phy_ra_assoc(rtwdev, wow_sta);
  463. rtw89_phy_set_bss_color(rtwdev, wow_vif);
  464. rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, wow_vif);
  465. }
  466. rtw89_mac_hw_mgnt_sec(rtwdev, wow);
  467. rtw89_hci_enable_intr(rtwdev);
  468. return 0;
  469. }
  470. static int rtw89_wow_enable_trx_pre(struct rtw89_dev *rtwdev)
  471. {
  472. int ret;
  473. rtw89_hci_ctrl_txdma_ch(rtwdev, false);
  474. rtw89_hci_ctrl_txdma_fw_ch(rtwdev, true);
  475. rtw89_mac_ptk_drop_by_band_and_wait(rtwdev, RTW89_MAC_0);
  476. ret = rtw89_hci_poll_txdma_ch(rtwdev);
  477. if (ret) {
  478. rtw89_err(rtwdev, "txdma ch busy\n");
  479. return ret;
  480. }
  481. rtw89_wow_set_rx_filter(rtwdev, true);
  482. ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, false);
  483. if (ret) {
  484. rtw89_err(rtwdev, "cfg ppdu status\n");
  485. return ret;
  486. }
  487. return 0;
  488. }
  489. static int rtw89_wow_enable_trx_post(struct rtw89_dev *rtwdev)
  490. {
  491. int ret;
  492. rtw89_hci_disable_intr(rtwdev);
  493. rtw89_hci_ctrl_trxhci(rtwdev, false);
  494. ret = rtw89_hci_poll_txdma_ch(rtwdev);
  495. if (ret) {
  496. rtw89_err(rtwdev, "failed to poll txdma ch idle pcie\n");
  497. return ret;
  498. }
  499. ret = rtw89_wow_config_mac(rtwdev, true);
  500. if (ret) {
  501. rtw89_err(rtwdev, "failed to config mac\n");
  502. return ret;
  503. }
  504. rtw89_wow_set_rx_filter(rtwdev, false);
  505. rtw89_hci_reset(rtwdev);
  506. return 0;
  507. }
  508. static int rtw89_wow_disable_trx_pre(struct rtw89_dev *rtwdev)
  509. {
  510. int ret;
  511. rtw89_hci_clr_idx_all(rtwdev);
  512. ret = rtw89_hci_rst_bdram(rtwdev);
  513. if (ret) {
  514. rtw89_warn(rtwdev, "reset bdram busy\n");
  515. return ret;
  516. }
  517. rtw89_hci_ctrl_trxhci(rtwdev, true);
  518. rtw89_hci_ctrl_txdma_ch(rtwdev, true);
  519. ret = rtw89_wow_config_mac(rtwdev, false);
  520. if (ret) {
  521. rtw89_err(rtwdev, "failed to config mac\n");
  522. return ret;
  523. }
  524. rtw89_hci_enable_intr(rtwdev);
  525. return 0;
  526. }
  527. static int rtw89_wow_disable_trx_post(struct rtw89_dev *rtwdev)
  528. {
  529. int ret;
  530. ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
  531. if (ret)
  532. rtw89_err(rtwdev, "cfg ppdu status\n");
  533. return ret;
  534. }
  535. static int rtw89_wow_fw_start(struct rtw89_dev *rtwdev)
  536. {
  537. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  538. struct rtw89_vif *rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv;
  539. int ret;
  540. rtw89_wow_pattern_write(rtwdev);
  541. ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif, true);
  542. if (ret) {
  543. rtw89_err(rtwdev, "wow: failed to enable keep alive\n");
  544. return ret;
  545. }
  546. ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif, true);
  547. if (ret) {
  548. rtw89_err(rtwdev, "wow: failed to enable disconnect detect\n");
  549. goto out;
  550. }
  551. ret = rtw89_wow_cfg_wake(rtwdev, true);
  552. if (ret) {
  553. rtw89_err(rtwdev, "wow: failed to config wake\n");
  554. goto out;
  555. }
  556. ret = rtw89_wow_check_fw_status(rtwdev, true);
  557. if (ret) {
  558. rtw89_err(rtwdev, "wow: failed to check enable fw ready\n");
  559. goto out;
  560. }
  561. out:
  562. return ret;
  563. }
  564. static int rtw89_wow_fw_stop(struct rtw89_dev *rtwdev)
  565. {
  566. struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
  567. struct rtw89_vif *rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv;
  568. int ret;
  569. rtw89_wow_pattern_clear(rtwdev);
  570. ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif, false);
  571. if (ret) {
  572. rtw89_err(rtwdev, "wow: failed to disable keep alive\n");
  573. goto out;
  574. }
  575. ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif, false);
  576. if (ret) {
  577. rtw89_err(rtwdev, "wow: failed to disable disconnect detect\n");
  578. goto out;
  579. }
  580. ret = rtw89_wow_cfg_wake(rtwdev, false);
  581. if (ret) {
  582. rtw89_err(rtwdev, "wow: failed to disable config wake\n");
  583. goto out;
  584. }
  585. rtw89_fw_release_general_pkt_list(rtwdev, true);
  586. ret = rtw89_wow_check_fw_status(rtwdev, false);
  587. if (ret) {
  588. rtw89_err(rtwdev, "wow: failed to check disable fw ready\n");
  589. goto out;
  590. }
  591. out:
  592. return ret;
  593. }
  594. static int rtw89_wow_enable(struct rtw89_dev *rtwdev)
  595. {
  596. int ret;
  597. set_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
  598. ret = rtw89_wow_enable_trx_pre(rtwdev);
  599. if (ret) {
  600. rtw89_err(rtwdev, "wow: failed to enable trx_pre\n");
  601. goto out;
  602. }
  603. rtw89_fw_release_general_pkt_list(rtwdev, true);
  604. ret = rtw89_wow_swap_fw(rtwdev, true);
  605. if (ret) {
  606. rtw89_err(rtwdev, "wow: failed to swap to wow fw\n");
  607. goto out;
  608. }
  609. ret = rtw89_wow_fw_start(rtwdev);
  610. if (ret) {
  611. rtw89_err(rtwdev, "wow: failed to let wow fw start\n");
  612. goto out;
  613. }
  614. rtw89_wow_enter_lps(rtwdev);
  615. ret = rtw89_wow_enable_trx_post(rtwdev);
  616. if (ret) {
  617. rtw89_err(rtwdev, "wow: failed to enable trx_post\n");
  618. goto out;
  619. }
  620. return 0;
  621. out:
  622. clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
  623. return ret;
  624. }
  625. static int rtw89_wow_disable(struct rtw89_dev *rtwdev)
  626. {
  627. int ret;
  628. ret = rtw89_wow_disable_trx_pre(rtwdev);
  629. if (ret) {
  630. rtw89_err(rtwdev, "wow: failed to disable trx_pre\n");
  631. goto out;
  632. }
  633. rtw89_wow_leave_lps(rtwdev);
  634. ret = rtw89_wow_fw_stop(rtwdev);
  635. if (ret) {
  636. rtw89_err(rtwdev, "wow: failed to swap to normal fw\n");
  637. goto out;
  638. }
  639. ret = rtw89_wow_swap_fw(rtwdev, false);
  640. if (ret) {
  641. rtw89_err(rtwdev, "wow: failed to disable trx_post\n");
  642. goto out;
  643. }
  644. ret = rtw89_wow_disable_trx_post(rtwdev);
  645. if (ret) {
  646. rtw89_err(rtwdev, "wow: failed to disable trx_pre\n");
  647. goto out;
  648. }
  649. out:
  650. clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
  651. return ret;
  652. }
  653. int rtw89_wow_resume(struct rtw89_dev *rtwdev)
  654. {
  655. int ret;
  656. if (!test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags)) {
  657. rtw89_err(rtwdev, "wow is not enabled\n");
  658. ret = -EPERM;
  659. goto out;
  660. }
  661. if (!rtw89_mac_get_power_state(rtwdev)) {
  662. rtw89_err(rtwdev, "chip is no power when resume\n");
  663. ret = -EPERM;
  664. goto out;
  665. }
  666. rtw89_wow_leave_deep_ps(rtwdev);
  667. rtw89_wow_show_wakeup_reason(rtwdev);
  668. ret = rtw89_wow_disable(rtwdev);
  669. if (ret)
  670. rtw89_err(rtwdev, "failed to disable wow\n");
  671. out:
  672. rtw89_wow_clear_wakeups(rtwdev);
  673. return ret;
  674. }
  675. int rtw89_wow_suspend(struct rtw89_dev *rtwdev, struct cfg80211_wowlan *wowlan)
  676. {
  677. int ret;
  678. ret = rtw89_wow_set_wakeups(rtwdev, wowlan);
  679. if (ret) {
  680. rtw89_err(rtwdev, "failed to set wakeup event\n");
  681. return ret;
  682. }
  683. rtw89_wow_leave_lps(rtwdev);
  684. ret = rtw89_wow_enable(rtwdev);
  685. if (ret) {
  686. rtw89_err(rtwdev, "failed to enable wow\n");
  687. return ret;
  688. }
  689. rtw89_wow_enter_deep_ps(rtwdev);
  690. return 0;
  691. }