1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
6 */
7 #include <linux/etherdevice.h>
8 #include <linux/ip.h>
9 #include <linux/fs.h>
10 #include <net/cfg80211.h>
11 #include <net/ipv6.h>
12 #include <net/tcp.h>
13 #include <net/addrconf.h>
14 #include "iwl-modparams.h"
15 #include "fw-api.h"
16 #include "mvm.h"
17 #include "fw/img.h"
18
iwl_mvm_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)19 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
20 struct ieee80211_vif *vif,
21 struct cfg80211_gtk_rekey_data *data)
22 {
23 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
24 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
25
26 mutex_lock(&mvm->mutex);
27
28 mvmvif->rekey_data.kek_len = data->kek_len;
29 mvmvif->rekey_data.kck_len = data->kck_len;
30 memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
31 memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
32 mvmvif->rekey_data.akm = data->akm & 0xFF;
33 mvmvif->rekey_data.replay_ctr =
34 cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
35 mvmvif->rekey_data.valid = true;
36
37 mutex_unlock(&mvm->mutex);
38 }
39
40 #if IS_ENABLED(CONFIG_IPV6)
iwl_mvm_ipv6_addr_change(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct inet6_dev * idev)41 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
42 struct ieee80211_vif *vif,
43 struct inet6_dev *idev)
44 {
45 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
46 struct inet6_ifaddr *ifa;
47 int idx = 0;
48
49 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
50
51 read_lock_bh(&idev->lock);
52 list_for_each_entry(ifa, &idev->addr_list, if_list) {
53 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
54 if (ifa->flags & IFA_F_TENTATIVE)
55 __set_bit(idx, mvmvif->tentative_addrs);
56 idx++;
57 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
58 break;
59 }
60 read_unlock_bh(&idev->lock);
61
62 mvmvif->num_target_ipv6_addrs = idx;
63 }
64 #endif
65
iwl_mvm_set_default_unicast_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int idx)66 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
67 struct ieee80211_vif *vif, int idx)
68 {
69 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
70
71 mvmvif->tx_key_idx = idx;
72 }
73
iwl_mvm_convert_p1k(u16 * p1k,__le16 * out)74 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
75 {
76 int i;
77
78 for (i = 0; i < IWL_P1K_SIZE; i++)
79 out[i] = cpu_to_le16(p1k[i]);
80 }
81
iwl_mvm_find_max_pn(struct ieee80211_key_conf * key,struct iwl_mvm_key_pn * ptk_pn,struct ieee80211_key_seq * seq,int tid,int queues)82 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
83 struct iwl_mvm_key_pn *ptk_pn,
84 struct ieee80211_key_seq *seq,
85 int tid, int queues)
86 {
87 const u8 *ret = seq->ccmp.pn;
88 int i;
89
90 /* get the PN from mac80211, used on the default queue */
91 ieee80211_get_key_rx_seq(key, tid, seq);
92
93 /* and use the internal data for the other queues */
94 for (i = 1; i < queues; i++) {
95 const u8 *tmp = ptk_pn->q[i].pn[tid];
96
97 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
98 ret = tmp;
99 }
100
101 return ret;
102 }
103
104 struct wowlan_key_reprogram_data {
105 bool error;
106 int wep_key_idx;
107 };
108
iwl_mvm_wowlan_program_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)109 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
110 struct ieee80211_vif *vif,
111 struct ieee80211_sta *sta,
112 struct ieee80211_key_conf *key,
113 void *_data)
114 {
115 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
116 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
117 struct wowlan_key_reprogram_data *data = _data;
118 int ret;
119
120 switch (key->cipher) {
121 case WLAN_CIPHER_SUITE_WEP40:
122 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
123 struct {
124 struct iwl_mvm_wep_key_cmd wep_key_cmd;
125 struct iwl_mvm_wep_key wep_key;
126 } __packed wkc = {
127 .wep_key_cmd.mac_id_n_color =
128 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
129 mvmvif->color)),
130 .wep_key_cmd.num_keys = 1,
131 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
132 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
133 .wep_key.key_index = key->keyidx,
134 .wep_key.key_size = key->keylen,
135 };
136
137 /*
138 * This will fail -- the key functions don't set support
139 * pairwise WEP keys. However, that's better than silently
140 * failing WoWLAN. Or maybe not?
141 */
142 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
143 break;
144
145 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
146 if (key->keyidx == mvmvif->tx_key_idx) {
147 /* TX key must be at offset 0 */
148 wkc.wep_key.key_offset = 0;
149 } else {
150 /* others start at 1 */
151 data->wep_key_idx++;
152 wkc.wep_key.key_offset = data->wep_key_idx;
153 }
154
155 mutex_lock(&mvm->mutex);
156 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
157 data->error = ret != 0;
158
159 mvm->ptk_ivlen = key->iv_len;
160 mvm->ptk_icvlen = key->icv_len;
161 mvm->gtk_ivlen = key->iv_len;
162 mvm->gtk_icvlen = key->icv_len;
163 mutex_unlock(&mvm->mutex);
164
165 /* don't upload key again */
166 return;
167 }
168 default:
169 data->error = true;
170 return;
171 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
172 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
173 return;
174 case WLAN_CIPHER_SUITE_AES_CMAC:
175 /*
176 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
177 * but we also shouldn't abort suspend due to that. It does have
178 * support for the IGTK key renewal, but doesn't really use the
179 * IGTK for anything. This means we could spuriously wake up or
180 * be deauthenticated, but that was considered acceptable.
181 */
182 return;
183 case WLAN_CIPHER_SUITE_TKIP:
184 case WLAN_CIPHER_SUITE_CCMP:
185 case WLAN_CIPHER_SUITE_GCMP:
186 case WLAN_CIPHER_SUITE_GCMP_256:
187 break;
188 }
189
190 mutex_lock(&mvm->mutex);
191 /*
192 * The D3 firmware hardcodes the key offset 0 as the key it
193 * uses to transmit packets to the AP, i.e. the PTK.
194 */
195 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
196 mvm->ptk_ivlen = key->iv_len;
197 mvm->ptk_icvlen = key->icv_len;
198 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
199 } else {
200 /*
201 * firmware only supports TSC/RSC for a single key,
202 * so if there are multiple keep overwriting them
203 * with new ones -- this relies on mac80211 doing
204 * list_add_tail().
205 */
206 mvm->gtk_ivlen = key->iv_len;
207 mvm->gtk_icvlen = key->icv_len;
208 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
209 }
210 mutex_unlock(&mvm->mutex);
211 data->error = ret != 0;
212 }
213
214 struct wowlan_key_rsc_tsc_data {
215 struct iwl_wowlan_rsc_tsc_params_cmd_v4 *rsc_tsc;
216 bool have_rsc_tsc;
217 };
218
iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)219 static void iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw *hw,
220 struct ieee80211_vif *vif,
221 struct ieee80211_sta *sta,
222 struct ieee80211_key_conf *key,
223 void *_data)
224 {
225 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
226 struct wowlan_key_rsc_tsc_data *data = _data;
227 struct aes_sc *aes_sc;
228 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
229 struct ieee80211_key_seq seq;
230 int i;
231
232 switch (key->cipher) {
233 default:
234 break;
235 case WLAN_CIPHER_SUITE_TKIP:
236 if (sta) {
237 u64 pn64;
238
239 tkip_sc =
240 data->rsc_tsc->params.all_tsc_rsc.tkip.unicast_rsc;
241 tkip_tx_sc =
242 &data->rsc_tsc->params.all_tsc_rsc.tkip.tsc;
243
244 pn64 = atomic64_read(&key->tx_pn);
245 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
246 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
247 } else {
248 tkip_sc =
249 data->rsc_tsc->params.all_tsc_rsc.tkip.multicast_rsc;
250 }
251
252 /*
253 * For non-QoS this relies on the fact that both the uCode and
254 * mac80211 use TID 0 (as they need to to avoid replay attacks)
255 * for checking the IV in the frames.
256 */
257 for (i = 0; i < IWL_NUM_RSC; i++) {
258 ieee80211_get_key_rx_seq(key, i, &seq);
259 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
260 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
261 }
262
263 data->have_rsc_tsc = true;
264 break;
265 case WLAN_CIPHER_SUITE_CCMP:
266 case WLAN_CIPHER_SUITE_GCMP:
267 case WLAN_CIPHER_SUITE_GCMP_256:
268 if (sta) {
269 struct aes_sc *aes_tx_sc;
270 u64 pn64;
271
272 aes_sc =
273 data->rsc_tsc->params.all_tsc_rsc.aes.unicast_rsc;
274 aes_tx_sc =
275 &data->rsc_tsc->params.all_tsc_rsc.aes.tsc;
276
277 pn64 = atomic64_read(&key->tx_pn);
278 aes_tx_sc->pn = cpu_to_le64(pn64);
279 } else {
280 aes_sc =
281 data->rsc_tsc->params.all_tsc_rsc.aes.multicast_rsc;
282 }
283
284 /*
285 * For non-QoS this relies on the fact that both the uCode and
286 * mac80211/our RX code use TID 0 for checking the PN.
287 */
288 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
289 struct iwl_mvm_sta *mvmsta;
290 struct iwl_mvm_key_pn *ptk_pn;
291 const u8 *pn;
292
293 mvmsta = iwl_mvm_sta_from_mac80211(sta);
294 rcu_read_lock();
295 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
296 if (WARN_ON(!ptk_pn)) {
297 rcu_read_unlock();
298 break;
299 }
300
301 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
302 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
303 mvm->trans->num_rx_queues);
304 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
305 ((u64)pn[4] << 8) |
306 ((u64)pn[3] << 16) |
307 ((u64)pn[2] << 24) |
308 ((u64)pn[1] << 32) |
309 ((u64)pn[0] << 40));
310 }
311
312 rcu_read_unlock();
313 } else {
314 for (i = 0; i < IWL_NUM_RSC; i++) {
315 u8 *pn = seq.ccmp.pn;
316
317 ieee80211_get_key_rx_seq(key, i, &seq);
318 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
319 ((u64)pn[4] << 8) |
320 ((u64)pn[3] << 16) |
321 ((u64)pn[2] << 24) |
322 ((u64)pn[1] << 32) |
323 ((u64)pn[0] << 40));
324 }
325 }
326 data->have_rsc_tsc = true;
327 break;
328 }
329 }
330
331 struct wowlan_key_rsc_v5_data {
332 struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
333 bool have_rsc;
334 int gtks;
335 int gtk_ids[4];
336 };
337
iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)338 static void iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw *hw,
339 struct ieee80211_vif *vif,
340 struct ieee80211_sta *sta,
341 struct ieee80211_key_conf *key,
342 void *_data)
343 {
344 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
345 struct wowlan_key_rsc_v5_data *data = _data;
346 struct ieee80211_key_seq seq;
347 __le64 *rsc;
348 int i;
349
350 /* only for ciphers that can be PTK/GTK */
351 switch (key->cipher) {
352 default:
353 return;
354 case WLAN_CIPHER_SUITE_TKIP:
355 case WLAN_CIPHER_SUITE_CCMP:
356 case WLAN_CIPHER_SUITE_GCMP:
357 case WLAN_CIPHER_SUITE_GCMP_256:
358 break;
359 }
360
361 if (sta) {
362 rsc = data->rsc->ucast_rsc;
363 } else {
364 if (WARN_ON(data->gtks >= ARRAY_SIZE(data->gtk_ids)))
365 return;
366 data->gtk_ids[data->gtks] = key->keyidx;
367 rsc = data->rsc->mcast_rsc[data->gtks % 2];
368 if (WARN_ON(key->keyidx >=
369 ARRAY_SIZE(data->rsc->mcast_key_id_map)))
370 return;
371 data->rsc->mcast_key_id_map[key->keyidx] = data->gtks % 2;
372 if (data->gtks >= 2) {
373 int prev = data->gtks - 2;
374 int prev_idx = data->gtk_ids[prev];
375
376 data->rsc->mcast_key_id_map[prev_idx] =
377 IWL_MCAST_KEY_MAP_INVALID;
378 }
379 data->gtks++;
380 }
381
382 switch (key->cipher) {
383 default:
384 WARN_ON(1);
385 break;
386 case WLAN_CIPHER_SUITE_TKIP:
387
388 /*
389 * For non-QoS this relies on the fact that both the uCode and
390 * mac80211 use TID 0 (as they need to to avoid replay attacks)
391 * for checking the IV in the frames.
392 */
393 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
394 ieee80211_get_key_rx_seq(key, i, &seq);
395
396 rsc[i] = cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
397 seq.tkip.iv16);
398 }
399
400 data->have_rsc = true;
401 break;
402 case WLAN_CIPHER_SUITE_CCMP:
403 case WLAN_CIPHER_SUITE_GCMP:
404 case WLAN_CIPHER_SUITE_GCMP_256:
405 /*
406 * For non-QoS this relies on the fact that both the uCode and
407 * mac80211/our RX code use TID 0 for checking the PN.
408 */
409 if (sta) {
410 struct iwl_mvm_sta *mvmsta;
411 struct iwl_mvm_key_pn *ptk_pn;
412 const u8 *pn;
413
414 mvmsta = iwl_mvm_sta_from_mac80211(sta);
415 rcu_read_lock();
416 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
417 if (WARN_ON(!ptk_pn)) {
418 rcu_read_unlock();
419 break;
420 }
421
422 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
423 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
424 mvm->trans->num_rx_queues);
425 rsc[i] = cpu_to_le64((u64)pn[5] |
426 ((u64)pn[4] << 8) |
427 ((u64)pn[3] << 16) |
428 ((u64)pn[2] << 24) |
429 ((u64)pn[1] << 32) |
430 ((u64)pn[0] << 40));
431 }
432
433 rcu_read_unlock();
434 } else {
435 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
436 u8 *pn = seq.ccmp.pn;
437
438 ieee80211_get_key_rx_seq(key, i, &seq);
439 rsc[i] = cpu_to_le64((u64)pn[5] |
440 ((u64)pn[4] << 8) |
441 ((u64)pn[3] << 16) |
442 ((u64)pn[2] << 24) |
443 ((u64)pn[1] << 32) |
444 ((u64)pn[0] << 40));
445 }
446 }
447 data->have_rsc = true;
448 break;
449 }
450 }
451
iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)452 static int iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm *mvm,
453 struct ieee80211_vif *vif,
454 struct iwl_mvm_vif_link_info *mvm_link)
455 {
456 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TSC_RSC_PARAM,
457 IWL_FW_CMD_VER_UNKNOWN);
458 int ret;
459
460 if (ver == 5) {
461 struct wowlan_key_rsc_v5_data data = {};
462 int i;
463
464 data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL);
465 if (!data.rsc)
466 return -ENOMEM;
467
468 for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++)
469 data.rsc->mcast_key_id_map[i] =
470 IWL_MCAST_KEY_MAP_INVALID;
471 data.rsc->sta_id = cpu_to_le32(mvm_link->ap_sta_id);
472
473 ieee80211_iter_keys(mvm->hw, vif,
474 iwl_mvm_wowlan_get_rsc_v5_data,
475 &data);
476
477 if (data.have_rsc)
478 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
479 CMD_ASYNC, sizeof(*data.rsc),
480 data.rsc);
481 else
482 ret = 0;
483 kfree(data.rsc);
484 } else if (ver == 4 || ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
485 struct wowlan_key_rsc_tsc_data data = {};
486 int size;
487
488 data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL);
489 if (!data.rsc_tsc)
490 return -ENOMEM;
491
492 if (ver == 4) {
493 size = sizeof(*data.rsc_tsc);
494 data.rsc_tsc->sta_id =
495 cpu_to_le32(mvm_link->ap_sta_id);
496 } else {
497 /* ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN */
498 size = sizeof(data.rsc_tsc->params);
499 }
500
501 ieee80211_iter_keys(mvm->hw, vif,
502 iwl_mvm_wowlan_get_rsc_tsc_data,
503 &data);
504
505 if (data.have_rsc_tsc)
506 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
507 CMD_ASYNC, size,
508 data.rsc_tsc);
509 else
510 ret = 0;
511 kfree(data.rsc_tsc);
512 } else {
513 ret = 0;
514 WARN_ON_ONCE(1);
515 }
516
517 return ret;
518 }
519
520 struct wowlan_key_tkip_data {
521 struct iwl_wowlan_tkip_params_cmd tkip;
522 bool have_tkip_keys;
523 };
524
iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)525 static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw,
526 struct ieee80211_vif *vif,
527 struct ieee80211_sta *sta,
528 struct ieee80211_key_conf *key,
529 void *_data)
530 {
531 struct wowlan_key_tkip_data *data = _data;
532 struct iwl_p1k_cache *rx_p1ks;
533 u8 *rx_mic_key;
534 struct ieee80211_key_seq seq;
535 u32 cur_rx_iv32 = 0;
536 u16 p1k[IWL_P1K_SIZE];
537 int i;
538
539 switch (key->cipher) {
540 default:
541 break;
542 case WLAN_CIPHER_SUITE_TKIP:
543 if (sta) {
544 u64 pn64;
545
546 rx_p1ks = data->tkip.rx_uni;
547
548 pn64 = atomic64_read(&key->tx_pn);
549
550 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
551 p1k);
552 iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k);
553
554 memcpy(data->tkip.mic_keys.tx,
555 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
556 IWL_MIC_KEY_SIZE);
557
558 rx_mic_key = data->tkip.mic_keys.rx_unicast;
559 } else {
560 rx_p1ks = data->tkip.rx_multi;
561 rx_mic_key = data->tkip.mic_keys.rx_mcast;
562 }
563
564 for (i = 0; i < IWL_NUM_RSC; i++) {
565 ieee80211_get_key_rx_seq(key, i, &seq);
566 /* wrapping isn't allowed, AP must rekey */
567 if (seq.tkip.iv32 > cur_rx_iv32)
568 cur_rx_iv32 = seq.tkip.iv32;
569 }
570
571 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
572 cur_rx_iv32, p1k);
573 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
574 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
575 cur_rx_iv32 + 1, p1k);
576 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
577
578 memcpy(rx_mic_key,
579 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
580 IWL_MIC_KEY_SIZE);
581
582 data->have_tkip_keys = true;
583 break;
584 }
585 }
586
587 struct wowlan_key_gtk_type_iter {
588 struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd;
589 };
590
iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)591 static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw,
592 struct ieee80211_vif *vif,
593 struct ieee80211_sta *sta,
594 struct ieee80211_key_conf *key,
595 void *_data)
596 {
597 struct wowlan_key_gtk_type_iter *data = _data;
598 __le32 *cipher = NULL;
599
600 if (key->keyidx == 4 || key->keyidx == 5)
601 cipher = &data->kek_kck_cmd->igtk_cipher;
602 if (key->keyidx == 6 || key->keyidx == 7)
603 cipher = &data->kek_kck_cmd->bigtk_cipher;
604
605 switch (key->cipher) {
606 default:
607 return;
608 case WLAN_CIPHER_SUITE_TKIP:
609 if (!sta)
610 data->kek_kck_cmd->gtk_cipher =
611 cpu_to_le32(STA_KEY_FLG_TKIP);
612 return;
613 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
614 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
615 if (cipher)
616 *cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
617 return;
618 case WLAN_CIPHER_SUITE_AES_CMAC:
619 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
620 if (cipher)
621 *cipher = cpu_to_le32(STA_KEY_FLG_CCM);
622 return;
623 case WLAN_CIPHER_SUITE_CCMP:
624 if (!sta)
625 data->kek_kck_cmd->gtk_cipher =
626 cpu_to_le32(STA_KEY_FLG_CCM);
627 return;
628 case WLAN_CIPHER_SUITE_GCMP:
629 case WLAN_CIPHER_SUITE_GCMP_256:
630 if (!sta)
631 data->kek_kck_cmd->gtk_cipher =
632 cpu_to_le32(STA_KEY_FLG_GCMP);
633 return;
634 }
635 }
636
iwl_mvm_send_patterns_v1(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan)637 static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
638 struct cfg80211_wowlan *wowlan)
639 {
640 struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
641 struct iwl_host_cmd cmd = {
642 .id = WOWLAN_PATTERNS,
643 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
644 };
645 int i, err;
646
647 if (!wowlan->n_patterns)
648 return 0;
649
650 cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
651
652 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
653 if (!pattern_cmd)
654 return -ENOMEM;
655
656 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
657
658 for (i = 0; i < wowlan->n_patterns; i++) {
659 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
660
661 memcpy(&pattern_cmd->patterns[i].mask,
662 wowlan->patterns[i].mask, mask_len);
663 memcpy(&pattern_cmd->patterns[i].pattern,
664 wowlan->patterns[i].pattern,
665 wowlan->patterns[i].pattern_len);
666 pattern_cmd->patterns[i].mask_size = mask_len;
667 pattern_cmd->patterns[i].pattern_size =
668 wowlan->patterns[i].pattern_len;
669 }
670
671 cmd.data[0] = pattern_cmd;
672 err = iwl_mvm_send_cmd(mvm, &cmd);
673 kfree(pattern_cmd);
674 return err;
675 }
676
iwl_mvm_send_patterns(struct iwl_mvm * mvm,struct iwl_mvm_vif_link_info * mvm_link,struct cfg80211_wowlan * wowlan)677 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
678 struct iwl_mvm_vif_link_info *mvm_link,
679 struct cfg80211_wowlan *wowlan)
680 {
681 struct iwl_wowlan_patterns_cmd *pattern_cmd;
682 struct iwl_host_cmd cmd = {
683 .id = WOWLAN_PATTERNS,
684 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
685 };
686 int i, err;
687 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
688 IWL_FW_CMD_VER_UNKNOWN);
689
690 if (!wowlan->n_patterns)
691 return 0;
692
693 cmd.len[0] = sizeof(*pattern_cmd) +
694 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
695
696 pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
697 if (!pattern_cmd)
698 return -ENOMEM;
699
700 pattern_cmd->n_patterns = wowlan->n_patterns;
701 if (ver >= 3)
702 pattern_cmd->sta_id = mvm_link->ap_sta_id;
703
704 for (i = 0; i < wowlan->n_patterns; i++) {
705 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
706
707 pattern_cmd->patterns[i].pattern_type =
708 WOWLAN_PATTERN_TYPE_BITMASK;
709
710 memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
711 wowlan->patterns[i].mask, mask_len);
712 memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
713 wowlan->patterns[i].pattern,
714 wowlan->patterns[i].pattern_len);
715 pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
716 pattern_cmd->patterns[i].u.bitmask.pattern_size =
717 wowlan->patterns[i].pattern_len;
718 }
719
720 cmd.data[0] = pattern_cmd;
721 err = iwl_mvm_send_cmd(mvm, &cmd);
722 kfree(pattern_cmd);
723 return err;
724 }
725
iwl_mvm_d3_reprogram(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * ap_sta)726 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
727 struct ieee80211_sta *ap_sta)
728 {
729 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
730 struct ieee80211_chanctx_conf *ctx;
731 u8 chains_static, chains_dynamic;
732 struct cfg80211_chan_def chandef, ap_def;
733 int ret, i;
734 struct iwl_binding_cmd_v1 binding_cmd = {};
735 struct iwl_time_quota_cmd quota_cmd = {};
736 struct iwl_time_quota_data *quota;
737 u32 status;
738
739 if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm) ||
740 ieee80211_vif_is_mld(vif)))
741 return -EINVAL;
742
743 /* add back the PHY */
744 if (WARN_ON(!mvmvif->deflink.phy_ctxt))
745 return -EINVAL;
746
747 rcu_read_lock();
748 ctx = rcu_dereference(vif->bss_conf.chanctx_conf);
749 if (WARN_ON(!ctx)) {
750 rcu_read_unlock();
751 return -EINVAL;
752 }
753 chandef = ctx->def;
754 ap_def = ctx->ap;
755 chains_static = ctx->rx_chains_static;
756 chains_dynamic = ctx->rx_chains_dynamic;
757 rcu_read_unlock();
758
759 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt, &chandef,
760 &ap_def, chains_static, chains_dynamic);
761 if (ret)
762 return ret;
763
764 /* add back the MAC */
765 mvmvif->uploaded = false;
766
767 if (WARN_ON(!vif->cfg.assoc))
768 return -EINVAL;
769
770 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
771 if (ret)
772 return ret;
773
774 /* add back binding - XXX refactor? */
775 binding_cmd.id_and_color =
776 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
777 mvmvif->deflink.phy_ctxt->color));
778 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
779 binding_cmd.phy =
780 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
781 mvmvif->deflink.phy_ctxt->color));
782 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
783 mvmvif->color));
784 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
785 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
786
787 status = 0;
788 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
789 IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
790 &status);
791 if (ret) {
792 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
793 return ret;
794 }
795
796 if (status) {
797 IWL_ERR(mvm, "Binding command failed: %u\n", status);
798 return -EIO;
799 }
800
801 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
802 if (ret)
803 return ret;
804 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
805 ap_sta);
806
807 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
808 if (ret)
809 return ret;
810
811 /* and some quota */
812 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, 0);
813 quota->id_and_color =
814 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
815 mvmvif->deflink.phy_ctxt->color));
816 quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
817 quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
818
819 for (i = 1; i < MAX_BINDINGS; i++) {
820 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, i);
821 quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
822 }
823
824 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
825 iwl_mvm_quota_cmd_size(mvm), "a_cmd);
826 if (ret)
827 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
828
829 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm, false))
830 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
831
832 return 0;
833 }
834
iwl_mvm_get_last_nonqos_seq(struct iwl_mvm * mvm,struct ieee80211_vif * vif)835 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
836 struct ieee80211_vif *vif)
837 {
838 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
839 struct iwl_nonqos_seq_query_cmd query_cmd = {
840 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
841 .mac_id_n_color =
842 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
843 mvmvif->color)),
844 };
845 struct iwl_host_cmd cmd = {
846 .id = NON_QOS_TX_COUNTER_CMD,
847 .flags = CMD_WANT_SKB,
848 };
849 int err;
850 u32 size;
851
852 cmd.data[0] = &query_cmd;
853 cmd.len[0] = sizeof(query_cmd);
854
855 err = iwl_mvm_send_cmd(mvm, &cmd);
856 if (err)
857 return err;
858
859 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
860 if (size < sizeof(__le16)) {
861 err = -EINVAL;
862 } else {
863 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
864 /* firmware returns next, not last-used seqno */
865 err = (u16) (err - 0x10);
866 }
867
868 iwl_free_resp(&cmd);
869 return err;
870 }
871
iwl_mvm_set_last_nonqos_seq(struct iwl_mvm * mvm,struct ieee80211_vif * vif)872 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
873 {
874 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
875 struct iwl_nonqos_seq_query_cmd query_cmd = {
876 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
877 .mac_id_n_color =
878 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
879 mvmvif->color)),
880 .value = cpu_to_le16(mvmvif->seqno),
881 };
882
883 /* return if called during restart, not resume from D3 */
884 if (!mvmvif->seqno_valid)
885 return;
886
887 mvmvif->seqno_valid = false;
888
889 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
890 sizeof(query_cmd), &query_cmd))
891 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
892 }
893
iwl_mvm_switch_to_d3(struct iwl_mvm * mvm)894 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
895 {
896 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
897
898 iwl_mvm_stop_device(mvm);
899 /*
900 * Set the HW restart bit -- this is mostly true as we're
901 * going to load new firmware and reprogram that, though
902 * the reprogramming is going to be manual to avoid adding
903 * all the MACs that aren't support.
904 * We don't have to clear up everything though because the
905 * reprogramming is manual. When we resume, we'll actually
906 * go through a proper restart sequence again to switch
907 * back to the runtime firmware image.
908 */
909 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
910
911 /* the fw is reset, so all the keys are cleared */
912 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
913
914 mvm->ptk_ivlen = 0;
915 mvm->ptk_icvlen = 0;
916 mvm->ptk_ivlen = 0;
917 mvm->ptk_icvlen = 0;
918
919 return iwl_mvm_load_d3_fw(mvm);
920 }
921
922 static int
iwl_mvm_get_wowlan_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct iwl_wowlan_config_cmd_v6 * wowlan_config_cmd,struct ieee80211_vif * vif,struct iwl_mvm_vif * mvmvif,struct ieee80211_sta * ap_sta)923 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
924 struct cfg80211_wowlan *wowlan,
925 struct iwl_wowlan_config_cmd_v6 *wowlan_config_cmd,
926 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
927 struct ieee80211_sta *ap_sta)
928 {
929 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
930
931 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
932
933 wowlan_config_cmd->is_11n_connection =
934 ap_sta->deflink.ht_cap.ht_supported;
935 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
936 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
937
938 if (ap_sta->mfp)
939 wowlan_config_cmd->flags |= IS_11W_ASSOC;
940
941 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
942 /* Query the last used seqno and set it */
943 int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
944
945 if (ret < 0)
946 return ret;
947
948 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
949 }
950
951 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 7)
952 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
953
954 if (wowlan->disconnect)
955 wowlan_config_cmd->wakeup_filter |=
956 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
957 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
958 if (wowlan->magic_pkt)
959 wowlan_config_cmd->wakeup_filter |=
960 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
961 if (wowlan->gtk_rekey_failure)
962 wowlan_config_cmd->wakeup_filter |=
963 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
964 if (wowlan->eap_identity_req)
965 wowlan_config_cmd->wakeup_filter |=
966 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
967 if (wowlan->four_way_handshake)
968 wowlan_config_cmd->wakeup_filter |=
969 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
970 if (wowlan->n_patterns)
971 wowlan_config_cmd->wakeup_filter |=
972 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
973
974 if (wowlan->rfkill_release)
975 wowlan_config_cmd->wakeup_filter |=
976 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
977
978 if (wowlan->tcp) {
979 /*
980 * Set the "link change" (really "link lost") flag as well
981 * since that implies losing the TCP connection.
982 */
983 wowlan_config_cmd->wakeup_filter |=
984 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
985 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
986 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
987 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
988 }
989
990 if (wowlan->any) {
991 wowlan_config_cmd->wakeup_filter |=
992 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
993 IWL_WOWLAN_WAKEUP_LINK_CHANGE |
994 IWL_WOWLAN_WAKEUP_RX_FRAME |
995 IWL_WOWLAN_WAKEUP_BCN_FILTERING);
996 }
997
998 return 0;
999 }
1000
iwl_mvm_wowlan_config_key_params(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)1001 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
1002 struct ieee80211_vif *vif,
1003 struct iwl_mvm_vif_link_info *mvm_link)
1004 {
1005 bool unified = fw_has_capa(&mvm->fw->ucode_capa,
1006 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1007 struct wowlan_key_reprogram_data key_data = {};
1008 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1009 int ret;
1010 u8 cmd_ver;
1011 size_t cmd_size;
1012
1013 if (!unified) {
1014 /*
1015 * if we have to configure keys, call ieee80211_iter_keys(),
1016 * as we need non-atomic context in order to take the
1017 * required locks.
1018 */
1019 /*
1020 * Note that currently we don't use CMD_ASYNC in the iterator.
1021 * In case of key_data.configure_keys, all the configured
1022 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1023 * take care of locking/unlocking mvm->mutex.
1024 */
1025 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1026 &key_data);
1027
1028 if (key_data.error)
1029 return -EIO;
1030 }
1031
1032 ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif, mvm_link);
1033 if (ret)
1034 return ret;
1035
1036 if (!fw_has_api(&mvm->fw->ucode_capa,
1037 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1038 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1039 IWL_FW_CMD_VER_UNKNOWN);
1040 struct wowlan_key_tkip_data tkip_data = {};
1041 int size;
1042
1043 if (ver == 2) {
1044 size = sizeof(tkip_data.tkip);
1045 tkip_data.tkip.sta_id =
1046 cpu_to_le32(mvm_link->ap_sta_id);
1047 } else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1048 size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1049 } else {
1050 WARN_ON_ONCE(1);
1051 return -EINVAL;
1052 }
1053
1054 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1055 &tkip_data);
1056
1057 if (tkip_data.have_tkip_keys) {
1058 /* send relevant data according to CMD version */
1059 ret = iwl_mvm_send_cmd_pdu(mvm,
1060 WOWLAN_TKIP_PARAM,
1061 CMD_ASYNC, size,
1062 &tkip_data.tkip);
1063 if (ret)
1064 return ret;
1065 }
1066 }
1067
1068 /* configure rekey data only if offloaded rekey is supported (d3) */
1069 if (mvmvif->rekey_data.valid) {
1070 struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1071 struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1072 &kek_kck_cmd;
1073 struct wowlan_key_gtk_type_iter gtk_type_data = {
1074 .kek_kck_cmd = _kek_kck_cmd,
1075 };
1076
1077 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1078 WOWLAN_KEK_KCK_MATERIAL,
1079 IWL_FW_CMD_VER_UNKNOWN);
1080 if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1081 cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1082 return -EINVAL;
1083
1084 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1085 >k_type_data);
1086
1087 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1088 mvmvif->rekey_data.kck_len);
1089 kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1090 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1091 mvmvif->rekey_data.kek_len);
1092 kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1093 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1094 kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1095 kek_kck_cmd.sta_id = cpu_to_le32(mvm_link->ap_sta_id);
1096
1097 if (cmd_ver == 4) {
1098 cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1099 } else {
1100 if (cmd_ver == 3)
1101 cmd_size =
1102 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1103 else
1104 cmd_size =
1105 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1106 /* skip the sta_id at the beginning */
1107 _kek_kck_cmd = (void *)
1108 ((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1109 }
1110
1111 IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1112 mvmvif->rekey_data.akm);
1113
1114 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1115 CMD_ASYNC, cmd_size, _kek_kck_cmd);
1116 if (ret)
1117 return ret;
1118 }
1119
1120 return 0;
1121 }
1122
1123 static int
iwl_mvm_wowlan_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct iwl_wowlan_config_cmd_v6 * wowlan_config_cmd_v6,struct ieee80211_vif * vif,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_vif_link_info * mvm_link,struct ieee80211_sta * ap_sta)1124 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1125 struct cfg80211_wowlan *wowlan,
1126 struct iwl_wowlan_config_cmd_v6 *wowlan_config_cmd_v6,
1127 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1128 struct iwl_mvm_vif_link_info *mvm_link,
1129 struct ieee80211_sta *ap_sta)
1130 {
1131 int ret;
1132 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1133 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1134
1135 mvm->offload_tid = wowlan_config_cmd_v6->offloading_tid;
1136
1137 if (!unified_image) {
1138 ret = iwl_mvm_switch_to_d3(mvm);
1139 if (ret)
1140 return ret;
1141
1142 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1143 if (ret)
1144 return ret;
1145 }
1146
1147 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, mvm_link);
1148 if (ret)
1149 return ret;
1150
1151 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) > 6) {
1152 struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1153 .wakeup_filter = wowlan_config_cmd_v6->wakeup_filter,
1154 .wowlan_ba_teardown_tids =
1155 wowlan_config_cmd_v6->wowlan_ba_teardown_tids,
1156 .is_11n_connection =
1157 wowlan_config_cmd_v6->is_11n_connection,
1158 .offloading_tid = wowlan_config_cmd_v6->offloading_tid,
1159 .flags = wowlan_config_cmd_v6->flags,
1160 .sta_id = wowlan_config_cmd_v6->sta_id,
1161 };
1162
1163 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1164 sizeof(wowlan_config_cmd),
1165 &wowlan_config_cmd);
1166 } else {
1167 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1168 sizeof(*wowlan_config_cmd_v6),
1169 wowlan_config_cmd_v6);
1170 }
1171 if (ret)
1172 return ret;
1173
1174 if (fw_has_api(&mvm->fw->ucode_capa,
1175 IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1176 ret = iwl_mvm_send_patterns(mvm, mvm_link, wowlan);
1177 else
1178 ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1179 if (ret)
1180 return ret;
1181
1182 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0,
1183 mvm_link->ap_sta_id);
1184 }
1185
1186 static int
iwl_mvm_netdetect_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct cfg80211_sched_scan_request * nd_config,struct ieee80211_vif * vif)1187 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1188 struct cfg80211_wowlan *wowlan,
1189 struct cfg80211_sched_scan_request *nd_config,
1190 struct ieee80211_vif *vif)
1191 {
1192 int ret;
1193 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1194 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1195
1196 if (!unified_image) {
1197 ret = iwl_mvm_switch_to_d3(mvm);
1198 if (ret)
1199 return ret;
1200 } else {
1201 /* In theory, we wouldn't have to stop a running sched
1202 * scan in order to start another one (for
1203 * net-detect). But in practice this doesn't seem to
1204 * work properly, so stop any running sched_scan now.
1205 */
1206 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1207 if (ret)
1208 return ret;
1209 }
1210
1211 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1212 IWL_MVM_SCAN_NETDETECT);
1213 if (ret)
1214 return ret;
1215
1216 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1217 return -EBUSY;
1218
1219 /* save the sched scan matchsets... */
1220 if (nd_config->n_match_sets) {
1221 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1222 sizeof(*nd_config->match_sets) *
1223 nd_config->n_match_sets,
1224 GFP_KERNEL);
1225 if (mvm->nd_match_sets)
1226 mvm->n_nd_match_sets = nd_config->n_match_sets;
1227 }
1228
1229 /* ...and the sched scan channels for later reporting */
1230 mvm->nd_channels = kmemdup(nd_config->channels,
1231 sizeof(*nd_config->channels) *
1232 nd_config->n_channels,
1233 GFP_KERNEL);
1234 if (mvm->nd_channels)
1235 mvm->n_nd_channels = nd_config->n_channels;
1236
1237 return 0;
1238 }
1239
iwl_mvm_free_nd(struct iwl_mvm * mvm)1240 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1241 {
1242 kfree(mvm->nd_match_sets);
1243 mvm->nd_match_sets = NULL;
1244 mvm->n_nd_match_sets = 0;
1245 kfree(mvm->nd_channels);
1246 mvm->nd_channels = NULL;
1247 mvm->n_nd_channels = 0;
1248 }
1249
__iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan,bool test)1250 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1251 struct cfg80211_wowlan *wowlan,
1252 bool test)
1253 {
1254 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1255 struct ieee80211_vif *vif = NULL;
1256 struct iwl_mvm_vif *mvmvif = NULL;
1257 struct ieee80211_sta *ap_sta = NULL;
1258 struct iwl_mvm_vif_link_info *mvm_link;
1259 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1260 /*
1261 * Program the minimum sleep time to 10 seconds, as many
1262 * platforms have issues processing a wakeup signal while
1263 * still being in the process of suspending.
1264 */
1265 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1266 };
1267 struct iwl_host_cmd d3_cfg_cmd = {
1268 .id = D3_CONFIG_CMD,
1269 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
1270 .data[0] = &d3_cfg_cmd_data,
1271 .len[0] = sizeof(d3_cfg_cmd_data),
1272 };
1273 int ret;
1274 int len __maybe_unused;
1275 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1276 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1277
1278 if (!wowlan) {
1279 /*
1280 * mac80211 shouldn't get here, but for D3 test
1281 * it doesn't warrant a warning
1282 */
1283 WARN_ON(!test);
1284 return -EINVAL;
1285 }
1286
1287 vif = iwl_mvm_get_bss_vif(mvm);
1288 if (IS_ERR_OR_NULL(vif))
1289 return 1;
1290
1291 ret = iwl_mvm_block_esr_sync(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
1292 if (ret)
1293 return ret;
1294
1295 mutex_lock(&mvm->mutex);
1296
1297 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1298
1299 synchronize_net();
1300
1301 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1302
1303 mvm_link = mvmvif->link[iwl_mvm_get_primary_link(vif)];
1304 if (WARN_ON_ONCE(!mvm_link)) {
1305 ret = -EINVAL;
1306 goto out_noreset;
1307 }
1308
1309 if (mvm_link->ap_sta_id == IWL_INVALID_STA) {
1310 /* if we're not associated, this must be netdetect */
1311 if (!wowlan->nd_config) {
1312 ret = 1;
1313 goto out_noreset;
1314 }
1315
1316 ret = iwl_mvm_netdetect_config(
1317 mvm, wowlan, wowlan->nd_config, vif);
1318 if (ret)
1319 goto out;
1320
1321 mvm->net_detect = true;
1322 } else {
1323 struct iwl_wowlan_config_cmd_v6 wowlan_config_cmd = {
1324 .offloading_tid = 0,
1325 };
1326
1327 wowlan_config_cmd.sta_id = mvm_link->ap_sta_id;
1328
1329 ap_sta = rcu_dereference_protected(
1330 mvm->fw_id_to_mac_id[mvm_link->ap_sta_id],
1331 lockdep_is_held(&mvm->mutex));
1332 if (IS_ERR_OR_NULL(ap_sta)) {
1333 ret = -EINVAL;
1334 goto out_noreset;
1335 }
1336
1337 ret = iwl_mvm_sta_ensure_queue(
1338 mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1339 if (ret)
1340 goto out_noreset;
1341
1342 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1343 vif, mvmvif, ap_sta);
1344 if (ret)
1345 goto out_noreset;
1346 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1347 vif, mvmvif, mvm_link, ap_sta);
1348 if (ret)
1349 goto out;
1350
1351 mvm->net_detect = false;
1352 }
1353
1354 ret = iwl_mvm_power_update_device(mvm);
1355 if (ret)
1356 goto out;
1357
1358 ret = iwl_mvm_power_update_mac(mvm);
1359 if (ret)
1360 goto out;
1361
1362 #ifdef CONFIG_IWLWIFI_DEBUGFS
1363 if (mvm->d3_wake_sysassert)
1364 d3_cfg_cmd_data.wakeup_flags |=
1365 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1366 #endif
1367
1368 /*
1369 * Prior to 9000 device family the driver needs to stop the dbg
1370 * recording before entering D3. In later devices the FW stops the
1371 * recording automatically.
1372 */
1373 if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1374 iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1375
1376 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1377
1378 /* must be last -- this switches firmware state */
1379 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1380 if (ret)
1381 goto out;
1382 #ifdef CONFIG_IWLWIFI_DEBUGFS
1383 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1384 if (len >= sizeof(u32)) {
1385 mvm->d3_test_pme_ptr =
1386 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1387 }
1388 #endif
1389 iwl_free_resp(&d3_cfg_cmd);
1390
1391 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1392
1393 ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1394 out:
1395 if (ret < 0) {
1396 iwl_mvm_free_nd(mvm);
1397
1398 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1399 }
1400 out_noreset:
1401 mutex_unlock(&mvm->mutex);
1402
1403 return ret;
1404 }
1405
iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1406 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1407 {
1408 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1409
1410 iwl_mvm_pause_tcm(mvm, true);
1411
1412 mutex_lock(&mvm->mutex);
1413 iwl_fw_runtime_suspend(&mvm->fwrt);
1414 mutex_unlock(&mvm->mutex);
1415
1416 return __iwl_mvm_suspend(hw, wowlan, false);
1417 }
1418
1419 struct iwl_multicast_key_data {
1420 u8 key[WOWLAN_KEY_MAX_SIZE];
1421 u8 len;
1422 u8 flags;
1423 u8 id;
1424 u8 ipn[6];
1425 };
1426
1427 /* converted data from the different status responses */
1428 struct iwl_wowlan_status_data {
1429 u64 replay_ctr;
1430 u32 num_of_gtk_rekeys;
1431 u32 received_beacons;
1432 u32 wakeup_reasons;
1433 u32 wake_packet_length;
1434 u32 wake_packet_bufsize;
1435 u16 pattern_number;
1436 u16 non_qos_seq_ctr;
1437 u16 qos_seq_ctr[8];
1438 u8 tid_tear_down;
1439 u8 tid_offloaded_tx;
1440
1441 struct {
1442 /* including RX MIC key for TKIP */
1443 u8 key[WOWLAN_KEY_MAX_SIZE];
1444 u8 len;
1445 u8 flags;
1446 u8 id;
1447 } gtk[WOWLAN_GTK_KEYS_NUM];
1448
1449 struct {
1450 /*
1451 * We store both the TKIP and AES representations
1452 * coming from the firmware because we decode the
1453 * data from there before we iterate the keys and
1454 * know which one we need.
1455 */
1456 struct {
1457 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1458 } tkip, aes;
1459
1460 /*
1461 * We use -1 for when we have valid data but don't know
1462 * the key ID from firmware, and thus it needs to be
1463 * installed with the last key (depending on rekeying).
1464 */
1465 s8 key_id;
1466 bool valid;
1467 } gtk_seq[2];
1468
1469 struct {
1470 /* Same as above */
1471 struct {
1472 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1473 u64 tx_pn;
1474 } tkip, aes;
1475 } ptk;
1476
1477 struct iwl_multicast_key_data igtk;
1478 struct iwl_multicast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
1479
1480 int num_mlo_keys;
1481 struct iwl_wowlan_mlo_gtk mlo_keys[WOWLAN_MAX_MLO_KEYS];
1482
1483 u8 *wake_packet;
1484 };
1485
iwl_mvm_report_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)1486 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1487 struct ieee80211_vif *vif,
1488 struct iwl_wowlan_status_data *status)
1489 {
1490 struct sk_buff *pkt = NULL;
1491 struct cfg80211_wowlan_wakeup wakeup = {
1492 .pattern_idx = -1,
1493 };
1494 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1495 u32 reasons = status->wakeup_reasons;
1496
1497 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1498 wakeup_report = NULL;
1499 goto report;
1500 }
1501
1502 pm_wakeup_event(mvm->dev, 0);
1503
1504 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1505 wakeup.magic_pkt = true;
1506
1507 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1508 wakeup.pattern_idx =
1509 status->pattern_number;
1510
1511 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1512 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
1513 IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
1514 wakeup.disconnect = true;
1515
1516 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1517 wakeup.gtk_rekey_failure = true;
1518
1519 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1520 wakeup.rfkill_release = true;
1521
1522 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1523 wakeup.eap_identity_req = true;
1524
1525 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1526 wakeup.four_way_handshake = true;
1527
1528 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1529 wakeup.tcp_connlost = true;
1530
1531 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1532 wakeup.tcp_nomoretokens = true;
1533
1534 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1535 wakeup.tcp_match = true;
1536
1537 if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC)
1538 wakeup.unprot_deauth_disassoc = true;
1539
1540 if (status->wake_packet) {
1541 int pktsize = status->wake_packet_bufsize;
1542 int pktlen = status->wake_packet_length;
1543 const u8 *pktdata = status->wake_packet;
1544 const struct ieee80211_hdr *hdr = (const void *)pktdata;
1545 int truncated = pktlen - pktsize;
1546
1547 /* this would be a firmware bug */
1548 if (WARN_ON_ONCE(truncated < 0))
1549 truncated = 0;
1550
1551 if (ieee80211_is_data(hdr->frame_control)) {
1552 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1553 int ivlen = 0, icvlen = 4; /* also FCS */
1554
1555 pkt = alloc_skb(pktsize, GFP_KERNEL);
1556 if (!pkt)
1557 goto report;
1558
1559 skb_put_data(pkt, pktdata, hdrlen);
1560 pktdata += hdrlen;
1561 pktsize -= hdrlen;
1562
1563 if (ieee80211_has_protected(hdr->frame_control)) {
1564 /*
1565 * This is unlocked and using gtk_i(c)vlen,
1566 * but since everything is under RTNL still
1567 * that's not really a problem - changing
1568 * it would be difficult.
1569 */
1570 if (is_multicast_ether_addr(hdr->addr1)) {
1571 ivlen = mvm->gtk_ivlen;
1572 icvlen += mvm->gtk_icvlen;
1573 } else {
1574 ivlen = mvm->ptk_ivlen;
1575 icvlen += mvm->ptk_icvlen;
1576 }
1577 }
1578
1579 /* if truncated, FCS/ICV is (partially) gone */
1580 if (truncated >= icvlen) {
1581 icvlen = 0;
1582 truncated -= icvlen;
1583 } else {
1584 icvlen -= truncated;
1585 truncated = 0;
1586 }
1587
1588 pktsize -= ivlen + icvlen;
1589 pktdata += ivlen;
1590
1591 skb_put_data(pkt, pktdata, pktsize);
1592
1593 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1594 goto report;
1595 wakeup.packet = pkt->data;
1596 wakeup.packet_present_len = pkt->len;
1597 wakeup.packet_len = pkt->len - truncated;
1598 wakeup.packet_80211 = false;
1599 } else {
1600 int fcslen = 4;
1601
1602 if (truncated >= 4) {
1603 truncated -= 4;
1604 fcslen = 0;
1605 } else {
1606 fcslen -= truncated;
1607 truncated = 0;
1608 }
1609 pktsize -= fcslen;
1610 wakeup.packet = status->wake_packet;
1611 wakeup.packet_present_len = pktsize;
1612 wakeup.packet_len = pktlen - truncated;
1613 wakeup.packet_80211 = true;
1614 }
1615 }
1616
1617 report:
1618 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1619 kfree_skb(pkt);
1620 }
1621
iwl_mvm_le64_to_aes_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1622 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1623 {
1624 u64 pn = le64_to_cpu(le_pn);
1625
1626 seq->ccmp.pn[0] = pn >> 40;
1627 seq->ccmp.pn[1] = pn >> 32;
1628 seq->ccmp.pn[2] = pn >> 24;
1629 seq->ccmp.pn[3] = pn >> 16;
1630 seq->ccmp.pn[4] = pn >> 8;
1631 seq->ccmp.pn[5] = pn;
1632 }
1633
iwl_mvm_aes_sc_to_seq(struct aes_sc * sc,struct ieee80211_key_seq * seq)1634 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1635 struct ieee80211_key_seq *seq)
1636 {
1637 iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1638 }
1639
iwl_mvm_le64_to_tkip_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1640 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1641 {
1642 u64 pn = le64_to_cpu(le_pn);
1643
1644 seq->tkip.iv16 = (u16)pn;
1645 seq->tkip.iv32 = (u32)(pn >> 16);
1646 }
1647
iwl_mvm_tkip_sc_to_seq(struct tkip_sc * sc,struct ieee80211_key_seq * seq)1648 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1649 struct ieee80211_key_seq *seq)
1650 {
1651 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1652 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1653 }
1654
iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf * key,struct ieee80211_key_seq * seq)1655 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1656 struct ieee80211_key_seq *seq)
1657 {
1658 int tid;
1659
1660 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1661 ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1662 }
1663
iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm * mvm,struct iwl_wowlan_status_data * status,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)1664 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1665 struct iwl_wowlan_status_data *status,
1666 struct ieee80211_sta *sta,
1667 struct ieee80211_key_conf *key)
1668 {
1669 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1670 struct iwl_mvm_key_pn *ptk_pn;
1671 int tid;
1672
1673 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1674
1675 if (!iwl_mvm_has_new_rx_api(mvm))
1676 return;
1677
1678
1679 rcu_read_lock();
1680 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1681 if (WARN_ON(!ptk_pn)) {
1682 rcu_read_unlock();
1683 return;
1684 }
1685
1686 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1687 int i;
1688
1689 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1690 memcpy(ptk_pn->q[i].pn[tid],
1691 status->ptk.aes.seq[tid].ccmp.pn,
1692 IEEE80211_CCMP_PN_LEN);
1693 }
1694 rcu_read_unlock();
1695 }
1696
iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data * status,union iwl_all_tsc_rsc * sc)1697 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1698 union iwl_all_tsc_rsc *sc)
1699 {
1700 int i;
1701
1702 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1703 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1704
1705 /* GTK RX counters */
1706 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1707 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1708 &status->gtk_seq[0].tkip.seq[i]);
1709 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1710 &status->gtk_seq[0].aes.seq[i]);
1711 }
1712 status->gtk_seq[0].valid = true;
1713 status->gtk_seq[0].key_id = -1;
1714
1715 /* PTK TX counter */
1716 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1717 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1718 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1719
1720 /* PTK RX counters */
1721 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1722 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1723 &status->ptk.tkip.seq[i]);
1724 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1725 &status->ptk.aes.seq[i]);
1726 }
1727 }
1728
1729 static void
iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc,unsigned int idx,unsigned int key_id)1730 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1731 struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1732 unsigned int idx, unsigned int key_id)
1733 {
1734 int tid;
1735
1736 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1737 iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1738 &status->gtk_seq[idx].tkip.seq[tid]);
1739 iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1740 &status->gtk_seq[idx].aes.seq[tid]);
1741 }
1742
1743 status->gtk_seq[idx].valid = true;
1744 status->gtk_seq[idx].key_id = key_id;
1745 }
1746
1747 static void
iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc)1748 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1749 struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1750 {
1751 int i, tid;
1752
1753 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1754 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1755 BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1756
1757 /* GTK RX counters */
1758 for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1759 u8 entry = sc->mcast_key_id_map[i];
1760
1761 if (entry < ARRAY_SIZE(sc->mcast_rsc))
1762 iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1763 entry, i);
1764 }
1765
1766 /* PTK TX counters not needed, assigned in device */
1767
1768 /* PTK RX counters */
1769 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1770 iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1771 &status->ptk.tkip.seq[tid]);
1772 iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1773 &status->ptk.aes.seq[tid]);
1774 }
1775 }
1776
iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,int idx)1777 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1778 struct iwl_wowlan_status_data *status,
1779 int idx)
1780 {
1781 switch (key->cipher) {
1782 case WLAN_CIPHER_SUITE_CCMP:
1783 case WLAN_CIPHER_SUITE_GCMP:
1784 case WLAN_CIPHER_SUITE_GCMP_256:
1785 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1786 break;
1787 case WLAN_CIPHER_SUITE_TKIP:
1788 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1789 break;
1790 default:
1791 WARN_ON(1);
1792 }
1793 }
1794
iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,bool installed)1795 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1796 struct iwl_wowlan_status_data *status,
1797 bool installed)
1798 {
1799 int i;
1800
1801 for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1802 if (!status->gtk_seq[i].valid)
1803 continue;
1804
1805 /* Handle the case where we know the key ID */
1806 if (status->gtk_seq[i].key_id == key->keyidx) {
1807 s8 new_key_id = -1;
1808
1809 if (status->num_of_gtk_rekeys)
1810 new_key_id = status->gtk[0].flags &
1811 IWL_WOWLAN_GTK_IDX_MASK;
1812
1813 /* Don't install a new key's value to an old key */
1814 if (new_key_id != key->keyidx)
1815 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1816 continue;
1817 }
1818
1819 /* handle the case where we didn't, last key only */
1820 if (status->gtk_seq[i].key_id == -1 &&
1821 (!status->num_of_gtk_rekeys || installed))
1822 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1823 }
1824 }
1825
1826 struct iwl_mvm_d3_gtk_iter_data {
1827 struct iwl_mvm *mvm;
1828 struct iwl_wowlan_status_data *status;
1829 u32 gtk_cipher, igtk_cipher, bigtk_cipher;
1830 bool unhandled_cipher, igtk_support, bigtk_support;
1831 int num_keys;
1832 };
1833
iwl_mvm_d3_find_last_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1834 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1835 struct ieee80211_vif *vif,
1836 struct ieee80211_sta *sta,
1837 struct ieee80211_key_conf *key,
1838 void *_data)
1839 {
1840 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1841 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1842
1843 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1844 return;
1845
1846 if (data->unhandled_cipher)
1847 return;
1848
1849 switch (key->cipher) {
1850 case WLAN_CIPHER_SUITE_WEP40:
1851 case WLAN_CIPHER_SUITE_WEP104:
1852 /* ignore WEP completely, nothing to do */
1853 return;
1854 case WLAN_CIPHER_SUITE_CCMP:
1855 case WLAN_CIPHER_SUITE_GCMP:
1856 case WLAN_CIPHER_SUITE_GCMP_256:
1857 case WLAN_CIPHER_SUITE_TKIP:
1858 /* we support these */
1859 data->gtk_cipher = key->cipher;
1860 break;
1861 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1862 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1863 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1864 case WLAN_CIPHER_SUITE_AES_CMAC:
1865 /* we support these */
1866 if (data->igtk_support &&
1867 (key->keyidx == 4 || key->keyidx == 5)) {
1868 data->igtk_cipher = key->cipher;
1869 } else if (data->bigtk_support &&
1870 (key->keyidx == 6 || key->keyidx == 7)) {
1871 data->bigtk_cipher = key->cipher;
1872 } else {
1873 data->unhandled_cipher = true;
1874 return;
1875 }
1876 break;
1877 default:
1878 /* everything else - disconnect from AP */
1879 data->unhandled_cipher = true;
1880 return;
1881 }
1882
1883 data->num_keys++;
1884 }
1885
1886 static void
iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data * key,struct ieee80211_key_seq * seq,u32 cipher)1887 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1888 struct ieee80211_key_seq *seq, u32 cipher)
1889 {
1890 switch (cipher) {
1891 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1892 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1893 BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1894 memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1895 break;
1896 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1897 case WLAN_CIPHER_SUITE_AES_CMAC:
1898 BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1899 memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1900 break;
1901 default:
1902 WARN_ON(1);
1903 }
1904 }
1905
1906 static void
iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data * status,struct ieee80211_key_conf * key,struct iwl_multicast_key_data * key_data)1907 iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data *status,
1908 struct ieee80211_key_conf *key,
1909 struct iwl_multicast_key_data *key_data)
1910 {
1911 if (status->num_of_gtk_rekeys && key_data->len) {
1912 /* remove rekeyed key */
1913 ieee80211_remove_key(key);
1914 } else {
1915 struct ieee80211_key_seq seq;
1916
1917 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data,
1918 &seq,
1919 key->cipher);
1920 ieee80211_set_key_rx_seq(key, 0, &seq);
1921 }
1922 }
1923
iwl_mvm_d3_update_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1924 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1925 struct ieee80211_vif *vif,
1926 struct ieee80211_sta *sta,
1927 struct ieee80211_key_conf *key,
1928 void *_data)
1929 {
1930 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1931 struct iwl_wowlan_status_data *status = data->status;
1932 s8 keyidx;
1933 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1934
1935 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1936 return;
1937
1938 if (data->unhandled_cipher)
1939 return;
1940
1941 switch (key->cipher) {
1942 case WLAN_CIPHER_SUITE_WEP40:
1943 case WLAN_CIPHER_SUITE_WEP104:
1944 /* ignore WEP completely, nothing to do */
1945 return;
1946 case WLAN_CIPHER_SUITE_CCMP:
1947 case WLAN_CIPHER_SUITE_GCMP:
1948 case WLAN_CIPHER_SUITE_GCMP_256:
1949 if (sta) {
1950 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1951 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1952 return;
1953 }
1954 fallthrough;
1955 case WLAN_CIPHER_SUITE_TKIP:
1956 if (sta) {
1957 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1958 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1959 return;
1960 }
1961 keyidx = key->keyidx;
1962 /* The current key is always sent by the FW, even if it wasn't
1963 * rekeyed during D3.
1964 * We remove an existing key if it has the same index as
1965 * a new key
1966 */
1967 if (status->num_of_gtk_rekeys &&
1968 ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1969 (status->gtk[1].len && keyidx == status->gtk[1].id))) {
1970 ieee80211_remove_key(key);
1971 } else {
1972 iwl_mvm_set_key_rx_seq(key, data->status, false);
1973 }
1974 break;
1975 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1976 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1977 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1978 case WLAN_CIPHER_SUITE_AES_CMAC:
1979 if (key->keyidx == 4 || key->keyidx == 5) {
1980 iwl_mvm_d3_update_igtk_bigtk(status, key,
1981 &status->igtk);
1982 }
1983 if (key->keyidx == 6 || key->keyidx == 7) {
1984 u8 idx = key->keyidx == status->bigtk[1].id;
1985
1986 iwl_mvm_d3_update_igtk_bigtk(status, key,
1987 &status->bigtk[idx]);
1988 }
1989 }
1990 }
1991
1992 struct iwl_mvm_d3_mlo_old_keys {
1993 u32 cipher[IEEE80211_MLD_MAX_NUM_LINKS][WOWLAN_MLO_GTK_KEY_NUM_TYPES];
1994 struct ieee80211_key_conf *key[IEEE80211_MLD_MAX_NUM_LINKS][8];
1995 };
1996
iwl_mvm_mlo_key_ciphers(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)1997 static void iwl_mvm_mlo_key_ciphers(struct ieee80211_hw *hw,
1998 struct ieee80211_vif *vif,
1999 struct ieee80211_sta *sta,
2000 struct ieee80211_key_conf *key,
2001 void *data)
2002 {
2003 struct iwl_mvm_d3_mlo_old_keys *old_keys = data;
2004 enum iwl_wowlan_mlo_gtk_type key_type;
2005
2006 if (key->link_id < 0)
2007 return;
2008
2009 if (WARN_ON(key->link_id >= IEEE80211_MLD_MAX_NUM_LINKS ||
2010 key->keyidx >= 8))
2011 return;
2012
2013 if (WARN_ON(old_keys->key[key->link_id][key->keyidx]))
2014 return;
2015
2016 switch (key->cipher) {
2017 case WLAN_CIPHER_SUITE_CCMP:
2018 case WLAN_CIPHER_SUITE_GCMP:
2019 case WLAN_CIPHER_SUITE_GCMP_256:
2020 key_type = WOWLAN_MLO_GTK_KEY_TYPE_GTK;
2021 break;
2022 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2023 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2024 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2025 case WLAN_CIPHER_SUITE_AES_CMAC:
2026 if (key->keyidx == 4 || key->keyidx == 5) {
2027 key_type = WOWLAN_MLO_GTK_KEY_TYPE_IGTK;
2028 break;
2029 } else if (key->keyidx == 6 || key->keyidx == 7) {
2030 key_type = WOWLAN_MLO_GTK_KEY_TYPE_BIGTK;
2031 break;
2032 }
2033 return;
2034 default:
2035 /* ignore WEP/TKIP or unknown ciphers */
2036 return;
2037 }
2038
2039 old_keys->cipher[key->link_id][key_type] = key->cipher;
2040 old_keys->key[key->link_id][key->keyidx] = key;
2041 }
2042
iwl_mvm_mlo_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm)2043 static bool iwl_mvm_mlo_gtk_rekey(struct iwl_wowlan_status_data *status,
2044 struct ieee80211_vif *vif,
2045 struct iwl_mvm *mvm)
2046 {
2047 int i;
2048 struct iwl_mvm_d3_mlo_old_keys *old_keys;
2049 bool ret = true;
2050
2051 IWL_DEBUG_WOWLAN(mvm, "Num of MLO Keys: %d\n", status->num_mlo_keys);
2052 if (!status->num_mlo_keys)
2053 return true;
2054
2055 old_keys = kzalloc(sizeof(*old_keys), GFP_KERNEL);
2056 if (!old_keys)
2057 return false;
2058
2059 /* find the cipher for each mlo key */
2060 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_mlo_key_ciphers, old_keys);
2061
2062 for (i = 0; i < status->num_mlo_keys; i++) {
2063 struct iwl_wowlan_mlo_gtk *mlo_key = &status->mlo_keys[i];
2064 struct ieee80211_key_conf *key, *old_key;
2065 struct ieee80211_key_seq seq;
2066 struct {
2067 struct ieee80211_key_conf conf;
2068 u8 key[32];
2069 } conf = {};
2070 u16 flags = le16_to_cpu(mlo_key->flags);
2071 int j, link_id, key_id, key_type;
2072
2073 link_id = u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_LINK_ID_MSK);
2074 key_id = u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_KEY_ID_MSK);
2075 key_type = u16_get_bits(flags,
2076 WOWLAN_MLO_GTK_FLAG_KEY_TYPE_MSK);
2077
2078 if (!(vif->valid_links & BIT(link_id)))
2079 continue;
2080
2081 if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS ||
2082 key_id >= 8 ||
2083 key_type >= WOWLAN_MLO_GTK_KEY_NUM_TYPES))
2084 continue;
2085
2086 conf.conf.cipher = old_keys->cipher[link_id][key_type];
2087 /* WARN_ON? */
2088 if (!conf.conf.cipher)
2089 continue;
2090
2091 conf.conf.keylen = 0;
2092 switch (conf.conf.cipher) {
2093 case WLAN_CIPHER_SUITE_CCMP:
2094 case WLAN_CIPHER_SUITE_GCMP:
2095 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
2096 break;
2097 case WLAN_CIPHER_SUITE_GCMP_256:
2098 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
2099 break;
2100 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2101 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2102 break;
2103 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2104 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2105 break;
2106 case WLAN_CIPHER_SUITE_AES_CMAC:
2107 conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2108 break;
2109 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2110 conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2111 break;
2112 }
2113
2114 if (WARN_ON(!conf.conf.keylen ||
2115 conf.conf.keylen > sizeof(conf.key)))
2116 continue;
2117
2118 memcpy(conf.conf.key, mlo_key->key, conf.conf.keylen);
2119 conf.conf.keyidx = key_id;
2120
2121 old_key = old_keys->key[link_id][key_id];
2122 if (old_key) {
2123 IWL_DEBUG_WOWLAN(mvm,
2124 "Remove MLO key id %d, link id %d\n",
2125 key_id, link_id);
2126 ieee80211_remove_key(old_key);
2127 }
2128
2129 IWL_DEBUG_WOWLAN(mvm, "Add MLO key id %d, link id %d\n",
2130 key_id, link_id);
2131 key = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2132 if (WARN_ON(IS_ERR(key))) {
2133 ret = false;
2134 goto out;
2135 }
2136
2137 /*
2138 * mac80211 expects the pn in big-endian
2139 * also note that seq is a union of all cipher types
2140 * (ccmp, gcmp, cmac, gmac), and they all have the same
2141 * pn field (of length 6) so just copy it to ccmp.pn.
2142 */
2143 for (j = 5; j >= 0; j--)
2144 seq.ccmp.pn[5 - j] = mlo_key->pn[j];
2145
2146 /* group keys are non-QoS and use TID 0 */
2147 ieee80211_set_key_rx_seq(key, 0, &seq);
2148 }
2149
2150 out:
2151 kfree(old_keys);
2152 return ret;
2153 }
2154
iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm,u32 gtk_cipher)2155 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
2156 struct ieee80211_vif *vif,
2157 struct iwl_mvm *mvm, u32 gtk_cipher)
2158 {
2159 int i, j;
2160 struct ieee80211_key_conf *key;
2161 struct {
2162 struct ieee80211_key_conf conf;
2163 u8 key[32];
2164 } conf = {
2165 .conf.cipher = gtk_cipher,
2166 };
2167 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2168
2169 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
2170 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
2171 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
2172 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
2173 BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk[0].key));
2174
2175 switch (gtk_cipher) {
2176 case WLAN_CIPHER_SUITE_CCMP:
2177 case WLAN_CIPHER_SUITE_GCMP:
2178 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
2179 break;
2180 case WLAN_CIPHER_SUITE_GCMP_256:
2181 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
2182 break;
2183 case WLAN_CIPHER_SUITE_TKIP:
2184 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
2185 break;
2186 default:
2187 WARN_ON(1);
2188 }
2189
2190 for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
2191 if (!status->gtk[i].len)
2192 continue;
2193
2194 conf.conf.keyidx = status->gtk[i].id;
2195 IWL_DEBUG_WOWLAN(mvm,
2196 "Received from FW GTK cipher %d, key index %d\n",
2197 conf.conf.cipher, conf.conf.keyidx);
2198 memcpy(conf.conf.key, status->gtk[i].key,
2199 sizeof(status->gtk[i].key));
2200
2201 key = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2202 if (IS_ERR(key))
2203 return false;
2204
2205 for (j = 0; j < ARRAY_SIZE(status->gtk_seq); j++) {
2206 if (!status->gtk_seq[j].valid ||
2207 status->gtk_seq[j].key_id != key->keyidx)
2208 continue;
2209 iwl_mvm_set_key_rx_seq_idx(key, status, j);
2210 break;
2211 }
2212 WARN_ON(j == ARRAY_SIZE(status->gtk_seq));
2213 }
2214
2215 return true;
2216 }
2217
2218 static bool
iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,u32 cipher,struct iwl_multicast_key_data * key_data)2219 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
2220 struct ieee80211_vif *vif, u32 cipher,
2221 struct iwl_multicast_key_data *key_data)
2222 {
2223 struct ieee80211_key_conf *key_config;
2224 struct {
2225 struct ieee80211_key_conf conf;
2226 u8 key[WOWLAN_KEY_MAX_SIZE];
2227 } conf = {
2228 .conf.cipher = cipher,
2229 .conf.keyidx = key_data->id,
2230 };
2231 struct ieee80211_key_seq seq;
2232 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2233
2234 if (!key_data->len)
2235 return true;
2236
2237 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf.conf.cipher);
2238
2239 switch (cipher) {
2240 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2241 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2242 break;
2243 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2244 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2245 break;
2246 case WLAN_CIPHER_SUITE_AES_CMAC:
2247 conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2248 break;
2249 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2250 conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2251 break;
2252 default:
2253 WARN_ON(1);
2254 }
2255 BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
2256 memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
2257
2258 key_config = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2259 if (IS_ERR(key_config))
2260 return false;
2261 ieee80211_set_key_rx_seq(key_config, 0, &seq);
2262
2263 if (key_config->keyidx == 4 || key_config->keyidx == 5) {
2264 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2265 struct iwl_mvm_vif_link_info *mvm_link;
2266
2267 link_id = link_id < 0 ? 0 : link_id;
2268 mvm_link = mvmvif->link[link_id];
2269 mvm_link->igtk = key_config;
2270 }
2271
2272 return true;
2273 }
2274
iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm * mvm)2275 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2276 {
2277 u8 notif_ver;
2278
2279 if (!fw_has_api(&mvm->fw->ucode_capa,
2280 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2281 return 6;
2282
2283 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2284 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2285 WOWLAN_GET_STATUSES, 0);
2286 if (!notif_ver)
2287 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2288 WOWLAN_GET_STATUSES, 7);
2289
2290 return notif_ver;
2291 }
2292
iwl_mvm_setup_connection_keep(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2293 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2294 struct ieee80211_vif *vif,
2295 struct iwl_wowlan_status_data *status)
2296 {
2297 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2298 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2299 .mvm = mvm,
2300 .status = status,
2301 };
2302 int i;
2303 u32 disconnection_reasons =
2304 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2305 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2306
2307 if (!status || !vif->bss_conf.bssid)
2308 return false;
2309
2310 if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2311 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2312 WOWLAN_INFO_NOTIFICATION,
2313 0))
2314 gtkdata.igtk_support = true;
2315
2316 if (iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2317 WOWLAN_INFO_NOTIFICATION,
2318 0) >= 3)
2319 gtkdata.bigtk_support = true;
2320
2321 /* find last GTK that we used initially, if any */
2322 ieee80211_iter_keys(mvm->hw, vif,
2323 iwl_mvm_d3_find_last_keys, >kdata);
2324 /* not trying to keep connections with MFP/unhandled ciphers */
2325 if (gtkdata.unhandled_cipher)
2326 return false;
2327 if (!gtkdata.num_keys)
2328 goto out;
2329
2330 /*
2331 * invalidate all other GTKs that might still exist and update
2332 * the one that we used
2333 */
2334 ieee80211_iter_keys(mvm->hw, vif,
2335 iwl_mvm_d3_update_keys, >kdata);
2336
2337 if (status->num_of_gtk_rekeys) {
2338 __be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2339
2340 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2341 status->num_of_gtk_rekeys);
2342
2343 if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2344 return false;
2345
2346 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2347 gtkdata.igtk_cipher,
2348 &status->igtk))
2349 return false;
2350
2351 for (i = 0; i < ARRAY_SIZE(status->bigtk); i++) {
2352 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2353 gtkdata.bigtk_cipher,
2354 &status->bigtk[i]))
2355 return false;
2356 }
2357
2358 if (!iwl_mvm_mlo_gtk_rekey(status, vif, mvm))
2359 return false;
2360
2361 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2362 (void *)&replay_ctr, GFP_KERNEL);
2363 }
2364
2365 out:
2366 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2367 WOWLAN_GET_STATUSES,
2368 IWL_FW_CMD_VER_UNKNOWN) < 10) {
2369 mvmvif->seqno_valid = true;
2370 /* +0x10 because the set API expects next-to-use, not last-used */
2371 mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2372 }
2373
2374 if (status->wakeup_reasons & disconnection_reasons)
2375 return false;
2376
2377 return true;
2378 }
2379
iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v2 * data)2380 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2381 struct iwl_wowlan_gtk_status_v2 *data)
2382 {
2383 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2384 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2385 sizeof(data->tkip_mic_key) >
2386 sizeof(status->gtk[0].key));
2387
2388 status->gtk[0].len = data->key_len;
2389 status->gtk[0].flags = data->key_flags;
2390
2391 memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2392
2393 /* if it's as long as the TKIP encryption key, copy MIC key */
2394 if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2395 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2396 data->tkip_mic_key, sizeof(data->tkip_mic_key));
2397 }
2398
iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v3 * data)2399 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2400 struct iwl_wowlan_gtk_status_v3 *data)
2401 {
2402 int data_idx, status_idx = 0;
2403
2404 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2405 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2406 sizeof(data[0].tkip_mic_key) >
2407 sizeof(status->gtk[0].key));
2408 BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2409 for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2410 if (!(data[data_idx].key_len))
2411 continue;
2412 status->gtk[status_idx].len = data[data_idx].key_len;
2413 status->gtk[status_idx].flags = data[data_idx].key_flags;
2414 status->gtk[status_idx].id = status->gtk[status_idx].flags &
2415 IWL_WOWLAN_GTK_IDX_MASK;
2416
2417 memcpy(status->gtk[status_idx].key, data[data_idx].key,
2418 sizeof(data[data_idx].key));
2419
2420 /* if it's as long as the TKIP encryption key, copy MIC key */
2421 if (status->gtk[status_idx].len ==
2422 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2423 memcpy(status->gtk[status_idx].key +
2424 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2425 data[data_idx].tkip_mic_key,
2426 sizeof(data[data_idx].tkip_mic_key));
2427 status_idx++;
2428 }
2429 }
2430
iwl_mvm_convert_igtk(struct iwl_wowlan_status_data * status,struct iwl_wowlan_igtk_status * data)2431 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2432 struct iwl_wowlan_igtk_status *data)
2433 {
2434 int i;
2435
2436 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2437 BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2438
2439 if (!data->key_len)
2440 return;
2441
2442 status->igtk.len = data->key_len;
2443 status->igtk.flags = data->key_flags;
2444 status->igtk.id = u32_get_bits(data->key_flags,
2445 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2446 + WOWLAN_IGTK_MIN_INDEX;
2447
2448 memcpy(status->igtk.key, data->key, sizeof(data->key));
2449
2450 /* mac80211 expects big endian for memcmp() to work, convert */
2451 for (i = 0; i < sizeof(data->ipn); i++)
2452 status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2453 }
2454
iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data * status,const struct iwl_wowlan_igtk_status * data)2455 static void iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data *status,
2456 const struct iwl_wowlan_igtk_status *data)
2457 {
2458 int data_idx, status_idx = 0;
2459
2460 BUILD_BUG_ON(ARRAY_SIZE(status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
2461
2462 for (data_idx = 0; data_idx < WOWLAN_BIGTK_KEYS_NUM; data_idx++) {
2463 if (!data[data_idx].key_len)
2464 continue;
2465
2466 status->bigtk[status_idx].len = data[data_idx].key_len;
2467 status->bigtk[status_idx].flags = data[data_idx].key_flags;
2468 status->bigtk[status_idx].id =
2469 u32_get_bits(data[data_idx].key_flags,
2470 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2471 + WOWLAN_BIGTK_MIN_INDEX;
2472
2473 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].key) <
2474 sizeof(data[data_idx].key));
2475 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].ipn) <
2476 sizeof(data[data_idx].ipn));
2477
2478 memcpy(status->bigtk[status_idx].key, data[data_idx].key,
2479 sizeof(data[data_idx].key));
2480 memcpy(status->bigtk[status_idx].ipn, data[data_idx].ipn,
2481 sizeof(data[data_idx].ipn));
2482 status_idx++;
2483 }
2484 }
2485
iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif * data,struct iwl_wowlan_status_data * status,u32 len)2486 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2487 struct iwl_wowlan_info_notif *data,
2488 struct iwl_wowlan_status_data *status,
2489 u32 len)
2490 {
2491 u32 expected_len = sizeof(*data) +
2492 data->num_mlo_link_keys * sizeof(status->mlo_keys[0]);
2493
2494 if (len < expected_len) {
2495 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2496 status = NULL;
2497 return;
2498 }
2499
2500 if (mvm->fast_resume)
2501 return;
2502
2503 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2504 iwl_mvm_convert_gtk_v3(status, data->gtk);
2505 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2506 iwl_mvm_convert_bigtk(status, data->bigtk);
2507 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2508 status->pattern_number = le16_to_cpu(data->pattern_number);
2509 status->tid_offloaded_tx = data->tid_offloaded_tx;
2510 if (IWL_FW_CHECK(mvm,
2511 data->tid_offloaded_tx >=
2512 ARRAY_SIZE(status->qos_seq_ctr),
2513 "tid_offloaded_tx is out of bound %d\n",
2514 data->tid_offloaded_tx))
2515 data->tid_offloaded_tx = 0;
2516 status->qos_seq_ctr[data->tid_offloaded_tx] =
2517 le16_to_cpu(data->qos_seq_ctr);
2518 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2519 status->num_of_gtk_rekeys =
2520 le32_to_cpu(data->num_of_gtk_rekeys);
2521 status->received_beacons = le32_to_cpu(data->received_beacons);
2522 status->tid_tear_down = data->tid_tear_down;
2523
2524 if (data->num_mlo_link_keys) {
2525 status->num_mlo_keys = data->num_mlo_link_keys;
2526 if (IWL_FW_CHECK(mvm,
2527 status->num_mlo_keys > WOWLAN_MAX_MLO_KEYS,
2528 "Too many mlo keys: %d, max %d\n",
2529 status->num_mlo_keys, WOWLAN_MAX_MLO_KEYS))
2530 status->num_mlo_keys = WOWLAN_MAX_MLO_KEYS;
2531 memcpy(status->mlo_keys, data->mlo_gtks,
2532 status->num_mlo_keys * sizeof(status->mlo_keys[0]));
2533 }
2534 }
2535
2536 static void
iwl_mvm_parse_wowlan_info_notif_v4(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif_v4 * data,struct iwl_wowlan_status_data * status,u32 len,bool has_mlo_keys)2537 iwl_mvm_parse_wowlan_info_notif_v4(struct iwl_mvm *mvm,
2538 struct iwl_wowlan_info_notif_v4 *data,
2539 struct iwl_wowlan_status_data *status,
2540 u32 len, bool has_mlo_keys)
2541 {
2542 u32 i;
2543 u32 expected_len = sizeof(*data);
2544
2545 if (has_mlo_keys)
2546 expected_len += (data->num_mlo_link_keys *
2547 sizeof(status->mlo_keys[0]));
2548
2549 if (len < expected_len) {
2550 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2551 status = NULL;
2552 return;
2553 }
2554
2555 if (mvm->fast_resume)
2556 return;
2557
2558 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2559 iwl_mvm_convert_gtk_v3(status, data->gtk);
2560 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2561 iwl_mvm_convert_bigtk(status, data->bigtk);
2562 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2563 status->pattern_number = le16_to_cpu(data->pattern_number);
2564 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2565 status->qos_seq_ctr[i] =
2566 le16_to_cpu(data->qos_seq_ctr[i]);
2567 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2568 status->num_of_gtk_rekeys =
2569 le32_to_cpu(data->num_of_gtk_rekeys);
2570 status->received_beacons = le32_to_cpu(data->received_beacons);
2571 status->tid_tear_down = data->tid_tear_down;
2572
2573 if (has_mlo_keys && data->num_mlo_link_keys) {
2574 status->num_mlo_keys = data->num_mlo_link_keys;
2575 if (IWL_FW_CHECK(mvm,
2576 status->num_mlo_keys > WOWLAN_MAX_MLO_KEYS,
2577 "Too many mlo keys: %d, max %d\n",
2578 status->num_mlo_keys, WOWLAN_MAX_MLO_KEYS))
2579 status->num_mlo_keys = WOWLAN_MAX_MLO_KEYS;
2580 memcpy(status->mlo_keys, data->mlo_gtks,
2581 status->num_mlo_keys * sizeof(status->mlo_keys[0]));
2582 }
2583 }
2584
2585 static void
iwl_mvm_parse_wowlan_info_notif_v2(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif_v2 * data,struct iwl_wowlan_status_data * status,u32 len)2586 iwl_mvm_parse_wowlan_info_notif_v2(struct iwl_mvm *mvm,
2587 struct iwl_wowlan_info_notif_v2 *data,
2588 struct iwl_wowlan_status_data *status,
2589 u32 len)
2590 {
2591 u32 i;
2592
2593 if (len < sizeof(*data)) {
2594 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2595 status = NULL;
2596 return;
2597 }
2598
2599 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2600 iwl_mvm_convert_gtk_v3(status, data->gtk);
2601 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2602 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2603 status->pattern_number = le16_to_cpu(data->pattern_number);
2604 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2605 status->qos_seq_ctr[i] =
2606 le16_to_cpu(data->qos_seq_ctr[i]);
2607 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2608 status->num_of_gtk_rekeys =
2609 le32_to_cpu(data->num_of_gtk_rekeys);
2610 status->received_beacons = le32_to_cpu(data->received_beacons);
2611 status->tid_tear_down = data->tid_tear_down;
2612 }
2613
2614 /* Occasionally, templates would be nice. This is one of those times ... */
2615 #define iwl_mvm_parse_wowlan_status_common(_ver) \
2616 static struct iwl_wowlan_status_data * \
2617 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \
2618 struct iwl_wowlan_status_ ##_ver *data,\
2619 int len) \
2620 { \
2621 struct iwl_wowlan_status_data *status; \
2622 int data_size, i; \
2623 \
2624 if (len < sizeof(*data)) { \
2625 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2626 return NULL; \
2627 } \
2628 \
2629 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \
2630 if (len != sizeof(*data) + data_size) { \
2631 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2632 return NULL; \
2633 } \
2634 \
2635 status = kzalloc(sizeof(*status), GFP_KERNEL); \
2636 if (!status) \
2637 return NULL; \
2638 \
2639 /* copy all the common fields */ \
2640 status->replay_ctr = le64_to_cpu(data->replay_ctr); \
2641 status->pattern_number = le16_to_cpu(data->pattern_number); \
2642 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \
2643 for (i = 0; i < 8; i++) \
2644 status->qos_seq_ctr[i] = \
2645 le16_to_cpu(data->qos_seq_ctr[i]); \
2646 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \
2647 status->num_of_gtk_rekeys = \
2648 le32_to_cpu(data->num_of_gtk_rekeys); \
2649 status->received_beacons = le32_to_cpu(data->received_beacons); \
2650 status->wake_packet_length = \
2651 le32_to_cpu(data->wake_packet_length); \
2652 status->wake_packet_bufsize = \
2653 le32_to_cpu(data->wake_packet_bufsize); \
2654 if (status->wake_packet_bufsize) { \
2655 status->wake_packet = \
2656 kmemdup(data->wake_packet, \
2657 status->wake_packet_bufsize, \
2658 GFP_KERNEL); \
2659 if (!status->wake_packet) { \
2660 kfree(status); \
2661 return NULL; \
2662 } \
2663 } else { \
2664 status->wake_packet = NULL; \
2665 } \
2666 \
2667 return status; \
2668 }
2669
2670 iwl_mvm_parse_wowlan_status_common(v6)
iwl_mvm_parse_wowlan_status_common(v7)2671 iwl_mvm_parse_wowlan_status_common(v7)
2672 iwl_mvm_parse_wowlan_status_common(v9)
2673 iwl_mvm_parse_wowlan_status_common(v12)
2674
2675 static struct iwl_wowlan_status_data *
2676 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2677 {
2678 struct iwl_wowlan_status_data *status;
2679 struct iwl_wowlan_get_status_cmd get_status_cmd = {
2680 .sta_id = cpu_to_le32(sta_id),
2681 };
2682 struct iwl_host_cmd cmd = {
2683 .id = WOWLAN_GET_STATUSES,
2684 .flags = CMD_WANT_SKB,
2685 .data = { &get_status_cmd, },
2686 .len = { sizeof(get_status_cmd), },
2687 };
2688 int ret, len;
2689 u8 notif_ver;
2690 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2691 IWL_FW_CMD_VER_UNKNOWN);
2692
2693 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2694 cmd.len[0] = 0;
2695
2696 lockdep_assert_held(&mvm->mutex);
2697
2698 ret = iwl_mvm_send_cmd(mvm, &cmd);
2699 if (ret) {
2700 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2701 return ERR_PTR(ret);
2702 }
2703
2704 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2705
2706 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2707 notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2708
2709 if (notif_ver < 7) {
2710 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2711
2712 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2713 if (!status)
2714 goto out_free_resp;
2715
2716 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2717 sizeof(status->gtk[0].key));
2718 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2719 sizeof(v6->gtk.tkip_mic_key) >
2720 sizeof(status->gtk[0].key));
2721
2722 /* copy GTK info to the right place */
2723 memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2724 sizeof(v6->gtk.decrypt_key));
2725 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2726 v6->gtk.tkip_mic_key,
2727 sizeof(v6->gtk.tkip_mic_key));
2728
2729 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2730
2731 /* hardcode the key length to 16 since v6 only supports 16 */
2732 status->gtk[0].len = 16;
2733
2734 /*
2735 * The key index only uses 2 bits (values 0 to 3) and
2736 * we always set bit 7 which means this is the
2737 * currently used key.
2738 */
2739 status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2740 } else if (notif_ver == 7) {
2741 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2742
2743 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2744 if (!status)
2745 goto out_free_resp;
2746
2747 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2748 iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2749 iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2750 } else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2751 struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2752
2753 /* these three command versions have same layout and size, the
2754 * difference is only in a few not used (reserved) fields.
2755 */
2756 status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2757 if (!status)
2758 goto out_free_resp;
2759
2760 iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2761 iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2762 iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2763
2764 status->tid_tear_down = v9->tid_tear_down;
2765 } else if (notif_ver == 12) {
2766 struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2767
2768 status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2769 if (!status)
2770 goto out_free_resp;
2771
2772 iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2773 iwl_mvm_convert_gtk_v3(status, v12->gtk);
2774 iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2775
2776 status->tid_tear_down = v12->tid_tear_down;
2777 } else {
2778 IWL_ERR(mvm,
2779 "Firmware advertises unknown WoWLAN status response %d!\n",
2780 notif_ver);
2781 status = NULL;
2782 }
2783
2784 out_free_resp:
2785 iwl_free_resp(&cmd);
2786 return status;
2787 }
2788
2789 /* releases the MVM mutex */
iwl_mvm_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2790 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2791 struct ieee80211_vif *vif,
2792 struct iwl_wowlan_status_data *status)
2793 {
2794 int i;
2795 bool keep = false;
2796 struct iwl_mvm_sta *mvm_ap_sta;
2797 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2798 int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2799 struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
2800 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
2801 PROT_OFFLOAD_GROUP,
2802 WOWLAN_INFO_NOTIFICATION,
2803 IWL_FW_CMD_VER_UNKNOWN);
2804
2805 if (WARN_ON(!mvm_link))
2806 goto out_unlock;
2807
2808 if (!status)
2809 goto out_unlock;
2810
2811 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2812 status->wakeup_reasons);
2813
2814 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, mvm_link->ap_sta_id);
2815 if (!mvm_ap_sta)
2816 goto out_unlock;
2817
2818 /* firmware stores last-used value, we store next value */
2819 if (wowlan_info_ver >= 5) {
2820 mvm_ap_sta->tid_data[status->tid_offloaded_tx].seq_number =
2821 status->qos_seq_ctr[status->tid_offloaded_tx] + 0x10;
2822 } else {
2823 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2824 mvm_ap_sta->tid_data[i].seq_number =
2825 status->qos_seq_ctr[i] + 0x10;
2826 }
2827
2828 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2829 i = mvm->offload_tid;
2830 iwl_trans_set_q_ptrs(mvm->trans,
2831 mvm_ap_sta->tid_data[i].txq_id,
2832 mvm_ap_sta->tid_data[i].seq_number >> 4);
2833 }
2834
2835 iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2836
2837 keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2838 out_unlock:
2839 mutex_unlock(&mvm->mutex);
2840 return keep;
2841 }
2842
2843 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2844 IWL_SCAN_MAX_PROFILES)
2845
2846 struct iwl_mvm_nd_results {
2847 u32 matched_profiles;
2848 u8 matches[ND_QUERY_BUF_LEN];
2849 };
2850
2851 static int
iwl_mvm_netdetect_query_results(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results)2852 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2853 struct iwl_mvm_nd_results *results)
2854 {
2855 struct iwl_scan_offload_match_info *query;
2856 struct iwl_host_cmd cmd = {
2857 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2858 .flags = CMD_WANT_SKB,
2859 };
2860 int ret, len;
2861 size_t query_len, matches_len;
2862 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2863
2864 ret = iwl_mvm_send_cmd(mvm, &cmd);
2865 if (ret) {
2866 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2867 return ret;
2868 }
2869
2870 if (fw_has_api(&mvm->fw->ucode_capa,
2871 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2872 query_len = sizeof(struct iwl_scan_offload_match_info);
2873 matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2874 max_profiles;
2875 } else {
2876 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2877 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2878 max_profiles;
2879 }
2880
2881 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2882 if (len < query_len) {
2883 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2884 ret = -EIO;
2885 goto out_free_resp;
2886 }
2887
2888 query = (void *)cmd.resp_pkt->data;
2889
2890 results->matched_profiles = le32_to_cpu(query->matched_profiles);
2891 memcpy(results->matches, query->matches, matches_len);
2892
2893 #ifdef CONFIG_IWLWIFI_DEBUGFS
2894 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2895 #endif
2896
2897 out_free_resp:
2898 iwl_free_resp(&cmd);
2899 return ret;
2900 }
2901
iwl_mvm_query_num_match_chans(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,int idx)2902 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2903 struct iwl_mvm_nd_results *results,
2904 int idx)
2905 {
2906 int n_chans = 0, i;
2907
2908 if (fw_has_api(&mvm->fw->ucode_capa,
2909 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2910 struct iwl_scan_offload_profile_match *matches =
2911 (void *)results->matches;
2912
2913 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2914 n_chans += hweight8(matches[idx].matching_channels[i]);
2915 } else {
2916 struct iwl_scan_offload_profile_match_v1 *matches =
2917 (void *)results->matches;
2918
2919 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2920 n_chans += hweight8(matches[idx].matching_channels[i]);
2921 }
2922
2923 return n_chans;
2924 }
2925
iwl_mvm_query_set_freqs(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,struct cfg80211_wowlan_nd_match * match,int idx)2926 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2927 struct iwl_mvm_nd_results *results,
2928 struct cfg80211_wowlan_nd_match *match,
2929 int idx)
2930 {
2931 int i;
2932 int n_channels = 0;
2933
2934 if (fw_has_api(&mvm->fw->ucode_capa,
2935 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2936 struct iwl_scan_offload_profile_match *matches =
2937 (void *)results->matches;
2938
2939 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2940 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2941 match->channels[n_channels++] =
2942 mvm->nd_channels[i]->center_freq;
2943 } else {
2944 struct iwl_scan_offload_profile_match_v1 *matches =
2945 (void *)results->matches;
2946
2947 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2948 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2949 match->channels[n_channels++] =
2950 mvm->nd_channels[i]->center_freq;
2951 }
2952 /* We may have ended up with fewer channels than we allocated. */
2953 match->n_channels = n_channels;
2954 }
2955
2956 /**
2957 * enum iwl_d3_notif - d3 notifications
2958 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2959 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2960 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2961 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2962 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2963 */
2964 enum iwl_d3_notif {
2965 IWL_D3_NOTIF_WOWLAN_INFO = BIT(0),
2966 IWL_D3_NOTIF_WOWLAN_WAKE_PKT = BIT(1),
2967 IWL_D3_NOTIF_PROT_OFFLOAD = BIT(2),
2968 IWL_D3_ND_MATCH_INFO = BIT(3),
2969 IWL_D3_NOTIF_D3_END_NOTIF = BIT(4)
2970 };
2971
2972 /* manage d3 resume data */
2973 struct iwl_d3_data {
2974 struct iwl_wowlan_status_data *status;
2975 bool test;
2976 u32 d3_end_flags;
2977 u32 notif_expected; /* bitmap - see &enum iwl_d3_notif */
2978 u32 notif_received; /* bitmap - see &enum iwl_d3_notif */
2979 struct iwl_mvm_nd_results *nd_results;
2980 bool nd_results_valid;
2981 };
2982
iwl_mvm_query_netdetect_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2983 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2984 struct ieee80211_vif *vif,
2985 struct iwl_d3_data *d3_data)
2986 {
2987 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2988 struct cfg80211_wowlan_wakeup wakeup = {
2989 .pattern_idx = -1,
2990 };
2991 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2992 unsigned long matched_profiles;
2993 u32 reasons = 0;
2994 int i, n_matches, ret;
2995
2996 if (WARN_ON(!d3_data || !d3_data->status))
2997 goto out;
2998
2999 reasons = d3_data->status->wakeup_reasons;
3000
3001 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
3002 wakeup.rfkill_release = true;
3003
3004 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
3005 goto out;
3006
3007 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3008 WOWLAN_INFO_NOTIFICATION, 0)) {
3009 IWL_INFO(mvm, "Query FW for ND results\n");
3010 ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
3011
3012 } else {
3013 IWL_INFO(mvm, "Notification based ND results\n");
3014 ret = d3_data->nd_results_valid ? 0 : -1;
3015 }
3016
3017 if (ret || !d3_data->nd_results->matched_profiles) {
3018 wakeup_report = NULL;
3019 goto out;
3020 }
3021
3022 matched_profiles = d3_data->nd_results->matched_profiles;
3023 if (mvm->n_nd_match_sets) {
3024 n_matches = hweight_long(matched_profiles);
3025 } else {
3026 IWL_ERR(mvm, "no net detect match information available\n");
3027 n_matches = 0;
3028 }
3029
3030 net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
3031 GFP_KERNEL);
3032 if (!net_detect || !n_matches)
3033 goto out_report_nd;
3034 net_detect->n_matches = n_matches;
3035 n_matches = 0;
3036
3037 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
3038 struct cfg80211_wowlan_nd_match *match;
3039 int idx, n_channels = 0;
3040
3041 n_channels = iwl_mvm_query_num_match_chans(mvm,
3042 d3_data->nd_results,
3043 i);
3044
3045 match = kzalloc(struct_size(match, channels, n_channels),
3046 GFP_KERNEL);
3047 if (!match)
3048 goto out_report_nd;
3049 match->n_channels = n_channels;
3050
3051 net_detect->matches[n_matches++] = match;
3052
3053 /* We inverted the order of the SSIDs in the scan
3054 * request, so invert the index here.
3055 */
3056 idx = mvm->n_nd_match_sets - i - 1;
3057 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
3058 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
3059 match->ssid.ssid_len);
3060
3061 if (mvm->n_nd_channels < n_channels)
3062 continue;
3063
3064 iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
3065 }
3066 /* We may have fewer matches than we allocated. */
3067 net_detect->n_matches = n_matches;
3068
3069 out_report_nd:
3070 wakeup.net_detect = net_detect;
3071 out:
3072 iwl_mvm_free_nd(mvm);
3073
3074 mutex_unlock(&mvm->mutex);
3075 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
3076
3077 if (net_detect) {
3078 for (i = 0; i < net_detect->n_matches; i++)
3079 kfree(net_detect->matches[i]);
3080 kfree(net_detect);
3081 }
3082 }
3083
iwl_mvm_d3_disconnect_iter(void * data,u8 * mac,struct ieee80211_vif * vif)3084 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
3085 struct ieee80211_vif *vif)
3086 {
3087 /* skip the one we keep connection on */
3088 if (data == vif)
3089 return;
3090
3091 if (vif->type == NL80211_IFTYPE_STATION)
3092 ieee80211_resume_disconnect(vif);
3093 }
3094
3095 enum rt_status {
3096 FW_ALIVE,
3097 FW_NEEDS_RESET,
3098 FW_ERROR,
3099 };
3100
iwl_mvm_check_rt_status(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3101 static enum rt_status iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
3102 struct ieee80211_vif *vif)
3103 {
3104 u32 err_id;
3105
3106 /* check for lmac1 error */
3107 if (iwl_fwrt_read_err_table(mvm->trans,
3108 mvm->trans->dbg.lmac_error_event_table[0],
3109 &err_id)) {
3110 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
3111 IWL_WARN(mvm, "Rfkill was toggled during suspend\n");
3112 if (vif) {
3113 struct cfg80211_wowlan_wakeup wakeup = {
3114 .rfkill_release = true,
3115 };
3116
3117 ieee80211_report_wowlan_wakeup(vif, &wakeup,
3118 GFP_KERNEL);
3119 }
3120
3121 return FW_NEEDS_RESET;
3122 }
3123 return FW_ERROR;
3124 }
3125
3126 /* check if we have lmac2 set and check for error */
3127 if (iwl_fwrt_read_err_table(mvm->trans,
3128 mvm->trans->dbg.lmac_error_event_table[1],
3129 NULL))
3130 return FW_ERROR;
3131
3132 /* check for umac error */
3133 if (iwl_fwrt_read_err_table(mvm->trans,
3134 mvm->trans->dbg.umac_error_event_table,
3135 NULL))
3136 return FW_ERROR;
3137
3138 return FW_ALIVE;
3139 }
3140
3141 /*
3142 * This function assumes:
3143 * 1. The mutex is already held.
3144 * 2. The callee functions unlock the mutex.
3145 */
3146 static bool
iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)3147 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
3148 struct ieee80211_vif *vif,
3149 struct iwl_d3_data *d3_data)
3150 {
3151 lockdep_assert_held(&mvm->mutex);
3152
3153 /* if FW uses status notification, status shouldn't be NULL here */
3154 if (!d3_data->status) {
3155 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3156 u8 sta_id = mvm->net_detect ? IWL_INVALID_STA :
3157 mvmvif->deflink.ap_sta_id;
3158
3159 /* bug - FW with MLO has status notification */
3160 WARN_ON(ieee80211_vif_is_mld(vif));
3161
3162 d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
3163 }
3164
3165 if (mvm->net_detect) {
3166 iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
3167 } else {
3168 bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
3169 d3_data->status);
3170
3171 #ifdef CONFIG_IWLWIFI_DEBUGFS
3172 if (keep)
3173 mvm->keep_vif = vif;
3174 #endif
3175
3176 return keep;
3177 }
3178 return false;
3179 }
3180
3181 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
3182 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
3183 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
3184 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
3185 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
3186 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
3187
iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm * mvm,struct iwl_wowlan_wake_pkt_notif * notif,struct iwl_wowlan_status_data * status,u32 len)3188 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
3189 struct iwl_wowlan_wake_pkt_notif *notif,
3190 struct iwl_wowlan_status_data *status,
3191 u32 len)
3192 {
3193 u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
3194
3195 if (len < sizeof(*notif)) {
3196 IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
3197 return -EIO;
3198 }
3199
3200 if (WARN_ON(!status)) {
3201 IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
3202 return -EIO;
3203 }
3204
3205 if (WARN_ON(!(status->wakeup_reasons &
3206 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
3207 IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
3208 status->wakeup_reasons);
3209 return -EIO;
3210 }
3211
3212 data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
3213
3214 /* data_size got the padding from the notification, remove it. */
3215 if (packet_len < data_size)
3216 data_size = packet_len;
3217
3218 status->wake_packet = kmemdup(notif->wake_packet, data_size,
3219 GFP_ATOMIC);
3220
3221 if (!status->wake_packet)
3222 return -ENOMEM;
3223
3224 status->wake_packet_length = packet_len;
3225 status->wake_packet_bufsize = data_size;
3226
3227 return 0;
3228 }
3229
iwl_mvm_nd_match_info_handler(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data,struct iwl_scan_offload_match_info * notif,u32 len)3230 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
3231 struct iwl_d3_data *d3_data,
3232 struct iwl_scan_offload_match_info *notif,
3233 u32 len)
3234 {
3235 struct iwl_wowlan_status_data *status = d3_data->status;
3236 struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
3237 struct iwl_mvm_nd_results *results = d3_data->nd_results;
3238 size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
3239 iwl_umac_scan_get_max_profiles(mvm->fw);
3240
3241 if (IS_ERR_OR_NULL(vif))
3242 return;
3243
3244 if (len < sizeof(struct iwl_scan_offload_match_info)) {
3245 IWL_ERR(mvm, "Invalid scan match info notification\n");
3246 return;
3247 }
3248
3249 if (!mvm->net_detect) {
3250 IWL_ERR(mvm, "Unexpected scan match info notification\n");
3251 return;
3252 }
3253
3254 if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
3255 IWL_ERR(mvm,
3256 "Ignore scan match info notification: no reason\n");
3257 return;
3258 }
3259
3260 #ifdef CONFIG_IWLWIFI_DEBUGFS
3261 mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
3262 #endif
3263
3264 results->matched_profiles = le32_to_cpu(notif->matched_profiles);
3265 IWL_INFO(mvm, "number of matched profiles=%u\n",
3266 results->matched_profiles);
3267
3268 if (results->matched_profiles) {
3269 memcpy(results->matches, notif->matches, matches_len);
3270 d3_data->nd_results_valid = true;
3271 }
3272
3273 /* no scan should be active at this point */
3274 mvm->scan_status = 0;
3275 for (i = 0; i < mvm->max_scans; i++)
3276 mvm->scan_uid_status[i] = 0;
3277 }
3278
iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)3279 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
3280 struct iwl_rx_packet *pkt, void *data)
3281 {
3282 struct iwl_mvm *mvm =
3283 container_of(notif_wait, struct iwl_mvm, notif_wait);
3284 struct iwl_d3_data *d3_data = data;
3285 u32 len = iwl_rx_packet_payload_len(pkt);
3286 int ret;
3287 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
3288 PROT_OFFLOAD_GROUP,
3289 WOWLAN_INFO_NOTIFICATION,
3290 IWL_FW_CMD_VER_UNKNOWN);
3291
3292
3293 switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
3294 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
3295
3296 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
3297 /* We might get two notifications due to dual bss */
3298 IWL_DEBUG_WOWLAN(mvm,
3299 "Got additional wowlan info notification\n");
3300 break;
3301 }
3302
3303 if (wowlan_info_ver < 2) {
3304 struct iwl_wowlan_info_notif_v1 *notif_v1 =
3305 (void *)pkt->data;
3306 struct iwl_wowlan_info_notif_v2 *notif_v2;
3307
3308 notif_v2 = kmemdup(notif_v1, sizeof(*notif_v2), GFP_ATOMIC);
3309
3310 if (!notif_v2)
3311 return false;
3312
3313 notif_v2->tid_tear_down = notif_v1->tid_tear_down;
3314 notif_v2->station_id = notif_v1->station_id;
3315 memset_after(notif_v2, 0, station_id);
3316 iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3317 d3_data->status,
3318 len);
3319 kfree(notif_v2);
3320
3321 } else if (wowlan_info_ver == 2) {
3322 struct iwl_wowlan_info_notif_v2 *notif_v2 =
3323 (void *)pkt->data;
3324
3325 iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3326 d3_data->status,
3327 len);
3328 } else if (wowlan_info_ver < 5) {
3329 struct iwl_wowlan_info_notif_v4 *notif =
3330 (void *)pkt->data;
3331
3332 iwl_mvm_parse_wowlan_info_notif_v4(mvm, notif,
3333 d3_data->status, len,
3334 wowlan_info_ver > 3);
3335 } else {
3336 struct iwl_wowlan_info_notif *notif =
3337 (void *)pkt->data;
3338
3339 iwl_mvm_parse_wowlan_info_notif(mvm, notif,
3340 d3_data->status, len);
3341 }
3342
3343 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
3344
3345 if (d3_data->status &&
3346 d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
3347 /* We are supposed to get also wake packet notif */
3348 d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3349
3350 break;
3351 }
3352 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
3353 struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
3354
3355 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
3356 /* We shouldn't get two wake packet notifications */
3357 IWL_ERR(mvm,
3358 "Got additional wowlan wake packet notification\n");
3359 } else {
3360 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3361 len = iwl_rx_packet_payload_len(pkt);
3362 ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
3363 d3_data->status,
3364 len);
3365 if (ret)
3366 IWL_ERR(mvm,
3367 "Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
3368 }
3369
3370 break;
3371 }
3372 case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
3373 struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
3374
3375 if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
3376 IWL_ERR(mvm,
3377 "Got additional netdetect match info\n");
3378 break;
3379 }
3380
3381 d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
3382
3383 /* explicitly set this in the 'expected' as well */
3384 d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
3385
3386 len = iwl_rx_packet_payload_len(pkt);
3387 iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
3388 break;
3389 }
3390 case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
3391 struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
3392
3393 d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
3394 d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
3395
3396 break;
3397 }
3398 default:
3399 WARN_ON(1);
3400 }
3401
3402 return d3_data->notif_received == d3_data->notif_expected;
3403 }
3404
iwl_mvm_resume_firmware(struct iwl_mvm * mvm,bool test)3405 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3406 {
3407 int ret;
3408 enum iwl_d3_status d3_status;
3409 struct iwl_host_cmd cmd = {
3410 .id = D0I3_END_CMD,
3411 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
3412 };
3413 bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3414 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3415
3416 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3417 if (ret)
3418 return ret;
3419
3420 if (d3_status != IWL_D3_STATUS_ALIVE) {
3421 IWL_INFO(mvm, "Device was reset during suspend\n");
3422 return -ENOENT;
3423 }
3424
3425 /*
3426 * We should trigger resume flow using command only for 22000 family
3427 * AX210 and above don't need the command since they have
3428 * the doorbell interrupt.
3429 */
3430 if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3431 fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3432 ret = iwl_mvm_send_cmd(mvm, &cmd);
3433 if (ret < 0)
3434 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3435 ret);
3436 }
3437
3438 return ret;
3439 }
3440
3441 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 3)
3442
iwl_mvm_d3_notif_wait(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data)3443 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3444 struct iwl_d3_data *d3_data)
3445 {
3446 static const u16 d3_resume_notif[] = {
3447 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3448 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3449 WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3450 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3451 };
3452 static const u16 d3_fast_resume_notif[] = {
3453 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3454 };
3455 struct iwl_notification_wait wait_d3_notif;
3456 int ret;
3457
3458 if (mvm->fast_resume)
3459 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3460 d3_fast_resume_notif,
3461 ARRAY_SIZE(d3_fast_resume_notif),
3462 iwl_mvm_wait_d3_notif, d3_data);
3463 else
3464 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3465 d3_resume_notif,
3466 ARRAY_SIZE(d3_resume_notif),
3467 iwl_mvm_wait_d3_notif, d3_data);
3468
3469 ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3470 if (ret) {
3471 iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3472 return ret;
3473 }
3474
3475 return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3476 IWL_MVM_D3_NOTIF_TIMEOUT);
3477 }
3478
iwl_mvm_d3_resume_notif_based(struct iwl_mvm * mvm)3479 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3480 {
3481 return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3482 WOWLAN_INFO_NOTIFICATION, 0) &&
3483 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3484 WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3485 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3486 D3_END_NOTIFICATION, 0);
3487 }
3488
__iwl_mvm_resume(struct iwl_mvm * mvm,bool test)3489 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3490 {
3491 struct ieee80211_vif *vif = NULL;
3492 int ret = 1;
3493 struct iwl_mvm_nd_results results = {};
3494 struct iwl_d3_data d3_data = {
3495 .test = test,
3496 .notif_expected =
3497 IWL_D3_NOTIF_WOWLAN_INFO |
3498 IWL_D3_NOTIF_D3_END_NOTIF,
3499 .nd_results_valid = false,
3500 .nd_results = &results,
3501 };
3502 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3503 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3504 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3505 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3506 bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3507 enum rt_status rt_status;
3508 bool keep = false;
3509
3510 mutex_lock(&mvm->mutex);
3511
3512 /* Apparently, the device went away and device_powered_off() was called,
3513 * don't even try to read the rt_status, the device is currently
3514 * inaccessible.
3515 */
3516 if (!test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
3517 IWL_INFO(mvm,
3518 "Can't resume, device_powered_off() was called during wowlan\n");
3519 goto err;
3520 }
3521
3522 mvm->last_reset_or_resume_time_jiffies = jiffies;
3523
3524 /* get the BSS vif pointer again */
3525 vif = iwl_mvm_get_bss_vif(mvm);
3526 if (IS_ERR_OR_NULL(vif))
3527 goto err;
3528
3529 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3530
3531 rt_status = iwl_mvm_check_rt_status(mvm, vif);
3532 if (rt_status != FW_ALIVE) {
3533 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3534 if (rt_status == FW_ERROR) {
3535 IWL_ERR(mvm, "FW Error occurred during suspend. Restarting.\n");
3536 iwl_mvm_dump_nic_error_log(mvm);
3537 iwl_dbg_tlv_time_point(&mvm->fwrt,
3538 IWL_FW_INI_TIME_POINT_FW_ASSERT,
3539 NULL);
3540 iwl_fw_dbg_collect_desc(&mvm->fwrt,
3541 &iwl_dump_desc_assert,
3542 false, 0);
3543 }
3544 ret = 1;
3545 goto err;
3546 }
3547
3548 if (resume_notif_based) {
3549 d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3550 if (!d3_data.status) {
3551 IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3552 ret = -ENOMEM;
3553 goto err;
3554 }
3555
3556 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3557 if (ret)
3558 goto err;
3559 } else {
3560 ret = iwl_mvm_resume_firmware(mvm, test);
3561 if (ret < 0)
3562 goto err;
3563 }
3564
3565 iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
3566
3567 /* after the successful handshake, we're out of D3 */
3568 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3569
3570 /* when reset is required we can't send these following commands */
3571 if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3572 goto query_wakeup_reasons;
3573
3574 /*
3575 * Query the current location and source from the D3 firmware so we
3576 * can play it back when we re-intiailize the D0 firmware
3577 */
3578 iwl_mvm_update_changed_regdom(mvm);
3579
3580 /* Re-configure PPAG settings */
3581 iwl_mvm_ppag_send_cmd(mvm);
3582
3583 if (!unified_image)
3584 /* Re-configure default SAR profile */
3585 iwl_mvm_sar_select_profile(mvm, 1, 1);
3586
3587 if (mvm->net_detect && unified_image) {
3588 /* If this is a non-unified image, we restart the FW,
3589 * so no need to stop the netdetect scan. If that
3590 * fails, continue and try to get the wake-up reasons,
3591 * but trigger a HW restart by keeping a failure code
3592 * in ret.
3593 */
3594 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3595 false);
3596 }
3597
3598 query_wakeup_reasons:
3599 keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3600 /* has unlocked the mutex, so skip that */
3601 goto out;
3602
3603 err:
3604 mutex_unlock(&mvm->mutex);
3605 out:
3606 if (d3_data.status)
3607 kfree(d3_data.status->wake_packet);
3608 kfree(d3_data.status);
3609 iwl_mvm_free_nd(mvm);
3610
3611 if (!d3_data.test && !mvm->net_detect)
3612 ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3613 IEEE80211_IFACE_ITER_NORMAL,
3614 iwl_mvm_d3_disconnect_iter,
3615 keep ? vif : NULL);
3616
3617 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3618
3619 /* no need to reset the device in unified images, if successful */
3620 if (unified_image && !ret) {
3621 /* nothing else to do if we already sent D0I3_END_CMD */
3622 if (d0i3_first)
3623 return 0;
3624
3625 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3626 D3_END_NOTIFICATION, 0)) {
3627 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3628 if (!ret)
3629 return 0;
3630 } else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3631 return 0;
3632 }
3633 }
3634
3635 /*
3636 * Reconfigure the device in one of the following cases:
3637 * 1. We are not using a unified image
3638 * 2. We are using a unified image but had an error while exiting D3
3639 */
3640 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3641
3642 /* regardless of what happened, we're now out of D3 */
3643 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3644
3645 return 1;
3646 }
3647
iwl_mvm_resume(struct ieee80211_hw * hw)3648 int iwl_mvm_resume(struct ieee80211_hw *hw)
3649 {
3650 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3651 int ret;
3652
3653 ret = __iwl_mvm_resume(mvm, false);
3654
3655 iwl_mvm_resume_tcm(mvm);
3656
3657 iwl_fw_runtime_resume(&mvm->fwrt);
3658
3659 return ret;
3660 }
3661
iwl_mvm_set_wakeup(struct ieee80211_hw * hw,bool enabled)3662 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3663 {
3664 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3665
3666 device_set_wakeup_enable(mvm->trans->dev, enabled);
3667 }
3668
iwl_mvm_fast_suspend(struct iwl_mvm * mvm)3669 void iwl_mvm_fast_suspend(struct iwl_mvm *mvm)
3670 {
3671 struct iwl_d3_manager_config d3_cfg_cmd_data = {};
3672 int ret;
3673
3674 lockdep_assert_held(&mvm->mutex);
3675
3676 IWL_DEBUG_WOWLAN(mvm, "Starting fast suspend flow\n");
3677
3678 mvm->fast_resume = true;
3679 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3680
3681 WARN_ON(iwl_mvm_power_update_device(mvm));
3682 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
3683 ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, CMD_SEND_IN_D3,
3684 sizeof(d3_cfg_cmd_data), &d3_cfg_cmd_data);
3685 if (ret)
3686 IWL_ERR(mvm,
3687 "fast suspend: couldn't send D3_CONFIG_CMD %d\n", ret);
3688
3689 ret = iwl_trans_d3_suspend(mvm->trans, false, false);
3690 if (ret)
3691 IWL_ERR(mvm, "fast suspend: trans_d3_suspend failed %d\n", ret);
3692 }
3693
iwl_mvm_fast_resume(struct iwl_mvm * mvm)3694 int iwl_mvm_fast_resume(struct iwl_mvm *mvm)
3695 {
3696 struct iwl_d3_data d3_data = {
3697 .notif_expected =
3698 IWL_D3_NOTIF_D3_END_NOTIF,
3699 };
3700 enum rt_status rt_status;
3701 int ret;
3702
3703 lockdep_assert_held(&mvm->mutex);
3704
3705 IWL_DEBUG_WOWLAN(mvm, "Starting the fast resume flow\n");
3706
3707 mvm->last_reset_or_resume_time_jiffies = jiffies;
3708 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3709
3710 rt_status = iwl_mvm_check_rt_status(mvm, NULL);
3711 if (rt_status != FW_ALIVE) {
3712 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3713 if (rt_status == FW_ERROR) {
3714 IWL_ERR(mvm,
3715 "iwl_mvm_check_rt_status failed, device is gone during suspend\n");
3716 iwl_mvm_dump_nic_error_log(mvm);
3717 iwl_dbg_tlv_time_point(&mvm->fwrt,
3718 IWL_FW_INI_TIME_POINT_FW_ASSERT,
3719 NULL);
3720 iwl_fw_dbg_collect_desc(&mvm->fwrt,
3721 &iwl_dump_desc_assert,
3722 false, 0);
3723 }
3724 mvm->trans->state = IWL_TRANS_NO_FW;
3725 ret = -ENODEV;
3726
3727 goto out;
3728 }
3729 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3730
3731 if (ret) {
3732 IWL_ERR(mvm, "Couldn't get the d3 notif %d\n", ret);
3733 mvm->trans->state = IWL_TRANS_NO_FW;
3734 }
3735
3736 out:
3737 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3738 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3739 mvm->fast_resume = false;
3740
3741 return ret;
3742 }
3743
3744 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_d3_test_open(struct inode * inode,struct file * file)3745 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3746 {
3747 struct iwl_mvm *mvm = inode->i_private;
3748 int err;
3749
3750 if (mvm->d3_test_active)
3751 return -EBUSY;
3752
3753 file->private_data = inode->i_private;
3754
3755 iwl_mvm_pause_tcm(mvm, true);
3756
3757 iwl_fw_runtime_suspend(&mvm->fwrt);
3758
3759 /* start pseudo D3 */
3760 rtnl_lock();
3761 wiphy_lock(mvm->hw->wiphy);
3762 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3763 wiphy_unlock(mvm->hw->wiphy);
3764 rtnl_unlock();
3765 if (err > 0)
3766 err = -EINVAL;
3767 if (err)
3768 return err;
3769
3770 mvm->d3_test_active = true;
3771 mvm->keep_vif = NULL;
3772 return 0;
3773 }
3774
iwl_mvm_d3_test_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3775 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3776 size_t count, loff_t *ppos)
3777 {
3778 struct iwl_mvm *mvm = file->private_data;
3779 unsigned long end = jiffies + 60 * HZ;
3780 u32 pme_asserted;
3781
3782 while (true) {
3783 /* read pme_ptr if available */
3784 if (mvm->d3_test_pme_ptr) {
3785 pme_asserted = iwl_trans_read_mem32(mvm->trans,
3786 mvm->d3_test_pme_ptr);
3787 if (pme_asserted)
3788 break;
3789 }
3790
3791 if (msleep_interruptible(100))
3792 break;
3793
3794 if (time_is_before_jiffies(end)) {
3795 IWL_ERR(mvm,
3796 "ending pseudo-D3 with timeout after ~60 seconds\n");
3797 return -ETIMEDOUT;
3798 }
3799 }
3800
3801 return 0;
3802 }
3803
iwl_mvm_d3_test_disconn_work_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)3804 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3805 struct ieee80211_vif *vif)
3806 {
3807 /* skip the one we keep connection on */
3808 if (_data == vif)
3809 return;
3810
3811 if (vif->type == NL80211_IFTYPE_STATION)
3812 ieee80211_connection_loss(vif);
3813 }
3814
iwl_mvm_d3_test_release(struct inode * inode,struct file * file)3815 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3816 {
3817 struct iwl_mvm *mvm = inode->i_private;
3818 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3819 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3820
3821 mvm->d3_test_active = false;
3822
3823 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3824
3825 rtnl_lock();
3826 wiphy_lock(mvm->hw->wiphy);
3827 __iwl_mvm_resume(mvm, true);
3828 wiphy_unlock(mvm->hw->wiphy);
3829 rtnl_unlock();
3830
3831 iwl_mvm_resume_tcm(mvm);
3832
3833 iwl_fw_runtime_resume(&mvm->fwrt);
3834
3835 iwl_abort_notification_waits(&mvm->notif_wait);
3836 if (!unified_image) {
3837 int remaining_time = 10;
3838
3839 ieee80211_restart_hw(mvm->hw);
3840
3841 /* wait for restart and disconnect all interfaces */
3842 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3843 remaining_time > 0) {
3844 remaining_time--;
3845 msleep(1000);
3846 }
3847
3848 if (remaining_time == 0)
3849 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3850 }
3851
3852 ieee80211_iterate_active_interfaces_atomic(
3853 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3854 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3855
3856 return 0;
3857 }
3858
3859 const struct file_operations iwl_dbgfs_d3_test_ops = {
3860 .open = iwl_mvm_d3_test_open,
3861 .read = iwl_mvm_d3_test_read,
3862 .release = iwl_mvm_d3_test_release,
3863 };
3864 #endif
3865