treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wireless / realtek / rtw88 / wow.c
blobaf5c27e1bb07b368cc94a6a17f4845316f9b8bcf
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019 Realtek Corporation
3 */
5 #include "main.h"
6 #include "fw.h"
7 #include "wow.h"
8 #include "reg.h"
9 #include "debug.h"
10 #include "mac.h"
11 #include "ps.h"
13 static void rtw_wow_show_wakeup_reason(struct rtw_dev *rtwdev)
15 u8 reason;
17 reason = rtw_read8(rtwdev, REG_WOWLAN_WAKE_REASON);
19 if (reason == RTW_WOW_RSN_RX_DEAUTH)
20 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: Rx deauth\n");
21 else if (reason == RTW_WOW_RSN_DISCONNECT)
22 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: AP is off\n");
23 else if (reason == RTW_WOW_RSN_RX_MAGIC_PKT)
24 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: Rx magic packet\n");
25 else if (reason == RTW_WOW_RSN_RX_GTK_REKEY)
26 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: Rx gtk rekey\n");
27 else if (reason == RTW_WOW_RSN_RX_PTK_REKEY)
28 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: Rx ptk rekey\n");
29 else if (reason == RTW_WOW_RSN_RX_PATTERN_MATCH)
30 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: Rx pattern match packet\n");
31 else if (reason == RTW_WOW_RSN_RX_NLO)
32 rtw_dbg(rtwdev, RTW_DBG_WOW, "Rx NLO\n");
33 else
34 rtw_warn(rtwdev, "Unknown wakeup reason %x\n", reason);
37 static void rtw_wow_pattern_write_cam(struct rtw_dev *rtwdev, u8 addr,
38 u32 wdata)
40 rtw_write32(rtwdev, REG_WKFMCAM_RWD, wdata);
41 rtw_write32(rtwdev, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1 |
42 BIT_WKFCAM_WE | BIT_WKFCAM_ADDR_V2(addr));
44 if (!check_hw_ready(rtwdev, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1, 0))
45 rtw_err(rtwdev, "failed to write pattern cam\n");
48 static void rtw_wow_pattern_write_cam_ent(struct rtw_dev *rtwdev, u8 id,
49 struct rtw_wow_pattern *rtw_pattern)
51 int i;
52 u8 addr;
53 u32 wdata;
55 for (i = 0; i < RTW_MAX_PATTERN_MASK_SIZE / 4; i++) {
56 addr = (id << 3) + i;
57 wdata = rtw_pattern->mask[i * 4];
58 wdata |= rtw_pattern->mask[i * 4 + 1] << 8;
59 wdata |= rtw_pattern->mask[i * 4 + 2] << 16;
60 wdata |= rtw_pattern->mask[i * 4 + 3] << 24;
61 rtw_wow_pattern_write_cam(rtwdev, addr, wdata);
64 wdata = rtw_pattern->crc;
65 addr = (id << 3) + RTW_MAX_PATTERN_MASK_SIZE / 4;
67 switch (rtw_pattern->type) {
68 case RTW_PATTERN_BROADCAST:
69 wdata |= BIT_WKFMCAM_BC | BIT_WKFMCAM_VALID;
70 break;
71 case RTW_PATTERN_MULTICAST:
72 wdata |= BIT_WKFMCAM_MC | BIT_WKFMCAM_VALID;
73 break;
74 case RTW_PATTERN_UNICAST:
75 wdata |= BIT_WKFMCAM_UC | BIT_WKFMCAM_VALID;
76 break;
77 default:
78 break;
80 rtw_wow_pattern_write_cam(rtwdev, addr, wdata);
83 /* RTK internal CRC16 for Pattern Cam */
84 static u16 __rtw_cal_crc16(u8 data, u16 crc)
86 u8 shift_in, data_bit;
87 u8 crc_bit4, crc_bit11, crc_bit15;
88 u16 crc_result;
89 int index;
91 for (index = 0; index < 8; index++) {
92 crc_bit15 = ((crc & BIT(15)) ? 1 : 0);
93 data_bit = (data & (BIT(0) << index) ? 1 : 0);
94 shift_in = crc_bit15 ^ data_bit;
96 crc_result = crc << 1;
98 if (shift_in == 0)
99 crc_result &= (~BIT(0));
100 else
101 crc_result |= BIT(0);
103 crc_bit11 = ((crc & BIT(11)) ? 1 : 0) ^ shift_in;
105 if (crc_bit11 == 0)
106 crc_result &= (~BIT(12));
107 else
108 crc_result |= BIT(12);
110 crc_bit4 = ((crc & BIT(4)) ? 1 : 0) ^ shift_in;
112 if (crc_bit4 == 0)
113 crc_result &= (~BIT(5));
114 else
115 crc_result |= BIT(5);
117 crc = crc_result;
119 return crc;
122 static u16 rtw_calc_crc(u8 *pdata, int length)
124 u16 crc = 0xffff;
125 int i;
127 for (i = 0; i < length; i++)
128 crc = __rtw_cal_crc16(pdata[i], crc);
130 /* get 1' complement */
131 return ~crc;
134 static void rtw_wow_pattern_generate(struct rtw_dev *rtwdev,
135 struct rtw_vif *rtwvif,
136 const struct cfg80211_pkt_pattern *pkt_pattern,
137 struct rtw_wow_pattern *rtw_pattern)
139 const u8 *mask;
140 const u8 *pattern;
141 u8 mask_hw[RTW_MAX_PATTERN_MASK_SIZE] = {0};
142 u8 content[RTW_MAX_PATTERN_SIZE] = {0};
143 u8 mac_addr[ETH_ALEN] = {0};
144 u8 mask_len;
145 u16 count;
146 int len;
147 int i;
149 pattern = pkt_pattern->pattern;
150 len = pkt_pattern->pattern_len;
151 mask = pkt_pattern->mask;
153 ether_addr_copy(mac_addr, rtwvif->mac_addr);
154 memset(rtw_pattern, 0, sizeof(*rtw_pattern));
156 mask_len = DIV_ROUND_UP(len, 8);
158 if (is_broadcast_ether_addr(pattern))
159 rtw_pattern->type = RTW_PATTERN_BROADCAST;
160 else if (is_multicast_ether_addr(pattern))
161 rtw_pattern->type = RTW_PATTERN_MULTICAST;
162 else if (ether_addr_equal(pattern, mac_addr))
163 rtw_pattern->type = RTW_PATTERN_UNICAST;
164 else
165 rtw_pattern->type = RTW_PATTERN_INVALID;
167 /* translate mask from os to mask for hw
168 * pattern from OS uses 'ethenet frame', like this:
169 * | 6 | 6 | 2 | 20 | Variable | 4 |
170 * |--------+--------+------+-----------+------------+-----|
171 * | 802.3 Mac Header | IP Header | TCP Packet | FCS |
172 * | DA | SA | Type |
174 * BUT, packet catched by our HW is in '802.11 frame', begin from LLC
175 * | 24 or 30 | 6 | 2 | 20 | Variable | 4 |
176 * |-------------------+--------+------+-----------+------------+-----|
177 * | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS |
178 * | Others | Tpye |
180 * Therefore, we need translate mask_from_OS to mask_to_hw.
181 * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
182 * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
183 * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
186 /* Shift 6 bits */
187 for (i = 0; i < mask_len - 1; i++) {
188 mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6));
189 mask_hw[i] |= u8_get_bits(mask[i + 1], GENMASK(5, 0)) << 2;
191 mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6));
193 /* Set bit 0-5 to zero */
194 mask_hw[0] &= (~GENMASK(5, 0));
196 memcpy(rtw_pattern->mask, mask_hw, RTW_MAX_PATTERN_MASK_SIZE);
198 /* To get the wake up pattern from the mask.
199 * We do not count first 12 bits which means
200 * DA[6] and SA[6] in the pattern to match HW design.
202 count = 0;
203 for (i = 12; i < len; i++) {
204 if ((mask[i / 8] >> (i % 8)) & 0x01) {
205 content[count] = pattern[i];
206 count++;
210 rtw_pattern->crc = rtw_calc_crc(content, count);
213 static void rtw_wow_pattern_clear_cam(struct rtw_dev *rtwdev)
215 bool ret;
217 rtw_write32(rtwdev, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1 |
218 BIT_WKFCAM_CLR_V1);
220 ret = check_hw_ready(rtwdev, REG_WKFMCAM_CMD, BIT_WKFCAM_POLLING_V1, 0);
221 if (!ret)
222 rtw_err(rtwdev, "failed to clean pattern cam\n");
225 static void rtw_wow_pattern_write(struct rtw_dev *rtwdev)
227 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
228 struct rtw_wow_pattern *rtw_pattern = rtw_wow->patterns;
229 int i = 0;
231 for (i = 0; i < rtw_wow->pattern_cnt; i++)
232 rtw_wow_pattern_write_cam_ent(rtwdev, i, rtw_pattern + i);
235 static void rtw_wow_pattern_clear(struct rtw_dev *rtwdev)
237 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
239 rtw_wow_pattern_clear_cam(rtwdev);
241 rtw_wow->pattern_cnt = 0;
242 memset(rtw_wow->patterns, 0, sizeof(rtw_wow->patterns));
245 static void rtw_wow_bb_stop(struct rtw_dev *rtwdev)
247 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
249 /* wait 100ms for firmware to finish TX */
250 msleep(100);
252 if (!rtw_read32_mask(rtwdev, REG_BCNQ_INFO, BIT_MGQ_CPU_EMPTY))
253 rtw_warn(rtwdev, "Wrong status of MGQ_CPU empty!\n");
255 rtw_wow->txpause = rtw_read8(rtwdev, REG_TXPAUSE);
256 rtw_write8(rtwdev, REG_TXPAUSE, 0xff);
257 rtw_write8_clr(rtwdev, REG_SYS_FUNC_EN, BIT_FEN_BB_RSTB);
260 static void rtw_wow_bb_start(struct rtw_dev *rtwdev)
262 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
264 rtw_write8_set(rtwdev, REG_SYS_FUNC_EN, BIT_FEN_BB_RSTB);
265 rtw_write8(rtwdev, REG_TXPAUSE, rtw_wow->txpause);
268 static void rtw_wow_rx_dma_stop(struct rtw_dev *rtwdev)
270 /* wait 100ms for HW to finish rx dma */
271 msleep(100);
273 rtw_write32_set(rtwdev, REG_RXPKT_NUM, BIT_RW_RELEASE);
275 if (!check_hw_ready(rtwdev, REG_RXPKT_NUM, BIT_RXDMA_IDLE, 1))
276 rtw_err(rtwdev, "failed to stop rx dma\n");
279 static void rtw_wow_rx_dma_start(struct rtw_dev *rtwdev)
281 rtw_write32_clr(rtwdev, REG_RXPKT_NUM, BIT_RW_RELEASE);
284 static bool rtw_wow_check_fw_status(struct rtw_dev *rtwdev, bool wow_enable)
286 bool ret;
288 /* wait 100ms for wow firmware to finish work */
289 msleep(100);
291 if (wow_enable) {
292 if (!rtw_read8(rtwdev, REG_WOWLAN_WAKE_REASON))
293 ret = 0;
294 } else {
295 if (rtw_read32_mask(rtwdev, REG_FE1IMR, BIT_FS_RXDONE) == 0 &&
296 rtw_read32_mask(rtwdev, REG_RXPKT_NUM, BIT_RW_RELEASE) == 0)
297 ret = 0;
300 if (ret)
301 rtw_err(rtwdev, "failed to check wow status %s\n",
302 wow_enable ? "enabled" : "disabled");
304 return ret;
307 static void rtw_wow_fw_security_type_iter(struct ieee80211_hw *hw,
308 struct ieee80211_vif *vif,
309 struct ieee80211_sta *sta,
310 struct ieee80211_key_conf *key,
311 void *data)
313 struct rtw_fw_key_type_iter_data *iter_data = data;
314 struct rtw_dev *rtwdev = hw->priv;
315 u8 hw_key_type;
317 if (vif != rtwdev->wow.wow_vif)
318 return;
320 switch (key->cipher) {
321 case WLAN_CIPHER_SUITE_WEP40:
322 hw_key_type = RTW_CAM_WEP40;
323 break;
324 case WLAN_CIPHER_SUITE_WEP104:
325 hw_key_type = RTW_CAM_WEP104;
326 break;
327 case WLAN_CIPHER_SUITE_TKIP:
328 hw_key_type = RTW_CAM_TKIP;
329 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
330 break;
331 case WLAN_CIPHER_SUITE_CCMP:
332 hw_key_type = RTW_CAM_AES;
333 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
334 break;
335 default:
336 rtw_err(rtwdev, "Unsupported key type for wowlan mode\n");
337 hw_key_type = 0;
338 break;
341 if (sta)
342 iter_data->pairwise_key_type = hw_key_type;
343 else
344 iter_data->group_key_type = hw_key_type;
347 static void rtw_wow_fw_security_type(struct rtw_dev *rtwdev)
349 struct rtw_fw_key_type_iter_data data = {};
350 struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
352 data.rtwdev = rtwdev;
353 rtw_iterate_keys(rtwdev, wow_vif,
354 rtw_wow_fw_security_type_iter, &data);
355 rtw_fw_set_aoac_global_info_cmd(rtwdev, data.pairwise_key_type,
356 data.group_key_type);
359 static int rtw_wow_fw_start(struct rtw_dev *rtwdev)
361 if (rtw_wow_mgd_linked(rtwdev)) {
362 rtw_send_rsvd_page_h2c(rtwdev);
363 rtw_wow_pattern_write(rtwdev);
364 rtw_wow_fw_security_type(rtwdev);
365 rtw_fw_set_disconnect_decision_cmd(rtwdev, true);
366 rtw_fw_set_keep_alive_cmd(rtwdev, true);
367 } else if (rtw_wow_no_link(rtwdev)) {
368 rtw_fw_set_nlo_info(rtwdev, true);
369 rtw_fw_update_pkt_probe_req(rtwdev, NULL);
370 rtw_fw_channel_switch(rtwdev, true);
373 rtw_fw_set_wowlan_ctrl_cmd(rtwdev, true);
374 rtw_fw_set_remote_wake_ctrl_cmd(rtwdev, true);
376 return rtw_wow_check_fw_status(rtwdev, true);
379 static int rtw_wow_fw_stop(struct rtw_dev *rtwdev)
381 if (rtw_wow_mgd_linked(rtwdev)) {
382 rtw_fw_set_disconnect_decision_cmd(rtwdev, false);
383 rtw_fw_set_keep_alive_cmd(rtwdev, false);
384 rtw_wow_pattern_clear(rtwdev);
385 } else if (rtw_wow_no_link(rtwdev)) {
386 rtw_fw_channel_switch(rtwdev, false);
387 rtw_fw_set_nlo_info(rtwdev, false);
390 rtw_fw_set_wowlan_ctrl_cmd(rtwdev, false);
391 rtw_fw_set_remote_wake_ctrl_cmd(rtwdev, false);
393 return rtw_wow_check_fw_status(rtwdev, false);
396 static void rtw_wow_avoid_reset_mac(struct rtw_dev *rtwdev)
398 /* When resuming from wowlan mode, some hosts issue signal
399 * (PCIE: PREST, USB: SE0RST) to device, and lead to reset
400 * mac core. If it happens, the connection to AP will be lost.
401 * Setting REG_RSV_CTRL Register can avoid this process.
403 switch (rtw_hci_type(rtwdev)) {
404 case RTW_HCI_TYPE_PCIE:
405 case RTW_HCI_TYPE_USB:
406 rtw_write8(rtwdev, REG_RSV_CTRL, BIT_WLOCK_1C_B6);
407 rtw_write8(rtwdev, REG_RSV_CTRL,
408 BIT_WLOCK_1C_B6 | BIT_R_DIS_PRST);
409 break;
410 default:
411 rtw_warn(rtwdev, "Unsupported hci type to disable reset MAC\n");
412 break;
416 static void rtw_wow_fw_media_status_iter(void *data, struct ieee80211_sta *sta)
418 struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
419 struct rtw_fw_media_status_iter_data *iter_data = data;
420 struct rtw_dev *rtwdev = iter_data->rtwdev;
422 rtw_fw_media_status_report(rtwdev, si->mac_id, iter_data->connect);
425 static void rtw_wow_fw_media_status(struct rtw_dev *rtwdev, bool connect)
427 struct rtw_fw_media_status_iter_data data;
429 data.rtwdev = rtwdev;
430 data.connect = connect;
432 rtw_iterate_stas_atomic(rtwdev, rtw_wow_fw_media_status_iter, &data);
435 static void rtw_wow_config_pno_rsvd_page(struct rtw_dev *rtwdev)
437 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
438 struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req;
439 struct cfg80211_ssid *ssid;
440 int i;
442 for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) {
443 ssid = &rtw_pno_req->match_sets[i].ssid;
444 rtw_add_rsvd_page_probe_req(rtwdev, ssid);
446 rtw_add_rsvd_page_probe_req(rtwdev, NULL);
447 rtw_add_rsvd_page(rtwdev, RSVD_NLO_INFO, false);
448 rtw_add_rsvd_page(rtwdev, RSVD_CH_INFO, true);
451 static void rtw_wow_config_linked_rsvd_page(struct rtw_dev *rtwdev)
453 rtw_add_rsvd_page(rtwdev, RSVD_PS_POLL, true);
454 rtw_add_rsvd_page(rtwdev, RSVD_QOS_NULL, true);
455 rtw_add_rsvd_page(rtwdev, RSVD_NULL, true);
456 rtw_add_rsvd_page(rtwdev, RSVD_LPS_PG_DPK, true);
457 rtw_add_rsvd_page(rtwdev, RSVD_LPS_PG_INFO, true);
460 static void rtw_wow_config_rsvd_page(struct rtw_dev *rtwdev)
462 rtw_reset_rsvd_page(rtwdev);
464 if (rtw_wow_mgd_linked(rtwdev)) {
465 rtw_wow_config_linked_rsvd_page(rtwdev);
466 } else if (test_bit(RTW_FLAG_WOWLAN, rtwdev->flags) &&
467 rtw_wow_no_link(rtwdev)) {
468 rtw_wow_config_pno_rsvd_page(rtwdev);
472 static int rtw_wow_dl_fw_rsvd_page(struct rtw_dev *rtwdev)
474 struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
476 rtw_wow_config_rsvd_page(rtwdev);
478 return rtw_fw_download_rsvd_page(rtwdev, wow_vif);
481 static int rtw_wow_swap_fw(struct rtw_dev *rtwdev, enum rtw_fw_type type)
483 struct rtw_fw_state *fw;
484 int ret;
486 switch (type) {
487 case RTW_WOWLAN_FW:
488 fw = &rtwdev->wow_fw;
489 break;
491 case RTW_NORMAL_FW:
492 fw = &rtwdev->fw;
493 break;
495 default:
496 rtw_warn(rtwdev, "unsupported firmware type to swap\n");
497 return -ENOENT;
500 ret = rtw_download_firmware(rtwdev, fw);
501 if (ret)
502 goto out;
504 rtw_fw_send_general_info(rtwdev);
505 rtw_fw_send_phydm_info(rtwdev);
506 rtw_wow_fw_media_status(rtwdev, true);
508 out:
509 return ret;
512 static void rtw_wow_check_pno(struct rtw_dev *rtwdev,
513 struct cfg80211_sched_scan_request *nd_config)
515 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
516 struct rtw_pno_request *pno_req = &rtw_wow->pno_req;
517 struct ieee80211_channel *channel;
518 int i, size;
520 if (!nd_config->n_match_sets || !nd_config->n_channels)
521 goto err;
523 pno_req->match_set_cnt = nd_config->n_match_sets;
524 size = sizeof(*pno_req->match_sets) * pno_req->match_set_cnt;
525 pno_req->match_sets = kmemdup(nd_config->match_sets, size, GFP_KERNEL);
526 if (!pno_req->match_sets)
527 goto err;
529 pno_req->channel_cnt = nd_config->n_channels;
530 size = sizeof(*nd_config->channels[0]) * nd_config->n_channels;
531 pno_req->channels = kmalloc(size, GFP_KERNEL);
532 if (!pno_req->channels)
533 goto channel_err;
535 for (i = 0 ; i < pno_req->channel_cnt; i++) {
536 channel = pno_req->channels + i;
537 memcpy(channel, nd_config->channels[i], sizeof(*channel));
540 pno_req->scan_plan = *nd_config->scan_plans;
541 pno_req->inited = true;
543 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: net-detect is enabled\n");
545 return;
547 channel_err:
548 kfree(pno_req->match_sets);
550 err:
551 rtw_dbg(rtwdev, RTW_DBG_WOW, "WOW: net-detect is disabled\n");
554 static int rtw_wow_leave_linked_ps(struct rtw_dev *rtwdev)
556 if (!test_bit(RTW_FLAG_WOWLAN, rtwdev->flags))
557 cancel_delayed_work_sync(&rtwdev->watch_dog_work);
559 rtw_leave_lps(rtwdev);
561 return 0;
564 static int rtw_wow_leave_no_link_ps(struct rtw_dev *rtwdev)
566 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
567 int ret = 0;
569 if (test_bit(RTW_FLAG_WOWLAN, rtwdev->flags)) {
570 if (rtw_fw_lps_deep_mode)
571 rtw_leave_lps_deep(rtwdev);
572 } else {
573 if (test_bit(RTW_FLAG_INACTIVE_PS, rtwdev->flags)) {
574 rtw_wow->ips_enabled = true;
575 ret = rtw_leave_ips(rtwdev);
576 if (ret)
577 return ret;
581 return 0;
584 static int rtw_wow_leave_ps(struct rtw_dev *rtwdev)
586 int ret = 0;
588 if (rtw_wow_mgd_linked(rtwdev))
589 ret = rtw_wow_leave_linked_ps(rtwdev);
590 else if (rtw_wow_no_link(rtwdev))
591 ret = rtw_wow_leave_no_link_ps(rtwdev);
593 return ret;
596 static int rtw_wow_restore_ps(struct rtw_dev *rtwdev)
598 int ret = 0;
600 if (rtw_wow_no_link(rtwdev) && rtwdev->wow.ips_enabled)
601 ret = rtw_enter_ips(rtwdev);
603 return ret;
606 static int rtw_wow_enter_linked_ps(struct rtw_dev *rtwdev)
608 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
609 struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
610 struct rtw_vif *rtwvif = (struct rtw_vif *)wow_vif->drv_priv;
612 rtw_enter_lps(rtwdev, rtwvif->port);
614 return 0;
617 static int rtw_wow_enter_no_link_ps(struct rtw_dev *rtwdev)
619 /* firmware enters deep ps by itself if supported */
620 set_bit(RTW_FLAG_LEISURE_PS_DEEP, rtwdev->flags);
622 return 0;
625 static int rtw_wow_enter_ps(struct rtw_dev *rtwdev)
627 int ret = 0;
629 if (rtw_wow_mgd_linked(rtwdev))
630 ret = rtw_wow_enter_linked_ps(rtwdev);
631 else if (rtw_wow_no_link(rtwdev) && rtw_fw_lps_deep_mode)
632 ret = rtw_wow_enter_no_link_ps(rtwdev);
634 return ret;
637 static void rtw_wow_stop_trx(struct rtw_dev *rtwdev)
639 rtw_wow_bb_stop(rtwdev);
640 rtw_wow_rx_dma_stop(rtwdev);
643 static int rtw_wow_start(struct rtw_dev *rtwdev)
645 int ret;
647 ret = rtw_wow_fw_start(rtwdev);
648 if (ret)
649 goto out;
651 rtw_hci_stop(rtwdev);
652 rtw_wow_bb_start(rtwdev);
653 rtw_wow_avoid_reset_mac(rtwdev);
655 out:
656 return ret;
659 static int rtw_wow_enable(struct rtw_dev *rtwdev)
661 int ret = 0;
663 rtw_wow_stop_trx(rtwdev);
665 ret = rtw_wow_swap_fw(rtwdev, RTW_WOWLAN_FW);
666 if (ret) {
667 rtw_err(rtwdev, "failed to swap wow fw\n");
668 goto error;
671 set_bit(RTW_FLAG_WOWLAN, rtwdev->flags);
673 ret = rtw_wow_dl_fw_rsvd_page(rtwdev);
674 if (ret) {
675 rtw_err(rtwdev, "failed to download wowlan rsvd page\n");
676 goto error;
679 ret = rtw_wow_start(rtwdev);
680 if (ret) {
681 rtw_err(rtwdev, "failed to start wow\n");
682 goto error;
685 return ret;
687 error:
688 clear_bit(RTW_FLAG_WOWLAN, rtwdev->flags);
689 return ret;
692 static int rtw_wow_stop(struct rtw_dev *rtwdev)
694 int ret;
696 /* some HCI related registers will be reset after resume,
697 * need to set them again.
699 ret = rtw_hci_setup(rtwdev);
700 if (ret) {
701 rtw_err(rtwdev, "failed to setup hci\n");
702 return ret;
705 ret = rtw_hci_start(rtwdev);
706 if (ret) {
707 rtw_err(rtwdev, "failed to start hci\n");
708 return ret;
711 ret = rtw_wow_fw_stop(rtwdev);
712 if (ret)
713 rtw_err(rtwdev, "failed to stop wowlan fw\n");
715 rtw_wow_bb_stop(rtwdev);
717 return ret;
720 static void rtw_wow_resume_trx(struct rtw_dev *rtwdev)
722 rtw_wow_rx_dma_start(rtwdev);
723 rtw_wow_bb_start(rtwdev);
724 ieee80211_queue_delayed_work(rtwdev->hw, &rtwdev->watch_dog_work,
725 RTW_WATCH_DOG_DELAY_TIME);
728 static int rtw_wow_disable(struct rtw_dev *rtwdev)
730 int ret;
732 clear_bit(RTW_FLAG_WOWLAN, rtwdev->flags);
734 ret = rtw_wow_stop(rtwdev);
735 if (ret) {
736 rtw_err(rtwdev, "failed to stop wow\n");
737 goto out;
740 ret = rtw_wow_swap_fw(rtwdev, RTW_NORMAL_FW);
741 if (ret) {
742 rtw_err(rtwdev, "failed to swap normal fw\n");
743 goto out;
746 ret = rtw_wow_dl_fw_rsvd_page(rtwdev);
747 if (ret)
748 rtw_err(rtwdev, "failed to download normal rsvd page\n");
750 out:
751 rtw_wow_resume_trx(rtwdev);
752 return ret;
755 static void rtw_wow_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
757 struct rtw_dev *rtwdev = data;
758 struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
759 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
761 /* Current wowlan function support setting of only one STATION vif.
762 * So when one suitable vif is found, stop the iteration.
764 if (rtw_wow->wow_vif || vif->type != NL80211_IFTYPE_STATION)
765 return;
767 switch (rtwvif->net_type) {
768 case RTW_NET_MGD_LINKED:
769 rtw_wow->wow_vif = vif;
770 break;
771 case RTW_NET_NO_LINK:
772 if (rtw_wow->pno_req.inited)
773 rtwdev->wow.wow_vif = vif;
774 break;
775 default:
776 break;
780 static int rtw_wow_set_wakeups(struct rtw_dev *rtwdev,
781 struct cfg80211_wowlan *wowlan)
783 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
784 struct rtw_wow_pattern *rtw_patterns = rtw_wow->patterns;
785 struct rtw_vif *rtwvif;
786 int i;
788 if (wowlan->disconnect)
789 set_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags);
790 if (wowlan->magic_pkt)
791 set_bit(RTW_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags);
792 if (wowlan->gtk_rekey_failure)
793 set_bit(RTW_WOW_FLAG_EN_REKEY_PKT, rtw_wow->flags);
795 if (wowlan->nd_config)
796 rtw_wow_check_pno(rtwdev, wowlan->nd_config);
798 rtw_iterate_vifs_atomic(rtwdev, rtw_wow_vif_iter, rtwdev);
799 if (!rtw_wow->wow_vif)
800 return -EPERM;
802 rtwvif = (struct rtw_vif *)rtw_wow->wow_vif->drv_priv;
803 if (wowlan->n_patterns && wowlan->patterns) {
804 rtw_wow->pattern_cnt = wowlan->n_patterns;
805 for (i = 0; i < wowlan->n_patterns; i++)
806 rtw_wow_pattern_generate(rtwdev, rtwvif,
807 wowlan->patterns + i,
808 rtw_patterns + i);
811 return 0;
814 static void rtw_wow_clear_wakeups(struct rtw_dev *rtwdev)
816 struct rtw_wow_param *rtw_wow = &rtwdev->wow;
817 struct rtw_pno_request *pno_req = &rtw_wow->pno_req;
819 if (pno_req->inited) {
820 kfree(pno_req->channels);
821 kfree(pno_req->match_sets);
824 memset(rtw_wow, 0, sizeof(rtwdev->wow));
827 int rtw_wow_suspend(struct rtw_dev *rtwdev, struct cfg80211_wowlan *wowlan)
829 int ret = 0;
831 ret = rtw_wow_set_wakeups(rtwdev, wowlan);
832 if (ret) {
833 rtw_err(rtwdev, "failed to set wakeup event\n");
834 goto out;
837 ret = rtw_wow_leave_ps(rtwdev);
838 if (ret) {
839 rtw_err(rtwdev, "failed to leave ps from normal mode\n");
840 goto out;
843 ret = rtw_wow_enable(rtwdev);
844 if (ret) {
845 rtw_err(rtwdev, "failed to enable wow\n");
846 rtw_wow_restore_ps(rtwdev);
847 goto out;
850 ret = rtw_wow_enter_ps(rtwdev);
851 if (ret)
852 rtw_err(rtwdev, "failed to enter ps for wow\n");
854 out:
855 return ret;
858 int rtw_wow_resume(struct rtw_dev *rtwdev)
860 int ret;
862 /* If wowlan mode is not enabled, do nothing */
863 if (!test_bit(RTW_FLAG_WOWLAN, rtwdev->flags)) {
864 rtw_err(rtwdev, "wow is not enabled\n");
865 ret = -EPERM;
866 goto out;
869 ret = rtw_wow_leave_ps(rtwdev);
870 if (ret) {
871 rtw_err(rtwdev, "failed to leave ps from wowlan mode\n");
872 goto out;
875 rtw_wow_show_wakeup_reason(rtwdev);
877 ret = rtw_wow_disable(rtwdev);
878 if (ret) {
879 rtw_err(rtwdev, "failed to disable wow\n");
880 goto out;
883 ret = rtw_wow_restore_ps(rtwdev);
884 if (ret)
885 rtw_err(rtwdev, "failed to restore ps to normal mode\n");
887 out:
888 rtw_wow_clear_wakeups(rtwdev);
889 return ret;