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
rtw89_wow_parse_akm(struct rtw89_dev * rtwdev,struct sk_buff * skb)15 void rtw89_wow_parse_akm(struct rtw89_dev *rtwdev, struct sk_buff *skb)
16 {
17 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
18 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
19 const u8 *rsn, *ies = mgmt->u.assoc_req.variable;
20 struct rtw89_rsn_ie *rsn_ie;
21
22 rsn = cfg80211_find_ie(WLAN_EID_RSN, ies, skb->len);
23 if (!rsn)
24 return;
25
26 rsn_ie = (struct rtw89_rsn_ie *)rsn;
27 rtw_wow->akm = rsn_ie->akm_cipher_suite.type;
28 }
29
30 #define RTW89_CIPHER_INFO_DEF(cipher) \
31 {WLAN_CIPHER_SUITE_ ## cipher, .fw_alg = RTW89_WOW_FW_ALG_ ## cipher, \
32 .len = WLAN_KEY_LEN_ ## cipher}
33
34 static const struct rtw89_cipher_info rtw89_cipher_info_defs[] = {
35 RTW89_CIPHER_INFO_DEF(WEP40),
36 RTW89_CIPHER_INFO_DEF(WEP104),
37 RTW89_CIPHER_INFO_DEF(TKIP),
38 RTW89_CIPHER_INFO_DEF(CCMP),
39 RTW89_CIPHER_INFO_DEF(GCMP),
40 RTW89_CIPHER_INFO_DEF(CCMP_256),
41 RTW89_CIPHER_INFO_DEF(GCMP_256),
42 RTW89_CIPHER_INFO_DEF(AES_CMAC),
43 };
44
45 #undef RTW89_CIPHER_INFO_DEF
46
47 static const
rtw89_cipher_alg_recognize(u32 cipher)48 struct rtw89_cipher_info *rtw89_cipher_alg_recognize(u32 cipher)
49 {
50 const struct rtw89_cipher_info *cipher_info_defs;
51 int i;
52
53 for (i = 0; i < ARRAY_SIZE(rtw89_cipher_info_defs); i++) {
54 cipher_info_defs = &rtw89_cipher_info_defs[i];
55 if (cipher_info_defs->cipher == cipher)
56 return cipher_info_defs;
57 }
58
59 return NULL;
60 }
61
_pn_to_iv(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,u8 * iv,u64 pn,u8 key_idx)62 static int _pn_to_iv(struct rtw89_dev *rtwdev, struct ieee80211_key_conf *key,
63 u8 *iv, u64 pn, u8 key_idx)
64 {
65 switch (key->cipher) {
66 case WLAN_CIPHER_SUITE_TKIP:
67 iv[0] = u64_get_bits(pn, RTW89_KEY_PN_1);
68 iv[1] = (u64_get_bits(pn, RTW89_KEY_PN_1) | 0x20) & 0x7f;
69 iv[2] = u64_get_bits(pn, RTW89_KEY_PN_0);
70 break;
71 case WLAN_CIPHER_SUITE_CCMP:
72 case WLAN_CIPHER_SUITE_GCMP:
73 case WLAN_CIPHER_SUITE_CCMP_256:
74 case WLAN_CIPHER_SUITE_GCMP_256:
75 iv[0] = u64_get_bits(pn, RTW89_KEY_PN_0);
76 iv[1] = u64_get_bits(pn, RTW89_KEY_PN_1);
77 iv[2] = 0;
78 break;
79 default:
80 return -EINVAL;
81 }
82
83 iv[3] = BIT(5) | ((key_idx & 0x3) << 6);
84 iv[4] = u64_get_bits(pn, RTW89_KEY_PN_2);
85 iv[5] = u64_get_bits(pn, RTW89_KEY_PN_3);
86 iv[6] = u64_get_bits(pn, RTW89_KEY_PN_4);
87 iv[7] = u64_get_bits(pn, RTW89_KEY_PN_5);
88
89 return 0;
90 }
91
rtw89_rx_pn_to_iv(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,u8 * iv)92 static int rtw89_rx_pn_to_iv(struct rtw89_dev *rtwdev,
93 struct ieee80211_key_conf *key,
94 u8 *iv)
95 {
96 struct ieee80211_key_seq seq;
97 int err;
98 u64 pn;
99
100 ieee80211_get_key_rx_seq(key, 0, &seq);
101
102 /* seq.ccmp.pn[] is BE order array */
103 pn = u64_encode_bits(seq.ccmp.pn[0], RTW89_KEY_PN_5) |
104 u64_encode_bits(seq.ccmp.pn[1], RTW89_KEY_PN_4) |
105 u64_encode_bits(seq.ccmp.pn[2], RTW89_KEY_PN_3) |
106 u64_encode_bits(seq.ccmp.pn[3], RTW89_KEY_PN_2) |
107 u64_encode_bits(seq.ccmp.pn[4], RTW89_KEY_PN_1) |
108 u64_encode_bits(seq.ccmp.pn[5], RTW89_KEY_PN_0);
109
110 err = _pn_to_iv(rtwdev, key, iv, pn, key->keyidx);
111 if (err)
112 return err;
113
114 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s key %d pn-%llx to iv-%*ph\n",
115 __func__, key->keyidx, pn, 8, iv);
116
117 return 0;
118 }
119
rtw89_tx_pn_to_iv(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,u8 * iv)120 static int rtw89_tx_pn_to_iv(struct rtw89_dev *rtwdev,
121 struct ieee80211_key_conf *key,
122 u8 *iv)
123 {
124 int err;
125 u64 pn;
126
127 pn = atomic64_inc_return(&key->tx_pn);
128 err = _pn_to_iv(rtwdev, key, iv, pn, key->keyidx);
129 if (err)
130 return err;
131
132 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s key %d pn-%llx to iv-%*ph\n",
133 __func__, key->keyidx, pn, 8, iv);
134
135 return 0;
136 }
137
_iv_to_pn(struct rtw89_dev * rtwdev,u8 * iv,u64 * pn,u8 * key_id,struct ieee80211_key_conf * key)138 static int _iv_to_pn(struct rtw89_dev *rtwdev, u8 *iv, u64 *pn, u8 *key_id,
139 struct ieee80211_key_conf *key)
140 {
141 switch (key->cipher) {
142 case WLAN_CIPHER_SUITE_TKIP:
143 *pn = u64_encode_bits(iv[2], RTW89_KEY_PN_0) |
144 u64_encode_bits(iv[0], RTW89_KEY_PN_1);
145 break;
146 case WLAN_CIPHER_SUITE_CCMP:
147 case WLAN_CIPHER_SUITE_GCMP:
148 case WLAN_CIPHER_SUITE_CCMP_256:
149 case WLAN_CIPHER_SUITE_GCMP_256:
150 *pn = u64_encode_bits(iv[0], RTW89_KEY_PN_0) |
151 u64_encode_bits(iv[1], RTW89_KEY_PN_1);
152 break;
153 default:
154 return -EINVAL;
155 }
156
157 *pn |= u64_encode_bits(iv[4], RTW89_KEY_PN_2) |
158 u64_encode_bits(iv[5], RTW89_KEY_PN_3) |
159 u64_encode_bits(iv[6], RTW89_KEY_PN_4) |
160 u64_encode_bits(iv[7], RTW89_KEY_PN_5);
161
162 if (key_id)
163 *key_id = *(iv + 3) >> 6;
164
165 return 0;
166 }
167
rtw89_rx_iv_to_pn(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,u8 * iv)168 static int rtw89_rx_iv_to_pn(struct rtw89_dev *rtwdev,
169 struct ieee80211_key_conf *key,
170 u8 *iv)
171 {
172 struct ieee80211_key_seq seq;
173 int err;
174 u64 pn;
175
176 err = _iv_to_pn(rtwdev, iv, &pn, NULL, key);
177 if (err)
178 return err;
179
180 /* seq.ccmp.pn[] is BE order array */
181 seq.ccmp.pn[0] = u64_get_bits(pn, RTW89_KEY_PN_5);
182 seq.ccmp.pn[1] = u64_get_bits(pn, RTW89_KEY_PN_4);
183 seq.ccmp.pn[2] = u64_get_bits(pn, RTW89_KEY_PN_3);
184 seq.ccmp.pn[3] = u64_get_bits(pn, RTW89_KEY_PN_2);
185 seq.ccmp.pn[4] = u64_get_bits(pn, RTW89_KEY_PN_1);
186 seq.ccmp.pn[5] = u64_get_bits(pn, RTW89_KEY_PN_0);
187
188 ieee80211_set_key_rx_seq(key, 0, &seq);
189 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s key %d iv-%*ph to pn-%*ph\n",
190 __func__, key->keyidx, 8, iv, 6, seq.ccmp.pn);
191
192 return 0;
193 }
194
rtw89_tx_iv_to_pn(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,u8 * iv)195 static int rtw89_tx_iv_to_pn(struct rtw89_dev *rtwdev,
196 struct ieee80211_key_conf *key,
197 u8 *iv)
198 {
199 int err;
200 u64 pn;
201
202 err = _iv_to_pn(rtwdev, iv, &pn, NULL, key);
203 if (err)
204 return err;
205
206 atomic64_set(&key->tx_pn, pn);
207 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s key %d iv-%*ph to pn-%llx\n",
208 __func__, key->keyidx, 8, iv, pn);
209
210 return 0;
211 }
212
rtw89_rx_pn_get_pmf(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,struct rtw89_wow_gtk_info * gtk_info)213 static int rtw89_rx_pn_get_pmf(struct rtw89_dev *rtwdev,
214 struct ieee80211_key_conf *key,
215 struct rtw89_wow_gtk_info *gtk_info)
216 {
217 struct ieee80211_key_seq seq;
218 u64 pn;
219
220 if (key->keyidx == 4)
221 memcpy(gtk_info->igtk[0], key->key, key->keylen);
222 else if (key->keyidx == 5)
223 memcpy(gtk_info->igtk[1], key->key, key->keylen);
224 else
225 return -EINVAL;
226
227 ieee80211_get_key_rx_seq(key, 0, &seq);
228
229 /* seq.ccmp.pn[] is BE order array */
230 pn = u64_encode_bits(seq.ccmp.pn[0], RTW89_KEY_PN_5) |
231 u64_encode_bits(seq.ccmp.pn[1], RTW89_KEY_PN_4) |
232 u64_encode_bits(seq.ccmp.pn[2], RTW89_KEY_PN_3) |
233 u64_encode_bits(seq.ccmp.pn[3], RTW89_KEY_PN_2) |
234 u64_encode_bits(seq.ccmp.pn[4], RTW89_KEY_PN_1) |
235 u64_encode_bits(seq.ccmp.pn[5], RTW89_KEY_PN_0);
236 gtk_info->ipn = cpu_to_le64(pn);
237 gtk_info->igtk_keyid = cpu_to_le32(key->keyidx);
238 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s key %d pn-%llx\n",
239 __func__, key->keyidx, pn);
240
241 return 0;
242 }
243
rtw89_rx_pn_set_pmf(struct rtw89_dev * rtwdev,struct ieee80211_key_conf * key,u64 pn)244 static int rtw89_rx_pn_set_pmf(struct rtw89_dev *rtwdev,
245 struct ieee80211_key_conf *key,
246 u64 pn)
247 {
248 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
249 struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
250 struct ieee80211_key_seq seq;
251
252 if (key->keyidx != aoac_rpt->igtk_key_id)
253 return 0;
254
255 /* seq.ccmp.pn[] is BE order array */
256 seq.ccmp.pn[0] = u64_get_bits(pn, RTW89_KEY_PN_5);
257 seq.ccmp.pn[1] = u64_get_bits(pn, RTW89_KEY_PN_4);
258 seq.ccmp.pn[2] = u64_get_bits(pn, RTW89_KEY_PN_3);
259 seq.ccmp.pn[3] = u64_get_bits(pn, RTW89_KEY_PN_2);
260 seq.ccmp.pn[4] = u64_get_bits(pn, RTW89_KEY_PN_1);
261 seq.ccmp.pn[5] = u64_get_bits(pn, RTW89_KEY_PN_0);
262
263 ieee80211_set_key_rx_seq(key, 0, &seq);
264 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s key %d pn-%*ph\n",
265 __func__, key->keyidx, 6, seq.ccmp.pn);
266
267 return 0;
268 }
269
rtw89_wow_get_key_info_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)270 static void rtw89_wow_get_key_info_iter(struct ieee80211_hw *hw,
271 struct ieee80211_vif *vif,
272 struct ieee80211_sta *sta,
273 struct ieee80211_key_conf *key,
274 void *data)
275 {
276 struct rtw89_dev *rtwdev = hw->priv;
277 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
278 struct rtw89_wow_key_info *key_info = &rtw_wow->key_info;
279 struct rtw89_wow_gtk_info *gtk_info = &rtw_wow->gtk_info;
280 const struct rtw89_cipher_info *cipher_info;
281 bool *err = data;
282 int ret;
283
284 cipher_info = rtw89_cipher_alg_recognize(key->cipher);
285
286 switch (key->cipher) {
287 case WLAN_CIPHER_SUITE_TKIP:
288 case WLAN_CIPHER_SUITE_CCMP:
289 case WLAN_CIPHER_SUITE_GCMP:
290 case WLAN_CIPHER_SUITE_CCMP_256:
291 case WLAN_CIPHER_SUITE_GCMP_256:
292 if (sta) {
293 ret = rtw89_tx_pn_to_iv(rtwdev, key,
294 key_info->ptk_tx_iv);
295 if (ret)
296 goto err;
297 ret = rtw89_rx_pn_to_iv(rtwdev, key,
298 key_info->ptk_rx_iv);
299 if (ret)
300 goto err;
301
302 rtw_wow->ptk_alg = cipher_info->fw_alg;
303 rtw_wow->ptk_keyidx = key->keyidx;
304 } else {
305 ret = rtw89_rx_pn_to_iv(rtwdev, key,
306 key_info->gtk_rx_iv[key->keyidx]);
307 if (ret)
308 goto err;
309
310 rtw_wow->gtk_alg = cipher_info->fw_alg;
311 key_info->gtk_keyidx = key->keyidx;
312 }
313 break;
314 case WLAN_CIPHER_SUITE_AES_CMAC:
315 ret = rtw89_rx_pn_get_pmf(rtwdev, key, gtk_info);
316 if (ret)
317 goto err;
318 break;
319 case WLAN_CIPHER_SUITE_WEP40:
320 case WLAN_CIPHER_SUITE_WEP104:
321 /* WEP only set group key in mac80211, but fw need to set
322 * both of pairwise key and group key.
323 */
324 rtw_wow->ptk_alg = cipher_info->fw_alg;
325 rtw_wow->ptk_keyidx = key->keyidx;
326 rtw_wow->gtk_alg = cipher_info->fw_alg;
327 key_info->gtk_keyidx = key->keyidx;
328 break;
329 default:
330 rtw89_debug(rtwdev, RTW89_DBG_WOW, "unsupport cipher %x\n",
331 key->cipher);
332 goto err;
333 }
334
335 return;
336 err:
337 *err = true;
338 }
339
rtw89_wow_set_key_info_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)340 static void rtw89_wow_set_key_info_iter(struct ieee80211_hw *hw,
341 struct ieee80211_vif *vif,
342 struct ieee80211_sta *sta,
343 struct ieee80211_key_conf *key,
344 void *data)
345 {
346 struct rtw89_dev *rtwdev = hw->priv;
347 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
348 struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
349 struct rtw89_set_key_info_iter_data *iter_data = data;
350 bool update_tx_key_info = iter_data->rx_ready;
351 int ret;
352
353 switch (key->cipher) {
354 case WLAN_CIPHER_SUITE_TKIP:
355 case WLAN_CIPHER_SUITE_CCMP:
356 case WLAN_CIPHER_SUITE_GCMP:
357 case WLAN_CIPHER_SUITE_CCMP_256:
358 case WLAN_CIPHER_SUITE_GCMP_256:
359 if (sta && !update_tx_key_info) {
360 ret = rtw89_rx_iv_to_pn(rtwdev, key,
361 aoac_rpt->ptk_rx_iv);
362 if (ret)
363 goto err;
364 }
365
366 if (sta && update_tx_key_info) {
367 ret = rtw89_tx_iv_to_pn(rtwdev, key,
368 aoac_rpt->ptk_tx_iv);
369 if (ret)
370 goto err;
371 }
372
373 if (!sta && !update_tx_key_info) {
374 ret = rtw89_rx_iv_to_pn(rtwdev, key,
375 aoac_rpt->gtk_rx_iv[key->keyidx]);
376 if (ret)
377 goto err;
378 }
379
380 if (!sta && update_tx_key_info && aoac_rpt->rekey_ok)
381 iter_data->gtk_cipher = key->cipher;
382 break;
383 case WLAN_CIPHER_SUITE_AES_CMAC:
384 if (update_tx_key_info) {
385 if (aoac_rpt->rekey_ok)
386 iter_data->igtk_cipher = key->cipher;
387 } else {
388 ret = rtw89_rx_pn_set_pmf(rtwdev, key,
389 aoac_rpt->igtk_ipn);
390 if (ret)
391 goto err;
392 }
393 break;
394 case WLAN_CIPHER_SUITE_WEP40:
395 case WLAN_CIPHER_SUITE_WEP104:
396 break;
397 default:
398 rtw89_debug(rtwdev, RTW89_DBG_WOW, "unsupport cipher %x\n",
399 key->cipher);
400 goto err;
401 }
402
403 return;
404
405 err:
406 iter_data->error = true;
407 }
408
rtw89_wow_key_clear(struct rtw89_dev * rtwdev)409 static void rtw89_wow_key_clear(struct rtw89_dev *rtwdev)
410 {
411 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
412
413 memset(&rtw_wow->aoac_rpt, 0, sizeof(rtw_wow->aoac_rpt));
414 memset(&rtw_wow->gtk_info, 0, sizeof(rtw_wow->gtk_info));
415 memset(&rtw_wow->key_info, 0, sizeof(rtw_wow->key_info));
416 rtw_wow->ptk_alg = 0;
417 rtw_wow->gtk_alg = 0;
418 }
419
rtw89_wow_construct_key_info(struct rtw89_dev * rtwdev)420 static void rtw89_wow_construct_key_info(struct rtw89_dev *rtwdev)
421 {
422 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
423 struct rtw89_wow_key_info *key_info = &rtw_wow->key_info;
424 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
425 struct ieee80211_vif *wow_vif = rtwvif_link_to_vif(rtwvif_link);
426 bool err = false;
427
428 rcu_read_lock();
429 ieee80211_iter_keys_rcu(rtwdev->hw, wow_vif,
430 rtw89_wow_get_key_info_iter, &err);
431 rcu_read_unlock();
432
433 if (err) {
434 rtw89_wow_key_clear(rtwdev);
435 return;
436 }
437
438 key_info->valid_check = RTW89_WOW_VALID_CHECK;
439 key_info->symbol_check_en = RTW89_WOW_SYMBOL_CHK_PTK |
440 RTW89_WOW_SYMBOL_CHK_GTK;
441 }
442
rtw89_wow_debug_aoac_rpt(struct rtw89_dev * rtwdev)443 static void rtw89_wow_debug_aoac_rpt(struct rtw89_dev *rtwdev)
444 {
445 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
446 struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
447
448 if (!rtw89_debug_is_enabled(rtwdev, RTW89_DBG_WOW))
449 return;
450
451 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] rpt_ver = %d\n",
452 aoac_rpt->rpt_ver);
453 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] sec_type = %d\n",
454 aoac_rpt->sec_type);
455 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] key_idx = %d\n",
456 aoac_rpt->key_idx);
457 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] pattern_idx = %d\n",
458 aoac_rpt->pattern_idx);
459 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] rekey_ok = %d\n",
460 aoac_rpt->rekey_ok);
461 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] ptk_tx_iv = %*ph\n",
462 8, aoac_rpt->ptk_tx_iv);
463 rtw89_debug(rtwdev, RTW89_DBG_WOW,
464 "[aoac_rpt] eapol_key_replay_count = %*ph\n",
465 8, aoac_rpt->eapol_key_replay_count);
466 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] ptk_rx_iv = %*ph\n",
467 8, aoac_rpt->ptk_rx_iv);
468 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[0] = %*ph\n",
469 8, aoac_rpt->gtk_rx_iv[0]);
470 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[1] = %*ph\n",
471 8, aoac_rpt->gtk_rx_iv[1]);
472 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[2] = %*ph\n",
473 8, aoac_rpt->gtk_rx_iv[2]);
474 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[3] = %*ph\n",
475 8, aoac_rpt->gtk_rx_iv[3]);
476 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] igtk_key_id = %llu\n",
477 aoac_rpt->igtk_key_id);
478 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] igtk_ipn = %llu\n",
479 aoac_rpt->igtk_ipn);
480 rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] igtk = %*ph\n",
481 32, aoac_rpt->igtk);
482 }
483
rtw89_wow_get_aoac_rpt_reg(struct rtw89_dev * rtwdev)484 static int rtw89_wow_get_aoac_rpt_reg(struct rtw89_dev *rtwdev)
485 {
486 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
487 struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
488 struct rtw89_mac_c2h_info c2h_info = {};
489 struct rtw89_mac_h2c_info h2c_info = {};
490 u8 igtk_ipn[8];
491 u8 key_idx;
492 int ret;
493
494 h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_AOAC_RPT_1;
495 h2c_info.content_len = 2;
496 ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, &c2h_info);
497 if (ret)
498 return ret;
499
500 aoac_rpt->key_idx =
501 u32_get_bits(c2h_info.u.c2hreg[0], RTW89_C2HREG_AOAC_RPT_1_W0_KEY_IDX);
502 key_idx = aoac_rpt->key_idx;
503 aoac_rpt->gtk_rx_iv[key_idx][0] =
504 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_0);
505 aoac_rpt->gtk_rx_iv[key_idx][1] =
506 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_1);
507 aoac_rpt->gtk_rx_iv[key_idx][2] =
508 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_2);
509 aoac_rpt->gtk_rx_iv[key_idx][3] =
510 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_3);
511 aoac_rpt->gtk_rx_iv[key_idx][4] =
512 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_4);
513 aoac_rpt->gtk_rx_iv[key_idx][5] =
514 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_5);
515 aoac_rpt->gtk_rx_iv[key_idx][6] =
516 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_6);
517 aoac_rpt->gtk_rx_iv[key_idx][7] =
518 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_7);
519 aoac_rpt->ptk_rx_iv[0] =
520 u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_0);
521 aoac_rpt->ptk_rx_iv[1] =
522 u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_1);
523 aoac_rpt->ptk_rx_iv[2] =
524 u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_2);
525 aoac_rpt->ptk_rx_iv[3] =
526 u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_3);
527
528 h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_AOAC_RPT_2;
529 h2c_info.content_len = 2;
530 ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, &c2h_info);
531 if (ret)
532 return ret;
533
534 aoac_rpt->ptk_rx_iv[4] =
535 u32_get_bits(c2h_info.u.c2hreg[0], RTW89_C2HREG_AOAC_RPT_2_W0_PTK_IV_4);
536 aoac_rpt->ptk_rx_iv[5] =
537 u32_get_bits(c2h_info.u.c2hreg[0], RTW89_C2HREG_AOAC_RPT_2_W0_PTK_IV_5);
538 aoac_rpt->ptk_rx_iv[6] =
539 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_PTK_IV_6);
540 aoac_rpt->ptk_rx_iv[7] =
541 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_PTK_IV_7);
542 igtk_ipn[0] =
543 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_IGTK_IPN_IV_0);
544 igtk_ipn[1] =
545 u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_IGTK_IPN_IV_1);
546 igtk_ipn[2] =
547 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_2);
548 igtk_ipn[3] =
549 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_3);
550 igtk_ipn[4] =
551 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_4);
552 igtk_ipn[5] =
553 u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_5);
554 igtk_ipn[6] =
555 u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_2_W3_IGTK_IPN_IV_6);
556 igtk_ipn[7] =
557 u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_2_W3_IGTK_IPN_IV_7);
558 aoac_rpt->igtk_ipn = u64_encode_bits(igtk_ipn[0], RTW89_IGTK_IPN_0) |
559 u64_encode_bits(igtk_ipn[1], RTW89_IGTK_IPN_1) |
560 u64_encode_bits(igtk_ipn[2], RTW89_IGTK_IPN_2) |
561 u64_encode_bits(igtk_ipn[3], RTW89_IGTK_IPN_3) |
562 u64_encode_bits(igtk_ipn[4], RTW89_IGTK_IPN_4) |
563 u64_encode_bits(igtk_ipn[5], RTW89_IGTK_IPN_5) |
564 u64_encode_bits(igtk_ipn[6], RTW89_IGTK_IPN_6) |
565 u64_encode_bits(igtk_ipn[7], RTW89_IGTK_IPN_7);
566
567 return 0;
568 }
569
rtw89_wow_get_aoac_rpt(struct rtw89_dev * rtwdev,bool rx_ready)570 static int rtw89_wow_get_aoac_rpt(struct rtw89_dev *rtwdev, bool rx_ready)
571 {
572 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
573 int ret;
574
575 if (!rtw_wow->ptk_alg)
576 return -EPERM;
577
578 if (!rx_ready) {
579 ret = rtw89_wow_get_aoac_rpt_reg(rtwdev);
580 if (ret) {
581 rtw89_err(rtwdev, "wow: failed to get aoac rpt by reg\n");
582 return ret;
583 }
584 } else {
585 ret = rtw89_fw_h2c_wow_request_aoac(rtwdev);
586 if (ret) {
587 rtw89_err(rtwdev, "wow: failed to get aoac rpt by pkt\n");
588 return ret;
589 }
590 }
591
592 rtw89_wow_debug_aoac_rpt(rtwdev);
593
594 return 0;
595 }
596
rtw89_wow_gtk_rekey(struct rtw89_dev * rtwdev,u32 cipher,u8 keyidx,u8 * gtk)597 static struct ieee80211_key_conf *rtw89_wow_gtk_rekey(struct rtw89_dev *rtwdev,
598 u32 cipher, u8 keyidx, u8 *gtk)
599 {
600 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
601 struct ieee80211_vif *wow_vif = rtwvif_link_to_vif(rtwvif_link);
602 const struct rtw89_cipher_info *cipher_info;
603 struct ieee80211_key_conf *rekey_conf;
604 struct ieee80211_key_conf *key;
605 u8 sz;
606
607 cipher_info = rtw89_cipher_alg_recognize(cipher);
608 sz = struct_size(rekey_conf, key, cipher_info->len);
609 rekey_conf = kmalloc(sz, GFP_KERNEL);
610 if (!rekey_conf)
611 return NULL;
612
613 rekey_conf->cipher = cipher;
614 rekey_conf->keyidx = keyidx;
615 rekey_conf->keylen = cipher_info->len;
616 memcpy(rekey_conf->key, gtk,
617 flex_array_size(rekey_conf, key, cipher_info->len));
618
619 /* ieee80211_gtk_rekey_add() will call set_key(), therefore we
620 * need to unlock mutex
621 */
622 mutex_unlock(&rtwdev->mutex);
623 key = ieee80211_gtk_rekey_add(wow_vif, rekey_conf, -1);
624 mutex_lock(&rtwdev->mutex);
625
626 kfree(rekey_conf);
627 if (IS_ERR(key)) {
628 rtw89_err(rtwdev, "ieee80211_gtk_rekey_add failed\n");
629 return NULL;
630 }
631
632 return key;
633 }
634
rtw89_wow_update_key_info(struct rtw89_dev * rtwdev,bool rx_ready)635 static void rtw89_wow_update_key_info(struct rtw89_dev *rtwdev, bool rx_ready)
636 {
637 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
638 struct ieee80211_vif *wow_vif = rtwvif_link_to_vif(rtwvif_link);
639 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
640 struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
641 struct rtw89_set_key_info_iter_data data = {.error = false,
642 .rx_ready = rx_ready};
643 struct ieee80211_bss_conf *bss_conf;
644 struct ieee80211_key_conf *key;
645
646 rcu_read_lock();
647 ieee80211_iter_keys_rcu(rtwdev->hw, wow_vif,
648 rtw89_wow_set_key_info_iter, &data);
649 rcu_read_unlock();
650
651 if (data.error) {
652 rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s error\n", __func__);
653 return;
654 }
655
656 if (!data.gtk_cipher)
657 return;
658
659 key = rtw89_wow_gtk_rekey(rtwdev, data.gtk_cipher, aoac_rpt->key_idx,
660 aoac_rpt->gtk);
661 if (!key)
662 return;
663
664 rtw89_rx_iv_to_pn(rtwdev, key,
665 aoac_rpt->gtk_rx_iv[key->keyidx]);
666
667 if (!data.igtk_cipher)
668 return;
669
670 key = rtw89_wow_gtk_rekey(rtwdev, data.igtk_cipher, aoac_rpt->igtk_key_id,
671 aoac_rpt->igtk);
672 if (!key)
673 return;
674
675 rtw89_rx_pn_set_pmf(rtwdev, key, aoac_rpt->igtk_ipn);
676
677 rcu_read_lock();
678
679 bss_conf = rtw89_vif_rcu_dereference_link(rtwvif_link, true);
680 ieee80211_gtk_rekey_notify(wow_vif, bss_conf->bssid,
681 aoac_rpt->eapol_key_replay_count,
682 GFP_ATOMIC);
683
684 rcu_read_unlock();
685 }
686
rtw89_wow_leave_deep_ps(struct rtw89_dev * rtwdev)687 static void rtw89_wow_leave_deep_ps(struct rtw89_dev *rtwdev)
688 {
689 __rtw89_leave_ps_mode(rtwdev);
690 }
691
rtw89_wow_enter_deep_ps(struct rtw89_dev * rtwdev)692 static void rtw89_wow_enter_deep_ps(struct rtw89_dev *rtwdev)
693 {
694 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
695
696 __rtw89_enter_ps_mode(rtwdev, rtwvif_link);
697 }
698
rtw89_wow_enter_ps(struct rtw89_dev * rtwdev)699 static void rtw89_wow_enter_ps(struct rtw89_dev *rtwdev)
700 {
701 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
702
703 if (rtw89_wow_mgd_linked(rtwdev))
704 rtw89_enter_lps(rtwdev, rtwvif_link, false);
705 else if (rtw89_wow_no_link(rtwdev))
706 rtw89_fw_h2c_fwips(rtwdev, rtwvif_link, true);
707 }
708
rtw89_wow_leave_ps(struct rtw89_dev * rtwdev,bool enable_wow)709 static void rtw89_wow_leave_ps(struct rtw89_dev *rtwdev, bool enable_wow)
710 {
711 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
712
713 if (rtw89_wow_mgd_linked(rtwdev)) {
714 rtw89_leave_lps(rtwdev);
715 } else if (rtw89_wow_no_link(rtwdev)) {
716 if (enable_wow)
717 rtw89_leave_ips(rtwdev);
718 else
719 rtw89_fw_h2c_fwips(rtwdev, rtwvif_link, false);
720 }
721 }
722
rtw89_wow_config_mac(struct rtw89_dev * rtwdev,bool enable_wow)723 static int rtw89_wow_config_mac(struct rtw89_dev *rtwdev, bool enable_wow)
724 {
725 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
726
727 return mac->wow_config_mac(rtwdev, enable_wow);
728 }
729
rtw89_wow_set_rx_filter(struct rtw89_dev * rtwdev,bool enable)730 static void rtw89_wow_set_rx_filter(struct rtw89_dev *rtwdev, bool enable)
731 {
732 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
733 enum rtw89_mac_fwd_target fwd_target = enable ?
734 RTW89_FWD_DONT_CARE :
735 RTW89_FWD_TO_HOST;
736
737 mac->typ_fltr_opt(rtwdev, RTW89_MGNT, fwd_target, RTW89_MAC_0);
738 mac->typ_fltr_opt(rtwdev, RTW89_CTRL, fwd_target, RTW89_MAC_0);
739 mac->typ_fltr_opt(rtwdev, RTW89_DATA, fwd_target, RTW89_MAC_0);
740 }
741
rtw89_wow_show_wakeup_reason(struct rtw89_dev * rtwdev)742 static void rtw89_wow_show_wakeup_reason(struct rtw89_dev *rtwdev)
743 {
744 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
745 struct ieee80211_vif *wow_vif = rtwvif_link_to_vif(rtwvif_link);
746 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
747 struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
748 struct cfg80211_wowlan_nd_info nd_info;
749 struct cfg80211_wowlan_wakeup wakeup = {
750 .pattern_idx = -1,
751 };
752 u32 wow_reason_reg;
753 u8 reason;
754
755 if (RTW89_CHK_FW_FEATURE(WOW_REASON_V1, &rtwdev->fw))
756 wow_reason_reg = rtwdev->chip->wow_reason_reg[RTW89_WOW_REASON_V1];
757 else
758 wow_reason_reg = rtwdev->chip->wow_reason_reg[RTW89_WOW_REASON_V0];
759
760 reason = rtw89_read8(rtwdev, wow_reason_reg);
761 switch (reason) {
762 case RTW89_WOW_RSN_RX_DEAUTH:
763 wakeup.disconnect = true;
764 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx deauth\n");
765 break;
766 case RTW89_WOW_RSN_DISCONNECT:
767 wakeup.disconnect = true;
768 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: AP is off\n");
769 break;
770 case RTW89_WOW_RSN_RX_MAGIC_PKT:
771 wakeup.magic_pkt = true;
772 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx magic packet\n");
773 break;
774 case RTW89_WOW_RSN_RX_GTK_REKEY:
775 wakeup.gtk_rekey_failure = true;
776 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx gtk rekey\n");
777 break;
778 case RTW89_WOW_RSN_RX_PATTERN_MATCH:
779 wakeup.pattern_idx = aoac_rpt->pattern_idx;
780 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx pattern match packet\n");
781 break;
782 case RTW89_WOW_RSN_RX_NLO:
783 /* Current firmware and driver don't report ssid index.
784 * Use 0 for n_matches based on its comment.
785 */
786 nd_info.n_matches = 0;
787 wakeup.net_detect = &nd_info;
788 rtw89_debug(rtwdev, RTW89_DBG_WOW, "Rx NLO\n");
789 break;
790 default:
791 rtw89_warn(rtwdev, "Unknown wakeup reason %x\n", reason);
792 ieee80211_report_wowlan_wakeup(wow_vif, NULL, GFP_KERNEL);
793 return;
794 }
795
796 ieee80211_report_wowlan_wakeup(wow_vif, &wakeup, GFP_KERNEL);
797 }
798
rtw89_wow_vif_iter(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link)799 static void rtw89_wow_vif_iter(struct rtw89_dev *rtwdev,
800 struct rtw89_vif_link *rtwvif_link)
801 {
802 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
803 struct ieee80211_vif *vif = rtwvif_link_to_vif(rtwvif_link);
804
805 /* Current WoWLAN function support setting of only vif in
806 * infra mode or no link mode. When one suitable vif is found,
807 * stop the iteration.
808 */
809 if (rtw_wow->rtwvif_link || vif->type != NL80211_IFTYPE_STATION)
810 return;
811
812 switch (rtwvif_link->net_type) {
813 case RTW89_NET_TYPE_INFRA:
814 if (rtw_wow_has_mgd_features(rtwdev))
815 rtw_wow->rtwvif_link = rtwvif_link;
816 break;
817 case RTW89_NET_TYPE_NO_LINK:
818 if (rtw_wow->pno_inited)
819 rtw_wow->rtwvif_link = rtwvif_link;
820 break;
821 default:
822 break;
823 }
824 }
825
__rtw89_cal_crc16(u8 data,u16 crc)826 static u16 __rtw89_cal_crc16(u8 data, u16 crc)
827 {
828 u8 shift_in, data_bit;
829 u8 crc_bit4, crc_bit11, crc_bit15;
830 u16 crc_result;
831 int index;
832
833 for (index = 0; index < 8; index++) {
834 crc_bit15 = crc & BIT(15) ? 1 : 0;
835 data_bit = data & BIT(index) ? 1 : 0;
836 shift_in = crc_bit15 ^ data_bit;
837
838 crc_result = crc << 1;
839
840 if (shift_in == 0)
841 crc_result &= ~BIT(0);
842 else
843 crc_result |= BIT(0);
844
845 crc_bit11 = (crc & BIT(11) ? 1 : 0) ^ shift_in;
846
847 if (crc_bit11 == 0)
848 crc_result &= ~BIT(12);
849 else
850 crc_result |= BIT(12);
851
852 crc_bit4 = (crc & BIT(4) ? 1 : 0) ^ shift_in;
853
854 if (crc_bit4 == 0)
855 crc_result &= ~BIT(5);
856 else
857 crc_result |= BIT(5);
858
859 crc = crc_result;
860 }
861 return crc;
862 }
863
rtw89_calc_crc(u8 * pdata,int length)864 static u16 rtw89_calc_crc(u8 *pdata, int length)
865 {
866 u16 crc = 0xffff;
867 int i;
868
869 for (i = 0; i < length; i++)
870 crc = __rtw89_cal_crc16(pdata[i], crc);
871
872 /* get 1' complement */
873 return ~crc;
874 }
875
rtw89_wow_pattern_get_type(struct rtw89_vif_link * rtwvif_link,struct rtw89_wow_cam_info * rtw_pattern,const u8 * pattern,u8 da_mask)876 static int rtw89_wow_pattern_get_type(struct rtw89_vif_link *rtwvif_link,
877 struct rtw89_wow_cam_info *rtw_pattern,
878 const u8 *pattern, u8 da_mask)
879 {
880 u8 da[ETH_ALEN];
881
882 ether_addr_copy_mask(da, pattern, da_mask);
883
884 /* Each pattern is divided into different kinds by DA address
885 * a. DA is broadcast address: set bc = 0;
886 * b. DA is multicast address: set mc = 0
887 * c. DA is unicast address same as dev's mac address: set uc = 0
888 * d. DA is unmasked. Also called wildcard type: set uc = bc = mc = 0
889 * e. Others is invalid type.
890 */
891
892 if (is_broadcast_ether_addr(da))
893 rtw_pattern->bc = true;
894 else if (is_multicast_ether_addr(da))
895 rtw_pattern->mc = true;
896 else if (ether_addr_equal(da, rtwvif_link->mac_addr) &&
897 da_mask == GENMASK(5, 0))
898 rtw_pattern->uc = true;
899 else if (!da_mask) /*da_mask == 0 mean wildcard*/
900 return 0;
901 else
902 return -EPERM;
903
904 return 0;
905 }
906
rtw89_wow_pattern_generate(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link,const struct cfg80211_pkt_pattern * pkt_pattern,struct rtw89_wow_cam_info * rtw_pattern)907 static int rtw89_wow_pattern_generate(struct rtw89_dev *rtwdev,
908 struct rtw89_vif_link *rtwvif_link,
909 const struct cfg80211_pkt_pattern *pkt_pattern,
910 struct rtw89_wow_cam_info *rtw_pattern)
911 {
912 u8 mask_hw[RTW89_MAX_PATTERN_MASK_SIZE * 4] = {0};
913 u8 content[RTW89_MAX_PATTERN_SIZE] = {0};
914 const u8 *mask;
915 const u8 *pattern;
916 u8 mask_len;
917 u16 count;
918 u32 len;
919 int i, ret;
920
921 pattern = pkt_pattern->pattern;
922 len = pkt_pattern->pattern_len;
923 mask = pkt_pattern->mask;
924 mask_len = DIV_ROUND_UP(len, 8);
925 memset(rtw_pattern, 0, sizeof(*rtw_pattern));
926
927 ret = rtw89_wow_pattern_get_type(rtwvif_link, rtw_pattern, pattern,
928 mask[0] & GENMASK(5, 0));
929 if (ret)
930 return ret;
931
932 /* translate mask from os to mask for hw
933 * pattern from OS uses 'ethenet frame', like this:
934 * | 6 | 6 | 2 | 20 | Variable | 4 |
935 * |--------+--------+------+-----------+------------+-----|
936 * | 802.3 Mac Header | IP Header | TCP Packet | FCS |
937 * | DA | SA | Type |
938 *
939 * BUT, packet catched by our HW is in '802.11 frame', begin from LLC
940 * | 24 or 30 | 6 | 2 | 20 | Variable | 4 |
941 * |-------------------+--------+------+-----------+------------+-----|
942 * | 802.11 MAC Header | LLC | IP Header | TCP Packet | FCS |
943 * | Others | Tpye |
944 *
945 * Therefore, we need translate mask_from_OS to mask_to_hw.
946 * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
947 * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
948 * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
949 */
950
951 /* Shift 6 bits */
952 for (i = 0; i < mask_len - 1; i++) {
953 mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6)) |
954 u8_get_bits(mask[i + 1], GENMASK(5, 0)) << 2;
955 }
956 mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6));
957
958 /* Set bit 0-5 to zero */
959 mask_hw[0] &= ~GENMASK(5, 0);
960
961 memcpy(rtw_pattern->mask, mask_hw, sizeof(rtw_pattern->mask));
962
963 /* To get the wake up pattern from the mask.
964 * We do not count first 12 bits which means
965 * DA[6] and SA[6] in the pattern to match HW design.
966 */
967 count = 0;
968 for (i = 12; i < len; i++) {
969 if ((mask[i / 8] >> (i % 8)) & 0x01) {
970 content[count] = pattern[i];
971 count++;
972 }
973 }
974
975 rtw_pattern->crc = rtw89_calc_crc(content, count);
976
977 return 0;
978 }
979
rtw89_wow_parse_patterns(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link,struct cfg80211_wowlan * wowlan)980 static int rtw89_wow_parse_patterns(struct rtw89_dev *rtwdev,
981 struct rtw89_vif_link *rtwvif_link,
982 struct cfg80211_wowlan *wowlan)
983 {
984 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
985 struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
986 int i;
987 int ret;
988
989 if (!wowlan->n_patterns || !wowlan->patterns)
990 return 0;
991
992 for (i = 0; i < wowlan->n_patterns; i++) {
993 rtw_pattern = &rtw_wow->patterns[i];
994 ret = rtw89_wow_pattern_generate(rtwdev, rtwvif_link,
995 &wowlan->patterns[i],
996 rtw_pattern);
997 if (ret) {
998 rtw89_err(rtwdev, "failed to generate pattern(%d)\n", i);
999 rtw_wow->pattern_cnt = 0;
1000 return ret;
1001 }
1002
1003 rtw_pattern->r_w = true;
1004 rtw_pattern->idx = i;
1005 rtw_pattern->negative_pattern_match = false;
1006 rtw_pattern->skip_mac_hdr = true;
1007 rtw_pattern->valid = true;
1008 }
1009 rtw_wow->pattern_cnt = wowlan->n_patterns;
1010
1011 return 0;
1012 }
1013
rtw89_wow_pattern_clear_cam(struct rtw89_dev * rtwdev)1014 static void rtw89_wow_pattern_clear_cam(struct rtw89_dev *rtwdev)
1015 {
1016 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1017 struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
1018 int i = 0;
1019
1020 for (i = 0; i < rtw_wow->pattern_cnt; i++) {
1021 rtw_pattern = &rtw_wow->patterns[i];
1022 rtw_pattern->valid = false;
1023 rtw89_fw_wow_cam_update(rtwdev, rtw_pattern);
1024 }
1025 }
1026
rtw89_wow_pattern_write(struct rtw89_dev * rtwdev)1027 static void rtw89_wow_pattern_write(struct rtw89_dev *rtwdev)
1028 {
1029 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1030 struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
1031 int i;
1032
1033 for (i = 0; i < rtw_wow->pattern_cnt; i++)
1034 rtw89_fw_wow_cam_update(rtwdev, rtw_pattern + i);
1035 }
1036
rtw89_wow_pattern_clear(struct rtw89_dev * rtwdev)1037 static void rtw89_wow_pattern_clear(struct rtw89_dev *rtwdev)
1038 {
1039 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1040
1041 rtw89_wow_pattern_clear_cam(rtwdev);
1042
1043 rtw_wow->pattern_cnt = 0;
1044 memset(rtw_wow->patterns, 0, sizeof(rtw_wow->patterns));
1045 }
1046
rtw89_wow_clear_wakeups(struct rtw89_dev * rtwdev)1047 static void rtw89_wow_clear_wakeups(struct rtw89_dev *rtwdev)
1048 {
1049 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1050
1051 rtw_wow->rtwvif_link = NULL;
1052 rtw89_core_release_all_bits_map(rtw_wow->flags, RTW89_WOW_FLAG_NUM);
1053 rtw_wow->pattern_cnt = 0;
1054 rtw_wow->pno_inited = false;
1055 }
1056
rtw89_wow_init_pno(struct rtw89_dev * rtwdev,struct cfg80211_sched_scan_request * nd_config)1057 static void rtw89_wow_init_pno(struct rtw89_dev *rtwdev,
1058 struct cfg80211_sched_scan_request *nd_config)
1059 {
1060 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1061
1062 if (!nd_config->n_match_sets || !nd_config->n_channels)
1063 return;
1064
1065 rtw_wow->nd_config = nd_config;
1066 rtw_wow->pno_inited = true;
1067
1068 INIT_LIST_HEAD(&rtw_wow->pno_pkt_list);
1069
1070 rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: net-detect is enabled\n");
1071 }
1072
rtw89_wow_set_wakeups(struct rtw89_dev * rtwdev,struct cfg80211_wowlan * wowlan)1073 static int rtw89_wow_set_wakeups(struct rtw89_dev *rtwdev,
1074 struct cfg80211_wowlan *wowlan)
1075 {
1076 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1077 struct rtw89_vif_link *rtwvif_link;
1078 struct rtw89_vif *rtwvif;
1079
1080 if (wowlan->disconnect)
1081 set_bit(RTW89_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags);
1082 if (wowlan->magic_pkt)
1083 set_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags);
1084 if (wowlan->n_patterns && wowlan->patterns)
1085 set_bit(RTW89_WOW_FLAG_EN_PATTERN, rtw_wow->flags);
1086
1087 if (wowlan->nd_config)
1088 rtw89_wow_init_pno(rtwdev, wowlan->nd_config);
1089
1090 rtw89_for_each_rtwvif(rtwdev, rtwvif) {
1091 /* use the link on HW-0 to do wow flow */
1092 rtwvif_link = rtw89_vif_get_link_inst(rtwvif, 0);
1093 if (!rtwvif_link)
1094 continue;
1095
1096 rtw89_wow_vif_iter(rtwdev, rtwvif_link);
1097 }
1098
1099 rtwvif_link = rtw_wow->rtwvif_link;
1100 if (!rtwvif_link)
1101 return -EPERM;
1102
1103 return rtw89_wow_parse_patterns(rtwdev, rtwvif_link, wowlan);
1104 }
1105
rtw89_wow_cfg_wake_pno(struct rtw89_dev * rtwdev,bool wow)1106 static int rtw89_wow_cfg_wake_pno(struct rtw89_dev *rtwdev, bool wow)
1107 {
1108 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
1109 int ret;
1110
1111 ret = rtw89_fw_h2c_cfg_pno(rtwdev, rtwvif_link, true);
1112 if (ret) {
1113 rtw89_err(rtwdev, "failed to config pno\n");
1114 return ret;
1115 }
1116
1117 ret = rtw89_fw_h2c_wow_wakeup_ctrl(rtwdev, rtwvif_link, wow);
1118 if (ret) {
1119 rtw89_err(rtwdev, "failed to fw wow wakeup ctrl\n");
1120 return ret;
1121 }
1122
1123 ret = rtw89_fw_h2c_wow_global(rtwdev, rtwvif_link, wow);
1124 if (ret) {
1125 rtw89_err(rtwdev, "failed to fw wow global\n");
1126 return ret;
1127 }
1128
1129 return 0;
1130 }
1131
rtw89_wow_cfg_wake(struct rtw89_dev * rtwdev,bool wow)1132 static int rtw89_wow_cfg_wake(struct rtw89_dev *rtwdev, bool wow)
1133 {
1134 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1135 struct rtw89_vif_link *rtwvif_link = rtw_wow->rtwvif_link;
1136 struct ieee80211_vif *wow_vif = rtwvif_link_to_vif(rtwvif_link);
1137 struct ieee80211_sta *wow_sta;
1138 struct rtw89_sta_link *rtwsta_link = NULL;
1139 struct rtw89_sta *rtwsta;
1140 int ret;
1141
1142 wow_sta = ieee80211_find_sta(wow_vif, wow_vif->cfg.ap_addr);
1143 if (wow_sta) {
1144 rtwsta = sta_to_rtwsta(wow_sta);
1145 rtwsta_link = rtwsta->links[rtwvif_link->link_id];
1146 if (!rtwsta_link)
1147 return -ENOLINK;
1148 }
1149
1150 if (wow) {
1151 if (rtw_wow->pattern_cnt)
1152 rtwvif_link->wowlan_pattern = true;
1153 if (test_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
1154 rtwvif_link->wowlan_magic = true;
1155 } else {
1156 rtwvif_link->wowlan_pattern = false;
1157 rtwvif_link->wowlan_magic = false;
1158 }
1159
1160 ret = rtw89_fw_h2c_wow_wakeup_ctrl(rtwdev, rtwvif_link, wow);
1161 if (ret) {
1162 rtw89_err(rtwdev, "failed to fw wow wakeup ctrl\n");
1163 return ret;
1164 }
1165
1166 if (wow) {
1167 ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif_link, rtwsta_link);
1168 if (ret) {
1169 rtw89_err(rtwdev, "failed to update dctl cam sec entry: %d\n",
1170 ret);
1171 return ret;
1172 }
1173 }
1174
1175 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif_link, rtwsta_link, NULL);
1176 if (ret) {
1177 rtw89_warn(rtwdev, "failed to send h2c cam\n");
1178 return ret;
1179 }
1180
1181 ret = rtw89_fw_h2c_wow_global(rtwdev, rtwvif_link, wow);
1182 if (ret) {
1183 rtw89_err(rtwdev, "failed to fw wow global\n");
1184 return ret;
1185 }
1186
1187 return 0;
1188 }
1189
rtw89_wow_check_fw_status(struct rtw89_dev * rtwdev,bool wow_enable)1190 static int rtw89_wow_check_fw_status(struct rtw89_dev *rtwdev, bool wow_enable)
1191 {
1192 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
1193 u8 polling;
1194 int ret;
1195
1196 ret = read_poll_timeout_atomic(rtw89_read8_mask, polling,
1197 wow_enable == !!polling,
1198 50, 50000, false, rtwdev,
1199 mac->wow_ctrl.addr, mac->wow_ctrl.mask);
1200 if (ret)
1201 rtw89_err(rtwdev, "failed to check wow status %s\n",
1202 wow_enable ? "enabled" : "disabled");
1203 return ret;
1204 }
1205
rtw89_wow_swap_fw(struct rtw89_dev * rtwdev,bool wow)1206 static int rtw89_wow_swap_fw(struct rtw89_dev *rtwdev, bool wow)
1207 {
1208 enum rtw89_fw_type fw_type = wow ? RTW89_FW_WOWLAN : RTW89_FW_NORMAL;
1209 enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen;
1210 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1211 struct rtw89_vif_link *rtwvif_link = rtw_wow->rtwvif_link;
1212 struct ieee80211_vif *wow_vif = rtwvif_link_to_vif(rtwvif_link);
1213 enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
1214 const struct rtw89_chip_info *chip = rtwdev->chip;
1215 bool include_bb = !!chip->bbmcu_nr;
1216 bool disable_intr_for_dlfw = false;
1217 struct ieee80211_sta *wow_sta;
1218 struct rtw89_sta_link *rtwsta_link = NULL;
1219 struct rtw89_sta *rtwsta;
1220 bool is_conn = true;
1221 int ret;
1222
1223 if (chip_id == RTL8852C || chip_id == RTL8922A)
1224 disable_intr_for_dlfw = true;
1225
1226 wow_sta = ieee80211_find_sta(wow_vif, wow_vif->cfg.ap_addr);
1227 if (wow_sta) {
1228 rtwsta = sta_to_rtwsta(wow_sta);
1229 rtwsta_link = rtwsta->links[rtwvif_link->link_id];
1230 if (!rtwsta_link)
1231 return -ENOLINK;
1232 } else {
1233 is_conn = false;
1234 }
1235
1236 if (disable_intr_for_dlfw)
1237 rtw89_hci_disable_intr(rtwdev);
1238
1239 ret = rtw89_fw_download(rtwdev, fw_type, include_bb);
1240 if (ret) {
1241 rtw89_warn(rtwdev, "download fw failed\n");
1242 return ret;
1243 }
1244
1245 if (disable_intr_for_dlfw)
1246 rtw89_hci_enable_intr(rtwdev);
1247
1248 rtw89_phy_init_rf_reg(rtwdev, true);
1249
1250 ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif_link, rtwsta_link,
1251 RTW89_ROLE_FW_RESTORE);
1252 if (ret) {
1253 rtw89_warn(rtwdev, "failed to send h2c role maintain\n");
1254 return ret;
1255 }
1256
1257 ret = rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, rtwvif_link, rtwsta_link);
1258 if (ret) {
1259 rtw89_warn(rtwdev, "failed to send h2c assoc cmac tbl\n");
1260 return ret;
1261 }
1262
1263 if (!is_conn)
1264 rtw89_cam_reset_keys(rtwdev);
1265
1266 ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif_link, rtwsta_link, !is_conn);
1267 if (ret) {
1268 rtw89_warn(rtwdev, "failed to send h2c join info\n");
1269 return ret;
1270 }
1271
1272 ret = rtw89_fw_h2c_cam(rtwdev, rtwvif_link, rtwsta_link, NULL);
1273 if (ret) {
1274 rtw89_warn(rtwdev, "failed to send h2c cam\n");
1275 return ret;
1276 }
1277
1278 if (is_conn) {
1279 ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwvif_link, rtwsta_link->mac_id);
1280 if (ret) {
1281 rtw89_warn(rtwdev, "failed to send h2c general packet\n");
1282 return ret;
1283 }
1284 rtw89_phy_ra_assoc(rtwdev, rtwsta_link);
1285 rtw89_phy_set_bss_color(rtwdev, rtwvif_link);
1286 rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, rtwvif_link);
1287 }
1288
1289 if (chip_gen == RTW89_CHIP_BE)
1290 rtw89_phy_rfk_pre_ntfy_and_wait(rtwdev, RTW89_PHY_0, 5);
1291
1292 rtw89_mac_hw_mgnt_sec(rtwdev, wow);
1293
1294 return 0;
1295 }
1296
rtw89_wow_enable_trx_pre(struct rtw89_dev * rtwdev)1297 static int rtw89_wow_enable_trx_pre(struct rtw89_dev *rtwdev)
1298 {
1299 int ret;
1300
1301 rtw89_hci_ctrl_txdma_ch(rtwdev, false);
1302 rtw89_hci_ctrl_txdma_fw_ch(rtwdev, true);
1303
1304 rtw89_mac_ptk_drop_by_band_and_wait(rtwdev, RTW89_MAC_0);
1305
1306 ret = rtw89_hci_poll_txdma_ch_idle(rtwdev);
1307 if (ret) {
1308 rtw89_err(rtwdev, "txdma ch busy\n");
1309 return ret;
1310 }
1311 rtw89_wow_set_rx_filter(rtwdev, true);
1312
1313 ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, false);
1314 if (ret) {
1315 rtw89_err(rtwdev, "cfg ppdu status\n");
1316 return ret;
1317 }
1318
1319 return 0;
1320 }
1321
rtw89_wow_enable_trx_post(struct rtw89_dev * rtwdev)1322 static int rtw89_wow_enable_trx_post(struct rtw89_dev *rtwdev)
1323 {
1324 int ret;
1325
1326 rtw89_hci_disable_intr(rtwdev);
1327 rtw89_hci_ctrl_trxhci(rtwdev, false);
1328
1329 ret = rtw89_hci_poll_txdma_ch_idle(rtwdev);
1330 if (ret) {
1331 rtw89_err(rtwdev, "failed to poll txdma ch idle pcie\n");
1332 return ret;
1333 }
1334
1335 ret = rtw89_wow_config_mac(rtwdev, true);
1336 if (ret) {
1337 rtw89_err(rtwdev, "failed to config mac\n");
1338 return ret;
1339 }
1340
1341 rtw89_wow_set_rx_filter(rtwdev, false);
1342 rtw89_hci_reset(rtwdev);
1343
1344 return 0;
1345 }
1346
rtw89_wow_disable_trx_pre(struct rtw89_dev * rtwdev)1347 static int rtw89_wow_disable_trx_pre(struct rtw89_dev *rtwdev)
1348 {
1349 int ret;
1350
1351 rtw89_hci_clr_idx_all(rtwdev);
1352
1353 ret = rtw89_hci_rst_bdram(rtwdev);
1354 if (ret) {
1355 rtw89_warn(rtwdev, "reset bdram busy\n");
1356 return ret;
1357 }
1358
1359 rtw89_hci_ctrl_trxhci(rtwdev, true);
1360 rtw89_hci_ctrl_txdma_ch(rtwdev, true);
1361
1362 ret = rtw89_wow_config_mac(rtwdev, false);
1363 if (ret) {
1364 rtw89_err(rtwdev, "failed to config mac\n");
1365 return ret;
1366 }
1367
1368 /* Before enabling interrupt, we need to get AOAC report by reg due to RX
1369 * not enabled yet. Also, we need to sync RX related IV from firmware to
1370 * mac80211 before receiving RX packets from driver.
1371 * After enabling interrupt, we can get AOAC report from h2c and c2h, and
1372 * can get TX IV and complete rekey info. We need to update TX related IV
1373 * and new GTK info if rekey happened.
1374 */
1375 ret = rtw89_wow_get_aoac_rpt(rtwdev, false);
1376 if (!ret)
1377 rtw89_wow_update_key_info(rtwdev, false);
1378
1379 rtw89_hci_enable_intr(rtwdev);
1380 ret = rtw89_wow_get_aoac_rpt(rtwdev, true);
1381 if (!ret)
1382 rtw89_wow_update_key_info(rtwdev, true);
1383
1384 return 0;
1385 }
1386
rtw89_wow_disable_trx_post(struct rtw89_dev * rtwdev)1387 static int rtw89_wow_disable_trx_post(struct rtw89_dev *rtwdev)
1388 {
1389 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
1390 int ret;
1391
1392 ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
1393 if (ret)
1394 rtw89_err(rtwdev, "cfg ppdu status\n");
1395
1396 rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, rtwvif_link, true);
1397
1398 return ret;
1399 }
1400
rtw89_fw_release_pno_pkt_list(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link)1401 static void rtw89_fw_release_pno_pkt_list(struct rtw89_dev *rtwdev,
1402 struct rtw89_vif_link *rtwvif_link)
1403 {
1404 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1405 struct list_head *pkt_list = &rtw_wow->pno_pkt_list;
1406 struct rtw89_pktofld_info *info, *tmp;
1407
1408 list_for_each_entry_safe(info, tmp, pkt_list, list) {
1409 rtw89_fw_h2c_del_pkt_offload(rtwdev, info->id);
1410 list_del(&info->list);
1411 kfree(info);
1412 }
1413 }
1414
rtw89_pno_scan_update_probe_req(struct rtw89_dev * rtwdev,struct rtw89_vif_link * rtwvif_link)1415 static int rtw89_pno_scan_update_probe_req(struct rtw89_dev *rtwdev,
1416 struct rtw89_vif_link *rtwvif_link)
1417 {
1418 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1419 struct cfg80211_sched_scan_request *nd_config = rtw_wow->nd_config;
1420 u8 num = nd_config->n_match_sets, i;
1421 struct rtw89_pktofld_info *info;
1422 struct sk_buff *skb;
1423 int ret;
1424
1425 for (i = 0; i < num; i++) {
1426 skb = ieee80211_probereq_get(rtwdev->hw, rtwvif_link->mac_addr,
1427 nd_config->match_sets[i].ssid.ssid,
1428 nd_config->match_sets[i].ssid.ssid_len,
1429 nd_config->ie_len);
1430 if (!skb)
1431 return -ENOMEM;
1432
1433 skb_put_data(skb, nd_config->ie, nd_config->ie_len);
1434
1435 info = kzalloc(sizeof(*info), GFP_KERNEL);
1436 if (!info) {
1437 kfree_skb(skb);
1438 rtw89_fw_release_pno_pkt_list(rtwdev, rtwvif_link);
1439 return -ENOMEM;
1440 }
1441
1442 ret = rtw89_fw_h2c_add_pkt_offload(rtwdev, &info->id, skb);
1443 if (ret) {
1444 kfree_skb(skb);
1445 kfree(info);
1446 rtw89_fw_release_pno_pkt_list(rtwdev, rtwvif_link);
1447 return ret;
1448 }
1449
1450 list_add_tail(&info->list, &rtw_wow->pno_pkt_list);
1451 kfree_skb(skb);
1452 }
1453
1454 return 0;
1455 }
1456
rtw89_pno_scan_offload(struct rtw89_dev * rtwdev,bool enable)1457 static int rtw89_pno_scan_offload(struct rtw89_dev *rtwdev, bool enable)
1458 {
1459 const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
1460 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1461 struct rtw89_vif_link *rtwvif_link = rtwdev->wow.rtwvif_link;
1462 int interval = rtw_wow->nd_config->scan_plans[0].interval;
1463 struct rtw89_scan_option opt = {};
1464 int ret;
1465
1466 if (enable) {
1467 ret = rtw89_pno_scan_update_probe_req(rtwdev, rtwvif_link);
1468 if (ret) {
1469 rtw89_err(rtwdev, "Update probe request failed\n");
1470 return ret;
1471 }
1472
1473 ret = mac->add_chan_list_pno(rtwdev, rtwvif_link);
1474 if (ret) {
1475 rtw89_err(rtwdev, "Update channel list failed\n");
1476 return ret;
1477 }
1478 }
1479
1480 opt.enable = enable;
1481 opt.repeat = RTW89_SCAN_NORMAL;
1482 opt.norm_pd = max(interval, 1) * 10; /* in unit of 100ms */
1483 opt.delay = max(rtw_wow->nd_config->delay, 1);
1484
1485 if (rtwdev->chip->chip_gen == RTW89_CHIP_BE) {
1486 opt.operation = enable ? RTW89_SCAN_OP_START : RTW89_SCAN_OP_STOP;
1487 opt.scan_mode = RTW89_SCAN_MODE_SA;
1488 opt.band = RTW89_PHY_0;
1489 opt.num_macc_role = 0;
1490 opt.mlo_mode = rtwdev->mlo_dbcc_mode;
1491 opt.num_opch = 0;
1492 opt.opch_end = RTW89_CHAN_INVALID;
1493 }
1494
1495 mac->scan_offload(rtwdev, &opt, rtwvif_link, true);
1496
1497 return 0;
1498 }
1499
rtw89_wow_fw_start(struct rtw89_dev * rtwdev)1500 static int rtw89_wow_fw_start(struct rtw89_dev *rtwdev)
1501 {
1502 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1503 struct rtw89_vif_link *rtwvif_link = rtw_wow->rtwvif_link;
1504 int ret;
1505
1506 if (rtw89_wow_no_link(rtwdev)) {
1507 ret = rtw89_pno_scan_offload(rtwdev, false);
1508 if (ret) {
1509 rtw89_err(rtwdev, "wow: failed to disable pno scan offload\n");
1510 return ret;
1511 }
1512
1513 ret = rtw89_pno_scan_offload(rtwdev, true);
1514 if (ret) {
1515 rtw89_err(rtwdev, "wow: failed to enable pno scan offload\n");
1516 return ret;
1517 }
1518 } else {
1519 rtw89_wow_pattern_write(rtwdev);
1520 rtw89_wow_construct_key_info(rtwdev);
1521
1522 ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif_link, true);
1523 if (ret) {
1524 rtw89_err(rtwdev, "wow: failed to enable keep alive\n");
1525 return ret;
1526 }
1527
1528 ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif_link, true);
1529 if (ret) {
1530 rtw89_err(rtwdev, "wow: failed to enable disconnect detect\n");
1531 return ret;
1532 }
1533
1534 ret = rtw89_fw_h2c_wow_gtk_ofld(rtwdev, rtwvif_link, true);
1535 if (ret) {
1536 rtw89_err(rtwdev, "wow: failed to enable GTK offload\n");
1537 return ret;
1538 }
1539
1540 ret = rtw89_fw_h2c_arp_offload(rtwdev, rtwvif_link, true);
1541 if (ret)
1542 rtw89_warn(rtwdev, "wow: failed to enable arp offload\n");
1543 }
1544
1545 if (rtw89_wow_no_link(rtwdev)) {
1546 ret = rtw89_wow_cfg_wake_pno(rtwdev, true);
1547 if (ret) {
1548 rtw89_err(rtwdev, "wow: failed to config wake PNO\n");
1549 return ret;
1550 }
1551 } else {
1552 ret = rtw89_wow_cfg_wake(rtwdev, true);
1553 if (ret) {
1554 rtw89_err(rtwdev, "wow: failed to config wake\n");
1555 return ret;
1556 }
1557 }
1558
1559 ret = rtw89_wow_check_fw_status(rtwdev, true);
1560 if (ret) {
1561 rtw89_err(rtwdev, "wow: failed to check enable fw ready\n");
1562 return ret;
1563 }
1564
1565 return 0;
1566 }
1567
rtw89_wow_fw_stop(struct rtw89_dev * rtwdev)1568 static int rtw89_wow_fw_stop(struct rtw89_dev *rtwdev)
1569 {
1570 struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1571 struct rtw89_vif_link *rtwvif_link = rtw_wow->rtwvif_link;
1572 int ret;
1573
1574 if (rtw89_wow_no_link(rtwdev)) {
1575 ret = rtw89_pno_scan_offload(rtwdev, false);
1576 if (ret) {
1577 rtw89_err(rtwdev, "wow: failed to disable pno scan offload\n");
1578 return ret;
1579 }
1580
1581 ret = rtw89_fw_h2c_cfg_pno(rtwdev, rtwvif_link, false);
1582 if (ret) {
1583 rtw89_err(rtwdev, "wow: failed to disable pno\n");
1584 return ret;
1585 }
1586
1587 rtw89_fw_release_pno_pkt_list(rtwdev, rtwvif_link);
1588 } else {
1589 rtw89_wow_pattern_clear(rtwdev);
1590
1591 ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif_link, false);
1592 if (ret) {
1593 rtw89_err(rtwdev, "wow: failed to disable keep alive\n");
1594 return ret;
1595 }
1596
1597 ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif_link, false);
1598 if (ret) {
1599 rtw89_err(rtwdev, "wow: failed to disable disconnect detect\n");
1600 return ret;
1601 }
1602
1603 ret = rtw89_fw_h2c_wow_gtk_ofld(rtwdev, rtwvif_link, false);
1604 if (ret) {
1605 rtw89_err(rtwdev, "wow: failed to disable GTK offload\n");
1606 return ret;
1607 }
1608
1609 ret = rtw89_fw_h2c_arp_offload(rtwdev, rtwvif_link, false);
1610 if (ret)
1611 rtw89_warn(rtwdev, "wow: failed to disable arp offload\n");
1612
1613 rtw89_wow_key_clear(rtwdev);
1614 rtw89_fw_release_general_pkt_list(rtwdev, true);
1615 }
1616
1617
1618 ret = rtw89_wow_cfg_wake(rtwdev, false);
1619 if (ret) {
1620 rtw89_err(rtwdev, "wow: failed to disable config wake\n");
1621 return ret;
1622 }
1623
1624 ret = rtw89_wow_check_fw_status(rtwdev, false);
1625 if (ret) {
1626 rtw89_err(rtwdev, "wow: failed to check disable fw ready\n");
1627 return ret;
1628 }
1629
1630 return 0;
1631 }
1632
rtw89_wow_enable(struct rtw89_dev * rtwdev)1633 static int rtw89_wow_enable(struct rtw89_dev *rtwdev)
1634 {
1635 int ret;
1636
1637 set_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
1638
1639 ret = rtw89_wow_enable_trx_pre(rtwdev);
1640 if (ret) {
1641 rtw89_err(rtwdev, "wow: failed to enable trx_pre\n");
1642 goto out;
1643 }
1644
1645 rtw89_fw_release_general_pkt_list(rtwdev, true);
1646
1647 ret = rtw89_wow_swap_fw(rtwdev, true);
1648 if (ret) {
1649 rtw89_err(rtwdev, "wow: failed to swap to wow fw\n");
1650 goto out;
1651 }
1652
1653 ret = rtw89_wow_fw_start(rtwdev);
1654 if (ret) {
1655 rtw89_err(rtwdev, "wow: failed to let wow fw start\n");
1656 goto out;
1657 }
1658
1659 rtw89_wow_enter_ps(rtwdev);
1660
1661 ret = rtw89_wow_enable_trx_post(rtwdev);
1662 if (ret) {
1663 rtw89_err(rtwdev, "wow: failed to enable trx_post\n");
1664 goto out;
1665 }
1666
1667 return 0;
1668
1669 out:
1670 clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
1671 return ret;
1672 }
1673
rtw89_wow_disable(struct rtw89_dev * rtwdev)1674 static int rtw89_wow_disable(struct rtw89_dev *rtwdev)
1675 {
1676 int ret;
1677
1678 ret = rtw89_wow_disable_trx_pre(rtwdev);
1679 if (ret) {
1680 rtw89_err(rtwdev, "wow: failed to disable trx_pre\n");
1681 goto out;
1682 }
1683
1684 rtw89_wow_leave_ps(rtwdev, false);
1685
1686 ret = rtw89_wow_fw_stop(rtwdev);
1687 if (ret) {
1688 rtw89_err(rtwdev, "wow: failed to swap to normal fw\n");
1689 goto out;
1690 }
1691
1692 ret = rtw89_wow_swap_fw(rtwdev, false);
1693 if (ret) {
1694 rtw89_err(rtwdev, "wow: failed to disable trx_post\n");
1695 goto out;
1696 }
1697
1698 ret = rtw89_wow_disable_trx_post(rtwdev);
1699 if (ret) {
1700 rtw89_err(rtwdev, "wow: failed to disable trx_pre\n");
1701 goto out;
1702 }
1703
1704 out:
1705 clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
1706 return ret;
1707 }
1708
rtw89_wow_restore_ps(struct rtw89_dev * rtwdev)1709 static void rtw89_wow_restore_ps(struct rtw89_dev *rtwdev)
1710 {
1711 if (rtw89_wow_no_link(rtwdev))
1712 rtw89_enter_ips(rtwdev);
1713 }
1714
rtw89_wow_resume(struct rtw89_dev * rtwdev)1715 int rtw89_wow_resume(struct rtw89_dev *rtwdev)
1716 {
1717 int ret;
1718
1719 if (!test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags)) {
1720 rtw89_err(rtwdev, "wow is not enabled\n");
1721 ret = -EPERM;
1722 goto out;
1723 }
1724
1725 if (!rtw89_mac_get_power_state(rtwdev)) {
1726 rtw89_err(rtwdev, "chip is no power when resume\n");
1727 ret = -EPERM;
1728 goto out;
1729 }
1730
1731 rtw89_wow_leave_deep_ps(rtwdev);
1732
1733 rtw89_wow_show_wakeup_reason(rtwdev);
1734
1735 ret = rtw89_wow_disable(rtwdev);
1736 if (ret)
1737 rtw89_err(rtwdev, "failed to disable wow\n");
1738
1739 rtw89_wow_restore_ps(rtwdev);
1740 out:
1741 rtw89_wow_clear_wakeups(rtwdev);
1742 return ret;
1743 }
1744
rtw89_wow_suspend(struct rtw89_dev * rtwdev,struct cfg80211_wowlan * wowlan)1745 int rtw89_wow_suspend(struct rtw89_dev *rtwdev, struct cfg80211_wowlan *wowlan)
1746 {
1747 int ret;
1748
1749 ret = rtw89_wow_set_wakeups(rtwdev, wowlan);
1750 if (ret) {
1751 rtw89_err(rtwdev, "failed to set wakeup event\n");
1752 return ret;
1753 }
1754
1755 rtw89_wow_leave_ps(rtwdev, true);
1756
1757 ret = rtw89_wow_enable(rtwdev);
1758 if (ret) {
1759 rtw89_err(rtwdev, "failed to enable wow\n");
1760 return ret;
1761 }
1762
1763 rtw89_wow_enter_deep_ps(rtwdev);
1764
1765 return 0;
1766 }
1767