1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2025 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 (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
936 /* Query the last used seqno and set it */
937 int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
938
939 if (ret < 0)
940 return ret;
941
942 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
943 }
944
945 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 7)
946 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
947
948 if (wowlan->disconnect)
949 wowlan_config_cmd->wakeup_filter |=
950 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
951 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
952 if (wowlan->magic_pkt)
953 wowlan_config_cmd->wakeup_filter |=
954 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
955 if (wowlan->gtk_rekey_failure)
956 wowlan_config_cmd->wakeup_filter |=
957 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
958 if (wowlan->eap_identity_req)
959 wowlan_config_cmd->wakeup_filter |=
960 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
961 if (wowlan->four_way_handshake)
962 wowlan_config_cmd->wakeup_filter |=
963 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
964 if (wowlan->n_patterns)
965 wowlan_config_cmd->wakeup_filter |=
966 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
967
968 if (wowlan->rfkill_release)
969 wowlan_config_cmd->wakeup_filter |=
970 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
971
972 if (wowlan->tcp) {
973 /*
974 * Set the "link change" (really "link lost") flag as well
975 * since that implies losing the TCP connection.
976 */
977 wowlan_config_cmd->wakeup_filter |=
978 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
979 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
980 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
981 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
982 }
983
984 if (wowlan->any) {
985 wowlan_config_cmd->wakeup_filter |=
986 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
987 IWL_WOWLAN_WAKEUP_LINK_CHANGE |
988 IWL_WOWLAN_WAKEUP_RX_FRAME |
989 IWL_WOWLAN_WAKEUP_BCN_FILTERING);
990 }
991
992 return 0;
993 }
994
iwl_mvm_wowlan_config_key_params(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)995 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
996 struct ieee80211_vif *vif,
997 struct iwl_mvm_vif_link_info *mvm_link)
998 {
999 bool unified = fw_has_capa(&mvm->fw->ucode_capa,
1000 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1001 struct wowlan_key_reprogram_data key_data = {};
1002 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1003 int ret;
1004 u8 cmd_ver;
1005 size_t cmd_size;
1006
1007 if (!unified) {
1008 /*
1009 * if we have to configure keys, call ieee80211_iter_keys(),
1010 * as we need non-atomic context in order to take the
1011 * required locks.
1012 */
1013 /*
1014 * Note that currently we don't use CMD_ASYNC in the iterator.
1015 * In case of key_data.configure_keys, all the configured
1016 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1017 * take care of locking/unlocking mvm->mutex.
1018 */
1019 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1020 &key_data);
1021
1022 if (key_data.error)
1023 return -EIO;
1024 }
1025
1026 ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif, mvm_link);
1027 if (ret)
1028 return ret;
1029
1030 if (!fw_has_api(&mvm->fw->ucode_capa,
1031 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1032 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1033 IWL_FW_CMD_VER_UNKNOWN);
1034 struct wowlan_key_tkip_data tkip_data = {};
1035 int size;
1036
1037 if (ver == 2) {
1038 size = sizeof(tkip_data.tkip);
1039 tkip_data.tkip.sta_id =
1040 cpu_to_le32(mvm_link->ap_sta_id);
1041 } else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1042 size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1043 } else {
1044 WARN_ON_ONCE(1);
1045 return -EINVAL;
1046 }
1047
1048 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1049 &tkip_data);
1050
1051 if (tkip_data.have_tkip_keys) {
1052 /* send relevant data according to CMD version */
1053 ret = iwl_mvm_send_cmd_pdu(mvm,
1054 WOWLAN_TKIP_PARAM,
1055 CMD_ASYNC, size,
1056 &tkip_data.tkip);
1057 if (ret)
1058 return ret;
1059 }
1060 }
1061
1062 /* configure rekey data only if offloaded rekey is supported (d3) */
1063 if (mvmvif->rekey_data.valid) {
1064 struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1065 struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1066 &kek_kck_cmd;
1067 struct wowlan_key_gtk_type_iter gtk_type_data = {
1068 .kek_kck_cmd = _kek_kck_cmd,
1069 };
1070
1071 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1072 WOWLAN_KEK_KCK_MATERIAL,
1073 IWL_FW_CMD_VER_UNKNOWN);
1074 if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1075 cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1076 return -EINVAL;
1077
1078 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1079 >k_type_data);
1080
1081 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1082 mvmvif->rekey_data.kck_len);
1083 kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1084 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1085 mvmvif->rekey_data.kek_len);
1086 kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1087 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1088 kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1089 kek_kck_cmd.sta_id = cpu_to_le32(mvm_link->ap_sta_id);
1090
1091 if (cmd_ver == 4) {
1092 cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1093 } else {
1094 if (cmd_ver == 3)
1095 cmd_size =
1096 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1097 else
1098 cmd_size =
1099 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1100 /* skip the sta_id at the beginning */
1101 _kek_kck_cmd = (void *)
1102 ((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1103 }
1104
1105 IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1106 mvmvif->rekey_data.akm);
1107
1108 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1109 CMD_ASYNC, cmd_size, _kek_kck_cmd);
1110 if (ret)
1111 return ret;
1112 }
1113
1114 return 0;
1115 }
1116
1117 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)1118 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1119 struct cfg80211_wowlan *wowlan,
1120 struct iwl_wowlan_config_cmd_v6 *wowlan_config_cmd_v6,
1121 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1122 struct iwl_mvm_vif_link_info *mvm_link,
1123 struct ieee80211_sta *ap_sta)
1124 {
1125 int ret;
1126 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1127 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1128
1129 mvm->offload_tid = wowlan_config_cmd_v6->offloading_tid;
1130
1131 if (!unified_image) {
1132 ret = iwl_mvm_switch_to_d3(mvm);
1133 if (ret)
1134 return ret;
1135
1136 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1137 if (ret)
1138 return ret;
1139 }
1140
1141 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, mvm_link);
1142 if (ret)
1143 return ret;
1144
1145 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) > 6) {
1146 struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1147 .wakeup_filter = wowlan_config_cmd_v6->wakeup_filter,
1148 .wowlan_ba_teardown_tids =
1149 wowlan_config_cmd_v6->wowlan_ba_teardown_tids,
1150 .is_11n_connection =
1151 wowlan_config_cmd_v6->is_11n_connection,
1152 .offloading_tid = wowlan_config_cmd_v6->offloading_tid,
1153 .flags = wowlan_config_cmd_v6->flags,
1154 .sta_id = wowlan_config_cmd_v6->sta_id,
1155 };
1156
1157 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1158 sizeof(wowlan_config_cmd),
1159 &wowlan_config_cmd);
1160 } else {
1161 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1162 sizeof(*wowlan_config_cmd_v6),
1163 wowlan_config_cmd_v6);
1164 }
1165 if (ret)
1166 return ret;
1167
1168 if (fw_has_api(&mvm->fw->ucode_capa,
1169 IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1170 ret = iwl_mvm_send_patterns(mvm, mvm_link, wowlan);
1171 else
1172 ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1173 if (ret)
1174 return ret;
1175
1176 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0,
1177 mvm_link->ap_sta_id);
1178 }
1179
1180 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)1181 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1182 struct cfg80211_wowlan *wowlan,
1183 struct cfg80211_sched_scan_request *nd_config,
1184 struct ieee80211_vif *vif)
1185 {
1186 int ret;
1187 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1188 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1189
1190 if (!unified_image) {
1191 ret = iwl_mvm_switch_to_d3(mvm);
1192 if (ret)
1193 return ret;
1194 } else {
1195 /* In theory, we wouldn't have to stop a running sched
1196 * scan in order to start another one (for
1197 * net-detect). But in practice this doesn't seem to
1198 * work properly, so stop any running sched_scan now.
1199 */
1200 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1201 if (ret)
1202 return ret;
1203 }
1204
1205 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1206 IWL_MVM_SCAN_NETDETECT);
1207 if (ret)
1208 return ret;
1209
1210 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1211 return -EBUSY;
1212
1213 /* save the sched scan matchsets... */
1214 if (nd_config->n_match_sets) {
1215 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1216 sizeof(*nd_config->match_sets) *
1217 nd_config->n_match_sets,
1218 GFP_KERNEL);
1219 if (mvm->nd_match_sets)
1220 mvm->n_nd_match_sets = nd_config->n_match_sets;
1221 }
1222
1223 /* ...and the sched scan channels for later reporting */
1224 mvm->nd_channels = kmemdup(nd_config->channels,
1225 sizeof(*nd_config->channels) *
1226 nd_config->n_channels,
1227 GFP_KERNEL);
1228 if (mvm->nd_channels)
1229 mvm->n_nd_channels = nd_config->n_channels;
1230
1231 return 0;
1232 }
1233
iwl_mvm_free_nd(struct iwl_mvm * mvm)1234 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1235 {
1236 kfree(mvm->nd_match_sets);
1237 mvm->nd_match_sets = NULL;
1238 mvm->n_nd_match_sets = 0;
1239 kfree(mvm->nd_channels);
1240 mvm->nd_channels = NULL;
1241 mvm->n_nd_channels = 0;
1242 }
1243
__iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan,bool test)1244 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1245 struct cfg80211_wowlan *wowlan,
1246 bool test)
1247 {
1248 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1249 struct ieee80211_vif *vif = NULL;
1250 struct iwl_mvm_vif *mvmvif = NULL;
1251 struct ieee80211_sta *ap_sta = NULL;
1252 struct iwl_mvm_vif_link_info *mvm_link;
1253 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1254 /*
1255 * Program the minimum sleep time to 10 seconds, as many
1256 * platforms have issues processing a wakeup signal while
1257 * still being in the process of suspending.
1258 */
1259 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1260 };
1261 struct iwl_host_cmd d3_cfg_cmd = {
1262 .id = D3_CONFIG_CMD,
1263 .flags = CMD_WANT_SKB,
1264 .data[0] = &d3_cfg_cmd_data,
1265 .len[0] = sizeof(d3_cfg_cmd_data),
1266 };
1267 int ret;
1268 int len __maybe_unused;
1269 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1270 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1271
1272 if (!wowlan) {
1273 /*
1274 * mac80211 shouldn't get here, but for D3 test
1275 * it doesn't warrant a warning
1276 */
1277 WARN_ON(!test);
1278 return -EINVAL;
1279 }
1280
1281 vif = iwl_mvm_get_bss_vif(mvm);
1282 if (IS_ERR_OR_NULL(vif))
1283 return 1;
1284
1285 ret = iwl_mvm_block_esr_sync(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
1286 if (ret)
1287 return ret;
1288
1289 mutex_lock(&mvm->mutex);
1290
1291 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1292
1293 synchronize_net();
1294
1295 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1296
1297 mvm_link = mvmvif->link[iwl_mvm_get_primary_link(vif)];
1298 if (WARN_ON_ONCE(!mvm_link)) {
1299 ret = -EINVAL;
1300 goto out_noreset;
1301 }
1302
1303 if (mvm_link->ap_sta_id == IWL_INVALID_STA) {
1304 /* if we're not associated, this must be netdetect */
1305 if (!wowlan->nd_config) {
1306 ret = 1;
1307 goto out_noreset;
1308 }
1309
1310 ret = iwl_mvm_netdetect_config(
1311 mvm, wowlan, wowlan->nd_config, vif);
1312 if (ret)
1313 goto out;
1314
1315 mvm->net_detect = true;
1316 } else {
1317 struct iwl_wowlan_config_cmd_v6 wowlan_config_cmd = {
1318 .offloading_tid = 0,
1319 };
1320
1321 wowlan_config_cmd.sta_id = mvm_link->ap_sta_id;
1322
1323 ap_sta = rcu_dereference_protected(
1324 mvm->fw_id_to_mac_id[mvm_link->ap_sta_id],
1325 lockdep_is_held(&mvm->mutex));
1326 if (IS_ERR_OR_NULL(ap_sta)) {
1327 ret = -EINVAL;
1328 goto out_noreset;
1329 }
1330
1331 ret = iwl_mvm_sta_ensure_queue(
1332 mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1333 if (ret)
1334 goto out_noreset;
1335
1336 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1337 vif, mvmvif, ap_sta);
1338 if (ret)
1339 goto out_noreset;
1340 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1341 vif, mvmvif, mvm_link, ap_sta);
1342 if (ret)
1343 goto out;
1344
1345 mvm->net_detect = false;
1346 }
1347
1348 ret = iwl_mvm_power_update_device(mvm);
1349 if (ret)
1350 goto out;
1351
1352 ret = iwl_mvm_power_update_mac(mvm);
1353 if (ret)
1354 goto out;
1355
1356 #ifdef CONFIG_IWLWIFI_DEBUGFS
1357 if (mvm->d3_wake_sysassert)
1358 d3_cfg_cmd_data.wakeup_flags |=
1359 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1360 #endif
1361
1362 /*
1363 * Prior to 9000 device family the driver needs to stop the dbg
1364 * recording before entering D3. In later devices the FW stops the
1365 * recording automatically.
1366 */
1367 if (mvm->trans->mac_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1368 iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1369
1370 /* must be last -- this switches firmware state */
1371 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1372 if (ret)
1373 goto out;
1374 #ifdef CONFIG_IWLWIFI_DEBUGFS
1375 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1376 if (len >= sizeof(u32)) {
1377 mvm->d3_test_pme_ptr =
1378 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1379 }
1380 #endif
1381 iwl_free_resp(&d3_cfg_cmd);
1382
1383 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1384
1385 ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1386 out:
1387 if (ret < 0) {
1388 iwl_mvm_free_nd(mvm);
1389
1390 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1391 }
1392 out_noreset:
1393 mutex_unlock(&mvm->mutex);
1394
1395 return ret;
1396 }
1397
iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1398 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1399 {
1400 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1401
1402 iwl_mvm_pause_tcm(mvm, true);
1403
1404 mutex_lock(&mvm->mutex);
1405 iwl_fw_runtime_suspend(&mvm->fwrt);
1406 mutex_unlock(&mvm->mutex);
1407
1408 return __iwl_mvm_suspend(hw, wowlan, false);
1409 }
1410
1411 struct iwl_multicast_key_data {
1412 u8 key[WOWLAN_KEY_MAX_SIZE];
1413 u8 len;
1414 u8 flags;
1415 u8 id;
1416 u8 ipn[6];
1417 };
1418
1419 /* converted data from the different status responses */
1420 struct iwl_wowlan_status_data {
1421 u64 replay_ctr;
1422 u32 num_of_gtk_rekeys;
1423 u32 received_beacons;
1424 u32 wakeup_reasons;
1425 u32 wake_packet_length;
1426 u32 wake_packet_bufsize;
1427 u16 pattern_number;
1428 u16 non_qos_seq_ctr;
1429 u16 qos_seq_ctr[8];
1430 u8 tid_tear_down;
1431 u8 tid_offloaded_tx;
1432
1433 struct {
1434 /* including RX MIC key for TKIP */
1435 u8 key[WOWLAN_KEY_MAX_SIZE];
1436 u8 len;
1437 u8 flags;
1438 u8 id;
1439 } gtk[WOWLAN_GTK_KEYS_NUM];
1440
1441 struct {
1442 /*
1443 * We store both the TKIP and AES representations
1444 * coming from the firmware because we decode the
1445 * data from there before we iterate the keys and
1446 * know which one we need.
1447 */
1448 struct {
1449 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1450 } tkip, aes;
1451
1452 /*
1453 * We use -1 for when we have valid data but don't know
1454 * the key ID from firmware, and thus it needs to be
1455 * installed with the last key (depending on rekeying).
1456 */
1457 s8 key_id;
1458 bool valid;
1459 } gtk_seq[2];
1460
1461 struct {
1462 /* Same as above */
1463 struct {
1464 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1465 u64 tx_pn;
1466 } tkip, aes;
1467 } ptk;
1468
1469 struct iwl_multicast_key_data igtk;
1470 struct iwl_multicast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
1471
1472 u8 *wake_packet;
1473 };
1474
iwl_mvm_report_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)1475 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1476 struct ieee80211_vif *vif,
1477 struct iwl_wowlan_status_data *status)
1478 {
1479 struct sk_buff *pkt = NULL;
1480 struct cfg80211_wowlan_wakeup wakeup = {
1481 .pattern_idx = -1,
1482 };
1483 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1484 u32 reasons = status->wakeup_reasons;
1485
1486 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1487 wakeup_report = NULL;
1488 goto report;
1489 }
1490
1491 pm_wakeup_event(mvm->dev, 0);
1492
1493 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1494 wakeup.magic_pkt = true;
1495
1496 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1497 wakeup.pattern_idx =
1498 status->pattern_number;
1499
1500 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1501 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
1502 IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
1503 wakeup.disconnect = true;
1504
1505 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1506 wakeup.gtk_rekey_failure = true;
1507
1508 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1509 wakeup.rfkill_release = true;
1510
1511 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1512 wakeup.eap_identity_req = true;
1513
1514 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1515 wakeup.four_way_handshake = true;
1516
1517 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1518 wakeup.tcp_connlost = true;
1519
1520 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1521 wakeup.tcp_nomoretokens = true;
1522
1523 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1524 wakeup.tcp_match = true;
1525
1526 if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC)
1527 wakeup.unprot_deauth_disassoc = true;
1528
1529 if (status->wake_packet) {
1530 int pktsize = status->wake_packet_bufsize;
1531 int pktlen = status->wake_packet_length;
1532 const u8 *pktdata = status->wake_packet;
1533 const struct ieee80211_hdr *hdr = (const void *)pktdata;
1534 int truncated = pktlen - pktsize;
1535
1536 /* this would be a firmware bug */
1537 if (WARN_ON_ONCE(truncated < 0))
1538 truncated = 0;
1539
1540 if (ieee80211_is_data(hdr->frame_control)) {
1541 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1542 int ivlen = 0, icvlen = 4; /* also FCS */
1543
1544 pkt = alloc_skb(pktsize, GFP_KERNEL);
1545 if (!pkt)
1546 goto report;
1547
1548 skb_put_data(pkt, pktdata, hdrlen);
1549 pktdata += hdrlen;
1550 pktsize -= hdrlen;
1551
1552 if (ieee80211_has_protected(hdr->frame_control)) {
1553 /*
1554 * This is unlocked and using gtk_i(c)vlen,
1555 * but since everything is under RTNL still
1556 * that's not really a problem - changing
1557 * it would be difficult.
1558 */
1559 if (is_multicast_ether_addr(hdr->addr1)) {
1560 ivlen = mvm->gtk_ivlen;
1561 icvlen += mvm->gtk_icvlen;
1562 } else {
1563 ivlen = mvm->ptk_ivlen;
1564 icvlen += mvm->ptk_icvlen;
1565 }
1566 }
1567
1568 /* if truncated, FCS/ICV is (partially) gone */
1569 if (truncated >= icvlen) {
1570 icvlen = 0;
1571 truncated -= icvlen;
1572 } else {
1573 icvlen -= truncated;
1574 truncated = 0;
1575 }
1576
1577 pktsize -= ivlen + icvlen;
1578 pktdata += ivlen;
1579
1580 skb_put_data(pkt, pktdata, pktsize);
1581
1582 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1583 goto report;
1584 wakeup.packet = pkt->data;
1585 wakeup.packet_present_len = pkt->len;
1586 wakeup.packet_len = pkt->len - truncated;
1587 wakeup.packet_80211 = false;
1588 } else {
1589 int fcslen = 4;
1590
1591 if (truncated >= 4) {
1592 truncated -= 4;
1593 fcslen = 0;
1594 } else {
1595 fcslen -= truncated;
1596 truncated = 0;
1597 }
1598 pktsize -= fcslen;
1599 wakeup.packet = status->wake_packet;
1600 wakeup.packet_present_len = pktsize;
1601 wakeup.packet_len = pktlen - truncated;
1602 wakeup.packet_80211 = true;
1603 }
1604 }
1605
1606 report:
1607 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1608 kfree_skb(pkt);
1609 }
1610
iwl_mvm_le64_to_aes_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1611 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1612 {
1613 u64 pn = le64_to_cpu(le_pn);
1614
1615 seq->ccmp.pn[0] = pn >> 40;
1616 seq->ccmp.pn[1] = pn >> 32;
1617 seq->ccmp.pn[2] = pn >> 24;
1618 seq->ccmp.pn[3] = pn >> 16;
1619 seq->ccmp.pn[4] = pn >> 8;
1620 seq->ccmp.pn[5] = pn;
1621 }
1622
iwl_mvm_aes_sc_to_seq(struct aes_sc * sc,struct ieee80211_key_seq * seq)1623 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1624 struct ieee80211_key_seq *seq)
1625 {
1626 iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1627 }
1628
iwl_mvm_le64_to_tkip_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1629 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1630 {
1631 u64 pn = le64_to_cpu(le_pn);
1632
1633 seq->tkip.iv16 = (u16)pn;
1634 seq->tkip.iv32 = (u32)(pn >> 16);
1635 }
1636
iwl_mvm_tkip_sc_to_seq(struct tkip_sc * sc,struct ieee80211_key_seq * seq)1637 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1638 struct ieee80211_key_seq *seq)
1639 {
1640 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1641 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1642 }
1643
iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf * key,struct ieee80211_key_seq * seq)1644 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1645 struct ieee80211_key_seq *seq)
1646 {
1647 int tid;
1648
1649 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1650 ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1651 }
1652
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)1653 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1654 struct iwl_wowlan_status_data *status,
1655 struct ieee80211_sta *sta,
1656 struct ieee80211_key_conf *key)
1657 {
1658 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1659 struct iwl_mvm_key_pn *ptk_pn;
1660 int tid;
1661
1662 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1663
1664 if (!iwl_mvm_has_new_rx_api(mvm))
1665 return;
1666
1667
1668 rcu_read_lock();
1669 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1670 if (WARN_ON(!ptk_pn)) {
1671 rcu_read_unlock();
1672 return;
1673 }
1674
1675 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1676 int i;
1677
1678 for (i = 1; i < mvm->trans->info.num_rxqs; i++)
1679 memcpy(ptk_pn->q[i].pn[tid],
1680 status->ptk.aes.seq[tid].ccmp.pn,
1681 IEEE80211_CCMP_PN_LEN);
1682 }
1683 rcu_read_unlock();
1684 }
1685
iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data * status,union iwl_all_tsc_rsc * sc,u8 key_idx)1686 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1687 union iwl_all_tsc_rsc *sc, u8 key_idx)
1688 {
1689 int i;
1690
1691 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1692 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1693
1694 /* GTK RX counters */
1695 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1696 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1697 &status->gtk_seq[0].tkip.seq[i]);
1698 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1699 &status->gtk_seq[0].aes.seq[i]);
1700 }
1701 status->gtk_seq[0].valid = true;
1702 status->gtk_seq[0].key_id = key_idx;
1703
1704 /* PTK TX counter */
1705 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1706 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1707 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1708
1709 /* PTK RX counters */
1710 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1711 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1712 &status->ptk.tkip.seq[i]);
1713 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1714 &status->ptk.aes.seq[i]);
1715 }
1716 }
1717
1718 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)1719 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1720 struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1721 unsigned int idx, unsigned int key_id)
1722 {
1723 int tid;
1724
1725 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1726 iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1727 &status->gtk_seq[idx].tkip.seq[tid]);
1728 iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1729 &status->gtk_seq[idx].aes.seq[tid]);
1730 }
1731
1732 status->gtk_seq[idx].valid = true;
1733 status->gtk_seq[idx].key_id = key_id;
1734 }
1735
1736 static void
iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc)1737 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1738 struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1739 {
1740 int i, tid;
1741
1742 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1743 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1744 BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1745
1746 /* GTK RX counters */
1747 for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1748 u8 entry = sc->mcast_key_id_map[i];
1749
1750 if (entry < ARRAY_SIZE(sc->mcast_rsc))
1751 iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1752 entry, i);
1753 }
1754
1755 /* PTK TX counters not needed, assigned in device */
1756
1757 /* PTK RX counters */
1758 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1759 iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1760 &status->ptk.tkip.seq[tid]);
1761 iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1762 &status->ptk.aes.seq[tid]);
1763 }
1764 }
1765
iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,int idx)1766 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1767 struct iwl_wowlan_status_data *status,
1768 int idx)
1769 {
1770 switch (key->cipher) {
1771 case WLAN_CIPHER_SUITE_CCMP:
1772 case WLAN_CIPHER_SUITE_GCMP:
1773 case WLAN_CIPHER_SUITE_GCMP_256:
1774 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1775 break;
1776 case WLAN_CIPHER_SUITE_TKIP:
1777 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1778 break;
1779 default:
1780 WARN_ON(1);
1781 }
1782 }
1783
iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status)1784 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1785 struct iwl_wowlan_status_data *status)
1786 {
1787 int i;
1788
1789 for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1790 if (!status->gtk_seq[i].valid)
1791 continue;
1792
1793 if (status->gtk_seq[i].key_id == key->keyidx)
1794 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1795 }
1796 }
1797
1798 struct iwl_mvm_d3_gtk_iter_data {
1799 struct iwl_mvm *mvm;
1800 struct iwl_wowlan_status_data *status;
1801 u32 gtk_cipher, igtk_cipher, bigtk_cipher;
1802 bool unhandled_cipher, igtk_support, bigtk_support;
1803 int num_keys;
1804 };
1805
iwl_mvm_d3_find_last_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1806 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1807 struct ieee80211_vif *vif,
1808 struct ieee80211_sta *sta,
1809 struct ieee80211_key_conf *key,
1810 void *_data)
1811 {
1812 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1813 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1814
1815 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1816 return;
1817
1818 if (data->unhandled_cipher)
1819 return;
1820
1821 switch (key->cipher) {
1822 case WLAN_CIPHER_SUITE_WEP40:
1823 case WLAN_CIPHER_SUITE_WEP104:
1824 /* ignore WEP completely, nothing to do */
1825 return;
1826 case WLAN_CIPHER_SUITE_CCMP:
1827 case WLAN_CIPHER_SUITE_GCMP:
1828 case WLAN_CIPHER_SUITE_GCMP_256:
1829 case WLAN_CIPHER_SUITE_TKIP:
1830 /* we support these */
1831 data->gtk_cipher = key->cipher;
1832 break;
1833 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1834 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1835 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1836 case WLAN_CIPHER_SUITE_AES_CMAC:
1837 /* we support these */
1838 if (data->igtk_support &&
1839 (key->keyidx == 4 || key->keyidx == 5)) {
1840 data->igtk_cipher = key->cipher;
1841 } else if (data->bigtk_support &&
1842 (key->keyidx == 6 || key->keyidx == 7)) {
1843 data->bigtk_cipher = key->cipher;
1844 } else {
1845 data->unhandled_cipher = true;
1846 return;
1847 }
1848 break;
1849 default:
1850 /* everything else - disconnect from AP */
1851 data->unhandled_cipher = true;
1852 return;
1853 }
1854
1855 data->num_keys++;
1856 }
1857
1858 static void
iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data * key,struct ieee80211_key_seq * seq,u32 cipher)1859 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1860 struct ieee80211_key_seq *seq, u32 cipher)
1861 {
1862 switch (cipher) {
1863 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1864 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1865 BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1866 memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1867 break;
1868 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1869 case WLAN_CIPHER_SUITE_AES_CMAC:
1870 BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1871 memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1872 break;
1873 default:
1874 WARN_ON(1);
1875 }
1876 }
1877
1878 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)1879 iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data *status,
1880 struct ieee80211_key_conf *key,
1881 struct iwl_multicast_key_data *key_data)
1882 {
1883 struct ieee80211_key_seq seq;
1884
1885 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, key->cipher);
1886 ieee80211_set_key_rx_seq(key, 0, &seq);
1887 }
1888
iwl_mvm_d3_update_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1889 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1890 struct ieee80211_vif *vif,
1891 struct ieee80211_sta *sta,
1892 struct ieee80211_key_conf *key,
1893 void *_data)
1894 {
1895 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1896 struct iwl_wowlan_status_data *status = data->status;
1897 s8 keyidx;
1898 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1899
1900 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1901 return;
1902
1903 if (data->unhandled_cipher)
1904 return;
1905
1906 switch (key->cipher) {
1907 case WLAN_CIPHER_SUITE_WEP40:
1908 case WLAN_CIPHER_SUITE_WEP104:
1909 /* ignore WEP completely, nothing to do */
1910 return;
1911 case WLAN_CIPHER_SUITE_CCMP:
1912 case WLAN_CIPHER_SUITE_GCMP:
1913 case WLAN_CIPHER_SUITE_GCMP_256:
1914 if (sta) {
1915 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1916 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1917 return;
1918 }
1919 fallthrough;
1920 case WLAN_CIPHER_SUITE_TKIP:
1921 if (sta) {
1922 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1923 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1924 return;
1925 }
1926 keyidx = key->keyidx;
1927 /*
1928 * Update the seq even if there was a rekey. If there was a
1929 * rekey, we will update again after replacing the key
1930 */
1931 if ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1932 (status->gtk[1].len && keyidx == status->gtk[1].id))
1933 iwl_mvm_set_key_rx_seq(key, status);
1934 break;
1935 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1936 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1937 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1938 case WLAN_CIPHER_SUITE_AES_CMAC:
1939 if (key->keyidx == 4 || key->keyidx == 5) {
1940 iwl_mvm_d3_update_igtk_bigtk(status, key,
1941 &status->igtk);
1942 }
1943 if (key->keyidx == 6 || key->keyidx == 7) {
1944 u8 idx = key->keyidx == status->bigtk[1].id;
1945
1946 iwl_mvm_d3_update_igtk_bigtk(status, key,
1947 &status->bigtk[idx]);
1948 }
1949 }
1950 }
1951
iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm,u32 gtk_cipher)1952 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
1953 struct ieee80211_vif *vif,
1954 struct iwl_mvm *mvm, u32 gtk_cipher)
1955 {
1956 int i, j;
1957 struct ieee80211_key_conf *key;
1958 DEFINE_RAW_FLEX(struct ieee80211_key_conf, conf, key,
1959 WOWLAN_KEY_MAX_SIZE);
1960 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1961 u8 key_data[WOWLAN_KEY_MAX_SIZE];
1962
1963 conf->cipher = gtk_cipher;
1964
1965 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
1966 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < WLAN_KEY_LEN_CCMP);
1967 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < WLAN_KEY_LEN_GCMP_256);
1968 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < WLAN_KEY_LEN_TKIP);
1969 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < sizeof(status->gtk[0].key));
1970
1971 switch (gtk_cipher) {
1972 case WLAN_CIPHER_SUITE_CCMP:
1973 case WLAN_CIPHER_SUITE_GCMP:
1974 conf->keylen = WLAN_KEY_LEN_CCMP;
1975 break;
1976 case WLAN_CIPHER_SUITE_GCMP_256:
1977 conf->keylen = WLAN_KEY_LEN_GCMP_256;
1978 break;
1979 case WLAN_CIPHER_SUITE_TKIP:
1980 conf->keylen = WLAN_KEY_LEN_TKIP;
1981 break;
1982 default:
1983 WARN_ON(1);
1984 }
1985
1986 for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
1987 if (!status->gtk[i].len)
1988 continue;
1989
1990 conf->keyidx = status->gtk[i].id;
1991 IWL_DEBUG_WOWLAN(mvm,
1992 "Received from FW GTK cipher %d, key index %d\n",
1993 conf->cipher, conf->keyidx);
1994 memcpy(conf->key, status->gtk[i].key,
1995 sizeof(status->gtk[i].key));
1996 memcpy(key_data, status->gtk[i].key, sizeof(status->gtk[i].key));
1997
1998 key = ieee80211_gtk_rekey_add(vif, status->gtk[i].id, key_data,
1999 sizeof(key_data), link_id);
2000 if (IS_ERR(key)) {
2001 /* FW may send also the old keys */
2002 if (PTR_ERR(key) == -EALREADY)
2003 continue;
2004 return false;
2005 }
2006
2007 for (j = 0; j < ARRAY_SIZE(status->gtk_seq); j++) {
2008 if (!status->gtk_seq[j].valid ||
2009 status->gtk_seq[j].key_id != key->keyidx)
2010 continue;
2011 iwl_mvm_set_key_rx_seq_idx(key, status, j);
2012 break;
2013 }
2014 WARN_ON(j == ARRAY_SIZE(status->gtk_seq));
2015 }
2016
2017 return true;
2018 }
2019
2020 static bool
iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,u32 cipher,struct iwl_multicast_key_data * key_data)2021 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
2022 struct ieee80211_vif *vif, u32 cipher,
2023 struct iwl_multicast_key_data *key_data)
2024 {
2025 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2026 DEFINE_RAW_FLEX(struct ieee80211_key_conf, conf, key,
2027 WOWLAN_KEY_MAX_SIZE);
2028 struct ieee80211_key_conf *key_config;
2029 struct ieee80211_key_seq seq;
2030 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2031 u8 key[WOWLAN_KEY_MAX_SIZE];
2032 s8 keyidx = key_data->id;
2033
2034 conf->cipher = cipher;
2035 conf->keyidx = keyidx;
2036
2037 if (!key_data->len)
2038 return true;
2039
2040 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf->cipher);
2041
2042 switch (cipher) {
2043 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2044 conf->keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2045 break;
2046 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2047 conf->keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2048 break;
2049 case WLAN_CIPHER_SUITE_AES_CMAC:
2050 conf->keylen = WLAN_KEY_LEN_AES_CMAC;
2051 break;
2052 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2053 conf->keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2054 break;
2055 default:
2056 WARN_ON(1);
2057 }
2058 BUILD_BUG_ON(WOWLAN_KEY_MAX_SIZE < sizeof(key_data->key));
2059 memcpy(conf->key, key_data->key, conf->keylen);
2060
2061 memcpy(key, key_data->key, sizeof(key_data->key));
2062
2063 key_config = ieee80211_gtk_rekey_add(vif, keyidx, key, sizeof(key),
2064 link_id);
2065 if (IS_ERR(key_config)) {
2066 /* FW may send also the old keys */
2067 return PTR_ERR(key_config) == -EALREADY;
2068 }
2069 ieee80211_set_key_rx_seq(key_config, 0, &seq);
2070
2071 if (keyidx == 4 || keyidx == 5) {
2072 struct iwl_mvm_vif_link_info *mvm_link;
2073
2074 link_id = link_id < 0 ? 0 : link_id;
2075 mvm_link = mvmvif->link[link_id];
2076 if (mvm_link->igtk)
2077 mvm_link->igtk->hw_key_idx = STA_KEY_IDX_INVALID;
2078 mvm_link->igtk = key_config;
2079 }
2080
2081 if (vif->type == NL80211_IFTYPE_STATION && (keyidx == 6 || keyidx == 7))
2082 rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
2083 key_config);
2084
2085 return true;
2086 }
2087
iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm * mvm)2088 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2089 {
2090 u8 notif_ver;
2091
2092 if (!fw_has_api(&mvm->fw->ucode_capa,
2093 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2094 return 6;
2095
2096 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2097 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2098 WOWLAN_GET_STATUSES, 0);
2099 if (!notif_ver)
2100 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2101 WOWLAN_GET_STATUSES, 7);
2102
2103 return notif_ver;
2104 }
2105
iwl_mvm_setup_connection_keep(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2106 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2107 struct ieee80211_vif *vif,
2108 struct iwl_wowlan_status_data *status)
2109 {
2110 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2111 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2112 .mvm = mvm,
2113 .status = status,
2114 };
2115 int i;
2116 u32 disconnection_reasons =
2117 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2118 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2119
2120 if (!status || !vif->bss_conf.bssid)
2121 return false;
2122
2123 if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2124 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2125 WOWLAN_INFO_NOTIFICATION,
2126 0))
2127 gtkdata.igtk_support = true;
2128
2129 if (iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2130 WOWLAN_INFO_NOTIFICATION,
2131 0) >= 3)
2132 gtkdata.bigtk_support = true;
2133
2134 /* find last GTK that we used initially, if any */
2135 ieee80211_iter_keys(mvm->hw, vif,
2136 iwl_mvm_d3_find_last_keys, >kdata);
2137 /* not trying to keep connections with MFP/unhandled ciphers */
2138 if (gtkdata.unhandled_cipher)
2139 return false;
2140 if (!gtkdata.num_keys)
2141 goto out;
2142
2143 /*
2144 * invalidate all other GTKs that might still exist and update
2145 * the one that we used
2146 */
2147 ieee80211_iter_keys(mvm->hw, vif,
2148 iwl_mvm_d3_update_keys, >kdata);
2149
2150 if (status->num_of_gtk_rekeys) {
2151 __be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2152
2153 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2154 status->num_of_gtk_rekeys);
2155
2156 if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2157 return false;
2158
2159 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2160 gtkdata.igtk_cipher,
2161 &status->igtk))
2162 return false;
2163
2164 for (i = 0; i < ARRAY_SIZE(status->bigtk); i++) {
2165 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2166 gtkdata.bigtk_cipher,
2167 &status->bigtk[i]))
2168 return false;
2169 }
2170
2171 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2172 (void *)&replay_ctr, GFP_KERNEL);
2173 }
2174
2175 out:
2176 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2177 WOWLAN_GET_STATUSES,
2178 IWL_FW_CMD_VER_UNKNOWN) < 10) {
2179 mvmvif->seqno_valid = true;
2180 /* +0x10 because the set API expects next-to-use, not last-used */
2181 mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2182 }
2183
2184 if (status->wakeup_reasons & disconnection_reasons)
2185 return false;
2186
2187 return true;
2188 }
2189
iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v2 * data)2190 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2191 struct iwl_wowlan_gtk_status_v2 *data)
2192 {
2193 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2194 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2195 sizeof(data->tkip_mic_key) >
2196 sizeof(status->gtk[0].key));
2197
2198 status->gtk[0].len = data->key_len;
2199 status->gtk[0].flags = data->key_flags;
2200 status->gtk[0].id = status->gtk[0].flags & IWL_WOWLAN_GTK_IDX_MASK;
2201
2202 memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2203
2204 /* if it's as long as the TKIP encryption key, copy MIC key */
2205 if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2206 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2207 data->tkip_mic_key, sizeof(data->tkip_mic_key));
2208 }
2209
iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v3 * data)2210 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2211 struct iwl_wowlan_gtk_status_v3 *data)
2212 {
2213 int data_idx, status_idx = 0;
2214
2215 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2216 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2217 sizeof(data[0].tkip_mic_key) >
2218 sizeof(status->gtk[0].key));
2219 BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2220 for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2221 if (!(data[data_idx].key_len))
2222 continue;
2223 status->gtk[status_idx].len = data[data_idx].key_len;
2224 status->gtk[status_idx].flags = data[data_idx].key_flags;
2225 status->gtk[status_idx].id = status->gtk[status_idx].flags &
2226 IWL_WOWLAN_GTK_IDX_MASK;
2227
2228 memcpy(status->gtk[status_idx].key, data[data_idx].key,
2229 sizeof(data[data_idx].key));
2230
2231 /* if it's as long as the TKIP encryption key, copy MIC key */
2232 if (status->gtk[status_idx].len ==
2233 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2234 memcpy(status->gtk[status_idx].key +
2235 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2236 data[data_idx].tkip_mic_key,
2237 sizeof(data[data_idx].tkip_mic_key));
2238 status_idx++;
2239 }
2240 }
2241
iwl_mvm_convert_igtk(struct iwl_wowlan_status_data * status,struct iwl_wowlan_igtk_status * data)2242 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2243 struct iwl_wowlan_igtk_status *data)
2244 {
2245 int i;
2246
2247 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2248 BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2249
2250 if (!data->key_len)
2251 return;
2252
2253 status->igtk.len = data->key_len;
2254 status->igtk.flags = data->key_flags;
2255 status->igtk.id = u32_get_bits(data->key_flags,
2256 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2257 + WOWLAN_IGTK_MIN_INDEX;
2258
2259 memcpy(status->igtk.key, data->key, sizeof(data->key));
2260
2261 /* mac80211 expects big endian for memcmp() to work, convert */
2262 for (i = 0; i < sizeof(data->ipn); i++)
2263 status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2264 }
2265
iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data * status,const struct iwl_wowlan_igtk_status * data)2266 static void iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data *status,
2267 const struct iwl_wowlan_igtk_status *data)
2268 {
2269 int data_idx, status_idx = 0;
2270
2271 BUILD_BUG_ON(ARRAY_SIZE(status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
2272
2273 for (data_idx = 0; data_idx < WOWLAN_BIGTK_KEYS_NUM; data_idx++) {
2274 if (!data[data_idx].key_len)
2275 continue;
2276
2277 status->bigtk[status_idx].len = data[data_idx].key_len;
2278 status->bigtk[status_idx].flags = data[data_idx].key_flags;
2279 status->bigtk[status_idx].id =
2280 u32_get_bits(data[data_idx].key_flags,
2281 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2282 + WOWLAN_BIGTK_MIN_INDEX;
2283
2284 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].key) <
2285 sizeof(data[data_idx].key));
2286 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].ipn) <
2287 sizeof(data[data_idx].ipn));
2288
2289 memcpy(status->bigtk[status_idx].key, data[data_idx].key,
2290 sizeof(data[data_idx].key));
2291 memcpy(status->bigtk[status_idx].ipn, data[data_idx].ipn,
2292 sizeof(data[data_idx].ipn));
2293 status_idx++;
2294 }
2295 }
2296
iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif * data,struct iwl_wowlan_status_data * status,u32 len)2297 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2298 struct iwl_wowlan_info_notif *data,
2299 struct iwl_wowlan_status_data *status,
2300 u32 len)
2301 {
2302 if (IWL_FW_CHECK(mvm, data->num_mlo_link_keys,
2303 "MLO is not supported, shouldn't receive MLO keys\n"))
2304 return;
2305
2306 if (len < sizeof(*data)) {
2307 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2308 status = NULL;
2309 return;
2310 }
2311
2312 if (mvm->fast_resume)
2313 return;
2314
2315 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2316 iwl_mvm_convert_gtk_v3(status, data->gtk);
2317 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2318 iwl_mvm_convert_bigtk(status, data->bigtk);
2319 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2320 status->pattern_number = le16_to_cpu(data->pattern_number);
2321 status->tid_offloaded_tx = data->tid_offloaded_tx;
2322 if (IWL_FW_CHECK(mvm,
2323 data->tid_offloaded_tx >=
2324 ARRAY_SIZE(status->qos_seq_ctr),
2325 "tid_offloaded_tx is out of bound %d\n",
2326 data->tid_offloaded_tx))
2327 data->tid_offloaded_tx = 0;
2328 status->qos_seq_ctr[data->tid_offloaded_tx] =
2329 le16_to_cpu(data->qos_seq_ctr);
2330 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2331 status->num_of_gtk_rekeys =
2332 le32_to_cpu(data->num_of_gtk_rekeys);
2333 status->received_beacons = le32_to_cpu(data->received_beacons);
2334 status->tid_tear_down = data->tid_tear_down;
2335 }
2336
2337 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)2338 iwl_mvm_parse_wowlan_info_notif_v3(struct iwl_mvm *mvm,
2339 struct iwl_wowlan_info_notif_v3 *data,
2340 struct iwl_wowlan_status_data *status,
2341 u32 len)
2342 {
2343 u32 i;
2344
2345 if (len < sizeof(*data)) {
2346 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2347 status = NULL;
2348 return;
2349 }
2350
2351 if (mvm->fast_resume)
2352 return;
2353
2354 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2355 iwl_mvm_convert_gtk_v3(status, data->gtk);
2356 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2357 iwl_mvm_convert_bigtk(status, data->bigtk);
2358 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2359 status->pattern_number = le16_to_cpu(data->pattern_number);
2360 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2361 status->qos_seq_ctr[i] =
2362 le16_to_cpu(data->qos_seq_ctr[i]);
2363 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2364 status->num_of_gtk_rekeys =
2365 le32_to_cpu(data->num_of_gtk_rekeys);
2366 status->received_beacons = le32_to_cpu(data->received_beacons);
2367 status->tid_tear_down = data->tid_tear_down;
2368 }
2369
2370 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)2371 iwl_mvm_parse_wowlan_info_notif_v1(struct iwl_mvm *mvm,
2372 struct iwl_wowlan_info_notif_v1 *data,
2373 struct iwl_wowlan_status_data *status,
2374 u32 len)
2375 {
2376 u32 i;
2377
2378 if (len < sizeof(*data)) {
2379 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2380 status = NULL;
2381 return;
2382 }
2383
2384 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2385 iwl_mvm_convert_gtk_v3(status, data->gtk);
2386 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2387 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2388 status->pattern_number = le16_to_cpu(data->pattern_number);
2389 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2390 status->qos_seq_ctr[i] =
2391 le16_to_cpu(data->qos_seq_ctr[i]);
2392 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2393 status->num_of_gtk_rekeys =
2394 le32_to_cpu(data->num_of_gtk_rekeys);
2395 status->received_beacons = le32_to_cpu(data->received_beacons);
2396 status->tid_tear_down = data->tid_tear_down;
2397 }
2398
2399 /* Occasionally, templates would be nice. This is one of those times ... */
2400 #define iwl_mvm_parse_wowlan_status_common(_ver) \
2401 static struct iwl_wowlan_status_data * \
2402 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \
2403 struct iwl_wowlan_status_ ##_ver *data,\
2404 int len) \
2405 { \
2406 struct iwl_wowlan_status_data *status; \
2407 int data_size, i; \
2408 \
2409 if (len < sizeof(*data)) { \
2410 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2411 return NULL; \
2412 } \
2413 \
2414 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \
2415 if (len != sizeof(*data) + data_size) { \
2416 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2417 return NULL; \
2418 } \
2419 \
2420 status = kzalloc(sizeof(*status), GFP_KERNEL); \
2421 if (!status) \
2422 return NULL; \
2423 \
2424 /* copy all the common fields */ \
2425 status->replay_ctr = le64_to_cpu(data->replay_ctr); \
2426 status->pattern_number = le16_to_cpu(data->pattern_number); \
2427 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \
2428 for (i = 0; i < 8; i++) \
2429 status->qos_seq_ctr[i] = \
2430 le16_to_cpu(data->qos_seq_ctr[i]); \
2431 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \
2432 status->num_of_gtk_rekeys = \
2433 le32_to_cpu(data->num_of_gtk_rekeys); \
2434 status->received_beacons = le32_to_cpu(data->received_beacons); \
2435 status->wake_packet_length = \
2436 le32_to_cpu(data->wake_packet_length); \
2437 status->wake_packet_bufsize = \
2438 le32_to_cpu(data->wake_packet_bufsize); \
2439 if (status->wake_packet_bufsize) { \
2440 status->wake_packet = \
2441 kmemdup(data->wake_packet, \
2442 status->wake_packet_bufsize, \
2443 GFP_KERNEL); \
2444 if (!status->wake_packet) { \
2445 kfree(status); \
2446 return NULL; \
2447 } \
2448 } else { \
2449 status->wake_packet = NULL; \
2450 } \
2451 \
2452 return status; \
2453 }
2454
2455 iwl_mvm_parse_wowlan_status_common(v6)
iwl_mvm_parse_wowlan_status_common(v7)2456 iwl_mvm_parse_wowlan_status_common(v7)
2457
2458 static struct iwl_wowlan_status_data *
2459 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2460 {
2461 struct iwl_wowlan_status_data *status;
2462 struct iwl_wowlan_get_status_cmd get_status_cmd = {
2463 .sta_id = cpu_to_le32(sta_id),
2464 };
2465 struct iwl_host_cmd cmd = {
2466 .id = WOWLAN_GET_STATUSES,
2467 .flags = CMD_WANT_SKB,
2468 .data = { &get_status_cmd, },
2469 .len = { sizeof(get_status_cmd), },
2470 };
2471 int ret, len;
2472 u8 notif_ver;
2473 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2474 IWL_FW_CMD_VER_UNKNOWN);
2475
2476 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2477 cmd.len[0] = 0;
2478
2479 lockdep_assert_held(&mvm->mutex);
2480
2481 ret = iwl_mvm_send_cmd(mvm, &cmd);
2482 if (ret) {
2483 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2484 return ERR_PTR(ret);
2485 }
2486
2487 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2488
2489 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2490 notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2491
2492 if (notif_ver < 7) {
2493 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2494
2495 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2496 if (!status)
2497 goto out_free_resp;
2498
2499 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2500 sizeof(status->gtk[0].key));
2501 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2502 sizeof(v6->gtk.tkip_mic_key) >
2503 sizeof(status->gtk[0].key));
2504
2505 /* copy GTK info to the right place */
2506 memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2507 sizeof(v6->gtk.decrypt_key));
2508 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2509 v6->gtk.tkip_mic_key,
2510 sizeof(v6->gtk.tkip_mic_key));
2511
2512 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc,
2513 v6->gtk.key_index);
2514
2515 /* hardcode the key length to 16 since v6 only supports 16 */
2516 status->gtk[0].len = 16;
2517
2518 /*
2519 * The key index only uses 2 bits (values 0 to 3) and
2520 * we always set bit 7 which means this is the
2521 * currently used key.
2522 */
2523 status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2524 status->gtk[0].id = v6->gtk.key_index;
2525 } else if (notif_ver == 7) {
2526 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2527
2528 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2529 if (!status)
2530 goto out_free_resp;
2531
2532 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc,
2533 v7->gtk[0].key_flags & IWL_WOWLAN_GTK_IDX_MASK);
2534 iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2535 iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2536 } else {
2537 IWL_ERR(mvm,
2538 "Firmware advertises unknown WoWLAN status response %d!\n",
2539 notif_ver);
2540 status = NULL;
2541 }
2542
2543 out_free_resp:
2544 iwl_free_resp(&cmd);
2545 return status;
2546 }
2547
2548 /* releases the MVM mutex */
iwl_mvm_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2549 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2550 struct ieee80211_vif *vif,
2551 struct iwl_wowlan_status_data *status)
2552 {
2553 int i;
2554 bool keep = false;
2555 struct iwl_mvm_sta *mvm_ap_sta;
2556 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2557 int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2558 struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
2559 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
2560 PROT_OFFLOAD_GROUP,
2561 WOWLAN_INFO_NOTIFICATION,
2562 IWL_FW_CMD_VER_UNKNOWN);
2563
2564 if (WARN_ON(!mvm_link))
2565 goto out_unlock;
2566
2567 if (!status)
2568 goto out_unlock;
2569
2570 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2571 status->wakeup_reasons);
2572
2573 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, mvm_link->ap_sta_id);
2574 if (!mvm_ap_sta)
2575 goto out_unlock;
2576
2577 /* firmware stores last-used value, we store next value */
2578 if (wowlan_info_ver >= 5) {
2579 mvm_ap_sta->tid_data[status->tid_offloaded_tx].seq_number =
2580 status->qos_seq_ctr[status->tid_offloaded_tx] + 0x10;
2581 } else {
2582 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2583 mvm_ap_sta->tid_data[i].seq_number =
2584 status->qos_seq_ctr[i] + 0x10;
2585 }
2586
2587 if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2588 i = mvm->offload_tid;
2589 iwl_trans_set_q_ptrs(mvm->trans,
2590 mvm_ap_sta->tid_data[i].txq_id,
2591 mvm_ap_sta->tid_data[i].seq_number >> 4);
2592 }
2593
2594 iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2595
2596 keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2597 out_unlock:
2598 mutex_unlock(&mvm->mutex);
2599 return keep;
2600 }
2601
2602 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2603 IWL_SCAN_MAX_PROFILES)
2604
2605 struct iwl_mvm_nd_results {
2606 u32 matched_profiles;
2607 u8 matches[ND_QUERY_BUF_LEN];
2608 };
2609
2610 static int
iwl_mvm_netdetect_query_results(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results)2611 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2612 struct iwl_mvm_nd_results *results)
2613 {
2614 struct iwl_scan_offload_match_info *query;
2615 struct iwl_host_cmd cmd = {
2616 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2617 .flags = CMD_WANT_SKB,
2618 };
2619 int ret, len;
2620 size_t query_len, matches_len;
2621 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2622
2623 ret = iwl_mvm_send_cmd(mvm, &cmd);
2624 if (ret) {
2625 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2626 return ret;
2627 }
2628
2629 if (fw_has_api(&mvm->fw->ucode_capa,
2630 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2631 query_len = sizeof(struct iwl_scan_offload_match_info);
2632 matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2633 max_profiles;
2634 } else {
2635 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2636 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2637 max_profiles;
2638 }
2639
2640 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2641 if (len < query_len) {
2642 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2643 ret = -EIO;
2644 goto out_free_resp;
2645 }
2646
2647 query = (void *)cmd.resp_pkt->data;
2648
2649 results->matched_profiles = le32_to_cpu(query->matched_profiles);
2650 memcpy(results->matches, query->matches, matches_len);
2651
2652 #ifdef CONFIG_IWLWIFI_DEBUGFS
2653 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2654 #endif
2655
2656 out_free_resp:
2657 iwl_free_resp(&cmd);
2658 return ret;
2659 }
2660
iwl_mvm_query_num_match_chans(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,int idx)2661 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2662 struct iwl_mvm_nd_results *results,
2663 int idx)
2664 {
2665 int n_chans = 0, i;
2666
2667 if (fw_has_api(&mvm->fw->ucode_capa,
2668 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2669 struct iwl_scan_offload_profile_match *matches =
2670 (void *)results->matches;
2671
2672 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2673 n_chans += hweight8(matches[idx].matching_channels[i]);
2674 } else {
2675 struct iwl_scan_offload_profile_match_v1 *matches =
2676 (void *)results->matches;
2677
2678 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2679 n_chans += hweight8(matches[idx].matching_channels[i]);
2680 }
2681
2682 return n_chans;
2683 }
2684
iwl_mvm_query_set_freqs(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,struct cfg80211_wowlan_nd_match * match,int idx)2685 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2686 struct iwl_mvm_nd_results *results,
2687 struct cfg80211_wowlan_nd_match *match,
2688 int idx)
2689 {
2690 int i;
2691 int n_channels = 0;
2692
2693 if (fw_has_api(&mvm->fw->ucode_capa,
2694 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2695 struct iwl_scan_offload_profile_match *matches =
2696 (void *)results->matches;
2697
2698 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2699 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2700 match->channels[n_channels++] =
2701 mvm->nd_channels[i]->center_freq;
2702 } else {
2703 struct iwl_scan_offload_profile_match_v1 *matches =
2704 (void *)results->matches;
2705
2706 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2707 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2708 match->channels[n_channels++] =
2709 mvm->nd_channels[i]->center_freq;
2710 }
2711 /* We may have ended up with fewer channels than we allocated. */
2712 match->n_channels = n_channels;
2713 }
2714
2715 /**
2716 * enum iwl_d3_notif - d3 notifications
2717 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2718 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2719 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2720 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2721 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2722 */
2723 enum iwl_d3_notif {
2724 IWL_D3_NOTIF_WOWLAN_INFO = BIT(0),
2725 IWL_D3_NOTIF_WOWLAN_WAKE_PKT = BIT(1),
2726 IWL_D3_NOTIF_PROT_OFFLOAD = BIT(2),
2727 IWL_D3_ND_MATCH_INFO = BIT(3),
2728 IWL_D3_NOTIF_D3_END_NOTIF = BIT(4)
2729 };
2730
2731 /* manage d3 resume data */
2732 struct iwl_d3_data {
2733 struct iwl_wowlan_status_data *status;
2734 bool test;
2735 u32 d3_end_flags;
2736 u32 notif_expected; /* bitmap - see &enum iwl_d3_notif */
2737 u32 notif_received; /* bitmap - see &enum iwl_d3_notif */
2738 struct iwl_mvm_nd_results *nd_results;
2739 bool nd_results_valid;
2740 };
2741
iwl_mvm_query_netdetect_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2742 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2743 struct ieee80211_vif *vif,
2744 struct iwl_d3_data *d3_data)
2745 {
2746 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2747 struct cfg80211_wowlan_wakeup wakeup = {
2748 .pattern_idx = -1,
2749 };
2750 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2751 unsigned long matched_profiles;
2752 u32 reasons = 0;
2753 int i, n_matches, ret;
2754
2755 if (WARN_ON(!d3_data || !d3_data->status))
2756 goto out;
2757
2758 reasons = d3_data->status->wakeup_reasons;
2759
2760 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2761 wakeup.rfkill_release = true;
2762
2763 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2764 goto out;
2765
2766 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2767 WOWLAN_INFO_NOTIFICATION, 0)) {
2768 IWL_INFO(mvm, "Query FW for ND results\n");
2769 ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2770
2771 } else {
2772 IWL_INFO(mvm, "Notification based ND results\n");
2773 ret = d3_data->nd_results_valid ? 0 : -1;
2774 }
2775
2776 if (ret || !d3_data->nd_results->matched_profiles) {
2777 wakeup_report = NULL;
2778 goto out;
2779 }
2780
2781 matched_profiles = d3_data->nd_results->matched_profiles;
2782 if (mvm->n_nd_match_sets) {
2783 n_matches = hweight_long(matched_profiles);
2784 } else {
2785 IWL_ERR(mvm, "no net detect match information available\n");
2786 n_matches = 0;
2787 }
2788
2789 net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2790 GFP_KERNEL);
2791 if (!net_detect || !n_matches)
2792 goto out_report_nd;
2793 net_detect->n_matches = n_matches;
2794 n_matches = 0;
2795
2796 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2797 struct cfg80211_wowlan_nd_match *match;
2798 int idx, n_channels = 0;
2799
2800 n_channels = iwl_mvm_query_num_match_chans(mvm,
2801 d3_data->nd_results,
2802 i);
2803
2804 match = kzalloc(struct_size(match, channels, n_channels),
2805 GFP_KERNEL);
2806 if (!match)
2807 goto out_report_nd;
2808 match->n_channels = n_channels;
2809
2810 net_detect->matches[n_matches++] = match;
2811
2812 /* We inverted the order of the SSIDs in the scan
2813 * request, so invert the index here.
2814 */
2815 idx = mvm->n_nd_match_sets - i - 1;
2816 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2817 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2818 match->ssid.ssid_len);
2819
2820 if (mvm->n_nd_channels < n_channels)
2821 continue;
2822
2823 iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2824 }
2825 /* We may have fewer matches than we allocated. */
2826 net_detect->n_matches = n_matches;
2827
2828 out_report_nd:
2829 wakeup.net_detect = net_detect;
2830 out:
2831 iwl_mvm_free_nd(mvm);
2832
2833 mutex_unlock(&mvm->mutex);
2834 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2835
2836 if (net_detect) {
2837 for (i = 0; i < net_detect->n_matches; i++)
2838 kfree(net_detect->matches[i]);
2839 kfree(net_detect);
2840 }
2841 }
2842
iwl_mvm_d3_disconnect_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2843 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2844 struct ieee80211_vif *vif)
2845 {
2846 /* skip the one we keep connection on */
2847 if (data == vif)
2848 return;
2849
2850 if (vif->type == NL80211_IFTYPE_STATION)
2851 ieee80211_resume_disconnect(vif);
2852 }
2853
2854 enum rt_status {
2855 FW_ALIVE,
2856 FW_NEEDS_RESET,
2857 FW_ERROR,
2858 };
2859
iwl_mvm_check_rt_status(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2860 static enum rt_status iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
2861 struct ieee80211_vif *vif)
2862 {
2863 u32 err_id;
2864
2865 /* check for lmac1 error */
2866 if (iwl_fwrt_read_err_table(mvm->trans,
2867 mvm->trans->dbg.lmac_error_event_table[0],
2868 &err_id)) {
2869 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
2870 IWL_WARN(mvm, "Rfkill was toggled during suspend\n");
2871 if (vif) {
2872 struct cfg80211_wowlan_wakeup wakeup = {
2873 .rfkill_release = true,
2874 };
2875
2876 ieee80211_report_wowlan_wakeup(vif, &wakeup,
2877 GFP_KERNEL);
2878 }
2879
2880 return FW_NEEDS_RESET;
2881 }
2882 return FW_ERROR;
2883 }
2884
2885 /* check if we have lmac2 set and check for error */
2886 if (iwl_fwrt_read_err_table(mvm->trans,
2887 mvm->trans->dbg.lmac_error_event_table[1],
2888 NULL))
2889 return FW_ERROR;
2890
2891 /* check for umac error */
2892 if (iwl_fwrt_read_err_table(mvm->trans,
2893 mvm->trans->dbg.umac_error_event_table,
2894 NULL))
2895 return FW_ERROR;
2896
2897 return FW_ALIVE;
2898 }
2899
2900 /*
2901 * This function assumes:
2902 * 1. The mutex is already held.
2903 * 2. The callee functions unlock the mutex.
2904 */
2905 static bool
iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2906 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
2907 struct ieee80211_vif *vif,
2908 struct iwl_d3_data *d3_data)
2909 {
2910 lockdep_assert_held(&mvm->mutex);
2911
2912 /* if FW uses status notification, status shouldn't be NULL here */
2913 if (!d3_data->status) {
2914 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2915 u8 sta_id = mvm->net_detect ? IWL_INVALID_STA :
2916 mvmvif->deflink.ap_sta_id;
2917
2918 /* bug - FW with MLO has status notification */
2919 WARN_ON(ieee80211_vif_is_mld(vif));
2920
2921 d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
2922 }
2923
2924 if (mvm->net_detect) {
2925 iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
2926 } else {
2927 bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
2928 d3_data->status);
2929
2930 #ifdef CONFIG_IWLWIFI_DEBUGFS
2931 if (keep)
2932 mvm->keep_vif = vif;
2933 #endif
2934
2935 return keep;
2936 }
2937 return false;
2938 }
2939
2940 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
2941 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
2942 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
2943 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
2944 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
2945 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
2946
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)2947 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
2948 struct iwl_wowlan_wake_pkt_notif *notif,
2949 struct iwl_wowlan_status_data *status,
2950 u32 len)
2951 {
2952 u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
2953
2954 if (len < sizeof(*notif)) {
2955 IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
2956 return -EIO;
2957 }
2958
2959 if (WARN_ON(!status)) {
2960 IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
2961 return -EIO;
2962 }
2963
2964 if (WARN_ON(!(status->wakeup_reasons &
2965 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
2966 IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
2967 status->wakeup_reasons);
2968 return -EIO;
2969 }
2970
2971 data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
2972
2973 /* data_size got the padding from the notification, remove it. */
2974 if (packet_len < data_size)
2975 data_size = packet_len;
2976
2977 status->wake_packet = kmemdup(notif->wake_packet, data_size,
2978 GFP_ATOMIC);
2979
2980 if (!status->wake_packet)
2981 return -ENOMEM;
2982
2983 status->wake_packet_length = packet_len;
2984 status->wake_packet_bufsize = data_size;
2985
2986 return 0;
2987 }
2988
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)2989 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
2990 struct iwl_d3_data *d3_data,
2991 struct iwl_scan_offload_match_info *notif,
2992 u32 len)
2993 {
2994 struct iwl_wowlan_status_data *status = d3_data->status;
2995 struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
2996 struct iwl_mvm_nd_results *results = d3_data->nd_results;
2997 size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2998 iwl_umac_scan_get_max_profiles(mvm->fw);
2999
3000 if (IS_ERR_OR_NULL(vif))
3001 return;
3002
3003 if (len < sizeof(struct iwl_scan_offload_match_info)) {
3004 IWL_ERR(mvm, "Invalid scan match info notification\n");
3005 return;
3006 }
3007
3008 if (!mvm->net_detect) {
3009 IWL_ERR(mvm, "Unexpected scan match info notification\n");
3010 return;
3011 }
3012
3013 if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
3014 IWL_ERR(mvm,
3015 "Ignore scan match info notification: no reason\n");
3016 return;
3017 }
3018
3019 #ifdef CONFIG_IWLWIFI_DEBUGFS
3020 mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
3021 #endif
3022
3023 results->matched_profiles = le32_to_cpu(notif->matched_profiles);
3024 IWL_INFO(mvm, "number of matched profiles=%u\n",
3025 results->matched_profiles);
3026
3027 if (results->matched_profiles) {
3028 memcpy(results->matches, notif->matches, matches_len);
3029 d3_data->nd_results_valid = true;
3030 }
3031
3032 /* no scan should be active at this point */
3033 mvm->scan_status = 0;
3034 for (i = 0; i < mvm->max_scans; i++)
3035 mvm->scan_uid_status[i] = 0;
3036 }
3037
iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)3038 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
3039 struct iwl_rx_packet *pkt, void *data)
3040 {
3041 struct iwl_mvm *mvm =
3042 container_of(notif_wait, struct iwl_mvm, notif_wait);
3043 struct iwl_d3_data *d3_data = data;
3044 u32 len = iwl_rx_packet_payload_len(pkt);
3045 int ret;
3046 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
3047 PROT_OFFLOAD_GROUP,
3048 WOWLAN_INFO_NOTIFICATION,
3049 IWL_FW_CMD_VER_UNKNOWN);
3050
3051
3052 switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
3053 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
3054
3055 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
3056 /* We might get two notifications due to dual bss */
3057 IWL_DEBUG_WOWLAN(mvm,
3058 "Got additional wowlan info notification\n");
3059 break;
3060 }
3061
3062 if (wowlan_info_ver == 1) {
3063 struct iwl_wowlan_info_notif_v1 *notif_v1 =
3064 (void *)pkt->data;
3065
3066 iwl_mvm_parse_wowlan_info_notif_v1(mvm, notif_v1,
3067 d3_data->status,
3068 len);
3069 } else if (wowlan_info_ver == 3) {
3070 struct iwl_wowlan_info_notif_v3 *notif =
3071 (void *)pkt->data;
3072
3073 iwl_mvm_parse_wowlan_info_notif_v3(mvm, notif,
3074 d3_data->status, len);
3075 } else if (wowlan_info_ver == 5) {
3076 struct iwl_wowlan_info_notif *notif =
3077 (void *)pkt->data;
3078
3079 iwl_mvm_parse_wowlan_info_notif(mvm, notif,
3080 d3_data->status, len);
3081 } else {
3082 IWL_FW_CHECK(mvm, 1,
3083 "Firmware advertises unknown WoWLAN info notification %d!\n",
3084 wowlan_info_ver);
3085 return false;
3086 }
3087
3088 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
3089
3090 if (d3_data->status &&
3091 d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
3092 /* We are supposed to get also wake packet notif */
3093 d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3094
3095 break;
3096 }
3097 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
3098 struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
3099
3100 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
3101 /* We shouldn't get two wake packet notifications */
3102 IWL_ERR(mvm,
3103 "Got additional wowlan wake packet notification\n");
3104 } else {
3105 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3106 len = iwl_rx_packet_payload_len(pkt);
3107 ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
3108 d3_data->status,
3109 len);
3110 if (ret)
3111 IWL_ERR(mvm,
3112 "Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
3113 }
3114
3115 break;
3116 }
3117 case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
3118 struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
3119
3120 if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
3121 IWL_ERR(mvm,
3122 "Got additional netdetect match info\n");
3123 break;
3124 }
3125
3126 d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
3127
3128 /* explicitly set this in the 'expected' as well */
3129 d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
3130
3131 len = iwl_rx_packet_payload_len(pkt);
3132 iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
3133 break;
3134 }
3135 case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
3136 struct iwl_d3_end_notif *notif = (void *)pkt->data;
3137
3138 d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
3139 d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
3140
3141 break;
3142 }
3143 default:
3144 WARN_ON(1);
3145 }
3146
3147 return d3_data->notif_received == d3_data->notif_expected;
3148 }
3149
iwl_mvm_resume_firmware(struct iwl_mvm * mvm,bool test)3150 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3151 {
3152 int ret;
3153 enum iwl_d3_status d3_status;
3154 struct iwl_host_cmd cmd = {
3155 .id = D0I3_END_CMD,
3156 .flags = CMD_WANT_SKB,
3157 };
3158 bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3159 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3160
3161 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3162 if (ret)
3163 return ret;
3164
3165 if (d3_status != IWL_D3_STATUS_ALIVE) {
3166 IWL_INFO(mvm, "Device was reset during suspend\n");
3167 return -ENOENT;
3168 }
3169
3170 /*
3171 * We should trigger resume flow using command only for 22000 family
3172 * AX210 and above don't need the command since they have
3173 * the doorbell interrupt.
3174 */
3175 if (mvm->trans->mac_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3176 fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3177 ret = iwl_mvm_send_cmd(mvm, &cmd);
3178 if (ret < 0)
3179 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3180 ret);
3181 }
3182
3183 return ret;
3184 }
3185
3186 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 3)
3187
iwl_mvm_d3_notif_wait(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data)3188 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3189 struct iwl_d3_data *d3_data)
3190 {
3191 static const u16 d3_resume_notif[] = {
3192 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3193 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3194 WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3195 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3196 };
3197 static const u16 d3_fast_resume_notif[] = {
3198 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3199 };
3200 struct iwl_notification_wait wait_d3_notif;
3201 int ret;
3202
3203 if (mvm->fast_resume)
3204 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3205 d3_fast_resume_notif,
3206 ARRAY_SIZE(d3_fast_resume_notif),
3207 iwl_mvm_wait_d3_notif, d3_data);
3208 else
3209 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3210 d3_resume_notif,
3211 ARRAY_SIZE(d3_resume_notif),
3212 iwl_mvm_wait_d3_notif, d3_data);
3213
3214 ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3215 if (ret) {
3216 iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3217 return ret;
3218 }
3219
3220 return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3221 IWL_MVM_D3_NOTIF_TIMEOUT);
3222 }
3223
iwl_mvm_d3_resume_notif_based(struct iwl_mvm * mvm)3224 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3225 {
3226 return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3227 WOWLAN_INFO_NOTIFICATION, 0) &&
3228 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3229 WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3230 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3231 D3_END_NOTIFICATION, 0);
3232 }
3233
__iwl_mvm_resume(struct iwl_mvm * mvm,bool test)3234 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3235 {
3236 struct ieee80211_vif *vif = NULL;
3237 int ret = 1;
3238 struct iwl_mvm_nd_results results = {};
3239 struct iwl_d3_data d3_data = {
3240 .test = test,
3241 .notif_expected =
3242 IWL_D3_NOTIF_WOWLAN_INFO |
3243 IWL_D3_NOTIF_D3_END_NOTIF,
3244 .nd_results_valid = false,
3245 .nd_results = &results,
3246 };
3247 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3248 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3249 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3250 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3251 bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3252 enum rt_status rt_status;
3253 bool keep = false;
3254
3255 mutex_lock(&mvm->mutex);
3256
3257 /* Apparently, the device went away and device_powered_off() was called,
3258 * don't even try to read the rt_status, the device is currently
3259 * inaccessible.
3260 */
3261 if (!test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status)) {
3262 IWL_INFO(mvm,
3263 "Can't resume, device_powered_off() was called during wowlan\n");
3264 goto err;
3265 }
3266
3267 mvm->last_reset_or_resume_time_jiffies = jiffies;
3268
3269 /* get the BSS vif pointer again */
3270 vif = iwl_mvm_get_bss_vif(mvm);
3271 if (IS_ERR_OR_NULL(vif))
3272 goto err;
3273
3274 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3275
3276 rt_status = iwl_mvm_check_rt_status(mvm, vif);
3277 if (rt_status != FW_ALIVE) {
3278 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3279 if (rt_status == FW_ERROR) {
3280 IWL_ERR(mvm, "FW Error occurred during suspend. Restarting.\n");
3281 iwl_mvm_dump_nic_error_log(mvm);
3282 iwl_dbg_tlv_time_point(&mvm->fwrt,
3283 IWL_FW_INI_TIME_POINT_FW_ASSERT,
3284 NULL);
3285 iwl_fw_dbg_collect_desc(&mvm->fwrt,
3286 &iwl_dump_desc_assert,
3287 false, 0);
3288 }
3289 ret = 1;
3290 goto err;
3291 }
3292
3293 if (resume_notif_based) {
3294 d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3295 if (!d3_data.status) {
3296 IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3297 ret = -ENOMEM;
3298 goto err;
3299 }
3300
3301 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3302 if (ret)
3303 goto err;
3304 } else {
3305 ret = iwl_mvm_resume_firmware(mvm, test);
3306 if (ret < 0)
3307 goto err;
3308 }
3309
3310 iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
3311
3312 /* when reset is required we can't send these following commands */
3313 if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3314 goto query_wakeup_reasons;
3315
3316 /*
3317 * Query the current location and source from the D3 firmware so we
3318 * can play it back when we re-intiailize the D0 firmware
3319 */
3320 iwl_mvm_update_changed_regdom(mvm);
3321
3322 /* Re-configure PPAG settings */
3323 iwl_mvm_ppag_send_cmd(mvm);
3324
3325 if (!unified_image)
3326 /* Re-configure default SAR profile */
3327 iwl_mvm_sar_select_profile(mvm, 1, 1);
3328
3329 if (mvm->net_detect && unified_image) {
3330 /* If this is a non-unified image, we restart the FW,
3331 * so no need to stop the netdetect scan. If that
3332 * fails, continue and try to get the wake-up reasons,
3333 * but trigger a HW restart by keeping a failure code
3334 * in ret.
3335 */
3336 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3337 false);
3338 }
3339
3340 query_wakeup_reasons:
3341 keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3342 /* has unlocked the mutex, so skip that */
3343 goto out;
3344
3345 err:
3346 mutex_unlock(&mvm->mutex);
3347 out:
3348 if (d3_data.status)
3349 kfree(d3_data.status->wake_packet);
3350 kfree(d3_data.status);
3351 iwl_mvm_free_nd(mvm);
3352
3353 if (!d3_data.test && !mvm->net_detect)
3354 ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3355 IEEE80211_IFACE_ITER_NORMAL,
3356 iwl_mvm_d3_disconnect_iter,
3357 keep ? vif : NULL);
3358
3359 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3360
3361 /* no need to reset the device in unified images, if successful */
3362 if (unified_image && !ret) {
3363 /* nothing else to do if we already sent D0I3_END_CMD */
3364 if (d0i3_first)
3365 return 0;
3366
3367 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3368 D3_END_NOTIFICATION, 0)) {
3369 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3370 if (!ret)
3371 return 0;
3372 } else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3373 return 0;
3374 }
3375 }
3376
3377 /*
3378 * Reconfigure the device in one of the following cases:
3379 * 1. We are not using a unified image
3380 * 2. We are using a unified image but had an error while exiting D3
3381 */
3382 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3383
3384 return 1;
3385 }
3386
iwl_mvm_resume(struct ieee80211_hw * hw)3387 int iwl_mvm_resume(struct ieee80211_hw *hw)
3388 {
3389 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3390 int ret;
3391
3392 ret = __iwl_mvm_resume(mvm, false);
3393
3394 iwl_mvm_resume_tcm(mvm);
3395
3396 iwl_fw_runtime_resume(&mvm->fwrt);
3397
3398 return ret;
3399 }
3400
iwl_mvm_set_wakeup(struct ieee80211_hw * hw,bool enabled)3401 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3402 {
3403 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3404
3405 device_set_wakeup_enable(mvm->trans->dev, enabled);
3406 }
3407
iwl_mvm_fast_suspend(struct iwl_mvm * mvm)3408 void iwl_mvm_fast_suspend(struct iwl_mvm *mvm)
3409 {
3410 struct iwl_d3_manager_config d3_cfg_cmd_data = {};
3411 int ret;
3412
3413 lockdep_assert_held(&mvm->mutex);
3414
3415 IWL_DEBUG_WOWLAN(mvm, "Starting fast suspend flow\n");
3416
3417 mvm->fast_resume = true;
3418 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3419
3420 WARN_ON(iwl_mvm_power_update_device(mvm));
3421 ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, 0,
3422 sizeof(d3_cfg_cmd_data), &d3_cfg_cmd_data);
3423 if (ret)
3424 IWL_ERR(mvm,
3425 "fast suspend: couldn't send D3_CONFIG_CMD %d\n", ret);
3426
3427 ret = iwl_trans_d3_suspend(mvm->trans, false, false);
3428 if (ret)
3429 IWL_ERR(mvm, "fast suspend: trans_d3_suspend failed %d\n", ret);
3430 }
3431
iwl_mvm_fast_resume(struct iwl_mvm * mvm)3432 int iwl_mvm_fast_resume(struct iwl_mvm *mvm)
3433 {
3434 struct iwl_d3_data d3_data = {
3435 .notif_expected =
3436 IWL_D3_NOTIF_D3_END_NOTIF,
3437 };
3438 enum rt_status rt_status;
3439 int ret;
3440
3441 lockdep_assert_held(&mvm->mutex);
3442
3443 IWL_DEBUG_WOWLAN(mvm, "Starting the fast resume flow\n");
3444
3445 mvm->last_reset_or_resume_time_jiffies = jiffies;
3446 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3447
3448 rt_status = iwl_mvm_check_rt_status(mvm, NULL);
3449 if (rt_status != FW_ALIVE) {
3450 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3451 if (rt_status == FW_ERROR) {
3452 IWL_ERR(mvm,
3453 "iwl_mvm_check_rt_status failed, device is gone during suspend\n");
3454 iwl_mvm_dump_nic_error_log(mvm);
3455 iwl_dbg_tlv_time_point(&mvm->fwrt,
3456 IWL_FW_INI_TIME_POINT_FW_ASSERT,
3457 NULL);
3458 iwl_fw_dbg_collect_desc(&mvm->fwrt,
3459 &iwl_dump_desc_assert,
3460 false, 0);
3461 }
3462 mvm->trans->state = IWL_TRANS_NO_FW;
3463 ret = -ENODEV;
3464
3465 goto out;
3466 }
3467 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3468
3469 if (ret) {
3470 IWL_ERR(mvm, "Couldn't get the d3 notif %d\n", ret);
3471 mvm->trans->state = IWL_TRANS_NO_FW;
3472 }
3473
3474 out:
3475 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3476 mvm->fast_resume = false;
3477
3478 return ret;
3479 }
3480
3481 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_d3_test_open(struct inode * inode,struct file * file)3482 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3483 {
3484 struct iwl_mvm *mvm = inode->i_private;
3485 int err;
3486
3487 if (mvm->d3_test_active)
3488 return -EBUSY;
3489
3490 file->private_data = inode->i_private;
3491
3492 iwl_mvm_pause_tcm(mvm, true);
3493
3494 iwl_fw_runtime_suspend(&mvm->fwrt);
3495
3496 /* start pseudo D3 */
3497 rtnl_lock();
3498 wiphy_lock(mvm->hw->wiphy);
3499 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3500 wiphy_unlock(mvm->hw->wiphy);
3501 rtnl_unlock();
3502 if (err > 0)
3503 err = -EINVAL;
3504 if (err)
3505 return err;
3506
3507 mvm->d3_test_active = true;
3508 mvm->keep_vif = NULL;
3509 return 0;
3510 }
3511
iwl_mvm_d3_test_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3512 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3513 size_t count, loff_t *ppos)
3514 {
3515 struct iwl_mvm *mvm = file->private_data;
3516 unsigned long end = jiffies + 60 * HZ;
3517 u32 pme_asserted;
3518
3519 while (true) {
3520 /* read pme_ptr if available */
3521 if (mvm->d3_test_pme_ptr) {
3522 pme_asserted = iwl_trans_read_mem32(mvm->trans,
3523 mvm->d3_test_pme_ptr);
3524 if (pme_asserted)
3525 break;
3526 }
3527
3528 #if defined(__linux__)
3529 if (msleep_interruptible(100))
3530 #elif defined(__FreeBSD__)
3531 if (linux_msleep_interruptible(100))
3532 #endif
3533 break;
3534
3535 if (time_is_before_jiffies(end)) {
3536 IWL_ERR(mvm,
3537 "ending pseudo-D3 with timeout after ~60 seconds\n");
3538 return -ETIMEDOUT;
3539 }
3540 }
3541
3542 return 0;
3543 }
3544
iwl_mvm_d3_test_disconn_work_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)3545 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3546 struct ieee80211_vif *vif)
3547 {
3548 /* skip the one we keep connection on */
3549 if (_data == vif)
3550 return;
3551
3552 if (vif->type == NL80211_IFTYPE_STATION)
3553 ieee80211_connection_loss(vif);
3554 }
3555
iwl_mvm_d3_test_release(struct inode * inode,struct file * file)3556 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3557 {
3558 struct iwl_mvm *mvm = inode->i_private;
3559 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3560 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3561
3562 mvm->d3_test_active = false;
3563
3564 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3565
3566 rtnl_lock();
3567 wiphy_lock(mvm->hw->wiphy);
3568 __iwl_mvm_resume(mvm, true);
3569 wiphy_unlock(mvm->hw->wiphy);
3570 rtnl_unlock();
3571
3572 iwl_mvm_resume_tcm(mvm);
3573
3574 iwl_fw_runtime_resume(&mvm->fwrt);
3575
3576 iwl_abort_notification_waits(&mvm->notif_wait);
3577 if (!unified_image) {
3578 int remaining_time = 10;
3579
3580 ieee80211_restart_hw(mvm->hw);
3581
3582 /* wait for restart and disconnect all interfaces */
3583 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3584 remaining_time > 0) {
3585 remaining_time--;
3586 #if defined(__linux__)
3587 msleep(1000);
3588 #elif defined(__FreeBSD__)
3589 linux_msleep(1000);
3590 #endif
3591 }
3592
3593 if (remaining_time == 0)
3594 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3595 }
3596
3597 ieee80211_iterate_active_interfaces_atomic(
3598 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3599 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3600
3601 return 0;
3602 }
3603
3604 const struct file_operations iwl_dbgfs_d3_test_ops = {
3605 .open = iwl_mvm_d3_test_open,
3606 .read = iwl_mvm_d3_test_read,
3607 .release = iwl_mvm_d3_test_release,
3608 };
3609 #endif
3610