xref: /linux/drivers/net/wireless/realtek/rtw89/wow.c (revision 30ce797d)
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 ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
425 	bool err = false;
426 
427 	rcu_read_lock();
428 	ieee80211_iter_keys_rcu(rtwdev->hw, wow_vif,
429 				rtw89_wow_get_key_info_iter, &err);
430 	rcu_read_unlock();
431 
432 	if (err) {
433 		rtw89_wow_key_clear(rtwdev);
434 		return;
435 	}
436 
437 	key_info->valid_check = RTW89_WOW_VALID_CHECK;
438 	key_info->symbol_check_en = RTW89_WOW_SYMBOL_CHK_PTK |
439 				    RTW89_WOW_SYMBOL_CHK_GTK;
440 }
441 
rtw89_wow_debug_aoac_rpt(struct rtw89_dev * rtwdev)442 static void rtw89_wow_debug_aoac_rpt(struct rtw89_dev *rtwdev)
443 {
444 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
445 	struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
446 
447 	if (!rtw89_debug_is_enabled(rtwdev, RTW89_DBG_WOW))
448 		return;
449 
450 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] rpt_ver = %d\n",
451 		    aoac_rpt->rpt_ver);
452 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] sec_type = %d\n",
453 		    aoac_rpt->sec_type);
454 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] key_idx = %d\n",
455 		    aoac_rpt->key_idx);
456 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] pattern_idx = %d\n",
457 		    aoac_rpt->pattern_idx);
458 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] rekey_ok = %d\n",
459 		    aoac_rpt->rekey_ok);
460 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] ptk_tx_iv = %*ph\n",
461 		    8, aoac_rpt->ptk_tx_iv);
462 	rtw89_debug(rtwdev, RTW89_DBG_WOW,
463 		    "[aoac_rpt] eapol_key_replay_count = %*ph\n",
464 		    8, aoac_rpt->eapol_key_replay_count);
465 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] ptk_rx_iv = %*ph\n",
466 		    8, aoac_rpt->ptk_rx_iv);
467 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[0] = %*ph\n",
468 		    8, aoac_rpt->gtk_rx_iv[0]);
469 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[1] = %*ph\n",
470 		    8, aoac_rpt->gtk_rx_iv[1]);
471 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[2] = %*ph\n",
472 		    8, aoac_rpt->gtk_rx_iv[2]);
473 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] gtk_rx_iv[3] = %*ph\n",
474 		    8, aoac_rpt->gtk_rx_iv[3]);
475 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] igtk_key_id = %llu\n",
476 		    aoac_rpt->igtk_key_id);
477 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] igtk_ipn = %llu\n",
478 		    aoac_rpt->igtk_ipn);
479 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "[aoac_rpt] igtk = %*ph\n",
480 		    32, aoac_rpt->igtk);
481 }
482 
rtw89_wow_get_aoac_rpt_reg(struct rtw89_dev * rtwdev)483 static int rtw89_wow_get_aoac_rpt_reg(struct rtw89_dev *rtwdev)
484 {
485 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
486 	struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
487 	struct rtw89_mac_c2h_info c2h_info = {};
488 	struct rtw89_mac_h2c_info h2c_info = {};
489 	u8 igtk_ipn[8];
490 	u8 key_idx;
491 	int ret;
492 
493 	h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_AOAC_RPT_1;
494 	h2c_info.content_len = 2;
495 	ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, &c2h_info);
496 	if (ret)
497 		return ret;
498 
499 	aoac_rpt->key_idx =
500 		u32_get_bits(c2h_info.u.c2hreg[0], RTW89_C2HREG_AOAC_RPT_1_W0_KEY_IDX);
501 	key_idx = aoac_rpt->key_idx;
502 	aoac_rpt->gtk_rx_iv[key_idx][0] =
503 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_0);
504 	aoac_rpt->gtk_rx_iv[key_idx][1] =
505 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_1);
506 	aoac_rpt->gtk_rx_iv[key_idx][2] =
507 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_2);
508 	aoac_rpt->gtk_rx_iv[key_idx][3] =
509 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_1_W1_IV_3);
510 	aoac_rpt->gtk_rx_iv[key_idx][4] =
511 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_4);
512 	aoac_rpt->gtk_rx_iv[key_idx][5] =
513 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_5);
514 	aoac_rpt->gtk_rx_iv[key_idx][6] =
515 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_6);
516 	aoac_rpt->gtk_rx_iv[key_idx][7] =
517 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_1_W2_IV_7);
518 	aoac_rpt->ptk_rx_iv[0] =
519 		u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_0);
520 	aoac_rpt->ptk_rx_iv[1] =
521 		u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_1);
522 	aoac_rpt->ptk_rx_iv[2] =
523 		u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_2);
524 	aoac_rpt->ptk_rx_iv[3] =
525 		u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_1_W3_PTK_IV_3);
526 
527 	h2c_info.id = RTW89_FWCMD_H2CREG_FUNC_AOAC_RPT_2;
528 	h2c_info.content_len = 2;
529 	ret = rtw89_fw_msg_reg(rtwdev, &h2c_info, &c2h_info);
530 	if (ret)
531 		return ret;
532 
533 	aoac_rpt->ptk_rx_iv[4] =
534 		u32_get_bits(c2h_info.u.c2hreg[0], RTW89_C2HREG_AOAC_RPT_2_W0_PTK_IV_4);
535 	aoac_rpt->ptk_rx_iv[5] =
536 		u32_get_bits(c2h_info.u.c2hreg[0], RTW89_C2HREG_AOAC_RPT_2_W0_PTK_IV_5);
537 	aoac_rpt->ptk_rx_iv[6] =
538 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_PTK_IV_6);
539 	aoac_rpt->ptk_rx_iv[7] =
540 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_PTK_IV_7);
541 	igtk_ipn[0] =
542 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_IGTK_IPN_IV_0);
543 	igtk_ipn[1] =
544 		u32_get_bits(c2h_info.u.c2hreg[1], RTW89_C2HREG_AOAC_RPT_2_W1_IGTK_IPN_IV_1);
545 	igtk_ipn[2] =
546 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_2);
547 	igtk_ipn[3] =
548 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_3);
549 	igtk_ipn[4] =
550 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_4);
551 	igtk_ipn[5] =
552 		u32_get_bits(c2h_info.u.c2hreg[2], RTW89_C2HREG_AOAC_RPT_2_W2_IGTK_IPN_IV_5);
553 	igtk_ipn[6] =
554 		u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_2_W3_IGTK_IPN_IV_6);
555 	igtk_ipn[7] =
556 		u32_get_bits(c2h_info.u.c2hreg[3], RTW89_C2HREG_AOAC_RPT_2_W3_IGTK_IPN_IV_7);
557 	aoac_rpt->igtk_ipn = u64_encode_bits(igtk_ipn[0], RTW89_IGTK_IPN_0) |
558 			     u64_encode_bits(igtk_ipn[1], RTW89_IGTK_IPN_1) |
559 			     u64_encode_bits(igtk_ipn[2], RTW89_IGTK_IPN_2) |
560 			     u64_encode_bits(igtk_ipn[3], RTW89_IGTK_IPN_3) |
561 			     u64_encode_bits(igtk_ipn[4], RTW89_IGTK_IPN_4) |
562 			     u64_encode_bits(igtk_ipn[5], RTW89_IGTK_IPN_5) |
563 			     u64_encode_bits(igtk_ipn[6], RTW89_IGTK_IPN_6) |
564 			     u64_encode_bits(igtk_ipn[7], RTW89_IGTK_IPN_7);
565 
566 	return 0;
567 }
568 
rtw89_wow_get_aoac_rpt(struct rtw89_dev * rtwdev,bool rx_ready)569 static int rtw89_wow_get_aoac_rpt(struct rtw89_dev *rtwdev, bool rx_ready)
570 {
571 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
572 	int ret;
573 
574 	if (!rtw_wow->ptk_alg)
575 		return -EPERM;
576 
577 	if (!rx_ready) {
578 		ret = rtw89_wow_get_aoac_rpt_reg(rtwdev);
579 		if (ret) {
580 			rtw89_err(rtwdev, "wow: failed to get aoac rpt by reg\n");
581 			return ret;
582 		}
583 	} else {
584 		ret = rtw89_fw_h2c_wow_request_aoac(rtwdev);
585 		if (ret) {
586 			rtw89_err(rtwdev, "wow: failed to get aoac rpt by pkt\n");
587 			return ret;
588 		}
589 	}
590 
591 	rtw89_wow_debug_aoac_rpt(rtwdev);
592 
593 	return 0;
594 }
595 
rtw89_wow_gtk_rekey(struct rtw89_dev * rtwdev,u32 cipher,u8 keyidx,u8 * gtk)596 static struct ieee80211_key_conf *rtw89_wow_gtk_rekey(struct rtw89_dev *rtwdev,
597 						      u32 cipher, u8 keyidx, u8 *gtk)
598 {
599 	struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
600 	const struct rtw89_cipher_info *cipher_info;
601 	struct ieee80211_key_conf *rekey_conf;
602 	struct ieee80211_key_conf *key;
603 	u8 sz;
604 
605 	cipher_info = rtw89_cipher_alg_recognize(cipher);
606 	sz = struct_size(rekey_conf, key, cipher_info->len);
607 	rekey_conf = kmalloc(sz, GFP_KERNEL);
608 	if (!rekey_conf)
609 		return NULL;
610 
611 	rekey_conf->cipher = cipher;
612 	rekey_conf->keyidx = keyidx;
613 	rekey_conf->keylen = cipher_info->len;
614 	memcpy(rekey_conf->key, gtk,
615 	       flex_array_size(rekey_conf, key, cipher_info->len));
616 
617 	/* ieee80211_gtk_rekey_add() will call set_key(), therefore we
618 	 * need to unlock mutex
619 	 */
620 	mutex_unlock(&rtwdev->mutex);
621 	key = ieee80211_gtk_rekey_add(wow_vif, rekey_conf, -1);
622 	mutex_lock(&rtwdev->mutex);
623 
624 	kfree(rekey_conf);
625 	if (IS_ERR(key)) {
626 		rtw89_err(rtwdev, "ieee80211_gtk_rekey_add failed\n");
627 		return NULL;
628 	}
629 
630 	return key;
631 }
632 
rtw89_wow_update_key_info(struct rtw89_dev * rtwdev,bool rx_ready)633 static void rtw89_wow_update_key_info(struct rtw89_dev *rtwdev, bool rx_ready)
634 {
635 	struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
636 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
637 	struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
638 	struct rtw89_set_key_info_iter_data data = {.error = false,
639 						    .rx_ready = rx_ready};
640 	struct ieee80211_key_conf *key;
641 
642 	rcu_read_lock();
643 	ieee80211_iter_keys_rcu(rtwdev->hw, wow_vif,
644 				rtw89_wow_set_key_info_iter, &data);
645 	rcu_read_unlock();
646 
647 	if (data.error) {
648 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "%s error\n", __func__);
649 		return;
650 	}
651 
652 	if (!data.gtk_cipher)
653 		return;
654 
655 	key = rtw89_wow_gtk_rekey(rtwdev, data.gtk_cipher, aoac_rpt->key_idx,
656 				  aoac_rpt->gtk);
657 	if (!key)
658 		return;
659 
660 	rtw89_rx_iv_to_pn(rtwdev, key,
661 			  aoac_rpt->gtk_rx_iv[key->keyidx]);
662 
663 	if (!data.igtk_cipher)
664 		return;
665 
666 	key = rtw89_wow_gtk_rekey(rtwdev, data.igtk_cipher, aoac_rpt->igtk_key_id,
667 				  aoac_rpt->igtk);
668 	if (!key)
669 		return;
670 
671 	rtw89_rx_pn_set_pmf(rtwdev, key, aoac_rpt->igtk_ipn);
672 	ieee80211_gtk_rekey_notify(wow_vif, wow_vif->bss_conf.bssid,
673 				   aoac_rpt->eapol_key_replay_count,
674 				   GFP_KERNEL);
675 }
676 
rtw89_wow_leave_deep_ps(struct rtw89_dev * rtwdev)677 static void rtw89_wow_leave_deep_ps(struct rtw89_dev *rtwdev)
678 {
679 	__rtw89_leave_ps_mode(rtwdev);
680 }
681 
rtw89_wow_enter_deep_ps(struct rtw89_dev * rtwdev)682 static void rtw89_wow_enter_deep_ps(struct rtw89_dev *rtwdev)
683 {
684 	struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
685 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
686 
687 	__rtw89_enter_ps_mode(rtwdev, rtwvif);
688 }
689 
rtw89_wow_enter_ps(struct rtw89_dev * rtwdev)690 static void rtw89_wow_enter_ps(struct rtw89_dev *rtwdev)
691 {
692 	struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
693 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
694 
695 	if (rtw89_wow_mgd_linked(rtwdev))
696 		rtw89_enter_lps(rtwdev, rtwvif, false);
697 	else if (rtw89_wow_no_link(rtwdev))
698 		rtw89_fw_h2c_fwips(rtwdev, rtwvif, true);
699 }
700 
rtw89_wow_leave_ps(struct rtw89_dev * rtwdev,bool enable_wow)701 static void rtw89_wow_leave_ps(struct rtw89_dev *rtwdev, bool enable_wow)
702 {
703 	struct ieee80211_vif *wow_vif = rtwdev->wow.wow_vif;
704 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
705 
706 	if (rtw89_wow_mgd_linked(rtwdev)) {
707 		rtw89_leave_lps(rtwdev);
708 	} else if (rtw89_wow_no_link(rtwdev)) {
709 		if (enable_wow)
710 			rtw89_leave_ips(rtwdev);
711 		else
712 			rtw89_fw_h2c_fwips(rtwdev, rtwvif, false);
713 	}
714 }
715 
rtw89_wow_config_mac(struct rtw89_dev * rtwdev,bool enable_wow)716 static int rtw89_wow_config_mac(struct rtw89_dev *rtwdev, bool enable_wow)
717 {
718 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
719 
720 	return mac->wow_config_mac(rtwdev, enable_wow);
721 }
722 
rtw89_wow_set_rx_filter(struct rtw89_dev * rtwdev,bool enable)723 static void rtw89_wow_set_rx_filter(struct rtw89_dev *rtwdev, bool enable)
724 {
725 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
726 	enum rtw89_mac_fwd_target fwd_target = enable ?
727 					       RTW89_FWD_DONT_CARE :
728 					       RTW89_FWD_TO_HOST;
729 
730 	mac->typ_fltr_opt(rtwdev, RTW89_MGNT, fwd_target, RTW89_MAC_0);
731 	mac->typ_fltr_opt(rtwdev, RTW89_CTRL, fwd_target, RTW89_MAC_0);
732 	mac->typ_fltr_opt(rtwdev, RTW89_DATA, fwd_target, RTW89_MAC_0);
733 }
734 
rtw89_wow_show_wakeup_reason(struct rtw89_dev * rtwdev)735 static void rtw89_wow_show_wakeup_reason(struct rtw89_dev *rtwdev)
736 {
737 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
738 	struct rtw89_wow_aoac_report *aoac_rpt = &rtw_wow->aoac_rpt;
739 	struct cfg80211_wowlan_nd_info nd_info;
740 	struct cfg80211_wowlan_wakeup wakeup = {
741 		.pattern_idx = -1,
742 	};
743 	u32 wow_reason_reg;
744 	u8 reason;
745 
746 	if (RTW89_CHK_FW_FEATURE(WOW_REASON_V1, &rtwdev->fw))
747 		wow_reason_reg = rtwdev->chip->wow_reason_reg[RTW89_WOW_REASON_V1];
748 	else
749 		wow_reason_reg = rtwdev->chip->wow_reason_reg[RTW89_WOW_REASON_V0];
750 
751 	reason = rtw89_read8(rtwdev, wow_reason_reg);
752 	switch (reason) {
753 	case RTW89_WOW_RSN_RX_DEAUTH:
754 		wakeup.disconnect = true;
755 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx deauth\n");
756 		break;
757 	case RTW89_WOW_RSN_DISCONNECT:
758 		wakeup.disconnect = true;
759 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: AP is off\n");
760 		break;
761 	case RTW89_WOW_RSN_RX_MAGIC_PKT:
762 		wakeup.magic_pkt = true;
763 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx magic packet\n");
764 		break;
765 	case RTW89_WOW_RSN_RX_GTK_REKEY:
766 		wakeup.gtk_rekey_failure = true;
767 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx gtk rekey\n");
768 		break;
769 	case RTW89_WOW_RSN_RX_PATTERN_MATCH:
770 		wakeup.pattern_idx = aoac_rpt->pattern_idx;
771 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: Rx pattern match packet\n");
772 		break;
773 	case RTW89_WOW_RSN_RX_NLO:
774 		/* Current firmware and driver don't report ssid index.
775 		 * Use 0 for n_matches based on its comment.
776 		 */
777 		nd_info.n_matches = 0;
778 		wakeup.net_detect = &nd_info;
779 		rtw89_debug(rtwdev, RTW89_DBG_WOW, "Rx NLO\n");
780 		break;
781 	default:
782 		rtw89_warn(rtwdev, "Unknown wakeup reason %x\n", reason);
783 		ieee80211_report_wowlan_wakeup(rtwdev->wow.wow_vif, NULL,
784 					       GFP_KERNEL);
785 		return;
786 	}
787 
788 	ieee80211_report_wowlan_wakeup(rtwdev->wow.wow_vif, &wakeup,
789 				       GFP_KERNEL);
790 }
791 
rtw89_wow_vif_iter(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)792 static void rtw89_wow_vif_iter(struct rtw89_dev *rtwdev, struct rtw89_vif *rtwvif)
793 {
794 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
795 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
796 
797 	/* Current WoWLAN function support setting of only vif in
798 	 * infra mode or no link mode. When one suitable vif is found,
799 	 * stop the iteration.
800 	 */
801 	if (rtw_wow->wow_vif || vif->type != NL80211_IFTYPE_STATION)
802 		return;
803 
804 	switch (rtwvif->net_type) {
805 	case RTW89_NET_TYPE_INFRA:
806 		if (rtw_wow_has_mgd_features(rtwdev))
807 			rtw_wow->wow_vif = vif;
808 		break;
809 	case RTW89_NET_TYPE_NO_LINK:
810 		if (rtw_wow->pno_inited)
811 			rtw_wow->wow_vif = vif;
812 		break;
813 	default:
814 		break;
815 	}
816 }
817 
__rtw89_cal_crc16(u8 data,u16 crc)818 static u16 __rtw89_cal_crc16(u8 data, u16 crc)
819 {
820 	u8 shift_in, data_bit;
821 	u8 crc_bit4, crc_bit11, crc_bit15;
822 	u16 crc_result;
823 	int index;
824 
825 	for (index = 0; index < 8; index++) {
826 		crc_bit15 = crc & BIT(15) ? 1 : 0;
827 		data_bit = data & BIT(index) ? 1 : 0;
828 		shift_in = crc_bit15 ^ data_bit;
829 
830 		crc_result = crc << 1;
831 
832 		if (shift_in == 0)
833 			crc_result &= ~BIT(0);
834 		else
835 			crc_result |= BIT(0);
836 
837 		crc_bit11 = (crc & BIT(11) ? 1 : 0) ^ shift_in;
838 
839 		if (crc_bit11 == 0)
840 			crc_result &= ~BIT(12);
841 		else
842 			crc_result |= BIT(12);
843 
844 		crc_bit4 = (crc & BIT(4) ? 1 : 0) ^ shift_in;
845 
846 		if (crc_bit4 == 0)
847 			crc_result &= ~BIT(5);
848 		else
849 			crc_result |= BIT(5);
850 
851 		crc = crc_result;
852 	}
853 	return crc;
854 }
855 
rtw89_calc_crc(u8 * pdata,int length)856 static u16 rtw89_calc_crc(u8 *pdata, int length)
857 {
858 	u16 crc = 0xffff;
859 	int i;
860 
861 	for (i = 0; i < length; i++)
862 		crc = __rtw89_cal_crc16(pdata[i], crc);
863 
864 	/* get 1' complement */
865 	return ~crc;
866 }
867 
rtw89_wow_pattern_get_type(struct rtw89_vif * rtwvif,struct rtw89_wow_cam_info * rtw_pattern,const u8 * pattern,u8 da_mask)868 static int rtw89_wow_pattern_get_type(struct rtw89_vif *rtwvif,
869 				      struct rtw89_wow_cam_info *rtw_pattern,
870 				      const u8 *pattern, u8 da_mask)
871 {
872 	u8 da[ETH_ALEN];
873 
874 	ether_addr_copy_mask(da, pattern, da_mask);
875 
876 	/* Each pattern is divided into different kinds by DA address
877 	 *  a. DA is broadcast address: set bc = 0;
878 	 *  b. DA is multicast address: set mc = 0
879 	 *  c. DA is unicast address same as dev's mac address: set uc = 0
880 	 *  d. DA is unmasked. Also called wildcard type: set uc = bc = mc = 0
881 	 *  e. Others is invalid type.
882 	 */
883 
884 	if (is_broadcast_ether_addr(da))
885 		rtw_pattern->bc = true;
886 	else if (is_multicast_ether_addr(da))
887 		rtw_pattern->mc = true;
888 	else if (ether_addr_equal(da, rtwvif->mac_addr) &&
889 		 da_mask == GENMASK(5, 0))
890 		rtw_pattern->uc = true;
891 	else if (!da_mask) /*da_mask == 0 mean wildcard*/
892 		return 0;
893 	else
894 		return -EPERM;
895 
896 	return 0;
897 }
898 
rtw89_wow_pattern_generate(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,const struct cfg80211_pkt_pattern * pkt_pattern,struct rtw89_wow_cam_info * rtw_pattern)899 static int rtw89_wow_pattern_generate(struct rtw89_dev *rtwdev,
900 				      struct rtw89_vif *rtwvif,
901 				      const struct cfg80211_pkt_pattern *pkt_pattern,
902 				      struct rtw89_wow_cam_info *rtw_pattern)
903 {
904 	u8 mask_hw[RTW89_MAX_PATTERN_MASK_SIZE * 4] = {0};
905 	u8 content[RTW89_MAX_PATTERN_SIZE] = {0};
906 	const u8 *mask;
907 	const u8 *pattern;
908 	u8 mask_len;
909 	u16 count;
910 	u32 len;
911 	int i, ret;
912 
913 	pattern = pkt_pattern->pattern;
914 	len = pkt_pattern->pattern_len;
915 	mask = pkt_pattern->mask;
916 	mask_len = DIV_ROUND_UP(len, 8);
917 	memset(rtw_pattern, 0, sizeof(*rtw_pattern));
918 
919 	ret = rtw89_wow_pattern_get_type(rtwvif, rtw_pattern, pattern,
920 					 mask[0] & GENMASK(5, 0));
921 	if (ret)
922 		return ret;
923 
924 	/* translate mask from os to mask for hw
925 	 * pattern from OS uses 'ethenet frame', like this:
926 	 * |    6   |    6   |   2  |     20    |  Variable  |  4  |
927 	 * |--------+--------+------+-----------+------------+-----|
928 	 * |    802.3 Mac Header    | IP Header | TCP Packet | FCS |
929 	 * |   DA   |   SA   | Type |
930 	 *
931 	 * BUT, packet catched by our HW is in '802.11 frame', begin from LLC
932 	 * |     24 or 30      |    6   |   2  |     20    |  Variable  |  4  |
933 	 * |-------------------+--------+------+-----------+------------+-----|
934 	 * | 802.11 MAC Header |       LLC     | IP Header | TCP Packet | FCS |
935 	 *		       | Others | Tpye |
936 	 *
937 	 * Therefore, we need translate mask_from_OS to mask_to_hw.
938 	 * We should left-shift mask by 6 bits, then set the new bit[0~5] = 0,
939 	 * because new mask[0~5] means 'SA', but our HW packet begins from LLC,
940 	 * bit[0~5] corresponds to first 6 Bytes in LLC, they just don't match.
941 	 */
942 
943 	/* Shift 6 bits */
944 	for (i = 0; i < mask_len - 1; i++) {
945 		mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6)) |
946 			     u8_get_bits(mask[i + 1], GENMASK(5, 0)) << 2;
947 	}
948 	mask_hw[i] = u8_get_bits(mask[i], GENMASK(7, 6));
949 
950 	/* Set bit 0-5 to zero */
951 	mask_hw[0] &= ~GENMASK(5, 0);
952 
953 	memcpy(rtw_pattern->mask, mask_hw, sizeof(rtw_pattern->mask));
954 
955 	/* To get the wake up pattern from the mask.
956 	 * We do not count first 12 bits which means
957 	 * DA[6] and SA[6] in the pattern to match HW design.
958 	 */
959 	count = 0;
960 	for (i = 12; i < len; i++) {
961 		if ((mask[i / 8] >> (i % 8)) & 0x01) {
962 			content[count] = pattern[i];
963 			count++;
964 		}
965 	}
966 
967 	rtw_pattern->crc = rtw89_calc_crc(content, count);
968 
969 	return 0;
970 }
971 
rtw89_wow_parse_patterns(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif,struct cfg80211_wowlan * wowlan)972 static int rtw89_wow_parse_patterns(struct rtw89_dev *rtwdev,
973 				    struct rtw89_vif *rtwvif,
974 				    struct cfg80211_wowlan *wowlan)
975 {
976 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
977 	struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
978 	int i;
979 	int ret;
980 
981 	if (!wowlan->n_patterns || !wowlan->patterns)
982 		return 0;
983 
984 	for (i = 0; i < wowlan->n_patterns; i++) {
985 		rtw_pattern = &rtw_wow->patterns[i];
986 		ret = rtw89_wow_pattern_generate(rtwdev, rtwvif,
987 						 &wowlan->patterns[i],
988 						 rtw_pattern);
989 		if (ret) {
990 			rtw89_err(rtwdev, "failed to generate pattern(%d)\n", i);
991 			rtw_wow->pattern_cnt = 0;
992 			return ret;
993 		}
994 
995 		rtw_pattern->r_w = true;
996 		rtw_pattern->idx = i;
997 		rtw_pattern->negative_pattern_match = false;
998 		rtw_pattern->skip_mac_hdr = true;
999 		rtw_pattern->valid = true;
1000 	}
1001 	rtw_wow->pattern_cnt = wowlan->n_patterns;
1002 
1003 	return 0;
1004 }
1005 
rtw89_wow_pattern_clear_cam(struct rtw89_dev * rtwdev)1006 static void rtw89_wow_pattern_clear_cam(struct rtw89_dev *rtwdev)
1007 {
1008 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1009 	struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
1010 	int i = 0;
1011 
1012 	for (i = 0; i < rtw_wow->pattern_cnt; i++) {
1013 		rtw_pattern = &rtw_wow->patterns[i];
1014 		rtw_pattern->valid = false;
1015 		rtw89_fw_wow_cam_update(rtwdev, rtw_pattern);
1016 	}
1017 }
1018 
rtw89_wow_pattern_write(struct rtw89_dev * rtwdev)1019 static void rtw89_wow_pattern_write(struct rtw89_dev *rtwdev)
1020 {
1021 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1022 	struct rtw89_wow_cam_info *rtw_pattern = rtw_wow->patterns;
1023 	int i;
1024 
1025 	for (i = 0; i < rtw_wow->pattern_cnt; i++)
1026 		rtw89_fw_wow_cam_update(rtwdev, rtw_pattern + i);
1027 }
1028 
rtw89_wow_pattern_clear(struct rtw89_dev * rtwdev)1029 static void rtw89_wow_pattern_clear(struct rtw89_dev *rtwdev)
1030 {
1031 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1032 
1033 	rtw89_wow_pattern_clear_cam(rtwdev);
1034 
1035 	rtw_wow->pattern_cnt = 0;
1036 	memset(rtw_wow->patterns, 0, sizeof(rtw_wow->patterns));
1037 }
1038 
rtw89_wow_clear_wakeups(struct rtw89_dev * rtwdev)1039 static void rtw89_wow_clear_wakeups(struct rtw89_dev *rtwdev)
1040 {
1041 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1042 
1043 	rtw_wow->wow_vif = NULL;
1044 	rtw89_core_release_all_bits_map(rtw_wow->flags, RTW89_WOW_FLAG_NUM);
1045 	rtw_wow->pattern_cnt = 0;
1046 	rtw_wow->pno_inited = false;
1047 }
1048 
rtw89_wow_init_pno(struct rtw89_dev * rtwdev,struct cfg80211_sched_scan_request * nd_config)1049 static void rtw89_wow_init_pno(struct rtw89_dev *rtwdev,
1050 			       struct cfg80211_sched_scan_request *nd_config)
1051 {
1052 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1053 
1054 	if (!nd_config->n_match_sets || !nd_config->n_channels)
1055 		return;
1056 
1057 	rtw_wow->nd_config = nd_config;
1058 	rtw_wow->pno_inited = true;
1059 
1060 	INIT_LIST_HEAD(&rtw_wow->pno_pkt_list);
1061 
1062 	rtw89_debug(rtwdev, RTW89_DBG_WOW, "WOW: net-detect is enabled\n");
1063 }
1064 
rtw89_wow_set_wakeups(struct rtw89_dev * rtwdev,struct cfg80211_wowlan * wowlan)1065 static int rtw89_wow_set_wakeups(struct rtw89_dev *rtwdev,
1066 				 struct cfg80211_wowlan *wowlan)
1067 {
1068 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1069 	struct rtw89_vif *rtwvif;
1070 
1071 	if (wowlan->disconnect)
1072 		set_bit(RTW89_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags);
1073 	if (wowlan->magic_pkt)
1074 		set_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags);
1075 	if (wowlan->n_patterns && wowlan->patterns)
1076 		set_bit(RTW89_WOW_FLAG_EN_PATTERN, rtw_wow->flags);
1077 
1078 	if (wowlan->nd_config)
1079 		rtw89_wow_init_pno(rtwdev, wowlan->nd_config);
1080 
1081 	rtw89_for_each_rtwvif(rtwdev, rtwvif)
1082 		rtw89_wow_vif_iter(rtwdev, rtwvif);
1083 
1084 	if (!rtw_wow->wow_vif)
1085 		return -EPERM;
1086 
1087 	rtwvif = (struct rtw89_vif *)rtw_wow->wow_vif->drv_priv;
1088 	return rtw89_wow_parse_patterns(rtwdev, rtwvif, wowlan);
1089 }
1090 
rtw89_wow_cfg_wake_pno(struct rtw89_dev * rtwdev,bool wow)1091 static int rtw89_wow_cfg_wake_pno(struct rtw89_dev *rtwdev, bool wow)
1092 {
1093 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1094 	struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
1095 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
1096 	int ret;
1097 
1098 	ret = rtw89_fw_h2c_cfg_pno(rtwdev, rtwvif, true);
1099 	if (ret) {
1100 		rtw89_err(rtwdev, "failed to config pno\n");
1101 		return ret;
1102 	}
1103 
1104 	ret = rtw89_fw_h2c_wow_wakeup_ctrl(rtwdev, rtwvif, wow);
1105 	if (ret) {
1106 		rtw89_err(rtwdev, "failed to fw wow wakeup ctrl\n");
1107 		return ret;
1108 	}
1109 
1110 	ret = rtw89_fw_h2c_wow_global(rtwdev, rtwvif, wow);
1111 	if (ret) {
1112 		rtw89_err(rtwdev, "failed to fw wow global\n");
1113 		return ret;
1114 	}
1115 
1116 	return 0;
1117 }
1118 
rtw89_wow_cfg_wake(struct rtw89_dev * rtwdev,bool wow)1119 static int rtw89_wow_cfg_wake(struct rtw89_dev *rtwdev, bool wow)
1120 {
1121 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1122 	struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
1123 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
1124 	struct ieee80211_sta *wow_sta;
1125 	struct rtw89_sta *rtwsta = NULL;
1126 	int ret;
1127 
1128 	wow_sta = ieee80211_find_sta(wow_vif, rtwvif->bssid);
1129 	if (wow_sta)
1130 		rtwsta = (struct rtw89_sta *)wow_sta->drv_priv;
1131 
1132 	if (wow) {
1133 		if (rtw_wow->pattern_cnt)
1134 			rtwvif->wowlan_pattern = true;
1135 		if (test_bit(RTW89_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
1136 			rtwvif->wowlan_magic = true;
1137 	} else {
1138 		rtwvif->wowlan_pattern = false;
1139 		rtwvif->wowlan_magic = false;
1140 	}
1141 
1142 	ret = rtw89_fw_h2c_wow_wakeup_ctrl(rtwdev, rtwvif, wow);
1143 	if (ret) {
1144 		rtw89_err(rtwdev, "failed to fw wow wakeup ctrl\n");
1145 		return ret;
1146 	}
1147 
1148 	if (wow) {
1149 		ret = rtw89_chip_h2c_dctl_sec_cam(rtwdev, rtwvif, rtwsta);
1150 		if (ret) {
1151 			rtw89_err(rtwdev, "failed to update dctl cam sec entry: %d\n",
1152 				  ret);
1153 			return ret;
1154 		}
1155 	}
1156 
1157 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
1158 	if (ret) {
1159 		rtw89_warn(rtwdev, "failed to send h2c cam\n");
1160 		return ret;
1161 	}
1162 
1163 	ret = rtw89_fw_h2c_wow_global(rtwdev, rtwvif, wow);
1164 	if (ret) {
1165 		rtw89_err(rtwdev, "failed to fw wow global\n");
1166 		return ret;
1167 	}
1168 
1169 	return 0;
1170 }
1171 
rtw89_wow_check_fw_status(struct rtw89_dev * rtwdev,bool wow_enable)1172 static int rtw89_wow_check_fw_status(struct rtw89_dev *rtwdev, bool wow_enable)
1173 {
1174 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
1175 	u8 polling;
1176 	int ret;
1177 
1178 	ret = read_poll_timeout_atomic(rtw89_read8_mask, polling,
1179 				       wow_enable == !!polling,
1180 				       50, 50000, false, rtwdev,
1181 				       mac->wow_ctrl.addr, mac->wow_ctrl.mask);
1182 	if (ret)
1183 		rtw89_err(rtwdev, "failed to check wow status %s\n",
1184 			  wow_enable ? "enabled" : "disabled");
1185 	return ret;
1186 }
1187 
rtw89_wow_swap_fw(struct rtw89_dev * rtwdev,bool wow)1188 static int rtw89_wow_swap_fw(struct rtw89_dev *rtwdev, bool wow)
1189 {
1190 	enum rtw89_fw_type fw_type = wow ? RTW89_FW_WOWLAN : RTW89_FW_NORMAL;
1191 	enum rtw89_chip_gen chip_gen = rtwdev->chip->chip_gen;
1192 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1193 	struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
1194 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
1195 	enum rtw89_core_chip_id chip_id = rtwdev->chip->chip_id;
1196 	const struct rtw89_chip_info *chip = rtwdev->chip;
1197 	bool include_bb = !!chip->bbmcu_nr;
1198 	bool disable_intr_for_dlfw = false;
1199 	struct ieee80211_sta *wow_sta;
1200 	struct rtw89_sta *rtwsta = NULL;
1201 	bool is_conn = true;
1202 	int ret;
1203 
1204 	if (chip_id == RTL8852C || chip_id == RTL8922A)
1205 		disable_intr_for_dlfw = true;
1206 
1207 	wow_sta = ieee80211_find_sta(wow_vif, rtwvif->bssid);
1208 	if (wow_sta)
1209 		rtwsta = (struct rtw89_sta *)wow_sta->drv_priv;
1210 	else
1211 		is_conn = false;
1212 
1213 	if (disable_intr_for_dlfw)
1214 		rtw89_hci_disable_intr(rtwdev);
1215 
1216 	ret = rtw89_fw_download(rtwdev, fw_type, include_bb);
1217 	if (ret) {
1218 		rtw89_warn(rtwdev, "download fw failed\n");
1219 		return ret;
1220 	}
1221 
1222 	if (disable_intr_for_dlfw)
1223 		rtw89_hci_enable_intr(rtwdev);
1224 
1225 	rtw89_phy_init_rf_reg(rtwdev, true);
1226 
1227 	ret = rtw89_fw_h2c_role_maintain(rtwdev, rtwvif, rtwsta,
1228 					 RTW89_ROLE_FW_RESTORE);
1229 	if (ret) {
1230 		rtw89_warn(rtwdev, "failed to send h2c role maintain\n");
1231 		return ret;
1232 	}
1233 
1234 	ret = rtw89_chip_h2c_assoc_cmac_tbl(rtwdev, wow_vif, wow_sta);
1235 	if (ret) {
1236 		rtw89_warn(rtwdev, "failed to send h2c assoc cmac tbl\n");
1237 		return ret;
1238 	}
1239 
1240 	if (!is_conn)
1241 		rtw89_cam_reset_keys(rtwdev);
1242 
1243 	ret = rtw89_fw_h2c_join_info(rtwdev, rtwvif, rtwsta, !is_conn);
1244 	if (ret) {
1245 		rtw89_warn(rtwdev, "failed to send h2c join info\n");
1246 		return ret;
1247 	}
1248 
1249 	ret = rtw89_fw_h2c_cam(rtwdev, rtwvif, rtwsta, NULL);
1250 	if (ret) {
1251 		rtw89_warn(rtwdev, "failed to send h2c cam\n");
1252 		return ret;
1253 	}
1254 
1255 	if (is_conn) {
1256 		ret = rtw89_fw_h2c_general_pkt(rtwdev, rtwvif, rtwsta->mac_id);
1257 		if (ret) {
1258 			rtw89_warn(rtwdev, "failed to send h2c general packet\n");
1259 			return ret;
1260 		}
1261 		rtw89_phy_ra_assoc(rtwdev, wow_sta);
1262 		rtw89_phy_set_bss_color(rtwdev, wow_vif);
1263 		rtw89_chip_cfg_txpwr_ul_tb_offset(rtwdev, wow_vif);
1264 	}
1265 
1266 	if (chip_gen == RTW89_CHIP_BE)
1267 		rtw89_phy_rfk_pre_ntfy_and_wait(rtwdev, RTW89_PHY_0, 5);
1268 
1269 	rtw89_mac_hw_mgnt_sec(rtwdev, wow);
1270 
1271 	return 0;
1272 }
1273 
rtw89_wow_enable_trx_pre(struct rtw89_dev * rtwdev)1274 static int rtw89_wow_enable_trx_pre(struct rtw89_dev *rtwdev)
1275 {
1276 	int ret;
1277 
1278 	rtw89_hci_ctrl_txdma_ch(rtwdev, false);
1279 	rtw89_hci_ctrl_txdma_fw_ch(rtwdev, true);
1280 
1281 	rtw89_mac_ptk_drop_by_band_and_wait(rtwdev, RTW89_MAC_0);
1282 
1283 	ret = rtw89_hci_poll_txdma_ch_idle(rtwdev);
1284 	if (ret) {
1285 		rtw89_err(rtwdev, "txdma ch busy\n");
1286 		return ret;
1287 	}
1288 	rtw89_wow_set_rx_filter(rtwdev, true);
1289 
1290 	ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, false);
1291 	if (ret) {
1292 		rtw89_err(rtwdev, "cfg ppdu status\n");
1293 		return ret;
1294 	}
1295 
1296 	return 0;
1297 }
1298 
rtw89_wow_enable_trx_post(struct rtw89_dev * rtwdev)1299 static int rtw89_wow_enable_trx_post(struct rtw89_dev *rtwdev)
1300 {
1301 	int ret;
1302 
1303 	rtw89_hci_disable_intr(rtwdev);
1304 	rtw89_hci_ctrl_trxhci(rtwdev, false);
1305 
1306 	ret = rtw89_hci_poll_txdma_ch_idle(rtwdev);
1307 	if (ret) {
1308 		rtw89_err(rtwdev, "failed to poll txdma ch idle pcie\n");
1309 		return ret;
1310 	}
1311 
1312 	ret = rtw89_wow_config_mac(rtwdev, true);
1313 	if (ret) {
1314 		rtw89_err(rtwdev, "failed to config mac\n");
1315 		return ret;
1316 	}
1317 
1318 	rtw89_wow_set_rx_filter(rtwdev, false);
1319 	rtw89_hci_reset(rtwdev);
1320 
1321 	return 0;
1322 }
1323 
rtw89_wow_disable_trx_pre(struct rtw89_dev * rtwdev)1324 static int rtw89_wow_disable_trx_pre(struct rtw89_dev *rtwdev)
1325 {
1326 	int ret;
1327 
1328 	rtw89_hci_clr_idx_all(rtwdev);
1329 
1330 	ret = rtw89_hci_rst_bdram(rtwdev);
1331 	if (ret) {
1332 		rtw89_warn(rtwdev, "reset bdram busy\n");
1333 		return ret;
1334 	}
1335 
1336 	rtw89_hci_ctrl_trxhci(rtwdev, true);
1337 	rtw89_hci_ctrl_txdma_ch(rtwdev, true);
1338 
1339 	ret = rtw89_wow_config_mac(rtwdev, false);
1340 	if (ret) {
1341 		rtw89_err(rtwdev, "failed to config mac\n");
1342 		return ret;
1343 	}
1344 
1345 	/* Before enabling interrupt, we need to get AOAC report by reg due to RX
1346 	 * not enabled yet. Also, we need to sync RX related IV from firmware to
1347 	 * mac80211 before receiving RX packets from driver.
1348 	 * After enabling interrupt, we can get AOAC report from h2c and c2h, and
1349 	 * can get TX IV and complete rekey info. We need to update TX related IV
1350 	 * and new GTK info if rekey happened.
1351 	 */
1352 	ret = rtw89_wow_get_aoac_rpt(rtwdev, false);
1353 	if (!ret)
1354 		rtw89_wow_update_key_info(rtwdev, false);
1355 
1356 	rtw89_hci_enable_intr(rtwdev);
1357 	ret = rtw89_wow_get_aoac_rpt(rtwdev, true);
1358 	if (!ret)
1359 		rtw89_wow_update_key_info(rtwdev, true);
1360 
1361 	return 0;
1362 }
1363 
rtw89_wow_disable_trx_post(struct rtw89_dev * rtwdev)1364 static int rtw89_wow_disable_trx_post(struct rtw89_dev *rtwdev)
1365 {
1366 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1367 	struct ieee80211_vif *vif = rtw_wow->wow_vif;
1368 	int ret;
1369 
1370 	ret = rtw89_mac_cfg_ppdu_status(rtwdev, RTW89_MAC_0, true);
1371 	if (ret)
1372 		rtw89_err(rtwdev, "cfg ppdu status\n");
1373 
1374 	rtw89_fw_h2c_set_bcn_fltr_cfg(rtwdev, vif, true);
1375 
1376 	return ret;
1377 }
1378 
rtw89_fw_release_pno_pkt_list(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)1379 static void rtw89_fw_release_pno_pkt_list(struct rtw89_dev *rtwdev,
1380 					  struct rtw89_vif *rtwvif)
1381 {
1382 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1383 	struct list_head *pkt_list = &rtw_wow->pno_pkt_list;
1384 	struct rtw89_pktofld_info *info, *tmp;
1385 
1386 	list_for_each_entry_safe(info, tmp, pkt_list, list) {
1387 		rtw89_fw_h2c_del_pkt_offload(rtwdev, info->id);
1388 		list_del(&info->list);
1389 		kfree(info);
1390 	}
1391 }
1392 
rtw89_pno_scan_update_probe_req(struct rtw89_dev * rtwdev,struct rtw89_vif * rtwvif)1393 static int rtw89_pno_scan_update_probe_req(struct rtw89_dev *rtwdev,
1394 					   struct rtw89_vif *rtwvif)
1395 {
1396 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1397 	struct cfg80211_sched_scan_request *nd_config = rtw_wow->nd_config;
1398 	u8 num = nd_config->n_match_sets, i;
1399 	struct rtw89_pktofld_info *info;
1400 	struct sk_buff *skb;
1401 	int ret;
1402 
1403 	for (i = 0; i < num; i++) {
1404 		skb = ieee80211_probereq_get(rtwdev->hw, rtwvif->mac_addr,
1405 					     nd_config->match_sets[i].ssid.ssid,
1406 					     nd_config->match_sets[i].ssid.ssid_len,
1407 					     nd_config->ie_len);
1408 		if (!skb)
1409 			return -ENOMEM;
1410 
1411 		skb_put_data(skb, nd_config->ie, nd_config->ie_len);
1412 
1413 		info = kzalloc(sizeof(*info), GFP_KERNEL);
1414 		if (!info) {
1415 			kfree_skb(skb);
1416 			rtw89_fw_release_pno_pkt_list(rtwdev, rtwvif);
1417 			return -ENOMEM;
1418 		}
1419 
1420 		ret = rtw89_fw_h2c_add_pkt_offload(rtwdev, &info->id, skb);
1421 		if (ret) {
1422 			kfree_skb(skb);
1423 			kfree(info);
1424 			rtw89_fw_release_pno_pkt_list(rtwdev, rtwvif);
1425 			return ret;
1426 		}
1427 
1428 		list_add_tail(&info->list, &rtw_wow->pno_pkt_list);
1429 		kfree_skb(skb);
1430 	}
1431 
1432 	return 0;
1433 }
1434 
rtw89_pno_scan_offload(struct rtw89_dev * rtwdev,bool enable)1435 static int rtw89_pno_scan_offload(struct rtw89_dev *rtwdev, bool enable)
1436 {
1437 	const struct rtw89_mac_gen_def *mac = rtwdev->chip->mac_def;
1438 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1439 	struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
1440 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
1441 	int interval = rtw_wow->nd_config->scan_plans[0].interval;
1442 	struct rtw89_scan_option opt = {};
1443 	int ret;
1444 
1445 	if (enable) {
1446 		ret = rtw89_pno_scan_update_probe_req(rtwdev, rtwvif);
1447 		if (ret) {
1448 			rtw89_err(rtwdev, "Update probe request failed\n");
1449 			return ret;
1450 		}
1451 
1452 		ret = mac->add_chan_list_pno(rtwdev, rtwvif);
1453 		if (ret) {
1454 			rtw89_err(rtwdev, "Update channel list failed\n");
1455 			return ret;
1456 		}
1457 	}
1458 
1459 	opt.enable = enable;
1460 	opt.repeat = RTW89_SCAN_NORMAL;
1461 	opt.norm_pd = max(interval, 1) * 10; /* in unit of 100ms */
1462 	opt.delay = max(rtw_wow->nd_config->delay, 1);
1463 
1464 	if (rtwdev->chip->chip_gen == RTW89_CHIP_BE) {
1465 		opt.operation = enable ? RTW89_SCAN_OP_START : RTW89_SCAN_OP_STOP;
1466 		opt.scan_mode = RTW89_SCAN_MODE_SA;
1467 		opt.band = RTW89_PHY_0;
1468 		opt.num_macc_role = 0;
1469 		opt.mlo_mode = rtwdev->mlo_dbcc_mode;
1470 		opt.num_opch = 0;
1471 		opt.opch_end = RTW89_CHAN_INVALID;
1472 	}
1473 
1474 	mac->scan_offload(rtwdev, &opt, rtwvif, true);
1475 
1476 	return 0;
1477 }
1478 
rtw89_wow_fw_start(struct rtw89_dev * rtwdev)1479 static int rtw89_wow_fw_start(struct rtw89_dev *rtwdev)
1480 {
1481 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1482 	struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
1483 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
1484 	int ret;
1485 
1486 	if (rtw89_wow_no_link(rtwdev)) {
1487 		ret = rtw89_pno_scan_offload(rtwdev, false);
1488 		if (ret) {
1489 			rtw89_err(rtwdev, "wow: failed to disable pno scan offload\n");
1490 			return ret;
1491 		}
1492 
1493 		ret = rtw89_pno_scan_offload(rtwdev, true);
1494 		if (ret) {
1495 			rtw89_err(rtwdev, "wow: failed to enable pno scan offload\n");
1496 			return ret;
1497 		}
1498 	} else {
1499 		rtw89_wow_pattern_write(rtwdev);
1500 		rtw89_wow_construct_key_info(rtwdev);
1501 
1502 		ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif, true);
1503 		if (ret) {
1504 			rtw89_err(rtwdev, "wow: failed to enable keep alive\n");
1505 			return ret;
1506 		}
1507 
1508 		ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif, true);
1509 		if (ret) {
1510 			rtw89_err(rtwdev, "wow: failed to enable disconnect detect\n");
1511 			return ret;
1512 		}
1513 
1514 		ret = rtw89_fw_h2c_wow_gtk_ofld(rtwdev, rtwvif, true);
1515 		if (ret) {
1516 			rtw89_err(rtwdev, "wow: failed to enable GTK offload\n");
1517 			return ret;
1518 		}
1519 
1520 		ret = rtw89_fw_h2c_arp_offload(rtwdev, rtwvif, true);
1521 		if (ret)
1522 			rtw89_warn(rtwdev, "wow: failed to enable arp offload\n");
1523 	}
1524 
1525 	if (rtw89_wow_no_link(rtwdev)) {
1526 		ret = rtw89_wow_cfg_wake_pno(rtwdev, true);
1527 		if (ret) {
1528 			rtw89_err(rtwdev, "wow: failed to config wake PNO\n");
1529 			return ret;
1530 		}
1531 	} else {
1532 		ret = rtw89_wow_cfg_wake(rtwdev, true);
1533 		if (ret) {
1534 			rtw89_err(rtwdev, "wow: failed to config wake\n");
1535 			return ret;
1536 		}
1537 	}
1538 
1539 	ret = rtw89_wow_check_fw_status(rtwdev, true);
1540 	if (ret) {
1541 		rtw89_err(rtwdev, "wow: failed to check enable fw ready\n");
1542 		return ret;
1543 	}
1544 
1545 	return 0;
1546 }
1547 
rtw89_wow_fw_stop(struct rtw89_dev * rtwdev)1548 static int rtw89_wow_fw_stop(struct rtw89_dev *rtwdev)
1549 {
1550 	struct rtw89_wow_param *rtw_wow = &rtwdev->wow;
1551 	struct ieee80211_vif *wow_vif = rtw_wow->wow_vif;
1552 	struct rtw89_vif *rtwvif = (struct rtw89_vif *)wow_vif->drv_priv;
1553 	int ret;
1554 
1555 	if (rtw89_wow_no_link(rtwdev)) {
1556 		ret = rtw89_pno_scan_offload(rtwdev, false);
1557 		if (ret) {
1558 			rtw89_err(rtwdev, "wow: failed to disable pno scan offload\n");
1559 			return ret;
1560 		}
1561 
1562 		ret = rtw89_fw_h2c_cfg_pno(rtwdev, rtwvif, false);
1563 		if (ret) {
1564 			rtw89_err(rtwdev, "wow: failed to disable pno\n");
1565 			return ret;
1566 		}
1567 
1568 		rtw89_fw_release_pno_pkt_list(rtwdev, rtwvif);
1569 	} else {
1570 		rtw89_wow_pattern_clear(rtwdev);
1571 
1572 		ret = rtw89_fw_h2c_keep_alive(rtwdev, rtwvif, false);
1573 		if (ret) {
1574 			rtw89_err(rtwdev, "wow: failed to disable keep alive\n");
1575 			return ret;
1576 		}
1577 
1578 		ret = rtw89_fw_h2c_disconnect_detect(rtwdev, rtwvif, false);
1579 		if (ret) {
1580 			rtw89_err(rtwdev, "wow: failed to disable disconnect detect\n");
1581 			return ret;
1582 		}
1583 
1584 		ret = rtw89_fw_h2c_wow_gtk_ofld(rtwdev, rtwvif, false);
1585 		if (ret) {
1586 			rtw89_err(rtwdev, "wow: failed to disable GTK offload\n");
1587 			return ret;
1588 		}
1589 
1590 		ret = rtw89_fw_h2c_arp_offload(rtwdev, rtwvif, false);
1591 		if (ret)
1592 			rtw89_warn(rtwdev, "wow: failed to disable arp offload\n");
1593 
1594 		rtw89_wow_key_clear(rtwdev);
1595 		rtw89_fw_release_general_pkt_list(rtwdev, true);
1596 	}
1597 
1598 
1599 	ret = rtw89_wow_cfg_wake(rtwdev, false);
1600 	if (ret) {
1601 		rtw89_err(rtwdev, "wow: failed to disable config wake\n");
1602 		return ret;
1603 	}
1604 
1605 	ret = rtw89_wow_check_fw_status(rtwdev, false);
1606 	if (ret) {
1607 		rtw89_err(rtwdev, "wow: failed to check disable fw ready\n");
1608 		return ret;
1609 	}
1610 
1611 	return 0;
1612 }
1613 
rtw89_wow_enable(struct rtw89_dev * rtwdev)1614 static int rtw89_wow_enable(struct rtw89_dev *rtwdev)
1615 {
1616 	int ret;
1617 
1618 	set_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
1619 
1620 	ret = rtw89_wow_enable_trx_pre(rtwdev);
1621 	if (ret) {
1622 		rtw89_err(rtwdev, "wow: failed to enable trx_pre\n");
1623 		goto out;
1624 	}
1625 
1626 	rtw89_fw_release_general_pkt_list(rtwdev, true);
1627 
1628 	ret = rtw89_wow_swap_fw(rtwdev, true);
1629 	if (ret) {
1630 		rtw89_err(rtwdev, "wow: failed to swap to wow fw\n");
1631 		goto out;
1632 	}
1633 
1634 	ret = rtw89_wow_fw_start(rtwdev);
1635 	if (ret) {
1636 		rtw89_err(rtwdev, "wow: failed to let wow fw start\n");
1637 		goto out;
1638 	}
1639 
1640 	rtw89_wow_enter_ps(rtwdev);
1641 
1642 	ret = rtw89_wow_enable_trx_post(rtwdev);
1643 	if (ret) {
1644 		rtw89_err(rtwdev, "wow: failed to enable trx_post\n");
1645 		goto out;
1646 	}
1647 
1648 	return 0;
1649 
1650 out:
1651 	clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
1652 	return ret;
1653 }
1654 
rtw89_wow_disable(struct rtw89_dev * rtwdev)1655 static int rtw89_wow_disable(struct rtw89_dev *rtwdev)
1656 {
1657 	int ret;
1658 
1659 	ret = rtw89_wow_disable_trx_pre(rtwdev);
1660 	if (ret) {
1661 		rtw89_err(rtwdev, "wow: failed to disable trx_pre\n");
1662 		goto out;
1663 	}
1664 
1665 	rtw89_wow_leave_ps(rtwdev, false);
1666 
1667 	ret = rtw89_wow_fw_stop(rtwdev);
1668 	if (ret) {
1669 		rtw89_err(rtwdev, "wow: failed to swap to normal fw\n");
1670 		goto out;
1671 	}
1672 
1673 	ret = rtw89_wow_swap_fw(rtwdev, false);
1674 	if (ret) {
1675 		rtw89_err(rtwdev, "wow: failed to disable trx_post\n");
1676 		goto out;
1677 	}
1678 
1679 	ret = rtw89_wow_disable_trx_post(rtwdev);
1680 	if (ret) {
1681 		rtw89_err(rtwdev, "wow: failed to disable trx_pre\n");
1682 		goto out;
1683 	}
1684 
1685 out:
1686 	clear_bit(RTW89_FLAG_WOWLAN, rtwdev->flags);
1687 	return ret;
1688 }
1689 
rtw89_wow_restore_ps(struct rtw89_dev * rtwdev)1690 static void rtw89_wow_restore_ps(struct rtw89_dev *rtwdev)
1691 {
1692 	if (rtw89_wow_no_link(rtwdev))
1693 		rtw89_enter_ips(rtwdev);
1694 }
1695 
rtw89_wow_resume(struct rtw89_dev * rtwdev)1696 int rtw89_wow_resume(struct rtw89_dev *rtwdev)
1697 {
1698 	int ret;
1699 
1700 	if (!test_bit(RTW89_FLAG_WOWLAN, rtwdev->flags)) {
1701 		rtw89_err(rtwdev, "wow is not enabled\n");
1702 		ret = -EPERM;
1703 		goto out;
1704 	}
1705 
1706 	if (!rtw89_mac_get_power_state(rtwdev)) {
1707 		rtw89_err(rtwdev, "chip is no power when resume\n");
1708 		ret = -EPERM;
1709 		goto out;
1710 	}
1711 
1712 	rtw89_wow_leave_deep_ps(rtwdev);
1713 
1714 	rtw89_wow_show_wakeup_reason(rtwdev);
1715 
1716 	ret = rtw89_wow_disable(rtwdev);
1717 	if (ret)
1718 		rtw89_err(rtwdev, "failed to disable wow\n");
1719 
1720 	rtw89_wow_restore_ps(rtwdev);
1721 out:
1722 	rtw89_wow_clear_wakeups(rtwdev);
1723 	return ret;
1724 }
1725 
rtw89_wow_suspend(struct rtw89_dev * rtwdev,struct cfg80211_wowlan * wowlan)1726 int rtw89_wow_suspend(struct rtw89_dev *rtwdev, struct cfg80211_wowlan *wowlan)
1727 {
1728 	int ret;
1729 
1730 	ret = rtw89_wow_set_wakeups(rtwdev, wowlan);
1731 	if (ret) {
1732 		rtw89_err(rtwdev, "failed to set wakeup event\n");
1733 		return ret;
1734 	}
1735 
1736 	rtw89_wow_leave_ps(rtwdev, true);
1737 
1738 	ret = rtw89_wow_enable(rtwdev);
1739 	if (ret) {
1740 		rtw89_err(rtwdev, "failed to enable wow\n");
1741 		return ret;
1742 	}
1743 
1744 	rtw89_wow_enter_deep_ps(rtwdev);
1745 
1746 	return 0;
1747 }
1748