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